blob: 1829d2f406b42ba22d6d2da7e73ffda3202647b1 [file] [log] [blame]
Brian Carlstromb9a07c12011-04-11 09:03:51 -07001/*
2 * Copyright (C) 2011 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 */
16package android.security;
17
Alex Klyubin54bb1592015-05-11 12:30:03 -070018import android.annotation.NonNull;
19import android.annotation.Nullable;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060020import android.annotation.SdkConstant;
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -060021import android.annotation.SdkConstant.SdkConstantType;
Eran Messeri55b8d082019-01-31 16:25:52 +000022import android.annotation.WorkerThread;
Brian Carlstromba1a6672011-05-24 21:54:37 -070023import android.app.Activity;
Brian Carlstrom67c30df2011-06-24 02:13:23 -070024import android.app.PendingIntent;
Robin Leeda236182016-08-12 12:46:28 +010025import android.app.Service;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070026import android.content.ComponentName;
27import android.content.Context;
28import android.content.Intent;
29import android.content.ServiceConnection;
Robin Lee39087b12015-05-05 15:57:17 +010030import android.net.Uri;
Jeff Sharkey0a17db12016-11-04 11:23:46 -060031import android.os.Binder;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070032import android.os.IBinder;
33import android.os.Looper;
Robin Lee306fe082014-06-19 14:04:24 +000034import android.os.Process;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070035import android.os.RemoteException;
Robin Lee306fe082014-06-19 14:04:24 +000036import android.os.UserHandle;
Alex Klyubin4a0ff7c2015-06-09 13:25:20 -070037import android.security.keystore.AndroidKeyStoreProvider;
Max Bires13f98ce2018-11-02 10:50:40 -070038import android.security.keystore.KeyPermanentlyInvalidatedException;
Alex Klyubin3f8d4d82015-05-13 09:15:00 -070039import android.security.keystore.KeyProperties;
40
Eran Messeri55b8d082019-01-31 16:25:52 +000041import com.android.org.conscrypt.TrustedCertificateStore;
42
Brian Carlstromb9a07c12011-04-11 09:03:51 -070043import java.io.ByteArrayInputStream;
Brian Carlstromd7524722011-05-17 16:20:36 -070044import java.io.Closeable;
Eran Messeri9ccec4d2018-08-24 15:29:05 +010045import java.io.Serializable;
Eran Messeri23c438d2017-11-23 17:20:52 +000046import java.security.KeyPair;
Brian Carlstrom93201f52011-06-09 15:05:35 -070047import java.security.Principal;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070048import java.security.PrivateKey;
Alex Klyubin4a0ff7c2015-06-09 13:25:20 -070049import java.security.UnrecoverableKeyException;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070050import java.security.cert.Certificate;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070051import java.security.cert.CertificateException;
52import java.security.cert.CertificateFactory;
Brian Carlstromb9a07c12011-04-11 09:03:51 -070053import java.security.cert.X509Certificate;
Rubin Xub4365912016-03-23 12:13:22 +000054import java.util.ArrayList;
55import java.util.Collection;
Brian Carlstromdb93b782011-07-01 00:12:17 -070056import java.util.List;
Kenny Rootb91773b2013-09-05 13:03:16 -070057import java.util.Locale;
Brian Carlstrom8e9929c2011-05-17 00:40:28 -070058import java.util.concurrent.BlockingQueue;
59import java.util.concurrent.LinkedBlockingQueue;
Kenny Root5423e682011-11-14 08:43:13 -080060
Eran Messeri9ccec4d2018-08-24 15:29:05 +010061import javax.security.auth.x500.X500Principal;
62
Brian Carlstromb9a07c12011-04-11 09:03:51 -070063/**
Brian Carlstrom93201f52011-06-09 15:05:35 -070064 * The {@code KeyChain} class provides access to private keys and
65 * their corresponding certificate chains in credential storage.
66 *
67 * <p>Applications accessing the {@code KeyChain} normally go through
68 * these steps:
69 *
70 * <ol>
71 *
72 * <li>Receive a callback from an {@link javax.net.ssl.X509KeyManager
73 * X509KeyManager} that a private key is requested.
74 *
75 * <li>Call {@link #choosePrivateKeyAlias
76 * choosePrivateKeyAlias} to allow the user to select from a
77 * list of currently available private keys and corresponding
78 * certificate chains. The chosen alias will be returned by the
79 * callback {@link KeyChainAliasCallback#alias}, or null if no private
80 * key is available or the user cancels the request.
81 *
82 * <li>Call {@link #getPrivateKey} and {@link #getCertificateChain} to
83 * retrieve the credentials to return to the corresponding {@link
84 * javax.net.ssl.X509KeyManager} callbacks.
85 *
86 * </ol>
87 *
88 * <p>An application may remember the value of a selected alias to
89 * avoid prompting the user with {@link #choosePrivateKeyAlias
90 * choosePrivateKeyAlias} on subsequent connections. If the alias is
91 * no longer valid, null will be returned on lookups using that value
Brian Carlstromca43c452011-06-29 18:53:17 -070092 *
93 * <p>An application can request the installation of private keys and
94 * certificates via the {@code Intent} provided by {@link
95 * #createInstallIntent}. Private keys installed via this {@code
96 * Intent} will be accessible via {@link #choosePrivateKeyAlias} while
97 * Certificate Authority (CA) certificates will be trusted by all
98 * applications through the default {@code X509TrustManager}.
Brian Carlstromb9a07c12011-04-11 09:03:51 -070099 */
Brian Carlstrom93201f52011-06-09 15:05:35 -0700100// TODO reference intent for credential installation when public
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700101public final class KeyChain {
102
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700103 /**
104 * @hide Also used by KeyChainService implementation
105 */
106 public static final String ACCOUNT_TYPE = "com.android.keychain";
107
108 /**
Kenny Roota3659062014-03-17 16:21:53 -0700109 * Package name for KeyChain chooser.
110 */
111 private static final String KEYCHAIN_PACKAGE = "com.android.keychain";
112
113 /**
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700114 * Action to bring up the KeyChainActivity
115 */
116 private static final String ACTION_CHOOSER = "com.android.keychain.CHOOSER";
117
118 /**
Kenny Root1a88d832014-02-07 09:12:48 -0800119 * Package name for the Certificate Installer.
120 */
121 private static final String CERT_INSTALLER_PACKAGE = "com.android.certinstaller";
122
123 /**
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700124 * Extra for use with {@link #ACTION_CHOOSER}
Brian Carlstromba1a6672011-05-24 21:54:37 -0700125 * @hide Also used by KeyChainActivity implementation
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700126 */
Brian Carlstromba1a6672011-05-24 21:54:37 -0700127 public static final String EXTRA_RESPONSE = "response";
128
129 /**
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700130 * Extra for use with {@link #ACTION_CHOOSER}
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700131 * @hide Also used by KeyChainActivity implementation
132 */
Robin Lee39087b12015-05-05 15:57:17 +0100133 public static final String EXTRA_URI = "uri";
Robin Lee3798ed52015-02-03 17:55:31 +0000134
135 /**
136 * Extra for use with {@link #ACTION_CHOOSER}
137 * @hide Also used by KeyChainActivity implementation
138 */
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700139 public static final String EXTRA_ALIAS = "alias";
140
141 /**
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700142 * Extra for use with {@link #ACTION_CHOOSER}
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700143 * @hide Also used by KeyChainActivity implementation
144 */
145 public static final String EXTRA_SENDER = "sender";
146
147 /**
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100148 * Extra for use with {@link #ACTION_CHOOSER}
149 * @hide Also used by KeyChainActivity implementation
150 */
151 public static final String EXTRA_KEY_TYPES = "key_types";
152
153 /**
154 * Extra for use with {@link #ACTION_CHOOSER}
155 * @hide Also used by KeyChainActivity implementation
156 */
157 public static final String EXTRA_ISSUERS = "issuers";
158
159 /**
Selim Gurun93ba4fe2012-02-14 11:48:49 -0800160 * Action to bring up the CertInstaller.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700161 */
162 private static final String ACTION_INSTALL = "android.credentials.INSTALL";
163
164 /**
165 * Optional extra to specify a {@code String} credential name on
166 * the {@code Intent} returned by {@link #createInstallIntent}.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700167 */
168 // Compatible with old com.android.certinstaller.CredentialHelper.CERT_NAME_KEY
169 public static final String EXTRA_NAME = "name";
170
171 /**
172 * Optional extra to specify an X.509 certificate to install on
173 * the {@code Intent} returned by {@link #createInstallIntent}.
174 * The extra value should be a PEM or ASN.1 DER encoded {@code
175 * byte[]}. An {@link X509Certificate} can be converted to DER
176 * encoded bytes with {@link X509Certificate#getEncoded}.
177 *
178 * <p>{@link #EXTRA_NAME} may be used to provide a default alias
179 * name for the installed certificate.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700180 */
181 // Compatible with old android.security.Credentials.CERTIFICATE
182 public static final String EXTRA_CERTIFICATE = "CERT";
183
184 /**
185 * Optional extra for use with the {@code Intent} returned by
186 * {@link #createInstallIntent} to specify a PKCS#12 key store to
187 * install. The extra value should be a {@code byte[]}. The bytes
188 * may come from an external source or be generated with {@link
Brian Carlstromca43c452011-06-29 18:53:17 -0700189 * java.security.KeyStore#store} on a "PKCS12" instance.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700190 *
191 * <p>The user will be prompted for the password to load the key store.
192 *
193 * <p>The key store will be scanned for {@link
194 * java.security.KeyStore.PrivateKeyEntry} entries and both the
195 * private key and associated certificate chain will be installed.
196 *
197 * <p>{@link #EXTRA_NAME} may be used to provide a default alias
198 * name for the installed credentials.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700199 */
200 // Compatible with old android.security.Credentials.PKCS12
201 public static final String EXTRA_PKCS12 = "PKCS12";
202
Selim Gurun93ba4fe2012-02-14 11:48:49 -0800203 /**
Selim Gurun93ba4fe2012-02-14 11:48:49 -0800204 * Broadcast Action: Indicates the trusted storage has changed. Sent when
205 * one of this happens:
206 *
207 * <ul>
208 * <li>a new CA is added,
209 * <li>an existing CA is removed or disabled,
210 * <li>a disabled CA is enabled,
211 * <li>trusted storage is reset (all user certs are cleared),
212 * <li>when permission to access a private key is changed.
213 * </ul>
Chad Brubaker4de59ef2016-05-02 13:17:31 -0700214 *
Chad Brubakerdbf01c12016-07-25 14:54:39 -0700215 * @deprecated Use {@link #ACTION_KEYCHAIN_CHANGED}, {@link #ACTION_TRUST_STORE_CHANGED} or
Chad Brubaker721afae2016-07-08 11:06:09 -0700216 * {@link #ACTION_KEY_ACCESS_CHANGED}. Apps that target a version higher than
Eran Messeri55b8d082019-01-31 16:25:52 +0000217 * {@link android.os.Build.VERSION_CODES#N_MR1} will only receive this broadcast if they
218 * register for it at runtime.
Selim Gurun93ba4fe2012-02-14 11:48:49 -0800219 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600220 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Selim Gurun93ba4fe2012-02-14 11:48:49 -0800221 public static final String ACTION_STORAGE_CHANGED = "android.security.STORAGE_CHANGED";
222
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700223 /**
Chad Brubaker4de59ef2016-05-02 13:17:31 -0700224 * Broadcast Action: Indicates the contents of the keychain has changed. Sent when a KeyChain
225 * entry is added, modified or removed.
226 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600227 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chad Brubaker4de59ef2016-05-02 13:17:31 -0700228 public static final String ACTION_KEYCHAIN_CHANGED = "android.security.action.KEYCHAIN_CHANGED";
229
230 /**
231 * Broadcast Action: Indicates the contents of the trusted certificate store has changed. Sent
232 * when one the following occurs:
233 *
234 * <ul>
235 * <li>A pre-installed CA is disabled or re-enabled</li>
236 * <li>A CA is added or removed from the trust store</li>
237 * </ul>
238 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600239 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chad Brubaker4de59ef2016-05-02 13:17:31 -0700240 public static final String ACTION_TRUST_STORE_CHANGED =
241 "android.security.action.TRUST_STORE_CHANGED";
242
243 /**
244 * Broadcast Action: Indicates that the access permissions for a private key have changed.
245 *
246 */
Jeff Sharkey0f3f60b2017-04-24 18:06:20 -0600247 @SdkConstant(SdkConstantType.BROADCAST_INTENT_ACTION)
Chad Brubaker4de59ef2016-05-02 13:17:31 -0700248 public static final String ACTION_KEY_ACCESS_CHANGED =
249 "android.security.action.KEY_ACCESS_CHANGED";
250
251 /**
252 * Used as a String extra field in {@link #ACTION_KEY_ACCESS_CHANGED} to supply the alias of
253 * the key.
254 */
255 public static final String EXTRA_KEY_ALIAS = "android.security.extra.KEY_ALIAS";
256
257 /**
258 * Used as a boolean extra field in {@link #ACTION_KEY_ACCESS_CHANGED} to supply if the key is
259 * accessible to the application.
260 */
261 public static final String EXTRA_KEY_ACCESSIBLE = "android.security.extra.KEY_ACCESSIBLE";
262
263 /**
Eran Messeri61692392018-03-26 16:43:14 +0100264 * Indicates that a call to {@link #generateKeyPair} was successful.
265 * @hide
266 */
267 public static final int KEY_GEN_SUCCESS = 0;
268
269 /**
270 * An alias was missing from the key specifications when calling {@link #generateKeyPair}.
271 * @hide
272 */
273 public static final int KEY_GEN_MISSING_ALIAS = 1;
274
275 /**
276 * A key attestation challenge was provided to {@link #generateKeyPair}, but it shouldn't
277 * have been provided.
278 * @hide
279 */
280 public static final int KEY_GEN_SUPERFLUOUS_ATTESTATION_CHALLENGE = 2;
281
282 /**
283 * Algorithm not supported by {@link #generateKeyPair}
284 * @hide
285 */
286 public static final int KEY_GEN_NO_SUCH_ALGORITHM = 3;
287
288 /**
289 * Invalid algorithm parameters when calling {@link #generateKeyPair}
290 * @hide
291 */
292 public static final int KEY_GEN_INVALID_ALGORITHM_PARAMETERS = 4;
293
294 /**
295 * Keystore is not available when calling {@link #generateKeyPair}
296 * @hide
297 */
298 public static final int KEY_GEN_NO_KEYSTORE_PROVIDER = 5;
299
300 /**
Eran Messeri607a9952018-07-09 17:58:26 +0100301 * StrongBox unavailable when calling {@link #generateKeyPair}
302 * @hide
303 */
304 public static final int KEY_GEN_STRONGBOX_UNAVAILABLE = 6;
305
306 /**
Eran Messeri61692392018-03-26 16:43:14 +0100307 * General failure while calling {@link #generateKeyPair}
308 * @hide
309 */
Eran Messeri607a9952018-07-09 17:58:26 +0100310 public static final int KEY_GEN_FAILURE = 7;
Eran Messeri61692392018-03-26 16:43:14 +0100311
312 /**
313 * Successful call to {@link #attestKey}
314 * @hide
315 */
316 public static final int KEY_ATTESTATION_SUCCESS = 0;
317
318 /**
319 * Attestation challenge missing when calling {@link #attestKey}
320 * @hide
321 */
322 public static final int KEY_ATTESTATION_MISSING_CHALLENGE = 1;
323
324 /**
325 * The caller requested Device ID attestation when calling {@link #attestKey}, but has no
326 * permissions to get device identifiers.
327 * @hide
328 */
329 public static final int KEY_ATTESTATION_CANNOT_COLLECT_DATA = 2;
330
331 /**
332 * The underlying hardware does not support Device ID attestation or cannot attest to the
333 * identifiers that are stored on the device. This indicates permanent inability
334 * to get attestation records on the device.
335 * @hide
336 */
337 public static final int KEY_ATTESTATION_CANNOT_ATTEST_IDS = 3;
338
339 /**
340 * General failure when calling {@link #attestKey}
341 * @hide
342 */
343 public static final int KEY_ATTESTATION_FAILURE = 4;
344
345 /**
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700346 * Returns an {@code Intent} that can be used for credential
347 * installation. The intent may be used without any extras, in
348 * which case the user will be able to install credentials from
349 * their own source.
350 *
351 * <p>Alternatively, {@link #EXTRA_CERTIFICATE} or {@link
352 * #EXTRA_PKCS12} maybe used to specify the bytes of an X.509
353 * certificate or a PKCS#12 key store for installation. These
Brian Carlstromca43c452011-06-29 18:53:17 -0700354 * extras may be combined with {@link #EXTRA_NAME} to provide a
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700355 * default alias name for credentials being installed.
356 *
357 * <p>When used with {@link Activity#startActivityForResult},
358 * {@link Activity#RESULT_OK} will be returned if a credential was
359 * successfully installed, otherwise {@link
360 * Activity#RESULT_CANCELED} will be returned.
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700361 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700362 @NonNull
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700363 public static Intent createInstallIntent() {
364 Intent intent = new Intent(ACTION_INSTALL);
Kenny Root1a88d832014-02-07 09:12:48 -0800365 intent.setClassName(CERT_INSTALLER_PACKAGE,
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700366 "com.android.certinstaller.CertInstallerMain");
367 return intent;
368 }
369
370 /**
Brian Carlstromba1a6672011-05-24 21:54:37 -0700371 * Launches an {@code Activity} for the user to select the alias
372 * for a private key and certificate pair for authentication. The
373 * selected alias or null will be returned via the
Brian Carlstrom93201f52011-06-09 15:05:35 -0700374 * KeyChainAliasCallback callback.
375 *
Eran Messeri0bc50f92018-06-01 16:03:39 +0100376 * <p>A device policy controller (as a device or profile owner) can
377 * intercept the request before the activity is shown, to pick a
378 * specific private key alias by implementing
379 * {@link android.app.admin.DeviceAdminReceiver#onChoosePrivateKeyAlias
380 * onChoosePrivateKeyAlias}.
Robin Lee3798ed52015-02-03 17:55:31 +0000381 *
382 * <p>{@code keyTypes} and {@code issuers} may be used to
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100383 * narrow down suggested choices to the user. If either {@code keyTypes}
384 * or {@code issuers} is specified and non-empty, and there are no
385 * matching certificates in the KeyChain, then the certificate
386 * selection prompt would be suppressed entirely.
Robin Lee3798ed52015-02-03 17:55:31 +0000387 *
388 * <p>{@code host} and {@code port} may be used to give the user
389 * more context about the server requesting the credentials.
390 *
Eran Messeri0bc50f92018-06-01 16:03:39 +0100391 * <p>{@code alias} allows the caller to preselect an existing
Robin Lee3798ed52015-02-03 17:55:31 +0000392 * alias which will still be subject to user confirmation.
393 *
394 * @param activity The {@link Activity} context to use for
395 * launching the new sub-Activity to prompt the user to select
396 * a private key; used only to call startActivity(); must not
397 * be null.
398 * @param response Callback to invoke when the request completes;
Mike Harriscd0eb712018-04-26 15:20:10 -0700399 * must not be null.
Robin Lee3798ed52015-02-03 17:55:31 +0000400 * @param keyTypes The acceptable types of asymmetric keys such as
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100401 * "RSA", "EC" or null.
Robin Lee3798ed52015-02-03 17:55:31 +0000402 * @param issuers The acceptable certificate issuers for the
403 * certificate matching the private key, or null.
404 * @param host The host name of the server requesting the
405 * certificate, or null if unavailable.
406 * @param port The port number of the server requesting the
407 * certificate, or -1 if unavailable.
408 * @param alias The alias to preselect if available, or null if
409 * unavailable.
410 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700411 public static void choosePrivateKeyAlias(@NonNull Activity activity,
412 @NonNull KeyChainAliasCallback response,
Mike Harriscd0eb712018-04-26 15:20:10 -0700413 @Nullable @KeyProperties.KeyAlgorithmEnum String[] keyTypes,
414 @Nullable Principal[] issuers,
Alex Klyubin54bb1592015-05-11 12:30:03 -0700415 @Nullable String host, int port, @Nullable String alias) {
Robin Lee39087b12015-05-05 15:57:17 +0100416 Uri uri = null;
417 if (host != null) {
418 uri = new Uri.Builder()
419 .authority(host + (port != -1 ? ":" + port : ""))
420 .build();
421 }
422 choosePrivateKeyAlias(activity, response, keyTypes, issuers, uri, alias);
Robin Lee3798ed52015-02-03 17:55:31 +0000423 }
424
425 /**
426 * Launches an {@code Activity} for the user to select the alias
427 * for a private key and certificate pair for authentication. The
428 * selected alias or null will be returned via the
429 * KeyChainAliasCallback callback.
430 *
Eran Messeri0bc50f92018-06-01 16:03:39 +0100431 * <p>A device policy controller (as a device or profile owner) can
432 * intercept the request before the activity is shown, to pick a
433 * specific private key alias by implementing
434 * {@link android.app.admin.DeviceAdminReceiver#onChoosePrivateKeyAlias
435 * onChoosePrivateKeyAlias}.
Robin Lee3798ed52015-02-03 17:55:31 +0000436 *
Brian Carlstrom93201f52011-06-09 15:05:35 -0700437 * <p>{@code keyTypes} and {@code issuers} may be used to
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100438 * narrow down suggested choices to the user. If either {@code keyTypes}
439 * or {@code issuers} is specified and non-empty, and there are no
440 * matching certificates in the KeyChain, then the certificate
441 * selection prompt would be suppressed entirely.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700442 *
Eran Messeri0bc50f92018-06-01 16:03:39 +0100443 * <p>{@code uri} may be used to give the user more context about
444 * the server requesting the credentials.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700445 *
Eran Messeri0bc50f92018-06-01 16:03:39 +0100446 * <p>{@code alias} allows the caller to preselect an existing
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700447 * alias which will still be subject to user confirmation.
448 *
Brian Carlstrom93201f52011-06-09 15:05:35 -0700449 * @param activity The {@link Activity} context to use for
450 * launching the new sub-Activity to prompt the user to select
451 * a private key; used only to call startActivity(); must not
452 * be null.
453 * @param response Callback to invoke when the request completes;
Mike Harriscd0eb712018-04-26 15:20:10 -0700454 * must not be null.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700455 * @param keyTypes The acceptable types of asymmetric keys such as
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100456 * "RSA", "EC" or null.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700457 * @param issuers The acceptable certificate issuers for the
458 * certificate matching the private key, or null.
Robin Lee39087b12015-05-05 15:57:17 +0100459 * @param uri The full URI the server is requesting the certificate
Robin Lee3798ed52015-02-03 17:55:31 +0000460 * for, or null if unavailable.
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700461 * @param alias The alias to preselect if available, or null if
462 * unavailable.
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100463 * @throws IllegalArgumentException if the specified issuers are not
464 * of type {@code X500Principal}.
Brian Carlstromba1a6672011-05-24 21:54:37 -0700465 */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700466 public static void choosePrivateKeyAlias(@NonNull Activity activity,
467 @NonNull KeyChainAliasCallback response,
Mike Harriscd0eb712018-04-26 15:20:10 -0700468 @Nullable @KeyProperties.KeyAlgorithmEnum String[] keyTypes,
469 @Nullable Principal[] issuers,
Robin Lee39087b12015-05-05 15:57:17 +0100470 @Nullable Uri uri, @Nullable String alias) {
Brian Carlstrom93201f52011-06-09 15:05:35 -0700471 /*
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100472 * Specifying keyTypes excludes certificates with different key types
473 * from the list of certificates presented to the user.
474 * In practice today, most servers would require RSA or EC
475 * certificates.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700476 *
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100477 * Specifying issuers narrows down the list by filtering out
478 * certificates with issuers which are not matching the provided ones.
479 * This has been reported to Chrome several times (crbug.com/731769).
480 * There's no concrete description on what to do when the client has no
481 * certificates that match the provided issuers.
482 * To be conservative, Android will not present the user with any
483 * certificates to choose from.
484 * If the list of issuers is empty then the client may send any
485 * certificate, see:
486 * https://tools.ietf.org/html/rfc5246#section-7.4.4
Brian Carlstrom93201f52011-06-09 15:05:35 -0700487 */
Brian Carlstromba1a6672011-05-24 21:54:37 -0700488 if (activity == null) {
489 throw new NullPointerException("activity == null");
490 }
491 if (response == null) {
492 throw new NullPointerException("response == null");
493 }
Brian Carlstroma00a2b32011-06-29 10:42:35 -0700494 Intent intent = new Intent(ACTION_CHOOSER);
Kenny Roota3659062014-03-17 16:21:53 -0700495 intent.setPackage(KEYCHAIN_PACKAGE);
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700496 intent.putExtra(EXTRA_RESPONSE, new AliasResponse(response));
Robin Lee39087b12015-05-05 15:57:17 +0100497 intent.putExtra(EXTRA_URI, uri);
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700498 intent.putExtra(EXTRA_ALIAS, alias);
Eran Messeri9ccec4d2018-08-24 15:29:05 +0100499 intent.putExtra(EXTRA_KEY_TYPES, keyTypes);
500 ArrayList<byte[]> issuersList = new ArrayList();
501 if (issuers != null) {
502 for (Principal issuer: issuers) {
503 // In a TLS client context (like Chrome), issuers would only
504 // be specified as X500Principals. No other use cases for
505 // specifying principals have been brought up. Under these
506 // circumstances, only allow issuers specified as
507 // X500Principals.
508 if (!(issuer instanceof X500Principal)) {
509 throw new IllegalArgumentException(String.format(
510 "Issuer %s is of type %s, not X500Principal",
511 issuer.toString(), issuer.getClass()));
512 }
513 // Pass the DER-encoded issuer as that's the most accurate
514 // representation and what is sent over the wire.
515 issuersList.add(((X500Principal) issuer).getEncoded());
516 }
517 }
518 intent.putExtra(EXTRA_ISSUERS, (Serializable) issuersList);
Brian Carlstrom67c30df2011-06-24 02:13:23 -0700519 // the PendingIntent is used to get calling package name
520 intent.putExtra(EXTRA_SENDER, PendingIntent.getActivity(activity, 0, new Intent(), 0));
Brian Carlstromba1a6672011-05-24 21:54:37 -0700521 activity.startActivity(intent);
522 }
523
Brian Carlstrom93201f52011-06-09 15:05:35 -0700524 private static class AliasResponse extends IKeyChainAliasCallback.Stub {
Brian Carlstrom93201f52011-06-09 15:05:35 -0700525 private final KeyChainAliasCallback keyChainAliasResponse;
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700526 private AliasResponse(KeyChainAliasCallback keyChainAliasResponse) {
Brian Carlstromba1a6672011-05-24 21:54:37 -0700527 this.keyChainAliasResponse = keyChainAliasResponse;
528 }
529 @Override public void alias(String alias) {
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700530 keyChainAliasResponse.alias(alias);
Brian Carlstromba1a6672011-05-24 21:54:37 -0700531 }
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700532 }
533
534 /**
Eran Messeri55b8d082019-01-31 16:25:52 +0000535 * Returns the {@code PrivateKey} for the requested alias, or null if the alias does not exist
536 * or the caller has no permission to access it (see note on exceptions below).
Brian Carlstrom93201f52011-06-09 15:05:35 -0700537 *
Robin Lee59e3baa2015-06-30 10:48:06 -0700538 * <p> This method may block while waiting for a connection to another process, and must never
539 * be called from the main thread.
Robin Leeda236182016-08-12 12:46:28 +0100540 * <p> As {@link Activity} and {@link Service} contexts are short-lived and can be destroyed
541 * at any time from the main thread, it is safer to rely on a long-lived context such as one
542 * returned from {@link Context#getApplicationContext()}.
Robin Lee59e3baa2015-06-30 10:48:06 -0700543 *
Eran Messeri55b8d082019-01-31 16:25:52 +0000544 * <p> If the caller provides a valid alias to which it was not granted access, then the
545 * caller must invoke {@link #choosePrivateKeyAlias} again to get another valid alias
546 * or a grant to access the same alias.
547 * <p>On Android versions prior to Q, when a key associated with the specified alias is
548 * unavailable, the method will throw a {@code KeyChainException} rather than return null.
549 * If the exception's cause (as obtained by calling {@code KeyChainException.getCause()})
550 * is a throwable of type {@code IllegalStateException} then the caller lacks a grant
551 * to access the key and certificates associated with this alias.
552 *
Robin Lee59e3baa2015-06-30 10:48:06 -0700553 * @param alias The alias of the desired private key, typically returned via
554 * {@link KeyChainAliasCallback#alias}.
Brian Carlstrom93201f52011-06-09 15:05:35 -0700555 * @throws KeyChainException if the alias was valid but there was some problem accessing it.
Robin Lee59e3baa2015-06-30 10:48:06 -0700556 * @throws IllegalStateException if called from the main thread.
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700557 */
Robin Lee59e3baa2015-06-30 10:48:06 -0700558 @Nullable @WorkerThread
Alex Klyubin54bb1592015-05-11 12:30:03 -0700559 public static PrivateKey getPrivateKey(@NonNull Context context, @NonNull String alias)
Brian Carlstrom93201f52011-06-09 15:05:35 -0700560 throws KeyChainException, InterruptedException {
Eran Messeri23c438d2017-11-23 17:20:52 +0000561 KeyPair keyPair = getKeyPair(context, alias);
562 if (keyPair != null) {
563 return keyPair.getPrivate();
564 }
565
566 return null;
567 }
568
569 /** @hide */
570 @Nullable @WorkerThread
571 public static KeyPair getKeyPair(@NonNull Context context, @NonNull String alias)
572 throws KeyChainException, InterruptedException {
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700573 if (alias == null) {
574 throw new NullPointerException("alias == null");
575 }
Shawn Willdendea66142016-11-16 06:01:06 -0700576 if (context == null) {
577 throw new NullPointerException("context == null");
578 }
Robin Lee28d68b12016-07-22 16:32:32 +0100579
580 final String keyId;
581 try (KeyChainConnection keyChainConnection = bind(context.getApplicationContext())) {
582 keyId = keyChainConnection.getService().requestPrivateKey(alias);
Brian Carlstrom93201f52011-06-09 15:05:35 -0700583 } catch (RemoteException e) {
584 throw new KeyChainException(e);
585 } catch (RuntimeException e) {
586 // only certain RuntimeExceptions can be propagated across the IKeyChainService call
587 throw new KeyChainException(e);
Robin Lee28d68b12016-07-22 16:32:32 +0100588 }
589
590 if (keyId == null) {
591 return null;
592 } else {
593 try {
Eran Messeri23c438d2017-11-23 17:20:52 +0000594 return AndroidKeyStoreProvider.loadAndroidKeyStoreKeyPairFromKeystore(
Robin Lee28d68b12016-07-22 16:32:32 +0100595 KeyStore.getInstance(), keyId, KeyStore.UID_SELF);
Max Bires13f98ce2018-11-02 10:50:40 -0700596 } catch (RuntimeException | UnrecoverableKeyException | KeyPermanentlyInvalidatedException e) {
Robin Lee28d68b12016-07-22 16:32:32 +0100597 throw new KeyChainException(e);
598 }
Brian Carlstromba1a6672011-05-24 21:54:37 -0700599 }
600 }
601
602 /**
Eran Messeri55b8d082019-01-31 16:25:52 +0000603 * Returns the {@code X509Certificate} chain for the requested alias, or null if the alias
604 * does not exist or the caller has no permission to access it (see note on exceptions
605 * in {@link #getPrivateKey}).
606 *
Rubin Xub4365912016-03-23 12:13:22 +0000607 * <p>
608 * <strong>Note:</strong> If a certificate chain was explicitly specified when the alias was
609 * installed, this method will return that chain. If only the client certificate was specified
610 * at the installation time, this method will try to build a certificate chain using all
611 * available trust anchors (preinstalled and user-added).
Brian Carlstrom93201f52011-06-09 15:05:35 -0700612 *
Robin Lee59e3baa2015-06-30 10:48:06 -0700613 * <p> This method may block while waiting for a connection to another process, and must never
614 * be called from the main thread.
Robin Leeda236182016-08-12 12:46:28 +0100615 * <p> As {@link Activity} and {@link Service} contexts are short-lived and can be destroyed
616 * at any time from the main thread, it is safer to rely on a long-lived context such as one
617 * returned from {@link Context#getApplicationContext()}.
Eran Messeri55b8d082019-01-31 16:25:52 +0000618 * <p> In case the caller specifies an alias for which it lacks a grant, it must call
619 * {@link #choosePrivateKeyAlias} again. See {@link #getPrivateKey} for more details on
620 * coping with this scenario.
Robin Lee59e3baa2015-06-30 10:48:06 -0700621 *
Brian Carlstrom93201f52011-06-09 15:05:35 -0700622 * @param alias The alias of the desired certificate chain, typically
623 * returned via {@link KeyChainAliasCallback#alias}.
624 * @throws KeyChainException if the alias was valid but there was some problem accessing it.
Robin Lee59e3baa2015-06-30 10:48:06 -0700625 * @throws IllegalStateException if called from the main thread.
Brian Carlstromba1a6672011-05-24 21:54:37 -0700626 */
Robin Lee59e3baa2015-06-30 10:48:06 -0700627 @Nullable @WorkerThread
Alex Klyubin54bb1592015-05-11 12:30:03 -0700628 public static X509Certificate[] getCertificateChain(@NonNull Context context,
629 @NonNull String alias) throws KeyChainException, InterruptedException {
Brian Carlstromba1a6672011-05-24 21:54:37 -0700630 if (alias == null) {
631 throw new NullPointerException("alias == null");
632 }
Kenny Root0150e482013-02-13 15:22:50 -0800633
Robin Lee28d68b12016-07-22 16:32:32 +0100634 final byte[] certificateBytes;
635 final byte[] certChainBytes;
636 try (KeyChainConnection keyChainConnection = bind(context.getApplicationContext())) {
637 IKeyChainService keyChainService = keyChainConnection.getService();
638 certificateBytes = keyChainService.getCertificate(alias);
Kenny Root0150e482013-02-13 15:22:50 -0800639 if (certificateBytes == null) {
640 return null;
641 }
Robin Lee28d68b12016-07-22 16:32:32 +0100642 certChainBytes = keyChainService.getCaCertificates(alias);
643 } catch (RemoteException e) {
644 throw new KeyChainException(e);
645 } catch (RuntimeException e) {
646 // only certain RuntimeExceptions can be propagated across the IKeyChainService call
647 throw new KeyChainException(e);
648 }
649
650 try {
Rubin Xub4365912016-03-23 12:13:22 +0000651 X509Certificate leafCert = toCertificate(certificateBytes);
Rubin Xub4365912016-03-23 12:13:22 +0000652 // If the keypair is installed with a certificate chain by either
653 // DevicePolicyManager.installKeyPair or CertInstaller, return that chain.
654 if (certChainBytes != null && certChainBytes.length != 0) {
655 Collection<X509Certificate> chain = toCertificates(certChainBytes);
656 ArrayList<X509Certificate> fullChain = new ArrayList<>(chain.size() + 1);
657 fullChain.add(leafCert);
658 fullChain.addAll(chain);
659 return fullChain.toArray(new X509Certificate[fullChain.size()]);
660 } else {
661 // If there isn't a certificate chain, either due to a pre-existing keypair
662 // installed before N, or no chain is explicitly installed under the new logic,
663 // fall back to old behavior of constructing the chain from trusted credentials.
664 //
665 // This logic exists to maintain old behaviour for already installed keypair, at
666 // the cost of potentially returning extra certificate chain for new clients who
667 // explicitly installed only the client certificate without a chain. The latter
668 // case is actually no different from pre-N behaviour of getCertificateChain(),
669 // in that sense this change introduces no regression. Besides the returned chain
670 // is still valid so the consumer of the chain should have no problem verifying it.
671 TrustedCertificateStore store = new TrustedCertificateStore();
672 List<X509Certificate> chain = store.getCertificateChain(leafCert);
673 return chain.toArray(new X509Certificate[chain.size()]);
674 }
Robin Lee28d68b12016-07-22 16:32:32 +0100675 } catch (CertificateException | RuntimeException e) {
Kenny Rootcfba6a02013-05-06 15:00:58 -0700676 throw new KeyChainException(e);
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700677 }
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700678 }
679
Kenny Rootbf556ac2013-04-01 15:10:22 -0700680 /**
681 * Returns {@code true} if the current device's {@code KeyChain} supports a
682 * specific {@code PrivateKey} type indicated by {@code algorithm} (e.g.,
683 * "RSA").
684 */
Alex Klyubin4d5443f2015-05-06 15:43:52 -0700685 public static boolean isKeyAlgorithmSupported(
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700686 @NonNull @KeyProperties.KeyAlgorithmEnum String algorithm) {
Kenny Rootb91773b2013-09-05 13:03:16 -0700687 final String algUpper = algorithm.toUpperCase(Locale.US);
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700688 return KeyProperties.KEY_ALGORITHM_EC.equals(algUpper)
689 || KeyProperties.KEY_ALGORITHM_RSA.equals(algUpper);
Kenny Rootbf556ac2013-04-01 15:10:22 -0700690 }
691
692 /**
693 * Returns {@code true} if the current device's {@code KeyChain} binds any
694 * {@code PrivateKey} of the given {@code algorithm} to the device once
695 * imported or generated. This can be used to tell if there is special
696 * hardware support that can be used to bind keys to the device in a way
697 * that makes it non-exportable.
Alex Klyubin469cbf52015-06-04 12:36:27 -0700698 *
699 * @deprecated Whether the key is bound to the secure hardware is known only
700 * once the key has been imported. To find out, use:
701 * <pre>{@code
702 * PrivateKey key = ...; // private key from KeyChain
703 *
704 * KeyFactory keyFactory =
705 * KeyFactory.getInstance(key.getAlgorithm(), "AndroidKeyStore");
706 * KeyInfo keyInfo = keyFactory.getKeySpec(key, KeyInfo.class);
Neil Fuller71fbb812015-11-30 09:51:33 +0000707 * if (keyInfo.isInsideSecureHardware()) {
Alex Klyubin469cbf52015-06-04 12:36:27 -0700708 * // The key is bound to the secure hardware of this Android
Neil Fuller71fbb812015-11-30 09:51:33 +0000709 * }}</pre>
Kenny Rootbf556ac2013-04-01 15:10:22 -0700710 */
Alex Klyubin469cbf52015-06-04 12:36:27 -0700711 @Deprecated
Alex Klyubin4d5443f2015-05-06 15:43:52 -0700712 public static boolean isBoundKeyAlgorithm(
Alex Klyubin3f8d4d82015-05-13 09:15:00 -0700713 @NonNull @KeyProperties.KeyAlgorithmEnum String algorithm) {
Kenny Root5b7e90a2013-04-02 11:23:41 -0700714 if (!isKeyAlgorithmSupported(algorithm)) {
Kenny Rootbf556ac2013-04-01 15:10:22 -0700715 return false;
716 }
717
Kenny Rootb91773b2013-09-05 13:03:16 -0700718 return KeyStore.getInstance().isHardwareBacked(algorithm);
Kenny Rootbf556ac2013-04-01 15:10:22 -0700719 }
720
Zoltan Szatmary-Banf0ae1352014-08-18 10:48:33 +0100721 /** @hide */
Alex Klyubin54bb1592015-05-11 12:30:03 -0700722 @NonNull
723 public static X509Certificate toCertificate(@NonNull byte[] bytes) {
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700724 if (bytes == null) {
Brian Carlstrom8e9929c2011-05-17 00:40:28 -0700725 throw new IllegalArgumentException("bytes == null");
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700726 }
727 try {
728 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
729 Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes));
730 return (X509Certificate) cert;
731 } catch (CertificateException e) {
732 throw new AssertionError(e);
733 }
734 }
735
Rubin Xub4365912016-03-23 12:13:22 +0000736 /** @hide */
737 @NonNull
738 public static Collection<X509Certificate> toCertificates(@NonNull byte[] bytes) {
739 if (bytes == null) {
740 throw new IllegalArgumentException("bytes == null");
741 }
742 try {
743 CertificateFactory certFactory = CertificateFactory.getInstance("X.509");
744 return (Collection<X509Certificate>) certFactory.generateCertificates(
745 new ByteArrayInputStream(bytes));
746 } catch (CertificateException e) {
747 throw new AssertionError(e);
748 }
749 }
750
Brian Carlstromd7524722011-05-17 16:20:36 -0700751 /**
752 * @hide for reuse by CertInstaller and Settings.
753 * @see KeyChain#bind
754 */
Robin Lee7f5c91c2017-02-08 21:27:02 +0000755 public static class KeyChainConnection implements Closeable {
Brian Carlstromd7524722011-05-17 16:20:36 -0700756 private final Context context;
757 private final ServiceConnection serviceConnection;
758 private final IKeyChainService service;
phweisse375fc42017-04-19 20:15:06 +0200759 protected KeyChainConnection(Context context,
760 ServiceConnection serviceConnection,
761 IKeyChainService service) {
Brian Carlstromd7524722011-05-17 16:20:36 -0700762 this.context = context;
763 this.serviceConnection = serviceConnection;
764 this.service = service;
765 }
766 @Override public void close() {
767 context.unbindService(serviceConnection);
768 }
769 public IKeyChainService getService() {
770 return service;
771 }
772 }
773
774 /**
775 * @hide for reuse by CertInstaller and Settings.
776 *
777 * Caller should call unbindService on the result when finished.
778 */
Robin Lee59e3baa2015-06-30 10:48:06 -0700779 @WorkerThread
Alex Klyubin54bb1592015-05-11 12:30:03 -0700780 public static KeyChainConnection bind(@NonNull Context context) throws InterruptedException {
Robin Lee306fe082014-06-19 14:04:24 +0000781 return bindAsUser(context, Process.myUserHandle());
782 }
783
784 /**
785 * @hide
786 */
Robin Lee59e3baa2015-06-30 10:48:06 -0700787 @WorkerThread
Alex Klyubin54bb1592015-05-11 12:30:03 -0700788 public static KeyChainConnection bindAsUser(@NonNull Context context, UserHandle user)
Robin Lee306fe082014-06-19 14:04:24 +0000789 throws InterruptedException {
Brian Carlstromd7524722011-05-17 16:20:36 -0700790 if (context == null) {
791 throw new NullPointerException("context == null");
792 }
793 ensureNotOnMainThread(context);
794 final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1);
795 ServiceConnection keyChainServiceConnection = new ServiceConnection() {
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700796 volatile boolean mConnectedAtLeastOnce = false;
Brian Carlstromd7524722011-05-17 16:20:36 -0700797 @Override public void onServiceConnected(ComponentName name, IBinder service) {
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700798 if (!mConnectedAtLeastOnce) {
799 mConnectedAtLeastOnce = true;
800 try {
Jeff Sharkey0a17db12016-11-04 11:23:46 -0600801 q.put(IKeyChainService.Stub.asInterface(Binder.allowBlocking(service)));
Fred Quintanaab8b84a2011-07-13 14:55:39 -0700802 } catch (InterruptedException e) {
803 // will never happen, since the queue starts with one available slot
804 }
Brian Carlstromd7524722011-05-17 16:20:36 -0700805 }
806 }
807 @Override public void onServiceDisconnected(ComponentName name) {}
808 };
Maggie Benthallda51e682013-08-08 22:35:44 -0400809 Intent intent = new Intent(IKeyChainService.class.getName());
810 ComponentName comp = intent.resolveSystemService(context.getPackageManager(), 0);
811 intent.setComponent(comp);
Robin Lee21bcbc52016-02-29 18:55:35 +0000812 if (comp == null || !context.bindServiceAsUser(
813 intent, keyChainServiceConnection, Context.BIND_AUTO_CREATE, user)) {
Brian Carlstromd7524722011-05-17 16:20:36 -0700814 throw new AssertionError("could not bind to KeyChainService");
815 }
816 return new KeyChainConnection(context, keyChainServiceConnection, q.take());
817 }
818
Alex Klyubin54bb1592015-05-11 12:30:03 -0700819 private static void ensureNotOnMainThread(@NonNull Context context) {
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700820 Looper looper = Looper.myLooper();
Brian Carlstrom8e9929c2011-05-17 00:40:28 -0700821 if (looper != null && looper == context.getMainLooper()) {
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700822 throw new IllegalStateException(
823 "calling this from your main thread can lead to deadlock");
824 }
825 }
Brian Carlstromb9a07c12011-04-11 09:03:51 -0700826}