Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 2 | /** |
Jeff Thompson | 7687dc0 | 2013-09-13 11:54:07 -0700 | [diff] [blame] | 3 | * Copyright (C) 2013 Regents of the University of California. |
| 4 | * @author: Jeff Thompson <jefft0@remap.ucla.edu> |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 5 | * See COPYING for copyright and distribution information. |
| 6 | */ |
| 7 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 8 | #ifndef NDN_DATA_HPP |
Jeff Thompson | a0d18c9 | 2013-08-06 13:55:32 -0700 | [diff] [blame] | 9 | #define NDN_DATA_HPP |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 10 | |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 11 | #include "common.hpp" |
Jeff Thompson | 5341219 | 2013-08-06 13:35:50 -0700 | [diff] [blame] | 12 | #include "name.hpp" |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 13 | #include "encoding/block.hpp" |
Jeff Thompson | 25b4e61 | 2013-10-10 16:03:24 -0700 | [diff] [blame] | 14 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 15 | |
| 16 | namespace ndn { |
| 17 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 18 | /** |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 19 | * A Signature is storage for the signature-related information (info and value) in a Data packet. |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 20 | */ |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 21 | class Signature { |
| 22 | public: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 23 | enum { |
| 24 | DigestSha256 = 0, |
| 25 | SignatureSha256WithRsa = 1 |
| 26 | }; |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 27 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 28 | Signature() |
| 29 | : type_(-1) |
| 30 | { |
| 31 | } |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 32 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 33 | Signature(const Block &info, const Block &value) |
| 34 | : info_(info) |
| 35 | , value_(value) |
| 36 | { |
| 37 | Buffer::const_iterator i = info_.value_begin(); |
| 38 | type_ = Tlv::readVarNumber(i, info_.value_end()); |
| 39 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 40 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 41 | operator bool() const |
| 42 | { |
| 43 | return type_ != -1; |
| 44 | } |
| 45 | |
| 46 | uint32_t |
| 47 | getType() const |
| 48 | { |
| 49 | return type_; |
| 50 | } |
| 51 | |
| 52 | const Block& |
| 53 | getInfo() const |
| 54 | { |
| 55 | return info_; |
| 56 | } |
| 57 | |
| 58 | void |
| 59 | setInfo(const Block &info) |
| 60 | { |
| 61 | info_ = info; |
| 62 | if (info_.hasWire() || info_.hasValue()) |
| 63 | { |
| 64 | info_.parse(); |
| 65 | const Block &signatureType = info_.get(Tlv::SignatureType); |
| 66 | |
| 67 | Buffer::const_iterator i = signatureType.value_begin(); |
| 68 | type_ = Tlv::readVarNumber(i, signatureType.value_end()); |
| 69 | } |
| 70 | else |
| 71 | { |
| 72 | type_ = -1; |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | const Block& |
| 77 | getValue() const |
| 78 | { |
| 79 | return value_; |
| 80 | } |
| 81 | |
| 82 | void |
| 83 | setValue(const Block &value) |
| 84 | { |
| 85 | value_ = value; |
| 86 | } |
| 87 | |
| 88 | void |
| 89 | reset() |
| 90 | { |
| 91 | type_ = -1; |
| 92 | info_.reset(); |
| 93 | value_.reset(); |
| 94 | } |
| 95 | |
| 96 | private: |
| 97 | int32_t type_; |
| 98 | |
| 99 | Block info_; |
| 100 | Block value_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
Jeff Thompson | f4585af | 2013-09-11 14:56:59 -0700 | [diff] [blame] | 103 | /** |
| 104 | * An MetaInfo holds the meta info which is signed inside the data packet. |
| 105 | */ |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 106 | class MetaInfo { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 107 | public: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 108 | enum { |
| 109 | TYPE_DEFAULT = 0, |
| 110 | TYPE_LINK = 1, |
| 111 | TYPE_KEY = 2 |
| 112 | }; |
| 113 | |
| 114 | MetaInfo() |
| 115 | : type_(TYPE_DEFAULT) |
| 116 | , freshnessPeriod_(-1) |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 117 | { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 118 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 119 | |
| 120 | uint32_t |
| 121 | getType() const |
| 122 | { return type_; } |
| 123 | |
| 124 | void |
| 125 | setType(uint32_t type) |
| 126 | { type_ = type; } |
| 127 | |
| 128 | Milliseconds |
| 129 | getFreshnessPeriod() const |
| 130 | { return freshnessPeriod_; } |
| 131 | |
| 132 | void |
| 133 | setFreshnessPeriod(Milliseconds freshnessPeriod) |
| 134 | { freshnessPeriod_ = freshnessPeriod; } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 135 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 136 | private: |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 137 | uint32_t type_; |
| 138 | Milliseconds freshnessPeriod_; |
| 139 | |
| 140 | Block wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 141 | }; |
| 142 | |
Jeff Thompson | 56ec9e2 | 2013-08-02 11:34:07 -0700 | [diff] [blame] | 143 | class Data { |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 144 | public: |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 145 | /** |
| 146 | * Create a new Data object with default values and where the signature is a blank Sha256WithRsaSignature. |
| 147 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 148 | Data() |
| 149 | { |
| 150 | } |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 151 | |
| 152 | /** |
| 153 | * Create a new Data object with the given name and default values and where the signature is a blank Sha256WithRsaSignature. |
| 154 | * @param name A reference to the name which is copied. |
| 155 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 156 | Data(const Name& name) |
| 157 | : name_(name) |
| 158 | { |
| 159 | } |
Jeff Thompson | 25bfdca | 2013-10-16 17:05:41 -0700 | [diff] [blame] | 160 | |
| 161 | /** |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 162 | * The virtual destructor. |
| 163 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 164 | virtual ~Data() |
| 165 | { |
| 166 | } |
Jeff Thompson | c69163b | 2013-10-12 13:49:50 -0700 | [diff] [blame] | 167 | |
| 168 | /** |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 169 | * Encode this Data for a particular wire format. If wireFormat is the default wire format, also set the defaultWireEncoding |
| 170 | * field to the encoded result. |
| 171 | * Even though this is const, if wireFormat is the default wire format we update the defaultWireEncoding. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 172 | * @param wireFormat A WireFormat object used to encode the input. If omitted, use WireFormat getDefaultWireFormat(). |
| 173 | * @return The encoded byte array. |
| 174 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 175 | const Block& |
| 176 | wireEncode() const; |
| 177 | |
| 178 | void |
| 179 | wireDecode(const Block &wire); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 180 | |
| 181 | /** |
Jeff Thompson | f1ffba8 | 2013-10-19 17:57:12 -0700 | [diff] [blame] | 182 | * Decode the input using a particular wire format and update this Data. If wireFormat is the default wire format, also |
| 183 | * set the defaultWireEncoding field to the input. |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 184 | * @param input The input byte array to be decoded. |
| 185 | * @param inputLength The length of input. |
| 186 | * @param wireFormat A WireFormat object used to decode the input. If omitted, use WireFormat getDefaultWireFormat(). |
| 187 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 188 | void |
| 189 | wireDecode(const uint8_t* input, size_t inputLength); |
| 190 | |
| 191 | const Signature& |
| 192 | getSignature() const |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 193 | { |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 194 | return signature_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 195 | } |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 196 | |
| 197 | /** |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 198 | * Set the signature to a copy of the given signature. |
| 199 | * @param signature The signature object which is cloned. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 200 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 20af073 | 2013-09-12 17:01:45 -0700 | [diff] [blame] | 201 | */ |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 202 | Data& |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 203 | setSignature(const Signature& signature) |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 204 | { |
| 205 | signature_ = signature; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 206 | onChanged(); |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 207 | return *this; |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 208 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 209 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 210 | const Name& |
| 211 | getName() const |
| 212 | { |
| 213 | return name_; |
| 214 | } |
| 215 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 216 | /** |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 217 | * Set name to a copy of the given Name. This is virtual so that a subclass can override to validate the name. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 218 | * @param name The Name which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 219 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 220 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 221 | void |
| 222 | setName(const Name& name) |
| 223 | { |
| 224 | name_ = name; |
| 225 | onChanged(); |
| 226 | } |
| 227 | |
| 228 | const MetaInfo& |
| 229 | getMetaInfo() const { return metaInfo_; } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 230 | |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 231 | /** |
| 232 | * Set metaInfo to a copy of the given MetaInfo. |
| 233 | * @param metaInfo The MetaInfo which is copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 234 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0cd8c4a | 2013-09-13 17:46:40 -0700 | [diff] [blame] | 235 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 236 | void |
Jeff Thompson | 6b58c2f | 2013-11-19 17:14:25 -0800 | [diff] [blame] | 237 | setMetaInfo(const MetaInfo& metaInfo) |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 238 | { |
| 239 | metaInfo_ = metaInfo; |
| 240 | onChanged(); |
| 241 | } |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 242 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 243 | const Block& |
| 244 | getContent() const { return content_; } |
| 245 | |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 246 | /** |
| 247 | * Set the content to a copy of the data in the vector. |
| 248 | * @param content A vector whose contents are copied. |
Jeff Thompson | 6d59197 | 2013-10-17 11:16:32 -0700 | [diff] [blame] | 249 | * @return This Data so that you can chain calls to update values. |
Jeff Thompson | 0899c0f | 2013-09-12 12:15:31 -0700 | [diff] [blame] | 250 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 251 | void |
Jeff Thompson | 10ad12a | 2013-09-24 16:19:11 -0700 | [diff] [blame] | 252 | setContent(const std::vector<uint8_t>& content) |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 253 | { |
| 254 | setContent(&content[0], content.size()); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 255 | onChanged(); |
| 256 | } |
Jeff Thompson | 3776d1c | 2013-09-17 14:22:51 -0700 | [diff] [blame] | 257 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 258 | void |
Jeff Thompson | 97223af | 2013-09-24 17:01:27 -0700 | [diff] [blame] | 259 | setContent(const uint8_t* content, size_t contentLength) |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 260 | { |
| 261 | OBufferStream os; |
| 262 | Tlv::writeVarNumber(os, Tlv::Content); |
| 263 | Tlv::writeVarNumber(os, contentLength); |
| 264 | os.write(reinterpret_cast<const char *>(content), contentLength); |
| 265 | |
| 266 | content_ = Block(os.buf()); |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 267 | onChanged(); |
Jeff Thompson | 46bd45f | 2013-08-08 16:46:41 -0700 | [diff] [blame] | 268 | } |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 269 | |
| 270 | void |
| 271 | setContent(const ConstBufferPtr &contentValue) |
| 272 | { |
| 273 | content_ = Block(Tlv::Content, contentValue); // not real a wire encoding yet |
| 274 | onChanged(); |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | setContent(const Block& content) |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 279 | { |
| 280 | content_ = content; |
| 281 | onChanged(); |
| 282 | } |
Jeff Thompson | c2b7b14 | 2013-09-12 15:29:04 -0700 | [diff] [blame] | 283 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 284 | private: |
Jeff Thompson | b7aefa00 | 2013-09-16 18:22:00 -0700 | [diff] [blame] | 285 | /** |
| 286 | * Clear the wire encoding. |
| 287 | */ |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 288 | inline void |
Jeff Thompson | 0050abe | 2013-09-17 12:50:25 -0700 | [diff] [blame] | 289 | onChanged(); |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 290 | |
| 291 | private: |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 292 | Name name_; |
Jeff Thompson | fec716d | 2013-09-11 13:54:36 -0700 | [diff] [blame] | 293 | MetaInfo metaInfo_; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 294 | Block content_; |
| 295 | Signature signature_; |
| 296 | |
| 297 | Block wire_; |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 298 | }; |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 299 | |
| 300 | |
| 301 | inline void |
| 302 | Data::onChanged() |
| 303 | { |
| 304 | // The values have changed, so the signature and wire format is invalidated |
| 305 | signature_.reset(); |
| 306 | wire_.reset(); |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Alexander Afanasyev | fadc97d | 2014-01-03 13:22:10 -0800 | [diff] [blame^] | 309 | } // namespace ndn |
| 310 | |
Jeff Thompson | 5cae5e5 | 2013-07-10 19:41:20 -0700 | [diff] [blame] | 311 | #endif |