Botan 3.4.0
Crypto and TLS for C&
Public Member Functions | List of all members
Botan::BER_Decoder Class Referencefinal

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (BER_Object &&obj)
 
 BER_Decoder (const BER_Decoder &other)
 
 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)
 
 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 (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 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<T>::value && std::is_trivial<T>::value
BER_Decoderget_next_value (T &out, ASN1_Type type_tag, ASN1_Class class_tag=ASN1_Class::ContextSpecific)
 
bool more_items () const
 
BER_Decoderoperator= (const BER_Decoder &)=delete
 
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)
 

Detailed Description

BER Decoding Object

Definition at line 22 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/8]

Botan::BER_Decoder::BER_Decoder ( const uint8_t buf[],
size_t len )

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

Definition at line 321 of file ber_dec.cpp.

321 {
322 m_data_src = std::make_unique<DataSource_Memory>(data, length);
323 m_source = m_data_src.get();
324}

Referenced by decode_optional(), and start_cons().

◆ BER_Decoder() [2/8]

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

◆ BER_Decoder() [3/8]

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

Set up to BER decode the data in vec

Definition at line 329 of file ber_dec.cpp.

329 {
330 m_data_src = std::make_unique<DataSource_Memory>(data);
331 m_source = m_data_src.get();
332}

◆ BER_Decoder() [4/8]

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

Set up to BER decode the data in vec

Definition at line 337 of file ber_dec.cpp.

337 {
338 m_data_src = std::make_unique<DataSource_Memory>(data.data(), data.size());
339 m_source = m_data_src.get();
340}

◆ BER_Decoder() [5/8]

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

Set up to BER decode the data in src

Definition at line 314 of file ber_dec.cpp.

314 {
315 m_source = &src;
316}

◆ BER_Decoder() [6/8]

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()) {}

◆ BER_Decoder() [7/8]

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

◆ BER_Decoder() [8/8]

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

Definition at line 345 of file ber_dec.cpp.

345 {
346 m_source = other.m_source;
347
348 // take ownership
349 std::swap(m_data_src, other.m_data_src);
350 m_parent = other.m_parent;
351}

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

356 {
357 obj.decode_from(*this);
358 return (*this);
359}

References Botan::ASN1_Object::decode_from().

◆ decode() [2/10]

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

Definition at line 186 of file ber_dec.h.

References decode().

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

444 {
445 BER_Object obj = get_next_object();
446 obj.assert_is_a(type_tag, class_tag);
447
448 if(obj.length() == 0) {
449 out.clear();
450 } else {
451 const bool negative = (obj.bits()[0] & 0x80) ? true : false;
452
453 if(negative) {
454 secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
455 for(size_t i = obj.length(); i > 0; --i) {
456 if(vec[i - 1]--) {
457 break;
458 }
459 }
460 for(size_t i = 0; i != obj.length(); ++i) {
461 vec[i] = ~vec[i];
462 }
463 out = BigInt(vec.data(), vec.size());
464 out.flip_sign();
465 } else {
466 out = BigInt(obj.bits(), obj.length());
467 }
468 }
469
470 return (*this);
471}
BER_Object get_next_object()
Definition ber_dec.cpp:231

References Botan::BER_Object::assert_is_a(), Botan::BER_Object::bits(), Botan::BigInt::clear(), 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() [5/10]

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

Definition at line 383 of file ber_dec.cpp.

383 {
384 BER_Object obj = get_next_object();
385 obj.assert_is_a(type_tag, class_tag);
386
387 if(obj.length() != 1) {
388 throw BER_Decoding_Error("BER boolean value had invalid size");
389 }
390
391 out = (obj.bits()[0]) ? true : false;
392 return (*this);
393}

References Botan::BER_Object::assert_is_a(), 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 506 of file ber_dec.cpp.

509 {
510 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
511 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
512 }
513
514 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
515 return (*this);
516}

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

◆ decode() [7/10]

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

Definition at line 181 of file ber_dec.h.

References decode().

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

398 {
399 BigInt integer;
400 decode(integer, type_tag, class_tag);
401
402 if(integer.is_negative()) {
403 throw BER_Decoding_Error("Decoded small integer value was negative");
404 }
405
406 if(integer.bits() > 32) {
407 throw BER_Decoding_Error("Decoded integer value larger than expected");
408 }
409
410 out = 0;
411 for(size_t i = 0; i != 4; ++i) {
412 out = (out << 8) | integer.byte_at(3 - i);
413 }
414
415 return (*this);
416}

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

521 {
522 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
523 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
524 }
525
526 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
527 return (*this);
528}

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

198 {
199 return decode(out, real_type, real_type, ASN1_Class::Universal);
200 }

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

257 {
258 T actual;
259 decode(actual);
260
261 if(actual != expected) {
262 throw Decoding_Error(error_msg);
263 }
264
265 return (*this);
266 }
FE_25519 T
Definition ge.cpp:34

References T.

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

