Botan 1.10.17
Botan::BER_Decoder Class Reference

#include <ber_dec.h>

Public Member Functions

 BER_Decoder (const BER_Decoder &)
 BER_Decoder (const byte[], size_t)
 BER_Decoder (const MemoryRegion< byte > &)
 BER_Decoder (DataSource &)
BER_Decoderdecode (bool &)
BER_Decoderdecode (bool &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (class ASN1_Object &)
BER_Decoderdecode (class BigInt &)
BER_Decoderdecode (class BigInt &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag)
BER_Decoderdecode (MemoryRegion< byte > &, ASN1_Tag, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
BER_Decoderdecode (size_t &)
BER_Decoderdecode (size_t &, ASN1_Tag, ASN1_Tag=CONTEXT_SPECIFIC)
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]

◆ BER_Decoder() [2/4]

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

Definition at line 275 of file ber_dec.cpp.

276 {
277 source = new DataSource_Memory(data, length);
278 owns = true;
279 pushed.type_tag = pushed.class_tag = NO_OBJECT;
280 parent = 0;
281 }
DataSource_Memory(const std::string &in)
Definition data_src.cpp:98

References Botan::DataSource_Memory::DataSource_Memory(), and Botan::NO_OBJECT.

◆ BER_Decoder() [3/4]

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

Definition at line 286 of file ber_dec.cpp.

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

References Botan::DataSource_Memory::DataSource_Memory(), and Botan::NO_OBJECT.

◆ BER_Decoder() [4/4]

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

Definition at line 297 of file ber_dec.cpp.

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 }

References BER_Decoder(), and Botan::NO_OBJECT.

◆ ~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]

◆ decode() [2/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.

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
BER_Decoding_Error(const std::string &)
Definition asn1_int.cpp:19

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

◆ decode() [3/9]

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

Definition at line 323 of file ber_dec.cpp.

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

References BER_Decoder(), and Botan::ASN1_Object::decode_from().

◆ decode() [4/9]

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

Definition at line 360 of file ber_dec.cpp.

361 {
362 return decode(out, INTEGER, UNIVERSAL);
363 }
@ INTEGER
Definition asn1_int.h:29

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

◆ decode() [5/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.

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 }

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

◆ decode() [6/9]

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

Definition at line 444 of file ber_dec.cpp.

445 {
446 return decode(out, real_type, real_type, UNIVERSAL);
447 }

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

◆ decode() [7/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.

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 copy_mem(T *out, const T *in, size_t n)
Definition mem_ops.h:22
@ BIT_STRING
Definition asn1_int.h:30
@ OCTET_STRING
Definition asn1_int.h:31
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition asn1_int.cpp:22

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

◆ decode() [8/9]

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

Definition at line 352 of file ber_dec.cpp.

353 {
354 return decode(out, INTEGER, UNIVERSAL);
355 }

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

◆ decode() [9/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.

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 }

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

◆ 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.

64 {
65 T actual;
66 decode(actual);
67
68 if(actual != expected)
69 throw Decoding_Error(error_msg);
70
71 return (*this);
72 }
Decoding_Error(const std::string &name)
Definition exceptn.h:140

References BER_Decoder(), and decode().

◆ 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.

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 }
bool more_items() const
Definition ber_dec.cpp:153

References BER_Decoder(), decode(), and more_items().

◆ decode_null()

BER_Decoder & Botan::BER_Decoder::decode_null ( )

Definition at line 332 of file ber_dec.cpp.

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 }
@ NULL_TAG
Definition asn1_int.h:32

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

◆ 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.

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 }
static BigInt decode(const byte buf[], size_t length, Base base=Binary)
Definition big_code.cpp:102

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

◆ 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.

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 }
void push_back(const BER_Object &)
Definition ber_dec.cpp:228
BER_Decoder(DataSource &)
Definition ber_dec.cpp:264
@ CONSTRUCTED
Definition asn1_int.h:25

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

◆ 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.

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 }
ASN1_Tag
Definition asn1_int.h:19
@ CONTEXT_SPECIFIC
Definition asn1_int.h:22

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

◆ discard_remaining()

BER_Decoder & Botan::BER_Decoder::discard_remaining ( )

Definition at line 185 of file ber_dec.cpp.

186 {
187 byte buf;
188 while(source->read_byte(buf))
189 ;
190 return (*this);
191 }

References BER_Decoder().

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

◆ end_cons()

BER_Decoder & Botan::BER_Decoder::end_cons ( )

Definition at line 252 of file ber_dec.cpp.

253 {
254 if(!parent)
255 throw Invalid_State("BER_Decoder::end_cons called with NULL parent");
256 if(!source->end_of_data())
257 throw Decoding_Error("BER_Decoder::end_cons called with data left");
258 return (*parent);
259 }
Invalid_State(const std::string &err)
Definition exceptn.h:27

References BER_Decoder(), Botan::Decoding_Error::Decoding_Error(), and Botan::Invalid_State::Invalid_State().

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), and Botan::X509_DN::decode_from().

◆ get_next_object()

BER_Object Botan::BER_Decoder::get_next_object ( )

Definition at line 196 of file ber_dec.cpp.

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 }
@ EOC
Definition asn1_int.h:27

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

Referenced by Botan::BER::decode(), decode(), decode(), decode(), Botan::AlternativeName::decode_from(), Botan::ASN1_EAC_String::decode_from(), Botan::ASN1_String::decode_from(), Botan::EAC_Time::decode_from(), Botan::OID::decode_from(), Botan::X509_Time::decode_from(), decode_null(), decode_optional(), decode_optional_string(), Botan::EC_Group::EC_Group(), get_next_object(), and start_cons().

◆ more_items()

bool Botan::BER_Decoder::more_items ( ) const

Definition at line 153 of file ber_dec.cpp.

154 {
155 if(source->end_of_data() && (pushed.type_tag == NO_OBJECT))
156 return false;
157 return true;
158 }

References Botan::NO_OBJECT.

Referenced by Botan::PK_Verifier::check_signature(), Botan::AlternativeName::decode_from(), Botan::CRL_Entry::decode_from(), Botan::Extensions::decode_from(), Botan::X509_DN::decode_from(), and decode_list().

◆ push_back()

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

Definition at line 228 of file ber_dec.cpp.

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

References Botan::Invalid_State::Invalid_State(), and Botan::NO_OBJECT.

Referenced by decode_optional(), and decode_optional_string().

◆ raw_bytes()

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

Definition at line 173 of file ber_dec.cpp.

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

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

Referenced by Botan::AlgorithmIdentifier::decode_from(), Botan::Attribute::decode_from(), and Botan::X509_DN::decode_from().

◆ start_cons()

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

◆ verify_end()

BER_Decoder & Botan::BER_Decoder::verify_end ( )

Definition at line 163 of file ber_dec.cpp.

164 {
165 if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT))
166 throw Invalid_State("BER_Decoder::verify_end called, but data remains");
167 return (*this);
168 }

References BER_Decoder(), Botan::Invalid_State::Invalid_State(), and Botan::NO_OBJECT.

Referenced by Botan::DL_Group::BER_decode(), Botan::AlternativeName::decode_from(), Botan::Extensions::decode_from(), and Botan::X509_DN::decode_from().


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