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