Botan
1.11.15
|
00001 /* 00002 * EAC ASN.1 Objects 00003 * (C) 2007-2008 FlexSecure GmbH 00004 * 2008-2010 Jack Lloyd 00005 * 00006 * Botan is released under the Simplified BSD License (see license.txt) 00007 */ 00008 00009 #ifndef BOTAN_EAC_ASN1_OBJ_H__ 00010 #define BOTAN_EAC_ASN1_OBJ_H__ 00011 00012 #include <botan/asn1_obj.h> 00013 #include <chrono> 00014 00015 namespace Botan { 00016 00017 /** 00018 * This class represents CVC EAC Time objects. 00019 * It only models year, month and day. Only limited sanity checks of 00020 * the inputted date value are performed. 00021 */ 00022 class BOTAN_DLL EAC_Time : public ASN1_Object 00023 { 00024 public: 00025 void encode_into(class DER_Encoder&) const; 00026 void decode_from(class BER_Decoder&); 00027 00028 /** 00029 * Get a this objects value as a string. 00030 * @return date string 00031 */ 00032 std::string as_string() const; 00033 00034 /** 00035 * Get a this objects value as a readable formatted string. 00036 * @return date string 00037 */ 00038 std::string readable_string() const; 00039 00040 /** 00041 * Find out whether this object's values have been set. 00042 * @return true if this object's internal values are set 00043 */ 00044 bool time_is_set() const; 00045 00046 /** 00047 * Compare this to another EAC_Time object. 00048 * @return -1 if this object's date is earlier than 00049 * other, +1 in the opposite case, and 0 if both dates are 00050 * equal. 00051 */ 00052 s32bit cmp(const EAC_Time& other) const; 00053 00054 /** 00055 * Set this' value by a string value. 00056 * @param str a string in the format "yyyy mm dd", 00057 * e.g. "2007 08 01" 00058 */ 00059 void set_to(const std::string& str); 00060 00061 /** 00062 * Add the specified number of years to this. 00063 * @param years the number of years to add 00064 */ 00065 void add_years(u32bit years); 00066 00067 /** 00068 * Add the specified number of months to this. 00069 * @param months the number of months to add 00070 */ 00071 void add_months(u32bit months); 00072 00073 /** 00074 * Get the year value of this objects. 00075 * @return year value 00076 */ 00077 u32bit get_year() const { return year; } 00078 00079 /** 00080 * Get the month value of this objects. 00081 * @return month value 00082 */ 00083 u32bit get_month() const { return month; } 00084 00085 /** 00086 * Get the day value of this objects. 00087 * @return day value 00088 */ 00089 u32bit get_day() const { return day; } 00090 00091 EAC_Time(const std::chrono::system_clock::time_point& time, 00092 ASN1_Tag tag = ASN1_Tag(0)); 00093 00094 EAC_Time(const std::string& yyyy_mm_dd, 00095 ASN1_Tag tag = ASN1_Tag(0)); 00096 00097 EAC_Time(u32bit year, u32bit month, u32bit day, 00098 ASN1_Tag tag = ASN1_Tag(0)); 00099 00100 virtual ~EAC_Time() {} 00101 private: 00102 std::vector<byte> encoded_eac_time() const; 00103 bool passes_sanity_check() const; 00104 u32bit year, month, day; 00105 ASN1_Tag tag; 00106 }; 00107 00108 /** 00109 * This class represents CVC CEDs. Only limited sanity checks of 00110 * the inputted date value are performed. 00111 */ 00112 class BOTAN_DLL ASN1_Ced : public EAC_Time 00113 { 00114 public: 00115 /** 00116 * Construct a CED from a string value. 00117 * @param str a string in the format "yyyy mm dd", 00118 * e.g. "2007 08 01" 00119 */ 00120 ASN1_Ced(const std::string& str = "") : 00121 EAC_Time(str, ASN1_Tag(37)) {} 00122 00123 /** 00124 * Construct a CED from a time point 00125 */ 00126 ASN1_Ced(const std::chrono::system_clock::time_point& time) : 00127 EAC_Time(time, ASN1_Tag(37)) {} 00128 00129 /** 00130 * Copy constructor (for general EAC_Time objects). 00131 * @param other the object to copy from 00132 */ 00133 ASN1_Ced(const EAC_Time& other) : 00134 EAC_Time(other.get_year(), other.get_month(), other.get_day(), 00135 ASN1_Tag(37)) 00136 {} 00137 }; 00138 00139 /** 00140 * This class represents CVC CEXs. Only limited sanity checks of 00141 * the inputted date value are performed. 00142 */ 00143 class BOTAN_DLL ASN1_Cex : public EAC_Time 00144 { 00145 public: 00146 /** 00147 * Construct a CEX from a string value. 00148 * @param str a string in the format "yyyy mm dd", 00149 * e.g. "2007 08 01" 00150 */ 00151 ASN1_Cex(const std::string& str = "") : 00152 EAC_Time(str, ASN1_Tag(36)) {} 00153 00154 ASN1_Cex(const std::chrono::system_clock::time_point& time) : 00155 EAC_Time(time, ASN1_Tag(36)) {} 00156 00157 ASN1_Cex(const EAC_Time& other) : 00158 EAC_Time(other.get_year(), other.get_month(), other.get_day(), 00159 ASN1_Tag(36)) 00160 {} 00161 }; 00162 00163 /** 00164 * Base class for car/chr of cv certificates. 00165 */ 00166 class BOTAN_DLL ASN1_EAC_String: public ASN1_Object 00167 { 00168 public: 00169 void encode_into(class DER_Encoder&) const; 00170 void decode_from(class BER_Decoder&); 00171 00172 /** 00173 * Get this objects string value. 00174 * @return string value 00175 */ 00176 std::string value() const; 00177 00178 /** 00179 * Get this objects string value. 00180 * @return string value in iso8859 encoding 00181 */ 00182 std::string iso_8859() const; 00183 00184 ASN1_Tag tagging() const; 00185 ASN1_EAC_String(const std::string& str, ASN1_Tag the_tag); 00186 00187 virtual ~ASN1_EAC_String() {} 00188 protected: 00189 bool sanity_check() const; 00190 private: 00191 std::string iso_8859_str; 00192 ASN1_Tag tag; 00193 }; 00194 00195 /** 00196 * This class represents CARs of CVCs. (String tagged with 2) 00197 */ 00198 class BOTAN_DLL ASN1_Car : public ASN1_EAC_String 00199 { 00200 public: 00201 /** 00202 * Create a CAR with the specified content. 00203 * @param str the CAR value 00204 */ 00205 ASN1_Car(std::string const& str = ""); 00206 }; 00207 00208 /** 00209 * This class represents CHRs of CVCs (tag 32) 00210 */ 00211 class BOTAN_DLL ASN1_Chr : public ASN1_EAC_String 00212 { 00213 public: 00214 /** 00215 * Create a CHR with the specified content. 00216 * @param str the CHR value 00217 */ 00218 ASN1_Chr(std::string const& str = ""); 00219 }; 00220 00221 /* 00222 * Comparison Operations 00223 */ 00224 bool BOTAN_DLL operator==(const EAC_Time&, const EAC_Time&); 00225 bool BOTAN_DLL operator!=(const EAC_Time&, const EAC_Time&); 00226 bool BOTAN_DLL operator<=(const EAC_Time&, const EAC_Time&); 00227 bool BOTAN_DLL operator>=(const EAC_Time&, const EAC_Time&); 00228 bool BOTAN_DLL operator>(const EAC_Time&, const EAC_Time&); 00229 bool BOTAN_DLL operator<(const EAC_Time&, const EAC_Time&); 00230 00231 bool BOTAN_DLL operator==(const ASN1_EAC_String&, const ASN1_EAC_String&); 00232 inline bool operator!=(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs) 00233 { 00234 return !(lhs == rhs); 00235 } 00236 00237 } 00238 00239 #endif