|
1 | 1 | package org.rustls.platformverifier
|
2 | 2 |
|
| 3 | +import android.annotation.SuppressLint |
3 | 4 | import android.content.Context
|
4 | 5 | import android.net.http.X509TrustManagerExtensions
|
5 | 6 | import android.os.Build
|
@@ -53,6 +54,8 @@ private class VerificationResult(
|
53 | 54 | // Only JNI and test code calls this, so unused code warnings are suppressed.
|
54 | 55 | // Internal for test code - no other Kotlin code should use this object directly.
|
55 | 56 | @Suppress("unused")
|
| 57 | +// We want to show a difference between Kotlin-side logs and those in Rust code |
| 58 | +@SuppressLint("LongLogTag") |
56 | 59 | internal object CertificateVerifier {
|
57 | 60 | private const val TAG = "rustls-platform-verifier-android"
|
58 | 61 |
|
@@ -161,7 +164,7 @@ internal object CertificateVerifier {
|
161 | 164 |
|
162 | 165 | @get:Synchronized
|
163 | 166 | private var systemCertificateDirectory: File? = System.getenv("ANDROID_ROOT")?.let { rootPath ->
|
164 |
| - File(rootPath + "/etc/security/cacerts") |
| 167 | + File("$rootPath/etc/security/cacerts") |
165 | 168 | }
|
166 | 169 |
|
167 | 170 | @get:Synchronized
|
@@ -196,7 +199,7 @@ internal object CertificateVerifier {
|
196 | 199 | certificateChain.add(certificate as X509Certificate)
|
197 | 200 | }
|
198 | 201 |
|
199 |
| - // Will never throw `ArrayIndexOutOfBoundsException` because `rustls`'s `ServerCertVerifer` trait |
| 202 | + // Will never throw `ArrayIndexOutOfBoundsException` because `rustls`'s `ServerCertVerifier` trait |
200 | 203 | // has a mandatory `end_entity` parameter in `verify_server_cert`.
|
201 | 204 | val endEntity = certificateChain[0]
|
202 | 205 |
|
@@ -251,7 +254,7 @@ internal object CertificateVerifier {
|
251 | 254 | }
|
252 | 255 |
|
253 | 256 | // TEST ONLY: Mock test suite cannot attempt to check revocation status if no OSCP data has been stapled,
|
254 |
| - // because Andriod requires certificates to an specify OCSP responder for network fetch in this case. |
| 257 | + // because Android requires certificates to an specify OCSP responder for network fetch in this case. |
255 | 258 | // If in testing w/o OCSP stapled, short-circuit here - only prior checks apply.
|
256 | 259 | if ((mockKeystore.size() != 0) && (ocspResponse == null)) {
|
257 | 260 | return VerificationResult(StatusCode.Ok)
|
@@ -283,7 +286,7 @@ internal object CertificateVerifier {
|
283 | 286 | // 1. Known root + OSCP stapled -> Full-chain revocation, no extra network use
|
284 | 287 | // 2. Known root + no OSCP stapled -> Full-chain revocation, with extra network use
|
285 | 288 | // 3. Unknown root + OSCP stapled -> End-entity-only revocation, no extra network use
|
286 |
| - // 4. Uknown root + no OSCP stapled -> End-entity-only revocation, with extra network use |
| 289 | + // 4. Unknown root + no OSCP stapled -> End-entity-only revocation, with extra network use |
287 | 290 | val parameters = PKIXBuilderParameters(keystore, null)
|
288 | 291 |
|
289 | 292 | val validator = CertPathValidator.getInstance("PKIX")
|
@@ -360,7 +363,7 @@ internal object CertificateVerifier {
|
360 | 363 | private fun hashPrincipal(principal: X500Principal): String {
|
361 | 364 | val hexDigits = "0123456789abcdef".toCharArray()
|
362 | 365 | val digest = MessageDigest.getInstance("MD5").digest(principal.encoded)
|
363 |
| - var hexChars = CharArray(8) |
| 366 | + val hexChars = CharArray(8) |
364 | 367 |
|
365 | 368 | for (i in 0..3) {
|
366 | 369 | // Kotlin doesn't support bitwise operators for bytes, only Int and Long.
|
@@ -393,25 +396,25 @@ internal object CertificateVerifier {
|
393 | 396 | val hash = hashPrincipal(root.subjectX500Principal)
|
394 | 397 | var i = 0
|
395 | 398 | while (true) {
|
396 |
| - val alias = hash + '.' + i |
| 399 | + val alias = "$hash.$i" |
397 | 400 |
|
398 | 401 | if (!File(loadedSystemCertificateDirectory, alias).exists()) {
|
399 | 402 | break
|
400 | 403 | }
|
401 | 404 |
|
402 |
| - val anchor = loadedSystemKeystore.getCertificate("system:" + alias) |
| 405 | + val anchor = loadedSystemKeystore.getCertificate("system:$alias") |
403 | 406 |
|
404 | 407 | // It's possible for `anchor` to be `null` if the user deleted a trust anchor.
|
405 | 408 | // Continue iterating as there may be further collisions after the deleted anchor.
|
406 | 409 | if (anchor == null) {
|
407 | 410 | continue
|
408 | 411 | // This should never happen
|
409 | 412 | } else if (anchor !is X509Certificate) {
|
410 |
| - // SAFETY: This logs a unique identifer (hash value) only in cases where a file within the |
| 413 | + // SAFETY: This logs a unique identifier (hash value) only in cases where a file within the |
411 | 414 | // system's root trust store is not a valid X509 certificate (extremely unlikely error).
|
412 | 415 | // The hash doesn't tell us any sensitive information about the invalid cert or reveal any of
|
413 | 416 | // its contents - it just lets us ID the bad file if a customer is having TLS failure issues.
|
414 |
| - Log.e(TAG, "anchor is not a certificate, alias: " + alias) |
| 417 | + Log.e(TAG, "anchor is not a certificate, alias: $alias") |
415 | 418 | continue
|
416 | 419 | // If subject and public key match, it's a system root.
|
417 | 420 | } else {
|
|
0 commit comments