OLD | NEW |
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 "chrome/browser/ui/webui/certificate_viewer_ui.h" | 5 #include "chrome/browser/ui/webui/certificate_viewer_ui.h" |
6 | 6 |
7 #include "base/string_number_conversions.h" | |
8 #include "base/i18n/time_formatting.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "chrome/browser/profiles/profile.h" | 7 #include "chrome/browser/profiles/profile.h" |
11 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 8 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
12 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" | 9 #include "chrome/browser/ui/webui/chrome_web_ui_data_source.h" |
13 #include "chrome/common/url_constants.h" | 10 #include "chrome/common/url_constants.h" |
14 #include "chrome/common/net/x509_certificate_model.h" | 11 #include "chrome/common/net/x509_certificate_model.h" |
15 #include "content/browser/cert_store.h" | |
16 #include "grit/browser_resources.h" | 12 #include "grit/browser_resources.h" |
17 #include "grit/generated_resources.h" | 13 #include "grit/generated_resources.h" |
18 #include "net/base/x509_certificate.h" | |
19 #include "ui/base/l10n/l10n_util.h" | 14 #include "ui/base/l10n/l10n_util.h" |
20 | 15 |
21 CertificateViewerUI::CertificateViewerUI(TabContents* contents) | 16 CertificateViewerUI::CertificateViewerUI(TabContents* contents) |
22 : HtmlDialogUI(contents) { | 17 : HtmlDialogUI(contents) { |
23 | 18 |
24 // Set up the chrome://view-cert source. | 19 // Set up the chrome://view-cert source. |
25 ChromeWebUIDataSource* html_source = | 20 ChromeWebUIDataSource* html_source = |
26 new ChromeWebUIDataSource(chrome::kChromeUICertificateViewerHost); | 21 new ChromeWebUIDataSource(chrome::kChromeUICertificateViewerHost); |
27 | 22 |
28 // Register callback handler to retrieve certificate information. | |
29 RegisterMessageCallback("requestCertificateInfo", | |
30 NewCallback(this, &CertificateViewerUI::RequestCertificateInfo)); | |
31 | |
32 // Localized strings. | 23 // Localized strings. |
33 html_source->AddLocalizedString("general", IDS_CERT_INFO_GENERAL_TAB_LABEL); | 24 html_source->AddLocalizedString("general", IDS_CERT_INFO_GENERAL_TAB_LABEL); |
34 html_source->AddLocalizedString("details", IDS_CERT_INFO_DETAILS_TAB_LABEL); | 25 html_source->AddLocalizedString("details", IDS_CERT_INFO_DETAILS_TAB_LABEL); |
35 html_source->AddLocalizedString("close", IDS_CLOSE); | 26 html_source->AddLocalizedString("close", IDS_CLOSE); |
| 27 html_source->AddLocalizedString("export", |
| 28 IDS_CERT_DETAILS_EXPORT_CERTIFICATE); |
36 html_source->AddLocalizedString("usages", | 29 html_source->AddLocalizedString("usages", |
37 IDS_CERT_INFO_VERIFIED_USAGES_GROUP); | 30 IDS_CERT_INFO_VERIFIED_USAGES_GROUP); |
38 html_source->AddLocalizedString("issuedTo", IDS_CERT_INFO_SUBJECT_GROUP); | 31 html_source->AddLocalizedString("issuedTo", IDS_CERT_INFO_SUBJECT_GROUP); |
39 html_source->AddLocalizedString("issuedBy", IDS_CERT_INFO_ISSUER_GROUP); | 32 html_source->AddLocalizedString("issuedBy", IDS_CERT_INFO_ISSUER_GROUP); |
40 html_source->AddLocalizedString("cn", IDS_CERT_INFO_COMMON_NAME_LABEL); | 33 html_source->AddLocalizedString("cn", IDS_CERT_INFO_COMMON_NAME_LABEL); |
41 html_source->AddLocalizedString("o", IDS_CERT_INFO_ORGANIZATION_LABEL); | 34 html_source->AddLocalizedString("o", IDS_CERT_INFO_ORGANIZATION_LABEL); |
42 html_source->AddLocalizedString("ou", | 35 html_source->AddLocalizedString("ou", |
43 IDS_CERT_INFO_ORGANIZATIONAL_UNIT_LABEL); | 36 IDS_CERT_INFO_ORGANIZATIONAL_UNIT_LABEL); |
44 html_source->AddLocalizedString("sn", IDS_CERT_INFO_SERIAL_NUMBER_LABEL); | 37 html_source->AddLocalizedString("sn", IDS_CERT_INFO_SERIAL_NUMBER_LABEL); |
45 html_source->AddLocalizedString("validity", IDS_CERT_INFO_VALIDITY_GROUP); | 38 html_source->AddLocalizedString("validity", IDS_CERT_INFO_VALIDITY_GROUP); |
(...skipping 18 matching lines...) Expand all Loading... |
64 html_source->add_resource_path("certificate_viewer.css", | 57 html_source->add_resource_path("certificate_viewer.css", |
65 IDR_CERTIFICATE_VIEWER_CSS); | 58 IDR_CERTIFICATE_VIEWER_CSS); |
66 html_source->set_default_resource(IDR_CERTIFICATE_VIEWER_HTML); | 59 html_source->set_default_resource(IDR_CERTIFICATE_VIEWER_HTML); |
67 | 60 |
68 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); | 61 Profile* profile = Profile::FromBrowserContext(contents->browser_context()); |
69 profile->GetChromeURLDataManager()->AddDataSource(html_source); | 62 profile->GetChromeURLDataManager()->AddDataSource(html_source); |
70 } | 63 } |
71 | 64 |
72 CertificateViewerUI::~CertificateViewerUI() { | 65 CertificateViewerUI::~CertificateViewerUI() { |
73 } | 66 } |
74 | |
75 // TODO(flackr): This is duplicated from cookies_view_handler.cc | |
76 // Decodes a pointer from a hex string. | |
77 void* HexStringToPointer(const std::string& str) { | |
78 std::vector<uint8> buffer; | |
79 if (!base::HexStringToBytes(str, &buffer) || | |
80 buffer.size() != sizeof(void*)) { | |
81 return NULL; | |
82 } | |
83 | |
84 return *reinterpret_cast<void**>(&buffer[0]); | |
85 } | |
86 | |
87 // Returns the certificate information of the requested certificate id from | |
88 // the CertStore to the javascript handler. | |
89 void CertificateViewerUI::RequestCertificateInfo(const ListValue* args) { | |
90 // The certificate id should be in the first argument. | |
91 std::string val; | |
92 if (!(args->GetString(0, &val))) { | |
93 return; | |
94 } | |
95 net::X509Certificate* cert = static_cast<net::X509Certificate*>( | |
96 HexStringToPointer(val)); | |
97 | |
98 // Certificate information. The keys in this dictionary's general key | |
99 // correspond to the IDs in the Html page. | |
100 DictionaryValue cert_info; | |
101 net::X509Certificate::OSCertHandle cert_hnd = cert->os_cert_handle(); | |
102 | |
103 // Get the certificate chain. | |
104 net::X509Certificate::OSCertHandles cert_chain; | |
105 x509_certificate_model::GetCertChainFromCert(cert_hnd, &cert_chain); | |
106 | |
107 // Certificate usage. | |
108 std::vector<std::string> usages; | |
109 x509_certificate_model::GetUsageStrings(cert_hnd, &usages); | |
110 std::string usagestr; | |
111 for (std::vector<std::string>::iterator it = usages.begin(); | |
112 it != usages.end(); ++it) { | |
113 if (usagestr.length() > 0) { | |
114 usagestr += "\n"; | |
115 } | |
116 usagestr += *it; | |
117 } | |
118 cert_info.SetString("general.usages", usagestr); | |
119 | |
120 // Standard certificate details. | |
121 const std::string alternative_text = | |
122 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT); | |
123 cert_info.SetString("general.title", l10n_util::GetStringFUTF8( | |
124 IDS_CERT_INFO_DIALOG_TITLE, UTF8ToUTF16(x509_certificate_model::GetTitle( | |
125 cert_chain.front())))); | |
126 | |
127 // Issued to information. | |
128 cert_info.SetString("general.issued-cn", | |
129 x509_certificate_model::GetSubjectCommonName(cert_hnd, alternative_text)); | |
130 cert_info.SetString("general.issued-o", | |
131 x509_certificate_model::GetSubjectOrgName(cert_hnd, alternative_text)); | |
132 cert_info.SetString("general.issued-ou", | |
133 x509_certificate_model::GetSubjectOrgUnitName(cert_hnd, | |
134 alternative_text)); | |
135 cert_info.SetString("general.issued-sn", | |
136 x509_certificate_model::GetSerialNumberHexified(cert_hnd, | |
137 alternative_text)); | |
138 | |
139 // Issuer information. | |
140 cert_info.SetString("general.issuer-cn", | |
141 x509_certificate_model::GetIssuerCommonName(cert_hnd, alternative_text)); | |
142 cert_info.SetString("general.issuer-o", | |
143 x509_certificate_model::GetIssuerOrgName(cert_hnd, alternative_text)); | |
144 cert_info.SetString("general.issuer-ou", | |
145 x509_certificate_model::GetIssuerOrgUnitName(cert_hnd, alternative_text)); | |
146 | |
147 // Validity period. | |
148 base::Time issued, expires; | |
149 std::string issued_str, expires_str; | |
150 if (x509_certificate_model::GetTimes(cert_hnd, &issued, &expires)) { | |
151 issued_str = UTF16ToUTF8( | |
152 base::TimeFormatShortDateNumeric(issued)); | |
153 expires_str = UTF16ToUTF8( | |
154 base::TimeFormatShortDateNumeric(expires)); | |
155 } else { | |
156 issued_str = alternative_text; | |
157 expires_str = alternative_text; | |
158 } | |
159 cert_info.SetString("general.issue-date", issued_str); | |
160 cert_info.SetString("general.expiry-date", expires_str); | |
161 | |
162 cert_info.SetString("general.sha256", | |
163 x509_certificate_model::HashCertSHA256(cert_hnd)); | |
164 cert_info.SetString("general.sha1", | |
165 x509_certificate_model::HashCertSHA1(cert_hnd)); | |
166 | |
167 // Certificate hierarchy is constructed from bottom up. | |
168 ListValue* children = NULL; | |
169 for (net::X509Certificate::OSCertHandles::const_iterator i = | |
170 cert_chain.begin(); i != cert_chain.end(); ++i) { | |
171 DictionaryValue* cert_node = new DictionaryValue(); | |
172 ListValue cert_details; | |
173 cert_node->SetString("label", x509_certificate_model::GetTitle(*i).c_str()); | |
174 cert_node->Set("payload.fields", GetCertificateFields(*i)); | |
175 // Add the child from the previous iteration. | |
176 if (children) | |
177 cert_node->Set("children", children); | |
178 | |
179 // Add this node to the children list for the next iteration. | |
180 children = new ListValue(); | |
181 children->Append(cert_node); | |
182 } | |
183 // Set the last node as the top of the certificate hierarchy. | |
184 cert_info.Set("hierarchy", children); | |
185 | |
186 // Send certificate information to javascript. | |
187 CallJavascriptFunction("cert_viewer.getCertificateInfo", cert_info); | |
188 } | |
189 | |
190 ListValue* CertificateViewerUI::GetCertificateFields( | |
191 net::X509Certificate::OSCertHandle cert) { | |
192 ListValue* root_list = new ListValue(); | |
193 DictionaryValue* node_details; | |
194 DictionaryValue* alt_node_details; | |
195 ListValue* cert_sub_fields; | |
196 root_list->Append(node_details = new DictionaryValue()); | |
197 node_details->SetString("label", x509_certificate_model::GetTitle(cert)); | |
198 | |
199 ListValue* cert_fields; | |
200 node_details->Set("children", cert_fields = new ListValue()); | |
201 cert_fields->Append(node_details = new DictionaryValue()); | |
202 | |
203 node_details->SetString("label", | |
204 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE)); | |
205 node_details->Set("children", cert_fields = new ListValue()); | |
206 | |
207 // Main certificate fields. | |
208 cert_fields->Append(node_details = new DictionaryValue()); | |
209 node_details->SetString("label", | |
210 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION)); | |
211 std::string version = x509_certificate_model::GetVersion(cert); | |
212 if (!version.empty()) | |
213 node_details->SetString("payload.val", | |
214 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT, | |
215 UTF8ToUTF16(version))); | |
216 | |
217 cert_fields->Append(node_details = new DictionaryValue()); | |
218 node_details->SetString("label", | |
219 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER)); | |
220 node_details->SetString("payload.val", | |
221 x509_certificate_model::GetSerialNumberHexified(cert, | |
222 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT))); | |
223 | |
224 cert_fields->Append(node_details = new DictionaryValue()); | |
225 node_details->SetString("label", | |
226 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); | |
227 node_details->SetString("payload.val", | |
228 x509_certificate_model::ProcessSecAlgorithmSignature(cert)); | |
229 | |
230 cert_fields->Append(node_details = new DictionaryValue()); | |
231 node_details->SetString("label", | |
232 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER)); | |
233 node_details->SetString("payload.val", | |
234 x509_certificate_model::GetIssuerName(cert)); | |
235 | |
236 // Validity period. | |
237 cert_fields->Append(node_details = new DictionaryValue()); | |
238 node_details->SetString("label", | |
239 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY)); | |
240 | |
241 node_details->Set("children", cert_sub_fields = new ListValue()); | |
242 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
243 node_details->SetString("label", | |
244 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE)); | |
245 cert_sub_fields->Append(alt_node_details = new DictionaryValue()); | |
246 alt_node_details->SetString("label", | |
247 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER)); | |
248 base::Time issued, expires; | |
249 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) { | |
250 node_details->SetString("payload.val", | |
251 UTF16ToUTF8(base::TimeFormatShortDateAndTime(issued))); | |
252 alt_node_details->SetString("payload.val", | |
253 UTF16ToUTF8(base::TimeFormatShortDateAndTime(expires))); | |
254 } | |
255 | |
256 cert_fields->Append(node_details = new DictionaryValue()); | |
257 node_details->SetString("label", | |
258 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT)); | |
259 node_details->SetString("payload.val", | |
260 x509_certificate_model::GetSubjectName(cert)); | |
261 | |
262 // Subject key information. | |
263 cert_fields->Append(node_details = new DictionaryValue()); | |
264 node_details->SetString("label", | |
265 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO)); | |
266 | |
267 node_details->Set("children", cert_sub_fields = new ListValue()); | |
268 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
269 node_details->SetString("label", | |
270 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG)); | |
271 node_details->SetString("payload.val", | |
272 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert)); | |
273 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
274 node_details->SetString("label", | |
275 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY)); | |
276 node_details->SetString("payload.val", | |
277 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert)); | |
278 | |
279 // Extensions. | |
280 x509_certificate_model::Extensions extensions; | |
281 x509_certificate_model::GetExtensions( | |
282 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL), | |
283 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL), | |
284 cert, &extensions); | |
285 | |
286 if (!extensions.empty()) { | |
287 cert_fields->Append(node_details = new DictionaryValue()); | |
288 node_details->SetString("label", | |
289 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS)); | |
290 | |
291 node_details->Set("children", cert_sub_fields = new ListValue()); | |
292 for (x509_certificate_model::Extensions::const_iterator i = | |
293 extensions.begin(); i != extensions.end(); ++i) { | |
294 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
295 node_details->SetString("label", i->name); | |
296 node_details->SetString("payload.val", i->value); | |
297 } | |
298 } | |
299 | |
300 cert_fields->Append(node_details = new DictionaryValue()); | |
301 node_details->SetString("label", | |
302 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); | |
303 node_details->SetString("payload.val", | |
304 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert)); | |
305 | |
306 cert_fields->Append(node_details = new DictionaryValue()); | |
307 node_details->SetString("label", | |
308 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE)); | |
309 node_details->SetString("payload.val", | |
310 x509_certificate_model::ProcessRawBitsSignatureWrap(cert)); | |
311 | |
312 cert_fields->Append(node_details = new DictionaryValue()); | |
313 node_details->SetString("label", | |
314 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP)); | |
315 node_details->Set("children", cert_sub_fields = new ListValue()); | |
316 | |
317 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
318 node_details->SetString("label", | |
319 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL)); | |
320 node_details->SetString("payload.val", | |
321 x509_certificate_model::HashCertSHA256(cert)); | |
322 cert_sub_fields->Append(node_details = new DictionaryValue()); | |
323 node_details->SetString("label", | |
324 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL)); | |
325 node_details->SetString("payload.val", | |
326 x509_certificate_model::HashCertSHA1(cert)); | |
327 return root_list; | |
328 } | |
329 | |
OLD | NEW |