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.identity.PresentationSession;
22 import android.security.keystore2.AndroidKeyStoreProvider;
23 
24 import java.security.Signature;
25 
26 import javax.crypto.Cipher;
27 import javax.crypto.Mac;
28 
29 /**
30  * A wrapper class for the crypto objects supported by BiometricPrompt and FingerprintManager.
31  * Currently the framework supports {@link Signature}, {@link Cipher}, {@link Mac},
32  * {@link IdentityCredential}, and {@link PresentationSession} objects.
33  * @hide
34  */
35 public class CryptoObject {
36     private final Object mCrypto;
37 
CryptoObject(@onNull Signature signature)38     public CryptoObject(@NonNull Signature signature) {
39         mCrypto = signature;
40     }
41 
CryptoObject(@onNull Cipher cipher)42     public CryptoObject(@NonNull Cipher cipher) {
43         mCrypto = cipher;
44     }
45 
CryptoObject(@onNull Mac mac)46     public CryptoObject(@NonNull Mac mac) {
47         mCrypto = mac;
48     }
49 
50     /**
51      * Create from a {@link IdentityCredential} object.
52      *
53      * @param credential a {@link IdentityCredential} object.
54      * @deprecated Use {@link PresentationSession} instead of {@link IdentityCredential}.
55      */
56     @Deprecated
CryptoObject(@onNull IdentityCredential credential)57     public CryptoObject(@NonNull IdentityCredential credential) {
58         mCrypto = credential;
59     }
60 
CryptoObject(@onNull PresentationSession session)61     public CryptoObject(@NonNull PresentationSession session) {
62         mCrypto = session;
63     }
64 
65     /**
66      * Get {@link Signature} object.
67      * @return {@link Signature} object or null if this doesn't contain one.
68      */
getSignature()69     public Signature getSignature() {
70         return mCrypto instanceof Signature ? (Signature) mCrypto : null;
71     }
72 
73     /**
74      * Get {@link Cipher} object.
75      * @return {@link Cipher} object or null if this doesn't contain one.
76      */
getCipher()77     public Cipher getCipher() {
78         return mCrypto instanceof Cipher ? (Cipher) mCrypto : null;
79     }
80 
81     /**
82      * Get {@link Mac} object.
83      * @return {@link Mac} object or null if this doesn't contain one.
84      */
getMac()85     public Mac getMac() {
86         return mCrypto instanceof Mac ? (Mac) mCrypto : null;
87     }
88 
89     /**
90      * Get {@link IdentityCredential} object.
91      * @return {@link IdentityCredential} object or null if this doesn't contain one.
92      * @deprecated Use {@link PresentationSession} instead of {@link IdentityCredential}.
93      */
94     @Deprecated
getIdentityCredential()95     public IdentityCredential getIdentityCredential() {
96         return mCrypto instanceof IdentityCredential ? (IdentityCredential) mCrypto : null;
97     }
98 
99     /**
100      * Get {@link PresentationSession} object.
101      * @return {@link PresentationSession} object or null if this doesn't contain one.
102      */
getPresentationSession()103     public PresentationSession getPresentationSession() {
104         return mCrypto instanceof PresentationSession ? (PresentationSession) mCrypto : null;
105     }
106 
107     /**
108      * @hide
109      * @return the opId associated with this object or 0 if none
110      */
getOpId()111     public final long getOpId() {
112         if (mCrypto == null) {
113             return 0;
114         } else if (mCrypto instanceof IdentityCredential) {
115             return ((IdentityCredential) mCrypto).getCredstoreOperationHandle();
116         } else if (mCrypto instanceof PresentationSession) {
117             return ((PresentationSession) mCrypto).getCredstoreOperationHandle();
118         }
119         return AndroidKeyStoreProvider.getKeyStoreOperationHandle(mCrypto);
120     }
121 }
122