2018-08-23 13:49:21 +00:00
|
|
|
//
|
2019-05-19 12:04:41 +00:00
|
|
|
// Authenticator.swift
|
2018-08-23 13:49:21 +00:00
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 2/9/17.
|
2022-02-04 11:42:58 +00:00
|
|
|
// Copyright (c) 2022 Davide De Rosa. All rights reserved.
|
2018-08-27 18:30:09 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-08-27 18:30:09 +00:00
|
|
|
//
|
|
|
|
// 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 13:49:21 +00:00
|
|
|
|
|
|
|
import Foundation
|
|
|
|
import SwiftyBeaver
|
2021-10-25 14:27:27 +00:00
|
|
|
import TunnelKitCore
|
2021-11-07 20:54:05 +00:00
|
|
|
import TunnelKitOpenVPNCore
|
|
|
|
import CTunnelKitCore
|
|
|
|
import CTunnelKitOpenVPNProtocol
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
fileprivate extension ZeroingData {
|
2019-03-30 19:16:04 +00:00
|
|
|
func appendSized(_ buf: ZeroingData) {
|
2018-08-23 13:49:21 +00:00
|
|
|
append(Z(UInt16(buf.count).bigEndian))
|
|
|
|
append(buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
extension OpenVPN {
|
2018-08-23 13:49:21 +00:00
|
|
|
class Authenticator {
|
|
|
|
private var controlBuffer: ZeroingData
|
|
|
|
|
|
|
|
private(set) var preMaster: ZeroingData
|
|
|
|
|
|
|
|
private(set) var random1: ZeroingData
|
|
|
|
|
|
|
|
private(set) var random2: ZeroingData
|
|
|
|
|
|
|
|
private(set) var serverRandom1: ZeroingData?
|
|
|
|
|
|
|
|
private(set) var serverRandom2: ZeroingData?
|
|
|
|
|
2020-02-29 18:11:15 +00:00
|
|
|
private(set) var username: ZeroingData?
|
2018-08-23 13:49:21 +00:00
|
|
|
|
2020-02-29 18:11:15 +00:00
|
|
|
private(set) var password: ZeroingData?
|
2018-08-23 13:49:21 +00:00
|
|
|
|
2019-05-01 08:37:39 +00:00
|
|
|
var withLocalOptions: Bool
|
|
|
|
|
2018-10-05 15:45:05 +00:00
|
|
|
init(_ username: String?, _ password: String?) throws {
|
2019-05-19 00:05:34 +00:00
|
|
|
preMaster = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.preMasterLength)
|
|
|
|
random1 = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.randomLength)
|
|
|
|
random2 = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.randomLength)
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
// XXX: not 100% secure, can't erase input username/password
|
2018-10-05 15:45:05 +00:00
|
|
|
if let username = username, let password = password {
|
|
|
|
self.username = Z(username, nullTerminated: true)
|
|
|
|
self.password = Z(password, nullTerminated: true)
|
|
|
|
} else {
|
|
|
|
self.username = nil
|
|
|
|
self.password = nil
|
|
|
|
}
|
2018-08-23 13:49:21 +00:00
|
|
|
|
2019-05-01 08:37:39 +00:00
|
|
|
withLocalOptions = true
|
|
|
|
|
2018-08-23 13:49:21 +00:00
|
|
|
controlBuffer = Z()
|
|
|
|
}
|
|
|
|
|
2020-02-29 18:11:15 +00:00
|
|
|
func reset() {
|
|
|
|
controlBuffer.zero()
|
|
|
|
preMaster.zero()
|
|
|
|
random1.zero()
|
|
|
|
random2.zero()
|
|
|
|
serverRandom1?.zero()
|
|
|
|
serverRandom2?.zero()
|
|
|
|
username = nil
|
|
|
|
password = nil
|
|
|
|
}
|
|
|
|
|
2018-08-23 13:49:21 +00:00
|
|
|
// MARK: Authentication request
|
|
|
|
|
|
|
|
// Ruby: on_tls_connect
|
2019-05-19 12:04:41 +00:00
|
|
|
func putAuth(into: TLSBox, options: Configuration) throws {
|
2018-08-23 13:49:21 +00:00
|
|
|
let raw = Z(ProtocolMacros.tlsPrefix)
|
|
|
|
|
|
|
|
// local keys
|
|
|
|
raw.append(preMaster)
|
|
|
|
raw.append(random1)
|
|
|
|
raw.append(random2)
|
|
|
|
|
2019-04-15 15:37:57 +00:00
|
|
|
// options string
|
2019-05-01 08:37:39 +00:00
|
|
|
let optsString: String
|
|
|
|
if withLocalOptions {
|
|
|
|
var opts = [
|
|
|
|
"V4",
|
|
|
|
"dev-type tun"
|
|
|
|
]
|
|
|
|
if let comp = options.compressionFraming {
|
|
|
|
switch comp {
|
|
|
|
case .compLZO:
|
|
|
|
opts.append("comp-lzo")
|
|
|
|
|
|
|
|
case .compress:
|
|
|
|
opts.append("compress")
|
|
|
|
|
|
|
|
default:
|
|
|
|
break
|
|
|
|
}
|
2019-04-15 15:37:57 +00:00
|
|
|
}
|
2019-05-01 08:37:39 +00:00
|
|
|
if let direction = options.tlsWrap?.key.direction?.rawValue {
|
|
|
|
opts.append("keydir \(direction)")
|
|
|
|
}
|
2019-05-01 21:18:54 +00:00
|
|
|
opts.append("cipher \(options.fallbackCipher.rawValue)")
|
2019-05-01 08:37:39 +00:00
|
|
|
opts.append("auth \(options.fallbackDigest.rawValue)")
|
|
|
|
opts.append("keysize \(options.fallbackCipher.keySize)")
|
|
|
|
if let strategy = options.tlsWrap?.strategy {
|
|
|
|
opts.append("tls-\(strategy)")
|
|
|
|
}
|
|
|
|
opts.append("key-method 2")
|
|
|
|
opts.append("tls-client")
|
|
|
|
optsString = opts.joined(separator: ",")
|
|
|
|
} else {
|
|
|
|
optsString = "V0 UNDEF"
|
2019-04-15 15:37:57 +00:00
|
|
|
}
|
|
|
|
log.debug("TLS.auth: Local options: \(optsString)")
|
|
|
|
raw.appendSized(Z(optsString, nullTerminated: true))
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
// credentials
|
2018-10-05 15:45:05 +00:00
|
|
|
if let username = username, let password = password {
|
|
|
|
raw.appendSized(username)
|
|
|
|
raw.appendSized(password)
|
|
|
|
} else {
|
|
|
|
raw.append(Z(UInt16(0)))
|
|
|
|
raw.append(Z(UInt16(0)))
|
|
|
|
}
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
// peer info
|
2021-01-02 23:56:02 +00:00
|
|
|
var extra: [String: String] = [:]
|
|
|
|
if let dataCiphers = options.dataCiphers {
|
|
|
|
extra["IV_CIPHERS"] = dataCiphers.map { $0.rawValue }.joined(separator: ":")
|
|
|
|
}
|
|
|
|
raw.appendSized(Z(CoreConfiguration.OpenVPN.peerInfo(extra: extra), nullTerminated: true))
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
if CoreConfiguration.logsSensitiveData {
|
|
|
|
log.debug("TLS.auth: Put plaintext (\(raw.count) bytes): \(raw.toHex())")
|
|
|
|
} else {
|
|
|
|
log.debug("TLS.auth: Put plaintext (\(raw.count) bytes)")
|
|
|
|
}
|
|
|
|
|
|
|
|
try into.putRawPlainText(raw.bytes, length: raw.count)
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: Server replies
|
|
|
|
|
|
|
|
func appendControlData(_ data: ZeroingData) {
|
|
|
|
controlBuffer.append(data)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseAuthReply() throws -> Bool {
|
|
|
|
let prefixLength = ProtocolMacros.tlsPrefix.count
|
|
|
|
|
|
|
|
// TLS prefix + random (x2) + opts length [+ opts]
|
2019-05-19 00:05:34 +00:00
|
|
|
guard (controlBuffer.count >= prefixLength + 2 * CoreConfiguration.OpenVPN.randomLength + 2) else {
|
2018-08-23 13:49:21 +00:00
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
let prefix = controlBuffer.withOffset(0, count: prefixLength)
|
|
|
|
guard prefix.isEqual(to: ProtocolMacros.tlsPrefix) else {
|
2019-05-19 12:19:23 +00:00
|
|
|
throw OpenVPNError.wrongControlDataPrefix
|
2018-08-23 13:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var offset = ProtocolMacros.tlsPrefix.count
|
|
|
|
|
2019-05-19 00:05:34 +00:00
|
|
|
let serverRandom1 = controlBuffer.withOffset(offset, count: CoreConfiguration.OpenVPN.randomLength)
|
|
|
|
offset += CoreConfiguration.OpenVPN.randomLength
|
2018-08-23 13:49:21 +00:00
|
|
|
|
2019-05-19 00:05:34 +00:00
|
|
|
let serverRandom2 = controlBuffer.withOffset(offset, count: CoreConfiguration.OpenVPN.randomLength)
|
|
|
|
offset += CoreConfiguration.OpenVPN.randomLength
|
2018-08-23 13:49:21 +00:00
|
|
|
|
|
|
|
let serverOptsLength = Int(controlBuffer.networkUInt16Value(fromOffset: offset))
|
|
|
|
offset += 2
|
|
|
|
|
|
|
|
guard controlBuffer.count >= offset + serverOptsLength else {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
let serverOpts = controlBuffer.withOffset(offset, count: serverOptsLength)
|
|
|
|
offset += serverOptsLength
|
|
|
|
|
|
|
|
if CoreConfiguration.logsSensitiveData {
|
|
|
|
log.debug("TLS.auth: Parsed server random: [\(serverRandom1.toHex()), \(serverRandom2.toHex())]")
|
|
|
|
} else {
|
|
|
|
log.debug("TLS.auth: Parsed server random")
|
|
|
|
}
|
|
|
|
|
|
|
|
if let serverOptsString = serverOpts.nullTerminatedString(fromOffset: 0) {
|
2019-05-01 08:24:04 +00:00
|
|
|
log.debug("TLS.auth: Parsed server options: \"\(serverOptsString)\"")
|
2018-08-23 13:49:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
self.serverRandom1 = serverRandom1
|
|
|
|
self.serverRandom2 = serverRandom2
|
|
|
|
controlBuffer.remove(untilOffset: offset)
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseMessages() -> [String] {
|
|
|
|
var messages = [String]()
|
|
|
|
var offset = 0
|
|
|
|
|
|
|
|
while true {
|
|
|
|
guard let msg = controlBuffer.nullTerminatedString(fromOffset: offset) else {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
messages.append(msg)
|
|
|
|
offset += msg.count + 1
|
|
|
|
}
|
|
|
|
|
|
|
|
controlBuffer.remove(untilOffset: offset)
|
|
|
|
|
|
|
|
return messages
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|