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

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (BER_Decoder &&other) noexcept
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 &&) noexcept
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 ()

Detailed Description

BER Decoding Object

Definition at line 25 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 35 of file ber_dec.h.

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

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

368 {
369 m_data_src = std::make_unique<DataSource_Memory>(data);
370 m_source = m_data_src.get();
371}

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

376 {
377 m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
378 m_source = m_data_src.get();
379}

◆ BER_Decoder() [5/9]

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

Set up to BER decode the data in src

Definition at line 355 of file ber_dec.cpp.

355: 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 55 of file ber_dec.h.

55: 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 60 of file ber_dec.h.

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

384 : m_parent(other.m_parent), m_source(other.m_source) {
385 // take ownership of other's data source
386 std::swap(m_data_src, other.m_data_src);
387}

References BER_Decoder().

◆ BER_Decoder() [9/9]

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

References BER_Decoder().

◆ ~BER_Decoder()

Botan::BER_Decoder::~BER_Decoder ( )
default

References BER_Decoder(), and get_next_value().

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

396 {
397 obj.decode_from(*this);
398 return (*this);
399}

References BER_Decoder(), and decode().

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

488 {
489 const BER_Object obj = get_next_object();
490 obj.assert_is_a(type_tag, class_tag);
491
492 if(obj.length() == 0) {
493 out.clear();
494 } else {
495 const uint8_t first = obj.bits()[0];
496 const bool negative = (first & 0x80) == 0x80;
497
498 if(negative) {
499 secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
500 for(size_t i = obj.length(); i > 0; --i) {
501 const bool gt0 = (vec[i - 1] > 0);
502 vec[i - 1] -= 1;
503 if(gt0) {
504 break;
505 }
506 }
507 for(size_t i = 0; i != obj.length(); ++i) {
508 vec[i] = ~vec[i];
509 }
510 out._assign_from_bytes(vec);
511 out.flip_sign();
512 } else {
513 out._assign_from_bytes(obj.data());
514 }
515 }
516
517 return (*this);
518}
BER_Object get_next_object()
Definition ber_dec.cpp:261
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68

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(), 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::McEliece_PublicKey::McEliece_PublicKey(), 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 423 of file ber_dec.cpp.

423 {
424 const BER_Object obj = get_next_object();
425 obj.assert_is_a(type_tag, class_tag);
426
427 if(obj.length() != 1) {
428 throw BER_Decoding_Error("BER boolean value had invalid size");
429 }
430
431 const uint8_t val = obj.bits()[0];
432
433 // TODO if decoding DER we should reject non-canonical booleans
434 out = (val != 0) ? true : false;
435
436 return (*this);
437}

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

556 {
557 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
558 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
559 }
560
561 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
562 return (*this);
563}

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

442 {
443 BigInt integer;
444 decode(integer, type_tag, class_tag);
445
446 if(integer.is_negative()) {
447 throw BER_Decoding_Error("Decoded small integer value was negative");
448 }
449
450 if(integer.bits() > 32) {
451 throw BER_Decoding_Error("Decoded integer value larger than expected");
452 }
453
454 out = 0;
455 for(size_t i = 0; i != 4; ++i) {
456 out = (out << 8) | integer.byte_at(3 - i);
457 }
458
459 return (*this);
460}

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

568 {
569 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
570 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
571 }
572
573 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
574 return (*this);
575}

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

465 {
466 if(T_bytes > 8) {
467 throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
468 }
469
470 BigInt integer;
471 decode(integer, type_tag, class_tag);
472
473 if(integer.bits() > 8 * T_bytes) {
474 throw BER_Decoding_Error("Decoded integer value larger than expected");
475 }
476
477 uint64_t out = 0;
478 for(size_t i = 0; i != 8; ++i) {
479 out = (out << 8) | integer.byte_at(7 - i);
480 }
481
482 return out;
483}

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:465

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

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

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

404 {
405 const BER_Object obj = get_next_object();
406 obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
407 if(obj.length() > 0) {
408 throw BER_Decoding_Error("NULL object had nonzero size");
409 }
410 return (*this);
411}

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

413 {
416 out = BigInt::from_bytes(out_vec);
417 return (*this);
418}
static BigInt from_bytes(std::span< const uint8_t > bytes)
Definition bigint.cpp:83

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

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

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

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

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

415 {
416 if(peek_next_object().is_a(type_tag, class_tag)) {
417 decode_list(vec, type_tag, class_tag);
418 return true;
419 }
420
421 return false;
422}
const BER_Object & peek_next_object()
Definition ber_dec.cpp:250
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:397

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

234 {
235 uint8_t buf = 0;
236 while(m_source->read_byte(buf) != 0) {}
237 return (*this);
238}

References BER_Decoder().

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

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

◆ get_next()

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

Definition at line 74 of file ber_dec.h.

74 {
75 ber = get_next_object();
76 return (*this);
77 }

References BER_Decoder(), get_next(), and get_next_object().

Referenced by Botan::OCSP::SingleResponse::decode_from(), and get_next().

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

261 {
262 BER_Object next;
263
264 if(m_pushed.is_set()) {
265 std::swap(next, m_pushed);
266 return next;
267 }
268
269 for(;;) {
272 decode_tag(m_source, type_tag, class_tag);
273 next.set_tagging(type_tag, class_tag);
274 if(next.is_set() == false) { // no more objects
275 return next;
276 }
277
278 size_t field_size = 0;
279 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
280 if(!m_source->check_available(length)) {
281 throw BER_Decoding_Error("Value truncated");
282 }
283
284 uint8_t* out = next.mutable_bits(length);
285 if(m_source->read(out, length) != length) {
286 throw BER_Decoding_Error("Value truncated");
287 }
288
289 if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
290 continue;
291 } else {
292 break;
293 }
294 }
295
296 return next;
297}
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(), 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 157 of file ber_dec.h.

159 {
160 const BER_Object obj = get_next_value(sizeof(T), type_tag, class_tag);
161
162 std::memcpy(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
163
164 return (*this);
165 }
BER_Decoder & get_next_value(T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
Definition ber_dec.h:157

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

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 207 of file ber_dec.cpp.

207 {
208 if(m_source->end_of_data() && !m_pushed.is_set()) {
209 return false;
210 }
211 return true;
212}

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 && )
defaultnoexcept

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

250 {
251 if(!m_pushed.is_set()) {
252 m_pushed = get_next_object();
253 }
254
255 return m_pushed;
256}

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

321 {
322 if(m_pushed.is_set()) {
323 throw Invalid_State("BER_Decoder: Only one push back is allowed");
324 }
325 m_pushed = std::move(obj);
326}

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

314 {
315 if(m_pushed.is_set()) {
316 throw Invalid_State("BER_Decoder: Only one push back is allowed");
317 }
318 m_pushed = obj;
319}

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

171 {
172 out.clear();
173 for(;;) {
174 if(auto next = this->read_next_byte()) {
175 out.push_back(*next);
176 } else {
177 break;
178 }
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 328 of file ber_dec.cpp.

328 {
329 BER_Object obj = get_next_object();
330 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
331 return BER_Decoder(std::move(obj), this);
332}

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

217 {
218 return verify_end("BER_Decoder::verify_end called, but data remains");
219}
BER_Decoder & verify_end()
Definition ber_dec.cpp:217

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

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

References BER_Decoder().


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