| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.security; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.hardware.security.keymint.KeyParameter; |
| 21 |
import android.os.Binder; |
| 22 |
import android.os.RemoteException; |
| 23 |
import android.os.ServiceSpecificException; |
| 24 |
import android.security.keymaster.KeymasterDefs; |
| 25 |
import android.system.keystore2.IKeystoreOperation; |
| 26 |
import android.system.keystore2.ResponseCode; |
| 27 |
import android.util.Log; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class KeyStoreOperation { |
| 33 |
static final String TAG = "KeyStoreOperation"; |
| 34 |
private final IKeystoreOperation mOperation; |
| 35 |
private final Long mChallenge; |
| 36 |
private final KeyParameter[] mParameters; |
| 37 |
|
| 38 |
public KeyStoreOperation( |
| 39 |
@NonNull IKeystoreOperation operation, |
| 40 |
Long challenge, |
| 41 |
KeyParameter[] parameters |
| 42 |
) { |
| 43 |
Binder.allowBlocking(operation.asBinder()); |
| 44 |
this.mOperation = operation; |
| 45 |
this.mChallenge = challenge; |
| 46 |
this.mParameters = parameters; |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
public Long getChallenge() { |
| 55 |
return mChallenge; |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
public KeyParameter[] getParameters() { |
| 63 |
return mParameters; |
| 64 |
} |
| 65 |
|
| 66 |
private <R> R handleExceptions(@NonNull CheckedRemoteRequest<R> request) |
| 67 |
throws KeyStoreException { |
| 68 |
try { |
| 69 |
return request.execute(); |
| 70 |
} catch (ServiceSpecificException e) { |
| 71 |
switch(e.errorCode) { |
| 72 |
case ResponseCode.OPERATION_BUSY: { |
| 73 |
throw new IllegalThreadStateException( |
| 74 |
"Cannot update the same operation concurrently." |
| 75 |
); |
| 76 |
} |
| 77 |
default: |
| 78 |
throw KeyStore2.getKeyStoreException(e.errorCode, e.getMessage()); |
| 79 |
} |
| 80 |
} catch (RemoteException e) { |
| 81 |
|
| 82 |
|
| 83 |
Log.e( |
| 84 |
TAG, |
| 85 |
"Remote exception while advancing a KeyStoreOperation.", |
| 86 |
e |
| 87 |
); |
| 88 |
throw new KeyStoreException(KeymasterDefs.KM_ERROR_INVALID_OPERATION_HANDLE, "", |
| 89 |
e.getMessage()); |
| 90 |
} |
| 91 |
} |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
public void updateAad(@NonNull byte[] input) throws KeyStoreException { |
| 100 |
handleExceptions(() -> { |
| 101 |
mOperation.updateAad(input); |
| 102 |
return 0; |
| 103 |
}); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
public byte[] update(@NonNull byte[] input) throws KeyStoreException { |
| 115 |
return handleExceptions(() -> mOperation.update(input)); |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public byte[] finish(byte[] input, byte[] signature) throws KeyStoreException { |
| 128 |
return handleExceptions(() -> mOperation.finish(input, signature)); |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
public void abort() throws KeyStoreException { |
| 138 |
handleExceptions(() -> { |
| 139 |
mOperation.abort(); |
| 140 |
return 0; |
| 141 |
}); |
| 142 |
} |
| 143 |
} |