blob: d8753be1ee3955078a5223abaf69be4f659d5281 [file] [log] [blame]
Jeff Thompson958bf9b2013-10-12 17:20:51 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
2/**
3 * Copyright (C) 2013 Regents of the University of California.
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
9#ifndef NDN_DER_HPP
10#define NDN_DER_HPP
11
12#include <vector>
13#include <string>
14#include <istream>
15#include <ostream>
16#include <sstream>
17
18#include <ndn-cpp/common.hpp>
19#include <ndn-cpp/encoding/oid.hpp>
Jeff Thompson415da1e2013-10-17 16:52:59 -070020#include <ndn-cpp/util/blob.hpp>
Jeff Thompson958bf9b2013-10-12 17:20:51 -070021
22#include "visitor/visitor.hpp"
23#include "visitor/void-visitor.hpp"
24#include "visitor/no-arguments-visitor.hpp"
25#include "visitor/void-no-arguments-visitor.hpp"
26
27namespace ndn {
28
Jeff Thompson958bf9b2013-10-12 17:20:51 -070029namespace der {
30
31enum DerType {
32 DER_EOC = 0,
33 DER_BOOLEAN = 1,
34 DER_INTEGER = 2,
35 DER_BIT_STRING = 3,
36 DER_OCTET_STRING = 4,
37 DER_NULL = 5,
38 DER_OBJECT_IDENTIFIER = 6,
39 DER_OBJECT_DESCRIPTOR = 7,
40 DER_EXTERNAL = 40,
41 DER_REAL = 9,
42 DER_ENUMERATED = 10,
43 DER_EMBEDDED_PDV = 43,
44 DER_UTF8_STRING = 12,
45 DER_RELATIVE_OID = 13,
46 DER_SEQUENCE = 48,
47 DER_SET = 49,
48 DER_NUMERIC_STRING = 18,
49 DER_PRINTABLE_STRING = 19,
50 DER_T61_STRING = 20,
51 DER_VIDEOTEX_STRING = 21,
52 DER_IA5_STRING = 22,
53 DER_UTC_TIME = 23,
54 DER_GENERALIZED_TIME = 24,
55 DER_GRAPHIC_STRING = 25,
56 DER_VISIBLE_STRING = 26,
57 DER_GENERAL_STRING = 27,
58 DER_UNIVERSAL_STRING = 28,
59 DER_CHARACTER_STRING = 29,
60 DER_BMP_STRING = 30,
61};
62
Jeff Thompson2d47db72013-10-17 15:19:52 -070063class InputIterator : public std::istream
64{
65public:
66 uint8_t ReadU8() { return static_cast<uint8_t> (get()); }
67 uint8_t PeekU8() { return static_cast<uint8_t> (peek()); }
68 bool IsEnd() const { return eof(); }
69 void Prev() { seekg(-1, std::ios_base::cur); }
70};
71
72class OutputIterator : public std::ostream
73{
74public:
75 void Write(const uint8_t * s, uint32_t n) { write (reinterpret_cast<const char*>(s),n); }
76 void WriteU8(const uint8_t s) { put (s); }
77 void WriteU8(const uint8_t s, uint32_t n) { for (uint32_t i = 0; i < n; i++) { put(s); } }
78};
79
Jeff Thompson958bf9b2013-10-12 17:20:51 -070080class DerComplex;
81
82class DerNode
83{
84public:
85 DerNode();
86
87 DerNode(DerType type);
88
Jeff Thompson2d47db72013-10-17 15:19:52 -070089 DerNode(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -070090
91 virtual
92 ~DerNode();
93
94 virtual int
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070095 getSize() { return header_.size() + payload_.size(); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070096
97 virtual void
Jeff Thompson2d47db72013-10-17 15:19:52 -070098 encode(OutputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -070099
100 void
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700101 setParent(DerComplex * parent) { parent_ = parent; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700102
103 static ptr_lib::shared_ptr<DerNode>
Jeff Thompson2d47db72013-10-17 15:19:52 -0700104 parse(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700105
106 const std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700107 getHeader() const { return header_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700108
109 std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700110 getHeader() { return header_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700111
112 const std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700113 getPayload() const { return payload_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700114
115 std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700116 getPayload() { return payload_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700117
118 const DerType&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700119 getType() { return type_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700120
Jeff Thompson415da1e2013-10-17 16:52:59 -0700121 virtual Blob
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700122 getRaw()
123 {
124 ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>());
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700125 blob->insert(blob->end(), header_.begin(), header_.end());
126 blob->insert(blob->end(), payload_.begin(), payload_.end());
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700127
128 return blob;
129 }
130
131 virtual void accept(VoidNoArgumentsVisitor& visitor) = 0;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700132 virtual void accept(VoidVisitor& visitor, ndnboost::any param) = 0;
133 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) = 0;
134 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) = 0;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700135
136protected:
137 void
Jeff Thompson2d47db72013-10-17 15:19:52 -0700138 decode(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700139
140 void
141 encodeHeader(int size);
142
143 int
Jeff Thompson2d47db72013-10-17 15:19:52 -0700144 decodeHeader(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700145
146protected:
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700147 DerType type_;
148 std::vector<uint8_t> header_;
149 std::vector<uint8_t> payload_;
150 DerComplex * parent_;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700151};
152
153
154typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList;
155
156class DerComplex : public DerNode
157{
158public:
159 DerComplex();
160
161 DerComplex(DerType type);
162
Jeff Thompson2d47db72013-10-17 15:19:52 -0700163 DerComplex(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700164
165 virtual
166 ~DerComplex();
167
168 virtual int
169 getSize();
170
171 void
172 addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true);
173
174 virtual void
Jeff Thompson2d47db72013-10-17 15:19:52 -0700175 encode(OutputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700176
177 const DerNodePtrList&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700178 getChildren() const { return nodeList_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700179
180 DerNodePtrList&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700181 getChildren() { return nodeList_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700182
Jeff Thompson415da1e2013-10-17 16:52:59 -0700183 virtual Blob
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700184 getRaw();
185
186private:
187 void
188 updateSize();
189
190 void
191 setChildChanged();
192
193private:
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700194 bool childChanged_;
195 int size_;
196 DerNodePtrList nodeList_;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700197};
198
199
200
201class DerByteString : public DerNode
202{
203public:
204 DerByteString(const std::string& str, DerType type);
205
206 DerByteString(const std::vector<uint8_t>& blob, DerType type);
207
Jeff Thompson2d47db72013-10-17 15:19:52 -0700208 DerByteString(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700209
210 virtual
211 ~DerByteString();
212};
213
214
215//0x01
216class DerBool : public DerNode
217{
218public:
219 DerBool(bool value);
220
Jeff Thompson2d47db72013-10-17 15:19:52 -0700221 DerBool(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700222
223 virtual
224 ~DerBool();
225
Jeff Thompsona92861a2013-10-16 14:06:23 -0700226 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
227 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
228 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
229 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700230};
231
232//0x02
233class DerInteger : public DerNode
234{
235public:
236 DerInteger(const std::vector<uint8_t>& blob);
237
Jeff Thompson2d47db72013-10-17 15:19:52 -0700238 DerInteger(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700239
240 virtual
241 ~DerInteger();
242
Jeff Thompsona92861a2013-10-16 14:06:23 -0700243 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
244 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
245 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
246 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700247};
248
249//0x03
250class DerBitString : public DerNode
251{
252public:
253 DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen);
254
Jeff Thompson2d47db72013-10-17 15:19:52 -0700255 DerBitString(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700256
257 virtual
258 ~DerBitString();
259
Jeff Thompsona92861a2013-10-16 14:06:23 -0700260 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
261 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
262 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
263 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700264};
265
266//0x04
267class DerOctetString : public DerByteString
268{
269public:
270 DerOctetString(const std::string& str);
271
272 DerOctetString(const std::vector<uint8_t>& blob);
273
Jeff Thompson2d47db72013-10-17 15:19:52 -0700274 DerOctetString(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700275
276 virtual
277 ~DerOctetString();
278
Jeff Thompsona92861a2013-10-16 14:06:23 -0700279 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
280 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
281 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
282 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700283};
284
285
286//0x05
287class DerNull : public DerNode
288{
289public:
290 DerNull();
291
Jeff Thompson2d47db72013-10-17 15:19:52 -0700292 DerNull(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700293
294 virtual
295 ~DerNull();
296
Jeff Thompsona92861a2013-10-16 14:06:23 -0700297 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
298 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
299 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
300 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700301};
302
303
304//0x06
305class DerOid : public DerNode
306{
307public:
308 DerOid(const OID& oid);
309
310 DerOid(const std::string& oidStr);
311
312 DerOid(const std::vector<int>& value);
313
Jeff Thompson2d47db72013-10-17 15:19:52 -0700314 DerOid(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700315
316 virtual
317 ~DerOid();
318
Jeff Thompsona92861a2013-10-16 14:06:23 -0700319 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
320 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
321 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
322 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700323
324 int
325 decode128(int& offset);
326
327private:
328 void
329 prepareEncoding(const std::vector<int>& value);
330
331 void
332 encode128(int value, std::ostringstream& os);
333};
334
335
336//0x10
337class DerSequence : public DerComplex
338{
339public:
340 DerSequence();
341
Jeff Thompson2d47db72013-10-17 15:19:52 -0700342 DerSequence(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700343
344 virtual
345 ~DerSequence();
346
Jeff Thompsona92861a2013-10-16 14:06:23 -0700347 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
348 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
349 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
350 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700351};
352
353//0x13
354class DerPrintableString : public DerByteString
355{
356public:
357 DerPrintableString(const std::string& str);
358
359 DerPrintableString(const std::vector<uint8_t>& blob);
360
Jeff Thompson2d47db72013-10-17 15:19:52 -0700361 DerPrintableString(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700362
363 virtual
364 ~DerPrintableString();
365
Jeff Thompsona92861a2013-10-16 14:06:23 -0700366 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
367 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
368 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
369 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700370};
371
372//0x1b
373class DerGtime : public DerNode
374{
375public:
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700376 DerGtime(const MillisecondsSince1970& time);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700377
Jeff Thompson2d47db72013-10-17 15:19:52 -0700378 DerGtime(InputIterator& start);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700379
380 virtual
381 ~DerGtime();
382
Jeff Thompsona92861a2013-10-16 14:06:23 -0700383 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
384 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
385 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
386 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700387
Jeff Thompsona92861a2013-10-16 14:06:23 -0700388 /**
389 * Convert to the ISO string representation of the time.
390 * @param time Milliseconds since 1/1/1970.
391 * @return The ISO string.
392 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700393 static std::string toIsoString(const MillisecondsSince1970& time);
Jeff Thompsona92861a2013-10-16 14:06:23 -0700394
395 /**
396 * Convert from the ISO string representation to the internal time format.
397 * @param isoString The ISO time formatted string.
398 * @return The time in milliseconds since 1/1/1970.
399 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700400 static MillisecondsSince1970 fromIsoString(const std::string& isoString);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700401};
402
403} // der
404
405}
406
407#endif