Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[77] Improve test suite for RSAEncrypter/Decrypter #81

Merged
merged 32 commits into from
Jul 30, 2018
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
bc7bbdf
[JOSE-80] Add shared user data introduced Xcode 9.3 to gitignore
mohemian-92817281 Apr 11, 2018
9c0c504
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 May 3, 2018
7d00453
[JOSE-80] Add failure test case for decrypting empty data
mohemian-92817281 May 3, 2018
9567ae8
[JOSE-80] Better assertions for failure case
mohemian-92817281 May 3, 2018
2265f17
[JOSE-80] privateKey2048 -> privateKeyAlice2048
mohemian-92817281 May 3, 2018
a87570c
[JOSE-80] publicKey2048 -> publicKeyAlice2048
mohemian-92817281 May 3, 2018
bcec1c5
[JOSE-80] Add Bob’s 2048 bit private key
mohemian-92817281 May 3, 2018
17ffa68
[JOSE-80] Add encryption test for Bob’s key
mohemian-92817281 May 3, 2018
f7350e5
[JOSE-80] keyPair2048 -> keyPairAlice2048
mohemian-92817281 May 3, 2018
9650165
[JOSE-80] Read Bob’s keypair from plist file
mohemian-92817281 May 3, 2018
79582f8
[JOSE-80] cipherTextBase64URL -> cipherTextWithAliceKeyBase64URL
mohemian-92817281 May 3, 2018
c660a0c
[JOSE-80] Load Bob’s key data; update cipher texts; add Bob decryptio…
mohemian-92817281 May 3, 2018
62f222d
[JOSE-80] Add test for encrypting the same data twice
mohemian-92817281 May 3, 2018
40ce19b
[JOSE-80] Add test to check if Alice’s and Bob’s cipher texts differ
mohemian-92817281 May 3, 2018
a7292e5
[JOSE-80] Add equality for decryption error
mohemian-92817281 May 3, 2018
ee5323a
[JOSE-80] Add default decryption error
mohemian-92817281 May 3, 2018
2d4f09f
[JOSE-80] Decrypting with the wrong key should throw an error
mohemian-92817281 May 7, 2018
8c58365
[JOSE-80] Update Bob’s cipher text
mohemian-92817281 May 7, 2018
f36fdb2
[JOSE-80] Fix Base64 variable names
mohemian-92817281 May 14, 2018
eeecbde
[JOSE-80] Use multiline literals for cipher texts
mohemian-92817281 May 14, 2018
9d675a3
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 18, 2018
03adc40
[JOSE-80] Re-generate Alice’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
25fe769
[JOSE-80] Re-generate Bob’s cypher text with openssl; adjust test
mohemian-92817281 Jul 26, 2018
ba2dac1
[JOSE-80] Better comments
mohemian-92817281 Jul 26, 2018
3d374e6
[JOSE-80] Remove nonsensical empty string test
mohemian-92817281 Jul 26, 2018
6a8a3c8
[JOSE-80] Rename TestKey.plist -> TestKeys.plist
mohemian-92817281 Jul 26, 2018
09b1963
[JOSE-80] Add decrypter test for zero-length data
mohemian-92817281 Jul 26, 2018
5a75cf4
[JOSE-80] Add tests for encrypting message of length `mLen` and `mLen…
mohemian-92817281 Jul 30, 2018
b221486
[JOSE-80] Better naming for zero length decryption test
mohemian-92817281 Jul 30, 2018
130d902
[JOSE-80] Add decryption tests for `len=k`, `len=k-1`, `len=k+1`
mohemian-92817281 Jul 30, 2018
f307b16
Merge branch 'master' into feature/JOSE-80-improve-tests
mohemian-92817281 Jul 30, 2018
fdbfc93
[JOSE-80] Use Alice’s key for new tests
mohemian-92817281 Jul 30, 2018
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,4 @@ fastlane/test_output

.idea/
sonar-reports/
/JOSESwift.xcworkspace/xcshareddata
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🙏

33 changes: 23 additions & 10 deletions Tests/CryptoTestCase.swift
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,14 @@ import XCTest

class CryptoTestCase: XCTestCase {
let message = "The true sign of intelligence is not knowledge but imagination."
let privateKey2048Tag = "com.airsidemobile.JOSESwift.testPrivateKey2048"
let privateKeyAlice2048Tag = "com.airsidemobile.JOSESwift.testprivateKeyAlice2048"
let privateKeyBob2048Tag = "com.airsidemobile.JOSESwift.testprivateKeyBob2048"
let privateKey4096Tag = "com.airsidemobile.JOSESwift.testPrivateKey4096"

var privateKey2048: SecKey?
var publicKey2048: SecKey?
var privateKeyAlice2048: SecKey?
var privateKeyBob2048: SecKey?
var publicKeyAlice2048: SecKey?
var publicKeyBob2048: SecKey?

var privateKey4096: SecKey?
var publicKey4096: SecKey?
Expand All @@ -42,7 +45,8 @@ class CryptoTestCase: XCTestCase {
v_TA
"""

var publicKey2048Data: Data!
var publicKeyAlice2048Data: Data!
var publicKeyBob2048Data: Data!
var publicKey4096Data: Data!

// Generated by OpenSSL for `publicKey` (without leading 0x00).
Expand Down Expand Up @@ -127,17 +131,26 @@ class CryptoTestCase: XCTestCase {
if
let path = Bundle(for: type(of: self)).path(forResource: "TestKey", ofType: "plist"),
let keyDict = NSDictionary(contentsOfFile: path),
let keyData2048 = Data(base64Encoded: keyDict[privateKey2048Tag] as! String),
let keyDataAlice2048 = Data(base64Encoded: keyDict[privateKeyAlice2048Tag] as! String),
let keyDataBob2048 = Data(base64Encoded: keyDict[privateKeyBob2048Tag] as! String),
let keyData4096 = Data(base64Encoded: keyDict[privateKey4096Tag] as! String)
{

// 2048
// 2048 - Alice

let keyPair2048 = setupSecKeyPair(size: 2048, data: keyData2048, tag: privateKey2048Tag)!
let keyPairAlice2048 = setupSecKeyPair(size: 2048, data: keyDataAlice2048, tag: privateKeyAlice2048Tag)!

privateKey2048 = keyPair2048.privateKey
publicKey2048 = keyPair2048.publicKey
publicKey2048Data = SecKeyCopyExternalRepresentation(publicKey2048!, nil)! as Data
privateKeyAlice2048 = keyPairAlice2048.privateKey
publicKeyAlice2048 = keyPairAlice2048.publicKey
publicKeyAlice2048Data = SecKeyCopyExternalRepresentation(publicKeyAlice2048!, nil)! as Data

// 2048 - Bob

let keyPairBob2048 = setupSecKeyPair(size: 2048, data: keyDataBob2048, tag: privateKeyBob2048Tag)!

privateKeyBob2048 = keyPairBob2048.privateKey
publicKeyBob2048 = keyPairBob2048.publicKey
publicKeyBob2048Data = SecKeyCopyExternalRepresentation(publicKeyBob2048!, nil)! as Data

// 4096

Expand Down
14 changes: 7 additions & 7 deletions Tests/DataRSAPublicKeyTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ import XCTest
class DataRSAPublicKeyTests: CryptoTestCase {

func testLeadingZeroDropped() {
let components = try! publicKey2048Data.rsaPublicKeyComponents()
let components = try! publicKeyAlice2048Data.rsaPublicKeyComponents()

XCTAssertEqual(try! [UInt8](publicKey2048Data).read(.sequence).read(.integer).first!, 0x00)
XCTAssertEqual(try! [UInt8](publicKeyAlice2048Data).read(.sequence).read(.integer).first!, 0x00)
XCTAssertNotEqual([UInt8](components.modulus).first!, 0x00)
}

func testPublicKey2048Modulus() {
let components = try? publicKey2048Data.rsaPublicKeyComponents()
func testpublicKeyAlice2048Modulus() {
let components = try? publicKeyAlice2048Data.rsaPublicKeyComponents()

XCTAssertNotNil(components)

Expand All @@ -43,8 +43,8 @@ class DataRSAPublicKeyTests: CryptoTestCase {
XCTAssertEqual(modulus, expectedModulus2048Data)
}

func testPublicKey2048Exponent() {
let components = try? publicKey2048Data.rsaPublicKeyComponents()
func testpublicKeyAlice2048Exponent() {
let components = try? publicKeyAlice2048Data.rsaPublicKeyComponents()

XCTAssertNotNil(components)

Expand Down Expand Up @@ -77,7 +77,7 @@ class DataRSAPublicKeyTests: CryptoTestCase {
let components = (expectedModulus2048Data, expectedExponentData)
let data = try! Data.representing(rsaPublicKeyComponents: components)

let expectedData = publicKey2048Data
let expectedData = publicKeyAlice2048Data

XCTAssertEqual(data, expectedData)
}
Expand Down
10 changes: 5 additions & 5 deletions Tests/JWETests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ class JWETests: CryptoTestCase {
func testJWERoundtrip() {
let header = JWEHeader(algorithm: .RSA1_5, encryptionAlgorithm: .A256CBCHS512)
let payload = Payload(message.data(using: .utf8)!)
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let jweEnc = try! JWE(header: header, payload: payload, encrypter: encrypter)

let jweDec = try! JWE(compactSerialization: jweEnc.compactSerializedData)
let decryptedPayload = try! jweDec.decrypt(with: privateKey2048!)
let decryptedPayload = try! jweDec.decrypt(with: privateKeyAlice2048!)

XCTAssertEqual(message.data(using: .utf8)!, decryptedPayload.data())
}

func testDecrypt() {
let compactSerializedJWE = "eyJhbGciOiJSU0ExXzUiLCJlbmMiOiJBMjU2Q0JDLUhTNTEyIn0.Od5AMgOHu6rcEYWkX7w_x_wnMlM5JfZaszCC4xtLGYU9d0BnPm95UWUrgShStGH6LHMxpGdru6gXpdxfhhrji12vUIzmkbyNW5M9wjx2t0e4pzzBSYxgOzFoa3jT9a0PcZfyqHIeTrcrTHtpSJ_CIDiZ3MIeqA7hjuRqu2YcTAE0v5TPLhHDVRBptkOggA5SL2-gRuUuYoWdanMw_JTHK4utXQZoSY1LTdub_Fh5ez1RqOouc3an5Hx6ImzyJS_cbO_l9xHpHjE7in6SeV9bAZTaYEaGnjGKEVaGQ7JiwtTA5rDfVQ5RHSn6blB2Hh5Am7mKzssYu9JjUmr3T-ez_g.M6QnlRxQQ5YS2rF4-wwT3g.4GAtq6fJWJt249SEuK5P_3xJGNYP_e_rhz0PVg9QnJXiRl030ggI9GGs3E_0pEPBs9_WJ3E60qQVoXTIMbJXSQ.bQc-W1Ph_0_3kX570pT8gjDlGyiK3kF8PlHiT7GWfMo"
let jwe = try! JWE(compactSerialization: compactSerializedJWE)
let payloadString = String(data: (try! jwe.decrypt(with: privateKey2048!)).data(), encoding: .utf8)!
let payloadString = String(data: (try! jwe.decrypt(with: privateKeyAlice2048!)).data(), encoding: .utf8)!

XCTAssertEqual(payloadString, "The true sign of intelligence is not knowledge but imagination.")
}

func testDecryptFails() {
let header = JWEHeader(algorithm: .RSA1_5, encryptionAlgorithm: .A256CBCHS512)
let payload = Payload(message.data(using: .utf8)!)
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKey2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let encrypter = Encrypter(keyEncryptionAlgorithm: .RSA1_5, keyEncryptionKey: publicKeyAlice2048!, contentEncyptionAlgorithm: .A256CBCHS512)!
let jweEnc = try! JWE(header: header, payload: payload, encrypter: encrypter)

let attributes: [String: Any] = [
Expand All @@ -66,7 +66,7 @@ class JWETests: CryptoTestCase {
kSecAttrKeySizeInBits as String: 2048,
kSecPrivateKeyAttrs as String: [
kSecAttrIsPermanent as String: false,
kSecAttrApplicationTag as String: privateKey2048Tag
kSecAttrApplicationTag as String: privateKeyAlice2048Tag
]
]

Expand Down
4 changes: 2 additions & 2 deletions Tests/JWKRSAEncodingTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import XCTest
class JWKRSAEncodingTests: CryptoTestCase {

func testPublicKeyEncoding() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"alg": "RS256",
"kid": "2011-04-29"
])
Expand All @@ -47,7 +47,7 @@ class JWKRSAEncodingTests: CryptoTestCase {
}

func testEncodingPublicKeyWithUnregisteredParameter() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"alg": "RS256",
"kid": "2011-04-29",
"breeze": "through"
Expand Down
8 changes: 4 additions & 4 deletions Tests/JWKRSAKeysTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import XCTest
class JWKRSAKeysTests: CryptoTestCase {

func testMergingDuplicateAdditionalParametersInPublicKey() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"kty": "wrongKty"
])

Expand Down Expand Up @@ -81,7 +81,7 @@ class JWKRSAKeysTests: CryptoTestCase {
}

func testPublicKeyKeyTypeIsPresent() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!)
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!)

XCTAssertEqual(jwk.keyType, .RSA)
XCTAssertEqual(jwk[JWKParameter.keyType.rawValue] ?? "", JWKKeyType.RSA.rawValue)
Expand All @@ -97,15 +97,15 @@ class JWKRSAKeysTests: CryptoTestCase {
}

func testSettingAndGettingAdditionalParameter() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"kid": "new on the block"
])

XCTAssertEqual(jwk["kid"] ?? "", "new on the block")
}

func testPublicKeyAllParametersArePresentInDict() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"kid": "new on the block",
"use": "test"
])
Expand Down
4 changes: 2 additions & 2 deletions Tests/JWKtoJSONTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import XCTest
class JWKtoJSONTests: CryptoTestCase {

func testJSONString() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"alg": "RS256",
"kid": "2011-04-29"
])
Expand All @@ -48,7 +48,7 @@ class JWKtoJSONTests: CryptoTestCase {
}

func testJSONData() {
let jwk = try! RSAPublicKey(publicKey: publicKey2048!, additionalParameters: [
let jwk = try! RSAPublicKey(publicKey: publicKeyAlice2048!, additionalParameters: [
"alg": "RS256",
"kid": "2011-04-29"
])
Expand Down
10 changes: 5 additions & 5 deletions Tests/JWSTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,26 +34,26 @@ class JWSTests: CryptoTestCase {
}

func testSignAndSerialize() {
guard publicKey2048 != nil, privateKey2048 != nil else {
guard publicKeyAlice2048 != nil, privateKeyAlice2048 != nil else {
XCTFail()
return
}

let header = JWSHeader(algorithm: .RS512)
let payload = Payload(message.data(using: .utf8)!)
let signer = Signer(signingAlgorithm: .RS512, privateKey: privateKey2048!)!
let signer = Signer(signingAlgorithm: .RS512, privateKey: privateKeyAlice2048!)!
let jws = try! JWS(header: header, payload: payload, signer: signer)
let compactSerializedJWS = jws.compactSerializedString

XCTAssertEqual(compactSerializedJWS, compactSerializedJWSConst)

let secondJWS = try! JWS(compactSerialization: compactSerializedJWS)

XCTAssertTrue(secondJWS.isValid(for: publicKey2048!))
XCTAssertTrue(secondJWS.isValid(for: publicKeyAlice2048!))
}

func testDeserializeFromCompactSerialization() {
guard privateKey2048 != nil else {
guard privateKeyAlice2048 != nil else {
XCTFail()
return
}
Expand All @@ -62,7 +62,7 @@ class JWSTests: CryptoTestCase {
XCTAssertEqual(String(data: jws.header.data(), encoding: .utf8), "{\"alg\":\"RS512\"}")
XCTAssertEqual(String(data: jws.payload.data(), encoding: .utf8), "The true sign of intelligence is not knowledge but imagination.")

let signer = Signer(signingAlgorithm: .RS512, privateKey: privateKey2048!)!
let signer = Signer(signingAlgorithm: .RS512, privateKey: privateKeyAlice2048!)!
let signature = try! signer.sign(header: JWSHeader(algorithm: .RS512), payload: Payload(message.data(using: .utf8)!))
XCTAssertEqual(jws.signature.data(), signature)
}
Expand Down
14 changes: 7 additions & 7 deletions Tests/JWSValidationTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class JWSValidationTests: CryptoTestCase {
func testIsValid() {
let jws = try! JWS(compactSerialization: compactSerializedJWSConst)

XCTAssertTrue(jws.isValid(for: publicKey2048!))
XCTAssertTrue(jws.isValid(for: publicKeyAlice2048!))
}

func testIsValidIsFalseForInvalidAlg() {
Expand All @@ -38,7 +38,7 @@ class JWSValidationTests: CryptoTestCase {

let jws = try! JWS(compactSerialization: malformedSerialization)

XCTAssertFalse(jws.isValid(for: publicKey2048!))
XCTAssertFalse(jws.isValid(for: publicKeyAlice2048!))
}

func testIsValidIsFalseForWrongSignature() {
Expand All @@ -47,7 +47,7 @@ class JWSValidationTests: CryptoTestCase {

let jws = try! JWS(compactSerialization: malformedSerialization)

XCTAssertFalse(jws.isValid(for: publicKey2048!))
XCTAssertFalse(jws.isValid(for: publicKeyAlice2048!))
}

func testIsValidIsFalseForWrongKey() {
Expand All @@ -59,13 +59,13 @@ class JWSValidationTests: CryptoTestCase {
func testValidatesDoesNotThrowForValidSignature() {
let jws = try! JWS(compactSerialization: compactSerializedJWSConst)

XCTAssertNoThrow(try jws.validate(with: publicKey2048!))
XCTAssertNoThrow(try jws.validate(with: publicKeyAlice2048!))
}

func testValidatesReturnsJWS() {
let jws = try! JWS(compactSerialization: compactSerializedJWSConst)

let validatedJWS = try! jws.validate(with: publicKey2048!)
let validatedJWS = try! jws.validate(with: publicKeyAlice2048!)

XCTAssertEqual(validatedJWS.compactSerializedString, compactSerializedJWSConst)
}
Expand All @@ -76,7 +76,7 @@ class JWSValidationTests: CryptoTestCase {

let jws = try! JWS(compactSerialization: malformedSerialization)

XCTAssertThrowsError(try jws.validate(with: publicKey2048!))
XCTAssertThrowsError(try jws.validate(with: publicKeyAlice2048!))
}

func testValidatesThrowsForWrongSignature() {
Expand All @@ -85,7 +85,7 @@ class JWSValidationTests: CryptoTestCase {

let jws = try! JWS(compactSerialization: malformedSerialization)

XCTAssertThrowsError(try jws.validate(with: publicKey2048!))
XCTAssertThrowsError(try jws.validate(with: publicKeyAlice2048!))
}

func testValidatesThrowsForWrongKey() {
Expand Down
Loading