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

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

Issue 2898573002: Refactor client cert private key handling. (Closed)
Patch Set: fix member order Created 3 years, 6 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
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/browser/aw_contents_client_bridge.h" 5 #include "android_webview/browser/aw_contents_client_bridge.h"
6 6
7 #include <memory> 7 #include <memory>
8 #include <utility> 8 #include <utility>
9 9
10 #include "android_webview/browser/aw_contents.h" 10 #include "android_webview/browser/aw_contents.h"
11 #include "android_webview/common/devtools_instrumentation.h" 11 #include "android_webview/common/devtools_instrumentation.h"
12 #include "android_webview/grit/components_strings.h" 12 #include "android_webview/grit/components_strings.h"
13 #include "base/android/jni_android.h" 13 #include "base/android/jni_android.h"
14 #include "base/android/jni_array.h" 14 #include "base/android/jni_array.h"
15 #include "base/android/jni_string.h" 15 #include "base/android/jni_string.h"
16 #include "base/callback_helpers.h" 16 #include "base/callback_helpers.h"
17 #include "base/macros.h" 17 #include "base/macros.h"
18 #include "base/memory/ptr_util.h" 18 #include "base/memory/ptr_util.h"
19 #include "base/memory/ref_counted.h" 19 #include "base/memory/ref_counted.h"
20 #include "base/message_loop/message_loop.h" 20 #include "base/message_loop/message_loop.h"
21 #include "content/public/browser/browser_thread.h" 21 #include "content/public/browser/browser_thread.h"
22 #include "content/public/browser/client_certificate_delegate.h" 22 #include "content/public/browser/client_certificate_delegate.h"
23 #include "content/public/browser/render_frame_host.h" 23 #include "content/public/browser/render_frame_host.h"
24 #include "content/public/browser/render_process_host.h" 24 #include "content/public/browser/render_process_host.h"
25 #include "content/public/browser/render_view_host.h" 25 #include "content/public/browser/render_view_host.h"
26 #include "content/public/browser/web_contents.h" 26 #include "content/public/browser/web_contents.h"
27 #include "jni/AwContentsClientBridge_jni.h" 27 #include "jni/AwContentsClientBridge_jni.h"
28 #include "net/cert/x509_certificate.h" 28 #include "net/cert/x509_certificate.h"
29 #include "net/http/http_response_headers.h" 29 #include "net/http/http_response_headers.h"
30 #include "net/ssl/openssl_client_key_store.h"
31 #include "net/ssl/ssl_cert_request_info.h" 30 #include "net/ssl/ssl_cert_request_info.h"
32 #include "net/ssl/ssl_client_cert_type.h" 31 #include "net/ssl/ssl_client_cert_type.h"
33 #include "net/ssl/ssl_platform_key_android.h" 32 #include "net/ssl/ssl_platform_key_android.h"
34 #include "net/ssl/ssl_private_key.h" 33 #include "net/ssl/ssl_private_key.h"
35 #include "ui/base/l10n/l10n_util.h" 34 #include "ui/base/l10n/l10n_util.h"
36 #include "url/gurl.h" 35 #include "url/gurl.h"
37 36
38 using base::android::AttachCurrentThread; 37 using base::android::AttachCurrentThread;
39 using base::android::ConvertJavaStringToUTF16; 38 using base::android::ConvertJavaStringToUTF16;
40 using base::android::ConvertUTF8ToJavaString; 39 using base::android::ConvertUTF8ToJavaString;
41 using base::android::ConvertUTF16ToJavaString; 40 using base::android::ConvertUTF16ToJavaString;
42 using base::android::HasException; 41 using base::android::HasException;
43 using base::android::JavaRef; 42 using base::android::JavaRef;
44 using base::android::ScopedJavaLocalRef; 43 using base::android::ScopedJavaLocalRef;
45 using base::android::ToJavaArrayOfStrings; 44 using base::android::ToJavaArrayOfStrings;
46 using content::BrowserThread; 45 using content::BrowserThread;
47 using content::WebContents; 46 using content::WebContents;
48 using std::vector; 47 using std::vector;
49 48
50 namespace android_webview { 49 namespace android_webview {
51 50
52 namespace { 51 namespace {
53 52
54 // Must be called on the I/O thread to record a client certificate
55 // and its private key in the OpenSSLClientKeyStore.
56 void RecordClientCertificateKey(net::X509Certificate* client_cert,
57 scoped_refptr<net::SSLPrivateKey> private_key) {
58 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
59 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
60 client_cert, std::move(private_key));
61 }
62
63 const void* const kAwContentsClientBridge = &kAwContentsClientBridge; 53 const void* const kAwContentsClientBridge = &kAwContentsClientBridge;
64 54
65 // This class is invented so that the UserData registry that we inject the 55 // This class is invented so that the UserData registry that we inject the
66 // AwContentsClientBridge object does not own and destroy it. 56 // AwContentsClientBridge object does not own and destroy it.
67 class UserData : public base::SupportsUserData::Data { 57 class UserData : public base::SupportsUserData::Data {
68 public: 58 public:
69 static AwContentsClientBridge* GetContents( 59 static AwContentsClientBridge* GetContents(
70 content::WebContents* web_contents) { 60 content::WebContents* web_contents) {
71 if (!web_contents) 61 if (!web_contents)
72 return NULL; 62 return NULL;
(...skipping 186 matching lines...) Expand 10 before | Expand all | Expand 10 after
259 // This method is inspired by OnSystemRequestCompletion() in 249 // This method is inspired by OnSystemRequestCompletion() in
260 // chrome/browser/ui/android/ssl_client_certificate_request.cc 250 // chrome/browser/ui/android/ssl_client_certificate_request.cc
261 void AwContentsClientBridge::ProvideClientCertificateResponse( 251 void AwContentsClientBridge::ProvideClientCertificateResponse(
262 JNIEnv* env, 252 JNIEnv* env,
263 const JavaRef<jobject>& obj, 253 const JavaRef<jobject>& obj,
264 int request_id, 254 int request_id,
265 const JavaRef<jobjectArray>& encoded_chain_ref, 255 const JavaRef<jobjectArray>& encoded_chain_ref,
266 const JavaRef<jobject>& private_key_ref) { 256 const JavaRef<jobject>& private_key_ref) {
267 DCHECK_CURRENTLY_ON(BrowserThread::UI); 257 DCHECK_CURRENTLY_ON(BrowserThread::UI);
268 258
259 // TODO(mattm): make this a unique_ptr and get rid of the guard stuff.
sgurun-gerrit only 2017/06/13 22:48:34 why not do it in this CL?
mattm 2017/06/13 23:40:59 Done.
269 content::ClientCertificateDelegate* delegate = 260 content::ClientCertificateDelegate* delegate =
270 pending_client_cert_request_delegates_.Lookup(request_id); 261 pending_client_cert_request_delegates_.Lookup(request_id);
271 DCHECK(delegate); 262 DCHECK(delegate);
272 263
273 if (encoded_chain_ref.is_null() || private_key_ref.is_null()) { 264 if (encoded_chain_ref.is_null() || private_key_ref.is_null()) {
274 LOG(ERROR) << "No client certificate selected"; 265 LOG(ERROR) << "No client certificate selected";
275 pending_client_cert_request_delegates_.Remove(request_id); 266 pending_client_cert_request_delegates_.Remove(request_id);
276 delegate->ContinueWithCertificate(nullptr); 267 delegate->ContinueWithCertificate(nullptr, nullptr);
277 delete delegate; 268 delete delegate;
278 return; 269 return;
279 } 270 }
280 271
281 // Make sure callback is run on error. 272 // Make sure callback is run on error.
282 base::ScopedClosureRunner guard(base::Bind( 273 base::ScopedClosureRunner guard(base::Bind(
283 &AwContentsClientBridge::HandleErrorInClientCertificateResponse, 274 &AwContentsClientBridge::HandleErrorInClientCertificateResponse,
284 base::Unretained(this), request_id)); 275 base::Unretained(this), request_id));
285 276
286 // Convert the encoded chain to a vector of strings. 277 // Convert the encoded chain to a vector of strings.
(...skipping 21 matching lines...) Expand all
308 if (!private_key) { 299 if (!private_key) {
309 LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; 300 LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
310 return; 301 return;
311 } 302 }
312 303
313 // Release the guard and |pending_client_cert_request_delegates_| references 304 // Release the guard and |pending_client_cert_request_delegates_| references
314 // to |delegate|. 305 // to |delegate|.
315 pending_client_cert_request_delegates_.Remove(request_id); 306 pending_client_cert_request_delegates_.Remove(request_id);
316 ignore_result(guard.Release()); 307 ignore_result(guard.Release());
317 308
318 // RecordClientCertificateKey() must be called on the I/O thread, 309 delegate->ContinueWithCertificate(std::move(client_cert),
319 // before the delegate is called with the selected certificate on 310 std::move(private_key));
320 // the UI thread. 311 delete delegate;
321 content::BrowserThread::PostTaskAndReply(
322 content::BrowserThread::IO, FROM_HERE,
323 base::Bind(&RecordClientCertificateKey, base::RetainedRef(client_cert),
324 base::Passed(&private_key)),
325 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
326 base::Owned(delegate), base::RetainedRef(client_cert)));
327 } 312 }
328 313
329 void AwContentsClientBridge::RunJavaScriptDialog( 314 void AwContentsClientBridge::RunJavaScriptDialog(
330 content::JavaScriptDialogType dialog_type, 315 content::JavaScriptDialogType dialog_type,
331 const GURL& origin_url, 316 const GURL& origin_url,
332 const base::string16& message_text, 317 const base::string16& message_text,
333 const base::string16& default_prompt_text, 318 const base::string16& default_prompt_text,
334 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { 319 const content::JavaScriptDialogManager::DialogClosedCallback& callback) {
335 DCHECK_CURRENTLY_ON(BrowserThread::UI); 320 DCHECK_CURRENTLY_ON(BrowserThread::UI);
336 JNIEnv* env = AttachCurrentThread(); 321 JNIEnv* env = AttachCurrentThread();
(...skipping 247 matching lines...) Expand 10 before | Expand all | Expand 10 after
584 pending_client_cert_request_delegates_.Remove(request_id); 569 pending_client_cert_request_delegates_.Remove(request_id);
585 570
586 delete delegate; 571 delete delegate;
587 } 572 }
588 573
589 bool RegisterAwContentsClientBridge(JNIEnv* env) { 574 bool RegisterAwContentsClientBridge(JNIEnv* env) {
590 return RegisterNativesImpl(env); 575 return RegisterNativesImpl(env);
591 } 576 }
592 577
593 } // namespace android_webview 578 } // namespace android_webview
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698