blob: 96010c410eab87ee080fef851990e55ee6e28744 [file] [log] [blame]
Alexander Afanasyevc169a812014-05-20 20:37:29 -04001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -08002/**
Alexander Afanasyevc169a812014-05-20 20:37:29 -04003 * Copyright (c) 2013-2014 Regents of the University of California.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07006 *
Alexander Afanasyevc169a812014-05-20 20:37:29 -04007 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -070020 *
21 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
22 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080023 */
24
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080025#include "asn_ext.hpp"
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080026#include "../../util/time.hpp"
27
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080028#include <boost/format.hpp>
29#include <boost/lexical_cast.hpp>
30
31using namespace CryptoPP;
32
33namespace ndn {
34
35size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
37 const time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080038{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070039 std::string str = time::toIsoString(time);
40 // For example, 20131226T232254
41 // 20131226T232254.100000
42 BOOST_ASSERT(str.size() >= 15);
43 std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z";
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080044
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080045 bt.Put(GENERALIZED_TIME);
46 size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
47 bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
48 return 1+lengthBytes+asn1time.size();
49}
50
51void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070052BERDecodeTime(CryptoPP::BufferedTransformation& bt,
53 time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080054{
55 byte b;
56 if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
57 BERDecodeError();
58
59 size_t bc;
60 if (!BERLengthDecode(bt, bc))
61 BERDecodeError();
62
63 SecByteBlock time_str(bc);
64 if (bc != bt.Get(time_str, bc))
65 BERDecodeError();
66
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080067 std::string str;
68 str.assign (time_str.begin(), time_str.end());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070069
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080070 if (b == UTC_TIME) {
71 if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
72 str = "20" + str;
73 else
74 str = "19" + str;
75 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070076
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070077 time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080078}
79
80} // namespace ndn