Skip to content

Commit

Permalink
Adds retrieval support for RSA keys
Browse files Browse the repository at this point in the history
  • Loading branch information
dm-zharov committed Sep 10, 2024
1 parent 1d9d3b1 commit da38d34
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
11 changes: 9 additions & 2 deletions Sources/SwiftSecurity/CryptoKit/SecKeyConvertible.swift
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ public protocol SecKeyConvertible: SecKeyRepresentable {
/// Creates a key from an X9.63 representation.
init<Bytes>(x963Representation: Bytes) throws where Bytes: ContiguousBytes

/// Creates a key from a Distinguished Encoding Rules (DER) encoded representation.
init<Bytes>(derRepresentation: Bytes) throws where Bytes : RandomAccessCollection, Bytes.Element == UInt8

/// An X9.63 representation of the key.
var x963Representation: Data { get }

/// A Distinguished Encoding Rules (DER) encoded representation of the private key.
var derRepresentation: Data { get }
}

// MARK: - CryptoKit
Expand Down Expand Up @@ -86,10 +92,11 @@ extension SecKeyConvertible {
let keyData: Data
switch secKeyDescriptor.keyType {
case .ecsecPrimeRandom:
// X9.63
keyData = x963Representation
case .rsa:
// override and use data in PKCS #1 format
throw SwiftSecurityError.unimplemented
// PCKS #1, DER-Encoded
keyData = derRepresentation
}

var error: Unmanaged<CFError>?
Expand Down
10 changes: 8 additions & 2 deletions Sources/SwiftSecurity/Keychain/Keychain.swift
Original file line number Diff line number Diff line change
Expand Up @@ -349,8 +349,14 @@ extension Keychain: SecKeyStore {
}
throw SwiftSecurityError.invalidParameter
}

return try T(x963Representation: data)

if let ecKey = try? T(x963Representation: data) {
return ecKey
} else if let rsaKey = try? T(derRepresentation: data) {
return rsaKey
} else {
throw SwiftSecurityError.invalidParameter
}
}
}

Expand Down

0 comments on commit da38d34

Please sign in to comment.