From 2eeb894cc4fa6f2448d2a5d5aa27e1a6599c0415 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 8 Aug 2018 11:06:37 +0100 Subject: [PATCH 1/6] fixed warnings --- Sources/CryptorRSA/CryptorRSADigest.swift | 2 +- Sources/CryptorRSA/CryptorRSAKey.swift | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Sources/CryptorRSA/CryptorRSADigest.swift b/Sources/CryptorRSA/CryptorRSADigest.swift index 5cbe04c..cef73ea 100644 --- a/Sources/CryptorRSA/CryptorRSADigest.swift +++ b/Sources/CryptorRSA/CryptorRSADigest.swift @@ -230,7 +230,7 @@ public extension Data { #else - public var engine: (_ data: UnsafeRawPointer, _ len: CC_LONG, _ md: UnsafeMutablePointer) -> UnsafeMutablePointer! { + public var engine: (_ data: UnsafeRawPointer, _ len: CC_LONG, _ md: UnsafeMutablePointer) -> UnsafeMutablePointer? { switch self { diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index 2218bbc..03e9c5f 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -683,7 +683,7 @@ public extension CryptorRSA { range: all ) - let keys = matches.flatMap { result -> PublicKey? in + let keys = matches.compactMap { result -> PublicKey? in #if swift(>=4.0) let match = result.range(at: 1) From c1cc9331a322465114b5438acacf2208d6bad836 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 8 Aug 2018 11:26:39 +0100 Subject: [PATCH 2/6] added conditional compilation for Swift < 4.1 --- Sources/CryptorRSA/CryptorRSAKey.swift | 21 ++++++++++++++++++++- 1 file changed, 20 insertions(+), 1 deletion(-) diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index 03e9c5f..0a807d0 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -682,7 +682,7 @@ public extension CryptorRSA { options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all ) - + #if swift(>=4.1) let keys = matches.compactMap { result -> PublicKey? in #if swift(>=4.0) @@ -699,6 +699,25 @@ public extension CryptorRSA { return try? CryptorRSA.createPublicKey(withPEM: String(thisKey)) } + #else + + let keys = matches.flatMap { result -> PublicKey? in + + #if swift(>=4.0) + let match = result.range(at: 1) + #else + let match = result.rangeAt( 1) + #endif + let start = pemString.index(pemString.startIndex, offsetBy: match.location) + let end = pemString.index(start, offsetBy: match.length) + + let range = start.. Date: Wed, 8 Aug 2018 11:46:14 +0100 Subject: [PATCH 3/6] removed check inside another check --- Sources/CryptorRSA/CryptorRSAKey.swift | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index 0a807d0..d8a996a 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -685,11 +685,7 @@ public extension CryptorRSA { #if swift(>=4.1) let keys = matches.compactMap { result -> PublicKey? in - #if swift(>=4.0) - let match = result.range(at: 1) - #else - let match = result.rangeAt( 1) - #endif + let match = result.range(at: 1) let start = pemString.index(pemString.startIndex, offsetBy: match.location) let end = pemString.index(start, offsetBy: match.length) @@ -703,11 +699,7 @@ public extension CryptorRSA { let keys = matches.flatMap { result -> PublicKey? in - #if swift(>=4.0) - let match = result.range(at: 1) - #else let match = result.rangeAt( 1) - #endif let start = pemString.index(pemString.startIndex, offsetBy: match.location) let end = pemString.index(start, offsetBy: match.length) From 31ea45ccd89ebbcf19880ddc7474e64a46448bb1 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 8 Aug 2018 11:56:12 +0100 Subject: [PATCH 4/6] fixed indenting --- Sources/CryptorRSA/CryptorRSAKey.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index d8a996a..1f816d2 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -682,7 +682,7 @@ public extension CryptorRSA { options: NSRegularExpression.MatchingOptions(rawValue: 0), range: all ) - #if swift(>=4.1) + #if swift(>=4.1) let keys = matches.compactMap { result -> PublicKey? in let match = result.range(at: 1) From c1cae5ba966e715746f06a020ad0ab7f9dd6a068 Mon Sep 17 00:00:00 2001 From: Kye Maloy Date: Wed, 8 Aug 2018 11:59:59 +0100 Subject: [PATCH 5/6] indentation fix --- Sources/CryptorRSA/CryptorRSAKey.swift | 449 ++++++++++++------------- 1 file changed, 224 insertions(+), 225 deletions(-) diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index 1f816d2..ec7c94d 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -20,9 +20,9 @@ // #if os(macOS) || os(iOS) || os(tvOS) || os(watchOS) - import CommonCrypto +import CommonCrypto #elseif os(Linux) - import OpenSSL +import OpenSSL #endif import Foundation @@ -36,11 +36,11 @@ public extension CryptorRSA { #if os(Linux) - typealias NativeKey = UnsafeMutablePointer + typealias NativeKey = UnsafeMutablePointer #else - typealias NativeKey = SecKey + typealias NativeKey = SecKey #endif @@ -58,15 +58,15 @@ public extension CryptorRSA { /// public class func createPublicKey(with data: Data) throws -> PublicKey { - #if os(Linux) - - let data = CryptorRSA.convertDerToPem(from: data, type: .publicType) - - #endif - - return try PublicKey(with: data) + #if os(Linux) + + let data = CryptorRSA.convertDerToPem(from: data, type: .publicType) + + #endif + + return try PublicKey(with: data) } - + /// /// Creates a public key by extracting it from a certificate. /// @@ -77,20 +77,20 @@ public extension CryptorRSA { /// public class func createPublicKey(extractingFrom data: Data) throws -> PublicKey { - #if !os(Linux) - - // Extact the data as a base64 string... - let str = String(data: data, encoding: .utf8) - guard let tmp = str else { - - throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Unable to create certificate from certificate data, incorrect format.") - } - - // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers... - let base64 = try CryptorRSA.base64String(for: tmp) - let data = Data(base64Encoded: base64)! - - #endif + #if !os(Linux) + + // Extact the data as a base64 string... + let str = String(data: data, encoding: .utf8) + guard let tmp = str else { + + throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Unable to create certificate from certificate data, incorrect format.") + } + + // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers... + let base64 = try CryptorRSA.base64String(for: tmp) + let data = Data(base64Encoded: base64)! + + #endif // Call the internal function to finish up... return try CryptorRSA.createPublicKey(data: data) @@ -111,15 +111,15 @@ public extension CryptorRSA { throw Error(code: ERR_INIT_PK, reason: "Couldn't decode base64 string") } - - #if os(Linux) - - // OpenSSL uses the PEM version when importing key... - data = CryptorRSA.convertDerToPem(from: data, type: .publicType) - - #endif - return try PublicKey(with: data) + #if os(Linux) + + // OpenSSL uses the PEM version when importing key... + data = CryptorRSA.convertDerToPem(from: data, type: .publicType) + + #endif + + return try PublicKey(with: data) } /// @@ -132,21 +132,21 @@ public extension CryptorRSA { /// public class func createPublicKey(withPEM pemString: String) throws -> PublicKey { - #if os(Linux) - - // OpenSSL takes the full PEM format... - let keyData = pemString.data(using: String.Encoding.utf8)! - - return try PublicKey(with: keyData) - - #else - - // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers - let base64String = try CryptorRSA.base64String(for: pemString) - - return try createPublicKey(withBase64: base64String) - - #endif + #if os(Linux) + + // OpenSSL takes the full PEM format... + let keyData = pemString.data(using: String.Encoding.utf8)! + + return try PublicKey(with: keyData) + + #else + + // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers + let base64String = try CryptorRSA.base64String(for: pemString) + + return try createPublicKey(withBase64: base64String) + + #endif } /// @@ -160,16 +160,16 @@ public extension CryptorRSA { /// public class func createPublicKey(withPEMNamed pemName: String, onPath path: String) throws -> PublicKey { - var certName = pemName - if !pemName.hasSuffix(PEM_SUFFIX) { - - certName = pemName.appending(PEM_SUFFIX) - } - - let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized - - let keyString = try String(contentsOf: fullPath, encoding: .utf8) - + var certName = pemName + if !pemName.hasSuffix(PEM_SUFFIX) { + + certName = pemName.appending(PEM_SUFFIX) + } + + let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized + + let keyString = try String(contentsOf: fullPath, encoding: .utf8) + return try createPublicKey(withPEM: keyString) } @@ -184,24 +184,24 @@ public extension CryptorRSA { /// public class func createPublicKey(withDERNamed derName: String, onPath path: String) throws -> PublicKey { - var certName = derName - if !derName.hasSuffix(DER_SUFFIX) { - - certName = derName.appending(DER_SUFFIX) - } - - let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized + var certName = derName + if !derName.hasSuffix(DER_SUFFIX) { + + certName = derName.appending(DER_SUFFIX) + } + + let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized let dataIn = try Data(contentsOf: fullPath) #if os(Linux) - - let data = CryptorRSA.convertDerToPem(from: dataIn, type: .publicType) - + + let data = CryptorRSA.convertDerToPem(from: dataIn, type: .publicType) + #else - - let data = dataIn - + + let data = dataIn + #endif return try PublicKey(with: data) @@ -218,29 +218,29 @@ public extension CryptorRSA { /// public class func createPublicKey(extractingFrom certName: String, onPath path: String) throws -> PublicKey { - var certNameFull = certName - if !certName.hasSuffix(CER_SUFFIX) { - - certNameFull = certName.appending(CER_SUFFIX) - } - + var certNameFull = certName + if !certName.hasSuffix(CER_SUFFIX) { + + certNameFull = certName.appending(CER_SUFFIX) + } + //let fullPath = path.appending(certName) - let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certNameFull) ).standardized + let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certNameFull) ).standardized // Import the data from the file... - #if os(Linux) - - // In OpenSSL, we can just get the data and don't have to worry about stripping off headers etc. - let data = try Data(contentsOf: fullPath) - - #else - - // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers... - let tmp = try String(contentsOf: fullPath, encoding: .utf8) - let base64 = try CryptorRSA.base64String(for: tmp) - let data = Data(base64Encoded: base64)! - - #endif + #if os(Linux) + + // In OpenSSL, we can just get the data and don't have to worry about stripping off headers etc. + let data = try Data(contentsOf: fullPath) + + #else + + // Get the Base64 representation of the PEM encoded string after stripping off the PEM markers... + let tmp = try String(contentsOf: fullPath, encoding: .utf8) + let base64 = try CryptorRSA.base64String(for: tmp) + let data = Data(base64Encoded: base64)! + + #endif return try CryptorRSA.createPublicKey(data: data) } @@ -285,13 +285,13 @@ public extension CryptorRSA { let dataIn = try Data(contentsOf: URL(fileURLWithPath: path)) #if os(Linux) - - let data = CryptorRSA.convertDerToPem(from: dataIn, type: .publicType) - + + let data = CryptorRSA.convertDerToPem(from: dataIn, type: .publicType) + #else - - let data = dataIn - + + let data = dataIn + #endif return try PublicKey(with: data) @@ -333,77 +333,77 @@ public extension CryptorRSA { internal class func createPublicKey(data: Data) throws -> PublicKey { #if os(Linux) + + let certbio = BIO_new(BIO_s_mem()) + defer { + BIO_free(certbio) + } + + // Move the key data to BIO + try data.withUnsafeBytes() { (buffer: UnsafePointer) in + + let len = BIO_write(certbio, buffer, Int32(data.count)) + guard len != 0 else { + let source = "Couldn't create BIO reference from key data" + if let reason = CryptorRSA.getLastError(source: source) { + + throw Error(code: ERR_ADD_KEY, reason: reason) + } + throw Error(code: ERR_ADD_KEY, reason: source + ": No OpenSSL error reported.") + } - let certbio = BIO_new(BIO_s_mem()) - defer { - BIO_free(certbio) - } - - // Move the key data to BIO - try data.withUnsafeBytes() { (buffer: UnsafePointer) in - - let len = BIO_write(certbio, buffer, Int32(data.count)) - guard len != 0 else { - let source = "Couldn't create BIO reference from key data" - if let reason = CryptorRSA.getLastError(source: source) { - - throw Error(code: ERR_ADD_KEY, reason: reason) - } - throw Error(code: ERR_ADD_KEY, reason: source + ": No OpenSSL error reported.") - } - - // The below is equivalent of BIO_flush... - BIO_ctrl(certbio, BIO_CTRL_FLUSH, 0, nil) - } - let cert = PEM_read_bio_X509(certbio, nil, nil, 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 { - 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 { - throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Error getting public key from certificate") - } - defer { -// RSA_free(key) - EVP_PKEY_free(evp_key) - } - + // The below is equivalent of BIO_flush... + BIO_ctrl(certbio, BIO_CTRL_FLUSH, 0, nil) + } + let cert = PEM_read_bio_X509(certbio, nil, nil, 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 { + 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 { + throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Error getting public key from certificate") + } + defer { + // RSA_free(key) + EVP_PKEY_free(evp_key) + } + #else - // Create a DER-encoded X.509 certificate object from the DER data... - let certificateData = SecCertificateCreateWithData(nil, data as CFData) - guard let certData = certificateData else { + // Create a DER-encoded X.509 certificate object from the DER data... + let certificateData = SecCertificateCreateWithData(nil, data as CFData) + guard let certData = certificateData else { - throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Unable to create certificate from certificate data.") - } + throw Error(code: ERR_CREATE_CERT_FAILED, reason: "Unable to create certificate from certificate data.") + } - // Now extract the public key from it... - var key: SecKey? = nil - let status: OSStatus = withUnsafeMutablePointer(to: &key) { ptr in - - // Retrieves the public key from a certificate... - SecCertificateCopyPublicKey(certData, UnsafeMutablePointer(ptr)) - } - if status != errSecSuccess || key == nil { + // Now extract the public key from it... + var key: SecKey? = nil + let status: OSStatus = withUnsafeMutablePointer(to: &key) { ptr in - throw Error(code: ERR_EXTRACT_PUBLIC_KEY_FAILED, reason: "Unable to extract public key from data.") - } + // Retrieves the public key from a certificate... + SecCertificateCopyPublicKey(certData, UnsafeMutablePointer(ptr)) + } + if status != errSecSuccess || key == nil { + throw Error(code: ERR_EXTRACT_PUBLIC_KEY_FAILED, reason: "Unable to extract public key from data.") + } + #endif - + return PublicKey(with: key!) } - + // MARK: -- Private Key Creation /// @@ -447,21 +447,21 @@ public extension CryptorRSA { /// public class func createPrivateKey(withPEM pemString: String) throws -> PrivateKey { - #if os(Linux) - - // OpenSSL takes the full PEM format... - let keyData = pemString.data(using: String.Encoding.utf8)! - - return try PrivateKey(with: keyData) - - #else - - // SecKey needs the PEM format stripped of the header info and converted to base64... - let base64String = try CryptorRSA.base64String(for: pemString) - - return try CryptorRSA.createPrivateKey(withBase64: base64String) - - #endif + #if os(Linux) + + // OpenSSL takes the full PEM format... + let keyData = pemString.data(using: String.Encoding.utf8)! + + return try PrivateKey(with: keyData) + + #else + + // SecKey needs the PEM format stripped of the header info and converted to base64... + let base64String = try CryptorRSA.base64String(for: pemString) + + return try CryptorRSA.createPrivateKey(withBase64: base64String) + + #endif } /// @@ -475,12 +475,12 @@ public extension CryptorRSA { /// public class func createPrivateKey(withPEMNamed pemName: String, onPath path: String) throws -> PrivateKey { - var certName = pemName - if !pemName.hasSuffix(PEM_SUFFIX) { - - certName = pemName.appending(PEM_SUFFIX) - } - let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized + var certName = pemName + if !pemName.hasSuffix(PEM_SUFFIX) { + + certName = pemName.appending(PEM_SUFFIX) + } + let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized let keyString = try String(contentsOf: fullPath, encoding: .utf8) @@ -498,23 +498,23 @@ public extension CryptorRSA { /// public class func createPrivateKey(withDERNamed derName: String, onPath path: String) throws -> PrivateKey { - var certName = derName - if !derName.hasSuffix(DER_SUFFIX) { - - certName = derName.appending(DER_SUFFIX) - } - let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized - + var certName = derName + if !derName.hasSuffix(DER_SUFFIX) { + + certName = derName.appending(DER_SUFFIX) + } + let fullPath = URL(fileURLWithPath: #file).appendingPathComponent( path.appending(certName) ).standardized + let dataIn = try Data(contentsOf: fullPath) #if os(Linux) - - let data = CryptorRSA.convertDerToPem(from: dataIn, type: .privateType) - + + let data = CryptorRSA.convertDerToPem(from: dataIn, type: .privateType) + #else - - let data = dataIn - + + let data = dataIn + #endif return try PrivateKey(with: data) @@ -560,9 +560,9 @@ public extension CryptorRSA { let dataIn = try Data(contentsOf: URL(fileURLWithPath: path)) #if os(Linux) - let data = CryptorRSA.convertDerToPem(from: dataIn, type: .privateType) + let data = CryptorRSA.convertDerToPem(from: dataIn, type: .privateType) #else - let data = dataIn + let data = dataIn #endif return try PrivateKey(with: data) @@ -609,12 +609,12 @@ public extension CryptorRSA { internal init(with data: Data, type: KeyType) throws { self.type = type - - // On macOS, we need to strip off the X509 header if it exists... + + // On macOS, we need to strip off the X509 header if it exists... #if !os(Linux) - - let data = try CryptorRSA.stripX509CertificateHeader(for: data) - + + let data = try CryptorRSA.stripX509CertificateHeader(for: data) + #endif reference = try CryptorRSA.createKey(from: data, type: type) @@ -677,15 +677,15 @@ public extension CryptorRSA { length: pemString.count ) - let matches = publicKeyRegexp.matches( - in: pemString, - options: NSRegularExpression.MatchingOptions(rawValue: 0), - range: all - ) + let matches = publicKeyRegexp.matches( + in: pemString, + options: NSRegularExpression.MatchingOptions(rawValue: 0), + range: all + ) #if swift(>=4.1) - let keys = matches.compactMap { result -> PublicKey? in + let keys = matches.compactMap { result -> PublicKey? in - let match = result.range(at: 1) + let match = result.range(at: 1) let start = pemString.index(pemString.startIndex, offsetBy: match.location) let end = pemString.index(start, offsetBy: match.length) @@ -694,22 +694,21 @@ public extension CryptorRSA { let thisKey = pemString[range] return try? CryptorRSA.createPublicKey(withPEM: String(thisKey)) - } - #else - - let keys = matches.flatMap { result -> PublicKey? in - - let match = result.rangeAt( 1) - let start = pemString.index(pemString.startIndex, offsetBy: match.location) - let end = pemString.index(start, offsetBy: match.length) - - let range = start.. PublicKey? in + + let match = result.rangeAt( 1) + let start = pemString.index(pemString.startIndex, offsetBy: match.location) + let end = pemString.index(start, offsetBy: match.length) + + let range = start.. Date: Wed, 8 Aug 2018 12:59:42 +0100 Subject: [PATCH 6/6] fixed error --- Sources/CryptorRSA/CryptorRSAKey.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/CryptorRSA/CryptorRSAKey.swift b/Sources/CryptorRSA/CryptorRSAKey.swift index ec7c94d..9f863b5 100644 --- a/Sources/CryptorRSA/CryptorRSAKey.swift +++ b/Sources/CryptorRSA/CryptorRSAKey.swift @@ -698,7 +698,7 @@ public extension CryptorRSA { #else let keys = matches.flatMap { result -> PublicKey? in - let match = result.rangeAt( 1) + let match = result.range(at: 1) let start = pemString.index(pemString.startIndex, offsetBy: match.location) let end = pemString.index(start, offsetBy: match.length)