1/*
2 * Copyright (C) 201 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.browser;
18
19import android.content.Context;
20import android.os.AsyncTask;
21import android.security.KeyChain;
22import android.security.KeyChainException;
23import android.webkit.ClientCertRequestHandler;
24import java.security.PrivateKey;
25import java.security.cert.X509Certificate;
26
27final class KeyChainLookup extends AsyncTask<Void, Void, Void> {
28    private final Context mContext;
29    private final ClientCertRequestHandler mHandler;
30    private final String mAlias;
31    KeyChainLookup(Context context, ClientCertRequestHandler handler, String alias) {
32        mContext = context.getApplicationContext();
33        mHandler = handler;
34        mAlias = alias;
35    }
36    @Override protected Void doInBackground(Void... params) {
37        PrivateKey privateKey;
38        X509Certificate[] certificateChain;
39        try {
40            privateKey = KeyChain.getPrivateKey(mContext, mAlias);
41            certificateChain = KeyChain.getCertificateChain(mContext, mAlias);
42        } catch (InterruptedException e) {
43            mHandler.ignore();
44            return null;
45        } catch (KeyChainException e) {
46            mHandler.ignore();
47            return null;
48        }
49        mHandler.proceed(privateKey, certificateChain);
50        return null;
51    }
52}
53