validity-period.cpp
Go to the documentation of this file.
1 /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
22 #include "validity-period.hpp"
23 #include "../encoding/block-helpers.hpp"
24 #include "../util/concepts.hpp"
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() + time::nanoseconds(1),
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
63 ValidityPeriod::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 
75 template size_t
76 ValidityPeriod::wireEncode<encoding::EncoderTag>(EncodingImpl<encoding::EncoderTag>& encoder) const;
77 
78 template size_t
79 ValidityPeriod::wireEncode<encoding::EstimatorTag>(EncodingImpl<encoding::EstimatorTag>& encoder) const;
80 
81 const Block&
83 {
84  if (m_wire.hasWire())
85  return m_wire;
86 
87  EncodingEstimator estimator;
88  size_t estimatedSize = wireEncode(estimator);
89 
90  EncodingBuffer buffer(estimatedSize, 0);
91  wireEncode(buffer);
92 
93  m_wire = buffer.block();
94  m_wire.parse();
95 
96  return m_wire;
97 }
98 
99 void
101 {
102  if (!wire.hasWire()) {
103  BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format"));
104  }
105 
106  m_wire = wire;
107  m_wire.parse();
108 
109  if (m_wire.type() != tlv::ValidityPeriod)
110  BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding ValidityPeriod"));
111 
112  if (m_wire.elements_size() != 2)
113  BOOST_THROW_EXCEPTION(Error("Does not have two sub-TLVs"));
114 
115  if (m_wire.elements()[NOT_BEFORE_OFFSET].type() != tlv::NotBefore ||
116  m_wire.elements()[NOT_BEFORE_OFFSET].value_size() != ISO_DATETIME_SIZE ||
117  m_wire.elements()[NOT_AFTER_OFFSET].type() != tlv::NotAfter ||
118  m_wire.elements()[NOT_AFTER_OFFSET].value_size() != ISO_DATETIME_SIZE) {
119  BOOST_THROW_EXCEPTION(Error("Invalid NotBefore or NotAfter field"));
120  }
121 
122  try {
123  m_notBefore = time_point_cast<TimePoint::duration>(
125  m_notAfter = time_point_cast<TimePoint::duration>(
127  }
128  catch (const std::bad_cast&) {
129  BOOST_THROW_EXCEPTION(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field"));
130  }
131 }
132 
135  const time::system_clock::TimePoint& notAfter)
136 {
137  m_wire.reset();
138  m_notBefore = time_point_cast<TimePoint::duration>(notBefore + TimePoint::duration(1) -
139  time::system_clock::TimePoint::duration(1));
140  m_notAfter = time_point_cast<TimePoint::duration>(notAfter);
141  return *this;
142 }
143 
144 std::pair<time::system_clock::TimePoint, time::system_clock::TimePoint>
146 {
147  return std::make_pair(m_notBefore, m_notAfter);
148 }
149 
150 bool
152 {
153  return m_notBefore <= now && now <= m_notAfter;
154 }
155 
156 bool
158 {
159  return (this->m_notBefore == other.m_notBefore &&
160  this->m_notAfter == other.m_notAfter);
161 }
162 
163 bool
165 {
166  return !(*this == other);
167 }
168 
169 std::ostream&
170 operator<<(std::ostream& os, const ValidityPeriod& period)
171 {
172  os << "(" << time::toIsoString(period.getPeriod().first)
173  << ", " << time::toIsoString(period.getPeriod().second) << ")";
174  return os;
175 }
176 
177 } // namespace security
178 } // namespace ndn
Copyright (c) 2013-2016 Regents of the University of California.
Definition: common.hpp:74
system_clock::TimePoint fromIsoString(const std::string &isoString)
Convert from the ISO string (YYYYMMDDTHHMMSS,fffffffff) representation to the internal time format...
Definition: time.cpp:147
EncodingImpl< EstimatorTag > EncodingEstimator
const element_container & elements() const
Get all subelements.
Definition: block.hpp:342
Class representing a wire element of NDN-TLV packet format.
Definition: block.hpp:43
bool operator==(const ValidityPeriod &other) const
size_t prependStringBlock(EncodingImpl< TAG > &encoder, uint32_t type, const std::string &value)
Helper to prepend TLV block type type with value from a string value.
static const size_t NOT_BEFORE_OFFSET
std::string readString(const Block &block)
Helper to read a string value from a block.
ValidityPeriod & setPeriod(const time::system_clock::TimePoint &notBefore, const time::system_clock::TimePoint &notAfter)
Set validity period [notBefore, notAfter].
std::ostream & operator<<(std::ostream &os, CommandInterestValidator::ErrorCode error)
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:56
Abstraction of validity period.
std::pair< time::system_clock::TimePoint, time::system_clock::TimePoint > getPeriod() const
Get the stored validity period.
EncodingImpl< EncoderTag > EncodingBuffer
static const size_t ISO_DATETIME_SIZE
bool operator!=(const ValidityPeriod &other) const
const Block & wireEncode() const
Encode ValidityPeriod into TLV block.
size_t elements_size() const
Definition: block.cpp:601
void reset()
Reset wire buffer of the element.
Definition: block.cpp:302
static const size_t NOT_AFTER_OFFSET
std::string toIsoString(const system_clock::TimePoint &timePoint)
Convert to the ISO string representation of the time (YYYYMMDDTHHMMSS,fffffffff)
Definition: time.cpp:130
void parse() const
Parse wire buffer into subblocks.
Definition: block.cpp:322
uint32_t type() const
Definition: block.hpp:324
time_point TimePoint
Definition: time.hpp:90
bool hasWire() const
Check if the Block has fully encoded wire.
Definition: block.cpp:471
bool isValid(const time::system_clock::TimePoint &now=time::system_clock::now()) const
Check if now falls within the validity period.
ValidityPeriod()
Set validity period [UNIX epoch + 1 nanosecond, UNIX epoch] that is always invalid.
void wireDecode(const Block &wire)
Decode ValidityPeriod from TLV block.
a concept check for TLV abstraction with .wireEncode method
Definition: concepts.hpp:40
a concept check for TLV abstraction with .wireDecode method and constructible from Block ...
Definition: concepts.hpp:76