Skip to content

Commit 8462393

Browse files
Android HW Trust Teamcopybara-github
authored andcommitted
Update LogHook for updated thread safety annotations.
PiperOrigin-RevId: 830895939
1 parent 6dae35c commit 8462393

5 files changed

Lines changed: 41 additions & 98 deletions

File tree

src/main/kotlin/Verifier.kt

Lines changed: 8 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -64,17 +64,10 @@ sealed interface VerificationResult {
6464
VerificationResult
6565
}
6666

67-
/**
68-
* Interface for logging info level key attestation events and information.
69-
*
70-
* Uses of the log hook must be thread safe within a single verify call.
71-
*/
67+
/** Interface for logging info level key attestation events and information. */
7268
@ThreadSafe
7369
interface LogHook {
74-
fun createRequestLog(): VerifyRequestLog
75-
}
7670

77-
interface VerifyRequestLog {
7871
/**
7972
* Logs the certificate chain which is being verified. Called for each call to [verify].
8073
*
@@ -121,9 +114,6 @@ interface VerifyRequestLog {
121114
* @param infoMessage The info level message to log.
122115
*/
123116
fun logInfoMessage(infoMessage: String)
124-
125-
/* Flushes any buffered logs. Called just before [verify] returns. */
126-
fun flush()
127117
}
128118

129119
/**
@@ -161,17 +151,15 @@ open class Verifier(
161151
challengeChecker: ChallengeChecker? = null,
162152
log: LogHook? = null,
163153
): VerificationResult {
164-
val requestLog = log?.createRequestLog()
165154
val result =
166155
try {
167156
val certPath = KeyAttestationCertPath(chain)
168-
runBlocking { internalVerify(certPath, challengeChecker, requestLog) }
157+
runBlocking { internalVerify(certPath, challengeChecker, log) }
169158
} catch (e: CertificateException) {
170-
requestLog?.logInputChain(chain.map { it.getEncoded().toByteString() })
159+
log?.logInputChain(chain.map { it.getEncoded().toByteString() })
171160
VerificationResult.ChainParsingFailure(e)
172161
}
173-
requestLog?.logResult(result)
174-
requestLog?.flush()
162+
log?.logResult(result)
175163
return result
176164
}
177165

@@ -194,17 +182,15 @@ open class Verifier(
194182
): ListenableFuture<VerificationResult> {
195183
val immutableChain = ImmutableList.copyOf(chain)
196184
return coroutineScope.future {
197-
val requestLog = log?.createRequestLog()
198185
val result =
199186
try {
200187
val certPath = KeyAttestationCertPath(immutableChain)
201-
internalVerify(certPath, challengeChecker, requestLog)
188+
internalVerify(certPath, challengeChecker, log)
202189
} catch (e: CertificateException) {
203-
requestLog?.logInputChain(immutableChain.map { it.getEncoded().toByteString() })
190+
log?.logInputChain(immutableChain.map { it.getEncoded().toByteString() })
204191
VerificationResult.ChainParsingFailure(e)
205192
}
206-
requestLog?.logResult(result)
207-
requestLog?.flush()
193+
log?.logResult(result)
208194
result
209195
}
210196
}
@@ -213,7 +199,7 @@ open class Verifier(
213199
private suspend fun internalVerify(
214200
certPath: KeyAttestationCertPath,
215201
challengeChecker: ChallengeChecker? = null,
216-
log: VerifyRequestLog? = null,
202+
log: LogHook? = null,
217203
): VerificationResult {
218204
log?.logInputChain(certPath.certificatesWithAnchor.map { it.getEncoded().toByteString() })
219205
log?.logCertSerialNumbers(

src/main/kotlin/testing/Certs.kt

Lines changed: 21 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.android.keyattestation.verifier.AuthorizationList
2020
import com.android.keyattestation.verifier.KeyDescription
2121
import com.android.keyattestation.verifier.KeyMintTag
2222
import com.android.keyattestation.verifier.Origin
23-
import com.android.keyattestation.verifier.ProvisioningInfoMap
2423
import com.android.keyattestation.verifier.RootOfTrust
2524
import com.android.keyattestation.verifier.SecurityLevel
2625
import com.android.keyattestation.verifier.VerifiedBootState
@@ -53,6 +52,7 @@ object Certs {
5352
val rootKey = certFactory.rootKey
5453
val factoryIntermediate = certFactory.factoryIntermediate
5554
val remoteIntermediate = certFactory.remoteIntermediate
55+
val strongBoxIntermediate = certFactory.strongBoxIntermediate
5656
val factoryAttestation = certFactory.factoryAttestation
5757
}
5858

@@ -100,8 +100,8 @@ object CertLists {
100100
val validStrongboxFactoryProvisioned by lazy {
101101
listOf(
102102
certFactory.generateLeafCert(extension = certFactory.STRONG_BOX_KEY_DESCRIPTION_EXT),
103-
certFactory.generateAttestationCert(issuer = certFactory.strongBoxIntermediate.subject),
104-
certFactory.strongBoxIntermediate,
103+
certFactory.generateAttestationCert(issuer = Certs.strongBoxIntermediate.subject),
104+
Certs.strongBoxIntermediate,
105105
Certs.root,
106106
)
107107
}
@@ -340,43 +340,6 @@ object CertLists {
340340
)
341341
}
342342

343-
val malformedProvisioningInfo by lazy {
344-
listOf(
345-
certFactory.generateLeafCert(),
346-
certFactory.generateAttestationCert(
347-
issuer = certFactory.rkpIntermediate.subject,
348-
signingKey = certFactory.rkpKey.private,
349-
extraExtension =
350-
Extension(
351-
ProvisioningInfoMap.OID,
352-
/* critical= */ false,
353-
ASN1Integer(BigInteger.valueOf(1234567)).encoded,
354-
),
355-
),
356-
certFactory.rkpIntermediate,
357-
Certs.remoteIntermediate,
358-
certFactory.root,
359-
)
360-
}
361-
362-
/* Different revoked serial numbers for testing. */
363-
@JvmField val REVOKED_SERIAL_NUMBER = 42.toBigInteger()
364-
@JvmField val REVOKED_SERIAL_NUMBER_BIG = 8000000000000.toBigInteger()
365-
@JvmField
366-
val REVOKED_SERIAL_NUMBER_LONG_STRING = "c35747a084470c3135aeefe2b8d40cd6".toBigInteger(16)
367-
@JvmField val REVOKED_SERIAL_NUMBER_ODD_LENGTH = 1228286566665971148.toBigInteger()
368-
369-
/* A chain where the attesstation certificate has {@link REVOKED_SERIAL_NUMBER}. */
370-
@JvmStatic
371-
val revoked by lazy {
372-
listOf(
373-
certFactory.generateLeafCert(),
374-
certFactory.generateAttestationCert(serialNumber = REVOKED_SERIAL_NUMBER),
375-
Certs.factoryIntermediate,
376-
certFactory.root,
377-
)
378-
}
379-
380343
private fun generateValidLeafCertWithAppendedTag(appendedTag: Int, appendedValue: ASN1Encodable) =
381344
certFactory.generateLeafCert(
382345
extension =
@@ -508,6 +471,24 @@ object Chains {
508471
)
509472
}
510473

