Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 2 | /* |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 3 | * Copyright (c) 2013-2018 Regents of the University of California. |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 4 | * |
| 5 | * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions). |
| 6 | * |
| 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. |
| 20 | */ |
| 21 | |
| 22 | #ifndef NDN_SECURITY_VALIDITY_PERIOD_HPP |
| 23 | #define NDN_SECURITY_VALIDITY_PERIOD_HPP |
| 24 | |
| 25 | #include "../common.hpp" |
| 26 | #include "../encoding/tlv.hpp" |
| 27 | #include "../encoding/block.hpp" |
| 28 | #include "../util/time.hpp" |
| 29 | |
| 30 | namespace ndn { |
| 31 | namespace security { |
| 32 | |
| 33 | |
| 34 | /** @brief Abstraction of validity period |
Junxiao Shi | 426d500 | 2018-08-23 10:52:29 +0000 | [diff] [blame] | 35 | * @sa docs/specs/certificate-format.rst |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 36 | */ |
| 37 | class ValidityPeriod |
| 38 | { |
| 39 | public: |
| 40 | class Error : public tlv::Error |
| 41 | { |
| 42 | public: |
Junxiao Shi | 68b5385 | 2018-07-25 13:56:38 -0600 | [diff] [blame] | 43 | using tlv::Error::Error; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | public: |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 47 | /** @brief Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 48 | */ |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 49 | ValidityPeriod(); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 50 | |
| 51 | /** @brief Create validity period from @p block |
| 52 | */ |
| 53 | explicit |
| 54 | ValidityPeriod(const Block& block); |
| 55 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 56 | /** @brief Create validity period [@p notBefore, @p notAfter] |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 57 | * @param notBefore exclusive beginning of the validity period range |
| 58 | * @param notAfter exclusive end of the validity period range |
| 59 | * |
| 60 | * @note The supplied time points will be rounded up to the whole seconds: |
| 61 | * - @p notBefore is rounded up the next whole second |
| 62 | * - @p notAfter is truncated to the previous whole second |
| 63 | */ |
| 64 | ValidityPeriod(const time::system_clock::TimePoint& notBefore, |
| 65 | const time::system_clock::TimePoint& notAfter); |
| 66 | |
| 67 | /** @brief Check if @p now falls within the validity period |
| 68 | * @param now Time point to check if it falls within the period |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 69 | * @return periodBegin <= @p now and @p now <= periodEnd |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 70 | */ |
| 71 | bool |
| 72 | isValid(const time::system_clock::TimePoint& now = time::system_clock::now()) const; |
| 73 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 74 | /** @brief Set validity period [@p notBefore, @p notAfter] |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 75 | * @param notBefore exclusive beginning of the validity period range |
| 76 | * @param notAfter exclusive end of the validity period range |
| 77 | * |
| 78 | * @note The supplied time points will be rounded up to the whole seconds: |
| 79 | * - @p notBefore is rounded up the next whole second |
| 80 | * - @p notAfter is truncated to the previous whole second |
| 81 | */ |
| 82 | ValidityPeriod& |
| 83 | setPeriod(const time::system_clock::TimePoint& notBefore, |
| 84 | const time::system_clock::TimePoint& notAfter); |
| 85 | |
| 86 | /** @brief Get the stored validity period |
| 87 | */ |
| 88 | std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint> |
| 89 | getPeriod() const; |
| 90 | |
| 91 | /** @brief Fast encoding or block size estimation |
| 92 | */ |
| 93 | template<encoding::Tag TAG> |
| 94 | size_t |
| 95 | wireEncode(EncodingImpl<TAG>& encoder) const; |
| 96 | |
| 97 | /** @brief Encode ValidityPeriod into TLV block |
| 98 | */ |
| 99 | const Block& |
| 100 | wireEncode() const; |
| 101 | |
| 102 | /** @brief Decode ValidityPeriod from TLV block |
| 103 | * @throw Error when an invalid TLV block supplied |
| 104 | */ |
| 105 | void |
| 106 | wireDecode(const Block& wire); |
| 107 | |
| 108 | public: // EqualityComparable concept |
| 109 | bool |
| 110 | operator==(const ValidityPeriod& other) const; |
| 111 | |
| 112 | bool |
| 113 | operator!=(const ValidityPeriod& other) const; |
| 114 | |
| 115 | private: |
| 116 | typedef boost::chrono::time_point<time::system_clock, time::seconds> TimePoint; |
| 117 | |
| 118 | TimePoint m_notBefore; |
| 119 | TimePoint m_notAfter; |
| 120 | |
| 121 | mutable Block m_wire; |
| 122 | }; |
| 123 | |
Davide Pesavento | 88a0d81 | 2017-08-19 21:31:42 -0400 | [diff] [blame] | 124 | NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ValidityPeriod); |
| 125 | |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 126 | std::ostream& |
| 127 | operator<<(std::ostream& os, const ValidityPeriod& period); |
| 128 | |
| 129 | } // namespace security |
| 130 | } // namespace ndn |
| 131 | |
| 132 | #endif // NDN_SECURITY_VALIDITY_PERIOD_HPP |