-
-
Notifications
You must be signed in to change notification settings - Fork 159
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rounded out the rest of the SmartCardStore API #450
Merged
Merged
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
maxgoedjen
reviewed
Mar 5, 2023
Comment on lines
89
to
194
kSecAttrKeyClass: kSecAttrKeyClassPublic | ||
]) | ||
var encryptError: SecurityError? | ||
var untyped: CFTypeRef? = SecKeyCreateWithData(secret.publicKey as CFData, attributes, &encryptError) | ||
guard let untypedSafe = untyped else { | ||
throw KeychainError(statusCode: errSecSuccess) | ||
} | ||
let key = untypedSafe as! SecKey | ||
let signatureAlgorithm: SecKeyAlgorithm | ||
switch (secret.algorithm, secret.keySize) { | ||
case (.ellipticCurve, 256): | ||
signatureAlgorithm = .ecdsaSignatureMessageX962SHA256 | ||
case (.ellipticCurve, 384): | ||
signatureAlgorithm = .ecdsaSignatureMessageX962SHA384 | ||
case (.rsa, 1024): | ||
signatureAlgorithm = .rsaSignatureMessagePKCS1v15SHA512 | ||
case (.rsa, 2048): | ||
signatureAlgorithm = .rsaSignatureMessagePKCS1v15SHA512 | ||
default: | ||
fatalError() | ||
} | ||
let signature = SecKeyVerifySignature(key, signatureAlgorithm, data as CFData, signature as CFData, &encryptError) | ||
if !signature { | ||
throw SigningError(error: encryptError) | ||
} | ||
return signature | ||
} | ||
|
||
public func encrypt(data: Data, with secret: SecretType) throws -> Data { | ||
let attributes = KeychainDictionary([ | ||
kSecAttrKeyType: secret.algorithm.secAttrKeyType, | ||
kSecAttrKeySizeInBits: secret.keySize, | ||
kSecAttrKeyClass: kSecAttrKeyClassPublic | ||
]) | ||
var encryptError: SecurityError? | ||
let untyped: CFTypeRef? = SecKeyCreateWithData(secret.publicKey as CFData, attributes, &encryptError) | ||
guard let untypedSafe = untyped else { | ||
throw KeychainError(statusCode: errSecSuccess) | ||
} | ||
let key = untypedSafe as! SecKey | ||
let signatureAlgorithm: SecKeyAlgorithm | ||
switch (secret.algorithm, secret.keySize) { | ||
case (.ellipticCurve, 256): | ||
signatureAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM | ||
case (.ellipticCurve, 384): | ||
signatureAlgorithm = .eciesEncryptionCofactorVariableIVX963SHA256AESGCM | ||
case (.rsa, 1024): | ||
signatureAlgorithm = .rsaEncryptionOAEPSHA512AESGCM | ||
case (.rsa, 2048): | ||
signatureAlgorithm = .rsaEncryptionOAEPSHA512AESGCM | ||
default: | ||
fatalError() | ||
} | ||
guard let signature = SecKeyCreateEncryptedData(key, signatureAlgorithm, data as CFData, &encryptError) else { | ||
throw SigningError(error: encryptError) | ||
} | ||
return signature as Data | ||
} | ||
|
||
public func decrypt(data: Data, with secret: SecretType) throws -> Data { | ||
guard let tokenID = tokenID else { fatalError() } | ||
let context = LAContext() | ||
context.localizedReason = "decrypt a file using secret \"\(secret.name)\"" | ||
context.localizedCancelTitle = "Deny" | ||
let attributes = KeychainDictionary([ | ||
kSecClass: kSecClassKey, | ||
kSecAttrKeyClass: kSecAttrKeyClassPrivate, | ||
kSecAttrApplicationLabel: secret.id as CFData, | ||
kSecAttrTokenID: tokenID, | ||
kSecUseAuthenticationContext: context, | ||
kSecReturnRef: true | ||
]) | ||
var untyped: CFTypeRef? | ||
let status = SecItemCopyMatching(attributes, &untyped) | ||
if status != errSecSuccess { | ||
throw KeychainError(statusCode: status) | ||
} | ||
guard let untypedSafe = untyped else { | ||
throw KeychainError(statusCode: errSecSuccess) | ||
} | ||
let key = untypedSafe as! SecKey | ||
var encryptError: SecurityError? | ||
let signatureAlgorithm: SecKeyAlgorithm | ||
switch (secret.algorithm, secret.keySize) { | ||
case (.ellipticCurve, 256): | ||
signatureAlgorithm = .eciesEncryptionStandardX963SHA256AESGCM | ||
case (.ellipticCurve, 384): | ||
signatureAlgorithm = .eciesEncryptionStandardX963SHA384AESGCM | ||
case (.rsa, 1024): | ||
signatureAlgorithm = .rsaEncryptionOAEPSHA512AESGCM | ||
case (.rsa, 2048): | ||
signatureAlgorithm = .rsaEncryptionOAEPSHA512AESGCM | ||
default: | ||
fatalError() | ||
} | ||
guard let signature = SecKeyCreateDecryptedData(key, signatureAlgorithm, data as CFData, &encryptError) else { | ||
throw SigningError(error: encryptError) | ||
} | ||
return signature as Data | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you move these to a same-file extension on the store, so it's clear that they're not part of the core API? Docs on the methods would be appreciated too since they're not documented in protocol like the others are.
Looks good! Nice job. |
maxgoedjen
approved these changes
Mar 12, 2023
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
I'm not sure who this is for, and I'm only 80% sure the openssh identifiers are correct, but you can do this, and you probably could support RSA keys on smart cards.
I could also probably move the verify and encrypt API up a few levels because it only needs a public key.
Also, for completeness, the OpenSSH API should have a way to load the public keys.