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

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 3 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 namespace {
34 // Must be called on the I/O thread to record a client certificate
35 // and its private key in the OpenSSLClientKeyStore.
36 void RecordClientCertificateKey(
37 const scoped_refptr<net::X509Certificate>& client_cert,
38 ScopedEVP_PKEY private_key) {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
40 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
41 client_cert.get(), private_key.get());
42 }
43
44 } // namespace
45
27 AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj) 46 AwContentsClientBridge::AwContentsClientBridge(JNIEnv* env, jobject obj)
28 : java_ref_(env, obj) { 47 : java_ref_(env, obj) {
29 DCHECK(obj); 48 DCHECK(obj);
30 Java_AwContentsClientBridge_setNativeContentsClientBridge( 49 Java_AwContentsClientBridge_setNativeContentsClientBridge(
31 env, obj, reinterpret_cast<intptr_t>(this)); 50 env, obj, reinterpret_cast<intptr_t>(this));
32 } 51 }
33 52
34 AwContentsClientBridge::~AwContentsClientBridge() { 53 AwContentsClientBridge::~AwContentsClientBridge() {
35 JNIEnv* env = AttachCurrentThread(); 54 JNIEnv* env = AttachCurrentThread();
36 55
37 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 56 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
38 if (obj.is_null()) 57 if (obj.is_null())
39 return; 58 return;
40 // Clear the weak reference from the java peer to the native object since 59 // 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. 60 // it is possible that java object lifetime can exceed the AwContens.
42 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj.obj(), 0); 61 Java_AwContentsClientBridge_setNativeContentsClientBridge(env, obj.obj(), 0);
43 } 62 }
44 63
45 void AwContentsClientBridge::AllowCertificateError( 64 void AwContentsClientBridge::AllowCertificateError(
46 int cert_error, 65 int cert_error,
47 net::X509Certificate* cert, 66 net::X509Certificate* cert,
48 const GURL& request_url, 67 const GURL& request_url,
49 const base::Callback<void(bool)>& callback, 68 const base::Callback<void(bool)>& callback,
50 bool* cancel_request) { 69 bool* cancel_request) {
51 70
52 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 71 DCHECK_CURRENTLY_ON(BrowserThread::UI);
53 JNIEnv* env = AttachCurrentThread(); 72 JNIEnv* env = AttachCurrentThread();
54 73
55 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 74 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
56 if (obj.is_null()) 75 if (obj.is_null())
57 return; 76 return;
58 77
59 std::string der_string; 78 std::string der_string;
60 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string); 79 net::X509Certificate::GetDEREncoded(cert->os_cert_handle(), &der_string);
61 ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray( 80 ScopedJavaLocalRef<jbyteArray> jcert = base::android::ToJavaByteArray(
62 env, 81 env,
63 reinterpret_cast<const uint8*>(der_string.data()), 82 reinterpret_cast<const uint8*>(der_string.data()),
64 der_string.length()); 83 der_string.length());
65 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString( 84 ScopedJavaLocalRef<jstring> jurl(ConvertUTF8ToJavaString(
66 env, request_url.spec())); 85 env, request_url.spec()));
67 // We need to add the callback before making the call to java side, 86 // We need to add the callback before making the call to java side,
68 // as it may do a synchronous callback prior to returning. 87 // as it may do a synchronous callback prior to returning.
69 int request_id = pending_cert_error_callbacks_.Add( 88 int request_id = pending_cert_error_callbacks_.Add(
70 new CertErrorCallback(callback)); 89 new CertErrorCallback(callback));
71 *cancel_request = !Java_AwContentsClientBridge_allowCertificateError( 90 *cancel_request = !Java_AwContentsClientBridge_allowCertificateError(
72 env, obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id); 91 env, obj.obj(), cert_error, jcert.obj(), jurl.obj(), request_id);
73 // if the request is cancelled, then cancel the stored callback 92 // if the request is cancelled, then cancel the stored callback
74 if (*cancel_request) { 93 if (*cancel_request) {
75 pending_cert_error_callbacks_.Remove(request_id); 94 pending_cert_error_callbacks_.Remove(request_id);
76 } 95 }
77 } 96 }
78 97
79 void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj, 98 void AwContentsClientBridge::ProceedSslError(JNIEnv* env, jobject obj,
80 jboolean proceed, jint id) { 99 jboolean proceed, jint id) {
81 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 100 DCHECK_CURRENTLY_ON(BrowserThread::UI);
82 CertErrorCallback* callback = pending_cert_error_callbacks_.Lookup(id); 101 CertErrorCallback* callback = pending_cert_error_callbacks_.Lookup(id);
83 if (!callback || callback->is_null()) { 102 if (!callback || callback->is_null()) {
84 LOG(WARNING) << "Ignoring unexpected ssl error proceed callback"; 103 LOG(WARNING) << "Ignoring unexpected ssl error proceed callback";
85 return; 104 return;
86 } 105 }
87 callback->Run(proceed); 106 callback->Run(proceed);
88 pending_cert_error_callbacks_.Remove(id); 107 pending_cert_error_callbacks_.Remove(id);
89 } 108 }
90 109
110 // This method is inspired by SelectClientCertificate() in
111 // chrome/browser/ui/android/ssl_client_certificate_request.cc
112 void AwContentsClientBridge::SelectClientCertificate(
113 net::SSLCertRequestInfo* cert_request_info,
114 const SelectCertificateCallback& callback) {
115 DCHECK_CURRENTLY_ON(BrowserThread::UI);
116
117 // Add the callback to id map.
118 int request_id = pending_client_cert_request_callbacks_.Add(
119 new SelectCertificateCallback(callback));
120 // Make sure callback is run on error.
121 base::ScopedClosureRunner guard(base::Bind(
122 &AwContentsClientBridge::HandleErrorInClientCertificateResponse,
123 base::Unretained(this),
124 request_id));
125
126 JNIEnv* env = base::android::AttachCurrentThread();
127 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
128 if (obj.is_null())
129 return;
130
131 // Build the |key_types| JNI parameter, as a String[]
132 std::vector<std::string> key_types;
133 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) {
134 switch (cert_request_info->cert_key_types[n]) {
135 case net::CLIENT_CERT_RSA_SIGN:
136 key_types.push_back("RSA");
137 break;
138 case net::CLIENT_CERT_DSS_SIGN:
139 key_types.push_back("DSA");
140 break;
141 case net::CLIENT_CERT_ECDSA_SIGN:
142 key_types.push_back("ECDSA");
143 break;
144 default:
145 // Ignore unknown types.
146 break;
147 }
148 }
149
150 ScopedJavaLocalRef<jobjectArray> key_types_ref =
151 base::android::ToJavaArrayOfStrings(env, key_types);
152 if (key_types_ref.is_null()) {
153 LOG(ERROR) << "Could not create key types array (String[])";
154 return;
155 }
156
157 // Build the |encoded_principals| JNI parameter, as a byte[][]
158 ScopedJavaLocalRef<jobjectArray> principals_ref =
159 base::android::ToJavaArrayOfByteArray(
160 env, cert_request_info->cert_authorities);
161 if (principals_ref.is_null()) {
162 LOG(ERROR) << "Could not create principals array (byte[][])";
163 return;
164 }
165
166 // Build the |host_name| and |port| JNI parameters, as a String and
167 // a jint.
168 ScopedJavaLocalRef<jstring> host_name_ref =
169 base::android::ConvertUTF8ToJavaString(
170 env, cert_request_info->host_and_port.host());
171
172 Java_AwContentsClientBridge_selectClientCertificate(
173 env,
174 obj.obj(),
175 request_id,
176 key_types_ref.obj(),
177 principals_ref.obj(),
178 host_name_ref.obj(),
179 cert_request_info->host_and_port.port());
180
181 // Release the guard.
182 ignore_result(guard.Release());
183 }
184
185 // This method is inspired by OnSystemRequestCompletion() in
186 // chrome/browser/ui/android/ssl_client_certificate_request.cc
187 void AwContentsClientBridge::ProvideClientCertificateResponse(
188 JNIEnv* env,
189 jobject obj,
190 int request_id,
191 jobjectArray encoded_chain_ref,
192 jobject private_key_ref) {
193 DCHECK_CURRENTLY_ON(BrowserThread::UI);
194
195 SelectCertificateCallback* callback =
196 pending_client_cert_request_callbacks_.Lookup(request_id);
197 if (!callback || callback->is_null()) {
boliu 2014/04/21 16:56:45 The only way I can think of this happening legitim
sgurun-gerrit only 2014/04/21 23:53:29 I think this is really safe and defensive programm
boliu 2014/04/22 00:20:55 What if in a future change, we accidentally introd
sgurun-gerrit only 2014/04/22 00:44:36 the best way to identify bugs is via tests, which
boliu 2014/04/22 00:47:57 Disagree. If you want to keep an invariant in code
sgurun-gerrit only 2014/04/22 01:12:27 Assertions and exceptions are very useful in debug
boliu 2014/04/22 01:32:17 How do tests apply to this discussion? You can tes
sgurun-gerrit only 2014/04/22 02:06:47 Of course you can. The point is test coverage is n
sgurun-gerrit only 2014/04/22 17:41:08 Done.
digit1 2014/04/22 18:11:13 I think Bo's point is that this should be a DCHECK
198 LOG(WARNING) << "Ignoring unexpected client certificate response callback";
199 return;
200 }
201 // Make sure callback is run on error.
202 base::ScopedClosureRunner guard(base::Bind(
203 &AwContentsClientBridge::HandleErrorInClientCertificateResponse,
204 base::Unretained(this),
205 request_id));
206 if (encoded_chain_ref == NULL || private_key_ref == NULL) {
207 LOG(ERROR) << "Client certificate request cancelled";
208 return;
209 }
210 // Convert the encoded chain to a vector of strings.
211 std::vector<std::string> encoded_chain_strings;
212 if (encoded_chain_ref) {
213 base::android::JavaArrayOfByteArrayToStringVector(
214 env, encoded_chain_ref, &encoded_chain_strings);
215 }
216
217 std::vector<base::StringPiece> encoded_chain;
218 for (size_t n = 0; n < encoded_chain_strings.size(); ++n)
219 encoded_chain.push_back(encoded_chain_strings[n]);
220
221 // Create the X509Certificate object from the encoded chain.
222 scoped_refptr<net::X509Certificate> client_cert(
223 net::X509Certificate::CreateFromDERCertChain(encoded_chain));
224 if (!client_cert.get()) {
225 LOG(ERROR) << "Could not decode client certificate chain";
226 return;
227 }
228
229 // Create an EVP_PKEY wrapper for the private key JNI reference.
230 ScopedEVP_PKEY private_key(
231 net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref));
232 if (!private_key.get()) {
233 LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
234 return;
235 }
236
237 // RecordClientCertificateKey() must be called on the I/O thread,
238 // before the callback is called with the selected certificate on
239 // the UI thread.
240 content::BrowserThread::PostTaskAndReply(
241 content::BrowserThread::IO,
242 FROM_HERE,
243 base::Bind(&RecordClientCertificateKey,
244 client_cert,
245 base::Passed(&private_key)),
246 base::Bind(*callback, client_cert));
247 pending_client_cert_request_callbacks_.Remove(request_id);
248
249 // Release the guard.
250 ignore_result(guard.Release());
251 }
252
91 void AwContentsClientBridge::RunJavaScriptDialog( 253 void AwContentsClientBridge::RunJavaScriptDialog(
92 content::JavaScriptMessageType message_type, 254 content::JavaScriptMessageType message_type,
93 const GURL& origin_url, 255 const GURL& origin_url,
94 const base::string16& message_text, 256 const base::string16& message_text,
95 const base::string16& default_prompt_text, 257 const base::string16& default_prompt_text,
96 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { 258 const content::JavaScriptDialogManager::DialogClosedCallback& callback) {
97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); 259 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
98 JNIEnv* env = AttachCurrentThread(); 260 JNIEnv* env = AttachCurrentThread();
99 261
100 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); 262 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env);
(...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after
199 content::JavaScriptDialogManager::DialogClosedCallback* callback = 361 content::JavaScriptDialogManager::DialogClosedCallback* callback =
200 pending_js_dialog_callbacks_.Lookup(id); 362 pending_js_dialog_callbacks_.Lookup(id);
201 if (!callback) { 363 if (!callback) {
202 LOG(WARNING) << "Unexpected JS dialog cancel. " << id; 364 LOG(WARNING) << "Unexpected JS dialog cancel. " << id;
203 return; 365 return;
204 } 366 }
205 callback->Run(false, base::string16()); 367 callback->Run(false, base::string16());
206 pending_js_dialog_callbacks_.Remove(id); 368 pending_js_dialog_callbacks_.Remove(id);
207 } 369 }
208 370
371 // Use to cleanup if there is an error in client certificate response.
372 void AwContentsClientBridge::HandleErrorInClientCertificateResponse(
373 int request_id) {
374 SelectCertificateCallback* callback =
375 pending_client_cert_request_callbacks_.Lookup(request_id);
376 callback->Run(scoped_refptr<net::X509Certificate>());
377 pending_client_cert_request_callbacks_.Remove(request_id);
378 }
379
209 bool RegisterAwContentsClientBridge(JNIEnv* env) { 380 bool RegisterAwContentsClientBridge(JNIEnv* env) {
210 return RegisterNativesImpl(env) >= 0; 381 return RegisterNativesImpl(env) >= 0;
211 } 382 }
212 383
213 } // namespace android_webview 384 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698