8#ifndef BOTAN_BER_DECODER_H_
9#define BOTAN_BER_DECODER_H_
11#include <botan/asn1_obj.h>
12#include <botan/secmem.h>
35 static Limits
DER() {
return Limits(
false, 0); }
42 static Limits
BER(
size_t max_nested_indef = 16) {
return Limits(
true, max_nested_indef); }
53 Limits(
bool allow_ber,
size_t max_nested_indef) :
54 m_allow_ber(allow_ber), m_max_nested_indef(max_nested_indef) {}
57 size_t m_max_nested_indef;
70 explicit BER_Decoder(std::span<const uint8_t> buf, Limits limits = Limits::BER());
75 explicit BER_Decoder(DataSource& src, Limits limits = Limits::BER());
138 bool more_items()
const;
188 template <
typename T>
190 requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
194 std::memcpy(
reinterpret_cast<uint8_t*
>(&out), obj.
bits(), obj.
length());
202 template <
typename Alloc>
206 if(
auto next = this->read_next_byte()) {
207 out.push_back(*next);
233 std::vector<uint8_t> out_vec;
241 template <
typename Alloc>
271 uint64_t decode_constrained_integer(
ASN1_Type type_tag,
ASN1_Class class_tag,
size_t T_bytes);
273 template <
typename T>
278 template <
typename T>
284 template <
typename T>
286 std::optional<T> optval;
288 out = optval ? *optval : default_value;
292 template <
typename T>
295 template <
typename T>
301 const T& default_value = T());
303 template <
typename T>
308 template <
typename T>
309 bool decode_optional_list(std::vector<T>& out,
313 template <
typename T>
318 if(actual != expected) {
328 template <
typename Alloc>
331 uint32_t expected_tag,
337 if(obj.
is_a(type_tag, class_tag)) {
339 BER_Decoder(obj, m_limits).decode(out, real_type).verify_end();
342 decode(out, real_type, type_tag, class_tag);
352 template <
typename Alloc>
365 std::optional<uint8_t> read_next_byte();
374 std::unique_ptr<DataSource> m_data_src;
384 if(obj.
is_a(type_tag, class_tag)) {
387 BER_Decoder(obj, m_limits).decode(out).verify_end();
390 this->
decode(out, type_tag, class_tag);
392 optval = std::move(out);
395 optval = std::nullopt;
410 const T& default_value) {
413 if(obj.
is_a(type_tag, class_tag)) {
414 obj.set_tagging(real_type, real_class);
416 decode(out, real_type, real_class);
#define BOTAN_PUBLIC_API(maj, min)
#define BOTAN_DEPRECATED(msg)
#define BOTAN_FUTURE_EXPLICIT
bool operator==(const Limits &) const =default
static Limits BER(size_t max_nested_indef=16)
size_t max_nested_indefinite_length() const
bool allow_ber_encoding() const
bool require_der_encoding() const
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(bool &out)
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
BER_Decoder & decode(BigInt &out)
BER_Decoder & decode_optional_string(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & raw_bytes(std::vector< uint8_t, Alloc > &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(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type)
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 & decode_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder start_sequence()
BER_Decoder & operator=(BER_Decoder &&) noexcept
BER_Decoder start_context_specific(uint32_t tag)
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
BER_Decoder & decode_and_check(const T &expected, std::string_view error_msg)
bool decode_optional_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode(size_t &out)
BER_Decoder & decode_optional_string(std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
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 & operator=(const BER_Decoder &)=delete
BER_Decoder & get_next(BER_Object &ber)
const uint8_t * bits() const
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
@ ExplicitContextSpecific
std::vector< T, secure_allocator< T > > secure_vector