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 | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 20 | DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt, |
| 21 | const time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 22 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 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"; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 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 |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | BERDecodeTime(CryptoPP::BufferedTransformation& bt, |
| 37 | time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 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 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 51 | std::string str; |
| 52 | str.assign (time_str.begin(), time_str.end()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 53 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 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 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 61 | time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | } // namespace ndn |