Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 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 | |
Alexander Afanasyev | 1c6976d | 2014-07-13 11:40:50 -0700 | [diff] [blame] | 26 | #include <boost/lexical_cast.hpp> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 27 | |
| 28 | namespace ndn { |
| 29 | |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 30 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<SignatureInfo>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodable<SignatureInfo>)); |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 32 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<SignatureInfo>)); |
Junxiao Shi | c2b8d24 | 2014-11-04 08:35:29 -0700 | [diff] [blame] | 33 | BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>)); |
| 34 | static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value, |
| 35 | "SignatureInfo::Error must inherit from tlv::Error"); |
| 36 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 37 | SignatureInfo::SignatureInfo() |
| 38 | : m_type(-1) |
| 39 | , m_hasKeyLocator(false) |
| 40 | { |
| 41 | } |
| 42 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 43 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 44 | : m_type(type) |
| 45 | , m_hasKeyLocator(false) |
| 46 | { |
| 47 | } |
| 48 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 49 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 50 | : m_type(type) |
| 51 | , m_hasKeyLocator(true) |
| 52 | , m_keyLocator(keyLocator) |
| 53 | { |
| 54 | } |
| 55 | |
| 56 | SignatureInfo::SignatureInfo(const Block& block) |
| 57 | { |
| 58 | wireDecode(block); |
| 59 | } |
| 60 | |
| 61 | void |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 62 | SignatureInfo::setSignatureType(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 63 | { |
| 64 | m_wire.reset(); |
| 65 | m_type = type; |
| 66 | } |
| 67 | |
| 68 | void |
| 69 | SignatureInfo::setKeyLocator(const KeyLocator& keyLocator) |
| 70 | { |
| 71 | m_wire.reset(); |
| 72 | m_keyLocator = keyLocator; |
| 73 | m_hasKeyLocator = true; |
| 74 | } |
| 75 | |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 76 | void |
| 77 | SignatureInfo::unsetKeyLocator() |
| 78 | { |
| 79 | m_wire.reset(); |
| 80 | m_keyLocator = KeyLocator(); |
| 81 | m_hasKeyLocator = false; |
| 82 | } |
| 83 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 84 | const KeyLocator& |
| 85 | SignatureInfo::getKeyLocator() const |
| 86 | { |
| 87 | if (m_hasKeyLocator) |
| 88 | return m_keyLocator; |
| 89 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 90 | BOOST_THROW_EXCEPTION(Error("KeyLocator does not exist")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 91 | } |
| 92 | |
| 93 | void |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 94 | SignatureInfo::setValidityPeriod(const security::ValidityPeriod& validityPeriod) |
| 95 | { |
| 96 | unsetValidityPeriod(); |
| 97 | m_otherTlvs.push_front(validityPeriod.wireEncode()); |
| 98 | } |
| 99 | |
| 100 | void |
| 101 | SignatureInfo::unsetValidityPeriod() |
| 102 | { |
| 103 | m_wire.reset(); |
| 104 | if (!m_otherTlvs.empty() && m_otherTlvs.front().type() == tlv::ValidityPeriod) { |
| 105 | m_otherTlvs.erase(m_otherTlvs.begin()); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | security::ValidityPeriod |
| 110 | SignatureInfo::getValidityPeriod() const |
| 111 | { |
| 112 | if (m_otherTlvs.empty() || m_otherTlvs.front().type() != tlv::ValidityPeriod) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 113 | BOOST_THROW_EXCEPTION(Error("SignatureInfo does not contain the requested ValidityPeriod field")); |
Yingdi Yu | 6be43f3 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | return security::ValidityPeriod(m_otherTlvs.front()); |
| 117 | } |
| 118 | |
| 119 | void |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 120 | SignatureInfo::appendTypeSpecificTlv(const Block& block) |
| 121 | { |
Zhiyi Zhang | 387e57b | 2016-08-01 18:50:00 -0700 | [diff] [blame] | 122 | m_wire.reset(); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 123 | m_otherTlvs.push_back(block); |
| 124 | } |
| 125 | |
| 126 | |
| 127 | const Block& |
| 128 | SignatureInfo::getTypeSpecificTlv(uint32_t type) const |
| 129 | { |
| 130 | for (std::list<Block>::const_iterator i = m_otherTlvs.begin(); |
| 131 | i != m_otherTlvs.end(); i++) { |
| 132 | if (i->type() == type) |
| 133 | return *i; |
| 134 | } |
| 135 | |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 136 | BOOST_THROW_EXCEPTION(Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" + |
| 137 | boost::lexical_cast<std::string>(type) + "] from SignatureInfo")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Alexander Afanasyev | 7463389 | 2015-02-08 18:08:46 -0800 | [diff] [blame] | 140 | template<encoding::Tag TAG> |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 141 | size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 142 | SignatureInfo::wireEncode(EncodingImpl<TAG>& encoder) const |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 143 | { |
| 144 | size_t totalLength = 0; |
| 145 | |
| 146 | for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin(); |
| 147 | i != m_otherTlvs.rend(); i++) { |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 148 | totalLength += encoder.prependBlock(*i); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | if (m_hasKeyLocator) |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 152 | totalLength += m_keyLocator.wireEncode(encoder); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 153 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 154 | totalLength += prependNonNegativeIntegerBlock(encoder, tlv::SignatureType, m_type); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 155 | |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 156 | totalLength += encoder.prependVarNumber(totalLength); |
| 157 | totalLength += encoder.prependVarNumber(tlv::SignatureInfo); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 158 | return totalLength; |
| 159 | } |
| 160 | |
| 161 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 162 | SignatureInfo::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 163 | |
| 164 | template size_t |
Alexander Afanasyev | d5c48e0 | 2015-06-24 11:58:14 -0700 | [diff] [blame] | 165 | SignatureInfo::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const; |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 166 | |
| 167 | |
| 168 | const Block& |
| 169 | SignatureInfo::wireEncode() const |
| 170 | { |
| 171 | if (m_wire.hasWire()) |
| 172 | return m_wire; |
| 173 | |
| 174 | EncodingEstimator estimator; |
| 175 | size_t estimatedSize = wireEncode(estimator); |
| 176 | |
| 177 | EncodingBuffer buffer(estimatedSize, 0); |
| 178 | wireEncode(buffer); |
| 179 | |
| 180 | m_wire = buffer.block(); |
| 181 | return m_wire; |
| 182 | } |
| 183 | |
| 184 | void |
| 185 | SignatureInfo::wireDecode(const Block& wire) |
| 186 | { |
| 187 | if (!wire.hasWire()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 188 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 189 | } |
| 190 | |
| 191 | m_hasKeyLocator = false; |
| 192 | |
| 193 | m_wire = wire; |
| 194 | m_wire.parse(); |
| 195 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 196 | if (m_wire.type() != tlv::SignatureInfo) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 197 | BOOST_THROW_EXCEPTION(tlv::Error("Unexpected TLV type when decoding Name")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 198 | |
| 199 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 200 | |
| 201 | // the first block must be SignatureType |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 202 | if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 203 | m_type = readNonNegativeInteger(*it); |
| 204 | it++; |
| 205 | } |
| 206 | else |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 207 | BOOST_THROW_EXCEPTION(Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not " |
| 208 | "SignatureType")); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 209 | |
| 210 | // the second block could be KeyLocator |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 211 | if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 212 | m_keyLocator.wireDecode(*it); |
| 213 | m_hasKeyLocator = true; |
| 214 | it++; |
| 215 | } |
| 216 | |
| 217 | // Decode the rest of type-specific TLVs, if any |
| 218 | while (it != m_wire.elements_end()) { |
Zhiyi Zhang | 387e57b | 2016-08-01 18:50:00 -0700 | [diff] [blame] | 219 | m_otherTlvs.push_back(*it); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 220 | it++; |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | bool |
| 225 | SignatureInfo::operator==(const SignatureInfo& rhs) const |
| 226 | { |
| 227 | return (m_type == rhs.m_type && |
| 228 | m_hasKeyLocator == rhs.m_hasKeyLocator && |
| 229 | m_keyLocator == rhs.m_keyLocator && |
| 230 | m_otherTlvs == rhs.m_otherTlvs); |
| 231 | } |
| 232 | |
Alexander Afanasyev | 4146975 | 2017-01-10 21:51:55 -0800 | [diff] [blame] | 233 | std::ostream& |
| 234 | operator<<(std::ostream& os, const SignatureInfo& info) |
| 235 | { |
| 236 | os << static_cast<tlv::SignatureTypeValue>(info.getSignatureType()); |
| 237 | if (info.hasKeyLocator()) { |
| 238 | os << " " << info.getKeyLocator(); |
| 239 | } |
| 240 | if (!info.m_otherTlvs.empty()) { |
| 241 | os << " { "; |
| 242 | for (const auto& block : info.m_otherTlvs) { |
| 243 | os << block.type() << " "; |
| 244 | } |
| 245 | os << "}"; |
| 246 | } |
| 247 | return os; |
| 248 | } |
| 249 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 250 | } // namespace ndn |