Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / e2e / SignalE2EIdentities.kt
SignalE2EIdentities.kt67 lines · 2.7 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal.e2e |
| 2 | |
| 3 | import org.signal.libsignal.protocol.IdentityKey |
| 4 | import org.signal.libsignal.protocol.message.PreKeySignalMessage |
| 5 | import com.vayunmathur.communicate.data.signal.e2e.SignalE2E.PreKeyUpload |
| 6 | |
| 7 | /** |
| 8 | * Identity/store queries for [SignalE2E] (split for file length). |
| 9 | * Extension functions on [SignalE2E]; behavior identical, call sites unchanged. |
| 10 | */ |
| 11 | |
| 12 | fun SignalE2E.callIdentityKey(aci: String): ByteArray? = |
| 13 | storedIdentityKey(aci)?.let { rawPublicKeyBytes(it) } |
| 14 | |
| 15 | fun SignalE2E.storedIdentityKey(aci: String): ByteArray? = |
| 16 | try { protocolStore.getIdentity(signalAddress(aci))?.serialize() } catch (_: Exception) { null } |
| 17 | |
| 18 | /** |
| 19 | * Record [identityKey] as the trusted identity for [aci] and archive existing sessions, so the next |
| 20 | * message builds a session against the accepted key. Only call this once the user has verified it. |
| 21 | */ |
| 22 | fun SignalE2E.acceptIdentity(aci: String, identityKey: ByteArray): Boolean = try { |
| 23 | val address = signalAddress(aci) |
| 24 | protocolStore.saveIdentity(address, IdentityKey(identityKey)) |
| 25 | // Every device's session was built against the old key. |
| 26 | deviceIdsWithSessions(aci).forEach { archiveSession(aci, it) } |
| 27 | true |
| 28 | } catch (ignored: Throwable) { |
| 29 | false |
| 30 | } |
| 31 | |
| 32 | /** |
| 33 | * The registration id to address [aci]'s device with, from our session with them. A send whose |
| 34 | * `destinationRegistrationId` does not match what the server holds is rejected as a stale device. |
| 35 | */ |
| 36 | fun SignalE2E.remoteRegistrationId(aci: String, deviceId: Int): Int? = try { |
| 37 | protocolStore.loadSession(signalAddress(aci, deviceId)).remoteRegistrationId |
| 38 | } catch (_: Throwable) { |
| 39 | null |
| 40 | } |
| 41 | |
| 42 | /** The sender's registration id, carried on every pre-key message. */ |
| 43 | fun SignalE2E.senderRegistrationId(ciphertext: ByteArray): Int? = try { |
| 44 | PreKeySignalMessage(ciphertext).registrationId |
| 45 | } catch (_: Throwable) { |
| 46 | null |
| 47 | } |
| 48 | |
| 49 | /** Whether our store holds a signed pre-key [id] whose public half is exactly [publicKey]. */ |
| 50 | fun SignalE2E.hasSignedPreKeyMatching(id: Int, publicKey: ByteArray?): Boolean { |
| 51 | if (publicKey == null) return true |
| 52 | return try { |
| 53 | val record = protocolStore.loadSignedPreKey(id) |
| 54 | record.keyPair.publicKey.serialize().contentEquals(publicKey) |
| 55 | } catch (_: Throwable) { |
| 56 | false |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | /** Rotate the signed pre-key regardless of what is stored, for when the server's copy is unusable. */ |
| 61 | fun SignalE2E.rotateSignedPreKeyNow(): PreKeyUpload.KeyEntity? = rotateSignedPreKey() |
| 62 | |
| 63 | /** Devices of [aci] we already have a session with, always including device 1. */ |
| 64 | fun SignalE2E.deviceIdsWithSessions(aci: String): List<Int> { |
| 65 | val subDevices = try { protocolStore.getSubDeviceSessions(aci) } catch (_: Exception) { emptyList() } |
| 66 | return (listOf(1) + subDevices).distinct().sorted() |
| 67 | } |