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

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (BER_Decoder &&other)=default
BOTAN_FUTURE_EXPLICIT BER_Decoder (BER_Object &&obj)
 BER_Decoder (const BER_Decoder &other)
BOTAN_FUTURE_EXPLICIT BER_Decoder (const BER_Object &obj)
 BER_Decoder (const secure_vector< uint8_t > &vec)
 BER_Decoder (const std::vector< uint8_t > &vec)
 BER_Decoder (const uint8_t buf[], size_t len)
 BER_Decoder (DataSource &src)
BOTAN_FUTURE_EXPLICIT BER_Decoder (std::span< const uint8_t > buf)
BER_Decoderdecode (ASN1_Object &obj, ASN1_Type type_tag=ASN1_Type::NoObject, ASN1_Class class_tag=ASN1_Class::NoObject)
BER_Decoderdecode (BigInt &out)
BER_Decoderdecode (BigInt &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoderdecode (bool &out)
BER_Decoderdecode (bool &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoderdecode (secure_vector< uint8_t > &v, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoderdecode (size_t &out)
BER_Decoderdecode (size_t &v, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoderdecode (std::vector< uint8_t > &v, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename Alloc>
BER_Decoderdecode (std::vector< uint8_t, Alloc > &out, ASN1_Type real_type)
template<typename T>
BER_Decoderdecode_and_check (const T &expected, std::string_view error_msg)
uint64_t decode_constrained_integer (ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
template<typename T>
BER_Decoderdecode_integer_type (T &out)
template<typename T>
BER_Decoderdecode_integer_type (T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename T>
BER_Decoderdecode_list (std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
BER_Decoderdecode_null ()
BER_Decoderdecode_octet_string_bigint (BigInt &b)
template<typename T>
BER_Decoderdecode_optional (std::optional< T > &out, ASN1_Type type_tag, ASN1_Class class_tag)
template<typename T>
BER_Decoderdecode_optional (T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
template<typename T>
BER_Decoderdecode_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 > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
template<typename Alloc>
BER_Decoderdecode_optional_string (std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, ASN1_Type expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
template<typename Alloc>
BER_Decoderdecode_optional_string (std::vector< uint8_t, Alloc > &out, ASN1_Type real_type, uint32_t expected_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
BER_Decoderdiscard_remaining ()
BER_Decoderend_cons ()
BER_Decoderget_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_Decoderget_next_value (T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
bool more_items () const
BER_Decoderoperator= (BER_Decoder &&)=default
BER_Decoderoperator= (const BER_Decoder &)=delete
const BER_Objectpeek_next_object ()
void push_back (BER_Object &&obj)
void push_back (const BER_Object &obj)
template<typename Alloc>
BER_Decoderraw_bytes (std::vector< uint8_t, Alloc > &out)
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_Decoderverify_end ()
BER_Decoderverify_end (std::string_view err_msg)
 ~BER_Decoder ()=default

Detailed Description

BER Decoding Object

Definition at line 22 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/9]

◆ BER_Decoder() [2/9]

BOTAN_FUTURE_EXPLICIT Botan::BER_Decoder::BER_Decoder ( std::span< const uint8_t > buf)
inline

Set up to BER decode the data in buf of length len

Definition at line 32 of file ber_dec.h.

32: BER_Decoder(buf.data(), buf.size()) {}
BER_Decoder(const uint8_t buf[], size_t len)
Definition ber_dec.cpp:335

References BER_Decoder(), and BOTAN_FUTURE_EXPLICIT.

◆ BER_Decoder() [3/9]

Botan::BER_Decoder::BER_Decoder ( const secure_vector< uint8_t > & vec)
explicit

Set up to BER decode the data in vec

Definition at line 343 of file ber_dec.cpp.

343 {
344 m_data_src = std::make_unique<DataSource_Memory>(data);
345 m_source = m_data_src.get();
346}

◆ BER_Decoder() [4/9]

Botan::BER_Decoder::BER_Decoder ( const std::vector< uint8_t > & vec)
explicit

Set up to BER decode the data in vec

Definition at line 351 of file ber_dec.cpp.

351 {
352 m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
353 m_source = m_data_src.get();
354}

◆ BER_Decoder() [5/9]

Botan::BER_Decoder::BER_Decoder ( DataSource & src)
explicit

Set up to BER decode the data in src

Definition at line 330 of file ber_dec.cpp.

330: m_source(&src) {}

◆ BER_Decoder() [6/9]

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

Set up to BER decode the data in obj

Definition at line 52 of file ber_dec.h.

52: BER_Decoder(obj.bits(), obj.length()) {}

References BER_Decoder(), and BOTAN_FUTURE_EXPLICIT.

◆ BER_Decoder() [7/9]

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

Set up to BER decode the data in obj

Definition at line 57 of file ber_dec.h.

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

References BER_Decoder(), and BOTAN_FUTURE_EXPLICIT.

◆ BER_Decoder() [8/9]

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

Definition at line 359 of file ber_dec.cpp.

359 : m_parent(other.m_parent), m_source(other.m_source) {
360 // take ownership of other's data source
361 std::swap(m_data_src, other.m_data_src);
362}

References BER_Decoder().

◆ BER_Decoder() [9/9]

Botan::BER_Decoder::BER_Decoder ( BER_Decoder && other)
default

References BER_Decoder().

◆ ~BER_Decoder()

Botan::BER_Decoder::~BER_Decoder ( )
default

References BER_Decoder().

Member Function Documentation

◆ decode() [1/10]

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

Definition at line 367 of file ber_dec.cpp.

367 {
368 obj.decode_from(*this);
369 return (*this);
370}

References BER_Decoder(), and Botan::ASN1_Object::decode_from().

◆ decode() [2/10]

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

Definition at line 198 of file ber_dec.h.

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

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

Referenced by decode().

◆ decode() [3/10]

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

Definition at line 459 of file ber_dec.cpp.

459 {
460 BER_Object obj = get_next_object();
461 obj.assert_is_a(type_tag, class_tag);
462
463 if(obj.length() == 0) {
464 out.clear();
465 } else {
466 const uint8_t first = obj.bits()[0];
467 const bool negative = (first & 0x80) == 0x80;
468
469 if(negative) {
470 secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
471 for(size_t i = obj.length(); i > 0; --i) {
472 const bool gt0 = (vec[i - 1] > 0);
473 vec[i - 1] -= 1;
474 if(gt0) {
475 break;
476 }
477 }
478 for(size_t i = 0; i != obj.length(); ++i) {
479 vec[i] = ~vec[i];
480 }
481 out._assign_from_bytes(vec);
482 out.flip_sign();
483 } else {
484 out._assign_from_bytes(obj.data());
485 }
486 }
487
488 return (*this);
489}
BER_Object get_next_object()
Definition ber_dec.cpp:248
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69

References Botan::BigInt::_assign_from_bytes(), Botan::BER_Object::assert_is_a(), BER_Decoder(), Botan::BER_Object::bits(), Botan::BigInt::clear(), Botan::BER_Object::data(), Botan::BigInt::flip_sign(), get_next_object(), and Botan::BER_Object::length().

◆ decode() [4/10]

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

Decode a BER encoded BOOLEAN

Definition at line 188 of file ber_dec.h.

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

Referenced by Botan::TLS::Certificate_Authorities::Certificate_Authorities(), 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::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(), decode_list(), decode_octet_string_bigint(), decode_optional(), decode_optional_implicit(), 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::pbes2_decrypt(), Botan::OCSP::Response::Response(), Botan::RSA_PublicKey::RSA_PublicKey(), Botan::TLS::Session::Session(), and Botan::X25519_PrivateKey::X25519_PrivateKey().

◆ decode() [5/10]

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

Definition at line 394 of file ber_dec.cpp.

394 {
395 BER_Object obj = get_next_object();
396 obj.assert_is_a(type_tag, class_tag);
397
398 if(obj.length() != 1) {
399 throw BER_Decoding_Error("BER boolean value had invalid size");
400 }
401
402 const uint8_t val = obj.bits()[0];
403
404 // TODO if decoding DER we should reject non-canonical booleans
405 out = (val != 0) ? true : false;
406
407 return (*this);
408}

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

◆ decode() [6/10]

BER_Decoder & Botan::BER_Decoder::decode ( secure_vector< uint8_t > & v,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Definition at line 524 of file ber_dec.cpp.

527 {
528 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
529 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
530 }
531
532 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
533 return (*this);
534}

References BER_Decoder(), Botan::BitString, get_next_object(), and Botan::OctetString.

◆ decode() [7/10]

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

Definition at line 193 of file ber_dec.h.

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

Referenced by decode().

◆ decode() [8/10]

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

Definition at line 413 of file ber_dec.cpp.

413 {
414 BigInt integer;
415 decode(integer, type_tag, class_tag);
416
417 if(integer.is_negative()) {
418 throw BER_Decoding_Error("Decoded small integer value was negative");
419 }
420
421 if(integer.bits() > 32) {
422 throw BER_Decoding_Error("Decoded integer value larger than expected");
423 }
424
425 out = 0;
426 for(size_t i = 0; i != 4; ++i) {
427 out = (out << 8) | integer.byte_at(3 - i);
428 }
429
430 return (*this);
431}

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

◆ decode() [9/10]

BER_Decoder & Botan::BER_Decoder::decode ( std::vector< uint8_t > & v,
ASN1_Type real_type,
ASN1_Type type_tag,
ASN1_Class class_tag = ASN1_Class::ContextSpecific )

Definition at line 536 of file ber_dec.cpp.

539 {
540 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
541 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
542 }
543
544 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
545 return (*this);
546}

References BER_Decoder(), Botan::BitString, get_next_object(), and Botan::OctetString.

◆ decode() [10/10]

template<typename Alloc>
BER_Decoder & Botan::BER_Decoder::decode ( std::vector< uint8_t, Alloc > & out,
ASN1_Type real_type )
inline

Definition at line 210 of file ber_dec.h.

210 {
211 return decode(out, real_type, real_type, ASN1_Class::Universal);
212 }

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

◆ decode_and_check()

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

Definition at line 282 of file ber_dec.h.

282 {
283 T actual;
284 decode(actual);
285
286 if(actual != expected) {
287 throw Decoding_Error(error_msg);
288 }
289
290 return (*this);
291 }

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_constrained_integer()

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

Definition at line 436 of file ber_dec.cpp.

436 {
437 if(T_bytes > 8) {
438 throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
439 }
440
441 BigInt integer;
442 decode(integer, type_tag, class_tag);
443
444 if(integer.bits() > 8 * T_bytes) {
445 throw BER_Decoding_Error("Decoded integer value larger than expected");
446 }
447
448 uint64_t out = 0;
449 for(size_t i = 0; i != 8; ++i) {
450 out = (out << 8) | integer.byte_at(7 - i);
451 }
452
453 return out;
454}

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

Referenced by decode_integer_type().

◆ decode_integer_type() [1/2]

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

Definition at line 242 of file ber_dec.h.

242 {
244 }
BER_Decoder & decode_integer_type(T &out)
Definition ber_dec.h:242

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

Definition at line 247 of file ber_dec.h.

247 {
248 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
249 return (*this);
250 }
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition ber_dec.cpp:436

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

◆ decode_list()

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

Definition at line 393 of file ber_dec.h.

393 {
394 BER_Decoder list = start_cons(type_tag, class_tag);
395
396 while(list.more_items()) {
397 T value;
398 list.decode(value);
399 vec.push_back(std::move(value));
400 }
401
402 list.end_cons();
403
404 return (*this);
405}
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:303

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_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 375 of file ber_dec.cpp.

375 {
376 BER_Object obj = get_next_object();
377 obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
378 if(obj.length() > 0) {
379 throw BER_Decoding_Error("NULL object had nonzero size");
380 }
381 return (*this);
382}

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::IPAddressBlocks::IPAddressChoice< V >::decode_from().

◆ 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 384 of file ber_dec.cpp.

384 {
387 out = BigInt::from_bytes(out_vec);
388 return (*this);
389}
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:87

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 > & out,
ASN1_Type type_tag,
ASN1_Class class_tag )

Definition at line 344 of file ber_dec.h.

344 {
345 BER_Object obj = get_next_object();
346
347 if(obj.is_a(type_tag, class_tag)) {
348 T out{};
349 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
350 BER_Decoder(std::move(obj)).decode(out).verify_end();
351 } else {
352 this->push_back(std::move(obj));
353 this->decode(out, type_tag, class_tag);
354 }
355 optval = std::move(out);
356 } else {
357 this->push_back(std::move(obj));
358 optval = std::nullopt;
359 }
360
361 return (*this);
362}
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:289

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

Definition at line 253 of file ber_dec.h.

253 {
254 std::optional<T> optval;
255 this->decode_optional(optval, type_tag, class_tag);
256 out = optval ? *optval : default_value;
257 return (*this);
258 }
BER_Decoder & decode_optional(T &out, ASN1_Type type_tag, ASN1_Class class_tag, const T &default_value=T())
Definition ber_dec.h:253

References BER_Decoder(), and decode_optional().

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

◆ 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() )

Definition at line 368 of file ber_dec.h.

373 {
374 BER_Object obj = get_next_object();
375
376 if(obj.is_a(type_tag, class_tag)) {
377 obj.set_tagging(real_type, real_class);
378 push_back(std::move(obj));
379 decode(out, real_type, real_class);
380 } else {
381 // Not what we wanted, push it back on the stream
382 out = default_value;
383 push_back(std::move(obj));
384 }
385
386 return (*this);
387}

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

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

◆ decode_optional_list()

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

Definition at line 411 of file ber_dec.h.

411 {
412 if(peek_next_object().is_a(type_tag, class_tag)) {
413 decode_list(vec, type_tag, class_tag);
414 return true;
415 }
416
417 return false;
418}
const BER_Object & peek_next_object()
Definition ber_dec.cpp:237
BER_Decoder & decode_list(std::vector< T > &out, ASN1_Type type_tag=ASN1_Type::Sequence, ASN1_Class class_tag=ASN1_Class::Universal)
Definition ber_dec.h:393

References decode_list(), and peek_next_object().

◆ decode_optional_string() [1/2]

template<typename Alloc>
BER_Decoder & Botan::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 )
inline

Definition at line 321 of file ber_dec.h.

324 {
325 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
326 }
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)
Definition ber_dec.h:297

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

◆ decode_optional_string() [2/2]

template<typename Alloc>
BER_Decoder & Botan::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 )
inline

Definition at line 297 of file ber_dec.h.

300 {
301 BER_Object obj = get_next_object();
302
303 ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
304
305 if(obj.is_a(type_tag, class_tag)) {
306 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
307 BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
308 } else {
309 push_back(std::move(obj));
310 decode(out, real_type, type_tag, class_tag);
311 }
312 } else {
313 out.clear();
314 push_back(std::move(obj));
315 }
316
317 return (*this);
318 }
ASN1_Type
Definition asn1_obj.h:43

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

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 231 of file ber_dec.cpp.

231 {
232 uint8_t buf = 0;
233 while(m_source->read_byte(buf) != 0) {}
234 return (*this);
235}

References BER_Decoder().

Referenced by Botan::Ed25519_PrivateKey::Ed25519_PrivateKey(), and Botan::X25519_PrivateKey::X25519_PrivateKey().

◆ end_cons()

◆ get_next()

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

Definition at line 71 of file ber_dec.h.

71 {
72 ber = get_next_object();
73 return (*this);
74 }

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 248 of file ber_dec.cpp.

248 {
249 BER_Object next;
250
251 if(m_pushed.is_set()) {
252 std::swap(next, m_pushed);
253 return next;
254 }
255
256 for(;;) {
259 decode_tag(m_source, type_tag, class_tag);
260 next.set_tagging(type_tag, class_tag);
261 if(next.is_set() == false) { // no more objects
262 return next;
263 }
264
265 size_t field_size = 0;
266 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
267 if(!m_source->check_available(length)) {
268 throw BER_Decoding_Error("Value truncated");
269 }
270
271 uint8_t* out = next.mutable_bits(length);
272 if(m_source->read(out, length) != length) {
273 throw BER_Decoding_Error("Value truncated");
274 }
275
276 if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
277 continue;
278 } else {
279 break;
280 }
281 }
282
283 return next;
284}
ASN1_Class
Definition asn1_obj.h:28

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

Referenced by decode(), decode(), decode(), decode(), 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::CRL_Entry::decode_from(), Botan::GeneralName::decode_from(), Botan::OID::decode_from(), decode_null(), decode_optional(), decode_optional_implicit(), decode_optional_string(), get_next(), get_next_value(), operator=(), peek_next_object(), and start_cons().

◆ get_next_octet_string()

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

Definition at line 200 of file ber_dec.h.

200 {
201 std::vector<uint8_t> out_vec;
203 return out_vec;
204 }

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 154 of file ber_dec.h.

156 {
157 BER_Object obj = get_next_object();
158 obj.assert_is_a(type_tag, class_tag);
159
160 if(obj.length() != sizeof(T)) {
161 throw BER_Decoding_Error("Size mismatch. Object value size is " + std::to_string(obj.length()) +
162 "; Output type size is " + std::to_string(sizeof(T)));
163 }
164
165 std::memcpy(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
166
167 return (*this);
168 }

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

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 204 of file ber_dec.cpp.

204 {
205 if(m_source->end_of_data() && !m_pushed.is_set()) {
206 return false;
207 }
208 return true;
209}

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

◆ operator=() [1/2]

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

References BER_Decoder(), and get_next_object().

◆ 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 237 of file ber_dec.cpp.

237 {
238 if(!m_pushed.is_set()) {
239 m_pushed = get_next_object();
240 }
241
242 return m_pushed;
243}

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::IPAddressBlocks::IPAddressChoice< V >::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressFamily::decode_from(), Botan::Cert_Extension::IPAddressBlocks::IPAddressOrRange< V >::decode_from(), 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 296 of file ber_dec.cpp.

296 {
297 if(m_pushed.is_set()) {
298 throw Invalid_State("BER_Decoder: Only one push back is allowed");
299 }
300 m_pushed = std::move(obj);
301}

◆ 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 289 of file ber_dec.cpp.

289 {
290 if(m_pushed.is_set()) {
291 throw Invalid_State("BER_Decoder: Only one push back is allowed");
292 }
293 m_pushed = obj;
294}

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

◆ raw_bytes()

template<typename Alloc>
BER_Decoder & Botan::BER_Decoder::raw_bytes ( std::vector< uint8_t, Alloc > & out)
inline

Definition at line 174 of file ber_dec.h.

174 {
175 out.clear();
176 uint8_t buf = 0;
177 while(m_source->read_byte(buf)) {
178 out.push_back(buf);
179 }
180 return (*this);
181 }

References BER_Decoder().

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 )

Definition at line 303 of file ber_dec.cpp.

303 {
304 BER_Object obj = get_next_object();
305 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
306 return BER_Decoder(std::move(obj), this);
307}

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

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_explicit_context_specific()

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

Definition at line 133 of file ber_dec.h.

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

◆ start_sequence()

◆ start_set()

BER_Decoder Botan::BER_Decoder::start_set ( )
inline

◆ verify_end() [1/2]

BER_Decoder & Botan::BER_Decoder::verify_end ( )

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

Definition at line 214 of file ber_dec.cpp.

214 {
215 return verify_end("BER_Decoder::verify_end called, but data remains");
216}
BER_Decoder & verify_end()
Definition ber_dec.cpp:214

References BER_Decoder(), and verify_end().

Referenced by Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), Botan::Ed448_PrivateKey::Ed448_PrivateKey(), Botan::pbes2_decrypt(), and verify_end().

◆ 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 221 of file ber_dec.cpp.

221 {
222 if(!m_source->end_of_data() || m_pushed.is_set()) {
223 throw Decoding_Error(err);
224 }
225 return (*this);
226}

References BER_Decoder().


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