421 {
422 if(T_bytes > 8) {
423 throw BER_Decoding_Error("Can't decode small integer over 8 bytes");
424 }
425
426 BigInt integer;
427 decode(integer, type_tag, class_tag);
428
429 if(integer.bits() > 8 * T_bytes) {
430 throw BER_Decoding_Error("Decoded integer value larger than expected");
431 }
432
433 uint64_t out = 0;
434 for(size_t i = 0; i != 8; ++i) {
435 out = (out << 8) | integer.byte_at(7 - i);
436 }
437
438 return out;
439}

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

◆ decode_integer_type() [1/2]

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

Definition at line 230 of file ber_dec.h.

230 {
231 return decode_integer_type<T>(out, ASN1_Type::Integer, ASN1_Class::Universal);
232 }

Referenced by 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 235 of file ber_dec.h.

235 {
236 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
237 return (*this);
238 }
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition ber_dec.cpp:421

References T.

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

364 {
365 BER_Decoder list = start_cons(type_tag, class_tag);
366
367 while(list.more_items()) {
368 T value;
369 list.decode(value);
370 vec.push_back(std::move(value));
371 }
372
373 list.end_cons();
374
375 return (*this);
376}
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition ber_dec.cpp:286

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

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

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 364 of file ber_dec.cpp.

364 {
365 BER_Object obj = get_next_object();
366 obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
367 if(obj.length() > 0) {
368 throw BER_Decoding_Error("NULL object had nonzero size");
369 }
370 return (*this);
371}

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

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

373 {
374 secure_vector<uint8_t> out_vec;
376 out = BigInt::decode(out_vec.data(), out_vec.size());
377 return (*this);
378}
static BigInt decode(const uint8_t buf[], size_t length)
Definition bigint.h:773

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

◆ decode_optional()

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

Definition at line 317 of file ber_dec.h.

317 {
318 BER_Object obj = get_next_object();
319
320 if(obj.is_a(type_tag, class_tag)) {
321 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
322 BER_Decoder(std::move(obj)).decode(out).verify_end();
323 } else {
324 push_back(std::move(obj));
325 decode(out, type_tag, class_tag);
326 }
327 } else {
328 out = default_value;
329 push_back(std::move(obj));
330 }
331
332 return (*this);
333}
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:272

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

Referenced by Botan::GeneralSubtree::decode_from(), Botan::Extensions::decode_from(), Botan::PSS_Params::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::EC_PrivateKey::EC_PrivateKey(), 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 339 of file ber_dec.h.

344 {
345 BER_Object obj = get_next_object();
346
347 if(obj.is_a(type_tag, class_tag)) {
348 obj.set_tagging(real_type, real_class);
349 push_back(std::move(obj));
350 decode(out, real_type, real_class);
351 } else {
352 // Not what we wanted, push it back on the stream
353 out = default_value;
354 push_back(std::move(obj));
355 }
356
357 return (*this);
358}

References 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_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 296 of file ber_dec.h.

299 {
300 return decode_optional_string(out, real_type, static_cast<uint32_t>(expected_tag), class_tag);
301 }
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:272

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

275 {
276 BER_Object obj = get_next_object();
277
278 ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
279
280 if(obj.is_a(type_tag, class_tag)) {
281 if(class_tag == ASN1_Class::ExplicitContextSpecific) {
282 BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
283 } else {
284 push_back(std::move(obj));
285 decode(out, real_type, type_tag, class_tag);
286 }
287 } else {
288 out.clear();
289 push_back(std::move(obj));
290 }
291
292 return (*this);
293 }
ASN1_Type
Definition asn1_obj.h:43

References decode(), Botan::BER_Object::is_a(), and verify_end().

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

◆ discard_remaining()

BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Discard any data that remains unread Returns (*this)

Definition at line 222 of file ber_dec.cpp.

222 {
223 uint8_t buf;
224 while(m_source->read_byte(buf)) {}
225 return (*this);
226}
size_t read_byte(uint8_t &out)
Definition data_src.cpp:26

References Botan::DataSource::read_byte().

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

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

Finish decoding a constructed data, throws if any data remains. Returns the parent of *this (ie the object on which start_cons was called).

Definition at line 295 of file ber_dec.cpp.

295 {
296 if(!m_parent) {
297 throw Invalid_State("BER_Decoder::end_cons called with null parent");
298 }
299 if(!m_source->end_of_data()) {
300 throw Decoding_Error("BER_Decoder::end_cons called with data left");
301 }
302 return (*m_parent);
303}
virtual bool end_of_data() const =0

