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 | |
| 28 | // TODO: Implement Time values. |
| 29 | class Time; |
| 30 | |
| 31 | namespace der { |
| 32 | |
| 33 | enum DerType { |
| 34 | DER_EOC = 0, |
| 35 | DER_BOOLEAN = 1, |
| 36 | DER_INTEGER = 2, |
| 37 | DER_BIT_STRING = 3, |
| 38 | DER_OCTET_STRING = 4, |
| 39 | DER_NULL = 5, |
| 40 | DER_OBJECT_IDENTIFIER = 6, |
| 41 | DER_OBJECT_DESCRIPTOR = 7, |
| 42 | DER_EXTERNAL = 40, |
| 43 | DER_REAL = 9, |
| 44 | DER_ENUMERATED = 10, |
| 45 | DER_EMBEDDED_PDV = 43, |
| 46 | DER_UTF8_STRING = 12, |
| 47 | DER_RELATIVE_OID = 13, |
| 48 | DER_SEQUENCE = 48, |
| 49 | DER_SET = 49, |
| 50 | DER_NUMERIC_STRING = 18, |
| 51 | DER_PRINTABLE_STRING = 19, |
| 52 | DER_T61_STRING = 20, |
| 53 | DER_VIDEOTEX_STRING = 21, |
| 54 | DER_IA5_STRING = 22, |
| 55 | DER_UTC_TIME = 23, |
| 56 | DER_GENERALIZED_TIME = 24, |
| 57 | DER_GRAPHIC_STRING = 25, |
| 58 | DER_VISIBLE_STRING = 26, |
| 59 | DER_GENERAL_STRING = 27, |
| 60 | DER_UNIVERSAL_STRING = 28, |
| 61 | DER_CHARACTER_STRING = 29, |
| 62 | DER_BMP_STRING = 30, |
| 63 | }; |
| 64 | |
| 65 | class DerComplex; |
| 66 | |
| 67 | class DerNode |
| 68 | { |
| 69 | public: |
| 70 | DerNode(); |
| 71 | |
| 72 | DerNode(DerType type); |
| 73 | |
| 74 | DerNode(std::istream& start); |
| 75 | |
| 76 | virtual |
| 77 | ~DerNode(); |
| 78 | |
| 79 | virtual int |
| 80 | getSize() { return m_header.size() + m_payload.size(); } |
| 81 | |
| 82 | virtual void |
| 83 | encode(std::ostream& start); |
| 84 | |
| 85 | void |
| 86 | setParent(DerComplex * parent) { m_parent = parent; } |
| 87 | |
| 88 | static ptr_lib::shared_ptr<DerNode> |
| 89 | parse(std::istream& start); |
| 90 | |
| 91 | const std::vector<uint8_t>& |
| 92 | getHeader() const { return m_header; } |
| 93 | |
| 94 | std::vector<uint8_t>& |
| 95 | getHeader() { return m_header; } |
| 96 | |
| 97 | const std::vector<uint8_t>& |
| 98 | getPayload() const { return m_payload; } |
| 99 | |
| 100 | std::vector<uint8_t>& |
| 101 | getPayload() { return m_payload; } |
| 102 | |
| 103 | const DerType& |
| 104 | getType() { return m_type; } |
| 105 | |
| 106 | virtual ptr_lib::shared_ptr<std::vector<uint8_t> > |
| 107 | getRaw() |
| 108 | { |
| 109 | ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>()); |
| 110 | blob->insert(blob->end(), m_header.begin(), m_header.end()); |
| 111 | blob->insert(blob->end(), m_payload.begin(), m_payload.end()); |
| 112 | |
| 113 | return blob; |
| 114 | } |
| 115 | |
| 116 | virtual void accept(VoidNoArgumentsVisitor& visitor) = 0; |
| 117 | virtual void accept(VoidVisitor& visitor, Any param) = 0; |
| 118 | virtual Any accept(NoArgumentsVisitor& visitor) = 0; |
| 119 | virtual Any accept(Visitor& visitor, Any param) = 0; |
| 120 | |
| 121 | protected: |
| 122 | void |
| 123 | decode(std::istream& start); |
| 124 | |
| 125 | void |
| 126 | encodeHeader(int size); |
| 127 | |
| 128 | int |
| 129 | decodeHeader(std::istream& start); |
| 130 | |
| 131 | protected: |
| 132 | DerType m_type; |
| 133 | std::vector<uint8_t> m_header; |
| 134 | std::vector<uint8_t> m_payload; |
| 135 | DerComplex * m_parent; |
| 136 | }; |
| 137 | |
| 138 | |
| 139 | typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList; |
| 140 | |
| 141 | class DerComplex : public DerNode |
| 142 | { |
| 143 | public: |
| 144 | DerComplex(); |
| 145 | |
| 146 | DerComplex(DerType type); |
| 147 | |
| 148 | DerComplex(std::istream& start); |
| 149 | |
| 150 | virtual |
| 151 | ~DerComplex(); |
| 152 | |
| 153 | virtual int |
| 154 | getSize(); |
| 155 | |
| 156 | void |
| 157 | addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true); |
| 158 | |
| 159 | virtual void |
| 160 | encode(std::ostream& start); |
| 161 | |
| 162 | const DerNodePtrList& |
| 163 | getChildren() const { return m_nodeList; } |
| 164 | |
| 165 | DerNodePtrList& |
| 166 | getChildren() { return m_nodeList; } |
| 167 | |
| 168 | virtual ptr_lib::shared_ptr<std::vector<uint8_t> > |
| 169 | getRaw(); |
| 170 | |
| 171 | private: |
| 172 | void |
| 173 | updateSize(); |
| 174 | |
| 175 | void |
| 176 | setChildChanged(); |
| 177 | |
| 178 | private: |
| 179 | bool m_childChanged; |
| 180 | int m_size; |
| 181 | DerNodePtrList m_nodeList; |
| 182 | }; |
| 183 | |
| 184 | |
| 185 | |
| 186 | class DerByteString : public DerNode |
| 187 | { |
| 188 | public: |
| 189 | DerByteString(const std::string& str, DerType type); |
| 190 | |
| 191 | DerByteString(const std::vector<uint8_t>& blob, DerType type); |
| 192 | |
| 193 | DerByteString(std::istream& start); |
| 194 | |
| 195 | virtual |
| 196 | ~DerByteString(); |
| 197 | }; |
| 198 | |
| 199 | |
| 200 | //0x01 |
| 201 | class DerBool : public DerNode |
| 202 | { |
| 203 | public: |
| 204 | DerBool(bool value); |
| 205 | |
| 206 | DerBool(std::istream& start); |
| 207 | |
| 208 | virtual |
| 209 | ~DerBool(); |
| 210 | |
| 211 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 212 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 213 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 214 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 215 | |
| 216 | }; |
| 217 | |
| 218 | //0x02 |
| 219 | class DerInteger : public DerNode |
| 220 | { |
| 221 | public: |
| 222 | DerInteger(const std::vector<uint8_t>& blob); |
| 223 | |
| 224 | DerInteger(std::istream& start); |
| 225 | |
| 226 | virtual |
| 227 | ~DerInteger(); |
| 228 | |
| 229 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 230 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 231 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 232 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 233 | |
| 234 | }; |
| 235 | |
| 236 | //0x03 |
| 237 | class DerBitString : public DerNode |
| 238 | { |
| 239 | public: |
| 240 | DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen); |
| 241 | |
| 242 | DerBitString(std::istream& start); |
| 243 | |
| 244 | virtual |
| 245 | ~DerBitString(); |
| 246 | |
| 247 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 248 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 249 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 250 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 251 | |
| 252 | }; |
| 253 | |
| 254 | //0x04 |
| 255 | class DerOctetString : public DerByteString |
| 256 | { |
| 257 | public: |
| 258 | DerOctetString(const std::string& str); |
| 259 | |
| 260 | DerOctetString(const std::vector<uint8_t>& blob); |
| 261 | |
| 262 | DerOctetString(std::istream& start); |
| 263 | |
| 264 | virtual |
| 265 | ~DerOctetString(); |
| 266 | |
| 267 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 268 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 269 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 270 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 271 | |
| 272 | }; |
| 273 | |
| 274 | |
| 275 | //0x05 |
| 276 | class DerNull : public DerNode |
| 277 | { |
| 278 | public: |
| 279 | DerNull(); |
| 280 | |
| 281 | DerNull(std::istream& start); |
| 282 | |
| 283 | virtual |
| 284 | ~DerNull(); |
| 285 | |
| 286 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 287 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 288 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 289 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 290 | |
| 291 | }; |
| 292 | |
| 293 | |
| 294 | //0x06 |
| 295 | class DerOid : public DerNode |
| 296 | { |
| 297 | public: |
| 298 | DerOid(const OID& oid); |
| 299 | |
| 300 | DerOid(const std::string& oidStr); |
| 301 | |
| 302 | DerOid(const std::vector<int>& value); |
| 303 | |
| 304 | DerOid(std::istream& start); |
| 305 | |
| 306 | virtual |
| 307 | ~DerOid(); |
| 308 | |
| 309 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 310 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 311 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 312 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 313 | |
| 314 | int |
| 315 | decode128(int& offset); |
| 316 | |
| 317 | private: |
| 318 | void |
| 319 | prepareEncoding(const std::vector<int>& value); |
| 320 | |
| 321 | void |
| 322 | encode128(int value, std::ostringstream& os); |
| 323 | }; |
| 324 | |
| 325 | |
| 326 | //0x10 |
| 327 | class DerSequence : public DerComplex |
| 328 | { |
| 329 | public: |
| 330 | DerSequence(); |
| 331 | |
| 332 | DerSequence(std::istream& start); |
| 333 | |
| 334 | virtual |
| 335 | ~DerSequence(); |
| 336 | |
| 337 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 338 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 339 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 340 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 341 | |
| 342 | }; |
| 343 | |
| 344 | //0x13 |
| 345 | class DerPrintableString : public DerByteString |
| 346 | { |
| 347 | public: |
| 348 | DerPrintableString(const std::string& str); |
| 349 | |
| 350 | DerPrintableString(const std::vector<uint8_t>& blob); |
| 351 | |
| 352 | DerPrintableString(std::istream& start); |
| 353 | |
| 354 | virtual |
| 355 | ~DerPrintableString(); |
| 356 | |
| 357 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 358 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 359 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 360 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 361 | }; |
| 362 | |
| 363 | //0x1b |
| 364 | class DerGtime : public DerNode |
| 365 | { |
| 366 | public: |
| 367 | DerGtime(const Time& time); |
| 368 | |
| 369 | DerGtime(std::istream& start); |
| 370 | |
| 371 | virtual |
| 372 | ~DerGtime(); |
| 373 | |
| 374 | virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); } |
| 375 | virtual void accept(VoidVisitor& visitor, Any param) { visitor.visit(*this, param); } |
| 376 | virtual Any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); } |
| 377 | virtual Any accept(Visitor& visitor, Any param) { return visitor.visit(*this, param); } |
| 378 | |
| 379 | }; |
| 380 | |
| 381 | } // der |
| 382 | |
| 383 | } |
| 384 | |
| 385 | #endif |