blob: 96010c410eab87ee080fef851990e55ee6e28744 [file] [log] [blame]
/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
/**
* Copyright (c) 2013-2014 Regents of the University of California.
*
* This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
*
* ndn-cxx library is free software: you can redistribute it and/or modify it under the
* terms of the GNU Lesser General Public License as published by the Free Software
* Foundation, either version 3 of the License, or (at your option) any later version.
*
* ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
* PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
*
* You should have received copies of the GNU General Public License and GNU Lesser
* General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
* <http://www.gnu.org/licenses/>.
*
* See AUTHORS.md for complete list of ndn-cxx authors and contributors.
*
* @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
* @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
*/
#include "asn_ext.hpp"
#include "../../util/time.hpp"
#include <boost/format.hpp>
#include <boost/lexical_cast.hpp>
using namespace CryptoPP;
namespace ndn {
size_t
DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
const time::system_clock::TimePoint& time)
{
std::string str = time::toIsoString(time);
// For example, 20131226T232254
// 20131226T232254.100000
BOOST_ASSERT(str.size() >= 15);
std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z";
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,
time::system_clock::TimePoint& 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::string str;
str.assign (time_str.begin(), time_str.end());
if (b == UTC_TIME) {
if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
str = "20" + str;
else
str = "19" + str;
}
time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
}
} // namespace ndn