Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil -*- */ |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 2 | /** |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame^] | 3 | * 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 Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 14 | */ |
| 15 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 16 | #include "asn_ext.hpp" |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 17 | #include "../../util/time.hpp" |
| 18 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 19 | #include <boost/format.hpp> |
| 20 | #include <boost/lexical_cast.hpp> |
| 21 | |
| 22 | using namespace CryptoPP; |
| 23 | |
| 24 | namespace ndn { |
| 25 | |
| 26 | size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 27 | DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt, |
| 28 | const time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 29 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 30 | 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 Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 35 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 36 | 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 | |
| 42 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 43 | BERDecodeTime(CryptoPP::BufferedTransformation& bt, |
| 44 | time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 45 | { |
| 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 Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 58 | std::string str; |
| 59 | str.assign (time_str.begin(), time_str.end()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 60 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 61 | 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 Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 67 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 68 | time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | } // namespace ndn |