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