Botan 3.9.0
Crypto and TLS for C&
asn1_print.cpp
Go to the documentation of this file.
1/*
2* (C) 2014,2015,2017 Jack Lloyd
3*
4* Botan is released under the Simplified BSD License (see license.txt)
5*/
6
7#include <botan/asn1_print.h>
8
9#include <botan/ber_dec.h>
10#include <botan/bigint.h>
11#include <botan/der_enc.h>
12#include <botan/hex.h>
13#include <botan/internal/fmt.h>
14#include <botan/internal/mem_utils.h>
15#include <iomanip>
16#include <sstream>
17
18namespace Botan {
19
20namespace {
21
22// Printable here means fits into an ASN.1 "PRINTABLE STRING" type
23bool is_printable_char(char c) {
24 if(c >= 'a' && c <= 'z') {
25 return true;
26 }
27
28 if(c >= 'A' && c <= 'Z') {
29 return true;
30 }
31
32 if(c >= '0' && c <= '9') {
33 return true;
34 }
35
36 if(c == '.' || c == ':' || c == '/' || c == '-') {
37 return true;
38 }
39
40 return false;
41}
42
43bool all_printable_chars(const uint8_t bits[], size_t bits_len) {
44 for(size_t i = 0; i != bits_len; ++i) {
45 if(!is_printable_char(bits[i])) {
46 return false;
47 }
48 }
49 return true;
50}
51
52/*
53* Special hack to handle GeneralName [2] and [6] (DNS name and URI)
54*/
55bool possibly_a_general_name(const uint8_t bits[], size_t bits_len) {
56 if(bits_len <= 2) {
57 return false;
58 }
59
60 if(bits[0] != 0x82 && bits[0] != 0x86) {
61 return false;
62 }
63
64 if(bits[1] != bits_len - 2) {
65 return false;
66 }
67
68 if(!all_printable_chars(bits + 2, bits_len - 2)) {
69 return false;
70 }
71
72 return true;
73}
74
75} // namespace
76
77std::string ASN1_Formatter::print(const uint8_t in[], size_t len) const {
78 std::ostringstream output;
79 print_to_stream(output, in, len);
80 return output.str();
81}
82
83void ASN1_Formatter::print_to_stream(std::ostream& output, const uint8_t in[], size_t len) const {
84 BER_Decoder dec(in, len);
85 decode(output, dec, 0);
86}
87
88void ASN1_Formatter::decode(std::ostream& output, BER_Decoder& decoder, size_t level) const {
89 BER_Object obj = decoder.get_next_object();
90
91 const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
92
93 while(obj.is_set()) {
94 const ASN1_Type type_tag = obj.type();
95 const ASN1_Class class_tag = obj.get_class();
96 const size_t length = obj.length();
97
98 /* hack to insert the tag+length back in front of the stuff now
99 that we've gotten the type info */
100 std::vector<uint8_t> bits;
101 DER_Encoder(bits).add_object(type_tag, class_tag, obj.bits(), obj.length());
102
103 BER_Decoder data(bits);
104
105 if(intersects(class_tag, ASN1_Class::Constructed)) {
106 BER_Decoder cons_info(obj.bits(), obj.length());
107
108 if(recurse_deeper) {
109 output << format(type_tag, class_tag, level, length, "");
110 decode(output, cons_info, level + 1); // recurse
111 } else {
112 output << format(type_tag, class_tag, level, length, format_bin(type_tag, class_tag, bits));
113 }
114 } else if(intersects(class_tag, ASN1_Class::Application) || intersects(class_tag, ASN1_Class::ContextSpecific)) {
115 bool success_parsing_cs = false;
116
117 if(m_print_context_specific) {
118 try {
119 if(possibly_a_general_name(bits.data(), bits.size())) {
120 output << format(type_tag, class_tag, level, level, bytes_to_string(std::span{bits}.subspan(2)));
121 success_parsing_cs = true;
122 } else if(recurse_deeper) {
123 std::vector<uint8_t> inner_bits;
124 data.decode(inner_bits, type_tag);
125
126 BER_Decoder inner(inner_bits);
127 std::ostringstream inner_data;
128 decode(inner_data, inner, level + 1); // recurse
129 output << inner_data.str();
130 success_parsing_cs = true;
131 }
132 } catch(...) {}
133 }
134
135 if(!success_parsing_cs) {
136 output << format(type_tag, class_tag, level, length, format_bin(type_tag, class_tag, bits));
137 }
138 } else if(type_tag == ASN1_Type::ObjectId) {
139 OID oid;
140 data.decode(oid);
141
142 const std::string name = oid.human_name_or_empty();
143 const std::string oid_str = oid.to_string();
144
145 if(name.empty()) {
146 output << format(type_tag, class_tag, level, length, oid_str);
147 } else {
148 output << format(type_tag, class_tag, level, length, fmt("{} [{}]", name, oid_str));
149 }
150 } else if(type_tag == ASN1_Type::Integer || type_tag == ASN1_Type::Enumerated) {
151 BigInt number;
152
153 if(type_tag == ASN1_Type::Integer) {
154 data.decode(number);
155 } else if(type_tag == ASN1_Type::Enumerated) {
156 data.decode(number, ASN1_Type::Enumerated, class_tag);
157 }
158
159 output << format(type_tag, class_tag, level, length, format_bn(number));
160 } else if(type_tag == ASN1_Type::Boolean) {
161 bool boolean = false;
162 data.decode(boolean);
163 output << format(type_tag, class_tag, level, length, (boolean ? "true" : "false"));
164 } else if(type_tag == ASN1_Type::Null) {
165 output << format(type_tag, class_tag, level, length, "");
166 } else if(type_tag == ASN1_Type::OctetString || type_tag == ASN1_Type::BitString) {
167 std::vector<uint8_t> decoded_bits;
168 data.decode(decoded_bits, type_tag);
169 bool printing_octet_string_worked = false;
170
171 if(recurse_deeper) {
172 try {
173 BER_Decoder inner(decoded_bits);
174
175 std::ostringstream inner_data;
176 decode(inner_data, inner, level + 1); // recurse
177
178 output << format(type_tag, class_tag, level, length, "");
179 output << inner_data.str();
180 printing_octet_string_worked = true;
181 } catch(...) {}
182 }
183
184 if(!printing_octet_string_worked) {
185 output << format(type_tag, class_tag, level, length, format_bin(type_tag, class_tag, decoded_bits));
186 }
187 } else if(ASN1_String::is_string_type(type_tag)) {
188 ASN1_String str;
189 data.decode(str);
190 output << format(type_tag, class_tag, level, length, str.value());
191 } else if(type_tag == ASN1_Type::UtcTime || type_tag == ASN1_Type::GeneralizedTime) {
192 ASN1_Time time;
193 data.decode(time);
194 output << format(type_tag, class_tag, level, length, time.readable_string());
195 } else {
196 output << "Unknown ASN.1 tag class=" << static_cast<int>(class_tag) << " type=" << static_cast<int>(type_tag)
197 << "\n";
198 }
199
200 obj = decoder.get_next_object();
201 }
202}
203
204namespace {
205
206std::string format_type(ASN1_Type type_tag, ASN1_Class class_tag) {
207 if(class_tag == ASN1_Class::Universal) {
208 return asn1_tag_to_string(type_tag);
209 }
210
211 if(class_tag == ASN1_Class::Constructed && (type_tag == ASN1_Type::Sequence || type_tag == ASN1_Type::Set)) {
212 return asn1_tag_to_string(type_tag);
213 }
214
215 std::ostringstream oss;
216
217 if(intersects(class_tag, ASN1_Class::Constructed)) {
218 oss << "cons ";
219 }
220
221 oss << "[" << std::to_string(static_cast<uint32_t>(type_tag)) << "]";
222
223 if(intersects(class_tag, ASN1_Class::Application)) {
224 oss << " appl";
225 }
227 oss << " context";
228 }
229
230 return oss.str();
231}
232
233} // namespace
234
235std::string ASN1_Pretty_Printer::format(
236 ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const {
237 bool should_skip = false;
238
239 if(value.length() > m_print_limit) {
240 should_skip = true;
241 }
242
243 if((type_tag == ASN1_Type::OctetString || type_tag == ASN1_Type::BitString) &&
244 value.length() > m_print_binary_limit) {
245 should_skip = true;
246 }
247
248 level += m_initial_level;
249
250 std::ostringstream oss;
251
252 oss << " d=" << std::setw(2) << level << ", l=" << std::setw(4) << length << ":" << std::string(level + 1, ' ')
253 << format_type(type_tag, class_tag);
254
255 if(!value.empty() && !should_skip) {
256 const size_t current_pos = static_cast<size_t>(oss.tellp());
257 const size_t spaces_to_align = (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
258
259 oss << std::string(spaces_to_align, ' ') << value;
260 }
261
262 oss << "\n";
263
264 return oss.str();
265}
266
267std::string ASN1_Pretty_Printer::format_bin(ASN1_Type /*type_tag*/,
268 ASN1_Class /*class_tag*/,
269 const std::vector<uint8_t>& vec) const {
270 if(all_printable_chars(vec.data(), vec.size())) {
271 return bytes_to_string(vec);
272 } else {
273 return hex_encode(vec);
274 }
275}
276
277std::string ASN1_Pretty_Printer::format_bn(const BigInt& bn) const {
278 if(bn.bits() < 16) {
279 return bn.to_dec_string();
280 } else {
281 return bn.to_hex_string();
282 }
283}
284
285} // namespace Botan
virtual std::string format_bin(ASN1_Type type_tag, ASN1_Class class_tag, const std::vector< uint8_t > &vec) const =0
virtual std::string format(ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const =0
std::string print(const uint8_t in[], size_t len) const
void print_to_stream(std::ostream &out, const uint8_t in[], size_t len) const
virtual std::string format_bn(const BigInt &bn) const =0
static bool is_string_type(ASN1_Type tag)
Definition asn1_str.cpp:60
BER_Object get_next_object()
Definition ber_dec.cpp:248
size_t length() const
Definition asn1_obj.h:152
const uint8_t * bits() const
Definition asn1_obj.h:150
bool is_set() const
Definition asn1_obj.h:138
ASN1_Type type() const
Definition asn1_obj.h:146
ASN1_Class get_class() const
Definition asn1_obj.h:148
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
Definition der_enc.cpp:224
std::string asn1_tag_to_string(ASN1_Type type)
Definition asn1_obj.cpp:93
ASN1_Class
Definition asn1_obj.h:28
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:43
std::string bytes_to_string(std::span< const uint8_t > bytes)
Definition mem_utils.h:45
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
Definition hex.cpp:35
bool intersects(ASN1_Class x, ASN1_Class y)
Definition asn1_obj.h:70