| 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 "chrome/browser/ui/views/ssl_client_certificate_selector.h" | 5 #include "chrome/browser/ui/views/ssl_client_certificate_selector.h" |
| 6 | 6 |
| 7 #include <utility> | 7 #include <utility> |
| 8 | 8 |
| 9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/bind_helpers.h" | 10 #include "base/bind_helpers.h" |
| 11 #include "base/strings/utf_string_conversions.h" | 11 #include "base/strings/utf_string_conversions.h" |
| 12 #include "build/build_config.h" | 12 #include "build/build_config.h" |
| 13 #include "chrome/browser/ui/browser_dialogs.h" | 13 #include "chrome/browser/ui/browser_dialogs.h" |
| 14 #include "chrome/grit/generated_resources.h" | 14 #include "chrome/grit/generated_resources.h" |
| 15 #include "content/public/browser/browser_thread.h" | 15 #include "content/public/browser/browser_thread.h" |
| 16 #include "content/public/browser/client_certificate_delegate.h" | 16 #include "content/public/browser/client_certificate_delegate.h" |
| 17 #include "content/public/browser/web_contents.h" | 17 #include "content/public/browser/web_contents.h" |
| 18 #include "net/cert/x509_certificate.h" | 18 #include "net/cert/x509_certificate.h" |
| 19 #include "net/ssl/ssl_cert_request_info.h" | 19 #include "net/ssl/ssl_cert_request_info.h" |
| 20 #include "ui/base/l10n/l10n_util.h" | 20 #include "ui/base/l10n/l10n_util.h" |
| 21 #include "ui/views/controls/label.h" | 21 #include "ui/views/controls/label.h" |
| 22 #include "ui/views/widget/widget.h" | 22 #include "ui/views/widget/widget.h" |
| 23 | 23 |
| 24 #if defined(USE_NSS_CERTS) && !defined(OS_CHROMEOS) | |
| 25 #include "chrome/browser/ui/crypto_module_password_dialog_nss.h" | |
| 26 #endif | |
| 27 | |
| 28 SSLClientCertificateSelector::SSLClientCertificateSelector( | 24 SSLClientCertificateSelector::SSLClientCertificateSelector( |
| 29 content::WebContents* web_contents, | 25 content::WebContents* web_contents, |
| 30 const scoped_refptr<net::SSLCertRequestInfo>& cert_request_info, | 26 const scoped_refptr<net::SSLCertRequestInfo>& cert_request_info, |
| 31 net::CertificateList client_certs, | 27 net::ClientCertIdentityList client_certs, |
| 32 std::unique_ptr<content::ClientCertificateDelegate> delegate) | 28 std::unique_ptr<content::ClientCertificateDelegate> delegate) |
| 33 : CertificateSelector(std::move(client_certs), web_contents), | 29 : CertificateSelector(std::move(client_certs), web_contents), |
| 34 SSLClientAuthObserver(web_contents->GetBrowserContext(), | 30 SSLClientAuthObserver(web_contents->GetBrowserContext(), |
| 35 cert_request_info, | 31 cert_request_info, |
| 36 std::move(delegate)), | 32 std::move(delegate)), |
| 37 WebContentsObserver(web_contents) { | 33 WebContentsObserver(web_contents) { |
| 38 chrome::RecordDialogCreation( | 34 chrome::RecordDialogCreation( |
| 39 chrome::DialogIdentifier::SSL_CLIENT_CERTIFICATE_SELECTOR); | 35 chrome::DialogIdentifier::SSL_CLIENT_CERTIFICATE_SELECTOR); |
| 40 } | 36 } |
| 41 | 37 |
| (...skipping 14 matching lines...) Expand all Loading... |
| 56 | 52 |
| 57 void SSLClientCertificateSelector::OnCertSelectedByNotification() { | 53 void SSLClientCertificateSelector::OnCertSelectedByNotification() { |
| 58 GetWidget()->Close(); | 54 GetWidget()->Close(); |
| 59 } | 55 } |
| 60 | 56 |
| 61 void SSLClientCertificateSelector::DeleteDelegate() { | 57 void SSLClientCertificateSelector::DeleteDelegate() { |
| 62 // This is here and not in Cancel() to give WebContentsDestroyed a chance | 58 // This is here and not in Cancel() to give WebContentsDestroyed a chance |
| 63 // to abort instead of proceeding with a null certificate. (This will be | 59 // to abort instead of proceeding with a null certificate. (This will be |
| 64 // ignored if there was a previous call to CertificateSelected or | 60 // ignored if there was a previous call to CertificateSelected or |
| 65 // CancelCertificateSelection.) | 61 // CancelCertificateSelection.) |
| 66 CertificateSelected(nullptr); | 62 CertificateSelected(nullptr, nullptr); |
| 67 chrome::CertificateSelector::DeleteDelegate(); | 63 chrome::CertificateSelector::DeleteDelegate(); |
| 68 } | 64 } |
| 69 | 65 |
| 70 bool SSLClientCertificateSelector::Accept() { | 66 bool SSLClientCertificateSelector::AcceptCertificate( |
| 71 scoped_refptr<net::X509Certificate> cert = GetSelectedCert(); | 67 std::unique_ptr<net::ClientCertIdentity> identity) { |
| 72 if (cert.get()) { | 68 // Remove the observer before we try acquiring private key, otherwise we |
| 73 // Remove the observer before we try unlocking, otherwise we might act on a | 69 // might act on a notification while waiting for the callback, causing us |
| 74 // notification while waiting for the unlock dialog, causing us to delete | 70 // to delete ourself before the callback gets called. |
| 75 // ourself before the Unlocked callback gets called. | 71 StopObserving(); |
| 76 StopObserving(); | 72 net::X509Certificate* cert = identity->certificate(); |
| 77 #if defined(USE_NSS_CERTS) && !defined(OS_CHROMEOS) | 73 net::ClientCertIdentity::SelfOwningAcquirePrivateKey( |
| 78 chrome::UnlockCertSlotIfNecessary( | 74 std::move(identity), |
| 79 cert.get(), chrome::kCryptoModulePasswordClientAuth, | 75 base::Bind(&SSLClientCertificateSelector::GotPrivateKey, |
| 80 cert_request_info()->host_and_port, GetWidget()->GetNativeView(), | 76 weak_ptr_factory_.GetWeakPtr(), base::Unretained(cert))); |
| 81 base::Bind(&SSLClientCertificateSelector::Unlocked, | 77 // GotPrivateKey() will close the dialog. It may be run synchronously. |
| 82 base::Unretained(this), base::RetainedRef(cert))); | |
| 83 #else | |
| 84 Unlocked(cert.get()); | |
| 85 #endif | |
| 86 return false; // Unlocked() will close the dialog. | |
| 87 } | |
| 88 | |
| 89 return false; | 78 return false; |
| 90 } | 79 } |
| 91 | 80 |
| 92 void SSLClientCertificateSelector::WebContentsDestroyed() { | 81 void SSLClientCertificateSelector::WebContentsDestroyed() { |
| 93 // If the dialog is closed by closing the containing tab, abort the request. | 82 // If the dialog is closed by closing the containing tab, abort the request. |
| 94 CancelCertificateSelection(); | 83 CancelCertificateSelection(); |
| 95 } | 84 } |
| 96 | 85 |
| 97 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) { | 86 void SSLClientCertificateSelector::GotPrivateKey( |
| 98 CertificateSelected(cert); | 87 net::X509Certificate* cert, |
| 88 scoped_refptr<net::SSLPrivateKey> private_key) { |
| 89 CertificateSelected(cert, private_key.get()); |
| 99 GetWidget()->Close(); | 90 GetWidget()->Close(); |
| 100 } | 91 } |
| 101 | 92 |
| 102 namespace chrome { | 93 namespace chrome { |
| 103 | 94 |
| 104 void ShowSSLClientCertificateSelector( | 95 void ShowSSLClientCertificateSelector( |
| 105 content::WebContents* contents, | 96 content::WebContents* contents, |
| 106 net::SSLCertRequestInfo* cert_request_info, | 97 net::SSLCertRequestInfo* cert_request_info, |
| 107 net::CertificateList client_certs, | 98 net::ClientCertIdentityList client_certs, |
| 108 std::unique_ptr<content::ClientCertificateDelegate> delegate) { | 99 std::unique_ptr<content::ClientCertificateDelegate> delegate) { |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 100 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 110 | 101 |
| 111 // Not all WebContentses can show modal dialogs. | 102 // Not all WebContentses can show modal dialogs. |
| 112 // | 103 // |
| 113 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to | 104 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to |
| 114 // show a dialog in Browser's implementation. https://crbug.com/456255 | 105 // show a dialog in Browser's implementation. https://crbug.com/456255 |
| 115 if (!SSLClientCertificateSelector::CanShow(contents)) | 106 if (!SSLClientCertificateSelector::CanShow(contents)) |
| 116 return; | 107 return; |
| 117 | 108 |
| 118 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector( | 109 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector( |
| 119 contents, cert_request_info, std::move(client_certs), | 110 contents, cert_request_info, std::move(client_certs), |
| 120 std::move(delegate)); | 111 std::move(delegate)); |
| 121 selector->Init(); | 112 selector->Init(); |
| 122 selector->Show(); | 113 selector->Show(); |
| 123 } | 114 } |
| 124 | 115 |
| 125 } // namespace chrome | 116 } // namespace chrome |
| OLD | NEW |