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\": \""
43 << base::HexEncode(sth.log_id.data(), sth.log_id.size()) << "\"\n"
44 << "}";
45 }
46
operator ==(const SignedTreeHead & lhs,const SignedTreeHead & rhs)47 bool operator==(const SignedTreeHead& lhs, const SignedTreeHead& rhs) {
48 return std::tie(lhs.version, lhs.timestamp, lhs.tree_size, lhs.log_id) ==
49 std::tie(rhs.version, rhs.timestamp, rhs.tree_size, rhs.log_id) &&
50 memcmp(lhs.sha256_root_hash, rhs.sha256_root_hash,
51 kSthRootHashLength) == 0 &&
52 lhs.signature.SignatureParametersMatch(
53 rhs.signature.hash_algorithm, rhs.signature.signature_algorithm) &&
54 lhs.signature.signature_data == rhs.signature.signature_data;
55 }
56
operator !=(const SignedTreeHead & lhs,const SignedTreeHead & rhs)57 bool operator!=(const SignedTreeHead& lhs, const SignedTreeHead& rhs) {
58 return !(lhs == rhs);
59 }
60
61 } // namespace net::ct
62