Jeff Thompson | a92861a | 2013-10-16 14:06:23 -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 | |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 9 | #include <stdexcept> |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 10 | #include "der-exception.hpp" |
| 11 | #include "../../util/logging.hpp" |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 12 | #include "../../c/util/time.h" |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 13 | #include "der.hpp" |
| 14 | |
| 15 | INIT_LOGGER("ndn.der.DER"); |
| 16 | |
| 17 | using namespace std; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 18 | |
| 19 | namespace ndn { |
| 20 | |
| 21 | namespace der { |
| 22 | |
| 23 | /* |
| 24 | * DerNode |
| 25 | */ |
| 26 | DerNode::DerNode() |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 27 | :parent_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 28 | {} |
| 29 | |
| 30 | DerNode::DerNode(DerType type) |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 31 | :type_(type), |
| 32 | parent_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 33 | {} |
| 34 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 35 | DerNode::DerNode(InputIterator& start) |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 36 | :parent_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 37 | { |
| 38 | decode(start); |
| 39 | } |
| 40 | |
| 41 | DerNode::~DerNode() |
| 42 | {} |
| 43 | |
| 44 | void |
| 45 | DerNode::encodeHeader(int size) |
| 46 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 47 | header_.push_back((char)type_); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 48 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 49 | if (size >= 127) { |
| 50 | int val = size; |
| 51 | char buf[sizeof(val) + 1]; |
| 52 | char *p = &(buf[sizeof(buf)-1]); |
| 53 | int n = 0; |
| 54 | int mask = (1 << 8) - 1; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 55 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 56 | while (val != 0) { |
| 57 | p[0] = (char)(val & mask); |
| 58 | p--; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 59 | n++; |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 60 | val >>= 8; |
| 61 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 62 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 63 | p[0] = (char)((1<<7) | n); |
| 64 | n++; |
| 65 | |
| 66 | header_.insert(header_.end(), p, p+n); |
| 67 | } |
| 68 | else if (size >= 0) |
| 69 | header_.push_back((char)size); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 70 | else |
| 71 | throw NegativeLengthException("Negative length"); |
| 72 | } |
| 73 | |
| 74 | int |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 75 | DerNode::decodeHeader(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 76 | { |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 77 | uint8_t type = start.ReadU8(); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 78 | // char type = start.get(); |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 79 | header_.push_back(type); |
| 80 | type_ = static_cast<DerType>((int)type); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 81 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 82 | uint8_t sizeLen = start.ReadU8(); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 83 | // char sizeLen = start.get(); |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 84 | header_.push_back(sizeLen); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 85 | |
| 86 | bool longFormat = sizeLen & (1 << 7); |
| 87 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 88 | if (!longFormat) { |
| 89 | // _LOG_DEBUG("Short Format"); |
| 90 | // _LOG_DEBUG("sizeLen: " << (int)sizeLen); |
| 91 | return (int)sizeLen; |
| 92 | } |
| 93 | else { |
| 94 | // _LOG_DEBUG("Long Format"); |
| 95 | uint8_t byte; |
| 96 | // char byte; |
| 97 | int lenCount = sizeLen & ((1<<7) - 1); |
| 98 | // _LOG_DEBUG("sizeLen: " << (int)sizeLen); |
| 99 | // _LOG_DEBUG("mask: " << (int)((1<<7) - 1)); |
| 100 | // _LOG_DEBUG("lenCount: " << (int)lenCount); |
| 101 | int size = 0; |
| 102 | do { |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 103 | byte = start.ReadU8(); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 104 | header_.push_back(byte); |
| 105 | size = size * 256 + (int)byte; |
| 106 | // _LOG_DEBUG("byte: " << (int)byte); |
| 107 | // _LOG_DEBUG("size: " << size); |
| 108 | lenCount--; |
| 109 | } while (lenCount > 0); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 110 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 111 | return size; |
| 112 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | void |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 116 | DerNode::encode(OutputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 117 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 118 | start.write((const char*)&header_[0], header_.size()); |
| 119 | start.write((const char*)&payload_[0], payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | void |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 123 | DerNode::decode(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 124 | { |
| 125 | int payloadSize = decodeHeader(start); |
| 126 | // _LOG_DEBUG("payloadSize: " << payloadSize); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 127 | if (payloadSize > 0 ) { |
| 128 | char buf[payloadSize]; |
| 129 | start.read(buf, payloadSize); |
| 130 | payload_.insert(payload_.end(), buf, buf + payloadSize); |
| 131 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 134 | ptr_lib::shared_ptr<DerNode> |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 135 | DerNode::parse(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 136 | { |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 137 | int type = ((uint8_t)start.PeekU8()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 138 | |
| 139 | // _LOG_DEBUG("Type: " << hex << setw(2) << setfill('0') << type); |
| 140 | switch(type) { |
| 141 | case DER_BOOLEAN: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 142 | return ptr_lib::shared_ptr<DerBool>(new DerBool(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 143 | case DER_INTEGER: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 144 | return ptr_lib::shared_ptr<DerInteger>(new DerInteger(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 145 | case DER_BIT_STRING: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 146 | return ptr_lib::shared_ptr<DerBitString>(new DerBitString(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 147 | case DER_OCTET_STRING: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 148 | return ptr_lib::shared_ptr<DerOctetString>(new DerOctetString(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 149 | case DER_NULL: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 150 | return ptr_lib::shared_ptr<DerNull>(new DerNull(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 151 | case DER_OBJECT_IDENTIFIER: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 152 | return ptr_lib::shared_ptr<DerOid>(new DerOid(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 153 | case DER_SEQUENCE: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 154 | return ptr_lib::shared_ptr<DerSequence>(new DerSequence(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 155 | case DER_PRINTABLE_STRING: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 156 | return ptr_lib::shared_ptr<DerPrintableString>(new DerPrintableString(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 157 | case DER_GENERALIZED_TIME: |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 158 | return ptr_lib::shared_ptr<DerGtime>(new DerGtime(start)); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 159 | default: |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 160 | throw DerDecodingException("Unimplemented DER type"); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 161 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | |
| 165 | /* |
| 166 | * DerComplex |
| 167 | */ |
| 168 | DerComplex::DerComplex() |
| 169 | :DerNode(), |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 170 | childChanged_(false), |
| 171 | size_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 172 | {} |
| 173 | |
| 174 | DerComplex::DerComplex(DerType type) |
| 175 | :DerNode(type), |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 176 | childChanged_(false), |
| 177 | size_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 178 | {} |
| 179 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 180 | DerComplex::DerComplex(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 181 | :DerNode(), |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 182 | childChanged_(false), |
| 183 | size_(0) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 184 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 185 | size_ = DerNode::decodeHeader(start); |
| 186 | // _LOG_DEBUG("Size: " << size_); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 187 | |
| 188 | int accSize = 0; |
| 189 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 190 | while (accSize < size_) { |
| 191 | // _LOG_DEBUG("accSize: " << accSize); |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 192 | ptr_lib::shared_ptr<DerNode> nodePtr = DerNode::parse(start); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 193 | accSize += nodePtr->getSize(); |
| 194 | addChild(nodePtr, false); |
| 195 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 196 | } |
| 197 | |
| 198 | DerComplex::~DerComplex() |
| 199 | {} |
| 200 | |
| 201 | int |
| 202 | DerComplex::getSize() |
| 203 | { |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 204 | if (childChanged_) { |
| 205 | updateSize(); |
| 206 | childChanged_ = false; |
| 207 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 208 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 209 | header_.clear(); |
| 210 | DerNode::encodeHeader(size_); |
| 211 | return size_ + header_.size(); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 212 | } |
| 213 | |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 214 | Blob |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 215 | DerComplex::getRaw() |
| 216 | { |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 217 | ptr_lib::shared_ptr<vector<uint8_t> > blob(new vector<uint8_t>()); |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 218 | blob->insert(blob->end(), header_.begin(), header_.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 219 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 220 | DerNodePtrList::iterator it = nodeList_.begin(); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 221 | for (; it != nodeList_.end(); it++) { |
Jeff Thompson | 415da1e | 2013-10-17 16:52:59 -0700 | [diff] [blame] | 222 | Blob childBlob = (*it)->getRaw(); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 223 | blob->insert(blob->end(), childBlob->begin(), childBlob->end()); |
| 224 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 225 | return blob; |
| 226 | } |
| 227 | |
| 228 | void |
| 229 | DerComplex::updateSize() |
| 230 | { |
| 231 | int newSize = 0; |
| 232 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 233 | DerNodePtrList::iterator it = nodeList_.begin(); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 234 | for (; it != nodeList_.end(); it++) { |
| 235 | newSize += (*it)->getSize(); |
| 236 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 237 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 238 | size_ = newSize; |
| 239 | childChanged_ = false; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | void |
Jeff Thompson | ce11576 | 2013-12-18 14:59:56 -0800 | [diff] [blame] | 243 | DerComplex::addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 244 | { |
| 245 | nodePtr->setParent(this); |
| 246 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 247 | nodeList_.push_back(nodePtr); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 248 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 249 | if (!notifyParent) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 250 | return; |
| 251 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 252 | if (childChanged_) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 253 | return; |
| 254 | else |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 255 | childChanged_ = true; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 256 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 257 | if (0 != parent_) |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 258 | parent_->setChildChanged(); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void |
| 262 | DerComplex::setChildChanged() |
| 263 | { |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 264 | if (0 != parent_ && !childChanged_) { |
| 265 | parent_->setChildChanged(); |
| 266 | childChanged_ = true; |
| 267 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 268 | else |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 269 | childChanged_ = true; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 273 | DerComplex::encode(OutputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 274 | { |
| 275 | updateSize(); |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 276 | header_.clear(); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 277 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 278 | DerNode::encodeHeader(size_); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 279 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 280 | start.write((const char*)&header_[0], header_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 281 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 282 | DerNodePtrList::iterator it = nodeList_.begin(); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 283 | for (; it != nodeList_.end(); it++) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 284 | (*it)->encode(start); |
| 285 | } |
| 286 | |
| 287 | |
| 288 | /* |
| 289 | * DerByteString |
| 290 | */ |
| 291 | DerByteString::DerByteString(const string& str, DerType type) |
| 292 | :DerNode(type) |
| 293 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 294 | payload_.insert(payload_.end(), str.begin(), str.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 295 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 296 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 297 | } |
| 298 | |
Jeff Thompson | 4affbf5 | 2013-10-18 14:36:46 -0700 | [diff] [blame] | 299 | DerByteString::DerByteString(const vector<uint8_t>& blob, DerType type) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 300 | :DerNode(type) |
| 301 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 302 | payload_.insert(payload_.end(), blob.begin(), blob.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 303 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 304 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 307 | DerByteString::DerByteString(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 308 | :DerNode(start) |
| 309 | {} |
| 310 | |
| 311 | DerByteString::~DerByteString() |
| 312 | {} |
| 313 | |
| 314 | |
| 315 | /* |
| 316 | * DerBool |
| 317 | */ |
| 318 | DerBool::DerBool(bool value) |
| 319 | :DerNode(DER_BOOLEAN) |
| 320 | |
| 321 | { |
| 322 | char payload = (value ? 0xFF : 0x00); |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 323 | payload_.push_back(payload); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 324 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 325 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 326 | } |
| 327 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 328 | DerBool::DerBool(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 329 | :DerNode(start) |
| 330 | {} |
| 331 | |
| 332 | DerBool::~DerBool() |
| 333 | {} |
| 334 | |
| 335 | |
| 336 | /* |
| 337 | * DerInteger |
| 338 | */ |
| 339 | DerInteger::DerInteger(const vector<uint8_t>& blob) |
| 340 | :DerNode(DER_INTEGER) |
| 341 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 342 | payload_.insert(payload_.end(), blob.begin(), blob.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 343 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 344 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 345 | } |
| 346 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 347 | DerInteger::DerInteger(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 348 | :DerNode(start) |
| 349 | {} |
| 350 | |
| 351 | DerInteger::~DerInteger() |
| 352 | {} |
| 353 | |
| 354 | |
| 355 | /* |
| 356 | * DerBitString |
| 357 | */ |
| 358 | DerBitString::DerBitString(const vector<uint8_t>& blob, uint8_t paddingLen) |
| 359 | :DerNode(DER_BIT_STRING) |
| 360 | { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 361 | payload_.push_back((char)paddingLen); |
| 362 | payload_.insert(payload_.end(), blob.begin(), blob.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 363 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 364 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 365 | } |
| 366 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 367 | DerBitString::DerBitString(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 368 | :DerNode(start) |
| 369 | {} |
| 370 | |
| 371 | DerBitString::~DerBitString() |
| 372 | {} |
| 373 | |
| 374 | |
| 375 | /* |
| 376 | * DerOctetString |
| 377 | */ |
| 378 | DerOctetString::DerOctetString(const string& str) |
| 379 | :DerByteString(str, DER_OCTET_STRING) |
| 380 | {} |
| 381 | |
| 382 | DerOctetString::DerOctetString(const vector<uint8_t>& blob) |
| 383 | :DerByteString(blob, DER_OCTET_STRING) |
| 384 | {} |
| 385 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 386 | DerOctetString::DerOctetString(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 387 | :DerByteString(start) |
| 388 | {} |
| 389 | |
| 390 | DerOctetString::~DerOctetString() |
| 391 | {} |
| 392 | |
| 393 | |
| 394 | /* |
| 395 | * DerNull |
| 396 | */ |
| 397 | DerNull::DerNull() |
| 398 | :DerNode(DER_NULL) |
| 399 | { |
| 400 | DerNode::encodeHeader(0); |
| 401 | } |
| 402 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 403 | DerNull::DerNull(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 404 | :DerNode(start) |
| 405 | {} |
| 406 | |
| 407 | DerNull::~DerNull() |
| 408 | {} |
| 409 | |
| 410 | |
| 411 | /* |
| 412 | * DerOid |
| 413 | */ |
| 414 | DerOid::DerOid(const OID& oid) |
| 415 | :DerNode(DER_OBJECT_IDENTIFIER) |
| 416 | { |
| 417 | prepareEncoding(oid.getIntegerList()); |
| 418 | } |
| 419 | |
| 420 | |
| 421 | DerOid::DerOid(const string& oidStr) |
| 422 | :DerNode(DER_OBJECT_IDENTIFIER) |
| 423 | { |
| 424 | vector<int> value; |
| 425 | |
| 426 | string str = oidStr + "."; |
| 427 | |
| 428 | size_t pos = 0; |
| 429 | size_t ppos = 0; |
| 430 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 431 | while (string::npos != pos) { |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 432 | ppos = pos; |
| 433 | |
| 434 | pos = str.find_first_of('.', pos); |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 435 | if (string::npos == pos) |
| 436 | break; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 437 | |
| 438 | value.push_back(atoi(str.substr(ppos, pos - ppos).c_str())); |
| 439 | |
| 440 | pos++; |
| 441 | } |
| 442 | |
| 443 | prepareEncoding(value); |
| 444 | } |
| 445 | |
| 446 | DerOid::DerOid(const vector<int>& value) |
| 447 | :DerNode(DER_OBJECT_IDENTIFIER) |
| 448 | { |
| 449 | prepareEncoding(value); |
| 450 | } |
| 451 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 452 | DerOid::DerOid(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 453 | :DerNode(start) |
| 454 | {} |
| 455 | |
| 456 | DerOid::~DerOid() |
| 457 | {} |
| 458 | |
| 459 | void |
| 460 | DerOid::prepareEncoding(const vector<int>& value) |
| 461 | { |
| 462 | ostringstream os; |
| 463 | |
| 464 | int firstNumber = 0; |
| 465 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 466 | if (value.size() >= 1) { |
| 467 | if (0 <= value[0] && 2 >= value[0]) |
| 468 | firstNumber = value[0] * 40; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 469 | else |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 470 | throw DerEncodingException("first integer of oid is out of range"); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 471 | } |
| 472 | else |
| 473 | throw DerEncodingException("no integer in oid"); |
| 474 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 475 | if (value.size() >= 2) { |
| 476 | if (0 <= value[1] && 39 >= value[1]) |
| 477 | firstNumber += value[1]; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 478 | else |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 479 | throw DerEncodingException("second integer of oid is out of range"); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 480 | } |
| 481 | |
| 482 | encode128(firstNumber, os); |
| 483 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 484 | if (value.size() > 2) { |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 485 | int i = 2; |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 486 | for (; i < value.size(); i++) |
| 487 | encode128(value[i], os); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | string output = os.str(); |
| 491 | DerNode::encodeHeader(output.size()); |
| 492 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 493 | payload_.insert(payload_.end(), output.begin(), output.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | void |
| 497 | DerOid::encode128(int value, ostringstream& os) |
| 498 | { |
| 499 | int mask = (1 << 7) - 1; |
| 500 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 501 | if (128 > value) { |
| 502 | uint8_t singleByte = (uint8_t) mask & value; |
| 503 | os.write((char *)&singleByte, 1); |
| 504 | } |
| 505 | else { |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 506 | uint8_t buf[(sizeof(value)*8 + 6)/7 + 1]; |
| 507 | uint8_t *p = &(buf[sizeof(buf)-1]); |
| 508 | int n = 1; |
| 509 | |
| 510 | p[0] = (uint8_t)(value & mask); |
| 511 | value >>= 7; |
| 512 | |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 513 | while (value != 0) { |
| 514 | (--p)[0] = (uint8_t)((value & mask) | (1 << 7)); |
| 515 | n++; |
| 516 | value >>= 7; |
| 517 | } |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 518 | |
| 519 | os.write((char *)p, n); |
| 520 | } |
| 521 | } |
| 522 | |
| 523 | int |
| 524 | DerOid::decode128(int & offset) |
| 525 | { |
| 526 | uint8_t flagMask = 0x80; |
| 527 | int result = 0; |
Jeff Thompson | 161a2e0 | 2013-10-16 16:04:08 -0700 | [diff] [blame] | 528 | while (payload_[offset] & flagMask) { |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 529 | result = 128 * result + (uint8_t) payload_[offset] - 128; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 530 | offset++; |
| 531 | } |
| 532 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 533 | result = result * 128 + payload_[offset]; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 534 | offset++; |
| 535 | |
| 536 | return result; |
| 537 | } |
| 538 | |
| 539 | |
| 540 | /* |
| 541 | * DerSequence |
| 542 | */ |
| 543 | DerSequence::DerSequence() |
| 544 | :DerComplex(DER_SEQUENCE) |
| 545 | {} |
| 546 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 547 | DerSequence::DerSequence(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 548 | :DerComplex(start) |
| 549 | {} |
| 550 | |
| 551 | DerSequence::~DerSequence() |
| 552 | {} |
| 553 | |
| 554 | |
| 555 | /* |
| 556 | * DerPrintableString |
| 557 | */ |
| 558 | DerPrintableString::DerPrintableString(const string& str) |
| 559 | :DerByteString(str, DER_PRINTABLE_STRING) |
| 560 | {} |
| 561 | |
| 562 | DerPrintableString::DerPrintableString(const vector<uint8_t>& blob) |
| 563 | :DerByteString(blob, DER_PRINTABLE_STRING) |
| 564 | {} |
| 565 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 566 | DerPrintableString::DerPrintableString(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 567 | :DerByteString(start) |
| 568 | {} |
| 569 | |
| 570 | DerPrintableString::~DerPrintableString() |
| 571 | {} |
| 572 | |
| 573 | |
| 574 | /* |
| 575 | * DerGtime |
| 576 | */ |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 577 | DerGtime::DerGtime(const MillisecondsSince1970& time) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 578 | :DerNode(DER_GENERALIZED_TIME) |
| 579 | { |
| 580 | string pTimeStr = toIsoString(time); |
| 581 | int index = pTimeStr.find_first_of('T'); |
| 582 | string derTime = pTimeStr.substr(0, index) + pTimeStr.substr(index+1, pTimeStr.size() - index -1) + "Z"; |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 583 | payload_.insert(payload_.end(), derTime.begin(), derTime.end()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 584 | |
Jeff Thompson | 7ff36fe | 2013-10-16 15:30:13 -0700 | [diff] [blame] | 585 | DerNode::encodeHeader(payload_.size()); |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 586 | } |
| 587 | |
Jeff Thompson | 2d47db7 | 2013-10-17 15:19:52 -0700 | [diff] [blame] | 588 | DerGtime::DerGtime(InputIterator& start) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 589 | :DerNode(start) |
| 590 | {} |
| 591 | |
| 592 | DerGtime::~DerGtime() |
| 593 | {} |
| 594 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 595 | string DerGtime::toIsoString(const MillisecondsSince1970& time) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 596 | { |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 597 | char isoString[25]; |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 598 | ndn_Error error; |
| 599 | if ((error = ndn_toIsoString(time, isoString))) |
| 600 | throw runtime_error(ndn_getErrorString(error)); |
| 601 | |
Jeff Thompson | 5a6ce83 | 2013-10-18 11:54:35 -0700 | [diff] [blame] | 602 | return isoString; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 603 | } |
| 604 | |
Jeff Thompson | 9a8e82f | 2013-10-17 14:13:43 -0700 | [diff] [blame] | 605 | MillisecondsSince1970 DerGtime::fromIsoString(const string& isoString) |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 606 | { |
Jeff Thompson | ea94620 | 2013-10-18 14:35:32 -0700 | [diff] [blame] | 607 | MillisecondsSince1970 milliseconds; |
| 608 | ndn_Error error; |
| 609 | if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds))) |
| 610 | throw runtime_error(ndn_getErrorString(error)); |
| 611 | |
| 612 | return milliseconds; |
Jeff Thompson | a92861a | 2013-10-16 14:06:23 -0700 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | } // der |
| 616 | |
| 617 | } |