File: der_enc.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 (338 lines) | stat: -rw-r--r-- 10,862 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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
/*************************************************
* DER Encoder Source File                        *
* (C) 1999-2005 The Botan Project                *
*************************************************/

#include <botan/der_enc.h>
#include <botan/bigint.h>
#include <botan/bit_ops.h>
#include <botan/parsing.h>

namespace Botan {

namespace {

/*************************************************
* DER encode an ASN.1 type tag                   *
*************************************************/
SecureVector<byte> encode_tag(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   if((class_tag | 0xE0) != 0xE0)
      throw Encoding_Error("DER_Encoder: Invalid class tag " +
                           to_string(class_tag));

   SecureVector<byte> encoded_tag;
   if(type_tag <= 30)
      encoded_tag.append((byte)(type_tag | class_tag));
   else
      {
      u32bit blocks = high_bit(type_tag) + 6;
      blocks = (blocks - (blocks % 7)) / 7;

      encoded_tag.append(class_tag | 0x1F);
      for(u32bit k = 0; k != blocks - 1; k++)
         encoded_tag.append(0x80 | ((type_tag >> 7*(blocks-k-1)) & 0x7F));
      encoded_tag.append(type_tag & 0x7F);
      }

   return encoded_tag;
   }

/*************************************************
* DER encode an ASN.1 length field               *
*************************************************/
SecureVector<byte> encode_length(u32bit length)
   {
   SecureVector<byte> encoded_length;
   if(length <= 127)
      encoded_length.append((byte)length);
   else
      {
      const u32bit top_byte = significant_bytes(length);
      encoded_length.append((byte)(0x80 | top_byte));
      for(u32bit j = 4-top_byte; j != 4; j++)
         encoded_length.append(get_byte(j, length));
      }
   return encoded_length;
   }

/*************************************************
* A comparison functor for sorting SET objects   *
*************************************************/
class DER_Cmp
   {
   public:
      bool operator()(const MemoryRegion<byte>&,
                      const MemoryRegion<byte>&) const;
   };

/*************************************************
* Compare two encodings, as specified by X.690   *
*************************************************/
bool DER_Cmp::operator()(const MemoryRegion<byte>& a,
                         const MemoryRegion<byte>& b) const
   {
   if(a.size() < b.size()) return true;
   if(a.size() > b.size()) return false;

   for(u32bit j = 0; j != a.size(); j++)
      {
      if(a[j] < b[j]) return true;
      if(a[j] > b[j]) return false;
      }
   return false;
   }

}

/*************************************************
* Return the encoded SEQUENCE/SET                *
*************************************************/
SecureVector<byte> DER_Encoder::DER_Sequence::get_contents()
   {
   const ASN1_Tag real_class_tag = ASN1_Tag(class_tag | CONSTRUCTED);

   SecureVector<byte> encoded_tag = encode_tag(type_tag, real_class_tag);

   if(is_a_set)
      {
      std::sort(set_contents.begin(), set_contents.end(), DER_Cmp());
      for(u32bit j = 0; j != set_contents.size(); j++)
         contents.append(set_contents[j]);
      set_contents.clear();
      }

   SecureVector<byte> encoded_length = encode_length(contents.size());

   SecureVector<byte> retval;
   retval.append(encoded_tag);
   retval.append(encoded_length);
   retval.append(contents);
   contents.destroy();
   return retval;
   }

/*************************************************
* Add an encoded value to the SEQUENCE/SET       *
*************************************************/
void DER_Encoder::DER_Sequence::add_bytes(const byte data[], u32bit length)
   {
   if(is_a_set)
      {
      set_contents.push_back(SecureVector<byte>(data, length));
      }
   else
      contents.append(data, length);
   }

/*************************************************
* Return the type and class taggings             *
*************************************************/
ASN1_Tag DER_Encoder::DER_Sequence::tag_of() const
   {
   return ASN1_Tag(type_tag | class_tag);
   }

/*************************************************
* DER_Sequence Constructor                       *
*************************************************/
DER_Encoder::DER_Sequence::DER_Sequence(ASN1_Tag t1, ASN1_Tag t2, bool b) :
   type_tag(t1), class_tag(t2), is_a_set(b)
   {
   }

/*************************************************
* Return the encoded contents                    *
*************************************************/
SecureVector<byte> DER_Encoder::get_contents()
   {
   if(sequence_level != 0)
      throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");

   SecureVector<byte> retval;
   retval = contents;
   contents.destroy();
   return retval;
   }

/*************************************************
* Start a new ASN.1 SEQUENCE/SET/EXPLICIT        *
*************************************************/
void DER_Encoder::start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag,
                             bool is_a_set)
   {
   sequence_level++;
   subsequences.push_back(DER_Sequence(type_tag, class_tag, is_a_set));
   }

