ct_log_response_parser.cc 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. // Copyright 2014 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_log_response_parser.h"
  5. #include <memory>
  6. #include "base/base64.h"
  7. #include "base/json/json_value_converter.h"
  8. #include "base/logging.h"
  9. #include "base/strings/string_piece.h"
  10. #include "base/time/time.h"
  11. #include "base/values.h"
  12. #include "net/cert/ct_serialization.h"
  13. #include "net/cert/signed_tree_head.h"
  14. namespace net::ct {
  15. namespace {
  16. // Structure for making JSON decoding easier. The string fields
  17. // are base64-encoded so will require further decoding.
  18. struct JsonSignedTreeHead {
  19. int tree_size;
  20. double timestamp;
  21. std::string sha256_root_hash;
  22. DigitallySigned signature;
  23. static void RegisterJSONConverter(
  24. base::JSONValueConverter<JsonSignedTreeHead>* converted);
  25. };
  26. bool ConvertSHA256RootHash(base::StringPiece s, std::string* result) {
  27. return base::Base64Decode(s, result) && result->size() == kSthRootHashLength;
  28. }
  29. bool ConvertTreeHeadSignature(base::StringPiece s, DigitallySigned* result) {
  30. std::string tree_head_signature;
  31. if (!base::Base64Decode(s, &tree_head_signature)) {
  32. return false;
  33. }
  34. base::StringPiece sp(tree_head_signature);
  35. return DecodeDigitallySigned(&sp, result);
  36. }
  37. void JsonSignedTreeHead::RegisterJSONConverter(
  38. base::JSONValueConverter<JsonSignedTreeHead>* converter) {
  39. converter->RegisterIntField("tree_size", &JsonSignedTreeHead::tree_size);
  40. converter->RegisterDoubleField("timestamp", &JsonSignedTreeHead::timestamp);
  41. converter->RegisterCustomField("sha256_root_hash",
  42. &JsonSignedTreeHead::sha256_root_hash,
  43. &ConvertSHA256RootHash);
  44. converter->RegisterCustomField<DigitallySigned>(
  45. "tree_head_signature",
  46. &JsonSignedTreeHead::signature,
  47. &ConvertTreeHeadSignature);
  48. }
  49. bool IsJsonSTHStructurallyValid(const JsonSignedTreeHead& sth) {
  50. return sth.tree_size >= 0 && sth.timestamp >= 0 &&
  51. !sth.sha256_root_hash.empty() && !sth.signature.signature_data.empty();
  52. }
  53. // Structure for making JSON decoding easier. The string fields
  54. // are base64-encoded so will require further decoding.
  55. struct JsonConsistencyProof {
  56. std::vector<std::unique_ptr<std::string>> proof_nodes;
  57. static void RegisterJSONConverter(
  58. base::JSONValueConverter<JsonConsistencyProof>* converter);
  59. };
  60. bool ConvertIndividualProofNode(const base::Value* value, std::string* result) {
  61. const std::string* b64_encoded_node = value->GetIfString();
  62. return b64_encoded_node && ConvertSHA256RootHash(*b64_encoded_node, result);
  63. }
  64. void JsonConsistencyProof::RegisterJSONConverter(
  65. base::JSONValueConverter<JsonConsistencyProof>* converter) {
  66. converter->RegisterRepeatedCustomValue<std::string>(
  67. "consistency", &JsonConsistencyProof::proof_nodes,
  68. &ConvertIndividualProofNode);
  69. }
  70. } // namespace
  71. bool FillSignedTreeHead(const base::Value& json_signed_tree_head,
  72. SignedTreeHead* signed_tree_head) {
  73. JsonSignedTreeHead parsed_sth;
  74. base::JSONValueConverter<JsonSignedTreeHead> converter;
  75. if (!converter.Convert(json_signed_tree_head, &parsed_sth) ||
  76. !IsJsonSTHStructurallyValid(parsed_sth)) {
  77. return false;
  78. }
  79. signed_tree_head->version = SignedTreeHead::V1;
  80. signed_tree_head->tree_size = parsed_sth.tree_size;
  81. signed_tree_head->timestamp = base::Time::FromJsTime(parsed_sth.timestamp);
  82. signed_tree_head->signature = parsed_sth.signature;
  83. memcpy(signed_tree_head->sha256_root_hash,
  84. parsed_sth.sha256_root_hash.c_str(),
  85. kSthRootHashLength);
  86. return true;
  87. }
  88. bool FillConsistencyProof(const base::Value& json_consistency_proof,
  89. std::vector<std::string>* consistency_proof) {
  90. JsonConsistencyProof parsed_proof;
  91. base::JSONValueConverter<JsonConsistencyProof> converter;
  92. if (!converter.Convert(json_consistency_proof, &parsed_proof)) {
  93. return false;
  94. }
  95. const base::Value::Dict* dict_value = json_consistency_proof.GetIfDict();
  96. if (!dict_value || !dict_value->Find("consistency")) {
  97. return false;
  98. }
  99. consistency_proof->reserve(parsed_proof.proof_nodes.size());
  100. for (const auto& proof_node : parsed_proof.proof_nodes) {
  101. consistency_proof->push_back(*proof_node);
  102. }
  103. return true;
  104. }
  105. } // namespace net::ct