All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
asn_ext.cpp
Go to the documentation of this file.
1 
9 #include "asn_ext.hpp"
10 #include "../../util/time.hpp"
11 
12 #include <boost/format.hpp>
13 #include <boost/lexical_cast.hpp>
14 
15 using namespace CryptoPP;
16 
17 namespace ndn {
18 
19 size_t
20 DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
22 {
23  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";
28 
29  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 
35 void
36 BERDecodeTime(CryptoPP::BufferedTransformation& bt,
38 {
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 
51  std::string str;
52  str.assign (time_str.begin(), time_str.end());
53 
54  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  }
60 
61  time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
62 }
63 
64 } // namespace ndn
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.hpp:163
size_t DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, const time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:20
void BERDecodeTime(CryptoPP::BufferedTransformation &bt, time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:36
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.hpp:135
time_point TimePoint
Definition: time.hpp:55