Botan  1.11.15
Public Member Functions
Botan::X509_Time Class Reference

#include <asn1_time.h>

Inheritance diagram for Botan::X509_Time:
Botan::ASN1_Object

List of all members.

Public Member Functions

std::string as_string () const
s32bit cmp (const X509_Time &) const
void decode_from (class BER_Decoder &)
void encode_into (class DER_Encoder &) const
std::string readable_string () const
void set_to (const std::string &)
void set_to (const std::string &, ASN1_Tag)
bool time_is_set () const
std::string to_string () const
 X509_Time (const std::chrono::system_clock::time_point &time)
 X509_Time (const std::string &="")
 X509_Time (const std::string &, ASN1_Tag)

Detailed Description

X.509 Time

Definition at line 19 of file asn1_time.h.


Constructor & Destructor Documentation

Botan::X509_Time::X509_Time ( const std::chrono::system_clock::time_point &  time)

Definition at line 28 of file asn1_time.cpp.

References Botan::calendar_value(), Botan::calendar_point::day, Botan::GENERALIZED_TIME, Botan::calendar_point::hour, Botan::calendar_point::minutes, Botan::calendar_point::month, Botan::calendar_point::seconds, Botan::UTC_TIME, and Botan::calendar_point::year.

   {
   calendar_point cal = calendar_value(time);

   year   = cal.year;
   month  = cal.month;
   day    = cal.day;
   hour   = cal.hour;
   minute = cal.minutes;
   second = cal.seconds;

   tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME;
   }
Botan::X509_Time::X509_Time ( const std::string &  time_str = "")

Definition at line 20 of file asn1_time.cpp.

References set_to().

   {
   set_to(time_str);
   }
Botan::X509_Time::X509_Time ( const std::string &  t_spec,
ASN1_Tag  t 
)

Definition at line 45 of file asn1_time.cpp.

References set_to().

                                                        : tag(t)
   {
   set_to(t_spec, tag);
   }

Member Function Documentation

std::string Botan::X509_Time::as_string ( ) const

Definition at line 186 of file asn1_time.cpp.

References readable_string(), time_is_set(), to_string(), and Botan::UTC_TIME.

Referenced by encode_into().

   {
   if(time_is_set() == false)
      throw Invalid_State("X509_Time::as_string: No time set");

   u32bit full_year = year;

   if(tag == UTC_TIME)
      {
      if(year < 1950 || year >= 2050)
         throw Encoding_Error("X509_Time: The time " + readable_string() +
                              " cannot be encoded as a UTCTime");

      full_year = (year >= 2000) ? (year - 2000) : (year - 1900);
      }

   std::string repr = std::to_string(full_year*10000000000 +
                                     month*100000000 +
                                     day*1000000 +
                                     hour*10000 +
                                     minute*100 +
                                     second) + "Z";

   u32bit desired_size = (tag == UTC_TIME) ? 13 : 15;

   while(repr.size() < desired_size)
      repr = "0" + repr;

   return repr;
   }
s32bit Botan::X509_Time::cmp ( const X509_Time other) const

Definition at line 262 of file asn1_time.cpp.

References time_is_set().

Referenced by Botan::operator!=(), Botan::operator<(), Botan::operator<=(), Botan::operator==(), Botan::operator>(), and Botan::operator>=().

   {
   if(time_is_set() == false)
      throw Invalid_State("X509_Time::cmp: No time set");

   const s32bit EARLIER = -1, LATER = 1, SAME_TIME = 0;

   if(year < other.year)     return EARLIER;
   if(year > other.year)     return LATER;
   if(month < other.month)   return EARLIER;
   if(month > other.month)   return LATER;
   if(day < other.day)       return EARLIER;
   if(day > other.day)       return LATER;
   if(hour < other.hour)     return EARLIER;
   if(hour > other.hour)     return LATER;
   if(minute < other.minute) return EARLIER;
   if(minute > other.minute) return LATER;
   if(second < other.second) return EARLIER;
   if(second > other.second) return LATER;

   return SAME_TIME;
   }
void Botan::X509_Time::decode_from ( class BER_Decoder from) [virtual]

Decode whatever this object is from from

Parameters:
fromthe BER_Decoder that will be read from

Implements Botan::ASN1_Object.

Definition at line 173 of file asn1_time.cpp.

References Botan::BER_Decoder::get_next_object(), Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, set_to(), Botan::ASN1::to_string(), Botan::Charset::transcode(), and Botan::BER_Object::type_tag.

   {
   BER_Object ber_time = source.get_next_object();

   set_to(Charset::transcode(ASN1::to_string(ber_time),
                             LATIN1_CHARSET,
                             LOCAL_CHARSET),
          ber_time.type_tag);
   }
void Botan::X509_Time::encode_into ( class DER_Encoder to) const [virtual]

Encode whatever this object is into to

Parameters:
tothe DER_Encoder that will be written to

Implements Botan::ASN1_Object.

Definition at line 159 of file asn1_time.cpp.

