1 /* 2 * Copyright (C) 2020 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.security; 18 19 import android.os.RemoteException; 20 import android.os.ServiceManager; 21 import android.os.ServiceSpecificException; 22 import android.security.apc.IConfirmationCallback; 23 import android.security.apc.IProtectedConfirmation; 24 import android.security.apc.ResponseCode; 25 import android.util.Log; 26 27 /** 28 * @hide 29 */ 30 public class AndroidProtectedConfirmation { 31 private static final String TAG = "AndroidProtectedConfirmation"; 32 33 public static final int ERROR_OK = ResponseCode.OK; 34 public static final int ERROR_CANCELED = ResponseCode.CANCELLED; 35 public static final int ERROR_ABORTED = ResponseCode.ABORTED; 36 public static final int ERROR_OPERATION_PENDING = ResponseCode.OPERATION_PENDING; 37 public static final int ERROR_IGNORED = ResponseCode.IGNORED; 38 public static final int ERROR_SYSTEM_ERROR = ResponseCode.SYSTEM_ERROR; 39 public static final int ERROR_UNIMPLEMENTED = ResponseCode.UNIMPLEMENTED; 40 41 public static final int FLAG_UI_OPTION_INVERTED = 42 IProtectedConfirmation.FLAG_UI_OPTION_INVERTED; 43 public static final int FLAG_UI_OPTION_MAGNIFIED = 44 IProtectedConfirmation.FLAG_UI_OPTION_MAGNIFIED; 45 46 private IProtectedConfirmation mProtectedConfirmation; 47 AndroidProtectedConfirmation()48 public AndroidProtectedConfirmation() { 49 mProtectedConfirmation = null; 50 } 51 getService()52 private synchronized IProtectedConfirmation getService() { 53 if (mProtectedConfirmation == null) { 54 mProtectedConfirmation = IProtectedConfirmation.Stub.asInterface(ServiceManager 55 .getService("android.security.apc")); 56 } 57 return mProtectedConfirmation; 58 } 59 60 /** 61 * Requests keystore call into the confirmationui HAL to display a prompt. 62 * 63 * @param listener the binder to use for callbacks. 64 * @param promptText the prompt to display. 65 * @param extraData extra data / nonce from application. 66 * @param locale the locale as a BCP 47 language tag. 67 * @param uiOptionsAsFlags the UI options to use, as flags. 68 * @return one of the {@code CONFIRMATIONUI_*} constants, for 69 * example {@code KeyStore.CONFIRMATIONUI_OK}. 70 */ presentConfirmationPrompt(IConfirmationCallback listener, String promptText, byte[] extraData, String locale, int uiOptionsAsFlags)71 public int presentConfirmationPrompt(IConfirmationCallback listener, String promptText, 72 byte[] extraData, String locale, int uiOptionsAsFlags) { 73 try { 74 getService().presentPrompt(listener, promptText, extraData, locale, 75 uiOptionsAsFlags); 76 return ERROR_OK; 77 } catch (RemoteException e) { 78 Log.w(TAG, "Cannot connect to keystore", e); 79 return ERROR_SYSTEM_ERROR; 80 } catch (ServiceSpecificException e) { 81 return e.errorCode; 82 } 83 } 84 85 /** 86 * Requests keystore call into the confirmationui HAL to cancel displaying a prompt. 87 * 88 * @param listener the binder passed to the {@link #presentConfirmationPrompt} method. 89 * @return one of the {@code CONFIRMATIONUI_*} constants, for 90 * example {@code KeyStore.CONFIRMATIONUI_OK}. 91 */ cancelConfirmationPrompt(IConfirmationCallback listener)92 public int cancelConfirmationPrompt(IConfirmationCallback listener) { 93 try { 94 getService().cancelPrompt(listener); 95 return ERROR_OK; 96 } catch (RemoteException e) { 97 Log.w(TAG, "Cannot connect to keystore", e); 98 return ERROR_SYSTEM_ERROR; 99 } catch (ServiceSpecificException e) { 100 return e.errorCode; 101 } 102 } 103 104 /** 105 * Requests keystore to check if the confirmationui HAL is available. 106 * 107 * @return whether the confirmationUI HAL is available. 108 */ isConfirmationPromptSupported()109 public boolean isConfirmationPromptSupported() { 110 try { 111 return getService().isSupported(); 112 } catch (RemoteException e) { 113 Log.w(TAG, "Cannot connect to keystore", e); 114 return false; 115 } 116 } 117 118 } 119