2018-09-09 23:26:04 +00:00
|
|
|
//
|
|
|
|
// ControlChannelSerializer.swift
|
|
|
|
// TunnelKit
|
|
|
|
//
|
|
|
|
// Created by Davide De Rosa on 9/10/18.
|
2023-03-17 15:58:36 +00:00
|
|
|
// Copyright (c) 2023 Davide De Rosa. All rights reserved.
|
2018-09-09 23:26:04 +00:00
|
|
|
//
|
2019-05-14 08:58:47 +00:00
|
|
|
// https://github.com/passepartoutvpn
|
2018-09-09 23:26:04 +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/>.
|
|
|
|
//
|
|
|
|
|
|
|
|
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-09-09 23:26:04 +00:00
|
|
|
|
|
|
|
private let log = SwiftyBeaver.self
|
|
|
|
|
|
|
|
protocol ControlChannelSerializer {
|
|
|
|
func reset()
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-09 23:26:04 +00:00
|
|
|
func serialize(packet: ControlPacket) throws -> Data
|
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
func deserialize(data: Data, start: Int, end: Int?) throws -> ControlPacket
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
extension OpenVPN.ControlChannel {
|
2018-09-09 23:26:04 +00:00
|
|
|
class PlainSerializer: ControlChannelSerializer {
|
|
|
|
func reset() {
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-09 23:26:04 +00:00
|
|
|
func serialize(packet: ControlPacket) throws -> Data {
|
2018-09-10 09:12:58 +00:00
|
|
|
return packet.serialized()
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
func deserialize(data packet: Data, start: Int, end: Int?) throws -> ControlPacket {
|
2018-09-10 09:12:58 +00:00
|
|
|
var offset = start
|
|
|
|
let end = end ?? packet.count
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-18 17:12:47 +00:00
|
|
|
guard end >= offset + PacketOpcodeLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing opcode")
|
2018-09-18 16:04:43 +00:00
|
|
|
}
|
|
|
|
let codeValue = packet[offset] >> 3
|
|
|
|
guard let code = PacketCode(rawValue: codeValue) else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Unknown code: \(codeValue))")
|
2018-09-18 16:04:43 +00:00
|
|
|
}
|
|
|
|
let key = packet[offset] & 0b111
|
2018-09-18 17:12:47 +00:00
|
|
|
offset += PacketOpcodeLength
|
2018-09-10 09:12:58 +00:00
|
|
|
|
2018-09-18 16:04:43 +00:00
|
|
|
log.debug("Control: Try read packet with code \(code) and key \(key)")
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-10 09:12:58 +00:00
|
|
|
guard end >= offset + PacketSessionIdLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing sessionId")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
let sessionId = packet.subdata(offset: offset, count: PacketSessionIdLength)
|
|
|
|
offset += PacketSessionIdLength
|
|
|
|
|
|
|
|
guard end >= offset + 1 else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing ackSize")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
let ackSize = packet[offset]
|
|
|
|
offset += 1
|
|
|
|
|
|
|
|
var ackIds: [UInt32]?
|
|
|
|
var ackRemoteSessionId: Data?
|
|
|
|
if ackSize > 0 {
|
|
|
|
guard end >= (offset + Int(ackSize) * PacketIdLength) else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing acks")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
var ids: [UInt32] = []
|
|
|
|
for _ in 0..<ackSize {
|
|
|
|
let id = packet.networkUInt32Value(from: offset)
|
|
|
|
ids.append(id)
|
|
|
|
offset += PacketIdLength
|
|
|
|
}
|
|
|
|
|
|
|
|
guard end >= offset + PacketSessionIdLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing remoteSessionId")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
let remoteSessionId = packet.subdata(offset: offset, count: PacketSessionIdLength)
|
|
|
|
offset += PacketSessionIdLength
|
|
|
|
|
|
|
|
ackIds = ids
|
|
|
|
ackRemoteSessionId = remoteSessionId
|
|
|
|
}
|
|
|
|
|
|
|
|
if code == .ackV1 {
|
|
|
|
guard let ackIds = ackIds else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Ack packet without ids")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
guard let ackRemoteSessionId = ackRemoteSessionId else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Ack packet without remoteSessionId")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
return ControlPacket(key: key, sessionId: sessionId, ackIds: ackIds as [NSNumber], ackRemoteSessionId: ackRemoteSessionId)
|
|
|
|
}
|
|
|
|
|
|
|
|
guard end >= offset + PacketIdLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing packetId")
|
2018-09-10 09:12:58 +00:00
|
|
|
}
|
|
|
|
let packetId = packet.networkUInt32Value(from: offset)
|
|
|
|
offset += PacketIdLength
|
|
|
|
|
|
|
|
var payload: Data?
|
|
|
|
if offset < end {
|
|
|
|
payload = packet.subdata(in: offset..<end)
|
|
|
|
}
|
|
|
|
|
|
|
|
let controlPacket = ControlPacket(code: code, key: key, sessionId: sessionId, packetId: packetId, payload: payload)
|
|
|
|
if let ackIds = ackIds {
|
|
|
|
controlPacket.ackIds = ackIds as [NSNumber]
|
|
|
|
controlPacket.ackRemoteSessionId = ackRemoteSessionId
|
|
|
|
}
|
|
|
|
return controlPacket
|
2018-09-09 23:26:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-09-19 20:15:16 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
extension OpenVPN.ControlChannel {
|
2018-09-19 20:15:16 +00:00
|
|
|
class AuthSerializer: ControlChannelSerializer {
|
|
|
|
private let encrypter: Encrypter
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let decrypter: Decrypter
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let prefixLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let hmacLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let authLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let preambleLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private var currentReplayId: BidirectionalState<UInt32>
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-04-23 14:44:00 +00:00
|
|
|
private let timestamp: UInt32
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
private let plain: PlainSerializer
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
init(withKey key: OpenVPN.StaticKey, digest: OpenVPN.Digest) throws {
|
2018-09-19 20:15:16 +00:00
|
|
|
let crypto = CryptoBox(cipherAlgorithm: nil, digestAlgorithm: digest.rawValue)
|
|
|
|
try crypto.configure(
|
|
|
|
withCipherEncKey: nil,
|
|
|
|
cipherDecKey: nil,
|
|
|
|
hmacEncKey: key.hmacSendKey,
|
|
|
|
hmacDecKey: key.hmacReceiveKey
|
|
|
|
)
|
|
|
|
encrypter = crypto.encrypter()
|
|
|
|
decrypter = crypto.decrypter()
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
prefixLength = PacketOpcodeLength + PacketSessionIdLength
|
|
|
|
hmacLength = crypto.digestLength()
|
|
|
|
authLength = hmacLength + PacketReplayIdLength + PacketReplayTimestampLength
|
|
|
|
preambleLength = prefixLength + authLength
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
currentReplayId = BidirectionalState(withResetValue: 1)
|
2019-04-23 14:44:00 +00:00
|
|
|
timestamp = UInt32(Date().timeIntervalSince1970)
|
2018-09-19 20:15:16 +00:00
|
|
|
plain = PlainSerializer()
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
func reset() {
|
|
|
|
currentReplayId.reset()
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
func serialize(packet: ControlPacket) throws -> Data {
|
2019-04-23 14:44:00 +00:00
|
|
|
return try serialize(packet: packet, timestamp: timestamp)
|
2018-09-19 20:15:16 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
func serialize(packet: ControlPacket, timestamp: UInt32) throws -> Data {
|
|
|
|
let data = try packet.serialized(withAuthenticator: encrypter, replayId: currentReplayId.outbound, timestamp: timestamp)
|
|
|
|
currentReplayId.outbound += 1
|
|
|
|
return data
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
// XXX: start/end are ignored, parses whole packet
|
|
|
|
func deserialize(data packet: Data, start: Int, end: Int?) throws -> ControlPacket {
|
|
|
|
let end = packet.count
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
// data starts with (prefix=(header + sessionId) + auth=(hmac + replayId))
|
|
|
|
guard end >= preambleLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing HMAC")
|
2018-09-19 20:15:16 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
// needs a copy for swapping
|
|
|
|
var authPacket = packet
|
|
|
|
let authCount = authPacket.count
|
2019-03-30 19:30:29 +00:00
|
|
|
try authPacket.withUnsafeMutableBytes {
|
|
|
|
let ptr = $0.bytePointer
|
2018-09-19 20:15:16 +00:00
|
|
|
PacketSwapCopy(ptr, packet, prefixLength, authLength)
|
|
|
|
try decrypter.verifyBytes(ptr, length: authCount, flags: nil)
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-09-19 20:15:16 +00:00
|
|
|
// TODO: validate replay packet id
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
do {
|
|
|
|
return try plain.deserialize(data: authPacket, start: authLength, end: nil)
|
|
|
|
} catch {
|
|
|
|
log.error("Control: Channel failure \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
2018-09-19 20:15:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-19 14:52:37 +00:00
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
extension OpenVPN.ControlChannel {
|
2018-10-19 14:52:37 +00:00
|
|
|
class CryptSerializer: ControlChannelSerializer {
|
|
|
|
private let encrypter: Encrypter
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private let decrypter: Decrypter
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private let headerLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private var adLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private let tagLength: Int
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private var currentReplayId: BidirectionalState<UInt32>
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2019-04-23 14:44:00 +00:00
|
|
|
private let timestamp: UInt32
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
private let plain: PlainSerializer
|
|
|
|
|
2019-05-19 12:04:41 +00:00
|
|
|
init(withKey key: OpenVPN.StaticKey) throws {
|
2018-10-19 14:52:37 +00:00
|
|
|
let crypto = CryptoBox(cipherAlgorithm: "AES-256-CTR", digestAlgorithm: "SHA256")
|
|
|
|
try crypto.configure(
|
|
|
|
withCipherEncKey: key.cipherEncryptKey,
|
|
|
|
cipherDecKey: key.cipherDecryptKey,
|
|
|
|
hmacEncKey: key.hmacSendKey,
|
|
|
|
hmacDecKey: key.hmacReceiveKey
|
|
|
|
)
|
|
|
|
encrypter = crypto.encrypter()
|
|
|
|
decrypter = crypto.decrypter()
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
headerLength = PacketOpcodeLength + PacketSessionIdLength
|
|
|
|
adLength = headerLength + PacketReplayIdLength + PacketReplayTimestampLength
|
2018-11-06 09:31:03 +00:00
|
|
|
tagLength = crypto.tagLength()
|
2018-10-19 14:52:37 +00:00
|
|
|
|
|
|
|
currentReplayId = BidirectionalState(withResetValue: 1)
|
2019-04-23 14:44:00 +00:00
|
|
|
timestamp = UInt32(Date().timeIntervalSince1970)
|
2018-10-19 14:52:37 +00:00
|
|
|
plain = PlainSerializer()
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
func reset() {
|
|
|
|
currentReplayId.reset()
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
func serialize(packet: ControlPacket) throws -> Data {
|
2019-04-23 14:44:00 +00:00
|
|
|
return try serialize(packet: packet, timestamp: timestamp)
|
2018-10-19 14:52:37 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
func serialize(packet: ControlPacket, timestamp: UInt32) throws -> Data {
|
|
|
|
let data = try packet.serialized(with: encrypter, replayId: currentReplayId.outbound, timestamp: timestamp, adLength: adLength)
|
|
|
|
currentReplayId.outbound += 1
|
|
|
|
return data
|
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
// XXX: start/end are ignored, parses whole packet
|
|
|
|
func deserialize(data packet: Data, start: Int, end: Int?) throws -> ControlPacket {
|
|
|
|
let end = end ?? packet.count
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
// data starts with (ad=(header + sessionId + replayId) + tag)
|
|
|
|
guard end >= start + adLength + tagLength else {
|
2023-07-02 09:56:40 +00:00
|
|
|
throw OpenVPNError.controlChannel(message: "Missing AD+TAG")
|
2018-10-19 14:52:37 +00:00
|
|
|
}
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
let encryptedCount = packet.count - adLength
|
|
|
|
var decryptedPacket = Data(count: decrypter.encryptionCapacity(withLength: encryptedCount))
|
|
|
|
var decryptedCount = 0
|
2019-03-30 19:30:29 +00:00
|
|
|
try packet.withUnsafeBytes {
|
|
|
|
let src = $0.bytePointer
|
2023-12-13 08:59:57 +00:00
|
|
|
var flags = CryptoFlags(iv: nil, ivLength: 0, ad: src, adLength: adLength, forTesting: false)
|
2019-03-30 19:30:29 +00:00
|
|
|
try decryptedPacket.withUnsafeMutableBytes {
|
|
|
|
let dest = $0.bytePointer
|
2018-10-19 14:52:37 +00:00
|
|
|
try decrypter.decryptBytes(src + flags.adLength, length: encryptedCount, dest: dest + headerLength, destLength: &decryptedCount, flags: &flags)
|
|
|
|
memcpy(dest, src, headerLength)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
decryptedPacket.count = headerLength + decryptedCount
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2018-10-19 14:52:37 +00:00
|
|
|
// TODO: validate replay packet id
|
2023-04-20 19:52:45 +00:00
|
|
|
|
2023-07-02 09:56:40 +00:00
|
|
|
do {
|
|
|
|
return try plain.deserialize(data: decryptedPacket, start: 0, end: nil)
|
|
|
|
} catch {
|
|
|
|
log.error("Control: Channel failure \(error)")
|
|
|
|
throw error
|
|
|
|
}
|
2018-10-19 14:52:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|