| 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.os.ServiceManager; |
| 21 |
import android.os.ServiceSpecificException; |
| 22 |
import android.security.legacykeystore.ILegacyKeystore; |
| 23 |
import android.util.Log; |
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
public class LegacyVpnProfileStore { |
| 33 |
private static final String TAG = "LegacyVpnProfileStore"; |
| 34 |
|
| 35 |
public static final int SYSTEM_ERROR = ILegacyKeystore.ERROR_SYSTEM_ERROR; |
| 36 |
public static final int PROFILE_NOT_FOUND = ILegacyKeystore.ERROR_ENTRY_NOT_FOUND; |
| 37 |
|
| 38 |
private static final String LEGACY_KEYSTORE_SERVICE_NAME = "android.security.legacykeystore"; |
| 39 |
|
| 40 |
private static ILegacyKeystore getService() { |
| 41 |
return ILegacyKeystore.Stub.asInterface( |
| 42 |
ServiceManager.checkService(LEGACY_KEYSTORE_SERVICE_NAME)); |
| 43 |
} |
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
public static boolean put(@NonNull String alias, @NonNull byte[] profile) { |
| 54 |
try { |
| 55 |
getService().put(alias, ILegacyKeystore.UID_SELF, profile); |
| 56 |
return true; |
| 57 |
} catch (Exception e) { |
| 58 |
Log.e(TAG, "Failed to put vpn profile.", e); |
| 59 |
return false; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static byte[] get(@NonNull String alias) { |
| 73 |
try { |
| 74 |
return getService().get(alias, ILegacyKeystore.UID_SELF); |
| 75 |
} catch (ServiceSpecificException e) { |
| 76 |
if (e.errorCode != PROFILE_NOT_FOUND) { |
| 77 |
Log.e(TAG, "Failed to get vpn profile.", e); |
| 78 |
} |
| 79 |
} catch (Exception e) { |
| 80 |
Log.e(TAG, "Failed to get vpn profile.", e); |
| 81 |
} |
| 82 |
return null; |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
public static boolean remove(@NonNull String alias) { |
| 92 |
try { |
| 93 |
getService().remove(alias, ILegacyKeystore.UID_SELF); |
| 94 |
return true; |
| 95 |
} catch (ServiceSpecificException e) { |
| 96 |
if (e.errorCode != PROFILE_NOT_FOUND) { |
| 97 |
Log.e(TAG, "Failed to remove vpn profile.", e); |
| 98 |
} |
| 99 |
} catch (Exception e) { |
| 100 |
Log.e(TAG, "Failed to remove vpn profile.", e); |
| 101 |
} |
| 102 |
return false; |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
public static @NonNull String[] list(@NonNull String prefix) { |
| 112 |
try { |
| 113 |
final String[] aliases = getService().list(prefix, ILegacyKeystore.UID_SELF); |
| 114 |
for (int i = 0; i < aliases.length; ++i) { |
| 115 |
aliases[i] = aliases[i].substring(prefix.length()); |
| 116 |
} |
| 117 |
return aliases; |
| 118 |
} catch (Exception e) { |
| 119 |
Log.e(TAG, "Failed to list vpn profiles.", e); |
| 120 |
} |
| 121 |
return new String[0]; |
| 122 |
} |
| 123 |
} |