blob: 052226afc3383341ee4d5e7d0fcdadc4d43df51f [file] [log] [blame]
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -08001/**
2 * Copyright (C) 2013 Regents of the University of California.
3 * @author: Jeff Thompson <jefft0@remap.ucla.edu>
4 * @author: Yingdi Yu <yingdi@cs.ucla.edu>
5 * @author: Alexander Afanasyev <alexander.afanasyev@ucla.edu>
6 * See COPYING for copyright and distribution information.
7 */
8
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -08009#include "asn_ext.hpp"
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080010#include "../../util/time.hpp"
11
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080012#include <boost/format.hpp>
13#include <boost/lexical_cast.hpp>
14
15using namespace CryptoPP;
16
17namespace ndn {
18
19size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070020DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
21 const time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080022{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070023 std::string str = time::toIsoString(time);
24 // For example, 20131226T232254
25 // 20131226T232254.100000
26 BOOST_ASSERT(str.size() >= 15);
27 std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z";
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080028
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080029 bt.Put(GENERALIZED_TIME);
30 size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
31 bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
32 return 1+lengthBytes+asn1time.size();
33}
34
35void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070036BERDecodeTime(CryptoPP::BufferedTransformation& bt,
37 time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080038{
39 byte b;
40 if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
41 BERDecodeError();
42
43 size_t bc;
44 if (!BERLengthDecode(bt, bc))
45 BERDecodeError();
46
47 SecByteBlock time_str(bc);
48 if (bc != bt.Get(time_str, bc))
49 BERDecodeError();
50
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080051 std::string str;
52 str.assign (time_str.begin(), time_str.end());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070053
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080054 if (b == UTC_TIME) {
55 if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
56 str = "20" + str;
57 else
58 str = "19" + str;
59 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070061 time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080062}
63
64} // namespace ndn