blob: 008cd3c5b97108be6f67e7df6a01aba791a01b14 [file] [log] [blame]
Yingdi Yu7a813892015-06-09 14:19:54 -07001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2015 Regents of the University of California.
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#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
42ValidityPeriod::ValidityPeriod(const time::system_clock::TimePoint& notBefore,
43 const time::system_clock::TimePoint& notAfter)
44 : m_notBefore(time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
45 time::system_clock::TimePoint::duration(1)))
46 , m_notAfter(time_point_cast<TimePoint::duration>(notAfter))
47{
48}
49
50ValidityPeriod::ValidityPeriod(const Block& block)
51{
52 wireDecode(block);
53}
54
55template<encoding::Tag TAG>
56size_t
57ValidityPeriod::wireEncode(EncodingImpl<TAG>& encoder) const
58{
59 size_t totalLength = 0;
60
61 totalLength += prependStringBlock(encoder, tlv::NotAfter, time::toIsoString(m_notAfter));
62 totalLength += prependStringBlock(encoder, tlv::NotBefore, time::toIsoString(m_notBefore));
63
64 totalLength += encoder.prependVarNumber(totalLength);
65 totalLength += encoder.prependVarNumber(tlv::ValidityPeriod);
66 return totalLength;
67}
68
69template size_t
70ValidityPeriod::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
71
72template size_t
73ValidityPeriod::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
74
75const Block&
76ValidityPeriod::wireEncode() const
77{
78 if (m_wire.hasWire())
79 return m_wire;
80
81 EncodingEstimator estimator;
82 size_t estimatedSize = wireEncode(estimator);
83
84 EncodingBuffer buffer(estimatedSize, 0);
85 wireEncode(buffer);
86
87 m_wire = buffer.block();
88 m_wire.parse();
89
90 return m_wire;
91}
92
93void
94ValidityPeriod::wireDecode(const Block& wire)
95{
96 if (!wire.hasWire()) {
97 throw Error("The supplied block does not contain wire format");
98 }
99
100 m_wire = wire;
101 m_wire.parse();
102
103 if (m_wire.type() != tlv::ValidityPeriod)
104 throw Error("Unexpected TLV type when decoding ValidityPeriod");
105
106 if (m_wire.elements_size() != 2)
107 throw Error("Does not have two sub-TLVs");
108
109 if (m_wire.elements()[NOT_BEFORE_OFFSET].type() != tlv::NotBefore ||
110 m_wire.elements()[NOT_BEFORE_OFFSET].value_size() != ISO_DATETIME_SIZE ||
111 m_wire.elements()[NOT_AFTER_OFFSET].type() != tlv::NotAfter ||
112 m_wire.elements()[NOT_AFTER_OFFSET].value_size() != ISO_DATETIME_SIZE) {
113 throw Error("Invalid NotBefore or NotAfter field");
114 }
115
116 try {
117 m_notBefore = time_point_cast<TimePoint::duration>(
118 time::fromIsoString(readString(m_wire.elements()[NOT_BEFORE_OFFSET])));
119 m_notAfter = time_point_cast<TimePoint::duration>(
120 time::fromIsoString(readString(m_wire.elements()[NOT_AFTER_OFFSET])));
121 }
122 catch (const std::bad_cast&) {
123 throw Error("Invalid date format in NOT-BEFORE or NOT-AFTER field");
124 }
125}
126
127ValidityPeriod&
128ValidityPeriod::setPeriod(const time::system_clock::TimePoint& notBefore,
129 const time::system_clock::TimePoint& notAfter)
130{
131 m_wire.reset();
132 m_notBefore = time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
133 time::system_clock::TimePoint::duration(1));
134 m_notAfter = time_point_cast<TimePoint::duration>(notAfter);
135 return *this;
136}
137
138std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
139ValidityPeriod::getPeriod() const
140{
141 return std::make_pair(m_notBefore, m_notAfter);
142}
143
144bool
145ValidityPeriod::isValid(const time::system_clock::TimePoint& now) const
146{
147 return m_notBefore < now && now < m_notAfter;
148}
149
150bool
151ValidityPeriod::operator==(const ValidityPeriod& other) const
152{
153 return (this->m_notBefore == other.m_notBefore &&
154 this->m_notAfter == other.m_notAfter);
155}
156
157bool
158ValidityPeriod::operator!=(const ValidityPeriod& other) const
159{
160 return !(*this == other);
161}
162
163std::ostream&
164operator<<(std::ostream& os, const ValidityPeriod& period)
165{
166 os << "(" << time::toIsoString(period.getPeriod().first)
167 << ", " << time::toIsoString(period.getPeriod().second) << ")";
168 return os;
169}
170
171} // namespace security
172} // namespace ndn