474+
/* Different revoked serial numbers for testing. */
475+
@JvmField val REVOKED_SERIAL_NUMBER = 42.toBigInteger()
476+
@JvmField val REVOKED_SERIAL_NUMBER_BIG = 8000000000000.toBigInteger()
477+
@JvmField
478+
val REVOKED_SERIAL_NUMBER_LONG_STRING = "c35747a084470c3135aeefe2b8d40cd6".toBigInteger(16)
479+
@JvmField val REVOKED_SERIAL_NUMBER_ODD_LENGTH = 1228286566665971148.toBigInteger()
480+
481+
/* A chain where the attesstation certificate has {@link REVOKED_SERIAL_NUMBER}. */
482+
@JvmStatic
483+
val revoked by lazy {
484+
KeyAttestationCertPath(
485+
certFactory.generateLeafCert(),
486+
certFactory.generateAttestationCert(serialNumber = REVOKED_SERIAL_NUMBER),
487+
Certs.factoryIntermediate,
488+
certFactory.root,
489+
)
490+
}
491+
511492
/* A factory chain with an additional intermediate certificate. */
512493
val forgedKeybox by lazy {
513494
val compromisedAttestationKey = certFactory.generateEcKeyPair()

src/main/kotlin/testing/FakeLogHook.kt

Lines changed: 0 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ import com.android.keyattestation.verifier.KeyDescription
2020
import com.android.keyattestation.verifier.LogHook
2121
import com.android.keyattestation.verifier.ProvisioningInfoMap
2222
import com.android.keyattestation.verifier.VerificationResult
23-
import com.android.keyattestation.verifier.VerifyRequestLog
2423
import com.google.errorprone.annotations.ThreadSafe
2524
import com.google.protobuf.ByteString
2625

@@ -31,20 +30,6 @@ import com.google.protobuf.ByteString
3130
* test.
3231
*/
3332
class FakeLogHook : LogHook {
34-
var fakeVerifyRequestLog = FakeVerifyRequestLog()
35-
36-
override fun createRequestLog(): VerifyRequestLog {
37-
return fakeVerifyRequestLog
38-
}
39-
}
40-
41-
/**
42-
* Fake implementation of [VerifyRequestLog] for testing.
43-
*
44-
* Stores the last values passed to each logging method. A new instance should be created for each
45-
* test.
46-
*/
47-
class FakeVerifyRequestLog : VerifyRequestLog {
4833
var inputChain = mutableListOf<ByteString>()
4934
var result: VerificationResult? = null
5035
var keyDescription: KeyDescription? = null
@@ -75,6 +60,4 @@ class FakeVerifyRequestLog : VerifyRequestLog {
7560
override fun logInfoMessage(infoMessage: String) {
7661
this.infoMessages.add(infoMessage)
7762
}
78-
79-
override fun flush() {}
8063
}

src/test/kotlin/VerifierTest.kt

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -201,7 +201,7 @@ class VerifierTest {
201201
)
202202
.await()
203203
)
204-
assertThat(logHook.fakeVerifyRequestLog.inputChain)
204+
assertThat(logHook.inputChain)
205205
.isEqualTo(CertLists.wrongTrustAnchor.map { it.encoded.toByteString() })
206206
}
207207

