| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "content/browser/ssl/ssl_client_auth_handler.h" | 5 #include "content/browser/ssl/ssl_client_auth_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "content/browser/loader/resource_dispatcher_host_impl.h" | 8 #include "content/browser/loader/resource_dispatcher_host_impl.h" |
| 9 #include "content/browser/loader/resource_request_info_impl.h" | 9 #include "content/browser/loader/resource_request_info_impl.h" |
| 10 #include "content/public/browser/browser_thread.h" | 10 #include "content/public/browser/browser_thread.h" |
| 11 #include "content/public/browser/content_browser_client.h" | 11 #include "content/public/browser/content_browser_client.h" |
| 12 #include "net/cert/x509_certificate.h" | 12 #include "net/cert/x509_certificate.h" |
| 13 #include "net/http/http_transaction_factory.h" | 13 #include "net/http/http_transaction_factory.h" |
| 14 #include "net/ssl/client_cert_store.h" |
| 14 #include "net/url_request/url_request.h" | 15 #include "net/url_request/url_request.h" |
| 15 #include "net/url_request/url_request_context.h" | 16 #include "net/url_request/url_request_context.h" |
| 16 | 17 |
| 17 namespace content { | 18 namespace content { |
| 18 | 19 |
| 19 SSLClientAuthHandler::SSLClientAuthHandler( | 20 SSLClientAuthHandler::SSLClientAuthHandler( |
| 21 scoped_ptr<net::ClientCertStore> client_cert_store, |
| 20 net::URLRequest* request, | 22 net::URLRequest* request, |
| 21 net::SSLCertRequestInfo* cert_request_info) | 23 net::SSLCertRequestInfo* cert_request_info) |
| 22 : request_(request), | 24 : request_(request), |
| 23 http_network_session_( | 25 http_network_session_( |
| 24 request_->context()->http_transaction_factory()->GetSession()), | 26 request_->context()->http_transaction_factory()->GetSession()), |
| 25 cert_request_info_(cert_request_info) { | 27 cert_request_info_(cert_request_info), |
| 28 client_cert_store_(client_cert_store.Pass()) { |
| 26 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 29 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 27 } | 30 } |
| 28 | 31 |
| 29 SSLClientAuthHandler::~SSLClientAuthHandler() { | 32 SSLClientAuthHandler::~SSLClientAuthHandler() { |
| 30 // If we were simply dropped, then act as if we selected no certificate. | 33 // If we were simply dropped, then act as if we selected no certificate. |
| 31 DoCertificateSelected(NULL); | 34 DoCertificateSelected(NULL); |
| 32 } | 35 } |
| 33 | 36 |
| 34 void SSLClientAuthHandler::OnRequestCancelled() { | 37 void SSLClientAuthHandler::OnRequestCancelled() { |
| 35 request_ = NULL; | 38 request_ = NULL; |
| 36 } | 39 } |
| 37 | 40 |
| 38 void SSLClientAuthHandler::SelectCertificate() { | 41 void SSLClientAuthHandler::SelectCertificate() { |
| 39 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 42 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 40 DCHECK(request_); | 43 DCHECK(request_); |
| 41 | 44 |
| 45 if (client_cert_store_) { |
| 46 client_cert_store_->GetClientCerts( |
| 47 *cert_request_info_, |
| 48 &cert_request_info_->client_certs, |
| 49 base::Bind(&SSLClientAuthHandler::DidGetClientCerts, this)); |
| 50 } else { |
| 51 DidGetClientCerts(); |
| 52 } |
| 53 } |
| 54 |
| 55 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate* cert) { |
| 56 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 57 |
| 58 VLOG(1) << this << " CertificateSelected " << cert; |
| 59 BrowserThread::PostTask( |
| 60 BrowserThread::IO, FROM_HERE, |
| 61 base::Bind( |
| 62 &SSLClientAuthHandler::DoCertificateSelected, this, |
| 63 make_scoped_refptr(cert))); |
| 64 } |
| 65 |
| 66 void SSLClientAuthHandler::DidGetClientCerts() { |
| 67 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 68 // Request may have cancelled while we were getting client certs. |
| 69 if (!request_) |
| 70 return; |
| 71 |
| 72 // Note that if |client_cert_store_| is NULL, we intentionally fall through to |
| 73 // DoCertificateSelected. This is for platforms where the client cert matching |
| 74 // is not performed by Chrome, the platform can handle the cert matching |
| 75 // before showing the dialog. |
| 76 if (client_cert_store_ && cert_request_info_->client_certs.empty()) { |
| 77 // No need to query the user if there are no certs to choose from. |
| 78 DoCertificateSelected(NULL); |
| 79 return; |
| 80 } |
| 81 |
| 42 int render_process_host_id; | 82 int render_process_host_id; |
| 43 int render_view_host_id; | 83 int render_view_host_id; |
| 44 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderView( | 84 if (!ResourceRequestInfo::ForRequest(request_)->GetAssociatedRenderView( |
| 45 &render_process_host_id, | 85 &render_process_host_id, |
| 46 &render_view_host_id)) | 86 &render_view_host_id)) |
| 47 NOTREACHED(); | 87 NOTREACHED(); |
| 48 | 88 |
| 49 // If the RVH does not exist by the time this task gets run, then the task | 89 // If the RVH does not exist by the time this task gets run, then the task |
| 50 // will be dropped and the scoped_refptr to SSLClientAuthHandler will go | 90 // will be dropped and the scoped_refptr to SSLClientAuthHandler will go |
| 51 // away, so we do not leak anything. The destructor takes care of ensuring | 91 // away, so we do not leak anything. The destructor takes care of ensuring |
| 52 // the net::URLRequest always gets a response. | 92 // the net::URLRequest always gets a response. |
| 53 BrowserThread::PostTask( | 93 BrowserThread::PostTask( |
| 54 BrowserThread::UI, FROM_HERE, | 94 BrowserThread::UI, FROM_HERE, |
| 55 base::Bind( | 95 base::Bind( |
| 56 &SSLClientAuthHandler::DoSelectCertificate, this, | 96 &SSLClientAuthHandler::DoSelectCertificate, this, |
| 57 render_process_host_id, render_view_host_id)); | 97 render_process_host_id, render_view_host_id)); |
| 58 } | 98 } |
| 59 | 99 |
| 60 void SSLClientAuthHandler::CertificateSelected(net::X509Certificate* cert) { | |
| 61 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 62 | |
| 63 VLOG(1) << this << " CertificateSelected " << cert; | |
| 64 BrowserThread::PostTask( | |
| 65 BrowserThread::IO, FROM_HERE, | |
| 66 base::Bind( | |
| 67 &SSLClientAuthHandler::DoCertificateSelected, this, | |
| 68 make_scoped_refptr(cert))); | |
| 69 } | |
| 70 | |
| 71 void SSLClientAuthHandler::DoCertificateSelected(net::X509Certificate* cert) { | 100 void SSLClientAuthHandler::DoCertificateSelected(net::X509Certificate* cert) { |
| 72 VLOG(1) << this << " DoCertificateSelected " << cert; | 101 VLOG(1) << this << " DoCertificateSelected " << cert; |
| 73 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); | 102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO)); |
| 74 // request_ could have been NULLed if the request was cancelled while the | 103 // request_ could have been NULLed if the request was cancelled while the |
| 75 // user was choosing a cert, or because we have already responded to the | 104 // user was choosing a cert, or because we have already responded to the |
| 76 // certificate. | 105 // certificate. |
| 77 if (request_) { | 106 if (request_) { |
| 78 request_->ContinueWithCertificate(cert); | 107 request_->ContinueWithCertificate(cert); |
| 79 | 108 |
| 80 ResourceDispatcherHostImpl::Get()-> | 109 ResourceDispatcherHostImpl::Get()-> |
| 81 ClearSSLClientAuthHandlerForRequest(request_); | 110 ClearSSLClientAuthHandlerForRequest(request_); |
| 82 request_ = NULL; | 111 request_ = NULL; |
| 83 } | 112 } |
| 84 } | 113 } |
| 85 | 114 |
| 86 void SSLClientAuthHandler::DoSelectCertificate( | 115 void SSLClientAuthHandler::DoSelectCertificate( |
| 87 int render_process_host_id, int render_view_host_id) { | 116 int render_process_host_id, int render_view_host_id) { |
| 88 GetContentClient()->browser()->SelectClientCertificate( | 117 GetContentClient()->browser()->SelectClientCertificate( |
| 89 render_process_host_id, | 118 render_process_host_id, |
| 90 render_view_host_id, | 119 render_view_host_id, |
| 91 http_network_session_, | 120 http_network_session_, |
| 92 cert_request_info_.get(), | 121 cert_request_info_.get(), |
| 93 base::Bind(&SSLClientAuthHandler::CertificateSelected, this)); | 122 base::Bind(&SSLClientAuthHandler::CertificateSelected, this)); |
| 94 } | 123 } |
| 95 | 124 |
| 96 } // namespace content | 125 } // namespace content |
| OLD | NEW |