| 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), |
| 34 weak_ptr_factory_(this) { |
| 38 chrome::RecordDialogCreation( | 35 chrome::RecordDialogCreation( |
| 39 chrome::DialogIdentifier::SSL_CLIENT_CERTIFICATE_SELECTOR); | 36 chrome::DialogIdentifier::SSL_CLIENT_CERTIFICATE_SELECTOR); |
| 40 } | 37 } |
| 41 | 38 |
| 42 SSLClientCertificateSelector::~SSLClientCertificateSelector() {} | 39 SSLClientCertificateSelector::~SSLClientCertificateSelector() {} |
| 43 | 40 |
| 44 void SSLClientCertificateSelector::Init() { | 41 void SSLClientCertificateSelector::Init() { |
| 45 StartObserving(); | 42 StartObserving(); |
| 46 std::unique_ptr<views::Label> text_label( | 43 std::unique_ptr<views::Label> text_label( |
| 47 new views::Label(l10n_util::GetStringFUTF16( | 44 new views::Label(l10n_util::GetStringFUTF16( |
| 48 IDS_CLIENT_CERT_DIALOG_TEXT, | 45 IDS_CLIENT_CERT_DIALOG_TEXT, |
| 49 base::ASCIIToUTF16(cert_request_info()->host_and_port.ToString())))); | 46 base::ASCIIToUTF16(cert_request_info()->host_and_port.ToString())))); |
| 50 text_label->SetMultiLine(true); | 47 text_label->SetMultiLine(true); |
| 51 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); | 48 text_label->SetHorizontalAlignment(gfx::ALIGN_LEFT); |
| 52 text_label->SetAllowCharacterBreak(true); | 49 text_label->SetAllowCharacterBreak(true); |
| 53 text_label->SizeToFit(kTableViewWidth); | 50 text_label->SizeToFit(kTableViewWidth); |
| 54 InitWithText(std::move(text_label)); | 51 InitWithText(std::move(text_label)); |
| 55 } | 52 } |
| 56 | 53 |
| 57 void SSLClientCertificateSelector::OnCertSelectedByNotification() { | 54 void SSLClientCertificateSelector::OnCertSelectedByNotification() { |
| 58 GetWidget()->Close(); | 55 GetWidget()->Close(); |
| 59 } | 56 } |
| 60 | 57 |
| 61 void SSLClientCertificateSelector::DeleteDelegate() { | 58 void SSLClientCertificateSelector::DeleteDelegate() { |
| 62 // This is here and not in Cancel() to give WebContentsDestroyed a chance | 59 // 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 | 60 // to abort instead of proceeding with a null certificate. (This will be |
| 64 // ignored if there was a previous call to CertificateSelected or | 61 // ignored if there was a previous call to CertificateSelected or |
| 65 // CancelCertificateSelection.) | 62 // CancelCertificateSelection.) |
| 66 CertificateSelected(nullptr); | 63 CertificateSelected(nullptr, nullptr); |
| 67 chrome::CertificateSelector::DeleteDelegate(); | 64 chrome::CertificateSelector::DeleteDelegate(); |
| 68 } | 65 } |
| 69 | 66 |
| 70 bool SSLClientCertificateSelector::Accept() { | 67 bool SSLClientCertificateSelector::Accept() { |
| 71 scoped_refptr<net::X509Certificate> cert = GetSelectedCert(); | 68 std::unique_ptr<net::ClientCertIdentity> identity = TakeSelectedCert(); |
| 72 if (cert.get()) { | 69 if (identity) { |
| 73 // Remove the observer before we try unlocking, otherwise we might act on a | 70 // Remove the observer before we try acquiring private key, otherwise we |
| 74 // notification while waiting for the unlock dialog, causing us to delete | 71 // might act on a notification while waiting for the callback, causing us |
| 75 // ourself before the Unlocked callback gets called. | 72 // to delete ourself before the callback gets called. |
| 76 StopObserving(); | 73 StopObserving(); |
| 77 #if defined(USE_NSS_CERTS) && !defined(OS_CHROMEOS) | 74 net::X509Certificate* cert = identity->certificate(); |
| 78 chrome::UnlockCertSlotIfNecessary( | 75 net::ClientCertIdentity::SelfOwningAcquirePrivateKey( |
| 79 cert.get(), chrome::kCryptoModulePasswordClientAuth, | 76 std::move(identity), |
| 80 cert_request_info()->host_and_port, GetWidget()->GetNativeView(), | 77 base::Bind(&SSLClientCertificateSelector::GotPrivateKey, |
| 81 base::Bind(&SSLClientCertificateSelector::Unlocked, | 78 weak_ptr_factory_.GetWeakPtr(), base::Unretained(cert))); |
| 82 base::Unretained(this), base::RetainedRef(cert))); | 79 // GotPrivateKey() will close the dialog. It may be run synchronously. |
| 83 #else | 80 return false; |
| 84 Unlocked(cert.get()); | |
| 85 #endif | |
| 86 return false; // Unlocked() will close the dialog. | |
| 87 } | 81 } |
| 88 | 82 |
| 89 return false; | 83 return false; |
| 90 } | 84 } |
| 91 | 85 |
| 92 void SSLClientCertificateSelector::WebContentsDestroyed() { | 86 void SSLClientCertificateSelector::WebContentsDestroyed() { |
| 93 // If the dialog is closed by closing the containing tab, abort the request. | 87 // If the dialog is closed by closing the containing tab, abort the request. |
| 94 CancelCertificateSelection(); | 88 CancelCertificateSelection(); |
| 95 } | 89 } |
| 96 | 90 |
| 97 void SSLClientCertificateSelector::Unlocked(net::X509Certificate* cert) { | 91 void SSLClientCertificateSelector::GotPrivateKey( |
| 98 CertificateSelected(cert); | 92 net::X509Certificate* cert, |
| 93 scoped_refptr<net::SSLPrivateKey> private_key) { |
| 94 CertificateSelected(cert, private_key.get()); |
| 99 GetWidget()->Close(); | 95 GetWidget()->Close(); |
| 100 } | 96 } |
| 101 | 97 |
| 102 namespace chrome { | 98 namespace chrome { |
| 103 | 99 |
| 104 void ShowSSLClientCertificateSelector( | 100 void ShowSSLClientCertificateSelector( |
| 105 content::WebContents* contents, | 101 content::WebContents* contents, |
| 106 net::SSLCertRequestInfo* cert_request_info, | 102 net::SSLCertRequestInfo* cert_request_info, |
| 107 net::CertificateList client_certs, | 103 net::ClientCertIdentityList client_certs, |
| 108 std::unique_ptr<content::ClientCertificateDelegate> delegate) { | 104 std::unique_ptr<content::ClientCertificateDelegate> delegate) { |
| 109 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | 105 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| 110 | 106 |
| 111 // Not all WebContentses can show modal dialogs. | 107 // Not all WebContentses can show modal dialogs. |
| 112 // | 108 // |
| 113 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to | 109 // TODO(davidben): Move this hook to the WebContentsDelegate and only try to |
| 114 // show a dialog in Browser's implementation. https://crbug.com/456255 | 110 // show a dialog in Browser's implementation. https://crbug.com/456255 |
| 115 if (!SSLClientCertificateSelector::CanShow(contents)) | 111 if (!SSLClientCertificateSelector::CanShow(contents)) |
| 116 return; | 112 return; |
| 117 | 113 |
| 118 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector( | 114 SSLClientCertificateSelector* selector = new SSLClientCertificateSelector( |
| 119 contents, cert_request_info, std::move(client_certs), | 115 contents, cert_request_info, std::move(client_certs), |
| 120 std::move(delegate)); | 116 std::move(delegate)); |
| 121 selector->Init(); | 117 selector->Init(); |
| 122 selector->Show(); | 118 selector->Show(); |
| 123 } | 119 } |
| 124 | 120 |
| 125 } // namespace chrome | 121 } // namespace chrome |
| OLD | NEW |