Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(814)

Side by Side Diff: android_webview/native/aw_contents_client_bridge.cc

Issue 235563005: Add client cert support to android_webview (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: code review phase 1 Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "android_webview/native/aw_contents_client_bridge.h" 5 #include "android_webview/native/aw_contents_client_bridge.h"
6 6
7 #include "android_webview/common/devtools_instrumentation.h" 7 #include "android_webview/common/devtools_instrumentation.h"
8 #include "base/android/jni_android.h" 8 #include "base/android/jni_android.h"
9 #include "base/android/jni_array.h" 9 #include "base/android/jni_array.h"
10 #include "base/android/jni_string.h" 10 #include "base/android/jni_string.h"
11 #include "base/callback.h" 11 #include "base/callback_helpers.h"
12 #include "content/public/browser/browser_thread.h" 12 #include "content/public/browser/browser_thread.h"
13 #include "jni/AwContentsClientBridge_jni.h" 13 #include "jni/AwContentsClientBridge_jni.h"
14 #include "net/android/keystore_openssl.h"
14 #include "net/cert/x509_certificate.h" 15 #include "net/cert/x509_certificate.h"
16 #include "net/ssl/openssl_client_key_store.h"
17 #include "net/ssl/ssl_cert_request_info.h"
18 #include "net/ssl/ssl_client_cert_type.h"
15 #include "url/gurl.h" 19 #include "url/gurl.h"
16 20
17 using base::android::AttachCurrentThread; 21 using base::android::AttachCurrentThread;
18 using base::android::ConvertJavaStringToUTF16; 22 using base::android::ConvertJavaStringToUTF16;
19 using base::android::ConvertUTF8ToJavaString; 23 using base::android::ConvertUTF8ToJavaString;
20 using base::android::ConvertUTF16ToJavaString; 24 using base::android::ConvertUTF16ToJavaString;
21 using base::android::JavaRef; 25 using base::android::JavaRef;
22 using base::android::ScopedJavaLocalRef; 26 using base::android::ScopedJavaLocalRef;
23 using content::BrowserThread; 27 using content::BrowserThread;
24 28
25 namespace android_webview { 29 namespace android_webview {
26 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_CURRENTLY_ON(content::BrowserThread::IO);
39 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
40 client_cert.get(), private_key.get());
41 }
42
27 AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj) 43 AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj)
28 : java_ref_(env, obj) { 44 : java_ref_(env, obj) {
29 DCHECK(obj); 45 DCHECK(obj);
30 Java_AwContentsClientBridge_setNativeContentsClientBridge( 46 Java_AwContentsClientBridge_setNativeContentsClientBridge(
31 env, obj, reinterpret_cast<intptr_t>(this)); 47 env, obj, reinterpret_cast<intptr_t>(this));
32 } 48 }
33 49
34 AwContentsClientBridge::~AwContentsClientBridge() { 50 AwContentsClientBridge::~AwContentsClientBridge() {
35 JNIEnv* env = AttachCurrentThread(); 51 JNIEnv* env = AttachCurrentThread();
36 52
37 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 53 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
38 if (obj.is_null()) 54 if (obj.is_null())
39 return; 55 return;
40 // Clear the weak reference from the java peer to the native object since 56 // Clear the weak reference from the java peer to the native object since
41 // it is possible that java object lifetime can exceed the AwContens. 57 // it is possible that java object lifetime can exceed the AwContens.
42 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj.obj(), 0); 58 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj.obj(), 0);
43 } 59 }
44 60
45 void AwContentsClientBridge::AllowCertificateError( 61 void AwContentsClientBridge::AllowCertificateError(
46 int cert_error, 62 int cert_error,
47 net::X509Certificate* cert, 63 net::X509Certificate* cert,
48 const GURL& request_url, 64 const GURL& request_url,
49 const base::Callback<void(bool)>& callback, 65 const base::Callback<void(bool)>& callback,
50 bool* cancel_request) { 66 bool* cancel_request) {
51 67
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 68 DCHECK_CURRENTLY_ON(BrowserThread::UI);
53 JNIEnv* env = AttachCurrentThread(); 69 JNIEnv* env = AttachCurrentThread();
54 70
55 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 71 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
56 if (obj.is_null()) 72 if (obj.is_null())
57 return; 73 return;
58 74
59 std::string der_string; 75 std::string der_string;
60 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string); 76 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string);
61 ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray( 77 ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray(
62 env, 78 env,
63 reinterpret_cast<const uint8*>(der_string.data()), 79 reinterpret_cast<const uint8*>(der_string.data()),
64 der_string.length()); 80 der_string.length());
65 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString( 81 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString(
66 env, request_url.spec())); 82 env, request_url.spec()));
67 // We need to add the callback before making the call to java side, 83 // We need to add the callback before making the call to java side,
68 // as it may do a synchronous callback prior to returning. 84 // as it may do a synchronous callback prior to returning.
69 int request_id = pending_cert_error_callbacks_.Add( 85 int request_id = pending_cert_error_callbacks_.Add(
70 new CertErrorCallback(callback)); 86 new CertErrorCallback(callback));
71 *cancel_request = !Java_AwContentsClientBridge_allowCertificateError( 87 *cancel_request = !Java_AwContentsClientBridge_allowCertificateError(
72 env, obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id); 88 env, obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id);
73 // if the request is cancelled, then cancel the stored callback 89 // if the request is cancelled, then cancel the stored callback
74 if (*cancel_request) { 90 if (*cancel_request) {
75 pending_cert_error_callbacks_.Remove(request_id); 91 pending_cert_error_callbacks_.Remove(request_id);
76 } 92 }
77 } 93 }
78 94
79 void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj, 95 void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj,
80 jboolean proceed, jint id) { 96 jboolean proceed, jint id) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 97 DCHECK_CURRENTLY_ON(BrowserThread::UI);
82 CertErrorCallback* callback = pending_cert_error_callbacks_.Lookup(id); 98 CertErrorCallback* callback = pending_cert_error_callbacks_.Lookup(id);
83 if (!callback || callback->is_null()) { 99 if (!callback || callback->is_null()) {
84 LOG(WARNING) << "Ignoring unexpected ssl error proceed callback"; 100 LOG(WARNING) << "Ignoring unexpected ssl error proceed callback";
85 return; 101 return;
86 } 102 }
87 callback->Run(proceed); 103 callback->Run(proceed);
88 pending_cert_error_callbacks_.Remove(id); 104 pending_cert_error_callbacks_.Remove(id);
89 } 105 }
90 106
107 // This method is almost same as SelectClientCertificate() in
108 // chrome/browser/ui/android/ssl_client_certificate_request.cc
109 void AwContentsClientBridge::SelectClientCertificate(
110 net::SSLCertRequestInfo* cert_request_info,
111 const SelectCertificateCallback& callback) {
112 DCHECK_CURRENTLY_ON(BrowserThread::UI);
113
114 // Ensure that callback(NULL) is posted as a task on the UI thread
115 // in case of an error.
116 base::Closure post_task_closure = base::Bind(
117 base::IgnoreResult(&content::BrowserThread::PostTask),
118 content::BrowserThread::UI,
119 FROM_HERE,
120 base::Bind(callback, scoped_refptr<net::X509Certificate>()));
121
122 base::ScopedClosureRunner guard(post_task_closure);
123
124 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
125 if (obj.is_null())
126 return;
127
128 // Build the |key_types| JNI parameter, as a String[]
129 std::vector<std::string> key_types;
130 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) {
131 switch (cert_request_info->cert_key_types[n]) {
132 case net::CLIENT_CERT_RSA_SIGN:
133 key_types.push_back("RSA");
134 break;
135 case net::CLIENT_CERT_DSS_SIGN:
136 key_types.push_back("DSA");
137 break;
138 case net::CLIENT_CERT_ECDSA_SIGN:
139 key_types.push_back("ECDSA");
140 break;
141 default:
142 // Ignore unknown types.
143 break;
144 }
145 }
146
147 JNIEnv* env = base::android::AttachCurrentThread();
148 ScopedJavaLocalRef<jobjectArray> key_types_ref =
149 base::android::ToJavaArrayOfStrings(env, key_types);
150 if (key_types_ref.is_null()) {
151 LOG(ERROR) << "Could not create key types array (String[])";
152 return;
153 }
154
155 // Build the |encoded_principals| JNI parameter, as a byte[][]
156 ScopedJavaLocalRef<jobjectArray> principals_ref =
157 base::android::ToJavaArrayOfByteArray(
158 env, cert_request_info->cert_authorities);
159 if (principals_ref.is_null()) {
160 LOG(ERROR) << "Could not create principals array (byte[][])";
161 return;
162 }
163
164 // Build the |host_name| and |port| JNI parameters, as a String and
165 // a jint.
166 ScopedJavaLocalRef<jstring> host_name_ref =
167 base::android::ConvertUTF8ToJavaString(
168 env, cert_request_info->host_and_port.host());
169
170 // Create a copy of the callback on the heap so that its address
171 // and ownership can be passed through and returned from Java via JNI.
172 scoped_ptr<SelectCertificateCallback> request(
173 new SelectCertificateCallback(callback));
174
175 jlong request_id = reinterpret_cast<intptr_t>(request.get());
176
177 if (!Java_AwContentsClientBridge_selectClientCertificate(
178 env,
179 obj.obj(),
180 request_id,
181 key_types_ref.obj(),
182 principals_ref.obj(),
183 host_name_ref.obj(),
184 cert_request_info->host_and_port.port())) {
185 return;
186 }
187
188 ignore_result(guard.Release());
189
190 // Ownership was transferred to Java.
191 SelectCertificateCallback* ALLOW_UNUSED dummy = request.release();
192 }
193
194 // This method is almost same as OnSystemRequestCompletion() in
195 // chrome/browser/ui/android/ssl_client_certificate_request.cc
196 void AwContentsClientBridge::ProvideClientCertificateResponse(
197 JNIEnv* env,
198 jobject obj,
199 jlong request_id,
200 jobjectArray encoded_chain_ref,
201 jobject private_key_ref) {
202 DCHECK_CURRENTLY_ON(BrowserThread::UI);
203
204 // Take back ownership of the request object.
205 scoped_ptr<SelectCertificateCallback> callback(
206 reinterpret_cast<SelectCertificateCallback*>(request_id));
207
208 // Ensure that callback(NULL) is called in case of an error.
209 base::Closure null_closure =
210 base::Bind(*callback, scoped_refptr<net::X509Certificate>());
211
212 base::ScopedClosureRunner guard(null_closure);
213
214 if (encoded_chain_ref == NULL || private_key_ref == NULL) {
215 LOG(ERROR) << "Client certificate request cancelled";
216 return;
217 }
218 // Convert the encoded chain to a vector of strings.
219 std::vector<std::string> encoded_chain_strings;
220 if (encoded_chain_ref) {
221 base::android::JavaArrayOfByteArrayToStringVector(
222 env, encoded_chain_ref, &encoded_chain_strings);
223 }
224
225 std::vector<base::StringPiece> encoded_chain;
226 for (size_t n = 0; n < encoded_chain_strings.size(); ++n)
227 encoded_chain.push_back(encoded_chain_strings[n]);
228
229 // Create the X509Certificate object from the encoded chain.
230 scoped_refptr<net::X509Certificate> client_cert(
231 net::X509Certificate::CreateFromDERCertChain(encoded_chain));
232 if (!client_cert.get()) {
233 LOG(ERROR) << "Could not decode client certificate chain";
234 return;
235 }
236
237 // Create an EVP_PKEY wrapper for the private key JNI reference.
238 ScopedEVP_PKEY private_key(
239 net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
240 if (!private_key.get()) {
241 LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
242 return;
243 }
244
245 ignore_result(guard.Release());
246
247 // RecordClientCertificateKey() must be called on the I/O thread,
248 // before the callback is called with the selected certificate on
249 // the UI thread.
250 content::BrowserThread::PostTaskAndReply(
251 content::BrowserThread::IO,
252 FROM_HERE,
253 base::Bind(&RecordClientCertificateKey,
254 client_cert,
255 base::Passed(&private_key)),
256 base::Bind(*callback, client_cert));
257 }
258
91 void AwContentsClientBridge::RunJavaScriptDialog( 259 void AwContentsClientBridge::RunJavaScriptDialog(
92 content::JavaScriptMessageType message_type, 260 content::JavaScriptMessageType message_type,
93 const GURL& origin_url, 261 const GURL& origin_url,
94 const base::string16& message_text, 262 const base::string16& message_text,
95 const base::string16& default_prompt_text, 263 const base::string16& default_prompt_text,
96 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { 264 const content::JavaScriptDialogManager::DialogClosedCallback& callback) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 265 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 JNIEnv* env = AttachCurrentThread(); 266 JNIEnv* env = AttachCurrentThread();
99 267
100 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 268 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
(...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after
204 } 372 }
205 callback->Run(false, base::string16()); 373 callback->Run(false, base::string16());
206 pending_js_dialog_callbacks_.Remove(id); 374 pending_js_dialog_callbacks_.Remove(id);
207 } 375 }
208 376
209 bool RegisterAwContentsClientBridge(JNIEnv* env) { 377 bool RegisterAwContentsClientBridge(JNIEnv* env) {
210 return RegisterNativesImpl(env) >= 0; 378 return RegisterNativesImpl(env) >= 0;
211 } 379 }
212 380
213 } // namespace android_webview 381 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698