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 | |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 18 | import android.app.Activity; |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 19 | import android.app.PendingIntent; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 20 | import android.content.ComponentName; |
| 21 | import android.content.Context; |
| 22 | import android.content.Intent; |
| 23 | import android.content.ServiceConnection; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 24 | import android.os.IBinder; |
| 25 | import android.os.Looper; |
| 26 | import android.os.RemoteException; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 27 | import java.io.ByteArrayInputStream; |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 28 | import java.io.Closeable; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 29 | import java.io.IOException; |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 30 | import java.security.KeyPair; |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 31 | import java.security.Principal; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 32 | import java.security.PrivateKey; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 33 | import java.security.cert.Certificate; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 34 | import java.security.cert.CertificateException; |
| 35 | import java.security.cert.CertificateFactory; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 36 | import java.security.cert.X509Certificate; |
Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 37 | import java.util.ArrayList; |
| 38 | import java.util.List; |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 39 | import java.util.concurrent.BlockingQueue; |
| 40 | import java.util.concurrent.LinkedBlockingQueue; |
Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 41 | import libcore.util.Objects; |
| 42 | import org.apache.harmony.xnet.provider.jsse.TrustedCertificateStore; |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 43 | |
| 44 | /** |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 45 | * The {@code KeyChain} class provides access to private keys and |
| 46 | * their corresponding certificate chains in credential storage. |
| 47 | * |
| 48 | * <p>Applications accessing the {@code KeyChain} normally go through |
| 49 | * these steps: |
| 50 | * |
| 51 | * <ol> |
| 52 | * |
| 53 | * <li>Receive a callback from an {@link javax.net.ssl.X509KeyManager |
| 54 | * X509KeyManager} that a private key is requested. |
| 55 | * |
| 56 | * <li>Call {@link #choosePrivateKeyAlias |
| 57 | * choosePrivateKeyAlias} to allow the user to select from a |
| 58 | * list of currently available private keys and corresponding |
| 59 | * certificate chains. The chosen alias will be returned by the |
| 60 | * callback {@link KeyChainAliasCallback#alias}, or null if no private |
| 61 | * key is available or the user cancels the request. |
| 62 | * |
| 63 | * <li>Call {@link #getPrivateKey} and {@link #getCertificateChain} to |
| 64 | * retrieve the credentials to return to the corresponding {@link |
| 65 | * javax.net.ssl.X509KeyManager} callbacks. |
| 66 | * |
| 67 | * </ol> |
| 68 | * |
| 69 | * <p>An application may remember the value of a selected alias to |
| 70 | * avoid prompting the user with {@link #choosePrivateKeyAlias |
| 71 | * choosePrivateKeyAlias} on subsequent connections. If the alias is |
| 72 | * no longer valid, null will be returned on lookups using that value |
Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 73 | * |
| 74 | * <p>An application can request the installation of private keys and |
| 75 | * certificates via the {@code Intent} provided by {@link |
| 76 | * #createInstallIntent}. Private keys installed via this {@code |
| 77 | * Intent} will be accessible via {@link #choosePrivateKeyAlias} while |
| 78 | * Certificate Authority (CA) certificates will be trusted by all |
| 79 | * applications through the default {@code X509TrustManager}. |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 80 | */ |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 81 | // TODO reference intent for credential installation when public |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 82 | public final class KeyChain { |
| 83 | |
| 84 | private static final String TAG = "KeyChain"; |
| 85 | |
| 86 | /** |
| 87 | * @hide Also used by KeyChainService implementation |
| 88 | */ |
| 89 | public static final String ACCOUNT_TYPE = "com.android.keychain"; |
| 90 | |
| 91 | /** |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 92 | * Action to bring up the KeyChainActivity |
| 93 | */ |
| 94 | private static final String ACTION_CHOOSER = "com.android.keychain.CHOOSER"; |
| 95 | |
| 96 | /** |
| 97 | * Extra for use with {@link #ACTION_CHOOSER} |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 98 | * @hide Also used by KeyChainActivity implementation |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 99 | */ |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 100 | public static final String EXTRA_RESPONSE = "response"; |
| 101 | |
| 102 | /** |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 103 | * Extra for use with {@link #ACTION_CHOOSER} |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 104 | * @hide Also used by KeyChainActivity implementation |
| 105 | */ |
| 106 | public static final String EXTRA_HOST = "host"; |
| 107 | |
| 108 | /** |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 109 | * Extra for use with {@link #ACTION_CHOOSER} |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 110 | * @hide Also used by KeyChainActivity implementation |
| 111 | */ |
| 112 | public static final String EXTRA_PORT = "port"; |
| 113 | |
| 114 | /** |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 115 | * Extra for use with {@link #ACTION_CHOOSER} |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 116 | * @hide Also used by KeyChainActivity implementation |
| 117 | */ |
| 118 | public static final String EXTRA_ALIAS = "alias"; |
| 119 | |
| 120 | /** |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 121 | * Extra for use with {@link #ACTION_CHOOSER} |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 122 | * @hide Also used by KeyChainActivity implementation |
| 123 | */ |
| 124 | public static final String EXTRA_SENDER = "sender"; |
| 125 | |
| 126 | /** |
Selim Gurun | 93ba4fe | 2012-02-14 11:48:49 -0800 | [diff] [blame] | 127 | * Action to bring up the CertInstaller. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 128 | */ |
| 129 | private static final String ACTION_INSTALL = "android.credentials.INSTALL"; |
| 130 | |
| 131 | /** |
| 132 | * Optional extra to specify a {@code String} credential name on |
| 133 | * the {@code Intent} returned by {@link #createInstallIntent}. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 134 | */ |
| 135 | // Compatible with old com.android.certinstaller.CredentialHelper.CERT_NAME_KEY |
| 136 | public static final String EXTRA_NAME = "name"; |
| 137 | |
| 138 | /** |
| 139 | * Optional extra to specify an X.509 certificate to install on |
| 140 | * the {@code Intent} returned by {@link #createInstallIntent}. |
| 141 | * The extra value should be a PEM or ASN.1 DER encoded {@code |
| 142 | * byte[]}. An {@link X509Certificate} can be converted to DER |
| 143 | * encoded bytes with {@link X509Certificate#getEncoded}. |
| 144 | * |
| 145 | * <p>{@link #EXTRA_NAME} may be used to provide a default alias |
| 146 | * name for the installed certificate. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 147 | */ |
| 148 | // Compatible with old android.security.Credentials.CERTIFICATE |
| 149 | public static final String EXTRA_CERTIFICATE = "CERT"; |
| 150 | |
| 151 | /** |
| 152 | * Optional extra for use with the {@code Intent} returned by |
| 153 | * {@link #createInstallIntent} to specify a PKCS#12 key store to |
| 154 | * install. The extra value should be a {@code byte[]}. The bytes |
| 155 | * may come from an external source or be generated with {@link |
Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 156 | * java.security.KeyStore#store} on a "PKCS12" instance. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 157 | * |
| 158 | * <p>The user will be prompted for the password to load the key store. |
| 159 | * |
| 160 | * <p>The key store will be scanned for {@link |
| 161 | * java.security.KeyStore.PrivateKeyEntry} entries and both the |
| 162 | * private key and associated certificate chain will be installed. |
| 163 | * |
| 164 | * <p>{@link #EXTRA_NAME} may be used to provide a default alias |
| 165 | * name for the installed credentials. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 166 | */ |
| 167 | // Compatible with old android.security.Credentials.PKCS12 |
| 168 | public static final String EXTRA_PKCS12 = "PKCS12"; |
| 169 | |
Selim Gurun | 93ba4fe | 2012-02-14 11:48:49 -0800 | [diff] [blame] | 170 | |
| 171 | /** |
| 172 | * @hide TODO This is temporary and will be removed |
| 173 | * Broadcast Action: Indicates the trusted storage has changed. Sent when |
| 174 | * one of this happens: |
| 175 | * |
| 176 | * <ul> |
| 177 | * <li>a new CA is added, |
| 178 | * <li>an existing CA is removed or disabled, |
| 179 | * <li>a disabled CA is enabled, |
| 180 | * <li>trusted storage is reset (all user certs are cleared), |
| 181 | * <li>when permission to access a private key is changed. |
| 182 | * </ul> |
| 183 | */ |
| 184 | public static final String ACTION_STORAGE_CHANGED = "android.security.STORAGE_CHANGED"; |
| 185 | |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 186 | /** |
| 187 | * Returns an {@code Intent} that can be used for credential |
| 188 | * installation. The intent may be used without any extras, in |
| 189 | * which case the user will be able to install credentials from |
| 190 | * their own source. |
| 191 | * |
| 192 | * <p>Alternatively, {@link #EXTRA_CERTIFICATE} or {@link |
| 193 | * #EXTRA_PKCS12} maybe used to specify the bytes of an X.509 |
| 194 | * certificate or a PKCS#12 key store for installation. These |
Brian Carlstrom | ca43c45 | 2011-06-29 18:53:17 -0700 | [diff] [blame] | 195 | * extras may be combined with {@link #EXTRA_NAME} to provide a |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 196 | * default alias name for credentials being installed. |
| 197 | * |
| 198 | * <p>When used with {@link Activity#startActivityForResult}, |
| 199 | * {@link Activity#RESULT_OK} will be returned if a credential was |
| 200 | * successfully installed, otherwise {@link |
| 201 | * Activity#RESULT_CANCELED} will be returned. |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 202 | */ |
| 203 | public static Intent createInstallIntent() { |
| 204 | Intent intent = new Intent(ACTION_INSTALL); |
| 205 | intent.setClassName("com.android.certinstaller", |
| 206 | "com.android.certinstaller.CertInstallerMain"); |
| 207 | return intent; |
| 208 | } |
| 209 | |
| 210 | /** |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 211 | * Launches an {@code Activity} for the user to select the alias |
| 212 | * for a private key and certificate pair for authentication. The |
| 213 | * selected alias or null will be returned via the |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 214 | * KeyChainAliasCallback callback. |
| 215 | * |
| 216 | * <p>{@code keyTypes} and {@code issuers} may be used to |
| 217 | * highlight suggested choices to the user, although to cope with |
| 218 | * sometimes erroneous values provided by servers, the user may be |
| 219 | * able to override these suggestions. |
| 220 | * |
| 221 | * <p>{@code host} and {@code port} may be used to give the user |
| 222 | * more context about the server requesting the credentials. |
| 223 | * |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 224 | * <p>{@code alias} allows the chooser to preselect an existing |
| 225 | * alias which will still be subject to user confirmation. |
| 226 | * |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 227 | * @param activity The {@link Activity} context to use for |
| 228 | * launching the new sub-Activity to prompt the user to select |
| 229 | * a private key; used only to call startActivity(); must not |
| 230 | * be null. |
| 231 | * @param response Callback to invoke when the request completes; |
| 232 | * must not be null |
| 233 | * @param keyTypes The acceptable types of asymmetric keys such as |
| 234 | * "RSA" or "DSA", or a null array. |
| 235 | * @param issuers The acceptable certificate issuers for the |
| 236 | * certificate matching the private key, or null. |
| 237 | * @param host The host name of the server requesting the |
| 238 | * certificate, or null if unavailable. |
| 239 | * @param port The port number of the server requesting the |
| 240 | * certificate, or -1 if unavailable. |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 241 | * @param alias The alias to preselect if available, or null if |
| 242 | * unavailable. |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 243 | */ |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 244 | public static void choosePrivateKeyAlias(Activity activity, KeyChainAliasCallback response, |
| 245 | String[] keyTypes, Principal[] issuers, |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 246 | String host, int port, |
| 247 | String alias) { |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 248 | /* |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 249 | * TODO currently keyTypes, issuers are unused. They are meant |
| 250 | * to follow the semantics and purpose of X509KeyManager |
| 251 | * method arguments. |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 252 | * |
| 253 | * keyTypes would allow the list to be filtered and typically |
| 254 | * will be set correctly by the server. In practice today, |
| 255 | * most all users will want only RSA, rarely DSA, and usually |
| 256 | * only a small number of certs will be available. |
| 257 | * |
| 258 | * issuers is typically not useful. Some servers historically |
| 259 | * will send the entire list of public CAs known to the |
| 260 | * server. Others will send none. If this is used, if there |
| 261 | * are no matches after applying the constraint, it should be |
| 262 | * ignored. |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 263 | */ |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 264 | if (activity == null) { |
| 265 | throw new NullPointerException("activity == null"); |
| 266 | } |
| 267 | if (response == null) { |
| 268 | throw new NullPointerException("response == null"); |
| 269 | } |
Brian Carlstrom | a00a2b3 | 2011-06-29 10:42:35 -0700 | [diff] [blame] | 270 | Intent intent = new Intent(ACTION_CHOOSER); |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 271 | intent.putExtra(EXTRA_RESPONSE, new AliasResponse(response)); |
Brian Carlstrom | 67c30df | 2011-06-24 02:13:23 -0700 | [diff] [blame] | 272 | intent.putExtra(EXTRA_HOST, host); |
| 273 | intent.putExtra(EXTRA_PORT, port); |
| 274 | intent.putExtra(EXTRA_ALIAS, alias); |
| 275 | // the PendingIntent is used to get calling package name |
| 276 | intent.putExtra(EXTRA_SENDER, PendingIntent.getActivity(activity, 0, new Intent(), 0)); |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 277 | activity.startActivity(intent); |
| 278 | } |
| 279 | |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 280 | private static class AliasResponse extends IKeyChainAliasCallback.Stub { |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 281 | private final KeyChainAliasCallback keyChainAliasResponse; |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 282 | private AliasResponse(KeyChainAliasCallback keyChainAliasResponse) { |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 283 | this.keyChainAliasResponse = keyChainAliasResponse; |
| 284 | } |
| 285 | @Override public void alias(String alias) { |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 286 | keyChainAliasResponse.alias(alias); |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 287 | } |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 288 | } |
| 289 | |
| 290 | /** |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 291 | * Returns the {@code PrivateKey} for the requested alias, or null |
| 292 | * if no there is no result. |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 293 | * |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 294 | * @param alias The alias of the desired private key, typically |
| 295 | * returned via {@link KeyChainAliasCallback#alias}. |
| 296 | * @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] | 297 | */ |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 298 | public static PrivateKey getPrivateKey(Context context, String alias) |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 299 | throws KeyChainException, InterruptedException { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 300 | if (alias == null) { |
| 301 | throw new NullPointerException("alias == null"); |
| 302 | } |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 303 | KeyChainConnection keyChainConnection = bind(context); |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 304 | try { |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 305 | IKeyChainService keyChainService = keyChainConnection.getService(); |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 306 | byte[] privateKeyBytes = keyChainService.getPrivateKey(alias); |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 307 | return toPrivateKey(privateKeyBytes); |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 308 | } catch (RemoteException e) { |
| 309 | throw new KeyChainException(e); |
| 310 | } catch (RuntimeException e) { |
| 311 | // only certain RuntimeExceptions can be propagated across the IKeyChainService call |
| 312 | throw new KeyChainException(e); |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 313 | } finally { |
| 314 | keyChainConnection.close(); |
| 315 | } |
| 316 | } |
| 317 | |
| 318 | /** |
| 319 | * Returns the {@code X509Certificate} chain for the requested |
| 320 | * alias, or null if no there is no result. |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 321 | * |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 322 | * @param alias The alias of the desired certificate chain, typically |
| 323 | * returned via {@link KeyChainAliasCallback#alias}. |
| 324 | * @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] | 325 | */ |
| 326 | public static X509Certificate[] getCertificateChain(Context context, String alias) |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 327 | throws KeyChainException, InterruptedException { |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 328 | if (alias == null) { |
| 329 | throw new NullPointerException("alias == null"); |
| 330 | } |
| 331 | KeyChainConnection keyChainConnection = bind(context); |
| 332 | try { |
Brian Carlstrom | ba1a667 | 2011-05-24 21:54:37 -0700 | [diff] [blame] | 333 | IKeyChainService keyChainService = keyChainConnection.getService(); |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 334 | byte[] certificateBytes = keyChainService.getCertificate(alias); |
Brian Carlstrom | db93b78 | 2011-07-01 00:12:17 -0700 | [diff] [blame] | 335 | List<X509Certificate> chain = new ArrayList<X509Certificate>(); |
| 336 | chain.add(toCertificate(certificateBytes)); |
| 337 | TrustedCertificateStore store = new TrustedCertificateStore(); |
| 338 | for (int i = 0; true; i++) { |
| 339 | X509Certificate cert = chain.get(i); |
| 340 | if (Objects.equal(cert.getSubjectX500Principal(), cert.getIssuerX500Principal())) { |
| 341 | break; |
| 342 | } |
| 343 | X509Certificate issuer = store.findIssuer(cert); |
| 344 | if (issuer == null) { |
| 345 | break; |
| 346 | } |
| 347 | chain.add(issuer); |
| 348 | } |
| 349 | return chain.toArray(new X509Certificate[chain.size()]); |
Brian Carlstrom | 93201f5 | 2011-06-09 15:05:35 -0700 | [diff] [blame] | 350 | } catch (RemoteException e) { |
| 351 | throw new KeyChainException(e); |
| 352 | } catch (RuntimeException e) { |
| 353 | // only certain RuntimeExceptions can be propagated across the IKeyChainService call |
| 354 | throw new KeyChainException(e); |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 355 | } finally { |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 356 | keyChainConnection.close(); |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 357 | } |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 358 | } |
| 359 | |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 360 | private static PrivateKey toPrivateKey(byte[] bytes) { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 361 | if (bytes == null) { |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 362 | throw new IllegalArgumentException("bytes == null"); |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 363 | } |
| 364 | try { |
Brian Carlstrom | 9d7faa9 | 2011-06-07 13:45:33 -0700 | [diff] [blame] | 365 | KeyPair keyPair = (KeyPair) Credentials.convertFromPem(bytes).get(0); |
| 366 | return keyPair.getPrivate(); |
| 367 | } catch (IOException e) { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 368 | throw new AssertionError(e); |
| 369 | } |
| 370 | } |
| 371 | |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 372 | private static X509Certificate toCertificate(byte[] bytes) { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 373 | if (bytes == null) { |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 374 | throw new IllegalArgumentException("bytes == null"); |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 375 | } |
| 376 | try { |
| 377 | CertificateFactory certFactory = CertificateFactory.getInstance("X.509"); |
| 378 | Certificate cert = certFactory.generateCertificate(new ByteArrayInputStream(bytes)); |
| 379 | return (X509Certificate) cert; |
| 380 | } catch (CertificateException e) { |
| 381 | throw new AssertionError(e); |
| 382 | } |
| 383 | } |
| 384 | |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 385 | /** |
| 386 | * @hide for reuse by CertInstaller and Settings. |
| 387 | * @see KeyChain#bind |
| 388 | */ |
| 389 | public final static class KeyChainConnection implements Closeable { |
| 390 | private final Context context; |
| 391 | private final ServiceConnection serviceConnection; |
| 392 | private final IKeyChainService service; |
| 393 | private KeyChainConnection(Context context, |
| 394 | ServiceConnection serviceConnection, |
| 395 | IKeyChainService service) { |
| 396 | this.context = context; |
| 397 | this.serviceConnection = serviceConnection; |
| 398 | this.service = service; |
| 399 | } |
| 400 | @Override public void close() { |
| 401 | context.unbindService(serviceConnection); |
| 402 | } |
| 403 | public IKeyChainService getService() { |
| 404 | return service; |
| 405 | } |
| 406 | } |
| 407 | |
| 408 | /** |
| 409 | * @hide for reuse by CertInstaller and Settings. |
| 410 | * |
| 411 | * Caller should call unbindService on the result when finished. |
| 412 | */ |
| 413 | public static KeyChainConnection bind(Context context) throws InterruptedException { |
| 414 | if (context == null) { |
| 415 | throw new NullPointerException("context == null"); |
| 416 | } |
| 417 | ensureNotOnMainThread(context); |
| 418 | final BlockingQueue<IKeyChainService> q = new LinkedBlockingQueue<IKeyChainService>(1); |
| 419 | ServiceConnection keyChainServiceConnection = new ServiceConnection() { |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 420 | volatile boolean mConnectedAtLeastOnce = false; |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 421 | @Override public void onServiceConnected(ComponentName name, IBinder service) { |
Fred Quintana | ab8b84a | 2011-07-13 14:55:39 -0700 | [diff] [blame] | 422 | if (!mConnectedAtLeastOnce) { |
| 423 | mConnectedAtLeastOnce = true; |
| 424 | try { |
| 425 | q.put(IKeyChainService.Stub.asInterface(service)); |
| 426 | } catch (InterruptedException e) { |
| 427 | // will never happen, since the queue starts with one available slot |
| 428 | } |
Brian Carlstrom | d752472 | 2011-05-17 16:20:36 -0700 | [diff] [blame] | 429 | } |
| 430 | } |
| 431 | @Override public void onServiceDisconnected(ComponentName name) {} |
| 432 | }; |
| 433 | boolean isBound = context.bindService(new Intent(IKeyChainService.class.getName()), |
| 434 | keyChainServiceConnection, |
| 435 | Context.BIND_AUTO_CREATE); |
| 436 | if (!isBound) { |
| 437 | throw new AssertionError("could not bind to KeyChainService"); |
| 438 | } |
| 439 | return new KeyChainConnection(context, keyChainServiceConnection, q.take()); |
| 440 | } |
| 441 | |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 442 | private static void ensureNotOnMainThread(Context context) { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 443 | Looper looper = Looper.myLooper(); |
Brian Carlstrom | 8e9929c | 2011-05-17 00:40:28 -0700 | [diff] [blame] | 444 | if (looper != null && looper == context.getMainLooper()) { |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 445 | throw new IllegalStateException( |
| 446 | "calling this from your main thread can lead to deadlock"); |
| 447 | } |
| 448 | } |
Brian Carlstrom | b9a07c1 | 2011-04-11 09:03:51 -0700 | [diff] [blame] | 449 | } |