f752dbb922fa01c54e20a0daee67aac2de04e48d
[platform/framework/web/crosswalk.git] / src / chrome / browser / ui / android / ssl_client_certificate_request.cc
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chrome/browser/ui/android/ssl_client_certificate_request.h"
6
7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h"
10 #include "base/basictypes.h"
11 #include "base/bind.h"
12 #include "base/callback_helpers.h"
13 #include "base/compiler_specific.h"
14 #include "base/logging.h"
15 #include "chrome/browser/ssl/ssl_client_certificate_selector.h"
16 #include "content/public/browser/browser_thread.h"
17 #include "jni/SSLClientCertificateRequest_jni.h"
18 #include "net/android/keystore_openssl.h"
19 #include "net/base/host_port_pair.h"
20 #include "net/cert/cert_database.h"
21 #include "net/cert/x509_certificate.h"
22 #include "net/ssl/openssl_client_key_store.h"
23 #include "net/ssl/ssl_cert_request_info.h"
24 #include "net/ssl/ssl_client_cert_type.h"
25
26
27 namespace chrome {
28
29 namespace {
30
31 typedef net::OpenSSLClientKeyStore::ScopedEVP_PKEY ScopedEVP_PKEY;
32
33 // Must be called on the I/O thread to record a client certificate
34 // and its private key in the OpenSSLClientKeyStore.
35 void RecordClientCertificateKey(
36     const scoped_refptr<net::X509Certificate>& client_cert,
37     ScopedEVP_PKEY private_key) {
38   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::IO));
39   net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
40       client_cert.get(), private_key.get());
41 }
42
43 void StartClientCertificateRequest(
44     const net::SSLCertRequestInfo* cert_request_info,
45     const chrome::SelectCertificateCallback& callback) {
46   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
47
48   // Ensure that callback(NULL) is posted as a task on the UI thread
49   // in case of an error.
50   base::Closure post_task_closure = base::Bind(
51       base::IgnoreResult(&content::BrowserThread::PostTask),
52       content::BrowserThread::UI,
53       FROM_HERE,
54       base::Bind(callback, scoped_refptr<net::X509Certificate>()));
55
56   base::ScopedClosureRunner guard(post_task_closure);
57
58   // Build the |key_types| JNI parameter, as a String[]
59   std::vector<std::string> key_types;
60   for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) {
61     switch (cert_request_info->cert_key_types[n]) {
62       case net::CLIENT_CERT_RSA_SIGN:
63         key_types.push_back("RSA");
64         break;
65       case net::CLIENT_CERT_DSS_SIGN:
66         key_types.push_back("DSA");
67         break;
68       case net::CLIENT_CERT_ECDSA_SIGN:
69         key_types.push_back("ECDSA");
70         break;
71       default:
72         // Ignore unknown types.
73         break;
74     }
75   }
76
77   JNIEnv* env = base::android::AttachCurrentThread();
78   ScopedJavaLocalRef<jobjectArray> key_types_ref =
79       base::android::ToJavaArrayOfStrings(env, key_types);
80   if (key_types_ref.is_null()) {
81     LOG(ERROR) << "Could not create key types array (String[])";
82     return;
83   }
84
85   // Build the |encoded_principals| JNI parameter, as a byte[][]
86   ScopedJavaLocalRef<jobjectArray> principals_ref =
87       base::android::ToJavaArrayOfByteArray(
88           env, cert_request_info->cert_authorities);
89   if (principals_ref.is_null()) {
90     LOG(ERROR) << "Could not create principals array (byte[][])";
91     return;
92   }
93
94   // Build the |host_name| and |port| JNI parameters, as a String and
95   // a jint.
96   ScopedJavaLocalRef<jstring> host_name_ref =
97       base::android::ConvertUTF8ToJavaString(
98           env, cert_request_info->host_and_port.host());
99
100   // Create a copy of the callback on the heap so that its address
101   // and ownership can be passed through and returned from Java via JNI.
102   scoped_ptr<chrome::SelectCertificateCallback> request(
103       new chrome::SelectCertificateCallback(callback));
104
105   jint request_id = reinterpret_cast<jint>(request.get());
106
107   if (!chrome::android::
108       Java_SSLClientCertificateRequest_selectClientCertificate(
109           env, request_id, key_types_ref.obj(), principals_ref.obj(),
110           host_name_ref.obj(), cert_request_info->host_and_port.port())) {
111     return;
112   }
113
114   ignore_result(guard.Release());
115
116   // Ownership was transferred to Java.
117   chrome::SelectCertificateCallback* ALLOW_UNUSED dummy =
118       request.release();
119 }
120
121 }  // namespace
122
123 namespace android {
124
125 // Called from JNI on request completion/result.
126 // |env| is the current thread's JNIEnv.
127 // |clazz| is the SSLClientCertificateRequest JNI class reference.
128 // |request_id| is the id passed to
129 // Java_SSLClientCertificateRequest_selectClientCertificate() in Start().
130 // |encoded_chain_ref| is a JNI reference to a Java array of byte arrays,
131 // each item holding a DER-encoded X.509 certificate.
132 // |private_key_ref| is the platform PrivateKey object JNI reference for
133 // the client certificate.
134 // Note: both |encoded_chain_ref| and |private_key_ref| will be NULL if
135 // the user didn't select a certificate.
136 static void OnSystemRequestCompletion(
137     JNIEnv* env,
138     jclass clazz,
139     jint request_id,
140     jobjectArray encoded_chain_ref,
141     jobject private_key_ref) {
142   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
143
144   // Take back ownership of the request object.
145   scoped_ptr<chrome::SelectCertificateCallback> callback(
146       reinterpret_cast<chrome::SelectCertificateCallback*>(request_id));
147
148   // Ensure that callback(NULL) is called in case of an error.
149   base::Closure null_closure =
150       base::Bind(*callback, scoped_refptr<net::X509Certificate>());
151
152   base::ScopedClosureRunner guard(null_closure);
153
154   if (encoded_chain_ref == NULL || private_key_ref == NULL) {
155     LOG(ERROR) << "Client certificate request cancelled";
156     return;
157   }
158
159   // Convert the encoded chain to a vector of strings.
160   std::vector<std::string> encoded_chain_strings;
161   if (encoded_chain_ref) {
162     base::android::JavaArrayOfByteArrayToStringVector(
163         env, encoded_chain_ref, &encoded_chain_strings);
164   }
165
166   std::vector<base::StringPiece> encoded_chain;
167   for (size_t n = 0; n < encoded_chain_strings.size(); ++n)
168     encoded_chain.push_back(encoded_chain_strings[n]);
169
170   // Create the X509Certificate object from the encoded chain.
171   scoped_refptr<net::X509Certificate> client_cert(
172       net::X509Certificate::CreateFromDERCertChain(encoded_chain));
173   if (!client_cert.get()) {
174     LOG(ERROR) << "Could not decode client certificate chain";
175     return;
176   }
177
178   // Create an EVP_PKEY wrapper for the private key JNI reference.
179   ScopedEVP_PKEY private_key(
180       net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
181   if (!private_key.get()) {
182     LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
183     return;
184   }
185
186   ignore_result(guard.Release());
187
188   // RecordClientCertificateKey() must be called on the I/O thread,
189   // before the callback is called with the selected certificate on
190   // the UI thread.
191   content::BrowserThread::PostTaskAndReply(
192       content::BrowserThread::IO,
193       FROM_HERE,
194       base::Bind(&RecordClientCertificateKey,
195                  client_cert,
196                  base::Passed(&private_key)),
197       base::Bind(*callback, client_cert));
198 }
199
200 static void NotifyClientCertificatesChanged() {
201   net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged();
202 }
203
204 static void NotifyClientCertificatesChangedOnIOThread(JNIEnv* env, jclass) {
205   if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
206     NotifyClientCertificatesChanged();
207   } else {
208     content::BrowserThread::PostTask(
209          content::BrowserThread::IO,
210          FROM_HERE,
211          base::Bind(&NotifyClientCertificatesChanged));
212   }
213 }
214
215 bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) {
216   return RegisterNativesImpl(env);
217 }
218
219 }  // namespace android
220
221 void ShowSSLClientCertificateSelector(
222     content::WebContents* contents,
223     const net::HttpNetworkSession* network_session,
224     net::SSLCertRequestInfo* cert_request_info,
225     const chrome::SelectCertificateCallback& callback) {
226   DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
227   StartClientCertificateRequest(cert_request_info, callback);
228 }
229
230 }  // namespace chrome