Skip to content

Commit

Permalink
Add Travis support and add Swift Lint fixes (#20)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
KyeMaloy97 authored Aug 1, 2018
1 parent fd85dd8 commit c514227
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 42 deletions.
2 changes: 1 addition & 1 deletion .swift-version
Original file line number Diff line number Diff line change
@@ -1 +1 @@
4.0
4.1.2
3 changes: 2 additions & 1 deletion .swiftlint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -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
20 changes: 10 additions & 10 deletions Sources/CryptorRSA/CryptorRSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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))
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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
Expand All @@ -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)))

Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
8 changes: 4 additions & 4 deletions Sources/CryptorRSA/CryptorRSAKey.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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<String.Index>(start..<end)
let range = start..<end

let thisKey = pemString[range]

Expand Down
2 changes: 1 addition & 1 deletion Sources/CryptorRSA/CryptorRSAUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -316,7 +316,7 @@ extension String {
for character in self {
collectedCharacters.append(character)
count += 1
if (count == length) {
if count == length {
// Reached the desired length
count = 0
result.append(String(collectedCharacters))
Expand Down
115 changes: 90 additions & 25 deletions Tests/CryptorRSATests/CryptorRSATests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,11 @@ class CryptorRSATests: XCTestCase {

func test_publicKeysFromComplexPEMFileWorksCorrectly() {

let input = CryptorRSATests.pemKeyString(name: "multiple-keys-testcase")
guard let input = CryptorRSATests.pemKeyString(name: "multiple-keys-testcase") else {
XCTFail()
return
}

let keys = CryptorRSA.PublicKey.publicKeys(withPEM: input)
XCTAssertEqual(keys.count, 9)

Expand Down Expand Up @@ -308,9 +312,9 @@ class CryptorRSATests: XCTestCase {
}

// MARK: Encyption/Decryption Tests
let publicKey: CryptorRSA.PublicKey = try! CryptorRSATests.publicKey(name: "public")
let privateKey: CryptorRSA.PrivateKey = try! CryptorRSATests.privateKey(name: "private")
let publicKey: CryptorRSA.PublicKey? = try? CryptorRSATests.publicKey(name: "public")
let privateKey: CryptorRSA.PrivateKey? = try? CryptorRSATests.privateKey(name: "private")

func test_simpleEncryption() throws {

Expand All @@ -326,7 +330,13 @@ class CryptorRSATests: XCTestCase {
print("Testing algorithm: \(name)")
let str = "Plain Text"
let plainText = try CryptorRSA.createPlaintext(with: str, using: .utf8)


guard let publicKey = self.publicKey,
let privateKey = self.privateKey else {
XCTFail("Could not find key")
return
}

let encrypted = try plainText.encrypted(with: publicKey, algorithm: algorithm)
XCTAssertNotNil(encrypted)
let decrypted = try encrypted!.decrypted(with: privateKey, algorithm: algorithm)
Expand All @@ -352,6 +362,12 @@ class CryptorRSATests: XCTestCase {
let str = [String](repeating: "a", count: 9999).joined(separator: "")
let plainText = try CryptorRSA.createPlaintext(with: str, using: .utf8)

guard let publicKey = self.publicKey,
let privateKey = self.privateKey else {
XCTFail("Could not find key")
return
}

let encrypted = try plainText.encrypted(with: publicKey, algorithm: algorithm)
XCTAssertNotNil(encrypted)
let decrypted = try encrypted!.decrypted(with: privateKey, algorithm: algorithm)
Expand All @@ -377,6 +393,12 @@ class CryptorRSATests: XCTestCase {
let data = CryptorRSATests.randomData(count: 2048)
let plainData = CryptorRSA.createPlaintext(with: data)

guard let publicKey = self.publicKey,
let privateKey = self.privateKey else {
XCTFail("Could not find key")
return
}

let encrypted = try plainData.encrypted(with: publicKey, algorithm: algorithm)
XCTAssertNotNil(encrypted)
let decrypted = try encrypted!.decrypted(with: privateKey, algorithm: algorithm)
Expand All @@ -395,6 +417,12 @@ class CryptorRSATests: XCTestCase {
(.sha256, ".sha256"),
(.sha384, ".sha384"),
(.sha512, ".sha512")]
guard let publicKey = self.publicKey,
let privateKey = self.privateKey else {
XCTFail("Could not find key")
return
}

// Test all the algorithms available...
for (algorithm, name) in algorithms {

Expand All @@ -416,6 +444,13 @@ class CryptorRSATests: XCTestCase {
(.sha256, ".sha256"),
(.sha384, ".sha384"),
(.sha512, ".sha512")]

guard let publicKey = self.publicKey,
let privateKey = self.privateKey else {
XCTFail("Could not find key")
return
}

// Test all the algorithms available...
for (algorithm, name) in algorithms {

Expand Down Expand Up @@ -445,11 +480,14 @@ class CryptorRSATests: XCTestCase {
-----END PUBLIC KEY-----
"""

let tokenPublicKey = try? CryptorRSA.createPublicKey(withPEM: certificatePEM)
XCTAssertNotNil(tokenPublicKey)
XCTAssertTrue(tokenPublicKey!.type == .publicType)
guard let tokenPublicKey = try? CryptorRSA.createPublicKey(withPEM: certificatePEM) else {
XCTFail("Public ket not made")
return
}

XCTAssertTrue(tokenPublicKey.type == .publicType)

let token = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpPU0UiLCJraWQiOiJhcHBJZC0xNTA0Njg1OTYxMDAwIn0.eyJpc3MiOiJhcHBpZC1vYXV0aC5uZy5ibHVlbWl4Lm5ldCIsImF1ZCI6IjUzOGU4NTI2YTcwNDdjMWM5ZTEzNDZhYzQ1MjA2NmQxYmE1ZmQzNTEiLCJleHAiOjE1MTgxOTkzNTgsInRlbmFudCI6ImQ3YmMzMjJjLWIyMjQtNDFjMS05MWVhLWZjNjM4YWUyYWQ0ZCIsImlhdCI6MTUxODE5NTc1OC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kZXZpY2Vfb3NfdmVyc2lvbiI6IjExLjIifX0.RTK5wV0b0mtbRayKg9IdGCnGXoA7bn4Gdx-YIQjaaELWJwpla2x1R1hMvL5It-MKMt_pyejzkdoTKR3v_VF4IMwnBWz83d0u6TVs28TbrgHAkXy6sAypIfEKc4gLOSHXkUBYREH2pbJSguxZNTwqKe_PKRSYtG0QrtffPUsESfnGkdfHdUsSigMjX5s5En8fLCGiNSQF2uyYREDFE6T0w5P3W5MR_Scloyhik1q7nv91PzlJ6Rn9_0F12zjzvPMTt7bobTdokaFVPcqjFWHJc4YCw-bzdBCxtzHxf3oVXeJzCzPNb_nOehZu-u7ue54NbYwcoZ_bokmsjCnQbFE_QA"

let tokenParts = token.split(separator: ".")
// JWT token should have 3 parts
Expand All @@ -467,16 +505,19 @@ class CryptorRSATests: XCTestCase {
// JWT also does base64url encoding, so make the proper replacements so its proper base64 encoding
sig = sig.replacingOccurrences(of: "-", with: "+")
sig = sig.replacingOccurrences(of: "_", with: "/")
let sigData = Data(base64Encoded: sig)!
XCTAssertNotNil(sigData)

guard let sigData = Data(base64Encoded: sig) else {
XCTFail("Unable to create Signature Data")
return
}

let message = CryptorRSA.createPlaintext(with: messageData)
XCTAssertNotNil(message)

let signature = CryptorRSA.createSigned(with: sigData)
XCTAssertNotNil(signature)

let verificationResult = try message.verify(with: tokenPublicKey!, signature: signature, algorithm: .sha256)
let verificationResult = try message.verify(with: tokenPublicKey, signature: signature, algorithm: .sha256)
XCTAssertTrue(verificationResult)
}

Expand All @@ -486,37 +527,61 @@ class CryptorRSATests: XCTestCase {
let description: String
}

static public func pemKeyString(name: String) -> 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)
}

Expand Down

0 comments on commit c514227

Please sign in to comment.