Skip to content

Commit

Permalink
Fix linting from previous PR (#218)
Browse files Browse the repository at this point in the history
  • Loading branch information
nickm01 authored Jul 28, 2021
1 parent d163550 commit b13b026
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 59 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -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)
Expand Down
2 changes: 1 addition & 1 deletion Source/Key.swift
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
import Foundation
import Security

public protocol Key: class {
public protocol Key: AnyObject {

var reference: SecKey { get }
var originalData: Data? { get }
Expand Down
10 changes: 5 additions & 5 deletions Source/SwiftyRSA.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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.
//

Expand Down Expand Up @@ -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
Expand Down
26 changes: 9 additions & 17 deletions Source/X509Certificate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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..<i {
for _ in 0..<long {
result.insert(CUnsignedChar(len & 0xFF), at: 1)
len = len >> 8
}
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -96,15 +91,14 @@ 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)
} catch {
throw SwiftyRSAError.asn1ParsingFailed
}


// Ensure the raw data is an ASN1 sequence
guard case .sequence(let nodes) = node else {
return false
Expand Down Expand Up @@ -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()
Expand All @@ -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)
Expand Down
83 changes: 48 additions & 35 deletions Tests/X509Tests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -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)
Expand All @@ -124,6 +139,4 @@ class X509CertificateTests: XCTestCase {

XCTAssertEqual(decrypted.data, data)
}


}

0 comments on commit b13b026

Please sign in to comment.