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