blob: 2ae28f507e0d64903d3dda03ff9040735f16ad83 [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
Brian Carlstrom0efca172012-09-04 23:01:07 -070019import com.android.org.bouncycastle.util.io.pem.PemObject;
20import com.android.org.bouncycastle.util.io.pem.PemReader;
21import com.android.org.bouncycastle.util.io.pem.PemWriter;
Shawn Willden8d8c7472016-02-02 08:27:39 -070022
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070023import java.io.ByteArrayInputStream;
24import java.io.ByteArrayOutputStream;
25import java.io.IOException;
26import java.io.InputStreamReader;
27import java.io.OutputStreamWriter;
28import java.io.Reader;
29import java.io.Writer;
Elliott Hughesd396a442013-06-28 16:24:48 -070030import java.nio.charset.StandardCharsets;
Brian Carlstrom0efca172012-09-04 23:01:07 -070031import java.security.cert.Certificate;
32import java.security.cert.CertificateEncodingException;
33import java.security.cert.CertificateException;
34import java.security.cert.CertificateFactory;
Kenny Root5423e682011-11-14 08:43:13 -080035import java.security.cert.X509Certificate;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070036import java.util.ArrayList;
37import java.util.List;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080038
39/**
40 * {@hide}
41 */
42public class Credentials {
43 private static final String LOGTAG = "Credentials";
Chia-chi Yeh44039172009-09-21 11:53:59 +080044
Chia-chi Yeh44039172009-09-21 11:53:59 +080045 public static final String INSTALL_ACTION = "android.credentials.INSTALL";
46
Kenny Root3e7be432013-03-28 09:25:51 -070047 public static final String INSTALL_AS_USER_ACTION = "android.credentials.INSTALL_AS_USER";
48
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080049 /** Key prefix for CA certificates. */
50 public static final String CA_CERTIFICATE = "CACERT_";
51
52 /** Key prefix for user certificates. */
53 public static final String USER_CERTIFICATE = "USRCERT_";
54
Janis Danisevskis64338c02017-04-19 09:17:17 -070055 /** Key prefix for user private and secret keys. */
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080056 public static final String USER_PRIVATE_KEY = "USRPKEY_";
57
Janis Danisevskis64338c02017-04-19 09:17:17 -070058 /** Key prefix for user secret keys.
59 * @deprecated use {@code USER_PRIVATE_KEY} for this category instead.
60 */
Alex Klyubinbaf28382015-03-26 14:46:55 -070061 public static final String USER_SECRET_KEY = "USRSKEY_";
62
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080063 /** Key prefix for VPN. */
64 public static final String VPN = "VPN_";
65
66 /** Key prefix for WIFI. */
67 public static final String WIFI = "WIFI_";
68
Jeff Sharkey69ddab42012-08-25 00:05:46 -070069 /** Key containing suffix of lockdown VPN profile. */
70 public static final String LOCKDOWN_VPN = "LOCKDOWN_VPN";
71
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080072 /** Data type for public keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070073 public static final String EXTRA_PUBLIC_KEY = "KEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080074
75 /** Data type for private keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070076 public static final String EXTRA_PRIVATE_KEY = "PKEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080077
Brian Carlstrom67c30df2011-06-24 02:13:23 -070078 // historically used by Android
79 public static final String EXTENSION_CRT = ".crt";
80 public static final String EXTENSION_P12 = ".p12";
81 // commonly used on Windows
82 public static final String EXTENSION_CER = ".cer";
83 public static final String EXTENSION_PFX = ".pfx";
84
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070085 /**
Kenny Root3e7be432013-03-28 09:25:51 -070086 * Intent extra: install the certificate bundle as this UID instead of
87 * system.
88 */
89 public static final String EXTRA_INSTALL_AS_UID = "install_as_uid";
90
91 /**
Kenny Root5423e682011-11-14 08:43:13 -080092 * Intent extra: name for the user's private key.
93 */
94 public static final String EXTRA_USER_PRIVATE_KEY_NAME = "user_private_key_name";
95
96 /**
97 * Intent extra: data for the user's private key in PEM-encoded PKCS#8.
98 */
99 public static final String EXTRA_USER_PRIVATE_KEY_DATA = "user_private_key_data";
100
101 /**
102 * Intent extra: name for the user's certificate.
103 */
104 public static final String EXTRA_USER_CERTIFICATE_NAME = "user_certificate_name";
105
106 /**
107 * Intent extra: data for the user's certificate in PEM-encoded X.509.
108 */
109 public static final String EXTRA_USER_CERTIFICATE_DATA = "user_certificate_data";
110
111 /**
112 * Intent extra: name for CA certificate chain
113 */
114 public static final String EXTRA_CA_CERTIFICATES_NAME = "ca_certificates_name";
115
116 /**
117 * Intent extra: data for CA certificate chain in PEM-encoded X.509.
118 */
119 public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data";
120
121 /**
Brian Carlstrom0efca172012-09-04 23:01:07 -0700122 * Convert objects to a PEM format which is used for
123 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700124 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700125 public static byte[] convertToPem(Certificate... objects)
126 throws IOException, CertificateEncodingException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700127 ByteArrayOutputStream bao = new ByteArrayOutputStream();
Elliott Hughesd396a442013-06-28 16:24:48 -0700128 Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700129 PemWriter pw = new PemWriter(writer);
130 for (Certificate o : objects) {
131 pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded()));
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700132 }
133 pw.close();
134 return bao.toByteArray();
135 }
136 /**
137 * Convert objects from PEM format, which is used for
Brian Carlstrom0efca172012-09-04 23:01:07 -0700138 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700139 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700140 public static List<X509Certificate> convertFromPem(byte[] bytes)
141 throws IOException, CertificateException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700142 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
Elliott Hughesd396a442013-06-28 16:24:48 -0700143 Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700144 PemReader pr = new PemReader(reader);
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700145
Shawn Willden8d8c7472016-02-02 08:27:39 -0700146 try {
147 CertificateFactory cf = CertificateFactory.getInstance("X509");
Brian Carlstrom0efca172012-09-04 23:01:07 -0700148
Shawn Willden8d8c7472016-02-02 08:27:39 -0700149 List<X509Certificate> result = new ArrayList<X509Certificate>();
150 PemObject o;
151 while ((o = pr.readPemObject()) != null) {
152 if (o.getType().equals("CERTIFICATE")) {
153 Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent()));
154 result.add((X509Certificate) c);
155 } else {
156 throw new IllegalArgumentException("Unknown type " + o.getType());
157 }
Brian Carlstrom0efca172012-09-04 23:01:07 -0700158 }
Shawn Willden8d8c7472016-02-02 08:27:39 -0700159 return result;
160 } finally {
161 pr.close();
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700162 }
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700163 }
164
Kenny Rootdb026712012-08-20 10:48:46 -0700165 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000166 * Delete all types (private key, user certificate, CA certificate) for a
Kenny Rootdb026712012-08-20 10:48:46 -0700167 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000168 * Returns {@code true} if the alias no longer contains any types.
Kenny Rootdb026712012-08-20 10:48:46 -0700169 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700170 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700171 return deleteAllTypesForAlias(keystore, alias, KeyStore.UID_SELF);
172 }
173
174 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000175 * Delete all types (private key, user certificate, CA certificate) for a
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700176 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000177 * Returns {@code true} if the alias no longer contains any types.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700178 */
179 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Rootdb026712012-08-20 10:48:46 -0700180 /*
181 * Make sure every type is deleted. There can be all three types, so
182 * don't use a conditional here.
183 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700184 return deleteUserKeyTypeForAlias(keystore, alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000185 & deleteCertificateTypesForAlias(keystore, alias, uid);
Kenny Root802768d2012-08-21 15:23:35 -0700186 }
187
188 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000189 * Delete certificate types (user certificate, CA certificate) for a
190 * particular {@code alias}. Both can exist for any given alias.
191 * Returns {@code true} if the alias no longer contains either type.
Kenny Root802768d2012-08-21 15:23:35 -0700192 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700193 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700194 return deleteCertificateTypesForAlias(keystore, alias, KeyStore.UID_SELF);
195 }
196
197 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000198 * Delete certificate types (user certificate, CA certificate) for a
199 * particular {@code alias}. Both can exist for any given alias.
200 * Returns {@code true} if the alias no longer contains either type.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700201 */
202 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Root802768d2012-08-21 15:23:35 -0700203 /*
204 * Make sure every certificate type is deleted. There can be two types,
205 * so don't use a conditional here.
206 */
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700207 return keystore.delete(Credentials.USER_CERTIFICATE + alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000208 & keystore.delete(Credentials.CA_CERTIFICATE + alias, uid);
Kenny Rootdb026712012-08-20 10:48:46 -0700209 }
Alex Klyubinbaf28382015-03-26 14:46:55 -0700210
211 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700212 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000213 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700214 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700215 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias) {
216 return deleteUserKeyTypeForAlias(keystore, alias, KeyStore.UID_SELF);
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700217 }
218
219 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700220 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000221 * Returns {@code true} if the entry no longer exists.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700222 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700223 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias, int uid) {
Janis Danisevskis906147c2018-11-06 14:14:05 -0800224 int ret = keystore.delete2(Credentials.USER_PRIVATE_KEY + alias, uid);
225 if (ret == KeyStore.KEY_NOT_FOUND) {
226 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
227 }
228 return ret == KeyStore.NO_ERROR;
Alex Klyubinbaf28382015-03-26 14:46:55 -0700229 }
230
231 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700232 * Delete legacy prefixed entry for a particular {@code alias}
Robin Leee4487ea2016-02-29 17:43:54 +0000233 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700234 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700235 public static boolean deleteLegacyKeyForAlias(KeyStore keystore, String alias, int uid) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700236 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700237 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800238}