Botan  1.10.9
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

Botan::BER_Decoder::BER_Decoder ( DataSource src)

Definition at line 258 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

Referenced by decode_optional().

259  {
260  source = &src;
261  owns = false;
262  pushed.type_tag = pushed.class_tag = NO_OBJECT;
263  parent = 0;
264  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const byte  data[],
size_t  length 
)

Definition at line 269 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

270  {
271  source = new DataSource_Memory(data, length);
272  owns = true;
273  pushed.type_tag = pushed.class_tag = NO_OBJECT;
274  parent = 0;
275  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const MemoryRegion< byte > &  data)

Definition at line 280 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

281  {
282  source = new DataSource_Memory(data);
283  owns = true;
284  pushed.type_tag = pushed.class_tag = NO_OBJECT;
285  parent = 0;
286  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::BER_Decoder ( const BER_Decoder other)

Definition at line 291 of file ber_dec.cpp.

References Botan::BER_Object::class_tag, Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

292  {
293  source = other.source;
294  owns = false;
295  if(other.owns)
296  {
297  other.owns = false;
298  owns = true;
299  }
300  pushed.type_tag = pushed.class_tag = NO_OBJECT;
301  parent = other.parent;
302  }
ASN1_Tag class_tag
Definition: asn1_int.h:82
ASN1_Tag type_tag
Definition: asn1_int.h:82
Botan::BER_Decoder::~BER_Decoder ( )

Definition at line 307 of file ber_dec.cpp.

308  {
309  if(owns)
310  delete source;
311  source = 0;
312  }

Member Function Documentation

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

Definition at line 346 of file ber_dec.cpp.

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

347  {
348  return decode(out, INTEGER, UNIVERSAL);
349  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt out)

Definition at line 354 of file ber_dec.cpp.

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

355  {
356  return decode(out, INTEGER, UNIVERSAL);
357  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & Botan::BER_Decoder::decode ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type 
)

Definition at line 438 of file ber_dec.cpp.

References decode(), and Botan::UNIVERSAL.

439  {
440  return decode(out, real_type, real_type, UNIVERSAL);
441  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & Botan::BER_Decoder::decode ( bool &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 370 of file ber_dec.cpp.

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

372  {
373  BER_Object obj = get_next_object();
374  obj.assert_is_a(type_tag, class_tag);
375 
376  if(obj.value.size() != 1)
377  throw BER_Decoding_Error("BER boolean value had invalid size");
378 
379  out = (obj.value[0]) ? true : false;
380  return (*this);
381  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode ( size_t &  out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 386 of file ber_dec.cpp.

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

388  {
389  BigInt integer;
390  decode(integer, type_tag, class_tag);
391 
392  if(integer.bits() > 32)
393  throw BER_Decoding_Error("Decoded integer value larger than expected");
394 
395  out = 0;
396  for(size_t i = 0; i != 4; ++i)
397  out = (out << 8) | integer.byte_at(3-i);
398 
399  return (*this);
400  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
BER_Decoder & Botan::BER_Decoder::decode ( class BigInt out,
ASN1_Tag  type_tag,
ASN1_Tag  class_tag = CONTEXT_SPECIFIC 
)

Definition at line 405 of file ber_dec.cpp.

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

407  {
408  BER_Object obj = get_next_object();
409  obj.assert_is_a(type_tag, class_tag);
410 
411  if(obj.value.empty())
412  out = 0;
413  else
414  {
415  const bool negative = (obj.value[0] & 0x80) ? true : false;
416 
417  if(negative)
418  {
419  for(size_t i = obj.value.size(); i > 0; --i)
420  if(obj.value[i-1]--)
421  break;
422  for(size_t i = 0; i != obj.value.size(); ++i)
423  obj.value[i] = ~obj.value[i];
424  }
425 
426  out = BigInt(&obj.value[0], obj.value.size());
427 
428  if(negative)
429  out.flip_sign();
430  }
431 
432  return (*this);
433  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
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 446 of file ber_dec.cpp.

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

449  {
450  if(real_type != OCTET_STRING && real_type != BIT_STRING)
451  throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
452 
453  BER_Object obj = get_next_object();
454  obj.assert_is_a(type_tag, class_tag);
455 
456  if(real_type == OCTET_STRING)
457  buffer = obj.value;
458  else
459  {
460  if(obj.value[0] >= 8)
461  throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
462 
463  buffer.resize(obj.value.size() - 1);
464  copy_mem(&buffer[0], &obj.value[1], obj.value.size() - 1);
465  }
466  return (*this);
467  }
void resize(size_t n)
Definition: secmem.h:211
size_t size() const
Definition: secmem.h:29
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:193
BER_Decoder & Botan::BER_Decoder::decode ( class ASN1_Object obj)

Definition at line 317 of file ber_dec.cpp.

References Botan::ASN1_Object::decode_from().

318  {
319  obj.decode_from(*this);
320  return (*this);
321  }
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(), Botan::EC_PrivateKey::EC_PrivateKey(), and Botan::IF_Scheme_PrivateKey::IF_Scheme_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:338
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 decode(), and more_items().

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:338
bool more_items() const
Definition: ber_dec.cpp:150
BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 326 of file ber_dec.cpp.

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

327  {
328  BER_Object obj = get_next_object();
329  obj.assert_is_a(NULL_TAG, UNIVERSAL);
330  if(obj.value.size())
331  throw BER_Decoding_Error("NULL object had nonzero size");
332  return (*this);
333  }
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode_octet_string_bigint ( class BigInt out)

Definition at line 359 of file ber_dec.cpp.

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

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

360  {
361  SecureVector<byte> out_vec;
362  decode(out_vec, OCTET_STRING);
363  out = BigInt::decode(&out_vec[0], out_vec.size());
364  return (*this);
365  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition: big_code.cpp:102
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 BER_Decoder(), Botan::BER_Object::class_tag, Botan::CONSTRUCTED, decode(), get_next_object(), push_back(), Botan::BER_Object::type_tag, and Botan::BER_Object::value.

Referenced by Botan::Extensions::decode_from().

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:258
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
void push_back(const BER_Object &)
Definition: ber_dec.cpp:222
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::decode_optional_string ( MemoryRegion< byte > &  out,
ASN1_Tag  real_type,
u16bit  type_no 
)

Definition at line 472 of file ber_dec.cpp.

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

475  {
476  BER_Object obj = get_next_object();
477 
478  ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
479 
480  out.clear();
481  push_back(obj);
482 
483  if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC)
484  decode(out, real_type, type_tag, CONTEXT_SPECIFIC);
485 
486  return (*this);
487  }
BER_Decoder & decode(bool &)
Definition: ber_dec.cpp:338
void push_back(const BER_Object &)
Definition: ber_dec.cpp:222
ASN1_Tag
Definition: asn1_int.h:19
BER_Object get_next_object()
Definition: ber_dec.cpp:193
BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Definition at line 182 of file ber_dec.cpp.

References Botan::DataSource::read_byte().

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

183  {
184  byte buf;
185  while(source->read_byte(buf))
186  ;
187  return (*this);
188  }
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19
BER_Decoder & Botan::BER_Decoder::end_cons ( )

Definition at line 246 of file ber_dec.cpp.

References Botan::DataSource::end_of_data().

Referenced by Botan::CRL_Entry::decode_from(), Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::Extensions::decode_from(), Botan::EAC1_1_gen_CVC< Derived >::decode_info(), Botan::EC_Group::EC_Group(), Botan::ECDSA_Signature::ECDSA_Signature(), Botan::IF_Scheme_PublicKey::IF_Scheme_PublicKey(), and Botan::X509::load_key().

247  {
248  if(!parent)
249  throw Invalid_State("BER_Decoder::end_cons called with NULL parent");
250  if(!source->end_of_data())
251  throw Decoding_Error("BER_Decoder::end_cons called with data left");
252  return (*parent);
253  }
virtual bool end_of_data() const =0
BER_Object Botan::BER_Decoder::get_next_object ( )

Definition at line 193 of file ber_dec.cpp.

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

Referenced by Botan::BER::decode(), 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(), decode_null(), decode_optional(), decode_optional_string(), Botan::EC_Group::EC_Group(), and start_cons().

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

Definition at line 222 of file ber_dec.cpp.

References Botan::NO_OBJECT, and Botan::BER_Object::type_tag.

Referenced by decode_optional(), and decode_optional_string().

223  {
224  if(pushed.type_tag != NO_OBJECT)
225  throw Invalid_State("BER_Decoder: Only one push back is allowed");
226  pushed = obj;
227  }
ASN1_Tag type_tag
Definition: asn1_int.h:82
BER_Decoder & Botan::BER_Decoder::raw_bytes ( MemoryRegion< byte > &  out)

Definition at line 170 of file ber_dec.cpp.

References Botan::MemoryRegion< T >::clear(), Botan::MemoryRegion< T >::push_back(), and Botan::DataSource::read_byte().

Referenced by Botan::X509_DN::decode_from(), Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), and Botan::EAC1_1_gen_CVC< Derived >::decode_info().

171  {
172  out.clear();
173  byte buf;
174  while(source->read_byte(buf))
175  out.push_back(buf);
176  return (*this);
177  }
void push_back(T x)
Definition: secmem.h:143
unsigned char byte
Definition: types.h:22
size_t read_byte(byte &out)
Definition: data_src.cpp:19
BER_Decoder Botan::BER_Decoder::start_cons ( ASN1_Tag  type_tag,
ASN1_Tag  class_tag = UNIVERSAL 
)
BER_Decoder & Botan::BER_Decoder::verify_end ( )

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