Botan 3.0.0-alpha0
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_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, const std::string &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 >
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 (const std::string &err_msg)
 

Detailed Description

BER Decoding Object

Definition at line 21 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/7]

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

331 {
332 m_data_src.reset(new DataSource_Memory(data, length));
333 m_source = m_data_src.get();
334 }

Referenced by decode_optional(), and start_cons().

◆ BER_Decoder() [2/7]

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

Set up to BER decode the data in vec

Definition at line 339 of file ber_dec.cpp.

340 {
341 m_data_src.reset(new DataSource_Memory(data));
342 m_source = m_data_src.get();
343 }

◆ BER_Decoder() [3/7]

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

Set up to BER decode the data in vec

Definition at line 348 of file ber_dec.cpp.

349 {
350 m_data_src.reset(new DataSource_Memory(data.data(), data.size()));
351 m_source = m_data_src.get();
352 }

◆ BER_Decoder() [4/7]

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

Set up to BER decode the data in src

Definition at line 322 of file ber_dec.cpp.

323 {
324 m_source = &src;
325 }

◆ BER_Decoder() [5/7]

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

Set up to BER decode the data in obj

Definition at line 47 of file ber_dec.h.

47 :
48 BER_Decoder(obj.bits(), obj.length()) {}
BER_Decoder(const uint8_t buf[], size_t len)
Definition: ber_dec.cpp:330

◆ BER_Decoder() [6/7]

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

Set up to BER decode the data in obj

Definition at line 53 of file ber_dec.h.

53 :
54 BER_Decoder(std::move(obj), nullptr) {}

◆ BER_Decoder() [7/7]

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

Definition at line 357 of file ber_dec.cpp.

358 {
359 m_source = other.m_source;
360
361 // take ownership
362 std::swap(m_data_src, other.m_data_src);
363 m_parent = other.m_parent;
364 }

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

371 {
372 obj.decode_from(*this);
373 return (*this);
374 }

References Botan::ASN1_Object::decode_from().

◆ decode() [2/10]

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

Definition at line 203 of file ber_dec.h.

204 {
206 }
BER_Decoder & decode(bool &out)
Definition: ber_dec.h:187

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

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

464 {
465 BER_Object obj = get_next_object();
466 obj.assert_is_a(type_tag, class_tag);
467
468 if(obj.length() == 0)
469 {
470 out.clear();
471 }
472 else
473 {
474 const bool negative = (obj.bits()[0] & 0x80) ? true : false;
475
476 if(negative)
477 {
478 secure_vector<uint8_t> vec(obj.bits(), obj.bits() + obj.length());
479 for(size_t i = obj.length(); i > 0; --i)
480 if(vec[i-1]--)
481 break;
482 for(size_t i = 0; i != obj.length(); ++i)
483 vec[i] = ~vec[i];
484 out = BigInt(vec.data(), vec.size());
485 out.flip_sign();
486 }
487 else
488 {
489 out = BigInt(obj.bits(), obj.length());
490 }
491 }
492
493 return (*this);
494 }
BER_Object get_next_object()
Definition: ber_dec.cpp:239

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

401 {
402 BER_Object obj = get_next_object();
403 obj.assert_is_a(type_tag, class_tag);
404
405 if(obj.length() != 1)
406 throw BER_Decoding_Error("BER boolean value had invalid size");
407
408 out = (obj.bits()[0]) ? true : false;
409 return (*this);
410 }

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

533 {
534 if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString)
535 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", static_cast<uint32_t>(real_type));
536
537 asn1_decode_binary_string(buffer, get_next_object(), real_type, type_tag, class_tag);
538 return (*this);
539 }

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

196 {
198 }

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

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

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

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

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

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

220 {
221 return decode(out, real_type, real_type, ASN1_Class::Universal);
222 }

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

◆ decode_and_check()

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

Definition at line 294 of file ber_dec.h.

296 {
297 T actual;
298 decode(actual);
299
300 if(actual != expected)
301 throw Decoding_Error(error_msg);
302
303 return (*this);
304 }
fe T
Definition: ge.cpp:36

References Botan::PEM_Code::decode(), and T.

Referenced by Botan::EC_PrivateKey::EC_PrivateKey(), Botan::Kyber_PrivateKey::Kyber_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 438 of file ber_dec.cpp.

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

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

260 {
261 return decode_integer_type<T>(out, ASN1_Type::Integer, ASN1_Class::Universal);
262 }

References Botan::Integer, and Botan::Universal.

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

268 {
269 out = static_cast<T>(decode_constrained_integer(type_tag, class_tag, sizeof(out)));
270 return (*this);
271 }
uint64_t decode_constrained_integer(ASN1_Type type_tag, ASN1_Class class_tag, size_t T_bytes)
Definition: ber_dec.cpp:438

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

429 {
430 BER_Decoder list = start_cons(type_tag, class_tag);
431
432 while(list.more_items())
433 {
434 T value;
435 list.decode(value);
436 vec.push_back(std::move(value));
437 }
438
439 list.end_cons();
440
441 return (*this);
442 }
BER_Decoder start_cons(ASN1_Type type_tag, ASN1_Class class_tag)
Definition: ber_dec.cpp:293

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

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

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 379 of file ber_dec.cpp.

380 {
381 BER_Object obj = get_next_object();
382 obj.assert_is_a(ASN1_Type::Null, ASN1_Class::Universal);
383 if(obj.length() > 0)
384 throw BER_Decoding_Error("NULL object had nonzero size");
385 return (*this);
386 }

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

