| 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 "chrome/browser/ui/android/ssl_client_certificate_request.h" | 5 #include "chrome/browser/ui/android/ssl_client_certificate_request.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <utility> | 8 #include <utility> |
| 9 | 9 |
| 10 #include "base/android/jni_array.h" | 10 #include "base/android/jni_array.h" |
| 11 #include "base/android/jni_string.h" | 11 #include "base/android/jni_string.h" |
| 12 #include "base/android/scoped_java_ref.h" | 12 #include "base/android/scoped_java_ref.h" |
| 13 #include "base/bind.h" | 13 #include "base/bind.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/logging.h" | 15 #include "base/logging.h" |
| 16 #include "base/memory/ref_counted.h" | 16 #include "base/memory/ref_counted.h" |
| 17 #include "chrome/browser/ssl/ssl_client_certificate_selector.h" | 17 #include "chrome/browser/ssl/ssl_client_certificate_selector.h" |
| 18 #include "chrome/browser/ui/android/view_android_helper.h" | 18 #include "chrome/browser/ui/android/view_android_helper.h" |
| 19 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 20 #include "content/public/browser/client_certificate_delegate.h" | 20 #include "content/public/browser/client_certificate_delegate.h" |
| 21 #include "jni/SSLClientCertificateRequest_jni.h" | 21 #include "jni/SSLClientCertificateRequest_jni.h" |
| 22 #include "net/base/host_port_pair.h" | 22 #include "net/base/host_port_pair.h" |
| 23 #include "net/cert/cert_database.h" | 23 #include "net/cert/cert_database.h" |
| 24 #include "net/cert/x509_certificate.h" | 24 #include "net/cert/x509_certificate.h" |
| 25 #include "net/ssl/openssl_client_key_store.h" | |
| 26 #include "net/ssl/ssl_cert_request_info.h" | 25 #include "net/ssl/ssl_cert_request_info.h" |
| 27 #include "net/ssl/ssl_client_cert_type.h" | 26 #include "net/ssl/ssl_client_cert_type.h" |
| 28 #include "net/ssl/ssl_platform_key_android.h" | 27 #include "net/ssl/ssl_platform_key_android.h" |
| 29 #include "net/ssl/ssl_private_key.h" | 28 #include "net/ssl/ssl_private_key.h" |
| 30 #include "ui/android/view_android.h" | 29 #include "ui/android/view_android.h" |
| 31 #include "ui/android/window_android.h" | 30 #include "ui/android/window_android.h" |
| 32 | 31 |
| 33 using base::android::JavaParamRef; | 32 using base::android::JavaParamRef; |
| 34 using base::android::ScopedJavaLocalRef; | 33 using base::android::ScopedJavaLocalRef; |
| 35 | 34 |
| 36 namespace chrome { | 35 namespace chrome { |
| 37 | 36 |
| 38 namespace { | 37 namespace { |
| 39 | 38 |
| 40 // Must be called on the I/O thread to record a client certificate | |
| 41 // and its private key in the OpenSSLClientKeyStore. | |
| 42 void RecordClientCertificateKey(net::X509Certificate* client_cert, | |
| 43 scoped_refptr<net::SSLPrivateKey> private_key) { | |
| 44 DCHECK_CURRENTLY_ON(content::BrowserThread::IO); | |
| 45 net::OpenSSLClientKeyStore::GetInstance()->RecordClientCertPrivateKey( | |
| 46 client_cert, std::move(private_key)); | |
| 47 } | |
| 48 | |
| 49 void StartClientCertificateRequest( | 39 void StartClientCertificateRequest( |
| 50 const net::SSLCertRequestInfo* cert_request_info, | 40 const net::SSLCertRequestInfo* cert_request_info, |
| 51 ui::WindowAndroid* window, | 41 ui::WindowAndroid* window, |
| 52 std::unique_ptr<content::ClientCertificateDelegate> delegate) { | 42 std::unique_ptr<content::ClientCertificateDelegate> delegate) { |
| 53 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 43 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 54 | 44 |
| 55 // Build the |key_types| JNI parameter, as a String[] | 45 // Build the |key_types| JNI parameter, as a String[] |
| 56 std::vector<std::string> key_types; | 46 std::vector<std::string> key_types; |
| 57 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) { | 47 for (size_t n = 0; n < cert_request_info->cert_key_types.size(); ++n) { |
| 58 switch (cert_request_info->cert_key_types[n]) { | 48 switch (cert_request_info->cert_key_types[n]) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 128 const JavaParamRef<jobjectArray>& encoded_chain_ref, | 118 const JavaParamRef<jobjectArray>& encoded_chain_ref, |
| 129 const JavaParamRef<jobject>& private_key_ref) { | 119 const JavaParamRef<jobject>& private_key_ref) { |
| 130 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 120 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 131 | 121 |
| 132 // Take back ownership of the delegate object. | 122 // Take back ownership of the delegate object. |
| 133 std::unique_ptr<content::ClientCertificateDelegate> delegate( | 123 std::unique_ptr<content::ClientCertificateDelegate> delegate( |
| 134 reinterpret_cast<content::ClientCertificateDelegate*>(request_id)); | 124 reinterpret_cast<content::ClientCertificateDelegate*>(request_id)); |
| 135 | 125 |
| 136 if (encoded_chain_ref == NULL || private_key_ref == NULL) { | 126 if (encoded_chain_ref == NULL || private_key_ref == NULL) { |
| 137 LOG(ERROR) << "No client certificate selected"; | 127 LOG(ERROR) << "No client certificate selected"; |
| 138 delegate->ContinueWithCertificate(nullptr); | 128 delegate->ContinueWithCertificate(nullptr, nullptr); |
| 139 return; | 129 return; |
| 140 } | 130 } |
| 141 | 131 |
| 142 // Convert the encoded chain to a vector of strings. | 132 // Convert the encoded chain to a vector of strings. |
| 143 std::vector<std::string> encoded_chain_strings; | 133 std::vector<std::string> encoded_chain_strings; |
| 144 if (encoded_chain_ref) { | 134 if (encoded_chain_ref) { |
| 145 base::android::JavaArrayOfByteArrayToStringVector( | 135 base::android::JavaArrayOfByteArrayToStringVector( |
| 146 env, encoded_chain_ref, &encoded_chain_strings); | 136 env, encoded_chain_ref, &encoded_chain_strings); |
| 147 } | 137 } |
| 148 | 138 |
| (...skipping 10 matching lines...) Expand all Loading... |
| 159 } | 149 } |
| 160 | 150 |
| 161 // Create an SSLPrivateKey wrapper for the private key JNI reference. | 151 // Create an SSLPrivateKey wrapper for the private key JNI reference. |
| 162 scoped_refptr<net::SSLPrivateKey> private_key = | 152 scoped_refptr<net::SSLPrivateKey> private_key = |
| 163 net::WrapJavaPrivateKey(client_cert.get(), private_key_ref); | 153 net::WrapJavaPrivateKey(client_cert.get(), private_key_ref); |
| 164 if (!private_key) { | 154 if (!private_key) { |
| 165 LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; | 155 LOG(ERROR) << "Could not create OpenSSL wrapper for private key"; |
| 166 return; | 156 return; |
| 167 } | 157 } |
| 168 | 158 |
| 169 // RecordClientCertificateKey() must be called on the I/O thread, | 159 delegate->ContinueWithCertificate(std::move(client_cert), |
| 170 // before the callback is called with the selected certificate on | 160 std::move(private_key)); |
| 171 // the UI thread. | |
| 172 content::BrowserThread::PostTaskAndReply( | |
| 173 content::BrowserThread::IO, FROM_HERE, | |
| 174 base::Bind(&RecordClientCertificateKey, base::RetainedRef(client_cert), | |
| 175 base::Passed(&private_key)), | |
| 176 base::Bind(&content::ClientCertificateDelegate::ContinueWithCertificate, | |
| 177 base::Owned(delegate.release()), | |
| 178 base::RetainedRef(client_cert))); | |
| 179 } | 161 } |
| 180 | 162 |
| 181 static void NotifyClientCertificatesChanged() { | 163 static void NotifyClientCertificatesChanged() { |
| 182 net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged(); | 164 net::CertDatabase::GetInstance()->OnAndroidKeyStoreChanged(); |
| 183 } | 165 } |
| 184 | 166 |
| 185 static void NotifyClientCertificatesChangedOnIOThread( | 167 static void NotifyClientCertificatesChangedOnIOThread( |
| 186 JNIEnv* env, | 168 JNIEnv* env, |
| 187 const JavaParamRef<jclass>&) { | 169 const JavaParamRef<jclass>&) { |
| 188 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) { | 170 if (content::BrowserThread::CurrentlyOn(content::BrowserThread::IO)) { |
| 189 NotifyClientCertificatesChanged(); | 171 NotifyClientCertificatesChanged(); |
| 190 } else { | 172 } else { |
| 191 content::BrowserThread::PostTask( | 173 content::BrowserThread::PostTask( |
| 192 content::BrowserThread::IO, | 174 content::BrowserThread::IO, |
| 193 FROM_HERE, | 175 FROM_HERE, |
| 194 base::Bind(&NotifyClientCertificatesChanged)); | 176 base::Bind(&NotifyClientCertificatesChanged)); |
| 195 } | 177 } |
| 196 } | 178 } |
| 197 | 179 |
| 198 bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) { | 180 bool RegisterSSLClientCertificateRequestAndroid(JNIEnv* env) { |
| 199 return RegisterNativesImpl(env); | 181 return RegisterNativesImpl(env); |
| 200 } | 182 } |
| 201 | 183 |
| 202 } // namespace android | 184 } // namespace android |
| 203 | 185 |
| 204 void ShowSSLClientCertificateSelector( | 186 void ShowSSLClientCertificateSelector( |
| 205 content::WebContents* contents, | 187 content::WebContents* contents, |
| 206 net::SSLCertRequestInfo* cert_request_info, | 188 net::SSLCertRequestInfo* cert_request_info, |
| 207 net::CertificateList unused_client_certs, | 189 net::ClientCertIdentityList unused_client_certs, |
| 208 std::unique_ptr<content::ClientCertificateDelegate> delegate) { | 190 std::unique_ptr<content::ClientCertificateDelegate> delegate) { |
| 209 ui::WindowAndroid* window = ViewAndroidHelper::FromWebContents(contents) | 191 ui::WindowAndroid* window = ViewAndroidHelper::FromWebContents(contents) |
| 210 ->GetViewAndroid()->GetWindowAndroid(); | 192 ->GetViewAndroid()->GetWindowAndroid(); |
| 211 DCHECK(window); | 193 DCHECK(window); |
| 212 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 194 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 213 StartClientCertificateRequest(cert_request_info, window, std::move(delegate)); | 195 StartClientCertificateRequest(cert_request_info, window, std::move(delegate)); |
| 214 } | 196 } |
| 215 | 197 |
| 216 } // namespace chrome | 198 } // namespace chrome |
| OLD | NEW |