ct_signed_certificate_timestamp_log_param.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. // Copyright 2013 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.
  4. #include "net/cert/ct_signed_certificate_timestamp_log_param.h"
  5. #include <algorithm>
  6. #include <memory>
  7. #include <utility>
  8. #include "base/base64.h"
  9. #include "base/strings/string_number_conversions.h"
  10. #include "base/values.h"
  11. #include "net/cert/ct_sct_to_string.h"
  12. #include "net/cert/signed_certificate_timestamp.h"
  13. namespace net {
  14. namespace {
  15. // Base64 encode the given |value| string and put it in |dict| with the
  16. // description |key|.
  17. void SetBinaryData(const char* key,
  18. base::StringPiece value,
  19. base::Value::Dict& dict) {
  20. std::string b64_value;
  21. base::Base64Encode(value, &b64_value);
  22. dict.Set(key, b64_value);
  23. }
  24. // Returns a dictionary where each key is a field of the SCT and its value
  25. // is this field's value in the SCT. This dictionary is meant to be used for
  26. // outputting a de-serialized SCT to the NetLog.
  27. base::Value SCTToDictionary(const ct::SignedCertificateTimestamp& sct,
  28. ct::SCTVerifyStatus status) {
  29. base::Value::Dict dict;
  30. dict.Set("origin", OriginToString(sct.origin));
  31. dict.Set("verification_status", StatusToString(status));
  32. dict.Set("version", sct.version);
  33. SetBinaryData("log_id", sct.log_id, dict);
  34. base::TimeDelta time_since_unix_epoch =
  35. sct.timestamp - base::Time::UnixEpoch();
  36. dict.Set("timestamp",
  37. base::NumberToString(time_since_unix_epoch.InMilliseconds()));
  38. SetBinaryData("extensions", sct.extensions, dict);
  39. dict.Set("hash_algorithm",
  40. HashAlgorithmToString(sct.signature.hash_algorithm));
  41. dict.Set("signature_algorithm",
  42. SignatureAlgorithmToString(sct.signature.signature_algorithm));
  43. SetBinaryData("signature_data", sct.signature.signature_data, dict);
  44. return base::Value(std::move(dict));
  45. }
  46. // Given a list of SCTs and their statuses, return a list Value where each item
  47. // is a dictionary created by SCTToDictionary.
  48. base::Value SCTListToPrintableValues(
  49. const SignedCertificateTimestampAndStatusList& sct_and_status_list) {
  50. base::Value output_scts(base::Value::Type::LIST);
  51. for (const auto& sct_and_status : sct_and_status_list) {
  52. output_scts.GetList().Append(
  53. SCTToDictionary(*(sct_and_status.sct.get()), sct_and_status.status));
  54. }
  55. return output_scts;
  56. }
  57. } // namespace
  58. base::Value NetLogSignedCertificateTimestampParams(
  59. const SignedCertificateTimestampAndStatusList* scts) {
  60. base::Value::Dict dict;
  61. dict.Set("scts", SCTListToPrintableValues(*scts));
  62. return base::Value(std::move(dict));
  63. }
  64. base::Value NetLogRawSignedCertificateTimestampParams(
  65. base::StringPiece embedded_scts,
  66. base::StringPiece sct_list_from_ocsp,
  67. base::StringPiece sct_list_from_tls_extension) {
  68. base::Value::Dict dict;
  69. SetBinaryData("embedded_scts", embedded_scts, dict);
  70. SetBinaryData("scts_from_ocsp_response", sct_list_from_ocsp, dict);
  71. SetBinaryData("scts_from_tls_extension", sct_list_from_tls_extension, dict);
  72. return base::Value(std::move(dict));
  73. }
  74. } // namespace net