blob: 410956f9e7662ecab0127934a0ddc3a59957c264 [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 Lee8eb453b2015-02-17 14:13:36 +000020import android.app.ActivityManagerNative;
Robin Leeb46e4932015-02-03 18:17:39 +000021import android.app.admin.IDevicePolicyManager;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070022import android.app.AlertDialog;
23import android.app.Dialog;
24import android.app.PendingIntent;
Robin Leeb46e4932015-02-03 18:17:39 +000025import android.content.Context;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070026import android.content.DialogInterface;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070027import android.content.Intent;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070028import android.content.pm.PackageManager;
29import android.content.res.Resources;
Robin Lee606d9d02015-05-05 15:57:13 +010030import android.net.Uri;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070031import android.os.AsyncTask;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070032import android.os.Bundle;
Robin Leeb46e4932015-02-03 18:17:39 +000033import android.os.IBinder;
34import android.os.RemoteException;
35import android.os.ServiceManager;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070036import android.security.Credentials;
Brian Carlstromf5b50a42011-06-09 16:05:09 -070037import android.security.IKeyChainAliasCallback;
Brian Carlstrombb04f702011-05-24 21:54:51 -070038import android.security.KeyChain;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070039import android.security.KeyStore;
Fred Quintanafb2e18e2011-07-13 14:54:05 -070040import android.util.Log;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070041import android.view.LayoutInflater;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070042import android.view.View;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070043import android.view.ViewGroup;
Selim Gurun9c2b71c2012-03-22 15:51:14 -070044import android.widget.AdapterView;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070045import android.widget.BaseAdapter;
46import android.widget.Button;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070047import android.widget.ListView;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070048import android.widget.RadioButton;
49import android.widget.TextView;
50import com.android.org.bouncycastle.asn1.x509.X509Name;
51import java.io.ByteArrayInputStream;
52import java.io.InputStream;
53import java.security.cert.CertificateException;
54import java.security.cert.CertificateFactory;
55import java.security.cert.X509Certificate;
56import java.util.ArrayList;
57import java.util.Arrays;
58import java.util.Collections;
Robin Leeb46e4932015-02-03 18:17:39 +000059import java.util.concurrent.ExecutionException;
Brian Carlstrom65e649e2011-06-24 02:13:28 -070060import java.util.List;
Fred Quintanafb2e18e2011-07-13 14:54:05 -070061
Brian Carlstrom65e649e2011-06-24 02:13:28 -070062import javax.security.auth.x500.X500Principal;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070063
Brian Carlstrom65e649e2011-06-24 02:13:28 -070064public class KeyChainActivity extends Activity {
Fred Quintanafb2e18e2011-07-13 14:54:05 -070065 private static final String TAG = "KeyChain";
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070066
67 private static String KEY_STATE = "state";
68
69 private static final int REQUEST_UNLOCK = 1;
70
Fred Quintanafb2e18e2011-07-13 14:54:05 -070071 private int mSenderUid;
72
73 private PendingIntent mSender;
74
Kenny Root6ba32f22014-07-31 14:03:16 -070075 private static enum State { INITIAL, UNLOCK_REQUESTED, UNLOCK_CANCELED };
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070076
77 private State mState;
78
Brian Carlstrom65e649e2011-06-24 02:13:28 -070079 // beware that some of these KeyStore operations such as saw and
80 // get do file I/O in the remote keystore process and while they
81 // do not cause StrictMode violations, they logically should not
82 // be done on the UI thread.
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070083 private KeyStore mKeyStore = KeyStore.getInstance();
84
Brian Carlstrom3e6251d2011-04-11 09:05:06 -070085 @Override public void onCreate(Bundle savedState) {
86 super.onCreate(savedState);
87 if (savedState == null) {
88 mState = State.INITIAL;
89 } else {
90 mState = (State) savedState.getSerializable(KEY_STATE);
91 if (mState == null) {
92 mState = State.INITIAL;
93 }
94 }
95 }
96
97 @Override public void onResume() {
98 super.onResume();
99
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700100 mSender = getIntent().getParcelableExtra(KeyChain.EXTRA_SENDER);
101 if (mSender == null) {
102 // if no sender, bail, we need to identify the app to the user securely.
103 finish(null);
104 return;
105 }
106 try {
107 mSenderUid = getPackageManager().getPackageInfo(
108 mSender.getIntentSender().getTargetPackage(), 0).applicationInfo.uid;
109 } catch (PackageManager.NameNotFoundException e) {
110 // if unable to find the sender package info bail,
111 // we need to identify the app to the user securely.
112 finish(null);
113 return;
114 }
115
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700116 // see if KeyStore has been unlocked, if not start activity to do so
117 switch (mState) {
118 case INITIAL:
Kenny Root4ff22962013-02-14 10:17:06 -0800119 if (!mKeyStore.isUnlocked()) {
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700120 mState = State.UNLOCK_REQUESTED;
121 this.startActivityForResult(new Intent(Credentials.UNLOCK_ACTION),
122 REQUEST_UNLOCK);
123 // Note that Credentials.unlock will start an
124 // Activity and we will be paused but then resumed
125 // when the unlock Activity completes and our
126 // onActivityResult is called with REQUEST_UNLOCK
127 return;
128 }
Robin Leeb46e4932015-02-03 18:17:39 +0000129 chooseCertificate();
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700130 return;
131 case UNLOCK_REQUESTED:
132 // we've already asked, but have not heard back, probably just rotated.
133 // wait to hear back via onActivityResult
134 return;
Kenny Root6ba32f22014-07-31 14:03:16 -0700135 case UNLOCK_CANCELED:
136 // User wanted to cancel the request, so exit.
137 mState = State.INITIAL;
Kenny Root13e867c2014-11-14 08:34:57 -0800138 finish(null);
Kenny Root6ba32f22014-07-31 14:03:16 -0700139 return;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700140 default:
141 throw new AssertionError();
142 }
143 }
144
Robin Leeb46e4932015-02-03 18:17:39 +0000145 private void chooseCertificate() {
146 // Start loading the set of certs to choose from now- if device policy doesn't return an
147 // alias, having aliases loading already will save some time waiting for UI to start.
148 final AliasLoader loader = new AliasLoader();
149 loader.execute();
150
151 final IKeyChainAliasCallback.Stub callback = new IKeyChainAliasCallback.Stub() {
152 @Override public void alias(String alias) {
153 // Use policy-suggested alias if provided
154 if (alias != null) {
155 finish(alias);
156 return;
157 }
158
159 // No suggested alias - instead finish loading and show UI to pick one
160 final CertificateAdapter certAdapter;
161 try {
162 certAdapter = loader.get();
163 } catch (InterruptedException | ExecutionException e) {
164 Log.e(TAG, "Loading certificate aliases interrupted", e);
165 finish(null);
166 return;
167 }
168 runOnUiThread(new Runnable() {
169 @Override public void run() {
170 displayCertChooserDialog(certAdapter);
171 }
172 });
173 }
174 };
175
176 // Give a profile or device owner the chance to intercept the request, if a private key
177 // access listener is registered with the DevicePolicyManagerService.
178 IDevicePolicyManager devicePolicyManager = IDevicePolicyManager.Stub.asInterface(
179 ServiceManager.getService(Context.DEVICE_POLICY_SERVICE));
180
Robin Lee606d9d02015-05-05 15:57:13 +0100181 Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI);
Robin Leeb46e4932015-02-03 18:17:39 +0000182 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
Robin Leeb46e4932015-02-03 18:17:39 +0000183 try {
Robin Lee8eb453b2015-02-17 14:13:36 +0000184 int uid = ActivityManagerNative.getDefault().getLaunchedFromUid(getActivityToken());
Robin Lee606d9d02015-05-05 15:57:13 +0100185 devicePolicyManager.choosePrivateKeyAlias(uid, uri, alias, callback);
Robin Leeb46e4932015-02-03 18:17:39 +0000186 } catch (RemoteException e) {
Robin Lee8eb453b2015-02-17 14:13:36 +0000187 Log.e(TAG, "Unable to request alias from DevicePolicyManager", e);
Robin Leeb46e4932015-02-03 18:17:39 +0000188 // Proceed without a suggested alias.
189 try {
190 callback.alias(null);
191 } catch (RemoteException shouldNeverHappen) {
192 finish(null);
193 }
194 }
Brian Carlstrom91940e72011-06-28 20:37:31 -0700195 }
196
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700197 private class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
198 @Override protected CertificateAdapter doInBackground(Void... params) {
Alex Klyubin44c777b2015-06-08 09:46:15 -0700199 String[] aliasArray = mKeyStore.list(Credentials.USER_PRIVATE_KEY);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700200 List<String> aliasList = ((aliasArray == null)
201 ? Collections.<String>emptyList()
202 : Arrays.asList(aliasArray));
203 Collections.sort(aliasList);
204 return new CertificateAdapter(aliasList);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700205 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700206 }
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700207
Brian Carlstrom91940e72011-06-28 20:37:31 -0700208 private void displayCertChooserDialog(final CertificateAdapter adapter) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700209 AlertDialog.Builder builder = new AlertDialog.Builder(this);
Brian Carlstromdf172302011-06-26 17:13:54 -0700210
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700211 TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null);
212 View footer = View.inflate(this, R.layout.cert_chooser_footer, null);
213
214 final ListView lv = (ListView) View.inflate(this, R.layout.cert_chooser, null);
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700215 lv.addHeaderView(contextView, null, false);
216 lv.addFooterView(footer, null, false);
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700217 lv.setAdapter(adapter);
218 builder.setView(lv);
Brian Carlstromdf172302011-06-26 17:13:54 -0700219
Selim Gurun9c2b71c2012-03-22 15:51:14 -0700220 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
221
222 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
223 lv.setItemChecked(position, true);
224 }
225 });
226
Brian Carlstrom91940e72011-06-28 20:37:31 -0700227 boolean empty = adapter.mAliases.isEmpty();
Brian Carlstromdf172302011-06-26 17:13:54 -0700228 int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
229 builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700230 @Override public void onClick(DialogInterface dialog, int id) {
231 dialog.cancel(); // will cause OnDismissListener to be called
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700232 }
233 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700234
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700235 String title;
Brian Carlstromdf172302011-06-26 17:13:54 -0700236 Resources res = getResources();
237 if (empty) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700238 title = res.getString(R.string.title_no_certs);
239 } else {
240 title = res.getString(R.string.title_select_cert);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700241 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
242 if (alias != null) {
Brian Carlstrom62316552011-07-10 12:26:12 -0700243 // if alias was requested, set it if found
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700244 int adapterPosition = adapter.mAliases.indexOf(alias);
245 if (adapterPosition != -1) {
246 int listViewPosition = adapterPosition+1;
247 lv.setItemChecked(listViewPosition, true);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700248 }
Brian Carlstrom62316552011-07-10 12:26:12 -0700249 } else if (adapter.mAliases.size() == 1) {
250 // if only one choice, preselect it
251 int adapterPosition = 0;
252 int listViewPosition = adapterPosition+1;
253 lv.setItemChecked(listViewPosition, true);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700254 }
255
256 builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() {
257 @Override public void onClick(DialogInterface dialog, int id) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700258 int listViewPosition = lv.getCheckedItemPosition();
259 int adapterPosition = listViewPosition-1;
260 String alias = ((adapterPosition >= 0)
261 ? adapter.getItem(adapterPosition)
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700262 : null);
263 finish(alias);
264 }
265 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700266 }
267 builder.setTitle(title);
Brian Carlstrom91940e72011-06-28 20:37:31 -0700268 final Dialog dialog = builder.create();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700269
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700270
271 // getTargetPackage guarantees that the returned string is
272 // supplied by the system, so that an application can not
273 // spoof its package.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700274 String pkg = mSender.getIntentSender().getTargetPackage();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700275 PackageManager pm = getPackageManager();
276 CharSequence applicationLabel;
277 try {
278 applicationLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
279 } catch (PackageManager.NameNotFoundException e) {
280 applicationLabel = pkg;
281 }
282 String appMessage = String.format(res.getString(R.string.requesting_application),
283 applicationLabel);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700284 String contextMessage = appMessage;
Robin Lee606d9d02015-05-05 15:57:13 +0100285 Uri uri = getIntent().getParcelableExtra(KeyChain.EXTRA_URI);
286 if (uri != null) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700287 String hostMessage = String.format(res.getString(R.string.requesting_server),
Robin Lee606d9d02015-05-05 15:57:13 +0100288 uri.getAuthority());
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700289 if (contextMessage == null) {
290 contextMessage = hostMessage;
291 } else {
292 contextMessage += " " + hostMessage;
293 }
294 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700295 contextView.setText(contextMessage);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700296
297 String installMessage = String.format(res.getString(R.string.install_new_cert_message),
298 Credentials.EXTENSION_PFX, Credentials.EXTENSION_P12);
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700299 TextView installText = (TextView) footer.findViewById(R.id.cert_chooser_install_message);
300 installText.setText(installMessage);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700301
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700302 Button installButton = (Button) footer.findViewById(R.id.cert_chooser_install_button);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700303 installButton.setOnClickListener(new View.OnClickListener() {
304 @Override public void onClick(View v) {
305 // remove dialog so that we will recreate with
306 // possibly new content after install returns
Brian Carlstrom91940e72011-06-28 20:37:31 -0700307 dialog.dismiss();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700308 Credentials.getInstance().install(KeyChainActivity.this);
309 }
310 });
311
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700312 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
313 @Override public void onCancel(DialogInterface dialog) {
314 finish(null);
315 }
316 });
Brian Carlstrom91940e72011-06-28 20:37:31 -0700317 dialog.show();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700318 }
319
320 private class CertificateAdapter extends BaseAdapter {
321 private final List<String> mAliases;
322 private final List<String> mSubjects = new ArrayList<String>();
323 private CertificateAdapter(List<String> aliases) {
324 mAliases = aliases;
325 mSubjects.addAll(Collections.nCopies(aliases.size(), (String) null));
326 }
327 @Override public int getCount() {
328 return mAliases.size();
329 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700330 @Override public String getItem(int adapterPosition) {
331 return mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700332 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700333 @Override public long getItemId(int adapterPosition) {
334 return adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700335 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700336 @Override public View getView(final int adapterPosition, View view, ViewGroup parent) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700337 ViewHolder holder;
338 if (view == null) {
339 LayoutInflater inflater = LayoutInflater.from(KeyChainActivity.this);
340 view = inflater.inflate(R.layout.cert_item, parent, false);
341 holder = new ViewHolder();
342 holder.mAliasTextView = (TextView) view.findViewById(R.id.cert_item_alias);
343 holder.mSubjectTextView = (TextView) view.findViewById(R.id.cert_item_subject);
344 holder.mRadioButton = (RadioButton) view.findViewById(R.id.cert_item_selected);
345 view.setTag(holder);
346 } else {
347 holder = (ViewHolder) view.getTag();
348 }
349
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700350 String alias = mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700351
352 holder.mAliasTextView.setText(alias);
353
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700354 String subject = mSubjects.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700355 if (subject == null) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700356 new CertLoader(adapterPosition, holder.mSubjectTextView).execute();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700357 } else {
358 holder.mSubjectTextView.setText(subject);
359 }
360
361 ListView lv = (ListView)parent;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700362 int listViewCheckedItemPosition = lv.getCheckedItemPosition();
363 int adapterCheckedItemPosition = listViewCheckedItemPosition-1;
364 holder.mRadioButton.setChecked(adapterPosition == adapterCheckedItemPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700365 return view;
366 }
367
368 private class CertLoader extends AsyncTask<Void, Void, String> {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700369 private final int mAdapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700370 private final TextView mSubjectView;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700371 private CertLoader(int adapterPosition, TextView subjectView) {
372 mAdapterPosition = adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700373 mSubjectView = subjectView;
374 }
375 @Override protected String doInBackground(Void... params) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700376 String alias = mAliases.get(mAdapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700377 byte[] bytes = mKeyStore.get(Credentials.USER_CERTIFICATE + alias);
378 if (bytes == null) {
379 return null;
380 }
381 InputStream in = new ByteArrayInputStream(bytes);
382 X509Certificate cert;
383 try {
384 CertificateFactory cf = CertificateFactory.getInstance("X.509");
385 cert = (X509Certificate)cf.generateCertificate(in);
386 } catch (CertificateException ignored) {
387 return null;
388 }
389 // bouncycastle can handle the emailAddress OID of 1.2.840.113549.1.9.1
390 X500Principal subjectPrincipal = cert.getSubjectX500Principal();
391 X509Name subjectName = X509Name.getInstance(subjectPrincipal.getEncoded());
392 String subjectString = subjectName.toString(true, X509Name.DefaultSymbols);
393 return subjectString;
394 }
395 @Override protected void onPostExecute(String subjectString) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700396 mSubjects.set(mAdapterPosition, subjectString);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700397 mSubjectView.setText(subjectString);
398 }
399 }
400 }
401
402 private static class ViewHolder {
403 TextView mAliasTextView;
404 TextView mSubjectTextView;
405 RadioButton mRadioButton;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700406 }
407
408 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
409 switch (requestCode) {
410 case REQUEST_UNLOCK:
Kenny Root4ff22962013-02-14 10:17:06 -0800411 if (mKeyStore.isUnlocked()) {
Kenny Root6ba32f22014-07-31 14:03:16 -0700412 mState = State.INITIAL;
Robin Leeb46e4932015-02-03 18:17:39 +0000413 chooseCertificate();
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700414 } else {
415 // user must have canceled unlock, give up
Kenny Root6ba32f22014-07-31 14:03:16 -0700416 mState = State.UNLOCK_CANCELED;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700417 }
418 return;
419 default:
420 throw new AssertionError();
421 }
422 }
423
Brian Carlstrombb04f702011-05-24 21:54:51 -0700424 private void finish(String alias) {
425 if (alias == null) {
426 setResult(RESULT_CANCELED);
427 } else {
428 Intent result = new Intent();
429 result.putExtra(Intent.EXTRA_TEXT, alias);
430 setResult(RESULT_OK, result);
431 }
Brian Carlstromf5b50a42011-06-09 16:05:09 -0700432 IKeyChainAliasCallback keyChainAliasResponse
433 = IKeyChainAliasCallback.Stub.asInterface(
Brian Carlstrombb04f702011-05-24 21:54:51 -0700434 getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE));
435 if (keyChainAliasResponse != null) {
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700436 new ResponseSender(keyChainAliasResponse, alias).execute();
437 return;
438 }
439 finish();
440 }
441
442 private class ResponseSender extends AsyncTask<Void, Void, Void> {
443 private IKeyChainAliasCallback mKeyChainAliasResponse;
444 private String mAlias;
445 private ResponseSender(IKeyChainAliasCallback keyChainAliasResponse, String alias) {
446 mKeyChainAliasResponse = keyChainAliasResponse;
447 mAlias = alias;
448 }
449 @Override protected Void doInBackground(Void... unused) {
Brian Carlstrombb04f702011-05-24 21:54:51 -0700450 try {
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700451 if (mAlias != null) {
452 KeyChain.KeyChainConnection connection = KeyChain.bind(KeyChainActivity.this);
453 try {
454 connection.getService().setGrant(mSenderUid, mAlias, true);
455 } finally {
456 connection.close();
457 }
458 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700459 mKeyChainAliasResponse.alias(mAlias);
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700460 } catch (InterruptedException ignored) {
461 Thread.currentThread().interrupt();
462 Log.d(TAG, "interrupted while granting access", ignored);
Brian Carlstrom2a858832011-05-26 09:30:26 -0700463 } catch (Exception ignored) {
464 // don't just catch RemoteException, caller could
465 // throw back a RuntimeException across processes
466 // which we should protect against.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700467 Log.e(TAG, "error while granting access", ignored);
Brian Carlstrombb04f702011-05-24 21:54:51 -0700468 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700469 return null;
Brian Carlstrombb04f702011-05-24 21:54:51 -0700470 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700471 @Override protected void onPostExecute(Void unused) {
472 finish();
473 }
Brian Carlstrombb04f702011-05-24 21:54:51 -0700474 }
475
Brian Carlstrom9e606df2011-06-07 12:03:08 -0700476 @Override public void onBackPressed() {
477 finish(null);
478 }
479
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700480 @Override protected void onSaveInstanceState(Bundle savedState) {
481 super.onSaveInstanceState(savedState);
482 if (mState != State.INITIAL) {
483 savedState.putSerializable(KEY_STATE, mState);
484 }
485 }
486}