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