Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2009 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package android.security; |
| 18 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 19 | import android.annotation.UnsupportedAppUsage; |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 20 | import android.content.ActivityNotFoundException; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.util.Log; |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 24 | |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 25 | import com.android.org.bouncycastle.util.io.pem.PemObject; |
| 26 | import com.android.org.bouncycastle.util.io.pem.PemReader; |
| 27 | import com.android.org.bouncycastle.util.io.pem.PemWriter; |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 28 | |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 29 | import java.io.ByteArrayInputStream; |
| 30 | import java.io.ByteArrayOutputStream; |
| 31 | import java.io.IOException; |
| 32 | import java.io.InputStreamReader; |
| 33 | import java.io.OutputStreamWriter; |
| 34 | import java.io.Reader; |
| 35 | import java.io.Writer; |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 36 | import java.nio.charset.StandardCharsets; |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 37 | import java.security.KeyPair; |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 38 | import java.security.cert.Certificate; |
| 39 | import java.security.cert.CertificateEncodingException; |
| 40 | import java.security.cert.CertificateException; |
| 41 | import java.security.cert.CertificateFactory; |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 42 | import java.security.cert.X509Certificate; |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 43 | import java.util.ArrayList; |
| 44 | import java.util.List; |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 45 | |
| 46 | /** |
| 47 | * {@hide} |
| 48 | */ |
| 49 | public class Credentials { |
| 50 | private static final String LOGTAG = "Credentials"; |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 51 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 52 | public static final String INSTALL_ACTION = "android.credentials.INSTALL"; |
| 53 | |
Kenny Root | 3e7be43 | 2013-03-28 09:25:51 -0700 | [diff] [blame] | 54 | public static final String INSTALL_AS_USER_ACTION = "android.credentials.INSTALL_AS_USER"; |
| 55 | |
Brian Carlstrom | 4a9e1a2 | 2011-04-22 15:45:22 -0700 | [diff] [blame] | 56 | public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK"; |
| 57 | |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 58 | /** Key prefix for CA certificates. */ |
| 59 | public static final String CA_CERTIFICATE = "CACERT_"; |
| 60 | |
| 61 | /** Key prefix for user certificates. */ |
| 62 | public static final String USER_CERTIFICATE = "USRCERT_"; |
| 63 | |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 64 | /** Key prefix for user private and secret keys. */ |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 65 | public static final String USER_PRIVATE_KEY = "USRPKEY_"; |
| 66 | |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 67 | /** Key prefix for user secret keys. |
| 68 | * @deprecated use {@code USER_PRIVATE_KEY} for this category instead. |
| 69 | */ |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 70 | public static final String USER_SECRET_KEY = "USRSKEY_"; |
| 71 | |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 72 | /** Key prefix for VPN. */ |
| 73 | public static final String VPN = "VPN_"; |
| 74 | |
| 75 | /** Key prefix for WIFI. */ |
| 76 | public static final String WIFI = "WIFI_"; |
| 77 | |
Jeff Sharkey | 69ddab4 | 2012-08-25 00:05:46 -0700 | [diff] [blame] | 78 | /** Key containing suffix of lockdown VPN profile. */ |
| 79 | public static final String LOCKDOWN_VPN = "LOCKDOWN_VPN"; |
| 80 | |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 81 | /** Data type for public keys. */ |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 82 | public static final String EXTRA_PUBLIC_KEY = "KEY"; |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 83 | |
| 84 | /** Data type for private keys. */ |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 85 | public static final String EXTRA_PRIVATE_KEY = "PKEY"; |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 86 | |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 87 | // historically used by Android |
| 88 | public static final String EXTENSION_CRT = ".crt"; |
| 89 | public static final String EXTENSION_P12 = ".p12"; |
| 90 | // commonly used on Windows |
| 91 | public static final String EXTENSION_CER = ".cer"; |
| 92 | public static final String EXTENSION_PFX = ".pfx"; |
| 93 | |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 94 | /** |
Kenny Root | 3e7be43 | 2013-03-28 09:25:51 -0700 | [diff] [blame] | 95 | * Intent extra: install the certificate bundle as this UID instead of |
| 96 | * system. |
| 97 | */ |
| 98 | public static final String EXTRA_INSTALL_AS_UID = "install_as_uid"; |
| 99 | |
| 100 | /** |
Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 101 | * Intent extra: name for the user's private key. |
| 102 | */ |
| 103 | public static final String EXTRA_USER_PRIVATE_KEY_NAME = "user_private_key_name"; |
| 104 | |
| 105 | /** |
| 106 | * Intent extra: data for the user's private key in PEM-encoded PKCS#8. |
| 107 | */ |
| 108 | public static final String EXTRA_USER_PRIVATE_KEY_DATA = "user_private_key_data"; |
| 109 | |
| 110 | /** |
| 111 | * Intent extra: name for the user's certificate. |
| 112 | */ |
| 113 | public static final String EXTRA_USER_CERTIFICATE_NAME = "user_certificate_name"; |
| 114 | |
| 115 | /** |
| 116 | * Intent extra: data for the user's certificate in PEM-encoded X.509. |
| 117 | */ |
| 118 | public static final String EXTRA_USER_CERTIFICATE_DATA = "user_certificate_data"; |
| 119 | |
| 120 | /** |
| 121 | * Intent extra: name for CA certificate chain |
| 122 | */ |
| 123 | public static final String EXTRA_CA_CERTIFICATES_NAME = "ca_certificates_name"; |
| 124 | |
| 125 | /** |
| 126 | * Intent extra: data for CA certificate chain in PEM-encoded X.509. |
| 127 | */ |
| 128 | public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data"; |
| 129 | |
| 130 | /** |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 131 | * Convert objects to a PEM format which is used for |
| 132 | * CA_CERTIFICATE and USER_CERTIFICATE entries. |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 133 | */ |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 134 | public static byte[] convertToPem(Certificate... objects) |
| 135 | throws IOException, CertificateEncodingException { |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 136 | ByteArrayOutputStream bao = new ByteArrayOutputStream(); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 137 | Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII); |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 138 | PemWriter pw = new PemWriter(writer); |
| 139 | for (Certificate o : objects) { |
| 140 | pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded())); |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 141 | } |
| 142 | pw.close(); |
| 143 | return bao.toByteArray(); |
| 144 | } |
| 145 | /** |
| 146 | * Convert objects from PEM format, which is used for |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 147 | * CA_CERTIFICATE and USER_CERTIFICATE entries. |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 148 | */ |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 149 | public static List<X509Certificate> convertFromPem(byte[] bytes) |
| 150 | throws IOException, CertificateException { |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 151 | ByteArrayInputStream bai = new ByteArrayInputStream(bytes); |
Elliott Hughes | d396a44 | 2013-06-28 16:24:48 -0700 | [diff] [blame] | 152 | Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII); |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 153 | PemReader pr = new PemReader(reader); |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 154 | |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 155 | try { |
| 156 | CertificateFactory cf = CertificateFactory.getInstance("X509"); |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 157 | |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 158 | List<X509Certificate> result = new ArrayList<X509Certificate>(); |
| 159 | PemObject o; |
| 160 | while ((o = pr.readPemObject()) != null) { |
| 161 | if (o.getType().equals("CERTIFICATE")) { |
| 162 | Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent())); |
| 163 | result.add((X509Certificate) c); |
| 164 | } else { |
| 165 | throw new IllegalArgumentException("Unknown type " + o.getType()); |
| 166 | } |
Brian Carlstrom | 0efca17 | 2012-09-04 23:01:07 -0700 | [diff] [blame] | 167 | } |
Shawn Willden | 8d8c747 | 2016-02-02 08:27:39 -0700 | [diff] [blame] | 168 | return result; |
| 169 | } finally { |
| 170 | pr.close(); |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 171 | } |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 172 | } |
| 173 | |
Chia-chi Yeh | 4403917 | 2009-09-21 11:53:59 +0800 | [diff] [blame] | 174 | private static Credentials singleton; |
| 175 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 176 | @UnsupportedAppUsage |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 177 | public static Credentials getInstance() { |
| 178 | if (singleton == null) { |
| 179 | singleton = new Credentials(); |
| 180 | } |
| 181 | return singleton; |
| 182 | } |
| 183 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 184 | @UnsupportedAppUsage |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 185 | public void unlock(Context context) { |
| 186 | try { |
Brian Carlstrom | 4a9e1a2 | 2011-04-22 15:45:22 -0700 | [diff] [blame] | 187 | Intent intent = new Intent(UNLOCK_ACTION); |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 188 | context.startActivity(intent); |
| 189 | } catch (ActivityNotFoundException e) { |
| 190 | Log.w(LOGTAG, e.toString()); |
| 191 | } |
| 192 | } |
| 193 | |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 194 | public void install(Context context) { |
| 195 | try { |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 196 | Intent intent = KeyChain.createInstallIntent(); |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 197 | context.startActivity(intent); |
| 198 | } catch (ActivityNotFoundException e) { |
| 199 | Log.w(LOGTAG, e.toString()); |
| 200 | } |
| 201 | } |
| 202 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 203 | @UnsupportedAppUsage |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 204 | public void install(Context context, KeyPair pair) { |
| 205 | try { |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 206 | Intent intent = KeyChain.createInstallIntent(); |
| 207 | intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded()); |
| 208 | intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded()); |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 209 | context.startActivity(intent); |
| 210 | } catch (ActivityNotFoundException e) { |
| 211 | Log.w(LOGTAG, e.toString()); |
| 212 | } |
| 213 | } |
| 214 | |
Mathew Inwood | 4dbdcf4 | 2018-08-16 18:49:37 +0100 | [diff] [blame] | 215 | @UnsupportedAppUsage |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 216 | public void install(Context context, String type, byte[] value) { |
| 217 | try { |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 218 | Intent intent = KeyChain.createInstallIntent(); |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 219 | intent.putExtra(type, value); |
| 220 | context.startActivity(intent); |
| 221 | } catch (ActivityNotFoundException e) { |
| 222 | Log.w(LOGTAG, e.toString()); |
| 223 | } |
| 224 | } |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 225 | |
| 226 | /** |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 227 | * Delete all types (private key, user certificate, CA certificate) for a |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 228 | * particular {@code alias}. All three can exist for any given alias. |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 229 | * Returns {@code true} if the alias no longer contains any types. |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 230 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 231 | public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) { |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 232 | return deleteAllTypesForAlias(keystore, alias, KeyStore.UID_SELF); |
| 233 | } |
| 234 | |
| 235 | /** |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 236 | * Delete all types (private key, user certificate, CA certificate) for a |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 237 | * particular {@code alias}. All three can exist for any given alias. |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 238 | * Returns {@code true} if the alias no longer contains any types. |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 239 | */ |
| 240 | public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias, int uid) { |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 241 | /* |
| 242 | * Make sure every type is deleted. There can be all three types, so |
| 243 | * don't use a conditional here. |
| 244 | */ |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 245 | return deleteUserKeyTypeForAlias(keystore, alias, uid) |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 246 | & deleteCertificateTypesForAlias(keystore, alias, uid); |
Kenny Root | 802768d | 2012-08-21 15:23:35 -0700 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | /** |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 250 | * Delete certificate types (user certificate, CA certificate) for a |
| 251 | * particular {@code alias}. Both can exist for any given alias. |
| 252 | * Returns {@code true} if the alias no longer contains either type. |
Kenny Root | 802768d | 2012-08-21 15:23:35 -0700 | [diff] [blame] | 253 | */ |
Alex Klyubin | dcdaf87 | 2015-05-13 15:57:09 -0700 | [diff] [blame] | 254 | public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias) { |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 255 | return deleteCertificateTypesForAlias(keystore, alias, KeyStore.UID_SELF); |
| 256 | } |
| 257 | |
| 258 | /** |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 259 | * Delete certificate types (user certificate, CA certificate) for a |
| 260 | * particular {@code alias}. Both can exist for any given alias. |
| 261 | * Returns {@code true} if the alias no longer contains either type. |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 262 | */ |
| 263 | public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias, int uid) { |
Kenny Root | 802768d | 2012-08-21 15:23:35 -0700 | [diff] [blame] | 264 | /* |
| 265 | * Make sure every certificate type is deleted. There can be two types, |
| 266 | * so don't use a conditional here. |
| 267 | */ |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 268 | return keystore.delete(Credentials.USER_CERTIFICATE + alias, uid) |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 269 | & keystore.delete(Credentials.CA_CERTIFICATE + alias, uid); |
Kenny Root | db02671 | 2012-08-20 10:48:46 -0700 | [diff] [blame] | 270 | } |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 271 | |
| 272 | /** |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 273 | * Delete user key for a particular {@code alias}. |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 274 | * Returns {@code true} if the entry no longer exists. |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 275 | */ |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 276 | public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias) { |
| 277 | return deleteUserKeyTypeForAlias(keystore, alias, KeyStore.UID_SELF); |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | /** |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 281 | * Delete user key for a particular {@code alias}. |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 282 | * Returns {@code true} if the entry no longer exists. |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 283 | */ |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 284 | public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias, int uid) { |
Janis Danisevskis | 906147c | 2018-11-06 14:14:05 -0800 | [diff] [blame] | 285 | int ret = keystore.delete2(Credentials.USER_PRIVATE_KEY + alias, uid); |
| 286 | if (ret == KeyStore.KEY_NOT_FOUND) { |
| 287 | return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid); |
| 288 | } |
| 289 | return ret == KeyStore.NO_ERROR; |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 290 | } |
| 291 | |
| 292 | /** |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 293 | * Delete legacy prefixed entry for a particular {@code alias} |
Robin Lee | e4487ea | 2016-02-29 17:43:54 +0000 | [diff] [blame] | 294 | * Returns {@code true} if the entry no longer exists. |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 295 | */ |
Janis Danisevskis | 64338c0 | 2017-04-19 09:17:17 -0700 | [diff] [blame] | 296 | public static boolean deleteLegacyKeyForAlias(KeyStore keystore, String alias, int uid) { |
Alex Klyubin | 3876b1b | 2015-09-09 14:55:03 -0700 | [diff] [blame] | 297 | return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid); |
Alex Klyubin | baf2838 | 2015-03-26 14:46:55 -0700 | [diff] [blame] | 298 | } |
Chia-chi Yeh | 9b7a3f1 | 2009-09-18 12:00:12 +0800 | [diff] [blame] | 299 | } |