1#ifndef NDNPH_KEYCHAIN_VALIDITY_PERIOD_HPP
2#define NDNPH_KEYCHAIN_VALIDITY_PERIOD_HPP
4#include "../packet/an.hpp"
5#include "../port/unixtime/port.hpp"
6#include "../tlv/ev-decoder.hpp"
7#include "../tlv/value.hpp"
15 const char* tz = getenv(
"TZ");
16 if (tz ==
nullptr || strlen(tz) >=
sizeof(m_tz)) {
19 strncpy(m_tz, tz,
sizeof(m_tz));
21 setenv(
"TZ",
"UTC", 1);
25 if (m_tz[0] !=
'\0') {
26 setenv(
"TZ", m_tz, 1);
46 time_t now = port::UnixTime::now() / 1000000;
83 EvDecoder::def<TT::NotBefore>([
this](
const Decoder::Tlv& d) {
86 EvDecoder::def<TT::NotAfter>([
this](
const Decoder::Tlv& d) {
87 return decodeTimestamp(d, &
notAfter);
92 static constexpr time_t MAX_TIME =
93 sizeof(time_t) <= 4 ? std::numeric_limits<time_t>::max() : 253402300799;
94 static const constexpr char*
const TIMESTAMP_FMT =
"%04d%02d%02dT%02d%02d%02d";
95 static constexpr size_t TIMESTAMP_LEN = 15;
97 class TimestampEncoder {
99 explicit TimestampEncoder(uint32_t tlvType, time_t t)
103 void encodeTo(Encoder& encoder)
const {
104 struct tm* m = gmtime(&t);
110 char buf[TIMESTAMP_LEN + 1];
111 int res = snprintf(buf,
sizeof(buf), TIMESTAMP_FMT, 1900 + m->tm_year, 1 + m->tm_mon,
112 m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec);
113 NDNPH_ASSERT(res >= 0 && res <
static_cast<int>(
sizeof(buf)));
114 encoder.prependTlv(tlvType, tlv::Value(
reinterpret_cast<const uint8_t*
>(buf), TIMESTAMP_LEN));
118 uint32_t tlvType = 0;
122 static bool decodeTimestamp(
const Decoder::Tlv& d, time_t* v) {
123 if (d.length != TIMESTAMP_LEN) {
127 char buf[TIMESTAMP_LEN + 1];
128 std::copy_n(d.value, TIMESTAMP_LEN, buf);
129 buf[TIMESTAMP_LEN] =
'\0';
132 if (sscanf(buf, TIMESTAMP_FMT, &m.tm_year, &m.tm_mon, &m.tm_mday, &m.tm_hour, &m.tm_min,
139 detail::UtcTimezone useUtc;
141 if (
sizeof(time_t) <= 4 && *v < 0 && (1900 + m.tm_year) >= 2038) {
166 intersection.
notBefore = std::max(lhs.notBefore, rhs.notBefore);
167 intersection.
notAfter = std::min(lhs.notAfter, rhs.notAfter);
Decoded TLV.
Definition decoder.hpp:13
TLV encoder that accepts items in reverse order.
Definition encoder.hpp:10
bool prependTlv(uint32_t type, OmitEmptyTag omitEmpty, const Arg &... arg)
Prepend TLV, measuring TLV-LENGTH automatically.
Definition encoder.hpp:143
static bool decode(const Decoder::Tlv &input, std::initializer_list< uint32_t > topTypes, const E &... defs)
Decode input TLV with a sequence of element definitions.
Definition ev-decoder.hpp:115
ValidityPeriod of a certificate.
Definition validity-period.hpp:37
bool decodeFrom(const Decoder::Tlv &input)
Definition validity-period.hpp:81
bool includes(time_t t)
Determine whether the timestamp (in seconds) is within validity period.
Definition validity-period.hpp:62
time_t notBefore
NotBefore field in seconds since Unix epoch.
Definition validity-period.hpp:149
static ValidityPeriod daysFromNow(uint64_t days)
Get a ValidityPeriod from now until days later.
Definition validity-period.hpp:51
static ValidityPeriod secondsFromNow(uint64_t seconds)
Get a ValidityPeriod from now until seconds later.
Definition validity-period.hpp:45
ValidityPeriod(time_t notBefore, time_t notAfter)
Definition validity-period.hpp:57
time_t notAfter
NotAfter field in seconds since Unix epoch.
Definition validity-period.hpp:152
ValidityPeriod intersect(const ValidityPeriod &other) const
Calculate the intersection of this and other ValidityPeriod.
Definition validity-period.hpp:72
static ValidityPeriod getMax()
Get a very long ValidityPeriod.
Definition validity-period.hpp:40
void encodeTo(Encoder &encoder) const
Definition validity-period.hpp:76
bool includesUnix(uint64_t t=port::UnixTime::now())
Determine whether the Unix timestamp (in microseconds) is within validity period.
Definition validity-period.hpp:67
Definition validity-period.hpp:12
~UtcTimezone()
Definition validity-period.hpp:24
UtcTimezone()
Definition validity-period.hpp:14
#define NDNPH_ASSERT(x)
Definition common.hpp:30
@ NotAfter
Definition an.hpp:59
@ ValidityPeriod
Definition an.hpp:57
@ NotBefore
Definition an.hpp:58
bool operator==(const ValidityPeriod &lhs, const ValidityPeriod &rhs)
Definition validity-period.hpp:156
#define NDNPH_DECLARE_NE(T, specifier)
Declare operator!= in terms of operator==.
Definition operators.hpp:7