From b13b026f565c1769b48ee33b92ccd1a2a5610f31 Mon Sep 17 00:00:00 2001 From: Nick McConnell Date: Wed, 28 Jul 2021 18:05:21 -0400 Subject: [PATCH] Fix linting from previous PR (#218) --- README.md | 2 +- Source/Key.swift | 2 +- Source/SwiftyRSA.swift | 10 ++--- Source/X509Certificate.swift | 26 ++++------- Tests/X509Tests.swift | 83 +++++++++++++++++++++--------------- 5 files changed, 64 insertions(+), 59 deletions(-) diff --git a/README.md b/README.md index 808b0cb..37e5a5a 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ SwiftyRSA ========= -**Maintainer(s):** [@starback](https://github.com/starback) +**Maintainer(s):** [@ikeith](https://github.com/ikeith) [![](https://img.shields.io/cocoapods/v/SwiftyRSA.svg)](https://cocoapods.org/pods/SwiftyRSA) ![](https://img.shields.io/badge/carthage-compatible-brightgreen.svg) diff --git a/Source/Key.swift b/Source/Key.swift index 25cd18c..2220cd2 100644 --- a/Source/Key.swift +++ b/Source/Key.swift @@ -9,7 +9,7 @@ import Foundation import Security -public protocol Key: class { +public protocol Key: AnyObject { var reference: SecKey { get } var originalData: Data? { get } diff --git a/Source/SwiftyRSA.swift b/Source/SwiftyRSA.swift index 7fbfba0..80f490c 100644 --- a/Source/SwiftyRSA.swift +++ b/Source/SwiftyRSA.swift @@ -3,7 +3,7 @@ // SwiftyRSA // // Created by Loïs Di Qual on 7/2/15. -// Contributions by Stchepinsky Nathan on 24/06/2021 +// // Copyright (c) 2015 Scoop Technologies, Inc. All rights reserved. // @@ -305,11 +305,11 @@ public enum SwiftyRSA { INTEGER -- public exponent */ - static func prependX509KeyHeader(keyData : Data) throws -> Data{ - if try keyData.isAnHeaderlessKey(){ - let x509certificate : Data = keyData.prependx509Header() + static func prependX509KeyHeader(keyData: Data) throws -> Data { + if try keyData.isAnHeaderlessKey() { + let x509certificate: Data = keyData.prependx509Header() return x509certificate - } else if try keyData.hasX509Header() { + } else if try keyData.hasX509Header() { return keyData } else { // invalideHeader throw SwiftyRSAError.x509CertificateFailed diff --git a/Source/X509Certificate.swift b/Source/X509Certificate.swift index 07191a7..2cb4f86 100644 --- a/Source/X509Certificate.swift +++ b/Source/X509Certificate.swift @@ -8,23 +8,20 @@ import Foundation - -/// /// Encoding/Decoding lengths as octets -/// private extension NSInteger { func encodedOctets() -> [CUnsignedChar] { // Short form if self < 128 { - return [CUnsignedChar(self)]; + return [CUnsignedChar(self)] } // Long form - let i = Int(log2(Double(self)) / 8 + 1) + let long = Int(log2(Double(self)) / 8 + 1) var len = self - var result: [CUnsignedChar] = [CUnsignedChar(i + 0x80)] + var result: [CUnsignedChar] = [CUnsignedChar(long + 0x80)] - for _ in 0..> 8 } @@ -48,9 +45,9 @@ private extension NSInteger { var result = UInt64(0) - for j in 1...octets { + for octet in 1...octets { result = (result << 8) - result = result + UInt64(octetBytes[startIdx + j]) + result = result + UInt64(octetBytes[startIdx + octet]) } startIdx += 1 + octets @@ -59,9 +56,7 @@ private extension NSInteger { } } - - -public extension Data{ +public extension Data { // This code source come from Heimdall project https://github.com/henrinormak/Heimdall published under MIT Licence /// This method prepend the X509 header to a given public key @@ -96,7 +91,7 @@ public extension Data{ return result as Data } - func hasX509Header() throws -> Bool{ + func hasX509Header() throws -> Bool { let node: Asn1Parser.Node do { node = try Asn1Parser.parse(data: self) @@ -104,7 +99,6 @@ public extension Data{ throw SwiftyRSAError.asn1ParsingFailed } - // Ensure the raw data is an ASN1 sequence guard case .sequence(let nodes) = node else { return false @@ -134,8 +128,6 @@ public extension Data{ } // The 2sd child has to be a bit string containing a sequence of 2 int - - let last = nodes[1] if case .bitString(let secondChildSequence) = last { return try secondChildSequence.isAnHeaderlessKey() @@ -144,7 +136,7 @@ public extension Data{ } } - func isAnHeaderlessKey() throws -> Bool{ + func isAnHeaderlessKey() throws -> Bool { let node: Asn1Parser.Node do { node = try Asn1Parser.parse(data: self) diff --git a/Tests/X509Tests.swift b/Tests/X509Tests.swift index 57652a8..f6f8365 100644 --- a/Tests/X509Tests.swift +++ b/Tests/X509Tests.swift @@ -19,36 +19,40 @@ class X509CertificateTests: XCTestCase { /// Verify the ASN1 sruc with the ASN1 parser (private key) func testX509CertificateValidityPrivateKey() throws { - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + guard let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } + + let privateKeyX509: Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) XCTAssertTrue(try privateKeyX509.hasX509Header()) } /// Test the function in charge of verifying if a key is headerless or not func testHeaderlessKeyVerificationFunc() throws { - let publicKeyData : Data = try! publicKey.data() - let privateKeyData : Data = try! privateKey.data() + guard let publicKeyData = try? publicKey.data(), let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } // Correct key XCTAssertTrue(try publicKeyData.isAnHeaderlessKey()) XCTAssertTrue(try privateKeyData.isAnHeaderlessKey()) // Example of incorrect key (here with a X509 header) - let publicKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + let publicKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) XCTAssertFalse(try publicKeyX509.isAnHeaderlessKey()) XCTAssertFalse(try privateKeyX509.isAnHeaderlessKey()) } /// Verify that the header added corresponds to the X509 key - func testX509HeaderVerificationPublicKey() throws{ + func testX509HeaderVerificationPublicKey() throws { // Generated on https://www.devglan.com/online-tools/rsa-encryption-decryption which uses X.509 certificate for public key guard let path = bundle.path(forResource: "swiftyrsa-public-base64-X509-format", ofType: "txt") else { return XCTFail("file not found in bundle") } let str = try String(contentsOf: URL(fileURLWithPath: path), encoding: .utf8) - if let publicKey = try? PublicKey(base64Encoded: str){ // Creating a public key strip the X509 header + if let publicKey = try? PublicKey(base64Encoded: str) { // Creating a public key strip the X509 header let publicKey509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKey.data()) let publicKey509Base64 = publicKey509.base64EncodedString() XCTAssertEqual(publicKey509Base64, str) @@ -59,33 +63,40 @@ class X509CertificateTests: XCTestCase { /// Test if the key's format is correct with the hasX509Header func func testX509KeyHeader() throws { - let publicKeyData : Data = try! publicKey.data() - let publicKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + guard let publicKeyData = try? publicKey.data(), let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } + + let publicKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) XCTAssertTrue(try publicKeyX509.hasX509Header()) XCTAssertTrue(try privateKeyX509.hasX509Header()) } /// Verify if the X509 header can be stripped - func testStripX509HeaderPrivateKey() throws{ - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + func testStripX509HeaderPrivateKey() throws { + guard let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } - let privateKeyStripped : Data = try! SwiftyRSA.stripKeyHeader(keyData: privateKeyX509) + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + + let privateKeyStripped = try SwiftyRSA.stripKeyHeader(keyData: privateKeyX509) XCTAssertEqual(privateKeyData, privateKeyStripped) } /// Test if a key with X509 header can encrypt and decrypt a given simple message - func testEncryptionDecryptionSimple() throws{ - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) - let publicKeyData : Data = try! publicKey.data() - let publicKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + func testEncryptionDecryptionSimple() throws { + guard let publicKeyData = try? publicKey.data(), let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } - let clear : String = "Hello world !" - let clearMessage : ClearMessage = try ClearMessage(string: clear, using: .utf8) + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + let publicKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + + let clear = "Hello world !" + let clearMessage = try ClearMessage(string: clear, using: .utf8) let encrypted = try clearMessage.encrypted(with: PublicKey(data: publicKeyX509), padding: .PKCS1) let decrypted = try encrypted.decrypted(with: PrivateKey(data: privateKeyX509), padding: .PKCS1) @@ -94,14 +105,16 @@ class X509CertificateTests: XCTestCase { } /// Test if a key with X509 header can encrypt and decrypt a given long message - func testEncryptionDecryptionLong() throws{ - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) - let publicKeyData : Data = try! publicKey.data() - let publicKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + func testEncryptionDecryptionLong() throws { + guard let publicKeyData = try? publicKey.data(), let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } + + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + let publicKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) let clear = [String](repeating: "a", count: 9999).joined(separator: "") - let clearMessage : ClearMessage = try ClearMessage(string: clear, using: .utf8) + let clearMessage = try ClearMessage(string: clear, using: .utf8) let encrypted = try clearMessage.encrypted(with: PublicKey(data: publicKeyX509), padding: .PKCS1) let decrypted = try encrypted.decrypted(with: PrivateKey(data: privateKeyX509), padding: .PKCS1) @@ -111,10 +124,12 @@ class X509CertificateTests: XCTestCase { /// Test if a key with X509 header can encrypt and decrypt a given random message func testEncryptionDecryptionRandomBytes() throws { - let privateKeyData : Data = try! privateKey.data() - let privateKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) - let publicKeyData : Data = try! publicKey.data() - let publicKeyX509 : Data = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) + guard let publicKeyData = try? publicKey.data(), let privateKeyData = try? privateKey.data() else { + return XCTFail("invalid data") + } + + let privateKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: privateKeyData) + let publicKeyX509 = try SwiftyRSA.prependX509KeyHeader(keyData: publicKeyData) let data = TestUtils.randomData(count: 2048) let clearMessage = ClearMessage(data: data) @@ -124,6 +139,4 @@ class X509CertificateTests: XCTestCase { XCTAssertEqual(decrypted.data, data) } - - }