Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(956)

Side by Side Diff: chrome/browser/ui/webui/certificate_viewer.cc

Issue 7528027: Add export function to WebUI certificate viewer. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 "base/json/json_writer.h" 5 #include "base/i18n/time_formatting.h"
6 #include "base/utf_string_conversions.h" 6 #include "base/utf_string_conversions.h"
7 #include "base/string_number_conversions.h" 7 #include "base/string_number_conversions.h"
8 #include "chrome/browser/ui/webui/certificate_viewer.h" 8 #include "chrome/browser/ui/webui/certificate_viewer.h"
9 #include "chrome/common/url_constants.h" 9 #include "chrome/common/url_constants.h"
10 #include "chrome/browser/ui/browser.h" 10 #include "chrome/browser/ui/browser.h"
11 #include "chrome/browser/ui/browser_list.h" 11 #include "chrome/browser/ui/browser_list.h"
12 #include "chrome/browser/ui/gtk/certificate_dialogs.h"
12 #include "chrome/browser/ui/views/html_dialog_view.h" 13 #include "chrome/browser/ui/views/html_dialog_view.h"
13 #include "chrome/browser/ui/browser_dialogs.h" 14 #include "chrome/browser/ui/browser_dialogs.h"
14 #include "chrome/common/net/x509_certificate_model.h" 15 #include "chrome/common/net/x509_certificate_model.h"
16 #include "content/browser/tab_contents/tab_contents.h"
15 #include "ui/base/l10n/l10n_util.h" 17 #include "ui/base/l10n/l10n_util.h"
16 #include "grit/generated_resources.h" 18 #include "grit/generated_resources.h"
17 19
18 namespace { 20 namespace {
19 21
20 // Default width/height of the dialog. 22 // Default width/height of the dialog.
21 const int kDefaultWidth = 450; 23 const int kDefaultWidth = 450;
22 const int kDefaultHeight = 450; 24 const int kDefaultHeight = 450;
23 25
24 } // namespace 26 } // namespace
25 27
26 // Shows a certificate using the WebUI certificate viewer. 28 // Shows a certificate using the WebUI certificate viewer.
27 void ShowCertificateViewer(gfx::NativeWindow parent, 29 void ShowCertificateViewer(gfx::NativeWindow parent,
28 net::X509Certificate* cert) { 30 net::X509Certificate* cert) {
29 CertificateViewerDialog::ShowDialog(parent, cert); 31 CertificateViewerDialog::ShowDialog(parent, cert);
30 } 32 }
31 33
34 ////////////////////////////////////////////////////////////////////////////////
35 // CertificateViewerDialog
36
32 void CertificateViewerDialog::ShowDialog(gfx::NativeWindow owning_window, 37 void CertificateViewerDialog::ShowDialog(gfx::NativeWindow owning_window,
33 net::X509Certificate* cert) { 38 net::X509Certificate* cert) {
34 Browser* browser = BrowserList::GetLastActive(); 39 Browser* browser = BrowserList::GetLastActive();
35 DCHECK(browser); 40 DCHECK(browser);
36 browser->BrowserShowHtmlDialog(new CertificateViewerDialog(cert), 41 browser->BrowserShowHtmlDialog(new CertificateViewerDialog(cert),
37 owning_window); 42 owning_window);
38 } 43 }
39 44
40 // TODO(flackr): This is duplicated from cookies_view_handler.cc
41 // Encodes a pointer value into a hex string.
42 std::string PointerToHexString(const void* pointer) {
43 return base::HexEncode(&pointer, sizeof(pointer));
44 }
45
46 CertificateViewerDialog::CertificateViewerDialog(net::X509Certificate* cert) 45 CertificateViewerDialog::CertificateViewerDialog(net::X509Certificate* cert)
47 : cert_(cert) { 46 : cert_(cert) {
48 // Construct the JSON string with a pointer to the stored certificate.
49 DictionaryValue args;
50 args.SetString("cert", PointerToHexString(cert_));
51 base::JSONWriter::Write(&args, false, &json_args_);
52
53 // Construct the dialog title from the certificate. 47 // Construct the dialog title from the certificate.
54 net::X509Certificate::OSCertHandles cert_chain; 48 net::X509Certificate::OSCertHandles cert_chain;
55 x509_certificate_model::GetCertChainFromCert(cert_->os_cert_handle(), 49 x509_certificate_model::GetCertChainFromCert(cert_->os_cert_handle(),
56 &cert_chain); 50 &cert_chain);
57 title_ = l10n_util::GetStringFUTF16(IDS_CERT_INFO_DIALOG_TITLE, 51 title_ = l10n_util::GetStringFUTF16(IDS_CERT_INFO_DIALOG_TITLE,
58 UTF8ToUTF16(x509_certificate_model::GetTitle(cert_chain.front()))); 52 UTF8ToUTF16(x509_certificate_model::GetTitle(cert_chain.front())));
59 } 53 }
60 54
61 bool CertificateViewerDialog::IsDialogModal() const { 55 bool CertificateViewerDialog::IsDialogModal() const {
62 return false; 56 return false;
63 } 57 }
64 58
65 string16 CertificateViewerDialog::GetDialogTitle() const { 59 string16 CertificateViewerDialog::GetDialogTitle() const {
66 return title_; 60 return title_;
67 } 61 }
68 62
69 GURL CertificateViewerDialog::GetDialogContentURL() const { 63 GURL CertificateViewerDialog::GetDialogContentURL() const {
70 return GURL(chrome::kChromeUICertificateViewerURL); 64 return GURL(chrome::kChromeUICertificateViewerURL);
71 } 65 }
72 66
73 void CertificateViewerDialog::GetWebUIMessageHandlers( 67 void CertificateViewerDialog::GetWebUIMessageHandlers(
74 std::vector<WebUIMessageHandler*>* handlers) const { 68 std::vector<WebUIMessageHandler*>* handlers) const {
69 handlers->push_back(new CertificateViewerDialogHandler(cert_));
75 } 70 }
76 71
77 void CertificateViewerDialog::GetDialogSize(gfx::Size* size) const { 72 void CertificateViewerDialog::GetDialogSize(gfx::Size* size) const {
78 size->SetSize(kDefaultWidth, kDefaultHeight); 73 size->SetSize(kDefaultWidth, kDefaultHeight);
79 } 74 }
80 75
81 std::string CertificateViewerDialog::GetDialogArgs() const { 76 std::string CertificateViewerDialog::GetDialogArgs() const {
82 return json_args_; 77 return std::string();
83 } 78 }
84 79
85 void CertificateViewerDialog::OnDialogClosed(const std::string& json_retval) { 80 void CertificateViewerDialog::OnDialogClosed(const std::string& json_retval) {
86 delete this; 81 delete this;
87 } 82 }
88 83
89 void CertificateViewerDialog::OnCloseContents(TabContents* source, 84 void CertificateViewerDialog::OnCloseContents(TabContents* source,
90 bool* out_close_dialog) { 85 bool* out_close_dialog) {
91 if (out_close_dialog) 86 if (out_close_dialog)
92 *out_close_dialog = true; 87 *out_close_dialog = true;
93 } 88 }
94 89
95 bool CertificateViewerDialog::ShouldShowDialogTitle() const { 90 bool CertificateViewerDialog::ShouldShowDialogTitle() const {
96 return true; 91 return true;
97 } 92 }
98 93
99 bool CertificateViewerDialog::HandleContextMenu( 94 bool CertificateViewerDialog::HandleContextMenu(
100 const ContextMenuParams& params) { 95 const ContextMenuParams& params) {
101 return true; 96 return true;
102 } 97 }
98
99 ////////////////////////////////////////////////////////////////////////////////
100 // CertificateViewerDialogHandler
101
102 CertificateViewerDialogHandler::CertificateViewerDialogHandler(
103 net::X509Certificate* cert) : cert_(cert) {
104 x509_certificate_model::GetCertChainFromCert(cert_->os_cert_handle(),
105 &cert_chain_);
106 }
107
108 void CertificateViewerDialogHandler::RegisterMessages() {
109 web_ui_->RegisterMessageCallback("ExportCertificate",
110 NewCallback(this,
111 &CertificateViewerDialogHandler::ExportCertificate));
112 web_ui_->RegisterMessageCallback("RequestCertificateInfo",
113 NewCallback(this,
114 &CertificateViewerDialogHandler::RequestCertificateInfo));
115 web_ui_->RegisterMessageCallback("RequestCertificateFields",
116 NewCallback(this,
117 &CertificateViewerDialogHandler::RequestCertificateFields));
118 }
119
120 void CertificateViewerDialogHandler::ExportCertificate(
121 const base::ListValue* args) {
122 int cert_index;
123 std::string val;
124 if (!(args->GetString(0, &val)))
Rick Byers 2011/08/09 17:29:18 You should be able to use GetDouble here rather th
flackr 2011/08/09 18:25:15 Done. Both ways seemed a little ugly. I tried usin
Rick Byers 2011/08/09 20:03:41 Yeah, Javascript has only doubles, so at least thi
125 return;
126 if (!(base::StringToInt(val, &cert_index)))
127 return;
128 if (cert_index < 0 || cert_index >= (int)cert_chain_.size())
129 return;
130 ShowCertExportDialog(web_ui_->tab_contents(),
131 web_ui_->tab_contents()->GetDialogRootWindow(),
132 cert_chain_[cert_index]);
133 }
134
flackr 2011/08/09 15:51:27 The following code is moved from chrome/browser/ui
135 void CertificateViewerDialogHandler::RequestCertificateInfo(
136 const base::ListValue* args) {
137 // Certificate information. The keys in this dictionary's general key
138 // correspond to the IDs in the Html page.
139 DictionaryValue cert_info;
140 net::X509Certificate::OSCertHandle cert_hnd = cert_->os_cert_handle();
141
142 // Get the certificate chain.
143 net::X509Certificate::OSCertHandles cert_chain;
144 x509_certificate_model::GetCertChainFromCert(cert_hnd, &cert_chain);
145
146 // Certificate usage.
147 std::vector<std::string> usages;
148 x509_certificate_model::GetUsageStrings(cert_hnd, &usages);
149 std::string usagestr;
150 for (std::vector<std::string>::iterator it = usages.begin();
151 it != usages.end(); ++it) {
152 if (usagestr.length() > 0) {
153 usagestr += "\n";
154 }
155 usagestr += *it;
156 }
157 cert_info.SetString("general.usages", usagestr);
158
159 // Standard certificate details.
160 const std::string alternative_text =
161 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT);
162 cert_info.SetString("general.title", l10n_util::GetStringFUTF8(
163 IDS_CERT_INFO_DIALOG_TITLE, UTF8ToUTF16(x509_certificate_model::GetTitle(
164 cert_chain.front()))));
165
166 // Issued to information.
167 cert_info.SetString("general.issued-cn",
168 x509_certificate_model::GetSubjectCommonName(cert_hnd, alternative_text));
169 cert_info.SetString("general.issued-o",
170 x509_certificate_model::GetSubjectOrgName(cert_hnd, alternative_text));
171 cert_info.SetString("general.issued-ou",
172 x509_certificate_model::GetSubjectOrgUnitName(cert_hnd,
173 alternative_text));
174 cert_info.SetString("general.issued-sn",
175 x509_certificate_model::GetSerialNumberHexified(cert_hnd,
176 alternative_text));
177
178 // Issuer information.
179 cert_info.SetString("general.issuer-cn",
180 x509_certificate_model::GetIssuerCommonName(cert_hnd, alternative_text));
181 cert_info.SetString("general.issuer-o",
182 x509_certificate_model::GetIssuerOrgName(cert_hnd, alternative_text));
183 cert_info.SetString("general.issuer-ou",
184 x509_certificate_model::GetIssuerOrgUnitName(cert_hnd, alternative_text));
185
186 // Validity period.
187 base::Time issued, expires;
188 std::string issued_str, expires_str;
189 if (x509_certificate_model::GetTimes(cert_hnd, &issued, &expires)) {
190 issued_str = UTF16ToUTF8(
191 base::TimeFormatShortDateNumeric(issued));
192 expires_str = UTF16ToUTF8(
193 base::TimeFormatShortDateNumeric(expires));
194 } else {
195 issued_str = alternative_text;
196 expires_str = alternative_text;
197 }
198 cert_info.SetString("general.issue-date", issued_str);
199 cert_info.SetString("general.expiry-date", expires_str);
200
201 cert_info.SetString("general.sha256",
202 x509_certificate_model::HashCertSHA256(cert_hnd));
203 cert_info.SetString("general.sha1",
204 x509_certificate_model::HashCertSHA1(cert_hnd));
205
206 // Certificate hierarchy is constructed from bottom up.
207 ListValue* children = NULL;
208 int index = 0;
209 for (net::X509Certificate::OSCertHandles::const_iterator i =
210 cert_chain.begin(); i != cert_chain.end(); ++i, ++index) {
211 DictionaryValue* cert_node = new DictionaryValue();
212 ListValue cert_details;
213 cert_node->SetString("label", x509_certificate_model::GetTitle(*i).c_str());
214 cert_node->SetString("payload.index", base::IntToString(index));
215 // Add the child from the previous iteration.
216 if (children)
217 cert_node->Set("children", children);
218
219 // Add this node to the children list for the next iteration.
220 children = new ListValue();
221 children->Append(cert_node);
222 }
223 // Set the last node as the top of the certificate hierarchy.
224 cert_info.Set("hierarchy", children);
225
226 // Send certificate information to javascript.
227 web_ui_->CallJavascriptFunction("cert_viewer.getCertificateInfo", cert_info);
228 }
229
230 void CertificateViewerDialogHandler::RequestCertificateFields(
231 const base::ListValue* args) {
232 int cert_index;
233 std::string val;
234 if (!(args->GetString(0, &val)))
235 return;
236 if (!(base::StringToInt(val, &cert_index)))
237 return;
238 if (cert_index < 0 || cert_index >= (int)cert_chain_.size())
239 return;
240 net::X509Certificate::OSCertHandle cert = cert_chain_[cert_index];
241
242 ListValue root_list;
243 DictionaryValue* node_details;
244 DictionaryValue* alt_node_details;
245 ListValue* cert_sub_fields;
246 root_list.Append(node_details = new DictionaryValue());
247 node_details->SetString("label", x509_certificate_model::GetTitle(cert));
248
249 ListValue* cert_fields;
250 node_details->Set("children", cert_fields = new ListValue());
251 cert_fields->Append(node_details = new DictionaryValue());
252
253 node_details->SetString("label",
254 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE));
255 node_details->Set("children", cert_fields = new ListValue());
256
257 // Main certificate fields.
258 cert_fields->Append(node_details = new DictionaryValue());
259 node_details->SetString("label",
260 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION));
261 std::string version = x509_certificate_model::GetVersion(cert);
262 if (!version.empty())
263 node_details->SetString("payload.val",
264 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT,
265 UTF8ToUTF16(version)));
266
267 cert_fields->Append(node_details = new DictionaryValue());
268 node_details->SetString("label",
269 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER));
270 node_details->SetString("payload.val",
271 x509_certificate_model::GetSerialNumberHexified(cert,
272 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT)));
273
274 cert_fields->Append(node_details = new DictionaryValue());
275 node_details->SetString("label",
276 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
277 node_details->SetString("payload.val",
278 x509_certificate_model::ProcessSecAlgorithmSignature(cert));
279
280 cert_fields->Append(node_details = new DictionaryValue());
281 node_details->SetString("label",
282 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER));
283 node_details->SetString("payload.val",
284 x509_certificate_model::GetIssuerName(cert));
285
286 // Validity period.
287 cert_fields->Append(node_details = new DictionaryValue());
288 node_details->SetString("label",
289 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY));
290
291 node_details->Set("children", cert_sub_fields = new ListValue());
292 cert_sub_fields->Append(node_details = new DictionaryValue());
293 node_details->SetString("label",
294 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE));
295 cert_sub_fields->Append(alt_node_details = new DictionaryValue());
296 alt_node_details->SetString("label",
297 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER));
298 base::Time issued, expires;
299 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) {
300 node_details->SetString("payload.val",
301 UTF16ToUTF8(base::TimeFormatShortDateAndTime(issued)));
302 alt_node_details->SetString("payload.val",
303 UTF16ToUTF8(base::TimeFormatShortDateAndTime(expires)));
304 }
305
306 cert_fields->Append(node_details = new DictionaryValue());
307 node_details->SetString("label",
308 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT));
309 node_details->SetString("payload.val",
310 x509_certificate_model::GetSubjectName(cert));
311
312 // Subject key information.
313 cert_fields->Append(node_details = new DictionaryValue());
314 node_details->SetString("label",
315 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO));
316
317 node_details->Set("children", cert_sub_fields = new ListValue());
318 cert_sub_fields->Append(node_details = new DictionaryValue());
319 node_details->SetString("label",
320 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG));
321 node_details->SetString("payload.val",
322 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert));
323 cert_sub_fields->Append(node_details = new DictionaryValue());
324 node_details->SetString("label",
325 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY));
326 node_details->SetString("payload.val",
327 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert));
328
329 // Extensions.
330 x509_certificate_model::Extensions extensions;
331 x509_certificate_model::GetExtensions(
332 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL),
333 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL),
334 cert, &extensions);
335
336 if (!extensions.empty()) {
337 cert_fields->Append(node_details = new DictionaryValue());
338 node_details->SetString("label",
339 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS));
340
341 node_details->Set("children", cert_sub_fields = new ListValue());
342 for (x509_certificate_model::Extensions::const_iterator i =
343 extensions.begin(); i != extensions.end(); ++i) {
344 cert_sub_fields->Append(node_details = new DictionaryValue());
345 node_details->SetString("label", i->name);
346 node_details->SetString("payload.val", i->value);
347 }
348 }
349
350 cert_fields->Append(node_details = new DictionaryValue());
351 node_details->SetString("label",
352 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG));
353 node_details->SetString("payload.val",
354 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert));
355
356 cert_fields->Append(node_details = new DictionaryValue());
357 node_details->SetString("label",
358 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE));
359 node_details->SetString("payload.val",
360 x509_certificate_model::ProcessRawBitsSignatureWrap(cert));
361
362 cert_fields->Append(node_details = new DictionaryValue());
363 node_details->SetString("label",
364 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP));
365 node_details->Set("children", cert_sub_fields = new ListValue());
366
367 cert_sub_fields->Append(node_details = new DictionaryValue());
368 node_details->SetString("label",
369 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL));
370 node_details->SetString("payload.val",
371 x509_certificate_model::HashCertSHA256(cert));
372 cert_sub_fields->Append(node_details = new DictionaryValue());
373 node_details->SetString("label",
374 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL));
375 node_details->SetString("payload.val",
376 x509_certificate_model::HashCertSHA1(cert));
377
378 // Send certificate information to javascript.
379 web_ui_->CallJavascriptFunction("cert_viewer.getCertificateFields",
380 root_list);
381 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698