From 53ff789d74eacac54972c8190d6a754306b7c512 Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Wed, 31 Jul 2024 15:35:13 +0300 Subject: [PATCH 1/2] Revert "add pending status" This reverts commit 7ccf173cc3ac8ebd17c8623892962e4f80c53b6d. --- Sources/WalletStorage/Enumerations.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/WalletStorage/Enumerations.swift b/Sources/WalletStorage/Enumerations.swift index 9e54950..362a5cf 100644 --- a/Sources/WalletStorage/Enumerations.swift +++ b/Sources/WalletStorage/Enumerations.swift @@ -47,9 +47,9 @@ public enum PrivateKeyType: String { case secureEnclaveP256 = "sep2" } + /// document status public enum DocumentStatus: String { case issued case deferred - case pending } From bd4af0b98b979f08911a0fc7e9c5f6700167a2fb Mon Sep 17 00:00:00 2001 From: Filippos Sakellaropoulos Date: Wed, 31 Jul 2024 23:26:10 +0300 Subject: [PATCH 2/2] Use kSecAttrDescription to save Document displayName property --- Sources/WalletStorage/Document.swift | 4 +++- Sources/WalletStorage/IssueRequest.swift | 2 +- .../WalletStorage/KeyChainStorageService.swift | 18 ++++++++---------- 3 files changed, 12 insertions(+), 12 deletions(-) diff --git a/Sources/WalletStorage/Document.swift b/Sources/WalletStorage/Document.swift index 5c6a19f..380f951 100644 --- a/Sources/WalletStorage/Document.swift +++ b/Sources/WalletStorage/Document.swift @@ -19,7 +19,7 @@ import MdocDataModel18013 /// wallet document structure public struct Document { - public init(id: String = UUID().uuidString, docType: String, docDataType: DocDataType, data: Data, privateKeyType: PrivateKeyType?, privateKey: Data?, createdAt: Date?, modifiedAt: Date? = nil, status: DocumentStatus) { + public init(id: String = UUID().uuidString, docType: String, docDataType: DocDataType, data: Data, privateKeyType: PrivateKeyType?, privateKey: Data?, createdAt: Date?, modifiedAt: Date? = nil, displayName: String?, status: DocumentStatus) { self.id = id self.docType = docType self.docDataType = docDataType @@ -28,6 +28,7 @@ public struct Document { self.privateKey = privateKey self.createdAt = createdAt ?? Date() self.modifiedAt = modifiedAt + self.displayName = displayName self.status = status } @@ -39,6 +40,7 @@ public struct Document { public let privateKey: Data? public let createdAt: Date public let modifiedAt: Date? + public let displayName: String? public let status: DocumentStatus public var isDeferred: Bool { status == .deferred } diff --git a/Sources/WalletStorage/IssueRequest.swift b/Sources/WalletStorage/IssueRequest.swift index d9e144f..2b8803b 100644 --- a/Sources/WalletStorage/IssueRequest.swift +++ b/Sources/WalletStorage/IssueRequest.swift @@ -55,7 +55,7 @@ public struct IssueRequest { public func saveToStorage(_ storageService: any DataStorageService, status: DocumentStatus) throws { // save key data to storage with id - let docKey = Document(id: id, docType: docType ?? "P256", docDataType: .cbor, data: Data(), privateKeyType: privateKeyType, privateKey: keyData, createdAt: Date(), status: status) + let docKey = Document(id: id, docType: docType ?? "P256", docDataType: .cbor, data: Data(), privateKeyType: privateKeyType, privateKey: keyData, createdAt: Date(), displayName: nil, status: status) try storageService.saveDocument(docKey, allowOverwrite: true) } diff --git a/Sources/WalletStorage/KeyChainStorageService.swift b/Sources/WalletStorage/KeyChainStorageService.swift index 84e3e6f..88af8b2 100644 --- a/Sources/WalletStorage/KeyChainStorageService.swift +++ b/Sources/WalletStorage/KeyChainStorageService.swift @@ -52,9 +52,8 @@ public class KeyChainStorageService: DataStorageService { return documents } - func loadDocumentsData(id: String?, docStatus: DocumentStatus, dataToLoadType: SavedKeyChainDataType = .doc, bCompatOldVersion: Bool = false) throws -> [[String: Any]]? { - var query = makeQuery(id: id, bForSave: false, status: docStatus, dataType: dataToLoadType) - if bCompatOldVersion { query[kSecAttrService as String] = if dataToLoadType == .doc { serviceName } else { serviceName + "_key" } } // to be removed in version 1 + func loadDocumentsData(id: String?, docStatus: DocumentStatus, dataToLoadType: SavedKeyChainDataType = .doc) throws -> [[String: Any]]? { + let query = makeQuery(id: id, bForSave: false, status: docStatus, dataType: dataToLoadType) var result: CFTypeRef? let status = SecItemCopyMatching(query as CFDictionary, &result) if status == errSecItemNotFound { return nil } @@ -63,12 +62,9 @@ public class KeyChainStorageService: DataStorageService { throw StorageError(description: statusMessage ?? "", code: Int(status)) } var res = result as! [[String: Any]] - if !bCompatOldVersion, dataToLoadType == .doc { - if let dicts2 = try loadDocumentsData(id: id, docStatus: docStatus, dataToLoadType: .key, bCompatOldVersion: bCompatOldVersion) { res.append(contentsOf: dicts2) } + if dataToLoadType == .doc { + if let dicts2 = try loadDocumentsData(id: id, docStatus: docStatus, dataToLoadType: .key) { res.append(contentsOf: dicts2) } } - // following lines to be removed in version 1 - if !bCompatOldVersion, dataToLoadType == .doc { if let dicts1 = try loadDocumentsData(id: id, docStatus: docStatus, dataToLoadType: .doc, bCompatOldVersion: true) { res.append(contentsOf: dicts1) } } - if !bCompatOldVersion, dataToLoadType == .key { if let dicts2 = try loadDocumentsData(id: id, docStatus: docStatus, dataToLoadType: .key, bCompatOldVersion: true) {dicts2.forEach { d in var d2 = d; d2[kSecAttrIsNegative as String] = true; res.append(d2) } } } return res } @@ -117,10 +113,12 @@ public class KeyChainStorageService: DataStorageService { // use this attribute to differentiate between document and key data query[kSecAttrIsNegative as String] = Self.getIsNegativeValueToUse(dataToSaveType) query[kSecAttrLabel as String] = document.docType + if let dn = document.displayName { query[kSecAttrDescription as String] = dn } query[kSecAttrType as String] = dataType var status = SecItemAdd(query as CFDictionary, nil) if allowOverwrite && status == errSecDuplicateItem { - let updated: [String: Any] = [kSecValueData: query[kSecValueData as String] as! Data, kSecAttrIsNegative: Self.getIsNegativeValueToUse(dataToSaveType), kSecAttrLabel: document.docType, kSecAttrType: dataType] as [String: Any] + var updated: [String: Any] = [kSecValueData: query[kSecValueData as String] as! Data, kSecAttrIsNegative: Self.getIsNegativeValueToUse(dataToSaveType), kSecAttrLabel: document.docType, kSecAttrDescription: document.displayName ?? "", kSecAttrType: dataType] as [String: Any] + if let dn = document.displayName { updated[kSecAttrDescription as String] = dn } query = makeQuery(id: document.id, bForSave: true, status: document.status, dataType: dataToSaveType) status = SecItemUpdate(query as CFDictionary, updated as CFDictionary) } @@ -164,6 +162,6 @@ public class KeyChainStorageService: DataStorageService { keyType = PrivateKeyType(rawValue: dict2[kSecAttrType as String] as? String ?? PrivateKeyType.derEncodedP256.rawValue)! privateKeyData = (dict2[kSecValueData as String] as! Data) } - return Document(id: dict1[kSecAttrAccount as String] as! String, docType: dict1[kSecAttrLabel as String] as? String ?? "", docDataType: DocDataType(rawValue: dict1[kSecAttrType as String] as? String ?? DocDataType.cbor.rawValue) ?? DocDataType.cbor, data: data, privateKeyType: keyType, privateKey: privateKeyData, createdAt: (dict1[kSecAttrCreationDate as String] as! Date), modifiedAt: dict1[kSecAttrModificationDate as String] as? Date, status: status) + return Document(id: dict1[kSecAttrAccount as String] as! String, docType: dict1[kSecAttrLabel as String] as? String ?? "", docDataType: DocDataType(rawValue: dict1[kSecAttrType as String] as? String ?? DocDataType.cbor.rawValue) ?? DocDataType.cbor, data: data, privateKeyType: keyType, privateKey: privateKeyData, createdAt: (dict1[kSecAttrCreationDate as String] as! Date), modifiedAt: dict1[kSecAttrModificationDate as String] as? Date, displayName: dict1[kSecAttrDescription as String] as? String, status: status) } }