blob: 72332ebd027398eee566c105d081f298c0f14201 [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;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070023import com.android.org.bouncycastle.openssl.PEMReader;
24import com.android.org.bouncycastle.openssl.PEMWriter;
25import java.io.ByteArrayInputStream;
26import java.io.ByteArrayOutputStream;
27import java.io.IOException;
28import java.io.InputStreamReader;
Kenny Root5423e682011-11-14 08:43:13 -080029import java.io.ObjectOutputStream;
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070030import java.io.OutputStreamWriter;
31import java.io.Reader;
32import java.io.Writer;
33import java.nio.charset.Charsets;
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080034import java.security.KeyPair;
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
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -070047 public static final String UNLOCK_ACTION = "com.android.credentials.UNLOCK";
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
55 /** Key prefix for user private keys. */
56 public static final String USER_PRIVATE_KEY = "USRPKEY_";
57
58 /** Key prefix for VPN. */
59 public static final String VPN = "VPN_";
60
61 /** Key prefix for WIFI. */
62 public static final String WIFI = "WIFI_";
63
64 /** Data type for public keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070065 public static final String EXTRA_PUBLIC_KEY = "KEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080066
67 /** Data type for private keys. */
Brian Carlstroma00a2b32011-06-29 10:42:35 -070068 public static final String EXTRA_PRIVATE_KEY = "PKEY";
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +080069
Brian Carlstrom67c30df2011-06-24 02:13:23 -070070 // historically used by Android
71 public static final String EXTENSION_CRT = ".crt";
72 public static final String EXTENSION_P12 = ".p12";
73 // commonly used on Windows
74 public static final String EXTENSION_CER = ".cer";
75 public static final String EXTENSION_PFX = ".pfx";
76
Brian Carlstrom9d7faa92011-06-07 13:45:33 -070077 /**
Kenny Root5423e682011-11-14 08:43:13 -080078 * Intent extra: name for the user's private key.
79 */
80 public static final String EXTRA_USER_PRIVATE_KEY_NAME = "user_private_key_name";
81
82 /**
83 * Intent extra: data for the user's private key in PEM-encoded PKCS#8.
84 */
85 public static final String EXTRA_USER_PRIVATE_KEY_DATA = "user_private_key_data";
86
87 /**
88 * Intent extra: name for the user's certificate.
89 */
90 public static final String EXTRA_USER_CERTIFICATE_NAME = "user_certificate_name";
91
92 /**
93 * Intent extra: data for the user's certificate in PEM-encoded X.509.
94 */
95 public static final String EXTRA_USER_CERTIFICATE_DATA = "user_certificate_data";
96
97 /**
98 * Intent extra: name for CA certificate chain
99 */
100 public static final String EXTRA_CA_CERTIFICATES_NAME = "ca_certificates_name";
101
102 /**
103 * Intent extra: data for CA certificate chain in PEM-encoded X.509.
104 */
105 public static final String EXTRA_CA_CERTIFICATES_DATA = "ca_certificates_data";
106
107 /**
Brian Carlstrom9d7faa92011-06-07 13:45:33 -0700108 * Convert objects to a PEM format, which is used for
109 * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
110 * entries.
111 */
112 public static byte[] convertToPem(Object... objects) throws IOException {
113 ByteArrayOutputStream bao = new ByteArrayOutputStream();
114 Writer writer = new OutputStreamWriter(bao, Charsets.US_ASCII);
115 PEMWriter pw = new PEMWriter(writer);
116 for (Object o : objects) {
117 pw.writeObject(o);
118 }
119 pw.close();
120 return bao.toByteArray();
121 }
122 /**
123 * Convert objects from PEM format, which is used for
124 * CA_CERTIFICATE, USER_CERTIFICATE, and USER_PRIVATE_KEY
125 * entries.
126 */
127 public static List<Object> convertFromPem(byte[] bytes) throws IOException {
128 ByteArrayInputStream bai = new ByteArrayInputStream(bytes);
129 Reader reader = new InputStreamReader(bai, Charsets.US_ASCII);
130 PEMReader pr = new PEMReader(reader);
131
132 List<Object> result = new ArrayList<Object>();
133 Object o;
134 while ((o = pr.readObject()) != null) {
135 result.add(o);
136 }
137 pr.close();
138 return result;
139 }
140
Chia-chi Yeh44039172009-09-21 11:53:59 +0800141 private static Credentials singleton;
142
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800143 public static Credentials getInstance() {
144 if (singleton == null) {
145 singleton = new Credentials();
146 }
147 return singleton;
148 }
149
150 public void unlock(Context context) {
151 try {
Brian Carlstrom4a9e1a22011-04-22 15:45:22 -0700152 Intent intent = new Intent(UNLOCK_ACTION);
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800153 context.startActivity(intent);
154 } catch (ActivityNotFoundException e) {
155 Log.w(LOGTAG, e.toString());
156 }
157 }
158
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700159 public void install(Context context) {
160 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700161 Intent intent = KeyChain.createInstallIntent();
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700162 context.startActivity(intent);
163 } catch (ActivityNotFoundException e) {
164 Log.w(LOGTAG, e.toString());
165 }
166 }
167
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800168 public void install(Context context, KeyPair pair) {
169 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700170 Intent intent = KeyChain.createInstallIntent();
171 intent.putExtra(EXTRA_PRIVATE_KEY, pair.getPrivate().getEncoded());
172 intent.putExtra(EXTRA_PUBLIC_KEY, pair.getPublic().getEncoded());
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800173 context.startActivity(intent);
174 } catch (ActivityNotFoundException e) {
175 Log.w(LOGTAG, e.toString());
176 }
177 }
178
179 public void install(Context context, String type, byte[] value) {
180 try {
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700181 Intent intent = KeyChain.createInstallIntent();
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800182 intent.putExtra(type, value);
183 context.startActivity(intent);
184 } catch (ActivityNotFoundException e) {
185 Log.w(LOGTAG, e.toString());
186 }
187 }
Kenny Rootdb026712012-08-20 10:48:46 -0700188
189 /**
190 * Delete all types (private key, certificate, CA certificate) for a
191 * particular {@code alias}. All three can exist for any given alias.
192 * Returns {@code true} if there was at least one of those types.
193 */
194 static boolean deleteAllTypesForAlias(KeyStore keystore, String alias) {
195 /*
196 * Make sure every type is deleted. There can be all three types, so
197 * don't use a conditional here.
198 */
199 return keystore.delKey(Credentials.USER_PRIVATE_KEY + alias)
200 | keystore.delete(Credentials.USER_CERTIFICATE + alias)
201 | keystore.delete(Credentials.CA_CERTIFICATE + alias);
202 }
Chia-chi Yeh9b7a3f12009-09-18 12:00:12 +0800203}