NS-3 based Named Data Networking (NDN) simulator
ndnSIM 2.5: NDN, CCN, CCNx, content centric networks
API Documentation
validity-period.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2 /*
3  * Copyright (c) 2013-2019 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 
25 
26 namespace ndn {
27 namespace security {
28 
29 BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ValidityPeriod>));
30 BOOST_CONCEPT_ASSERT((WireEncodable<ValidityPeriod>));
32 BOOST_CONCEPT_ASSERT((WireDecodable<ValidityPeriod>));
33 static_assert(std::is_base_of<tlv::Error, ValidityPeriod::Error>::value,
34  "ValidityPeriod::Error must inherit from tlv::Error");
35 
36 static const size_t ISO_DATETIME_SIZE = 15;
37 static const size_t NOT_BEFORE_OFFSET = 0;
38 static const size_t NOT_AFTER_OFFSET = 1;
39 
40 using boost::chrono::time_point_cast;
41 
43  : ValidityPeriod(time::system_clock::TimePoint() + 1_ns,
44  time::system_clock::TimePoint())
45 {
46 }
47 
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 
57 {
58  wireDecode(block);
59 }
60 
61 template<encoding::Tag TAG>
62 size_t
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 
76 
77 const Block&
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 
95 void
97 {
98  if (!wire.hasWire()) {
99  NDN_THROW(Error("The supplied block does not contain wire format"));
100  }
101 
102  m_wire = wire;
103  m_wire.parse();
104 
105  if (m_wire.type() != tlv::ValidityPeriod)
106  NDN_THROW(Error("ValidityPeriod", m_wire.type()));
107 
108  if (m_wire.elements_size() != 2)
109  NDN_THROW(Error("ValidityPeriod does not have two sub-TLVs"));
110 
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) {
115  NDN_THROW(Error("Invalid NotBefore or NotAfter field"));
116  }
117 
118  try {
119  m_notBefore = time_point_cast<TimePoint::duration>(
121  m_notAfter = time_point_cast<TimePoint::duration>(
123  }
124  catch (const std::bad_cast&) {
125  NDN_THROW(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field"));
126  }
127 }
128 
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 
140 std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
142 {
143  return std::make_pair(m_notBefore, m_notAfter);
144 }
145 
146 bool
148 {
149  return m_notBefore <= now && now <= m_notAfter;
150 }
151 
152 std::ostream&
153 operator<<(std::ostream& os, const ValidityPeriod& period)
154 {
155  os << "(" << time::toIsoString(period.getPeriod().first)
156  << ", " << time::toIsoString(period.getPeriod().second) << ")";
157  return os;
158 }
159 
160 } // namespace security
161 } // namespace ndn
ndn::tlv::ValidityPeriod
@ ValidityPeriod
Definition: tlv.hpp:143
ndn::Block::elements
const element_container & elements() const
Get container of sub-elements.
Definition: block.hpp:391
ndn::time::system_clock::TimePoint
time_point TimePoint
Definition: time.hpp:195
ndn::security::NOT_AFTER_OFFSET
static const size_t NOT_AFTER_OFFSET
Definition: validity-period.cpp:38
ndn::security::ValidityPeriod::getPeriod
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
Definition: validity-period.cpp:141
ndn::encoding::readString
std::string readString(const Block &block)
Read TLV-VALUE of a TLV element as a string.
Definition: block-helpers.cpp:122
concepts.hpp
ndn::Block::reset
void reset() noexcept
Reset the Block to a default-constructed state.
Definition: block.cpp:250
ndn::security::ValidityPeriod::setPeriod
ValidityPeriod & setPeriod(const time::system_clock::TimePoint &notBefore, const time::system_clock::TimePoint &notAfter)
Set validity period [notBefore, notAfter].
Definition: validity-period.cpp:130
ndn::security::ValidityPeriod::ValidityPeriod
ValidityPeriod()
Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid.
Definition: validity-period.cpp:42
ndn::WireDecodable
a concept check for TLV abstraction with .wireDecode method and constructible from Block
Definition: concepts.hpp:81
ndn::encoding::EncodingEstimator
EncodingImpl< EstimatorTag > EncodingEstimator
Definition: encoding-buffer-fwd.hpp:39
ndn::security::ValidityPeriod::wireDecode
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
Definition: validity-period.cpp:96
ndn::security::ValidityPeriod::Error
Definition: validity-period.hpp:41
ndn::Block::type
uint32_t type() const
Return the TLV-TYPE of the Block.
Definition: block.hpp:274
ndn::time::fromIsoString
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format.
Definition: time.cpp:164
ndn::WireEncodable
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:45
ndn::security::ValidityPeriod
Abstraction of validity period.
Definition: validity-period.hpp:38
ndn::time
Definition: time-custom-clock.hpp:28
ndn::security::NOT_BEFORE_OFFSET
static const size_t NOT_BEFORE_OFFSET
Definition: validity-period.cpp:37
validity-period.hpp
ndn::security::operator<<
std::ostream & operator<<(std::ostream &os, const SigningInfo &si)
Definition: signing-info.cpp:201
NDN_THROW
#define NDN_THROW(e)
Definition: exception.hpp:61
ndn::tlv::NotBefore
@ NotBefore
Definition: tlv.hpp:144
ndn::security::NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS
NDN_CXX_DEFINE_WIRE_ENCODE_INSTANTIATIONS(SafeBag)
ndn::time::toIsoString
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:145
ndn::security::ValidityPeriod::wireEncode
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
Definition: validity-period.cpp:78
ndn::tlv::NotAfter
@ NotAfter
Definition: tlv.hpp:145
ndn::Block::parse
void parse() const
Parse TLV-VALUE into sub-elements.
Definition: block.cpp:325
ndn::encoding::EncodingImpl
Definition: encoding-buffer-fwd.hpp:36
block-helpers.hpp
ndn::security::ValidityPeriod::isValid
bool isValid(const time::system_clock::TimePoint &now=time::system_clock::now()) const
Check if now falls within the validity period.
Definition: validity-period.cpp:147
ndn::Block
Represents a TLV element of NDN packet format.
Definition: block.hpp:43
ndn::Block::elements_size
size_t elements_size() const
Equivalent to elements().size()
Definition: block.hpp:415
ndn::Block::hasWire
bool hasWire() const noexcept
Check if the Block contains a fully encoded wire representation.
Definition: block.hpp:230
ndn::encoding::prependStringBlock
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Prepend a TLV element containing a string.
Definition: block-helpers.cpp:104
ndn::security::ISO_DATETIME_SIZE
static const size_t ISO_DATETIME_SIZE
Definition: validity-period.cpp:36
ndn::WireEncodableWithEncodingBuffer
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:61
ndn
Copyright (c) 2011-2015 Regents of the University of California.
Definition: ndn-strategy-choice-helper.hpp:34
ndn::encoding::EncodingBuffer
EncodingImpl< EncoderTag > EncodingBuffer
Definition: encoding-buffer-fwd.hpp:38