7#include <botan/asn1_print.h>
8#include <botan/bigint.h>
10#include <botan/der_enc.h>
11#include <botan/ber_dec.h>
12#include <botan/oids.h>
21bool all_printable_chars(
const uint8_t bits[],
size_t bits_len)
23 for(
size_t i = 0; i != bits_len; ++i)
29 if((std::isalnum(c) || c ==
'.' || c ==
':' || c ==
'/' || c ==
'-') ==
false)
38bool possibly_a_general_name(
const uint8_t bits[],
size_t bits_len)
43 if(bits[0] != 0x82 && bits[0] != 0x86)
46 if(bits[1] != bits_len - 2)
49 if(all_printable_chars(bits + 2, bits_len - 2) ==
false)
59 std::ostringstream output;
69 decode(output, dec, 0);
72void ASN1_Formatter::decode(std::ostream& output,
78 const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
84 const size_t length = obj.
length();
88 std::vector<uint8_t> bits;
99 output <<
format(type_tag, class_tag, level, length,
"");
100 decode(output, cons_info, level + 1);
104 output <<
format(type_tag, class_tag, level, length,
110 bool success_parsing_cs =
false;
112 if(m_print_context_specific)
116 if(possibly_a_general_name(bits.data(), bits.size()))
118 output <<
format(type_tag, class_tag, level, level,
120 success_parsing_cs =
true;
122 else if(recurse_deeper)
124 std::vector<uint8_t> inner_bits;
125 data.decode(inner_bits, type_tag);
127 BER_Decoder inner(inner_bits);
128 std::ostringstream inner_data;
129 decode(inner_data, inner, level + 1);
130 output << inner_data.str();
131 success_parsing_cs =
true;
139 if(success_parsing_cs ==
false)
141 output <<
format(type_tag, class_tag, level, length,
153 out = oid.to_string();
157 out +=
" [" + oid.to_string() +
"]";
160 output <<
format(type_tag, class_tag, level, length, out);
184 data.decode(
boolean);
185 output <<
format(type_tag, class_tag, level, length, (
boolean ?
"true" :
"false"));
189 output <<
format(type_tag, class_tag, level, length,
"");
193 std::vector<uint8_t> decoded_bits;
194 data.decode(decoded_bits, type_tag);
195 bool printing_octet_string_worked =
false;
201 BER_Decoder inner(decoded_bits);
203 std::ostringstream inner_data;
204 decode(inner_data, inner, level + 1);
206 output <<
format(type_tag, class_tag, level, length,
"");
207 output << inner_data.str();
208 printing_octet_string_worked =
true;
215 if(!printing_octet_string_worked)
217 output <<
format(type_tag, class_tag, level, length,
218 format_bin(type_tag, class_tag, decoded_bits));
225 output <<
format(type_tag, class_tag, level, length, str.value());
231 output <<
format(type_tag, class_tag, level, length, time.readable_string());
235 output <<
"Unknown ASN.1 tag class=" <<
static_cast<int>(class_tag)
236 <<
" type=" <<
static_cast<int>(type_tag) <<
"\n";
258 name +=
"[" + std::to_string(type_tag) +
"]";
274std::string ASN1_Pretty_Printer::format(
ASN1_Tag type_tag,
278 const std::string& value)
const
280 bool should_skip =
false;
282 if(value.length() > m_print_limit)
288 value.length() > m_print_binary_limit)
293 level += m_initial_level;
295 std::ostringstream oss;
297 oss <<
" d=" << std::setw(2) << level
298 <<
", l=" << std::setw(4) << length <<
":"
299 << std::string(level + 1,
' ') << format_type(type_tag, class_tag);
301 if(value !=
"" && !should_skip)
303 const size_t current_pos =
static_cast<size_t>(oss.tellp());
304 const size_t spaces_to_align =
305 (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
307 oss << std::string(spaces_to_align,
' ') << value;
315std::string ASN1_Pretty_Printer::format_bin(
ASN1_Tag ,
317 const std::vector<uint8_t>& vec)
const
319 if(all_printable_chars(vec.data(), vec.size()))
static bool is_string_type(ASN1_Tag tag)
BER_Object get_next_object()
const uint8_t * bits() const
ASN1_Tag get_class() const
static std::vector< uint8_t > encode(const BigInt &n)
DER_Encoder & add_object(ASN1_Tag type_tag, ASN1_Tag class_tag, const uint8_t rep[], size_t length)
BOTAN_UNSTABLE_API std::string oid2str_or_empty(const OID &oid)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
const char * cast_uint8_ptr_to_char(const uint8_t *b)
std::string asn1_tag_to_string(ASN1_Tag type)