blob: 8a020dfbbccaa9f8fbd41e2899fc8d70ae142e17 [file] [log] [blame]
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -08002/**
Alexander Afanasyevdfa52c42014-04-24 21:10:11 -07003 * Copyright (c) 2013-2014, Regents of the University of California.
4 * All rights reserved.
5 *
6 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
7 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
8 *
9 * This file licensed under New BSD License. See COPYING for detailed information about
10 * ndn-cxx library copyright, permissions, and redistribution restrictions.
11 *
12 * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html>
13 * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/>
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080014 */
15
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080016#include "asn_ext.hpp"
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080017#include "../../util/time.hpp"
18
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080019#include <boost/format.hpp>
20#include <boost/lexical_cast.hpp>
21
22using namespace CryptoPP;
23
24namespace ndn {
25
26size_t
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070027DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt,
28 const time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080029{
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070030 std::string str = time::toIsoString(time);
31 // For example, 20131226T232254
32 // 20131226T232254.100000
33 BOOST_ASSERT(str.size() >= 15);
34 std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z";
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080035
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080036 bt.Put(GENERALIZED_TIME);
37 size_t lengthBytes = DERLengthEncode(bt, asn1time.size());
38 bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size());
39 return 1+lengthBytes+asn1time.size();
40}
41
42void
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070043BERDecodeTime(CryptoPP::BufferedTransformation& bt,
44 time::system_clock::TimePoint& time)
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080045{
46 byte b;
47 if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME))
48 BERDecodeError();
49
50 size_t bc;
51 if (!BERLengthDecode(bt, bc))
52 BERDecodeError();
53
54 SecByteBlock time_str(bc);
55 if (bc != bt.Get(time_str, bc))
56 BERDecodeError();
57
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080058 std::string str;
59 str.assign (time_str.begin(), time_str.end());
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070060
Alexander Afanasyev049f8f72013-12-26 19:07:15 -080061 if (b == UTC_TIME) {
62 if (boost::lexical_cast<int>(str.substr(0,2)) < 50)
63 str = "20" + str;
64 else
65 str = "19" + str;
66 }
Alexander Afanasyevfdbfc6d2014-04-14 15:12:11 -070067
Alexander Afanasyevaa0e7da2014-03-17 14:37:33 -070068 time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6));
Alexander Afanasyev0ea6e082013-12-26 15:16:37 -080069}
70
71} // namespace ndn