Skip to content

Commit da38d34

Browse files
committed
Adds retrieval support for RSA keys
1 parent 1d9d3b1 commit da38d34

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

Sources/SwiftSecurity/CryptoKit/SecKeyConvertible.swift

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff 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>?

Sources/SwiftSecurity/Keychain/Keychain.swift

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff 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

0 commit comments

Comments
 (0)