2018-08-23 11:43:04 +00:00
|
|
|
//
|
|
|
|
// SessionProxy+Configuration.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
2018-08-27 18:30:09 +00:00
|
|
|
// Created by Davide De Rosa on 8/23/18.
|
|
|
|
// Copyright (c) 2018 Davide De Rosa. All rights reserved.
|
|
|
|
//
|
|
|
|
// https://github.com/keeshux
|
|
|
|
//
|
|
|
|
// 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 11:43:04 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
import Foundation
|
|
|
|
|
|
|
|
extension SessionProxy {
|
2018-10-05 17:13:59 +00:00
|
|
|
|
|
|
|
/// A pair of credentials for authentication.
|
|
|
|
public struct Credentials: Codable, Equatable {
|
|
|
|
|
|
|
|
/// The username.
|
|
|
|
public let username: String
|
|
|
|
|
|
|
|
/// The password.
|
|
|
|
public let password: String
|
|
|
|
|
|
|
|
/// :nodoc
|
|
|
|
public init(_ username: String, _ password: String) {
|
|
|
|
self.username = username
|
|
|
|
self.password = password
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Equatable
|
|
|
|
|
|
|
|
/// :nodoc:
|
|
|
|
public static func ==(lhs: Credentials, rhs: Credentials) -> Bool {
|
|
|
|
return (lhs.username == rhs.username) && (lhs.password == rhs.password)
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// The available encryption algorithms.
|
2018-09-06 08:55:56 +00:00
|
|
|
public enum Cipher: String, Codable, CustomStringConvertible {
|
2018-08-23 13:44:31 +00:00
|
|
|
|
|
|
|
// WARNING: must match OpenSSL algorithm names
|
|
|
|
|
|
|
|
/// AES encryption with 128-bit key size and CBC.
|
|
|
|
case aes128cbc = "AES-128-CBC"
|
|
|
|
|
2018-09-06 08:37:53 +00:00
|
|
|
/// AES encryption with 192-bit key size and CBC.
|
|
|
|
case aes192cbc = "AES-192-CBC"
|
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// AES encryption with 256-bit key size and CBC.
|
|
|
|
case aes256cbc = "AES-256-CBC"
|
|
|
|
|
|
|
|
/// AES encryption with 128-bit key size and GCM.
|
|
|
|
case aes128gcm = "AES-128-GCM"
|
|
|
|
|
2018-09-06 08:37:53 +00:00
|
|
|
/// AES encryption with 192-bit key size and GCM.
|
|
|
|
case aes192gcm = "AES-192-GCM"
|
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// AES encryption with 256-bit key size and GCM.
|
|
|
|
case aes256gcm = "AES-256-GCM"
|
2018-09-04 13:48:57 +00:00
|
|
|
|
|
|
|
/// Digest should be ignored when this is `true`.
|
|
|
|
public var embedsDigest: Bool {
|
|
|
|
return rawValue.hasSuffix("-GCM")
|
|
|
|
}
|
2018-09-06 08:55:56 +00:00
|
|
|
|
2018-09-06 09:14:04 +00:00
|
|
|
/// Returns a generic name for this cipher.
|
2018-09-06 08:55:56 +00:00
|
|
|
public var genericName: String {
|
2018-09-06 09:13:10 +00:00
|
|
|
return rawValue.hasSuffix("-GCM") ? "AES-GCM" : "AES-CBC"
|
2018-09-06 08:55:56 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// :nodoc:
|
|
|
|
public var description: String {
|
|
|
|
return rawValue
|
|
|
|
}
|
2018-08-23 13:44:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// The available message digest algorithms.
|
2018-09-06 08:55:56 +00:00
|
|
|
public enum Digest: String, Codable, CustomStringConvertible {
|
2018-08-23 13:44:31 +00:00
|
|
|
|
|
|
|
// WARNING: must match OpenSSL algorithm names
|
|
|
|
|
|
|
|
/// SHA1 message digest.
|
|
|
|
case sha1 = "SHA1"
|
|
|
|
|
2018-09-06 08:37:53 +00:00
|
|
|
/// SHA224 message digest.
|
|
|
|
case sha224 = "SHA224"
|
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// SHA256 message digest.
|
|
|
|
case sha256 = "SHA256"
|
2018-09-06 08:37:53 +00:00
|
|
|
|
|
|
|
/// SHA256 message digest.
|
|
|
|
case sha384 = "SHA384"
|
|
|
|
|
|
|
|
/// SHA256 message digest.
|
|
|
|
case sha512 = "SHA512"
|
2018-09-06 08:55:56 +00:00
|
|
|
|
2018-09-06 09:14:04 +00:00
|
|
|
/// Returns a generic name for this digest.
|
2018-09-06 08:55:56 +00:00
|
|
|
public var genericName: String {
|
|
|
|
return "HMAC"
|
|
|
|
}
|
|
|
|
|
|
|
|
/// :nodoc:
|
|
|
|
public var description: String {
|
|
|
|
return "\(genericName)-\(rawValue)"
|
|
|
|
}
|
2018-08-23 13:44:31 +00:00
|
|
|
}
|
|
|
|
|
2018-08-23 11:43:04 +00:00
|
|
|
/// The way to create a `SessionProxy.Configuration` object for a `SessionProxy`.
|
|
|
|
public struct ConfigurationBuilder {
|
2018-08-30 09:05:34 +00:00
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// The cipher algorithm for data encryption.
|
|
|
|
public var cipher: Cipher
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// The digest algorithm for HMAC.
|
|
|
|
public var digest: Digest
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// The CA for TLS negotiation (PEM format).
|
|
|
|
public let ca: CryptoContainer
|
2018-08-23 21:55:10 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// The optional client certificate for TLS negotiation (PEM format).
|
|
|
|
public var clientCertificate: CryptoContainer?
|
2018-08-23 15:42:30 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// The private key for the certificate in `clientCertificate` (PEM format).
|
|
|
|
public var clientKey: CryptoContainer?
|
2018-08-23 15:42:30 +00:00
|
|
|
|
2019-02-23 10:51:37 +00:00
|
|
|
/// If true, checks EKU of server certificate.
|
2019-02-23 10:57:58 +00:00
|
|
|
public var checksEKU: Bool?
|
2019-02-23 10:51:37 +00:00
|
|
|
|
2018-09-09 00:43:59 +00:00
|
|
|
/// Sets compression framing, disabled by default.
|
|
|
|
public var compressionFraming: CompressionFraming
|
|
|
|
|
2018-09-19 20:22:35 +00:00
|
|
|
/// The optional TLS wrapping.
|
|
|
|
public var tlsWrap: TLSWrap?
|
|
|
|
|
2018-08-23 11:43:04 +00:00
|
|
|
/// Sends periodical keep-alive packets if set.
|
|
|
|
public var keepAliveInterval: TimeInterval?
|
|
|
|
|
|
|
|
/// The number of seconds after which a renegotiation should be initiated. If `nil`, the client will never initiate a renegotiation.
|
|
|
|
public var renegotiatesAfter: TimeInterval?
|
|
|
|
|
2018-10-18 09:36:02 +00:00
|
|
|
/// Server is patched for the PIA VPN provider.
|
2018-10-25 06:55:45 +00:00
|
|
|
public var usesPIAPatches: Bool?
|
2018-10-18 09:36:02 +00:00
|
|
|
|
2019-01-05 21:25:38 +00:00
|
|
|
/// Optionally override the server DNS entries.
|
|
|
|
public var dnsServers: [String]?
|
|
|
|
|
2018-08-23 11:43:04 +00:00
|
|
|
/// :nodoc:
|
2018-10-25 06:02:48 +00:00
|
|
|
public init(ca: CryptoContainer) {
|
2018-08-23 13:44:31 +00:00
|
|
|
cipher = .aes128cbc
|
|
|
|
digest = .sha1
|
2018-10-25 06:02:48 +00:00
|
|
|
self.ca = ca
|
|
|
|
clientCertificate = nil
|
|
|
|
clientKey = nil
|
2019-02-23 10:51:37 +00:00
|
|
|
checksEKU = false
|
2018-08-30 09:05:34 +00:00
|
|
|
compressionFraming = .disabled
|
2018-09-19 20:22:35 +00:00
|
|
|
tlsWrap = nil
|
2018-09-08 10:55:22 +00:00
|
|
|
keepAliveInterval = nil
|
2018-08-23 11:43:04 +00:00
|
|
|
renegotiatesAfter = nil
|
2018-10-18 09:36:02 +00:00
|
|
|
usesPIAPatches = false
|
2019-01-05 21:25:38 +00:00
|
|
|
dnsServers = nil
|
2018-08-23 11:43:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
Builds a `SessionProxy.Configuration` object.
|
|
|
|
|
|
|
|
- Returns: A `SessionProxy.Configuration` object with this builder.
|
|
|
|
*/
|
|
|
|
public func build() -> Configuration {
|
|
|
|
return Configuration(
|
2018-08-23 13:44:31 +00:00
|
|
|
cipher: cipher,
|
|
|
|
digest: digest,
|
2018-10-25 06:02:48 +00:00
|
|
|
ca: ca,
|
|
|
|
clientCertificate: clientCertificate,
|
|
|
|
clientKey: clientKey,
|
2019-02-23 10:51:37 +00:00
|
|
|
checksEKU: checksEKU,
|
2018-08-30 09:05:34 +00:00
|
|
|
compressionFraming: compressionFraming,
|
2018-09-19 20:22:35 +00:00
|
|
|
tlsWrap: tlsWrap,
|
2018-08-23 11:43:04 +00:00
|
|
|
keepAliveInterval: keepAliveInterval,
|
2018-10-18 09:36:02 +00:00
|
|
|
renegotiatesAfter: renegotiatesAfter,
|
2019-01-05 21:25:38 +00:00
|
|
|
usesPIAPatches: usesPIAPatches,
|
|
|
|
dnsServers: dnsServers
|
2018-08-23 11:43:04 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// The immutable configuration for `SessionProxy`.
|
2018-10-25 06:33:40 +00:00
|
|
|
public struct Configuration: Codable, Equatable {
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.cipher`
|
|
|
|
public let cipher: Cipher
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-08-23 13:44:31 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.digest`
|
|
|
|
public let digest: Digest
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.ca`
|
|
|
|
public let ca: CryptoContainer
|
2018-08-23 21:55:10 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.clientCertificate`
|
|
|
|
public let clientCertificate: CryptoContainer?
|
2018-08-23 15:42:30 +00:00
|
|
|
|
2018-10-25 06:02:48 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.clientKey`
|
|
|
|
public let clientKey: CryptoContainer?
|
2018-08-23 15:42:30 +00:00
|
|
|
|
2019-02-23 10:51:37 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.checksEKU`
|
2019-02-23 10:57:58 +00:00
|
|
|
public let checksEKU: Bool?
|
2019-02-23 10:51:37 +00:00
|
|
|
|
2018-08-30 09:05:34 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.compressionFraming`
|
|
|
|
public let compressionFraming: CompressionFraming
|
2018-08-23 11:43:04 +00:00
|
|
|
|
2018-09-19 20:22:35 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.tlsWrap`
|
|
|
|
public var tlsWrap: TLSWrap?
|
|
|
|
|
2018-08-23 11:43:04 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.keepAliveInterval`
|
|
|
|
public let keepAliveInterval: TimeInterval?
|
|
|
|
|
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.renegotiatesAfter`
|
|
|
|
public let renegotiatesAfter: TimeInterval?
|
2018-10-18 09:36:02 +00:00
|
|
|
|
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.usesPIAPatches`
|
2018-10-25 06:55:45 +00:00
|
|
|
public let usesPIAPatches: Bool?
|
2018-10-25 06:53:27 +00:00
|
|
|
|
2019-01-05 21:25:38 +00:00
|
|
|
/// - Seealso: `SessionProxy.ConfigurationBuilder.dnsServers`
|
|
|
|
public let dnsServers: [String]?
|
|
|
|
|
2018-10-25 06:53:27 +00:00
|
|
|
/**
|
|
|
|
Returns a `SessionProxy.ConfigurationBuilder` to use this configuration as a starting point for a new one.
|
|
|
|
|
|
|
|
- Returns: An editable `SessionProxy.ConfigurationBuilder` initialized with this configuration.
|
|
|
|
*/
|
|
|
|
public func builder() -> SessionProxy.ConfigurationBuilder {
|
|
|
|
var builder = SessionProxy.ConfigurationBuilder(ca: ca)
|
|
|
|
builder.cipher = cipher
|
|
|
|
builder.digest = digest
|
|
|
|
builder.clientCertificate = clientCertificate
|
|
|
|
builder.clientKey = clientKey
|
2019-02-23 10:51:37 +00:00
|
|
|
builder.checksEKU = checksEKU
|
2018-10-25 06:53:27 +00:00
|
|
|
builder.compressionFraming = compressionFraming
|
|
|
|
builder.tlsWrap = tlsWrap
|
|
|
|
builder.keepAliveInterval = keepAliveInterval
|
|
|
|
builder.renegotiatesAfter = renegotiatesAfter
|
|
|
|
builder.usesPIAPatches = usesPIAPatches
|
2019-01-05 21:25:38 +00:00
|
|
|
builder.dnsServers = dnsServers
|
2018-10-25 06:53:27 +00:00
|
|
|
return builder
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Equatable
|
|
|
|
|
|
|
|
/// :nodoc:
|
|
|
|
public static func ==(lhs: Configuration, rhs: Configuration) -> Bool {
|
|
|
|
return
|
|
|
|
(lhs.cipher == rhs.cipher) &&
|
|
|
|
(lhs.digest == rhs.digest) &&
|
|
|
|
(lhs.ca == rhs.ca) &&
|
|
|
|
(lhs.clientCertificate == rhs.clientCertificate) &&
|
|
|
|
(lhs.clientKey == rhs.clientKey) &&
|
2019-02-23 10:51:37 +00:00
|
|
|
(lhs.checksEKU == rhs.checksEKU) &&
|
2018-10-25 06:53:27 +00:00
|
|
|
(lhs.compressionFraming == rhs.compressionFraming) &&
|
|
|
|
(lhs.keepAliveInterval == rhs.keepAliveInterval) &&
|
|
|
|
(lhs.renegotiatesAfter == rhs.renegotiatesAfter) &&
|
2019-01-05 21:25:38 +00:00
|
|
|
(lhs.usesPIAPatches == rhs.usesPIAPatches) &&
|
|
|
|
(lhs.dnsServers == rhs.dnsServers)
|
2018-10-25 06:53:27 +00:00
|
|
|
}
|
2018-08-23 11:43:04 +00:00
|
|
|
}
|
|
|
|
}
|