OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 |
| 5 #include "net/cert/ct_trans_item.h" |
| 6 |
| 7 #include "net/cert/merkle_consistency_proof.h" |
| 8 #include "net/cert/signed_certificate_timestamp.h" |
| 9 #include "net/cert/signed_tree_head.h" |
| 10 |
| 11 namespace net { |
| 12 namespace ct { |
| 13 |
| 14 std::ostream& operator<<(std::ostream& stream, const TransType& type) { |
| 15 switch (type) { |
| 16 case TransType::X509_SCT: |
| 17 return stream << "X509_SCT"; |
| 18 case TransType::SIGNED_TREE_HEAD: |
| 19 return stream << "SIGNED_TREE_HEAD"; |
| 20 case TransType::CONSISTENCY_PROOF: |
| 21 return stream << "CONSISTENCY_PROOF"; |
| 22 } |
| 23 |
| 24 return stream << static_cast<uint16_t>(type); |
| 25 } |
| 26 |
| 27 TransItemV1::TransItemV1() {} |
| 28 |
| 29 TransItemV1::~TransItemV1() {} |
| 30 |
| 31 TransType TransItemV1::type() const { |
| 32 CHECK(type_); |
| 33 return *type_; |
| 34 } |
| 35 |
| 36 scoped_refptr<SignedCertificateTimestamp> TransItemV1::x509_sct() const { |
| 37 CHECK_EQ(TransType::X509_SCT, type()); |
| 38 return x509_sct_; |
| 39 } |
| 40 |
| 41 const SignedTreeHead& TransItemV1::signed_tree_head() const { |
| 42 CHECK_EQ(TransType::SIGNED_TREE_HEAD, type()); |
| 43 return *signed_tree_head_; |
| 44 } |
| 45 |
| 46 const MerkleConsistencyProof& TransItemV1::consistency_proof() const { |
| 47 CHECK_EQ(TransType::CONSISTENCY_PROOF, type()); |
| 48 return *consistency_proof_; |
| 49 } |
| 50 |
| 51 void TransItemV1::SetX509Sct(scoped_refptr<SignedCertificateTimestamp> sct) { |
| 52 Clear(); |
| 53 x509_sct_ = sct; |
| 54 type_.reset(new TransType(TransType::X509_SCT)); |
| 55 } |
| 56 |
| 57 void TransItemV1::SetSignedTreeHead(const SignedTreeHead& sth) { |
| 58 Clear(); |
| 59 signed_tree_head_.reset(new SignedTreeHead(sth)); |
| 60 type_.reset(new TransType(TransType::SIGNED_TREE_HEAD)); |
| 61 } |
| 62 |
| 63 void TransItemV1::SetConsistencyProof(const MerkleConsistencyProof& proof) { |
| 64 Clear(); |
| 65 consistency_proof_.reset(new MerkleConsistencyProof(proof)); |
| 66 type_.reset(new TransType(TransType::CONSISTENCY_PROOF)); |
| 67 } |
| 68 |
| 69 void TransItemV1::Clear() { |
| 70 if (type_ == nullptr) { |
| 71 return; |
| 72 } |
| 73 |
| 74 switch (*type_) { |
| 75 case TransType::X509_SCT: |
| 76 x509_sct_ = nullptr; |
| 77 break; |
| 78 case TransType::SIGNED_TREE_HEAD: |
| 79 signed_tree_head_.reset(nullptr); |
| 80 break; |
| 81 case TransType::CONSISTENCY_PROOF: |
| 82 consistency_proof_.reset(nullptr); |
| 83 break; |
| 84 } |
| 85 |
| 86 type_.reset(nullptr); |
| 87 } |
| 88 |
| 89 std::ostream& operator<<(std::ostream& stream, const TransItemV1& item) { |
| 90 if (!item.IsTypeSet()) { |
| 91 return stream << "TransItemV1"; |
| 92 } |
| 93 |
| 94 return stream << "TransItemV1(" << item.type() << ")"; |
| 95 } |
| 96 |
| 97 TransItem::TransItem() {} |
| 98 |
| 99 TransItem::TransItem(scoped_ptr<TransItemV1> item) { |
| 100 Set(std::move(item)); |
| 101 } |
| 102 |
| 103 TransItem::~TransItem() {} |
| 104 |
| 105 TransItem::Version TransItem::version() const { |
| 106 CHECK(version_); |
| 107 return *version_; |
| 108 } |
| 109 |
| 110 const TransItemV1& TransItem::v1() const { |
| 111 CHECK_EQ(Version::V1, version()); |
| 112 return *v1_; |
| 113 } |
| 114 |
| 115 void TransItem::Set(scoped_ptr<TransItemV1> item) { |
| 116 Clear(); |
| 117 v1_.swap(item); |
| 118 version_.reset(new Version(Version::V1)); |
| 119 } |
| 120 |
| 121 void TransItem::Clear() { |
| 122 if (version_ == nullptr) { |
| 123 return; |
| 124 } |
| 125 |
| 126 switch (*version_) { |
| 127 case Version::V1: |
| 128 v1_.reset(nullptr); |
| 129 break; |
| 130 } |
| 131 |
| 132 version_.reset(nullptr); |
| 133 } |
| 134 |
| 135 std::ostream& operator<<(std::ostream& stream, |
| 136 const TransItem::Version& version) { |
| 137 switch (version) { |
| 138 case TransItem::Version::V1: |
| 139 return stream << "v1"; |
| 140 } |
| 141 |
| 142 return stream << "v" << (static_cast<unsigned>(version) + 1); |
| 143 } |
| 144 |
| 145 std::ostream& operator<<(std::ostream& stream, const TransItem& item) { |
| 146 if (!item.IsVersionSet()) { |
| 147 return stream << "TransItem"; |
| 148 } |
| 149 |
| 150 switch (item.version()) { |
| 151 case TransItem::Version::V1: |
| 152 return stream << "TransItem(" << item.version() << ", " |
| 153 << item.v1().type() << ")"; |
| 154 } |
| 155 |
| 156 return stream << "TransItem(" << item.version(); |
| 157 } |
| 158 |
| 159 } // namespace ct |
| 160 } // namespace net |
OLD | NEW |