blob: 08f417662523755c1b9f607a2510eae7a7f1de76 [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
Andrei Onea4aa2a202019-02-27 14:22:05 +000023import android.annotation.UnsupportedAppUsage;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070024import java.io.ByteArrayInputStream;
25import java.io.ByteArrayOutputStream;
26import java.io.IOException;
27import java.io.InputStreamReader;
28import java.io.OutputStreamWriter;
29import java.io.Reader;
30import java.io.Writer;
Elliott Hughesd396a442013-06-28 16:24:48 -070031import java.nio.charset.StandardCharsets;
Brian Carlstrom0efca172012-09-04 23:01:07 -070032import java.security.cert.Certificate;
33import java.security.cert.CertificateEncodingException;
34import java.security.cert.CertificateException;
35import java.security.cert.CertificateFactory;
Kenny Root5423e682011-11-14 08:43:13 -080036import java.security.cert.X509Certificate;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070037import java.util.ArrayList;
38import java.util.List;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080039
40/**
41 * {@hide}
42 */
43public class Credentials {
44 private static final String LOGTAG = "Credentials";
Chia-chi Yeh44039172009-09-21 11:53:59 +080045
Chia-chi Yeh44039172009-09-21 11:53:59 +080046 public static final String INSTALL_ACTION = "android.credentials.INSTALL";
47
Kenny Root3e7be432013-03-28 09:25:51 -070048 public static final String INSTALL_AS_USER_ACTION = "android.credentials.INSTALL_AS_USER";
49
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080050 /** Key prefix for CA certificates. */
51 public static final String CA_CERTIFICATE = "CACERT_";
52
53 /** Key prefix for user certificates. */
54 public static final String USER_CERTIFICATE = "USRCERT_";
55
Janis Danisevskis64338c02017-04-19 09:17:17 -070056 /** Key prefix for user private and secret keys. */
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080057 public static final String USER_PRIVATE_KEY = "USRPKEY_";
58
Janis Danisevskis64338c02017-04-19 09:17:17 -070059 /** Key prefix for user secret keys.
60 * @deprecated use {@code USER_PRIVATE_KEY} for this category instead.
61 */
Alex Klyubinbaf28382015-03-26 14:46:55 -070062 public static final String USER_SECRET_KEY = "USRSKEY_";
63
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080064 /** Key prefix for VPN. */
65 public static final String VPN = "VPN_";
66
67 /** Key prefix for WIFI. */
68 public static final String WIFI = "WIFI_";
69
Jeff Sharkey69ddab42012-08-25 00:05:46 -070070 /** Key containing suffix of lockdown VPN profile. */
71 public static final String LOCKDOWN_VPN = "LOCKDOWN_VPN";
72
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080073 /** Data type for public keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070074 public static final String EXTRA_PUBLIC_KEY = "KEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080075
76 /** Data type for private keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070077 public static final String EXTRA_PRIVATE_KEY = "PKEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080078
Brian Carlstrom67c30df2011-06-24 02:13:23 -070079 // historically used by Android
80 public static final String EXTENSION_CRT = ".crt";
81 public static final String EXTENSION_P12 = ".p12";
82 // commonly used on Windows
83 public static final String EXTENSION_CER = ".cer";
84 public static final String EXTENSION_PFX = ".pfx";
85
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070086 /**
Kenny Root3e7be432013-03-28 09:25:51 -070087 * Intent extra: install the certificate bundle as this UID instead of
88 * system.
89 */
90 public static final String EXTRA_INSTALL_AS_UID = "install_as_uid";
91
92 /**
Kenny Root5423e682011-11-14 08:43:13 -080093 * Intent extra: name for the user's private key.
94 */
95 public static final String EXTRA_USER_PRIVATE_KEY_NAME = "user_private_key_name";
96
97 /**
98 * Intent extra: data for the user's private key in PEM-encoded PKCS#8.
99 */
100 public static final String EXTRA_USER_PRIVATE_KEY_DATA = "user_private_key_data";
101
102 /**
103 * Intent extra: name for the user's certificate.
104 */
105 public static final String EXTRA_USER_CERTIFICATE_NAME = "user_certificate_name";
106
107 /**
108 * Intent extra: data for the user's certificate in PEM-encoded X.509.
109 */
110 public static final String EXTRA_USER_CERTIFICATE_DATA = "user_certificate_data";
111
112 /**
113 * Intent extra: name for CA certificate chain
114 */
115 public static final String EXTRA_CA_CERTIFICATES_NAME = "ca_certificates_name";
116
117 /**
118 * Intent extra: data for CA certificate chain in PEM-encoded X.509.
119 */
120 public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data";
121
122 /**
Brian Carlstrom0efca172012-09-04 23:01:07 -0700123 * Convert objects to a PEM format which is used for
124 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700125 */
Andrei Onea4aa2a202019-02-27 14:22:05 +0000126 @UnsupportedAppUsage
Brian Carlstrom0efca172012-09-04 23:01:07 -0700127 public static byte[] convertToPem(Certificate... objects)
128 throws IOException, CertificateEncodingException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700129 ByteArrayOutputStream bao = new ByteArrayOutputStream();
Elliott Hughesd396a442013-06-28 16:24:48 -0700130 Writer writer = new OutputStreamWriter(bao, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700131 PemWriter pw = new PemWriter(writer);
132 for (Certificate o : objects) {
133 pw.writeObject(new PemObject("CERTIFICATE", o.getEncoded()));
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700134 }
135 pw.close();
136 return bao.toByteArray();
137 }
138 /**
139 * Convert objects from PEM format, which is used for
Brian Carlstrom0efca172012-09-04 23:01:07 -0700140 * CA_CERTIFICATE and USER_CERTIFICATE entries.
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700141 */
Brian Carlstrom0efca172012-09-04 23:01:07 -0700142 public static List<X509Certificate> convertFromPem(byte[] bytes)
143 throws IOException, CertificateException {
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700144 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
Elliott Hughesd396a442013-06-28 16:24:48 -0700145 Reader reader = new InputStreamReader(bai, StandardCharsets.US_ASCII);
Brian Carlstrom0efca172012-09-04 23:01:07 -0700146 PemReader pr = new PemReader(reader);
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700147
Shawn Willden8d8c7472016-02-02 08:27:39 -0700148 try {
149 CertificateFactory cf = CertificateFactory.getInstance("X509");
Brian Carlstrom0efca172012-09-04 23:01:07 -0700150
Shawn Willden8d8c7472016-02-02 08:27:39 -0700151 List<X509Certificate> result = new ArrayList<X509Certificate>();
152 PemObject o;
153 while ((o = pr.readPemObject()) != null) {
154 if (o.getType().equals("CERTIFICATE")) {
155 Certificate c = cf.generateCertificate(new ByteArrayInputStream(o.getContent()));
156 result.add((X509Certificate) c);
157 } else {
158 throw new IllegalArgumentException("Unknown type " + o.getType());
159 }
Brian Carlstrom0efca172012-09-04 23:01:07 -0700160 }
Shawn Willden8d8c7472016-02-02 08:27:39 -0700161 return result;
162 } finally {
163 pr.close();
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700164 }
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700165 }
166
Kenny Rootdb026712012-08-20 10:48:46 -0700167 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000168 * Delete all types (private key, user certificate, CA certificate) for a
Kenny Rootdb026712012-08-20 10:48:46 -0700169 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000170 * Returns {@code true} if the alias no longer contains any types.
Kenny Rootdb026712012-08-20 10:48:46 -0700171 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700172 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700173 return deleteAllTypesForAlias(keystore, alias, KeyStore.UID_SELF);
174 }
175
176 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000177 * Delete all types (private key, user certificate, CA certificate) for a
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700178 * particular {@code alias}. All three can exist for any given alias.
Robin Leee4487ea2016-02-29 17:43:54 +0000179 * Returns {@code true} if the alias no longer contains any types.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700180 */
181 public static boolean deleteAllTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Rootdb026712012-08-20 10:48:46 -0700182 /*
183 * Make sure every type is deleted. There can be all three types, so
184 * don't use a conditional here.
185 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700186 return deleteUserKeyTypeForAlias(keystore, alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000187 & deleteCertificateTypesForAlias(keystore, alias, uid);
Kenny Root802768d2012-08-21 15:23:35 -0700188 }
189
190 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000191 * Delete certificate types (user certificate, CA certificate) for a
192 * particular {@code alias}. Both can exist for any given alias.
193 * Returns {@code true} if the alias no longer contains either type.
Kenny Root802768d2012-08-21 15:23:35 -0700194 */
Alex Klyubindcdaf872015-05-13 15:57:09 -0700195 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700196 return deleteCertificateTypesForAlias(keystore, alias, KeyStore.UID_SELF);
197 }
198
199 /**
Robin Leee4487ea2016-02-29 17:43:54 +0000200 * Delete certificate types (user certificate, CA certificate) for a
201 * particular {@code alias}. Both can exist for any given alias.
202 * Returns {@code true} if the alias no longer contains either type.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700203 */
204 public static boolean deleteCertificateTypesForAlias(KeyStore keystore, String alias, int uid) {
Kenny Root802768d2012-08-21 15:23:35 -0700205 /*
206 * Make sure every certificate type is deleted. There can be two types,
207 * so don't use a conditional here.
208 */
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700209 return keystore.delete(Credentials.USER_CERTIFICATE + alias, uid)
Robin Leee4487ea2016-02-29 17:43:54 +0000210 & keystore.delete(Credentials.CA_CERTIFICATE + alias, uid);
Kenny Rootdb026712012-08-20 10:48:46 -0700211 }
Alex Klyubinbaf28382015-03-26 14:46:55 -0700212
213 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700214 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000215 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700216 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700217 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias) {
218 return deleteUserKeyTypeForAlias(keystore, alias, KeyStore.UID_SELF);
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700219 }
220
221 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700222 * Delete user key for a particular {@code alias}.
Robin Leee4487ea2016-02-29 17:43:54 +0000223 * Returns {@code true} if the entry no longer exists.
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700224 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700225 public static boolean deleteUserKeyTypeForAlias(KeyStore keystore, String alias, int uid) {
Janis Danisevskis906147c2018-11-06 14:14:05 -0800226 int ret = keystore.delete2(Credentials.USER_PRIVATE_KEY + alias, uid);
227 if (ret == KeyStore.KEY_NOT_FOUND) {
228 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
229 }
230 return ret == KeyStore.NO_ERROR;
Alex Klyubinbaf28382015-03-26 14:46:55 -0700231 }
232
233 /**
Janis Danisevskis64338c02017-04-19 09:17:17 -0700234 * Delete legacy prefixed entry for a particular {@code alias}
Robin Leee4487ea2016-02-29 17:43:54 +0000235 * Returns {@code true} if the entry no longer exists.
Alex Klyubinbaf28382015-03-26 14:46:55 -0700236 */
Janis Danisevskis64338c02017-04-19 09:17:17 -0700237 public static boolean deleteLegacyKeyForAlias(KeyStore keystore, String alias, int uid) {
Alex Klyubin3876b1b2015-09-09 14:55:03 -0700238 return keystore.delete(Credentials.USER_SECRET_KEY + alias, uid);
Alex Klyubinbaf28382015-03-26 14:46:55 -0700239 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800240}