Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [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 | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 3 | * Copyright (c) 2013-2014 Regents of the University of California. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 6 | * |
Alexander Afanasyev | c169a81 | 2014-05-20 20:37:29 -0400 | [diff] [blame] | 7 | * ndn-cxx library is free software: you can redistribute it and/or modify it under the |
| 8 | * terms of the GNU Lesser General Public License as published by the Free Software |
| 9 | * Foundation, either version 3 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY |
| 12 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A |
| 13 | * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details. |
| 14 | * |
| 15 | * You should have received copies of the GNU General Public License and GNU Lesser |
| 16 | * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see |
| 17 | * <http://www.gnu.org/licenses/>. |
| 18 | * |
| 19 | * See AUTHORS.md for complete list of ndn-cxx authors and contributors. |
Alexander Afanasyev | dfa52c4 | 2014-04-24 21:10:11 -0700 | [diff] [blame] | 20 | * |
| 21 | * @author Alexander Afanasyev <http://lasr.cs.ucla.edu/afanasyev/index.html> |
| 22 | * @author Yingdi Yu <http://irl.cs.ucla.edu/~yingdi/> |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 23 | */ |
| 24 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 25 | #include "asn_ext.hpp" |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 26 | #include "../../util/time.hpp" |
| 27 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 28 | #include <boost/format.hpp> |
| 29 | #include <boost/lexical_cast.hpp> |
| 30 | |
| 31 | using namespace CryptoPP; |
| 32 | |
| 33 | namespace ndn { |
| 34 | |
| 35 | size_t |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 36 | DEREncodeGeneralTime(CryptoPP::BufferedTransformation& bt, |
| 37 | const time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 38 | { |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 39 | std::string str = time::toIsoString(time); |
| 40 | // For example, 20131226T232254 |
| 41 | // 20131226T232254.100000 |
| 42 | BOOST_ASSERT(str.size() >= 15); |
| 43 | std::string asn1time = str.substr(0, 8) + str.substr(9,6) + "Z"; |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 44 | |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 45 | bt.Put(GENERALIZED_TIME); |
| 46 | size_t lengthBytes = DERLengthEncode(bt, asn1time.size()); |
| 47 | bt.Put(reinterpret_cast<const uint8_t*>(asn1time.c_str()), asn1time.size()); |
| 48 | return 1+lengthBytes+asn1time.size(); |
| 49 | } |
| 50 | |
| 51 | void |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 52 | BERDecodeTime(CryptoPP::BufferedTransformation& bt, |
| 53 | time::system_clock::TimePoint& time) |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 54 | { |
| 55 | byte b; |
| 56 | if (!bt.Get(b) || (b != GENERALIZED_TIME && b != UTC_TIME)) |
| 57 | BERDecodeError(); |
| 58 | |
| 59 | size_t bc; |
| 60 | if (!BERLengthDecode(bt, bc)) |
| 61 | BERDecodeError(); |
| 62 | |
| 63 | SecByteBlock time_str(bc); |
| 64 | if (bc != bt.Get(time_str, bc)) |
| 65 | BERDecodeError(); |
| 66 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 67 | std::string str; |
| 68 | str.assign (time_str.begin(), time_str.end()); |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 69 | |
Alexander Afanasyev | 049f8f7 | 2013-12-26 19:07:15 -0800 | [diff] [blame] | 70 | if (b == UTC_TIME) { |
| 71 | if (boost::lexical_cast<int>(str.substr(0,2)) < 50) |
| 72 | str = "20" + str; |
| 73 | else |
| 74 | str = "19" + str; |
| 75 | } |
Alexander Afanasyev | fdbfc6d | 2014-04-14 15:12:11 -0700 | [diff] [blame] | 76 | |
Alexander Afanasyev | aa0e7da | 2014-03-17 14:37:33 -0700 | [diff] [blame] | 77 | time = time::fromIsoString(str.substr(0, 8) + "T" + str.substr(8, 6)); |
Alexander Afanasyev | 0ea6e08 | 2013-12-26 15:16:37 -0800 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | } // namespace ndn |