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