/src/Botan-3.13.0/build/include/public/botan/asn1_obj.h
Line | Count | Source |
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 <iosfwd> |
12 | | #include <optional> |
13 | | #include <span> |
14 | | #include <string> |
15 | | #include <string_view> |
16 | | #include <vector> |
17 | | |
18 | | namespace Botan { |
19 | | |
20 | | class BER_Decoder; |
21 | | class DER_Encoder; |
22 | | class ASN1_Time; // in asn1_time.h |
23 | | |
24 | | /** |
25 | | * Alias for ASN1_Time, reflecting the name used by the X.509 ASN.1 modules |
26 | | */ |
27 | | typedef ASN1_Time X509_Time; |
28 | | |
29 | | /** |
30 | | * ASN.1 Class Tags |
31 | | */ |
32 | | enum class ASN1_Class : uint32_t /* NOLINT(performance-enum-size) */ { |
33 | | Universal = 0b0000'0000, |
34 | | Application = 0b0100'0000, |
35 | | ContextSpecific = 0b1000'0000, |
36 | | Private = 0b1100'0000, |
37 | | |
38 | | Constructed = 0b0010'0000, |
39 | | ExplicitContextSpecific = Constructed | ContextSpecific, |
40 | | |
41 | | NoObject = 0xFF00 |
42 | | }; |
43 | | |
44 | | /** |
45 | | * ASN.1 Type Tags |
46 | | */ |
47 | | enum class ASN1_Type : uint32_t /* NOLINT(performance-enum-size) */ { |
48 | | Eoc = 0x00, |
49 | | Boolean = 0x01, |
50 | | Integer = 0x02, |
51 | | BitString = 0x03, |
52 | | OctetString = 0x04, |
53 | | Null = 0x05, |
54 | | ObjectId = 0x06, |
55 | | Enumerated = 0x0A, |
56 | | Sequence = 0x10, |
57 | | Set = 0x11, |
58 | | |
59 | | Utf8String = 0x0C, |
60 | | NumericString = 0x12, |
61 | | PrintableString = 0x13, |
62 | | TeletexString = 0x14, |
63 | | Ia5String = 0x16, |
64 | | VisibleString = 0x1A, |
65 | | UniversalString = 0x1C, |
66 | | BmpString = 0x1E, |
67 | | |
68 | | UtcTime = 0x17, |
69 | | GeneralizedTime = 0x18, |
70 | | |
71 | | NoObject = 0xFF00, |
72 | | }; |
73 | | |
74 | | /** |
75 | | * Return true if the two class tags have any bits in common |
76 | | */ |
77 | 0 | inline bool intersects(ASN1_Class x, ASN1_Class y) { |
78 | 0 | return (static_cast<uint32_t>(x) & static_cast<uint32_t>(y)) != 0; |
79 | 0 | } |
80 | | |
81 | | /** |
82 | | * Return the bitwise combination of two type tags |
83 | | */ |
84 | 0 | inline ASN1_Type operator|(ASN1_Type x, ASN1_Type y) { |
85 | 0 | return static_cast<ASN1_Type>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y)); |
86 | 0 | } |
87 | | |
88 | | /** |
89 | | * Return the bitwise combination of two class tags |
90 | | */ |
91 | 0 | inline ASN1_Class operator|(ASN1_Class x, ASN1_Class y) { |
92 | 0 | return static_cast<ASN1_Class>(static_cast<uint32_t>(x) | static_cast<uint32_t>(y)); |
93 | 0 | } |
94 | | |
95 | | /** |
96 | | * Combine a type tag and a class tag into a single tagging value |
97 | | */ |
98 | 0 | inline uint32_t operator|(ASN1_Type x, ASN1_Class y) { |
99 | 0 | return static_cast<uint32_t>(x) | static_cast<uint32_t>(y); |
100 | 0 | } |
101 | | |
102 | | /** |
103 | | * Combine a class tag and a type tag into a single tagging value |
104 | | */ |
105 | 0 | inline uint32_t operator|(ASN1_Class x, ASN1_Type y) { |
106 | 0 | return static_cast<uint32_t>(x) | static_cast<uint32_t>(y); |
107 | 0 | } |
108 | | |
109 | | /** |
110 | | * Return a descriptive string for an ASN.1 type tag |
111 | | */ |
112 | | std::string BOTAN_UNSTABLE_API asn1_tag_to_string(ASN1_Type type); |
113 | | |
114 | | /** |
115 | | * Return a descriptive string for an ASN.1 class tag |
116 | | */ |
117 | | std::string BOTAN_UNSTABLE_API asn1_class_to_string(ASN1_Class type); |
118 | | |
119 | | /** |
120 | | * Basic ASN.1 Object Interface |
121 | | */ |
122 | | class BOTAN_PUBLIC_API(2, 0) ASN1_Object { |
123 | | public: |
124 | | /** |
125 | | * Encode whatever this object is into to |
126 | | * @param to the DER_Encoder that will be written to |
127 | | */ |
128 | | virtual void encode_into(DER_Encoder& to) const = 0; |
129 | | |
130 | | /** |
131 | | * Decode whatever this object is from from |
132 | | * @param from the BER_Decoder that will be read from |
133 | | */ |
134 | | virtual void decode_from(BER_Decoder& from) = 0; |
135 | | |
136 | | /** |
137 | | * Return the encoding of this object. This is a convenience |
138 | | * method when just one object needs to be serialized. Use |
139 | | * DER_Encoder for complicated encodings. |
140 | | */ |
141 | | std::vector<uint8_t> BER_encode() const; |
142 | | |
143 | | /** |
144 | | * Default constructor |
145 | | */ |
146 | 170k | ASN1_Object() = default; |
147 | | |
148 | | /** |
149 | | * Copy constructor |
150 | | */ |
151 | 169k | ASN1_Object(const ASN1_Object&) = default; |
152 | | |
153 | | /** |
154 | | * Copy assignment |
155 | | */ |
156 | 0 | ASN1_Object& operator=(const ASN1_Object&) = default; |
157 | | |
158 | | /** |
159 | | * Move constructor |
160 | | */ |
161 | 339k | ASN1_Object(ASN1_Object&&) = default; |
162 | | |
163 | | /** |
164 | | * Move assignment |
165 | | */ |
166 | 0 | ASN1_Object& operator=(ASN1_Object&&) = default; |
167 | | |
168 | 679k | virtual ~ASN1_Object() = default; |
169 | | }; |
170 | | |
171 | | /** |
172 | | * ASN.1 BIT STRING with explicit unused-bit count. |
173 | | */ |
174 | | class BOTAN_PUBLIC_API(3, 13) ASN1_BitString final { |
175 | | public: |
176 | | /** |
177 | | * Create an empty BIT STRING |
178 | | */ |
179 | 0 | ASN1_BitString() = default; |
180 | | |
181 | | /** |
182 | | * Create a BIT STRING from the given bits |
183 | | * |
184 | | * @param bytes the bits, without the leading unused-bit count octet |
185 | | * @param unused_bits the number of unused bits in the final byte |
186 | | * |
187 | | * Throws Invalid_Argument if unused_bits is 8 or larger, if unused_bits is |
188 | | * non-zero for an empty BIT STRING, or if any of the unused bits are set. |
189 | | */ |
190 | | ASN1_BitString(std::vector<uint8_t> bytes, size_t unused_bits); |
191 | | |
192 | | /** |
193 | | * Create a BIT STRING from the given bits |
194 | | * |
195 | | * @param bytes the bits, without the leading unused-bit count octet |
196 | | * @param unused_bits the number of unused bits in the final byte |
197 | | * |
198 | | * Throws Invalid_Argument if unused_bits is 8 or larger, if unused_bits is |
199 | | * non-zero for an empty BIT STRING, or if any of the unused bits are set. |
200 | | */ |
201 | | ASN1_BitString(std::span<const uint8_t> bytes, size_t unused_bits); |
202 | | |
203 | | /** |
204 | | * Return the bits, without the leading unused-bit count octet |
205 | | */ |
206 | 0 | std::span<const uint8_t> bytes() const { return std::span{m_bytes}; } |
207 | | |
208 | | /** |
209 | | * Return the number of unused bits in the final byte |
210 | | */ |
211 | 0 | size_t unused_bits() const { return m_unused_bits; } |
212 | | |
213 | | /** |
214 | | * Return the number of significant bits in this BIT STRING |
215 | | */ |
216 | | size_t bit_length() const; |
217 | | |
218 | | /** |
219 | | * Return the value of a single bit, counting from the most significant bit |
220 | | * of the first byte |
221 | | * |
222 | | * Throws Invalid_Argument if bit is not less than bit_length() |
223 | | */ |
224 | | bool bit_at(size_t bit) const; |
225 | | |
226 | | private: |
227 | | std::vector<uint8_t> m_bytes; |
228 | | size_t m_unused_bits = 0; |
229 | | }; |
230 | | |
231 | | /** |
232 | | * BER Encoded Object |
233 | | */ |
234 | | class BOTAN_PUBLIC_API(2, 0) BER_Object final { |
235 | | public: |
236 | | /** |
237 | | * Create an unset BER_Object |
238 | | */ |
239 | 0 | BER_Object() = default; |
240 | | |
241 | | /** |
242 | | * Copy constructor |
243 | | */ |
244 | | BER_Object(const BER_Object& other) = default; |
245 | | |
246 | | /** |
247 | | * Move constructor |
248 | | */ |
249 | 0 | BER_Object(BER_Object&& other) = default; |
250 | | |
251 | | /** |
252 | | * Copy assignment |
253 | | */ |
254 | 0 | BER_Object& operator=(const BER_Object& other) = default; |
255 | | |
256 | | /** |
257 | | * Move assignment |
258 | | */ |
259 | 0 | BER_Object& operator=(BER_Object&& other) = default; |
260 | | |
261 | | ~BER_Object(); |
262 | | |
263 | | /** |
264 | | * Return true if this object holds a value, ie if it was not read past the |
265 | | * end of the input |
266 | | */ |
267 | 0 | bool is_set() const { return m_type_tag != ASN1_Type::NoObject; } |
268 | | |
269 | | /** |
270 | | * Return the type and class tags combined into a single value |
271 | | */ |
272 | 0 | uint32_t tagging() const { return type_tag() | class_tag(); } |
273 | | |
274 | | /** |
275 | | * Return the type tag of this object |
276 | | */ |
277 | 0 | ASN1_Type type_tag() const { return m_type_tag; } |
278 | | |
279 | | /** |
280 | | * Return the class tag of this object |
281 | | */ |
282 | 0 | ASN1_Class class_tag() const { return m_class_tag; } |
283 | | |
284 | | /** |
285 | | * Return the type tag of this object, an alias for type_tag() |
286 | | */ |
287 | 0 | ASN1_Type type() const { return m_type_tag; } |
288 | | |
289 | | /** |
290 | | * Return the class tag of this object, an alias for class_tag() |
291 | | */ |
292 | 0 | ASN1_Class get_class() const { return m_class_tag; } |
293 | | |
294 | | /** |
295 | | * Return a pointer to the contents of this object, excluding the tag and |
296 | | * length header |
297 | | */ |
298 | 0 | const uint8_t* bits() const { return m_value.data(); } |
299 | | |
300 | | /** |
301 | | * Return the length in bytes of the contents of this object |
302 | | */ |
303 | 0 | size_t length() const { return m_value.size(); } |
304 | | |
305 | | /** |
306 | | * Return the contents of this object, excluding the tag and length header |
307 | | */ |
308 | 0 | std::span<const uint8_t> data() const { return std::span{m_value}; } |
309 | | |
310 | | /** |
311 | | * Throw BER_Decoding_Error unless this object has the expected tagging |
312 | | * |
313 | | * @param type_tag the expected type tag |
314 | | * @param class_tag the expected class tag |
315 | | * @param descr a description of the object, used in the exception message |
316 | | */ |
317 | | void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr = "object") const; |
318 | | |
319 | | /** |
320 | | * Return true if this object has the given tagging |
321 | | * |
322 | | * @param type_tag the expected type tag |
323 | | * @param class_tag the expected class tag |
324 | | */ |
325 | | bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const; |
326 | | |
327 | | /** |
328 | | * Return true if this object has the given tagging |
329 | | * |
330 | | * @param type_tag the expected type tag, typically a context specific tag number |
331 | | * @param class_tag the expected class tag |
332 | | */ |
333 | | bool is_a(int type_tag, ASN1_Class class_tag) const; |
334 | | |
335 | | private: |
336 | | ASN1_Type m_type_tag = ASN1_Type::NoObject; |
337 | | ASN1_Class m_class_tag = ASN1_Class::Universal; |
338 | | std::vector<uint8_t> m_value; |
339 | | |
340 | | friend class BER_Decoder; |
341 | | |
342 | | void set_tagging(ASN1_Type type_tag, ASN1_Class class_tag); |
343 | | |
344 | 0 | uint8_t* mutable_bits(size_t length) { |
345 | 0 | m_value.resize(length); |
346 | 0 | return m_value.data(); |
347 | 0 | } |
348 | | }; |
349 | | |
350 | | /* |
351 | | * ASN.1 Utility Functions |
352 | | */ |
353 | | class DataSource; |
354 | | |
355 | | namespace ASN1 { |
356 | | |
357 | | /** |
358 | | * Return the DER tag and length header of a SEQUENCE with the given contents length |
359 | | * @param contents_len the length in bytes of the SEQUENCE contents |
360 | | */ |
361 | | std::vector<uint8_t> der_sequence_header(size_t contents_len); |
362 | | |
363 | | /** |
364 | | * Return the contents wrapped in a DER SEQUENCE |
365 | | * @param val the contents of the SEQUENCE |
366 | | */ |
367 | | std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& val); |
368 | | |
369 | | /** |
370 | | * Return the contents wrapped in a DER SEQUENCE |
371 | | * @param bits the contents of the SEQUENCE |
372 | | * @param len the length of bits in bytes |
373 | | */ |
374 | | std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len); |
375 | | |
376 | | /** |
377 | | * Return the contents of a BER object interpreted as a string |
378 | | * @param obj the object whose contents are converted |
379 | | */ |
380 | | std::string to_string(const BER_Object& obj); |
381 | | |
382 | | /** |
383 | | * Heuristics tests; is this object possibly BER? |
384 | | * @param src a data source that will be peeked at but not modified |
385 | | */ |
386 | | bool maybe_BER(DataSource& src); |
387 | | |
388 | | } // namespace ASN1 |
389 | | |
390 | | /** |
391 | | * General BER Decoding Error Exception |
392 | | */ |
393 | | class BOTAN_PUBLIC_API(2, 0) BER_Decoding_Error : public Decoding_Error { |
394 | | public: |
395 | | /** |
396 | | * Create a BER decoding error |
397 | | * @param err a description of the problem encountered |
398 | | */ |
399 | | explicit BER_Decoding_Error(std::string_view err); |
400 | | }; |
401 | | |
402 | | /** |
403 | | * Exception For Incorrect BER Taggings |
404 | | */ |
405 | | class BOTAN_PUBLIC_API(2, 0) BER_Bad_Tag final : public BER_Decoding_Error { |
406 | | public: |
407 | | /** |
408 | | * Create a bad tag error |
409 | | * @param msg a description of the problem encountered |
410 | | * @param tagging the offending tagging value |
411 | | */ |
412 | | BER_Bad_Tag(std::string_view msg, uint32_t tagging); |
413 | | }; |
414 | | |
415 | | /** |
416 | | * This class represents ASN.1 object identifiers. |
417 | | */ |
418 | | class BOTAN_PUBLIC_API(2, 0) OID final : public ASN1_Object { |
419 | | public: |
420 | | /** |
421 | | * Create an uninitialised OID object |
422 | | */ |
423 | 0 | explicit OID() = default; |
424 | | |
425 | | /** |
426 | | * Construct an OID from a string. |
427 | | * @param str a string in the form "a.b.c" etc., where a,b,c are integers |
428 | | * |
429 | | * Note: it is currently required that each integer fit into 32 bits |
430 | | */ |
431 | | explicit OID(std::string_view str); |
432 | | |
433 | | /** |
434 | | * Initialize an OID from a sequence of integer values |
435 | | */ |
436 | | OID(std::initializer_list<uint32_t> init); |
437 | | |
438 | | /** |
439 | | * Initialize an OID from a vector of integer values |
440 | | */ |
441 | | explicit OID(std::vector<uint32_t>&& init); |
442 | | |
443 | | /** |
444 | | * Construct an OID from a string. |
445 | | * @param str a string in the form "a.b.c" etc., where a,b,c are numbers |
446 | | * or any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier") |
447 | | */ |
448 | | static OID from_string(std::string_view str); |
449 | | |
450 | | /** |
451 | | * Construct an OID from a name |
452 | | * @param name any known OID name (for example "RSA" or "X509v3.SubjectKeyIdentifier") |
453 | | */ |
454 | | static std::optional<OID> from_name(std::string_view name); |
455 | | |
456 | | /** |
457 | | * Register a new OID in the internal table |
458 | | */ |
459 | | static void register_oid(const OID& oid, std::string_view name); |
460 | | |
461 | | void encode_into(DER_Encoder& to) const override; |
462 | | void decode_from(BER_Decoder& from) override; |
463 | | |
464 | | /** |
465 | | * Find out whether this OID is empty |
466 | | * @return true is no OID value is set |
467 | | */ |
468 | 337k | bool empty() const { return m_id.empty(); } |
469 | | |
470 | | /** |
471 | | * Find out whether this OID has a value |
472 | | * @return true is this OID has a value |
473 | | */ |
474 | 169k | bool has_value() const { return !empty(); } |
475 | | |
476 | | /** |
477 | | * Get this OID as a dotted-decimal string |
478 | | * @return string representing this OID |
479 | | */ |
480 | | std::string to_string() const; |
481 | | |
482 | | /** |
483 | | * If there is a known name associated with this OID, return that. |
484 | | * Otherwise return the result of to_string |
485 | | */ |
486 | | std::string to_formatted_string() const; |
487 | | |
488 | | /** |
489 | | * If there is a known name associated with this OID, return that. |
490 | | * Otherwise return the empty string. |
491 | | */ |
492 | | std::string human_name_or_empty() const; |
493 | | |
494 | | /** |
495 | | * If there is a known name associated with this OID, return that. |
496 | | * Otherwise return nullopt. |
497 | | */ |
498 | | std::optional<std::string> registered_name() const; |
499 | | |
500 | | /** |
501 | | * Return true if the OID in *this is registered in the internal |
502 | | * set of constants as a known OID. |
503 | | */ |
504 | | bool registered_oid() const; |
505 | | |
506 | | /** |
507 | | * Compare two OIDs. |
508 | | * @return true if they are equal, false otherwise |
509 | | */ |
510 | 507k | bool operator==(const OID& other) const { return m_id == other.m_id; } |
511 | | |
512 | | /** |
513 | | * Return a hash code for this OID |
514 | | * |
515 | | * This value is only meant as a std::unordered_map hash and |
516 | | * can change value from release to release. |
517 | | */ |
518 | | uint64_t hash_code() const; |
519 | | |
520 | | /** |
521 | | * Check if this OID matches the provided value |
522 | | */ |
523 | | bool matches(std::initializer_list<uint32_t> other) const; |
524 | | |
525 | | /** |
526 | | * Get this OID as list (vector) of its components. |
527 | | * @return vector representing this OID |
528 | | */ |
529 | | BOTAN_DEPRECATED("Do not access the integer values, use eg to_string") |
530 | 0 | const std::vector<uint32_t>& get_components() const { |
531 | 0 | return m_id; |
532 | 0 | } |
533 | | |
534 | | /** |
535 | | * Get this OID as list (vector) of its components. |
536 | | * @return vector representing this OID |
537 | | */ |
538 | | BOTAN_DEPRECATED("Do not access the integer values, use eg to_string") |
539 | 0 | const std::vector<uint32_t>& get_id() const { |
540 | 0 | return m_id; |
541 | 0 | } |
542 | | |
543 | | private: |
544 | | std::vector<uint32_t> m_id; |
545 | | }; |
546 | | |
547 | | /** |
548 | | * Write an OID to a stream, in the format produced by OID::to_string |
549 | | * @param out the stream to write to |
550 | | * @param oid the OID to write |
551 | | * @return the stream |
552 | | */ |
553 | | BOTAN_PUBLIC_API(3, 0) std::ostream& operator<<(std::ostream& out, const OID& oid); |
554 | | |
555 | | /** |
556 | | * Compare two OIDs. |
557 | | * @param a the first OID |
558 | | * @param b the second OID |
559 | | * @return true if a is not equal to b |
560 | | */ |
561 | 27 | inline bool operator!=(const OID& a, const OID& b) { |
562 | 27 | return !(a == b); |
563 | 27 | } |
564 | | |
565 | | /** |
566 | | * Compare two OIDs. |
567 | | * @param a the first OID |
568 | | * @param b the second OID |
569 | | * @return true if a is lexicographically smaller than b |
570 | | */ |
571 | | BOTAN_PUBLIC_API(2, 0) bool operator<(const OID& a, const OID& b); |
572 | | |
573 | | /** |
574 | | * ASN.1 string type |
575 | | * This class normalizes all inputs to a UTF-8 std::string |
576 | | */ |
577 | | class BOTAN_PUBLIC_API(2, 0) ASN1_String final : public ASN1_Object { |
578 | | public: |
579 | | void encode_into(DER_Encoder& to) const override; |
580 | | void decode_from(BER_Decoder& from) override; |
581 | | |
582 | | /** |
583 | | * Return the string type tag this value was encoded with |
584 | | */ |
585 | 0 | ASN1_Type tagging() const { return m_tag; } |
586 | | |
587 | | /** |
588 | | * Return the value of this string, converted to UTF-8 |
589 | | */ |
590 | 0 | const std::string& value() const { return m_utf8_str; } |
591 | | |
592 | | /** |
593 | | * Return the length in bytes of the UTF-8 representation of this string |
594 | | */ |
595 | 0 | size_t size() const { return value().size(); } |
596 | | |
597 | | /** |
598 | | * Return true if this string is empty |
599 | | */ |
600 | 0 | bool empty() const { return m_utf8_str.empty(); } |
601 | | |
602 | | /** |
603 | | * Return true iff this is a tag for a known string type we can handle. |
604 | | */ |
605 | | static bool is_string_type(ASN1_Type tag); |
606 | | |
607 | | /** |
608 | | * Compare two strings, ignoring the type tag they were encoded with |
609 | | */ |
610 | 0 | bool operator==(const ASN1_String& other) const { return value() == other.value(); } |
611 | | |
612 | 0 | friend bool operator<(const ASN1_String& a, const ASN1_String& b) { return a.value() < b.value(); } |
613 | | |
614 | | /** |
615 | | * Create a string, tagged as PrintableString if possible and otherwise as |
616 | | * Utf8String |
617 | | * |
618 | | * @param utf8 the value of the string, encoded as UTF-8 |
619 | | */ |
620 | | explicit ASN1_String(std::string_view utf8 = ""); |
621 | | |
622 | | /** |
623 | | * Create a string with a specific type tag |
624 | | * |
625 | | * @param utf8 the value of the string, encoded as UTF-8 |
626 | | * @param tag the string type tag to encode this value with |
627 | | * |
628 | | * Throws Invalid_Argument if tag is not a string type that is a subset of |
629 | | * UTF-8, or if utf8 is not a valid value for that type. |
630 | | */ |
631 | | ASN1_String(std::string_view utf8, ASN1_Type tag); |
632 | | |
633 | | private: |
634 | | std::vector<uint8_t> m_data; |
635 | | std::string m_utf8_str; |
636 | | ASN1_Type m_tag; |
637 | | }; |
638 | | |
639 | | /** |
640 | | * Algorithm Identifier |
641 | | */ |
642 | | class BOTAN_PUBLIC_API(2, 0) AlgorithmIdentifier final : public ASN1_Object { |
643 | | public: |
644 | | /** |
645 | | * Selects how an absent parameters field is encoded |
646 | | */ |
647 | | enum Encoding_Option : uint8_t { USE_NULL_PARAM, USE_EMPTY_PARAM }; /* NOLINT(*-use-enum-class) */ |
648 | | |
649 | | void encode_into(DER_Encoder& to) const override; |
650 | | void decode_from(BER_Decoder& from) override; |
651 | | |
652 | | /** |
653 | | * Create an empty AlgorithmIdentifier |
654 | | */ |
655 | 0 | AlgorithmIdentifier() = default; |
656 | | |
657 | | /** |
658 | | * Create an AlgorithmIdentifier with no parameters |
659 | | * @param oid the algorithm OID |
660 | | * @param enc whether the empty parameters are encoded as NULL or omitted |
661 | | */ |
662 | | AlgorithmIdentifier(const OID& oid, Encoding_Option enc); |
663 | | |
664 | | /** |
665 | | * Create an AlgorithmIdentifier with no parameters |
666 | | * @param oid_name a name or dotted decimal string identifying the algorithm |
667 | | * @param enc whether the empty parameters are encoded as NULL or omitted |
668 | | */ |
669 | | AlgorithmIdentifier(std::string_view oid_name, Encoding_Option enc); |
670 | | |
671 | | /** |
672 | | * Create an AlgorithmIdentifier |
673 | | * @param oid the algorithm OID |
674 | | * @param params the DER encoded parameters |
675 | | */ |
676 | | AlgorithmIdentifier(const OID& oid, const std::vector<uint8_t>& params); |
677 | | |
678 | | /** |
679 | | * Create an AlgorithmIdentifier |
680 | | * @param oid_name a name or dotted decimal string identifying the algorithm |
681 | | * @param params the DER encoded parameters |
682 | | */ |
683 | | AlgorithmIdentifier(std::string_view oid_name, const std::vector<uint8_t>& params); |
684 | | |
685 | | /** |
686 | | * Return the algorithm OID |
687 | | */ |
688 | 0 | const OID& oid() const { return m_oid; } |
689 | | |
690 | | /** |
691 | | * Return the DER encoded parameters, which may be empty |
692 | | */ |
693 | 0 | const std::vector<uint8_t>& parameters() const { return m_parameters; } |
694 | | |
695 | | /** |
696 | | * Return the algorithm OID |
697 | | */ |
698 | 0 | BOTAN_DEPRECATED("Use AlgorithmIdentifier::oid") const OID& get_oid() const { return m_oid; } |
699 | | |
700 | | /** |
701 | | * Return the DER encoded parameters, which may be empty |
702 | | */ |
703 | 0 | BOTAN_DEPRECATED("Use AlgorithmIdentifier::parameters") const std::vector<uint8_t>& get_parameters() const { |
704 | 0 | return m_parameters; |
705 | 0 | } |
706 | | |
707 | | /** |
708 | | * Return true if the parameters consist of a single DER encoded NULL |
709 | | */ |
710 | | bool parameters_are_null() const; |
711 | | |
712 | | /** |
713 | | * Return true if the parameters field is absent |
714 | | */ |
715 | 0 | bool parameters_are_empty() const { return m_parameters.empty(); } |
716 | | |
717 | | /** |
718 | | * Return true if the parameters field is absent or a single DER encoded NULL |
719 | | */ |
720 | 0 | bool parameters_are_null_or_empty() const { return parameters_are_empty() || parameters_are_null(); } |
721 | | |
722 | | /** |
723 | | * Return true if neither the OID nor the parameters have been set |
724 | | */ |
725 | 0 | bool empty() const { return m_oid.empty() && m_parameters.empty(); } |
726 | | |
727 | | private: |
728 | | OID m_oid; |
729 | | std::vector<uint8_t> m_parameters; |
730 | | }; |
731 | | |
732 | | /** |
733 | | * Compare two AlgorithmIdentifiers |
734 | | * |
735 | | * @param x the first AlgorithmIdentifier |
736 | | * @param y the second AlgorithmIdentifier |
737 | | * @return true if x is equal to y |
738 | | */ |
739 | | BOTAN_PUBLIC_API(2, 0) bool operator==(const AlgorithmIdentifier& x, const AlgorithmIdentifier& y); |
740 | | |
741 | | /** |
742 | | * Compare two AlgorithmIdentifiers |
743 | | * |
744 | | * @param x the first AlgorithmIdentifier |
745 | | * @param y the second AlgorithmIdentifier |
746 | | * @return true if x is not equal to y |
747 | | */ |
748 | | BOTAN_PUBLIC_API(2, 0) bool operator!=(const AlgorithmIdentifier& x, const AlgorithmIdentifier& y); |
749 | | |
750 | | } // namespace Botan |
751 | | |
752 | | /** |
753 | | * Specialization of std::hash allowing OIDs to be used as keys in unordered containers |
754 | | */ |
755 | | template <> |
756 | | class std::hash<Botan::OID> { |
757 | | public: |
758 | | /** |
759 | | * Return a hash of the OID; see OID::hash_code |
760 | | */ |
761 | 20 | size_t operator()(const Botan::OID& oid) const noexcept { return static_cast<size_t>(oid.hash_code()); } |
762 | | }; |
763 | | |
764 | | /* |
765 | | In 3.11 ASN1_Time was split out to its own header as <chrono> is huge in C++20 |
766 | | However we continue to include this header (when not building the library), |
767 | | to avoid breaking applications which would expect it to still be available. |
768 | | |
769 | | TODO(Botan4) remove this |
770 | | */ |
771 | | #if defined(BOTAN_AMALGAMATION_H_) || (!defined(BOTAN_IS_BEING_BUILT) && !defined(__clang_analyzer__)) |
772 | | #include <botan/asn1_time.h> |
773 | | #endif |
774 | | |
775 | | #endif |