Chromium Code Reviews| OLD | NEW |
|---|---|
| 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; | |
|
wtc
2014/04/22 22:47:16
Nit: we have crypto/scoped_nss_types.h for similar
| |
| 32 | |
| 33 namespace { | |
|
wtc
2014/04/22 22:47:16
Nit: add a blank line after this line.
sgurun-gerrit only
2014/04/23 00:50:47
Done.
| |
| 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) { | |
|
wtc
2014/04/22 22:47:16
Nit: the for loop index is usually named |i| by co
sgurun-gerrit only
2014/04/23 00:50:47
Done.
| |
| 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 DCHECK(callback); | |
| 198 | |
| 199 // Make sure callback is run on error. | |
| 200 base::ScopedClosureRunner guard(base::Bind( | |
| 201 &AwContentsClientBridge::HandleErrorInClientCertificateResponse, | |
| 202 base::Unretained(this), | |
| 203 request_id)); | |
| 204 if (encoded_chain_ref == NULL || private_key_ref == NULL) { | |
| 205 LOG(ERROR) << "Client certificate request cancelled"; | |
| 206 return; | |
| 207 } | |
| 208 // Convert the encoded chain to a vector of strings. | |
| 209 std::vector<std::string> encoded_chain_strings; | |
| 210 if (encoded_chain_ref) { | |
| 211 base::android::JavaArrayOfByteArrayToStringVector( | |
| 212 env, encoded_chain_ref, &encoded_chain_strings); | |
| 213 } | |
| 214 | |
| 215 std::vector<base::StringPiece> encoded_chain; | |
| 216 for (size_t n = 0; n < encoded_chain_strings.size(); ++n) | |
| 217 encoded_chain.push_back(encoded_chain_strings[n]); | |
| 218 | |
| 219 // Create the X509Certificate object from the encoded chain. | |
| 220 scoped_refptr<net::X509Certificate> client_cert( | |
| 221 net::X509Certificate::CreateFromDERCertChain(encoded_chain)); | |
| 222 if (!client_cert.get()) { | |
| 223 LOG(ERROR) << "Could not decode client certificate chain"; | |
| 224 return; | |
| 225 } | |
| 226 | |
| 227 // Create an EVP_PKEY wrapper for the private key JNI reference. | |
| 228 ScopedEVP_PKEY private_key( | |
| 229 net::android::GetOpenSSLPrivateKeyWrapper(private_key_ref)); | |
| 230 if (!private_key.get()) { | |
| 231 LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; | |
| 232 return; | |
| 233 } | |
| 234 | |
| 235 // RecordClientCertificateKey() must be called on the I/O thread, | |
| 236 // before the callback is called with the selected certificate on | |
| 237 // the UI thread. | |
| 238 content::BrowserThread::PostTaskAndReply( | |
| 239 content::BrowserThread::IO, | |
| 240 FROM_HERE, | |
| 241 base::Bind(&RecordClientCertificateKey, | |
| 242 client_cert, | |
| 243 base::Passed(&private_key)), | |
| 244 base::Bind(*callback, client_cert)); | |
| 245 pending_client_cert_request_callbacks_.Remove(request_id); | |
| 246 | |
| 247 // Release the guard. | |
| 248 ignore_result(guard.Release()); | |
| 249 } | |
| 250 | |
| 91 void AwContentsClientBridge::RunJavaScriptDialog( | 251 void AwContentsClientBridge::RunJavaScriptDialog( |
| 92 content::JavaScriptMessageType message_type, | 252 content::JavaScriptMessageType message_type, |
| 93 const GURL& origin_url, | 253 const GURL& origin_url, |
| 94 const base::string16& message_text, | 254 const base::string16& message_text, |
| 95 const base::string16& default_prompt_text, | 255 const base::string16& default_prompt_text, |
| 96 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { | 256 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 257 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 JNIEnv* env = AttachCurrentThread(); | 258 JNIEnv* env = AttachCurrentThread(); |
| 99 | 259 |
| 100 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 260 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| (...skipping 98 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 content::JavaScriptDialogManager::DialogClosedCallback* callback = | 359 content::JavaScriptDialogManager::DialogClosedCallback* callback = |
| 200 pending_js_dialog_callbacks_.Lookup(id); | 360 pending_js_dialog_callbacks_.Lookup(id); |
| 201 if (!callback) { | 361 if (!callback) { |
| 202 LOG(WARNING) << "Unexpected JS dialog cancel. " << id; | 362 LOG(WARNING) << "Unexpected JS dialog cancel. " << id; |
| 203 return; | 363 return; |
| 204 } | 364 } |
| 205 callback->Run(false, base::string16()); | 365 callback->Run(false, base::string16()); |
| 206 pending_js_dialog_callbacks_.Remove(id); | 366 pending_js_dialog_callbacks_.Remove(id); |
| 207 } | 367 } |
| 208 | 368 |
| 369 // Use to cleanup if there is an error in client certificate response. | |
| 370 void AwContentsClientBridge::HandleErrorInClientCertificateResponse( | |
| 371 int request_id) { | |
| 372 SelectCertificateCallback* callback = | |
| 373 pending_client_cert_request_callbacks_.Lookup(request_id); | |
| 374 callback->Run(scoped_refptr<net::X509Certificate>()); | |
| 375 pending_client_cert_request_callbacks_.Remove(request_id); | |
| 376 } | |
| 377 | |
| 209 bool RegisterAwContentsClientBridge(JNIEnv* env) { | 378 bool RegisterAwContentsClientBridge(JNIEnv* env) { |
| 210 return RegisterNativesImpl(env) >= 0; | 379 return RegisterNativesImpl(env) >= 0; |
| 211 } | 380 } |
| 212 | 381 |
| 213 } // namespace android_webview | 382 } // namespace android_webview |
| OLD | NEW |