tunnelkit/Tests/TunnelKitOpenVPNTests/EncryptionTests.swift

162 lines
7.2 KiB
Swift
Raw Normal View History

2018-08-23 08:19:25 +00:00
//
// EncryptionTests.swift
// TunnelKitOpenVPNTests
2018-08-23 08:19:25 +00:00
//
// Created by Davide De Rosa on 7/7/18.
2023-03-17 15:58:36 +00:00
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
//
// https://github.com/passepartoutvpn
//
// This file is part of TunnelKit.
//
// TunnelKit is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// TunnelKit is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with TunnelKit. If not, see <http://www.gnu.org/licenses/>.
//
// This file incorporates work covered by the following copyright and
// permission notice:
//
// Copyright (c) 2018-Present Private Internet Access
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
2018-08-23 08:19:25 +00:00
import XCTest
2021-10-25 14:27:27 +00:00
@testable import TunnelKitCore
@testable import TunnelKitOpenVPNCore
import CTunnelKitCore
import CTunnelKitOpenVPNProtocol
2018-08-23 08:19:25 +00:00
class EncryptionTests: XCTestCase {
2018-09-20 21:20:08 +00:00
private var cipherEncKey: ZeroingData!
2018-08-23 08:19:25 +00:00
2018-09-20 21:20:08 +00:00
private var cipherDecKey: ZeroingData!
2023-04-20 19:52:45 +00:00
2018-09-20 21:20:08 +00:00
private var hmacEncKey: ZeroingData!
2023-04-20 19:52:45 +00:00
2018-09-20 21:20:08 +00:00
private var hmacDecKey: ZeroingData!
2023-04-20 19:52:45 +00:00
2018-08-23 08:19:25 +00:00
override func setUp() {
2018-09-20 21:20:08 +00:00
cipherEncKey = try! SecureRandom.safeData(length: 32)
cipherDecKey = try! SecureRandom.safeData(length: 32)
hmacEncKey = try! SecureRandom.safeData(length: 32)
hmacDecKey = try! SecureRandom.safeData(length: 32)
2018-08-23 08:19:25 +00:00
}
override func tearDown() {
// Put teardown code here. This method is called after the invocation of each test method in the class.
}
func testCBC() {
2018-09-20 21:20:08 +00:00
let (client, server) = clientServer("aes-128-cbc", "sha256")
2018-08-23 08:19:25 +00:00
let plain = Data(hex: "00112233445566778899")
let encrypted = try! client.encrypter().encryptData(plain, flags: nil)
let decrypted = try! server.decrypter().decryptData(encrypted, flags: nil)
2018-08-23 08:19:25 +00:00
XCTAssertEqual(plain, decrypted)
}
func testHMAC() {
2018-09-20 21:20:08 +00:00
let (client, server) = clientServer(nil, "sha256")
let plain = Data(hex: "00112233445566778899")
let encrypted = try! client.encrypter().encryptData(plain, flags: nil)
XCTAssertNoThrow(try server.decrypter().verifyData(encrypted, flags: nil))
}
2023-04-20 19:52:45 +00:00
2018-08-23 08:19:25 +00:00
func testGCM() {
2018-09-20 21:20:08 +00:00
let (client, server) = clientServer("aes-256-gcm", nil)
2023-04-20 19:52:45 +00:00
2018-10-19 14:21:13 +00:00
let packetId: [UInt8] = [0x56, 0x34, 0x12, 0x00]
let ad: [UInt8] = [0x00, 0x12, 0x34, 0x56]
var flags = packetId.withUnsafeBufferPointer { iv in
ad.withUnsafeBufferPointer { ad in
CryptoFlags(iv: iv.baseAddress,
ivLength: iv.count,
ad: ad.baseAddress,
adLength: ad.count,
forTesting: true)
2020-04-05 15:08:23 +00:00
}
}
2018-08-23 08:19:25 +00:00
let plain = Data(hex: "00112233445566778899")
let encrypted = try! client.encrypter().encryptData(plain, flags: &flags)
let decrypted = try! server.decrypter().decryptData(encrypted, flags: &flags)
2018-08-23 08:19:25 +00:00
XCTAssertEqual(plain, decrypted)
}
2023-04-20 19:52:45 +00:00
2019-04-02 16:58:30 +00:00
func testCTR() {
let (client, server) = clientServer("aes-256-ctr", "sha256")
let original = Data(hex: "0000000000")
let ad: [UInt8] = [UInt8](Data(hex: "38afa8f1162096081e000000015ba35373"))
2020-04-05 15:08:23 +00:00
var flags = ad.withUnsafeBufferPointer {
CryptoFlags(iv: nil,
ivLength: 0,
ad: $0.baseAddress,
adLength: $0.count,
forTesting: true)
2020-04-05 15:08:23 +00:00
}
2019-04-02 16:58:30 +00:00
// let expEncrypted = Data(hex: "319bb8e7f8f7930cc4625079dd32a6ef9540c2fc001c53f909f712037ae9818af840b88714")
let encrypted = try! client.encrypter().encryptData(original, flags: &flags)
// XCTAssertEqual(encrypted, expEncrypted)
let decrypted = try! server.decrypter().decryptData(encrypted, flags: &flags)
XCTAssertEqual(decrypted, original)
}
func testCertificateMD5() {
2021-10-25 14:27:27 +00:00
let path = Bundle.module.path(forResource: "pia-2048", ofType: "pem")!
2019-02-27 19:08:59 +00:00
let md5 = try! TLSBox.md5(forCertificatePath: path)
let exp = "e2fccccaba712ccc68449b1c56427ac1"
XCTAssertEqual(md5, exp)
}
2023-04-20 19:52:45 +00:00
func testPrivateKeyDecryption() {
2019-04-01 22:09:18 +00:00
privateTestPrivateKeyDecryption(pkcs: "1")
privateTestPrivateKeyDecryption(pkcs: "8")
2019-04-01 22:09:18 +00:00
}
2023-04-20 19:52:45 +00:00
2019-04-01 22:09:18 +00:00
private func privateTestPrivateKeyDecryption(pkcs: String) {
2021-10-25 14:27:27 +00:00
let bundle = Bundle.module
2019-04-01 22:09:18 +00:00
let encryptedPath = bundle.path(forResource: "tunnelbear", ofType: "enc.\(pkcs).key")!
let decryptedPath = bundle.path(forResource: "tunnelbear", ofType: "key")!
2023-04-20 19:52:45 +00:00
XCTAssertThrowsError(try TLSBox.decryptedPrivateKey(fromPath: encryptedPath, passphrase: "wrongone"))
let decryptedViaPath = try! TLSBox.decryptedPrivateKey(fromPath: encryptedPath, passphrase: "foobar")
let encryptedPEM = try! String(contentsOfFile: encryptedPath, encoding: .utf8)
let decryptedViaString = try! TLSBox.decryptedPrivateKey(fromPEM: encryptedPEM, passphrase: "foobar")
XCTAssertEqual(decryptedViaPath, decryptedViaString)
2023-04-20 19:52:45 +00:00
let expDecrypted = try! String(contentsOfFile: decryptedPath)
XCTAssertEqual(decryptedViaPath, expDecrypted)
}
2023-04-20 19:52:45 +00:00
2019-04-02 16:58:30 +00:00
func testCertificatePreamble() {
2021-10-25 14:27:27 +00:00
let url = Bundle.module.url(forResource: "tunnelbear", withExtension: "crt")!
2019-05-19 13:50:30 +00:00
let cert = OpenVPN.CryptoContainer(pem: try! String(contentsOf: url))
2019-04-02 16:58:30 +00:00
XCTAssert(cert.pem.hasPrefix("-----BEGIN"))
2018-09-20 08:58:24 +00:00
}
2023-04-20 19:52:45 +00:00
2018-09-20 21:20:08 +00:00
private func clientServer(_ c: String?, _ d: String?) -> (CryptoBox, CryptoBox) {
let client = CryptoBox(cipherAlgorithm: c, digestAlgorithm: d)
let server = CryptoBox(cipherAlgorithm: c, digestAlgorithm: d)
XCTAssertNoThrow(try client.configure(withCipherEncKey: cipherEncKey, cipherDecKey: cipherDecKey, hmacEncKey: hmacEncKey, hmacDecKey: hmacDecKey))
XCTAssertNoThrow(try server.configure(withCipherEncKey: cipherDecKey, cipherDecKey: cipherEncKey, hmacEncKey: hmacDecKey, hmacDecKey: hmacEncKey))
return (client, server)
}
2018-08-23 08:19:25 +00:00
}