References Botan::DER_Encoder::add_object(), as_string(), Botan::GENERALIZED_TIME, Botan::LATIN1_CHARSET, Botan::LOCAL_CHARSET, Botan::Charset::transcode(), Botan::UNIVERSAL, and Botan::UTC_TIME.

   {
   if(tag != GENERALIZED_TIME && tag != UTC_TIME)
      throw Invalid_Argument("X509_Time: Bad encoding tag");

   der.add_object(tag, UNIVERSAL,
                  Charset::transcode(as_string(),
                                     LOCAL_CHARSET,
                                     LATIN1_CHARSET));
   }
std::string Botan::X509_Time::readable_string ( ) const

Definition at line 228 of file asn1_time.cpp.

References time_is_set().

Referenced by as_string().

   {
   if(time_is_set() == false)
      throw Invalid_State("X509_Time::readable_string: No time set");

   std::string output(24, 0);

   std::sprintf(&output[0], "%04d/%02d/%02d %02d:%02d:%02d UTC",
                year, month, day, hour, minute, second);

   output.resize(23); // remove trailing null

   return output;
   }
void Botan::X509_Time::set_to ( const std::string &  time_str)

Definition at line 53 of file asn1_time.cpp.

References Botan::GENERALIZED_TIME, Botan::Charset::is_digit(), Botan::NO_OBJECT, Botan::to_u32bit(), and Botan::UTC_TIME.

Referenced by decode_from(), and X509_Time().

   {
   if(time_str == "")
      {
      year = month = day = hour = minute = second = 0;
      tag = NO_OBJECT;
      return;
      }

   std::vector<std::string> params;
   std::string current;

   for(size_t j = 0; j != time_str.size(); ++j)
      {
      if(Charset::is_digit(time_str[j]))
         current += time_str[j];
      else
         {
         if(current != "")
            params.push_back(current);
         current.clear();
         }
      }
   if(current != "")
      params.push_back(current);

   if(params.size() < 3 || params.size() > 6)
      throw Invalid_Argument("Invalid time specification " + time_str);

   year   = to_u32bit(params[0]);
   month  = to_u32bit(params[1]);
   day    = to_u32bit(params[2]);
   hour   = (params.size() >= 4) ? to_u32bit(params[3]) : 0;
   minute = (params.size() >= 5) ? to_u32bit(params[4]) : 0;
   second = (params.size() == 6) ? to_u32bit(params[5]) : 0;

   tag = (year >= 2050) ? GENERALIZED_TIME : UTC_TIME;

   if(!passes_sanity_check())
      throw Invalid_Argument("Invalid time specification " + time_str);
   }
void Botan::X509_Time::set_to ( const std::string &  t_spec,
ASN1_Tag  spec_tag 
)

Definition at line 98 of file asn1_time.cpp.

References Botan::GENERALIZED_TIME, Botan::ASN1::to_string(), Botan::to_u32bit(), and Botan::UTC_TIME.

   {
   if(spec_tag == GENERALIZED_TIME)
      {
      if(t_spec.size() != 13 && t_spec.size() != 15)
         throw Invalid_Argument("Invalid GeneralizedTime: " + t_spec);
      }
   else if(spec_tag == UTC_TIME)
      {
      if(t_spec.size() != 11 && t_spec.size() != 13)
         throw Invalid_Argument("Invalid UTCTime: " + t_spec);
      }
   else
      {
      throw Invalid_Argument("Invalid time tag " + std::to_string(spec_tag) + " val " + t_spec);
      }

   if(t_spec[t_spec.size()-1] != 'Z')
      throw Invalid_Argument("Invalid time encoding: " + t_spec);

   const size_t YEAR_SIZE = (spec_tag == UTC_TIME) ? 2 : 4;

   std::vector<std::string> params;
   std::string current;

   for(size_t j = 0; j != YEAR_SIZE; ++j)
      current += t_spec[j];
   params.push_back(current);
   current.clear();

   for(size_t j = YEAR_SIZE; j != t_spec.size() - 1; ++j)
      {
      current += t_spec[j];
      if(current.size() == 2)
         {
         params.push_back(current);
         current.clear();
         }
      }

   year   = to_u32bit(params[0]);
   month  = to_u32bit(params[1]);
   day    = to_u32bit(params[2]);
   hour   = to_u32bit(params[3]);
   minute = to_u32bit(params[4]);
   second = (params.size() == 6) ? to_u32bit(params[5]) : 0;
   tag    = spec_tag;

   if(spec_tag == UTC_TIME)
      {
      if(year >= 50) year += 1900;
      else           year += 2000;
      }

   if(!passes_sanity_check())
      throw Invalid_Argument("Invalid time specification " + t_spec);
   }

Definition at line 220 of file asn1_time.cpp.

Referenced by as_string(), cmp(), and readable_string().

   {
   return (year != 0);
   }
std::string Botan::X509_Time::to_string ( ) const [inline]

Definition at line 29 of file asn1_time.h.

Referenced by as_string().

{ return readable_string(); }

The documentation for this class was generated from the following files: