Alex Klyubin | cc21bb3 | 2015-03-31 16:50:37 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 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 | |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 17 | package android.security.keystore; |
Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 18 | |
Kevin Chyn | 7d07c89 | 2020-02-18 18:18:17 -0800 | [diff] [blame] | 19 | import android.hardware.biometrics.BiometricManager; |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 20 | import android.security.GateKeeper; |
| 21 | import android.security.KeyStore; |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 22 | import android.security.keymaster.KeymasterArguments; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 23 | import android.security.keymaster.KeymasterDefs; |
Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 24 | |
Alex Klyubin | a95550f | 2015-07-16 16:32:23 -0700 | [diff] [blame] | 25 | import java.security.ProviderException; |
Kevin Chyn | 057b743 | 2018-09-24 14:36:39 -0700 | [diff] [blame] | 26 | import java.util.ArrayList; |
| 27 | import java.util.List; |
Alex Klyubin | a95550f | 2015-07-16 16:32:23 -0700 | [diff] [blame] | 28 | |
Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 29 | /** |
| 30 | * @hide |
| 31 | */ |
| 32 | public abstract class KeymasterUtils { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 33 | |
Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 34 | private KeymasterUtils() {} |
| 35 | |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 36 | public static int getDigestOutputSizeBits(int keymasterDigest) { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 37 | switch (keymasterDigest) { |
| 38 | case KeymasterDefs.KM_DIGEST_NONE: |
| 39 | return -1; |
| 40 | case KeymasterDefs.KM_DIGEST_MD5: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 41 | return 128; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 42 | case KeymasterDefs.KM_DIGEST_SHA1: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 43 | return 160; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 44 | case KeymasterDefs.KM_DIGEST_SHA_2_224: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 45 | return 224; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 46 | case KeymasterDefs.KM_DIGEST_SHA_2_256: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 47 | return 256; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 48 | case KeymasterDefs.KM_DIGEST_SHA_2_384: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 49 | return 384; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 50 | case KeymasterDefs.KM_DIGEST_SHA_2_512: |
Alex Klyubin | 7ea5090 | 2015-04-30 10:50:45 -0700 | [diff] [blame] | 51 | return 512; |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 52 | default: |
| 53 | throw new IllegalArgumentException("Unknown digest: " + keymasterDigest); |
| 54 | } |
| 55 | } |
| 56 | |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 57 | public static boolean isKeymasterBlockModeIndCpaCompatibleWithSymmetricCrypto( |
| 58 | int keymasterBlockMode) { |
Alex Klyubin | 5927c9f | 2015-04-10 13:28:03 -0700 | [diff] [blame] | 59 | switch (keymasterBlockMode) { |
| 60 | case KeymasterDefs.KM_MODE_ECB: |
| 61 | return false; |
| 62 | case KeymasterDefs.KM_MODE_CBC: |
| 63 | case KeymasterDefs.KM_MODE_CTR: |
| 64 | case KeymasterDefs.KM_MODE_GCM: |
| 65 | return true; |
| 66 | default: |
| 67 | throw new IllegalArgumentException("Unsupported block mode: " + keymasterBlockMode); |
| 68 | } |
| 69 | } |
| 70 | |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 71 | public static boolean isKeymasterPaddingSchemeIndCpaCompatibleWithAsymmetricCrypto( |
| 72 | int keymasterPadding) { |
| 73 | switch (keymasterPadding) { |
| 74 | case KeymasterDefs.KM_PAD_NONE: |
| 75 | return false; |
| 76 | case KeymasterDefs.KM_PAD_RSA_OAEP: |
| 77 | case KeymasterDefs.KM_PAD_RSA_PKCS1_1_5_ENCRYPT: |
| 78 | return true; |
| 79 | default: |
| 80 | throw new IllegalArgumentException( |
Alex Klyubin | fdbc02a | 2015-06-29 14:39:29 -0700 | [diff] [blame] | 81 | "Unsupported asymmetric encryption padding scheme: " + keymasterPadding); |
Alex Klyubin | 3ceb1a0 | 2015-06-05 15:51:06 -0700 | [diff] [blame] | 82 | } |
| 83 | } |
| 84 | |
Max Bires | 1cfd95e | 2020-03-12 16:26:07 -0700 | [diff] [blame^] | 85 | private static void addSids(KeymasterArguments args, UserAuthArgs spec) { |
| 86 | // If both biometric and credential are accepted, then just use the root sid from gatekeeper |
| 87 | if (spec.getUserAuthenticationType() == (KeyProperties.AUTH_BIOMETRIC_STRONG |
| 88 | | KeyProperties.AUTH_DEVICE_CREDENTIAL)) { |
| 89 | if (spec.getBoundToSpecificSecureUserId() != GateKeeper.INVALID_SECURE_USER_ID) { |
| 90 | args.addUnsignedLong(KeymasterDefs.KM_TAG_USER_SECURE_ID, |
| 91 | KeymasterArguments.toUint64(spec.getBoundToSpecificSecureUserId())); |
| 92 | } else { |
| 93 | // The key is authorized for use for the specified amount of time after the user has |
| 94 | // authenticated. Whatever unlocks the secure lock screen should authorize this key. |
| 95 | args.addUnsignedLong(KeymasterDefs.KM_TAG_USER_SECURE_ID, |
| 96 | KeymasterArguments.toUint64(getRootSid())); |
| 97 | } |
| 98 | } else { |
| 99 | List<Long> sids = new ArrayList<>(); |
| 100 | if ((spec.getUserAuthenticationType() & KeyProperties.AUTH_BIOMETRIC_STRONG) != 0) { |
| 101 | final BiometricManager bm = KeyStore.getApplicationContext() |
| 102 | .getSystemService(BiometricManager.class); |
| 103 | |
| 104 | // TODO: Restore permission check in getAuthenticatorIds once the ID is no longer |
| 105 | // needed here. |
| 106 | |
| 107 | final long[] biometricSids = bm.getAuthenticatorIds(); |
| 108 | |
| 109 | if (biometricSids.length == 0) { |
| 110 | throw new IllegalStateException( |
| 111 | "At least one biometric must be enrolled to create keys requiring user" |
| 112 | + " authentication for every use"); |
| 113 | } |
| 114 | |
| 115 | if (spec.getBoundToSpecificSecureUserId() != GateKeeper.INVALID_SECURE_USER_ID) { |
| 116 | sids.add(spec.getBoundToSpecificSecureUserId()); |
| 117 | } else if (spec.isInvalidatedByBiometricEnrollment()) { |
| 118 | // The biometric-only SIDs will change on biometric enrollment or removal of all |
| 119 | // enrolled templates, invalidating the key. |
| 120 | for (long sid : biometricSids) { |
| 121 | sids.add(sid); |
| 122 | } |
| 123 | } else { |
| 124 | // The root SID will *not* change on fingerprint enrollment, or removal of all |
| 125 | // enrolled fingerprints, allowing the key to remain valid. |
| 126 | sids.add(getRootSid()); |
| 127 | } |
| 128 | } else if ((spec.getUserAuthenticationType() & KeyProperties.AUTH_DEVICE_CREDENTIAL) |
| 129 | != 0) { |
| 130 | sids.add(getRootSid()); |
| 131 | } else { |
| 132 | throw new IllegalStateException("Invalid or no authentication type specified."); |
| 133 | } |
| 134 | |
| 135 | for (int i = 0; i < sids.size(); i++) { |
| 136 | args.addUnsignedLong(KeymasterDefs.KM_TAG_USER_SECURE_ID, |
| 137 | KeymasterArguments.toUint64(sids.get(i))); |
| 138 | } |
| 139 | } |
| 140 | } |
| 141 | |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 142 | /** |
| 143 | * Adds keymaster arguments to express the key's authorization policy supported by user |
| 144 | * authentication. |
| 145 | * |
Max Bires | 04b682d | 2020-01-14 16:10:36 -0800 | [diff] [blame] | 146 | * @param args The arguments sent to keymaster that need to be populated from the spec |
| 147 | * @param spec The user authentication relevant portions of the spec passed in from the caller. |
| 148 | * This spec will be translated into the relevant keymaster tags to be loaded into args. |
Alex Klyubin | b6e6286 | 2015-07-06 10:31:07 -0700 | [diff] [blame] | 149 | * @throws IllegalStateException if user authentication is required but the system is in a wrong |
| 150 | * state (e.g., secure lock screen not set up) for generating or importing keys that |
| 151 | * require user authentication. |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 152 | */ |
Brian Young | 36716eb | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 153 | public static void addUserAuthArgs(KeymasterArguments args, UserAuthArgs spec) { |
Brian Young | 36716eb | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 154 | |
Brian Young | 5437b81 | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 155 | if (spec.isUserConfirmationRequired()) { |
David Zeuthen | a8e8b65 | 2017-10-25 14:25:55 -0400 | [diff] [blame] | 156 | args.addBoolean(KeymasterDefs.KM_TAG_TRUSTED_CONFIRMATION_REQUIRED); |
| 157 | } |
| 158 | |
Shawn Willden | 3c1830b | 2018-03-27 16:10:37 -0600 | [diff] [blame] | 159 | if (spec.isUserPresenceRequired()) { |
Brian Young | 5437b81 | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 160 | args.addBoolean(KeymasterDefs.KM_TAG_TRUSTED_USER_PRESENCE_REQUIRED); |
| 161 | } |
| 162 | |
Brian Young | 9272dab | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 163 | if (spec.isUnlockedDeviceRequired()) { |
| 164 | args.addBoolean(KeymasterDefs.KM_TAG_UNLOCKED_DEVICE_REQUIRED); |
| 165 | } |
| 166 | |
Brian Young | 5437b81 | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 167 | if (!spec.isUserAuthenticationRequired()) { |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 168 | args.addBoolean(KeymasterDefs.KM_TAG_NO_AUTH_REQUIRED); |
| 169 | return; |
| 170 | } |
| 171 | |
Max Bires | 04b682d | 2020-01-14 16:10:36 -0800 | [diff] [blame] | 172 | if (spec.getUserAuthenticationValidityDurationSeconds() == 0) { |
Kevin Chyn | 7d07c89 | 2020-02-18 18:18:17 -0800 | [diff] [blame] | 173 | // Every use of this key needs to be authorized by the user. |
Max Bires | 1cfd95e | 2020-03-12 16:26:07 -0700 | [diff] [blame^] | 174 | addSids(args, spec); |
Max Bires | 04b682d | 2020-01-14 16:10:36 -0800 | [diff] [blame] | 175 | args.addEnum(KeymasterDefs.KM_TAG_USER_AUTH_TYPE, spec.getUserAuthenticationType()); |
Kevin Chyn | 057b743 | 2018-09-24 14:36:39 -0700 | [diff] [blame] | 176 | |
Brian Young | 5437b81 | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 177 | if (spec.isUserAuthenticationValidWhileOnBody()) { |
Shawn Willden | adef496 | 2016-01-29 07:07:16 -0700 | [diff] [blame] | 178 | throw new ProviderException("Key validity extension while device is on-body is not " |
| 179 | + "supported for keys requiring fingerprint authentication"); |
| 180 | } |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 181 | } else { |
Max Bires | 1cfd95e | 2020-03-12 16:26:07 -0700 | [diff] [blame^] | 182 | addSids(args, spec); |
Max Bires | a811787 | 2020-03-11 01:53:59 -0700 | [diff] [blame] | 183 | args.addEnum(KeymasterDefs.KM_TAG_USER_AUTH_TYPE, spec.getUserAuthenticationType()); |
Alex Klyubin | ae6cb7a | 2015-06-22 18:09:35 -0700 | [diff] [blame] | 184 | args.addUnsignedInt(KeymasterDefs.KM_TAG_AUTH_TIMEOUT, |
Brian Young | 5437b81 | 2018-02-23 18:04:20 +0000 | [diff] [blame] | 185 | spec.getUserAuthenticationValidityDurationSeconds()); |
| 186 | if (spec.isUserAuthenticationValidWhileOnBody()) { |
Shawn Willden | adef496 | 2016-01-29 07:07:16 -0700 | [diff] [blame] | 187 | args.addBoolean(KeymasterDefs.KM_TAG_ALLOW_WHILE_ON_BODY); |
| 188 | } |
Alex Klyubin | 1eda77a | 2015-04-28 14:21:01 -0700 | [diff] [blame] | 189 | } |
| 190 | } |
Alex Klyubin | a95550f | 2015-07-16 16:32:23 -0700 | [diff] [blame] | 191 | |
| 192 | /** |
| 193 | * Adds {@code KM_TAG_MIN_MAC_LENGTH} tag, if necessary, to the keymaster arguments for |
| 194 | * generating or importing a key. This tag may only be needed for symmetric keys (e.g., HMAC, |
| 195 | * AES-GCM). |
| 196 | */ |
| 197 | public static void addMinMacLengthAuthorizationIfNecessary(KeymasterArguments args, |
| 198 | int keymasterAlgorithm, |
| 199 | int[] keymasterBlockModes, |
| 200 | int[] keymasterDigests) { |
| 201 | switch (keymasterAlgorithm) { |
| 202 | case KeymasterDefs.KM_ALGORITHM_AES: |
| 203 | if (com.android.internal.util.ArrayUtils.contains( |
| 204 | keymasterBlockModes, KeymasterDefs.KM_MODE_GCM)) { |
| 205 | // AES GCM key needs the minimum length of AEAD tag specified. |
| 206 | args.addUnsignedInt(KeymasterDefs.KM_TAG_MIN_MAC_LENGTH, |
| 207 | AndroidKeyStoreAuthenticatedAESCipherSpi.GCM |
| 208 | .MIN_SUPPORTED_TAG_LENGTH_BITS); |
| 209 | } |
| 210 | break; |
| 211 | case KeymasterDefs.KM_ALGORITHM_HMAC: |
| 212 | // HMAC key needs the minimum length of MAC set to the output size of the associated |
| 213 | // digest. This is because we do not offer a way to generate shorter MACs and |
| 214 | // don't offer a way to verify MACs (other than by generating them). |
| 215 | if (keymasterDigests.length != 1) { |
| 216 | throw new ProviderException( |
| 217 | "Unsupported number of authorized digests for HMAC key: " |
| 218 | + keymasterDigests.length |
| 219 | + ". Exactly one digest must be authorized"); |
| 220 | } |
| 221 | int keymasterDigest = keymasterDigests[0]; |
| 222 | int digestOutputSizeBits = getDigestOutputSizeBits(keymasterDigest); |
| 223 | if (digestOutputSizeBits == -1) { |
| 224 | throw new ProviderException( |
| 225 | "HMAC key authorized for unsupported digest: " |
| 226 | + KeyProperties.Digest.fromKeymaster(keymasterDigest)); |
| 227 | } |
| 228 | args.addUnsignedInt(KeymasterDefs.KM_TAG_MIN_MAC_LENGTH, digestOutputSizeBits); |
| 229 | break; |
| 230 | } |
| 231 | } |
Shawn Willden | c38eae5 | 2016-02-22 23:28:34 +0000 | [diff] [blame] | 232 | |
| 233 | private static long getRootSid() { |
| 234 | long rootSid = GateKeeper.getSecureUserId(); |
| 235 | if (rootSid == 0) { |
| 236 | throw new IllegalStateException("Secure lock screen must be enabled" |
| 237 | + " to create keys requiring user authentication"); |
| 238 | } |
| 239 | return rootSid; |
| 240 | } |
Alex Klyubin | d23a1f7 | 2015-03-27 14:39:28 -0700 | [diff] [blame] | 241 | } |