| 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.os.ServiceManager; |
| 22 |
import android.os.ServiceSpecificException; |
| 23 |
import android.security.maintenance.IKeystoreMaintenance; |
| 24 |
import android.system.keystore2.Domain; |
| 25 |
import android.system.keystore2.KeyDescriptor; |
| 26 |
import android.system.keystore2.ResponseCode; |
| 27 |
import android.util.Log; |
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
public class AndroidKeyStoreMaintenance { |
| 34 |
private static final String TAG = "AndroidKeyStoreMaintenance"; |
| 35 |
|
| 36 |
public static final int SYSTEM_ERROR = ResponseCode.SYSTEM_ERROR; |
| 37 |
public static final int INVALID_ARGUMENT = ResponseCode.INVALID_ARGUMENT; |
| 38 |
public static final int PERMISSION_DENIED = ResponseCode.PERMISSION_DENIED; |
| 39 |
public static final int KEY_NOT_FOUND = ResponseCode.KEY_NOT_FOUND; |
| 40 |
|
| 41 |
private static IKeystoreMaintenance getService() { |
| 42 |
return IKeystoreMaintenance.Stub.asInterface( |
| 43 |
ServiceManager.checkService("android.security.maintenance")); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static int onUserAdded(@NonNull int userId) { |
| 54 |
try { |
| 55 |
getService().onUserAdded(userId); |
| 56 |
return 0; |
| 57 |
} catch (ServiceSpecificException e) { |
| 58 |
Log.e(TAG, "onUserAdded failed", e); |
| 59 |
return e.errorCode; |
| 60 |
} catch (Exception e) { |
| 61 |
Log.e(TAG, "Can not connect to keystore", e); |
| 62 |
return SYSTEM_ERROR; |
| 63 |
} |
| 64 |
} |
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
public static int onUserRemoved(int userId) { |
| 74 |
try { |
| 75 |
getService().onUserRemoved(userId); |
| 76 |
return 0; |
| 77 |
} catch (ServiceSpecificException e) { |
| 78 |
Log.e(TAG, "onUserRemoved failed", e); |
| 79 |
return e.errorCode; |
| 80 |
} catch (Exception e) { |
| 81 |
Log.e(TAG, "Can not connect to keystore", e); |
| 82 |
return SYSTEM_ERROR; |
| 83 |
} |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
public static int onUserPasswordChanged(int userId, @Nullable byte[] password) { |
| 96 |
try { |
| 97 |
getService().onUserPasswordChanged(userId, password); |
| 98 |
return 0; |
| 99 |
} catch (ServiceSpecificException e) { |
| 100 |
Log.e(TAG, "onUserPasswordChanged failed", e); |
| 101 |
return e.errorCode; |
| 102 |
} catch (Exception e) { |
| 103 |
Log.e(TAG, "Can not connect to keystore", e); |
| 104 |
return SYSTEM_ERROR; |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static int clearNamespace(@Domain int domain, long namespace) { |
| 113 |
try { |
| 114 |
getService().clearNamespace(domain, namespace); |
| 115 |
return 0; |
| 116 |
} catch (ServiceSpecificException e) { |
| 117 |
Log.e(TAG, "clearNamespace failed", e); |
| 118 |
return e.errorCode; |
| 119 |
} catch (Exception e) { |
| 120 |
Log.e(TAG, "Can not connect to keystore", e); |
| 121 |
return SYSTEM_ERROR; |
| 122 |
} |
| 123 |
} |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
public static int getState(int userId) { |
| 132 |
try { |
| 133 |
return getService().getState(userId); |
| 134 |
} catch (ServiceSpecificException e) { |
| 135 |
Log.e(TAG, "getState failed", e); |
| 136 |
return e.errorCode; |
| 137 |
} catch (Exception e) { |
| 138 |
Log.e(TAG, "Can not connect to keystore", e); |
| 139 |
return SYSTEM_ERROR; |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
public static void onDeviceOffBody() { |
| 147 |
try { |
| 148 |
getService().onDeviceOffBody(); |
| 149 |
} catch (Exception e) { |
| 150 |
|
| 151 |
|
| 152 |
Log.e(TAG, "Error while reporting device off body event.", e); |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static int migrateKeyNamespace(KeyDescriptor source, KeyDescriptor destination) { |
| 175 |
try { |
| 176 |
getService().migrateKeyNamespace(source, destination); |
| 177 |
return 0; |
| 178 |
} catch (ServiceSpecificException e) { |
| 179 |
Log.e(TAG, "migrateKeyNamespace failed", e); |
| 180 |
return e.errorCode; |
| 181 |
} catch (Exception e) { |
| 182 |
Log.e(TAG, "Can not connect to keystore", e); |
| 183 |
return SYSTEM_ERROR; |
| 184 |
} |
| 185 |
} |
| 186 |
} |