blob: ff92c789f0726c81b688103b0e2d90e31c8f05fe [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>
20
21#include "visitor/visitor.hpp"
22#include "visitor/void-visitor.hpp"
23#include "visitor/no-arguments-visitor.hpp"
24#include "visitor/void-no-arguments-visitor.hpp"
25
26namespace ndn {
27
Jeff Thompson958bf9b2013-10-12 17:20:51 -070028namespace der {
29
30enum DerType {
31 DER_EOC = 0,
32 DER_BOOLEAN = 1,
33 DER_INTEGER = 2,
34 DER_BIT_STRING = 3,
35 DER_OCTET_STRING = 4,
36 DER_NULL = 5,
37 DER_OBJECT_IDENTIFIER = 6,
38 DER_OBJECT_DESCRIPTOR = 7,
39 DER_EXTERNAL = 40,
40 DER_REAL = 9,
41 DER_ENUMERATED = 10,
42 DER_EMBEDDED_PDV = 43,
43 DER_UTF8_STRING = 12,
44 DER_RELATIVE_OID = 13,
45 DER_SEQUENCE = 48,
46 DER_SET = 49,
47 DER_NUMERIC_STRING = 18,
48 DER_PRINTABLE_STRING = 19,
49 DER_T61_STRING = 20,
50 DER_VIDEOTEX_STRING = 21,
51 DER_IA5_STRING = 22,
52 DER_UTC_TIME = 23,
53 DER_GENERALIZED_TIME = 24,
54 DER_GRAPHIC_STRING = 25,
55 DER_VISIBLE_STRING = 26,
56 DER_GENERAL_STRING = 27,
57 DER_UNIVERSAL_STRING = 28,
58 DER_CHARACTER_STRING = 29,
59 DER_BMP_STRING = 30,
60};
61
62class DerComplex;
63
64class DerNode
65{
66public:
67 DerNode();
68
69 DerNode(DerType type);
70
71 DerNode(std::istream& start);
72
73 virtual
74 ~DerNode();
75
76 virtual int
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070077 getSize() { return header_.size() + payload_.size(); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070078
79 virtual void
80 encode(std::ostream& start);
81
82 void
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070083 setParent(DerComplex * parent) { parent_ = parent; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070084
85 static ptr_lib::shared_ptr<DerNode>
86 parse(std::istream& start);
87
88 const std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070089 getHeader() const { return header_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070090
91 std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070092 getHeader() { return header_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070093
94 const std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070095 getPayload() const { return payload_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070096
97 std::vector<uint8_t>&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -070098 getPayload() { return payload_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -070099
100 const DerType&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700101 getType() { return type_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700102
103 virtual ptr_lib::shared_ptr<std::vector<uint8_t> >
104 getRaw()
105 {
106 ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>());
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700107 blob->insert(blob->end(), header_.begin(), header_.end());
108 blob->insert(blob->end(), payload_.begin(), payload_.end());
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700109
110 return blob;
111 }
112
113 virtual void accept(VoidNoArgumentsVisitor& visitor) = 0;
Jeff Thompsond0a7d6d2013-10-15 12:34:26 -0700114 virtual void accept(VoidVisitor& visitor, ndnboost::any param) = 0;
115 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) = 0;
116 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) = 0;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700117
118protected:
119 void
120 decode(std::istream& start);
121
122 void
123 encodeHeader(int size);
124
125 int
126 decodeHeader(std::istream& start);
127
128protected:
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700129 DerType type_;
130 std::vector<uint8_t> header_;
131 std::vector<uint8_t> payload_;
132 DerComplex * parent_;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700133};
134
135
136typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList;
137
138class DerComplex : public DerNode
139{
140public:
141 DerComplex();
142
143 DerComplex(DerType type);
144
145 DerComplex(std::istream& start);
146
147 virtual
148 ~DerComplex();
149
150 virtual int
151 getSize();
152
153 void
154 addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true);
155
156 virtual void
157 encode(std::ostream& start);
158
159 const DerNodePtrList&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700160 getChildren() const { return nodeList_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700161
162 DerNodePtrList&
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700163 getChildren() { return nodeList_; }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700164
165 virtual ptr_lib::shared_ptr<std::vector<uint8_t> >
166 getRaw();
167
168private:
169 void
170 updateSize();
171
172 void
173 setChildChanged();
174
175private:
Jeff Thompson7ff36fe2013-10-16 15:30:13 -0700176 bool childChanged_;
177 int size_;
178 DerNodePtrList nodeList_;
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700179};
180
181
182
183class DerByteString : public DerNode
184{
185public:
186 DerByteString(const std::string& str, DerType type);
187
188 DerByteString(const std::vector<uint8_t>& blob, DerType type);
189
190 DerByteString(std::istream& start);
191
192 virtual
193 ~DerByteString();
194};
195
196
197//0x01
198class DerBool : public DerNode
199{
200public:
201 DerBool(bool value);
202
203 DerBool(std::istream& start);
204
205 virtual
206 ~DerBool();
207
Jeff Thompsona92861a2013-10-16 14:06:23 -0700208 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
209 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
210 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
211 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700212};
213
214//0x02
215class DerInteger : public DerNode
216{
217public:
218 DerInteger(const std::vector<uint8_t>& blob);
219
220 DerInteger(std::istream& start);
221
222 virtual
223 ~DerInteger();
224
Jeff Thompsona92861a2013-10-16 14:06:23 -0700225 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
226 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
227 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
228 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700229};
230
231//0x03
232class DerBitString : public DerNode
233{
234public:
235 DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen);
236
237 DerBitString(std::istream& start);
238
239 virtual
240 ~DerBitString();
241
Jeff Thompsona92861a2013-10-16 14:06:23 -0700242 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
243 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
244 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
245 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700246};
247
248//0x04
249class DerOctetString : public DerByteString
250{
251public:
252 DerOctetString(const std::string& str);
253
254 DerOctetString(const std::vector<uint8_t>& blob);
255
256 DerOctetString(std::istream& start);
257
258 virtual
259 ~DerOctetString();
260
Jeff Thompsona92861a2013-10-16 14:06:23 -0700261 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
262 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
263 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
264 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700265};
266
267
268//0x05
269class DerNull : public DerNode
270{
271public:
272 DerNull();
273
274 DerNull(std::istream& start);
275
276 virtual
277 ~DerNull();
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//0x06
287class DerOid : public DerNode
288{
289public:
290 DerOid(const OID& oid);
291
292 DerOid(const std::string& oidStr);
293
294 DerOid(const std::vector<int>& value);
295
296 DerOid(std::istream& start);
297
298 virtual
299 ~DerOid();
300
Jeff Thompsona92861a2013-10-16 14:06:23 -0700301 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
302 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
303 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
304 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700305
306 int
307 decode128(int& offset);
308
309private:
310 void
311 prepareEncoding(const std::vector<int>& value);
312
313 void
314 encode128(int value, std::ostringstream& os);
315};
316
317
318//0x10
319class DerSequence : public DerComplex
320{
321public:
322 DerSequence();
323
324 DerSequence(std::istream& start);
325
326 virtual
327 ~DerSequence();
328
Jeff Thompsona92861a2013-10-16 14:06:23 -0700329 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
330 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
331 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
332 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700333};
334
335//0x13
336class DerPrintableString : public DerByteString
337{
338public:
339 DerPrintableString(const std::string& str);
340
341 DerPrintableString(const std::vector<uint8_t>& blob);
342
343 DerPrintableString(std::istream& start);
344
345 virtual
346 ~DerPrintableString();
347
Jeff Thompsona92861a2013-10-16 14:06:23 -0700348 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
349 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
350 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
351 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700352};
353
354//0x1b
355class DerGtime : public DerNode
356{
357public:
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700358 DerGtime(const MillisecondsSince1970& time);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700359
360 DerGtime(std::istream& start);
361
362 virtual
363 ~DerGtime();
364
Jeff Thompsona92861a2013-10-16 14:06:23 -0700365 virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
366 virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
367 virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
368 virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700369
Jeff Thompsona92861a2013-10-16 14:06:23 -0700370 /**
371 * Convert to the ISO string representation of the time.
372 * @param time Milliseconds since 1/1/1970.
373 * @return The ISO string.
374 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700375 static std::string toIsoString(const MillisecondsSince1970& time);
Jeff Thompsona92861a2013-10-16 14:06:23 -0700376
377 /**
378 * Convert from the ISO string representation to the internal time format.
379 * @param isoString The ISO time formatted string.
380 * @return The time in milliseconds since 1/1/1970.
381 */
Jeff Thompson9a8e82f2013-10-17 14:13:43 -0700382 static MillisecondsSince1970 fromIsoString(const std::string& isoString);
Jeff Thompson958bf9b2013-10-12 17:20:51 -0700383};
384
385} // der
386
387}
388
389#endif