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>
14#include <botan/internal/mem_utils.h>
23bool is_printable_char(
char c) {
24 if(c >=
'a' && c <=
'z') {
28 if(c >=
'A' && c <=
'Z') {
32 if(c >=
'0' && c <=
'9') {
36 if(c ==
'.' || c ==
':' || c ==
'/' || c ==
'-') {
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])) {
55bool possibly_a_general_name(
const uint8_t bits[],
size_t bits_len) {
60 if(bits[0] != 0x82 && bits[0] != 0x86) {
64 if(bits[1] != bits_len - 2) {
68 if(!all_printable_chars(bits + 2, bits_len - 2)) {
78 std::ostringstream output;
85 decode(output, dec, 0);
88void ASN1_Formatter::decode(std::ostream& output,
BER_Decoder& decoder,
size_t level)
const {
91 const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
96 const size_t length = obj.
length();
100 std::vector<uint8_t> bits;
109 output <<
format(type_tag, class_tag, level, length,
"");
110 decode(output, cons_info, level + 1);
112 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
115 bool success_parsing_cs =
false;
117 if(m_print_context_specific) {
119 if(possibly_a_general_name(bits.data(), bits.size())) {
121 success_parsing_cs =
true;
122 }
else if(recurse_deeper) {
123 std::vector<uint8_t> inner_bits;
124 data.decode(inner_bits, type_tag);
126 BER_Decoder inner(inner_bits);
127 std::ostringstream inner_data;
128 decode(inner_data, inner, level + 1);
129 output << inner_data.str();
130 success_parsing_cs =
true;
135 if(!success_parsing_cs) {
136 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
142 const std::string name = oid.human_name_or_empty();
143 const std::string oid_str = oid.to_string();
146 output <<
format(type_tag, class_tag, level, length, oid_str);
148 output <<
format(type_tag, class_tag, level, length,
fmt(
"{} [{}]", name, oid_str));
159 output <<
format(type_tag, class_tag, level, length,
format_bn(number));
161 bool boolean =
false;
162 data.decode(
boolean);
163 output <<
format(type_tag, class_tag, level, length, (
boolean ?
"true" :
"false"));
165 output <<
format(type_tag, class_tag, level, length,
"");
167 std::vector<uint8_t> decoded_bits;
168 data.decode(decoded_bits, type_tag);
169 bool printing_octet_string_worked =
false;
173 BER_Decoder inner(decoded_bits);
175 std::ostringstream inner_data;
176 decode(inner_data, inner, level + 1);
178 output <<
format(type_tag, class_tag, level, length,
"");
179 output << inner_data.str();
180 printing_octet_string_worked =
true;
184 if(!printing_octet_string_worked) {
185 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, decoded_bits));
190 output <<
format(type_tag, class_tag, level, length, str.value());
194 output <<
format(type_tag, class_tag, level, length, time.readable_string());
196 output <<
"Unknown ASN.1 tag class=" <<
static_cast<int>(class_tag) <<
" type=" <<
static_cast<int>(type_tag)
215 std::ostringstream oss;
221 oss <<
"[" << std::to_string(
static_cast<uint32_t
>(type_tag)) <<
"]";
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;
239 if(value.length() > m_print_limit) {
244 value.length() > m_print_binary_limit) {
248 level += m_initial_level;
250 std::ostringstream oss;
252 oss <<
" d=" << std::setw(2) << level <<
", l=" << std::setw(4) << length <<
":" << std::string(level + 1,
' ')
253 << format_type(type_tag, class_tag);
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);
259 oss << std::string(spaces_to_align,
' ') << value;
267std::string ASN1_Pretty_Printer::format_bin(
ASN1_Type ,
269 const std::vector<uint8_t>& vec)
const {
270 if(all_printable_chars(vec.data(), vec.size())) {
277std::string ASN1_Pretty_Printer::format_bn(
const BigInt& bn)
const {
279 return bn.to_dec_string();
281 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)
std::string bytes_to_string(std::span< const uint8_t > bytes)
void hex_encode(char output[], const uint8_t input[], size_t input_length, bool uppercase)
bool intersects(ASN1_Class x, ASN1_Class y)