| 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.compat.annotation.ChangeId; |
| 21 |
import android.compat.annotation.Disabled; |
| 22 |
import android.os.Binder; |
| 23 |
import android.os.RemoteException; |
| 24 |
import android.os.ServiceManager; |
| 25 |
import android.os.ServiceSpecificException; |
| 26 |
import android.security.keymaster.KeymasterDefs; |
| 27 |
import android.system.keystore2.Domain; |
| 28 |
import android.system.keystore2.IKeystoreService; |
| 29 |
import android.system.keystore2.KeyDescriptor; |
| 30 |
import android.system.keystore2.KeyEntryResponse; |
| 31 |
import android.system.keystore2.ResponseCode; |
| 32 |
import android.util.Log; |
| 33 |
|
| 34 |
import java.util.Calendar; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
public class KeyStore2 { |
| 42 |
private static final String TAG = "KeyStore"; |
| 43 |
|
| 44 |
private static final int RECOVERY_GRACE_PERIOD_MS = 50; |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
@ChangeId |
| 89 |
@Disabled |
| 90 |
static final long KEYSTORE_OPERATION_CREATION_MAY_FAIL = 169897160L; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
private IKeystoreService mBinder; |
| 96 |
|
| 97 |
|
| 98 |
@FunctionalInterface |
| 99 |
interface CheckedRemoteRequest<R> { |
| 100 |
R execute(IKeystoreService service) throws RemoteException; |
| 101 |
} |
| 102 |
|
| 103 |
private <R> R handleRemoteExceptionWithRetry(@NonNull CheckedRemoteRequest<R> request) |
| 104 |
throws KeyStoreException { |
| 105 |
IKeystoreService service = getService(false ); |
| 106 |
boolean firstTry = true; |
| 107 |
while (true) { |
| 108 |
try { |
| 109 |
return request.execute(service); |
| 110 |
} catch (ServiceSpecificException e) { |
| 111 |
throw getKeyStoreException(e.errorCode, e.getMessage()); |
| 112 |
} catch (RemoteException e) { |
| 113 |
if (firstTry) { |
| 114 |
Log.w(TAG, "Looks like we may have lost connection to the Keystore " |
| 115 |
+ "daemon."); |
| 116 |
Log.w(TAG, "Retrying after giving Keystore " |
| 117 |
+ RECOVERY_GRACE_PERIOD_MS + "ms to recover."); |
| 118 |
interruptedPreservingSleep(RECOVERY_GRACE_PERIOD_MS); |
| 119 |
service = getService(true ); |
| 120 |
firstTry = false; |
| 121 |
} else { |
| 122 |
Log.e(TAG, "Cannot connect to Keystore daemon.", e); |
| 123 |
throw new KeyStoreException(ResponseCode.SYSTEM_ERROR, "", e.getMessage()); |
| 124 |
} |
| 125 |
} |
| 126 |
} |
| 127 |
} |
| 128 |
|
| 129 |
private static final String KEYSTORE2_SERVICE_NAME = |
| 130 |
"android.system.keystore2.IKeystoreService/default"; |
| 131 |
|
| 132 |
private KeyStore2() { |
| 133 |
mBinder = null; |
| 134 |
} |
| 135 |
|
| 136 |
public static KeyStore2 getInstance() { |
| 137 |
return new KeyStore2(); |
| 138 |
} |
| 139 |
|
| 140 |
private synchronized IKeystoreService getService(boolean retryLookup) { |
| 141 |
if (mBinder == null || retryLookup) { |
| 142 |
mBinder = IKeystoreService.Stub.asInterface(ServiceManager |
| 143 |
.getService(KEYSTORE2_SERVICE_NAME)); |
| 144 |
Binder.allowBlocking(mBinder.asBinder()); |
| 145 |
} |
| 146 |
return mBinder; |
| 147 |
} |
| 148 |
|
| 149 |
void delete(KeyDescriptor descriptor) throws KeyStoreException { |
| 150 |
handleRemoteExceptionWithRetry((service) -> { |
| 151 |
service.deleteKey(descriptor); |
| 152 |
return 0; |
| 153 |
}); |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public KeyDescriptor[] list(int domain, long namespace) throws KeyStoreException { |
| 160 |
return handleRemoteExceptionWithRetry((service) -> service.listEntries(domain, namespace)); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
public KeyDescriptor[] listBatch(int domain, long namespace, String startPastAlias) |
| 167 |
throws KeyStoreException { |
| 168 |
return handleRemoteExceptionWithRetry( |
| 169 |
(service) -> service.listEntriesBatched(domain, namespace, startPastAlias)); |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
private static final String KEYSTORE_ENGINE_GRANT_ALIAS_PREFIX = |
| 181 |
"ks2_keystore-engine_grant_id:0x"; |
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
public static String makeKeystoreEngineGrantString(long grantId) { |
| 195 |
return String.format("%s%016X", KEYSTORE_ENGINE_GRANT_ALIAS_PREFIX, grantId); |
| 196 |
} |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
public static KeyDescriptor keystoreEngineGrantString2KeyDescriptor(String grantString) { |
| 207 |
KeyDescriptor key = new KeyDescriptor(); |
| 208 |
key.domain = Domain.GRANT; |
| 209 |
key.nspace = Long.parseUnsignedLong( |
| 210 |
grantString.substring(KEYSTORE_ENGINE_GRANT_ALIAS_PREFIX.length()), 16); |
| 211 |
key.alias = null; |
| 212 |
key.blob = null; |
| 213 |
return key; |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
public KeyDescriptor grant(KeyDescriptor descriptor, int granteeUid, int accessVector) |
| 229 |
throws KeyStoreException { |
| 230 |
return handleRemoteExceptionWithRetry( |
| 231 |
(service) -> service.grant(descriptor, granteeUid, accessVector) |
| 232 |
); |
| 233 |
} |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
public void ungrant(KeyDescriptor descriptor, int granteeUid) |
| 244 |
throws KeyStoreException { |
| 245 |
handleRemoteExceptionWithRetry((service) -> { |
| 246 |
service.ungrant(descriptor, granteeUid); |
| 247 |
return 0; |
| 248 |
}); |
| 249 |
} |
| 250 |
|
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
public KeyEntryResponse getKeyEntry(@NonNull KeyDescriptor descriptor) |
| 260 |
throws KeyStoreException { |
| 261 |
return handleRemoteExceptionWithRetry((service) -> service.getKeyEntry(descriptor)); |
| 262 |
} |
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
|
| 268 |
|
| 269 |
|
| 270 |
|
| 271 |
|
| 272 |
public KeyStoreSecurityLevel getSecurityLevel(int securityLevel) |
| 273 |
throws KeyStoreException { |
| 274 |
return handleRemoteExceptionWithRetry((service) -> |
| 275 |
new KeyStoreSecurityLevel( |
| 276 |
service.getSecurityLevel(securityLevel) |
| 277 |
) |
| 278 |
); |
| 279 |
} |
| 280 |
|
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
public void updateSubcomponents(@NonNull KeyDescriptor key, byte[] publicCert, |
| 291 |
byte[] publicCertChain) throws KeyStoreException { |
| 292 |
handleRemoteExceptionWithRetry((service) -> { |
| 293 |
service.updateSubcomponent(key, publicCert, publicCertChain); |
| 294 |
return 0; |
| 295 |
}); |
| 296 |
} |
| 297 |
|
| 298 |
|
| 299 |
|
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
public void deleteKey(@NonNull KeyDescriptor descriptor) |
| 306 |
throws KeyStoreException { |
| 307 |
handleRemoteExceptionWithRetry((service) -> { |
| 308 |
service.deleteKey(descriptor); |
| 309 |
return 0; |
| 310 |
}); |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
public int getNumberOfEntries(int domain, long namespace) throws KeyStoreException { |
| 317 |
return handleRemoteExceptionWithRetry((service) |
| 318 |
-> service.getNumberOfEntries(domain, namespace)); |
| 319 |
} |
| 320 |
protected static void interruptedPreservingSleep(long millis) { |
| 321 |
boolean wasInterrupted = false; |
| 322 |
Calendar calendar = Calendar.getInstance(); |
| 323 |
long target = calendar.getTimeInMillis() + millis; |
| 324 |
while (true) { |
| 325 |
try { |
| 326 |
Thread.sleep(target - calendar.getTimeInMillis()); |
| 327 |
break; |
| 328 |
} catch (InterruptedException e) { |
| 329 |
wasInterrupted = true; |
| 330 |
} catch (IllegalArgumentException e) { |
| 331 |
|
| 332 |
|
| 333 |
break; |
| 334 |
} |
| 335 |
} |
| 336 |
if (wasInterrupted) { |
| 337 |
Thread.currentThread().interrupt(); |
| 338 |
} |
| 339 |
} |
| 340 |
|
| 341 |
static KeyStoreException getKeyStoreException(int errorCode, String serviceErrorMessage) { |
| 342 |
if (errorCode > 0) { |
| 343 |
|
| 344 |
switch (errorCode) { |
| 345 |
case ResponseCode.LOCKED: |
| 346 |
return new KeyStoreException(errorCode, "User authentication required", |
| 347 |
serviceErrorMessage); |
| 348 |
case ResponseCode.UNINITIALIZED: |
| 349 |
return new KeyStoreException(errorCode, "Keystore not initialized", |
| 350 |
serviceErrorMessage); |
| 351 |
case ResponseCode.SYSTEM_ERROR: |
| 352 |
return new KeyStoreException(errorCode, "System error", serviceErrorMessage); |
| 353 |
case ResponseCode.PERMISSION_DENIED: |
| 354 |
return new KeyStoreException(errorCode, "Permission denied", |
| 355 |
serviceErrorMessage); |
| 356 |
case ResponseCode.KEY_NOT_FOUND: |
| 357 |
return new KeyStoreException(errorCode, "Key not found", serviceErrorMessage); |
| 358 |
case ResponseCode.VALUE_CORRUPTED: |
| 359 |
return new KeyStoreException(errorCode, "Key blob corrupted", |
| 360 |
serviceErrorMessage); |
| 361 |
case ResponseCode.KEY_PERMANENTLY_INVALIDATED: |
| 362 |
return new KeyStoreException(errorCode, "Key permanently invalidated", |
| 363 |
serviceErrorMessage); |
| 364 |
case ResponseCode.OUT_OF_KEYS: |
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
return new KeyStoreException(errorCode, serviceErrorMessage, |
| 369 |
KeyStoreException.RKP_TEMPORARILY_UNAVAILABLE); |
| 370 |
default: |
| 371 |
return new KeyStoreException(errorCode, String.valueOf(errorCode), |
| 372 |
serviceErrorMessage); |
| 373 |
} |
| 374 |
} else { |
| 375 |
|
| 376 |
switch (errorCode) { |
| 377 |
case KeymasterDefs.KM_ERROR_INVALID_AUTHORIZATION_TIMEOUT: |
| 378 |
|
| 379 |
|
| 380 |
return new KeyStoreException(errorCode, |
| 381 |
"Invalid user authentication validity duration", |
| 382 |
serviceErrorMessage); |
| 383 |
default: |
| 384 |
return new KeyStoreException(errorCode, |
| 385 |
KeymasterDefs.getErrorMessage(errorCode), |
| 386 |
serviceErrorMessage); |
| 387 |
} |
| 388 |
} |
| 389 |
} |
| 390 |
|
| 391 |
} |