00001
00002
00003
00004
00005
00006
00007
00008 #include <botan/ber_dec.h>
00009 #include <botan/bigint.h>
00010 #include <botan/get_byte.h>
00011
00012 namespace Botan {
00013
00014 namespace {
00015
00016
00017
00018
00019 u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag)
00020 {
00021 byte b;
00022 if(!ber->read_byte(b))
00023 {
00024 class_tag = type_tag = NO_OBJECT;
00025 return 0;
00026 }
00027
00028 if((b & 0x1F) != 0x1F)
00029 {
00030 type_tag = ASN1_Tag(b & 0x1F);
00031 class_tag = ASN1_Tag(b & 0xE0);
00032 return 1;
00033 }
00034
00035 u32bit tag_bytes = 1;
00036 class_tag = ASN1_Tag(b & 0xE0);
00037
00038 u32bit tag_buf = 0;
00039 while(true)
00040 {
00041 if(!ber->read_byte(b))
00042 throw BER_Decoding_Error("Long-form tag truncated");
00043 if(tag_buf & 0xFF000000)
00044 throw BER_Decoding_Error("Long-form tag overflowed 32 bits");
00045 ++tag_bytes;
00046 tag_buf = (tag_buf << 7) | (b & 0x7F);
00047 if((b & 0x80) == 0) break;
00048 }
00049 type_tag = ASN1_Tag(tag_buf);
00050 return tag_bytes;
00051 }
00052
00053
00054
00055
00056 u32bit find_eoc(DataSource*);
00057
00058
00059
00060
00061 u32bit decode_length(DataSource* ber, u32bit& field_size)
00062 {
00063 byte b;
00064 if(!ber->read_byte(b))
00065 throw BER_Decoding_Error("Length field not found");
00066 field_size = 1;
00067 if((b & 0x80) == 0)
00068 return b;
00069
00070 field_size += (b & 0x7F);
00071 if(field_size == 1) return find_eoc(ber);
00072 if(field_size > 5)
00073 throw BER_Decoding_Error("Length field is too large");
00074
00075 u32bit length = 0;
00076
00077 for(u32bit j = 0; j != field_size - 1; ++j)
00078 {
00079 if(get_byte(0, length) != 0)
00080 throw BER_Decoding_Error("Field length overflow");
00081 if(!ber->read_byte(b))
00082 throw BER_Decoding_Error("Corrupted length field");
00083 length = (length << 8) | b;
00084 }
00085 return length;
00086 }
00087
00088
00089
00090
00091 u32bit decode_length(DataSource* ber)
00092 {
00093 u32bit dummy;
00094 return decode_length(ber, dummy);
00095 }
00096
00097
00098
00099
00100 u32bit find_eoc(DataSource* ber)
00101 {
00102 SecureVector<byte> buffer(DEFAULT_BUFFERSIZE), data;
00103
00104 while(true)
00105 {
00106 const u32bit got = ber->peek(buffer, buffer.size(), data.size());
00107 if(got == 0)
00108 break;
00109 data.append(buffer, got);
00110 }
00111
00112 DataSource_Memory source(data);
00113 data.destroy();
00114
00115 u32bit length = 0;
00116 while(true)
00117 {
00118 ASN1_Tag type_tag, class_tag;
00119 u32bit tag_size = decode_tag(&source, type_tag, class_tag);
00120 if(type_tag == NO_OBJECT)
00121 break;
00122
00123 u32bit length_size = 0;
00124 u32bit item_size = decode_length(&source, length_size);
00125 source.discard_next(item_size);
00126
00127 length += item_size + length_size + tag_size;
00128
00129 if(type_tag == EOC)
00130 break;
00131 }
00132 return length;
00133 }
00134
00135 }
00136
00137
00138
00139
00140 void BER_Object::assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag)
00141 {
00142 if(this->type_tag != type_tag || this->class_tag != class_tag)
00143 throw BER_Decoding_Error("Tag mismatch when decoding");
00144 }
00145
00146
00147
00148
00149 bool BER_Decoder::more_items() const
00150 {
00151 if(source->end_of_data() && (pushed.type_tag == NO_OBJECT))
00152 return false;
00153 return true;
00154 }
00155
00156
00157
00158
00159 BER_Decoder& BER_Decoder::verify_end()
00160 {
00161 if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT))
00162 throw Invalid_State("BER_Decoder::verify_end called, but data remains");
00163 return (*this);
00164 }
00165
00166
00167
00168
00169 BER_Decoder& BER_Decoder::raw_bytes(MemoryRegion<byte>& out)
00170 {
00171 out.destroy();
00172 byte buf;
00173 while(source->read_byte(buf))
00174 out.append(buf);
00175 return (*this);
00176 }
00177
00178
00179
00180
00181 BER_Decoder& BER_Decoder::discard_remaining()
00182 {
00183 byte buf;
00184 while(source->read_byte(buf))
00185 ;
00186 return (*this);
00187 }
00188
00189
00190
00191
00192 BER_Object BER_Decoder::get_next_object()
00193 {
00194 BER_Object next;
00195
00196 if(pushed.type_tag != NO_OBJECT)
00197 {
00198 next = pushed;
00199 pushed.class_tag = pushed.type_tag = NO_OBJECT;
00200 return next;
00201 }
00202
00203 decode_tag(source, next.type_tag, next.class_tag);
00204 if(next.type_tag == NO_OBJECT)
00205 return next;
00206
00207 u32bit length = decode_length(source);
00208 next.value.resize(length);
00209 if(source->read(next.value, length) != length)
00210 throw BER_Decoding_Error("Value truncated");
00211
00212 if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
00213 return get_next_object();
00214
00215 return next;
00216 }
00217
00218
00219
00220
00221 void BER_Decoder::push_back(const BER_Object& obj)
00222 {
00223 if(pushed.type_tag != NO_OBJECT)
00224 throw Invalid_State("BER_Decoder: Only one push back is allowed");
00225 pushed = obj;
00226 }
00227
00228
00229
00230
00231 BER_Decoder BER_Decoder::start_cons(ASN1_Tag type_tag,
00232 ASN1_Tag class_tag)
00233 {
00234 BER_Object obj = get_next_object();
00235 obj.assert_is_a(type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
00236
00237 BER_Decoder result(obj.value, obj.value.size());
00238 result.parent = this;
00239 return result;
00240 }
00241
00242
00243
00244
00245 BER_Decoder& BER_Decoder::end_cons()
00246 {
00247 if(!parent)
00248 throw Invalid_State("BER_Decoder::end_cons called with NULL parent");
00249 if(!source->end_of_data())
00250 throw Decoding_Error("BER_Decoder::end_cons called with data left");
00251 return (*parent);
00252 }
00253
00254
00255
00256
00257 BER_Decoder::BER_Decoder(DataSource& src)
00258 {
00259 source = &src;
00260 owns = false;
00261 pushed.type_tag = pushed.class_tag = NO_OBJECT;
00262 parent = 0;
00263 }
00264
00265
00266
00267
00268 BER_Decoder::BER_Decoder(const byte data[], u32bit length)
00269 {
00270 source = new DataSource_Memory(data, length);
00271 owns = true;
00272 pushed.type_tag = pushed.class_tag = NO_OBJECT;
00273 parent = 0;
00274 }
00275
00276
00277
00278
00279 BER_Decoder::BER_Decoder(const MemoryRegion<byte>& data)
00280 {
00281 source = new DataSource_Memory(data);
00282 owns = true;
00283 pushed.type_tag = pushed.class_tag = NO_OBJECT;
00284 parent = 0;
00285 }
00286
00287
00288
00289
00290 BER_Decoder::BER_Decoder(const BER_Decoder& other)
00291 {
00292 source = other.source;
00293 owns = false;
00294 if(other.owns)
00295 {
00296 other.owns = false;
00297 owns = true;
00298 }
00299 pushed.type_tag = pushed.class_tag = NO_OBJECT;
00300 parent = other.parent;
00301 }
00302
00303
00304
00305
00306 BER_Decoder::~BER_Decoder()
00307 {
00308 if(owns)
00309 delete source;
00310 source = 0;
00311 }
00312
00313
00314
00315
00316 BER_Decoder& BER_Decoder::decode(ASN1_Object& obj)
00317 {
00318 obj.decode_from(*this);
00319 return (*this);
00320 }
00321
00322
00323
00324
00325 BER_Decoder& BER_Decoder::decode_null()
00326 {
00327 BER_Object obj = get_next_object();
00328 obj.assert_is_a(NULL_TAG, UNIVERSAL);
00329 if(obj.value.size())
00330 throw BER_Decoding_Error("NULL object had nonzero size");
00331 return (*this);
00332 }
00333
00334
00335
00336
00337 BER_Decoder& BER_Decoder::decode(bool& out)
00338 {
00339 return decode(out, BOOLEAN, UNIVERSAL);
00340 }
00341
00342
00343
00344
00345 BER_Decoder& BER_Decoder::decode(u32bit& out)
00346 {
00347 return decode(out, INTEGER, UNIVERSAL);
00348 }
00349
00350
00351
00352
00353 BER_Decoder& BER_Decoder::decode(BigInt& out)
00354 {
00355 return decode(out, INTEGER, UNIVERSAL);
00356 }
00357
00358 BER_Decoder& BER_Decoder::decode_octet_string_bigint(BigInt& out)
00359 {
00360 SecureVector<byte> out_vec;
00361 decode(out_vec, OCTET_STRING);
00362 out = BigInt::decode(&out_vec[0], out_vec.size());
00363 return (*this);
00364 }
00365
00366
00367
00368
00369 BER_Decoder& BER_Decoder::decode(bool& out,
00370 ASN1_Tag type_tag, ASN1_Tag class_tag)
00371 {
00372 BER_Object obj = get_next_object();
00373 obj.assert_is_a(type_tag, class_tag);
00374
00375 if(obj.value.size() != 1)
00376 throw BER_Decoding_Error("BER boolean value had invalid size");
00377
00378 out = (obj.value[0]) ? true : false;
00379 return (*this);
00380 }
00381
00382
00383
00384
00385 BER_Decoder& BER_Decoder::decode(u32bit& out,
00386 ASN1_Tag type_tag, ASN1_Tag class_tag)
00387 {
00388 BigInt integer;
00389 decode(integer, type_tag, class_tag);
00390 out = integer.to_u32bit();
00391 return (*this);
00392 }
00393
00394
00395
00396
00397 BER_Decoder& BER_Decoder::decode(BigInt& out,
00398 ASN1_Tag type_tag, ASN1_Tag class_tag)
00399 {
00400 BER_Object obj = get_next_object();
00401 obj.assert_is_a(type_tag, class_tag);
00402
00403 if(obj.value.empty())
00404 out = 0;
00405 else
00406 {
00407 const bool negative = (obj.value[0] & 0x80) ? true : false;
00408
00409 if(negative)
00410 {
00411 for(u32bit j = obj.value.size(); j > 0; --j)
00412 if(obj.value[j-1]--)
00413 break;
00414 for(u32bit j = 0; j != obj.value.size(); ++j)
00415 obj.value[j] = ~obj.value[j];
00416 }
00417
00418 out = BigInt(obj.value, obj.value.size());
00419
00420 if(negative)
00421 out.flip_sign();
00422 }
00423
00424 return (*this);
00425 }
00426
00427
00428
00429
00430 BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& out, ASN1_Tag real_type)
00431 {
00432 return decode(out, real_type, real_type, UNIVERSAL);
00433 }
00434
00435
00436
00437
00438 BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& buffer,
00439 ASN1_Tag real_type,
00440 ASN1_Tag type_tag, ASN1_Tag class_tag)
00441 {
00442 if(real_type != OCTET_STRING && real_type != BIT_STRING)
00443 throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
00444
00445 BER_Object obj = get_next_object();
00446 obj.assert_is_a(type_tag, class_tag);
00447
00448 if(real_type == OCTET_STRING)
00449 buffer = obj.value;
00450 else
00451 {
00452 if(obj.value[0] >= 8)
00453 throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
00454 buffer.set(obj.value + 1, obj.value.size() - 1);
00455 }
00456 return (*this);
00457 }
00458
00459
00460
00461
00462 BER_Decoder& BER_Decoder::decode_optional_string(MemoryRegion<byte>& out,
00463 ASN1_Tag real_type,
00464 u16bit type_no)
00465 {
00466 BER_Object obj = get_next_object();
00467
00468 ASN1_Tag type_tag = static_cast<ASN1_Tag>(type_no);
00469
00470 out.clear();
00471 push_back(obj);
00472
00473 if(obj.type_tag == type_tag && obj.class_tag == CONTEXT_SPECIFIC)
00474 decode(out, real_type, type_tag, CONTEXT_SPECIFIC);
00475
00476 return (*this);
00477 }
00478
00479 }