aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/cert/cvc/eac_asn_obj.h
blob: ab8536e45260c1eb3b5d1b7fdd8afb10e234c6d5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
/*
* EAC ASN.1 Objects
* (C) 2007-2008 FlexSecure GmbH
*     2008-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/

#ifndef BOTAN_EAC_ASN1_OBJ_H__
#define BOTAN_EAC_ASN1_OBJ_H__

#include <botan/asn1_obj.h>

namespace Botan {

/**
* This class represents CVC EAC Time objects.
* It only models year, month and day. Only limited sanity checks of
* the inputted date value are performed.
*/
class BOTAN_DLL EAC_Time : public ASN1_Object
   {
   public:
      void encode_into(class DER_Encoder&) const;
      void decode_from(class BER_Decoder&);

      /**
      * Get a this objects value as a string.
      * @return date string
      */
      std::string as_string() const;

      /**
      * Get a this objects value as a readable formatted string.
      * @return date string
      */
      std::string readable_string() const;

      /**
      * Find out whether this object's values have been set.
      * @return true if this object's internal values are set
      */
      bool time_is_set() const;

      /**
      * Compare this to another EAC_Time object.
      * @return -1 if this object's date is earlier than
      * other, +1 in the opposite case, and 0 if both dates are
      * equal.
      */
      s32bit cmp(const EAC_Time& other) const;

      /**
      * Set this' value by a string value.
      * @param str a string in the format "yyyy mm dd",
      * e.g. "2007 08 01"
      */
      void set_to(const std::string& str);

      /**
      * Add the specified number of years to this.
      * @param years the number of years to add
      */
      void add_years(u32bit years);

      /**
      * Add the specified number of months to this.
      * @param months the number of months to add
      */
      void add_months(u32bit months);

      /**
      * Get the year value of this objects.
      * @return year value
      */
      u32bit get_year() const { return year; }

      /**
      * Get the month value of this objects.
      * @return month value
      */
      u32bit get_month() const { return month; }

      /**
      * Get the day value of this objects.
      * @return day value
      */
      u32bit get_day() const { return day; }

      EAC_Time(const std::chrono::system_clock::time_point& time,
               ASN1_Tag tag = ASN1_Tag(0));

      EAC_Time(const std::string& yyyy_mm_dd,
               ASN1_Tag tag = ASN1_Tag(0));

      EAC_Time(u32bit year, u32bit month, u32bit day,
               ASN1_Tag tag = ASN1_Tag(0));

      virtual ~EAC_Time() {}
   private:
      std::vector<byte> encoded_eac_time() const;
      bool passes_sanity_check() const;
      u32bit year, month, day;
      ASN1_Tag tag;
   };

/**
* This class represents CVC CEDs. Only limited sanity checks of
* the inputted date value are performed.
*/
class BOTAN_DLL ASN1_Ced : public EAC_Time
   {
   public:
      /**
      * Construct a CED from a string value.
      * @param str a string in the format "yyyy mm dd",
      * e.g. "2007 08 01"
      */
      ASN1_Ced(const std::string& str = "") :
         EAC_Time(str, ASN1_Tag(37)) {}

      /**
      * Construct a CED from a time point
      */
      ASN1_Ced(const std::chrono::system_clock::time_point& time) :
         EAC_Time(time, ASN1_Tag(37)) {}

      /**
      * Copy constructor (for general EAC_Time objects).
      * @param other the object to copy from
      */
      ASN1_Ced(const EAC_Time& other) :
         EAC_Time(other.get_year(), other.get_month(), other.get_day(),
                  ASN1_Tag(37))
         {}
   };

/**
* This class represents CVC CEXs. Only limited sanity checks of
* the inputted date value are performed.
*/
class BOTAN_DLL ASN1_Cex : public EAC_Time
   {
   public:
      /**
      * Construct a CEX from a string value.
      * @param str a string in the format "yyyy mm dd",
      * e.g. "2007 08 01"
      */
      ASN1_Cex(const std::string& str = "") :
         EAC_Time(str, ASN1_Tag(36)) {}

      ASN1_Cex(const std::chrono::system_clock::time_point& time) :
         EAC_Time(time, ASN1_Tag(36)) {}

      ASN1_Cex(const EAC_Time& other) :
         EAC_Time(other.get_year(), other.get_month(), other.get_day(),
                  ASN1_Tag(36))
         {}
   };

/**
* Base class for car/chr of cv certificates.
*/
class BOTAN_DLL ASN1_EAC_String: public ASN1_Object
   {
   public:
      void encode_into(class DER_Encoder&) const;
      void decode_from(class BER_Decoder&);

      /**
      * Get this objects string value.
      * @return string value
      */
      std::string value() const;

      /**
      * Get this objects string value.
      * @return string value in iso8859 encoding
      */
      std::string iso_8859() const;

      ASN1_Tag tagging() const;
      ASN1_EAC_String(const std::string& str, ASN1_Tag the_tag);

      virtual ~ASN1_EAC_String() {}
   protected:
      bool sanity_check() const;
   private:
      std::string iso_8859_str;
      ASN1_Tag tag;
   };

/**
* This class represents CARs of CVCs. (String tagged with 2)
*/
class BOTAN_DLL ASN1_Car : public ASN1_EAC_String
   {
   public:
      /**
      * Create a CAR with the specified content.
      * @param str the CAR value
      */
      ASN1_Car(std::string const& str = "");
   };

/**
* This class represents CHRs of CVCs (tag 32)
*/
class BOTAN_DLL ASN1_Chr : public ASN1_EAC_String
   {
   public:
      /**
      * Create a CHR with the specified content.
      * @param str the CHR value
      */
      ASN1_Chr(std::string const& str = "");
   };

/*
* Comparison Operations
*/
bool BOTAN_DLL operator==(const EAC_Time&, const EAC_Time&);
bool BOTAN_DLL operator!=(const EAC_Time&, const EAC_Time&);
bool BOTAN_DLL operator<=(const EAC_Time&, const EAC_Time&);
bool BOTAN_DLL operator>=(const EAC_Time&, const EAC_Time&);
bool BOTAN_DLL operator>(const EAC_Time&, const EAC_Time&);
bool BOTAN_DLL operator<(const EAC_Time&, const EAC_Time&);

bool BOTAN_DLL operator==(const ASN1_EAC_String&, const ASN1_EAC_String&);
inline bool operator!=(const ASN1_EAC_String& lhs, const ASN1_EAC_String& rhs)
   {
   return !(lhs == rhs);
   }

}

#endif