8#ifndef BOTAN_BER_DECODER_H_
9#define BOTAN_BER_DECODER_H_
11#include <botan/asn1_obj.h>
12#include <botan/secmem.h>
13#include <botan/strong_type_helpers.h>
51 static Limits
BER(
size_t max_nested_indef = 16) {
99 copy.m_allow_standalone_eoc =
true;
120 copy.m_reject_default_value_encoding =
true;
130 Limits(
bool allow_ber,
131 size_t max_nested_indef,
135 m_allow_ber(allow_ber),
136 m_max_nested_indef(max_nested_indef),
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;
160 explicit BER_Decoder(std::span<const uint8_t> buf, Limits limits = Limits::BER());
165 explicit BER_Decoder(DataSource& src, Limits limits = Limits::BER());
246 bool more_items()
const;
325 template <
typename T>
327 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
331 std::memcpy(
reinterpret_cast<uint8_t*
>(&out), obj.
bits(), obj.
length());
341 template <
typename T>
347 while(
const size_t got = this->read_bytes(std::span{buf})) {
348 out.insert(out.end(), buf, buf + got);
377 std::vector<uint8_t> out_vec;
388 template <
typename T>
402 template <
typename T>
408 return decode_into_byte_vector(
unwrap_strong_type(v), real_type, type_tag, class_tag);
456 template <
typename T>
469 out.assign(bits.
bytes().begin(), bits.
bytes().end());
485 BER_Decoder& decode_named_bitstring(uint64_t& bits,
515 uint64_t decode_constrained_integer(
ASN1_Type type_tag,
ASN1_Class class_tag,
size_t T_bytes);
522 template <
typename T>
535 template <
typename T>
549 template <
typename T>
551 std::optional<T> optval;
553 out = optval ? *optval : default_value;
565 template <
typename T>
567 std::optional<T> optval;
569 if(optval.has_value()) {
570 if(m_limits.require_der_encoding() && m_limits.reject_default_value_encoding() &&
571 *optval == default_value) {
574 out = std::move(*optval);
588 template <
typename T>
602 template <
typename T>
608 const T& default_value = T());
622 template <
typename F>
625 std::forward<F>(fn)(*this);
636 template <
typename T>
638 obj.set_tagging(real_type, real_class);
640 if constexpr(std::is_base_of_v<ASN1_Object, T>) {
644 return decode(out, real_type, real_class);
658 template <
typename T>
673 template <
typename T>
686 template <
typename T>
687 bool decode_optional_list(std::vector<T>& vec,
697 template <
typename T>
702 if(actual != expected) {
717 template <
typename T>
721 uint32_t expected_tag,
728 if(obj.
is_a(type_tag, class_tag)) {
730 BER_Decoder(obj, m_limits).decode(out, real_type).verify_end();
733 decode(out, real_type, type_tag, class_tag);
751 template <
typename T>
768 template <
typename T>
771 uint32_t expected_tag,
778 if(obj.
is_a(type_tag, class_tag)) {
780 BER_Decoder(obj, m_limits).decode_octet_aligned_bitstring(out).verify_end();
801 template <
typename T>
814 std::optional<uint8_t> read_next_byte();
816 size_t read_bytes(std::span<uint8_t> out);
820 BER_Decoder& decode_into_byte_vector(std::vector<uint8_t>& v,
835 std::unique_ptr<DataSource> m_data_src;
845 if(obj.
is_a(type_tag, class_tag)) {
848 BER_Decoder(obj, m_limits).decode(out).verify_end();
851 if constexpr(std::is_base_of_v<ASN1_Object, T>) {
856 this->
decode(out, type_tag, class_tag);
859 optval = std::move(out);
862 optval = std::nullopt;
877 const T& default_value) {
880 if(obj.
is_a(type_tag, class_tag)) {
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_DEPRECATED(msg)
#define BOTAN_FUTURE_EXPLICIT
size_t unused_bits() const
std::span< const uint8_t > bytes() const
bool operator==(const Limits &) const =default
static Limits BER(size_t max_nested_indef=16)
Limits with_default_value_encoding_rejected() const
static constexpr size_t DefaultMaxObjectSize
bool reject_default_value_encoding() const
Limits with_standalone_eoc_allowed() const
Limits with_max_object_size(std::optional< size_t > max_object_size) const
size_t max_nested_indefinite_length() const
bool allow_ber_encoding() const
std::optional< size_t > max_object_size() const
bool allow_standalone_eoc() const
bool require_der_encoding() const
BER_Decoder & raw_bytes(T &out_wrapped)
BER_Decoder & decode_optional_string(T &out_wrapped, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BOTAN_FUTURE_EXPLICIT BER_Decoder(BER_Object &&obj)
BER_Decoder(BER_Decoder &&other) noexcept
const BER_Object & peek_next_object()
void push_back(const BER_Object &obj)
BER_Object get_next_object()
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode_optional_octet_aligned_bitstring(T &out_wrapped, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode_bitstring(ASN1_BitString &out, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode(bool &out)
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
BER_Decoder & decode(T &out, ASN1_Type real_type)
BER_Decoder & decode_implicit(T &out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class)
BER_Decoder & decode(BigInt &out)
BER_Decoder & decode_integer_type(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
std::vector< uint8_t > get_next_octet_string()
BOTAN_FUTURE_EXPLICIT BER_Decoder(const BER_Object &obj, Limits limits=Limits::BER())
BER_Decoder & decode_optional_octet_aligned_bitstring(T &out, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder start_explicit_context_specific(uint32_t tag)
BER_Decoder(const uint8_t buf[], size_t len, Limits limits=Limits::BER())
BER_Decoder(const BER_Decoder &other)=delete
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
BER_Decoder start_sequence()
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)
BER_Decoder start_context_specific(uint32_t tag)
BER_Decoder & decode(T &v, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode_octet_aligned_bitstring(T &out_wrapped, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
BER_Decoder & decode_implicit(BER_Object obj, T &out, ASN1_Type real_type, ASN1_Class real_class)
BER_Decoder & decode_optional_string(T &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
BER_Decoder & decode(size_t &out)
BER_Decoder & decode_integer_type(T &out)
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())
BER_Decoder & decode_default(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value)
BER_Decoder & decode_optional_field(uint32_t tag_no, ASN1_Class class_tag, F &&fn)
BER_Decoder & operator=(const BER_Decoder &)=delete
BER_Decoder & get_next(BER_Object &ber)
BER_Decoder & decode_list(std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
const uint8_t * bits() const
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
@ ExplicitContextSpecific
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