der-node.hpp
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
24 #ifndef NDN_DER_NODE_HPP
25 #define NDN_DER_NODE_HPP
26 
27 #include <ndn-cpp/util/blob.hpp>
28 #include <ndn-cpp/encoding/oid.hpp>
29 #include "../../util/dynamic-uint8-vector.hpp"
30 #include "der-node-type.hpp"
31 
32 namespace ndn {
33 
38 class DerNode {
39 public:
40  virtual size_t
41  getSize();
42 
47  virtual Blob
48  encode();
49 
57  static ptr_lib::shared_ptr<DerNode>
58  parse(const uint8_t* inputBuf, size_t startIdx = 0);
59 
65  virtual Blob
66  toVal();
67 
72  Blob
73  getPayload() { return Blob(&payload_[0], payloadPosition_); }
74 
82  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
83  getChildren();
84 
85  class DerStructure;
86 
88  // Now for all the node types...
89  // Class definitions are after the definition of DerNode.
91 
92  class DerByteString;
93  class DerBoolean;
94  class DerInteger;
95  class DerBitString;
96  class DerOctetString;
97  class DerNull;
98  class DerOid;
99  class DerSequence;
100  class DerPrintableString;
101  class DerGeneralizedTime;
102 
113  static DerNode::DerSequence&
115  (const std::vector<ptr_lib::shared_ptr<DerNode> >&children, size_t index);
116 
117 protected:
124  : nodeType_(nodeType),
125  parent_(0),
126  payload_(0),
127  payloadPosition_(0)
128  {
129  }
130 
135  void
136  encodeHeader(size_t size);
137 
144  size_t
145  decodeHeader(const uint8_t* inputBuf, size_t startIdx);
146 
152  virtual void
153  decode(const uint8_t* inputBuf, size_t startIdx);
154 
161  void
162  payloadAppend(const uint8_t *value, size_t valueLength)
163  {
164  payloadPosition_ = payload_.copy(value, valueLength, payloadPosition_);
165  }
166 
167  DerStructure* parent_;
168  DerNodeType nodeType_;
169  std::vector<uint8_t> header_;
170  DynamicUInt8Vector payload_;
171  size_t payloadPosition_;
172 };
173 
178 public:
183  virtual size_t
184  getSize();
185 
190  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
191  getChildren();
192 
193  void
194  addChild(const ptr_lib::shared_ptr<DerNode>& node, bool notifyParent = false)
195  {
196  node->parent_ = this;
197  nodeList_.push_back(node);
198 
199  if (notifyParent) {
200  if (parent_)
201  parent_->setChildChanged();
202  }
203 
204  childChanged_ = true;
205  }
206 
212  virtual Blob
213  encode();
214 
215 protected:
222  : DerNode(nodeType),
223  childChanged_(false),
224  size_(0)
225  {
226  }
227 
234  virtual void
235  decode(const uint8_t* inputBuf, size_t startIdx);
236 
237 private:
238  void
239  updateSize();
240 
244  void
245  setChildChanged()
246  {
247  if (parent_)
248  parent_->setChildChanged();
249  childChanged_ = true;
250  }
251 
252  bool childChanged_;
253  std::vector<ptr_lib::shared_ptr<DerNode> > nodeList_;
254  size_t size_;
255 };
256 
261 public:
266  virtual Blob
267  toVal();
268 
269 protected:
279  (const uint8_t* inputData, size_t inputDataLength, DerNodeType nodeType)
280  : DerNode(nodeType)
281  {
282  if (inputData) {
283  payloadAppend(inputData, inputDataLength);
284  encodeHeader(inputDataLength);
285  }
286  }
287 };
288 
294 class DerNode::DerBoolean : public DerNode {
295 public:
300  DerBoolean(bool value)
301  : DerNode(DerNodeType_Boolean)
302  {
303  uint8_t val = value ? 0xff : 0x00;
304  payloadAppend(&val, 1);
305  encodeHeader(1);
306  }
307 
308  DerBoolean()
309  : DerNode(DerNodeType_Boolean)
310  {
311  }
312 };
313 
317 class DerNode::DerInteger : public DerNode {
318 public:
323  DerInteger(int integer);
324 
325  DerInteger();
326 
332  int
333  toIntegerVal() const;
334 };
335 
340 public:
348  DerBitString(const uint8_t* inputBuf, size_t inputBufLength, int paddingLen)
349  : DerNode(DerNodeType_BitString)
350  {
351  uint8_t pad = paddingLen & 0xff;
352  payloadAppend(&pad, 1);
353  payloadAppend(inputBuf, inputBufLength);
354  encodeHeader(payloadPosition_);
355  }
356 
357  DerBitString()
358  : DerNode(DerNodeType_BitString)
359  {
360  }
361 };
362 
369 public:
370  DerOctetString(const uint8_t* inputData, size_t inputDataLength)
371  : DerByteString(inputData, inputDataLength, DerNodeType_OctetString)
372  {
373  }
374 
376  : DerByteString(0, 0, DerNodeType_OctetString)
377  {
378  }
379 };
380 
384 class DerNode::DerNull : public DerNode {
385 public:
390  : DerNode(DerNodeType_Null)
391  {
392  encodeHeader(0);
393  }
394 };
395 
399 class DerNode::DerOid : public DerNode {
400 public:
406  DerOid(const std::string& oidStr)
407  : DerNode(DerNodeType_ObjectIdentifier)
408  {
409  // Use OID to construct the integer list.
410  OID tempOid(oidStr);
411  prepareEncoding(tempOid.getIntegerList());
412  }
413 
419  DerOid(const OID& oid)
420  : DerNode(DerNodeType_ObjectIdentifier)
421  {
422  prepareEncoding(oid.getIntegerList());
423  }
424 
425  DerOid()
426  : DerNode(DerNodeType_ObjectIdentifier)
427  {
428  }
429 
434  virtual Blob
435  toVal();
436 
437 private:
442  void
443  prepareEncoding(const std::vector<int>& value);
444 
451  static std::vector<uint8_t>
452  encode128(int value);
453 
460  int
461  decode128(size_t offset, size_t& skip);
462 };
463 
465 public:
470  : DerStructure(DerNodeType_Sequence)
471  {
472  }
473 };
474 
482 public:
483  DerPrintableString(const uint8_t* inputData, size_t inputDataLength)
484  : DerByteString(inputData, inputDataLength, DerNodeType_PrintableString)
485  {
486  }
487 
489  : DerByteString(0, 0, DerNodeType_PrintableString)
490  {
491  }
492 };
493 
499 public:
505  : DerNode(DerNodeType_GeneralizedTime)
506  {
507  std::string derTime = toDerTimeString(msSince1970);
508  payloadAppend((const uint8_t*)&derTime[0], derTime.size());
509  encodeHeader(payloadPosition_);
510  }
511 
513  : DerNode(DerNodeType_GeneralizedTime)
514  {
515  }
516 
524 
530  static std::string
531  toIsoString(const MillisecondsSince1970& time);
532 
538  static MillisecondsSince1970
539  fromIsoString(const std::string& isoString);
540 
541 private:
547  static std::string
548  toDerTimeString(MillisecondsSince1970 msSince1970);
549 };
550 
551 }
552 
553 #endif
Blob getPayload()
Get a copy of the payload bytes.
Definition: der-node.hpp:73
DerOid(const OID &oid)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:419
DerOctetString extends DerByteString to encode a string of bytes.
Definition: der-node.hpp:368
Copyright (C) 2013-2016 Regents of the University of California.
Definition: common.hpp:35
DerSequence()
Create a DerSequence.
Definition: der-node.hpp:469
A DerBitString extends DerNode to handle a bit string.
Definition: der-node.hpp:339
virtual Blob encode()
Get the raw data encoding for this node.
Definition: der-node.cpp:40
DerBitString(const uint8_t *inputBuf, size_t inputBufLength, int paddingLen)
Create a DerBitString with the given padding and inputBuf.
Definition: der-node.hpp:348
virtual size_t getSize()
Override to get the total length of the encoding, including children.
Definition: der-node.cpp:192
static DerNode::DerSequence & getSequence(const std::vector< ptr_lib::shared_ptr< DerNode > > &children, size_t index)
Check that index is in bounds for the children list, cast children[index] to DerSequence and return i...
Definition: der-node.cpp:98
int toIntegerVal() const
Parse the payload as an integer and return the value.
Definition: der-node.cpp:304
void encodeHeader(size_t size)
Encode the given size and update the header.
Definition: der-node.cpp:112
DerNull()
Create a DerNull.
Definition: der-node.hpp:389
virtual Blob toVal()
Convert the encoded data to a standard representation.
Definition: der-node.cpp:85
DerNodeType
The DerNodeType enum defines the known DER node types.
Definition: der-node-type.hpp:32
virtual void decode(const uint8_t *inputBuf, size_t startIdx)
Override the base decode to decode and store the data from an input buffer.
Definition: der-node.cpp:230
Definition: der-node.hpp:464
DerInteger extends DerNode to encode an integer value.
Definition: der-node.hpp:317
MillisecondsSince1970 toMillisecondsSince1970()
Interpret the result of toVal() as a time string and return the milliseconds since 1970...
Definition: der-node.cpp:420
static MillisecondsSince1970 fromIsoString(const std::string &isoString)
Convert from the ISO string representation to the internal time format.
Definition: der-node.cpp:450
virtual Blob encode()
Override the base encode to return raw data encoding for this node and its children.
Definition: der-node.cpp:210
A DerPrintableString extends DerByteString to handle a a printable string.
Definition: der-node.hpp:481
DerByteString(const uint8_t *inputData, size_t inputDataLength, DerNodeType nodeType)
Create a DerByteString with the given inputData and nodeType.
Definition: der-node.hpp:279
DerOid(const std::string &oidStr)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:406
virtual Blob toVal()
Override to return the string representation of the OID.
Definition: der-node.cpp:320
A DerStructure extends DerNode to hold other DerNodes.
Definition: der-node.hpp:177
A Blob holds a pointer to an immutable byte array implemented as const std::vector.
Definition: blob.hpp:42
A DerOid extends DerNode to represent an object identifier.
Definition: der-node.hpp:399
DerBoolean extends DerNode to encode a boolean value.
Definition: der-node.hpp:294
virtual Blob toVal()
Override to return just the byte string.
Definition: der-node.cpp:260
double MillisecondsSince1970
The calendar time represented as the number of milliseconds since 1/1/1970.
Definition: common.hpp:116
A DerNull extends DerNode to encode a null value.
Definition: der-node.hpp:384
DerNode implements the DER node types used in encoding/decoding DER-formatted data.
Definition: der-node.hpp:38
DerStructure(DerNodeType nodeType)
Create a DerStructure with the given nodeType.
Definition: der-node.hpp:221
size_t decodeHeader(const uint8_t *inputBuf, size_t startIdx)
Extract the header from an input buffer and return the size.
Definition: der-node.cpp:146
virtual void decode(const uint8_t *inputBuf, size_t startIdx)
Decode and store the data from an input buffer.
Definition: der-node.cpp:180
DerNode(DerNodeType nodeType)
Create a generic DER node with the given nodeType.
Definition: der-node.hpp:123
Definition: oid.hpp:31
size_t copy(const uint8_t *value, size_t valueLength, size_t offset)
Copy value into the vector at offset, using ensureLength to make sure the vector has enough size...
Definition: dynamic-uint8-vector.hpp:68
static std::string toIsoString(const MillisecondsSince1970 &time)
Convert to the ISO string representation of the time.
Definition: der-node.cpp:439
DerBoolean(bool value)
Create a new DerBoolean for the value.
Definition: der-node.hpp:300
virtual const std::vector< ptr_lib::shared_ptr< DerNode > > & getChildren()
Get the children of this node.
Definition: der-node.cpp:204
virtual const std::vector< ptr_lib::shared_ptr< DerNode > > & getChildren()
If this object is a DerSequence, get the children of this node.
Definition: der-node.cpp:91
A DerGeneralizedTime extends DerNode to represent a date and time, with millisecond accuracy...
Definition: der-node.hpp:498
A DynamicUInt8Vector extends ndn_DynamicUInt8Array to hold a shared_ptr > for use wit...
Definition: dynamic-uint8-vector.hpp:37
DerGeneralizedTime(MillisecondsSince1970 msSince1970)
Create a DerGeneralizedTime with the given milliseconds since 1970.
Definition: der-node.hpp:504
void payloadAppend(const uint8_t *value, size_t valueLength)
Call payload_.copy to copy value into payload_ at payloadPosition_.
Definition: der-node.hpp:162
static ptr_lib::shared_ptr< DerNode > parse(const uint8_t *inputBuf, size_t startIdx=0)
Parse the data from the input buffer recursively and return the root as an object of a subclass of De...
Definition: der-node.cpp:53
A DerByteString extends DerNode to handle byte strings.
Definition: der-node.hpp:260