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 17 package android.webkit; 18 19 import android.os.Handler; 20 import java.security.PrivateKey; 21 import java.security.cert.CertificateEncodingException; 22 import java.security.cert.X509Certificate; 23 import org.apache.harmony.xnet.provider.jsse.NativeCrypto; 24 import org.apache.harmony.xnet.provider.jsse.OpenSSLKey; 25 import org.apache.harmony.xnet.provider.jsse.OpenSSLKeyHolder; 26 27 /** 28 * ClientCertRequestHandler: class responsible for handling client 29 * certificate requests. This class is passed as a parameter to 30 * BrowserCallback.displayClientCertRequestDialog and is meant to 31 * receive the user's response. 32 * 33 * @hide 34 */ 35 public final class ClientCertRequestHandler extends Handler { 36 37 private final BrowserFrame mBrowserFrame; 38 private final int mHandle; 39 private final String mHostAndPort; 40 private final SslClientCertLookupTable mTable; 41 ClientCertRequestHandler(BrowserFrame browserFrame, 42 int handle, 43 String host_and_port, 44 SslClientCertLookupTable table) { 45 mBrowserFrame = browserFrame; 46 mHandle = handle; 47 mHostAndPort = host_and_port; 48 mTable = table; 49 } 50 51 /** 52 * Proceed with the specified private key and client certificate chain. 53 */ 54 public void proceed(PrivateKey privateKey, X509Certificate[] chain) { 55 try { 56 byte[][] chainBytes = NativeCrypto.encodeCertificates(chain); 57 mTable.Allow(mHostAndPort, privateKey, chainBytes); 58 59 if (privateKey instanceof OpenSSLKeyHolder) { 60 OpenSSLKey pkey = ((OpenSSLKeyHolder) privateKey).getOpenSSLKey(); 61 setSslClientCertFromCtx(pkey.getPkeyContext(), chainBytes); 62 } else { 63 setSslClientCertFromPKCS8(privateKey.getEncoded(), chainBytes); 64 } 65 } catch (CertificateEncodingException e) { 66 post(new Runnable() { 67 public void run() { 68 mBrowserFrame.nativeSslClientCert(mHandle, 0, null); 69 return; 70 } 71 }); 72 } 73 } 74 75 /** 76 * Proceed with the specified private key bytes and client certificate chain. 77 */ 78 private void setSslClientCertFromCtx(final long ctx, final byte[][] chainBytes) { 79 post(new Runnable() { 80 public void run() { 81 mBrowserFrame.nativeSslClientCert(mHandle, ctx, chainBytes); 82 } 83 }); 84 } 85 86 /** 87 * Proceed with the specified private key context and client certificate chain. 88 */ 89 private void setSslClientCertFromPKCS8(final byte[] key, final byte[][] chainBytes) { 90 post(new Runnable() { 91 public void run() { 92 mBrowserFrame.nativeSslClientCert(mHandle, key, chainBytes); 93 } 94 }); 95 } 96 97 /** 98 * Igore the request for now, the user may be prompted again. 99 */ 100 public void ignore() { 101 post(new Runnable() { 102 public void run() { 103 mBrowserFrame.nativeSslClientCert(mHandle, 0, null); 104 } 105 }); 106 } 107 108 /** 109 * Cancel this request, remember the users negative choice. 110 */ 111 public void cancel() { 112 mTable.Deny(mHostAndPort); 113 post(new Runnable() { 114 public void run() { 115 mBrowserFrame.nativeSslClientCert(mHandle, 0, null); 116 } 117 }); 118 } 119 } 120