File tree Expand file tree Collapse file tree 2 files changed +17
-4
lines changed
Expand file tree Collapse file tree 2 files changed +17
-4
lines changed Original file line number Diff line number Diff line change @@ -14,8 +14,14 @@ public protocol SecKeyConvertible: SecKeyRepresentable {
1414 /// Creates a key from an X9.63 representation.
1515 init < Bytes> ( x963Representation: Bytes ) throws where Bytes: ContiguousBytes
1616
17+ /// Creates a key from a Distinguished Encoding Rules (DER) encoded representation.
18+ init < Bytes> ( derRepresentation: Bytes ) throws where Bytes : RandomAccessCollection , Bytes. Element == UInt8
19+
1720 /// An X9.63 representation of the key.
1821 var x963Representation : Data { get }
22+
23+ /// A Distinguished Encoding Rules (DER) encoded representation of the private key.
24+ var derRepresentation : Data { get }
1925}
2026
2127// MARK: - CryptoKit
@@ -86,10 +92,11 @@ extension SecKeyConvertible {
8692 let keyData : Data
8793 switch secKeyDescriptor. keyType {
8894 case . ecsecPrimeRandom:
95+ // X9.63
8996 keyData = x963Representation
9097 case . rsa:
91- // override and use data in PKCS #1 format
92- throw SwiftSecurityError . unimplemented
98+ // PCKS #1, DER-Encoded
99+ keyData = derRepresentation
93100 }
94101
95102 var error : Unmanaged < CFError > ?
Original file line number Diff line number Diff line change @@ -349,8 +349,14 @@ extension Keychain: SecKeyStore {
349349 }
350350 throw SwiftSecurityError . invalidParameter
351351 }
352-
353- return try T ( x963Representation: data)
352+
353+ if let ecKey = try ? T ( x963Representation: data) {
354+ return ecKey
355+ } else if let rsaKey = try ? T ( derRepresentation: data) {
356+ return rsaKey
357+ } else {
358+ throw SwiftSecurityError . invalidParameter
359+ }
354360 }
355361}
356362
You can’t perform that action at this time.
0 commit comments