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