From 55201a8f83c8785945be6828e3dc58dbd7eae0e4 Mon Sep 17 00:00:00 2001 From: Paul Toffoloni <69189821+ptoffy@users.noreply.github.com> Date: Mon, 16 Sep 2024 13:34:21 +0200 Subject: [PATCH] extras: Add RSA primitives decoding API (#249) Motivation: At times we might need to get the raw elements of an RSA key, e.g when debugging. Modifications: Add a getKeyPrimitives method to the RSA (public and private) key structures that returns the elements. To do this we leverage the built in BoringSSL methods (CCryptoBoringSSL_RSA_get0_n etc.) which return the raw elements of the key. In the Security version of the key we convert the key to a BoringSSL one and just call the BoringSSL method. Once we get ASN.1 support in _CryptoExtras it might make sense switching the implementation of this method to decode the ASN.1 structure and returns the elements from there. Result: New APIs to fetch the key primitives. --------- Co-authored-by: Cory Benfield --- .../_CryptoExtras/RSA/RSA+BlindSigning.swift | 15 ++++++++++ Sources/_CryptoExtras/RSA/RSA.swift | 30 +++++++++++++++++++ Sources/_CryptoExtras/RSA/RSA_boring.swift | 20 +++++++++++++ Sources/_CryptoExtras/RSA/RSA_security.swift | 4 +++ .../TestRSABlindSigning.swift | 14 +++++++++ .../TestRSAEncryption.swift | 11 +++++++ Tests/_CryptoExtrasTests/TestRSASigning.swift | 11 +++++++ 7 files changed, 105 insertions(+) diff --git a/Sources/_CryptoExtras/RSA/RSA+BlindSigning.swift b/Sources/_CryptoExtras/RSA/RSA+BlindSigning.swift index ea918bf7..9108c039 100644 --- a/Sources/_CryptoExtras/RSA/RSA+BlindSigning.swift +++ b/Sources/_CryptoExtras/RSA/RSA+BlindSigning.swift @@ -26,6 +26,16 @@ extension _RSA.BlindSigning { public struct PublicKey: Sendable { public typealias Parameters = _RSA.BlindSigning.Parameters + public struct Primitives: Sendable, Hashable { + public var modulus: Data + public var publicExponent: Data + + public init(modulus: Data, publicExponent: Data) { + self.modulus = modulus + self.publicExponent = publicExponent + } + } + private var backing: BackingPublicKey private let parameters: Parameters @@ -113,6 +123,11 @@ extension _RSA.BlindSigning { self.backing = backing self.parameters = parameters } + + public func getKeyPrimitives() throws -> Primitives { + let (n, e) = try self.backing.getKeyPrimitives() + return Primitives(modulus: n, publicExponent: e) + } } } diff --git a/Sources/_CryptoExtras/RSA/RSA.swift b/Sources/_CryptoExtras/RSA/RSA.swift index 53494819..bd4bf516 100644 --- a/Sources/_CryptoExtras/RSA/RSA.swift +++ b/Sources/_CryptoExtras/RSA/RSA.swift @@ -45,6 +45,16 @@ extension _RSA { extension _RSA.Signing { public struct PublicKey: Sendable { + public struct Primitives: Sendable, Hashable { + public var modulus: Data + public var publicExponent: Data + + public init(modulus: Data, publicExponent: Data) { + self.modulus = modulus + self.publicExponent = publicExponent + } + } + private var backing: BackingPublicKey /// Construct an RSA public key from a PEM representation. @@ -132,6 +142,11 @@ extension _RSA.Signing { fileprivate init(_ backing: BackingPublicKey) { self.backing = backing } + + public func getKeyPrimitives() throws -> Primitives { + let (n, e) = try self.backing.getKeyPrimitives() + return Primitives(modulus: n, publicExponent: e) + } } } @@ -426,6 +441,16 @@ extension _RSA.Signing { extension _RSA.Encryption { /// Identical to ``_RSA/Signing/PublicKey``. public struct PublicKey { + public struct Primitives: Sendable, Hashable { + public var modulus: Data + public var publicExponent: Data + + public init(modulus: Data, publicExponent: Data) { + self.modulus = modulus + self.publicExponent = publicExponent + } + } + private var backing: BackingPublicKey /// Construct an RSA public key from a PEM representation. @@ -483,6 +508,11 @@ extension _RSA.Encryption { public var pemRepresentation: String { self.backing.pemRepresentation } public var keySizeInBits: Int { self.backing.keySizeInBits } fileprivate init(_ backing: BackingPublicKey) { self.backing = backing } + + public func getKeyPrimitives() throws -> Primitives { + let (n, e) = try self.backing.getKeyPrimitives() + return Primitives(modulus: n, publicExponent: e) + } } /// Identical to ``_RSA/Signing/PrivateKey``. diff --git a/Sources/_CryptoExtras/RSA/RSA_boring.swift b/Sources/_CryptoExtras/RSA/RSA_boring.swift index bb670c03..9b9c50bf 100644 --- a/Sources/_CryptoExtras/RSA/RSA_boring.swift +++ b/Sources/_CryptoExtras/RSA/RSA_boring.swift @@ -60,6 +60,10 @@ internal struct BoringSSLRSAPublicKey: Sendable { fileprivate init(_ backing: Backing) { self.backing = backing } + + func getKeyPrimitives() throws -> (n: Data, e: Data) { + try self.backing.getKeyPrimitives() + } } @@ -465,6 +469,22 @@ extension BoringSSLRSAPublicKey { deinit { CCryptoBoringSSL_EVP_PKEY_free(self.pointer) } + + fileprivate func getKeyPrimitives() -> (n: Data, e: Data) { + let key = CCryptoBoringSSL_EVP_PKEY_get0_RSA(self.pointer) + + func getPrimitive(_ getPointer: (OpaquePointer?) -> UnsafePointer?) -> Data { + let ptr = getPointer(key) + let size = Int(CCryptoBoringSSL_BN_num_bytes(ptr)) + var data = Data(count: size) + data.withUnsafeMutableBytes { dataPtr in + _ = CCryptoBoringSSL_BN_bn2bin(ptr, dataPtr.baseAddress) + } + return data + } + + return (getPrimitive(CCryptoBoringSSL_RSA_get0_n), getPrimitive(CCryptoBoringSSL_RSA_get0_e)) + } } } diff --git a/Sources/_CryptoExtras/RSA/RSA_security.swift b/Sources/_CryptoExtras/RSA/RSA_security.swift index 05fda8d1..6d84c4cb 100644 --- a/Sources/_CryptoExtras/RSA/RSA_security.swift +++ b/Sources/_CryptoExtras/RSA/RSA_security.swift @@ -72,6 +72,10 @@ internal struct SecurityRSAPublicKey: @unchecked Sendable { fileprivate init(_ backing: SecKey) { self.backing = backing } + + func getKeyPrimitives() throws -> (n: Data, e: Data) { + try BoringSSLRSAPublicKey(derRepresentation: self.derRepresentation).getKeyPrimitives() + } } // unchecked sendable until `SecKey` gets sendable annotations diff --git a/Tests/_CryptoExtrasTests/TestRSABlindSigning.swift b/Tests/_CryptoExtrasTests/TestRSABlindSigning.swift index 3a67561f..5a0a2766 100644 --- a/Tests/_CryptoExtrasTests/TestRSABlindSigning.swift +++ b/Tests/_CryptoExtrasTests/TestRSABlindSigning.swift @@ -202,4 +202,18 @@ final class TestRSABlindSigning: XCTestCase { parameters: .RSABSSA_SHA384_PSS_Randomized ) } + + func testGetKeyPrimitives() throws { + for testVector in RFC9474TestVector.allValues { + let n = try Data(hexString: testVector.n) + let e = try Data(hexString: testVector.e) + + let primitives = try _RSA.BlindSigning.PublicKey( + n: n, e: e, + parameters: testVector.parameters + ).getKeyPrimitives() + XCTAssertEqual(primitives.modulus, n) + XCTAssertEqual(primitives.publicExponent, e) + } + } } diff --git a/Tests/_CryptoExtrasTests/TestRSAEncryption.swift b/Tests/_CryptoExtrasTests/TestRSAEncryption.swift index fe6ad142..e5f8fe88 100644 --- a/Tests/_CryptoExtrasTests/TestRSAEncryption.swift +++ b/Tests/_CryptoExtrasTests/TestRSAEncryption.swift @@ -108,6 +108,17 @@ final class TestRSAEncryption: XCTestCase { e: bytesValues.randomElement()! ) } + + func testGetKeyPrimitives() throws { + for testVector in RFC9474TestVector.allValues { + let n = try Data(hexString: testVector.n) + let e = try Data(hexString: testVector.e) + + let primitives = try _RSA.Encryption.PublicKey(n: n, e: e).getKeyPrimitives() + XCTAssertEqual(primitives.modulus, n) + XCTAssertEqual(primitives.publicExponent, e) + } + } } struct RSAEncryptionOAEPTestGroup: Codable { diff --git a/Tests/_CryptoExtrasTests/TestRSASigning.swift b/Tests/_CryptoExtrasTests/TestRSASigning.swift index 8ae65fb8..fe47743b 100644 --- a/Tests/_CryptoExtrasTests/TestRSASigning.swift +++ b/Tests/_CryptoExtrasTests/TestRSASigning.swift @@ -708,6 +708,17 @@ final class TestRSASigning: XCTestCase { ) } + func testGetKeyPrimitives() throws { + for testVector in RFC9474TestVector.allValues { + let n = try Data(hexString: testVector.n) + let e = try Data(hexString: testVector.e) + + let primitives = try _RSA.Signing.PublicKey(n: n, e: e).getKeyPrimitives() + XCTAssertEqual(primitives.modulus, n) + XCTAssertEqual(primitives.publicExponent, e) + } + } + private func testPKCS1Group(_ group: RSAPKCS1TestGroup) throws { let derKey: _RSA.Signing.PublicKey let pemKey: _RSA.Signing.PublicKey