blob: 57db20be1145acdaee57f7eb20321c5d0b9d548b [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
19import android.content.ActivityNotFoundException;
20import android.content.Context;
21import android.content.Intent;
22import android.util.Log;
Shawn Willden8d8c7472016-02-02 08:27:39 -070023
Brian Carlstrom0efca172012-09-04 23:01:07 -070024import com.android.org.bouncycastle.util.io.pem.PemObject;
25import com.android.org.bouncycastle.util.io.pem.PemReader;
26import com.android.org.bouncycastle.util.io.pem.PemWriter;
Shawn Willden8d8c7472016-02-02 08:27:39 -070027
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070028import java.io.ByteArrayInputStream;
29import java.io.ByteArrayOutputStream;
30import java.io.IOException;
31import java.io.InputStreamReader;
32import java.io.OutputStreamWriter;
33import java.io.Reader;
34import java.io.Writer;
Elliott Hughesd396a442013-06-28 16:24:48 -070035import java.nio.charset.StandardCharsets;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080036import java.security.KeyPair;
Brian Carlstrom0efca172012-09-04 23:01:07 -070037import java.security.cert.Certificate;
38import java.security.cert.CertificateEncodingException;
39import java.security.cert.CertificateException;
40import java.security.cert.CertificateFactory;
Kenny Root5423e682011-11-14 08:43:13 -080041import java.security.cert.X509Certificate;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070042import java.util.ArrayList;
43import java.util.List;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080044
45/**
46 * {@hide}
47 */
48public class Credentials {
49 private static final String LOGTAG = "Credentials";
Chia-chi Yeh44039172009-09-21 11:53:59 +080050
Chia-chi Yeh44039172009-09-21 11:53:59 +080051 public static final String INSTALL_ACTION = "android.credentials.INSTALL";
52
Kenny Root3e7be432013-03-28 09:25:51 -070053 public static final String INSTALL_AS_USER_ACTION = "android.credentials.INSTALL_AS_USER";
54
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -070055 public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK";
56
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080057 /** Key prefix for CA certificates. */
58 public static final String CA_CERTIFICATE = "CACERT_";
59
60 /** Key prefix for user certificates. */
61 public static final String USER_CERTIFICATE = "USRCERT_";
62
Janis Danisevskis64338c02017-04-19 09:17:17 -070063 /** Key prefix for user private and secret keys. */
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080064 public static final String USER_PRIVATE_KEY = "USRPKEY_";
65
Janis Danisevskis64338c02017-04-19 09:17:17 -070066 /** Key prefix for user secret keys.
67 * @deprecated use {@code USER_PRIVATE_KEY} for this category instead.
68 */
Alex Klyubinbaf28382015-03-26 14:46:55 -070069 public static final String USER_SECRET_KEY = "USRSKEY_";
70
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080071 /** Key prefix for VPN. */
72 public static final String VPN = "VPN_";
73
74 /** Key prefix for WIFI. */
75 public static final String WIFI = "WIFI_";
76
Jeff Sharkey69ddab42012-08-25 00:05:46 -070077 /** Key containing suffix of lockdown VPN profile. */
78 public static final String LOCKDOWN_VPN = "LOCKDOWN_VPN";
79
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080080 /** Data type for public keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070081 public static final String EXTRA_PUBLIC_KEY = "KEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080082
83 /** Data type for private keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070084 public static final String EXTRA_PRIVATE_KEY = "PKEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080085
Brian Carlstrom67c30df2011-06-24 02:13:23 -070086 // historically used by Android
87 public static final String EXTENSION_CRT = ".crt";
88 public static final String EXTENSION_P12 = ".p12";
89 // commonly used on Windows
90 public static final String EXTENSION_CER = ".cer";
91 public static final String EXTENSION_PFX = ".pfx";
92
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070093 /**
Kenny Root3e7be432013-03-28 09:25:51 -070094 * Intent extra: install the certificate bundle as this UID instead of
95 * system.
96 */
97 public static final String EXTRA_INSTALL_AS_UID = "install_as_uid";
98
99 /**
Kenny Root5423e682011-11-14 08:43:13 -0800100 * Intent extra: name for the user's private key.
101 */
102 public static final String EXTRA_USER_PRIVATE_KEY_NAME = "user_private_key_name";
103
104 /**
105 * Intent extra: data for the user's private key in PEM-encoded PKCS#8.
106 */
107 public static final String EXTRA_USER_PRIVATE_KEY_DATA = "user_private_key_data";
108
109 /**
110 * Intent extra: name for the user's certificate.
111 */
112 public static final String EXTRA_USER_CERTIFICATE_NAME = "user_certificate_name";
113
114 /**
115 * Intent extra: data for the user's certificate in PEM-encoded X.509.
116 */
117 public static final String EXTRA_USER_CERTIFICATE_DATA = "user_certificate_data";
118
119 /**
120 * Intent extra: name for CA certificate chain
121 */
122 public static final String EXTRA_CA_CERTIFICATES_NAME = "ca_certificates_name";
123
124 /**
125 * Intent extra: data for CA certificate chain in PEM-encoded X.509.
126 */
127 public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data";
128
129 /**
Brian Carlstrom0efca172012-09-04 23:01:07 -0700130 * Convert objects to a PEM format which is used for
131 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700132 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700133 public static byte[] convertToPem(Certificate... objects)
134 throws IOException, CertificateEncodingException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700135 ByteArrayOutputStream bao = new ByteArrayOutputStream();
Elliott Hughesd396a442013-06-28 16:24:48 -0700136 Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700137 PemWriter pw = new PemWriter(writer);
138 for (Certificate o : objects) {
139 pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded()));
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700140 }
141 pw.close();
142 return bao.toByteArray();
143 }
144 /**
145 * Convert objects from PEM format, which is used for
Brian Carlstrom0efca172012-09-04 23:01:07 -0700146 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700147 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700148 public static List<X509Certificate> convertFromPem(byte[] bytes)
149 throws IOException, CertificateException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700150 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
Elliott Hughesd396a442013-06-28 16:24:48 -0700151 Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700152 PemReader pr = new PemReader(reader);
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700153
Shawn Willden8d8c7472016-02-02 08:27:39 -0700154 try {
155 CertificateFactory cf = CertificateFactory.getInstance("X509");
Brian Carlstrom0efca172012-09-04 23:01:07 -0700156
Shawn Willden8d8c7472016-02-02 08:27:39 -0700157 List<X509Certificate> result = new ArrayList<X509Certificate>();
158 PemObject o;
159 while ((o = pr.readPemObject()) != null) {
160 if (o.getType().equals("CERTIFICATE")) {
161 Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent()));
162 result.add((X509Certificate) c);
163 } else {
164 throw new IllegalArgumentException("Unknown type " + o.getType());
165 }
Brian Carlstrom0efca172012-09-04 23:01:07 -0700166 }
Shawn Willden8d8c7472016-02-02 08:27:39 -0700167 return result;
168 } finally {
169 pr.close();
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700170 }
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700171 }
172
Chia-chi Yeh44039172009-09-21 11:53:59 +0800173 private static Credentials singleton;
174
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800175 public static Credentials getInstance() {
176 if (singleton == null) {
177 singleton = new Credentials();
178 }
179 return singleton;
180 }
181
182 public void unlock(Context context) {
183 try {
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -0700184 Intent intent = new Intent(UNLOCK_ACTION);
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800185 context.startActivity(intent);
186 } catch (ActivityNotFoundException e) {
187 Log.w(LOGTAG, e.toString());
188 }
189 }
190
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700191 public void install(Context context) {
192 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700193 Intent intent = KeyChain.createInstallIntent();
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700194 context.startActivity(intent);
195 } catch (ActivityNotFoundException e) {
196 Log.w(LOGTAG, e.toString());
197 }
198 }
199
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800200 public void install(Context context, KeyPair pair) {
201 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700202 Intent intent = KeyChain.createInstallIntent();
203 intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded());
204 intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded());
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800205 context.startActivity(intent);
206 } catch (ActivityNotFoundException e) {
207 Log.w(LOGTAG, e.toString());
208 }
209 }
210
211 public void install(Context context, String type, byte[] value) {
212 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700213 Intent intent = KeyChain.createInstallIntent();
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800214 intent.putExtra(type, value);
215 context.startActivity(intent);
216 } catch (ActivityNotFoundException e) {
217 Log.w(LOGTAG, e.toString());
218 }
219 }
Kenny Rootdb026712012-08-20 10:48:46 -0700220
221 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000222 * Delete all types (private key, user certificate, CA certificate) for a
Kenny Rootdb026712012-08-20 10:48:46 -0700223 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000224 * Returns {@code true} if the alias no longer contains any types.
Kenny Rootdb026712012-08-20 10:48:46 -0700225 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700226 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700227 return deleteAllTypesForAlias(keystore, alias, KeyStore.UID_SELF);
228 }
229
230 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000231 * Delete all types (private key, user certificate, CA certificate) for a
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700232 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000233 * Returns {@code true} if the alias no longer contains any types.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700234 */
235 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Rootdb026712012-08-20 10:48:46 -0700236 /*
237 * Make sure every type is deleted. There can be all three types, so
238 * don't use a conditional here.
239 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700240 return deleteUserKeyTypeForAlias(keystore, alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000241 & deleteCertificateTypesForAlias(keystore, alias, uid);
Kenny Root802768d2012-08-21 15:23:35 -0700242 }
243
244 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000245 * Delete certificate types (user certificate, CA certificate) for a
246 * particular {@code alias}. Both can exist for any given alias.
247 * Returns {@code true} if the alias no longer contains either type.
Kenny Root802768d2012-08-21 15:23:35 -0700248 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700249 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700250 return deleteCertificateTypesForAlias(keystore, alias, KeyStore.UID_SELF);
251 }
252
253 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000254 * Delete certificate types (user certificate, CA certificate) for a
255 * particular {@code alias}. Both can exist for any given alias.
256 * Returns {@code true} if the alias no longer contains either type.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700257 */
258 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Root802768d2012-08-21 15:23:35 -0700259 /*
260 * Make sure every certificate type is deleted. There can be two types,
261 * so don't use a conditional here.
262 */
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700263 return keystore.delete(Credentials.USER_CERTIFICATE + alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000264 & keystore.delete(Credentials.CA_CERTIFICATE + alias, uid);
Kenny Rootdb026712012-08-20 10:48:46 -0700265 }
Alex Klyubinbaf28382015-03-26 14:46:55 -0700266
267 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700268 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000269 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700270 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700271 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias) {
272 return deleteUserKeyTypeForAlias(keystore, alias, KeyStore.UID_SELF);
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700273 }
274
275 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700276 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000277 * Returns {@code true} if the entry no longer exists.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700278 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700279 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias, int uid) {
280 return keystore.delete(Credentials.USER_PRIVATE_KEY + alias, uid) ||
281 keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700282 }
283
284 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700285 * Delete legacy prefixed entry for a particular {@code alias}
Robin Leee4487ea2016-02-29 17:43:54 +0000286 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700287 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700288 public static boolean deleteLegacyKeyForAlias(KeyStore keystore, String alias, int uid) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700289 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700290 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800291}