blob: d09657f4bc89efdd1b1a44422c431c1768907fa9 [file] [log] [blame]
Yingdi Yu7a813892015-06-09 14:19:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Yingdi Yu10bf63a2015-11-04 14:14:37 -08003 * Copyright (c) 2013-2016 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
35 * @sa docs/tutorials/certificate-format.rst
36 */
37class ValidityPeriod
38{
39public:
40 class Error : public tlv::Error
41 {
42 public:
43 explicit
44 Error(const std::string& what)
45 : tlv::Error(what)
46 {
47 }
48 };
49
50public:
Yingdi Yu10bf63a2015-11-04 14:14:37 -080051 /** @brief Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid
Yingdi Yu7a813892015-06-09 14:19:54 -070052 */
Yingdi Yu10bf63a2015-11-04 14:14:37 -080053 ValidityPeriod();
Yingdi Yu7a813892015-06-09 14:19:54 -070054
55 /** @brief Create validity period from @p block
56 */
57 explicit
58 ValidityPeriod(const Block& block);
59
Yingdi Yu10bf63a2015-11-04 14:14:37 -080060 /** @brief Create validity period [@p notBefore, @p notAfter]
Yingdi Yu7a813892015-06-09 14:19:54 -070061 * @param notBefore exclusive beginning of the validity period range
62 * @param notAfter exclusive end of the validity period range
63 *
64 * @note The supplied time points will be rounded up to the whole seconds:
65 * - @p notBefore is rounded up the next whole second
66 * - @p notAfter is truncated to the previous whole second
67 */
68 ValidityPeriod(const time::system_clock::TimePoint& notBefore,
69 const time::system_clock::TimePoint& notAfter);
70
71 /** @brief Check if @p now falls within the validity period
72 * @param now Time point to check if it falls within the period
Yingdi Yu10bf63a2015-11-04 14:14:37 -080073 * @return periodBegin <= @p now and @p now <= periodEnd
Yingdi Yu7a813892015-06-09 14:19:54 -070074 */
75 bool
76 isValid(const time::system_clock::TimePoint& now = time::system_clock::now()) const;
77
Yingdi Yu10bf63a2015-11-04 14:14:37 -080078 /** @brief Set validity period [@p notBefore, @p notAfter]
Yingdi Yu7a813892015-06-09 14:19:54 -070079 * @param notBefore exclusive beginning of the validity period range
80 * @param notAfter exclusive end of the validity period range
81 *
82 * @note The supplied time points will be rounded up to the whole seconds:
83 * - @p notBefore is rounded up the next whole second
84 * - @p notAfter is truncated to the previous whole second
85 */
86 ValidityPeriod&
87 setPeriod(const time::system_clock::TimePoint& notBefore,
88 const time::system_clock::TimePoint& notAfter);
89
90 /** @brief Get the stored validity period
91 */
92 std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
93 getPeriod() const;
94
95 /** @brief Fast encoding or block size estimation
96 */
97 template<encoding::Tag TAG>
98 size_t
99 wireEncode(EncodingImpl<TAG>& encoder) const;
100
101 /** @brief Encode ValidityPeriod into TLV block
102 */
103 const Block&
104 wireEncode() const;
105
106 /** @brief Decode ValidityPeriod from TLV block
107 * @throw Error when an invalid TLV block supplied
108 */
109 void
110 wireDecode(const Block& wire);
111
112public: // EqualityComparable concept
113 bool
114 operator==(const ValidityPeriod& other) const;
115
116 bool
117 operator!=(const ValidityPeriod& other) const;
118
119private:
120 typedef boost::chrono::time_point<time::system_clock, time::seconds> TimePoint;
121
122 TimePoint m_notBefore;
123 TimePoint m_notAfter;
124
125 mutable Block m_wire;
126};
127
128std::ostream&
129operator<<(std::ostream& os, const ValidityPeriod& period);
130
131} // namespace security
132} // namespace ndn
133
134#endif // NDN_SECURITY_VALIDITY_PERIOD_HPP