Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 1 | /* -*- 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 | |
| 26 | namespace ndn { |
| 27 | |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 28 | namespace der { |
| 29 | |
| 30 | enum 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 | |
| 62 | class DerComplex; |
| 63 | |
| 64 | class DerNode |
| 65 | { |
| 66 | public: |
| 67 | DerNode(); |
| 68 | |
| 69 | DerNode(DerType type); |
| 70 | |
| 71 | DerNode(std::istream& start); |
| 72 | |
| 73 | virtual |
| 74 | ~DerNode(); |
| 75 | |
| 76 | virtual int |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 77 | getSize() { return header_.size() + payload_.size(); } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 78 | |
| 79 | virtual void |
| 80 | encode(std::ostream& start); |
| 81 | |
| 82 | void |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 83 | setParent(DerComplex * parent) { parent_ = parent; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 84 | |
| 85 | static ptr_lib::shared_ptr<DerNode> |
| 86 | parse(std::istream& start); |
| 87 | |
| 88 | const std::vector<uint8_t>& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 89 | getHeader() const { return header_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 90 | |
| 91 | std::vector<uint8_t>& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 92 | getHeader() { return header_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 93 | |
| 94 | const std::vector<uint8_t>& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 95 | getPayload() const { return payload_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 96 | |
| 97 | std::vector<uint8_t>& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 98 | getPayload() { return payload_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 99 | |
| 100 | const DerType& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 101 | getType() { return type_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 102 | |
| 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 Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 107 | blob->insert(blob->end(), header_.begin(), header_.end()); |
| 108 | blob->insert(blob->end(), payload_.begin(), payload_.end()); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 109 | |
| 110 | return blob; |
| 111 | } |
| 112 | |
| 113 | virtual void accept(VoidNoArgumentsVisitor& visitor) = 0; |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame] | 114 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 117 | |
| 118 | protected: |
| 119 | void |
| 120 | decode(std::istream& start); |
| 121 | |
| 122 | void |
| 123 | encodeHeader(int size); |
| 124 | |
| 125 | int |
| 126 | decodeHeader(std::istream& start); |
| 127 | |
| 128 | protected: |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 129 | DerType type_; |
| 130 | std::vector<uint8_t> header_; |
| 131 | std::vector<uint8_t> payload_; |
| 132 | DerComplex * parent_; |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | |
| 136 | typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList; |
| 137 | |
| 138 | class DerComplex : public DerNode |
| 139 | { |
| 140 | public: |
| 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 Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 160 | getChildren() const { return nodeList_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 161 | |
| 162 | DerNodePtrList& |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 163 | getChildren() { return nodeList_; } |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 164 | |
| 165 | virtual ptr_lib::shared_ptr<std::vector<uint8_t> > |
| 166 | getRaw(); |
| 167 | |
| 168 | private: |
| 169 | void |
| 170 | updateSize(); |
| 171 | |
| 172 | void |
| 173 | setChildChanged(); |
| 174 | |
| 175 | private: |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 176 | bool childChanged_; |
| 177 | int size_; |
| 178 | DerNodePtrList nodeList_; |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 179 | }; |
| 180 | |
| 181 | |
| 182 | |
| 183 | class DerByteString : public DerNode |
| 184 | { |
| 185 | public: |
| 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 |
| 198 | class DerBool : public DerNode |
| 199 | { |
| 200 | public: |
| 201 | DerBool(bool value); |
| 202 | |
| 203 | DerBool(std::istream& start); |
| 204 | |
| 205 | virtual |
| 206 | ~DerBool(); |
| 207 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 208 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 212 | }; |
| 213 | |
| 214 | //0x02 |
| 215 | class DerInteger : public DerNode |
| 216 | { |
| 217 | public: |
| 218 | DerInteger(const std::vector<uint8_t>& blob); |
| 219 | |
| 220 | DerInteger(std::istream& start); |
| 221 | |
| 222 | virtual |
| 223 | ~DerInteger(); |
| 224 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 225 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 229 | }; |
| 230 | |
| 231 | //0x03 |
| 232 | class DerBitString : public DerNode |
| 233 | { |
| 234 | public: |
| 235 | DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen); |
| 236 | |
| 237 | DerBitString(std::istream& start); |
| 238 | |
| 239 | virtual |
| 240 | ~DerBitString(); |
| 241 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 242 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | //0x04 |
| 249 | class DerOctetString : public DerByteString |
| 250 | { |
| 251 | public: |
| 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 Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 261 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | |
| 268 | //0x05 |
| 269 | class DerNull : public DerNode |
| 270 | { |
| 271 | public: |
| 272 | DerNull(); |
| 273 | |
| 274 | DerNull(std::istream& start); |
| 275 | |
| 276 | virtual |
| 277 | ~DerNull(); |
| 278 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 279 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 283 | }; |
| 284 | |
| 285 | |
| 286 | //0x06 |
| 287 | class DerOid : public DerNode |
| 288 | { |
| 289 | public: |
| 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 Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 301 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 305 | |
| 306 | int |
| 307 | decode128(int& offset); |
| 308 | |
| 309 | private: |
| 310 | void |
| 311 | prepareEncoding(const std::vector<int>& value); |
| 312 | |
| 313 | void |
| 314 | encode128(int value, std::ostringstream& os); |
| 315 | }; |
| 316 | |
| 317 | |
| 318 | //0x10 |
| 319 | class DerSequence : public DerComplex |
| 320 | { |
| 321 | public: |
| 322 | DerSequence(); |
| 323 | |
| 324 | DerSequence(std::istream& start); |
| 325 | |
| 326 | virtual |
| 327 | ~DerSequence(); |
| 328 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 329 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 333 | }; |
| 334 | |
| 335 | //0x13 |
| 336 | class DerPrintableString : public DerByteString |
| 337 | { |
| 338 | public: |
| 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 Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 348 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 352 | }; |
| 353 | |
| 354 | //0x1b |
| 355 | class DerGtime : public DerNode |
| 356 | { |
| 357 | public: |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame^] | 358 | DerGtime(const MillisecondsSince1970& time); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 359 | |
| 360 | DerGtime(std::istream& start); |
| 361 | |
| 362 | virtual |
| 363 | ~DerGtime(); |
| 364 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 365 | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 369 | |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 370 | /** |
| 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 Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame^] | 375 | static std::string toIsoString(const MillisecondsSince1970& time); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 376 | |
| 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 Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame^] | 382 | static MillisecondsSince1970 fromIsoString(const std::string& isoString); |
Jeff Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 383 | }; |
| 384 | |
| 385 | } // der |
| 386 | |
| 387 | } |
| 388 | |
| 389 | #endif |