1 // Copyright 2014 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #ifdef UNSAFE_BUFFERS_BUILD
6 // TODO(crbug.com/40284755): Remove this and spanify to fix the errors.
7 #pragma allow_unsafe_buffers
8 #endif
9
10 #include "net/cert/ct_log_response_parser.h"
11
12 #include <memory>
13 #include <string>
14 #include <string_view>
15
16 #include "base/base64.h"
17 #include "base/json/json_reader.h"
18 #include "base/time/time.h"
19 #include "base/values.h"
20 #include "net/cert/ct_serialization.h"
21 #include "net/cert/signed_tree_head.h"
22 #include "net/test/ct_test_util.h"
23 #include "testing/gtest/include/gtest/gtest.h"
24
25 namespace net::ct {
26
TEST(CTLogResponseParserTest,ParsesValidJsonSTH)27 TEST(CTLogResponseParserTest, ParsesValidJsonSTH) {
28 std::optional<base::Value> sample_sth_json =
29 base::JSONReader::Read(GetSampleSTHAsJson());
30 SignedTreeHead tree_head;
31 EXPECT_TRUE(FillSignedTreeHead(*sample_sth_json, &tree_head));
32
33 SignedTreeHead sample_sth;
34 ASSERT_TRUE(GetSampleSignedTreeHead(&sample_sth));
35
36 ASSERT_EQ(SignedTreeHead::V1, tree_head.version);
37 ASSERT_EQ(sample_sth.timestamp, tree_head.timestamp);
38 ASSERT_EQ(sample_sth.tree_size, tree_head.tree_size);
39
40 // Copy the field from the SignedTreeHead because it's not null terminated
41 // there and ASSERT_STREQ expects null-terminated strings.
42 char actual_hash[kSthRootHashLength + 1];
43 memcpy(actual_hash, tree_head.sha256_root_hash, kSthRootHashLength);
44 actual_hash[kSthRootHashLength] = '\0';
45 std::string expected_sha256_root_hash = GetSampleSTHSHA256RootHash();
46 ASSERT_STREQ(expected_sha256_root_hash.c_str(), actual_hash);
47
48 const DigitallySigned& expected_signature(sample_sth.signature);
49
50 ASSERT_EQ(tree_head.signature.hash_algorithm,
51 expected_signature.hash_algorithm);
52 ASSERT_EQ(tree_head.signature.signature_algorithm,
53 expected_signature.signature_algorithm);
54 ASSERT_EQ(tree_head.signature.signature_data,
55 expected_signature.signature_data);
56 }
57
TEST(CTLogResponseParserTest,FailsToParseMissingFields)58 TEST(CTLogResponseParserTest, FailsToParseMissingFields) {
59 std::optional<base::Value> missing_signature_sth = base::JSONReader::Read(
60 CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
61 GetSampleSTHSHA256RootHash(), ""));
62
63 SignedTreeHead tree_head;
64 ASSERT_FALSE(FillSignedTreeHead(*missing_signature_sth, &tree_head));
65
66 std::optional<base::Value> missing_root_hash_sth = base::JSONReader::Read(
67 CreateSignedTreeHeadJsonString(1 /* tree_size */, 123456u /* timestamp */,
68 "", GetSampleSTHTreeHeadSignature()));
69 ASSERT_FALSE(FillSignedTreeHead(*missing_root_hash_sth, &tree_head));
70 }
71
TEST(CTLogResponseParserTest,FailsToParseIncorrectLengthRootHash)72 TEST(CTLogResponseParserTest, FailsToParseIncorrectLengthRootHash) {
73 SignedTreeHead tree_head;
74
75 std::string too_long_hash;
76 base::Base64Decode(
77 std::string_view("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoArK\n"),
78 &too_long_hash);
79 std::optional<base::Value> too_long_hash_json =
80 base::JSONReader::Read(CreateSignedTreeHeadJsonString(
81 1 /* tree_size */, 123456u /* timestamp */,
82 GetSampleSTHSHA256RootHash(), too_long_hash));
83 ASSERT_FALSE(FillSignedTreeHead(*too_long_hash_json, &tree_head));
84
85 std::string too_short_hash;
86 base::Base64Decode(
87 std::string_view("/WHFMgXtI/umKKuACJIN0Bb73TcILm9WkeU6qszvoA==\n"),
88 &too_short_hash);
89 std::optional<base::Value> too_short_hash_json =
90 base::JSONReader::Read(CreateSignedTreeHeadJsonString(
91 1 /* tree_size */, 123456u /* timestamp */,
92 GetSampleSTHSHA256RootHash(), too_short_hash));
93 ASSERT_FALSE(FillSignedTreeHead(*too_short_hash_json, &tree_head));
94 }
95
TEST(CTLogResponseParserTest,ParsesJsonSTHWithLargeTimestamp)96 TEST(CTLogResponseParserTest, ParsesJsonSTHWithLargeTimestamp) {
97 SignedTreeHead tree_head;
98
99 std::optional<base::Value> large_timestamp_json =
100 base::JSONReader::Read(CreateSignedTreeHeadJsonString(
101 100, INT64_C(1) << 34, GetSampleSTHSHA256RootHash(),
102 GetSampleSTHTreeHeadSignature()));
103
104 ASSERT_TRUE(FillSignedTreeHead(*large_timestamp_json, &tree_head));
105
106 base::Time expected_time =
107 base::Time::UnixEpoch() + base::Milliseconds(INT64_C(1) << 34);
108
109 EXPECT_EQ(tree_head.timestamp, expected_time);
110 }
111
TEST(CTLogResponseParserTest,ParsesConsistencyProofSuccessfully)112 TEST(CTLogResponseParserTest, ParsesConsistencyProofSuccessfully) {
113 std::string first(32, 'a');
114 std::string second(32, 'b');
115 std::string third(32, 'c');
116
117 std::vector<std::string> raw_nodes;
118 raw_nodes.push_back(first);
119 raw_nodes.push_back(second);
120 raw_nodes.push_back(third);
121 std::optional<base::Value> sample_consistency_proof =
122 base::JSONReader::Read(CreateConsistencyProofJsonString(raw_nodes));
123
124 std::vector<std::string> output;
125
126 ASSERT_TRUE(FillConsistencyProof(*sample_consistency_proof, &output));
127
128 EXPECT_EQ(output[0], first);
129 EXPECT_EQ(output[1], second);
130 EXPECT_EQ(output[2], third);
131 }
132
TEST(CTLogResponseParserTest,FailsOnInvalidProofJson)133 TEST(CTLogResponseParserTest, FailsOnInvalidProofJson) {
134 std::vector<std::string> output;
135
136 std::optional<base::Value> badly_encoded =
137 base::JSONReader::Read(std::string("{\"consistency\": [\"notbase64\"]}"));
138 EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
139
140 std::optional<base::Value> not_a_string =
141 base::JSONReader::Read(std::string("{\"consistency\": [42, 16]}"));
142 EXPECT_FALSE(FillConsistencyProof(*badly_encoded, &output));
143
144 std::optional<base::Value> missing_consistency =
145 base::JSONReader::Read(std::string("{}"));
146 EXPECT_FALSE(FillConsistencyProof(*missing_consistency, &output));
147
148 std::optional<base::Value> not_a_dict =
149 base::JSONReader::Read(std::string("[]"));
150 EXPECT_FALSE(FillConsistencyProof(*not_a_dict, &output));
151 }
152
TEST(CTLogResponseParserTest,ParsesProofJsonWithExtraFields)153 TEST(CTLogResponseParserTest, ParsesProofJsonWithExtraFields) {
154 std::vector<std::string> output;
155
156 std::optional<base::Value> badly_encoded = base::JSONReader::Read(
157 std::string("{\"consistency\": [], \"somethingelse\": 3}"));
158 EXPECT_TRUE(FillConsistencyProof(*badly_encoded, &output));
159 }
160
161 } // namespace net::ct
162