blob: 7741314396f8b93915fd4c612eeeafb8dca9a2a2 [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 Afanasyevaa0e7da2014-03-17 14:37:33 -070020DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, const time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080021{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070022 std::string str = time::toIsoString(time);
23 // For example, 20131226T232254
24 // 20131226T232254.100000
25 BOOST_ASSERT(str.size() >= 15);
26 std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z";
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080027
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080028 bt.Put(GENERALIZED_TIME);
29 size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
30 bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
31 return 1+lengthBytes+asn1time.size();
32}
33
34void
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070035BERDecodeTime(CryptoPP::BufferedTransformation &bt, time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080036{
37 byte b;
38 if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
39 BERDecodeError();
40
41 size_t bc;
42 if (!BERLengthDecode(bt, bc))
43 BERDecodeError();
44
45 SecByteBlock time_str(bc);
46 if (bc != bt.Get(time_str, bc))
47 BERDecodeError();
48
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080049 std::string str;
50 str.assign (time_str.begin(), time_str.end());
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080051
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080052 if (b == UTC_TIME) {
53 if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
54 str = "20" + str;
55 else
56 str = "19" + str;
57 }
58
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070059 time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080060}
61
62} // namespace ndn