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