blob: ba05b7acc61e8b5c4013a93bdee200dfb4aec047 [file] [log] [blame]
Yingdi Yu7a813892015-06-09 14:19:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento88a0d812017-08-19 21:31:42 -04002/*
Junxiao Shi68b53852018-07-25 13:56:38 -06003 * Copyright (c) 2013-2018 Regents of the University of California.
Yingdi Yu7a813892015-06-09 14:19:54 -07004 *
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
30namespace ndn {
31namespace security {
32
33
34/** @brief Abstraction of validity period
Junxiao Shi426d5002018-08-23 10:52:29 +000035 * @sa docs/specs/certificate-format.rst
Yingdi Yu7a813892015-06-09 14:19:54 -070036 */
37class ValidityPeriod
38{
39public:
40 class Error : public tlv::Error
41 {
42 public:
Junxiao Shi68b53852018-07-25 13:56:38 -060043 using tlv::Error::Error;
Yingdi Yu7a813892015-06-09 14:19:54 -070044 };
45
46public:
Yingdi Yu10bf63a2015-11-04 14:14:37 -080047 /** @brief Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid
Yingdi Yu7a813892015-06-09 14:19:54 -070048 */
Yingdi Yu10bf63a2015-11-04 14:14:37 -080049 ValidityPeriod();
Yingdi Yu7a813892015-06-09 14:19:54 -070050
51 /** @brief Create validity period from @p block
52 */
53 explicit
54 ValidityPeriod(const Block& block);
55
Yingdi Yu10bf63a2015-11-04 14:14:37 -080056 /** @brief Create validity period [@p notBefore, @p notAfter]
Yingdi Yu7a813892015-06-09 14:19:54 -070057 * @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 Yu10bf63a2015-11-04 14:14:37 -080069 * @return periodBegin <= @p now and @p now <= periodEnd
Yingdi Yu7a813892015-06-09 14:19:54 -070070 */
71 bool
72 isValid(const time::system_clock::TimePoint& now = time::system_clock::now()) const;
73
Yingdi Yu10bf63a2015-11-04 14:14:37 -080074 /** @brief Set validity period [@p notBefore, @p notAfter]
Yingdi Yu7a813892015-06-09 14:19:54 -070075 * @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
108public: // EqualityComparable concept
109 bool
110 operator==(const ValidityPeriod& other) const;
111
112 bool
113 operator!=(const ValidityPeriod& other) const;
114
115private:
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 Pesavento88a0d812017-08-19 21:31:42 -0400124NDN_CXX_DECLARE_WIRE_ENCODE_INSTANTIATIONS(ValidityPeriod);
125
Yingdi Yu7a813892015-06-09 14:19:54 -0700126std::ostream&
127operator<<(std::ostream& os, const ValidityPeriod& period);
128
129} // namespace security
130} // namespace ndn
131
132#endif // NDN_SECURITY_VALIDITY_PERIOD_HPP