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 |
| 77 | getSize() { return m_header.size() + m_payload.size(); } |
| 78 | |
| 79 | virtual void |
| 80 | encode(std::ostream& start); |
| 81 | |
| 82 | void |
| 83 | setParent(DerComplex * parent) { m_parent = parent; } |
| 84 | |
| 85 | static ptr_lib::shared_ptr<DerNode> |
| 86 | parse(std::istream& start); |
| 87 | |
| 88 | const std::vector<uint8_t>& |
| 89 | getHeader() const { return m_header; } |
| 90 | |
| 91 | std::vector<uint8_t>& |
| 92 | getHeader() { return m_header; } |
| 93 | |
| 94 | const std::vector<uint8_t>& |
| 95 | getPayload() const { return m_payload; } |
| 96 | |
| 97 | std::vector<uint8_t>& |
| 98 | getPayload() { return m_payload; } |
| 99 | |
| 100 | const DerType& |
| 101 | getType() { return m_type; } |
| 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>()); |
| 107 | blob->insert(blob->end(), m_header.begin(), m_header.end()); |
| 108 | blob->insert(blob->end(), m_payload.begin(), m_payload.end()); |
| 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: |
| 129 | DerType m_type; |
| 130 | std::vector<uint8_t> m_header; |
| 131 | std::vector<uint8_t> m_payload; |
| 132 | DerComplex * m_parent; |
| 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& |
| 160 | getChildren() const { return m_nodeList; } |
| 161 | |
| 162 | DerNodePtrList& |
| 163 | getChildren() { return m_nodeList; } |
| 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: |
| 176 | bool m_childChanged; |
| 177 | int m_size; |
| 178 | DerNodePtrList m_nodeList; |
| 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 | |
| 208 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 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 | |
| 215 | //0x02 |
| 216 | class DerInteger : public DerNode |
| 217 | { |
| 218 | public: |
| 219 | DerInteger(const std::vector<uint8_t>& blob); |
| 220 | |
| 221 | DerInteger(std::istream& start); |
| 222 | |
| 223 | virtual |
| 224 | ~DerInteger(); |
| 225 | |
| 226 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 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 Thompson | 958bf9b | 2013-10-12 17:20:51 -0700 | [diff] [blame] | 230 | |
| 231 | }; |
| 232 | |
| 233 | //0x03 |
| 234 | class DerBitString : public DerNode |
| 235 | { |
| 236 | public: |
| 237 | DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen); |
| 238 | |
| 239 | DerBitString(std::istream& start); |
| 240 | |
| 241 | virtual |
| 242 | ~DerBitString(); |
| 243 | |
| 244 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 245 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 246 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 247 | 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] | 248 | |
| 249 | }; |
| 250 | |
| 251 | //0x04 |
| 252 | class DerOctetString : public DerByteString |
| 253 | { |
| 254 | public: |
| 255 | DerOctetString(const std::string& str); |
| 256 | |
| 257 | DerOctetString(const std::vector<uint8_t>& blob); |
| 258 | |
| 259 | DerOctetString(std::istream& start); |
| 260 | |
| 261 | virtual |
| 262 | ~DerOctetString(); |
| 263 | |
| 264 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 265 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 266 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 267 | 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] | 268 | |
| 269 | }; |
| 270 | |
| 271 | |
| 272 | //0x05 |
| 273 | class DerNull : public DerNode |
| 274 | { |
| 275 | public: |
| 276 | DerNull(); |
| 277 | |
| 278 | DerNull(std::istream& start); |
| 279 | |
| 280 | virtual |
| 281 | ~DerNull(); |
| 282 | |
| 283 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 284 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 285 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 286 | 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] | 287 | |
| 288 | }; |
| 289 | |
| 290 | |
| 291 | //0x06 |
| 292 | class DerOid : public DerNode |
| 293 | { |
| 294 | public: |
| 295 | DerOid(const OID& oid); |
| 296 | |
| 297 | DerOid(const std::string& oidStr); |
| 298 | |
| 299 | DerOid(const std::vector<int>& value); |
| 300 | |
| 301 | DerOid(std::istream& start); |
| 302 | |
| 303 | virtual |
| 304 | ~DerOid(); |
| 305 | |
| 306 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 307 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 308 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 309 | 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] | 310 | |
| 311 | int |
| 312 | decode128(int& offset); |
| 313 | |
| 314 | private: |
| 315 | void |
| 316 | prepareEncoding(const std::vector<int>& value); |
| 317 | |
| 318 | void |
| 319 | encode128(int value, std::ostringstream& os); |
| 320 | }; |
| 321 | |
| 322 | |
| 323 | //0x10 |
| 324 | class DerSequence : public DerComplex |
| 325 | { |
| 326 | public: |
| 327 | DerSequence(); |
| 328 | |
| 329 | DerSequence(std::istream& start); |
| 330 | |
| 331 | virtual |
| 332 | ~DerSequence(); |
| 333 | |
| 334 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 335 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 336 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 337 | 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] | 338 | |
| 339 | }; |
| 340 | |
| 341 | //0x13 |
| 342 | class DerPrintableString : public DerByteString |
| 343 | { |
| 344 | public: |
| 345 | DerPrintableString(const std::string& str); |
| 346 | |
| 347 | DerPrintableString(const std::vector<uint8_t>& blob); |
| 348 | |
| 349 | DerPrintableString(std::istream& start); |
| 350 | |
| 351 | virtual |
| 352 | ~DerPrintableString(); |
| 353 | |
| 354 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 355 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 356 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 357 | 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] | 358 | }; |
| 359 | |
| 360 | //0x1b |
| 361 | class DerGtime : public DerNode |
| 362 | { |
| 363 | public: |
| 364 | DerGtime(const Time& time); |
| 365 | |
| 366 | DerGtime(std::istream& start); |
| 367 | |
| 368 | virtual |
| 369 | ~DerGtime(); |
| 370 | |
| 371 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
Jeff Thompson | d0a7d6d | 2013-10-15 12:34:26 -0700 | [diff] [blame^] | 372 | virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); } |
| 373 | virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 374 | 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] | 375 | |
| 376 | }; |
| 377 | |
| 378 | } // der |
| 379 | |
| 380 | } |
| 381 | |
| 382 | #endif |