1package com.peachpasswords.sync23import java.security.MessageDigest4import javax.inject.Inject5import javax.inject.Singleton6import kotlinx.coroutines.CoroutineScope7import kotlinx.coroutines.Dispatchers8import kotlinx.coroutines.Job9import kotlinx.coroutines.channels.awaitClose10import kotlinx.coroutines.flow.Flow11import kotlinx.coroutines.flow.callbackFlow12import kotlinx.coroutines.launch13import kotlinx.coroutines.withTimeout14import kotlinx.coroutines.withTimeoutOrNull15import org.json.JSONObject1617sealed interface RelayAdoptionEvent {18 19 data class InvitationReady(val invitation: String) : RelayAdoptionEvent20 data class AwaitingPeerConfirmation(21 val sas: String,22 val deviceName: String,23 ) : RelayAdoptionEvent24 data class Adopted(val deviceName: String) : RelayAdoptionEvent25 data object Expired : RelayAdoptionEvent26 data class Rejected(val reason: String) : RelayAdoptionEvent27}2829@Singleton30class RelayAdoptionHost @Inject constructor(31 private val relaySettings: RelaySettingsStore,32) {33 private var job: Job? = null34 private var socket: RelayBinarySocket? = null3536 fun start(37 scope: CoroutineScope,38 ourStaticPrivateKey: ByteArray,39 ourStaticPublicKey: ByteArray,40 localIdentity: LocalDeviceIdentity,41 sourceDeps: AdoptionSourceV2Deps,42 windowMs: Long = PAIRING_WINDOW_MS,43 ): Flow<RelayAdoptionEvent> = callbackFlow {44 45 46 47 48 val relayUrl = when (val decision = relaySettings.relayInForce()) {49 is RelayDecision.Refuse -> {50 trySend(RelayAdoptionEvent.Rejected(decision.reason.wire()))51 close()52 return@callbackFlow53 }5455 is RelayDecision.Dial -> decision.relayUrl56 }57 val token = Rendezvous.newPairingToken()58 val invitation = createPairingInvitation(59 relayUrl,60 token,61 noiseStaticPublicKeyHex = Rendezvous.bytesToHex(ourStaticPublicKey),62 )6364 job = scope.launch(Dispatchers.IO) {65 try {66 val connected = OkHttpRelaySocket.connectPairing(67 Rendezvous.buildUrl(relayUrl, token),68 )69 socket = connected70 71 72 trySend(RelayAdoptionEvent.InvitationReady(invitation))7374 val adopted = withTimeout(windowMs) {75 runRelayAdoptionSourceSession(76 socket = connected,77 expectedToken = token,78 relayUrl = relayUrl,79 ourStaticPrivateKey = ourStaticPrivateKey,80 ourStaticPublicKey = ourStaticPublicKey,81 localIdentity = localIdentity,82 sourceDeps = sourceDeps,83 confirm = { sas, request ->84 trySend(85 RelayAdoptionEvent.AwaitingPeerConfirmation(86 sas = sas,87 deviceName = capDeviceName(request.deviceName.ifBlank { "New device" }),88 ),89 )90 91 92 93 true94 },95 )96 }97 if (adopted == null) {98 trySend(RelayAdoptionEvent.Rejected("adoption_rejected"))99 } else {100 trySend(101 RelayAdoptionEvent.Adopted(102 capDeviceName(adopted.deviceName?.takeIf(String::isNotBlank) ?: "New device"),103 ),104 )105 }106 } catch (error: kotlinx.coroutines.TimeoutCancellationException) {107 trySend(RelayAdoptionEvent.Expired)108 } catch (error: kotlinx.coroutines.CancellationException) {109 110 111 112 113 114 throw error115 } catch (error: Throwable) {116 trySend(RelayAdoptionEvent.Rejected(error.message ?: "relay_adoption_failed"))117 } finally {118 socket?.close()119 socket = null120 close()121 }122 }123124 awaitClose { stop() }125 }126127 fun stop() {128 job?.cancel()129 job = null130 socket?.close()131 socket = null132 }133}134135136137138139140141142internal suspend fun runRelayAdoptionSourceSession(143 socket: RelayBinarySocket,144 expectedToken: String,145 relayUrl: String,146 ourStaticPrivateKey: ByteArray,147 ourStaticPublicKey: ByteArray,148 localIdentity: LocalDeviceIdentity,149 sourceDeps: AdoptionSourceV2Deps,150 confirm: suspend (sas: String, request: AdoptionFrame.Request) -> Boolean,151): PairedDevice? {152 val hello = JSONObject(String(socket.receive(), Charsets.UTF_8))153 require(hello.optString("type") == "PEACH_HELLO") { "expected PEACH_HELLO" }154 require(155 MessageDigest.isEqual(156 expectedToken.toByteArray(Charsets.US_ASCII),157 hello.optString("token").toByteArray(Charsets.US_ASCII),158 ),159 ) { "pairing token mismatch" }160161 socket.send(162 JSONObject()163 .put("type", "PEACH_HELLO_ACK")164 .put(165 "ephemeralPublicKeyHex",166 ourStaticPublicKey.joinToString("") { "%02x".format(it) },167 )168 .toString()169 .toByteArray(Charsets.UTF_8),170 )171172 val noise = noiseNKResponder(173 ourStaticPrivateKey,174 ourStaticPublicKey,175 socket::send,176 socket::receive,177 )178 try {179 val channel = object : PairingTransport {180 override suspend fun send(msg: ByteArray) = socket.send(noise.encrypt(msg))181 override suspend fun receive(): ByteArray = noise.decrypt(socket.receive())182 override suspend fun close() = socket.close()183 }184185 val request = receiveFrame(channel)186 require(request is AdoptionFrame.Request || request is AdoptionFrame.RequestV2) { "expected ADOPT_REQUEST" }187 if (request is AdoptionFrame.Request && !confirm(SASDerivation.derive(noise.handshakeHash), request)) return null188189 val pairState = AdoptionPairState(190 rendezvousSecretHex = Rendezvous.bytesToHex(191 Rendezvous.deriveSecret(noise.handshakeHash),192 ),193 relayUrl = Rendezvous.normalizeRelayUrl(relayUrl),194 )195 val boundDeps = object : AdoptionSourceV2Deps {196 override val sourceIdentity = localIdentity197 override val pairState: AdoptionPairState = pairState198 override suspend fun rootKeyHex() = sourceDeps.rootKeyHex()199 override suspend fun vaultId() = sourceDeps.vaultId()200 override suspend fun license() = sourceDeps.license()201 override suspend fun applyLicense(license: String) = sourceDeps.applyLicense(license)202 override suspend fun onConsent(request: AdoptionFrame.Request) = true203204 205 206 207 override suspend fun onConsent(request: AdoptionFrame.RequestV2): Boolean =208 confirm(209 SASDerivation.derive(noise.handshakeHash),210 AdoptionFrame.Request(211 sessionRequestId = request.deviceId,212 deviceName = request.deviceName,213 deviceType = request.deviceType,214 license = request.license,215 snapshotCapable = request.snapshotCapable,216 ),217 )218 override suspend fun persistPendingHolder(request: AdoptionFrame.Request, nowMs: Long) = sourceDeps.persistPendingHolder(request, nowMs)219 override suspend fun completePendingHolder(request: AdoptionFrame.Request, device: PairedDevice) = sourceDeps.completePendingHolder(request, device)220 override suspend fun persistPendingIdentity(device: PairedDevice, nowMs: Long) = sourceDeps.persistPendingIdentity(device, nowMs)221 override suspend fun completePendingIdentity(device: PairedDevice) = sourceDeps.completePendingIdentity(device)222 override suspend fun upsertPairedDevice(device: PairedDevice) = sourceDeps.upsertPairedDevice(device)223 override fun hasDeviceIdentityCollision(device: PairedDevice) = sourceDeps.hasDeviceIdentityCollision(device)224 override suspend fun rotateSourceIdentity() = sourceDeps.rotateSourceIdentity()225 override fun snapshotCapable() = sourceDeps.snapshotCapable()226 override suspend fun vaultSnapshot() = sourceDeps.vaultSnapshot()227 override suspend fun outgoingDeviceManifest() = sourceDeps.outgoingDeviceManifest()228 override suspend fun markSynced(deviceId: String) = sourceDeps.markSynced(deviceId)229 override suspend fun duressSlotJson() = sourceDeps.duressSlotJson()230 }231 val adopted = when (request) {232 is AdoptionFrame.RequestV2 -> grantAdoptionV2(channel, request, boundDeps)233 is AdoptionFrame.Request -> grantAdoption(channel, request, boundDeps)234 else -> null235 } ?: return null236 237 val receiverWantsSnapshot = when (request) {238 is AdoptionFrame.Request -> request.snapshotCapable == true239 is AdoptionFrame.RequestV2 -> request.snapshotCapable == true240 else -> false241 }242 if (receiverWantsSnapshot) {243 try {244 val snapshot = boundDeps.vaultSnapshot()245 if (snapshot != null) {246 val duressSlot = runCatching { boundDeps.duressSlotJson() }.getOrNull()247 sendVaultSnapshot(channel, snapshot.first, snapshot.second, duressSlot)248 val applied = receiveFrame(channel)249 if (applied is AdoptionFrame.SnapshotApplied) {250 val manifest = boundDeps.outgoingDeviceManifest()251 if (manifest != null) {252 sendFrame(channel, AdoptionFrame.DeviceManifestFrame(manifest))253 withTimeoutOrNull(5_000) { receiveFrame(channel) }254 }255 boundDeps.markSynced(adopted.deviceId)256 }257 }258 } catch (error: Exception) {259 if (error is SnapshotDuressFrameTooLargeException) throw error260 if (error is kotlinx.coroutines.CancellationException) throw error261 PeachSyncLog.w("adoption: snapshot send failed — reason=${relayLogReason(error)}")262 }263 }264 return adopted265 } finally {266 noise.close()267 }268}