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