Botan 3.14.1
Crypto and TLS for C&
Botan::BER_Decoder Class Referencefinal

#include <ber_dec.h>

Classes

class  Limits

Public Member Functions

 BER_Decoder (BER_Decoder &&other) noexcept
BOTAN_FUTURE_EXPLICIT BER_Decoder (BER_Object &&obj)
 BER_Decoder (BER_Object &&obj, Limits limits)
 BER_Decoder (const BER_Decoder &other)=delete
BOTAN_FUTURE_EXPLICIT BER_Decoder (const BER_Object &obj, Limits limits=Limits::BER())
 BER_Decoder (const uint8_t buf[], size_t len, Limits limits=Limits::BER())
 BER_Decoder (DataSource &src, Limits limits=Limits::BER())
 BER_Decoder (std::span< const uint8_t > buf, Limits limits=Limits::BER())
BER_Decoder & decode (ASN1_Object &obj, ASN1_Type type_tag=ASN1_Type::NoObject, ASN1_Class class_tag=ASN1_Class::NoObject)
BER_Decoder & decode (BigInt &out)
BER_Decoder & decode (BigInt &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode (bool &out)
BER_Decoder & decode (bool &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & decode (size_t &out)
BER_Decoder & decode (size_t &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode (T &out, ASN1_Type real_type)
template<typename T>
BER_Decoder & decode (T &v, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode_and_check (const T &expected, std::string_view error_msg)
BER_Decoder & decode_bitstring (ASN1_BitString &out, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
uint64_t decode_constrained_integer (ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
template<typename T>
BER_Decoder & decode_default (T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value)
template<typename T>
BER_Decoder & decode_implicit (BER_Object obj, T &out, ASN1_Type real_type, ASN1_Class real_class)
template<typename T>
BER_Decoder & decode_implicit (T &out, ASN1_Type type_tag, ASN1_Class class_tag, ASN1_Type real_type, ASN1_Class real_class)
template<typename T>
BER_Decoder & decode_integer_type (T &out)
template<typename T>
BER_Decoder & decode_integer_type (T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode_list (std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode_named_bitstring (uint64_t &bits, size_t width, ASN1_Type type_tag=ASN1_Type::BitString, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoder & decode_null ()
template<typename T>
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_octet_string_bigint (BigInt &b)
template<typename T>
BER_Decoder & decode_optional (std::optional< T > &optval, ASN1_Type type_tag, ASN1_Class class_tag)
template<typename T>
BER_Decoder & decode_optional (T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
template<typename F>
BER_Decoder & decode_optional_field (uint32_t tag_no, ASN1_Class class_tag, F &&fn)
template<typename T>
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())
template<typename T>
bool decode_optional_list (std::vector< T > &vec, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
template<typename T>
BER_Decoder & decode_optional_octet_aligned_bitstring (T &out, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode_optional_octet_aligned_bitstring (T &out_wrapped, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode_optional_string (T &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoder & decode_optional_string (T &out_wrapped, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoder & discard_remaining ()
BER_Decoder & end_cons ()
BER_Decoder & get_next (BER_Object &ber)
BER_Object get_next_object ()
std::vector< uint8_t > get_next_octet_string ()
template<typename T>
requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
BER_Decoder & get_next_value (T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Limits limits () const
bool more_items () const
BER_Decoder & operator= (BER_Decoder &&) noexcept
BER_Decoder & operator= (const BER_Decoder &)=delete
const BER_Object & peek_next_object ()
void push_back (BER_Object &&obj)
void push_back (const BER_Object &obj)
template<typename T>
BER_Decoder & raw_bytes (T &out_wrapped)
BER_Decoder start_cons (ASN1_Type type_tag, ASN1_Class class_tag)
BER_Decoder start_context_specific (uint32_t tag)
BER_Decoder start_explicit_context_specific (uint32_t tag)
BER_Decoder start_sequence ()
BER_Decoder start_set ()
BER_Decoder & verify_end ()
BER_Decoder & verify_end (std::string_view err_msg)
 ~BER_Decoder ()

Detailed Description

BER Decoding Object

Definition at line 29 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/8]

◆ BER_Decoder() [2/8]

Botan::BER_Decoder::BER_Decoder ( std::span< const uint8_t > buf,
Limits limits = Limits::BER() )
explicit

Set up to BER decode the data in buf

Definition at line 664 of file ber_dec.cpp.

664 : m_limits(limits) {
665 m_data_src = std::make_unique<DataSource_Memory>(buf);
666 m_source = m_data_src.get();
667}

References limits().

◆ BER_Decoder() [3/8]

Botan::BER_Decoder::BER_Decoder ( DataSource & src,
Limits limits = Limits::BER() )
explicit

Set up to BER decode the data in src

Definition at line 659 of file ber_dec.cpp.

659: m_limits(limits), m_source(&src) {}

References limits().

◆ BER_Decoder() [4/8]

BOTAN_FUTURE_EXPLICIT Botan::BER_Decoder::BER_Decoder ( const BER_Object & obj,
Limits limits = Limits::BER() )
inline

Set up to BER decode the data in obj

Definition at line 170 of file ber_dec.h.

170 :
171 BER_Decoder(obj.data(), limits) {}

References Botan::BER_Decoder::Limits::BER(), BER_Decoder(), BOTAN_FUTURE_EXPLICIT, and limits().

◆ BER_Decoder() [5/8]

BOTAN_FUTURE_EXPLICIT Botan::BER_Decoder::BER_Decoder ( BER_Object && obj)
inline

Set up to BER decode the data in obj TODO(Botan4) remove this?

Definition at line 177 of file ber_dec.h.

177: BER_Decoder(std::move(obj), nullptr) {}

References BER_Decoder(), and BOTAN_FUTURE_EXPLICIT.

◆ BER_Decoder() [6/8]

Botan::BER_Decoder::BER_Decoder ( BER_Object && obj,
Limits limits )

Set up to BER decode the data in obj, taking ownership of its contents

Definition at line 651 of file ber_dec.cpp.

651 : m_limits(limits) {
652 m_data_src = std::make_unique<DataSource_BERObject>(std::move(obj));
653 m_source = m_data_src.get();
654}

References limits().

◆ BER_Decoder() [7/8]

Botan::BER_Decoder::BER_Decoder ( const BER_Decoder & other)
delete

References BER_Decoder().

◆ BER_Decoder() [8/8]

Botan::BER_Decoder::BER_Decoder ( BER_Decoder && other)
defaultnoexcept

Move constructor

References BER_Decoder().

◆ ~BER_Decoder()

Botan::BER_Decoder::~BER_Decoder ( )
default

Member Function Documentation

◆ decode() [1/9]

BER_Decoder & Botan::BER_Decoder::decode ( ASN1_Object & obj,
ASN1_Type type_tag = ASN1_Type::NoObject,
ASN1_Class class_tag = ASN1_Class::NoObject )

Request an object decode itself from this stream

Parameters
objthe object to decode into
type_tagmust be NoObject; implicit tagging is not supported here
class_tagmust be NoObject; implicit tagging is not supported here

Definition at line 676 of file ber_dec.cpp.

676 {
677 // TODO support this case properly
678 if(type_tag != ASN1_Type::NoObject || class_tag != ASN1_Class::NoObject) {
679 throw Not_Implemented("BER_Decoder::decode(ASN1_Object) does not support implicit tagged decoding");
680 }
681 obj.decode_from(*this);
682 return (*this);
683}

References BER_Decoder(), decode(), and Botan::NoObject.

◆ decode() [2/9]

BER_Decoder & Botan::BER_Decoder::decode ( BigInt & out)
inline

Decode a BER encoded INTEGER

Definition at line 371 of file ber_dec.h.

BER_Decoder & decode(bool &out)
Definition ber_dec.h:361

References BER_Decoder(), decode(), Botan::Integer, and Botan::Universal.

Referenced by decode().

◆ decode() [3/9]

BER_Decoder & Botan::BER_Decoder::decode ( BigInt & v,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Decode an INTEGER with an IMPLICIT tagging

Parameters
vwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 780 of file ber_dec.cpp.

780 {
781 const BER_Object obj = get_next_object();
782 obj.assert_is_a(type_tag, class_tag);
783
784 // An INTEGER must have at least one content octet (X.690 section 8.3.1)
785 if(obj.length() == 0) {
786 throw BER_Decoding_Error("INTEGER encoding has no content octets");
787 }
788
789 // DER requires minimal INTEGER encoding (X.690 section 8.3.2)
790 if(m_limits.require_der_encoding()) {
791 if(obj.length() > 1) {
792 if(obj.bits()[0] == 0x00 && (obj.bits()[1] & 0x80) == 0) {
793 throw BER_Decoding_Error("Detected non-minimal INTEGER encoding in DER structure");
794 }
795 if(obj.bits()[0] == 0xFF && (obj.bits()[1] & 0x80) != 0) {
796 throw BER_Decoding_Error("Detected non-minimal INTEGER encoding in DER structure");
797 }
798 }
799 }
800
801 out = ASN1::integer_from_contents(obj.data());
802
803 return (*this);
804}
BER_Object get_next_object()
Definition ber_dec.cpp:521
BigInt integer_from_contents(std::span< const uint8_t > contents)
Definition ber_dec.cpp:806

References Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::BER_Object::bits(), Botan::BER_Object::data(), get_next_object(), Botan::ASN1::integer_from_contents(), and Botan::BER_Object::length().

◆ decode() [4/9]

BER_Decoder & Botan::BER_Decoder::decode ( bool & out)
inline

Decode a BER encoded BOOLEAN

Definition at line 361 of file ber_dec.h.

References BER_Decoder(), Botan::Boolean, decode(), and Botan::Universal.

Referenced by decode(), decode(), decode(), decode(), decode_and_check(), decode_constrained_integer(), Botan::AlgorithmIdentifier::decode_from(), Botan::AlternativeName::decode_from(), Botan::Attribute::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::decode_from(), Botan::Cert_Extension::TNAuthList::Entry::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_DN::decode_from(), Botan::X509_Object::decode_from(), Botan::X509_Serial_Number::decode_from(), decode_implicit(), decode_list(), decode_octet_string_bigint(), decode_optional(), decode_optional_string(), Botan::Ed25519_PrivateKey::Ed25519_PrivateKey(), Botan::Ed448_PrivateKey::Ed448_PrivateKey(), get_next_octet_string(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::PKCS12::PKCS12(), Botan::pkcs12_pbe_decrypt(), Botan::OCSP::Response::Response(), Botan::RSA_PublicKey::RSA_PublicKey(), Botan::TLS::Session::Session(), and Botan::X25519_PrivateKey::X25519_PrivateKey().

◆ decode() [5/9]

BER_Decoder & Botan::BER_Decoder::decode ( bool & v,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Decode a BOOLEAN with an IMPLICIT tagging

Parameters
vwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 707 of file ber_dec.cpp.

707 {
708 const BER_Object obj = get_next_object();
709 obj.assert_is_a(type_tag, class_tag);
710
711 if(obj.length() != 1) {
712 throw BER_Decoding_Error("BER boolean value had invalid size");
713 }
714
715 const uint8_t val = obj.bits()[0];
716
717 // DER requires boolean values to be exactly 0x00 or 0xFF
718 if(m_limits.require_der_encoding() && val != 0x00 && val != 0xFF) {
719 throw BER_Decoding_Error("Detected non-canonical boolean encoding in DER structure");
720 }
721
722 out = (val != 0) ? true : false;
723
724 return (*this);
725}

References Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::BER_Object::bits(), get_next_object(), and Botan::BER_Object::length().

◆ decode() [6/9]

BER_Decoder & Botan::BER_Decoder::decode ( size_t & out)
inline

Decode a small BER encoded INTEGER

Definition at line 366 of file ber_dec.h.

References BER_Decoder(), decode(), Botan::Integer, and Botan::Universal.

Referenced by decode().

◆ decode() [7/9]

BER_Decoder & Botan::BER_Decoder::decode ( size_t & v,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Decode a small INTEGER with an IMPLICIT tagging

Parameters
vwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 730 of file ber_dec.cpp.

730 {
731 BigInt integer;
732 decode(integer, type_tag, class_tag);
733
734 if(integer.signum() < 0) {
735 throw BER_Decoding_Error("Decoded small integer value was negative");
736 }
737
738 if(integer.bits() > 32) {
739 throw BER_Decoding_Error("Decoded integer value larger than expected");
740 }
741
742 out = 0;
743 for(size_t i = 0; i != 4; ++i) {
744 out = (out << 8) | integer.byte_at(3 - i);
745 }
746
747 return (*this);
748}

References BER_Decoder(), Botan::BigInt::bits(), Botan::BigInt::byte_at(), decode(), and Botan::BigInt::signum().

◆ decode() [8/9]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode ( T & out,
ASN1_Type real_type )
inline

BER decode a BIT STRING or OCTET STRING

Parameters
outwhere the contents are written
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString

Definition at line 390 of file ber_dec.h.

390 {
391 return decode(unwrap_strong_type(out), real_type, real_type, ASN1_Class::Universal);
392 }
constexpr decltype(auto) unwrap_strong_type(T &&t)
Generically unwraps a strong type to its underlying type.

References BER_Decoder(), decode(), Botan::Universal, and Botan::unwrap_strong_type().

◆ decode() [9/9]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode ( T & v,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode a BIT STRING or OCTET STRING with an IMPLICIT tagging

Parameters
vwhere the contents are written
real_typeeither ASN1_Type::OctetString or ASN1_Type::BitString
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 404 of file ber_dec.h.

407 {
408 return decode_into_byte_vector(unwrap_strong_type(v), real_type, type_tag, class_tag);
409 }

References BER_Decoder(), Botan::ContextSpecific, and Botan::unwrap_strong_type().

◆ decode_and_check()

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_and_check ( const T & expected,
std::string_view error_msg )
inline

Decode a value and throw unless it equals the expected value

Parameters
expectedthe value the encoding must have
error_msgthe message of the exception thrown on a mismatch

Definition at line 698 of file ber_dec.h.

698 {
699 T actual;
700 decode(actual);
701
702 if(actual != expected) {
703 throw Decoding_Error(error_msg);
704 }
705
706 return (*this);
707 }

References BER_Decoder(), and decode().

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), Botan::OCSP::Response::Response(), Botan::RSA_PrivateKey::RSA_PrivateKey(), and Botan::TLS::Session::Session().

◆ decode_bitstring()

BER_Decoder & Botan::BER_Decoder::decode_bitstring ( ASN1_BitString & out,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )

Decode a BIT STRING, retaining the count of unused bits

Parameters
outwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 1047 of file ber_dec.cpp.

1047 {
1048 const BER_Object obj = get_next_object();
1049
1050 std::vector<uint8_t> bits;
1051 uint8_t unused_bits = 0;
1052
1053 if(is_constructed(obj.class_tag())) {
1054 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
1055 if(m_limits.require_der_encoding()) {
1056 throw BER_Decoding_Error("Detected constructed string encoding in DER structure");
1057 }
1058 bits.reserve(obj.length()); // upper possible bound on the output size
1059 unused_bits = asn1_concat_constructed_bit_string(bits, obj, m_limits, 0);
1060 } else {
1061 unused_bits = asn1_bitstring_unused_bits(obj, type_tag, class_tag, m_limits.require_der_encoding());
1062 bits.assign(obj.bits() + 1, obj.bits() + obj.length());
1063 }
1064
1065 if(unused_bits > 0 && !bits.empty()) {
1066 bits.back() &= static_cast<uint8_t>(0xFF << unused_bits);
1067 }
1068
1069 out = ASN1_BitString(std::move(bits), unused_bits);
1070 return (*this);
1071}

References Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::BER_Object::bits(), Botan::BER_Object::class_tag(), Botan::Constructed, get_next_object(), and Botan::BER_Object::length().

Referenced by Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::decode_from(), decode_named_bitstring(), and decode_octet_aligned_bitstring().

◆ decode_constrained_integer()

uint64_t Botan::BER_Decoder::decode_constrained_integer ( ASN1_Type type_tag,
ASN1_Class class_tag,
size_t T_bytes )

Decode a non-negative INTEGER that fits in at most T_bytes bytes

Throws BER_Decoding_Error if the value is negative or too large.

Parameters
type_tagthe expected type tag
class_tagthe expected class tag
T_bytesthe maximum size of the value in bytes, at most 8

Definition at line 753 of file ber_dec.cpp.

753 {
754 if(T_bytes > 8) {
755 throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
756 }
757
758 BigInt integer;
759 decode(integer, type_tag, class_tag);
760
761 if(integer.is_negative()) {
762 throw BER_Decoding_Error("Decoded small integer value was negative");
763 }
764
765 if(integer.bits() > 8 * T_bytes) {
766 throw BER_Decoding_Error("Decoded integer value larger than expected");
767 }
768
769 uint64_t out = 0;
770 for(size_t i = 0; i != 8; ++i) {
771 out = (out << 8) | integer.byte_at(7 - i);
772 }
773
774 return out;
775}

References Botan::BigInt::bits(), Botan::BigInt::byte_at(), decode(), and Botan::BigInt::is_negative().

Referenced by decode_integer_type().

◆ decode_default()

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_default ( T & out,
ASN1_Type type_tag,
ASN1_Class class_tag,
const T & default_value )
inline

Decode a field carrying an ASN.1 DEFAULT value: if the field is absent out is set to default_value. Unlike decode_optional this is only for fields with a DEFAULT (not bare OPTIONAL fields): when the decoder is configured with Limits::with_default_value_encoding_rejected() and is in DER mode, a field that is present but equal to default_value is rejected, since DER requires such components to be omitted.

Definition at line 566 of file ber_dec.h.

566 {
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 }
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:550

References BER_Decoder(), and decode_optional().

◆ decode_implicit() [1/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_implicit ( BER_Object obj,
T & out,
ASN1_Type real_type,
ASN1_Class real_class )
inline

Decode an already-extracted BER_Object as if its tag were real_type/real_class. Used to consume IMPLICIT-tagged values whose body matches a different universal type (e.g. a context-specific [8] body that should be parsed as an OID).

Definition at line 637 of file ber_dec.h.

637 {
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 }
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:605

References BER_Decoder(), decode(), and push_back().

Referenced by Botan::AlternativeName::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::Cert_Extension::DistributionPointName::decode_from(), decode_implicit(), and decode_optional_implicit().

◆ decode_implicit() [2/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_implicit ( T & out,
ASN1_Type type_tag,
ASN1_Class class_tag,
ASN1_Type real_type,
ASN1_Class real_class )
inline

Read the next object, requiring it be tagged type_tag/class_tag, and decode it as if its tag were real_type/real_class.

Parameters
outwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag
real_typethe type tag the contents should be parsed as
real_classthe class tag the contents should be parsed as

Definition at line 659 of file ber_dec.h.

660 {
661 BER_Object obj = get_next_object();
662 obj.assert_is_a(type_tag, class_tag);
663 return decode_implicit(std::move(obj), out, real_type, real_class);
664 }
BER_Decoder & decode_implicit(BER_Object obj, T &out, ASN1_Type real_type, ASN1_Class real_class)
Definition ber_dec.h:637

References Botan::BER_Object::assert_is_a(), BER_Decoder(), decode_implicit(), and get_next_object().

◆ decode_integer_type() [1/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_integer_type ( T & out)
inline

Decode an INTEGER into an integral type, throwing if it does not fit

Parameters
outwhere the value is written

Definition at line 523 of file ber_dec.h.

523 {
525 }
BER_Decoder & decode_integer_type(T &out)
Definition ber_dec.h:523

References BER_Decoder(), decode_integer_type(), Botan::Integer, and Botan::Universal.

Referenced by decode_integer_type(), and Botan::TLS::Session::Session().

◆ decode_integer_type() [2/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_integer_type ( T & out,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode an INTEGER with an IMPLICIT tagging into an integral type, throwing if it does not fit

Parameters
outwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 536 of file ber_dec.h.

536 {
537 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
538 return (*this);
539 }
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition ber_dec.cpp:753

References BER_Decoder(), Botan::ContextSpecific, and decode_constrained_integer().

◆ decode_list()

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_list ( std::vector< T > & vec,
ASN1_Type type_tag = ASN1_Type::Sequence,
ASN1_Class class_tag = ASN1_Class::Universal )

Decode a constructed object containing a list of homogeneously typed values

Parameters
vecwhere the values are appended
type_tagthe expected type tag of the constructed object
class_tagthe expected class tag of the constructed object

Decode a list of homogeneously typed values

Definition at line 895 of file ber_dec.h.

895 {
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}
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:619

References BER_Decoder(), decode(), end_cons(), more_items(), push_back(), and start_cons().

Referenced by Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::decode_from(), decode_optional_list(), Botan::OCSP::Response::Response(), and Botan::TLS::Session::Session().

◆ decode_named_bitstring()

BER_Decoder & Botan::BER_Decoder::decode_named_bitstring ( uint64_t & bits,
size_t width,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )

Decode a BIT STRING that is actually a bit set, such as X.509 KeyUsage

The first bit of the encoding becomes the most significant of the width bits of bits. Throws if more than width bits are encoded, or in DER mode if the encoding is not minimal (ie the final bit is unset).

Parameters
bitswhere the bit set is written
widththe number of named bits, at most 64
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 1073 of file ber_dec.cpp.

1076 {
1077 if(width > 64) {
1078 throw Invalid_Argument("BER_Decoder: Named BIT STRING width is too large");
1079 }
1080
1081 ASN1_BitString bits;
1082 decode_bitstring(bits, type_tag, class_tag);
1083
1084 if(bits.bit_length() > width) {
1085 throw BER_Decoding_Error("Named BIT STRING exceeds declared width");
1086 }
1087
1088 if(m_limits.require_der_encoding() && bits.bit_length() > 0 && !bits.bit_at(bits.bit_length() - 1)) {
1089 throw BER_Decoding_Error("Named BIT STRING is not minimally encoded");
1090 }
1091
1092 uint64_t decoded = 0;
1093 for(size_t bit = 0; bit != bits.bit_length(); ++bit) {
1094 if(bits.bit_at(bit)) {
1095 decoded |= uint64_t(1) << (width - 1 - bit);
1096 }
1097 }
1098
1099 out = decoded;
1100 return (*this);
1101}
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

References BER_Decoder(), Botan::ASN1_BitString::bit_at(), Botan::ASN1_BitString::bit_length(), and decode_bitstring().

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Decode a BER encoded NULL, throwing if the next object is anything else

Definition at line 688 of file ber_dec.cpp.

688 {
689 const BER_Object obj = get_next_object();
690 obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
691 if(obj.length() > 0) {
692 throw BER_Decoding_Error("NULL object had nonzero size");
693 }
694 return (*this);
695}

References Botan::BER_Object::assert_is_a(), BER_Decoder(), get_next_object(), Botan::BER_Object::length(), Botan::Null, and Botan::Universal.

Referenced by Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::decode_from(), and Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::decode_from().

◆ decode_octet_aligned_bitstring()

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_octet_aligned_bitstring ( T & out_wrapped,
ASN1_Type type_tag = ASN1_Type::BitString,
ASN1_Class class_tag = ASN1_Class::Universal )
inline

Decode a BIT STRING, throwing unless it has no unused bits

Parameters
out_wrappedwhere the bits are written
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 458 of file ber_dec.h.

460 {
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 }

References BER_Decoder(), Botan::BitString, Botan::ASN1_BitString::bytes(), decode_bitstring(), Botan::Universal, Botan::ASN1_BitString::unused_bits(), and Botan::unwrap_strong_type().

Referenced by Botan::X509_Object::decode_from(), decode_optional_octet_aligned_bitstring(), Botan::X509::load_key(), and Botan::OCSP::Response::Response().

◆ decode_octet_string_bigint()

BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( BigInt & b)

Decode an integer value which is typed as an octet string

Definition at line 697 of file ber_dec.cpp.

697 {
700 out = BigInt::from_bytes(out_vec);
701 return (*this);
702}
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:84
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128

References BER_Decoder(), decode(), Botan::BigInt::from_bytes(), and Botan::OctetString.

◆ decode_optional() [1/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional ( std::optional< T > & optval,
ASN1_Type type_tag,
ASN1_Class class_tag )

Decode an OPTIONAL field, setting optval to nullopt if absent

Parameters
optvalwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag

Decode an OPTIONAL or DEFAULT element

Definition at line 842 of file ber_dec.h.

842 {
843 BER_Object obj = get_next_object();
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}

References BER_Decoder(), decode(), Botan::ExplicitContextSpecific, get_next_object(), Botan::BER_Object::is_a(), and push_back().

◆ decode_optional() [2/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional ( T & out,
ASN1_Type type_tag,
ASN1_Class class_tag,
const T & default_value = T() )
inline

Decode an OPTIONAL field, setting out to default_value if absent

Parameters
outwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag
default_valuethe value used if the field is not present

Definition at line 550 of file ber_dec.h.

550 {
551 std::optional<T> optval;
552 this->decode_optional(optval, type_tag, class_tag);
553 out = optval ? *optval : default_value;
554 return (*this);
555 }

References BER_Decoder(), and decode_optional().

Referenced by decode_default(), Botan::Extensions::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::OCSP::SingleResponse::decode_from(), decode_optional(), and Botan::OCSP::Response::Response().

◆ decode_optional_field()

template<typename F>
BER_Decoder & Botan::BER_Decoder::decode_optional_field ( uint32_t tag_no,
ASN1_Class class_tag,
F && fn )
inline

Decode an OPTIONAL field identified by a context-specific tag number.

If the next object is tagged [tag_no] with class class_tag, fn is invoked to decode it (fn must consume exactly that object). Otherwise the stream is left unchanged and fn is not called.

For a SEQUENCE of optional tagged fields that (per DER) appear at most once and in increasing tag order: call this once per field in tag order, then end_cons(), which rejects any unconsumed object (i.e. a duplicate, out-of-order, or unknown-tag field).

Definition at line 623 of file ber_dec.h.

623 {
624 if(peek_next_object().is_a(tag_no, class_tag)) {
625 std::forward<F>(fn)(*this);
626 }
627 return (*this);
628 }
const BER_Object & peek_next_object()
Definition ber_dec.cpp:510

References BER_Decoder(), and peek_next_object().

Referenced by Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from().

◆ decode_optional_implicit()

template<typename T>
BER_Decoder & Botan::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() )

Decode an OPTIONAL IMPLICIT tagged field, setting out to default_value if absent

Parameters
outwhere the value is written
type_tagthe expected type tag
class_tagthe expected class tag
real_typethe type tag the contents should be parsed as
real_classthe class tag the contents should be parsed as
default_valuethe value used if the field is not present

Decode an OPTIONAL or DEFAULT element with an IMPLICIT tagging

Definition at line 872 of file ber_dec.h.

877 {
878 BER_Object obj = get_next_object();
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}

References BER_Decoder(), decode_implicit(), get_next_object(), Botan::BER_Object::is_a(), and push_back().

◆ decode_optional_list()

template<typename T>
bool Botan::BER_Decoder::decode_optional_list ( std::vector< T > & vec,
ASN1_Type type_tag = ASN1_Type::Sequence,
ASN1_Class class_tag = ASN1_Class::Universal )

Decode a list of homogeneously typed values if one is present

Parameters
vecwhere the values are appended
type_tagthe expected type tag of the constructed object
class_tagthe expected class tag of the constructed object
Returns
true if the list was present

Decode an optional list of homogeneously typed values

Definition at line 913 of file ber_dec.h.

913 {
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}
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

References decode_list(), and peek_next_object().

◆ decode_optional_octet_aligned_bitstring() [1/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional_octet_aligned_bitstring ( T & out,
ASN1_Type expected_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode an OPTIONAL BIT STRING with no unused bits, clearing out if it is absent

Parameters
outwhere the bits are written
expected_tagthe expected type tag
class_tagthe expected class tag

Definition at line 803 of file ber_dec.h.

805 {
806 return decode_optional_octet_aligned_bitstring(out, static_cast<uint32_t>(expected_tag), class_tag);
807 }
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

References BER_Decoder(), Botan::ContextSpecific, and decode_optional_octet_aligned_bitstring().

◆ decode_optional_octet_aligned_bitstring() [2/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional_octet_aligned_bitstring ( T & out_wrapped,
uint32_t expected_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode an OPTIONAL BIT STRING with no unused bits, clearing out if it is absent

Parameters
out_wrappedwhere the bits are written
expected_tagthe expected tag number
class_tagthe expected class tag

Definition at line 770 of file ber_dec.h.

772 {
773 BER_Object obj = get_next_object();
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 }
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
ASN1_Type
Definition asn1_obj.h:47

References BER_Decoder(), Botan::ContextSpecific, decode_octet_aligned_bitstring(), Botan::ExplicitContextSpecific, get_next_object(), Botan::BER_Object::is_a(), push_back(), and Botan::unwrap_strong_type().

Referenced by decode_optional_octet_aligned_bitstring().

◆ decode_optional_string() [1/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional_string ( T & out,
ASN1_Type real_type,
ASN1_Type expected_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode an OPTIONAL string type, clearing out if it is absent

Parameters
outwhere the contents are written
real_typethe type tag the contents should be parsed as
expected_tagthe expected type tag
class_tagthe expected class tag

Definition at line 753 of file ber_dec.h.

756 {
757 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
758 }
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

References BER_Decoder(), Botan::ContextSpecific, and decode_optional_string().

◆ decode_optional_string() [2/2]

template<typename T>
BER_Decoder & Botan::BER_Decoder::decode_optional_string ( T & out_wrapped,
ASN1_Type real_type,
uint32_t expected_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Decode an OPTIONAL string type, clearing out if it is absent

Parameters
out_wrappedwhere the contents are written
real_typethe type tag the contents should be parsed as
expected_tagthe expected tag number
class_tagthe expected class tag

Definition at line 719 of file ber_dec.h.

722 {
723 BER_Object obj = get_next_object();
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 }

References BER_Decoder(), Botan::ContextSpecific, decode(), Botan::ExplicitContextSpecific, get_next_object(), Botan::BER_Object::is_a(), push_back(), and Botan::unwrap_strong_type().

Referenced by decode_optional_string(), and Botan::OCSP::Response::Response().

◆ discard_remaining()

BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Discard any data that remains unread Returns (*this)

Definition at line 488 of file ber_dec.cpp.

488 {
489 m_pushed = BER_Object();
490 uint8_t buf[64];
491 while(m_source->read(buf, sizeof(buf)) != 0) {}
492 return (*this);
493}

References BER_Decoder().

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

◆ get_next()

BER_Decoder & Botan::BER_Decoder::get_next ( BER_Object & ber)
inline

Get the next object in the data stream, storing it in ber

If EOF, ber is set to an object with type NO_OBJECT.

Definition at line 214 of file ber_dec.h.

214 {
215 ber = get_next_object();
216 return (*this);
217 }

References BER_Decoder(), and get_next_object().

Referenced by Botan::OCSP::SingleResponse::decode_from().

◆ get_next_object()

BER_Object Botan::BER_Decoder::get_next_object ( )

Get the next object in the data stream. If EOF, returns an object with type NO_OBJECT.

Definition at line 521 of file ber_dec.cpp.

521 {
522 BER_Object next;
523
524 if(m_pushed.is_set()) {
525 std::swap(next, m_pushed);
526 return next;
527 }
528
529 for(;;) {
532 decode_tag(m_source, type_tag, class_tag);
533 next.set_tagging(type_tag, class_tag);
534 if(next.is_set() == false) { // no more objects
535 return next;
536 }
537
538 const size_t allow_indef = m_limits.allow_ber_encoding() ? m_limits.max_nested_indefinite_length() : 0;
539 const bool der_mode = m_limits.require_der_encoding();
540 const auto dl = decode_length(m_source, allow_indef, der_mode, is_constructed(class_tag));
541
542 // Per X.690 8.1.5 the only valid EOC encoding is the two-octet
543 // sequence 0x00 0x00. Reject any other length encoding on a tag of
544 // (Eoc, Universal) before we consume the "content" bytes.
545 if(type_tag == ASN1_Type::Eoc && class_tag == ASN1_Class::Universal &&
546 (dl.content_length() != 0 || dl.indefinite_length())) {
547 throw BER_Decoding_Error("EOC marker with non-zero length");
548 }
549
550 if(const auto max_size = m_limits.max_object_size(); max_size && dl.content_length() > *max_size) {
551 throw BER_Decoding_Error("Encoded object exceeds maximum size");
552 }
553
554 if(!m_source->check_available(dl.total_length())) {
555 throw BER_Decoding_Error("Value truncated");
556 }
557
558 uint8_t* out = next.mutable_bits(dl.content_length());
559 if(m_source->read(out, dl.content_length()) != dl.content_length()) {
560 throw BER_Decoding_Error("Value truncated");
561 }
562
563 if(dl.indefinite_length()) {
564 // After reading the data consume the 2-byte EOC
565 uint8_t eoc[2] = {0xFF, 0xFF};
566 if(m_source->read(eoc, 2) != 2 || eoc[0] != 0x00 || eoc[1] != 0x00) {
567 throw BER_Decoding_Error("Missing or malformed EOC marker");
568 }
569 }
570
571 if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
572 if(m_limits.require_der_encoding()) {
573 throw BER_Decoding_Error("Detected EOC marker in DER structure");
574 }
575 // An EOC marker is only valid as an indefinite-length terminator, which
576 // is consumed above when reading the indefinite-length object. A
577 // standalone EOC is rejected unless the caller opted to tolerate it.
578 if(m_limits.allow_standalone_eoc()) {
579 continue;
580 }
581 throw BER_Decoding_Error("Encountered EOC marker outside of indefinite-length encoding");
582 }
583
584 break;
585 }
586
587 return next;
588}
ASN1_Class
Definition asn1_obj.h:32

References Botan::Eoc, Botan::BER_Object::is_set(), Botan::NoObject, Botan::BER_Object::tagging(), and Botan::Universal.

Referenced by decode(), decode(), decode_bitstring(), Botan::AlternativeName::decode_from(), Botan::ASN1_String::decode_from(), Botan::ASN1_Time::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::decode_from(), Botan::Cert_Extension::TNAuthList::Entry::decode_from(), Botan::GeneralName::decode_from(), Botan::OID::decode_from(), decode_implicit(), decode_null(), decode_optional(), decode_optional_implicit(), decode_optional_octet_aligned_bitstring(), decode_optional_string(), get_next(), peek_next_object(), Botan::OCSP::Response::Response(), and start_cons().

◆ get_next_octet_string()

std::vector< uint8_t > Botan::BER_Decoder::get_next_octet_string ( )
inline

Decode the next object as an OCTET STRING and return its contents

Definition at line 376 of file ber_dec.h.

376 {
377 std::vector<uint8_t> out_vec;
379 return out_vec;
380 }

References decode(), and Botan::OctetString.

Referenced by Botan::OCSP::Response::Response().

◆ get_next_value()

template<typename T>
requires std::is_standard_layout_v<T> && std::is_trivial_v<T>
BER_Decoder & Botan::BER_Decoder::get_next_value ( T & out,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )
inline

Get next object and copy value to POD type Asserts value length is equal to POD type sizeof. Asserts Type tag and optional Class tag according to parameters. Copy value to POD type (struct, union, C-style array, std::array, etc.).

Parameters
outPOD type reference where to copy object value
type_tagASN1_Type enum to assert type on object read
class_tagASN1_Type enum to assert class on object read (default: CONTEXT_SPECIFIC)
Returns
this reference

Definition at line 326 of file ber_dec.h.

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 }
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:326

References BER_Decoder(), Botan::BER_Object::bits(), Botan::ContextSpecific, get_next_value(), and Botan::BER_Object::length().

Referenced by get_next_value(), and ~BER_Decoder().

◆ limits()

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 461 of file ber_dec.cpp.

461 {
462 if(m_source->end_of_data() && !m_pushed.is_set()) {
463 return false;
464 }
465 return true;
466}

Referenced by Botan::AlternativeName::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), Botan::X509_DN::decode_from(), decode_list(), Botan::PKCS12::PKCS12(), and Botan::OCSP::Response::Response().

◆ operator=() [1/2]

BER_Decoder & Botan::BER_Decoder::operator= ( BER_Decoder && )
defaultnoexcept

Move assignment

References BER_Decoder().

◆ operator=() [2/2]

BER_Decoder & Botan::BER_Decoder::operator= ( const BER_Decoder & )
delete

References BER_Decoder().

◆ peek_next_object()

const BER_Object & Botan::BER_Decoder::peek_next_object ( )

Peek at the next object without removing it from the stream

If an object has been pushed, then it returns that object. Otherwise it reads the next object and pushes it. Thus, a you call peek_next_object followed by push_back without a subsequent read, it will fail.

Definition at line 510 of file ber_dec.cpp.

510 {
511 if(!m_pushed.is_set()) {
512 m_pushed = get_next_object();
513 }
514
515 return m_pushed;
516}

References get_next_object().

Referenced by Botan::Cert_Extension::ASBlocks::ASIdentifierChoice::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::decode_from(), Botan::Cert_Extension::DistributionPointName::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressChoice< V >::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::ML_DSA_Expanding_Keypair_Codec::decode_keypair(), decode_optional_field(), and decode_optional_list().

◆ push_back() [1/2]

void Botan::BER_Decoder::push_back ( BER_Object && obj)

Push an object back onto the stream. Throws if another object was previously pushed and has not been subsequently read out.

Definition at line 612 of file ber_dec.cpp.

612 {
613 if(m_pushed.is_set()) {
614 throw Invalid_State("BER_Decoder: Only one push back is allowed");
615 }
616 m_pushed = std::move(obj);
617}

◆ push_back() [2/2]

void Botan::BER_Decoder::push_back ( const BER_Object & obj)

Push an object back onto the stream. Throws if another object was previously pushed and has not been subsequently read out.

Definition at line 605 of file ber_dec.cpp.

605 {
606 if(m_pushed.is_set()) {
607 throw Invalid_State("BER_Decoder: Only one push back is allowed");
608 }
609 m_pushed = obj;
610}

Referenced by decode_implicit(), decode_list(), decode_optional(), decode_optional_implicit(), decode_optional_octet_aligned_bitstring(), decode_optional_string(), Botan::GOST_3410_PublicKey::GOST_3410_PublicKey(), and Botan::McEliece_PrivateKey::McEliece_PrivateKey().

◆ raw_bytes()

template<typename T>
BER_Decoder & Botan::BER_Decoder::raw_bytes ( T & out_wrapped)
inline

Save all the bytes remaining in the source

Parameters
out_wrappedwhere the remaining bytes are written

Definition at line 343 of file ber_dec.h.

343 {
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 }

References BER_Decoder(), and Botan::unwrap_strong_type().

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::X509_DN::decode_from(), Botan::X509_Object::decode_from(), and Botan::OCSP::Response::Response().

◆ start_cons()

BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Type type_tag,
ASN1_Class class_tag )

Start decoding a constructed object, returning a decoder for its contents. The returned decoder must be closed with end_cons().

Parameters
type_tagthe expected type tag
class_tagthe expected class tag

Definition at line 619 of file ber_dec.cpp.

619 {
620 BER_Object obj = get_next_object();
621 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
622
623 // In DER mode the elements of a universal SET must appear in sorted order
624 if(m_limits.require_der_encoding() && type_tag == ASN1_Type::Set && class_tag == ASN1_Class::Universal) {
625 verify_set_is_sorted(std::span<const uint8_t>{obj.bits(), obj.length()});
626 }
627
628 BER_Decoder child(std::move(obj), this);
629 return child;
630}

References Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::BER_Object::bits(), Botan::Constructed, get_next_object(), Botan::BER_Object::length(), Botan::Set, and Botan::Universal.

Referenced by decode_list(), start_context_specific(), start_explicit_context_specific(), start_sequence(), and start_set().

◆ start_context_specific()

BER_Decoder Botan::BER_Decoder::start_context_specific ( uint32_t tag)
inline

Start decoding an IMPLICIT context specific constructed object, returning a decoder for its contents. The returned decoder must be closed with end_cons().

Parameters
tagthe expected context specific tag number

Definition at line 294 of file ber_dec.h.

294 {
296 }

References BER_Decoder(), Botan::ContextSpecific, and start_cons().

Referenced by Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::PKCS12::PKCS12(), and Botan::OCSP::Response::Response().

◆ start_explicit_context_specific()

BER_Decoder Botan::BER_Decoder::start_explicit_context_specific ( uint32_t tag)
inline

Start decoding an EXPLICIT context specific constructed object, returning a decoder for its contents. The returned decoder must be closed with end_cons().

Parameters
tagthe expected context specific tag number

Definition at line 305 of file ber_dec.h.

References BER_Decoder(), Botan::ExplicitContextSpecific, and start_cons().

◆ start_sequence()

BER_Decoder Botan::BER_Decoder::start_sequence ( )
inline

Start decoding a SEQUENCE, returning a decoder for its contents. The returned decoder must be closed with end_cons().

Definition at line 279 of file ber_dec.h.

References BER_Decoder(), Botan::Sequence, start_cons(), and Botan::Universal.

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::AlternativeName::decode_from(), Botan::Attribute::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdentifiers::decode_from(), Botan::Cert_Extension::ASBlocks::ASIdOrRange::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::decode_from(), Botan::Cert_Extension::TNAuthList::Entry::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::X509_DN::decode_from(), Botan::X509_Object::decode_from(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::PKCS12::PKCS12(), Botan::pkcs12_pbe_decrypt(), Botan::OCSP::Response::Response(), Botan::RSA_PrivateKey::RSA_PrivateKey(), Botan::RSA_PublicKey::RSA_PublicKey(), and Botan::TLS::Session::Session().

◆ start_set()

BER_Decoder Botan::BER_Decoder::start_set ( )
inline

Start decoding a SET, returning a decoder for its contents. The returned decoder must be closed with end_cons().

Definition at line 285 of file ber_dec.h.

References BER_Decoder(), Botan::Set, start_cons(), and Botan::Universal.

Referenced by Botan::Attribute::decode_from(), and Botan::X509_DN::decode_from().

◆ verify_end() [1/2]

◆ verify_end() [2/2]

BER_Decoder & Botan::BER_Decoder::verify_end ( std::string_view err_msg)

Verify the stream is concluded, throws otherwise. Returns (*this)

Definition at line 478 of file ber_dec.cpp.

478 {
479 if(!m_source->end_of_data() || m_pushed.is_set()) {
480 throw Decoding_Error(err);
481 }
482 return (*this);
483}

References BER_Decoder().


The documentation for this class was generated from the following files: