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; | |
| 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 almost same as SelectClientCertificate() in | |
|
boliu
2014/04/18 17:10:12
inspired by?
sgurun-gerrit only
2014/04/19 01:28:50
Done.
| |
| 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 // Ensure that the callback runs on any error case. | |
| 118 base::ScopedClosureRunner guard(base::Bind( | |
| 119 callback, scoped_refptr<net::X509Certificate>())); | |
|
boliu
2014/04/18 17:10:12
Insert the callback into the map here and re-use H
sgurun-gerrit only
2014/04/19 01:28:50
Done.
| |
| 120 | |
| 121 JNIEnv* env = base::android::AttachCurrentThread(); | |
| 122 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | |
| 123 if (obj.is_null()) | |
| 124 return; | |
| 125 | |
| 126 // Build the |key_types| JNI parameter, as a String[] | |
| 127 std::vector<std::string> key_types; | |
| 128 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) { | |
| 129 switch (cert_request_info->cert_key_types[n]) { | |
| 130 case net::CLIENT_CERT_RSA_SIGN: | |
| 131 key_types.push_back("RSA"); | |
| 132 break; | |
| 133 case net::CLIENT_CERT_DSS_SIGN: | |
| 134 key_types.push_back("DSA"); | |
| 135 break; | |
| 136 case net::CLIENT_CERT_ECDSA_SIGN: | |
| 137 key_types.push_back("ECDSA"); | |
| 138 break; | |
| 139 default: | |
| 140 // Ignore unknown types. | |
| 141 break; | |
| 142 } | |
| 143 } | |
| 144 | |
| 145 ScopedJavaLocalRef<jobjectArray> key_types_ref = | |
| 146 base::android::ToJavaArrayOfStrings(env, key_types); | |
| 147 if (key_types_ref.is_null()) { | |
| 148 LOG(ERROR) << "Could not create key types array (String[])"; | |
| 149 return; | |
| 150 } | |
| 151 | |
| 152 // Build the |encoded_principals| JNI parameter, as a byte[][] | |
| 153 ScopedJavaLocalRef<jobjectArray> principals_ref = | |
| 154 base::android::ToJavaArrayOfByteArray( | |
| 155 env, cert_request_info->cert_authorities); | |
| 156 if (principals_ref.is_null()) { | |
| 157 LOG(ERROR) << "Could not create principals array (byte[][])"; | |
| 158 return; | |
| 159 } | |
| 160 | |
| 161 // Build the |host_name| and |port| JNI parameters, as a String and | |
| 162 // a jint. | |
| 163 ScopedJavaLocalRef<jstring> host_name_ref = | |
| 164 base::android::ConvertUTF8ToJavaString( | |
| 165 env, cert_request_info->host_and_port.host()); | |
| 166 | |
| 167 // Add the callback to id map. | |
| 168 int request_id = pending_client_cert_request_callbacks_.Add( | |
| 169 new SelectCertificateCallback(callback)); | |
| 170 | |
| 171 // Release the guard. Java will call the callback. | |
| 172 ignore_result(guard.Release()); | |
|
boliu
2014/04/18 17:10:12
Keep this below the java call (to prevent someone
sgurun-gerrit only
2014/04/19 01:28:50
Done.
| |
| 173 | |
| 174 Java_AwContentsClientBridge_selectClientCertificate( | |
| 175 env, | |
| 176 obj.obj(), | |
| 177 request_id, | |
| 178 key_types_ref.obj(), | |
| 179 principals_ref.obj(), | |
| 180 host_name_ref.obj(), | |
| 181 cert_request_info->host_and_port.port()); | |
| 182 } | |
| 183 | |
| 184 // Use to cleanup if there is an error in client certificate response. | |
| 185 void AwContentsClientBridge::HandleErrorInClientCertificateResponse( | |
| 186 int request_id) { | |
| 187 SelectCertificateCallback* callback = | |
| 188 pending_client_cert_request_callbacks_.Lookup(request_id); | |
| 189 callback->Run(scoped_refptr<net::X509Certificate>()); | |
| 190 pending_client_cert_request_callbacks_.Remove(request_id); | |
| 191 } | |
| 192 | |
| 193 // This method is almost same as OnSystemRequestCompletion() in | |
| 194 // chrome/browser/ui/android/ssl_client_certificate_request.cc | |
| 195 void AwContentsClientBridge::ProvideClientCertificateResponse( | |
| 196 JNIEnv* env, | |
| 197 jobject obj, | |
| 198 int request_id, | |
| 199 jobjectArray encoded_chain_ref, | |
| 200 jobject private_key_ref) { | |
| 201 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 202 | |
| 203 SelectCertificateCallback* callback = | |
| 204 pending_client_cert_request_callbacks_.Lookup(request_id); | |
| 205 if (!callback || callback->is_null()) { | |
| 206 LOG(WARNING) << "Ignoring unexpected client certificate response callback"; | |
| 207 return; | |
| 208 } | |
| 209 // Make sure callback is run on error. | |
| 210 base::ScopedClosureRunner guard(base::Bind( | |
| 211 &AwContentsClientBridge::HandleErrorInClientCertificateResponse, | |
| 212 base::Unretained(this), | |
| 213 request_id)); | |
| 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()); | |
|
boliu
2014/04/18 17:10:12
Again, move below PostTaskAndReply
sgurun-gerrit only
2014/04/19 01:28:50
Done.
| |
| 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 pending_client_cert_request_callbacks_.Remove(request_id); | |
| 258 } | |
| 259 | |
| 91 void AwContentsClientBridge::RunJavaScriptDialog( | 260 void AwContentsClientBridge::RunJavaScriptDialog( |
| 92 content::JavaScriptMessageType message_type, | 261 content::JavaScriptMessageType message_type, |
| 93 const GURL& origin_url, | 262 const GURL& origin_url, |
| 94 const base::string16& message_text, | 263 const base::string16& message_text, |
| 95 const base::string16& default_prompt_text, | 264 const base::string16& default_prompt_text, |
| 96 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { | 265 const content::JavaScriptDialogManager::DialogClosedCallback& callback) { |
| 97 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 266 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 98 JNIEnv* env = AttachCurrentThread(); | 267 JNIEnv* env = AttachCurrentThread(); |
| 99 | 268 |
| 100 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); | 269 ScopedJavaLocalRef<jobject> obj = java_ref_.get(env); |
| (...skipping 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 204 } | 373 } |
| 205 callback->Run(false, base::string16()); | 374 callback->Run(false, base::string16()); |
| 206 pending_js_dialog_callbacks_.Remove(id); | 375 pending_js_dialog_callbacks_.Remove(id); |
| 207 } | 376 } |
| 208 | 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 |