Remove redundant

This commit is contained in:
Max Goedjen 2023-03-11 17:51:55 -08:00
parent e2afb92fbf
commit 383fb28339
No known key found for this signature in database
1 changed files with 7 additions and 21 deletions

View File

@ -161,7 +161,7 @@ extension SmartCard.Store {
var untyped: CFTypeRef? var untyped: CFTypeRef?
SecItemCopyMatching(attributes, &untyped) SecItemCopyMatching(attributes, &untyped)
guard let typed = untyped as? [[CFString: Any]] else { return } guard let typed = untyped as? [[CFString: Any]] else { return }
let wrapped: [SmartCard.Secret] = typed.map { let wrapped = typed.map {
let name = $0[kSecAttrLabel] as? String ?? "Unnamed" let name = $0[kSecAttrLabel] as? String ?? "Unnamed"
let tokenID = $0[kSecAttrApplicationLabel] as! Data let tokenID = $0[kSecAttrApplicationLabel] as! Data
let algorithm = Algorithm(secAttr: $0[kSecAttrKeyType] as! NSNumber) let algorithm = Algorithm(secAttr: $0[kSecAttrKeyType] as! NSNumber)
@ -175,20 +175,6 @@ extension SmartCard.Store {
secrets.append(contentsOf: wrapped) secrets.append(contentsOf: wrapped)
} }
private func signatureAlgorithm(for secret: SmartCard.Secret) -> SecKeyAlgorithm {
switch (secret.algorithm, secret.keySize) {
case (.ellipticCurve, 256):
return .ecdsaSignatureMessageX962SHA256
case (.ellipticCurve, 384):
return .ecdsaSignatureMessageX962SHA384
case (.rsa, 1024), (.rsa, 2048):
return .rsaSignatureMessagePKCS1v15SHA512
default:
fatalError()
}
}
} }
@ -214,11 +200,11 @@ extension SmartCard.Store {
var encryptError: SecurityError? var encryptError: SecurityError?
let untyped: CFTypeRef? = SecKeyCreateWithData(secret.publicKey as CFData, attributes, &encryptError) let untyped: CFTypeRef? = SecKeyCreateWithData(secret.publicKey as CFData, attributes, &encryptError)
guard let untypedSafe = untyped else { guard let untypedSafe = untyped else {
throw SmartCard.KeychainError(statusCode: errSecSuccess) throw KeychainError(statusCode: errSecSuccess)
} }
let key = untypedSafe as! SecKey let key = untypedSafe as! SecKey
guard let signature = SecKeyCreateEncryptedData(key, encryptionAlgorithm(for: secret), data as CFData, &encryptError) else { guard let signature = SecKeyCreateEncryptedData(key, encryptionAlgorithm(for: secret), data as CFData, &encryptError) else {
throw SmartCard.SigningError(error: encryptError) throw SigningError(error: encryptError)
} }
return signature as Data return signature as Data
} }
@ -245,20 +231,20 @@ extension SmartCard.Store {
var untyped: CFTypeRef? var untyped: CFTypeRef?
let status = SecItemCopyMatching(attributes, &untyped) let status = SecItemCopyMatching(attributes, &untyped)
if status != errSecSuccess { if status != errSecSuccess {
throw SmartCard.KeychainError(statusCode: status) throw KeychainError(statusCode: status)
} }
guard let untypedSafe = untyped else { guard let untypedSafe = untyped else {
throw SmartCard.KeychainError(statusCode: errSecSuccess) throw KeychainError(statusCode: errSecSuccess)
} }
let key = untypedSafe as! SecKey let key = untypedSafe as! SecKey
var encryptError: SecurityError? var encryptError: SecurityError?
guard let signature = SecKeyCreateDecryptedData(key, encryptionAlgorithm(for: secret), data as CFData, &encryptError) else { guard let signature = SecKeyCreateDecryptedData(key, encryptionAlgorithm(for: secret), data as CFData, &encryptError) else {
throw SmartCard.SigningError(error: encryptError) throw SigningError(error: encryptError)
} }
return signature as Data return signature as Data
} }
private func encryptionAlgorithm(for secret: SmartCard.Secret) -> SecKeyAlgorithm { private func encryptionAlgorithm(for secret: SecretType) -> SecKeyAlgorithm {
switch (secret.algorithm, secret.keySize) { switch (secret.algorithm, secret.keySize) {
case (.ellipticCurve, 256): case (.ellipticCurve, 256):
return .eciesEncryptionCofactorVariableIVX963SHA256AESGCM return .eciesEncryptionCofactorVariableIVX963SHA256AESGCM