blob: 6698426ac9a2c21bc118837b08a2d1a2f59f418c [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
9#include <ndn-cpp/ndn-cpp-config.h>
10#include "asn_ext.hpp"
11
12#if NDN_CPP_HAVE_TIME_H
13#include <time.h>
14#endif
15#if NDN_CPP_HAVE_SYS_TIME_H
16#include <sys/time.h>
17#endif
18
19#include <boost/format.hpp>
20#include <boost/lexical_cast.hpp>
21
22using namespace CryptoPP;
23
24namespace ndn {
25
26size_t
27DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 time)
28{
29#ifndef NDN_CPP_HAVE_GMTIME_SUPPORT
30 throw Asn::Error("Time functions are not supported by the standard library");
31#endif
32
33 if (time < 0)
34 throw Asn::Error("Calendar time value out of range");
35 else if (time > 2e14)
36 // 2e14 is about the year 8300. We don't want to go over a 4-digit year.
37 throw Asn::Error("Calendar time value out of range");
38
39 time_t secondsSince1970 = time / 1000;
40 struct tm* gmt = gmtime(&secondsSince1970);
41
42 std::string asn1time ((boost::format("%04d%02d%02d%02d%02d%02d%sZ")
43 % (1900 + gmt->tm_year)
44 % (gmt->tm_mon + 1)
45 % gmt->tm_mday
46 % gmt->tm_hour
47 % gmt->tm_min
48 % gmt->tm_sec).str());
49 // = os.str();
50
51 bt.Put(GENERALIZED_TIME);
52 size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
53 bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
54 return 1+lengthBytes+asn1time.size();
55}
56
57void
58BERDecodeTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 &time)
59{
60 byte b;
61 if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
62 BERDecodeError();
63
64 size_t bc;
65 if (!BERLengthDecode(bt, bc))
66 BERDecodeError();
67
68 SecByteBlock time_str(bc);
69 if (bc != bt.Get(time_str, bc))
70 BERDecodeError();
71
72 std::vector<std::string> params;
73 std::string current;
74 std::locale cLocale("C");
75
76 for(uint32_t j = 0; j != time_str.size(); ++j)
77 {
78 if(std::isdigit(reinterpret_cast<char&>(time_str[j]), cLocale))
79 current += time_str[j];
80 else
81 {
82 if(current != "")
83 params.push_back(current);
84 current.clear();
85 }
86 }
87 if(current != "")
88 params.push_back(current);
89
90 if(params.size() < 3 || params.size() > 6)
91 throw Asn::Error("Invalid time specification " + std::string(time_str.begin(), time_str.end()));
92
93 struct tm gmt;
94 gmt.tm_year = boost::lexical_cast<int>(params[0]);
95 gmt.tm_mon = boost::lexical_cast<int>(params[1]) - 1;
96 gmt.tm_mday = boost::lexical_cast<int>(params[2]);
97 gmt.tm_hour = (params.size() >= 4) ? boost::lexical_cast<int>(params[3]) : 0;
98 gmt.tm_min = (params.size() >= 5) ? boost::lexical_cast<int>(params[4]) : 0;
99 gmt.tm_sec = (params.size() == 6) ? boost::lexical_cast<int>(params[5]) : 0;
100
101 if (b == GENERALIZED_TIME)
102 gmt.tm_year -= 1900;
103
104 time = timegm(&gmt) * 1000;
105}
106
107} // namespace ndn