1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
|
//
// MultiHopPostQuantumKeyExchanging.swift
// PacketTunnel
//
// Created by Mojgan on 2024-07-15.
// Copyright © 2025 Mullvad VPN AB. All rights reserved.
//
import MullvadREST
import MullvadRustRuntime
import MullvadSettings
import MullvadTypes
import PacketTunnelCore
import WireGuardKitTypes
final class MultiHopEphemeralPeerExchanger: EphemeralPeerExchangingProtocol {
let entry: SelectedRelay
let exit: SelectedRelay
let keyExchanger: EphemeralPeerExchangeActorProtocol
let devicePrivateKey: PrivateKey
let onFinish: () -> Void
let onUpdateConfiguration: (EphemeralPeerNegotiationState) async -> Void
let enablePostQuantum: Bool
let enableDaita: Bool
private var entryPeerKey: EphemeralPeerKey!
private var exitPeerKey: EphemeralPeerKey!
private var daitaParameters: DaitaV2Parameters?
private let defaultGatewayAddressRange = [IPAddressRange(from: "\(LocalNetworkIPs.gatewayAddress.rawValue)/32")!]
private let allTrafficRange = [
IPAddressRange(from: "\(LocalNetworkIPs.defaultRouteIpV4.rawValue)/0")!,
IPAddressRange(from: "\(LocalNetworkIPs.defaultRouteIpV6.rawValue)/0")!,
]
private var state: StateMachine = .initial
enum StateMachine {
case initial
case negotiatingWithEntry
case negotiatingBetweenEntryAndExit
case makeConnection
}
init(
entry: SelectedRelay,
exit: SelectedRelay,
devicePrivateKey: PrivateKey,
keyExchanger: EphemeralPeerExchangeActorProtocol,
enablePostQuantum: Bool,
enableDaita: Bool,
onUpdateConfiguration: @escaping (EphemeralPeerNegotiationState) async -> Void,
onFinish: @escaping () -> Void
) {
self.entry = entry
self.exit = exit
self.devicePrivateKey = devicePrivateKey
self.keyExchanger = keyExchanger
self.enablePostQuantum = enablePostQuantum
self.enableDaita = enableDaita
self.onUpdateConfiguration = onUpdateConfiguration
self.onFinish = onFinish
}
func start() async {
guard state == .initial else { return }
await negotiateWithEntry()
}
public func receiveEphemeralPeerPrivateKey(
_ ephemeralPeerPrivateKey: PrivateKey,
daitaParameters: DaitaV2Parameters?
) async {
if state == .negotiatingWithEntry {
self.daitaParameters = daitaParameters
entryPeerKey = EphemeralPeerKey(ephemeralKey: ephemeralPeerPrivateKey)
await negotiateBetweenEntryAndExit()
} else if state == .negotiatingBetweenEntryAndExit {
exitPeerKey = EphemeralPeerKey(ephemeralKey: ephemeralPeerPrivateKey)
await makeConnection()
}
}
func receivePostQuantumKey(
_ preSharedKey: PreSharedKey,
ephemeralKey: PrivateKey,
daitaParameters: DaitaV2Parameters?
) async {
if state == .negotiatingWithEntry {
self.daitaParameters = daitaParameters
entryPeerKey = EphemeralPeerKey(preSharedKey: preSharedKey, ephemeralKey: ephemeralKey)
await negotiateBetweenEntryAndExit()
} else if state == .negotiatingBetweenEntryAndExit {
exitPeerKey = EphemeralPeerKey(preSharedKey: preSharedKey, ephemeralKey: ephemeralKey)
await makeConnection()
}
}
private func negotiateWithEntry() async {
state = .negotiatingWithEntry
await onUpdateConfiguration(
.single(
EphemeralPeerRelayConfiguration(
relay: entry,
configuration: EphemeralPeerConfiguration(
privateKey: devicePrivateKey,
allowedIPs: defaultGatewayAddressRange,
daitaParameters: daitaParameters
)
)))
keyExchanger.startNegotiation(
with: devicePrivateKey,
enablePostQuantum: enablePostQuantum,
enableDaita: enableDaita
)
}
private func negotiateBetweenEntryAndExit() async {
state = .negotiatingBetweenEntryAndExit
await onUpdateConfiguration(
.multi(
entry: EphemeralPeerRelayConfiguration(
relay: entry,
configuration: EphemeralPeerConfiguration(
privateKey: entryPeerKey.ephemeralKey,
preSharedKey: entryPeerKey.preSharedKey,
allowedIPs: [IPAddressRange(from: "\(exit.endpoint.ipv4Relay.ip)/32")!],
daitaParameters: self.daitaParameters
)
),
exit: EphemeralPeerRelayConfiguration(
relay: exit,
configuration: EphemeralPeerConfiguration(
privateKey: devicePrivateKey,
allowedIPs: defaultGatewayAddressRange,
daitaParameters: nil
)
)
))
// Daita is always disabled when negotiating with the exit peer in the multihop scenarios
keyExchanger.startNegotiation(
with: devicePrivateKey,
enablePostQuantum: enablePostQuantum,
enableDaita: false
)
}
private func makeConnection() async {
state = .makeConnection
await onUpdateConfiguration(
.multi(
entry: EphemeralPeerRelayConfiguration(
relay: entry,
configuration: EphemeralPeerConfiguration(
privateKey: entryPeerKey.ephemeralKey,
preSharedKey: entryPeerKey.preSharedKey,
allowedIPs: [IPAddressRange(from: "\(exit.endpoint.ipv4Relay.ip)/32")!],
daitaParameters: self.daitaParameters
)
),
exit: EphemeralPeerRelayConfiguration(
relay: exit,
configuration: EphemeralPeerConfiguration(
privateKey: exitPeerKey.ephemeralKey,
preSharedKey: exitPeerKey.preSharedKey,
allowedIPs: allTrafficRange,
daitaParameters: nil
)
)
))
self.onFinish()
}
}
|