From c514227ecbc7ab9db91380b8036b20b46a2d8583 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 1 Aug 2018 12:23:56 +0100 Subject: [PATCH] Add Travis support and add Swift Lint fixes (#20) * added .travis.yml * Updated for Swift 4.2 Range type changes * fixed syntax issues for Swift linter * typo * fixed more lint errors * more fixes * added more guards * updated Swift verison --- .swift-version | 2 +- .swiftlint.yml | 3 +- .travis.yml | 41 +++++++ Sources/CryptorRSA/CryptorRSA.swift | 20 ++-- Sources/CryptorRSA/CryptorRSAKey.swift | 8 +- Sources/CryptorRSA/CryptorRSAUtilities.swift | 2 +- Tests/CryptorRSATests/CryptorRSATests.swift | 115 +++++++++++++++---- 7 files changed, 149 insertions(+), 42 deletions(-) create mode 100644 .travis.yml diff --git a/.swift-version b/.swift-version index 5186d07..4d0dcda 100644 --- a/.swift-version +++ b/.swift-version @@ -1 +1 @@ -4.0 +4.1.2 diff --git a/.swiftlint.yml b/.swiftlint.yml index 1fe1bfa..1393797 100644 --- a/.swiftlint.yml +++ b/.swiftlint.yml @@ -2,10 +2,11 @@ included: - Sources - Tests disabled_rules: + - nesting - trailing_newline - force_cast - function_body_length - - variable_name + - identifier_name - line_length - trailing_whitespace - type_name diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..0cab965 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,41 @@ +# Travis CI build file for BlueRSA + +# whitelist (branches that should be built) +branches: + only: + - master + - develop + - /^issue.*$/ + +matrix: + include: + - os: linux + dist: trusty + sudo: required + env: SWIFT_SNAPSHOT=4.0.3 + - os: linux + dist: trusty + sudo: required + - os: linux + dist: trusty + sudo: required + env: SWIFT_SNAPSHOT=swift-4.2-DEVELOPMENT-SNAPSHOT-2018-06-23-a + - os: linux + dist: trusty + sudo: required + services: docker + env: DOCKER_IMAGE=ubuntu:16.04 + - os: osx + osx_image: xcode9.2 + sudo: required + env: SWIFT_SNAPSHOT=4.0.3 + - os: osx + osx_image: xcode9.4 + sudo: required + +before_install: + - git clone https://github.com/IBM-Swift/Package-Builder.git + +script: + - openssl version + - ./Package-Builder/build-package.sh -projectDir $TRAVIS_BUILD_DIR diff --git a/Sources/CryptorRSA/CryptorRSA.swift b/Sources/CryptorRSA/CryptorRSA.swift index 6f59f4c..454262d 100644 --- a/Sources/CryptorRSA/CryptorRSA.swift +++ b/Sources/CryptorRSA/CryptorRSA.swift @@ -224,7 +224,7 @@ public class CryptorRSA { #if os(Linux) // Convert RSA key to EVP - var evp_key = EVP_PKEY_new() + var evp_key = EVP_PKEY_new() var rc = EVP_PKEY_set1_RSA(evp_key, key.reference) guard rc == 1 else { let source = "Couldn't create key reference from key data" @@ -275,7 +275,7 @@ public class CryptorRSA { throw Error(code: ERR_ENCRYPTION_FAILED, reason: reason) } - throw Error(code: ERR_ENCRYPTION_FAILED , reason: source + ": No OpenSSL error reported.") + throw Error(code: ERR_ENCRYPTION_FAILED, reason: source + ": No OpenSSL error reported.") } // EVP_SealUpdate is a complex macros and therefore the compiler doesnt @@ -292,9 +292,9 @@ public class CryptorRSA { throw Error(code: ERR_ENCRYPTION_FAILED, reason: reason) } - throw Error(code: ERR_ENCRYPTION_FAILED , reason: source + ": No OpenSSL error reported.") + throw Error(code: ERR_ENCRYPTION_FAILED, reason: source + ": No OpenSSL error reported.") } - encLength = encLength + processedLength + encLength += processedLength let cipher = Data(bytes: encrypted, count: Int(encLength)) let ekFinal = Data(bytes: ek!, count: Int(encKeyLength)) @@ -347,7 +347,7 @@ public class CryptorRSA { #if os(Linux) // Convert RSA key to EVP - var evp_key = EVP_PKEY_new() + var evp_key = EVP_PKEY_new() var status = EVP_PKEY_set1_RSA(evp_key, key.reference) guard status == 1 else { let source = "Couldn't create key reference from key data" @@ -402,7 +402,7 @@ public class CryptorRSA { throw Error(code: ERR_DECRYPTION_FAILED, reason: reason) } - throw Error(code: ERR_DECRYPTION_FAILED , reason: source + ": No OpenSSL error reported.") + throw Error(code: ERR_DECRYPTION_FAILED, reason: source + ": No OpenSSL error reported.") } // EVP_OpenUpdate is a complex macros and therefore the compiler doesnt @@ -419,9 +419,9 @@ public class CryptorRSA { throw Error(code: ERR_DECRYPTION_FAILED, reason: reason) } - throw Error(code: ERR_DECRYPTION_FAILED , reason: source + ": No OpenSSL error reported.") + throw Error(code: ERR_DECRYPTION_FAILED, reason: source + ": No OpenSSL error reported.") } - decMsgLen = decMsgLen + processedLen + decMsgLen += processedLen return PlaintextData(with: Data(bytes: decrypted, count: Int(decMsgLen))) @@ -479,7 +479,7 @@ public class CryptorRSA { } // convert RSA key to EVP - let evp_key = EVP_PKEY_new() + let evp_key = EVP_PKEY_new() var rc = EVP_PKEY_set1_RSA(evp_key, key.reference) guard rc == 1 else { let source = "Couldn't create key reference from key data" @@ -579,7 +579,7 @@ public class CryptorRSA { } // convert RSA key to EVP - let evp_key = EVP_PKEY_new() + let evp_key = EVP_PKEY_new() var rc = EVP_PKEY_set1_RSA(evp_key, key.reference) guard rc == 1 else { let source = "Couldn't create key reference from key data" diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index 057bd39..2218bbc 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -357,19 +357,19 @@ public extension CryptorRSA { } let cert = PEM_read_bio_X509(certbio, nil, nil, nil) - if (cert == nil) { + if cert == nil { print("Error loading cert into memory\n") throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Error loading cert into memory.") } // Extract the certificate's public key data. let evp_key = X509_get_pubkey(cert) - if ( evp_key == nil) { + if evp_key == nil { throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Error getting public key from certificate") } let key = EVP_PKEY_get1_RSA( evp_key) - if ( key == nil) { + if key == nil { throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Error getting public key from certificate") } defer { @@ -693,7 +693,7 @@ public extension CryptorRSA { let start = pemString.index(pemString.startIndex, offsetBy: match.location) let end = pemString.index(start, offsetBy: match.length) - let range = Range(start.. String { + static public func pemKeyString(name: String) -> String? { - let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") + guard let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") else { + XCTFail("Could not create pemKeyString") + return nil + } + XCTAssertNotNil(path) - return (try! String(contentsOfFile: path!.path, encoding: String.Encoding.utf8)) + guard let returnValue: String = try? String(contentsOfFile: path.path, encoding: String.Encoding.utf8) else { + XCTFail("Could not create returnValue") + return nil + } + + XCTAssertNotNil(returnValue) + + return returnValue } - static public func derKeyData(name: String) -> Data { + static public func derKeyData(name: String) -> Data? { - let path = CryptorRSATests.getFilePath(for: name, ofType: "der") - XCTAssertNotNil(path) + guard let path = CryptorRSATests.getFilePath(for: name, ofType: "der") else { + XCTFail("Could not get file path") + return nil + } + + guard let returnValue: Data = try? Data(contentsOf: URL(fileURLWithPath: path.path)) else { + XCTFail("Could not create derKeyData") + return nil + } - return (try! Data(contentsOf: URL(fileURLWithPath: path!.path))) + return returnValue } + + enum MyError : Error { + case invalidPath() + } static public func publicKey(name: String) throws -> CryptorRSA.PublicKey { - let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") - XCTAssertNotNil(path) + guard let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") else { + throw MyError.invalidPath() + } - let pemString = try String(contentsOf: path!, encoding: String.Encoding.ascii) + let pemString = try String(contentsOf: path, encoding: String.Encoding.ascii) return try CryptorRSA.createPublicKey(withPEM: pemString) } static public func privateKey(name: String) throws -> CryptorRSA.PrivateKey { - let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") - XCTAssertNotNil(path) + guard let path = CryptorRSATests.getFilePath(for: name, ofType: "pem") else { + throw MyError.invalidPath() + } - let pemString = try String(contentsOf: path!, encoding: String.Encoding.ascii) + let pemString = try String(contentsOf: path, encoding: String.Encoding.ascii) return try CryptorRSA.createPrivateKey(withPEM: pemString) }