References Botan::DataSource::end_of_data().

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::X509_DN::decode_from(), Botan::GeneralSubtree::decode_from(), Botan::Extensions::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Cert_Extension::CRL_Distribution_Points::Distribution_Point::decode_from(), Botan::PSS_Params::decode_from(), Botan::OCSP::CertID::decode_from(), Botan::OCSP::SingleResponse::decode_from(), Botan::Attribute::decode_from(), Botan::X509_Object::decode_from(), decode_list(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::X509::load_key(), Botan::McEliece_PrivateKey::McEliece_PrivateKey(), Botan::McEliece_PublicKey::McEliece_PublicKey(), Botan::pbes2_decrypt(), Botan::OCSP::Response::Response(), and Botan::RSA_PublicKey::RSA_PublicKey().

◆ get_next()

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

Definition at line 69 of file ber_dec.h.

69 {
70 ber = get_next_object();
71 return (*this);
72 }

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

231 {
232 BER_Object next;
233
234 if(m_pushed.is_set()) {
235 std::swap(next, m_pushed);
236 return next;
237 }
238
239 for(;;) {
240 ASN1_Type type_tag;
241 ASN1_Class class_tag;
242 decode_tag(m_source, type_tag, class_tag);
243 next.set_tagging(type_tag, class_tag);
244 if(next.is_set() == false) { // no more objects
245 return next;
246 }
247
248 size_t field_size;
249 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
250 if(!m_source->check_available(length)) {
251 throw BER_Decoding_Error("Value truncated");
252 }
253
254 uint8_t* out = next.mutable_bits(length);
255 if(m_source->read(out, length) != length) {
256 throw BER_Decoding_Error("Value truncated");
257 }
258
259 if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc)) {
260 continue;
261 } else {
262 break;
263 }
264 }
265
266 return next;
267}
bool is_set() const
Definition asn1_obj.h:138
virtual size_t read(uint8_t out[], size_t length)=0
virtual bool check_available(size_t n)=0
ASN1_Class
Definition asn1_obj.h:28

References Botan::DataSource::check_available(), Botan::Eoc, Botan::BER_Object::is_set(), Botan::DataSource::read(), and Botan::BER_Object::tagging().

Referenced by decode(), decode(), decode(), decode(), Botan::OID::decode_from(), Botan::ASN1_Time::decode_from(), Botan::ASN1_String::decode_from(), Botan::AlternativeName::decode_from(), Botan::GeneralName::decode_from(), decode_null(), decode_optional(), decode_optional_implicit(), and start_cons().

◆ get_next_octet_string()

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

Definition at line 188 of file ber_dec.h.

188 {
189 std::vector<uint8_t> out_vec;
191 return out_vec;
192 }

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

◆ get_next_value()

template<typename T >
requires std::is_standard_layout<T>::value && std::is_trivial<T>::value
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 142 of file ber_dec.h.

144 {
145 BER_Object obj = get_next_object();
146 obj.assert_is_a(type_tag, class_tag);
147
148 if(obj.length() != sizeof(T)) {
149 throw BER_Decoding_Error("Size mismatch. Object value size is " + std::to_string(obj.length()) +
150 "; Output type size is " + std::to_string(sizeof(T)));
151 }
152
153 copy_mem(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
154
155 return (*this);
156 }
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:146

References Botan::BER_Object::assert_is_a(), Botan::BER_Object::bits(), Botan::copy_mem(), Botan::BER_Object::length(), and T.

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 195 of file ber_dec.cpp.

195 {
196 if(m_source->end_of_data() && !m_pushed.is_set()) {
197 return false;
198 }
199 return true;
200}

References Botan::DataSource::end_of_data(), and Botan::BER_Object::is_set().

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

◆ operator=()

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

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

279 {
280 if(m_pushed.is_set()) {
281 throw Invalid_State("BER_Decoder: Only one push back is allowed");
282 }
283 m_pushed = std::move(obj);
284}

References Botan::BER_Object::is_set().

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

272 {
273 if(m_pushed.is_set()) {
274 throw Invalid_State("BER_Decoder: Only one push back is allowed");
275 }
276 m_pushed = obj;
277}

References Botan::BER_Object::is_set().

Referenced by decode_list(), decode_optional(), decode_optional_implicit(), 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 162 of file ber_dec.h.

162 {
163 out.clear();
164 uint8_t buf;
165 while(m_source->read_byte(buf)) {
166 out.push_back(buf);
167 }
168 return (*this);
169 }

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::X509_DN::decode_from(), Botan::Attribute::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 286 of file ber_dec.cpp.

286 {
287 BER_Object obj = get_next_object();
288 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
289 return BER_Decoder(std::move(obj), this);
290}

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

Referenced by decode_list().

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

◆ start_sequence()

BER_Decoder Botan::BER_Decoder::start_sequence ( )
inline

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

205 {
206 return verify_end("BER_Decoder::verify_end called, but data remains");
207}
BER_Decoder & verify_end()
Definition ber_dec.cpp:205

References verify_end().

Referenced by Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), decode_optional_string(), 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 212 of file ber_dec.cpp.

212 {
213 if(!m_source->end_of_data() || m_pushed.is_set()) {
214 throw Decoding_Error(err);
215 }
216 return (*this);
217}

References Botan::DataSource::end_of_data(), and Botan::BER_Object::is_set().


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