389 {
390 secure_vector<uint8_t> out_vec;
392 out = BigInt::decode(out_vec.data(), out_vec.size());
393 return (*this);
394 }
static BigInt decode(const uint8_t buf[], size_t length)
Definition: bigint.h:790

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

369 {
370 BER_Object obj = get_next_object();
371
372 if(obj.is_a(type_tag, class_tag))
373 {
375 {
376 BER_Decoder(std::move(obj)).decode(out).verify_end();
377 }
378 else
379 {
380 push_back(std::move(obj));
381 decode(out, type_tag, class_tag);
382 }
383 }
384 else
385 {
386 out = default_value;
387 push_back(std::move(obj));
388 }
389
390 return (*this);
391 }
void push_back(const BER_Object &obj)
Definition: ber_dec.cpp:279

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

404 {
405 BER_Object obj = get_next_object();
406
407 if(obj.is_a(type_tag, class_tag))
408 {
409 obj.set_tagging(real_type, real_class);
410 push_back(std::move(obj));
411 decode(out, real_type, real_class);
412 }
413 else
414 {
415 // Not what we wanted, push it back on the stream
416 out = default_value;
417 push_back(std::move(obj));
418 }
419
420 return (*this);
421 }

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

345 {
346 return decode_optional_string(out, real_type,
347 static_cast<uint32_t>(expected_tag),
348 class_tag);
349 }
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:310

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

314 {
315 BER_Object obj = get_next_object();
316
317 ASN1_Type type_tag = static_cast<ASN1_Type>(expected_tag);
318
319 if(obj.is_a(type_tag, class_tag))
320 {
322 {
323 BER_Decoder(std::move(obj)).decode(out, real_type).verify_end();
324 }
325 else
326 {
327 push_back(std::move(obj));
328 decode(out, real_type, type_tag, class_tag);
329 }
330 }
331 else
332 {
333 out.clear();
334 push_back(std::move(obj));
335 }
336
337 return (*this);
338 }
ASN1_Type
Definition: asn1_obj.h:39

References decode(), Botan::PEM_Code::decode(), Botan::ExplicitContextSpecific, 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 228 of file ber_dec.cpp.

229 {
230 uint8_t buf;
231 while(m_source->read_byte(buf))
232 {}
233 return (*this);
234 }
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23

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

◆ get_next()

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

Definition at line 66 of file ber_dec.h.

67 {
68 ber = get_next_object();
69 return (*this);
70 }

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

240 {
241 BER_Object next;
242
243 if(m_pushed.is_set())
244 {
245 std::swap(next, m_pushed);
246 return next;
247 }
248
249 for(;;)
250 {
251 ASN1_Type type_tag;
252 ASN1_Class class_tag;
253 decode_tag(m_source, type_tag, class_tag);
254 next.set_tagging(type_tag, class_tag);
255 if(next.is_set() == false) // no more objects
256 return next;
257
258 size_t field_size;
259 const size_t length = decode_length(m_source, field_size, ALLOWED_EOC_NESTINGS);
260 if(!m_source->check_available(length))
261 throw BER_Decoding_Error("Value truncated");
262
263 uint8_t* out = next.mutable_bits(length);
264 if(m_source->read(out, length) != length)
265 throw BER_Decoding_Error("Value truncated");
266
267 if(next.tagging() == static_cast<uint32_t>(ASN1_Type::Eoc))
268 continue;
269 else
270 break;
271 }
272
273 return next;
274 }
bool is_set() const
Definition: asn1_obj.h:141
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:24

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

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

209 {
210 std::vector<uint8_t> out_vec;
212 return out_vec;
213 }

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

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

◆ get_next_value()

template<typename 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 148 of file ber_dec.h.

151 {
152 static_assert(std::is_standard_layout<T>::value && std::is_trivial<T>::value, "Type must be POD");
153
154 BER_Object obj = get_next_object();
155 obj.assert_is_a(type_tag, class_tag);
156
157 if (obj.length() != sizeof(T))
158 throw BER_Decoding_Error(
159 "Size mismatch. Object value size is " +
160 std::to_string(obj.length()) +
161 "; Output type size is " +
162 std::to_string(sizeof(T)));
163
164 copy_mem(reinterpret_cast<uint8_t*>(&out), obj.bits(), obj.length());
165
166 return (*this);
167 }
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
constexpr void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:126

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

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Return true if there is at least one more item remaining

Definition at line 200 of file ber_dec.cpp.

201 {
202 if(m_source->end_of_data() && !m_pushed.is_set())
203 return false;
204 return true;
205 }

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

Referenced by Botan::PK_Verifier::check_signature(), 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 286 of file ber_dec.cpp.

287 {
288 if(m_pushed.is_set())
289 throw Invalid_State("BER_Decoder: Only one push back is allowed");
290 m_pushed = std::move(obj);
291 }

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

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

References Botan::BER_Object::is_set().

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

◆ raw_bytes()

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

Definition at line 173 of file ber_dec.h.

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

References push_back().

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

294 {
295 BER_Object obj = get_next_object();
296 obj.assert_is_a(type_tag, class_tag | ASN1_Class::Constructed);
297 return BER_Decoder(std::move(obj), this);
298 }

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

References Botan::ExplicitContextSpecific.

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

211 {
212 return verify_end("BER_Decoder::verify_end called, but data remains");
213 }
BER_Decoder & verify_end()
Definition: ber_dec.cpp:210

References verify_end().

Referenced by Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), decode_optional_string(), Botan::pbes2_decrypt(), Botan::TLS::Session::Session(), and verify_end().

◆ verify_end() [2/2]

BER_Decoder & Botan::BER_Decoder::verify_end ( const std::string &  err_msg)

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

Definition at line 218 of file ber_dec.cpp.

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

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


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