File: ber_dec.cpp

package info (click to toggle)
monotone 0.31-6
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 20,680 kB
  • ctags: 14,801
  • sloc: cpp: 87,711; ansic: 64,862; sh: 5,691; lisp: 954; perl: 783; makefile: 509; python: 265; sql: 98; sed: 16
file content (271 lines) | stat: -rw-r--r-- 7,395 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*************************************************
* BER Decoder Source File                        *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/asn1.h>
#include <botan/bit_ops.h>

namespace Botan {

namespace {

/*************************************************
* BER decode an ASN.1 type tag                   *
*************************************************/
u32bit decode_tag(DataSource* ber, ASN1_Tag& type_tag, ASN1_Tag& class_tag)
   {
   byte b;
   if(!ber->read_byte(b))
      {
      class_tag = type_tag = NO_OBJECT;
      return 0;
      }

   if((b & 0x1F) != 0x1F)
      {
      type_tag = ASN1_Tag(b & 0x1F);
      class_tag = ASN1_Tag(b & 0xE0);
      return 1;
      }

   u32bit tag_bytes = 1;
   class_tag = ASN1_Tag(b & 0xE0);

   u32bit tag_buf = 0;
   while(true)
      {
      if(!ber->read_byte(b))
         throw Decoding_Error("BER long-form tag truncated");
      if(tag_buf & 0xFF000000)
         throw Decoding_Error("BER long-form tag overflow");
      tag_bytes++;
      tag_buf = (tag_buf << 7) | (b & 0x7F);
      if((b & 0x80) == 0) break;
      }
   type_tag = ASN1_Tag(tag_buf);
   return tag_bytes;
   }

/*************************************************
* Find the EOC marker                            *
*************************************************/
u32bit find_eoc(DataSource*);

/*************************************************
* BER decode an ASN.1 length field               *
*************************************************/
u32bit decode_length(DataSource* ber, u32bit& field_size)
   {
   byte b;
   if(!ber->read_byte(b))
      throw BER_Decoding_Error("Length field not found");
   field_size = 1;
   if((b & 0x80) == 0)
      return b;

   field_size += (b & 0x7F);
   if(field_size == 1) return find_eoc(ber);
   if(field_size > 5)
      throw BER_Decoding_Error("Length field is too large");

   u32bit length = 0;

   for(u32bit j = 0; j != field_size - 1; j++)
      {
      if(get_byte(0, length) != 0)
         throw BER_Decoding_Error("Field length overflow");
      if(!ber->read_byte(b))
         throw BER_Decoding_Error("Corrupted length field");
      length = (length << 8) | b;
      }
   return length;
   }

/*************************************************
* BER decode an ASN.1 length field               *
*************************************************/
u32bit decode_length(DataSource* ber)
   {
   u32bit dummy;
   return decode_length(ber, dummy);
   }

/*************************************************
* Find the EOC marker                            *
*************************************************/
u32bit find_eoc(DataSource* ber)
   {
   SecureVector<byte> data;

   while(true)
      {
      SecureVector<byte> buffer(DEFAULT_BUFFERSIZE);

      const u32bit got = ber->peek(buffer, buffer.size(), data.size());
      if(got == 0)
         break;
      data.append(buffer, got);
      }

   DataSource_Memory source(data);
   data.destroy();

   u32bit length = 0;
   while(true)
      {
      ASN1_Tag type_tag, class_tag;
      u32bit tag_size = decode_tag(&source, type_tag, class_tag);
      if(type_tag == NO_OBJECT)
         break;

      u32bit length_size = 0;
      u32bit item_size = decode_length(&source, length_size);
      source.discard_next(item_size);

      length += item_size + length_size + tag_size;

      if(type_tag == EOC)
         break;
      }
   return length;
   }

}

/*************************************************
* Check if more objects are there                *
*************************************************/
bool BER_Decoder::more_items() const
   {
   if(source->end_of_data() && (pushed.type_tag == NO_OBJECT))
      return false;
   return true;
   }

/*************************************************
* Verify that no bytes remain in the source      *
*************************************************/
void BER_Decoder::verify_end() const
   {
   if(!source->end_of_data() || (pushed.type_tag != NO_OBJECT))
      throw Invalid_State("BER_Decoder::verify_end called, but data remains");
   }

/*************************************************
* Return all the bytes remaining in the source   *
*************************************************/
SecureVector<byte> BER_Decoder::get_remaining()
   {
   SecureVector<byte> out;
   byte buf;
   while(source->read_byte(buf))
      out.append(buf);
   return out;
   }

/*************************************************
* Discard all the bytes remaining in the source  *
*************************************************/
void BER_Decoder::discard_remaining()
   {
   byte buf;
   while(source->read_byte(buf))
      ;
   }

/*************************************************
* Return the BER encoding of the next object     *
*************************************************/
BER_Object BER_Decoder::get_next_object()
   {
   BER_Object next;

   if(pushed.type_tag != NO_OBJECT)
      {
      next = pushed;
      pushed.class_tag = pushed.type_tag = NO_OBJECT;
      return next;
      }

   decode_tag(source, next.type_tag, next.class_tag);
   if(next.type_tag == NO_OBJECT)
      return next;

   u32bit length = decode_length(source);
   next.value.create(length);
   if(source->read(next.value, length) != length)
      throw BER_Decoding_Error("Value truncated");

   if(next.type_tag == EOC && next.class_tag == UNIVERSAL)
      return get_next_object();

   return next;
   }

/*************************************************
* Push a object back into the stream             *
*************************************************/
void BER_Decoder::push_back(const BER_Object& obj)
   {
   if(pushed.type_tag != NO_OBJECT)
      throw Invalid_State("BER_Decoder: Only one push back is allowed");
   pushed = obj;
   }

/*************************************************
* BER_Decoder Constructor                        *
*************************************************/
BER_Decoder::BER_Decoder(DataSource& src)
   {
   source = &src;
   owns = false;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Constructor                        *
 *************************************************/
BER_Decoder::BER_Decoder(const byte data[], u32bit length)
   {
   source = new DataSource_Memory(data, length);
   owns = true;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Constructor                        *
*************************************************/
BER_Decoder::BER_Decoder(const MemoryRegion<byte>& data)
   {
   source = new DataSource_Memory(data);
   owns = true;
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Copy Constructor                   *
*************************************************/
BER_Decoder::BER_Decoder(const BER_Decoder& other)
   {
   source = other.source;
   owns = false;
   if(other.owns)
      {
      other.owns = false;
      owns = true;
      }
   pushed.type_tag = pushed.class_tag = NO_OBJECT;
   }

/*************************************************
* BER_Decoder Destructor                         *
*************************************************/
BER_Decoder::~BER_Decoder()
   {
   if(owns)
      delete source;
   source = 0;
   }

}