|
| 1 | +// |
| 2 | +// MultiHopPostQuantumKeyExchanging.swift |
| 3 | +// PacketTunnel |
| 4 | +// |
| 5 | +// Created by Mojgan on 2024-07-15. |
| 6 | +// Copyright © 2024 Mullvad VPN AB. All rights reserved. |
| 7 | +// |
| 8 | + |
| 9 | +import MullvadREST |
| 10 | +import MullvadRustRuntime |
| 11 | +import MullvadSettings |
| 12 | +import MullvadTypes |
| 13 | +import PacketTunnelCore |
| 14 | +import WireGuardKitTypes |
| 15 | + |
| 16 | +final class MultiHopPostQuantumKeyExchanging: PostQuantumKeyExchangingProtocol { |
| 17 | + let entry: SelectedRelay |
| 18 | + let exit: SelectedRelay |
| 19 | + let keyExchanger: PostQuantumKeyExchangeActorProtocol |
| 20 | + let devicePrivateKey: PrivateKey |
| 21 | + let onFinish: () -> Void |
| 22 | + let onUpdateConfiguration: (PostQuantumNegotiationState) -> Void |
| 23 | + |
| 24 | + private var entryPostQuantumKey: PostQuantumKey! |
| 25 | + private var exitPostQuantumKey: PostQuantumKey! |
| 26 | + |
| 27 | + private let defaultGatewayAddressRange = [IPAddressRange(from: "\(LocalNetworkIPs.gatewayAddress.rawValue)/32")!] |
| 28 | + private let allTrafficRange = [ |
| 29 | + IPAddressRange(from: "\(LocalNetworkIPs.defaultRouteIpV4.rawValue)/0")!, |
| 30 | + IPAddressRange(from: "\(LocalNetworkIPs.defaultRouteIpV6.rawValue)/0")!, |
| 31 | + ] |
| 32 | + |
| 33 | + private var state: StateMachine = .initial |
| 34 | + |
| 35 | + enum StateMachine { |
| 36 | + case initial |
| 37 | + case negotiatingWithEntry |
| 38 | + case negotiatingBetweenEntryAndExit |
| 39 | + case makeConnection |
| 40 | + } |
| 41 | + |
| 42 | + init( |
| 43 | + entry: SelectedRelay, |
| 44 | + exit: SelectedRelay, |
| 45 | + devicePrivateKey: PrivateKey, |
| 46 | + keyExchanger: PostQuantumKeyExchangeActorProtocol, |
| 47 | + onUpdateConfiguration: @escaping (PostQuantumNegotiationState) -> Void, |
| 48 | + onFinish: @escaping () -> Void |
| 49 | + ) { |
| 50 | + self.entry = entry |
| 51 | + self.exit = exit |
| 52 | + self.devicePrivateKey = devicePrivateKey |
| 53 | + self.keyExchanger = keyExchanger |
| 54 | + self.onUpdateConfiguration = onUpdateConfiguration |
| 55 | + self.onFinish = onFinish |
| 56 | + } |
| 57 | + |
| 58 | + func start() { |
| 59 | + guard state == .initial else { return } |
| 60 | + negotiateWithEntry() |
| 61 | + } |
| 62 | + |
| 63 | + func receivePostQuantumKey( |
| 64 | + _ preSharedKey: PreSharedKey, |
| 65 | + ephemeralKey: PrivateKey |
| 66 | + ) { |
| 67 | + if state == .negotiatingWithEntry { |
| 68 | + entryPostQuantumKey = PostQuantumKey(preSharedKey: preSharedKey, ephemeralKey: ephemeralKey) |
| 69 | + negotiateBetweenEntryAndExit() |
| 70 | + } else if state == .negotiatingBetweenEntryAndExit { |
| 71 | + exitPostQuantumKey = PostQuantumKey(preSharedKey: preSharedKey, ephemeralKey: ephemeralKey) |
| 72 | + makeConnection() |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private func negotiateWithEntry() { |
| 77 | + state = .negotiatingWithEntry |
| 78 | + onUpdateConfiguration(.single(PostQuantumConfigurationRelay( |
| 79 | + relay: entry, |
| 80 | + configuration: PostQuantumConfiguration( |
| 81 | + privateKey: devicePrivateKey, |
| 82 | + allowedIPs: defaultGatewayAddressRange |
| 83 | + ) |
| 84 | + ))) |
| 85 | + keyExchanger.startNegotiation(with: devicePrivateKey) |
| 86 | + } |
| 87 | + |
| 88 | + private func negotiateBetweenEntryAndExit() { |
| 89 | + state = .negotiatingBetweenEntryAndExit |
| 90 | + onUpdateConfiguration(.multi( |
| 91 | + entry: PostQuantumConfigurationRelay( |
| 92 | + relay: entry, |
| 93 | + configuration: PostQuantumConfiguration( |
| 94 | + privateKey: entryPostQuantumKey.ephemeralKey, |
| 95 | + preSharedKey: entryPostQuantumKey.preSharedKey, |
| 96 | + allowedIPs: [IPAddressRange(from: "\(exit.endpoint.ipv4Relay.ip)/32")!] |
| 97 | + ) |
| 98 | + ), |
| 99 | + exit: PostQuantumConfigurationRelay( |
| 100 | + relay: exit, |
| 101 | + configuration: PostQuantumConfiguration( |
| 102 | + privateKey: devicePrivateKey, |
| 103 | + allowedIPs: defaultGatewayAddressRange |
| 104 | + ) |
| 105 | + ) |
| 106 | + )) |
| 107 | + keyExchanger.startNegotiation(with: devicePrivateKey) |
| 108 | + } |
| 109 | + |
| 110 | + private func makeConnection() { |
| 111 | + state = .makeConnection |
| 112 | + onUpdateConfiguration(.multi( |
| 113 | + entry: PostQuantumConfigurationRelay( |
| 114 | + relay: entry, |
| 115 | + configuration: PostQuantumConfiguration( |
| 116 | + privateKey: entryPostQuantumKey.ephemeralKey, |
| 117 | + preSharedKey: entryPostQuantumKey.preSharedKey, |
| 118 | + allowedIPs: [IPAddressRange(from: "\(exit.endpoint.ipv4Relay.ip)/32")!] |
| 119 | + ) |
| 120 | + ), |
| 121 | + exit: PostQuantumConfigurationRelay( |
| 122 | + relay: exit, |
| 123 | + configuration: PostQuantumConfiguration( |
| 124 | + privateKey: exitPostQuantumKey.ephemeralKey, |
| 125 | + preSharedKey: exitPostQuantumKey.preSharedKey, |
| 126 | + allowedIPs: allTrafficRange |
| 127 | + ) |
| 128 | + ) |
| 129 | + )) |
| 130 | + self.onFinish() |
| 131 | + } |
| 132 | +} |
0 commit comments