| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/ssl/ssl_add_certificate.h" | |
| 6 | |
| 7 #include "base/macros.h" | |
| 8 #include "base/strings/string_number_conversions.h" | |
| 9 #include "base/strings/utf_string_conversions.h" | |
| 10 #include "build/build_config.h" | |
| 11 #include "chrome/browser/certificate_viewer.h" | |
| 12 #include "chrome/browser/infobars/infobar_service.h" | |
| 13 #include "chrome/grit/generated_resources.h" | |
| 14 #include "components/infobars/core/confirm_infobar_delegate.h" | |
| 15 #include "components/infobars/core/infobar.h" | |
| 16 #include "components/infobars/core/infobar_delegate.h" | |
| 17 #include "components/infobars/core/simple_alert_infobar_delegate.h" | |
| 18 #include "content/public/browser/browser_thread.h" | |
| 19 #include "content/public/browser/render_frame_host.h" | |
| 20 #include "content/public/browser/web_contents.h" | |
| 21 #include "net/base/net_errors.h" | |
| 22 #include "net/cert/cert_database.h" | |
| 23 #include "net/cert/x509_certificate.h" | |
| 24 #include "ui/base/l10n/l10n_util.h" | |
| 25 #include "ui/gfx/vector_icons_public.h" | |
| 26 | |
| 27 using content::BrowserThread; | |
| 28 using content::RenderFrameHost; | |
| 29 using content::WebContents; | |
| 30 | |
| 31 namespace chrome { | |
| 32 | |
| 33 namespace { | |
| 34 | |
| 35 class SSLAddCertificateInfoBarDelegate : public ConfirmInfoBarDelegate { | |
| 36 public: | |
| 37 // Creates an SSL certificate enrollment result infobar and delegate and adds | |
| 38 // the infobar to |infobar_service|. | |
| 39 static void Create(InfoBarService* infobar_service, | |
| 40 net::X509Certificate* cert); | |
| 41 | |
| 42 private: | |
| 43 explicit SSLAddCertificateInfoBarDelegate(net::X509Certificate* cert); | |
| 44 ~SSLAddCertificateInfoBarDelegate() override; | |
| 45 | |
| 46 // ConfirmInfoBarDelegate: | |
| 47 Type GetInfoBarType() const override; | |
| 48 infobars::InfoBarDelegate::InfoBarIdentifier GetIdentifier() const override; | |
| 49 gfx::VectorIconId GetVectorIconId() const override; | |
| 50 base::string16 GetMessageText() const override; | |
| 51 int GetButtons() const override; | |
| 52 base::string16 GetButtonLabel(InfoBarButton button) const override; | |
| 53 bool Accept() override; | |
| 54 | |
| 55 // The certificate that was added. | |
| 56 scoped_refptr<net::X509Certificate> cert_; | |
| 57 | |
| 58 DISALLOW_COPY_AND_ASSIGN(SSLAddCertificateInfoBarDelegate); | |
| 59 }; | |
| 60 | |
| 61 // static | |
| 62 void SSLAddCertificateInfoBarDelegate::Create(InfoBarService* infobar_service, | |
| 63 net::X509Certificate* cert) { | |
| 64 infobar_service->AddInfoBar(infobar_service->CreateConfirmInfoBar( | |
| 65 std::unique_ptr<ConfirmInfoBarDelegate>( | |
| 66 new SSLAddCertificateInfoBarDelegate(cert)))); | |
| 67 } | |
| 68 | |
| 69 SSLAddCertificateInfoBarDelegate::SSLAddCertificateInfoBarDelegate( | |
| 70 net::X509Certificate* cert) | |
| 71 : cert_(cert) { | |
| 72 } | |
| 73 | |
| 74 SSLAddCertificateInfoBarDelegate::~SSLAddCertificateInfoBarDelegate() { | |
| 75 } | |
| 76 | |
| 77 infobars::InfoBarDelegate::Type | |
| 78 SSLAddCertificateInfoBarDelegate::GetInfoBarType() const { | |
| 79 return PAGE_ACTION_TYPE; | |
| 80 } | |
| 81 | |
| 82 infobars::InfoBarDelegate::InfoBarIdentifier | |
| 83 SSLAddCertificateInfoBarDelegate::GetIdentifier() const { | |
| 84 return SSL_ADD_CERTIFICATE_INFOBAR_DELEGATE; | |
| 85 } | |
| 86 | |
| 87 gfx::VectorIconId SSLAddCertificateInfoBarDelegate::GetVectorIconId() const { | |
| 88 // TODO(davidben): Use a more appropriate icon. | |
| 89 return gfx::VectorIconId::AUTOLOGIN; | |
| 90 } | |
| 91 | |
| 92 base::string16 SSLAddCertificateInfoBarDelegate::GetMessageText() const { | |
| 93 // TODO(evanm): GetDisplayName should return UTF-16. | |
| 94 return l10n_util::GetStringFUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_LABEL, | |
| 95 base::UTF8ToUTF16( | |
| 96 cert_->issuer().GetDisplayName())); | |
| 97 } | |
| 98 | |
| 99 int SSLAddCertificateInfoBarDelegate::GetButtons() const { | |
| 100 return BUTTON_OK; | |
| 101 } | |
| 102 | |
| 103 base::string16 SSLAddCertificateInfoBarDelegate::GetButtonLabel( | |
| 104 InfoBarButton button) const { | |
| 105 DCHECK_EQ(BUTTON_OK, button); | |
| 106 return l10n_util::GetStringUTF16(IDS_ADD_CERT_SUCCESS_INFOBAR_BUTTON); | |
| 107 } | |
| 108 | |
| 109 bool SSLAddCertificateInfoBarDelegate::Accept() { | |
| 110 WebContents* web_contents = | |
| 111 InfoBarService::WebContentsFromInfoBar(infobar()); | |
| 112 ShowCertificateViewer(web_contents, | |
| 113 web_contents->GetTopLevelNativeWindow(), | |
| 114 cert_.get()); | |
| 115 // It looks weird to hide the infobar just as the dialog opens. | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 void ShowErrorInfoBar(int message_id, | |
| 120 int render_process_id, | |
| 121 int render_frame_id, | |
| 122 int cert_error) { | |
| 123 WebContents* web_contents = WebContents::FromRenderFrameHost( | |
| 124 RenderFrameHost::FromID(render_process_id, render_frame_id)); | |
| 125 if (!web_contents) | |
| 126 return; | |
| 127 | |
| 128 // TODO(davidben): Use a more appropriate icon. | |
| 129 // TODO(davidben): Display a more user-friendly error string. | |
| 130 SimpleAlertInfoBarDelegate::Create( | |
| 131 InfoBarService::FromWebContents(web_contents), | |
| 132 infobars::InfoBarDelegate::SSL_ADD_CERTIFICATE, | |
| 133 0, | |
| 134 gfx::VectorIconId::AUTOLOGIN, | |
| 135 l10n_util::GetStringFUTF16( | |
| 136 IDS_ADD_CERT_ERR_INVALID_CERT, base::IntToString16(-cert_error), | |
| 137 base::ASCIIToUTF16(net::ErrorToString(cert_error))), | |
| 138 true); | |
| 139 } | |
| 140 | |
| 141 void ShowSuccessInfoBar(int render_process_id, | |
| 142 int render_frame_id, | |
| 143 net::X509Certificate* cert) { | |
| 144 WebContents* web_contents = WebContents::FromRenderFrameHost( | |
| 145 RenderFrameHost::FromID(render_process_id, render_frame_id)); | |
| 146 if (!web_contents) | |
| 147 return; | |
| 148 | |
| 149 SSLAddCertificateInfoBarDelegate::Create( | |
| 150 InfoBarService::FromWebContents(web_contents), cert); | |
| 151 } | |
| 152 | |
| 153 } // namespace | |
| 154 | |
| 155 void SSLAddCertificate( | |
| 156 net::CertificateMimeType cert_type, | |
| 157 const void* cert_data, | |
| 158 size_t cert_size, | |
| 159 int render_process_id, | |
| 160 int render_frame_id) { | |
| 161 // Chromium only supports X.509 User certificates on non-Android | |
| 162 // platforms. Note that this method should not be called for other | |
| 163 // certificate mime types. | |
| 164 if (cert_type != net::CERTIFICATE_MIME_TYPE_X509_USER_CERT) | |
| 165 return; | |
| 166 | |
| 167 scoped_refptr<net::X509Certificate> cert; | |
| 168 if (cert_data != NULL) { | |
| 169 cert = net::X509Certificate::CreateFromBytes( | |
| 170 reinterpret_cast<const char*>(cert_data), cert_size); | |
| 171 } | |
| 172 // NOTE: Passing a NULL cert pointer if |cert_data| was NULL is | |
| 173 // intentional here. | |
| 174 | |
| 175 // Check if we have a corresponding private key. | |
| 176 int cert_error = net::CertDatabase::GetInstance()->CheckUserCert(cert.get()); | |
| 177 if (cert_error != net::OK) { | |
| 178 LOG_IF(ERROR, cert_error == net::ERR_NO_PRIVATE_KEY_FOR_CERT) | |
| 179 << "No corresponding private key in store for cert: " | |
| 180 << (cert.get() ? cert->subject().GetDisplayName() : "NULL"); | |
| 181 | |
| 182 BrowserThread::PostTask( | |
| 183 BrowserThread::UI, FROM_HERE, | |
| 184 base::Bind(&ShowErrorInfoBar, IDS_ADD_CERT_ERR_INVALID_CERT, | |
| 185 render_process_id, render_frame_id, cert_error)); | |
| 186 return; | |
| 187 } | |
| 188 | |
| 189 // Install it. | |
| 190 cert_error = net::CertDatabase::GetInstance()->AddUserCert(cert.get()); | |
| 191 | |
| 192 // Show the appropriate infobar. | |
| 193 if (cert_error != net::OK) { | |
| 194 BrowserThread::PostTask( | |
| 195 BrowserThread::UI, FROM_HERE, | |
| 196 base::Bind(&ShowErrorInfoBar, IDS_ADD_CERT_ERR_FAILED, | |
| 197 render_process_id, render_frame_id, cert_error)); | |
| 198 } else { | |
| 199 BrowserThread::PostTask( | |
| 200 BrowserThread::UI, FROM_HERE, | |
| 201 base::Bind(&ShowSuccessInfoBar, render_process_id, render_frame_id, | |
| 202 base::RetainedRef(cert))); | |
| 203 } | |
| 204 } | |
| 205 | |
| 206 } // namespace chrome | |
| OLD | NEW |