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 | |
Yingdi Yu | 61ec272 | 2014-01-20 14:22:32 -0800 | [diff] [blame] | 9 | #include <ndn-cpp-dev/ndn-cpp-config.h> |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 10 | #include "asn_ext.hpp" |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 11 | #include "../../util/time.hpp" |
| 12 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 13 | #include <boost/format.hpp> |
| 14 | #include <boost/lexical_cast.hpp> |
| 15 | |
| 16 | using namespace CryptoPP; |
| 17 | |
| 18 | namespace ndn { |
| 19 | |
| 20 | size_t |
| 21 | DEREncodeGeneralTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 time) |
| 22 | { |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 23 | if (time < 0) |
| 24 | throw Asn::Error("Calendar time value out of range"); |
| 25 | else if (time > 2e14) |
| 26 | // 2e14 is about the year 8300. We don't want to go over a 4-digit year. |
| 27 | throw Asn::Error("Calendar time value out of range"); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 28 | |
Alexander Afanasyev | 1e0a077 | 2014-01-28 20:07:07 -0800 | [diff] [blame] | 29 | |
| 30 | boost::posix_time::ptime boostTime = |
| 31 | UNIX_EPOCH_TIME + boost::posix_time::milliseconds(time); |
| 32 | |
| 33 | const boost::format f = boost::format("%04d%02d%02d%02d%02d%02dZ") |
| 34 | % boostTime.date().year_month_day().year |
| 35 | % boostTime.date().year_month_day().month.as_number() |
| 36 | % boostTime.date().year_month_day().day.as_number() |
| 37 | % boostTime.time_of_day().hours() |
| 38 | % boostTime.time_of_day().minutes() |
| 39 | % boostTime.time_of_day().seconds() |
| 40 | ; |
| 41 | |
| 42 | std::string asn1time = f.str(); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 43 | |
| 44 | bt.Put(GENERALIZED_TIME); |
| 45 | size_t lengthBytes = DERLengthEncode(bt, asn1time.size()); |
| 46 | bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size()); |
| 47 | return 1+lengthBytes+asn1time.size(); |
| 48 | } |
| 49 | |
| 50 | void |
| 51 | BERDecodeTime(CryptoPP::BufferedTransformation &bt, MillisecondsSince1970 &time) |
| 52 | { |
| 53 | byte b; |
| 54 | if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME)) |
| 55 | BERDecodeError(); |
| 56 | |
| 57 | size_t bc; |
| 58 | if (!BERLengthDecode(bt, bc)) |
| 59 | BERDecodeError(); |
| 60 | |
| 61 | SecByteBlock time_str(bc); |
| 62 | if (bc != bt.Get(time_str, bc)) |
| 63 | BERDecodeError(); |
| 64 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 65 | std::string str; |
| 66 | str.assign (time_str.begin(), time_str.end()); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 67 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 68 | if (b == UTC_TIME) { |
| 69 | if (boost::lexical_cast<int>(str.substr(0,2)) < 50) |
| 70 | str = "20" + str; |
| 71 | else |
| 72 | str = "19" + str; |
| 73 | } |
| 74 | |
| 75 | time = fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | } // namespace ndn |