From a14cf7bdf98f0e0cefe09a86662465499460a3e5 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Wed, 29 May 2024 00:39:43 +0300 Subject: [PATCH 1/2] Load and use document data as IssuerSigned array --- .../eudi-lib-ios-wallet-kit/EudiWallet.swift | 18 +++++++++++++----- .../Services/OpenId4VpService.swift | 4 ++-- .../Services/StorageManager.swift | 8 ++++---- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/Sources/eudi-lib-ios-wallet-kit/EudiWallet.swift b/Sources/eudi-lib-ios-wallet-kit/EudiWallet.swift index 4093792..d7cf538 100644 --- a/Sources/eudi-lib-ios-wallet-kit/EudiWallet.swift +++ b/Sources/eudi-lib-ios-wallet-kit/EudiWallet.swift @@ -22,6 +22,7 @@ import WalletStorage import LocalAuthentication import CryptoKit import OpenID4VCI +import SwiftCBOR /// User wallet implementation public final class EudiWallet: ObservableObject { @@ -96,14 +97,21 @@ public final class EudiWallet: ObservableObject { } func finalizeIssuing(id: String, data: Data, docType: String?, format: DataFormat, issueReq: IssueRequest, openId4VCIService: OpenId4VCIService) async throws -> WalletStorage.Document { + let iss = IssuerSigned(data: [UInt8](data)) + let deviceResponse = iss != nil ? nil : DeviceResponse(data: [UInt8](data)) guard let ddt = DocDataType(rawValue: format.rawValue) else { throw WalletError(description: "Invalid format \(format.rawValue)") } - let docTypeToSave = docType ?? (format == .cbor ? DeviceResponse(data: [UInt8](data))?.documents?.first?.docType : nil) + let docTypeToSave = docType ?? (format == .cbor ? iss?.issuerAuth.mso.docType ?? deviceResponse?.documents?.first?.docType : nil) + var dataToSave: Data? = data + if let deviceResponse { + if let iss = deviceResponse.documents?.first?.issuerSigned { dataToSave = Data(iss.encode(options: CBOROptions())) } else { dataToSave = nil } + } guard let docTypeToSave else { throw WalletError(description: "Unknown document type") } + guard let dataToSave else { throw WalletError(description: "Issued data cannot be recognized") } var issued: WalletStorage.Document if !openId4VCIService.usedSecureEnclave { - issued = WalletStorage.Document(id: id, docType: docTypeToSave, docDataType: ddt, data: data, privateKeyType: .x963EncodedP256, privateKey: issueReq.keyData, createdAt: Date()) + issued = WalletStorage.Document(id: id, docType: docTypeToSave, docDataType: ddt, data: dataToSave, privateKeyType: .x963EncodedP256, privateKey: issueReq.keyData, createdAt: Date()) } else { - issued = WalletStorage.Document(id: id, docType: docTypeToSave, docDataType: ddt, data: data, privateKeyType: .secureEnclaveP256, privateKey: issueReq.keyData, createdAt: Date()) + issued = WalletStorage.Document(id: id, docType: docTypeToSave, docDataType: ddt, data: dataToSave, privateKeyType: .secureEnclaveP256, privateKey: issueReq.keyData, createdAt: Date()) } try issueReq.saveToStorage(storage.storageService) try endIssueDocument(issued) @@ -186,7 +194,7 @@ public final class EudiWallet: ObservableObject { try? storageService.deleteDocuments() let docSamples = (sampleDataFiles ?? ["EUDI_sample_data"]).compactMap { Data(name:$0) } .compactMap(SignUpResponse.decomposeCBORSignupResponse(data:)).flatMap {$0} - .map { Document(docType: $0.docType, docDataType: .cbor, data: $0.drData, privateKeyType: .x963EncodedP256, privateKey: $0.pkData, createdAt: Date.distantPast, modifiedAt: nil) } + .map { Document(docType: $0.docType, docDataType: .cbor, data: $0.issData, privateKeyType: .x963EncodedP256, privateKey: $0.pkData, createdAt: Date.distantPast, modifiedAt: nil) } do { for docSample in docSamples { try storageService.saveDocument(docSample, allowOverwrite: true) @@ -212,7 +220,7 @@ public final class EudiWallet: ObservableObject { if let docType { guard docs.count > 0 else { throw WalletError(description: "No documents of type \(docType) found") } } let cborsWithKeys = docs.compactMap { $0.getCborData() } guard cborsWithKeys.count > 0 else { throw WalletError(description: "Documents decode error") } - parameters = [InitializeKeys.document_signup_response_obj.rawValue: cborsWithKeys.map(\.dr), InitializeKeys.device_private_key_obj.rawValue: cborsWithKeys.map(\.dpk)] + parameters = [InitializeKeys.document_signup_issuer_signed_obj.rawValue: cborsWithKeys.map(\.iss), InitializeKeys.device_private_key_obj.rawValue: cborsWithKeys.map(\.dpk)] if let trustedReaderCertificates { parameters[InitializeKeys.trusted_certificates.rawValue] = trustedReaderCertificates } parameters[InitializeKeys.device_auth_method.rawValue] = deviceAuthMethod.rawValue default: diff --git a/Sources/eudi-lib-ios-wallet-kit/Services/OpenId4VpService.swift b/Sources/eudi-lib-ios-wallet-kit/Services/OpenId4VpService.swift index ff8cb81..1461ff4 100644 --- a/Sources/eudi-lib-ios-wallet-kit/Services/OpenId4VpService.swift +++ b/Sources/eudi-lib-ios-wallet-kit/Services/OpenId4VpService.swift @@ -31,7 +31,7 @@ import X509 public class OpenId4VpService: PresentationService { public var status: TransferStatus = .initialized var openid4VPlink: String - var docs: [DeviceResponse]! + var docs: [IssuerSigned]! var iaca: [SecCertificate]! var dauthMethod: DeviceAuthMethod var devicePrivateKeys: [CoseKeyPrivate]! @@ -114,7 +114,7 @@ public class OpenId4VpService: PresentationService { return } logger.info("Openid4vp request items: \(itemsToSend)") - guard let (deviceResponse, _, _) = try MdocHelpers.getDeviceResponseToSend(deviceRequest: nil, deviceResponses: docs, selectedItems: itemsToSend, eReaderKey: eReaderPub, devicePrivateKeys: devicePrivateKeys, sessionTranscript: sessionTranscript, dauthMethod: .deviceSignature) else { throw PresentationSession.makeError(str: "DOCUMENT_ERROR") } + guard let (deviceResponse, _, _) = try MdocHelpers.getDeviceResponseToSend(deviceRequest: nil, issuerSigned: docs, selectedItems: itemsToSend, eReaderKey: eReaderPub, devicePrivateKeys: devicePrivateKeys, sessionTranscript: sessionTranscript, dauthMethod: .deviceSignature) else { throw PresentationSession.makeError(str: "DOCUMENT_ERROR") } // Obtain consent let vpTokenStr = Data(deviceResponse.toCBOR(options: CBOROptions()).encode()).base64URLEncodedString() try await SendVpToken(vpTokenStr, pd, resolved, onSuccess) diff --git a/Sources/eudi-lib-ios-wallet-kit/Services/StorageManager.swift b/Sources/eudi-lib-ios-wallet-kit/Services/StorageManager.swift index 570c78c..c2657b6 100644 --- a/Sources/eudi-lib-ios-wallet-kit/Services/StorageManager.swift +++ b/Sources/eudi-lib-ios-wallet-kit/Services/StorageManager.swift @@ -82,11 +82,11 @@ public class StorageManager: ObservableObject { } func toModel(doc: WalletStorage.Document) -> (any MdocDecodable)? { - guard let (dr,dpk) = doc.getCborData() else { return nil } + guard let (iss,dpk) = doc.getCborData() else { return nil } return switch doc.docType { - case EuPidModel.euPidDocType: EuPidModel(id: doc.id, createdAt: doc.createdAt, response: dr, devicePrivateKey: dpk) - case IsoMdlModel.isoDocType: IsoMdlModel(id: doc.id, createdAt: doc.createdAt, response: dr, devicePrivateKey: dpk) - default: GenericMdocModel(id: doc.id, createdAt: doc.createdAt, response: dr, devicePrivateKey: dpk, docType: doc.docType, title: doc.docType.translated()) + case EuPidModel.euPidDocType: EuPidModel(id: doc.id, createdAt: doc.createdAt, issuerSigned: iss, devicePrivateKey: dpk) + case IsoMdlModel.isoDocType: IsoMdlModel(id: doc.id, createdAt: doc.createdAt, issuerSigned: iss, devicePrivateKey: dpk) + default: GenericMdocModel(id: doc.id, createdAt: doc.createdAt, issuerSigned: iss, devicePrivateKey: dpk, docType: doc.docType, title: doc.docType.translated()) } } From 240aa070177b111eca4206becfad3c057df31aa5 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Wed, 29 May 2024 11:30:23 +0300 Subject: [PATCH 2/2] Update package references --- Package.resolved | 12 ++++++------ Package.swift | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/Package.resolved b/Package.resolved index 325fb74..b69347d 100644 --- a/Package.resolved +++ b/Package.resolved @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-iso18013-data-model.git", "state" : { - "revision" : "39134521d5c8df9302e60a1d98d3cb6405253d12", - "version" : "0.2.4" + "revision" : "bf62cc73ae2cea61e98020d2d037c153500207e7", + "version" : "0.2.5" } }, { @@ -32,8 +32,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-iso18013-data-transfer.git", "state" : { - "revision" : "8840a914bd12f060b94b749fbf5a1af62799522c", - "version" : "0.2.5" + "revision" : "bf4b3d68fe600d502a2860c9d409f88877d2ec1a", + "version" : "0.2.6" } }, { @@ -77,8 +77,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-wallet-storage.git", "state" : { - "revision" : "f66412818bc51b4f5b9fa6777914677da8ecf0c2", - "version" : "0.1.8" + "revision" : "10a4ab52f6224b49f098f0386a00268cb782010d", + "version" : "0.1.9" } }, { diff --git a/Package.swift b/Package.swift index 3d13e0a..6e5c003 100644 --- a/Package.swift +++ b/Package.swift @@ -15,8 +15,8 @@ let package = Package( dependencies: [ .package(url: "https://github.com/apple/swift-docc-plugin", from: "1.0.0"), .package(url: "https://github.com/apple/swift-log.git", from: "1.5.3"), - .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-iso18013-data-transfer.git", .upToNextMajor(from: "0.2.5")), - .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-wallet-storage.git", .upToNextMajor(from: "0.1.7")), + .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-iso18013-data-transfer.git", .upToNextMajor(from: "0.2.6")), + .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-wallet-storage.git", .upToNextMajor(from: "0.1.9")), .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-siop-openid4vp-swift.git", exact: "0.0.74"), .package(url: "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-openid4vci-swift.git", exact: "0.0.7"), ],