blob: 7216a2201c88aab7cdbed755a4b10dbfe7758f50 [file] [log] [blame]
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +08001/*
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
17package android.security;
18
Mathew Inwood4dbdcf42018-08-16 18:49:37 +010019import android.annotation.UnsupportedAppUsage;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080020import android.content.ActivityNotFoundException;
21import android.content.Context;
22import android.content.Intent;
23import android.util.Log;
Shawn Willden8d8c7472016-02-02 08:27:39 -070024
Brian Carlstrom0efca172012-09-04 23:01:07 -070025import com.android.org.bouncycastle.util.io.pem.PemObject;
26import com.android.org.bouncycastle.util.io.pem.PemReader;
27import com.android.org.bouncycastle.util.io.pem.PemWriter;
Shawn Willden8d8c7472016-02-02 08:27:39 -070028
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070029import java.io.ByteArrayInputStream;
30import java.io.ByteArrayOutputStream;
31import java.io.IOException;
32import java.io.InputStreamReader;
33import java.io.OutputStreamWriter;
34import java.io.Reader;
35import java.io.Writer;
Elliott Hughesd396a442013-06-28 16:24:48 -070036import java.nio.charset.StandardCharsets;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080037import java.security.KeyPair;
Brian Carlstrom0efca172012-09-04 23:01:07 -070038import java.security.cert.Certificate;
39import java.security.cert.CertificateEncodingException;
40import java.security.cert.CertificateException;
41import java.security.cert.CertificateFactory;
Kenny Root5423e682011-11-14 08:43:13 -080042import java.security.cert.X509Certificate;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070043import java.util.ArrayList;
44import java.util.List;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080045
46/**
47 * {@hide}
48 */
49public class Credentials {
50 private static final String LOGTAG = "Credentials";
Chia-chi Yeh44039172009-09-21 11:53:59 +080051
Chia-chi Yeh44039172009-09-21 11:53:59 +080052 public static final String INSTALL_ACTION = "android.credentials.INSTALL";
53
Kenny Root3e7be432013-03-28 09:25:51 -070054 public static final String INSTALL_AS_USER_ACTION = "android.credentials.INSTALL_AS_USER";
55
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -070056 public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK";
57
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080058 /** 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 Danisevskis64338c02017-04-19 09:17:17 -070064 /** Key prefix for user private and secret keys. */
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080065 public static final String USER_PRIVATE_KEY = "USRPKEY_";
66
Janis Danisevskis64338c02017-04-19 09:17:17 -070067 /** Key prefix for user secret keys.
68 * @deprecated use {@code USER_PRIVATE_KEY} for this category instead.
69 */
Alex Klyubinbaf28382015-03-26 14:46:55 -070070 public static final String USER_SECRET_KEY = "USRSKEY_";
71
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080072 /** 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 Sharkey69ddab42012-08-25 00:05:46 -070078 /** Key containing suffix of lockdown VPN profile. */
79 public static final String LOCKDOWN_VPN = "LOCKDOWN_VPN";
80
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080081 /** Data type for public keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070082 public static final String EXTRA_PUBLIC_KEY = "KEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080083
84 /** Data type for private keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070085 public static final String EXTRA_PRIVATE_KEY = "PKEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080086
Brian Carlstrom67c30df2011-06-24 02:13:23 -070087 // 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 Carlstrom9d7faa92011-06-07 13:45:33 -070094 /**
Kenny Root3e7be432013-03-28 09:25:51 -070095 * 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 Root5423e682011-11-14 08:43:13 -0800101 * 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 Carlstrom0efca172012-09-04 23:01:07 -0700131 * Convert objects to a PEM format which is used for
132 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700133 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700134 public static byte[] convertToPem(Certificate... objects)
135 throws IOException, CertificateEncodingException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700136 ByteArrayOutputStream bao = new ByteArrayOutputStream();
Elliott Hughesd396a442013-06-28 16:24:48 -0700137 Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700138 PemWriter pw = new PemWriter(writer);
139 for (Certificate o : objects) {
140 pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded()));
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700141 }
142 pw.close();
143 return bao.toByteArray();
144 }
145 /**
146 * Convert objects from PEM format, which is used for
Brian Carlstrom0efca172012-09-04 23:01:07 -0700147 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700148 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700149 public static List<X509Certificate> convertFromPem(byte[] bytes)
150 throws IOException, CertificateException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700151 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
Elliott Hughesd396a442013-06-28 16:24:48 -0700152 Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700153 PemReader pr = new PemReader(reader);
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700154
Shawn Willden8d8c7472016-02-02 08:27:39 -0700155 try {
156 CertificateFactory cf = CertificateFactory.getInstance("X509");
Brian Carlstrom0efca172012-09-04 23:01:07 -0700157
Shawn Willden8d8c7472016-02-02 08:27:39 -0700158 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 Carlstrom0efca172012-09-04 23:01:07 -0700167 }
Shawn Willden8d8c7472016-02-02 08:27:39 -0700168 return result;
169 } finally {
170 pr.close();
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700171 }
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700172 }
173
Chia-chi Yeh44039172009-09-21 11:53:59 +0800174 private static Credentials singleton;
175
Mathew Inwood4dbdcf42018-08-16 18:49:37 +0100176 @UnsupportedAppUsage
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800177 public static Credentials getInstance() {
178 if (singleton == null) {
179 singleton = new Credentials();
180 }
181 return singleton;
182 }
183
Mathew Inwood4dbdcf42018-08-16 18:49:37 +0100184 @UnsupportedAppUsage
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800185 public void unlock(Context context) {
186 try {
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -0700187 Intent intent = new Intent(UNLOCK_ACTION);
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800188 context.startActivity(intent);
189 } catch (ActivityNotFoundException e) {
190 Log.w(LOGTAG, e.toString());
191 }
192 }
193
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700194 public void install(Context context) {
195 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700196 Intent intent = KeyChain.createInstallIntent();
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700197 context.startActivity(intent);
198 } catch (ActivityNotFoundException e) {
199 Log.w(LOGTAG, e.toString());
200 }
201 }
202
Mathew Inwood4dbdcf42018-08-16 18:49:37 +0100203 @UnsupportedAppUsage
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800204 public void install(Context context, KeyPair pair) {
205 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700206 Intent intent = KeyChain.createInstallIntent();
207 intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded());
208 intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded());
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800209 context.startActivity(intent);
210 } catch (ActivityNotFoundException e) {
211 Log.w(LOGTAG, e.toString());
212 }
213 }
214
Mathew Inwood4dbdcf42018-08-16 18:49:37 +0100215 @UnsupportedAppUsage
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800216 public void install(Context context, String type, byte[] value) {
217 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700218 Intent intent = KeyChain.createInstallIntent();
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800219 intent.putExtra(type, value);
220 context.startActivity(intent);
221 } catch (ActivityNotFoundException e) {
222 Log.w(LOGTAG, e.toString());
223 }
224 }
Kenny Rootdb026712012-08-20 10:48:46 -0700225
226 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000227 * Delete all types (private key, user certificate, CA certificate) for a
Kenny Rootdb026712012-08-20 10:48:46 -0700228 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000229 * Returns {@code true} if the alias no longer contains any types.
Kenny Rootdb026712012-08-20 10:48:46 -0700230 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700231 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700232 return deleteAllTypesForAlias(keystore, alias, KeyStore.UID_SELF);
233 }
234
235 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000236 * Delete all types (private key, user certificate, CA certificate) for a
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700237 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000238 * Returns {@code true} if the alias no longer contains any types.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700239 */
240 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Rootdb026712012-08-20 10:48:46 -0700241 /*
242 * Make sure every type is deleted. There can be all three types, so
243 * don't use a conditional here.
244 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700245 return deleteUserKeyTypeForAlias(keystore, alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000246 & deleteCertificateTypesForAlias(keystore, alias, uid);
Kenny Root802768d2012-08-21 15:23:35 -0700247 }
248
249 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000250 * 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 Root802768d2012-08-21 15:23:35 -0700253 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700254 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700255 return deleteCertificateTypesForAlias(keystore, alias, KeyStore.UID_SELF);
256 }
257
258 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000259 * 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 Klyubin3876b1b2015-09-09 14:55:03 -0700262 */
263 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Root802768d2012-08-21 15:23:35 -0700264 /*
265 * Make sure every certificate type is deleted. There can be two types,
266 * so don't use a conditional here.
267 */
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700268 return keystore.delete(Credentials.USER_CERTIFICATE + alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000269 & keystore.delete(Credentials.CA_CERTIFICATE + alias, uid);
Kenny Rootdb026712012-08-20 10:48:46 -0700270 }
Alex Klyubinbaf28382015-03-26 14:46:55 -0700271
272 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700273 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000274 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700275 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700276 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias) {
277 return deleteUserKeyTypeForAlias(keystore, alias, KeyStore.UID_SELF);
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700278 }
279
280 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700281 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000282 * Returns {@code true} if the entry no longer exists.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700283 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700284 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias, int uid) {
285 return keystore.delete(Credentials.USER_PRIVATE_KEY + alias, uid) ||
286 keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700287 }
288
289 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700290 * Delete legacy prefixed entry for a particular {@code alias}
Robin Leee4487ea2016-02-29 17:43:54 +0000291 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700292 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700293 public static boolean deleteLegacyKeyForAlias(KeyStore keystore, String alias, int uid) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700294 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700295 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800296}