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

Side by Side Diff: chrome/browser/ui/android/ssl_client_certificate_request.cc

Issue 859213006: Cancel client auth requests when not promptable. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@client-auth-cancel-1
Patch Set: extension test Created 5 years, 10 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 "chrome/browser/ui/android/ssl_client_certificate_request.h" 5 #include "chrome/browser/ui/android/ssl_client_certificate_request.h"
6 6
7 #include "base/android/jni_array.h" 7 #include "base/android/jni_array.h"
8 #include "base/android/jni_string.h" 8 #include "base/android/jni_string.h"
9 #include "base/android/scoped_java_ref.h" 9 #include "base/android/scoped_java_ref.h"
10 #include "base/basictypes.h" 10 #include "base/basictypes.h"
11 #include "base/bind.h" 11 #include "base/bind.h"
12 #include "base/callback_helpers.h" 12 #include "base/callback_helpers.h"
13 #include "base/compiler_specific.h" 13 #include "base/compiler_specific.h"
14 #include "base/logging.h" 14 #include "base/logging.h"
15 #include "chrome/browser/ssl/ssl_client_certificate_selector.h" 15 #include "chrome/browser/ssl/ssl_client_certificate_selector.h"
16 #include "chrome/browser/ui/android/window_android_helper.h" 16 #include "chrome/browser/ui/android/window_android_helper.h"
17 #include "content/public/browser/browser_thread.h" 17 #include "content/public/browser/browser_thread.h"
18 #include "content/public/browser/client_certificate_delegate.h"
18 #include "crypto/scoped_openssl_types.h" 19 #include "crypto/scoped_openssl_types.h"
19 #include "jni/SSLClientCertificateRequest_jni.h" 20 #include "jni/SSLClientCertificateRequest_jni.h"
20 #include "net/android/keystore_openssl.h" 21 #include "net/android/keystore_openssl.h"
21 #include "net/base/host_port_pair.h" 22 #include "net/base/host_port_pair.h"
22 #include "net/cert/cert_database.h" 23 #include "net/cert/cert_database.h"
23 #include "net/cert/x509_certificate.h" 24 #include "net/cert/x509_certificate.h"
24 #include "net/ssl/openssl_client_key_store.h" 25 #include "net/ssl/openssl_client_key_store.h"
25 #include "net/ssl/ssl_cert_request_info.h" 26 #include "net/ssl/ssl_cert_request_info.h"
26 #include "net/ssl/ssl_client_cert_type.h" 27 #include "net/ssl/ssl_client_cert_type.h"
27 #include "ui/base/android/window_android.h" 28 #include "ui/base/android/window_android.h"
28 29
29 30
30 namespace chrome { 31 namespace chrome {
31 32
32 namespace { 33 namespace {
33 34
34 // Must be called on the I/O thread to record a client certificate 35 // Must be called on the I/O thread to record a client certificate
35 // and its private key in the OpenSSLClientKeyStore. 36 // and its private key in the OpenSSLClientKeyStore.
36 void RecordClientCertificateKey( 37 void RecordClientCertificateKey(
37 const scoped_refptr<net::X509Certificate>& client_cert, 38 const scoped_refptr<net::X509Certificate>& client_cert,
38 crypto::ScopedEVP_PKEY private_key) { 39 crypto::ScopedEVP_PKEY private_key) {
39 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); 40 DCHECK_CURRENTLY_ON(content::BrowserThread::IO);
40 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey( 41 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey(
41 client_cert.get(), private_key.get()); 42 client_cert.get(), private_key.get());
42 } 43 }
43 44
44 void StartClientCertificateRequest( 45 void StartClientCertificateRequest(
45 const net::SSLCertRequestInfo* cert_request_info, 46 const net::SSLCertRequestInfo* cert_request_info,
46 ui::WindowAndroid* window, 47 ui::WindowAndroid* window,
47 const chrome::SelectCertificateCallback& callback) { 48 scoped_ptr<content::ClientCertificateDelegate> delegate) {
48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 49 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
49 50
50 // Ensure that callback(NULL) is posted as a task on the UI thread 51 // Ensure that ContinueWithCertificate(nullptr) is called in case of an error.
51 // in case of an error. 52 base::ScopedClosureRunner guard(base::Bind(
52 base::Closure post_task_closure = base::Bind( 53 &content::ClientCertificateDelegate::ContinueWithCertificate,
53 base::IgnoreResult(&content::BrowserThread::PostTask), 54 base::Unretained(delegate.get()), scoped_refptr<net::X509Certificate>()));
54 content::BrowserThread::UI,
55 FROM_HERE,
56 base::Bind(callback, scoped_refptr<net::X509Certificate>()));
57
58 base::ScopedClosureRunner guard(post_task_closure);
59 55
60 // Build the |key_types| JNI parameter, as a String[] 56 // Build the |key_types| JNI parameter, as a String[]
61 std::vector<std::string> key_types; 57 std::vector<std::string> key_types;
62 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) { 58 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) {
63 switch (cert_request_info->cert_key_types[n]) { 59 switch (cert_request_info->cert_key_types[n]) {
64 case net::CLIENT_CERT_RSA_SIGN: 60 case net::CLIENT_CERT_RSA_SIGN:
65 key_types.push_back("RSA"); 61 key_types.push_back("RSA");
66 break; 62 break;
67 case net::CLIENT_CERT_DSS_SIGN: 63 case net::CLIENT_CERT_DSS_SIGN:
68 key_types.push_back("DSA"); 64 key_types.push_back("DSA");
(...skipping 23 matching lines...) Expand all
92 LOG(ERROR) << "Could not create principals array (byte[][])"; 88 LOG(ERROR) << "Could not create principals array (byte[][])";
93 return; 89 return;
94 } 90 }
95 91
96 // Build the |host_name| and |port| JNI parameters, as a String and 92 // Build the |host_name| and |port| JNI parameters, as a String and
97 // a jint. 93 // a jint.
98 ScopedJavaLocalRef<jstring> host_name_ref = 94 ScopedJavaLocalRef<jstring> host_name_ref =
99 base::android::ConvertUTF8ToJavaString( 95 base::android::ConvertUTF8ToJavaString(
100 env, cert_request_info->host_and_port.host()); 96 env, cert_request_info->host_and_port.host());
101 97
102 // Create a copy of the callback on the heap so that its address 98 // Pass the address of the delegate through to Java.
103 // and ownership can be passed through and returned from Java via JNI. 99 jlong request_id = reinterpret_cast<intptr_t>(delegate.get());
104 scoped_ptr<chrome::SelectCertificateCallback> request(
105 new chrome::SelectCertificateCallback(callback));
106
107 jlong request_id = reinterpret_cast<intptr_t>(request.get());
108 100
109 if (!chrome::android:: 101 if (!chrome::android::
110 Java_SSLClientCertificateRequest_selectClientCertificate( 102 Java_SSLClientCertificateRequest_selectClientCertificate(
111 env, 103 env,
112 request_id, 104 request_id,
113 window->GetJavaObject().obj(), 105 window->GetJavaObject().obj(),
114 key_types_ref.obj(), 106 key_types_ref.obj(),
115 principals_ref.obj(), 107 principals_ref.obj(),
116 host_name_ref.obj(), 108 host_name_ref.obj(),
117 cert_request_info->host_and_port.port())) { 109 cert_request_info->host_and_port.port())) {
118 return; 110 return;
119 } 111 }
120 112
121 ignore_result(guard.Release()); 113 ignore_result(guard.Release());
122 114
123 // Ownership was transferred to Java. 115 // Ownership was transferred to Java.
124 ignore_result(request.release()); 116 ignore_result(delegate.release());
125 } 117 }
126 118
127 } // namespace 119 } // namespace
128 120
129 namespace android { 121 namespace android {
130 122
131 // Called from JNI on request completion/result. 123 // Called from JNI on request completion/result.
132 // |env| is the current thread's JNIEnv. 124 // |env| is the current thread's JNIEnv.
133 // |clazz| is the SSLClientCertificateRequest JNI class reference. 125 // |clazz| is the SSLClientCertificateRequest JNI class reference.
134 // |request_id| is the id passed to 126 // |request_id| is the id passed to
135 // Java_SSLClientCertificateRequest_selectClientCertificate() in Start(). 127 // Java_SSLClientCertificateRequest_selectClientCertificate() in Start().
136 // |encoded_chain_ref| is a JNI reference to a Java array of byte arrays, 128 // |encoded_chain_ref| is a JNI reference to a Java array of byte arrays,
137 // each item holding a DER-encoded X.509 certificate. 129 // each item holding a DER-encoded X.509 certificate.
138 // |private_key_ref| is the platform PrivateKey object JNI reference for 130 // |private_key_ref| is the platform PrivateKey object JNI reference for
139 // the client certificate. 131 // the client certificate.
140 // Note: both |encoded_chain_ref| and |private_key_ref| will be NULL if 132 // Note: both |encoded_chain_ref| and |private_key_ref| will be NULL if
141 // the user didn't select a certificate. 133 // the user didn't select a certificate.
142 static void OnSystemRequestCompletion( 134 static void OnSystemRequestCompletion(
143 JNIEnv* env, 135 JNIEnv* env,
144 jclass clazz, 136 jclass clazz,
145 jlong request_id, 137 jlong request_id,
146 jobjectArray encoded_chain_ref, 138 jobjectArray encoded_chain_ref,
147 jobject private_key_ref) { 139 jobject private_key_ref) {
148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 140 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
149 141
150 // Take back ownership of the request object. 142 // Take back ownership of the delegate object.
151 scoped_ptr<chrome::SelectCertificateCallback> callback( 143 scoped_ptr<content::ClientCertificateDelegate> delegate(
152 reinterpret_cast<chrome::SelectCertificateCallback*>(request_id)); 144 reinterpret_cast<content::ClientCertificateDelegate*>(request_id));
153 145
154 // Ensure that callback(NULL) is called in case of an error. 146 // Ensure that ContinueWithCertificate(nullptr) is called in case of an error.
155 base::Closure null_closure = 147 base::ScopedClosureRunner guard(base::Bind(
156 base::Bind(*callback, scoped_refptr<net::X509Certificate>()); 148 &content::ClientCertificateDelegate::ContinueWithCertificate,
157 149 base::Unretained(delegate.get()), scoped_refptr<net::X509Certificate>()));
158 base::ScopedClosureRunner guard(null_closure);
159 150
160 if (encoded_chain_ref == NULL || private_key_ref == NULL) { 151 if (encoded_chain_ref == NULL || private_key_ref == NULL) {
161 LOG(ERROR) << "Client certificate request cancelled"; 152 LOG(ERROR) << "Client certificate request cancelled";
162 return; 153 return;
163 } 154 }
164 155
165 // Convert the encoded chain to a vector of strings. 156 // Convert the encoded chain to a vector of strings.
166 std::vector<std::string> encoded_chain_strings; 157 std::vector<std::string> encoded_chain_strings;
167 if (encoded_chain_ref) { 158 if (encoded_chain_ref) {
168 base::android::JavaArrayOfByteArrayToStringVector( 159 base::android::JavaArrayOfByteArrayToStringVector(
(...skipping 19 matching lines...) Expand all
188 LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; 179 LOG(ERROR) << "Could not create OpenSSL wrapper for private key";
189 return; 180 return;
190 } 181 }
191 182
192 ignore_result(guard.Release()); 183 ignore_result(guard.Release());
193 184
194 // RecordClientCertificateKey() must be called on the I/O thread, 185 // RecordClientCertificateKey() must be called on the I/O thread,
195 // before the callback is called with the selected certificate on 186 // before the callback is called with the selected certificate on
196 // the UI thread. 187 // the UI thread.
197 content::BrowserThread::PostTaskAndReply( 188 content::BrowserThread::PostTaskAndReply(
198 content::BrowserThread::IO, 189 content::BrowserThread::IO, FROM_HERE,
199 FROM_HERE, 190 base::Bind(&RecordClientCertificateKey, client_cert,
200 base::Bind(&RecordClientCertificateKey,
201 client_cert,
202 base::Passed(&private_key)), 191 base::Passed(&private_key)),
203 base::Bind(*callback, client_cert)); 192 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate,
193 base::Owned(delegate.release()), client_cert));
204 } 194 }
205 195
206 static void NotifyClientCertificatesChanged() { 196 static void NotifyClientCertificatesChanged() {
207 net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged(); 197 net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged();
208 } 198 }
209 199
210 static void NotifyClientCertificatesChangedOnIOThread(JNIEnv* env, jclass) { 200 static void NotifyClientCertificatesChangedOnIOThread(JNIEnv* env, jclass) {
211 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) { 201 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) {
212 NotifyClientCertificatesChanged(); 202 NotifyClientCertificatesChanged();
213 } else { 203 } else {
214 content::BrowserThread::PostTask( 204 content::BrowserThread::PostTask(
215 content::BrowserThread::IO, 205 content::BrowserThread::IO,
216 FROM_HERE, 206 FROM_HERE,
217 base::Bind(&NotifyClientCertificatesChanged)); 207 base::Bind(&NotifyClientCertificatesChanged));
218 } 208 }
219 } 209 }
220 210
221 bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) { 211 bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) {
222 return RegisterNativesImpl(env); 212 return RegisterNativesImpl(env);
223 } 213 }
224 214
225 } // namespace android 215 } // namespace android
226 216
227 void ShowSSLClientCertificateSelector( 217 void ShowSSLClientCertificateSelector(
228 content::WebContents* contents, 218 content::WebContents* contents,
229 net::SSLCertRequestInfo* cert_request_info, 219 net::SSLCertRequestInfo* cert_request_info,
230 const chrome::SelectCertificateCallback& callback) { 220 scoped_ptr<content::ClientCertificateDelegate> delegate) {
231 ui::WindowAndroid* window = 221 ui::WindowAndroid* window =
232 WindowAndroidHelper::FromWebContents(contents)->GetWindowAndroid(); 222 WindowAndroidHelper::FromWebContents(contents)->GetWindowAndroid();
233 DCHECK(window); 223 DCHECK(window);
234 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); 224 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
235 StartClientCertificateRequest(cert_request_info, window, callback); 225 StartClientCertificateRequest(cert_request_info, window, delegate.Pass());
236 } 226 }
237 227
238 } // namespace chrome 228 } // namespace chrome
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698