From 1e5ec17bfe98f9620f3025a1680ce6a9ca78357a Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Fri, 26 Jan 2024 08:24:54 +0200 Subject: [PATCH 1/4] Refactor Issue request objct --- .../IssueRequest.swift | 52 +++++++++++++------ 1 file changed, 37 insertions(+), 15 deletions(-) diff --git a/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift b/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift index 7cd166f..722b472 100644 --- a/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift +++ b/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift @@ -37,13 +37,20 @@ public struct IssueRequest { self.keyData = keyData return } - if privateKeyType == .derEncodedP256 || privateKeyType == .pemStringDataP256 || privateKeyType == .x963EncodedP256 { - let p256 = P256.Signing.PrivateKey() - self.keyData = switch privateKeyType { case .derEncodedP256: p256.derRepresentation; case .pemStringDataP256: p256.pemRepresentation.data(using: .utf8)!; case .x963EncodedP256: p256.x963Representation; default: Data() } - } else if privateKeyType == .secureEnclaveP256 { - let secureEnclaveKey = try SecureEnclave.P256.Signing.PrivateKey() + switch privateKeyType { + case .derEncodedP256: + let p256 = P256.KeyAgreement.PrivateKey() + self.keyData = p256.derRepresentation + case .pemStringDataP256: + let p256 = P256.KeyAgreement.PrivateKey() + self.keyData = p256.pemRepresentation.data(using: .utf8)! + case .x963EncodedP256: + let p256 = P256.KeyAgreement.PrivateKey() + self.keyData = p256.x963Representation + case .secureEnclaveP256: + let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey() self.keyData = secureEnclaveKey.dataRepresentation - } + } } public func saveToStorage(_ storageService: any DataStorageService) throws { @@ -59,25 +66,40 @@ public struct IssueRequest { public func toCoseKeyPrivate() throws -> CoseKeyPrivate { guard let keyData else { fatalError("Key data not loaded") } - if privateKeyType == .derEncodedP256 || privateKeyType == .pemStringDataP256 || privateKeyType == .x963EncodedP256 { - let p256 = switch privateKeyType { case .derEncodedP256: try P256.Signing.PrivateKey(derRepresentation: keyData); case .x963EncodedP256: try P256.Signing.PrivateKey(x963Representation: keyData); case .pemStringDataP256: try P256.Signing.PrivateKey(pemRepresentation: String(data: keyData, encoding: .utf8)!); default: P256.Signing.PrivateKey() } + switch privateKeyType { + case .derEncodedP256: + let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData) + return CoseKeyPrivate(privateKeyx963Data: p256.x963Representation, crv: .p256) + case .x963EncodedP256: + let p256 = try P256.KeyAgreement.PrivateKey(x963Representation: keyData) + return CoseKeyPrivate(privateKeyx963Data: p256.x963Representation, crv: .p256) + case .pemStringDataP256: + let p256 = try P256.KeyAgreement.PrivateKey(pemRepresentation: String(data: keyData, encoding: .utf8)!) return CoseKeyPrivate(privateKeyx963Data: p256.x963Representation, crv: .p256) - } else { - let se256 = try SecureEnclave.P256.Signing.PrivateKey(dataRepresentation: keyData) - return CoseKeyPrivate(publicKeyx963Data: se256.publicKey.x963Representation, secureEnclaveData: keyData) + case .secureEnclaveP256: + let se256 = try SecureEnclave.P256.KeyAgreement.PrivateKey(dataRepresentation: keyData) + return CoseKeyPrivate(publicKeyx963Data: se256.publicKey.x963Representation, secureEnclaveKeyID: keyData) } } public func getPublicKeyPEM() throws -> String { guard let keyData else { fatalError("Key data not loaded") } - if privateKeyType == .derEncodedP256 || privateKeyType == .pemStringDataP256 || privateKeyType == .x963EncodedP256 { - let p256 = switch privateKeyType { case .derEncodedP256: try P256.Signing.PrivateKey(derRepresentation: keyData); case .x963EncodedP256: try P256.Signing.PrivateKey(x963Representation: keyData); case .pemStringDataP256: try P256.Signing.PrivateKey(pemRepresentation: String(data: keyData, encoding: .utf8)!); default: P256.Signing.PrivateKey() } + switch privateKeyType { + case .derEncodedP256: + let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData) return p256.publicKey.pemRepresentation - } else { - let se256 = try SecureEnclave.P256.Signing.PrivateKey(dataRepresentation: keyData) + case .pemStringDataP256: + let p256 = try P256.KeyAgreement.PrivateKey(pemRepresentation: String(data: keyData, encoding: .utf8)!) + return p256.publicKey.pemRepresentation + case .x963EncodedP256: + let p256 = try P256.KeyAgreement.PrivateKey(x963Representation: keyData) + return p256.publicKey.pemRepresentation + case .secureEnclaveP256: + let se256 = try SecureEnclave.P256.KeyAgreement.PrivateKey(dataRepresentation: keyData) return se256.publicKey.pemRepresentation } } + } From 811d81005d48dd9f1ff408e7957d4c3d89dff7b5 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Fri, 26 Jan 2024 23:54:33 +0200 Subject: [PATCH 2/4] remove old comments --- .../eudi-lib-ios-wallet-storage/KeyChainStorageService.swift | 3 --- 1 file changed, 3 deletions(-) diff --git a/Sources/eudi-lib-ios-wallet-storage/KeyChainStorageService.swift b/Sources/eudi-lib-ios-wallet-storage/KeyChainStorageService.swift index 305bfc1..8a04f0e 100644 --- a/Sources/eudi-lib-ios-wallet-storage/KeyChainStorageService.swift +++ b/Sources/eudi-lib-ios-wallet-storage/KeyChainStorageService.swift @@ -116,9 +116,6 @@ public class KeyChainStorageService: DataStorageService { } public func deleteDocumentData(id: String, for saveType: SavedKeyChainDataType) throws { - // kSecAttrAccount is used to store the secret Id so that we can look it up later - // kSecAttrService is always set to serviceName to enable us to lookup all our secrets later if needed - // kSecAttrType is used to store the secret type to allow us to cast it to the right Type on search let query: [String: Any] = [kSecClass: kSecClassGenericPassword, kSecAttrService: serviceToSave(for: saveType), kSecAttrAccount: id] as [String: Any] let status = SecItemDelete(query as CFDictionary) let statusMessage = SecCopyErrorMessageString(status, nil) as? String From 6c79c57f33192dcb7a8765763c21d2b8391a0495 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Sat, 27 Jan 2024 02:20:10 +0200 Subject: [PATCH 3/4] Refactor issue request --- .../IssueRequest.swift | 22 +++++++++---------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift b/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift index 722b472..880f43e 100644 --- a/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift +++ b/Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift @@ -20,16 +20,16 @@ import MdocDataModel18013 /// Issue request structure public struct IssueRequest { - public let id: String - public let docType: String? - public var keyData: Data? - public let privateKeyType: PrivateKeyType + public var id: String + public var docType: String? + public var keyData: Data + public var privateKeyType: PrivateKeyType /// Initialize issue request with id /// /// - Parameters: /// - id: a key identifier (uuid) - public init(id: String = UUID().uuidString, docType: String? = nil, privateKeyType: PrivateKeyType = .x963EncodedP256, keyData: Data? = nil) throws { + public init(id: String = UUID().uuidString, docType: String? = nil, privateKeyType: PrivateKeyType = .secureEnclaveP256, keyData: Data? = nil) throws { self.id = id self.docType = docType self.privateKeyType = privateKeyType @@ -48,7 +48,7 @@ public struct IssueRequest { let p256 = P256.KeyAgreement.PrivateKey() self.keyData = p256.x963Representation case .secureEnclaveP256: - let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey() + let secureEnclaveKey = try SecureEnclave.P256.KeyAgreement.PrivateKey() self.keyData = secureEnclaveKey.dataRepresentation } } @@ -59,13 +59,14 @@ public struct IssueRequest { try storageService.saveDocument(docKey, allowOverwrite: true) } - public mutating func loadFromStorage(_ storageService: any DataStorageService, id: String) throws { - guard let doc = try storageService.loadDocument(id: id) else { return } - keyData = doc.privateKey + public init?(_ storageService: any DataStorageService, id: String) throws { + guard let doc = try storageService.loadDocument(id: id), let pk = doc.privateKey, let pkt = doc.privateKeyType else { return nil } + self.id = id + keyData = pk + privateKeyType = pkt } public func toCoseKeyPrivate() throws -> CoseKeyPrivate { - guard let keyData else { fatalError("Key data not loaded") } switch privateKeyType { case .derEncodedP256: let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData) @@ -83,7 +84,6 @@ public struct IssueRequest { } public func getPublicKeyPEM() throws -> String { - guard let keyData else { fatalError("Key data not loaded") } switch privateKeyType { case .derEncodedP256: let p256 = try P256.KeyAgreement.PrivateKey(derRepresentation: keyData) From 4707702ce0976a7911fd464ddf65b80b6a1ab864 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Mon, 29 Jan 2024 21:02:17 +0200 Subject: [PATCH 4/4] Update Package.resolved --- Package.resolved | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Package.resolved b/Package.resolved index 59234b8..6dd37c6 100644 --- a/Package.resolved +++ b/Package.resolved @@ -5,8 +5,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/eu-digital-identity-wallet/eudi-lib-ios-iso18013-data-model.git", "state" : { - "revision" : "7f7f98e53de6f75796be405265cb060a4a1073d5", - "version" : "0.1.8" + "revision" : "f9811c94bdd6ea756a14f00aff9507eb932ae03d", + "version" : "0.2.0" } }, { @@ -23,8 +23,8 @@ "kind" : "remoteSourceControl", "location" : "https://github.com/apple/swift-log.git", "state" : { - "revision" : "532d8b529501fb73a2455b179e0bbb6d49b652ed", - "version" : "1.5.3" + "revision" : "e97a6fcb1ab07462881ac165fdbb37f067e205d5", + "version" : "1.5.4" } }, {