7#include <botan/asn1_print.h>
9#include <botan/ber_dec.h>
10#include <botan/bigint.h>
11#include <botan/der_enc.h>
13#include <botan/internal/fmt.h>
22bool 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) {
39bool possibly_a_general_name(
const uint8_t bits[],
size_t bits_len) {
44 if(bits[0] != 0x82 && bits[0] != 0x86) {
48 if(bits[1] != bits_len - 2) {
52 if(all_printable_chars(bits + 2, bits_len - 2) ==
false) {
62 std::ostringstream output;
69 decode(output, dec, 0);
72void ASN1_Formatter::decode(std::ostream& output,
BER_Decoder& decoder,
size_t level)
const {
75 const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
80 const size_t length = obj.
length();
84 std::vector<uint8_t> bits;
93 output <<
format(type_tag, class_tag, level, length,
"");
94 decode(output, cons_info, level + 1);
96 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
99 bool success_parsing_cs =
false;
101 if(m_print_context_specific) {
103 if(possibly_a_general_name(bits.data(), bits.size())) {
106 success_parsing_cs =
true;
107 }
else if(recurse_deeper) {
108 std::vector<uint8_t> inner_bits;
109 data.decode(inner_bits, type_tag);
111 BER_Decoder inner(inner_bits);
112 std::ostringstream inner_data;
113 decode(inner_data, inner, level + 1);
114 output << inner_data.str();
115 success_parsing_cs =
true;
120 if(success_parsing_cs ==
false) {
121 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
127 const std::string
name = oid.human_name_or_empty();
128 const std::string oid_str = oid.to_string();
131 output <<
format(type_tag, class_tag, level, length, oid_str);
133 output <<
format(type_tag, class_tag, level, length,
fmt(
"{} [{}]",
name, oid_str));
144 output <<
format(type_tag, class_tag, level, length,
format_bn(number));
147 data.decode(
boolean);
148 output <<
format(type_tag, class_tag, level, length, (
boolean ?
"true" :
"false"));
150 output <<
format(type_tag, class_tag, level, length,
"");
152 std::vector<uint8_t> decoded_bits;
153 data.decode(decoded_bits, type_tag);
154 bool printing_octet_string_worked =
false;
158 BER_Decoder inner(decoded_bits);
160 std::ostringstream inner_data;
161 decode(inner_data, inner, level + 1);
163 output <<
format(type_tag, class_tag, level, length,
"");
164 output << inner_data.str();
165 printing_octet_string_worked =
true;
169 if(!printing_octet_string_worked) {
170 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, decoded_bits));
175 output <<
format(type_tag, class_tag, level, length, str.value());
179 output <<
format(type_tag, class_tag, level, length, time.readable_string());
181 output <<
"Unknown ASN.1 tag class=" <<
static_cast<int>(class_tag) <<
" type=" <<
static_cast<int>(type_tag)
200 std::ostringstream oss;
206 oss <<
"[" << std::to_string(
static_cast<uint32_t
>(type_tag)) <<
"]";
220std::string ASN1_Pretty_Printer::format(
221 ASN1_Type type_tag,
ASN1_Class class_tag,
size_t level,
size_t length, std::string_view value)
const {
222 bool should_skip =
false;
224 if(value.length() > m_print_limit) {
229 value.length() > m_print_binary_limit) {
233 level += m_initial_level;
235 std::ostringstream oss;
237 oss <<
" d=" << std::setw(2) << level <<
", l=" << std::setw(4) << length <<
":" << std::string(level + 1,
' ')
238 << format_type(type_tag, class_tag);
240 if(!value.empty() && !should_skip) {
241 const size_t current_pos =
static_cast<size_t>(oss.tellp());
242 const size_t spaces_to_align = (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
244 oss << std::string(spaces_to_align,
' ') << value;
252std::string ASN1_Pretty_Printer::format_bin(
ASN1_Type ,
254 const std::vector<uint8_t>& vec)
const {
255 if(all_printable_chars(vec.data(), vec.size())) {
262std::string ASN1_Pretty_Printer::format_bn(
const BigInt& bn)
const {
264 return bn.to_dec_string();
266 return bn.to_hex_string();
static bool is_string_type(ASN1_Type tag)
BER_Object get_next_object()
const uint8_t * bits() const
ASN1_Class get_class() const
DER_Encoder & add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length)
std::string asn1_tag_to_string(ASN1_Type type)
std::string fmt(std::string_view format, const T &... args)
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)
bool intersects(ASN1_Class x, ASN1_Class y)