Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
| 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>)); |
| 32 | BOOST_CONCEPT_ASSERT((WireDecodable<SignatureInfo>)); |
| 33 | static_assert(std::is_base_of<tlv::Error, SignatureInfo::Error>::value, |
| 34 | "SignatureInfo::Error must inherit from tlv::Error"); |
| 35 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 36 | SignatureInfo::SignatureInfo() |
| 37 | : m_type(-1) |
| 38 | , m_hasKeyLocator(false) |
| 39 | { |
| 40 | } |
| 41 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 42 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 43 | : m_type(type) |
| 44 | , m_hasKeyLocator(false) |
| 45 | { |
| 46 | } |
| 47 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 48 | SignatureInfo::SignatureInfo(tlv::SignatureTypeValue type, const KeyLocator& keyLocator) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 49 | : m_type(type) |
| 50 | , m_hasKeyLocator(true) |
| 51 | , m_keyLocator(keyLocator) |
| 52 | { |
| 53 | } |
| 54 | |
| 55 | SignatureInfo::SignatureInfo(const Block& block) |
| 56 | { |
| 57 | wireDecode(block); |
| 58 | } |
| 59 | |
| 60 | void |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 61 | SignatureInfo::setSignatureType(tlv::SignatureTypeValue type) |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 62 | { |
| 63 | m_wire.reset(); |
| 64 | m_type = type; |
| 65 | } |
| 66 | |
| 67 | void |
| 68 | SignatureInfo::setKeyLocator(const KeyLocator& keyLocator) |
| 69 | { |
| 70 | m_wire.reset(); |
| 71 | m_keyLocator = keyLocator; |
| 72 | m_hasKeyLocator = true; |
| 73 | } |
| 74 | |
Alexander Afanasyev | a7c7f9d | 2014-07-13 11:51:52 -0700 | [diff] [blame] | 75 | void |
| 76 | SignatureInfo::unsetKeyLocator() |
| 77 | { |
| 78 | m_wire.reset(); |
| 79 | m_keyLocator = KeyLocator(); |
| 80 | m_hasKeyLocator = false; |
| 81 | } |
| 82 | |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 83 | const KeyLocator& |
| 84 | SignatureInfo::getKeyLocator() const |
| 85 | { |
| 86 | if (m_hasKeyLocator) |
| 87 | return m_keyLocator; |
| 88 | else |
| 89 | throw Error("KeyLocator does not exist"); |
| 90 | } |
| 91 | |
| 92 | void |
| 93 | SignatureInfo::appendTypeSpecificTlv(const Block& block) |
| 94 | { |
| 95 | m_otherTlvs.push_back(block); |
| 96 | } |
| 97 | |
| 98 | |
| 99 | const Block& |
| 100 | SignatureInfo::getTypeSpecificTlv(uint32_t type) const |
| 101 | { |
| 102 | for (std::list<Block>::const_iterator i = m_otherTlvs.begin(); |
| 103 | i != m_otherTlvs.end(); i++) { |
| 104 | if (i->type() == type) |
| 105 | return *i; |
| 106 | } |
| 107 | |
| 108 | throw Error("(SignatureInfo::getTypeSpecificTlv) Requested a non-existed type [" + |
| 109 | boost::lexical_cast<std::string>(type) + "] from SignatureInfo"); |
| 110 | } |
| 111 | |
| 112 | template<bool T> |
| 113 | size_t |
| 114 | SignatureInfo::wireEncode(EncodingImpl<T>& block) const |
| 115 | { |
| 116 | size_t totalLength = 0; |
| 117 | |
| 118 | for (std::list<Block>::const_reverse_iterator i = m_otherTlvs.rbegin(); |
| 119 | i != m_otherTlvs.rend(); i++) { |
| 120 | totalLength += block.appendBlock(*i); |
| 121 | } |
| 122 | |
| 123 | if (m_hasKeyLocator) |
| 124 | totalLength += m_keyLocator.wireEncode(block); |
| 125 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 126 | totalLength += prependNonNegativeIntegerBlock(block, tlv::SignatureType, m_type); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 127 | |
| 128 | totalLength += block.prependVarNumber(totalLength); |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 129 | totalLength += block.prependVarNumber(tlv::SignatureInfo); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 130 | return totalLength; |
| 131 | } |
| 132 | |
| 133 | template size_t |
| 134 | SignatureInfo::wireEncode<true>(EncodingImpl<true>& block) const; |
| 135 | |
| 136 | template size_t |
| 137 | SignatureInfo::wireEncode<false>(EncodingImpl<false>& block) const; |
| 138 | |
| 139 | |
| 140 | const Block& |
| 141 | SignatureInfo::wireEncode() const |
| 142 | { |
| 143 | if (m_wire.hasWire()) |
| 144 | return m_wire; |
| 145 | |
| 146 | EncodingEstimator estimator; |
| 147 | size_t estimatedSize = wireEncode(estimator); |
| 148 | |
| 149 | EncodingBuffer buffer(estimatedSize, 0); |
| 150 | wireEncode(buffer); |
| 151 | |
| 152 | m_wire = buffer.block(); |
| 153 | return m_wire; |
| 154 | } |
| 155 | |
| 156 | void |
| 157 | SignatureInfo::wireDecode(const Block& wire) |
| 158 | { |
| 159 | if (!wire.hasWire()) { |
| 160 | throw Error("The supplied block does not contain wire format"); |
| 161 | } |
| 162 | |
| 163 | m_hasKeyLocator = false; |
| 164 | |
| 165 | m_wire = wire; |
| 166 | m_wire.parse(); |
| 167 | |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 168 | if (m_wire.type() != tlv::SignatureInfo) |
| 169 | throw tlv::Error("Unexpected TLV type when decoding Name"); |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 170 | |
| 171 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 172 | |
| 173 | // the first block must be SignatureType |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 174 | if (it != m_wire.elements_end() && it->type() == tlv::SignatureType) { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 175 | m_type = readNonNegativeInteger(*it); |
| 176 | it++; |
| 177 | } |
| 178 | else |
| 179 | throw Error("SignatureInfo does not have sub-TLV or the first sub-TLV is not SignatureType"); |
| 180 | |
| 181 | // the second block could be KeyLocator |
Steve DiBenedetto | 54ce668 | 2014-07-22 13:22:57 -0600 | [diff] [blame] | 182 | if (it != m_wire.elements_end() && it->type() == tlv::KeyLocator) { |
Yingdi Yu | 4a55705 | 2014-07-09 16:40:37 -0700 | [diff] [blame] | 183 | m_keyLocator.wireDecode(*it); |
| 184 | m_hasKeyLocator = true; |
| 185 | it++; |
| 186 | } |
| 187 | |
| 188 | // Decode the rest of type-specific TLVs, if any |
| 189 | while (it != m_wire.elements_end()) { |
| 190 | appendTypeSpecificTlv(*it); |
| 191 | it++; |
| 192 | } |
| 193 | } |
| 194 | |
| 195 | bool |
| 196 | SignatureInfo::operator==(const SignatureInfo& rhs) const |
| 197 | { |
| 198 | return (m_type == rhs.m_type && |
| 199 | m_hasKeyLocator == rhs.m_hasKeyLocator && |
| 200 | m_keyLocator == rhs.m_keyLocator && |
| 201 | m_otherTlvs == rhs.m_otherTlvs); |
| 202 | } |
| 203 | |
| 204 | } // namespace ndn |