blob: c30497b1c1a8c193b3fa24f734da8341706c5301 [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/*
3 * Copyright (c) 2013-2017 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#include "validity-period.hpp"
23#include "../encoding/block-helpers.hpp"
24#include "../util/concepts.hpp"
25
26namespace ndn {
27namespace security {
28
29BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ValidityPeriod>));
30BOOST_CONCEPT_ASSERT((WireEncodable<ValidityPeriod>));
31BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<ValidityPeriod>));
32BOOST_CONCEPT_ASSERT((WireDecodable<ValidityPeriod>));
33static_assert(std::is_base_of<tlv::Error, ValidityPeriod::Error>::value,
34 "ValidityPeriod::Error must inherit from tlv::Error");
35
36static const size_t ISO_DATETIME_SIZE = 15;
37static const size_t NOT_BEFORE_OFFSET = 0;
38static const size_t NOT_AFTER_OFFSET = 1;
39
40using boost::chrono::time_point_cast;
41
Yingdi Yu10bf63a2015-11-04 14:14:37 -080042ValidityPeriod::ValidityPeriod()
43 : ValidityPeriod(time::system_clock::TimePoint() + time::nanoseconds(1),
44 time::system_clock::TimePoint())
45{
46}
47
Yingdi Yu7a813892015-06-09 14:19:54 -070048ValidityPeriod::ValidityPeriod(const time::system_clock::TimePoint& notBefore,
49 const time::system_clock::TimePoint& notAfter)
50 : m_notBefore(time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
51 time::system_clock::TimePoint::duration(1)))
52 , m_notAfter(time_point_cast<TimePoint::duration>(notAfter))
53{
54}
55
56ValidityPeriod::ValidityPeriod(const Block& block)
57{
58 wireDecode(block);
59}
60
61template<encoding::Tag TAG>
62size_t
63ValidityPeriod::wireEncode(EncodingImpl<TAG>& encoder) const
64{
65 size_t totalLength = 0;
66
67 totalLength += prependStringBlock(encoder, tlv::NotAfter, time::toIsoString(m_notAfter));
68 totalLength += prependStringBlock(encoder, tlv::NotBefore, time::toIsoString(m_notBefore));
69
70 totalLength += encoder.prependVarNumber(totalLength);
71 totalLength += encoder.prependVarNumber(tlv::ValidityPeriod);
72 return totalLength;
73}
74
Davide Pesavento88a0d812017-08-19 21:31:42 -040075NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(ValidityPeriod);
Yingdi Yu7a813892015-06-09 14:19:54 -070076
77const Block&
78ValidityPeriod::wireEncode() const
79{
80 if (m_wire.hasWire())
81 return m_wire;
82
83 EncodingEstimator estimator;
84 size_t estimatedSize = wireEncode(estimator);
85
86 EncodingBuffer buffer(estimatedSize, 0);
87 wireEncode(buffer);
88
89 m_wire = buffer.block();
90 m_wire.parse();
91
92 return m_wire;
93}
94
95void
96ValidityPeriod::wireDecode(const Block& wire)
97{
98 if (!wire.hasWire()) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -070099 BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
Yingdi Yu7a813892015-06-09 14:19:54 -0700100 }
101
102 m_wire = wire;
103 m_wire.parse();
104
105 if (m_wire.type() != tlv::ValidityPeriod)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700106 BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding ValidityPeriod"));
Yingdi Yu7a813892015-06-09 14:19:54 -0700107
108 if (m_wire.elements_size() != 2)
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700109 BOOST_THROW_EXCEPTION(Error("Does not have two sub-TLVs"));
Yingdi Yu7a813892015-06-09 14:19:54 -0700110
111 if (m_wire.elements()[NOT_BEFORE_OFFSET].type() != tlv::NotBefore ||
112 m_wire.elements()[NOT_BEFORE_OFFSET].value_size() != ISO_DATETIME_SIZE ||
113 m_wire.elements()[NOT_AFTER_OFFSET].type() != tlv::NotAfter ||
114 m_wire.elements()[NOT_AFTER_OFFSET].value_size() != ISO_DATETIME_SIZE) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700115 BOOST_THROW_EXCEPTION(Error("Invalid NotBefore or NotAfter field"));
Yingdi Yu7a813892015-06-09 14:19:54 -0700116 }
117
118 try {
119 m_notBefore = time_point_cast<TimePoint::duration>(
120 time::fromIsoString(readString(m_wire.elements()[NOT_BEFORE_OFFSET])));
121 m_notAfter = time_point_cast<TimePoint::duration>(
122 time::fromIsoString(readString(m_wire.elements()[NOT_AFTER_OFFSET])));
123 }
124 catch (const std::bad_cast&) {
Spyridon Mastorakis0d2ed2e2015-07-27 19:09:12 -0700125 BOOST_THROW_EXCEPTION(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field"));
Yingdi Yu7a813892015-06-09 14:19:54 -0700126 }
127}
128
129ValidityPeriod&
130ValidityPeriod::setPeriod(const time::system_clock::TimePoint& notBefore,
131 const time::system_clock::TimePoint& notAfter)
132{
133 m_wire.reset();
134 m_notBefore = time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
135 time::system_clock::TimePoint::duration(1));
136 m_notAfter = time_point_cast<TimePoint::duration>(notAfter);
137 return *this;
138}
139
140std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
141ValidityPeriod::getPeriod() const
142{
143 return std::make_pair(m_notBefore, m_notAfter);
144}
145
146bool
147ValidityPeriod::isValid(const time::system_clock::TimePoint& now) const
148{
Yingdi Yu10bf63a2015-11-04 14:14:37 -0800149 return m_notBefore <= now && now <= m_notAfter;
Yingdi Yu7a813892015-06-09 14:19:54 -0700150}
151
152bool
153ValidityPeriod::operator==(const ValidityPeriod& other) const
154{
155 return (this->m_notBefore == other.m_notBefore &&
156 this->m_notAfter == other.m_notAfter);
157}
158
159bool
160ValidityPeriod::operator!=(const ValidityPeriod& other) const
161{
162 return !(*this == other);
163}
164
165std::ostream&
166operator<<(std::ostream& os, const ValidityPeriod& period)
167{
168 os << "(" << time::toIsoString(period.getPeriod().first)
169 << ", " << time::toIsoString(period.getPeriod().second) << ")";
170 return os;
171}
172
173} // namespace security
174} // namespace ndn