| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.hardware.biometrics; |
| 18 |
|
| 19 |
import static android.Manifest.permission.TEST_BIOMETRIC; |
| 20 |
import static android.Manifest.permission.USE_BIOMETRIC; |
| 21 |
import static android.Manifest.permission.USE_BIOMETRIC_INTERNAL; |
| 22 |
import static android.hardware.biometrics.BiometricManager.Authenticators; |
| 23 |
|
| 24 |
import android.annotation.CallbackExecutor; |
| 25 |
import android.annotation.IntDef; |
| 26 |
import android.annotation.NonNull; |
| 27 |
import android.annotation.Nullable; |
| 28 |
import android.annotation.RequiresPermission; |
| 29 |
import android.annotation.TestApi; |
| 30 |
import android.content.Context; |
| 31 |
import android.content.DialogInterface; |
| 32 |
import android.hardware.face.FaceManager; |
| 33 |
import android.hardware.fingerprint.FingerprintManager; |
| 34 |
import android.os.Binder; |
| 35 |
import android.os.CancellationSignal; |
| 36 |
import android.os.IBinder; |
| 37 |
import android.os.Parcel; |
| 38 |
import android.os.RemoteException; |
| 39 |
import android.os.ServiceManager; |
| 40 |
import android.security.identity.IdentityCredential; |
| 41 |
import android.security.identity.PresentationSession; |
| 42 |
import android.security.keystore.KeyProperties; |
| 43 |
import android.text.TextUtils; |
| 44 |
import android.util.Log; |
| 45 |
|
| 46 |
import com.android.internal.R; |
| 47 |
import com.android.internal.util.FrameworkStatsLog; |
| 48 |
|
| 49 |
import java.lang.annotation.Retention; |
| 50 |
import java.lang.annotation.RetentionPolicy; |
| 51 |
import java.security.Signature; |
| 52 |
import java.util.List; |
| 53 |
import java.util.concurrent.Executor; |
| 54 |
|
| 55 |
import javax.crypto.Cipher; |
| 56 |
import javax.crypto.Mac; |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public class BiometricPrompt implements BiometricAuthenticator, BiometricConstants { |
| 62 |
|
| 63 |
private static final String TAG = "BiometricPrompt"; |
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static final int HIDE_DIALOG_DELAY = 2000; |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public static final int DISMISSED_REASON_BIOMETRIC_CONFIRMED = 1; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
public static final int DISMISSED_REASON_NEGATIVE = 2; |
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
public static final int DISMISSED_REASON_USER_CANCEL = 3; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
public static final int DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED = 4; |
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
public static final int DISMISSED_REASON_ERROR = 5; |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
public static final int DISMISSED_REASON_SERVER_REQUESTED = 6; |
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public static final int DISMISSED_REASON_CREDENTIAL_CONFIRMED = 7; |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
@IntDef({DISMISSED_REASON_BIOMETRIC_CONFIRMED, |
| 120 |
DISMISSED_REASON_NEGATIVE, |
| 121 |
DISMISSED_REASON_USER_CANCEL, |
| 122 |
DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED, |
| 123 |
DISMISSED_REASON_ERROR, |
| 124 |
DISMISSED_REASON_SERVER_REQUESTED, |
| 125 |
DISMISSED_REASON_CREDENTIAL_CONFIRMED}) |
| 126 |
@Retention(RetentionPolicy.SOURCE) |
| 127 |
public @interface DismissedReason {} |
| 128 |
|
| 129 |
private static class ButtonInfo { |
| 130 |
Executor executor; |
| 131 |
DialogInterface.OnClickListener listener; |
| 132 |
ButtonInfo(Executor ex, DialogInterface.OnClickListener l) { |
| 133 |
executor = ex; |
| 134 |
listener = l; |
| 135 |
} |
| 136 |
} |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
public static class Builder { |
| 142 |
private PromptInfo mPromptInfo; |
| 143 |
private ButtonInfo mNegativeButtonInfo; |
| 144 |
private Context mContext; |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
public Builder(Context context) { |
| 151 |
mPromptInfo = new PromptInfo(); |
| 152 |
mContext = context; |
| 153 |
} |
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
@NonNull |
| 161 |
public Builder setTitle(@NonNull CharSequence title) { |
| 162 |
mPromptInfo.setTitle(title); |
| 163 |
return this; |
| 164 |
} |
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
@RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 173 |
@NonNull |
| 174 |
public Builder setUseDefaultTitle() { |
| 175 |
mPromptInfo.setUseDefaultTitle(true); |
| 176 |
return this; |
| 177 |
} |
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
@NonNull |
| 185 |
public Builder setSubtitle(@NonNull CharSequence subtitle) { |
| 186 |
mPromptInfo.setSubtitle(subtitle); |
| 187 |
return this; |
| 188 |
} |
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
@NonNull |
| 196 |
public Builder setDescription(@NonNull CharSequence description) { |
| 197 |
mPromptInfo.setDescription(description); |
| 198 |
return this; |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
@RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 208 |
@NonNull |
| 209 |
public Builder setTextForDeviceCredential( |
| 210 |
@Nullable CharSequence title, |
| 211 |
@Nullable CharSequence subtitle, |
| 212 |
@Nullable CharSequence description) { |
| 213 |
if (title != null) { |
| 214 |
mPromptInfo.setDeviceCredentialTitle(title); |
| 215 |
} |
| 216 |
if (subtitle != null) { |
| 217 |
mPromptInfo.setDeviceCredentialSubtitle(subtitle); |
| 218 |
} |
| 219 |
if (description != null) { |
| 220 |
mPromptInfo.setDeviceCredentialDescription(description); |
| 221 |
} |
| 222 |
return this; |
| 223 |
} |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
@NonNull |
| 240 |
public Builder setNegativeButton(@NonNull CharSequence text, |
| 241 |
@NonNull @CallbackExecutor Executor executor, |
| 242 |
@NonNull DialogInterface.OnClickListener listener) { |
| 243 |
if (TextUtils.isEmpty(text)) { |
| 244 |
throw new IllegalArgumentException("Text must be set and non-empty"); |
| 245 |
} |
| 246 |
if (executor == null) { |
| 247 |
throw new IllegalArgumentException("Executor must not be null"); |
| 248 |
} |
| 249 |
if (listener == null) { |
| 250 |
throw new IllegalArgumentException("Listener must not be null"); |
| 251 |
} |
| 252 |
mPromptInfo.setNegativeButtonText(text); |
| 253 |
mNegativeButtonInfo = new ButtonInfo(executor, listener); |
| 254 |
return this; |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
@NonNull |
| 278 |
public Builder setConfirmationRequired(boolean requireConfirmation) { |
| 279 |
mPromptInfo.setConfirmationRequested(requireConfirmation); |
| 280 |
return this; |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
@Deprecated |
| 302 |
@NonNull |
| 303 |
public Builder setDeviceCredentialAllowed(boolean allowed) { |
| 304 |
mPromptInfo.setDeviceCredentialAllowed(allowed); |
| 305 |
return this; |
| 306 |
} |
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
|
| 319 |
|
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
|
| 326 |
|
| 327 |
|
| 328 |
|
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
@NonNull |
| 341 |
public Builder setAllowedAuthenticators(@Authenticators.Types int authenticators) { |
| 342 |
mPromptInfo.setAuthenticators(authenticators); |
| 343 |
return this; |
| 344 |
} |
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
|
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
@TestApi |
| 365 |
@NonNull |
| 366 |
@RequiresPermission(anyOf = {TEST_BIOMETRIC, USE_BIOMETRIC_INTERNAL}) |
| 367 |
public Builder setAllowedSensorIds(@NonNull List<Integer> sensorIds) { |
| 368 |
mPromptInfo.setAllowedSensorIds(sensorIds); |
| 369 |
return this; |
| 370 |
} |
| 371 |
|
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
@TestApi |
| 379 |
@NonNull |
| 380 |
@RequiresPermission(anyOf = {TEST_BIOMETRIC, USE_BIOMETRIC_INTERNAL}) |
| 381 |
public Builder setAllowBackgroundAuthentication(boolean allow) { |
| 382 |
mPromptInfo.setAllowBackgroundAuthentication(allow); |
| 383 |
return this; |
| 384 |
} |
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
|
| 390 |
|
| 391 |
|
| 392 |
|
| 393 |
@NonNull |
| 394 |
public Builder setDisallowBiometricsIfPolicyExists(boolean checkDevicePolicyManager) { |
| 395 |
mPromptInfo.setDisallowBiometricsIfPolicyExists(checkDevicePolicyManager); |
| 396 |
return this; |
| 397 |
} |
| 398 |
|
| 399 |
|
| 400 |
|
| 401 |
|
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
@NonNull |
| 406 |
public Builder setReceiveSystemEvents(boolean set) { |
| 407 |
mPromptInfo.setReceiveSystemEvents(set); |
| 408 |
return this; |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
@NonNull |
| 419 |
public Builder setIgnoreEnrollmentState(boolean ignoreEnrollmentState) { |
| 420 |
mPromptInfo.setIgnoreEnrollmentState(ignoreEnrollmentState); |
| 421 |
return this; |
| 422 |
} |
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
@NonNull |
| 431 |
public Builder setIsForLegacyFingerprintManager(int sensorId) { |
| 432 |
mPromptInfo.setIsForLegacyFingerprintManager(sensorId); |
| 433 |
return this; |
| 434 |
} |
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
|
| 439 |
|
| 440 |
|
| 441 |
|
| 442 |
|
| 443 |
|
| 444 |
@NonNull |
| 445 |
public BiometricPrompt build() { |
| 446 |
final CharSequence title = mPromptInfo.getTitle(); |
| 447 |
final CharSequence negative = mPromptInfo.getNegativeButtonText(); |
| 448 |
final boolean useDefaultTitle = mPromptInfo.isUseDefaultTitle(); |
| 449 |
final boolean deviceCredentialAllowed = mPromptInfo.isDeviceCredentialAllowed(); |
| 450 |
final @Authenticators.Types int authenticators = mPromptInfo.getAuthenticators(); |
| 451 |
final boolean willShowDeviceCredentialButton = deviceCredentialAllowed |
| 452 |
|| isCredentialAllowed(authenticators); |
| 453 |
|
| 454 |
if (TextUtils.isEmpty(title) && !useDefaultTitle) { |
| 455 |
throw new IllegalArgumentException("Title must be set and non-empty"); |
| 456 |
} else if (TextUtils.isEmpty(negative) && !willShowDeviceCredentialButton) { |
| 457 |
throw new IllegalArgumentException("Negative text must be set and non-empty"); |
| 458 |
} else if (!TextUtils.isEmpty(negative) && willShowDeviceCredentialButton) { |
| 459 |
throw new IllegalArgumentException("Can't have both negative button behavior" |
| 460 |
+ " and device credential enabled"); |
| 461 |
} |
| 462 |
return new BiometricPrompt(mContext, mPromptInfo, mNegativeButtonInfo); |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
private class OnAuthenticationCancelListener implements CancellationSignal.OnCancelListener { |
| 467 |
private final long mAuthRequestId; |
| 468 |
|
| 469 |
OnAuthenticationCancelListener(long id) { |
| 470 |
mAuthRequestId = id; |
| 471 |
} |
| 472 |
|
| 473 |
@Override |
| 474 |
public void onCancel() { |
| 475 |
Log.d(TAG, "Cancel BP authentication requested for: " + mAuthRequestId); |
| 476 |
cancelAuthentication(mAuthRequestId); |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
private final IBinder mToken = new Binder(); |
| 481 |
private final Context mContext; |
| 482 |
private final IAuthService mService; |
| 483 |
private final PromptInfo mPromptInfo; |
| 484 |
private final ButtonInfo mNegativeButtonInfo; |
| 485 |
|
| 486 |
private CryptoObject mCryptoObject; |
| 487 |
private Executor mExecutor; |
| 488 |
private AuthenticationCallback mAuthenticationCallback; |
| 489 |
|
| 490 |
private final IBiometricServiceReceiver mBiometricServiceReceiver = |
| 491 |
new IBiometricServiceReceiver.Stub() { |
| 492 |
|
| 493 |
@Override |
| 494 |
public void onAuthenticationSucceeded(@AuthenticationResultType int authenticationType) { |
| 495 |
mExecutor.execute(() -> { |
| 496 |
final AuthenticationResult result = |
| 497 |
new AuthenticationResult(mCryptoObject, authenticationType); |
| 498 |
mAuthenticationCallback.onAuthenticationSucceeded(result); |
| 499 |
}); |
| 500 |
} |
| 501 |
|
| 502 |
@Override |
| 503 |
public void onAuthenticationFailed() { |
| 504 |
mExecutor.execute(() -> { |
| 505 |
mAuthenticationCallback.onAuthenticationFailed(); |
| 506 |
}); |
| 507 |
} |
| 508 |
|
| 509 |
@Override |
| 510 |
public void onError(@BiometricAuthenticator.Modality int modality, int error, |
| 511 |
int vendorCode) { |
| 512 |
|
| 513 |
String errorMessage = null; |
| 514 |
switch (modality) { |
| 515 |
case TYPE_FACE: |
| 516 |
errorMessage = FaceManager.getErrorString(mContext, error, vendorCode); |
| 517 |
break; |
| 518 |
|
| 519 |
case TYPE_FINGERPRINT: |
| 520 |
errorMessage = FingerprintManager.getErrorString(mContext, error, vendorCode); |
| 521 |
break; |
| 522 |
} |
| 523 |
|
| 524 |
|
| 525 |
|
| 526 |
if (errorMessage == null) { |
| 527 |
switch (error) { |
| 528 |
case BIOMETRIC_ERROR_CANCELED: |
| 529 |
errorMessage = mContext.getString(R.string.biometric_error_canceled); |
| 530 |
break; |
| 531 |
case BIOMETRIC_ERROR_USER_CANCELED: |
| 532 |
errorMessage = mContext.getString(R.string.biometric_error_user_canceled); |
| 533 |
break; |
| 534 |
case BIOMETRIC_ERROR_HW_NOT_PRESENT: |
| 535 |
errorMessage = mContext.getString(R.string.biometric_error_hw_unavailable); |
| 536 |
break; |
| 537 |
case BIOMETRIC_ERROR_NO_DEVICE_CREDENTIAL: |
| 538 |
errorMessage = mContext.getString( |
| 539 |
R.string.biometric_error_device_not_secured); |
| 540 |
break; |
| 541 |
default: |
| 542 |
Log.e(TAG, "Unknown error, modality: " + modality |
| 543 |
+ " error: " + error |
| 544 |
+ " vendorCode: " + vendorCode); |
| 545 |
errorMessage = mContext.getString(R.string.biometric_error_generic); |
| 546 |
break; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
final String stringToSend = errorMessage; |
| 551 |
mExecutor.execute(() -> { |
| 552 |
mAuthenticationCallback.onAuthenticationError(error, stringToSend); |
| 553 |
}); |
| 554 |
} |
| 555 |
|
| 556 |
@Override |
| 557 |
public void onAcquired(int acquireInfo, String message) { |
| 558 |
mExecutor.execute(() -> { |
| 559 |
mAuthenticationCallback.onAuthenticationHelp(acquireInfo, message); |
| 560 |
}); |
| 561 |
} |
| 562 |
|
| 563 |
@Override |
| 564 |
public void onDialogDismissed(int reason) { |
| 565 |
|
| 566 |
if (reason == DISMISSED_REASON_NEGATIVE) { |
| 567 |
mNegativeButtonInfo.executor.execute(() -> { |
| 568 |
mNegativeButtonInfo.listener.onClick(null, DialogInterface.BUTTON_NEGATIVE); |
| 569 |
}); |
| 570 |
} else { |
| 571 |
Log.e(TAG, "Unknown reason: " + reason); |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
@Override |
| 576 |
public void onSystemEvent(int event) { |
| 577 |
mExecutor.execute(() -> { |
| 578 |
mAuthenticationCallback.onSystemEvent(event); |
| 579 |
}); |
| 580 |
} |
| 581 |
}; |
| 582 |
|
| 583 |
private BiometricPrompt(Context context, PromptInfo promptInfo, ButtonInfo negativeButtonInfo) { |
| 584 |
mContext = context; |
| 585 |
mPromptInfo = promptInfo; |
| 586 |
mNegativeButtonInfo = negativeButtonInfo; |
| 587 |
mService = IAuthService.Stub.asInterface( |
| 588 |
ServiceManager.getService(Context.AUTH_SERVICE)); |
| 589 |
} |
| 590 |
|
| 591 |
|
| 592 |
|
| 593 |
|
| 594 |
|
| 595 |
@NonNull |
| 596 |
public CharSequence getTitle() { |
| 597 |
return mPromptInfo.getTitle(); |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
|
| 603 |
|
| 604 |
|
| 605 |
@RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 606 |
public boolean shouldUseDefaultTitle() { |
| 607 |
return mPromptInfo.isUseDefaultTitle(); |
| 608 |
} |
| 609 |
|
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
|
| 614 |
@Nullable |
| 615 |
public CharSequence getSubtitle() { |
| 616 |
return mPromptInfo.getSubtitle(); |
| 617 |
} |
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
@Nullable |
| 624 |
public CharSequence getDescription() { |
| 625 |
return mPromptInfo.getDescription(); |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
|
| 632 |
|
| 633 |
@Nullable |
| 634 |
public CharSequence getNegativeButtonText() { |
| 635 |
return mPromptInfo.getNegativeButtonText(); |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
public boolean isConfirmationRequired() { |
| 645 |
return mPromptInfo.isConfirmationRequested(); |
| 646 |
} |
| 647 |
|
| 648 |
|
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
|
| 653 |
|
| 654 |
|
| 655 |
@Nullable |
| 656 |
public int getAllowedAuthenticators() { |
| 657 |
return mPromptInfo.getAuthenticators(); |
| 658 |
} |
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
@TestApi |
| 665 |
@NonNull |
| 666 |
public List<Integer> getAllowedSensorIds() { |
| 667 |
return mPromptInfo.getAllowedSensorIds(); |
| 668 |
} |
| 669 |
|
| 670 |
|
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
@TestApi |
| 675 |
public boolean isAllowBackgroundAuthentication() { |
| 676 |
return mPromptInfo.isAllowBackgroundAuthentication(); |
| 677 |
} |
| 678 |
|
| 679 |
|
| 680 |
|
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
|
| 696 |
|
| 697 |
|
| 698 |
|
| 699 |
|
| 700 |
public static final class CryptoObject extends android.hardware.biometrics.CryptoObject { |
| 701 |
public CryptoObject(@NonNull Signature signature) { |
| 702 |
super(signature); |
| 703 |
} |
| 704 |
|
| 705 |
public CryptoObject(@NonNull Cipher cipher) { |
| 706 |
super(cipher); |
| 707 |
} |
| 708 |
|
| 709 |
public CryptoObject(@NonNull Mac mac) { |
| 710 |
super(mac); |
| 711 |
} |
| 712 |
|
| 713 |
|
| 714 |
|
| 715 |
|
| 716 |
|
| 717 |
|
| 718 |
|
| 719 |
@Deprecated |
| 720 |
public CryptoObject(@NonNull IdentityCredential credential) { |
| 721 |
super(credential); |
| 722 |
} |
| 723 |
|
| 724 |
public CryptoObject(@NonNull PresentationSession session) { |
| 725 |
super(session); |
| 726 |
} |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
|
| 732 |
public Signature getSignature() { |
| 733 |
return super.getSignature(); |
| 734 |
} |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
|
| 740 |
public Cipher getCipher() { |
| 741 |
return super.getCipher(); |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
|
| 746 |
|
| 747 |
|
| 748 |
public Mac getMac() { |
| 749 |
return super.getMac(); |
| 750 |
} |
| 751 |
|
| 752 |
|
| 753 |
|
| 754 |
|
| 755 |
|
| 756 |
|
| 757 |
@Deprecated |
| 758 |
public @Nullable IdentityCredential getIdentityCredential() { |
| 759 |
return super.getIdentityCredential(); |
| 760 |
} |
| 761 |
|
| 762 |
|
| 763 |
|
| 764 |
|
| 765 |
|
| 766 |
public @Nullable PresentationSession getPresentationSession() { |
| 767 |
return super.getPresentationSession(); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
|
| 775 |
public static final int AUTHENTICATION_RESULT_TYPE_DEVICE_CREDENTIAL = 1; |
| 776 |
|
| 777 |
|
| 778 |
|
| 779 |
|
| 780 |
|
| 781 |
public static final int AUTHENTICATION_RESULT_TYPE_BIOMETRIC = 2; |
| 782 |
|
| 783 |
|
| 784 |
|
| 785 |
|
| 786 |
|
| 787 |
@Retention(RetentionPolicy.SOURCE) |
| 788 |
@IntDef({AUTHENTICATION_RESULT_TYPE_DEVICE_CREDENTIAL, AUTHENTICATION_RESULT_TYPE_BIOMETRIC}) |
| 789 |
public @interface AuthenticationResultType { |
| 790 |
} |
| 791 |
|
| 792 |
|
| 793 |
|
| 794 |
|
| 795 |
|
| 796 |
|
| 797 |
public static class AuthenticationResult extends BiometricAuthenticator.AuthenticationResult { |
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
public AuthenticationResult(CryptoObject crypto, |
| 805 |
@AuthenticationResultType int authenticationType) { |
| 806 |
|
| 807 |
super(crypto, authenticationType, null , 0 ); |
| 808 |
} |
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
public CryptoObject getCryptoObject() { |
| 816 |
return (CryptoObject) super.getCryptoObject(); |
| 817 |
} |
| 818 |
|
| 819 |
|
| 820 |
|
| 821 |
|
| 822 |
|
| 823 |
|
| 824 |
|
| 825 |
public @AuthenticationResultType int getAuthenticationType() { |
| 826 |
return super.getAuthenticationType(); |
| 827 |
} |
| 828 |
} |
| 829 |
|
| 830 |
|
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
public abstract static class AuthenticationCallback extends |
| 837 |
BiometricAuthenticator.AuthenticationCallback { |
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
|
| 844 |
@Override |
| 845 |
public void onAuthenticationError(int errorCode, CharSequence errString) {} |
| 846 |
|
| 847 |
|
| 848 |
|
| 849 |
|
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
@Override |
| 855 |
public void onAuthenticationHelp(int helpCode, CharSequence helpString) {} |
| 856 |
|
| 857 |
|
| 858 |
|
| 859 |
|
| 860 |
|
| 861 |
public void onAuthenticationSucceeded(AuthenticationResult result) {} |
| 862 |
|
| 863 |
|
| 864 |
|
| 865 |
|
| 866 |
@Override |
| 867 |
public void onAuthenticationFailed() {} |
| 868 |
|
| 869 |
|
| 870 |
|
| 871 |
|
| 872 |
|
| 873 |
@Override |
| 874 |
public void onAuthenticationAcquired(int acquireInfo) {} |
| 875 |
|
| 876 |
|
| 877 |
|
| 878 |
|
| 879 |
|
| 880 |
public void onSystemEvent(int event) {} |
| 881 |
} |
| 882 |
|
| 883 |
|
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
@RequiresPermission(USE_BIOMETRIC_INTERNAL) |
| 894 |
public void authenticateUser(@NonNull CancellationSignal cancel, |
| 895 |
@NonNull @CallbackExecutor Executor executor, |
| 896 |
@NonNull AuthenticationCallback callback, |
| 897 |
int userId) { |
| 898 |
if (cancel == null) { |
| 899 |
throw new IllegalArgumentException("Must supply a cancellation signal"); |
| 900 |
} |
| 901 |
if (executor == null) { |
| 902 |
throw new IllegalArgumentException("Must supply an executor"); |
| 903 |
} |
| 904 |
if (callback == null) { |
| 905 |
throw new IllegalArgumentException("Must supply a callback"); |
| 906 |
} |
| 907 |
|
| 908 |
authenticateInternal(0 , cancel, executor, callback, userId); |
| 909 |
} |
| 910 |
|
| 911 |
|
| 912 |
|
| 913 |
|
| 914 |
|
| 915 |
|
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
@RequiresPermission(USE_BIOMETRIC) |
| 924 |
public long authenticateForOperation( |
| 925 |
@NonNull CancellationSignal cancel, |
| 926 |
@NonNull @CallbackExecutor Executor executor, |
| 927 |
@NonNull AuthenticationCallback callback, |
| 928 |
long operationId) { |
| 929 |
if (cancel == null) { |
| 930 |
throw new IllegalArgumentException("Must supply a cancellation signal"); |
| 931 |
} |
| 932 |
if (executor == null) { |
| 933 |
throw new IllegalArgumentException("Must supply an executor"); |
| 934 |
} |
| 935 |
if (callback == null) { |
| 936 |
throw new IllegalArgumentException("Must supply a callback"); |
| 937 |
} |
| 938 |
|
| 939 |
return authenticateInternal(operationId, cancel, executor, callback, mContext.getUserId()); |
| 940 |
} |
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
@RequiresPermission(USE_BIOMETRIC) |
| 981 |
public void authenticate(@NonNull CryptoObject crypto, |
| 982 |
@NonNull CancellationSignal cancel, |
| 983 |
@NonNull @CallbackExecutor Executor executor, |
| 984 |
@NonNull AuthenticationCallback callback) { |
| 985 |
|
| 986 |
FrameworkStatsLog.write(FrameworkStatsLog.AUTH_PROMPT_AUTHENTICATE_INVOKED, |
| 987 |
true , |
| 988 |
mPromptInfo.isConfirmationRequested(), |
| 989 |
mPromptInfo.isDeviceCredentialAllowed(), |
| 990 |
mPromptInfo.getAuthenticators() != Authenticators.EMPTY_SET, |
| 991 |
mPromptInfo.getAuthenticators()); |
| 992 |
|
| 993 |
if (crypto == null) { |
| 994 |
throw new IllegalArgumentException("Must supply a crypto object"); |
| 995 |
} |
| 996 |
if (cancel == null) { |
| 997 |
throw new IllegalArgumentException("Must supply a cancellation signal"); |
| 998 |
} |
| 999 |
if (executor == null) { |
| 1000 |
throw new IllegalArgumentException("Must supply an executor"); |
| 1001 |
} |
| 1002 |
if (callback == null) { |
| 1003 |
throw new IllegalArgumentException("Must supply a callback"); |
| 1004 |
} |
| 1005 |
|
| 1006 |
|
| 1007 |
@Authenticators.Types int authenticators = mPromptInfo.getAuthenticators(); |
| 1008 |
if (authenticators == Authenticators.EMPTY_SET) { |
| 1009 |
authenticators = Authenticators.BIOMETRIC_STRONG; |
| 1010 |
} |
| 1011 |
final int biometricStrength = authenticators & Authenticators.BIOMETRIC_WEAK; |
| 1012 |
if ((biometricStrength & ~Authenticators.BIOMETRIC_STRONG) != 0) { |
| 1013 |
throw new IllegalArgumentException("Only Strong biometrics supported with crypto"); |
| 1014 |
} |
| 1015 |
|
| 1016 |
authenticateInternal(crypto, cancel, executor, callback, mContext.getUserId()); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
|
| 1040 |
|
| 1041 |
|
| 1042 |
|
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
@RequiresPermission(USE_BIOMETRIC) |
| 1047 |
public void authenticate(@NonNull CancellationSignal cancel, |
| 1048 |
@NonNull @CallbackExecutor Executor executor, |
| 1049 |
@NonNull AuthenticationCallback callback) { |
| 1050 |
|
| 1051 |
FrameworkStatsLog.write(FrameworkStatsLog.AUTH_PROMPT_AUTHENTICATE_INVOKED, |
| 1052 |
false , |
| 1053 |
mPromptInfo.isConfirmationRequested(), |
| 1054 |
mPromptInfo.isDeviceCredentialAllowed(), |
| 1055 |
mPromptInfo.getAuthenticators() != Authenticators.EMPTY_SET, |
| 1056 |
mPromptInfo.getAuthenticators()); |
| 1057 |
|
| 1058 |
if (cancel == null) { |
| 1059 |
throw new IllegalArgumentException("Must supply a cancellation signal"); |
| 1060 |
} |
| 1061 |
if (executor == null) { |
| 1062 |
throw new IllegalArgumentException("Must supply an executor"); |
| 1063 |
} |
| 1064 |
if (callback == null) { |
| 1065 |
throw new IllegalArgumentException("Must supply a callback"); |
| 1066 |
} |
| 1067 |
authenticateInternal(null , cancel, executor, callback, mContext.getUserId()); |
| 1068 |
} |
| 1069 |
|
| 1070 |
private void cancelAuthentication(long requestId) { |
| 1071 |
if (mService != null) { |
| 1072 |
try { |
| 1073 |
mService.cancelAuthentication(mToken, mContext.getPackageName(), requestId); |
| 1074 |
} catch (RemoteException e) { |
| 1075 |
Log.e(TAG, "Unable to cancel authentication", e); |
| 1076 |
} |
| 1077 |
} |
| 1078 |
} |
| 1079 |
|
| 1080 |
private void authenticateInternal( |
| 1081 |
@Nullable CryptoObject crypto, |
| 1082 |
@NonNull CancellationSignal cancel, |
| 1083 |
@NonNull @CallbackExecutor Executor executor, |
| 1084 |
@NonNull AuthenticationCallback callback, |
| 1085 |
int userId) { |
| 1086 |
|
| 1087 |
mCryptoObject = crypto; |
| 1088 |
final long operationId = crypto != null ? crypto.getOpId() : 0L; |
| 1089 |
authenticateInternal(operationId, cancel, executor, callback, userId); |
| 1090 |
} |
| 1091 |
|
| 1092 |
private long authenticateInternal( |
| 1093 |
long operationId, |
| 1094 |
@NonNull CancellationSignal cancel, |
| 1095 |
@NonNull @CallbackExecutor Executor executor, |
| 1096 |
@NonNull AuthenticationCallback callback, |
| 1097 |
int userId) { |
| 1098 |
|
| 1099 |
|
| 1100 |
if (mCryptoObject != null && mCryptoObject.getOpId() != operationId) { |
| 1101 |
Log.w(TAG, "CryptoObject operation ID does not match argument; setting field to null"); |
| 1102 |
mCryptoObject = null; |
| 1103 |
} |
| 1104 |
|
| 1105 |
try { |
| 1106 |
if (cancel.isCanceled()) { |
| 1107 |
Log.w(TAG, "Authentication already canceled"); |
| 1108 |
return -1; |
| 1109 |
} |
| 1110 |
|
| 1111 |
mExecutor = executor; |
| 1112 |
mAuthenticationCallback = callback; |
| 1113 |
|
| 1114 |
final PromptInfo promptInfo; |
| 1115 |
if (operationId != 0L) { |
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
Parcel parcel = Parcel.obtain(); |
| 1121 |
mPromptInfo.writeToParcel(parcel, 0 ); |
| 1122 |
parcel.setDataPosition(0); |
| 1123 |
promptInfo = new PromptInfo(parcel); |
| 1124 |
if (promptInfo.getAuthenticators() == Authenticators.EMPTY_SET) { |
| 1125 |
promptInfo.setAuthenticators(Authenticators.BIOMETRIC_STRONG); |
| 1126 |
} |
| 1127 |
} else { |
| 1128 |
promptInfo = mPromptInfo; |
| 1129 |
} |
| 1130 |
|
| 1131 |
final long authId = mService.authenticate(mToken, operationId, userId, |
| 1132 |
mBiometricServiceReceiver, mContext.getPackageName(), promptInfo); |
| 1133 |
cancel.setOnCancelListener(new OnAuthenticationCancelListener(authId)); |
| 1134 |
return authId; |
| 1135 |
} catch (RemoteException e) { |
| 1136 |
Log.e(TAG, "Remote exception while authenticating", e); |
| 1137 |
mExecutor.execute(() -> callback.onAuthenticationError( |
| 1138 |
BiometricPrompt.BIOMETRIC_ERROR_HW_UNAVAILABLE, |
| 1139 |
mContext.getString(R.string.biometric_error_hw_unavailable))); |
| 1140 |
return -1; |
| 1141 |
} |
| 1142 |
} |
| 1143 |
|
| 1144 |
private static boolean isCredentialAllowed(@Authenticators.Types int allowedAuthenticators) { |
| 1145 |
return (allowedAuthenticators & Authenticators.DEVICE_CREDENTIAL) != 0; |
| 1146 |
} |
| 1147 |
} |