/*************************************************
* Finish the current ASN.1 SEQUENCE/SET/EXPLICIT *
*************************************************/
void DER_Encoder::end_cons(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   if(sequence_level == 0)
      throw Invalid_State("DER_Encoder::end_cons: No such sequence");
   sequence_level--;
   if(subsequences[sequence_level].tag_of() != ASN1_Tag(type_tag | class_tag))
      throw Invalid_Argument("DER_Encoder::end_cons: Tag mismatch");

   SecureVector<byte> seq = subsequences[sequence_level].get_contents();
   subsequences.pop_back();
   add_raw_octets(seq);
   }

/*************************************************
* Start a new ASN.1 SEQUENCE                     *
*************************************************/
void DER_Encoder::start_sequence(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   start_cons(type_tag, class_tag, false);
   }

/*************************************************
* Finish the current ASN.1 SEQUENCE              *
*************************************************/
void DER_Encoder::end_sequence(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   end_cons(type_tag, class_tag);
   }

/*************************************************
* Start a new ASN.1 SET                          *
*************************************************/
void DER_Encoder::start_set(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   start_cons(type_tag, class_tag, true);
   }

/*************************************************
* Finish the current ASN.1 SET                   *
*************************************************/
void DER_Encoder::end_set(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   end_cons(type_tag, class_tag);
   }

/*************************************************
* Start a new ASN.1 SEQUENCE                     *
*************************************************/
void DER_Encoder::start_sequence()
   {
   start_sequence(SEQUENCE, UNIVERSAL);
   }

/*************************************************
* Finish the current ASN.1 SEQUENCE              *
*************************************************/
void DER_Encoder::end_sequence()
   {
   end_sequence(SEQUENCE, UNIVERSAL);
   }

/*************************************************
* Start a new ASN.1 SET                          *
*************************************************/
void DER_Encoder::start_set()
   {
   start_set(SET, UNIVERSAL);
   }

/*************************************************
* Finish the current ASN.1 SET                   *
*************************************************/
void DER_Encoder::end_set()
   {
   end_set(SET, UNIVERSAL);
   }

/*************************************************
* Start a new ASN.1 EXPLICIT encoding            *
*************************************************/
void DER_Encoder::start_explicit(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   start_cons(type_tag, class_tag, false);
   }

/*************************************************
* Finish the current ASN.1 EXPLICIT encoding     *
*************************************************/
void DER_Encoder::end_explicit(ASN1_Tag type_tag, ASN1_Tag class_tag)
   {
   end_cons(type_tag, class_tag);
   }

/*************************************************
* Write raw octets into the stream               *
*************************************************/
void DER_Encoder::add_raw_octets(const MemoryRegion<byte>& octets)
   {
   add_raw_octets(octets.begin(), octets.size());
   }

/*************************************************
* Write raw octets into the stream               *
*************************************************/
void DER_Encoder::add_raw_octets(const byte octets[], u32bit length)
   {
   if(sequence_level == 0)
      contents.append(octets, length);
   else
      subsequences[sequence_level-1].add_bytes(octets, length);
   }

/*************************************************
* Write the encoding of the octet(s)             *
*************************************************/
void DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
                             const byte rep[], u32bit length)
   {
   SecureVector<byte> encoded_tag = encode_tag(type_tag, class_tag);
   SecureVector<byte> encoded_length = encode_length(length);

   SecureVector<byte> buffer;
   buffer.append(encoded_tag);
   buffer.append(encoded_length);
   buffer.append(rep, length);

   add_raw_octets(buffer);
   }

/*************************************************
* Write the encoding of the octet(s)             *
*************************************************/
void DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
                             const MemoryRegion<byte>& rep_buf)
   {
   const byte* rep = rep_buf.begin();
   const u32bit rep_len = rep_buf.size();
   add_object(type_tag, class_tag, rep, rep_len);
   }

/*************************************************
* Write the encoding of the octet(s)             *
*************************************************/
void DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag,
                             const std::string& rep_str)
   {
   const byte* rep = (const byte*)rep_str.c_str();
   const u32bit rep_len = rep_str.size();
   add_object(type_tag, class_tag, rep, rep_len);
   }

/*************************************************
* Write the encoding of the octet                *
*************************************************/
void DER_Encoder::add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, byte rep)
   {
   add_object(type_tag, class_tag, &rep, 1);
   }

/*************************************************
* DER_Encoder Constructor                        *
*************************************************/
DER_Encoder::DER_Encoder()
    {
   sequence_level = 0;
   }

}