Don't show supervised user as "already on this device" while they're being imported.
[chromium-blink-merge.git] / chrome / browser / ui / webui / certificate_viewer_webui.cc
blob7f9c668fcd45eab183a565c2b3f4935221c36391
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"
7 #include "base/bind.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/certificate_viewer_ui.h"
18 #include "chrome/browser/ui/webui/constrained_web_dialog_ui.h"
19 #include "chrome/common/net/x509_certificate_model.h"
20 #include "chrome/common/url_constants.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "content/public/browser/web_contents.h"
23 #include "ui/base/l10n/l10n_util.h"
24 #include "ui/gfx/geometry/size.h"
26 using content::WebContents;
27 using content::WebUIMessageHandler;
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 CertificateViewerModalDialog::CertificateViewerModalDialog(
41 net::X509Certificate* cert)
42 : cert_(cert), webui_(NULL), window_(NULL) {
43 // Construct the dialog title from the certificate.
44 title_ = l10n_util::GetStringFUTF16(
45 IDS_CERT_INFO_DIALOG_TITLE,
46 base::UTF8ToUTF16(
47 x509_certificate_model::GetTitle(cert_->os_cert_handle())));
50 CertificateViewerModalDialog::~CertificateViewerModalDialog() {
53 void CertificateViewerModalDialog::Show(content::WebContents* web_contents,
54 gfx::NativeWindow parent) {
55 window_ = chrome::ShowWebDialog(parent,
56 web_contents->GetBrowserContext(),
57 this);
60 gfx::NativeWindow
61 CertificateViewerModalDialog::GetNativeWebContentsModalDialog() {
62 #if defined(USE_AURA)
63 return window_;
64 #else
65 NOTREACHED();
66 return NULL;
67 #endif
70 ui::ModalType CertificateViewerModalDialog::GetDialogModalType() const {
71 return ui::MODAL_TYPE_SYSTEM;
74 base::string16 CertificateViewerModalDialog::GetDialogTitle() const {
75 return title_;
78 GURL CertificateViewerModalDialog::GetDialogContentURL() const {
79 return GURL(chrome::kChromeUICertificateViewerDialogURL);
82 void CertificateViewerModalDialog::GetWebUIMessageHandlers(
83 std::vector<WebUIMessageHandler*>* handlers) const {
84 handlers->push_back(new CertificateViewerDialogHandler(
85 const_cast<CertificateViewerModalDialog*>(this), cert_.get()));
88 void CertificateViewerModalDialog::GetDialogSize(gfx::Size* size) const {
89 const int kDefaultWidth = 544;
90 const int kDefaultHeight = 628;
91 size->SetSize(kDefaultWidth, kDefaultHeight);
94 std::string CertificateViewerModalDialog::GetDialogArgs() const {
95 std::string data;
97 // Certificate information. The keys in this dictionary's general key
98 // correspond to the IDs in the Html page.
99 base::DictionaryValue cert_info;
100 net::X509Certificate::OSCertHandle cert_hnd = cert_->os_cert_handle();
102 // Get the certificate chain.
103 net::X509Certificate::OSCertHandles cert_chain;
104 cert_chain.push_back(cert_->os_cert_handle());
105 const net::X509Certificate::OSCertHandles& certs =
106 cert_->GetIntermediateCertificates();
107 cert_chain.insert(cert_chain.end(), certs.begin(), certs.end());
109 // Certificate usage.
110 std::vector<std::string> usages;
111 x509_certificate_model::GetUsageStrings(cert_hnd, &usages);
112 std::string usagestr;
113 for (std::vector<std::string>::iterator it = usages.begin();
114 it != usages.end(); ++it) {
115 if (usagestr.length() > 0) {
116 usagestr += "\n";
118 usagestr += *it;
120 cert_info.SetString("general.usages", usagestr);
122 // Standard certificate details.
123 const std::string alternative_text =
124 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT);
125 cert_info.SetString("general.title", l10n_util::GetStringFUTF8(
126 IDS_CERT_INFO_DIALOG_TITLE,
127 base::UTF8ToUTF16(x509_certificate_model::GetTitle(
128 cert_chain.front()))));
130 // Issued to information.
131 cert_info.SetString("general.issued-cn",
132 x509_certificate_model::GetSubjectCommonName(cert_hnd, alternative_text));
133 cert_info.SetString("general.issued-o",
134 x509_certificate_model::GetSubjectOrgName(cert_hnd, alternative_text));
135 cert_info.SetString("general.issued-ou",
136 x509_certificate_model::GetSubjectOrgUnitName(cert_hnd,
137 alternative_text));
138 cert_info.SetString("general.issued-sn",
139 x509_certificate_model::GetSerialNumberHexified(cert_hnd,
140 alternative_text));
142 // Issuer information.
143 cert_info.SetString("general.issuer-cn",
144 x509_certificate_model::GetIssuerCommonName(cert_hnd, alternative_text));
145 cert_info.SetString("general.issuer-o",
146 x509_certificate_model::GetIssuerOrgName(cert_hnd, alternative_text));
147 cert_info.SetString("general.issuer-ou",
148 x509_certificate_model::GetIssuerOrgUnitName(cert_hnd, alternative_text));
150 // Validity period.
151 base::Time issued, expires;
152 std::string issued_str, expires_str;
153 if (x509_certificate_model::GetTimes(cert_hnd, &issued, &expires)) {
154 issued_str = base::UTF16ToUTF8(
155 base::TimeFormatFriendlyDateAndTime(issued));
156 expires_str = base::UTF16ToUTF8(
157 base::TimeFormatFriendlyDateAndTime(expires));
158 } else {
159 issued_str = alternative_text;
160 expires_str = alternative_text;
162 cert_info.SetString("general.issue-date", issued_str);
163 cert_info.SetString("general.expiry-date", expires_str);
165 cert_info.SetString("general.sha256",
166 x509_certificate_model::HashCertSHA256(cert_hnd));
167 cert_info.SetString("general.sha1",
168 x509_certificate_model::HashCertSHA1(cert_hnd));
170 // Certificate hierarchy is constructed from bottom up.
171 base::ListValue* children = NULL;
172 int index = 0;
173 for (net::X509Certificate::OSCertHandles::const_iterator i =
174 cert_chain.begin(); i != cert_chain.end(); ++i, ++index) {
175 base::DictionaryValue* cert_node = new base::DictionaryValue();
176 base::ListValue cert_details;
177 cert_node->SetString("label", x509_certificate_model::GetTitle(*i).c_str());
178 cert_node->SetDouble("payload.index", index);
179 // Add the child from the previous iteration.
180 if (children)
181 cert_node->Set("children", children);
183 // Add this node to the children list for the next iteration.
184 children = new base::ListValue();
185 children->Append(cert_node);
187 // Set the last node as the top of the certificate hierarchy.
188 cert_info.Set("hierarchy", children);
190 base::JSONWriter::Write(cert_info, &data);
192 return data;
195 void CertificateViewerModalDialog::OnDialogShown(
196 content::WebUI* webui,
197 content::RenderViewHost* render_view_host) {
198 webui_ = webui;
201 void CertificateViewerModalDialog::OnDialogClosed(
202 const std::string& json_retval) {
205 void CertificateViewerModalDialog::OnCloseContents(WebContents* source,
206 bool* out_close_dialog) {
207 *out_close_dialog = true;
210 bool CertificateViewerModalDialog::ShouldShowDialogTitle() const {
211 return true;
214 ////////////////////////////////////////////////////////////////////////////////
215 // CertificateViewerDialog
217 CertificateViewerDialog::CertificateViewerDialog(net::X509Certificate* cert)
218 : CertificateViewerModalDialog(cert),
219 dialog_(NULL) {
222 CertificateViewerDialog::~CertificateViewerDialog() {
225 void CertificateViewerDialog::Show(WebContents* web_contents,
226 gfx::NativeWindow parent) {
227 // TODO(bshe): UI tweaks needed for Aura HTML Dialog, such as adding padding
228 // on the title for Aura ConstrainedWebDialogUI.
229 dialog_ = ShowConstrainedWebDialog(web_contents->GetBrowserContext(), this,
230 web_contents);
233 gfx::NativeWindow CertificateViewerDialog::GetNativeWebContentsModalDialog() {
234 return dialog_->GetNativeDialog();
237 GURL CertificateViewerDialog::GetDialogContentURL() const {
238 return GURL(chrome::kChromeUICertificateViewerURL);
241 ui::ModalType CertificateViewerDialog::GetDialogModalType() const {
242 return ui::MODAL_TYPE_NONE;
245 ////////////////////////////////////////////////////////////////////////////////
246 // CertificateViewerDialogHandler
248 CertificateViewerDialogHandler::CertificateViewerDialogHandler(
249 CertificateViewerModalDialog* dialog,
250 net::X509Certificate* cert)
251 : cert_(cert), dialog_(dialog) {
252 cert_chain_.push_back(cert_->os_cert_handle());
253 const net::X509Certificate::OSCertHandles& certs =
254 cert_->GetIntermediateCertificates();
255 cert_chain_.insert(cert_chain_.end(), certs.begin(), certs.end());
258 CertificateViewerDialogHandler::~CertificateViewerDialogHandler() {
261 void CertificateViewerDialogHandler::RegisterMessages() {
262 web_ui()->RegisterMessageCallback("exportCertificate",
263 base::Bind(&CertificateViewerDialogHandler::ExportCertificate,
264 base::Unretained(this)));
265 web_ui()->RegisterMessageCallback("requestCertificateFields",
266 base::Bind(&CertificateViewerDialogHandler::RequestCertificateFields,
267 base::Unretained(this)));
270 void CertificateViewerDialogHandler::ExportCertificate(
271 const base::ListValue* args) {
272 int cert_index = GetCertificateIndex(args);
273 if (cert_index < 0)
274 return;
276 gfx::NativeWindow window =
277 platform_util::GetTopLevel(dialog_->GetNativeWebContentsModalDialog());
278 ShowCertExportDialog(web_ui()->GetWebContents(),
279 window,
280 cert_chain_.begin() + cert_index,
281 cert_chain_.end());
284 void CertificateViewerDialogHandler::RequestCertificateFields(
285 const base::ListValue* args) {
286 int cert_index = GetCertificateIndex(args);
287 if (cert_index < 0)
288 return;
290 net::X509Certificate::OSCertHandle cert = cert_chain_[cert_index];
292 base::ListValue root_list;
293 base::DictionaryValue* node_details;
294 base::DictionaryValue* alt_node_details;
295 base::ListValue* cert_sub_fields;
296 root_list.Append(node_details = new base::DictionaryValue());
297 node_details->SetString("label", x509_certificate_model::GetTitle(cert));
299 base::ListValue* cert_fields;
300 node_details->Set("children", cert_fields = new base::ListValue());
301 cert_fields->Append(node_details = new base::DictionaryValue());
303 node_details->SetString("label",
304 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE));
305 node_details->Set("children", cert_fields = new base::ListValue());
307 // Main certificate fields.
308 cert_fields->Append(node_details = new base::DictionaryValue());
309 node_details->SetString("label",
310 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION));
311 std::string version = x509_certificate_model::GetVersion(cert);
312 if (!version.empty())
313 node_details->SetString("payload.val",
314 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT,
315 base::UTF8ToUTF16(version)));
317 cert_fields->Append(node_details = new base::DictionaryValue());
318 node_details->SetString("label",
319 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER));
320 node_details->SetString("payload.val",
321 x509_certificate_model::GetSerialNumberHexified(cert,
322 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT)));
324 cert_fields->Append(node_details = new base::DictionaryValue());
325 node_details->SetString("label",
326 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
327 node_details->SetString("payload.val",
328 x509_certificate_model::ProcessSecAlgorithmSignature(cert));
330 cert_fields->Append(node_details = new base::DictionaryValue());
331 node_details->SetString("label",
332 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER));
333 node_details->SetString("payload.val",
334 x509_certificate_model::GetIssuerName(cert));
336 // Validity period.
337 cert_fields->Append(node_details = new base::DictionaryValue());
338 node_details->SetString("label",
339 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY));
341 node_details->Set("children", cert_sub_fields = new base::ListValue());
342 cert_sub_fields->Append(node_details = new base::DictionaryValue());
343 node_details->SetString("label",
344 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE));
345 cert_sub_fields->Append(alt_node_details = new base::DictionaryValue());
346 alt_node_details->SetString("label",
347 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER));
348 base::Time issued, expires;
349 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) {
350 node_details->SetString(
351 "payload.val",
352 base::UTF16ToUTF8(
353 base::TimeFormatShortDateAndTimeWithTimeZone(issued)));
354 alt_node_details->SetString(
355 "payload.val",
356 base::UTF16ToUTF8(
357 base::TimeFormatShortDateAndTimeWithTimeZone(expires)));
360 cert_fields->Append(node_details = new base::DictionaryValue());
361 node_details->SetString("label",
362 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT));
363 node_details->SetString("payload.val",
364 x509_certificate_model::GetSubjectName(cert));
366 // Subject key information.
367 cert_fields->Append(node_details = new base::DictionaryValue());
368 node_details->SetString("label",
369 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO));
371 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_DETAILS_SUBJECT_KEY_ALG));
375 node_details->SetString("payload.val",
376 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert));
377 cert_sub_fields->Append(node_details = new base::DictionaryValue());
378 node_details->SetString("label",
379 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY));
380 node_details->SetString("payload.val",
381 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert));
383 // Extensions.
384 x509_certificate_model::Extensions extensions;
385 x509_certificate_model::GetExtensions(
386 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL),
387 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL),
388 cert, &extensions);
390 if (!extensions.empty()) {
391 cert_fields->Append(node_details = new base::DictionaryValue());
392 node_details->SetString("label",
393 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS));
395 node_details->Set("children", cert_sub_fields = new base::ListValue());
396 for (x509_certificate_model::Extensions::const_iterator i =
397 extensions.begin(); i != extensions.end(); ++i) {
398 cert_sub_fields->Append(node_details = new base::DictionaryValue());
399 node_details->SetString("label", i->name);
400 node_details->SetString("payload.val", i->value);
404 cert_fields->Append(node_details = new base::DictionaryValue());
405 node_details->SetString("label",
406 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
407 node_details->SetString("payload.val",
408 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert));
410 cert_fields->Append(node_details = new base::DictionaryValue());
411 node_details->SetString("label",
412 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE));
413 node_details->SetString("payload.val",
414 x509_certificate_model::ProcessRawBitsSignatureWrap(cert));
416 cert_fields->Append(node_details = new base::DictionaryValue());
417 node_details->SetString("label",
418 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP));
419 node_details->Set("children", cert_sub_fields = new base::ListValue());
421 cert_sub_fields->Append(node_details = new base::DictionaryValue());
422 node_details->SetString("label",
423 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL));
424 node_details->SetString("payload.val",
425 x509_certificate_model::HashCertSHA256(cert));
426 cert_sub_fields->Append(node_details = new base::DictionaryValue());
427 node_details->SetString("label",
428 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL));
429 node_details->SetString("payload.val",
430 x509_certificate_model::HashCertSHA1(cert));
432 // Send certificate information to javascript.
433 web_ui()->CallJavascriptFunction("cert_viewer.getCertificateFields",
434 root_list);
437 int CertificateViewerDialogHandler::GetCertificateIndex(
438 const base::ListValue* args) const {
439 int cert_index;
440 double val;
441 if (!(args->GetDouble(0, &val)))
442 return -1;
443 cert_index = static_cast<int>(val);
444 if (cert_index < 0 || cert_index >= static_cast<int>(cert_chain_.size()))
445 return -1;
446 return cert_index;