asn_ext.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
25 #include "asn_ext.hpp"
26 #include "../../util/time.hpp"
27 
28 #include <boost/format.hpp>
29 #include <boost/lexical_cast.hpp>
30 
31 using namespace CryptoPP;
32 
33 namespace ndn {
34 
35 size_t
36 DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
38 {
39  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";
44 
45  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 
51 void
52 BERDecodeTime(CryptoPP::BufferedTransformation& bt,
54 {
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 
67  std::string str;
68  str.assign (time_str.begin(), time_str.end());
69 
70  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  }
76 
77  time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
78 }
79 
80 } // namespace ndn
Copyright (c) 2013-2017 Regents of the University of California.
Definition: common.hpp:75
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:152
Copyright (c) 2013-2017 Regents of the University of California.
Definition: oid.hpp:29
size_t DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, const time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:36
void BERDecodeTime(CryptoPP::BufferedTransformation &bt, time::system_clock::TimePoint &time)
Definition: asn_ext.cpp:52
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:131
time_point TimePoint
Definition: time.hpp:90