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
|
/*
* (C) 2014,2015,2017 Jack Lloyd
*
* Botan is released under the Simplified BSD License (see license.txt)
*/
#include <botan/asn1_print.h>
#include <botan/bigint.h>
#include <botan/hex.h>
#include <botan/der_enc.h>
#include <botan/ber_dec.h>
#include <botan/asn1_time.h>
#include <botan/asn1_str.h>
#include <botan/oids.h>
#include <iomanip>
#include <sstream>
#include <cctype>
namespace Botan {
std::string ASN1_Pretty_Printer::print(const uint8_t in[], size_t len) const
{
std::ostringstream output;
print_to_stream(output, in, len);
return output.str();
}
void ASN1_Pretty_Printer::print_to_stream(std::ostream& output,
const uint8_t in[],
size_t len) const
{
BER_Decoder dec(in, len);
decode(output, dec, m_initial_level);
}
std::string ASN1_Pretty_Printer::format_binary(const std::vector<uint8_t>& in) const
{
std::ostringstream out;
size_t unprintable = 0;
for(size_t i = 0; i != in.size(); ++i)
{
const int c = in[i];
if(std::isalnum(c))
{
out << static_cast<char>(c);
}
else
{
out << "x" << std::hex << static_cast<int>(c) << std::dec;
++unprintable;
if(unprintable >= in.size() / 4)
{
return hex_encode(in);
}
}
}
return out.str();
}
void ASN1_Pretty_Printer::emit(std::ostream& output,
const std::string& type,
size_t level, size_t length,
const std::string& value) const
{
std::ostringstream oss;
oss << " d=" << std::setw(2) << level
<< ", l=" << std::setw(4) << length << ":"
<< std::string(level + 1, ' ') << type;
bool should_skip = false;
if(value.length() > m_print_limit)
{
should_skip = true;
}
if((type == "OCTET STRING" || type == "BIT STRING") && value.length() > m_print_binary_limit)
{
should_skip = true;
}
const std::string s = oss.str();
output << s;
if(value != "" && !should_skip)
{
const size_t spaces_to_align =
(s.size() >= m_value_column) ? 1 : (m_value_column - s.size());
output << std::string(spaces_to_align, ' ') << value;
}
output << "\n";
}
void ASN1_Pretty_Printer::decode(std::ostream& output,
BER_Decoder& decoder,
size_t level) const
{
BER_Object obj = decoder.get_next_object();
while(obj.type_tag != NO_OBJECT)
{
const ASN1_Tag type_tag = obj.type_tag;
const ASN1_Tag class_tag = obj.class_tag;
const size_t length = obj.value.size();
/* hack to insert the tag+length back in front of the stuff now
that we've gotten the type info */
DER_Encoder encoder;
encoder.add_object(type_tag, class_tag, obj.value);
std::vector<uint8_t> bits = encoder.get_contents_unlocked();
BER_Decoder data(bits);
if(class_tag & CONSTRUCTED)
{
BER_Decoder cons_info(obj.value);
if(type_tag == SEQUENCE)
{
emit(output, "SEQUENCE", level, length);
decode(output, cons_info, level + 1); // recurse
}
else if(type_tag == SET)
{
emit(output, "SET", level, length);
decode(output, cons_info, level + 1); // recurse
}
else
{
std::string name;
if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC))
{
name = "cons [" + std::to_string(type_tag) + "]";
if(class_tag & APPLICATION)
{
name += " appl";
}
if(class_tag & CONTEXT_SPECIFIC)
{
name += " context";
}
}
else
{
name = asn1_tag_to_string(type_tag) + " (cons)";
}
emit(output, name, level, length);
decode(output, cons_info, level + 1); // recurse
}
}
else if((class_tag & APPLICATION) || (class_tag & CONTEXT_SPECIFIC))
{
if(m_print_context_specific)
{
try
{
std::vector<uint8_t> bits;
data.decode(bits, type_tag);
BER_Decoder inner(bits);
decode(output, inner, level + 1); // recurse
}
catch(...)
{
emit(output, "[" + std::to_string(type_tag) + "]", level, length, format_binary(bits));
}
}
else
{
emit(output, "[" + std::to_string(type_tag) + "]", level, length, format_binary(bits));
}
}
else if(type_tag == OBJECT_ID)
{
OID oid;
data.decode(oid);
std::string out = OIDS::lookup(oid);
if(out.empty())
{
out = oid.as_string();
}
else
{
out += " [" + oid.as_string() + "]";
}
emit(output, asn1_tag_to_string(type_tag), level, length, out);
}
else if(type_tag == INTEGER || type_tag == ENUMERATED)
{
BigInt number;
if(type_tag == INTEGER)
{
data.decode(number);
}
else if(type_tag == ENUMERATED)
{
data.decode(number, ENUMERATED, class_tag);
}
std::vector<uint8_t> rep;
/* If it's small, it's probably a number, not a hash */
if(number.bits() <= 20)
{
rep = BigInt::encode(number, BigInt::Decimal);
}
else
{
rep = BigInt::encode(number, BigInt::Hexadecimal);
}
std::string str;
for(size_t i = 0; i != rep.size(); ++i)
{
str += static_cast<char>(rep[i]);
}
emit(output, asn1_tag_to_string(type_tag), level, length, str);
}
else if(type_tag == BOOLEAN)
{
bool boolean;
data.decode(boolean);
emit(output, asn1_tag_to_string(type_tag), level, length, (boolean ? "true" : "false"));
}
else if(type_tag == NULL_TAG)
{
emit(output, asn1_tag_to_string(type_tag), level, length);
}
else if(type_tag == OCTET_STRING || type_tag == BIT_STRING)
{
std::vector<uint8_t> decoded_bits;
data.decode(decoded_bits, type_tag);
try
{
BER_Decoder inner(decoded_bits);
decode(output, inner, level + 1);
}
catch(...)
{
emit(output, asn1_tag_to_string(type_tag), level, length, format_binary(decoded_bits));
}
}
else if(ASN1_String::is_string_type(type_tag))
{
ASN1_String str;
data.decode(str);
emit(output, asn1_tag_to_string(type_tag), level, length, str.value());
}
else if(type_tag == UTC_TIME || type_tag == GENERALIZED_TIME)
{
X509_Time time;
data.decode(time);
emit(output, asn1_tag_to_string(type_tag), level, length, time.readable_string());
}
else
{
output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag)
<< " type=" << static_cast<int>(type_tag) << "\n";;
}
obj = decoder.get_next_object();
}
}
}
|