Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / signal / SignalAuthData.kt
SignalAuthData.kt136 lines · 6.2 KiBRaw
| 1 | package com.vayunmathur.communicate.data.signal |
| 2 | |
| 3 | import android.content.Context |
| 4 | import androidx.core.content.edit |
| 5 | import com.vayunmathur.communicate.data.KeystoreSecretSealer |
| 6 | import com.vayunmathur.communicate.data.SecretSealer |
| 7 | import kotlinx.serialization.Serializable |
| 8 | import kotlinx.serialization.encodeToString |
| 9 | import kotlinx.serialization.json.Json as KotlinJson |
| 10 | |
| 11 | /** |
| 12 | * Persistent auth data for the Signal primary client. |
| 13 | * |
| 14 | * Real Signal registration (see PushServiceSocket verification-session and registration paths, |
| 15 | * RegistrationApiV2, RegistrationSessionRequestBody, AccountAttributes): |
| 16 | * - Two identity keypairs (ACI and PNI) each IdentityKeyPair, uploaded as base64 of serialized 33B. |
| 17 | * - Two registrationIds in AccountAttributes. |
| 18 | * - Account password for Basic auth on registration and WebSocket. |
| 19 | * - Unidentified-access key 32B. |
| 20 | * - Per-identity signed EC prekeys and Kyber-1024 last-resort prekeys (1569B with tag). |
| 21 | * |
| 22 | * Persisted under communicate_signal_auth. Keeps legacy single-identity fields for compat. |
| 23 | */ |
| 24 | @Serializable |
| 25 | data class SignalAuthData( |
| 26 | val phoneNumber: String, |
| 27 | val aci: String = "", |
| 28 | val pni: String = "", |
| 29 | val deviceId: Int = 1, |
| 30 | val identityPrivateKey: String = "", |
| 31 | val identityPublicKey: String = "", |
| 32 | val registrationId: Int = 0, |
| 33 | val signedPreKeyId: Int = 0, |
| 34 | val signedPreKeyPublic: String = "", |
| 35 | val signedPreKeyPrivate: String = "", |
| 36 | val signedPreKeySignature: String = "", |
| 37 | val pqLastResortKeyId: Int = 0, |
| 38 | val pqLastResortPublic: String = "", |
| 39 | val pqLastResortSecret: String = "", |
| 40 | val pqLastResortSignature: String = "", |
| 41 | val kyberPreKeyId: Int = 0, |
| 42 | val kyberPreKeyPublic: String = "", |
| 43 | val kyberPreKeySecret: String = "", |
| 44 | val kyberPreKeySignature: String = "", |
| 45 | val aciIdentityPrivateKey: String = "", |
| 46 | val aciIdentityPublicKey: String = "", |
| 47 | val pniIdentityPrivateKey: String = "", |
| 48 | val pniIdentityPublicKey: String = "", |
| 49 | val aciRegistrationId: Int = 0, |
| 50 | val pniRegistrationId: Int = 0, |
| 51 | val aciSignedPreKeyId: Int = 0, |
| 52 | val aciSignedPreKeyPublic: String = "", |
| 53 | val aciSignedPreKeyPrivate: String = "", |
| 54 | val aciSignedPreKeySignature: String = "", |
| 55 | val pniSignedPreKeyId: Int = 0, |
| 56 | val pniSignedPreKeyPublic: String = "", |
| 57 | val pniSignedPreKeyPrivate: String = "", |
| 58 | val pniSignedPreKeySignature: String = "", |
| 59 | val aciPqLastResortKeyId: Int = 0, |
| 60 | val aciPqLastResortPublic: String = "", |
| 61 | val aciPqLastResortSecret: String = "", |
| 62 | val aciPqLastResortSignature: String = "", |
| 63 | val pniPqLastResortKeyId: Int = 0, |
| 64 | val pniPqLastResortPublic: String = "", |
| 65 | val pniPqLastResortSecret: String = "", |
| 66 | val pniPqLastResortSignature: String = "", |
| 67 | val password: String = "", |
| 68 | val unidentifiedAccessKey: String = "", |
| 69 | val registrationLock: String? = null, |
| 70 | val verificationSessionId: String? = null, |
| 71 | val registered: Boolean = false, |
| 72 | val profileName: String = "", |
| 73 | ) { |
| 74 | fun effectiveAciPrivate(): String = aciIdentityPrivateKey.ifEmpty { identityPrivateKey } |
| 75 | fun effectiveAciPublic(): String = aciIdentityPublicKey.ifEmpty { identityPublicKey } |
| 76 | fun effectiveAciRegId(): Int = if (aciRegistrationId != 0) aciRegistrationId else registrationId |
| 77 | fun effectiveAciSignedId(): Int = if (aciSignedPreKeyId != 0) aciSignedPreKeyId else signedPreKeyId |
| 78 | fun effectiveAciSignedPub(): String = aciSignedPreKeyPublic.ifEmpty { signedPreKeyPublic } |
| 79 | fun effectiveAciSignedPriv(): String = aciSignedPreKeyPrivate.ifEmpty { signedPreKeyPrivate } |
| 80 | fun effectiveAciSignedSig(): String = aciSignedPreKeySignature.ifEmpty { signedPreKeySignature } |
| 81 | fun effectiveAciPqId(): Int = if (aciPqLastResortKeyId != 0) aciPqLastResortKeyId else pqLastResortKeyId |
| 82 | fun effectiveAciPqPub(): String = aciPqLastResortPublic.ifEmpty { pqLastResortPublic } |
| 83 | fun effectiveAciPqSec(): String = aciPqLastResortSecret.ifEmpty { pqLastResortSecret } |
| 84 | fun effectiveAciPqSig(): String = aciPqLastResortSignature.ifEmpty { pqLastResortSignature } |
| 85 | |
| 86 | /** Preference keys and factories for [SignalAuthData]. */ |
| 87 | companion object { |
| 88 | private const val PREFS_NAME = "communicate_signal_auth" |
| 89 | private const val KEY_AUTH_DATA = "auth_data" |
| 90 | |
| 91 | /** |
| 92 | * Load the sealed (or legacy plaintext) auth JSON. This store holds |
| 93 | * identity-key material, so failure handling is conservative: |
| 94 | * |
| 95 | * Sealed: unseal then parse; unseal/parse failure returns null |
| 96 | * (behaves as 'not registered') and the stored blob is left intact |
| 97 | * for retry on the next launch — never overwritten or deleted. |
| 98 | * Legacy plaintext: parse first and rewrite sealed ONLY after a |
| 99 | * successful parse (verify-before-overwrite). Idempotent. |
| 100 | */ |
| 101 | fun load(context: Context, sealer: SecretSealer = KeystoreSecretSealer.shared): SignalAuthData? { |
| 102 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 103 | val raw = prefs.getString(KEY_AUTH_DATA, null) ?: return null |
| 104 | if (sealer.isSealed(raw)) { |
| 105 | val json = sealer.unseal(raw) ?: return null |
| 106 | return try { |
| 107 | KotlinJson.decodeFromString<SignalAuthData>(json) |
| 108 | } catch (_: Exception) { |
| 109 | null |
| 110 | } |
| 111 | } |
| 112 | val parsed = try { |
| 113 | KotlinJson.decodeFromString<SignalAuthData>(raw) |
| 114 | } catch (_: Exception) { |
| 115 | return null |
| 116 | } |
| 117 | sealer.seal(raw)?.let { sealed -> |
| 118 | prefs.edit { putString(KEY_AUTH_DATA, sealed) } |
| 119 | } |
| 120 | return parsed |
| 121 | } |
| 122 | |
| 123 | /** Save the whole auth JSON sealed; on seal failure nothing is written (never plaintext). */ |
| 124 | fun save(context: Context, authData: SignalAuthData, sealer: SecretSealer = KeystoreSecretSealer.shared) { |
| 125 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 126 | val json = KotlinJson.encodeToString(authData) |
| 127 | val sealed = sealer.seal(json) ?: return |
| 128 | prefs.edit { putString(KEY_AUTH_DATA, sealed) } |
| 129 | } |
| 130 | |
| 131 | fun clear(context: Context) { |
| 132 | val prefs = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) |
| 133 | prefs.edit { remove(KEY_AUTH_DATA) } |
| 134 | } |
| 135 | } |
| 136 | } |