Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 1 | /** |
| 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 Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 9 | #include "asn_ext.hpp" |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 10 | #include "../../util/time.hpp" |
| 11 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 12 | #include <boost/format.hpp> |
| 13 | #include <boost/lexical_cast.hpp> |
| 14 | |
| 15 | using namespace CryptoPP; |
| 16 | |
| 17 | namespace ndn { |
| 18 | |
| 19 | size_t |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 20 | DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, const time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 21 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 22 | 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 Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 27 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 28 | 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 | |
| 34 | void |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 35 | BERDecodeTime(CryptoPP::BufferedTransformation &bt, time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 36 | { |
| 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 Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 49 | std::string str; |
| 50 | str.assign (time_str.begin(), time_str.end()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 51 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 52 | 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 Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 59 | time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | } // namespace ndn |