1 // Copyright 2016 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 #include "net/cert/signed_tree_head.h"
6
7 #include <string.h>
8
9 #include <ostream>
10
11 #include "base/strings/string_number_conversions.h"
12
13 namespace net::ct {
14
15 SignedTreeHead::SignedTreeHead() = default;
16
SignedTreeHead(Version version,const base::Time & timestamp,uint64_t tree_size,const char sha256_root_hash[kSthRootHashLength],const DigitallySigned & signature,const std::string & log_id)17 SignedTreeHead::SignedTreeHead(Version version,
18 const base::Time& timestamp,
19 uint64_t tree_size,
20 const char sha256_root_hash[kSthRootHashLength],
21 const DigitallySigned& signature,
22 const std::string& log_id)
23 : version(version),
24 timestamp(timestamp),
25 tree_size(tree_size),
26 signature(signature),
27 log_id(log_id) {
28 memcpy(this->sha256_root_hash, sha256_root_hash, kSthRootHashLength);
29 }
30
31 SignedTreeHead::SignedTreeHead(const SignedTreeHead& other) = default;
32
33 SignedTreeHead::~SignedTreeHead() = default;
34
PrintTo(const SignedTreeHead & sth,std::ostream * os)35 void PrintTo(const SignedTreeHead& sth, std::ostream* os) {
36 (*os) << "{\n"
37 << "\t\"version\": " << sth.version << ",\n"
38 << "\t\"timestamp\": " << sth.timestamp << ",\n"
39 << "\t\"tree_size\": " << sth.tree_size << ",\n"
40 << "\t\"sha256_root_hash\": \""
41 << base::HexEncode(sth.sha256_root_hash, kSthRootHashLength)
42 << "\",\n\t\"log_id\": \"" << base::HexEncode(sth.log_id) << "\"\n"
43 << "}";
44 }
45
operator ==(const SignedTreeHead & lhs,const SignedTreeHead & rhs)46 bool operator==(const SignedTreeHead& lhs, const SignedTreeHead& rhs) {
47 return std::tie(lhs.version, lhs.timestamp, lhs.tree_size, lhs.log_id) ==
48 std::tie(rhs.version, rhs.timestamp, rhs.tree_size, rhs.log_id) &&
49 memcmp(lhs.sha256_root_hash, rhs.sha256_root_hash,
50 kSthRootHashLength) == 0 &&
51 lhs.signature.SignatureParametersMatch(
52 rhs.signature.hash_algorithm, rhs.signature.signature_algorithm) &&
53 lhs.signature.signature_data == rhs.signature.signature_data;
54 }
55
operator !=(const SignedTreeHead & lhs,const SignedTreeHead & rhs)56 bool operator!=(const SignedTreeHead& lhs, const SignedTreeHead& rhs) {
57 return !(lhs == rhs);
58 }
59
60 } // namespace net::ct
61