| 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.annotation.Nullable; |
| 21 |
import android.hardware.security.keymint.HardwareAuthToken; |
| 22 |
import android.os.RemoteException; |
| 23 |
import android.os.ServiceManager; |
| 24 |
import android.os.ServiceSpecificException; |
| 25 |
import android.security.authorization.IKeystoreAuthorization; |
| 26 |
import android.security.authorization.LockScreenEvent; |
| 27 |
import android.system.keystore2.ResponseCode; |
| 28 |
import android.util.Log; |
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
public class Authorization { |
| 35 |
private static final String TAG = "KeystoreAuthorization"; |
| 36 |
|
| 37 |
public static final int SYSTEM_ERROR = ResponseCode.SYSTEM_ERROR; |
| 38 |
|
| 39 |
private static IKeystoreAuthorization getService() { |
| 40 |
return IKeystoreAuthorization.Stub.asInterface( |
| 41 |
ServiceManager.checkService("android.security.authorization")); |
| 42 |
} |
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
public static int addAuthToken(@NonNull HardwareAuthToken authToken) { |
| 51 |
try { |
| 52 |
getService().addAuthToken(authToken); |
| 53 |
return 0; |
| 54 |
} catch (RemoteException | NullPointerException e) { |
| 55 |
Log.w(TAG, "Can not connect to keystore", e); |
| 56 |
return SYSTEM_ERROR; |
| 57 |
} catch (ServiceSpecificException e) { |
| 58 |
return e.errorCode; |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
public static int addAuthToken(@NonNull byte[] authToken) { |
| 68 |
return addAuthToken(AuthTokenUtils.toHardwareAuthToken(authToken)); |
| 69 |
} |
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static int onLockScreenEvent(@NonNull boolean locked, @NonNull int userId, |
| 83 |
@Nullable byte[] syntheticPassword, @Nullable long[] unlockingSids) { |
| 84 |
try { |
| 85 |
if (locked) { |
| 86 |
getService().onLockScreenEvent(LockScreenEvent.LOCK, userId, null, unlockingSids); |
| 87 |
} else { |
| 88 |
getService().onLockScreenEvent( |
| 89 |
LockScreenEvent.UNLOCK, userId, syntheticPassword, unlockingSids); |
| 90 |
} |
| 91 |
return 0; |
| 92 |
} catch (RemoteException | NullPointerException e) { |
| 93 |
Log.w(TAG, "Can not connect to keystore", e); |
| 94 |
return SYSTEM_ERROR; |
| 95 |
} catch (ServiceSpecificException e) { |
| 96 |
return e.errorCode; |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
} |