Botan 3.14.1
Crypto and TLS for C&
ber_dec.h
Go to the documentation of this file.
1/*
2* BER Decoder
3* (C) 1999-2010,2018 Jack Lloyd
4*
5* Botan is released under the Simplified BSD License (see license.txt)
6*/
7
8#ifndef BOTAN_BER_DECODER_H_
9#define BOTAN_BER_DECODER_H_
10
11#include <botan/asn1_obj.h>
12#include <botan/secmem.h>
13#include <botan/strong_type_helpers.h>
14#include <cstring>
15#include <memory>
16#include <optional>
17#include <span>
18#include <type_traits>
19#include <utility>
20
21namespace Botan {
22
23class BigInt;
24class DataSource;
25
26/**
27* BER Decoding Object
28*/
29class BOTAN_PUBLIC_API(2, 0) BER_Decoder final {
30 public:
31 /**
32 * Controls what encoding rules the decoder accepts.
33 */
34 class BOTAN_PUBLIC_API(3, 12) Limits final {
35 public:
36 /**
37 * The default maximum size in bytes of a single decoded object.
38 */
39 static constexpr size_t DefaultMaxObjectSize = 128 * 1024 * 1024;
40
41 /**
42 * Accept only DER encodings
43 */
44 static Limits DER() { return Limits(false, 0, false, DefaultMaxObjectSize, false); }
45
46 /**
47 * Accept non-canonical BER encodings.
48 *
49 * @param max_nested_indef maximum number of nested indefinite-length encodings accepted
50 */
51 static Limits BER(size_t max_nested_indef = 16) {
52 return Limits(true, max_nested_indef, false, DefaultMaxObjectSize, false);
53 }
54
55 /**
56 * If true, non-canonical BER encodings are accepted
57 */
58 bool allow_ber_encoding() const { return m_allow_ber; }
59
60 /**
61 * If true, only DER encodings are accepted
62 */
63 bool require_der_encoding() const { return !allow_ber_encoding(); }
64
65 /**
66 * The maximum number of nested indefinite-length encodings accepted
67 */
68 size_t max_nested_indefinite_length() const { return m_max_nested_indef; }
69
70 /**
71 * If true, a standalone EOC marker (one that does not terminate an
72 * indefinite-length encoding) is skipped rather than rejected. Some
73 * BER producers emit trailing EOC markers; accepting them is needed to
74 * parse such data (eg CMS signatures in PDFs). Off by default.
75 */
76 bool allow_standalone_eoc() const { return m_allow_standalone_eoc; }
77
78 /**
79 * The maximum size in bytes of a single decoded object, or nullopt if
80 * no object size limit is enforced.
81 */
82 std::optional<size_t> max_object_size() const { return m_max_object_size; }
83
84 /**
85 * If true, a DER component that is explicitly encoded with a value
86 * equal to its DEFAULT is rejected (such components must be omitted in
87 * DER). Only applies in DER mode and only to fields decoded via
88 * decode_default(). Off by default, since Botan and many other
89 * implementations emit such components. See decode_default().
90 */
91 bool reject_default_value_encoding() const { return m_reject_default_value_encoding; }
92
93 /**
94 * Return a copy of these limits that tolerates standalone EOC markers.
95 * See allow_standalone_eoc().
96 */
98 Limits copy = *this;
99 copy.m_allow_standalone_eoc = true;
100 return copy;
101 }
102
103 /**
104 * Return a copy of these limits with the given maximum object size.
105 * A value of nullopt disables the object size limit. See
106 * max_object_size().
107 */
108 Limits with_max_object_size(std::optional<size_t> max_object_size) const {
109 Limits copy = *this;
110 copy.m_max_object_size = max_object_size;
111 return copy;
112 }
113
114 /**
115 * Return a copy of these limits that rejects DER components encoded
116 * equal to their DEFAULT value. See reject_default_value_encoding().
117 */
119 Limits copy = *this;
120 copy.m_reject_default_value_encoding = true;
121 return copy;
122 }
123
124 /**
125 * Compare two sets of limits for equality
126 */
127 bool operator==(const Limits&) const = default;
128
129 private:
130 Limits(bool allow_ber,
131 size_t max_nested_indef,
133 std::optional<size_t> max_object_size,
135 m_allow_ber(allow_ber),
136 m_max_nested_indef(max_nested_indef),
137 m_allow_standalone_eoc(allow_standalone_eoc),
138 m_max_object_size(max_object_size),
139 m_reject_default_value_encoding(reject_default_value_encoding) {}
140
141 bool m_allow_ber;
142 size_t m_max_nested_indef;
143 bool m_allow_standalone_eoc;
144 std::optional<size_t> m_max_object_size;
145 bool m_reject_default_value_encoding;
146 };
147
148 // TODO(Botan4) have the various Limits arguments default to Limits::DER()
149
150 /**
151 * Set up to BER decode the data in buf of length len
152 */
153 BOTAN_DEPRECATED("Use BER_Decoder(span) constructor")
154 BER_Decoder(const uint8_t buf[], size_t len, Limits limits = Limits::BER()) :
155 BER_Decoder(std::span{buf, len}, limits) {}
156
157 /**
158 * Set up to BER decode the data in buf
159 */
160 explicit BER_Decoder(std::span<const uint8_t> buf, Limits limits = Limits::BER());
161
162 /**
163 * Set up to BER decode the data in src
164 */
165 explicit BER_Decoder(DataSource& src, Limits limits = Limits::BER());
166
167 /**
168 * Set up to BER decode the data in obj
169 */
172
173 /**
174 * Set up to BER decode the data in obj
175 * TODO(Botan4) remove this?
176 */
177 BOTAN_FUTURE_EXPLICIT BER_Decoder(BER_Object&& obj) : BER_Decoder(std::move(obj), nullptr) {}
178
179 /**
180 * Set up to BER decode the data in obj, taking ownership of its contents
181 */
182 BER_Decoder(BER_Object&& obj, Limits limits);
183
184 BER_Decoder(const BER_Decoder& other) = delete;
185
186 /**
187 * Move constructor
188 */
189 BER_Decoder(BER_Decoder&& other) noexcept;
190
192
193 /**
194 * Move assignment
195 */
197
198 /**
199 * Returns the limits currently applied to this decoder
200 */
201 Limits limits() const { return m_limits; }
202
203 /**
204 * Get the next object in the data stream.
205 * If EOF, returns an object with type NO_OBJECT.
206 */
207 BER_Object get_next_object();
208
209 /**
210 * Get the next object in the data stream, storing it in @p ber
211 *
212 * If EOF, @p ber is set to an object with type NO_OBJECT.
213 */
215 ber = get_next_object();
216 return (*this);
217 }
218
219 /**
220 * Peek at the next object without removing it from the stream
221 *
222 * If an object has been pushed, then it returns that object.
223 * Otherwise it reads the next object and pushes it. Thus, a you
224 * call peek_next_object followed by push_back without a
225 * subsequent read, it will fail.
226 */
227 const BER_Object& peek_next_object();
228
229 /**
230 * Push an object back onto the stream. Throws if another
231 * object was previously pushed and has not been subsequently
232 * read out.
233 */
234 void push_back(const BER_Object& obj);
235
236 /**
237 * Push an object back onto the stream. Throws if another
238 * object was previously pushed and has not been subsequently
239 * read out.
240 */
241 void push_back(BER_Object&& obj);
242
243 /**
244 * Return true if there is at least one more item remaining
245 */
246 bool more_items() const;
247
248 /**
249 * Verify the stream is concluded, throws otherwise.
250 * Returns (*this)
251 */
252 BER_Decoder& verify_end();
253
254 /**
255 * Verify the stream is concluded, throws otherwise.
256 * Returns (*this)
257 */
258 BER_Decoder& verify_end(std::string_view err_msg);
259
260 /**
261 * Discard any data that remains unread
262 * Returns (*this)
263 */
264 BER_Decoder& discard_remaining();
265
266 /**
267 * Start decoding a constructed object, returning a decoder for its contents.
268 * The returned decoder must be closed with end_cons().
269 *
270 * @param type_tag the expected type tag
271 * @param class_tag the expected class tag
272 */
273 BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag);
274
275 /**
276 * Start decoding a SEQUENCE, returning a decoder for its contents.
277 * The returned decoder must be closed with end_cons().
278 */
280
281 /**
282 * Start decoding a SET, returning a decoder for its contents.
283 * The returned decoder must be closed with end_cons().
284 */
286
287 /**
288 * Start decoding an IMPLICIT context specific constructed object, returning
289 * a decoder for its contents. The returned decoder must be closed with
290 * end_cons().
291 *
292 * @param tag the expected context specific tag number
293 */
297
298 /**
299 * Start decoding an EXPLICIT context specific constructed object, returning
300 * a decoder for its contents. The returned decoder must be closed with
301 * end_cons().
302 *
303 * @param tag the expected context specific tag number
304 */
308
309 /**
310 * Finish decoding a constructed data, throws if any data remains.
311 * Returns the parent of *this (ie the object on which start_cons was called).
312 */
313 BER_Decoder& end_cons();
314
315 /**
316 * Get next object and copy value to POD type
317 * Asserts value length is equal to POD type sizeof.
318 * Asserts Type tag and optional Class tag according to parameters.
319 * Copy value to POD type (struct, union, C-style array, std::array, etc.).
320 * @param out POD type reference where to copy object value
321 * @param type_tag ASN1_Type enum to assert type on object read
322 * @param class_tag ASN1_Type enum to assert class on object read (default: CONTEXT_SPECIFIC)
323 * @return this reference
324 */
325 template <typename T>
327 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
328 {
329 const BER_Object obj = get_next_value(sizeof(T), type_tag, class_tag);
330
331 std::memcpy(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
332
333 return (*this);
334 }
335
336 /**
337 * Save all the bytes remaining in the source
338 *
339 * @param out_wrapped where the remaining bytes are written
340 */
341 template <typename T>
343 BER_Decoder& raw_bytes(T& out_wrapped) {
344 auto& out = unwrap_strong_type(out_wrapped);
345 out.clear();
346 uint8_t buf[64];
347 while(const size_t got = this->read_bytes(std::span{buf})) {
348 out.insert(out.end(), buf, buf + got);
349 }
350 return (*this);
351 }
352
353 /**
354 * Decode a BER encoded NULL, throwing if the next object is anything else
355 */
356 BER_Decoder& decode_null();
357
358 /**
359 * Decode a BER encoded BOOLEAN
360 */
362
363 /**
364 * Decode a small BER encoded INTEGER
365 */
367
368 /**
369 * Decode a BER encoded INTEGER
370 */
372
373 /**
374 * Decode the next object as an OCTET STRING and return its contents
375 */
376 std::vector<uint8_t> get_next_octet_string() {
377 std::vector<uint8_t> out_vec;
379 return out_vec;
380 }
381
382 /**
383 * BER decode a BIT STRING or OCTET STRING
384 *
385 * @param out where the contents are written
386 * @param real_type either ASN1_Type::OctetString or ASN1_Type::BitString
387 */
388 template <typename T>
390 BER_Decoder& decode(T& out, ASN1_Type real_type) {
391 return decode(unwrap_strong_type(out), real_type, real_type, ASN1_Class::Universal);
392 }
393
394 /**
395 * Decode a BIT STRING or OCTET STRING with an IMPLICIT tagging
396 *
397 * @param v where the contents are written
398 * @param real_type either ASN1_Type::OctetString or ASN1_Type::BitString
399 * @param type_tag the expected type tag
400 * @param class_tag the expected class tag
401 */
402 template <typename T>
405 ASN1_Type real_type,
406 ASN1_Type type_tag,
408 return decode_into_byte_vector(unwrap_strong_type(v), real_type, type_tag, class_tag);
409 }
410
411 /**
412 * Decode a BOOLEAN with an IMPLICIT tagging
413 *
414 * @param v where the value is written
415 * @param type_tag the expected type tag
416 * @param class_tag the expected class tag
417 */
418 BER_Decoder& decode(bool& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
419
420 /**
421 * Decode a small INTEGER with an IMPLICIT tagging
422 *
423 * @param v where the value is written
424 * @param type_tag the expected type tag
425 * @param class_tag the expected class tag
426 */
427 BER_Decoder& decode(size_t& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
428
429 /**
430 * Decode an INTEGER with an IMPLICIT tagging
431 *
432 * @param v where the value is written
433 * @param type_tag the expected type tag
434 * @param class_tag the expected class tag
435 */
436 BER_Decoder& decode(BigInt& v, ASN1_Type type_tag, ASN1_Class class_tag = ASN1_Class::ContextSpecific);
437
438 /**
439 * Decode a BIT STRING, retaining the count of unused bits
440 *
441 * @param out where the value is written
442 * @param type_tag the expected type tag
443 * @param class_tag the expected class tag
444 */
445 BER_Decoder& decode_bitstring(ASN1_BitString& out,
448
449 /**
450 * Decode a BIT STRING, throwing unless it has no unused bits
451 *
452 * @param out_wrapped where the bits are written
453 * @param type_tag the expected type tag
454 * @param class_tag the expected class tag
455 */
456 template <typename T>
460 ASN1_Class class_tag = ASN1_Class::Universal) {
461 auto& out = unwrap_strong_type(out_wrapped);
462 ASN1_BitString bits;
463 decode_bitstring(bits, type_tag, class_tag);
464
465 if(bits.unused_bits() != 0) {
466 throw Decoding_Error("Expected octet-aligned BIT STRING");
467 }
468
469 out.assign(bits.bytes().begin(), bits.bytes().end());
470 return (*this);
471 }
472
473 /**
474 * Decode a BIT STRING that is actually a bit set, such as X.509 KeyUsage
475 *
476 * The first bit of the encoding becomes the most significant of the @p width
477 * bits of @p bits. Throws if more than @p width bits are encoded, or in DER
478 * mode if the encoding is not minimal (ie the final bit is unset).
479 *
480 * @param bits where the bit set is written
481 * @param width the number of named bits, at most 64
482 * @param type_tag the expected type tag
483 * @param class_tag the expected class tag
484 */
485 BER_Decoder& decode_named_bitstring(uint64_t& bits,
486 size_t width,
489
490 /**
491 * Request an object decode itself from this stream
492 *
493 * @param obj the object to decode into
494 * @param type_tag must be NoObject; implicit tagging is not supported here
495 * @param class_tag must be NoObject; implicit tagging is not supported here
496 */
497 BER_Decoder& decode(ASN1_Object& obj,
499 ASN1_Class class_tag = ASN1_Class::NoObject);
500
501 /**
502 * Decode an integer value which is typed as an octet string
503 */
504 BER_Decoder& decode_octet_string_bigint(BigInt& b);
505
506 /**
507 * Decode a non-negative INTEGER that fits in at most @p T_bytes bytes
508 *
509 * Throws BER_Decoding_Error if the value is negative or too large.
510 *
511 * @param type_tag the expected type tag
512 * @param class_tag the expected class tag
513 * @param T_bytes the maximum size of the value in bytes, at most 8
514 */
515 uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes);
516
517 /**
518 * Decode an INTEGER into an integral type, throwing if it does not fit
519 *
520 * @param out where the value is written
521 */
522 template <typename T>
526
527 /**
528 * Decode an INTEGER with an IMPLICIT tagging into an integral type,
529 * throwing if it does not fit
530 *
531 * @param out where the value is written
532 * @param type_tag the expected type tag
533 * @param class_tag the expected class tag
534 */
535 template <typename T>
537 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
538 return (*this);
539 }
540
541 /**
542 * Decode an OPTIONAL field, setting @p out to @p default_value if absent
543 *
544 * @param out where the value is written
545 * @param type_tag the expected type tag
546 * @param class_tag the expected class tag
547 * @param default_value the value used if the field is not present
548 */
549 template <typename T>
550 BER_Decoder& decode_optional(T& out, ASN1_Type type_tag, ASN1_Class class_tag, const T& default_value = T()) {
551 std::optional<T> optval;
552 this->decode_optional(optval, type_tag, class_tag);
553 out = optval ? *optval : default_value;
554 return (*this);
555 }
556
557 /**
558 * Decode a field carrying an ASN.1 DEFAULT value: if the field is absent
559 * @p out is set to @p default_value. Unlike decode_optional this is only
560 * for fields with a DEFAULT (not bare OPTIONAL fields): when the decoder is
561 * configured with Limits::with_default_value_encoding_rejected() and is in
562 * DER mode, a field that is present but equal to @p default_value is
563 * rejected, since DER requires such components to be omitted.
564 */
565 template <typename T>
566 BER_Decoder& decode_default(T& out, ASN1_Type type_tag, ASN1_Class class_tag, const T& default_value) {
567 std::optional<T> optval;
568 this->decode_optional(optval, type_tag, class_tag);
569 if(optval.has_value()) {
570 if(m_limits.require_der_encoding() && m_limits.reject_default_value_encoding() &&
571 *optval == default_value) {
572 throw BER_Decoding_Error("DER component encoded with its DEFAULT value");
573 }
574 out = std::move(*optval);
575 } else {
576 out = default_value;
577 }
578 return (*this);
579 }
580
581 /**
582 * Decode an OPTIONAL field, setting @p optval to nullopt if absent
583 *
584 * @param optval where the value is written
585 * @param type_tag the expected type tag
586 * @param class_tag the expected class tag
587 */
588 template <typename T>
589 BER_Decoder& decode_optional(std::optional<T>& optval, ASN1_Type type_tag, ASN1_Class class_tag);
590
591 /**
592 * Decode an OPTIONAL IMPLICIT tagged field, setting @p out to
593 * @p default_value if absent
594 *
595 * @param out where the value is written
596 * @param type_tag the expected type tag
597 * @param class_tag the expected class tag
598 * @param real_type the type tag the contents should be parsed as
599 * @param real_class the class tag the contents should be parsed as
600 * @param default_value the value used if the field is not present
601 */
602 template <typename T>
603 BER_Decoder& decode_optional_implicit(T& out,
604 ASN1_Type type_tag,
605 ASN1_Class class_tag,
606 ASN1_Type real_type,
607 ASN1_Class real_class,
608 const T& default_value = T());
609
610 /**
611 * Decode an OPTIONAL field identified by a context-specific tag number.
612 *
613 * If the next object is tagged [tag_no] with class @p class_tag, @p fn is
614 * invoked to decode it (fn must consume exactly that object). Otherwise the
615 * stream is left unchanged and fn is not called.
616 *
617 * For a SEQUENCE of optional tagged fields that (per DER) appear at most
618 * once and in increasing tag order: call this once per field in tag order,
619 * then end_cons(), which rejects any unconsumed object (i.e. a duplicate,
620 * out-of-order, or unknown-tag field).
621 */
622 template <typename F>
623 BER_Decoder& decode_optional_field(uint32_t tag_no, ASN1_Class class_tag, F&& fn) {
624 if(peek_next_object().is_a(tag_no, class_tag)) {
625 std::forward<F>(fn)(*this);
626 }
627 return (*this);
628 }
629
630 /**
631 * Decode an already-extracted BER_Object as if its tag were
632 * `real_type`/`real_class`. Used to consume IMPLICIT-tagged values
633 * whose body matches a different universal type (e.g. a
634 * context-specific [8] body that should be parsed as an OID).
635 */
636 template <typename T>
637 BER_Decoder& decode_implicit(BER_Object obj, T& out, ASN1_Type real_type, ASN1_Class real_class) {
638 obj.set_tagging(real_type, real_class);
639 push_back(std::move(obj));
640 if constexpr(std::is_base_of_v<ASN1_Object, T>) {
641 // The object was re-tagged above; decode_from checks the real tag itself
642 return decode(out);
643 } else {
644 return decode(out, real_type, real_class);
645 }
646 }
647
648 /**
649 * Read the next object, requiring it be tagged `type_tag`/`class_tag`, and
650 * decode it as if its tag were `real_type`/`real_class`.
651 *
652 * @param out where the value is written
653 * @param type_tag the expected type tag
654 * @param class_tag the expected class tag
655 * @param real_type the type tag the contents should be parsed as
656 * @param real_class the class tag the contents should be parsed as
657 */
658 template <typename T>
660 T& out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class) {
662 obj.assert_is_a(type_tag, class_tag);
663 return decode_implicit(std::move(obj), out, real_type, real_class);
664 }
665
666 /**
667 * Decode a constructed object containing a list of homogeneously typed values
668 *
669 * @param vec where the values are appended
670 * @param type_tag the expected type tag of the constructed object
671 * @param class_tag the expected class tag of the constructed object
672 */
673 template <typename T>
674 BER_Decoder& decode_list(std::vector<T>& vec,
677
678 /**
679 * Decode a list of homogeneously typed values if one is present
680 *
681 * @param vec where the values are appended
682 * @param type_tag the expected type tag of the constructed object
683 * @param class_tag the expected class tag of the constructed object
684 * @return true if the list was present
685 */
686 template <typename T>
687 bool decode_optional_list(std::vector<T>& vec,
690
691 /**
692 * Decode a value and throw unless it equals the expected value
693 *
694 * @param expected the value the encoding must have
695 * @param error_msg the message of the exception thrown on a mismatch
696 */
697 template <typename T>
698 BER_Decoder& decode_and_check(const T& expected, std::string_view error_msg) {
699 T actual;
700 decode(actual);
701
702 if(actual != expected) {
703 throw Decoding_Error(error_msg);
704 }
705
706 return (*this);
707 }
708
709 /**
710 * Decode an OPTIONAL string type, clearing @p out if it is absent
711 *
712 * @param out_wrapped where the contents are written
713 * @param real_type the type tag the contents should be parsed as
714 * @param expected_tag the expected tag number
715 * @param class_tag the expected class tag
716 */
717 template <typename T>
720 ASN1_Type real_type,
721 uint32_t expected_tag,
724 auto& out = unwrap_strong_type(out_wrapped);
725
726 const ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
727
728 if(obj.is_a(type_tag, class_tag)) {
729 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
730 BER_Decoder(obj, m_limits).decode(out, real_type).verify_end();
731 } else {
732 push_back(std::move(obj));
733 decode(out, real_type, type_tag, class_tag);
734 }
735 } else {
736 out.clear();
737 push_back(std::move(obj));
738 }
739
740 return (*this);
741 }
742
743 /**
744 * Decode an OPTIONAL string type, clearing @p out if it is absent
745 *
746 * @param out where the contents are written
747 * @param real_type the type tag the contents should be parsed as
748 * @param expected_tag the expected type tag
749 * @param class_tag the expected class tag
750 */
751 template <typename T>
754 ASN1_Type real_type,
755 ASN1_Type expected_tag,
757 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
758 }
759
760 /**
761 * Decode an OPTIONAL BIT STRING with no unused bits, clearing @p out if it
762 * is absent
763 *
764 * @param out_wrapped where the bits are written
765 * @param expected_tag the expected tag number
766 * @param class_tag the expected class tag
767 */
768 template <typename T>
771 uint32_t expected_tag,
774 auto& out = unwrap_strong_type(out_wrapped);
775
776 const ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
777
778 if(obj.is_a(type_tag, class_tag)) {
779 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
780 BER_Decoder(obj, m_limits).decode_octet_aligned_bitstring(out).verify_end();
781 } else {
782 push_back(std::move(obj));
783 decode_octet_aligned_bitstring(out, type_tag, class_tag);
784 }
785 } else {
786 out.clear();
787 push_back(std::move(obj));
788 }
789
790 return (*this);
791 }
792
793 /**
794 * Decode an OPTIONAL BIT STRING with no unused bits, clearing @p out if it
795 * is absent
796 *
797 * @param out where the bits are written
798 * @param expected_tag the expected type tag
799 * @param class_tag the expected class tag
800 */
801 template <typename T>
804 ASN1_Type expected_tag,
806 return decode_optional_octet_aligned_bitstring(out, static_cast<uint32_t>(expected_tag), class_tag);
807 }
808
810
811 private:
812 BER_Decoder(BER_Object&& obj, BER_Decoder* parent);
813
814 std::optional<uint8_t> read_next_byte();
815
816 size_t read_bytes(std::span<uint8_t> out);
817
818 BER_Object get_next_value(size_t sizeofT, ASN1_Type type_tag, ASN1_Class class_tag);
819
820 BER_Decoder& decode_into_byte_vector(std::vector<uint8_t>& v,
821 ASN1_Type real_type,
822 ASN1_Type type_tag,
824
825 BER_Decoder& decode_into_byte_vector(secure_vector<uint8_t>& v,
826 ASN1_Type real_type,
827 ASN1_Type type_tag,
829
830 Limits m_limits;
831 BER_Decoder* m_parent = nullptr;
832 BER_Object m_pushed;
833 // either m_data_src.get() or an unowned pointer
834 DataSource* m_source;
835 std::unique_ptr<DataSource> m_data_src;
836};
837
838/**
839* Decode an OPTIONAL or DEFAULT element
840*/
841template <typename T>
842BER_Decoder& BER_Decoder::decode_optional(std::optional<T>& optval, ASN1_Type type_tag, ASN1_Class class_tag) {
844
845 if(obj.is_a(type_tag, class_tag)) {
846 T out{};
847 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
848 BER_Decoder(obj, m_limits).decode(out).verify_end();
849 } else {
850 this->push_back(std::move(obj));
851 if constexpr(std::is_base_of_v<ASN1_Object, T>) {
852 // Object types check the tag in decode_from; a re-tagging
853 // implicit override of an object is not supported here
854 this->decode(out);
855 } else {
856 this->decode(out, type_tag, class_tag);
857 }
858 }
859 optval = std::move(out);
860 } else {
861 this->push_back(std::move(obj));
862 optval = std::nullopt;
863 }
864
865 return (*this);
866}
867
868/**
869* Decode an OPTIONAL or DEFAULT element with an IMPLICIT tagging
870*/
871template <typename T>
873 ASN1_Type type_tag,
874 ASN1_Class class_tag,
875 ASN1_Type real_type,
876 ASN1_Class real_class,
877 const T& default_value) {
879
880 if(obj.is_a(type_tag, class_tag)) {
881 decode_implicit(std::move(obj), out, real_type, real_class);
882 } else {
883 // Not what we wanted, push it back on the stream
884 out = default_value;
885 push_back(std::move(obj));
886 }
887
888 return (*this);
889}
890
891/**
892* Decode a list of homogeneously typed values
893*/
894template <typename T>
895BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec, ASN1_Type type_tag, ASN1_Class class_tag) {
896 BER_Decoder list = start_cons(type_tag, class_tag);
897
898 while(list.more_items()) {
899 T value;
900 list.decode(value);
901 vec.push_back(std::move(value));
902 }
903
904 list.end_cons();
905
906 return (*this);
907}
908
909/**
910* Decode an optional list of homogeneously typed values
911*/
912template <typename T>
913bool BER_Decoder::decode_optional_list(std::vector<T>& vec, ASN1_Type type_tag, ASN1_Class class_tag) {
914 if(peek_next_object().is_a(type_tag, class_tag)) {
915 decode_list(vec, type_tag, class_tag);
916 return true;
917 }
918
919 return false;
920}
921
922} // namespace Botan
923
924#endif
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
size_t unused_bits() const
Definition asn1_obj.h:211
std::span< const uint8_t > bytes() const
Definition asn1_obj.h:206
bool operator==(const Limits &) const =default
static Limits BER(size_t max_nested_indef=16)
Definition ber_dec.h:51
Limits with_default_value_encoding_rejected() const
Definition ber_dec.h:118
static constexpr size_t DefaultMaxObjectSize
Definition ber_dec.h:39
bool reject_default_value_encoding() const
Definition ber_dec.h:91
Limits with_standalone_eoc_allowed() const
Definition ber_dec.h:97
static Limits DER()
Definition ber_dec.h:44
Limits with_max_object_size(std::optional< size_t > max_object_size) const
Definition ber_dec.h:108
size_t max_nested_indefinite_length() const
Definition ber_dec.h:68
bool allow_ber_encoding() const
Definition ber_dec.h:58
std::optional< size_t > max_object_size() const
Definition ber_dec.h:82
bool allow_standalone_eoc() const
Definition ber_dec.h:76
bool require_der_encoding() const
Definition ber_dec.h:63
BER_Decoder & raw_bytes(T &out_wrapped)
Definition ber_dec.h:343
BER_Decoder & decode_optional_string(T &out_wrapped, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:719
BOTAN_FUTURE_EXPLICIT BER_Decoder(BER_Object &&obj)
Definition ber_dec.h:177
BER_Decoder start_set()
Definition ber_dec.h:285
BER_Decoder(BER_Decoder &&other) noexcept
const BER_Object & peek_next_object()
Definition ber_dec.cpp:510
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:605
BER_Object get_next_object()
Definition ber_dec.cpp:521
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:326
BER_Decoder & decode_optional_octet_aligned_bitstring(T &out_wrapped, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:770
BER_Decoder & decode_bitstring(ASN1_BitString &out, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.cpp:1047
BER_Decoder & decode(bool &out)
Definition ber_dec.h:361
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition ber_dec.cpp:753
BER_Decoder & decode(T &out, ASN1_Type real_type)
Definition ber_dec.h:390
bool more_items() const
Definition ber_dec.cpp:461
BER_Decoder & decode_implicit(T &out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class)
Definition ber_dec.h:659
Limits limits() const
Definition ber_dec.h:201
BER_Decoder & decode(BigInt &out)
Definition ber_dec.h:371
BER_Decoder & decode_integer_type(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:536
std::vector< uint8_t > get_next_octet_string()
Definition ber_dec.h:376
BOTAN_FUTURE_EXPLICIT BER_Decoder(const BER_Object &obj, Limits limits=Limits::BER())
Definition ber_dec.h:170
BER_Decoder & decode_optional_octet_aligned_bitstring(T &out, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:803
BER_Decoder start_explicit_context_specific(uint32_t tag)
Definition ber_dec.h:305
BER_Decoder(const uint8_t buf[], size_t len, Limits limits=Limits::BER())
Definition ber_dec.h:154
BER_Decoder & end_cons()
Definition ber_dec.cpp:635
BER_Decoder(const BER_Decoder &other)=delete
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:619
BER_Decoder start_sequence()
Definition ber_dec.h:279
BER_Decoder & operator=(BER_Decoder &&) noexcept
bool decode_optional_list(std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:913
BER_Decoder start_context_specific(uint32_t tag)
Definition ber_dec.h:294
BER_Decoder & decode(T &v, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:404
BER_Decoder & decode_octet_aligned_bitstring(T &out_wrapped, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:458
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:550
BER_Decoder & decode_implicit(BER_Object obj, T &out, ASN1_Type real_type, ASN1_Class real_class)
Definition ber_dec.h:637
BER_Decoder & decode_optional_string(T &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:753
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
Definition ber_dec.h:698
BER_Decoder & decode(size_t &out)
Definition ber_dec.h:366
BER_Decoder & decode_integer_type(T &out)
Definition ber_dec.h:523
BER_Decoder & decode_optional_implicit(T &out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class, const T &default_value=T())
Definition ber_dec.h:872
BER_Decoder & decode_default(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value)
Definition ber_dec.h:566
BER_Decoder & decode_optional_field(uint32_t tag_no, ASN1_Class class_tag, F &&fn)
Definition ber_dec.h:623
BER_Decoder & operator=(const BER_Decoder &)=delete
BER_Decoder & get_next(BER_Object &ber)
Definition ber_dec.h:214
BER_Decoder & decode_list(std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:895
size_t length() const
Definition asn1_obj.h:303
const uint8_t * bits() const
Definition asn1_obj.h:298
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition asn1_obj.cpp:97
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
Definition asn1_obj.cpp:65
ASN1_Class
Definition asn1_obj.h:32
ASN1_Type
Definition asn1_obj.h:47
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128