security: CryptoPP functions are used directly to encode/decode DER/BER
This change eliminates the need for custom der decoder/encoder.
Change-Id: I5be2e55cec2b63157927a4ad87fffe8e8651ed3c
diff --git a/src/encoding/cryptopp/asn_ext.cpp b/src/encoding/cryptopp/asn_ext.cpp
new file mode 100644
index 0000000..6698426
--- /dev/null
+++ b/src/encoding/cryptopp/asn_ext.cpp
@@ -0,0 +1,107 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#include <ndn-cpp/ndn-cpp-config.h>
+#include "asn_ext.hpp"
+
+#if NDN_CPP_HAVE_TIME_H
+#include <time.h>
+#endif
+#if NDN_CPP_HAVE_SYS_TIME_H
+#include <sys/time.h>
+#endif
+
+#include <boost/format.hpp>
+#include <boost/lexical_cast.hpp>
+
+using namespace CryptoPP;
+
+namespace ndn {
+
+size_t
+DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 time)
+{
+#ifndef NDN_CPP_HAVE_GMTIME_SUPPORT
+ throw Asn::Error("Time functions are not supported by the standard library");
+#endif
+
+ if (time < 0)
+ throw Asn::Error("Calendar time value out of range");
+ else if (time > 2e14)
+ // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
+ throw Asn::Error("Calendar time value out of range");
+
+ time_t secondsSince1970 = time / 1000;
+ struct tm* gmt = gmtime(&secondsSince1970);
+
+ std::string asn1time ((boost::format("%04d%02d%02d%02d%02d%02d%sZ")
+ % (1900 + gmt->tm_year)
+ % (gmt->tm_mon + 1)
+ % gmt->tm_mday
+ % gmt->tm_hour
+ % gmt->tm_min
+ % gmt->tm_sec).str());
+ // = os.str();
+
+ bt.Put(GENERALIZED_TIME);
+ size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
+ bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
+ return 1+lengthBytes+asn1time.size();
+}
+
+void
+BERDecodeTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 &time)
+{
+ byte b;
+ if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
+ BERDecodeError();
+
+ size_t bc;
+ if (!BERLengthDecode(bt, bc))
+ BERDecodeError();
+
+ SecByteBlock time_str(bc);
+ if (bc != bt.Get(time_str, bc))
+ BERDecodeError();
+
+ std::vector<std::string> params;
+ std::string current;
+ std::locale cLocale("C");
+
+ for(uint32_t j = 0; j != time_str.size(); ++j)
+ {
+ if(std::isdigit(reinterpret_cast<char&>(time_str[j]), cLocale))
+ current += time_str[j];
+ else
+ {
+ if(current != "")
+ params.push_back(current);
+ current.clear();
+ }
+ }
+ if(current != "")
+ params.push_back(current);
+
+ if(params.size() < 3 || params.size() > 6)
+ throw Asn::Error("Invalid time specification " + std::string(time_str.begin(), time_str.end()));
+
+ struct tm gmt;
+ gmt.tm_year = boost::lexical_cast<int>(params[0]);
+ gmt.tm_mon = boost::lexical_cast<int>(params[1]) - 1;
+ gmt.tm_mday = boost::lexical_cast<int>(params[2]);
+ gmt.tm_hour = (params.size() >= 4) ? boost::lexical_cast<int>(params[3]) : 0;
+ gmt.tm_min = (params.size() >= 5) ? boost::lexical_cast<int>(params[4]) : 0;
+ gmt.tm_sec = (params.size() == 6) ? boost::lexical_cast<int>(params[5]) : 0;
+
+ if (b == GENERALIZED_TIME)
+ gmt.tm_year -= 1900;
+
+ time = timegm(&gmt) * 1000;
+}
+
+} // namespace ndn
diff --git a/src/encoding/cryptopp/asn_ext.hpp b/src/encoding/cryptopp/asn_ext.hpp
new file mode 100644
index 0000000..a7be874
--- /dev/null
+++ b/src/encoding/cryptopp/asn_ext.hpp
@@ -0,0 +1,30 @@
+/**
+ * Copyright (C) 2013 Regents of the University of California.
+ * @author: Jeff Thompson <jefft0@remap.ucla.edu>
+ * @author: Yingdi Yu <yingdi@cs.ucla.edu>
+ * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
+ * See COPYING for copyright and distribution information.
+ */
+
+#ifndef NDN_ASN_EXT_HPP
+#define NDN_ASN_EXT_HPP
+
+#include <ndn-cpp/common.hpp>
+#include <cryptopp/asn.h>
+
+namespace ndn {
+
+namespace Asn {
+struct Error : public std::runtime_error { Error(const std::string &what) : std::runtime_error(what) {} };
+}
+
+size_t
+DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 time);
+
+void
+BERDecodeTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 &time);
+
+} // namespace ndn
+
+#endif // NDN_ASN_EXT_HPP
+
diff --git a/src/encoding/der/der-exception.cpp b/src/encoding/der/der-exception.cpp
deleted file mode 100644
index 84ab5c1..0000000
--- a/src/encoding/der/der-exception.cpp
+++ /dev/null
@@ -1,29 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "der-exception.hpp"
-
-using namespace std;
-
-namespace ndn {
-
-namespace der
-{
-
-DerException::DerException(const string& errorMessage) throw()
- : errorMessage_(errorMessage)
-{
-}
-
-DerException::~DerException() throw()
-{
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/der-exception.hpp b/src/encoding/der/der-exception.hpp
deleted file mode 100644
index 5016697..0000000
--- a/src/encoding/der/der-exception.hpp
+++ /dev/null
@@ -1,31 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_EXCEPTION_HPP
-#define NDN_DER_EXCEPTION_HPP
-
-#include <stdexcept>
-#include <string>
-
-namespace ndn {
-
-namespace der {
-
-struct DerException : public std::runtime_error { DerException(const std::string &msg) : std::runtime_error(msg) {} };
-
-class NegativeLengthException : public DerException { NegativeLengthException(const std::string &msg) : DerException(msg) {} };
-
-class DerEncodingException : public DerException { DerEncodingException(const std::string &msg) : DerException(msg) {} };
-
-class DerDecodingException : public DerException { DerDecodingException(const std::string &msg) : DerException(msg) {} };
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/der.cpp b/src/encoding/der/der.cpp
deleted file mode 100644
index 94578b4..0000000
--- a/src/encoding/der/der.cpp
+++ /dev/null
@@ -1,617 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include <stdexcept>
-#include "der-exception.hpp"
-#include "../../util/logging.hpp"
-#include "../../c/util/time.h"
-#include "der.hpp"
-
-INIT_LOGGER("ndn.der.DER");
-
-using namespace std;
-
-namespace ndn {
-
-namespace der {
-
-/*
- * DerNode
- */
-DerNode::DerNode()
- :parent_(0)
-{}
-
-DerNode::DerNode(DerType type)
- :type_(type),
- parent_(0)
-{}
-
-DerNode::DerNode(InputIterator& start)
- :parent_(0)
-{
- decode(start);
-}
-
-DerNode::~DerNode()
-{}
-
-void
-DerNode::encodeHeader(int size)
-{
- header_.push_back((char)type_);
-
- if (size >= 127) {
- int val = size;
- char buf[sizeof(val) + 1];
- char *p = &(buf[sizeof(buf)-1]);
- int n = 0;
- int mask = (1 << 8) - 1;
-
- while (val != 0) {
- p[0] = (char)(val & mask);
- p--;
- n++;
- val >>= 8;
- }
-
- p[0] = (char)((1<<7) | n);
- n++;
-
- header_.insert(header_.end(), p, p+n);
- }
- else if (size >= 0)
- header_.push_back((char)size);
- else
- throw NegativeLengthException("Negative length");
-}
-
-int
-DerNode::decodeHeader(InputIterator& start)
-{
- uint8_t type = start.ReadU8();
- // char type = start.get();
- header_.push_back(type);
- type_ = static_cast<DerType>((int)type);
-
- uint8_t sizeLen = start.ReadU8();
- // char sizeLen = start.get();
- header_.push_back(sizeLen);
-
- bool longFormat = sizeLen & (1 << 7);
-
- if (!longFormat) {
- // _LOG_DEBUG("Short Format");
- // _LOG_DEBUG("sizeLen: " << (int)sizeLen);
- return (int)sizeLen;
- }
- else {
- // _LOG_DEBUG("Long Format");
- uint8_t byte;
- // char byte;
- int lenCount = sizeLen & ((1<<7) - 1);
- // _LOG_DEBUG("sizeLen: " << (int)sizeLen);
- // _LOG_DEBUG("mask: " << (int)((1<<7) - 1));
- // _LOG_DEBUG("lenCount: " << (int)lenCount);
- int size = 0;
- do {
- byte = start.ReadU8();
- header_.push_back(byte);
- size = size * 256 + (int)byte;
- // _LOG_DEBUG("byte: " << (int)byte);
- // _LOG_DEBUG("size: " << size);
- lenCount--;
- } while (lenCount > 0);
-
- return size;
- }
-}
-
-void
-DerNode::encode(OutputIterator& start)
-{
- start.write((const char*)&header_[0], header_.size());
- start.write((const char*)&payload_[0], payload_.size());
-}
-
-void
-DerNode::decode(InputIterator& start)
-{
- int payloadSize = decodeHeader(start);
- // _LOG_DEBUG("payloadSize: " << payloadSize);
- if (payloadSize > 0 ) {
- char buf[payloadSize];
- start.read(buf, payloadSize);
- payload_.insert(payload_.end(), buf, buf + payloadSize);
- }
-}
-
-ptr_lib::shared_ptr<DerNode>
-DerNode::parse(InputIterator& start)
-{
- int type = ((uint8_t)start.PeekU8());
-
- // _LOG_DEBUG("Type: " << hex << setw(2) << setfill('0') << type);
- switch(type) {
- case DER_BOOLEAN:
- return ptr_lib::shared_ptr<DerBool>(new DerBool(start));
- case DER_INTEGER:
- return ptr_lib::shared_ptr<DerInteger>(new DerInteger(start));
- case DER_BIT_STRING:
- return ptr_lib::shared_ptr<DerBitString>(new DerBitString(start));
- case DER_OCTET_STRING:
- return ptr_lib::shared_ptr<DerOctetString>(new DerOctetString(start));
- case DER_NULL:
- return ptr_lib::shared_ptr<DerNull>(new DerNull(start));
- case DER_OBJECT_IDENTIFIER:
- return ptr_lib::shared_ptr<DerOid>(new DerOid(start));
- case DER_SEQUENCE:
- return ptr_lib::shared_ptr<DerSequence>(new DerSequence(start));
- case DER_PRINTABLE_STRING:
- return ptr_lib::shared_ptr<DerPrintableString>(new DerPrintableString(start));
- case DER_GENERALIZED_TIME:
- return ptr_lib::shared_ptr<DerGtime>(new DerGtime(start));
- default:
- throw DerDecodingException("Unimplemented DER type");
- }
-}
-
-
-/*
- * DerComplex
- */
-DerComplex::DerComplex()
- :DerNode(),
- childChanged_(false),
- size_(0)
-{}
-
-DerComplex::DerComplex(DerType type)
- :DerNode(type),
- childChanged_(false),
- size_(0)
-{}
-
-DerComplex::DerComplex(InputIterator& start)
- :DerNode(),
- childChanged_(false),
- size_(0)
-{
- size_ = DerNode::decodeHeader(start);
- // _LOG_DEBUG("Size: " << size_);
-
- int accSize = 0;
-
- while (accSize < size_) {
- // _LOG_DEBUG("accSize: " << accSize);
- ptr_lib::shared_ptr<DerNode> nodePtr = DerNode::parse(start);
- accSize += nodePtr->getSize();
- addChild(nodePtr, false);
- }
-}
-
-DerComplex::~DerComplex()
-{}
-
-int
-DerComplex::getSize()
-{
- if (childChanged_) {
- updateSize();
- childChanged_ = false;
- }
-
- header_.clear();
- DerNode::encodeHeader(size_);
- return size_ + header_.size();
-}
-
-Blob
-DerComplex::getRaw()
-{
- ptr_lib::shared_ptr<vector<uint8_t> > blob(new vector<uint8_t>());
- blob->insert(blob->end(), header_.begin(), header_.end());
-
- DerNodePtrList::iterator it = nodeList_.begin();
- for (; it != nodeList_.end(); it++) {
- Blob childBlob = (*it)->getRaw();
- blob->insert(blob->end(), childBlob->begin(), childBlob->end());
- }
- return blob;
-}
-
-void
-DerComplex::updateSize()
-{
- int newSize = 0;
-
- DerNodePtrList::iterator it = nodeList_.begin();
- for (; it != nodeList_.end(); it++) {
- newSize += (*it)->getSize();
- }
-
- size_ = newSize;
- childChanged_ = false;
-}
-
-void
-DerComplex::addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent)
-{
- nodePtr->setParent(this);
-
- nodeList_.push_back(nodePtr);
-
- if (!notifyParent)
- return;
-
- if (childChanged_)
- return;
- else
- childChanged_ = true;
-
- if (0 != parent_)
- parent_->setChildChanged();
-}
-
-void
-DerComplex::setChildChanged()
-{
- if (0 != parent_ && !childChanged_) {
- parent_->setChildChanged();
- childChanged_ = true;
- }
- else
- childChanged_ = true;
-}
-
-void
-DerComplex::encode(OutputIterator& start)
-{
- updateSize();
- header_.clear();
-
- DerNode::encodeHeader(size_);
-
- start.write((const char*)&header_[0], header_.size());
-
- DerNodePtrList::iterator it = nodeList_.begin();
- for (; it != nodeList_.end(); it++)
- (*it)->encode(start);
-}
-
-
-/*
- * DerByteString
- */
-DerByteString::DerByteString(const string& str, DerType type)
- :DerNode(type)
-{
- payload_.insert(payload_.end(), str.begin(), str.end());
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerByteString::DerByteString(const vector<uint8_t>& blob, DerType type)
- :DerNode(type)
-{
- payload_.insert(payload_.end(), blob.begin(), blob.end());
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerByteString::DerByteString(InputIterator& start)
- :DerNode(start)
-{}
-
-DerByteString::~DerByteString()
-{}
-
-
-/*
- * DerBool
- */
-DerBool::DerBool(bool value)
- :DerNode(DER_BOOLEAN)
-
-{
- char payload = (value ? 0xFF : 0x00);
- payload_.push_back(payload);
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerBool::DerBool(InputIterator& start)
- :DerNode(start)
-{}
-
-DerBool::~DerBool()
-{}
-
-
-/*
- * DerInteger
- */
-DerInteger::DerInteger(const vector<uint8_t>& blob)
- :DerNode(DER_INTEGER)
-{
- payload_.insert(payload_.end(), blob.begin(), blob.end());
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerInteger::DerInteger(InputIterator& start)
- :DerNode(start)
-{}
-
-DerInteger::~DerInteger()
-{}
-
-
-/*
- * DerBitString
- */
-DerBitString::DerBitString(const vector<uint8_t>& blob, uint8_t paddingLen)
- :DerNode(DER_BIT_STRING)
-{
- payload_.push_back((char)paddingLen);
- payload_.insert(payload_.end(), blob.begin(), blob.end());
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerBitString::DerBitString(InputIterator& start)
- :DerNode(start)
-{}
-
-DerBitString::~DerBitString()
-{}
-
-
-/*
- * DerOctetString
- */
-DerOctetString::DerOctetString(const string& str)
- :DerByteString(str, DER_OCTET_STRING)
-{}
-
-DerOctetString::DerOctetString(const vector<uint8_t>& blob)
- :DerByteString(blob, DER_OCTET_STRING)
-{}
-
-DerOctetString::DerOctetString(InputIterator& start)
- :DerByteString(start)
-{}
-
-DerOctetString::~DerOctetString()
-{}
-
-
-/*
- * DerNull
- */
-DerNull::DerNull()
- :DerNode(DER_NULL)
-{
- DerNode::encodeHeader(0);
-}
-
-DerNull::DerNull(InputIterator& start)
- :DerNode(start)
-{}
-
-DerNull::~DerNull()
-{}
-
-
-/*
- * DerOid
- */
-DerOid::DerOid(const OID& oid)
- :DerNode(DER_OBJECT_IDENTIFIER)
-{
- prepareEncoding(oid.getIntegerList());
-}
-
-
-DerOid::DerOid(const string& oidStr)
- :DerNode(DER_OBJECT_IDENTIFIER)
-{
- vector<int> value;
-
- string str = oidStr + ".";
-
- size_t pos = 0;
- size_t ppos = 0;
-
- while (string::npos != pos) {
- ppos = pos;
-
- pos = str.find_first_of('.', pos);
- if (string::npos == pos)
- break;
-
- value.push_back(atoi(str.substr(ppos, pos - ppos).c_str()));
-
- pos++;
- }
-
- prepareEncoding(value);
-}
-
-DerOid::DerOid(const vector<int>& value)
- :DerNode(DER_OBJECT_IDENTIFIER)
-{
- prepareEncoding(value);
-}
-
-DerOid::DerOid(InputIterator& start)
- :DerNode(start)
-{}
-
-DerOid::~DerOid()
-{}
-
-void
-DerOid::prepareEncoding(const vector<int>& value)
-{
- ostringstream os;
-
- int firstNumber = 0;
-
- if (value.size() >= 1) {
- if (0 <= value[0] && 2 >= value[0])
- firstNumber = value[0] * 40;
- else
- throw DerEncodingException("first integer of oid is out of range");
- }
- else
- throw DerEncodingException("no integer in oid");
-
- if (value.size() >= 2) {
- if (0 <= value[1] && 39 >= value[1])
- firstNumber += value[1];
- else
- throw DerEncodingException("second integer of oid is out of range");
- }
-
- encode128(firstNumber, os);
-
- if (value.size() > 2) {
- int i = 2;
- for (; i < value.size(); i++)
- encode128(value[i], os);
- }
-
- string output = os.str();
- DerNode::encodeHeader(output.size());
-
- payload_.insert(payload_.end(), output.begin(), output.end());
-}
-
-void
-DerOid::encode128(int value, ostringstream& os)
-{
- int mask = (1 << 7) - 1;
-
- if (128 > value) {
- uint8_t singleByte = (uint8_t) mask & value;
- os.write((char *)&singleByte, 1);
- }
- else {
- uint8_t buf[(sizeof(value)*8 + 6)/7 + 1];
- uint8_t *p = &(buf[sizeof(buf)-1]);
- int n = 1;
-
- p[0] = (uint8_t)(value & mask);
- value >>= 7;
-
- while (value != 0) {
- (--p)[0] = (uint8_t)((value & mask) | (1 << 7));
- n++;
- value >>= 7;
- }
-
- os.write((char *)p, n);
- }
-}
-
-int
-DerOid::decode128(int & offset)
-{
- uint8_t flagMask = 0x80;
- int result = 0;
- while (payload_[offset] & flagMask) {
- result = 128 * result + (uint8_t) payload_[offset] - 128;
- offset++;
- }
-
- result = result * 128 + payload_[offset];
- offset++;
-
- return result;
-}
-
-
-/*
- * DerSequence
- */
-DerSequence::DerSequence()
- :DerComplex(DER_SEQUENCE)
-{}
-
-DerSequence::DerSequence(InputIterator& start)
- :DerComplex(start)
-{}
-
-DerSequence::~DerSequence()
-{}
-
-
-/*
- * DerPrintableString
- */
-DerPrintableString::DerPrintableString(const string& str)
- :DerByteString(str, DER_PRINTABLE_STRING)
-{}
-
-DerPrintableString::DerPrintableString(const vector<uint8_t>& blob)
- :DerByteString(blob, DER_PRINTABLE_STRING)
-{}
-
-DerPrintableString::DerPrintableString(InputIterator& start)
- :DerByteString(start)
-{}
-
-DerPrintableString::~DerPrintableString()
-{}
-
-
-/*
- * DerGtime
- */
-DerGtime::DerGtime(const MillisecondsSince1970& time)
- :DerNode(DER_GENERALIZED_TIME)
-{
- string pTimeStr = toIsoString(time);
- int index = pTimeStr.find_first_of('T');
- string derTime = pTimeStr.substr(0, index) + pTimeStr.substr(index+1, pTimeStr.size() - index -1) + "Z";
- payload_.insert(payload_.end(), derTime.begin(), derTime.end());
-
- DerNode::encodeHeader(payload_.size());
-}
-
-DerGtime::DerGtime(InputIterator& start)
- :DerNode(start)
-{}
-
-DerGtime::~DerGtime()
-{}
-
-string DerGtime::toIsoString(const MillisecondsSince1970& time)
-{
- char isoString[25];
- ndn_Error error;
- if ((error = ndn_toIsoString(time, isoString)))
- throw runtime_error(ndn_getErrorString(error));
-
- return isoString;
-}
-
-MillisecondsSince1970 DerGtime::fromIsoString(const string& isoString)
-{
- MillisecondsSince1970 milliseconds;
- ndn_Error error;
- if ((error = ndn_fromIsoString(isoString.c_str(), &milliseconds)))
- throw runtime_error(ndn_getErrorString(error));
-
- return milliseconds;
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/der.hpp b/src/encoding/der/der.hpp
deleted file mode 100644
index d8753be..0000000
--- a/src/encoding/der/der.hpp
+++ /dev/null
@@ -1,407 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_HPP
-#define NDN_DER_HPP
-
-#include <vector>
-#include <string>
-#include <istream>
-#include <ostream>
-#include <sstream>
-
-#include <ndn-cpp/common.hpp>
-#include <ndn-cpp/encoding/oid.hpp>
-#include <ndn-cpp/util/blob.hpp>
-
-#include "visitor/visitor.hpp"
-#include "visitor/void-visitor.hpp"
-#include "visitor/no-arguments-visitor.hpp"
-#include "visitor/void-no-arguments-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-enum DerType {
- DER_EOC = 0,
- DER_BOOLEAN = 1,
- DER_INTEGER = 2,
- DER_BIT_STRING = 3,
- DER_OCTET_STRING = 4,
- DER_NULL = 5,
- DER_OBJECT_IDENTIFIER = 6,
- DER_OBJECT_DESCRIPTOR = 7,
- DER_EXTERNAL = 40,
- DER_REAL = 9,
- DER_ENUMERATED = 10,
- DER_EMBEDDED_PDV = 43,
- DER_UTF8_STRING = 12,
- DER_RELATIVE_OID = 13,
- DER_SEQUENCE = 48,
- DER_SET = 49,
- DER_NUMERIC_STRING = 18,
- DER_PRINTABLE_STRING = 19,
- DER_T61_STRING = 20,
- DER_VIDEOTEX_STRING = 21,
- DER_IA5_STRING = 22,
- DER_UTC_TIME = 23,
- DER_GENERALIZED_TIME = 24,
- DER_GRAPHIC_STRING = 25,
- DER_VISIBLE_STRING = 26,
- DER_GENERAL_STRING = 27,
- DER_UNIVERSAL_STRING = 28,
- DER_CHARACTER_STRING = 29,
- DER_BMP_STRING = 30,
-};
-
-class InputIterator : public std::istream
-{
-public:
- uint8_t ReadU8() { return static_cast<uint8_t> (get()); }
- uint8_t PeekU8() { return static_cast<uint8_t> (peek()); }
- bool IsEnd() const { return eof(); }
- void Prev() { seekg(-1, std::ios_base::cur); }
-};
-
-class OutputIterator : public std::ostream
-{
-public:
- void Write(const uint8_t * s, uint32_t n) { write (reinterpret_cast<const char*>(s),n); }
- void WriteU8(const uint8_t s) { put (s); }
- void WriteU8(const uint8_t s, uint32_t n) { for (uint32_t i = 0; i < n; i++) { put(s); } }
-};
-
-class DerComplex;
-
-class DerNode
-{
-public:
- DerNode();
-
- DerNode(DerType type);
-
- DerNode(InputIterator& start);
-
- virtual
- ~DerNode();
-
- virtual int
- getSize() { return header_.size() + payload_.size(); }
-
- virtual void
- encode(OutputIterator& start);
-
- void
- setParent(DerComplex * parent) { parent_ = parent; }
-
- static ptr_lib::shared_ptr<DerNode>
- parse(InputIterator& start);
-
- const std::vector<uint8_t>&
- getHeader() const { return header_; }
-
- std::vector<uint8_t>&
- getHeader() { return header_; }
-
- const std::vector<uint8_t>&
- getPayload() const { return payload_; }
-
- std::vector<uint8_t>&
- getPayload() { return payload_; }
-
- const DerType&
- getType() { return type_; }
-
- virtual Blob
- getRaw()
- {
- ptr_lib::shared_ptr<std::vector<uint8_t> > blob(new std::vector<uint8_t>());
- blob->insert(blob->end(), header_.begin(), header_.end());
- blob->insert(blob->end(), payload_.begin(), payload_.end());
-
- return blob;
- }
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) = 0;
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) = 0;
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) = 0;
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) = 0;
-
-protected:
- void
- decode(InputIterator& start);
-
- void
- encodeHeader(int size);
-
- int
- decodeHeader(InputIterator& start);
-
-protected:
- DerType type_;
- std::vector<uint8_t> header_;
- std::vector<uint8_t> payload_;
- DerComplex * parent_;
-};
-
-
-typedef std::vector<ptr_lib::shared_ptr<DerNode> > DerNodePtrList;
-
-class DerComplex : public DerNode
-{
-public:
- DerComplex();
-
- DerComplex(DerType type);
-
- DerComplex(InputIterator& start);
-
- virtual
- ~DerComplex();
-
- virtual int
- getSize();
-
- void
- addChild(ptr_lib::shared_ptr<DerNode> nodePtr, bool notifyParent = true);
-
- virtual void
- encode(OutputIterator& start);
-
- const DerNodePtrList&
- getChildren() const { return nodeList_; }
-
- DerNodePtrList&
- getChildren() { return nodeList_; }
-
- virtual Blob
- getRaw();
-
-private:
- void
- updateSize();
-
- void
- setChildChanged();
-
-private:
- bool childChanged_;
- int size_;
- DerNodePtrList nodeList_;
-};
-
-
-
-class DerByteString : public DerNode
-{
-public:
- DerByteString(const std::string& str, DerType type);
-
- DerByteString(const std::vector<uint8_t>& blob, DerType type);
-
- DerByteString(InputIterator& start);
-
- virtual
- ~DerByteString();
-};
-
-
-//0x01
-class DerBool : public DerNode
-{
-public:
- DerBool(bool value);
-
- DerBool(InputIterator& start);
-
- virtual
- ~DerBool();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-//0x02
-class DerInteger : public DerNode
-{
-public:
- DerInteger(const std::vector<uint8_t>& blob);
-
- DerInteger(InputIterator& start);
-
- virtual
- ~DerInteger();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-//0x03
-class DerBitString : public DerNode
-{
-public:
- DerBitString(const std::vector<uint8_t>& blob, uint8_t paddingLen);
-
- DerBitString(InputIterator& start);
-
- virtual
- ~DerBitString();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-//0x04
-class DerOctetString : public DerByteString
-{
-public:
- DerOctetString(const std::string& str);
-
- DerOctetString(const std::vector<uint8_t>& blob);
-
- DerOctetString(InputIterator& start);
-
- virtual
- ~DerOctetString();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-
-//0x05
-class DerNull : public DerNode
-{
-public:
- DerNull();
-
- DerNull(InputIterator& start);
-
- virtual
- ~DerNull();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-
-//0x06
-class DerOid : public DerNode
-{
-public:
- DerOid(const OID& oid);
-
- DerOid(const std::string& oidStr);
-
- DerOid(const std::vector<int>& value);
-
- DerOid(InputIterator& start);
-
- virtual
- ~DerOid();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-
- int
- decode128(int& offset);
-
-private:
- void
- prepareEncoding(const std::vector<int>& value);
-
- void
- encode128(int value, std::ostringstream& os);
-};
-
-
-//0x10
-class DerSequence : public DerComplex
-{
-public:
- DerSequence();
-
- DerSequence(InputIterator& start);
-
- virtual
- ~DerSequence();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-//0x13
-class DerPrintableString : public DerByteString
-{
-public:
- DerPrintableString(const std::string& str);
-
- DerPrintableString(const std::vector<uint8_t>& blob);
-
- DerPrintableString(InputIterator& start);
-
- virtual
- ~DerPrintableString();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-};
-
-//0x1b
-class DerGtime : public DerNode
-{
-public:
- DerGtime(const MillisecondsSince1970& time);
-
- DerGtime(InputIterator& start);
-
- virtual
- ~DerGtime();
-
- virtual void accept(VoidNoArgumentsVisitor& visitor) { visitor.visit(*this); }
- virtual void accept(VoidVisitor& visitor, ndnboost::any param) { visitor.visit(*this, param); }
- virtual ndnboost::any accept(NoArgumentsVisitor& visitor) { return visitor.visit(*this); }
- virtual ndnboost::any accept(Visitor& visitor, ndnboost::any param) { return visitor.visit(*this, param); }
-
- /**
- * Convert to the ISO string representation of the time.
- * @param time Milliseconds since 1/1/1970.
- * @return The ISO string.
- */
- static std::string toIsoString(const MillisecondsSince1970& time);
-
- /**
- * Convert from the ISO string representation to the internal time format.
- * @param isoString The ISO time formatted string.
- * @return The time in milliseconds since 1/1/1970.
- */
- static MillisecondsSince1970 fromIsoString(const std::string& isoString);
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/certificate-data-visitor.cpp b/src/encoding/der/visitor/certificate-data-visitor.cpp
deleted file mode 100644
index 7edf794..0000000
--- a/src/encoding/der/visitor/certificate-data-visitor.cpp
+++ /dev/null
@@ -1,157 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "simple-visitor.hpp"
-#include "public-key-visitor.hpp"
-#include "../der.hpp"
-
-#include <ndn-cpp/security/certificate/certificate.hpp>
-#include <ndn-cpp/security/certificate/certificate-subject-description.hpp>
-#include <ndn-cpp/security/certificate/certificate-extension.hpp>
-
-#include "../../../util/logging.hpp"
-#include "certificate-data-visitor.hpp"
-
-using namespace std;
-
-INIT_LOGGER("ndn.der.CertificateDataVisitor");
-
-namespace ndn {
-
-namespace der {
-
-/*
- * CertificateDataVisitor
- */
-void
-CertificateDataVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- // _LOG_DEBUG("CertificateDataVisitor::visit");
-
- DerNodePtrList& children = derSeq.getChildren();
- CertificateValidityVisitor validityVisitor;
- children[0]->accept(validityVisitor, param);
- CertificateSubjectVisitor subjectVisitor;
- children[1]->accept(subjectVisitor, param);
- PublicKeyVisitor pubkeyVisitor;
- Certificate* certData = ndnboost::any_cast<Certificate*>(param);
- certData->setPublicKeyInfo(*ndnboost::any_cast<ptr_lib::shared_ptr<PublicKey> >(children[2]->accept(pubkeyVisitor)));
-
- if(children.size() > 3)
- {
- CertificateExtensionVisitor extnVisitor;
- children[3]->accept(extnVisitor, param);
- }
-}
-
-/*
- * CertValidityVisitor
- */
-void
-CertificateValidityVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- // _LOG_DEBUG("CertValidityVisitor::visit");
-
- Certificate* certData = ndnboost::any_cast<Certificate*>(param);
-
- DerNodePtrList& children = derSeq.getChildren();
-
- SimpleVisitor simpleVisitor;
-
- MillisecondsSince1970 notBefore = ndnboost::any_cast<MillisecondsSince1970>(children[0]->accept(simpleVisitor));
- MillisecondsSince1970 notAfter = ndnboost::any_cast<MillisecondsSince1970>(children[1]->accept(simpleVisitor));
-
- // _LOG_DEBUG("parsed notBefore: " << notBefore);
- // _LOG_DEBUG("parsed notAfter: " << notAfter);
-
- certData->setNotBefore(notBefore);
- certData->setNotAfter(notAfter);
-}
-
-/*
- * CertSubDescryptVisitor
- */
-void
-CertificateSubjectDescriptionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- Certificate* certData = ndnboost::any_cast<Certificate*>(param);
-
- DerNodePtrList& children = derSeq.getChildren();
-
- SimpleVisitor simpleVisitor;
-
- OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
- string value = ndnboost::any_cast<string>(children[1]->accept(simpleVisitor));
-
- CertificateSubjectDescription subDescrypt(oid, value);
-
- certData->addSubjectDescription(subDescrypt);
-}
-
-/*
- * CertSubjectVisitor
- */
-void
-CertificateSubjectVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- // _LOG_DEBUG("CertSubjectVisitor::visit");
-
- DerNodePtrList& children = derSeq.getChildren();
-
- CertificateSubjectDescriptionVisitor descryptVisitor;
-
- DerNodePtrList::iterator it = children.begin();
-
- while(it != children.end()) {
- (*it)->accept(descryptVisitor, param);
- it++;
- }
-}
-
-/*
- * CertExtnEntryVisitor
- */
-void
-CertificateExtensionEntryVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- Certificate* certData = ndnboost::any_cast<Certificate*>(param);
-
- DerNodePtrList& children = derSeq.getChildren();
-
- SimpleVisitor simpleVisitor;
-
- OID oid = ndnboost::any_cast<OID>(children[0]->accept(simpleVisitor));
- bool critical = ndnboost::any_cast<bool>(children[1]->accept(simpleVisitor));
- const vector<uint8_t>& value = ndnboost::any_cast<const vector<uint8_t>&>(children[2]->accept(simpleVisitor));
-
- CertificateExtension extension(oid, critical, value);
-
- certData->addExtension(extension);
-}
-
-/*
- * CertExtensionVisitor
- */
-void
-CertificateExtensionVisitor::visit(DerSequence& derSeq, ndnboost::any param)
-{
- DerNodePtrList& children = derSeq.getChildren();
-
- CertificateExtensionEntryVisitor extnEntryVisitor;
-
- DerNodePtrList::iterator it = children.begin();
-
- while(it != children.end()) {
- (*it)->accept(extnEntryVisitor, param);
- it++;
- }
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/certificate-data-visitor.hpp b/src/encoding/der/visitor/certificate-data-visitor.hpp
deleted file mode 100644
index ec73bd7..0000000
--- a/src/encoding/der/visitor/certificate-data-visitor.hpp
+++ /dev/null
@@ -1,51 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_CERTIFICATE_DATA_VISITOR_HPP
-#define NDN_CERTIFICATE_DATA_VISITOR_HPP
-
-#include "void-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class CertificateDataVisitor : public VoidVisitor {
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-class CertificateValidityVisitor : public VoidVisitor {
-public:
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-class CertificateSubjectDescriptionVisitor : public VoidVisitor {
-public:
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-class CertificateSubjectVisitor : public VoidVisitor {
-public:
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-class CertificateExtensionEntryVisitor : public VoidVisitor {
-public:
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-class CertificateExtensionVisitor : public VoidVisitor {
-public:
- virtual void visit(DerSequence&, ndnboost::any);
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/no-arguments-visitor.cpp b/src/encoding/der/visitor/no-arguments-visitor.cpp
deleted file mode 100644
index 06f2157..0000000
--- a/src/encoding/der/visitor/no-arguments-visitor.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "no-arguments-visitor.hpp"
-
-namespace ndn {
-
-namespace der
-{
-ndnboost::any
-NoArgumentsVisitor::visit(DerBool& derBool)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerInteger& derInteger)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerPrintableString& derPStr)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerBitString& derBStr)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerNull& derNull)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerOctetString& derOStr)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerOid& derOid)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerSequence& derSeq)
-{ return ndnboost::any(0); }
-
-ndnboost::any
-NoArgumentsVisitor::visit(DerGtime& derGtime)
-{ return ndnboost::any(0); }
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/no-arguments-visitor.hpp b/src/encoding/der/visitor/no-arguments-visitor.hpp
deleted file mode 100644
index 09c24c1..0000000
--- a/src/encoding/der/visitor/no-arguments-visitor.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_NO_ARGUMENTS_VISITOR_HPP
-#define NDN_DER_NO_ARGUMENTS_VISITOR_HPP
-
-#include "visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class NoArgumentsVisitor
-{
-public:
- virtual ndnboost::any visit(DerBool& );
- virtual ndnboost::any visit(DerInteger& );
- virtual ndnboost::any visit(DerPrintableString&);
- virtual ndnboost::any visit(DerBitString& );
- virtual ndnboost::any visit(DerNull& );
- virtual ndnboost::any visit(DerOctetString& );
- virtual ndnboost::any visit(DerOid& );
- virtual ndnboost::any visit(DerSequence& );
- virtual ndnboost::any visit(DerGtime& );
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/print-visitor.cpp b/src/encoding/der/visitor/print-visitor.cpp
deleted file mode 100644
index 80a4f5d..0000000
--- a/src/encoding/der/visitor/print-visitor.cpp
+++ /dev/null
@@ -1,131 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "print-visitor.hpp"
-#include "../der.hpp"
-#include <iostream>
-#include <iomanip>
-
-using namespace std;
-
-namespace ndn {
-
-namespace der {
-
-void
-PrintVisitor::visit(DerBool& derBool, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derBool.getHeader(), indent);
- printData(derBool.getPayload(), indent + " ");
-}
-
-void
-PrintVisitor::visit(DerInteger& derInteger, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derInteger.getHeader(), indent);
- printData(derInteger.getPayload(), indent + " ");
-}
-
-void
-PrintVisitor::visit(DerPrintableString& derPStr, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derPStr.getHeader(), indent);
- printData(derPStr.getPayload(), indent + " ");
-}
-
-void
-PrintVisitor::visit(DerBitString& derBStr, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derBStr.getHeader(), indent);
- const vector<uint8_t>& payload = derBStr.getPayload();
- cout << indent << " " << " " << hex << setw(2) << setfill('0') << (int)(uint8_t)payload[0] << endl;
- printData(payload, indent + " ", 1);
-}
-
-void
-PrintVisitor::visit(DerNull& derNull, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derNull.getHeader(), indent);
- printData(derNull.getPayload(), indent + " ");
-
-}
-
-void
-PrintVisitor::visit(DerOctetString& derOStr, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derOStr.getHeader(), indent);
- printData(derOStr.getPayload(), indent + " ");
-}
-
-void
-PrintVisitor::visit(DerOid& derOid, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derOid.getHeader(), indent);
- printData(derOid.getPayload(), indent + " ");
-
-}
-
-void
-PrintVisitor::visit(DerGtime& derGtime, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derGtime.getHeader(), indent);
- printData(derGtime.getPayload(), indent + " ");
-}
-
-void
-PrintVisitor::visit(DerSequence& derSequence, ndnboost::any param)
-{
- const string& indent = ndnboost::any_cast<const string&>(param);
-
- printData(derSequence.getHeader(), indent);
-
- DerNodePtrList& children = derSequence.getChildren();
- DerNodePtrList::iterator it = children.begin();
- for(; it != children.end(); it++)
- (*it)->accept(*this, indent + " | ");
-}
-
-
-void
-PrintVisitor::printData(const vector<uint8_t>& blob, const string& indent, int offset)
-{
- cout << indent;
-
- int count = 0;
- for(int i = offset; i < blob.size(); i++)
- {
- cout << " " << hex << setw(2) << setfill('0') << (int)blob[i];
- count++;
- if(8 == count)
- {
- count = 0;
- cout << "\n" << indent;
- }
- }
- cout << endl;
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/print-visitor.hpp b/src/encoding/der/visitor/print-visitor.hpp
deleted file mode 100644
index 45d0bfd..0000000
--- a/src/encoding/der/visitor/print-visitor.hpp
+++ /dev/null
@@ -1,40 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_PRINT_VISITOR_HPP
-#define NDN_PRINT_VISITOR_HPP
-
-#include <vector>
-#include <string>
-#include "void-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class PrintVisitor : public VoidVisitor {
-public:
- virtual void visit(DerBool&, ndnboost::any);
- virtual void visit(DerInteger&, ndnboost::any);
- virtual void visit(DerPrintableString&, ndnboost::any);
- virtual void visit(DerBitString&, ndnboost::any);
- virtual void visit(DerNull&, ndnboost::any);
- virtual void visit(DerOctetString&, ndnboost::any);
- virtual void visit(DerOid&, ndnboost::any);
- virtual void visit(DerSequence&, ndnboost::any);
- virtual void visit(DerGtime&, ndnboost::any);
-
-private:
- static void printData(const std::vector<uint8_t>& blob, const std::string& indent, int offset = 0);
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/public-key-visitor.cpp b/src/encoding/der/visitor/public-key-visitor.cpp
deleted file mode 100644
index a5f5542..0000000
--- a/src/encoding/der/visitor/public-key-visitor.cpp
+++ /dev/null
@@ -1,34 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "simple-visitor.hpp"
-#include <ndn-cpp/security/certificate/public-key.hpp>
-#include "../der.hpp"
-#include "public-key-visitor.hpp"
-
-using namespace std;
-
-namespace ndn {
-
-namespace der {
-
-ndnboost::any
-PublicKeyVisitor::visit(DerSequence& derSeq)
-{
- DerNodePtrList& children = derSeq.getChildren();
-
- SimpleVisitor simpleVisitor;
- ptr_lib::shared_ptr<DerSequence> algoSeq = ptr_lib::dynamic_pointer_cast<DerSequence>(children[0]);
- OID algorithm = ndnboost::any_cast<OID>(algoSeq->getChildren()[0]->accept(simpleVisitor));
- Blob raw = derSeq.getRaw();
- return ndnboost::any(ptr_lib::shared_ptr<PublicKey>(new PublicKey(algorithm, raw)));
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/public-key-visitor.hpp b/src/encoding/der/visitor/public-key-visitor.hpp
deleted file mode 100644
index 54dfb66..0000000
--- a/src/encoding/der/visitor/public-key-visitor.hpp
+++ /dev/null
@@ -1,27 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_PUBLIC_KEY_VISITOR_HPP
-#define NDN_PUBLIC_KEY_VISITOR_HPP
-
-#include "no-arguments-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class PublicKeyVisitor : public NoArgumentsVisitor {
- public:
- virtual ndnboost::any visit(DerSequence& );
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/simple-visitor.cpp b/src/encoding/der/visitor/simple-visitor.cpp
deleted file mode 100644
index c56f999..0000000
--- a/src/encoding/der/visitor/simple-visitor.cpp
+++ /dev/null
@@ -1,99 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "simple-visitor.hpp"
-#include "../der.hpp"
-#include <ndn-cpp/encoding/oid.hpp>
-
-using namespace std;
-
-namespace ndn {
-
-namespace der
-{
-
-ndnboost::any
-SimpleVisitor::visit(DerBool& derBool)
-{
- bool result = true;
-
- if(0 == derBool.getPayload()[0])
- result = false;
-
- return ndnboost::any(result);
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerInteger& derInteger)
-{
- return ndnboost::any(derInteger.getPayload());
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerPrintableString& derPStr)
-{
- return ndnboost::any(string((const char*)&derPStr.getPayload()[0], derPStr.getPayload().size()));
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerBitString& derBStr)
-{
- return ndnboost::any(derBStr.getPayload());
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerNull& derNull)
-{
- return ndnboost::any();
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerOctetString& derOStr)
-{
- vector<uint8_t> result(derOStr.getPayload());
- return ndnboost::any(result);
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerOid& derOid)
-{
- vector<int> intList;
- int offset = 0;
-
- vector<uint8_t>& blob = derOid.getPayload();
-
- int first = blob[offset];
-
- intList.push_back(first / 40);
- intList.push_back(first % 40);
-
- offset++;
-
- while(offset < blob.size()){
- intList.push_back(derOid.decode128(offset));
- }
-
- return ndnboost::any(OID(intList));
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerSequence& derSeq)
-{
- return ndnboost::any();
-}
-
-ndnboost::any
-SimpleVisitor::visit(DerGtime& derGtime)
-{
- string str((const char*)&derGtime.getPayload()[0], derGtime.getPayload().size());
- return ndnboost::any(DerGtime::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)));
-}
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/simple-visitor.hpp b/src/encoding/der/visitor/simple-visitor.hpp
deleted file mode 100644
index 3f0584e..0000000
--- a/src/encoding/der/visitor/simple-visitor.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_SIMPLE_VISITOR_HPP
-#define NDN_DER_SIMPLE_VISITOR_HPP
-
-#include "no-arguments-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class SimpleVisitor : public NoArgumentsVisitor
-{
-public:
- virtual ndnboost::any visit(DerBool& );
- virtual ndnboost::any visit(DerInteger& );
- virtual ndnboost::any visit(DerPrintableString&);
- virtual ndnboost::any visit(DerBitString& );
- virtual ndnboost::any visit(DerNull& );
- virtual ndnboost::any visit(DerOctetString& );
- virtual ndnboost::any visit(DerOid& );
- virtual ndnboost::any visit(DerSequence& );
- virtual ndnboost::any visit(DerGtime& );
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/visitor.hpp b/src/encoding/der/visitor/visitor.hpp
deleted file mode 100644
index 06c469f..0000000
--- a/src/encoding/der/visitor/visitor.hpp
+++ /dev/null
@@ -1,54 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_VISITOR_HPP
-#define NDN_DER_VISITOR_HPP
-
-#include <ndn-cpp/common.hpp>
-
-#if NDN_CPP_USE_SYSTEM_BOOST
-#include <boost/any.hpp>
-namespace ndnboost = boost;
-#else
-// We can use ndnboost::any because this is an internal header and will not conflict with the application if it uses boost::any.
-#include <ndnboost/any.hpp>
-#endif
-
-namespace ndn {
-
-namespace der {
-
-class DerBool;
-class DerInteger;
-class DerPrintableString;
-class DerBitString;
-class DerNull;
-class DerOctetString;
-class DerOid;
-class DerSequence;
-class DerGtime;
-
-class Visitor
-{
-public:
- virtual ndnboost::any visit(DerBool&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerInteger&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerPrintableString&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerBitString&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerNull&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerOctetString&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerOid&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerSequence&, ndnboost::any) = 0;
- virtual ndnboost::any visit(DerGtime&, ndnboost::any) = 0;
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/void-no-arguments-visitor.hpp b/src/encoding/der/visitor/void-no-arguments-visitor.hpp
deleted file mode 100644
index 7e60d38..0000000
--- a/src/encoding/der/visitor/void-no-arguments-visitor.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_VOID_NO_ARGUMENTS_VISITOR_HPP
-#define NDN_DER_VOID_NO_ARGUMENTS_VISITOR_HPP
-
-#include "visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class VoidNoArgumentsVisitor
-{
-public:
- virtual void visit(DerBool& ) = 0;
- virtual void visit(DerInteger& ) = 0;
- virtual void visit(DerPrintableString&) = 0;
- virtual void visit(DerBitString& ) = 0;
- virtual void visit(DerNull& ) = 0;
- virtual void visit(DerOctetString& ) = 0;
- virtual void visit(DerOid& ) = 0;
- virtual void visit(DerSequence& ) = 0;
- virtual void visit(DerGtime& ) = 0;
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/der/visitor/void-visitor.cpp b/src/encoding/der/visitor/void-visitor.cpp
deleted file mode 100644
index dd8217a..0000000
--- a/src/encoding/der/visitor/void-visitor.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#include "void-visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-void
-VoidVisitor::visit(DerBool& derBool, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerInteger& derInteger, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerPrintableString& derPStr, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerBitString& derBStr, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerNull& derNull, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerOctetString& derOStr, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerOid& derOid, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerSequence& derSequence, ndnboost::any)
-{}
-
-void
-VoidVisitor::visit(DerGtime& derGtime, ndnboost::any)
-{}
-
-} // der
-
-}
diff --git a/src/encoding/der/visitor/void-visitor.hpp b/src/encoding/der/visitor/void-visitor.hpp
deleted file mode 100644
index e853ee6..0000000
--- a/src/encoding/der/visitor/void-visitor.hpp
+++ /dev/null
@@ -1,36 +0,0 @@
-/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
-/**
- * Copyright (C) 2013 Regents of the University of California.
- * @author: Yingdi Yu <yingdi@cs.ucla.edu>
- * @author: Jeff Thompson <jefft0@remap.ucla.edu>
- * See COPYING for copyright and distribution information.
- */
-
-#ifndef NDN_DER_VOID_VISITOR_HPP
-#define NDN_DER_VOID_VISITOR_HPP
-
-#include "visitor.hpp"
-
-namespace ndn {
-
-namespace der {
-
-class VoidVisitor
-{
-public:
- virtual void visit(DerBool&, ndnboost::any);
- virtual void visit(DerInteger&, ndnboost::any);
- virtual void visit(DerPrintableString&, ndnboost::any);
- virtual void visit(DerBitString&, ndnboost::any);
- virtual void visit(DerNull&, ndnboost::any);
- virtual void visit(DerOctetString&, ndnboost::any);
- virtual void visit(DerOid&, ndnboost::any);
- virtual void visit(DerSequence&, ndnboost::any);
- virtual void visit(DerGtime&, ndnboost::any);
-};
-
-} // der
-
-}
-
-#endif
diff --git a/src/encoding/oid.cpp b/src/encoding/oid.cpp
index 633d1cf..2db72f7 100644
--- a/src/encoding/oid.cpp
+++ b/src/encoding/oid.cpp
@@ -9,8 +9,10 @@
#include <sstream>
#include <ndn-cpp/encoding/oid.hpp>
+#include <cryptopp/asn.h>
using namespace std;
+using namespace CryptoPP;
namespace ndn {
@@ -48,7 +50,8 @@
return convert.str();
}
-bool OID::equal(const OID& oid) const
+bool
+OID::equal(const OID& oid) const
{
vector<int>::const_iterator i = oid_.begin();
vector<int>::const_iterator j = oid.oid_.begin();
@@ -64,4 +67,75 @@
return false;
}
+inline void
+EncodeValue(BufferedTransformation &bt, word32 v)
+{
+ for (unsigned int i=RoundUpToMultipleOf(STDMAX(7U,BitPrecision(v)), 7U)-7; i != 0; i-=7)
+ bt.Put((byte)(0x80 | ((v >> i) & 0x7f)));
+ bt.Put((byte)(v & 0x7f));
+}
+
+inline size_t
+DecodeValue(BufferedTransformation &bt, word32 &v)
+{
+ byte b;
+ size_t i=0;
+ v = 0;
+ while (true)
+ {
+ if (!bt.Get(b))
+ BERDecodeError();
+ i++;
+ if (v >> (8*sizeof(v)-7)) // v about to overflow
+ BERDecodeError();
+ v <<= 7;
+ v += b & 0x7f;
+ if (!(b & 0x80))
+ return i;
+ }
+}
+
+void
+OID::encode(CryptoPP::BufferedTransformation &out) const
+{
+ assert(oid_.size() >= 2);
+ ByteQueue temp;
+ temp.Put(byte(oid_[0] * 40 + oid_[1]));
+ for (size_t i=2; i<oid_.size(); i++)
+ EncodeValue(temp, oid_[i]);
+ out.Put(OBJECT_IDENTIFIER);
+ DERLengthEncode(out, temp.CurrentSize());
+ temp.TransferTo(out);
+}
+
+void
+OID::decode(CryptoPP::BufferedTransformation &in)
+{
+ byte b;
+ if (!in.Get(b) || b != OBJECT_IDENTIFIER)
+ BERDecodeError();
+
+ size_t length;
+ if (!BERLengthDecode(in, length) || length < 1)
+ BERDecodeError();
+
+ if (!in.Get(b))
+ BERDecodeError();
+
+ length--;
+ oid_.resize(2);
+ oid_[0] = b / 40;
+ oid_[1] = b % 40;
+
+ while (length > 0)
+ {
+ word32 v;
+ size_t valueLen = DecodeValue(in, v);
+ if (valueLen > length)
+ BERDecodeError();
+ oid_.push_back(v);
+ length -= valueLen;
+ }
+}
+
}