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
|
/*************************************************
* BER Decoding Source File *
* (C) 1999-2006 The Botan Project *
*************************************************/
#include <botan/ber_dec.h>
#include <botan/asn1_int.h>
#include <botan/parsing.h>
#include <botan/bigint.h>
#include <assert.h>
namespace Botan {
namespace {
/*************************************************
* Check an object's type and size *
*************************************************/
void check_object(const BER_Object& obj,
ASN1_Tag type_tag, ASN1_Tag class_tag,
u32bit length = 0, bool check_length = false)
{
if(obj.type_tag != type_tag || obj.class_tag != class_tag)
throw BER_Decoding_Error("Tag mismatch when decoding");
if(check_length && obj.value.size() != length)
throw BER_Decoding_Error("Incorrect size for type");
}
}
/*************************************************
* Decode a BER encoded NULL *
*************************************************/
BER_Decoder& BER_Decoder::decode_null()
{
check_object(get_next_object(), NULL_TAG, UNIVERSAL, 0, true);
return (*this);
}
/*************************************************
* Decode a BER encoded BOOLEAN *
*************************************************/
BER_Decoder& BER_Decoder::decode(bool& out)
{
return decode(out, BOOLEAN, UNIVERSAL);
}
/*************************************************
* Decode a small BER encoded INTEGER *
*************************************************/
BER_Decoder& BER_Decoder::decode(u32bit& out)
{
return decode(out, INTEGER, UNIVERSAL);
}
/*************************************************
* Decode a BER encoded INTEGER *
*************************************************/
BER_Decoder& BER_Decoder::decode(BigInt& out)
{
return decode(out, INTEGER, UNIVERSAL);
}
/*************************************************
* Decode a BER encoded BOOLEAN *
*************************************************/
BER_Decoder& BER_Decoder::decode(bool& out,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BER_Object obj = get_next_object();
check_object(obj, type_tag, class_tag, 1, true);
out = (obj.value[0]) ? true : false;
return (*this);
}
/*************************************************
* Decode a small BER encoded INTEGER *
*************************************************/
BER_Decoder& BER_Decoder::decode(u32bit& out,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BigInt integer;
decode(integer, type_tag, class_tag);
out = integer.to_u32bit();
return (*this);
}
/*************************************************
* Decode a BER encoded INTEGER *
*************************************************/
BER_Decoder& BER_Decoder::decode(BigInt& out,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BER_Object obj = get_next_object();
check_object(obj, type_tag, class_tag);
if(obj.value.is_empty())
out = 0;
else
{
const bool negative = (obj.value[0] & 0x80) ? true : false;
if(negative)
{
for(u32bit j = obj.value.size(); j > 0; --j)
if(obj.value[j-1]--)
break;
for(u32bit j = 0; j != obj.value.size(); ++j)
obj.value[j] = ~obj.value[j];
}
out = BigInt(obj.value, obj.value.size());
if(negative)
out.flip_sign();
}
return (*this);
}
/*************************************************
* BER decode a BIT STRING or OCTET STRING *
*************************************************/
BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& out, ASN1_Tag real_type)
{
return decode(out, real_type, real_type, UNIVERSAL);
}
/*************************************************
* BER decode a BIT STRING or OCTET STRING *
*************************************************/
BER_Decoder& BER_Decoder::decode(MemoryRegion<byte>& buffer,
ASN1_Tag real_type,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
if(real_type != OCTET_STRING && real_type != BIT_STRING)
throw BER_Bad_Tag("Bad tag for {BIT,OCTET} STRING", real_type);
BER_Object obj = get_next_object();
check_object(obj, type_tag, class_tag);
if(real_type == OCTET_STRING)
buffer = obj.value;
else
{
if(obj.value[0] >= 8)
throw BER_Decoding_Error("Bad number of unused bits in BIT STRING");
buffer.set(obj.value + 1, obj.value.size() - 1);
}
return (*this);
}
namespace BER {
/*************************************************
* Decode and return a BER encoded SEQUENCE *
*************************************************/
BER_Decoder get_subsequence(BER_Decoder& decoder)
{
return get_subsequence(decoder, SEQUENCE, CONSTRUCTED);
}
/*************************************************
* Decode and return a BER encoded SET *
*************************************************/
BER_Decoder get_subset(BER_Decoder& decoder)
{
return get_subset(decoder, SET, CONSTRUCTED);
}
/*************************************************
* Decode and return a BER encoded SEQUENCE *
*************************************************/
BER_Decoder get_subsequence(BER_Decoder& decoder,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BER_Object obj = decoder.get_next_object();
check_object(obj, type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
return BER_Decoder(obj.value, obj.value.size());
}
/*************************************************
* Decode and return a BER encoded SET *
*************************************************/
BER_Decoder get_subset(BER_Decoder& decoder,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BER_Object obj = decoder.get_next_object();
check_object(obj, type_tag, ASN1_Tag(class_tag | CONSTRUCTED));
return BER_Decoder(obj.value, obj.value.size());
}
/*************************************************
* Convert a BER object into a string object *
*************************************************/
std::string to_string(const BER_Object& obj)
{
std::string str((const char*)obj.value.begin(), obj.value.size());
return str;
}
/*************************************************
* Decode an OPTIONAL string type *
*************************************************/
bool decode_optional_string(BER_Decoder& in, MemoryRegion<byte>& out,
ASN1_Tag real_type,
ASN1_Tag type_tag, ASN1_Tag class_tag)
{
BER_Object obj = in.get_next_object();
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
{
if(class_tag & CONSTRUCTED)
{
BER_Decoder stored_value(obj.value);
stored_value.decode(out, real_type);
stored_value.verify_end();
}
else
{
in.push_back(obj);
in.decode(out, real_type, type_tag, class_tag);
}
return true;
}
else
{
out.clear();
in.push_back(obj);
return false;
}
}
}
}
|