Skip to content

Commit

Permalink
Merge pull request #7 from eu-digital-identity-wallet/develop
Browse files Browse the repository at this point in the history
Refactor Issue request objct
  • Loading branch information
phisakel authored Jan 29, 2024
2 parents e7a5873 + 4707702 commit f664128
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 32 deletions.
8 changes: 4 additions & 4 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
},
{
Expand All @@ -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"
}
},
{
Expand Down
72 changes: 47 additions & 25 deletions Sources/eudi-lib-ios-wallet-storage/IssueRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -20,30 +20,37 @@ 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
if let keyData {
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 {
Expand All @@ -52,32 +59,47 @@ 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") }
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
}
}

}


Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit f664128

Please sign in to comment.