| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 1 | /* |
| 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 | */ |
| 16 | package android.security; |
| 17 | |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 18 | import android.annotation.NonNull; |
| 19 | import android.annotation.Nullable; |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 20 | import android.app.Activity; |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 21 | import android.app.PendingIntent; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 22 | import android.content.ComponentName; |
| 23 | import android.content.Context; |
| 24 | import android.content.Intent; |
| 25 | import android.content.ServiceConnection; |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 26 | import android.net.Uri; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 27 | import android.os.IBinder; |
| 28 | import android.os.Looper; |
| Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 29 | import android.os.Process; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 30 | import android.os.RemoteException; |
| Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 31 | import android.os.UserHandle; |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 32 | import android.security.keystore.KeyProperties; |
| 33 | |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 34 | import java.io.ByteArrayInputStream; |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 35 | import java.io.Closeable; |
| Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 36 | import java.security.InvalidKeyException; |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 37 | import java.security.Principal; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 38 | import java.security.PrivateKey; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 39 | import java.security.cert.Certificate; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 40 | import java.security.cert.CertificateException; |
| 41 | import java.security.cert.CertificateFactory; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 42 | import java.security.cert.X509Certificate; |
| Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 43 | import java.util.List; |
| Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 44 | import java.util.Locale; |
| Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 45 | import java.util.concurrent.BlockingQueue; |
| 46 | import java.util.concurrent.LinkedBlockingQueue; |
| Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 47 | |
| Kenny Root | 12e7522 | 2013-04-23 22:34:24 -0700 | [diff] [blame] | 48 | import com.android.org.conscrypt.OpenSSLEngine; |
| 49 | import com.android.org.conscrypt.TrustedCertificateStore; |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 50 | |
| 51 | /** |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 52 | * The {@code KeyChain} class provides access to private keys and |
| 53 | * their corresponding certificate chains in credential storage. |
| 54 | * |
| 55 | * <p>Applications accessing the {@code KeyChain} normally go through |
| 56 | * these steps: |
| 57 | * |
| 58 | * <ol> |
| 59 | * |
| 60 | * <li>Receive a callback from an {@link javax.net.ssl.X509KeyManager |
| 61 | * X509KeyManager} that a private key is requested. |
| 62 | * |
| 63 | * <li>Call {@link #choosePrivateKeyAlias |
| 64 | * choosePrivateKeyAlias} to allow the user to select from a |
| 65 | * list of currently available private keys and corresponding |
| 66 | * certificate chains. The chosen alias will be returned by the |
| 67 | * callback {@link KeyChainAliasCallback#alias}, or null if no private |
| 68 | * key is available or the user cancels the request. |
| 69 | * |
| 70 | * <li>Call {@link #getPrivateKey} and {@link #getCertificateChain} to |
| 71 | * retrieve the credentials to return to the corresponding {@link |
| 72 | * javax.net.ssl.X509KeyManager} callbacks. |
| 73 | * |
| 74 | * </ol> |
| 75 | * |
| 76 | * <p>An application may remember the value of a selected alias to |
| 77 | * avoid prompting the user with {@link #choosePrivateKeyAlias |
| 78 | * choosePrivateKeyAlias} on subsequent connections. If the alias is |
| 79 | * no longer valid, null will be returned on lookups using that value |
| Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 80 | * |
| 81 | * <p>An application can request the installation of private keys and |
| 82 | * certificates via the {@code Intent} provided by {@link |
| 83 | * #createInstallIntent}. Private keys installed via this {@code |
| 84 | * Intent} will be accessible via {@link #choosePrivateKeyAlias} while |
| 85 | * Certificate Authority (CA) certificates will be trusted by all |
| 86 | * applications through the default {@code X509TrustManager}. |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 87 | */ |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 88 | // TODO reference intent for credential installation when public |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 89 | public final class KeyChain { |
| 90 | |
| 91 | private static final String TAG = "KeyChain"; |
| 92 | |
| 93 | /** |
| 94 | * @hide Also used by KeyChainService implementation |
| 95 | */ |
| 96 | public static final String ACCOUNT_TYPE = "com.android.keychain"; |
| 97 | |
| 98 | /** |
| Kenny Root | a365906 | 2014-03-17 16:21:53 -0700 | [diff] [blame] | 99 | * Package name for KeyChain chooser. |
| 100 | */ |
| 101 | private static final String KEYCHAIN_PACKAGE = "com.android.keychain"; |
| 102 | |
| 103 | /** |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 104 | * Action to bring up the KeyChainActivity |
| 105 | */ |
| 106 | private static final String ACTION_CHOOSER = "com.android.keychain.CHOOSER"; |
| 107 | |
| 108 | /** |
| Kenny Root | 1a88d83 | 2014-02-07 09:12:48 -0800 | [diff] [blame] | 109 | * Package name for the Certificate Installer. |
| 110 | */ |
| 111 | private static final String CERT_INSTALLER_PACKAGE = "com.android.certinstaller"; |
| 112 | |
| 113 | /** |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 114 | * Extra for use with {@link #ACTION_CHOOSER} |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 115 | * @hide Also used by KeyChainActivity implementation |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 116 | */ |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 117 | public static final String EXTRA_RESPONSE = "response"; |
| 118 | |
| 119 | /** |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 120 | * Extra for use with {@link #ACTION_CHOOSER} |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 121 | * @hide Also used by KeyChainActivity implementation |
| 122 | */ |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 123 | public static final String EXTRA_URI = "uri"; |
| Robin Lee | 3798ed5 | 2015-02-03 17:55:31 +0000 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * Extra for use with {@link #ACTION_CHOOSER} |
| 127 | * @hide Also used by KeyChainActivity implementation |
| 128 | */ |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 129 | public static final String EXTRA_ALIAS = "alias"; |
| 130 | |
| 131 | /** |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 132 | * Extra for use with {@link #ACTION_CHOOSER} |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 133 | * @hide Also used by KeyChainActivity implementation |
| 134 | */ |
| 135 | public static final String EXTRA_SENDER = "sender"; |
| 136 | |
| 137 | /** |
| Selim Gurun | 93ba4fe | 2012-02-14 11:48:49 -0800 | [diff] [blame] | 138 | * Action to bring up the CertInstaller. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 139 | */ |
| 140 | private static final String ACTION_INSTALL = "android.credentials.INSTALL"; |
| 141 | |
| 142 | /** |
| 143 | * Optional extra to specify a {@code String} credential name on |
| 144 | * the {@code Intent} returned by {@link #createInstallIntent}. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 145 | */ |
| 146 | // Compatible with old com.android.certinstaller.CredentialHelper.CERT_NAME_KEY |
| 147 | public static final String EXTRA_NAME = "name"; |
| 148 | |
| 149 | /** |
| 150 | * Optional extra to specify an X.509 certificate to install on |
| 151 | * the {@code Intent} returned by {@link #createInstallIntent}. |
| 152 | * The extra value should be a PEM or ASN.1 DER encoded {@code |
| 153 | * byte[]}. An {@link X509Certificate} can be converted to DER |
| 154 | * encoded bytes with {@link X509Certificate#getEncoded}. |
| 155 | * |
| 156 | * <p>{@link #EXTRA_NAME} may be used to provide a default alias |
| 157 | * name for the installed certificate. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 158 | */ |
| 159 | // Compatible with old android.security.Credentials.CERTIFICATE |
| 160 | public static final String EXTRA_CERTIFICATE = "CERT"; |
| 161 | |
| 162 | /** |
| 163 | * Optional extra for use with the {@code Intent} returned by |
| 164 | * {@link #createInstallIntent} to specify a PKCS#12 key store to |
| 165 | * install. The extra value should be a {@code byte[]}. The bytes |
| 166 | * may come from an external source or be generated with {@link |
| Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 167 | * java.security.KeyStore#store} on a "PKCS12" instance. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 168 | * |
| 169 | * <p>The user will be prompted for the password to load the key store. |
| 170 | * |
| 171 | * <p>The key store will be scanned for {@link |
| 172 | * java.security.KeyStore.PrivateKeyEntry} entries and both the |
| 173 | * private key and associated certificate chain will be installed. |
| 174 | * |
| 175 | * <p>{@link #EXTRA_NAME} may be used to provide a default alias |
| 176 | * name for the installed credentials. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 177 | */ |
| 178 | // Compatible with old android.security.Credentials.PKCS12 |
| 179 | public static final String EXTRA_PKCS12 = "PKCS12"; |
| 180 | |
| Selim Gurun | 93ba4fe | 2012-02-14 11:48:49 -0800 | [diff] [blame] | 181 | |
| 182 | /** |
| Selim Gurun | 93ba4fe | 2012-02-14 11:48:49 -0800 | [diff] [blame] | 183 | * Broadcast Action: Indicates the trusted storage has changed. Sent when |
| 184 | * one of this happens: |
| 185 | * |
| 186 | * <ul> |
| 187 | * <li>a new CA is added, |
| 188 | * <li>an existing CA is removed or disabled, |
| 189 | * <li>a disabled CA is enabled, |
| 190 | * <li>trusted storage is reset (all user certs are cleared), |
| 191 | * <li>when permission to access a private key is changed. |
| 192 | * </ul> |
| 193 | */ |
| 194 | public static final String ACTION_STORAGE_CHANGED = "android.security.STORAGE_CHANGED"; |
| 195 | |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 196 | /** |
| 197 | * Returns an {@code Intent} that can be used for credential |
| 198 | * installation. The intent may be used without any extras, in |
| 199 | * which case the user will be able to install credentials from |
| 200 | * their own source. |
| 201 | * |
| 202 | * <p>Alternatively, {@link #EXTRA_CERTIFICATE} or {@link |
| 203 | * #EXTRA_PKCS12} maybe used to specify the bytes of an X.509 |
| 204 | * certificate or a PKCS#12 key store for installation. These |
| Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 205 | * extras may be combined with {@link #EXTRA_NAME} to provide a |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 206 | * default alias name for credentials being installed. |
| 207 | * |
| 208 | * <p>When used with {@link Activity#startActivityForResult}, |
| 209 | * {@link Activity#RESULT_OK} will be returned if a credential was |
| 210 | * successfully installed, otherwise {@link |
| 211 | * Activity#RESULT_CANCELED} will be returned. |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 212 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 213 | @NonNull |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 214 | public static Intent createInstallIntent() { |
| 215 | Intent intent = new Intent(ACTION_INSTALL); |
| Kenny Root | 1a88d83 | 2014-02-07 09:12:48 -0800 | [diff] [blame] | 216 | intent.setClassName(CERT_INSTALLER_PACKAGE, |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 217 | "com.android.certinstaller.CertInstallerMain"); |
| 218 | return intent; |
| 219 | } |
| 220 | |
| 221 | /** |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 222 | * Launches an {@code Activity} for the user to select the alias |
| 223 | * for a private key and certificate pair for authentication. The |
| 224 | * selected alias or null will be returned via the |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 225 | * KeyChainAliasCallback callback. |
| 226 | * |
| Robin Lee | 3798ed5 | 2015-02-03 17:55:31 +0000 | [diff] [blame] | 227 | * <p>The device or profile owner can intercept this before the activity |
| 228 | * is shown, to pick a specific private key alias. |
| 229 | * |
| 230 | * <p>{@code keyTypes} and {@code issuers} may be used to |
| 231 | * highlight suggested choices to the user, although to cope with |
| 232 | * sometimes erroneous values provided by servers, the user may be |
| 233 | * able to override these suggestions. |
| 234 | * |
| 235 | * <p>{@code host} and {@code port} may be used to give the user |
| 236 | * more context about the server requesting the credentials. |
| 237 | * |
| 238 | * <p>{@code alias} allows the chooser to preselect an existing |
| 239 | * alias which will still be subject to user confirmation. |
| 240 | * |
| 241 | * @param activity The {@link Activity} context to use for |
| 242 | * launching the new sub-Activity to prompt the user to select |
| 243 | * a private key; used only to call startActivity(); must not |
| 244 | * be null. |
| 245 | * @param response Callback to invoke when the request completes; |
| 246 | * must not be null |
| 247 | * @param keyTypes The acceptable types of asymmetric keys such as |
| 248 | * "RSA" or "DSA", or a null array. |
| 249 | * @param issuers The acceptable certificate issuers for the |
| 250 | * certificate matching the private key, or null. |
| 251 | * @param host The host name of the server requesting the |
| 252 | * certificate, or null if unavailable. |
| 253 | * @param port The port number of the server requesting the |
| 254 | * certificate, or -1 if unavailable. |
| 255 | * @param alias The alias to preselect if available, or null if |
| 256 | * unavailable. |
| 257 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 258 | public static void choosePrivateKeyAlias(@NonNull Activity activity, |
| 259 | @NonNull KeyChainAliasCallback response, |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 260 | @KeyProperties.KeyAlgorithmEnum String[] keyTypes, Principal[] issuers, |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 261 | @Nullable String host, int port, @Nullable String alias) { |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 262 | Uri uri = null; |
| 263 | if (host != null) { |
| 264 | uri = new Uri.Builder() |
| 265 | .authority(host + (port != -1 ? ":" + port : "")) |
| 266 | .build(); |
| 267 | } |
| 268 | choosePrivateKeyAlias(activity, response, keyTypes, issuers, uri, alias); |
| Robin Lee | 3798ed5 | 2015-02-03 17:55:31 +0000 | [diff] [blame] | 269 | } |
| 270 | |
| 271 | /** |
| 272 | * Launches an {@code Activity} for the user to select the alias |
| 273 | * for a private key and certificate pair for authentication. The |
| 274 | * selected alias or null will be returned via the |
| 275 | * KeyChainAliasCallback callback. |
| 276 | * |
| 277 | * <p>The device or profile owner can intercept this before the activity |
| 278 | * is shown, to pick a specific private key alias.</p> |
| 279 | * |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 280 | * <p>{@code keyTypes} and {@code issuers} may be used to |
| 281 | * highlight suggested choices to the user, although to cope with |
| 282 | * sometimes erroneous values provided by servers, the user may be |
| 283 | * able to override these suggestions. |
| 284 | * |
| 285 | * <p>{@code host} and {@code port} may be used to give the user |
| 286 | * more context about the server requesting the credentials. |
| 287 | * |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 288 | * <p>{@code alias} allows the chooser to preselect an existing |
| 289 | * alias which will still be subject to user confirmation. |
| 290 | * |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 291 | * @param activity The {@link Activity} context to use for |
| 292 | * launching the new sub-Activity to prompt the user to select |
| 293 | * a private key; used only to call startActivity(); must not |
| 294 | * be null. |
| 295 | * @param response Callback to invoke when the request completes; |
| 296 | * must not be null |
| 297 | * @param keyTypes The acceptable types of asymmetric keys such as |
| Alex Klyubin | cd2329d | 2015-01-14 16:45:51 -0800 | [diff] [blame] | 298 | * "EC" or "RSA", or a null array. |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 299 | * @param issuers The acceptable certificate issuers for the |
| 300 | * certificate matching the private key, or null. |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 301 | * @param uri The full URI the server is requesting the certificate |
| Robin Lee | 3798ed5 | 2015-02-03 17:55:31 +0000 | [diff] [blame] | 302 | * for, or null if unavailable. |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 303 | * @param alias The alias to preselect if available, or null if |
| 304 | * unavailable. |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 305 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 306 | public static void choosePrivateKeyAlias(@NonNull Activity activity, |
| 307 | @NonNull KeyChainAliasCallback response, |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 308 | @KeyProperties.KeyAlgorithmEnum String[] keyTypes, Principal[] issuers, |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 309 | @Nullable Uri uri, @Nullable String alias) { |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 310 | /* |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 311 | * TODO currently keyTypes, issuers are unused. They are meant |
| 312 | * to follow the semantics and purpose of X509KeyManager |
| 313 | * method arguments. |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 314 | * |
| 315 | * keyTypes would allow the list to be filtered and typically |
| 316 | * will be set correctly by the server. In practice today, |
| Alex Klyubin | cd2329d | 2015-01-14 16:45:51 -0800 | [diff] [blame] | 317 | * most all users will want only RSA or EC, and usually |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 318 | * only a small number of certs will be available. |
| 319 | * |
| 320 | * issuers is typically not useful. Some servers historically |
| 321 | * will send the entire list of public CAs known to the |
| 322 | * server. Others will send none. If this is used, if there |
| 323 | * are no matches after applying the constraint, it should be |
| 324 | * ignored. |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 325 | */ |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 326 | if (activity == null) { |
| 327 | throw new NullPointerException("activity == null"); |
| 328 | } |
| 329 | if (response == null) { |
| 330 | throw new NullPointerException("response == null"); |
| 331 | } |
| Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 332 | Intent intent = new Intent(ACTION_CHOOSER); |
| Kenny Root | a365906 | 2014-03-17 16:21:53 -0700 | [diff] [blame] | 333 | intent.setPackage(KEYCHAIN_PACKAGE); |
| Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 334 | intent.putExtra(EXTRA_RESPONSE, new AliasResponse(response)); |
| Robin Lee | 39087b1 | 2015-05-05 15:57:17 +0100 | [diff] [blame^] | 335 | intent.putExtra(EXTRA_URI, uri); |
| Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 336 | intent.putExtra(EXTRA_ALIAS, alias); |
| 337 | // the PendingIntent is used to get calling package name |
| 338 | intent.putExtra(EXTRA_SENDER, PendingIntent.getActivity(activity, 0, new Intent(), 0)); |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 339 | activity.startActivity(intent); |
| 340 | } |
| 341 | |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 342 | private static class AliasResponse extends IKeyChainAliasCallback.Stub { |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 343 | private final KeyChainAliasCallback keyChainAliasResponse; |
| Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 344 | private AliasResponse(KeyChainAliasCallback keyChainAliasResponse) { |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 345 | this.keyChainAliasResponse = keyChainAliasResponse; |
| 346 | } |
| 347 | @Override public void alias(String alias) { |
| Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 348 | keyChainAliasResponse.alias(alias); |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 349 | } |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | /** |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 353 | * Returns the {@code PrivateKey} for the requested alias, or null |
| 354 | * if no there is no result. |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 355 | * |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 356 | * @param alias The alias of the desired private key, typically |
| 357 | * returned via {@link KeyChainAliasCallback#alias}. |
| 358 | * @throws KeyChainException if the alias was valid but there was some problem accessing it. |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 359 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 360 | @Nullable |
| 361 | public static PrivateKey getPrivateKey(@NonNull Context context, @NonNull String alias) |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 362 | throws KeyChainException, InterruptedException { |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 363 | if (alias == null) { |
| 364 | throw new NullPointerException("alias == null"); |
| 365 | } |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 366 | KeyChainConnection keyChainConnection = bind(context); |
| Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 367 | try { |
| Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 368 | final IKeyChainService keyChainService = keyChainConnection.getService(); |
| 369 | final String keyId = keyChainService.requestPrivateKey(alias); |
| 370 | if (keyId == null) { |
| 371 | throw new KeyChainException("keystore had a problem"); |
| 372 | } |
| 373 | |
| 374 | final OpenSSLEngine engine = OpenSSLEngine.getInstance("keystore"); |
| 375 | return engine.getPrivateKeyById(keyId); |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 376 | } catch (RemoteException e) { |
| 377 | throw new KeyChainException(e); |
| 378 | } catch (RuntimeException e) { |
| 379 | // only certain RuntimeExceptions can be propagated across the IKeyChainService call |
| 380 | throw new KeyChainException(e); |
| Kenny Root | 5423e68 | 2011-11-14 08:43:13 -0800 | [diff] [blame] | 381 | } catch (InvalidKeyException e) { |
| 382 | throw new KeyChainException(e); |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 383 | } finally { |
| 384 | keyChainConnection.close(); |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | /** |
| 389 | * Returns the {@code X509Certificate} chain for the requested |
| 390 | * alias, or null if no there is no result. |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 391 | * |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 392 | * @param alias The alias of the desired certificate chain, typically |
| 393 | * returned via {@link KeyChainAliasCallback#alias}. |
| 394 | * @throws KeyChainException if the alias was valid but there was some problem accessing it. |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 395 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 396 | @Nullable |
| 397 | public static X509Certificate[] getCertificateChain(@NonNull Context context, |
| 398 | @NonNull String alias) throws KeyChainException, InterruptedException { |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 399 | if (alias == null) { |
| 400 | throw new NullPointerException("alias == null"); |
| 401 | } |
| 402 | KeyChainConnection keyChainConnection = bind(context); |
| 403 | try { |
| Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 404 | IKeyChainService keyChainService = keyChainConnection.getService(); |
| Kenny Root | 0150e48 | 2013-02-13 15:22:50 -0800 | [diff] [blame] | 405 | |
| 406 | final byte[] certificateBytes = keyChainService.getCertificate(alias); |
| 407 | if (certificateBytes == null) { |
| 408 | return null; |
| 409 | } |
| 410 | |
| Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 411 | TrustedCertificateStore store = new TrustedCertificateStore(); |
| Kenny Root | 54e03af | 2012-08-07 10:04:26 -0700 | [diff] [blame] | 412 | List<X509Certificate> chain = store |
| 413 | .getCertificateChain(toCertificate(certificateBytes)); |
| Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 414 | return chain.toArray(new X509Certificate[chain.size()]); |
| Kenny Root | cfba6a0 | 2013-05-06 15:00:58 -0700 | [diff] [blame] | 415 | } catch (CertificateException e) { |
| 416 | throw new KeyChainException(e); |
| Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 417 | } catch (RemoteException e) { |
| 418 | throw new KeyChainException(e); |
| 419 | } catch (RuntimeException e) { |
| 420 | // only certain RuntimeExceptions can be propagated across the IKeyChainService call |
| 421 | throw new KeyChainException(e); |
| Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 422 | } finally { |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 423 | keyChainConnection.close(); |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 424 | } |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 425 | } |
| 426 | |
| Kenny Root | bf556ac | 2013-04-01 15:10:22 -0700 | [diff] [blame] | 427 | /** |
| 428 | * Returns {@code true} if the current device's {@code KeyChain} supports a |
| 429 | * specific {@code PrivateKey} type indicated by {@code algorithm} (e.g., |
| 430 | * "RSA"). |
| 431 | */ |
| Alex Klyubin | 4d5443f | 2015-05-06 15:43:52 -0700 | [diff] [blame] | 432 | public static boolean isKeyAlgorithmSupported( |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 433 | @NonNull @KeyProperties.KeyAlgorithmEnum String algorithm) { |
| Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 434 | final String algUpper = algorithm.toUpperCase(Locale.US); |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 435 | return KeyProperties.KEY_ALGORITHM_EC.equals(algUpper) |
| 436 | || KeyProperties.KEY_ALGORITHM_RSA.equals(algUpper); |
| Kenny Root | bf556ac | 2013-04-01 15:10:22 -0700 | [diff] [blame] | 437 | } |
| 438 | |
| 439 | /** |
| 440 | * Returns {@code true} if the current device's {@code KeyChain} binds any |
| 441 | * {@code PrivateKey} of the given {@code algorithm} to the device once |
| 442 | * imported or generated. This can be used to tell if there is special |
| 443 | * hardware support that can be used to bind keys to the device in a way |
| 444 | * that makes it non-exportable. |
| 445 | */ |
| Alex Klyubin | 4d5443f | 2015-05-06 15:43:52 -0700 | [diff] [blame] | 446 | public static boolean isBoundKeyAlgorithm( |
| Alex Klyubin | 3f8d4d8 | 2015-05-13 09:15:00 -0700 | [diff] [blame] | 447 | @NonNull @KeyProperties.KeyAlgorithmEnum String algorithm) { |
| Kenny Root | 5b7e90a | 2013-04-02 11:23:41 -0700 | [diff] [blame] | 448 | if (!isKeyAlgorithmSupported(algorithm)) { |
| Kenny Root | bf556ac | 2013-04-01 15:10:22 -0700 | [diff] [blame] | 449 | return false; |
| 450 | } |
| 451 | |
| Kenny Root | b91773b | 2013-09-05 13:03:16 -0700 | [diff] [blame] | 452 | return KeyStore.getInstance().isHardwareBacked(algorithm); |
| Kenny Root | bf556ac | 2013-04-01 15:10:22 -0700 | [diff] [blame] | 453 | } |
| 454 | |
| Zoltan Szatmary-Ban | f0ae135 | 2014-08-18 10:48:33 +0100 | [diff] [blame] | 455 | /** @hide */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 456 | @NonNull |
| 457 | public static X509Certificate toCertificate(@NonNull byte[] bytes) { |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 458 | if (bytes == null) { |
| Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 459 | throw new IllegalArgumentException("bytes == null"); |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 460 | } |
| 461 | try { |
| 462 | CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 463 | Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes)); |
| 464 | return (X509Certificate) cert; |
| 465 | } catch (CertificateException e) { |
| 466 | throw new AssertionError(e); |
| 467 | } |
| 468 | } |
| 469 | |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 470 | /** |
| 471 | * @hide for reuse by CertInstaller and Settings. |
| 472 | * @see KeyChain#bind |
| 473 | */ |
| 474 | public final static class KeyChainConnection implements Closeable { |
| 475 | private final Context context; |
| 476 | private final ServiceConnection serviceConnection; |
| 477 | private final IKeyChainService service; |
| 478 | private KeyChainConnection(Context context, |
| 479 | ServiceConnection serviceConnection, |
| 480 | IKeyChainService service) { |
| 481 | this.context = context; |
| 482 | this.serviceConnection = serviceConnection; |
| 483 | this.service = service; |
| 484 | } |
| 485 | @Override public void close() { |
| 486 | context.unbindService(serviceConnection); |
| 487 | } |
| 488 | public IKeyChainService getService() { |
| 489 | return service; |
| 490 | } |
| 491 | } |
| 492 | |
| 493 | /** |
| 494 | * @hide for reuse by CertInstaller and Settings. |
| 495 | * |
| 496 | * Caller should call unbindService on the result when finished. |
| 497 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 498 | public static KeyChainConnection bind(@NonNull Context context) throws InterruptedException { |
| Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 499 | return bindAsUser(context, Process.myUserHandle()); |
| 500 | } |
| 501 | |
| 502 | /** |
| 503 | * @hide |
| 504 | */ |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 505 | public static KeyChainConnection bindAsUser(@NonNull Context context, UserHandle user) |
| Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 506 | throws InterruptedException { |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 507 | if (context == null) { |
| 508 | throw new NullPointerException("context == null"); |
| 509 | } |
| 510 | ensureNotOnMainThread(context); |
| 511 | final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1); |
| 512 | ServiceConnection keyChainServiceConnection = new ServiceConnection() { |
| Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 513 | volatile boolean mConnectedAtLeastOnce = false; |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 514 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
| Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 515 | if (!mConnectedAtLeastOnce) { |
| 516 | mConnectedAtLeastOnce = true; |
| 517 | try { |
| 518 | q.put(IKeyChainService.Stub.asInterface(service)); |
| 519 | } catch (InterruptedException e) { |
| 520 | // will never happen, since the queue starts with one available slot |
| 521 | } |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 522 | } |
| 523 | } |
| 524 | @Override public void onServiceDisconnected(ComponentName name) {} |
| 525 | }; |
| Maggie Benthall | da51e68 | 2013-08-08 22:35:44 -0400 | [diff] [blame] | 526 | Intent intent = new Intent(IKeyChainService.class.getName()); |
| 527 | ComponentName comp = intent.resolveSystemService(context.getPackageManager(), 0); |
| 528 | intent.setComponent(comp); |
| Robin Lee | 306fe08 | 2014-06-19 14:04:24 +0000 | [diff] [blame] | 529 | boolean isBound = context.bindServiceAsUser(intent, |
| 530 | keyChainServiceConnection, |
| 531 | Context.BIND_AUTO_CREATE, |
| 532 | user); |
| Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 533 | if (!isBound) { |
| 534 | throw new AssertionError("could not bind to KeyChainService"); |
| 535 | } |
| 536 | return new KeyChainConnection(context, keyChainServiceConnection, q.take()); |
| 537 | } |
| 538 | |
| Alex Klyubin | 54bb159 | 2015-05-11 12:30:03 -0700 | [diff] [blame] | 539 | private static void ensureNotOnMainThread(@NonNull Context context) { |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 540 | Looper looper = Looper.myLooper(); |
| Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 541 | if (looper != null && looper == context.getMainLooper()) { |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 542 | throw new IllegalStateException( |
| 543 | "calling this from your main thread can lead to deadlock"); |
| 544 | } |
| 545 | } |
| Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 546 | } |