1 /*
2  * Copyright (C) 2018 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 android.hardware.biometrics;
18 
19 import android.annotation.NonNull;
20 import android.security.identity.IdentityCredential;
21 import android.security.keystore.AndroidKeyStoreProvider;
22 
23 import java.security.Signature;
24 
25 import javax.crypto.Cipher;
26 import javax.crypto.Mac;
27 
28 /**
29  * A wrapper class for the crypto objects supported by BiometricPrompt and FingerprintManager.
30  * Currently the framework supports {@link Signature}, {@link Cipher}, {@link Mac} and
31  * {@link IdentityCredential} objects.
32  * @hide
33  */
34 public class CryptoObject {
35     private final Object mCrypto;
36 
CryptoObject(@onNull Signature signature)37     public CryptoObject(@NonNull Signature signature) {
38         mCrypto = signature;
39     }
40 
CryptoObject(@onNull Cipher cipher)41     public CryptoObject(@NonNull Cipher cipher) {
42         mCrypto = cipher;
43     }
44 
CryptoObject(@onNull Mac mac)45     public CryptoObject(@NonNull Mac mac) {
46         mCrypto = mac;
47     }
48 
CryptoObject(@onNull IdentityCredential credential)49     public CryptoObject(@NonNull IdentityCredential credential) {
50         mCrypto = credential;
51     }
52 
53     /**
54      * Get {@link Signature} object.
55      * @return {@link Signature} object or null if this doesn't contain one.
56      */
getSignature()57     public Signature getSignature() {
58         return mCrypto instanceof Signature ? (Signature) mCrypto : null;
59     }
60 
61     /**
62      * Get {@link Cipher} object.
63      * @return {@link Cipher} object or null if this doesn't contain one.
64      */
getCipher()65     public Cipher getCipher() {
66         return mCrypto instanceof Cipher ? (Cipher) mCrypto : null;
67     }
68 
69     /**
70      * Get {@link Mac} object.
71      * @return {@link Mac} object or null if this doesn't contain one.
72      */
getMac()73     public Mac getMac() {
74         return mCrypto instanceof Mac ? (Mac) mCrypto : null;
75     }
76 
77     /**
78      * Get {@link IdentityCredential} object.
79      * @return {@link IdentityCredential} object or null if this doesn't contain one.
80      */
getIdentityCredential()81     public IdentityCredential getIdentityCredential() {
82         return mCrypto instanceof IdentityCredential ? (IdentityCredential) mCrypto : null;
83     }
84 
85     /**
86      * @hide
87      * @return the opId associated with this object or 0 if none
88      */
getOpId()89     public final long getOpId() {
90         if (mCrypto == null) {
91             return 0;
92         } else if (mCrypto instanceof IdentityCredential) {
93             return ((IdentityCredential) mCrypto).getCredstoreOperationHandle();
94         }
95         return AndroidKeyStoreProvider.getKeyStoreOperationHandle(mCrypto);
96     }
97 };
98