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 java.security.PrivateKey; 20 import java.security.cert.CertificateEncodingException; 21 import java.security.cert.X509Certificate; 22 import org.apache.harmony.xnet.provider.jsse.NativeCrypto; 23 24 /** 25 * ClientCertRequestHandler: class responsible for handling client 26 * certificate requests. This class is passed as a parameter to 27 * BrowserCallback.displayClientCertRequestDialog and is meant to 28 * receive the user's response. 29 * 30 * @hide 31 */ 32 public final class ClientCertRequestHandler { 33 34 private final BrowserFrame mBrowserFrame; 35 private final int mHandle; 36 private final String mHostAndPort; 37 private final SslClientCertLookupTable mTable; ClientCertRequestHandler(BrowserFrame browserFrame, int handle, String host_and_port, SslClientCertLookupTable table)38 ClientCertRequestHandler(BrowserFrame browserFrame, 39 int handle, 40 String host_and_port, 41 SslClientCertLookupTable table) { 42 mBrowserFrame = browserFrame; 43 mHandle = handle; 44 mHostAndPort = host_and_port; 45 mTable = table; 46 } 47 48 /** 49 * Proceed with the specified private key and client certificate chain. 50 */ proceed(PrivateKey privateKey, X509Certificate[] chain)51 public void proceed(PrivateKey privateKey, X509Certificate[] chain) { 52 byte[] privateKeyBytes = privateKey.getEncoded(); 53 byte[][] chainBytes; 54 try { 55 chainBytes = NativeCrypto.encodeCertificates(chain); 56 } catch (CertificateEncodingException e) { 57 mBrowserFrame.nativeSslClientCert(mHandle, null, null); 58 return; 59 } 60 mTable.Allow(mHostAndPort, privateKeyBytes, chainBytes); 61 mBrowserFrame.nativeSslClientCert(mHandle, privateKeyBytes, chainBytes); 62 } 63 64 /** 65 * Igore the request for now, the user may be prompted again. 66 */ ignore()67 public void ignore() { 68 mBrowserFrame.nativeSslClientCert(mHandle, null, null); 69 } 70 71 /** 72 * Cancel this request, remember the users negative choice. 73 */ cancel()74 public void cancel() { 75 mTable.Deny(mHostAndPort); 76 mBrowserFrame.nativeSslClientCert(mHandle, null, null); 77 } 78 } 79