| 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.app.compat.CompatChanges; |
| 21 |
import android.hardware.security.keymint.KeyParameter; |
| 22 |
import android.os.Binder; |
| 23 |
import android.os.RemoteException; |
| 24 |
import android.os.ServiceSpecificException; |
| 25 |
import android.security.keystore.BackendBusyException; |
| 26 |
import android.security.keystore.KeyStoreConnectException; |
| 27 |
import android.system.keystore2.AuthenticatorSpec; |
| 28 |
import android.system.keystore2.CreateOperationResponse; |
| 29 |
import android.system.keystore2.IKeystoreSecurityLevel; |
| 30 |
import android.system.keystore2.KeyDescriptor; |
| 31 |
import android.system.keystore2.KeyMetadata; |
| 32 |
import android.system.keystore2.ResponseCode; |
| 33 |
import android.util.Log; |
| 34 |
|
| 35 |
import java.util.Calendar; |
| 36 |
import java.util.Collection; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
public class KeyStoreSecurityLevel { |
| 45 |
private static final String TAG = "KeyStoreSecurityLevel"; |
| 46 |
private final IKeystoreSecurityLevel mSecurityLevel; |
| 47 |
|
| 48 |
public KeyStoreSecurityLevel(IKeystoreSecurityLevel securityLevel) { |
| 49 |
Binder.allowBlocking(securityLevel.asBinder()); |
| 50 |
this.mSecurityLevel = securityLevel; |
| 51 |
} |
| 52 |
|
| 53 |
private <R> R handleExceptions(CheckedRemoteRequest<R> request) throws KeyStoreException { |
| 54 |
try { |
| 55 |
return request.execute(); |
| 56 |
} catch (ServiceSpecificException e) { |
| 57 |
throw KeyStore2.getKeyStoreException(e.errorCode, e.getMessage()); |
| 58 |
} catch (RemoteException e) { |
| 59 |
|
| 60 |
|
| 61 |
Log.e(TAG, "Could not connect to Keystore.", e); |
| 62 |
throw new KeyStoreException(ResponseCode.SYSTEM_ERROR, "", e.getMessage()); |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
public KeyStoreOperation createOperation(@NonNull KeyDescriptor keyDescriptor, |
| 77 |
Collection<KeyParameter> args) throws KeyStoreException { |
| 78 |
while (true) { |
| 79 |
try { |
| 80 |
CreateOperationResponse createOperationResponse = |
| 81 |
mSecurityLevel.createOperation( |
| 82 |
keyDescriptor, |
| 83 |
args.toArray(new KeyParameter[args.size()]), |
| 84 |
false |
| 85 |
); |
| 86 |
Long challenge = null; |
| 87 |
if (createOperationResponse.operationChallenge != null) { |
| 88 |
challenge = createOperationResponse.operationChallenge.challenge; |
| 89 |
} |
| 90 |
KeyParameter[] parameters = null; |
| 91 |
if (createOperationResponse.parameters != null) { |
| 92 |
parameters = createOperationResponse.parameters.keyParameter; |
| 93 |
} |
| 94 |
return new KeyStoreOperation( |
| 95 |
createOperationResponse.iOperation, |
| 96 |
challenge, |
| 97 |
parameters); |
| 98 |
} catch (ServiceSpecificException e) { |
| 99 |
switch (e.errorCode) { |
| 100 |
case ResponseCode.BACKEND_BUSY: { |
| 101 |
long backOffHint = (long) (Math.random() * 80 + 20); |
| 102 |
if (CompatChanges.isChangeEnabled( |
| 103 |
KeyStore2.KEYSTORE_OPERATION_CREATION_MAY_FAIL)) { |
| 104 |
|
| 105 |
|
| 106 |
throw new BackendBusyException(backOffHint); |
| 107 |
} else { |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
interruptedPreservingSleep(backOffHint); |
| 116 |
} |
| 117 |
break; |
| 118 |
} |
| 119 |
default: |
| 120 |
throw KeyStore2.getKeyStoreException(e.errorCode, e.getMessage()); |
| 121 |
} |
| 122 |
} catch (RemoteException e) { |
| 123 |
Log.w(TAG, "Cannot connect to keystore", e); |
| 124 |
throw new KeyStoreConnectException(); |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public KeyMetadata generateKey(@NonNull KeyDescriptor descriptor, KeyDescriptor attestationKey, |
| 143 |
Collection<KeyParameter> args, int flags, byte[] entropy) |
| 144 |
throws KeyStoreException { |
| 145 |
return handleExceptions(() -> mSecurityLevel.generateKey( |
| 146 |
descriptor, attestationKey, args.toArray(new KeyParameter[args.size()]), |
| 147 |
flags, entropy)); |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
public KeyMetadata importKey(KeyDescriptor descriptor, KeyDescriptor attestationKey, |
| 164 |
Collection<KeyParameter> args, int flags, byte[] keyData) |
| 165 |
throws KeyStoreException { |
| 166 |
return handleExceptions(() -> mSecurityLevel.importKey(descriptor, attestationKey, |
| 167 |
args.toArray(new KeyParameter[args.size()]), flags, keyData)); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
public KeyMetadata importWrappedKey(@NonNull KeyDescriptor wrappedKeyDescriptor, |
| 185 |
@NonNull KeyDescriptor wrappingKeyDescriptor, |
| 186 |
@NonNull byte[] wrappedKey, byte[] maskingKey, |
| 187 |
Collection<KeyParameter> args, @NonNull AuthenticatorSpec[] authenticatorSpecs) |
| 188 |
throws KeyStoreException { |
| 189 |
KeyDescriptor keyDescriptor = new KeyDescriptor(); |
| 190 |
keyDescriptor.alias = wrappedKeyDescriptor.alias; |
| 191 |
keyDescriptor.nspace = wrappedKeyDescriptor.nspace; |
| 192 |
keyDescriptor.blob = wrappedKey; |
| 193 |
keyDescriptor.domain = wrappedKeyDescriptor.domain; |
| 194 |
|
| 195 |
return handleExceptions(() -> mSecurityLevel.importWrappedKey(keyDescriptor, |
| 196 |
wrappingKeyDescriptor, maskingKey, |
| 197 |
args.toArray(new KeyParameter[args.size()]), authenticatorSpecs)); |
| 198 |
} |
| 199 |
|
| 200 |
protected static void interruptedPreservingSleep(long millis) { |
| 201 |
boolean wasInterrupted = false; |
| 202 |
Calendar calendar = Calendar.getInstance(); |
| 203 |
long target = calendar.getTimeInMillis() + millis; |
| 204 |
while (true) { |
| 205 |
try { |
| 206 |
Thread.sleep(target - calendar.getTimeInMillis()); |
| 207 |
break; |
| 208 |
} catch (InterruptedException e) { |
| 209 |
wasInterrupted = true; |
| 210 |
} catch (IllegalArgumentException e) { |
| 211 |
|
| 212 |
|
| 213 |
break; |
| 214 |
} |
| 215 |
} |
| 216 |
if (wasInterrupted) { |
| 217 |
Thread.currentThread().interrupt(); |
| 218 |
} |
| 219 |
} |
| 220 |
} |