Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 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 | |
| 26 | namespace ndn { |
| 27 | namespace security { |
| 28 | |
| 29 | BOOST_CONCEPT_ASSERT((boost::EqualityComparable<ValidityPeriod>)); |
| 30 | BOOST_CONCEPT_ASSERT((WireEncodable<ValidityPeriod>)); |
| 31 | BOOST_CONCEPT_ASSERT((WireEncodableWithEncodingBuffer<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 | |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 42 | ValidityPeriod::ValidityPeriod() |
| 43 | : ValidityPeriod(time::system_clock::TimePoint() + time::nanoseconds(1), |
| 44 | time::system_clock::TimePoint()) |
| 45 | { |
| 46 | } |
| 47 | |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 48 | ValidityPeriod::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 | |
| 56 | ValidityPeriod::ValidityPeriod(const Block& block) |
| 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& |
| 82 | ValidityPeriod::wireEncode() const |
| 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 |
| 100 | ValidityPeriod::wireDecode(const Block& wire) |
| 101 | { |
| 102 | if (!wire.hasWire()) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 103 | BOOST_THROW_EXCEPTION(Error("The supplied block does not contain wire format")); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | m_wire = wire; |
| 107 | m_wire.parse(); |
| 108 | |
| 109 | if (m_wire.type() != tlv::ValidityPeriod) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 110 | BOOST_THROW_EXCEPTION(Error("Unexpected TLV type when decoding ValidityPeriod")); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 111 | |
| 112 | if (m_wire.elements_size() != 2) |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 113 | BOOST_THROW_EXCEPTION(Error("Does not have two sub-TLVs")); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 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) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 119 | BOOST_THROW_EXCEPTION(Error("Invalid NotBefore or NotAfter field")); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | try { |
| 123 | m_notBefore = time_point_cast<TimePoint::duration>( |
| 124 | time::fromIsoString(readString(m_wire.elements()[NOT_BEFORE_OFFSET]))); |
| 125 | m_notAfter = time_point_cast<TimePoint::duration>( |
| 126 | time::fromIsoString(readString(m_wire.elements()[NOT_AFTER_OFFSET]))); |
| 127 | } |
| 128 | catch (const std::bad_cast&) { |
Spyridon Mastorakis | 0d2ed2e | 2015-07-27 19:09:12 -0700 | [diff] [blame] | 129 | BOOST_THROW_EXCEPTION(Error("Invalid date format in NOT-BEFORE or NOT-AFTER field")); |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 130 | } |
| 131 | } |
| 132 | |
| 133 | ValidityPeriod& |
| 134 | ValidityPeriod::setPeriod(const time::system_clock::TimePoint& notBefore, |
| 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> |
| 145 | ValidityPeriod::getPeriod() const |
| 146 | { |
| 147 | return std::make_pair(m_notBefore, m_notAfter); |
| 148 | } |
| 149 | |
| 150 | bool |
| 151 | ValidityPeriod::isValid(const time::system_clock::TimePoint& now) const |
| 152 | { |
Yingdi Yu | 10bf63a | 2015-11-04 14:14:37 -0800 | [diff] [blame] | 153 | return m_notBefore <= now && now <= m_notAfter; |
Yingdi Yu | 7a81389 | 2015-06-09 14:19:54 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | bool |
| 157 | ValidityPeriod::operator==(const ValidityPeriod& other) const |
| 158 | { |
| 159 | return (this->m_notBefore == other.m_notBefore && |
| 160 | this->m_notAfter == other.m_notAfter); |
| 161 | } |
| 162 | |
| 163 | bool |
| 164 | ValidityPeriod::operator!=(const ValidityPeriod& other) const |
| 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 |