blob: 30d86eeeddaa6a90b2903f6a17b5e80e44c6561e [file] [log] [blame]
Brian Carlstrom3e6251d2011-04-11 09:05:06 -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 */
16
17package com.android.keychain;
18
Brian Carlstrom65e649e2011-06-24 02:13:28 -070019import android.app.Activity;
Robin Leeb46e4932015-02-03 18:17:39 +000020import android.app.admin.IDevicePolicyManager;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070021import android.app.AlertDialog;
22import android.app.Dialog;
23import android.app.PendingIntent;
Robin Leeb46e4932015-02-03 18:17:39 +000024import android.content.Context;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070025import android.content.DialogInterface;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070026import android.content.Intent;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070027import android.content.pm.PackageManager;
28import android.content.res.Resources;
Robin Lee606d9d02015-05-05 15:57:13 +010029import android.net.Uri;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070030import android.os.AsyncTask;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070031import android.os.Bundle;
Robin Leeb46e4932015-02-03 18:17:39 +000032import android.os.IBinder;
33import android.os.RemoteException;
34import android.os.ServiceManager;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070035import android.security.Credentials;
Brian Carlstromf5b50a42011-06-09 16:05:09 -070036import android.security.IKeyChainAliasCallback;
Brian Carlstrombb04f702011-05-24 21:54:51 -070037import android.security.KeyChain;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070038import android.security.KeyStore;
Fred Quintanafb2e18e2011-07-13 14:54:05 -070039import android.util.Log;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070040import android.view.LayoutInflater;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070041import android.view.View;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070042import android.view.ViewGroup;
Selim Gurun9c2b71c2012-03-22 15:51:14 -070043import android.widget.AdapterView;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070044import android.widget.BaseAdapter;
45import android.widget.Button;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070046import android.widget.ListView;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070047import android.widget.RadioButton;
48import android.widget.TextView;
49import com.android.org.bouncycastle.asn1.x509.X509Name;
50import java.io.ByteArrayInputStream;
51import java.io.InputStream;
52import java.security.cert.CertificateException;
53import java.security.cert.CertificateFactory;
54import java.security.cert.X509Certificate;
55import java.util.ArrayList;
56import java.util.Arrays;
57import java.util.Collections;
Robin Leeb46e4932015-02-03 18:17:39 +000058import java.util.concurrent.ExecutionException;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070059import java.util.List;
Fred Quintanafb2e18e2011-07-13 14:54:05 -070060
Brian Carlstrom65e649e2011-06-24 02:13:28 -070061import javax.security.auth.x500.X500Principal;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070062
Brian Carlstrom65e649e2011-06-24 02:13:28 -070063public class KeyChainActivity extends Activity {
Fred Quintanafb2e18e2011-07-13 14:54:05 -070064 private static final String TAG = "KeyChain";
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070065
66 private static String KEY_STATE = "state";
67
68 private static final int REQUEST_UNLOCK = 1;
69
Fred Quintanafb2e18e2011-07-13 14:54:05 -070070 private int mSenderUid;
71
72 private PendingIntent mSender;
73
Kenny Root6ba32f22014-07-31 14:03:16 -070074 private static enum State { INITIAL, UNLOCK_REQUESTED, UNLOCK_CANCELED };
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070075
76 private State mState;
77
Brian Carlstrom65e649e2011-06-24 02:13:28 -070078 // beware that some of these KeyStore operations such as saw and
79 // get do file I/O in the remote keystore process and while they
80 // do not cause StrictMode violations, they logically should not
81 // be done on the UI thread.
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070082 private KeyStore mKeyStore = KeyStore.getInstance();
83
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070084 @Override public void onCreate(Bundle savedState) {
85 super.onCreate(savedState);
86 if (savedState == null) {
87 mState = State.INITIAL;
88 } else {
89 mState = (State) savedState.getSerializable(KEY_STATE);
90 if (mState == null) {
91 mState = State.INITIAL;
92 }
93 }
94 }
95
96 @Override public void onResume() {
97 super.onResume();
98
Fred Quintanafb2e18e2011-07-13 14:54:05 -070099 mSender = getIntent().getParcelableExtra(KeyChain.EXTRA_SENDER);
100 if (mSender == null) {
101 // if no sender, bail, we need to identify the app to the user securely.
102 finish(null);
103 return;
104 }
105 try {
106 mSenderUid = getPackageManager().getPackageInfo(
107 mSender.getIntentSender().getTargetPackage(), 0).applicationInfo.uid;
108 } catch (PackageManager.NameNotFoundException e) {
109 // if unable to find the sender package info bail,
110 // we need to identify the app to the user securely.
111 finish(null);
112 return;
113 }
114
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700115 // see if KeyStore has been unlocked, if not start activity to do so
116 switch (mState) {
117 case INITIAL:
Kenny Root4ff22962013-02-14 10:17:06 -0800118 if (!mKeyStore.isUnlocked()) {
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700119 mState = State.UNLOCK_REQUESTED;
120 this.startActivityForResult(new Intent(Credentials.UNLOCK_ACTION),
121 REQUEST_UNLOCK);
122 // Note that Credentials.unlock will start an
123 // Activity and we will be paused but then resumed
124 // when the unlock Activity completes and our
125 // onActivityResult is called with REQUEST_UNLOCK
126 return;
127 }
Robin Leeb46e4932015-02-03 18:17:39 +0000128 chooseCertificate();
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700129 return;
130 case UNLOCK_REQUESTED:
131 // we've already asked, but have not heard back, probably just rotated.
132 // wait to hear back via onActivityResult
133 return;
Kenny Root6ba32f22014-07-31 14:03:16 -0700134 case UNLOCK_CANCELED:
135 // User wanted to cancel the request, so exit.
136 mState = State.INITIAL;
Kenny Root13e867c2014-11-14 08:34:57 -0800137 finish(null);
Kenny Root6ba32f22014-07-31 14:03:16 -0700138 return;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700139 default:
140 throw new AssertionError();
141 }
142 }
143
Robin Leeb46e4932015-02-03 18:17:39 +0000144 private void chooseCertificate() {
145 // Start loading the set of certs to choose from now- if device policy doesn't return an
146 // alias, having aliases loading already will save some time waiting for UI to start.
Eran Messeri96235f62017-10-20 10:27:28 +0100147 final AliasLoader loader = new AliasLoader(mKeyStore, this);
Robin Leeb46e4932015-02-03 18:17:39 +0000148 loader.execute();
149
150 final IKeyChainAliasCallback.Stub callback = new IKeyChainAliasCallback.Stub() {
151 @Override public void alias(String alias) {
152 // Use policy-suggested alias if provided
153 if (alias != null) {
154 finish(alias);
155 return;
156 }
157
158 // No suggested alias - instead finish loading and show UI to pick one
159 final CertificateAdapter certAdapter;
160 try {
161 certAdapter = loader.get();
162 } catch (InterruptedException | ExecutionException e) {
163 Log.e(TAG, "Loading certificate aliases interrupted", e);
164 finish(null);
165 return;
166 }
167 runOnUiThread(new Runnable() {
168 @Override public void run() {
169 displayCertChooserDialog(certAdapter);
170 }
171 });
172 }
173 };
174
175 // Give a profile or device owner the chance to intercept the request, if a private key
176 // access listener is registered with the DevicePolicyManagerService.
177 IDevicePolicyManager devicePolicyManager = IDevicePolicyManager.Stub.asInterface(
178 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
179
Robin Lee606d9d02015-05-05 15:57:13 +0100180 Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI);
Robin Leeb46e4932015-02-03 18:17:39 +0000181 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
Robin Leeb46e4932015-02-03 18:17:39 +0000182 try {
Robin Lee9bfaabb2015-07-31 14:52:54 +0100183 devicePolicyManager.choosePrivateKeyAlias(mSenderUid, uri, alias, callback);
Robin Leeb46e4932015-02-03 18:17:39 +0000184 } catch (RemoteException e) {
Robin Lee8eb453b2015-02-17 14:13:36 +0000185 Log.e(TAG, "Unable to request alias from DevicePolicyManager", e);
Robin Leeb46e4932015-02-03 18:17:39 +0000186 // Proceed without a suggested alias.
187 try {
188 callback.alias(null);
189 } catch (RemoteException shouldNeverHappen) {
190 finish(null);
191 }
192 }
Brian Carlstrom91940e72011-06-28 20:37:31 -0700193 }
194
Eran Messeri96235f62017-10-20 10:27:28 +0100195 private static class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
196 private final KeyStore mKeyStore;
197 private final Context mContext;
198 public AliasLoader(KeyStore keyStore, Context context) {
199 mKeyStore = keyStore;
200 mContext = context;
201 }
202
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700203 @Override protected CertificateAdapter doInBackground(Void... params) {
Alex Klyubin44c777b2015-06-08 09:46:15 -0700204 String[] aliasArray = mKeyStore.list(Credentials.USER_PRIVATE_KEY);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700205 List<String> aliasList = ((aliasArray == null)
206 ? Collections.<String>emptyList()
207 : Arrays.asList(aliasArray));
208 Collections.sort(aliasList);
Eran Messeri96235f62017-10-20 10:27:28 +0100209 return new CertificateAdapter(mKeyStore, mContext, aliasList);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700210 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700211 }
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700212
Brian Carlstrom91940e72011-06-28 20:37:31 -0700213 private void displayCertChooserDialog(final CertificateAdapter adapter) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700214 AlertDialog.Builder builder = new AlertDialog.Builder(this);
Brian Carlstromdf172302011-06-26 17:13:54 -0700215
Brian Carlstrom91940e72011-06-28 20:37:31 -0700216 boolean empty = adapter.mAliases.isEmpty();
Brian Carlstromdf172302011-06-26 17:13:54 -0700217 int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
Robin Lee128549b2016-07-11 19:34:59 +0100218 builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700219 @Override public void onClick(DialogInterface dialog, int id) {
220 dialog.cancel(); // will cause OnDismissListener to be called
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700221 }
222 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700223
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700224 String title;
Robin Lee8c9d4332016-04-26 12:49:24 +0100225 int selectedItem = -1;
Brian Carlstromdf172302011-06-26 17:13:54 -0700226 Resources res = getResources();
227 if (empty) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700228 title = res.getString(R.string.title_no_certs);
229 } else {
230 title = res.getString(R.string.title_select_cert);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700231 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
Robin Lee8c9d4332016-04-26 12:49:24 +0100232
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700233 if (alias != null) {
Brian Carlstrom62316552011-07-10 12:26:12 -0700234 // if alias was requested, set it if found
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700235 int adapterPosition = adapter.mAliases.indexOf(alias);
236 if (adapterPosition != -1) {
Robin Lee8c9d4332016-04-26 12:49:24 +0100237 // increase by 1 to account for item 0 being the header.
238 selectedItem = adapterPosition + 1;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700239 }
Brian Carlstrom62316552011-07-10 12:26:12 -0700240 } else if (adapter.mAliases.size() == 1) {
241 // if only one choice, preselect it
Robin Lee8c9d4332016-04-26 12:49:24 +0100242 selectedItem = 1;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700243 }
244
245 builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() {
246 @Override public void onClick(DialogInterface dialog, int id) {
Robin Lee8c9d4332016-04-26 12:49:24 +0100247 if (dialog instanceof AlertDialog) {
248 ListView lv = ((AlertDialog) dialog).getListView();
249 int listViewPosition = lv.getCheckedItemPosition();
250 int adapterPosition = listViewPosition-1;
251 String alias = ((adapterPosition >= 0)
252 ? adapter.getItem(adapterPosition)
253 : null);
254 finish(alias);
255 } else {
256 Log.wtf(TAG, "Expected AlertDialog, got " + dialog, new Exception());
257 finish(null);
258 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700259 }
260 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700261 }
262 builder.setTitle(title);
Robin Lee8c9d4332016-04-26 12:49:24 +0100263 builder.setSingleChoiceItems(adapter, selectedItem, null);
Robin Lee8c9d4332016-04-26 12:49:24 +0100264 final AlertDialog dialog = builder.create();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700265
Robin Lee128549b2016-07-11 19:34:59 +0100266 // Show text above and below the list to explain what the certificate will be used for,
267 // and how to install another one, respectively.
Robin Lee8c9d4332016-04-26 12:49:24 +0100268 TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null);
Robin Lee8c9d4332016-04-26 12:49:24 +0100269
270 final ListView lv = dialog.getListView();
271 lv.addHeaderView(contextView, null, false);
Robin Lee128549b2016-07-11 19:34:59 +0100272 lv.addFooterView(View.inflate(this, R.layout.cert_install, null));
Robin Lee8c9d4332016-04-26 12:49:24 +0100273 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
274 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Robin Lee128549b2016-07-11 19:34:59 +0100275 if (position == 0) {
276 // Header. Just text; ignore clicks.
277 return;
278 } else if (position == adapter.getCount() + 1) {
279 // Footer. Remove dialog so that we will recreate with possibly new content
280 // after install returns.
281 dialog.dismiss();
282 Credentials.getInstance().install(KeyChainActivity.this);
283 } else {
284 dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(true);
285 lv.setItemChecked(position, true);
286 adapter.notifyDataSetChanged();
287 }
Robin Lee8c9d4332016-04-26 12:49:24 +0100288 }
289 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700290
291 // getTargetPackage guarantees that the returned string is
292 // supplied by the system, so that an application can not
293 // spoof its package.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700294 String pkg = mSender.getIntentSender().getTargetPackage();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700295 PackageManager pm = getPackageManager();
296 CharSequence applicationLabel;
297 try {
298 applicationLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
299 } catch (PackageManager.NameNotFoundException e) {
300 applicationLabel = pkg;
301 }
302 String appMessage = String.format(res.getString(R.string.requesting_application),
303 applicationLabel);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700304 String contextMessage = appMessage;
Robin Lee606d9d02015-05-05 15:57:13 +0100305 Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI);
306 if (uri != null) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700307 String hostMessage = String.format(res.getString(R.string.requesting_server),
Robin Lee606d9d02015-05-05 15:57:13 +0100308 uri.getAuthority());
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700309 if (contextMessage == null) {
310 contextMessage = hostMessage;
311 } else {
312 contextMessage += " " + hostMessage;
313 }
314 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700315 contextView.setText(contextMessage);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700316
Robin Lee128549b2016-07-11 19:34:59 +0100317 if (selectedItem == -1) {
318 dialog.setOnShowListener(new DialogInterface.OnShowListener() {
319 @Override
320 public void onShow(DialogInterface dialogInterface) {
321 dialog.getButton(DialogInterface.BUTTON_POSITIVE).setEnabled(false);
322 }
323 });
324 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700325 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
326 @Override public void onCancel(DialogInterface dialog) {
327 finish(null);
328 }
329 });
Brian Carlstrom91940e72011-06-28 20:37:31 -0700330 dialog.show();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700331 }
332
Eran Messeri96235f62017-10-20 10:27:28 +0100333 private static class CertificateAdapter extends BaseAdapter {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700334 private final List<String> mAliases;
335 private final List<String> mSubjects = new ArrayList<String>();
Eran Messeri96235f62017-10-20 10:27:28 +0100336 private final KeyStore mKeyStore;
337 private final Context mContext;
338
339 private CertificateAdapter(KeyStore keyStore, Context context, List<String> aliases) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700340 mAliases = aliases;
341 mSubjects.addAll(Collections.nCopies(aliases.size(), (String) null));
Eran Messeri96235f62017-10-20 10:27:28 +0100342 mKeyStore = keyStore;
343 mContext = context;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700344 }
345 @Override public int getCount() {
346 return mAliases.size();
347 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700348 @Override public String getItem(int adapterPosition) {
349 return mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700350 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700351 @Override public long getItemId(int adapterPosition) {
352 return adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700353 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700354 @Override public View getView(final int adapterPosition, View view, ViewGroup parent) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700355 ViewHolder holder;
356 if (view == null) {
Eran Messeri96235f62017-10-20 10:27:28 +0100357 LayoutInflater inflater = LayoutInflater.from(mContext);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700358 view = inflater.inflate(R.layout.cert_item, parent, false);
359 holder = new ViewHolder();
360 holder.mAliasTextView = (TextView) view.findViewById(R.id.cert_item_alias);
361 holder.mSubjectTextView = (TextView) view.findViewById(R.id.cert_item_subject);
362 holder.mRadioButton = (RadioButton) view.findViewById(R.id.cert_item_selected);
363 view.setTag(holder);
364 } else {
365 holder = (ViewHolder) view.getTag();
366 }
367
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700368 String alias = mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700369
370 holder.mAliasTextView.setText(alias);
371
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700372 String subject = mSubjects.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700373 if (subject == null) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700374 new CertLoader(adapterPosition, holder.mSubjectTextView).execute();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700375 } else {
376 holder.mSubjectTextView.setText(subject);
377 }
378
379 ListView lv = (ListView)parent;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700380 int listViewCheckedItemPosition = lv.getCheckedItemPosition();
381 int adapterCheckedItemPosition = listViewCheckedItemPosition-1;
382 holder.mRadioButton.setChecked(adapterPosition == adapterCheckedItemPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700383 return view;
384 }
385
386 private class CertLoader extends AsyncTask<Void, Void, String> {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700387 private final int mAdapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700388 private final TextView mSubjectView;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700389 private CertLoader(int adapterPosition, TextView subjectView) {
390 mAdapterPosition = adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700391 mSubjectView = subjectView;
392 }
393 @Override protected String doInBackground(Void... params) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700394 String alias = mAliases.get(mAdapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700395 byte[] bytes = mKeyStore.get(Credentials.USER_CERTIFICATE + alias);
396 if (bytes == null) {
397 return null;
398 }
399 InputStream in = new ByteArrayInputStream(bytes);
400 X509Certificate cert;
401 try {
402 CertificateFactory cf = CertificateFactory.getInstance("X.509");
403 cert = (X509Certificate)cf.generateCertificate(in);
404 } catch (CertificateException ignored) {
405 return null;
406 }
407 // bouncycastle can handle the emailAddress OID of 1.2.840.113549.1.9.1
408 X500Principal subjectPrincipal = cert.getSubjectX500Principal();
409 X509Name subjectName = X509Name.getInstance(subjectPrincipal.getEncoded());
410 String subjectString = subjectName.toString(true, X509Name.DefaultSymbols);
411 return subjectString;
412 }
413 @Override protected void onPostExecute(String subjectString) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700414 mSubjects.set(mAdapterPosition, subjectString);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700415 mSubjectView.setText(subjectString);
416 }
417 }
418 }
419
420 private static class ViewHolder {
421 TextView mAliasTextView;
422 TextView mSubjectTextView;
423 RadioButton mRadioButton;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700424 }
425
426 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
427 switch (requestCode) {
428 case REQUEST_UNLOCK:
Kenny Root4ff22962013-02-14 10:17:06 -0800429 if (mKeyStore.isUnlocked()) {
Kenny Root6ba32f22014-07-31 14:03:16 -0700430 mState = State.INITIAL;
Robin Leeb46e4932015-02-03 18:17:39 +0000431 chooseCertificate();
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700432 } else {
433 // user must have canceled unlock, give up
Kenny Root6ba32f22014-07-31 14:03:16 -0700434 mState = State.UNLOCK_CANCELED;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700435 }
436 return;
437 default:
438 throw new AssertionError();
439 }
440 }
441
Brian Carlstrombb04f702011-05-24 21:54:51 -0700442 private void finish(String alias) {
443 if (alias == null) {
444 setResult(RESULT_CANCELED);
445 } else {
446 Intent result = new Intent();
447 result.putExtra(Intent.EXTRA_TEXT, alias);
448 setResult(RESULT_OK, result);
449 }
Brian Carlstromf5b50a42011-06-09 16:05:09 -0700450 IKeyChainAliasCallback keyChainAliasResponse
451 = IKeyChainAliasCallback.Stub.asInterface(
Brian Carlstrombb04f702011-05-24 21:54:51 -0700452 getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE));
453 if (keyChainAliasResponse != null) {
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700454 new ResponseSender(keyChainAliasResponse, alias).execute();
455 return;
456 }
457 finish();
458 }
459
460 private class ResponseSender extends AsyncTask<Void, Void, Void> {
461 private IKeyChainAliasCallback mKeyChainAliasResponse;
462 private String mAlias;
463 private ResponseSender(IKeyChainAliasCallback keyChainAliasResponse, String alias) {
464 mKeyChainAliasResponse = keyChainAliasResponse;
465 mAlias = alias;
466 }
467 @Override protected Void doInBackground(Void... unused) {
Brian Carlstrombb04f702011-05-24 21:54:51 -0700468 try {
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700469 if (mAlias != null) {
470 KeyChain.KeyChainConnection connection = KeyChain.bind(KeyChainActivity.this);
471 try {
472 connection.getService().setGrant(mSenderUid, mAlias, true);
473 } finally {
474 connection.close();
475 }
476 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700477 mKeyChainAliasResponse.alias(mAlias);
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700478 } catch (InterruptedException ignored) {
479 Thread.currentThread().interrupt();
480 Log.d(TAG, "interrupted while granting access", ignored);
Brian Carlstrom2a858832011-05-26 09:30:26 -0700481 } catch (Exception ignored) {
482 // don't just catch RemoteException, caller could
483 // throw back a RuntimeException across processes
484 // which we should protect against.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700485 Log.e(TAG, "error while granting access", ignored);
Brian Carlstrombb04f702011-05-24 21:54:51 -0700486 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700487 return null;
Brian Carlstrombb04f702011-05-24 21:54:51 -0700488 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700489 @Override protected void onPostExecute(Void unused) {
490 finish();
491 }
Brian Carlstrombb04f702011-05-24 21:54:51 -0700492 }
493
Brian Carlstrom9e606df2011-06-07 12:03:08 -0700494 @Override public void onBackPressed() {
495 finish(null);
496 }
497
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700498 @Override protected void onSaveInstanceState(Bundle savedState) {
499 super.onSaveInstanceState(savedState);
500 if (mState != State.INITIAL) {
501 savedState.putSerializable(KEY_STATE, mState);
502 }
503 }
504}