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/ui/webui/certificate_viewer_webui.h" | 5 #include "chrome/browser/ui/webui/certificate_viewer_webui.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include <utility> | 8 #include <utility> |
9 | 9 |
10 #include "base/bind.h" | 10 #include "base/bind.h" |
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
284 } | 284 } |
285 | 285 |
286 void CertificateViewerDialogHandler::RequestCertificateFields( | 286 void CertificateViewerDialogHandler::RequestCertificateFields( |
287 const base::ListValue* args) { | 287 const base::ListValue* args) { |
288 int cert_index = GetCertificateIndex(args); | 288 int cert_index = GetCertificateIndex(args); |
289 if (cert_index < 0) | 289 if (cert_index < 0) |
290 return; | 290 return; |
291 | 291 |
292 net::X509Certificate::OSCertHandle cert = cert_chain_[cert_index]; | 292 net::X509Certificate::OSCertHandle cert = cert_chain_[cert_index]; |
293 | 293 |
294 // In the following the list.Append(...); list.GetDictionary(...); pattern is | |
295 // necessary to obtain a raw dict pointer to the last element of the list. | |
294 base::ListValue root_list; | 296 base::ListValue root_list; |
295 base::DictionaryValue* node_details; | 297 base::DictionaryValue* node_details; |
296 base::DictionaryValue* alt_node_details; | 298 base::DictionaryValue* alt_node_details; |
297 base::ListValue* cert_sub_fields; | 299 base::ListValue* cert_sub_fields; |
298 root_list.Append( | 300 root_list.Append(base::MakeUnique<base::DictionaryValue>()); |
299 base::WrapUnique(node_details = new base::DictionaryValue())); | 301 root_list.GetDictionary(root_list.GetSize() - 1, &node_details); |
flackr
2017/03/15 15:29:39
I think this would be a lot cleaner and more effic
jdoerrie
2017/03/23 18:11:17
Done. This turned out to be quite tricky with the
flackr
2017/03/23 19:23:57
I see that, thanks for bearing with me on this. I
| |
300 node_details->SetString("label", x509_certificate_model::GetTitle(cert)); | 302 node_details->SetString("label", x509_certificate_model::GetTitle(cert)); |
301 | 303 |
302 base::ListValue* cert_fields; | 304 base::ListValue* cert_fields; |
303 node_details->Set("children", cert_fields = new base::ListValue()); | 305 node_details->Set("children", cert_fields = new base::ListValue()); |
304 cert_fields->Append( | 306 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
305 base::WrapUnique(node_details = new base::DictionaryValue())); | 307 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
306 | 308 |
307 node_details->SetString("label", | 309 node_details->SetString("label", |
308 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE)); | 310 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE)); |
309 node_details->Set("children", cert_fields = new base::ListValue()); | 311 node_details->Set("children", cert_fields = new base::ListValue()); |
310 | 312 |
311 // Main certificate fields. | 313 // Main certificate fields. |
312 cert_fields->Append( | 314 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
313 base::WrapUnique(node_details = new base::DictionaryValue())); | 315 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
314 node_details->SetString("label", | 316 node_details->SetString("label", |
315 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION)); | 317 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VERSION)); |
316 std::string version = x509_certificate_model::GetVersion(cert); | 318 std::string version = x509_certificate_model::GetVersion(cert); |
317 if (!version.empty()) | 319 if (!version.empty()) |
318 node_details->SetString("payload.val", | 320 node_details->SetString("payload.val", |
319 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT, | 321 l10n_util::GetStringFUTF8(IDS_CERT_DETAILS_VERSION_FORMAT, |
320 base::UTF8ToUTF16(version))); | 322 base::UTF8ToUTF16(version))); |
321 | 323 |
322 cert_fields->Append( | 324 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
323 base::WrapUnique(node_details = new base::DictionaryValue())); | 325 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
324 node_details->SetString("label", | 326 node_details->SetString("label", |
325 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER)); | 327 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SERIAL_NUMBER)); |
326 node_details->SetString("payload.val", | 328 node_details->SetString("payload.val", |
327 x509_certificate_model::GetSerialNumberHexified(cert, | 329 x509_certificate_model::GetSerialNumberHexified(cert, |
328 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT))); | 330 l10n_util::GetStringUTF8(IDS_CERT_INFO_FIELD_NOT_PRESENT))); |
329 | 331 |
330 cert_fields->Append( | 332 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
331 base::WrapUnique(node_details = new base::DictionaryValue())); | 333 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
332 node_details->SetString("label", | 334 node_details->SetString("label", |
333 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); | 335 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); |
334 node_details->SetString("payload.val", | 336 node_details->SetString("payload.val", |
335 x509_certificate_model::ProcessSecAlgorithmSignature(cert)); | 337 x509_certificate_model::ProcessSecAlgorithmSignature(cert)); |
336 | 338 |
337 cert_fields->Append( | 339 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
338 base::WrapUnique(node_details = new base::DictionaryValue())); | 340 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
339 node_details->SetString("label", | 341 node_details->SetString("label", |
340 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER)); | 342 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_ISSUER)); |
341 node_details->SetString("payload.val", | 343 node_details->SetString("payload.val", |
342 x509_certificate_model::GetIssuerName(cert)); | 344 x509_certificate_model::GetIssuerName(cert)); |
343 | 345 |
344 // Validity period. | 346 // Validity period. |
345 cert_fields->Append( | 347 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
346 base::WrapUnique(node_details = new base::DictionaryValue())); | 348 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
347 node_details->SetString("label", | 349 node_details->SetString("label", |
348 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY)); | 350 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_VALIDITY)); |
349 | 351 |
350 node_details->Set("children", cert_sub_fields = new base::ListValue()); | 352 node_details->Set("children", cert_sub_fields = new base::ListValue()); |
351 cert_sub_fields->Append( | 353 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
352 base::WrapUnique(node_details = new base::DictionaryValue())); | 354 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, &node_details); |
353 node_details->SetString("label", | 355 node_details->SetString("label", |
354 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE)); | 356 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_BEFORE)); |
355 cert_sub_fields->Append( | 357 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
356 base::WrapUnique(alt_node_details = new base::DictionaryValue())); | 358 // |Append| might have invalidated |node_details|, which is why it needs to be |
359 // queried again. | |
360 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 2, &node_details); | |
361 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, | |
362 &alt_node_details); | |
357 alt_node_details->SetString("label", | 363 alt_node_details->SetString("label", |
358 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER)); | 364 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_NOT_AFTER)); |
359 base::Time issued, expires; | 365 base::Time issued, expires; |
360 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) { | 366 if (x509_certificate_model::GetTimes(cert, &issued, &expires)) { |
361 node_details->SetString( | 367 node_details->SetString( |
362 "payload.val", | 368 "payload.val", |
363 base::UTF16ToUTF8( | 369 base::UTF16ToUTF8( |
364 base::TimeFormatShortDateAndTimeWithTimeZone(issued))); | 370 base::TimeFormatShortDateAndTimeWithTimeZone(issued))); |
365 alt_node_details->SetString( | 371 alt_node_details->SetString( |
366 "payload.val", | 372 "payload.val", |
367 base::UTF16ToUTF8( | 373 base::UTF16ToUTF8( |
368 base::TimeFormatShortDateAndTimeWithTimeZone(expires))); | 374 base::TimeFormatShortDateAndTimeWithTimeZone(expires))); |
369 } | 375 } |
370 | 376 |
371 cert_fields->Append( | 377 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
372 base::WrapUnique(node_details = new base::DictionaryValue())); | 378 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
373 node_details->SetString("label", | 379 node_details->SetString("label", |
374 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT)); | 380 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT)); |
375 node_details->SetString("payload.val", | 381 node_details->SetString("payload.val", |
376 x509_certificate_model::GetSubjectName(cert)); | 382 x509_certificate_model::GetSubjectName(cert)); |
377 | 383 |
378 // Subject key information. | 384 // Subject key information. |
379 cert_fields->Append( | 385 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
380 base::WrapUnique(node_details = new base::DictionaryValue())); | 386 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
381 node_details->SetString("label", | 387 node_details->SetString("label", |
382 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO)); | 388 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_INFO)); |
383 | 389 |
384 node_details->Set("children", cert_sub_fields = new base::ListValue()); | 390 node_details->Set("children", cert_sub_fields = new base::ListValue()); |
385 cert_sub_fields->Append( | 391 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
386 base::WrapUnique(node_details = new base::DictionaryValue())); | 392 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, &node_details); |
387 node_details->SetString("label", | 393 node_details->SetString("label", |
388 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG)); | 394 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY_ALG)); |
389 node_details->SetString("payload.val", | 395 node_details->SetString("payload.val", |
390 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert)); | 396 x509_certificate_model::ProcessSecAlgorithmSubjectPublicKey(cert)); |
391 cert_sub_fields->Append( | 397 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
392 base::WrapUnique(node_details = new base::DictionaryValue())); | 398 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, &node_details); |
393 node_details->SetString("label", | 399 node_details->SetString("label", |
394 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY)); | 400 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_SUBJECT_KEY)); |
395 node_details->SetString("payload.val", | 401 node_details->SetString("payload.val", |
396 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert)); | 402 x509_certificate_model::ProcessSubjectPublicKeyInfo(cert)); |
397 | 403 |
398 // Extensions. | 404 // Extensions. |
399 x509_certificate_model::Extensions extensions; | 405 x509_certificate_model::Extensions extensions; |
400 x509_certificate_model::GetExtensions( | 406 x509_certificate_model::GetExtensions( |
401 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL), | 407 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_CRITICAL), |
402 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL), | 408 l10n_util::GetStringUTF8(IDS_CERT_EXTENSION_NON_CRITICAL), |
403 cert, &extensions); | 409 cert, &extensions); |
404 | 410 |
405 if (!extensions.empty()) { | 411 if (!extensions.empty()) { |
406 cert_fields->Append( | 412 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
407 base::WrapUnique(node_details = new base::DictionaryValue())); | 413 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
408 node_details->SetString("label", | 414 node_details->SetString("label", |
409 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS)); | 415 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_EXTENSIONS)); |
410 | 416 |
411 node_details->Set("children", cert_sub_fields = new base::ListValue()); | 417 node_details->Set("children", cert_sub_fields = new base::ListValue()); |
412 for (x509_certificate_model::Extensions::const_iterator i = | 418 for (x509_certificate_model::Extensions::const_iterator i = |
413 extensions.begin(); i != extensions.end(); ++i) { | 419 extensions.begin(); i != extensions.end(); ++i) { |
414 cert_sub_fields->Append( | 420 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
415 base::WrapUnique(node_details = new base::DictionaryValue())); | 421 cert_sub_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
416 node_details->SetString("label", i->name); | 422 node_details->SetString("label", i->name); |
417 node_details->SetString("payload.val", i->value); | 423 node_details->SetString("payload.val", i->value); |
418 } | 424 } |
419 } | 425 } |
420 | 426 |
421 cert_fields->Append( | 427 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
422 base::WrapUnique(node_details = new base::DictionaryValue())); | 428 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
423 node_details->SetString("label", | 429 node_details->SetString("label", |
424 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); | 430 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_ALG)); |
425 node_details->SetString("payload.val", | 431 node_details->SetString("payload.val", |
426 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert)); | 432 x509_certificate_model::ProcessSecAlgorithmSignatureWrap(cert)); |
427 | 433 |
428 cert_fields->Append( | 434 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
429 base::WrapUnique(node_details = new base::DictionaryValue())); | 435 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
430 node_details->SetString("label", | 436 node_details->SetString("label", |
431 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE)); | 437 l10n_util::GetStringUTF8(IDS_CERT_DETAILS_CERTIFICATE_SIG_VALUE)); |
432 node_details->SetString("payload.val", | 438 node_details->SetString("payload.val", |
433 x509_certificate_model::ProcessRawBitsSignatureWrap(cert)); | 439 x509_certificate_model::ProcessRawBitsSignatureWrap(cert)); |
434 | 440 |
435 cert_fields->Append( | 441 cert_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
436 base::WrapUnique(node_details = new base::DictionaryValue())); | 442 cert_fields->GetDictionary(cert_fields->GetSize() - 1, &node_details); |
437 node_details->SetString("label", | 443 node_details->SetString("label", |
438 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP)); | 444 l10n_util::GetStringUTF8(IDS_CERT_INFO_FINGERPRINTS_GROUP)); |
439 node_details->Set("children", cert_sub_fields = new base::ListValue()); | 445 node_details->Set("children", cert_sub_fields = new base::ListValue()); |
440 | 446 |
441 cert_sub_fields->Append( | 447 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
442 base::WrapUnique(node_details = new base::DictionaryValue())); | 448 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, &node_details); |
443 node_details->SetString("label", | 449 node_details->SetString("label", |
444 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL)); | 450 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA256_FINGERPRINT_LABEL)); |
445 node_details->SetString("payload.val", | 451 node_details->SetString("payload.val", |
446 x509_certificate_model::HashCertSHA256(cert)); | 452 x509_certificate_model::HashCertSHA256(cert)); |
447 cert_sub_fields->Append( | 453 cert_sub_fields->Append(base::MakeUnique<base::DictionaryValue>()); |
448 base::WrapUnique(node_details = new base::DictionaryValue())); | 454 cert_sub_fields->GetDictionary(cert_sub_fields->GetSize() - 1, &node_details); |
449 node_details->SetString("label", | 455 node_details->SetString("label", |
450 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL)); | 456 l10n_util::GetStringUTF8(IDS_CERT_INFO_SHA1_FINGERPRINT_LABEL)); |
451 node_details->SetString("payload.val", | 457 node_details->SetString("payload.val", |
452 x509_certificate_model::HashCertSHA1(cert)); | 458 x509_certificate_model::HashCertSHA1(cert)); |
453 | 459 |
454 // Send certificate information to javascript. | 460 // Send certificate information to javascript. |
455 web_ui()->CallJavascriptFunctionUnsafe("cert_viewer.getCertificateFields", | 461 web_ui()->CallJavascriptFunctionUnsafe("cert_viewer.getCertificateFields", |
456 root_list); | 462 root_list); |
457 } | 463 } |
458 | 464 |
459 int CertificateViewerDialogHandler::GetCertificateIndex( | 465 int CertificateViewerDialogHandler::GetCertificateIndex( |
460 const base::ListValue* args) const { | 466 const base::ListValue* args) const { |
461 int cert_index; | 467 int cert_index; |
462 double val; | 468 double val; |
463 if (!(args->GetDouble(0, &val))) | 469 if (!(args->GetDouble(0, &val))) |
464 return -1; | 470 return -1; |
465 cert_index = static_cast<int>(val); | 471 cert_index = static_cast<int>(val); |
466 if (cert_index < 0 || cert_index >= static_cast<int>(cert_chain_.size())) | 472 if (cert_index < 0 || cert_index >= static_cast<int>(cert_chain_.size())) |
467 return -1; | 473 return -1; |
468 return cert_index; | 474 return cert_index; |
469 } | 475 } |
OLD | NEW |