tunnelkit/Sources/TunnelKitOpenVPNProtocol/Authenticator.swift

247 lines
9.0 KiB
Swift
Raw Normal View History

//
// Authenticator.swift
// TunnelKit
//
// Created by Davide De Rosa on 2/9/17.
2024-01-14 13:33:14 +00:00
// Copyright (c) 2024 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.
//
import Foundation
import SwiftyBeaver
2021-10-25 14:27:27 +00:00
import TunnelKitCore
import TunnelKitOpenVPNCore
import CTunnelKitCore
import CTunnelKitOpenVPNProtocol
private let log = SwiftyBeaver.self
fileprivate extension ZeroingData {
2019-03-30 19:16:04 +00:00
func appendSized(_ buf: ZeroingData) {
append(Z(UInt16(buf.count).bigEndian))
append(buf)
}
}
extension OpenVPN {
class Authenticator {
private var controlBuffer: ZeroingData
2023-04-20 19:52:45 +00:00
private(set) var preMaster: ZeroingData
2023-04-20 19:52:45 +00:00
private(set) var random1: ZeroingData
2023-04-20 19:52:45 +00:00
private(set) var random2: ZeroingData
2023-04-20 19:52:45 +00:00
private(set) var serverRandom1: ZeroingData?
private(set) var serverRandom2: ZeroingData?
private(set) var username: ZeroingData?
2023-04-20 19:52:45 +00:00
private(set) var password: ZeroingData?
2023-04-20 19:52:45 +00:00
var withLocalOptions: Bool
2023-04-20 19:52:45 +00:00
2018-10-05 15:45:05 +00:00
init(_ username: String?, _ password: String?) throws {
preMaster = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.preMasterLength)
random1 = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.randomLength)
random2 = try SecureRandom.safeData(length: CoreConfiguration.OpenVPN.randomLength)
2023-04-20 19:52:45 +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
}
2023-04-20 19:52:45 +00:00
withLocalOptions = true
2023-04-20 19:52:45 +00:00
controlBuffer = Z()
}
2023-04-20 19:52:45 +00:00
func reset() {
controlBuffer.zero()
preMaster.zero()
random1.zero()
random2.zero()
serverRandom1?.zero()
serverRandom2?.zero()
username = nil
password = nil
}
2023-04-20 19:52:45 +00:00
// MARK: Authentication request
// Ruby: on_tls_connect
func putAuth(into: TLSBox, options: Configuration) throws {
let raw = Z(ProtocolMacros.tlsPrefix)
2023-04-20 19:52:45 +00:00
// local keys
raw.append(preMaster)
raw.append(random1)
raw.append(random2)
2023-04-20 19:52:45 +00:00
// options string
let optsString: String
if withLocalOptions {
var opts = [
"V4",
"dev-type tun"
]
if let comp = options.compressionFraming {
switch comp {
case .compLZO:
opts.append("comp-lzo")
2023-04-20 19:52:45 +00:00
case .compress:
opts.append("compress")
2023-04-20 19:52:45 +00:00
default:
break
}
}
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)")
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"
}
log.debug("TLS.auth: Local options: \(optsString)")
raw.appendSized(Z(optsString, nullTerminated: true))
2023-04-20 19:52:45 +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)))
}
// peer info
var extra: [String: String] = [:]
if let dataCiphers = options.dataCiphers {
2022-10-26 22:31:30 +00:00
extra["IV_CIPHERS"] = dataCiphers.map(\.rawValue).joined(separator: ":")
}
raw.appendSized(Z(CoreConfiguration.OpenVPN.peerInfo(extra: extra), nullTerminated: true))
if CoreConfiguration.logsSensitiveData {
log.debug("TLS.auth: Put plaintext (\(raw.count) bytes): \(raw.toHex())")
} else {
log.debug("TLS.auth: Put plaintext (\(raw.count) bytes)")
}
2023-04-20 19:52:45 +00:00
try into.putRawPlainText(raw.bytes, length: raw.count)
}
2023-04-20 19:52:45 +00:00
// MARK: Server replies
func appendControlData(_ data: ZeroingData) {
controlBuffer.append(data)
}
2023-04-20 19:52:45 +00:00
func parseAuthReply() throws -> Bool {
let prefixLength = ProtocolMacros.tlsPrefix.count
// TLS prefix + random (x2) + opts length [+ opts]
2023-04-20 19:52:45 +00:00
guard controlBuffer.count >= prefixLength + 2 * CoreConfiguration.OpenVPN.randomLength + 2 else {
return false
}
2023-04-20 19:52:45 +00:00
let prefix = controlBuffer.withOffset(0, count: prefixLength)
guard prefix.isEqual(to: ProtocolMacros.tlsPrefix) else {
throw OpenVPNError.wrongControlDataPrefix
}
2023-04-20 19:52:45 +00:00
var offset = ProtocolMacros.tlsPrefix.count
2023-04-20 19:52:45 +00:00
let serverRandom1 = controlBuffer.withOffset(offset, count: CoreConfiguration.OpenVPN.randomLength)
offset += CoreConfiguration.OpenVPN.randomLength
2023-04-20 19:52:45 +00:00
let serverRandom2 = controlBuffer.withOffset(offset, count: CoreConfiguration.OpenVPN.randomLength)
offset += CoreConfiguration.OpenVPN.randomLength
2023-04-20 19:52:45 +00:00
let serverOptsLength = Int(controlBuffer.networkUInt16Value(fromOffset: offset))
offset += 2
2023-04-20 19:52:45 +00:00
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")
}
2023-04-20 19:52:45 +00:00
if let serverOptsString = serverOpts.nullTerminatedString(fromOffset: 0) {
log.debug("TLS.auth: Parsed server options: \"\(serverOptsString)\"")
}
2023-04-20 19:52:45 +00:00
self.serverRandom1 = serverRandom1
self.serverRandom2 = serverRandom2
controlBuffer.remove(untilOffset: offset)
2023-04-20 19:52:45 +00:00
return true
}
2023-04-20 19:52:45 +00:00
func parseMessages() -> [String] {
var messages = [String]()
var offset = 0
2023-04-20 19:52:45 +00:00
while true {
guard let msg = controlBuffer.nullTerminatedString(fromOffset: offset) else {
break
}
messages.append(msg)
offset += msg.count + 1
}
controlBuffer.remove(untilOffset: offset)
return messages
}
}
}