Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | package com.android.server.biometrics; |
| 18 | |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 19 | import static android.hardware.biometrics.BiometricManager.Authenticators; |
| 20 | |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 21 | import android.content.Context; |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 22 | import android.hardware.biometrics.BiometricConstants; |
| 23 | import android.hardware.biometrics.BiometricManager; |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 24 | import android.hardware.biometrics.BiometricPrompt; |
Curtis Belmonte | 6601bc3 | 2020-01-09 17:46:31 -0800 | [diff] [blame] | 25 | import android.hardware.biometrics.BiometricPrompt.AuthenticationResultType; |
Ilya Matyukhin | ef410e3 | 2020-02-04 13:39:48 -0800 | [diff] [blame] | 26 | import android.hardware.biometrics.IBiometricNativeHandle; |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 27 | import android.os.Build; |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 28 | import android.os.Bundle; |
Ilya Matyukhin | ef410e3 | 2020-02-04 13:39:48 -0800 | [diff] [blame] | 29 | import android.os.NativeHandle; |
Kevin Chyn | 16cac92f | 2019-06-12 12:52:16 -0700 | [diff] [blame] | 30 | import android.os.UserHandle; |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 31 | import android.provider.Settings; |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 32 | import android.util.Slog; |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 33 | |
Ilya Matyukhin | ef410e3 | 2020-02-04 13:39:48 -0800 | [diff] [blame] | 34 | import java.io.FileDescriptor; |
| 35 | import java.io.IOException; |
| 36 | |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 37 | public class Utils { |
| 38 | public static boolean isDebugEnabled(Context context, int targetUserId) { |
Kevin Chyn | 16cac92f | 2019-06-12 12:52:16 -0700 | [diff] [blame] | 39 | if (targetUserId == UserHandle.USER_NULL) { |
| 40 | return false; |
| 41 | } |
| 42 | |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 43 | if (!(Build.IS_ENG || Build.IS_USERDEBUG)) { |
| 44 | return false; |
| 45 | } |
| 46 | |
| 47 | if (Settings.Secure.getIntForUser(context.getContentResolver(), |
| 48 | Settings.Secure.BIOMETRIC_DEBUG_ENABLED, 0, |
| 49 | targetUserId) == 0) { |
| 50 | return false; |
| 51 | } |
| 52 | return true; |
| 53 | } |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 54 | |
| 55 | /** |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 56 | * Combines {@link BiometricPrompt#KEY_ALLOW_DEVICE_CREDENTIAL} with |
| 57 | * {@link BiometricPrompt#KEY_AUTHENTICATORS_ALLOWED}, as the former is not flexible enough. |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 58 | */ |
| 59 | public static void combineAuthenticatorBundles(Bundle bundle) { |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 60 | // Cache and remove explicit ALLOW_DEVICE_CREDENTIAL boolean flag from the bundle. |
| 61 | final boolean deviceCredentialAllowed = |
| 62 | bundle.getBoolean(BiometricPrompt.KEY_ALLOW_DEVICE_CREDENTIAL, false); |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 63 | bundle.remove(BiometricPrompt.KEY_ALLOW_DEVICE_CREDENTIAL); |
| 64 | |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 65 | final @Authenticators.Types int authenticators; |
| 66 | if (bundle.containsKey(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED)) { |
| 67 | // Ignore ALLOW_DEVICE_CREDENTIAL flag if AUTH_TYPES_ALLOWED is defined. |
| 68 | authenticators = bundle.getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED, 0); |
| 69 | } else { |
| 70 | // Otherwise, use ALLOW_DEVICE_CREDENTIAL flag along with Weak+ biometrics by default. |
| 71 | authenticators = deviceCredentialAllowed |
| 72 | ? Authenticators.DEVICE_CREDENTIAL | Authenticators.BIOMETRIC_WEAK |
| 73 | : Authenticators.BIOMETRIC_WEAK; |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 74 | } |
Curtis Belmonte | 13eb581 | 2019-10-22 14:17:30 -0700 | [diff] [blame] | 75 | |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 76 | bundle.putInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED, authenticators); |
| 77 | } |
| 78 | |
| 79 | /** |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 80 | * @param authenticators composed of one or more values from {@link Authenticators} |
| 81 | * @return true if device credential is allowed. |
| 82 | */ |
Kevin Chyn | 98e9225 | 2020-02-06 16:28:33 -0800 | [diff] [blame] | 83 | public static boolean isCredentialRequested(@Authenticators.Types int authenticators) { |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 84 | return (authenticators & Authenticators.DEVICE_CREDENTIAL) != 0; |
| 85 | } |
| 86 | |
| 87 | /** |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 88 | * @param bundle should be first processed by {@link #combineAuthenticatorBundles(Bundle)} |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 89 | * @return true if device credential is allowed. |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 90 | */ |
Kevin Chyn | 98e9225 | 2020-02-06 16:28:33 -0800 | [diff] [blame] | 91 | public static boolean isCredentialRequested(Bundle bundle) { |
| 92 | return isCredentialRequested(bundle.getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED)); |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | /** |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 96 | * Checks if any of the publicly defined strengths are set. |
| 97 | * |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 98 | * @param authenticators composed of one or more values from {@link Authenticators} |
| 99 | * @return minimal allowed biometric strength or 0 if biometric authentication is not allowed. |
| 100 | */ |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 101 | public static int getPublicBiometricStrength(@Authenticators.Types int authenticators) { |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 102 | // Only biometrics WEAK and above are allowed to integrate with the public APIs. |
| 103 | return authenticators & Authenticators.BIOMETRIC_WEAK; |
| 104 | } |
| 105 | |
| 106 | /** |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 107 | * Checks if any of the publicly defined strengths are set. |
| 108 | * |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 109 | * @param bundle should be first processed by {@link #combineAuthenticatorBundles(Bundle)} |
| 110 | * @return minimal allowed biometric strength or 0 if biometric authentication is not allowed. |
| 111 | */ |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 112 | public static int getPublicBiometricStrength(Bundle bundle) { |
| 113 | return getPublicBiometricStrength( |
| 114 | bundle.getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED)); |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | /** |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 118 | * Checks if any of the publicly defined strengths are set. |
| 119 | * |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 120 | * @param bundle should be first processed by {@link #combineAuthenticatorBundles(Bundle)} |
| 121 | * @return true if biometric authentication is allowed. |
| 122 | */ |
Kevin Chyn | 98e9225 | 2020-02-06 16:28:33 -0800 | [diff] [blame] | 123 | public static boolean isBiometricRequested(Bundle bundle) { |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 124 | return getPublicBiometricStrength(bundle) != 0; |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | /** |
| 128 | * @param sensorStrength the strength of the sensor |
| 129 | * @param requestedStrength the strength that it must meet |
| 130 | * @return true only if the sensor is at least as strong as the requested strength |
| 131 | */ |
| 132 | public static boolean isAtLeastStrength(int sensorStrength, int requestedStrength) { |
Kevin Chyn | 7d07c89 | 2020-02-18 18:18:17 -0800 | [diff] [blame^] | 133 | // Clear out any bits that are not reserved for biometric |
| 134 | sensorStrength = sensorStrength & Authenticators.BIOMETRIC_MIN_STRENGTH; |
| 135 | |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 136 | // If the authenticator contains bits outside of the requested strength, it is too weak. |
Kevin Chyn | 7d07c89 | 2020-02-18 18:18:17 -0800 | [diff] [blame^] | 137 | if ((sensorStrength & ~requestedStrength) != 0) { |
| 138 | return false; |
| 139 | } |
| 140 | |
| 141 | for (int i = Authenticators.BIOMETRIC_MAX_STRENGTH; |
| 142 | i <= requestedStrength; i = i << 1 | 1) { |
| 143 | if (i == sensorStrength) { |
| 144 | return true; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | Slog.e(BiometricService.TAG, "Unknown sensorStrength: " + sensorStrength |
| 149 | + ", requestedStrength: " + requestedStrength); |
| 150 | return false; |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 151 | } |
| 152 | |
| 153 | /** |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 154 | * Checks if the authenticator configuration is a valid combination of the public APIs |
| 155 | * @param bundle |
| 156 | * @return |
| 157 | */ |
| 158 | public static boolean isValidAuthenticatorConfig(Bundle bundle) { |
| 159 | final int authenticators = bundle.getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED); |
| 160 | return isValidAuthenticatorConfig(authenticators); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * Checks if the authenticator configuration is a valid combination of the public APIs |
| 165 | * @param authenticators |
| 166 | * @return |
| 167 | */ |
| 168 | public static boolean isValidAuthenticatorConfig(int authenticators) { |
| 169 | // The caller is not required to set the authenticators. But if they do, check the below. |
| 170 | if (authenticators == 0) { |
| 171 | return true; |
| 172 | } |
| 173 | |
| 174 | // Check if any of the non-biometric and non-credential bits are set. If so, this is |
| 175 | // invalid. |
| 176 | final int testBits = ~(Authenticators.DEVICE_CREDENTIAL |
| 177 | | Authenticators.BIOMETRIC_MIN_STRENGTH); |
| 178 | if ((authenticators & testBits) != 0) { |
| 179 | Slog.e(BiometricService.TAG, "Non-biometric, non-credential bits found." |
| 180 | + " Authenticators: " + authenticators); |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | // Check that biometrics bits are either NONE, WEAK, or STRONG. If NONE, DEVICE_CREDENTIAL |
| 185 | // should be set. |
| 186 | final int biometricBits = authenticators & Authenticators.BIOMETRIC_MIN_STRENGTH; |
| 187 | if (biometricBits == Authenticators.EMPTY_SET |
Kevin Chyn | 98e9225 | 2020-02-06 16:28:33 -0800 | [diff] [blame] | 188 | && isCredentialRequested(authenticators)) { |
Kevin Chyn | d04b43d | 2019-12-13 12:56:41 -0800 | [diff] [blame] | 189 | return true; |
| 190 | } else if (biometricBits == Authenticators.BIOMETRIC_STRONG) { |
| 191 | return true; |
| 192 | } else if (biometricBits == Authenticators.BIOMETRIC_WEAK) { |
| 193 | return true; |
| 194 | } |
| 195 | |
| 196 | Slog.e(BiometricService.TAG, "Unsupported biometric flags. Authenticators: " |
| 197 | + authenticators); |
| 198 | // Non-supported biometric flags are being used |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | /** |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 203 | * Converts error codes from BiometricConstants, which are used in most of the internal plumbing |
| 204 | * and eventually returned to {@link BiometricPrompt.AuthenticationCallback} to public |
| 205 | * {@link BiometricManager} constants, which are used by APIs such as |
| 206 | * {@link BiometricManager#canAuthenticate(int)} |
| 207 | * |
| 208 | * @param biometricConstantsCode see {@link BiometricConstants} |
| 209 | * @return see {@link BiometricManager} |
| 210 | */ |
| 211 | public static int biometricConstantsToBiometricManager(int biometricConstantsCode) { |
| 212 | final int biometricManagerCode; |
| 213 | |
| 214 | switch (biometricConstantsCode) { |
| 215 | case BiometricConstants.BIOMETRIC_SUCCESS: |
| 216 | biometricManagerCode = BiometricManager.BIOMETRIC_SUCCESS; |
| 217 | break; |
| 218 | case BiometricConstants.BIOMETRIC_ERROR_NO_BIOMETRICS: |
| 219 | case BiometricConstants.BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL: |
| 220 | biometricManagerCode = BiometricManager.BIOMETRIC_ERROR_NONE_ENROLLED; |
| 221 | break; |
| 222 | case BiometricConstants.BIOMETRIC_ERROR_HW_UNAVAILABLE: |
| 223 | biometricManagerCode = BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE; |
| 224 | break; |
| 225 | case BiometricConstants.BIOMETRIC_ERROR_HW_NOT_PRESENT: |
| 226 | biometricManagerCode = BiometricManager.BIOMETRIC_ERROR_NO_HARDWARE; |
| 227 | break; |
Kevin Chyn | 98e9225 | 2020-02-06 16:28:33 -0800 | [diff] [blame] | 228 | case BiometricConstants.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED: |
| 229 | biometricManagerCode = BiometricManager.BIOMETRIC_ERROR_SECURITY_UPDATE_REQUIRED; |
| 230 | break; |
Ilya Matyukhin | 30f1dd8 | 2019-11-18 18:08:56 -0800 | [diff] [blame] | 231 | default: |
| 232 | Slog.e(BiometricService.TAG, "Unhandled result code: " + biometricConstantsCode); |
| 233 | biometricManagerCode = BiometricManager.BIOMETRIC_ERROR_HW_UNAVAILABLE; |
| 234 | break; |
| 235 | } |
| 236 | return biometricManagerCode; |
Kevin Chyn | c70d6b8 | 2019-10-03 15:32:37 -0700 | [diff] [blame] | 237 | } |
Curtis Belmonte | 6601bc3 | 2020-01-09 17:46:31 -0800 | [diff] [blame] | 238 | |
| 239 | /** |
| 240 | * Converts a {@link BiometricPrompt} dismissal reason to an authentication type at the level of |
| 241 | * granularity supported by {@link BiometricPrompt.AuthenticationResult}. |
| 242 | * |
| 243 | * @param reason The reason that the {@link BiometricPrompt} was dismissed. Must be one of: |
| 244 | * {@link BiometricPrompt#DISMISSED_REASON_CREDENTIAL_CONFIRMED}, |
| 245 | * {@link BiometricPrompt#DISMISSED_REASON_BIOMETRIC_CONFIRMED}, or |
| 246 | * {@link BiometricPrompt#DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED} |
| 247 | * @return An integer representing the authentication type for {@link |
| 248 | * BiometricPrompt.AuthenticationResult}. |
| 249 | * @throws IllegalArgumentException if given an invalid dismissal reason. |
| 250 | */ |
| 251 | public static @AuthenticationResultType int getAuthenticationTypeForResult(int reason) { |
| 252 | switch (reason) { |
| 253 | case BiometricPrompt.DISMISSED_REASON_CREDENTIAL_CONFIRMED: |
| 254 | return BiometricPrompt.AUTHENTICATION_RESULT_TYPE_DEVICE_CREDENTIAL; |
| 255 | |
| 256 | case BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRMED: |
| 257 | case BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED: |
| 258 | return BiometricPrompt.AUTHENTICATION_RESULT_TYPE_BIOMETRIC; |
| 259 | |
| 260 | default: |
| 261 | throw new IllegalArgumentException("Unsupported dismissal reason: " + reason); |
| 262 | } |
| 263 | } |
Ilya Matyukhin | ef410e3 | 2020-02-04 13:39:48 -0800 | [diff] [blame] | 264 | |
| 265 | /** |
| 266 | * Converts an {@link IBiometricNativeHandle} to a {@link NativeHandle} by duplicating the |
| 267 | * the underlying file descriptors. |
| 268 | * |
| 269 | * Both the original and new handle must be closed after use. |
| 270 | * |
| 271 | * @param h {@link IBiometricNativeHandle} received as a binder call argument. Usually used to |
| 272 | * identify a WindowManager window. Can be null. |
| 273 | * @return A {@link NativeHandle} representation of {@code h}. Will be null if either {@code h} |
| 274 | * or its contents are null. |
| 275 | */ |
| 276 | public static NativeHandle dupNativeHandle(IBiometricNativeHandle h) { |
| 277 | NativeHandle handle = null; |
| 278 | if (h != null && h.fds != null && h.ints != null) { |
| 279 | FileDescriptor[] fds = new FileDescriptor[h.fds.length]; |
| 280 | for (int i = 0; i < h.fds.length; ++i) { |
| 281 | try { |
| 282 | fds[i] = h.fds[i].dup().getFileDescriptor(); |
| 283 | } catch (IOException e) { |
| 284 | return null; |
| 285 | } |
| 286 | } |
| 287 | handle = new NativeHandle(fds, h.ints, true /* own */); |
| 288 | } |
| 289 | return handle; |
| 290 | } |
Kevin Chyn | 4858da4 | 2019-04-11 13:02:56 -0700 | [diff] [blame] | 291 | } |