|
|
@@ -61,7 +61,69 @@
|
|
|
XCTAssertEqual(rsaSecKeyRawRep, Data(rsaCryptoSwiftRawRep))
|
|
|
XCTAssertEqual(rsaSecKeyRawRep, try rsaCryptoSwift.publicKeyExternalRepresentation())
|
|
|
}
|
|
|
+
|
|
|
+ /// From CryptoSwift RSA -> External Representation -> SecKey
|
|
|
+ ///
|
|
|
+ /// This test enforces that
|
|
|
+ /// 1) We can export the raw external representation of a 1 byte CryptoSwift RSA Public Key
|
|
|
+ /// 2) And that we can import / create an RSA SecKey from that raw external representation
|
|
|
+ /// 3) Proves interoperability between Apple's `Security` Framework and `CryptoSwift`
|
|
|
+ func testRSAExternalRepresentationPublic_1ByteExponent() throws {
|
|
|
+
|
|
|
+ // Generate a CryptoSwift RSA Key
|
|
|
+ let n: Array<UInt8> = [
|
|
|
+ 0xDF, 0x74, 0xEA, 0xA8, 0xB4, 0x77, 0x79, 0xE5, 0x26, 0x0E, 0xD1, 0xD2, 0xF3, 0xDA, 0x85, 0xDE,
|
|
|
+ 0x9E, 0x0C, 0x3F, 0xB5, 0x9F, 0xAA, 0xDD, 0x99, 0x8E, 0x3D, 0xE5, 0x66, 0x32, 0xE3, 0x8C, 0x21,
|
|
|
+ 0x83, 0xBF, 0x0A, 0xFD, 0x70, 0x93, 0x65, 0x34, 0x38, 0xAE, 0x29, 0xBA, 0x0E, 0x0E, 0x05, 0x22,
|
|
|
+ 0x4F, 0x86, 0x5D, 0x3D, 0x66, 0xCE, 0x88, 0xC0, 0x1F, 0x0D, 0xC0, 0xF3, 0x94, 0xEC, 0x40, 0xD3,
|
|
|
+ 0x80, 0xC3, 0x9C, 0x41, 0x46, 0x01, 0xFF, 0xA4, 0x0D, 0x08, 0x8D, 0x49, 0xED, 0x99, 0xB6, 0xA0,
|
|
|
+ 0xBB, 0x6E, 0x72, 0x1C, 0xF3, 0xE2, 0x92, 0x1B, 0xFA, 0x8E, 0x19, 0x02, 0x15, 0x69, 0xAC, 0xB8,
|
|
|
+ 0x51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0x03
|
|
|
+ ]
|
|
|
|
|
|
+ let rsaCryptoSwift = RSA(n: n, e: e)
|
|
|
+
|
|
|
+ // Get the key's rawExternalRepresentation
|
|
|
+ let rsaCryptoSwiftRawRep = try rsaCryptoSwift.publicKeyDER()
|
|
|
+
|
|
|
+ // We should be able to instantiate an RSA SecKey from this data
|
|
|
+ let attributes: [String: Any] = [
|
|
|
+ kSecAttrKeyType as String: kSecAttrKeyTypeRSA,
|
|
|
+ kSecAttrKeyClass as String: kSecAttrKeyClassPublic,
|
|
|
+ kSecAttrKeySizeInBits as String: 2048,
|
|
|
+ kSecAttrIsPermanent as String: false
|
|
|
+ ]
|
|
|
+ var error: Unmanaged<CFError>?
|
|
|
+ guard let rsaSecKey = SecKeyCreateWithData(Data(rsaCryptoSwiftRawRep) as CFData, attributes as CFDictionary, &error) else {
|
|
|
+ XCTFail("Error constructing SecKey from raw key data: \(error.debugDescription)")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Get the SecKey's external representation
|
|
|
+ var externalRepError: Unmanaged<CFError>?
|
|
|
+ guard let rsaSecKeyRawRep = SecKeyCopyExternalRepresentation(rsaSecKey, &externalRepError) as? Data else {
|
|
|
+ XCTFail("Failed to copy external representation for RSA SecKey")
|
|
|
+ return
|
|
|
+ }
|
|
|
+
|
|
|
+ // Ensure both the CryptoSwift Ext Rep and the SecKey Ext Rep match
|
|
|
+ XCTAssertEqual(rsaSecKeyRawRep, Data(rsaCryptoSwiftRawRep))
|
|
|
+ XCTAssertEqual(rsaSecKeyRawRep, try rsaCryptoSwift.publicKeyExternalRepresentation())
|
|
|
+ }
|
|
|
+
|
|
|
/// From CryptoSwift RSA -> External Representation -> SecKey
|
|
|
///
|
|
|
/// This test enforces that
|