/src/botan/build/include/botan/asn1_obj.h
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * (C) 1999-2007,2018,2020 Jack Lloyd |
3 | | * |
4 | | * Botan is released under the Simplified BSD License (see license.txt) |
5 | | */ |
6 | | |
7 | | #ifndef BOTAN_ASN1_OBJECT_TYPES_H_ |
8 | | #define BOTAN_ASN1_OBJECT_TYPES_H_ |
9 | | |
10 | | #include <botan/exceptn.h> |
11 | | #include <botan/secmem.h> |
12 | | #include <chrono> |
13 | | #include <iosfwd> |
14 | | #include <optional> |
15 | | #include <string> |
16 | | #include <string_view> |
17 | | #include <unordered_map> |
18 | | #include <vector> |
19 | | |
20 | | namespace Botan { |
21 | | |
22 | | class BER_Decoder; |
23 | | class DER_Encoder; |
24 | | |
25 | | /** |
26 | | * ASN.1 Class Tags |
27 | | */ |
28 | | enum class ASN1_Class : uint32_t { |
29 | | Universal = 0b0000'0000, |
30 | | Application = 0b0100'0000, |
31 | | ContextSpecific = 0b1000'0000, |
32 | | Private = 0b1100'0000, |
33 | | |
34 | | Constructed = 0b0010'0000, |
35 | | ExplicitContextSpecific = Constructed | ContextSpecific, |
36 | | |
37 | | NoObject = 0xFF00 |
38 | | }; |
39 | | |
40 | | /** |
41 | | * ASN.1 Type Tags |
42 | | */ |
43 | | enum class ASN1_Type : uint32_t { |
44 | | Eoc = 0x00, |
45 | | Boolean = 0x01, |
46 | | Integer = 0x02, |
47 | | BitString = 0x03, |
48 | | OctetString = 0x04, |
49 | | Null = 0x05, |
50 | | ObjectId = 0x06, |
51 | | Enumerated = 0x0A, |
52 | | Sequence = 0x10, |
53 | | Set = 0x11, |
54 | | |
55 | | Utf8String = 0x0C, |
56 | | NumericString = 0x12, |
57 | | PrintableString = 0x13, |
58 | | TeletexString = 0x14, |
59 | | Ia5String = 0x16, |
60 | | VisibleString = 0x1A, |
61 | | UniversalString = 0x1C, |
62 | | BmpString = 0x1E, |
63 | | |
64 | | UtcTime = 0x17, |
65 | | GeneralizedTime = 0x18, |
66 | | |
67 | | NoObject = 0xFF00, |
68 | | }; |
69 | | |
70 | 0 | inline bool intersects(ASN1_Class x, ASN1_Class y) { |
71 | 0 | return static_cast<uint32_t>(x) & static_cast<uint32_t>(y); |
72 | 0 | } |
73 | | |
74 | 0 | inline ASN1_Type operator|(ASN1_Type x, ASN1_Type y) { |
75 | 0 | return static_cast<ASN1_Type>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y)); |
76 | 0 | } |
77 | | |
78 | 0 | inline ASN1_Class operator|(ASN1_Class x, ASN1_Class y) { |
79 | 0 | return static_cast<ASN1_Class>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y)); |
80 | 0 | } |
81 | | |
82 | 0 | inline uint32_t operator|(ASN1_Type x, ASN1_Class y) { |
83 | 0 | return static_cast<uint32_t>(x) | static_cast<uint32_t>(y); |
84 | 0 | } |
85 | | |
86 | 0 | inline uint32_t operator|(ASN1_Class x, ASN1_Type y) { |
87 | 0 | return static_cast<uint32_t>(x) | static_cast<uint32_t>(y); |
88 | 0 | } |
89 | | |
90 | | std::string BOTAN_UNSTABLE_API asn1_tag_to_string(ASN1_Type type); |
91 | | std::string BOTAN_UNSTABLE_API asn1_class_to_string(ASN1_Class type); |
92 | | |
93 | | /** |
94 | | * Basic ASN.1 Object Interface |
95 | | */ |
96 | | class BOTAN_PUBLIC_API(2, 0) ASN1_Object { |
97 | | public: |
98 | | /** |
99 | | * Encode whatever this object is into to |
100 | | * @param to the DER_Encoder that will be written to |
101 | | */ |
102 | | virtual void encode_into(DER_Encoder& to) const = 0; |
103 | | |
104 | | /** |
105 | | * Decode whatever this object is from from |
106 | | * @param from the BER_Decoder that will be read from |
107 | | */ |
108 | | virtual void decode_from(BER_Decoder& from) = 0; |
109 | | |
110 | | /** |
111 | | * Return the encoding of this object. This is a convenience |
112 | | * method when just one object needs to be serialized. Use |
113 | | * DER_Encoder for complicated encodings. |
114 | | */ |
115 | | std::vector<uint8_t> BER_encode() const; |
116 | | |
117 | 1.55k | ASN1_Object() = default; |
118 | 2.20k | ASN1_Object(const ASN1_Object&) = default; |
119 | 0 | ASN1_Object& operator=(const ASN1_Object&) = default; |
120 | 3.75k | virtual ~ASN1_Object() = default; |
121 | | }; |
122 | | |
123 | | /** |
124 | | * BER Encoded Object |
125 | | */ |
126 | | class BOTAN_PUBLIC_API(2, 0) BER_Object final { |
127 | | public: |
128 | 0 | BER_Object() : m_type_tag(ASN1_Type::NoObject), m_class_tag(ASN1_Class::Universal) {} |
129 | | |
130 | | BER_Object(const BER_Object& other) = default; |
131 | | |
132 | 0 | BER_Object& operator=(const BER_Object& other) = default; |
133 | | |
134 | 0 | BER_Object(BER_Object&& other) = default; |
135 | | |
136 | 0 | BER_Object& operator=(BER_Object&& other) = default; |
137 | | |
138 | 0 | bool is_set() const { return m_type_tag != ASN1_Type::NoObject; } |
139 | | |
140 | 0 | uint32_t tagging() const { return type_tag() | class_tag(); } |
141 | | |
142 | 0 | ASN1_Type type_tag() const { return m_type_tag; } |
143 | | |
144 | 0 | ASN1_Class class_tag() const { return m_class_tag; } |
145 | | |
146 | 0 | ASN1_Type type() const { return m_type_tag; } |
147 | | |
148 | 0 | ASN1_Class get_class() const { return m_class_tag; } |
149 | | |
150 | 0 | const uint8_t* bits() const { return m_value.data(); } |
151 | | |
152 | 0 | size_t length() const { return m_value.size(); } |
153 | | |
154 | | void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr = "object") const; |
155 | | |
156 | | bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const; |
157 | | |
158 | | bool is_a(int type_tag, ASN1_Class class_tag) const; |
159 | | |
160 | | private: |
161 | | ASN1_Type m_type_tag; |
162 | | ASN1_Class m_class_tag; |
163 | | secure_vector<uint8_t> m_value; |
164 | | |
165 | | friend class BER_Decoder; |
166 | | |
167 | | void set_tagging(ASN1_Type type_tag, ASN1_Class class_tag); |
168 | | |
169 | 0 | uint8_t* mutable_bits(size_t length) { |
170 | 0 | m_value.resize(length); |
171 | 0 | return m_value.data(); |
172 | 0 | } |
173 | | }; |
174 | | |
175 | | /* |
176 | | * ASN.1 Utility Functions |
177 | | */ |
178 | | class DataSource; |
179 | | |
180 | | namespace ASN1 { |
181 | | |
182 | | std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& val); |
183 | | std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len); |
184 | | std::string to_string(const BER_Object& obj); |
185 | | |
186 | | /** |
187 | | * Heuristics tests; is this object possibly BER? |
188 | | * @param src a data source that will be peeked at but not modified |
189 | | */ |
190 | | bool maybe_BER(DataSource& src); |
191 | | |
192 | | } // namespace ASN1 |
193 | | |
194 | | /** |
195 | | * General BER Decoding Error Exception |
196 | | */ |
197 | | class BOTAN_PUBLIC_API(2, 0) BER_Decoding_Error : public Decoding_Error { |
198 | | public: |
199 | | explicit BER_Decoding_Error(std::string_view); |
200 | | }; |
201 | | |
202 | | /** |
203 | | * Exception For Incorrect BER Taggings |
204 | | */ |
205 | | class BOTAN_PUBLIC_API(2, 0) BER_Bad_Tag final : public BER_Decoding_Error { |
206 | | public: |
207 | | BER_Bad_Tag(std::string_view msg, uint32_t tagging); |
208 | | }; |
209 | | |
210 | | /** |
211 | | * This class represents ASN.1 object identifiers. |
212 | | */ |
213 | | class BOTAN_PUBLIC_API(2, 0) OID final : public ASN1_Object { |
214 | | public: |
215 | | /** |
216 | | * Create an uninitialied OID object |
217 | | */ |
218 | 0 | explicit OID() = default; |
219 | | |
220 | | /** |
221 | | * Construct an OID from a string. |
222 | | * @param str a string in the form "a.b.c" etc., where a,b,c are numbers |
223 | | */ |
224 | | explicit OID(std::string_view str); |
225 | | |
226 | | /** |
227 | | * Initialize an OID from a sequence of integer values |
228 | | */ |
229 | 1.54k | explicit OID(std::initializer_list<uint32_t> init) : m_id(init) { |
230 | 1.54k | BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID"); |
231 | 1.54k | } |
232 | | |
233 | | /** |
234 | | * Initialize an OID from a vector of integer values |
235 | | */ |
236 | 0 | explicit OID(std::vector<uint32_t>&& init) : m_id(init) { |
237 | 0 | BOTAN_ARG_CHECK(m_id.size() > 2 && m_id[0] <= 2 && (m_id[0] != 2 || m_id[1] <= 39), "Invalid OID"); |
238 | 0 | } |
239 | | |
240 | | /** |
241 | | * Construct an OID from a string. |
242 | | * @param str a string in the form "a.b.c" etc., where a,b,c are numbers |
243 | | * or any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier") |
244 | | */ |
245 | | static OID from_string(std::string_view str); |
246 | | |
247 | | /** |
248 | | * Construct an OID from a name |
249 | | * @param name any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier") |
250 | | */ |
251 | | static std::optional<OID> from_name(std::string_view name); |
252 | | |
253 | | /** |
254 | | * Register a new OID in the internal table |
255 | | */ |
256 | | static void register_oid(const OID& oid, std::string_view name); |
257 | | |
258 | | void encode_into(DER_Encoder&) const override; |
259 | | void decode_from(BER_Decoder&) override; |
260 | | |
261 | | /** |
262 | | * Find out whether this OID is empty |
263 | | * @return true is no OID value is set |
264 | | */ |
265 | 48 | bool empty() const { return m_id.empty(); } |
266 | | |
267 | | /** |
268 | | * Find out whether this OID has a value |
269 | | * @return true is this OID has a value |
270 | | */ |
271 | 16 | bool has_value() const { return (m_id.empty() == false); } |
272 | | |
273 | | /** |
274 | | * Get this OID as list (vector) of its components. |
275 | | * @return vector representing this OID |
276 | | */ |
277 | 16 | const std::vector<uint32_t>& get_components() const { return m_id; } |
278 | | |
279 | 0 | const std::vector<uint32_t>& get_id() const { return get_components(); } |
280 | | |
281 | | /** |
282 | | * Get this OID as a dotted-decimal string |
283 | | * @return string representing this OID |
284 | | */ |
285 | | std::string to_string() const; |
286 | | |
287 | | /** |
288 | | * If there is a known name associated with this OID, return that. |
289 | | * Otherwise return the result of to_string |
290 | | */ |
291 | | std::string to_formatted_string() const; |
292 | | |
293 | | /** |
294 | | * If there is a known name associated with this OID, return that. |
295 | | * Otherwise return the empty string. |
296 | | */ |
297 | | std::string human_name_or_empty() const; |
298 | | |
299 | | /** |
300 | | * Return true if the OID in *this is registered in the internal |
301 | | * set of constants as a known OID. |
302 | | */ |
303 | | bool registered_oid() const; |
304 | | |
305 | | /** |
306 | | * Compare two OIDs. |
307 | | * @return true if they are equal, false otherwise |
308 | | */ |
309 | 488 | bool operator==(const OID& other) const { return m_id == other.m_id; } |
310 | | |
311 | | private: |
312 | | std::unordered_map<std::string, std::string> load_oid2str_map(); |
313 | | std::unordered_map<std::string, OID> load_str2oid_map(); |
314 | | |
315 | | std::vector<uint32_t> m_id; |
316 | | }; |
317 | | |
318 | | std::ostream& operator<<(std::ostream& out, const OID& oid); |
319 | | |
320 | | /** |
321 | | * Compare two OIDs. |
322 | | * @param a the first OID |
323 | | * @param b the second OID |
324 | | * @return true if a is not equal to b |
325 | | */ |
326 | 24 | inline bool operator!=(const OID& a, const OID& b) { |
327 | 24 | return !(a == b); |
328 | 24 | } |
329 | | |
330 | | /** |
331 | | * Compare two OIDs. |
332 | | * @param a the first OID |
333 | | * @param b the second OID |
334 | | * @return true if a is lexicographically smaller than b |
335 | | */ |
336 | | bool BOTAN_PUBLIC_API(2, 0) operator<(const OID& a, const OID& b); |
337 | | |
338 | | /** |
339 | | * Time (GeneralizedTime/UniversalTime) |
340 | | */ |
341 | | class BOTAN_PUBLIC_API(2, 0) ASN1_Time final : public ASN1_Object { |
342 | | public: |
343 | | /// DER encode a ASN1_Time |
344 | | void encode_into(DER_Encoder&) const override; |
345 | | |
346 | | // Decode a BER encoded ASN1_Time |
347 | | void decode_from(BER_Decoder&) override; |
348 | | |
349 | | /// Return an internal string representation of the time |
350 | | std::string to_string() const; |
351 | | |
352 | | /// Returns a human friendly string replesentation of no particular formatting |
353 | | std::string readable_string() const; |
354 | | |
355 | | /// Return if the time has been set somehow |
356 | | bool time_is_set() const; |
357 | | |
358 | | /// Compare this time against another |
359 | | int32_t cmp(const ASN1_Time& other) const; |
360 | | |
361 | | /// Create an invalid ASN1_Time |
362 | | ASN1_Time() = default; |
363 | | |
364 | | /// Create a ASN1_Time from a time point |
365 | | explicit ASN1_Time(const std::chrono::system_clock::time_point& time); |
366 | | |
367 | | /// Create an ASN1_Time from string |
368 | | ASN1_Time(std::string_view t_spec); |
369 | | |
370 | | /// Create an ASN1_Time from string and a specified tagging (Utc or Generalized) |
371 | | ASN1_Time(std::string_view t_spec, ASN1_Type tag); |
372 | | |
373 | | /// Returns a STL timepoint object |
374 | | std::chrono::system_clock::time_point to_std_timepoint() const; |
375 | | |
376 | | /// Return time since epoch |
377 | | uint64_t time_since_epoch() const; |
378 | | |
379 | | private: |
380 | | void set_to(std::string_view t_spec, ASN1_Type type); |
381 | | bool passes_sanity_check() const; |
382 | | |
383 | | uint32_t m_year = 0; |
384 | | uint32_t m_month = 0; |
385 | | uint32_t m_day = 0; |
386 | | uint32_t m_hour = 0; |
387 | | uint32_t m_minute = 0; |
388 | | uint32_t m_second = 0; |
389 | | ASN1_Type m_tag = ASN1_Type::NoObject; |
390 | | }; |
391 | | |
392 | | /* |
393 | | * Comparison Operations |
394 | | */ |
395 | | bool BOTAN_PUBLIC_API(2, 0) operator==(const ASN1_Time&, const ASN1_Time&); |
396 | | bool BOTAN_PUBLIC_API(2, 0) operator!=(const ASN1_Time&, const ASN1_Time&); |
397 | | bool BOTAN_PUBLIC_API(2, 0) operator<=(const ASN1_Time&, const ASN1_Time&); |
398 | | bool BOTAN_PUBLIC_API(2, 0) operator>=(const ASN1_Time&, const ASN1_Time&); |
399 | | bool BOTAN_PUBLIC_API(2, 0) operator<(const ASN1_Time&, const ASN1_Time&); |
400 | | bool BOTAN_PUBLIC_API(2, 0) operator>(const ASN1_Time&, const ASN1_Time&); |
401 | | |
402 | | typedef ASN1_Time X509_Time; |
403 | | |
404 | | /** |
405 | | * ASN.1 string type |
406 | | * This class normalizes all inputs to a UTF-8 std::string |
407 | | */ |
408 | | class BOTAN_PUBLIC_API(2, 0) ASN1_String final : public ASN1_Object { |
409 | | public: |
410 | | void encode_into(DER_Encoder&) const override; |
411 | | void decode_from(BER_Decoder&) override; |
412 | | |
413 | 0 | ASN1_Type tagging() const { return m_tag; } |
414 | | |
415 | 0 | const std::string& value() const { return m_utf8_str; } |
416 | | |
417 | 0 | size_t size() const { return value().size(); } |
418 | | |
419 | 0 | bool empty() const { return m_utf8_str.empty(); } |
420 | | |
421 | | /** |
422 | | * Return true iff this is a tag for a known string type we can handle. |
423 | | */ |
424 | | static bool is_string_type(ASN1_Type tag); |
425 | | |
426 | 0 | bool operator==(const ASN1_String& other) const { return value() == other.value(); } |
427 | | |
428 | | explicit ASN1_String(std::string_view utf8 = ""); |
429 | | ASN1_String(std::string_view utf8, ASN1_Type tag); |
430 | | |
431 | | private: |
432 | | std::vector<uint8_t> m_data; |
433 | | std::string m_utf8_str; |
434 | | ASN1_Type m_tag; |
435 | | }; |
436 | | |
437 | | /** |
438 | | * Algorithm Identifier |
439 | | */ |
440 | | class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final : public ASN1_Object { |
441 | | public: |
442 | | enum Encoding_Option { USE_NULL_PARAM, USE_EMPTY_PARAM }; |
443 | | |
444 | | void encode_into(DER_Encoder&) const override; |
445 | | void decode_from(BER_Decoder&) override; |
446 | | |
447 | | AlgorithmIdentifier() = default; |
448 | | |
449 | | AlgorithmIdentifier(const OID& oid, Encoding_Option enc); |
450 | | AlgorithmIdentifier(std::string_view oid_name, Encoding_Option enc); |
451 | | |
452 | | AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& params); |
453 | | AlgorithmIdentifier(std::string_view oid_name, const std::vector<uint8_t>& params); |
454 | | |
455 | 0 | const OID& oid() const { return m_oid; } |
456 | | |
457 | 0 | const std::vector<uint8_t>& parameters() const { return m_parameters; } |
458 | | |
459 | | BOTAN_DEPRECATED("Use AlgorithmIdentifier::oid") |
460 | | |
461 | 0 | const OID& get_oid() const { return m_oid; } |
462 | | |
463 | | BOTAN_DEPRECATED("Use AlgorithmIdentifier::parameters") |
464 | | |
465 | 0 | const std::vector<uint8_t>& get_parameters() const { return m_parameters; } |
466 | | |
467 | | bool parameters_are_null() const; |
468 | | |
469 | 0 | bool parameters_are_empty() const { return m_parameters.empty(); } |
470 | | |
471 | 0 | bool parameters_are_null_or_empty() const { return parameters_are_empty() || parameters_are_null(); } |
472 | | |
473 | 0 | bool empty() const { return m_oid.empty() && m_parameters.empty(); } |
474 | | |
475 | | private: |
476 | | OID m_oid; |
477 | | std::vector<uint8_t> m_parameters; |
478 | | }; |
479 | | |
480 | | /* |
481 | | * Comparison Operations |
482 | | */ |
483 | | bool BOTAN_PUBLIC_API(2, 0) operator==(const AlgorithmIdentifier&, const AlgorithmIdentifier&); |
484 | | bool BOTAN_PUBLIC_API(2, 0) operator!=(const AlgorithmIdentifier&, const AlgorithmIdentifier&); |
485 | | |
486 | | } // namespace Botan |
487 | | |
488 | | #endif |