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.
5 #include "chrome/browser/ui/webui/certificate_viewer_webui.h"
8 #include "base/bind_helpers.h"
9 #include "base/i18n/time_formatting.h"
10 #include "base/json/json_writer.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/certificate_viewer.h"
14 #include "chrome/browser/platform_util.h"
15 #include "chrome/browser/ui/browser_dialogs.h"
16 #include "chrome/browser/ui/certificate_dialogs.h"
17 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
18 #include "chrome/common/net/x509_certificate_model.h"
19 #include "chrome/common/url_constants.h"
20 #include "content/public/browser/web_contents.h"
21 #include "grit/generated_resources.h"
22 #include "ui/base/l10n/l10n_util.h"
23 #include "ui/gfx/size.h"
25 using content::WebContents
;
26 using content::WebUIMessageHandler
;
27 using web_modal::NativeWebContentsModalDialog
;
29 // Shows a certificate using the WebUI certificate viewer.
30 void ShowCertificateViewer(WebContents
* web_contents
,
31 gfx::NativeWindow parent
,
32 net::X509Certificate
* cert
) {
33 CertificateViewerDialog
* dialog
= new CertificateViewerDialog(cert
);
34 dialog
->Show(web_contents
, parent
);
37 ////////////////////////////////////////////////////////////////////////////////
38 // CertificateViewerDialog
40 CertificateViewerDialog::CertificateViewerDialog(net::X509Certificate
* cert
)
41 : cert_(cert
), dialog_(NULL
) {
42 // Construct the dialog title from the certificate.
43 net::X509Certificate::OSCertHandles cert_chain
;
44 x509_certificate_model::GetCertChainFromCert(cert_
->os_cert_handle(),
46 title_
= l10n_util::GetStringFUTF16(IDS_CERT_INFO_DIALOG_TITLE
,
47 base::UTF8ToUTF16(x509_certificate_model::GetTitle(cert_chain
.front())));
50 CertificateViewerDialog::~CertificateViewerDialog() {
53 void CertificateViewerDialog::Show(WebContents
* web_contents
,
54 gfx::NativeWindow parent
) {
55 // TODO(bshe): UI tweaks needed for Aura HTML Dialog, such as adding padding
56 // on the title for Aura ConstrainedWebDialogUI.
57 dialog_
= CreateConstrainedWebDialog(
58 web_contents
->GetBrowserContext(),
64 ui::ModalType
CertificateViewerDialog::GetDialogModalType() const {
65 return ui::MODAL_TYPE_NONE
;
68 base::string16
CertificateViewerDialog::GetDialogTitle() const {
72 GURL
CertificateViewerDialog::GetDialogContentURL() const {
73 return GURL(chrome::kChromeUICertificateViewerURL
);
76 void CertificateViewerDialog::GetWebUIMessageHandlers(
77 std::vector
<WebUIMessageHandler
*>* handlers
) const {
78 handlers
->push_back(new CertificateViewerDialogHandler(
79 const_cast<CertificateViewerDialog
*>(this), cert_
.get()));
82 void CertificateViewerDialog::GetDialogSize(gfx::Size
* size
) const {
83 const int kDefaultWidth
= 544;
84 const int kDefaultHeight
= 628;
85 size
->SetSize(kDefaultWidth
, kDefaultHeight
);
88 std::string
CertificateViewerDialog::GetDialogArgs() const {
91 // Certificate information. The keys in this dictionary's general key
92 // correspond to the IDs in the Html page.
93 base::DictionaryValue cert_info
;
94 net::X509Certificate::OSCertHandle cert_hnd
= cert_
->os_cert_handle();
96 // Get the certificate chain.
97 net::X509Certificate::OSCertHandles cert_chain
;
98 x509_certificate_model::GetCertChainFromCert(cert_hnd
, &cert_chain
);
100 // Certificate usage.
101 std::vector
<std::string
> usages
;
102 x509_certificate_model::GetUsageStrings(cert_hnd
, &usages
);
103 std::string usagestr
;
104 for (std::vector
<std::string
>::iterator it
= usages
.begin();
105 it
!= usages
.end(); ++it
) {
106 if (usagestr
.length() > 0) {
111 cert_info
.SetString("general.usages", usagestr
);
113 // Standard certificate details.
114 const std::string alternative_text
=
115 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT
);
116 cert_info
.SetString("general.title", l10n_util::GetStringFUTF8(
117 IDS_CERT_INFO_DIALOG_TITLE
,
118 base::UTF8ToUTF16(x509_certificate_model::GetTitle(
119 cert_chain
.front()))));
121 // Issued to information.
122 cert_info
.SetString("general.issued-cn",
123 x509_certificate_model::GetSubjectCommonName(cert_hnd
, alternative_text
));
124 cert_info
.SetString("general.issued-o",
125 x509_certificate_model::GetSubjectOrgName(cert_hnd
, alternative_text
));
126 cert_info
.SetString("general.issued-ou",
127 x509_certificate_model::GetSubjectOrgUnitName(cert_hnd
,
129 cert_info
.SetString("general.issued-sn",
130 x509_certificate_model::GetSerialNumberHexified(cert_hnd
,
133 // Issuer information.
134 cert_info
.SetString("general.issuer-cn",
135 x509_certificate_model::GetIssuerCommonName(cert_hnd
, alternative_text
));
136 cert_info
.SetString("general.issuer-o",
137 x509_certificate_model::GetIssuerOrgName(cert_hnd
, alternative_text
));
138 cert_info
.SetString("general.issuer-ou",
139 x509_certificate_model::GetIssuerOrgUnitName(cert_hnd
, alternative_text
));
142 base::Time issued
, expires
;
143 std::string issued_str
, expires_str
;
144 if (x509_certificate_model::GetTimes(cert_hnd
, &issued
, &expires
)) {
145 issued_str
= base::UTF16ToUTF8(
146 base::TimeFormatShortDateNumeric(issued
));
147 expires_str
= base::UTF16ToUTF8(
148 base::TimeFormatShortDateNumeric(expires
));
150 issued_str
= alternative_text
;
151 expires_str
= alternative_text
;
153 cert_info
.SetString("general.issue-date", issued_str
);
154 cert_info
.SetString("general.expiry-date", expires_str
);
156 cert_info
.SetString("general.sha256",
157 x509_certificate_model::HashCertSHA256(cert_hnd
));
158 cert_info
.SetString("general.sha1",
159 x509_certificate_model::HashCertSHA1(cert_hnd
));
161 // Certificate hierarchy is constructed from bottom up.
162 base::ListValue
* children
= NULL
;
164 for (net::X509Certificate::OSCertHandles::const_iterator i
=
165 cert_chain
.begin(); i
!= cert_chain
.end(); ++i
, ++index
) {
166 base::DictionaryValue
* cert_node
= new base::DictionaryValue();
167 base::ListValue cert_details
;
168 cert_node
->SetString("label", x509_certificate_model::GetTitle(*i
).c_str());
169 cert_node
->SetDouble("payload.index", index
);
170 // Add the child from the previous iteration.
172 cert_node
->Set("children", children
);
174 // Add this node to the children list for the next iteration.
175 children
= new base::ListValue();
176 children
->Append(cert_node
);
178 // Set the last node as the top of the certificate hierarchy.
179 cert_info
.Set("hierarchy", children
);
181 base::JSONWriter::Write(&cert_info
, &data
);
186 void CertificateViewerDialog::OnDialogShown(
187 content::WebUI
* webui
,
188 content::RenderViewHost
* render_view_host
) {
191 void CertificateViewerDialog::OnDialogClosed(const std::string
& json_retval
) {
194 void CertificateViewerDialog::OnCloseContents(WebContents
* source
,
195 bool* out_close_dialog
) {
196 if (out_close_dialog
)
197 *out_close_dialog
= true;
200 bool CertificateViewerDialog::ShouldShowDialogTitle() const {
204 ////////////////////////////////////////////////////////////////////////////////
205 // CertificateViewerDialogHandler
207 CertificateViewerDialogHandler::CertificateViewerDialogHandler(
208 CertificateViewerDialog
* dialog
,
209 net::X509Certificate
* cert
) : cert_(cert
), dialog_(dialog
) {
210 x509_certificate_model::GetCertChainFromCert(cert_
->os_cert_handle(),
214 CertificateViewerDialogHandler::~CertificateViewerDialogHandler() {
217 void CertificateViewerDialogHandler::RegisterMessages() {
218 web_ui()->RegisterMessageCallback("exportCertificate",
219 base::Bind(&CertificateViewerDialogHandler::ExportCertificate
,
220 base::Unretained(this)));
221 web_ui()->RegisterMessageCallback("requestCertificateFields",
222 base::Bind(&CertificateViewerDialogHandler::RequestCertificateFields
,
223 base::Unretained(this)));
226 void CertificateViewerDialogHandler::ExportCertificate(
227 const base::ListValue
* args
) {
228 int cert_index
= GetCertificateIndex(args
);
232 NativeWebContentsModalDialog window
=
233 platform_util::GetTopLevel(dialog_
->dialog()->GetNativeDialog());
234 ShowCertExportDialog(web_ui()->GetWebContents(),
236 cert_chain_
[cert_index
]);
239 void CertificateViewerDialogHandler::RequestCertificateFields(
240 const base::ListValue
* args
) {
241 int cert_index
= GetCertificateIndex(args
);
245 net::X509Certificate::OSCertHandle cert
= cert_chain_
[cert_index
];
247 base::ListValue root_list
;
248 base::DictionaryValue
* node_details
;
249 base::DictionaryValue
* alt_node_details
;
250 base::ListValue
* cert_sub_fields
;
251 root_list
.Append(node_details
= new base::DictionaryValue());
252 node_details
->SetString("label", x509_certificate_model::GetTitle(cert
));
254 base::ListValue
* cert_fields
;
255 node_details
->Set("children", cert_fields
= new base::ListValue());
256 cert_fields
->Append(node_details
= new base::DictionaryValue());
258 node_details
->SetString("label",
259 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE
));
260 node_details
->Set("children", cert_fields
= new base::ListValue());
262 // Main certificate fields.
263 cert_fields
->Append(node_details
= new base::DictionaryValue());
264 node_details
->SetString("label",
265 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION
));
266 std::string version
= x509_certificate_model::GetVersion(cert
);
267 if (!version
.empty())
268 node_details
->SetString("payload.val",
269 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT
,
270 base::UTF8ToUTF16(version
)));
272 cert_fields
->Append(node_details
= new base::DictionaryValue());
273 node_details
->SetString("label",
274 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER
));
275 node_details
->SetString("payload.val",
276 x509_certificate_model::GetSerialNumberHexified(cert
,
277 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT
)));
279 cert_fields
->Append(node_details
= new base::DictionaryValue());
280 node_details
->SetString("label",
281 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG
));
282 node_details
->SetString("payload.val",
283 x509_certificate_model::ProcessSecAlgorithmSignature(cert
));
285 cert_fields
->Append(node_details
= new base::DictionaryValue());
286 node_details
->SetString("label",
287 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER
));
288 node_details
->SetString("payload.val",
289 x509_certificate_model::GetIssuerName(cert
));
292 cert_fields
->Append(node_details
= new base::DictionaryValue());
293 node_details
->SetString("label",
294 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY
));
296 node_details
->Set("children", cert_sub_fields
= new base::ListValue());
297 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
298 node_details
->SetString("label",
299 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE
));
300 cert_sub_fields
->Append(alt_node_details
= new base::DictionaryValue());
301 alt_node_details
->SetString("label",
302 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER
));
303 base::Time issued
, expires
;
304 if (x509_certificate_model::GetTimes(cert
, &issued
, &expires
)) {
305 node_details
->SetString("payload.val",
306 base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(issued
)));
307 alt_node_details
->SetString("payload.val",
308 base::UTF16ToUTF8(base::TimeFormatShortDateAndTime(expires
)));
311 cert_fields
->Append(node_details
= new base::DictionaryValue());
312 node_details
->SetString("label",
313 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT
));
314 node_details
->SetString("payload.val",
315 x509_certificate_model::GetSubjectName(cert
));
317 // Subject key information.
318 cert_fields
->Append(node_details
= new base::DictionaryValue());
319 node_details
->SetString("label",
320 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO
));
322 node_details
->Set("children", cert_sub_fields
= new base::ListValue());
323 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
324 node_details
->SetString("label",
325 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG
));
326 node_details
->SetString("payload.val",
327 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert
));
328 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
329 node_details
->SetString("label",
330 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY
));
331 node_details
->SetString("payload.val",
332 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert
));
335 x509_certificate_model::Extensions extensions
;
336 x509_certificate_model::GetExtensions(
337 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL
),
338 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL
),
341 if (!extensions
.empty()) {
342 cert_fields
->Append(node_details
= new base::DictionaryValue());
343 node_details
->SetString("label",
344 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS
));
346 node_details
->Set("children", cert_sub_fields
= new base::ListValue());
347 for (x509_certificate_model::Extensions::const_iterator i
=
348 extensions
.begin(); i
!= extensions
.end(); ++i
) {
349 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
350 node_details
->SetString("label", i
->name
);
351 node_details
->SetString("payload.val", i
->value
);
355 cert_fields
->Append(node_details
= new base::DictionaryValue());
356 node_details
->SetString("label",
357 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG
));
358 node_details
->SetString("payload.val",
359 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert
));
361 cert_fields
->Append(node_details
= new base::DictionaryValue());
362 node_details
->SetString("label",
363 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE
));
364 node_details
->SetString("payload.val",
365 x509_certificate_model::ProcessRawBitsSignatureWrap(cert
));
367 cert_fields
->Append(node_details
= new base::DictionaryValue());
368 node_details
->SetString("label",
369 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP
));
370 node_details
->Set("children", cert_sub_fields
= new base::ListValue());
372 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
373 node_details
->SetString("label",
374 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL
));
375 node_details
->SetString("payload.val",
376 x509_certificate_model::HashCertSHA256(cert
));
377 cert_sub_fields
->Append(node_details
= new base::DictionaryValue());
378 node_details
->SetString("label",
379 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL
));
380 node_details
->SetString("payload.val",
381 x509_certificate_model::HashCertSHA1(cert
));
383 // Send certificate information to javascript.
384 web_ui()->CallJavascriptFunction("cert_viewer.getCertificateFields",
388 int CertificateViewerDialogHandler::GetCertificateIndex(
389 const base::ListValue
* args
) const {
392 if (!(args
->GetDouble(0, &val
)))
394 cert_index
= static_cast<int>(val
);
395 if (cert_index
< 0 || cert_index
>= static_cast<int>(cert_chain_
.size()))