Skip to content

Commit 09eaf26

Browse files
agxpcopybara-github
authored andcommitted
Add Flogger logging and ThreadSafe annotations to Verifier.
PiperOrigin-RevId: 850090876
1 parent 851433e commit 09eaf26

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright 2024 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package com.android.keyattestation.verifier
18+
19+
import com.google.common.flogger.GoogleLogger
20+
import com.google.errorprone.annotations.ThreadSafe
21+
import com.google.protobuf.ByteString
22+
23+
/** A [LogHook] that logs to GoogleLogger. */
24+
@ThreadSafe
25+
class GoogleLoggerLogHook : LogHook {
26+
override fun createRequestLog(): VerifyRequestLog = GoogleLoggerRequestLog(logger)
27+
28+
internal companion object {
29+
val logger = GoogleLogger.forEnclosingClass()
30+
}
31+
}
32+
33+
@ThreadSafe
34+
private class GoogleLoggerRequestLog(private val logger: GoogleLogger) : VerifyRequestLog {
35+
override fun logInputChain(inputChain: List<ByteString>) {}
36+
37+
override fun logResult(result: VerificationResult) {
38+
when (result) {
39+
is VerificationResult.Success -> logger.atInfo().log("Attestation verification succeeded.")
40+
is VerificationResult.ChallengeMismatch ->
41+
logger.atWarning().log("Attestation challenge mismatch.")
42+
is VerificationResult.PathValidationFailure ->
43+
logger.atWarning().withCause(result.cause).log("Certificate path validation failed.")
44+
is VerificationResult.ChainParsingFailure ->
45+
logger.atWarning().withCause(result.cause).log("Failed to parse certificate chain.")
46+
is VerificationResult.ExtensionParsingFailure ->
47+
logger.atWarning().withCause(result.cause).log("Failed to parse key description extension.")
48+
is VerificationResult.ExtensionConstraintViolation ->
49+
logger.atWarning().log("Constraint violation: %s", result.cause)
50+
is VerificationResult.SoftwareAttestationUnsupported -> {}
51+
}
52+
}
53+
54+
override fun logKeyDescription(keyDescription: KeyDescription) {}
55+
56+
override fun logProvisioningInfoMap(provisioningInfoMap: ProvisioningInfoMap) {}
57+
58+
override fun logCertSerialNumbers(certSerialNumbers: List<String>) {}
59+
60+
override fun logInfoMessage(infoMessage: String) {}
61+
62+
override fun flush() {}
63+
}

src/main/kotlin/Verifier.kt

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,28 +42,44 @@ import kotlinx.coroutines.guava.future
4242
import kotlinx.coroutines.runBlocking
4343

4444
/** The result of verifying an Android Key Attestation certificate chain. */
45+
@ThreadSafe
4546
sealed interface VerificationResult {
47+
@ThreadSafe
4648
data class Success(
47-
val publicKey: PublicKey,
49+
@field:ThreadSafe.Suppress(reason = "PublicKey is immutable") val publicKey: PublicKey,
4850
val challenge: ByteString,
4951
val securityLevel: SecurityLevel,
5052
val verifiedBootState: VerifiedBootState,
5153
val deviceInformation: ProvisioningInfoMap?,
54+
@field:ThreadSafe.Suppress(reason = "DeviceIdentity is deeply immutable")
5255
val attestedDeviceIds: DeviceIdentity,
5356
) : VerificationResult
5457

55-
data object ChallengeMismatch : VerificationResult
58+
@ThreadSafe data object ChallengeMismatch : VerificationResult
5659

57-
data class PathValidationFailure(val cause: CertPathValidatorException) : VerificationResult
60+
@ThreadSafe
61+
data class PathValidationFailure(
62+
@field:ThreadSafe.Suppress(reason = "Exceptions are generally immutable after creation")
63+
val cause: CertPathValidatorException
64+
) : VerificationResult
5865

59-
data class ChainParsingFailure(val cause: Exception) : VerificationResult
66+
@ThreadSafe
67+
data class ChainParsingFailure(
68+
@field:ThreadSafe.Suppress(reason = "Exceptions are generally immutable after creation")
69+
val cause: Exception
70+
) : VerificationResult
6071

61-
data class ExtensionParsingFailure(val cause: ExtensionParsingException) : VerificationResult
72+
@ThreadSafe
73+
data class ExtensionParsingFailure(
74+
@field:ThreadSafe.Suppress(reason = "Exceptions are generally immutable after creation")
75+
val cause: ExtensionParsingException
76+
) : VerificationResult
6277

78+
@ThreadSafe
6379
data class ExtensionConstraintViolation(val cause: String, val reason: KeyAttestationReason) :
6480
VerificationResult
6581

66-
data object SoftwareAttestationUnsupported : VerificationResult
82+
@ThreadSafe data object SoftwareAttestationUnsupported : VerificationResult
6783
}
6884

6985
/**

src/main/kotlin/provider/RevocationChecker.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ class RevocationChecker(private val revokedSerials: Set<String>) : PKIXRevocatio
4343
require(cert is X509Certificate)
4444

4545
if (revokedSerials.contains(cert.serialNumber.toString(16))) {
46-
// TODO: b/356234568 - Surface the revocation reason.
46+
// TODO: google-internal bug - Surface the revocation reason.
4747
throw CertPathValidatorException(
4848
"Certificate has been revoked: ${cert.serialNumber}",
4949
null,

0 commit comments

Comments
 (0)