Skip to content

Commit

Permalink
Address a few bugs in IsoMdlPresentation and modify MDoc (#44)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
rschulman authored Oct 23, 2024
1 parent 98cb2bb commit 8824cb9
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 30 deletions.
30 changes: 3 additions & 27 deletions Sources/MobileSdk/IsoMdlPresentation.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public class IsoMdlPresentation {
var bleManager: MDocHolderBLECentral!
var useL2CAP: Bool

init?(
public init?(
mdoc: MDoc, engagement: DeviceEngagement,
callback: BLESessionStateDelegate, useL2CAP: Bool
) {
Expand Down Expand Up @@ -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<CFError>?
guard
let derSignature = SecKeyCreateSignature(
secKey,
signingKey,
.ecdsaSignatureMessageX962SHA256,
payload as CFData,
&error) as Data?
Expand Down
5 changes: 2 additions & 3 deletions Sources/MobileSdk/MDoc.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 8824cb9

Please sign in to comment.