peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014-2017, Regents of the University of California |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 4 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 5 | * This file is part of NDN DeLorean, An Authentication System for Data Archives in |
| 6 | * Named Data Networking. See AUTHORS.md for complete list of NDN DeLorean authors |
| 7 | * and contributors. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 8 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 9 | * NDN DeLorean is free software: you can redistribute it and/or modify it under |
| 10 | * the terms of the GNU General Public License as published by the Free Software |
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
| 12 | * version. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 13 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 14 | * NDN DeLorean is distributed in the hope that it will be useful, but WITHOUT ANY |
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 16 | * PARTICULAR PURPOSE. See the GNU General Public License for more details. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 17 | * |
Alexander Afanasyev | be998ac | 2017-05-06 13:11:42 -0700 | [diff] [blame] | 18 | * You should have received a copy of the GNU General Public License along with NDN |
| 19 | * DeLorean, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 20 | */ |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 21 | |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 22 | #include "leaf.hpp" |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 23 | #include "tlv.hpp" |
| 24 | #include <ndn-cxx/security/digest-sha256.hpp> |
| 25 | #include <ndn-cxx/encoding/block-helpers.hpp> |
| 26 | #include <ndn-cxx/util/crypto.hpp> |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 28 | namespace ndn { |
| 29 | namespace delorean { |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 30 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 31 | const Name Leaf::EMPTY_NAME; |
| 32 | const size_t Leaf::N_LOGGER_LEAF_SUFFIX = 4; |
| 33 | const ssize_t Leaf::OFFSET_LEAF_SEQNO = -2; |
| 34 | const ssize_t Leaf::OFFSET_LEAF_HASH = -1; |
| 35 | |
| 36 | Leaf::Leaf() |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 37 | { |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 38 | } |
| 39 | |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 40 | Leaf::Leaf(const Name& dataName, |
| 41 | const Timestamp& timestamp, |
| 42 | const NonNegativeInteger& dataSeqNo, |
| 43 | const NonNegativeInteger& signerSeqNo, |
| 44 | const Name& loggerName) |
| 45 | : m_dataName(dataName) |
| 46 | , m_timestamp(timestamp) |
| 47 | , m_dataSeqNo(dataSeqNo) |
| 48 | , m_signerSeqNo(signerSeqNo) |
| 49 | , m_loggerName(loggerName) |
| 50 | { |
| 51 | if (m_dataSeqNo < m_signerSeqNo) |
| 52 | throw Error("Leaf: signer seqNo should be less than the data seqNo"); |
| 53 | } |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 54 | |
| 55 | void |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 56 | Leaf::setDataSeqNo(const NonNegativeInteger& dataSeqNo) |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 57 | { |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 58 | if (dataSeqNo < m_signerSeqNo) |
| 59 | throw Error("Leaf: signer seqNo should be less than the data seqNo"); |
| 60 | |
| 61 | m_wire.reset(); |
| 62 | m_dataSeqNo = dataSeqNo; |
| 63 | } |
| 64 | |
| 65 | void |
| 66 | Leaf::setDataName(const Name& dataName) |
| 67 | { |
| 68 | m_wire.reset(); |
| 69 | m_dataName = dataName; |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | Leaf::setTimestamp(const Timestamp& timestamp) |
| 74 | { |
| 75 | m_wire.reset(); |
| 76 | m_timestamp = timestamp; |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | Leaf::setSignerSeqNo(const NonNegativeInteger& signerSeqNo) |
| 81 | { |
| 82 | if (m_dataSeqNo < signerSeqNo) |
| 83 | throw Error("Leaf: signer seqNo should be less than the data seqNo"); |
| 84 | |
| 85 | m_wire.reset(); |
| 86 | m_signerSeqNo = signerSeqNo; |
| 87 | } |
| 88 | |
| 89 | void |
| 90 | Leaf::setLoggerName(const Name& loggerName) |
| 91 | { |
| 92 | m_loggerName = loggerName; |
| 93 | } |
| 94 | |
| 95 | ndn::ConstBufferPtr |
| 96 | Leaf::getHash() const |
| 97 | { |
| 98 | wireEncode(); |
| 99 | return ndn::crypto::sha256(m_wire.wire(), m_wire.size()); |
| 100 | } |
| 101 | |
| 102 | shared_ptr<Data> |
| 103 | Leaf::encode() const |
| 104 | { |
| 105 | auto data = make_shared<Data>(); |
| 106 | |
| 107 | ndn::ConstBufferPtr hash = getHash(); |
| 108 | |
| 109 | // Name |
| 110 | Name dataName = m_loggerName; |
| 111 | dataName.appendNumber(m_dataSeqNo).append(hash->buf(), hash->size()); |
| 112 | data->setName(dataName); |
| 113 | |
| 114 | // Content |
| 115 | data->setContent(wireEncode()); |
| 116 | |
| 117 | // Signature |
| 118 | ndn::DigestSha256 sig; |
| 119 | data->setSignature(sig); |
| 120 | |
| 121 | Block sigValue(tlv::SignatureValue, |
| 122 | ndn::crypto::sha256(data->wireEncode().value(), |
| 123 | data->wireEncode().value_size() - |
| 124 | data->getSignature().getValue().size())); |
| 125 | data->setSignatureValue(sigValue); |
| 126 | |
| 127 | data->wireEncode(); |
| 128 | |
| 129 | return data; |
| 130 | } |
| 131 | |
| 132 | void |
| 133 | Leaf::decode(const Data& data) |
| 134 | { |
| 135 | const Name& dataName = data.getName(); |
| 136 | |
| 137 | if (!m_loggerName.isPrefixOf(dataName)) |
| 138 | throw Error("decode: leaf data name does not match logger name"); |
| 139 | |
| 140 | if (m_loggerName.size() + N_LOGGER_LEAF_SUFFIX != dataName.size()) |
| 141 | throw Error("decode: leaf data name does not follow the naming convention"); |
| 142 | |
| 143 | ndn::ConstBufferPtr leafHash; |
| 144 | NonNegativeInteger dataSeqNo; |
| 145 | try { |
| 146 | leafHash = make_shared<ndn::Buffer>(dataName.get(OFFSET_LEAF_HASH).value(), |
| 147 | dataName.get(OFFSET_LEAF_HASH).value_size()); |
| 148 | |
| 149 | dataSeqNo = dataName.get(OFFSET_LEAF_SEQNO).toNumber(); |
| 150 | } |
| 151 | catch (tlv::Error&) { |
| 152 | throw Error("decode: logger name encoding error"); |
| 153 | } |
| 154 | |
| 155 | wireDecode(data.getContent().blockFromValue()); |
| 156 | |
| 157 | if (*leafHash != *getHash()) |
| 158 | throw Error("decode: inconsistent hash"); |
| 159 | |
| 160 | if (m_dataSeqNo != dataSeqNo) |
| 161 | throw Error("decode: seqNo does not match"); |
| 162 | } |
| 163 | |
| 164 | template<ndn::encoding::Tag TAG> |
| 165 | size_t |
| 166 | Leaf::wireEncode(ndn::EncodingImpl<TAG>& block) const |
| 167 | { |
| 168 | size_t totalLength = 0; |
| 169 | |
Alexander Afanasyev | b1ba9c9 | 2017-05-06 13:16:18 -0700 | [diff] [blame] | 170 | totalLength += prependNonNegativeIntegerBlock(block, tlv::SignerSeqNo, m_signerSeqNo); |
| 171 | totalLength += prependNonNegativeIntegerBlock(block, tlv::DataSeqNo, m_dataSeqNo); |
| 172 | totalLength += prependNonNegativeIntegerBlock(block, tlv::Timestamp, m_timestamp); |
Yingdi Yu | 0c3e591 | 2015-03-17 14:22:38 -0700 | [diff] [blame] | 173 | totalLength += m_dataName.wireEncode(block); |
| 174 | |
| 175 | totalLength += block.prependVarNumber(totalLength); |
| 176 | totalLength += block.prependVarNumber(tlv::LoggerLeaf); |
| 177 | |
| 178 | return totalLength; |
| 179 | } |
| 180 | |
| 181 | template size_t |
| 182 | Leaf::wireEncode<ndn::encoding::EncoderTag>(ndn::EncodingImpl<ndn::encoding::EncoderTag>&) const; |
| 183 | |
| 184 | template size_t |
| 185 | Leaf::wireEncode<ndn::encoding::EstimatorTag>(ndn::EncodingImpl<ndn::encoding::EstimatorTag>&) const; |
| 186 | |
| 187 | |
| 188 | const Block& |
| 189 | Leaf::wireEncode() const |
| 190 | { |
| 191 | if (m_wire.hasWire()) |
| 192 | return m_wire; |
| 193 | |
| 194 | ndn::EncodingEstimator estimator; |
| 195 | size_t estimatedSize = wireEncode(estimator); |
| 196 | |
| 197 | ndn::EncodingBuffer buffer(estimatedSize, 0); |
| 198 | wireEncode(buffer); |
| 199 | |
| 200 | m_wire = buffer.block(); |
| 201 | return m_wire; |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | Leaf::wireDecode(const Block& wire) |
| 206 | { |
| 207 | if (!wire.hasWire()) { |
| 208 | throw Error("The supplied block does not contain wire format"); |
| 209 | } |
| 210 | |
| 211 | m_wire = wire; |
| 212 | m_wire.parse(); |
| 213 | |
| 214 | if (m_wire.type() != tlv::LoggerLeaf) |
| 215 | throw tlv::Error("Unexpected TLV type when decoding logger leaf"); |
| 216 | |
| 217 | Block::element_const_iterator it = m_wire.elements_begin(); |
| 218 | |
| 219 | // the first block must be dataName |
| 220 | if (it != m_wire.elements_end() && it->type() == tlv::Name) { |
| 221 | m_dataName.wireDecode(*it); |
| 222 | it++; |
| 223 | } |
| 224 | else |
| 225 | throw Error("The first sub-TLV is not Name"); |
| 226 | |
| 227 | // the second block must be timestamp |
| 228 | if (it != m_wire.elements_end() && it->type() == tlv::Timestamp) { |
| 229 | m_timestamp = readNonNegativeInteger(*it); |
| 230 | it++; |
| 231 | } |
| 232 | else |
| 233 | throw Error("The second sub-TLV is not Timestamp"); |
| 234 | |
| 235 | // the third block must be DataSeqNo |
| 236 | if (it != m_wire.elements_end() && it->type() == tlv::DataSeqNo) { |
| 237 | m_dataSeqNo = readNonNegativeInteger(*it); |
| 238 | it++; |
| 239 | } |
| 240 | else |
| 241 | throw Error("The third sub-TLV is not DataSeqNo"); |
| 242 | |
| 243 | // the third block must be SignerSeqNo |
| 244 | if (it != m_wire.elements_end() && it->type() == tlv::SignerSeqNo) { |
| 245 | m_signerSeqNo = readNonNegativeInteger(*it); |
| 246 | it++; |
| 247 | } |
| 248 | else |
| 249 | throw Error("The fourth sub-TLV is not SignerSeqNo"); |
| 250 | |
| 251 | if (it != m_wire.elements_end()) |
| 252 | throw Error("No more sub-TLV in LoggerLeaf"); |
peizhen guo | cf4df2d | 2014-08-12 13:22:32 -0700 | [diff] [blame] | 253 | } |
| 254 | |
Alexander Afanasyev | 49e2e4c | 2017-05-06 13:42:57 -0700 | [diff] [blame^] | 255 | } // namespace delorean |
| 256 | } // namespace ndn |