blob: 7b317022d789d29dce5ed5e47b87988c4ebf7bc5 [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;
30import 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.
147 final AliasLoader loader = new AliasLoader();
148 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
180 String host = getIntent().getStringExtra(KeyChain.EXTRA_HOST);
181 int port = getIntent().getIntExtra(KeyChain.EXTRA_PORT, -1);
182 String url = getIntent().getStringExtra(KeyChain.EXTRA_URL);
183 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
184
185 try {
Robin Lee8eb453b2015-02-17 14:13:36 +0000186 int uid = ActivityManagerNative.getDefault().getLaunchedFromUid(getActivityToken());
187 devicePolicyManager.choosePrivateKeyAlias(uid, host, port, url, alias, callback);
Robin Leeb46e4932015-02-03 18:17:39 +0000188 } catch (RemoteException e) {
Robin Lee8eb453b2015-02-17 14:13:36 +0000189 Log.e(TAG, "Unable to request alias from DevicePolicyManager", e);
Robin Leeb46e4932015-02-03 18:17:39 +0000190 // Proceed without a suggested alias.
191 try {
192 callback.alias(null);
193 } catch (RemoteException shouldNeverHappen) {
194 finish(null);
195 }
196 }
Brian Carlstrom91940e72011-06-28 20:37:31 -0700197 }
198
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700199 private class AliasLoader extends AsyncTask<Void, Void, CertificateAdapter> {
200 @Override protected CertificateAdapter doInBackground(Void... params) {
201 String[] aliasArray = mKeyStore.saw(Credentials.USER_PRIVATE_KEY);
202 List<String> aliasList = ((aliasArray == null)
203 ? Collections.<String>emptyList()
204 : Arrays.asList(aliasArray));
205 Collections.sort(aliasList);
206 return new CertificateAdapter(aliasList);
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700207 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700208 }
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700209
Brian Carlstrom91940e72011-06-28 20:37:31 -0700210 private void displayCertChooserDialog(final CertificateAdapter adapter) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700211 AlertDialog.Builder builder = new AlertDialog.Builder(this);
Brian Carlstromdf172302011-06-26 17:13:54 -0700212
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700213 TextView contextView = (TextView) View.inflate(this, R.layout.cert_chooser_header, null);
214 View footer = View.inflate(this, R.layout.cert_chooser_footer, null);
215
216 final ListView lv = (ListView) View.inflate(this, R.layout.cert_chooser, null);
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700217 lv.addHeaderView(contextView, null, false);
218 lv.addFooterView(footer, null, false);
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700219 lv.setAdapter(adapter);
220 builder.setView(lv);
Brian Carlstromdf172302011-06-26 17:13:54 -0700221
Selim Gurun9c2b71c2012-03-22 15:51:14 -0700222 lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
223
224 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
225 lv.setItemChecked(position, true);
226 }
227 });
228
Brian Carlstrom91940e72011-06-28 20:37:31 -0700229 boolean empty = adapter.mAliases.isEmpty();
Brian Carlstromdf172302011-06-26 17:13:54 -0700230 int negativeLabel = empty ? android.R.string.cancel : R.string.deny_button;
231 builder.setNegativeButton(negativeLabel, new DialogInterface.OnClickListener() {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700232 @Override public void onClick(DialogInterface dialog, int id) {
233 dialog.cancel(); // will cause OnDismissListener to be called
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700234 }
235 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700236
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700237 String title;
Brian Carlstromdf172302011-06-26 17:13:54 -0700238 Resources res = getResources();
239 if (empty) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700240 title = res.getString(R.string.title_no_certs);
241 } else {
242 title = res.getString(R.string.title_select_cert);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700243 String alias = getIntent().getStringExtra(KeyChain.EXTRA_ALIAS);
244 if (alias != null) {
Brian Carlstrom62316552011-07-10 12:26:12 -0700245 // if alias was requested, set it if found
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700246 int adapterPosition = adapter.mAliases.indexOf(alias);
247 if (adapterPosition != -1) {
248 int listViewPosition = adapterPosition+1;
249 lv.setItemChecked(listViewPosition, true);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700250 }
Brian Carlstrom62316552011-07-10 12:26:12 -0700251 } else if (adapter.mAliases.size() == 1) {
252 // if only one choice, preselect it
253 int adapterPosition = 0;
254 int listViewPosition = adapterPosition+1;
255 lv.setItemChecked(listViewPosition, true);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700256 }
257
258 builder.setPositiveButton(R.string.allow_button, new DialogInterface.OnClickListener() {
259 @Override public void onClick(DialogInterface dialog, int id) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700260 int listViewPosition = lv.getCheckedItemPosition();
261 int adapterPosition = listViewPosition-1;
262 String alias = ((adapterPosition >= 0)
263 ? adapter.getItem(adapterPosition)
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700264 : null);
265 finish(alias);
266 }
267 });
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700268 }
269 builder.setTitle(title);
Brian Carlstrom91940e72011-06-28 20:37:31 -0700270 final Dialog dialog = builder.create();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700271
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700272
273 // getTargetPackage guarantees that the returned string is
274 // supplied by the system, so that an application can not
275 // spoof its package.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700276 String pkg = mSender.getIntentSender().getTargetPackage();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700277 PackageManager pm = getPackageManager();
278 CharSequence applicationLabel;
279 try {
280 applicationLabel = pm.getApplicationLabel(pm.getApplicationInfo(pkg, 0)).toString();
281 } catch (PackageManager.NameNotFoundException e) {
282 applicationLabel = pkg;
283 }
284 String appMessage = String.format(res.getString(R.string.requesting_application),
285 applicationLabel);
286
287 String contextMessage = appMessage;
288 String host = getIntent().getStringExtra(KeyChain.EXTRA_HOST);
289 if (host != null) {
290 String hostString = host;
291 int port = getIntent().getIntExtra(KeyChain.EXTRA_PORT, -1);
292 if (port != -1) {
293 hostString += ":" + port;
294 }
295 String hostMessage = String.format(res.getString(R.string.requesting_server),
296 hostString);
297 if (contextMessage == null) {
298 contextMessage = hostMessage;
299 } else {
300 contextMessage += " " + hostMessage;
301 }
302 }
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700303 contextView.setText(contextMessage);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700304
305 String installMessage = String.format(res.getString(R.string.install_new_cert_message),
306 Credentials.EXTENSION_PFX, Credentials.EXTENSION_P12);
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700307 TextView installText = (TextView) footer.findViewById(R.id.cert_chooser_install_message);
308 installText.setText(installMessage);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700309
Brian Carlstrom4bf9e1a2011-06-28 21:45:31 -0700310 Button installButton = (Button) footer.findViewById(R.id.cert_chooser_install_button);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700311 installButton.setOnClickListener(new View.OnClickListener() {
312 @Override public void onClick(View v) {
313 // remove dialog so that we will recreate with
314 // possibly new content after install returns
Brian Carlstrom91940e72011-06-28 20:37:31 -0700315 dialog.dismiss();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700316 Credentials.getInstance().install(KeyChainActivity.this);
317 }
318 });
319
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700320 dialog.setOnCancelListener(new DialogInterface.OnCancelListener() {
321 @Override public void onCancel(DialogInterface dialog) {
322 finish(null);
323 }
324 });
Brian Carlstrom91940e72011-06-28 20:37:31 -0700325 dialog.show();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700326 }
327
328 private class CertificateAdapter extends BaseAdapter {
329 private final List<String> mAliases;
330 private final List<String> mSubjects = new ArrayList<String>();
331 private CertificateAdapter(List<String> aliases) {
332 mAliases = aliases;
333 mSubjects.addAll(Collections.nCopies(aliases.size(), (String) null));
334 }
335 @Override public int getCount() {
336 return mAliases.size();
337 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700338 @Override public String getItem(int adapterPosition) {
339 return mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700340 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700341 @Override public long getItemId(int adapterPosition) {
342 return adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700343 }
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700344 @Override public View getView(final int adapterPosition, View view, ViewGroup parent) {
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700345 ViewHolder holder;
346 if (view == null) {
347 LayoutInflater inflater = LayoutInflater.from(KeyChainActivity.this);
348 view = inflater.inflate(R.layout.cert_item, parent, false);
349 holder = new ViewHolder();
350 holder.mAliasTextView = (TextView) view.findViewById(R.id.cert_item_alias);
351 holder.mSubjectTextView = (TextView) view.findViewById(R.id.cert_item_subject);
352 holder.mRadioButton = (RadioButton) view.findViewById(R.id.cert_item_selected);
353 view.setTag(holder);
354 } else {
355 holder = (ViewHolder) view.getTag();
356 }
357
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700358 String alias = mAliases.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700359
360 holder.mAliasTextView.setText(alias);
361
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700362 String subject = mSubjects.get(adapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700363 if (subject == null) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700364 new CertLoader(adapterPosition, holder.mSubjectTextView).execute();
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700365 } else {
366 holder.mSubjectTextView.setText(subject);
367 }
368
369 ListView lv = (ListView)parent;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700370 int listViewCheckedItemPosition = lv.getCheckedItemPosition();
371 int adapterCheckedItemPosition = listViewCheckedItemPosition-1;
372 holder.mRadioButton.setChecked(adapterPosition == adapterCheckedItemPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700373 return view;
374 }
375
376 private class CertLoader extends AsyncTask<Void, Void, String> {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700377 private final int mAdapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700378 private final TextView mSubjectView;
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700379 private CertLoader(int adapterPosition, TextView subjectView) {
380 mAdapterPosition = adapterPosition;
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700381 mSubjectView = subjectView;
382 }
383 @Override protected String doInBackground(Void... params) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700384 String alias = mAliases.get(mAdapterPosition);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700385 byte[] bytes = mKeyStore.get(Credentials.USER_CERTIFICATE + alias);
386 if (bytes == null) {
387 return null;
388 }
389 InputStream in = new ByteArrayInputStream(bytes);
390 X509Certificate cert;
391 try {
392 CertificateFactory cf = CertificateFactory.getInstance("X.509");
393 cert = (X509Certificate)cf.generateCertificate(in);
394 } catch (CertificateException ignored) {
395 return null;
396 }
397 // bouncycastle can handle the emailAddress OID of 1.2.840.113549.1.9.1
398 X500Principal subjectPrincipal = cert.getSubjectX500Principal();
399 X509Name subjectName = X509Name.getInstance(subjectPrincipal.getEncoded());
400 String subjectString = subjectName.toString(true, X509Name.DefaultSymbols);
401 return subjectString;
402 }
403 @Override protected void onPostExecute(String subjectString) {
Brian Carlstrom5dd41f62011-06-29 18:51:34 -0700404 mSubjects.set(mAdapterPosition, subjectString);
Brian Carlstrom65e649e2011-06-24 02:13:28 -0700405 mSubjectView.setText(subjectString);
406 }
407 }
408 }
409
410 private static class ViewHolder {
411 TextView mAliasTextView;
412 TextView mSubjectTextView;
413 RadioButton mRadioButton;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700414 }
415
416 @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) {
417 switch (requestCode) {
418 case REQUEST_UNLOCK:
Kenny Root4ff22962013-02-14 10:17:06 -0800419 if (mKeyStore.isUnlocked()) {
Kenny Root6ba32f22014-07-31 14:03:16 -0700420 mState = State.INITIAL;
Robin Leeb46e4932015-02-03 18:17:39 +0000421 chooseCertificate();
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700422 } else {
423 // user must have canceled unlock, give up
Kenny Root6ba32f22014-07-31 14:03:16 -0700424 mState = State.UNLOCK_CANCELED;
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700425 }
426 return;
427 default:
428 throw new AssertionError();
429 }
430 }
431
Brian Carlstrombb04f702011-05-24 21:54:51 -0700432 private void finish(String alias) {
433 if (alias == null) {
434 setResult(RESULT_CANCELED);
435 } else {
436 Intent result = new Intent();
437 result.putExtra(Intent.EXTRA_TEXT, alias);
438 setResult(RESULT_OK, result);
439 }
Brian Carlstromf5b50a42011-06-09 16:05:09 -0700440 IKeyChainAliasCallback keyChainAliasResponse
441 = IKeyChainAliasCallback.Stub.asInterface(
Brian Carlstrombb04f702011-05-24 21:54:51 -0700442 getIntent().getIBinderExtra(KeyChain.EXTRA_RESPONSE));
443 if (keyChainAliasResponse != null) {
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700444 new ResponseSender(keyChainAliasResponse, alias).execute();
445 return;
446 }
447 finish();
448 }
449
450 private class ResponseSender extends AsyncTask<Void, Void, Void> {
451 private IKeyChainAliasCallback mKeyChainAliasResponse;
452 private String mAlias;
453 private ResponseSender(IKeyChainAliasCallback keyChainAliasResponse, String alias) {
454 mKeyChainAliasResponse = keyChainAliasResponse;
455 mAlias = alias;
456 }
457 @Override protected Void doInBackground(Void... unused) {
Brian Carlstrombb04f702011-05-24 21:54:51 -0700458 try {
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700459 if (mAlias != null) {
460 KeyChain.KeyChainConnection connection = KeyChain.bind(KeyChainActivity.this);
461 try {
462 connection.getService().setGrant(mSenderUid, mAlias, true);
463 } finally {
464 connection.close();
465 }
466 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700467 mKeyChainAliasResponse.alias(mAlias);
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700468 } catch (InterruptedException ignored) {
469 Thread.currentThread().interrupt();
470 Log.d(TAG, "interrupted while granting access", ignored);
Brian Carlstrom2a858832011-05-26 09:30:26 -0700471 } catch (Exception ignored) {
472 // don't just catch RemoteException, caller could
473 // throw back a RuntimeException across processes
474 // which we should protect against.
Fred Quintanafb2e18e2011-07-13 14:54:05 -0700475 Log.e(TAG, "error while granting access", ignored);
Brian Carlstrombb04f702011-05-24 21:54:51 -0700476 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700477 return null;
Brian Carlstrombb04f702011-05-24 21:54:51 -0700478 }
Brian Carlstrom7d9aa752011-07-07 11:52:27 -0700479 @Override protected void onPostExecute(Void unused) {
480 finish();
481 }
Brian Carlstrombb04f702011-05-24 21:54:51 -0700482 }
483
Brian Carlstrom9e606df2011-06-07 12:03:08 -0700484 @Override public void onBackPressed() {
485 finish(null);
486 }
487
Brian Carlstrom3e6251d2011-04-11 09:05:06 -0700488 @Override protected void onSaveInstanceState(Bundle savedState) {
489 super.onSaveInstanceState(savedState);
490 if (mState != State.INITIAL) {
491 savedState.putSerializable(KEY_STATE, mState);
492 }
493 }
494}