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
|
/*
* BER Decoder
* (C) 1999-2010 Jack Lloyd
*
* Distributed under the terms of the Botan license
*/
#ifndef BOTAN_BER_DECODER_H__
#define BOTAN_BER_DECODER_H__
#include <botan/asn1_oid.h>
#include <botan/data_src.h>
namespace Botan {
/**
* BER Decoding Object
*/
class BOTAN_DLL BER_Decoder
{
public:
BER_Object get_next_object();
std::vector<byte> get_next_octet_string();
void push_back(const BER_Object& obj);
bool more_items() const;
BER_Decoder& verify_end();
BER_Decoder& discard_remaining();
BER_Decoder start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag = UNIVERSAL);
BER_Decoder& end_cons();
BER_Decoder& get_next(BER_Object& ber);
BER_Decoder& raw_bytes(secure_vector<byte>& v);
BER_Decoder& raw_bytes(std::vector<byte>& v);
BER_Decoder& decode_null();
BER_Decoder& decode(bool& v);
BER_Decoder& decode(size_t& v);
BER_Decoder& decode(class BigInt& v);
BER_Decoder& decode(std::vector<byte>& v, ASN1_Tag type_tag);
BER_Decoder& decode(secure_vector<byte>& v, ASN1_Tag type_tag);
BER_Decoder& decode(bool& v,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
BER_Decoder& decode(size_t& v,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
BER_Decoder& decode(class BigInt& v,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
BER_Decoder& decode(std::vector<byte>& v,
ASN1_Tag real_type,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
BER_Decoder& decode(secure_vector<byte>& v,
ASN1_Tag real_type,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC);
BER_Decoder& decode(class ASN1_Object& obj,
ASN1_Tag type_tag = NO_OBJECT,
ASN1_Tag class_tag = NO_OBJECT);
BER_Decoder& decode_octet_string_bigint(class BigInt& b);
u64bit decode_constrained_integer(ASN1_Tag type_tag,
ASN1_Tag class_tag,
size_t T_bytes);
template<typename T> BER_Decoder& decode_integer_type(T& out)
{
return decode_integer_type<T>(out, INTEGER, UNIVERSAL);
}
template<typename T>
BER_Decoder& decode_integer_type(T& out,
ASN1_Tag type_tag,
ASN1_Tag class_tag = CONTEXT_SPECIFIC)
{
out = decode_constrained_integer(type_tag, class_tag, sizeof(out));
return (*this);
}
template<typename T>
BER_Decoder& decode_optional(T& out,
ASN1_Tag type_tag,
ASN1_Tag class_tag,
const T& default_value = T());
template<typename T>
BER_Decoder& decode_list(std::vector<T>& out,
ASN1_Tag type_tag = SEQUENCE,
ASN1_Tag class_tag = UNIVERSAL);
template<typename T>
BER_Decoder& decode_and_check(const T& expected,
const std::string& error_msg)
{
T actual;
decode(actual);
if(actual != expected)
throw Decoding_Error(error_msg);
return (*this);
}
BER_Decoder& decode_optional_string(std::vector<byte>& out,
ASN1_Tag real_type,
u16bit type_no);
BER_Decoder& decode_optional_string(secure_vector<byte>& out,
ASN1_Tag real_type,
u16bit type_no);
BER_Decoder& operator=(const BER_Decoder&) = delete;
BER_Decoder(DataSource&);
BER_Decoder(const byte[], size_t);
BER_Decoder(const secure_vector<byte>&);
BER_Decoder(const std::vector<byte>& vec);
BER_Decoder(const BER_Decoder&);
~BER_Decoder();
private:
BER_Decoder* parent;
DataSource* source;
BER_Object pushed;
mutable bool owns;
};
/*
* Decode an OPTIONAL or DEFAULT element
*/
template<typename T>
BER_Decoder& BER_Decoder::decode_optional(T& out,
ASN1_Tag type_tag,
ASN1_Tag class_tag,
const T& default_value)
{
BER_Object obj = get_next_object();
if(obj.type_tag == type_tag && obj.class_tag == class_tag)
{
if((class_tag & CONSTRUCTED) && (class_tag & CONTEXT_SPECIFIC))
BER_Decoder(obj.value).decode(out).verify_end();
else
{
push_back(obj);
decode(out, type_tag, class_tag);
}
}
else
{
out = default_value;
push_back(obj);
}
return (*this);
}
/*
* Decode a list of homogenously typed values
*/
template<typename T>
BER_Decoder& BER_Decoder::decode_list(std::vector<T>& vec,
ASN1_Tag type_tag,
ASN1_Tag class_tag)
{
BER_Decoder list = start_cons(type_tag, class_tag);
while(list.more_items())
{
T value;
list.decode(value);
vec.push_back(value);
}
list.end_cons();
return (*this);
}
}
#endif
|