Modern-Apps.git
Publicgit clone https://git.maproj.org/Modern-Apps.gitModern-Apps.git / communicate-dev / src / main / java / com / vayunmathur / communicate / data / rcs / RcsGbaAuth.kt
RcsGbaAuth.kt82 lines · 3.2 KiBRaw
| 1 | package com.vayunmathur.communicate.data.rcs |
| 2 | |
| 3 | /** |
| 4 | * GBA-shaped credentials + digest auth for FT-over-HTTP (3GPP TS 33.220 / |
| 5 | * RFC 2617). |
| 6 | * |
| 7 | * Live bootstrapping lives in [RcsGbaBootstrap] (stub-linked |
| 8 | * `TelephonyManager.bootstrapAuthenticationRequest`, SMS-role permission). |
| 9 | * This object holds: |
| 10 | * - [GbaCredentials]: bootstrapped NAF credentials (btId + key), from live |
| 11 | * bootstrap or privileged injection ([injected]). |
| 12 | * - [digestResponse]/[digestAuthorizationHeader]: the qop=auth construction |
| 13 | * both the GBA and the plain-digest paths share, unit-tested against the |
| 14 | * RFC 2617 known-answer vector. |
| 15 | * |
| 16 | * When bootstrapping is unavailable (no privilege, modem refusal), uploads |
| 17 | * use the plain-digest fallback (empty password), exactly like a client |
| 18 | * whose GBA challenge fails — the server either accepts or the upload |
| 19 | * degrades, never crashes. |
| 20 | */ |
| 21 | object RcsGbaAuth { |
| 22 | /** Bootstrapped NAF credentials: digest username + raw key. */ |
| 23 | data class GbaCredentials(val btId: String, val key: ByteArray) |
| 24 | |
| 25 | /** |
| 26 | * Injected GBA credentials (privileged builds only). Null in normal |
| 27 | * dev builds — the uploader falls back to plain digest. |
| 28 | */ |
| 29 | @Volatile |
| 30 | var injected: GbaCredentials? = null |
| 31 | |
| 32 | /** |
| 33 | * Digest `response` hash (RFC 2617 qop=auth) — pure, unit-tested against |
| 34 | * the RFC's known-answer vector. |
| 35 | */ |
| 36 | fun digestResponse( |
| 37 | username: String, |
| 38 | password: String, |
| 39 | realm: String, |
| 40 | nonce: String, |
| 41 | method: String, |
| 42 | uri: String, |
| 43 | cnonce: String, |
| 44 | ): String { |
| 45 | val ha1 = md5("$username:$realm:$password") |
| 46 | val ha2 = md5("$method:$uri") |
| 47 | return md5("$ha1:$nonce:00000001:$cnonce:auth:$ha2") |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Full digest `Authorization` header *value* from a `WWW-Authenticate` / |
| 52 | * `Proxy-Authenticate` challenge (RFC 2617 qop=auth). Shared by SIP |
| 53 | * challenge retries ([RcsSipTransport]) and FT-over-HTTP |
| 54 | * ([RcsFileTransferHttp]). Empty when the challenge isn't digest or |
| 55 | * lacks realm/nonce. |
| 56 | */ |
| 57 | private const val CNONCE_LENGTH = 16 |
| 58 | fun digestAuthorizationHeader( |
| 59 | challenge: String, |
| 60 | method: String, |
| 61 | uri: String, |
| 62 | username: String, |
| 63 | password: String = "", |
| 64 | cnonce: String = java.util.UUID.randomUUID().toString().replace("-", "").take(CNONCE_LENGTH), |
| 65 | ): String { |
| 66 | if (!challenge.contains("Digest", ignoreCase = true)) return "" |
| 67 | fun param(name: String): String = |
| 68 | Regex("$name=\"([^\"]+)\"", RegexOption.IGNORE_CASE).find(challenge) |
| 69 | ?.groupValues?.getOrNull(1).orEmpty() |
| 70 | val realm = param("realm") |
| 71 | val nonce = param("nonce") |
| 72 | if (realm.isBlank() || nonce.isBlank()) return "" |
| 73 | val response = digestResponse(username, password, realm, nonce, method, uri, cnonce) |
| 74 | return "Digest username=\"$username\", realm=\"$realm\", nonce=\"$nonce\", uri=\"$uri\", " + |
| 75 | "response=\"$response\", qop=auth, nc=00000001, cnonce=\"$cnonce\"" |
| 76 | } |
| 77 | |
| 78 | private fun md5(s: String): String { |
| 79 | val md = java.security.MessageDigest.getInstance("MD5") |
| 80 | return md.digest(s.toByteArray(Charsets.UTF_8)).joinToString("") { "%02x".format(it) } |
| 81 | } |
| 82 | } |