Skip to content

Commit

Permalink
Merge pull request #19 from eu-digital-identity-wallet/develop
Browse files Browse the repository at this point in the history
Use kSecAttrDescription to save Document displayName property
  • Loading branch information
phisakel authored Jul 31, 2024
2 parents b9ac265 + bd4af0b commit 2fbdcb7
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 13 deletions.
4 changes: 3 additions & 1 deletion Sources/WalletStorage/Document.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -28,6 +28,7 @@ public struct Document {
self.privateKey = privateKey
self.createdAt = createdAt ?? Date()
self.modifiedAt = modifiedAt
self.displayName = displayName
self.status = status
}

Expand All @@ -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 }

Expand Down
2 changes: 1 addition & 1 deletion Sources/WalletStorage/Enumerations.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ public enum PrivateKeyType: String {
case secureEnclaveP256 = "sep2"
}


/// document status
public enum DocumentStatus: String {
case issued
case deferred
case pending
}
2 changes: 1 addition & 1 deletion Sources/WalletStorage/IssueRequest.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

Expand Down
18 changes: 8 additions & 10 deletions Sources/WalletStorage/KeyChainStorageService.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand All @@ -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
}

Expand Down Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 2fbdcb7

Please sign in to comment.