Botan  1.10.16
Public Member Functions | List of all members
Botan::BER_Decoder Class Reference

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (DataSource &)
 
 BER_Decoder (const byte[], size_t)
 
 BER_Decoder (const MemoryRegion< byte > &)
 
 BER_Decoder (const BER_Decoder &)
 
BER_Decoderdecode (bool &)
 
BER_Decoderdecode (size_t &)
 
BER_Decoderdecode (class BigInt &)
 
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag)
 
BER_Decoderdecode (bool &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (size_t &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (class BigInt &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
 
BER_Decoderdecode (class ASN1_Object &)
 
template<typename T >
BER_Decoderdecode_and_check (const T &expected, const std::string &error_msg)
 
template<typename T >
BER_Decoderdecode_list (std::vector< T > &out, bool clear_out=true)
 
BER_Decoderdecode_null ()
 
BER_Decoderdecode_octet_string_bigint (class BigInt &)
 
template<typename T >
BER_Decoderdecode_optional (T &out, ASN1_Tag type_tag, ASN1_Tag class_tag, const T &default_value=T())
 
BER_Decoderdecode_optional_string (MemoryRegion< byte > &, ASN1_Tag, u16bit)
 
BER_Decoderdiscard_remaining ()
 
BER_Decoderend_cons ()
 
BER_Object get_next_object ()
 
bool more_items () const
 
void push_back (const BER_Object &)
 
BER_Decoderraw_bytes (MemoryRegion< byte > &)
 
BER_Decoder start_cons (ASN1_Tag, ASN1_Tag=UNIVERSAL)
 
BER_Decoderverify_end ()
 
 ~BER_Decoder ()
 

Detailed Description

BER Decoding Object

Definition at line 19 of file ber_dec.h.

Constructor & Destructor Documentation

◆ BER_Decoder() [1/4]

Botan::BER_Decoder::BER_Decoder ( DataSource src)

Definition at line 264 of file ber_dec.cpp.

References Botan::NO_OBJECT.

265  {
266  source = &src;
267  owns = false;
268  pushed.type_tag = pushed.class_tag = NO_OBJECT;
269  parent = 0;
270  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ BER_Decoder() [2/4]

Botan::BER_Decoder::BER_Decoder ( const byte  data[],
size_t  length 
)

Definition at line 275 of file ber_dec.cpp.

References Botan::NO_OBJECT.

276  {
277  source = new DataSource_Memory(data, length);
278  owns = true;
279  pushed.type_tag = pushed.class_tag = NO_OBJECT;
280  parent = 0;
281  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ BER_Decoder() [3/4]

Botan::BER_Decoder::BER_Decoder ( const MemoryRegion< byte > &  data)

Definition at line 286 of file ber_dec.cpp.

References Botan::NO_OBJECT.

287  {
288  source = new DataSource_Memory(data);
289  owns = true;
290  pushed.type_tag = pushed.class_tag = NO_OBJECT;
291  parent = 0;
292  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ BER_Decoder() [4/4]

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

Definition at line 297 of file ber_dec.cpp.

References Botan::NO_OBJECT.

298  {
299  source = other.source;
300  owns = false;
301  if(other.owns)
302  {
303  other.owns = false;
304  owns = true;
305  }
306  pushed.type_tag = pushed.class_tag = NO_OBJECT;
307  parent = other.parent;
308  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ ~BER_Decoder()

Botan::BER_Decoder::~BER_Decoder ( )

Definition at line 313 of file ber_dec.cpp.

314  {
315  if(owns)
316  delete source;
317  source = 0;
318  }

Member Function Documentation

◆ decode() [1/9]

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

◆ decode() [2/9]

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

Definition at line 352 of file ber_dec.cpp.

References Botan::PEM_Code::decode(), Botan::INTEGER, and Botan::UNIVERSAL.

353  {
354  return decode(out, INTEGER, UNIVERSAL);
355  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344

◆ decode() [3/9]

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

Definition at line 360 of file ber_dec.cpp.

References Botan::PEM_Code::decode(), Botan::INTEGER, and Botan::UNIVERSAL.

361  {
362  return decode(out, INTEGER, UNIVERSAL);
363  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344

◆ decode() [4/9]

BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type 
)

Definition at line 444 of file ber_dec.cpp.

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

445  {
446  return decode(out, real_type, real_type, UNIVERSAL);
447  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344

◆ decode() [5/9]

BER_Decoder & Botan::BER_Decoder::decode ( bool &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 376 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

378  {
379  BER_Object obj = get_next_object();
380  obj.assert_is_a(type_tag, class_tag);
381 
382  if(obj.value.size() != 1)
383  throw BER_Decoding_Error("BER boolean value had invalid size");
384 
385  out = (obj.value[0]) ? true : false;
386  return (*this);
387  }
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ decode() [6/9]

BER_Decoder & Botan::BER_Decoder::decode ( size_t &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 392 of file ber_dec.cpp.

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

394  {
395  BigInt integer;
396  decode(integer, type_tag, class_tag);
397 
398  if(integer.bits() > 32)
399  throw BER_Decoding_Error("Decoded integer value larger than expected");
400 
401  out = 0;
402  for(size_t i = 0; i != 4; ++i)
403  out = (out << 8) | integer.byte_at(3-i);
404 
405  return (*this);
406  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344

◆ decode() [7/9]

BER_Decoder & Botan::BER_Decoder::decode ( class BigInt out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 411 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::MemoryRegion< T >::empty(), Botan::BigInt::flip_sign(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

413  {
414  BER_Object obj = get_next_object();
415  obj.assert_is_a(type_tag, class_tag);
416 
417  if(obj.value.empty())
418  out = 0;
419  else
420  {
421  const bool negative = (obj.value[0] & 0x80) ? true : false;
422 
423  if(negative)
424  {
425  for(size_t i = obj.value.size(); i > 0; --i)
426  if(obj.value[i-1]--)
427  break;
428  for(size_t i = 0; i != obj.value.size(); ++i)
429  obj.value[i] = ~obj.value[i];
430  }
431 
432  out = BigInt(&obj.value[0], obj.value.size());
433 
434  if(negative)
435  out.flip_sign();
436  }
437 
438  return (*this);
439  }
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ decode() [8/9]

BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  buffer,
ASN1_Tag  real_type,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 452 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::BIT_STRING, Botan::copy_mem(), Botan::MemoryRegion< T >::empty(), Botan::OCTET_STRING, Botan::MemoryRegion< T >::resize(), Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

455  {
456  if(real_type != OCTET_STRING && real_type != BIT_STRING)
457  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
458 
459  BER_Object obj = get_next_object();
460  obj.assert_is_a(type_tag, class_tag);
461 
462  if(real_type == OCTET_STRING)
463  buffer = obj.value;
464  else
465  {
466  if(obj.value.empty())
467  throw BER_Decoding_Error("Invalid BIT STRING");
468  if(obj.value[0] >= 8)
469  throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
470 
471  buffer.resize(obj.value.size() - 1);
472  copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1);
473  }
474  return (*this);
475  }
void resize(size_t n)
Definition: secmem.h:211
void copy_mem(T *out, const T *in, size_t n)
Definition: mem_ops.h:22
BER_Object get_next_object()
Definition: ber_dec.cpp:196
size_t size() const
Definition: secmem.h:29

◆ decode() [9/9]

BER_Decoder & Botan::BER_Decoder::decode ( class ASN1_Object obj)

Definition at line 323 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

324  {
325  obj.decode_from(*this);
326  return (*this);
327  }

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

References Botan::PEM_Code::decode().

Referenced by Botan::EC_Group::EC_Group(), and Botan::EC_PrivateKey::EC_PrivateKey().

64  {
65  T actual;
66  decode(actual);
67 
68  if(actual != expected)
69  throw Decoding_Error(error_msg);
70 
71  return (*this);
72  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344

◆ decode_list()

template<typename T >
BER_Decoder & Botan::BER_Decoder::decode_list ( std::vector< T > &  out,
bool  clear_out = true 
)

Definition at line 125 of file ber_dec.h.

References Botan::PEM_Code::decode().

Referenced by Botan::Cert_Extension::Issuer_Alternative_Name::Issuer_Alternative_Name().

126  {
127  if(clear_it)
128  vec.clear();
129 
130  while(more_items())
131  {
132  T value;
133  decode(value);
134  vec.push_back(value);
135  }
136  return (*this);
137  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
bool more_items() const
Definition: ber_dec.cpp:153

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 332 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::NULL_TAG, Botan::MemoryRegion< T >::size(), Botan::UNIVERSAL, and Botan::BER_Object::value.

333  {
334  BER_Object obj = get_next_object();
335  obj.assert_is_a(NULL_TAG, UNIVERSAL);
336  if(obj.value.size())
337  throw BER_Decoding_Error("NULL object had nonzero size");
338  return (*this);
339  }
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ decode_octet_string_bigint()

BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( class BigInt out)

Definition at line 365 of file ber_dec.cpp.

References Botan::PEM_Code::decode(), Botan::BigInt::decode(), Botan::OCTET_STRING, and Botan::MemoryRegion< T >::size().

Referenced by Botan::EC_Group::EC_Group().

366  {
367  SecureVector<byte> out_vec;
368  decode(out_vec, OCTET_STRING);
369  out = BigInt::decode(&out_vec[0], out_vec.size());
370  return (*this);
371  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102

◆ decode_optional()

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

Definition at line 95 of file ber_dec.h.

References Botan::BER_Object::class_tag, Botan::CONSTRUCTED, decode(), Botan::PEM_Code::decode(), Botan::BER_Object::type_tag, Botan::BER_Object::value, and verify_end().

Referenced by Botan::Extensions::decode_from(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_CRL::X509_CRL().

99  {
100  BER_Object obj = get_next_object();
101 
102  if(obj.type_tag == type_tag && obj.class_tag == class_tag)
103  {
104  if(class_tag & CONSTRUCTED)
105  BER_Decoder(obj.value).decode(out).verify_end();
106  else
107  {
108  push_back(obj);
109  decode(out, type_tag, class_tag);
110  }
111  }
112  else
113  {
114  out = default_value;
115  push_back(obj);
116  }
117 
118  return (*this);
119  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:264
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
void push_back(const BER_Object &)
Definition: ber_dec.cpp:228
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ decode_optional_string()

BER_Decoder & Botan::BER_Decoder::decode_optional_string ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type,
u16bit  type_no 
)

Definition at line 480 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::MemoryRegion< T >::clear(), Botan::CONTEXT_SPECIFIC, Botan::PEM_Code::decode(), and Botan::BER_Object::type_tag.

Referenced by Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), and Botan::X509_Certificate::X509_Certificate().

483  {
484  BER_Object obj = get_next_object();
485 
486  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
487 
488  out.clear();
489  push_back(obj);
490 
491  if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC)
492  decode(out, real_type, type_tag, CONTEXT_SPECIFIC);
493 
494  return (*this);
495  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:344
void push_back(const BER_Object &)
Definition: ber_dec.cpp:228
ASN1_Tag
Definition: asn1_int.h:19
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ discard_remaining()

BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Definition at line 185 of file ber_dec.cpp.

Referenced by Botan::DL_Group::BER_decode().

186  {
187  byte buf;
188  while(source->read_byte(buf))
189  ;
190  return (*this);
191  }
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

◆ get_next_object()

BER_Object Botan::BER_Decoder::get_next_object ( )

Definition at line 196 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::EOC, Botan::NO_OBJECT, Botan::MemoryRegion< T >::resize(), Botan::BER_Object::type_tag, Botan::UNIVERSAL, and Botan::BER_Object::value.

Referenced by Botan::BER::decode(), Botan::ASN1_String::decode_from(), Botan::OID::decode_from(), Botan::EAC_Time::decode_from(), Botan::X509_Time::decode_from(), Botan::AlternativeName::decode_from(), Botan::ASN1_EAC_String::decode_from(), Botan::EC_Group::EC_Group(), Botan::PKCS10_Request::PKCS10_Request(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_CRL::X509_CRL().

197  {
198  BER_Object next;
199 
200  if(pushed.type_tag != NO_OBJECT)
201  {
202  next = pushed;
203  pushed.class_tag = pushed.type_tag = NO_OBJECT;
204  return next;
205  }
206 
207  decode_tag(source, next.type_tag, next.class_tag);
208  if(next.type_tag == NO_OBJECT)
209  return next;
210 
211  const size_t length = decode_length(source);
212  if(!source->check_available(length))
213  throw BER_Decoding_Error("Value truncated");
214 
215  next.value.resize(length);
216  if(source->read(&next.value[0], length) != length)
217  throw BER_Decoding_Error("Value truncated");
218 
219  if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
220  return get_next_object();
221 
222  return next;
223  }
virtual bool check_available(size_t n)=0
virtual size_t read(byte out[], size_t length)=0
BER_Object get_next_object()
Definition: ber_dec.cpp:196
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

◆ push_back()

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

Definition at line 228 of file ber_dec.cpp.

References Botan::NO_OBJECT.

229  {
230  if(pushed.type_tag != NO_OBJECT)
231  throw Invalid_State("BER_Decoder: Only one push back is allowed");
232  pushed = obj;
233  }
ASN1_Tag type_tag
Definition: asn1_int.h:82

◆ raw_bytes()

BER_Decoder & Botan::BER_Decoder::raw_bytes ( MemoryRegion< byte > &  out)

◆ start_cons()

BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)

Definition at line 238 of file ber_dec.cpp.

References Botan::BER_Object::assert_is_a(), Botan::CONSTRUCTED, Botan::MemoryRegion< T >::size(), and Botan::BER_Object::value.

Referenced by Botan::DL_Group::BER_decode(), Botan::CMS_Encoder::can_compress_with(), Botan::PK_Verifier::check_signature(), Botan::CRL_Entry::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::Extensions::decode_from(), Botan::AlternativeName::decode_from(), Botan::EAC1_1_gen_CVC< EAC1_1_Req >::decode_info(), Botan::EAC1_1_ADO::EAC1_1_ADO(), Botan::EC_Group::EC_Group(), Botan::EC_PrivateKey::EC_PrivateKey(), Botan::ECDSA_Signature::ECDSA_Signature(), Botan::EAC1_1_ADO::get_car(), Botan::EAC1_1_CVC::get_chat_value(), Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey(), Botan::Cert_Extension::Issuer_Alternative_Name::Issuer_Alternative_Name(), Botan::CMS_Decoder::layer_info(), Botan::X509::load_key(), Botan::EAC1_1_Req::operator==(), Botan::DER_Encoder::start_explicit(), Botan::Cert_Extension::Subject_Key_ID::Subject_Key_ID(), Botan::X509_Certificate::X509_Certificate(), and Botan::X509_Object::X509_Object().

240  {
241  BER_Object obj = get_next_object();
242  obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
243 
244  BER_Decoder result(&obj.value[0], obj.value.size());
245  result.parent = this;
246  return result;
247  }
BER_Decoder(DataSource &)
Definition: ber_dec.cpp:264
ASN1_Tag
Definition: asn1_int.h:19
BER_Object get_next_object()
Definition: ber_dec.cpp:196

◆ verify_end()

BER_Decoder & Botan::BER_Decoder::verify_end ( )

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