From 8824cb941847b4e1ff9956742a72daefe590a749 Mon Sep 17 00:00:00 2001 From: Ross Schulman Date: Wed, 23 Oct 2024 08:26:21 -0400 Subject: [PATCH] Address a few bugs in IsoMdlPresentation and modify MDoc (#44) * Remove unused argument from MDoc init and make IsoMdlPresentation init public * Modify query to retrieve signing key from KeyStore * Rename MDoc init argument * Have consumer pass signing key into submitNamespaces --- Sources/MobileSdk/IsoMdlPresentation.swift | 30 +++------------------- Sources/MobileSdk/MDoc.swift | 5 ++-- 2 files changed, 5 insertions(+), 30 deletions(-) diff --git a/Sources/MobileSdk/IsoMdlPresentation.swift b/Sources/MobileSdk/IsoMdlPresentation.swift index b039e65..17a3cb3 100644 --- a/Sources/MobileSdk/IsoMdlPresentation.swift +++ b/Sources/MobileSdk/IsoMdlPresentation.swift @@ -19,7 +19,7 @@ public class IsoMdlPresentation { var bleManager: MDocHolderBLECentral! var useL2CAP: Bool - init?( + public init?( mdoc: MDoc, engagement: DeviceEngagement, callback: BLESessionStateDelegate, useL2CAP: Bool ) { @@ -49,38 +49,14 @@ public class IsoMdlPresentation { bleManager.disconnectFromDevice(session: self.session) } - public func submitNamespaces(items: [String: [String: [String]]]) { + public func submitNamespaces(items: [String: [String: [String]]], signingKey: SecKey) { do { let payload = try session.generateResponse(permittedItems: items) - let query = - [ - kSecClass: kSecClassKey, - kSecAttrApplicationLabel: self.mdoc.keyAlias, - kSecReturnRef: true - ] as [String: Any] - // Find and cast the result as a SecKey instance. - var item: CFTypeRef? - var secKey: SecKey - switch SecItemCopyMatching(query as CFDictionary, &item) { - case errSecSuccess: - // swiftlint:disable force_cast - secKey = item as! SecKey - // swiftlint:enable force_cast - case errSecItemNotFound: - self.callback.update(state: .error(.generic("Key not found"))) - self.cancel() - return - case let status: - self.callback.update( - state: .error(.generic("Keychain read failed: \(status)"))) - self.cancel() - return - } var error: Unmanaged? guard let derSignature = SecKeyCreateSignature( - secKey, + signingKey, .ecdsaSignatureMessageX962SHA256, payload as CFData, &error) as Data? diff --git a/Sources/MobileSdk/MDoc.swift b/Sources/MobileSdk/MDoc.swift index b11b4ab..4b682e5 100644 --- a/Sources/MobileSdk/MDoc.swift +++ b/Sources/MobileSdk/MDoc.swift @@ -15,13 +15,12 @@ public class MDoc: Credential { /// IssuerSignedItemBytes will be bytes, but its composition is defined here /// https://github.com/spruceid/isomdl/blob/f7b05dfa/src/definitions/issuer_signed.rs#L18 public init?( - fromMDoc issuerAuth: Data, namespaces: [MDocNamespace: [IssuerSignedItemBytes]], - keyAlias: String + fromMDoc mdocBytes: Data, keyAlias: String ) { self.keyAlias = keyAlias do { try self.inner = SpruceIDMobileSdkRs.Mdoc.fromCborEncodedDocument( - cborEncodedDocument: issuerAuth, keyAlias: keyAlias) + cborEncodedDocument: mdocBytes, keyAlias: keyAlias) } catch { print("\(error)") return nil