Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 2 | /* |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2017 Regents of the University of California. |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
| 20 | */ |
| 21 | |
| 22 | #include "signature-info.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 23 | #include "encoding/block-helpers.hpp" |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 24 | #include "util/concepts.hpp" |
Alexander Afanasyev | 15f6731 | 2014-07-22 15:11:09 -0700 | [diff] [blame] | 25 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 26 | namespace ndn { |
| 27 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 28 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>)); |
| 29 | BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 31 | BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>)); |
| 32 | static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value, |
| 33 | "SignatureInfo::Error must inherit from tlv::Error"); |
| 34 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 35 | SignatureInfo::SignatureInfo() |
| 36 | : m_type(-1) |
| 37 | , m_hasKeyLocator(false) |
| 38 | { |
| 39 | } |
| 40 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 41 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 42 | : m_type(type) |
| 43 | , m_hasKeyLocator(false) |
| 44 | { |
| 45 | } |
| 46 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 47 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 48 | : m_type(type) |
| 49 | , m_hasKeyLocator(true) |
| 50 | , m_keyLocator(keyLocator) |
| 51 | { |
| 52 | } |
| 53 | |
| 54 | SignatureInfo::SignatureInfo(const Block& block) |
| 55 | { |
| 56 | wireDecode(block); |
| 57 | } |
| 58 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 59 | template<encoding::Tag TAG> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 60 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 61 | SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 62 | { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 63 | if (m_type == -1) { |
| 64 | BOOST_THROW_EXCEPTION(Error("Cannot encode invalid SignatureInfo")); |
| 65 | } |
| 66 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 67 | // SignatureInfo ::= SIGNATURE-INFO-TLV TLV-LENGTH |
| 68 | // SignatureType |
| 69 | // KeyLocator? |
| 70 | // ValidityPeriod? (if present, stored as first item of m_otherTlvs) |
| 71 | // other SignatureType-specific sub-elements* |
| 72 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 73 | size_t totalLength = 0; |
| 74 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 75 | for (auto i = m_otherTlvs.rbegin(); i != m_otherTlvs.rend(); i++) { |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 76 | totalLength += encoder.prependBlock(*i); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 77 | } |
| 78 | |
| 79 | if (m_hasKeyLocator) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 80 | totalLength += m_keyLocator.wireEncode(encoder); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 81 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 82 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType, m_type); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 83 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 84 | totalLength += encoder.prependVarNumber(totalLength); |
| 85 | totalLength += encoder.prependVarNumber(tlv::SignatureInfo); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 86 | return totalLength; |
| 87 | } |
| 88 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 89 | NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SignatureInfo); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 90 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 91 | const Block& |
| 92 | SignatureInfo::wireEncode() const |
| 93 | { |
| 94 | if (m_wire.hasWire()) |
| 95 | return m_wire; |
| 96 | |
| 97 | EncodingEstimator estimator; |
| 98 | size_t estimatedSize = wireEncode(estimator); |
| 99 | |
| 100 | EncodingBuffer buffer(estimatedSize, 0); |
| 101 | wireEncode(buffer); |
| 102 | |
| 103 | m_wire = buffer.block(); |
| 104 | return m_wire; |
| 105 | } |
| 106 | |
| 107 | void |
| 108 | SignatureInfo::wireDecode(const Block& wire) |
| 109 | { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 110 | m_hasKeyLocator = false; |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 111 | m_otherTlvs.clear(); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 112 | |
| 113 | m_wire = wire; |
| 114 | m_wire.parse(); |
| 115 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 116 | if (m_wire.type() != tlv::SignatureInfo) |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 117 | BOOST_THROW_EXCEPTION(Error("Decoding SignatureInfo, but TLV-TYPE is " + to_string(m_wire.type()))); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 118 | |
| 119 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 120 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 121 | // the first sub-element must be SignatureType |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 122 | if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) { |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 123 | m_type = readNonNegativeIntegerAs<int32_t>(*it); |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 124 | ++it; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 125 | } |
| 126 | else |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 127 | BOOST_THROW_EXCEPTION(Error("Missing SignatureType in SignatureInfo")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 128 | |
Junxiao Shi | 605671d | 2017-08-26 13:41:06 +0000 | [diff] [blame] | 129 | // the second sub-element could be KeyLocator |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 130 | if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 131 | m_keyLocator.wireDecode(*it); |
| 132 | m_hasKeyLocator = true; |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 133 | ++it; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 136 | // store SignatureType-specific sub-elements, if any |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 137 | while (it != m_wire.elements_end()) { |
Zhiyi Zhang | 387e57b | 2016-08-01 18:50:00 -0700 | [diff] [blame] | 138 | m_otherTlvs.push_back(*it); |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 139 | ++it; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 143 | void |
| 144 | SignatureInfo::setSignatureType(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 145 | { |
Junxiao Shi | a9181d7 | 2017-08-13 16:47:45 +0000 | [diff] [blame] | 146 | m_wire.reset(); |
| 147 | m_type = type; |
| 148 | } |
| 149 | |
| 150 | const KeyLocator& |
| 151 | SignatureInfo::getKeyLocator() const |
| 152 | { |
| 153 | if (m_hasKeyLocator) |
| 154 | return m_keyLocator; |
| 155 | else |
| 156 | BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist in SignatureInfo")); |
| 157 | } |
| 158 | |
| 159 | void |
| 160 | SignatureInfo::setKeyLocator(const KeyLocator& keyLocator) |
| 161 | { |
| 162 | m_wire.reset(); |
| 163 | m_keyLocator = keyLocator; |
| 164 | m_hasKeyLocator = true; |
| 165 | } |
| 166 | |
| 167 | void |
| 168 | SignatureInfo::unsetKeyLocator() |
| 169 | { |
| 170 | m_wire.reset(); |
| 171 | m_keyLocator = KeyLocator(); |
| 172 | m_hasKeyLocator = false; |
| 173 | } |
| 174 | |
| 175 | security::ValidityPeriod |
| 176 | SignatureInfo::getValidityPeriod() const |
| 177 | { |
| 178 | if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) { |
| 179 | BOOST_THROW_EXCEPTION(Error("ValidityPeriod does not exist in SignatureInfo")); |
| 180 | } |
| 181 | |
| 182 | return security::ValidityPeriod(m_otherTlvs.front()); |
| 183 | } |
| 184 | |
| 185 | void |
| 186 | SignatureInfo::setValidityPeriod(const security::ValidityPeriod& validityPeriod) |
| 187 | { |
| 188 | unsetValidityPeriod(); |
| 189 | m_otherTlvs.push_front(validityPeriod.wireEncode()); |
| 190 | } |
| 191 | |
| 192 | void |
| 193 | SignatureInfo::unsetValidityPeriod() |
| 194 | { |
| 195 | if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) { |
| 196 | m_otherTlvs.pop_front(); |
| 197 | m_wire.reset(); |
| 198 | } |
| 199 | } |
| 200 | |
| 201 | const Block& |
| 202 | SignatureInfo::getTypeSpecificTlv(uint32_t type) const |
| 203 | { |
| 204 | for (const Block& block : m_otherTlvs) { |
| 205 | if (block.type() == type) |
| 206 | return block; |
| 207 | } |
| 208 | |
| 209 | BOOST_THROW_EXCEPTION(Error("TLV-TYPE " + to_string(type) + " sub-element does not exist in SignatureInfo")); |
| 210 | } |
| 211 | |
| 212 | void |
| 213 | SignatureInfo::appendTypeSpecificTlv(const Block& block) |
| 214 | { |
| 215 | m_wire.reset(); |
| 216 | m_otherTlvs.push_back(block); |
| 217 | } |
| 218 | |
| 219 | bool |
| 220 | operator==(const SignatureInfo& lhs, const SignatureInfo& rhs) |
| 221 | { |
| 222 | return lhs.m_type == rhs.m_type && |
| 223 | lhs.m_hasKeyLocator == rhs.m_hasKeyLocator && |
| 224 | lhs.m_keyLocator == rhs.m_keyLocator && |
| 225 | lhs.m_otherTlvs == rhs.m_otherTlvs; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 226 | } |
| 227 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 228 | std::ostream& |
| 229 | operator<<(std::ostream& os, const SignatureInfo& info) |
| 230 | { |
| 231 | os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType()); |
| 232 | if (info.hasKeyLocator()) { |
| 233 | os << " " << info.getKeyLocator(); |
| 234 | } |
| 235 | if (!info.m_otherTlvs.empty()) { |
| 236 | os << " { "; |
| 237 | for (const auto& block : info.m_otherTlvs) { |
| 238 | os << block.type() << " "; |
| 239 | } |
| 240 | os << "}"; |
| 241 | } |
| 242 | return os; |
| 243 | } |
| 244 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 245 | } // namespace ndn |