|
| 1 | +/* |
| 2 | + * Copyright 2025 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 androidx.annotation.RequiresApi |
| 20 | +import com.google.errorprone.annotations.Immutable |
| 21 | +import com.google.errorprone.annotations.ThreadSafe |
| 22 | + |
| 23 | +/** |
| 24 | + * Configuration for validating the extensions in an Android attenstation certificate, as described |
| 25 | + * at https://source.android.com/docs/security/features/keystore/attestation. |
| 26 | + */ |
| 27 | +@ThreadSafe |
| 28 | +data class ExtensionConstraintConfig( |
| 29 | + val keyOrigin: ValidationLevel<Origin> = ValidationLevel.STRICT(Origin.GENERATED), |
| 30 | + val securityLevel: SecurityLevelValidationLevel = SecurityLevelValidationLevel.STRICT(), |
| 31 | + val rootOfTrust: ValidationLevel<RootOfTrust> = ValidationLevel.STRICT(null), |
| 32 | + val authorizationListOrdering: AuthorizationListOrdering = AuthorizationListOrdering.IGNORE, |
| 33 | +) |
| 34 | + |
| 35 | +/** |
| 36 | + * Configuration for validating a single extension in an Android attenstation certificate. |
| 37 | + * |
| 38 | + * @param expectedVal The expected value of the extension. If null, the extension is checked for |
| 39 | + * existence but not equality. |
| 40 | + */ |
| 41 | +@Immutable(containerOf = ["T"]) |
| 42 | +sealed interface ValidationLevel<out T> { |
| 43 | + @Immutable(containerOf = ["T"]) data class STRICT<T>(val expectedVal: T?) : ValidationLevel<T> |
| 44 | + |
| 45 | + @Immutable data object IGNORE : ValidationLevel<Nothing> |
| 46 | +} |
| 47 | + |
| 48 | +/** |
| 49 | + * Configuration for validating the attestationSecurityLevel and keyMintSecurityLevel fields in an |
| 50 | + * Android attenstation certificate. |
| 51 | + */ |
| 52 | +@Immutable |
| 53 | +sealed interface SecurityLevelValidationLevel { |
| 54 | + /** |
| 55 | + * Checks that the attestationSecurityLevel is both (1) one of {TRUSTED_ENVIRONMENT, STRONG_BOX} |
| 56 | + * and (2) equal to the keyMintSecurityLevel. |
| 57 | + * |
| 58 | + * If expectedVal is provided, checks that both the attestationSecurityLevel and |
| 59 | + * keyMintSecurityLevel are equal to the expected value. |
| 60 | + */ |
| 61 | + @Immutable |
| 62 | + data class STRICT(val expectedVal: SecurityLevel? = null) : SecurityLevelValidationLevel { |
| 63 | + init { |
| 64 | + require(expectedVal != SecurityLevel.SOFTWARE) { |
| 65 | + "STRICT validation level cannot be used with SOFTWARE security level." |
| 66 | + } |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * Checks that the attestationSecurityLevel is equal to the keyMintSecurityLevel, regardless of |
| 72 | + * security level |
| 73 | + */ |
| 74 | + @Immutable data object CONSISTENT : SecurityLevelValidationLevel |
| 75 | + |
| 76 | + /** |
| 77 | + * Checks that attestationSecurityLevel and keyMintSecurityLevel both exist and are correctly |
| 78 | + * formed. If they are unequal, [Verifier.verify] will return the lower securityLevel. |
| 79 | + */ |
| 80 | + @Immutable data object EXISTS : SecurityLevelValidationLevel |
| 81 | +} |
| 82 | + |
| 83 | +/** |
| 84 | + * Configuration for validating the ordering of the extensions in the AuthorizationList sequence in |
| 85 | + * an Android attenstation certificate. |
| 86 | + */ |
| 87 | +@Immutable |
| 88 | +sealed interface AuthorizationListOrdering { |
| 89 | + |
| 90 | + /** |
| 91 | + * Checks that the extensions in the AuthorizationList sequence appear in the order specified by |
| 92 | + * https://source.android.com/docs/security/features/keystore/attestation#schema. |
| 93 | + */ |
| 94 | + @Immutable data object STRICT : AuthorizationListOrdering |
| 95 | + |
| 96 | + /** Allows the extensions in the AuthorizationList sequence appear in any order. */ |
| 97 | + @Immutable data object IGNORE : AuthorizationListOrdering |
| 98 | +} |
| 99 | + |
| 100 | +/** Evaluates whether the [extension] is satisfied by the [ValidationLevel]. */ |
| 101 | +fun <T> ValidationLevel<T>.isSatisfiedBy(extension: T?): Boolean = |
| 102 | + when (this) { |
| 103 | + is ValidationLevel.STRICT -> |
| 104 | + if (expectedVal == null) extension != null else extension == expectedVal |
| 105 | + is ValidationLevel.IGNORE -> true |
| 106 | + } |
| 107 | + |
| 108 | +/** Evaluates whether the [keyDescription] is satisfied by the [SecurityLevelValidationLevel]. */ |
| 109 | +@RequiresApi(24) |
| 110 | +fun SecurityLevelValidationLevel.isSatisfiedBy(keyDescription: KeyDescription): Boolean { |
| 111 | + val securityLevelsMatch = |
| 112 | + keyDescription.attestationSecurityLevel == keyDescription.keyMintSecurityLevel |
| 113 | + |
| 114 | + return when (this) { |
| 115 | + is SecurityLevelValidationLevel.STRICT -> { |
| 116 | + val securityLevelIsExpected = |
| 117 | + if (this.expectedVal != null) keyDescription.attestationSecurityLevel == this.expectedVal |
| 118 | + else keyDescription.attestationSecurityLevel != SecurityLevel.SOFTWARE |
| 119 | + securityLevelsMatch && securityLevelIsExpected |
| 120 | + } |
| 121 | + is SecurityLevelValidationLevel.CONSISTENT -> securityLevelsMatch |
| 122 | + is SecurityLevelValidationLevel.EXISTS -> true |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +/** Evaluates whether the [keyDescription] is satisfied by the [AuthorizationListOrdering]. */ |
| 127 | +@RequiresApi(24) |
| 128 | +fun AuthorizationListOrdering.isSatisfiedBy(keyDescription: KeyDescription): Boolean = |
| 129 | + when (this) { |
| 130 | + is AuthorizationListOrdering.STRICT -> |
| 131 | + keyDescription.softwareEnforced.areTagsOrdered && |
| 132 | + keyDescription.hardwareEnforced.areTagsOrdered |
| 133 | + is AuthorizationListOrdering.IGNORE -> true |
| 134 | + } |
0 commit comments