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 
75  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
76  getChildren();
77 
78  class DerStructure;
79 
81  // Now for all the node types...
82  // Class definitions are after the definition of DerNode.
84 
85  class DerByteString;
86  class DerBoolean;
87  class DerInteger;
88  class DerBitString;
89  class DerOctetString;
90  class DerNull;
91  class DerOid;
92  class DerSequence;
93  class DerPrintableString;
94  class DerGeneralizedTime;
95 
106  static DerNode::DerSequence&
108  (const std::vector<ptr_lib::shared_ptr<DerNode> >&children, size_t index);
109 
110 protected:
117  : nodeType_(nodeType),
118  parent_(0),
119  payload_(0),
120  payloadPosition_(0)
121  {
122  }
123 
128  void
129  encodeHeader(size_t size);
130 
137  size_t
138  decodeHeader(const uint8_t* inputBuf, size_t startIdx);
139 
145  virtual void
146  decode(const uint8_t* inputBuf, size_t startIdx);
147 
154  void
155  payloadAppend(const uint8_t *value, size_t valueLength)
156  {
157  payloadPosition_ = payload_.copy(value, valueLength, payloadPosition_);
158  }
159 
160  DerStructure* parent_;
161  DerNodeType nodeType_;
162  std::vector<uint8_t> header_;
163  DynamicUInt8Vector payload_;
164  size_t payloadPosition_;
165 };
166 
171 public:
176  virtual size_t
177  getSize();
178 
183  virtual const std::vector<ptr_lib::shared_ptr<DerNode> >&
184  getChildren();
185 
186  void
187  addChild(const ptr_lib::shared_ptr<DerNode>& node, bool notifyParent = false)
188  {
189  node->parent_ = this;
190  nodeList_.push_back(node);
191 
192  if (notifyParent) {
193  if (parent_)
194  parent_->setChildChanged();
195  }
196 
197  childChanged_ = true;
198  }
199 
205  virtual Blob
206  encode();
207 
208 protected:
215  : DerNode(nodeType),
216  childChanged_(false),
217  size_(0)
218  {
219  }
220 
227  virtual void
228  decode(const uint8_t* inputBuf, size_t startIdx);
229 
230 private:
231  void
232  updateSize();
233 
237  void
238  setChildChanged()
239  {
240  if (parent_)
241  parent_->setChildChanged();
242  childChanged_ = true;
243  }
244 
245  bool childChanged_;
246  std::vector<ptr_lib::shared_ptr<DerNode> > nodeList_;
247  size_t size_;
248 };
249 
254 public:
259  virtual Blob
260  toVal();
261 
262 protected:
272  (const uint8_t* inputData, size_t inputDataLength, DerNodeType nodeType)
273  : DerNode(nodeType)
274  {
275  if (inputData) {
276  payloadAppend(inputData, inputDataLength);
277  encodeHeader(inputDataLength);
278  }
279  }
280 };
281 
287 class DerNode::DerBoolean : public DerNode {
288 public:
293  DerBoolean(bool value)
294  : DerNode(DerNodeType_Boolean)
295  {
296  uint8_t val = value ? 0xff : 0x00;
297  payloadAppend(&val, 1);
298  encodeHeader(1);
299  }
300 
301  DerBoolean()
302  : DerNode(DerNodeType_Boolean)
303  {
304  }
305 };
306 
310 class DerNode::DerInteger : public DerNode {
311 public:
316  DerInteger(int integer);
317 
318  DerInteger();
319 
325  int
326  toIntegerVal() const;
327 };
328 
333 public:
341  DerBitString(const uint8_t* inputBuf, size_t inputBufLength, int paddingLen)
342  : DerNode(DerNodeType_BitString)
343  {
344  uint8_t pad = paddingLen & 0xff;
345  payloadAppend(&pad, 1);
346  payloadAppend(inputBuf, inputBufLength);
347  encodeHeader(payloadPosition_);
348  }
349 
350  DerBitString()
351  : DerNode(DerNodeType_BitString)
352  {
353  }
354 };
355 
362 public:
363  DerOctetString(const uint8_t* inputData, size_t inputDataLength)
364  : DerByteString(inputData, inputDataLength, DerNodeType_OctetString)
365  {
366  }
367 
369  : DerByteString(0, 0, DerNodeType_OctetString)
370  {
371  }
372 };
373 
377 class DerNode::DerNull : public DerNode {
378 public:
383  : DerNode(DerNodeType_Null)
384  {
385  encodeHeader(0);
386  }
387 };
388 
392 class DerNode::DerOid : public DerNode {
393 public:
399  DerOid(const std::string& oidStr)
400  : DerNode(DerNodeType_ObjectIdentifier)
401  {
402  // Use OID to construct the integer list.
403  OID tempOid(oidStr);
404  prepareEncoding(tempOid.getIntegerList());
405  }
406 
412  DerOid(const OID& oid)
413  : DerNode(DerNodeType_ObjectIdentifier)
414  {
415  prepareEncoding(oid.getIntegerList());
416  }
417 
418  DerOid()
419  : DerNode(DerNodeType_ObjectIdentifier)
420  {
421  }
422 
427  virtual Blob
428  toVal();
429 
430 private:
435  void
436  prepareEncoding(const std::vector<int>& value);
437 
444  static std::vector<uint8_t>
445  encode128(int value);
446 
453  int
454  decode128(size_t offset, size_t& skip);
455 };
456 
458 public:
463  : DerStructure(DerNodeType_Sequence)
464  {
465  }
466 };
467 
475 public:
476  DerPrintableString(const uint8_t* inputData, size_t inputDataLength)
477  : DerByteString(inputData, inputDataLength, DerNodeType_PrintableString)
478  {
479  }
480 
482  : DerByteString(0, 0, DerNodeType_PrintableString)
483  {
484  }
485 };
486 
492 public:
498  : DerNode(DerNodeType_GeneralizedTime)
499  {
500  std::string derTime = toDerTimeString(msSince1970);
501  payloadAppend((const uint8_t*)&derTime[0], derTime.size());
502  encodeHeader(payloadPosition_);
503  }
504 
506  : DerNode(DerNodeType_GeneralizedTime)
507  {
508  }
509 
517 
523  static std::string
524  toIsoString(const MillisecondsSince1970& time);
525 
531  static MillisecondsSince1970
532  fromIsoString(const std::string& isoString);
533 
534 private:
540  static std::string
541  toDerTimeString(MillisecondsSince1970 msSince1970);
542 };
543 
544 }
545 
546 #endif
DerOid(const OID &oid)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:412
DerOctetString extends DerByteString to encode a string of bytes.
Definition: der-node.hpp:361
Copyright (C) 2013-2015 Regents of the University of California.
Definition: common.hpp:35
DerSequence()
Create a DerSequence.
Definition: der-node.hpp:462
A DerBitString extends DerNode to handle a bit string.
Definition: der-node.hpp:332
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:341
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:294
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:382
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:457
DerInteger extends DerNode to encode an integer value.
Definition: der-node.hpp:310
MillisecondsSince1970 toMillisecondsSince1970()
Interpret the result of toVal() as a time string and return the milliseconds since 1970...
Definition: der-node.cpp:406
static MillisecondsSince1970 fromIsoString(const std::string &isoString)
Convert from the ISO string representation to the internal time format.
Definition: der-node.cpp:436
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:474
DerByteString(const uint8_t *inputData, size_t inputDataLength, DerNodeType nodeType)
Create a DerByteString with the given inputData and nodeType.
Definition: der-node.hpp:272
DerOid(const std::string &oidStr)
Create a DerOid with the given object identifier.
Definition: der-node.hpp:399
virtual Blob toVal()
Override to return the string representation of the OID.
Definition: der-node.cpp:306
A DerStructure extends DerNode to hold other DerNodes.
Definition: der-node.hpp:170
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:392
DerBoolean extends DerNode to encode a boolean value.
Definition: der-node.hpp:287
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:377
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:214
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:116
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:67
static std::string toIsoString(const MillisecondsSince1970 &time)
Convert to the ISO string representation of the time.
Definition: der-node.cpp:425
DerBoolean(bool value)
Create a new DerBoolean for the value.
Definition: der-node.hpp:293
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:491
A DynamicUInt8Vector extends ndn_DynamicUInt8Array to hold a shared_ptr > for use wit...
Definition: dynamic-uint8-vector.hpp:36
DerGeneralizedTime(MillisecondsSince1970 msSince1970)
Create a DerGeneralizedTime with the given milliseconds since 1970.
Definition: der-node.hpp:497
void payloadAppend(const uint8_t *value, size_t valueLength)
Call payload_.copy to copy value into payload_ at payloadPosition_.
Definition: der-node.hpp:155
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:253