@@ -210,8 +210,7 @@ class VerifierTest {
210210
val logHook = FakeLogHook()
211211
val chain = readCertList("blueline/sdk28/TEE_EC_NONE.pem")
212212
assertIs<VerificationResult.Success>(verifier.verifyAsync(this, chain, log = logHook).await())
213-
assertThat(logHook.fakeVerifyRequestLog.keyDescription)
214-
.isEqualTo(chain.first().keyDescription())
213+
assertThat(logHook.keyDescription).isEqualTo(chain[0].keyDescription())
215214
}
216215

217216
@Test
@@ -226,7 +225,7 @@ class VerifierTest {
226225
assertIs<VerificationResult.Success>(
227226
verifierWithTestRoot.verifyAsync(this, CertLists.invalidBootPatchLevel, log = logHook).await()
228227
)
229-
assertThat(logHook.fakeVerifyRequestLog.infoMessages).isNotEmpty()
228+
assertThat(logHook.infoMessages).isNotEmpty()
230229
}
231230

232231
@Test

src/test/kotlin/provider/RevocationCheckerTest.kt

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,8 @@
1616

1717
package com.android.keyattestation.verifier.provider
1818

19-
import com.android.keyattestation.verifier.testing.CertLists
2019
import com.android.keyattestation.verifier.testing.Certs
20+
import com.android.keyattestation.verifier.testing.Chains
2121
import com.android.keyattestation.verifier.testing.FakeCalendar
2222
import java.security.Security
2323
import java.security.cert.CertPathValidator
@@ -39,34 +39,28 @@ class RevocationCheckerTest {
3939
private val revocationChecker =
4040
RevocationChecker(
4141
setOf(
42-
CertLists.REVOKED_SERIAL_NUMBER.toString(16),
43-
CertLists.REVOKED_SERIAL_NUMBER_BIG.toString(16),
44-
CertLists.REVOKED_SERIAL_NUMBER_LONG_STRING.toString(16),
45-
CertLists.REVOKED_SERIAL_NUMBER_ODD_LENGTH.toString(16),
42+
Chains.REVOKED_SERIAL_NUMBER.toString(16),
43+
Chains.REVOKED_SERIAL_NUMBER_BIG.toString(16),
44+
Chains.REVOKED_SERIAL_NUMBER_LONG_STRING.toString(16),
45+
Chains.REVOKED_SERIAL_NUMBER_ODD_LENGTH.toString(16),
4646
)
4747
)
4848
private val validator = CertPathValidator.getInstance("KeyAttestation")
4949
private val pkixValidator = CertPathValidator.getInstance("PKIX")
5050

5151
@Test
5252
fun withoutRevocationChecker_validationSucceeds() {
53-
validator.validate(KeyAttestationCertPath(CertLists.revoked), params)
54-
pkixValidator.validate(KeyAttestationCertPath(CertLists.revoked), params)
53+
validator.validate(Chains.revoked, params)
54+
pkixValidator.validate(Chains.revoked, params)
5555
}
5656

5757
@Test
5858
fun withRevocationChecker_throwsCertPathValidatorException() {
5959
assertFailsWith<CertPathValidatorException> {
60-
validator.validate(
61-
KeyAttestationCertPath(CertLists.revoked),
62-
params.apply { addCertPathChecker(revocationChecker) },
63-
)
60+
validator.validate(Chains.revoked, params.apply { addCertPathChecker(revocationChecker) })
6461
}
6562
assertFailsWith<CertPathValidatorException> {
66-
pkixValidator.validate(
67-
KeyAttestationCertPath(CertLists.revoked),
68-
params.apply { addCertPathChecker(revocationChecker) },
69-
)
63+
pkixValidator.validate(Chains.revoked, params.apply { addCertPathChecker(revocationChecker) })
7064
}
7165
}
7266

0 commit comments

Comments
 (0)