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