7#include <botan/asn1_print.h>
9#include <botan/asn1_time.h>
10#include <botan/ber_dec.h>
11#include <botan/bigint.h>
12#include <botan/der_enc.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/mem_utils.h>
24bool is_printable_char(
char c) {
25 if(c >=
'a' && c <=
'z') {
29 if(c >=
'A' && c <=
'Z') {
33 if(c >=
'0' && c <=
'9') {
37 if(c ==
'.' || c ==
':' || c ==
'/' || c ==
'-') {
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])) {
56bool possibly_a_general_name(
const uint8_t bits[],
size_t bits_len) {
61 if(bits[0] != 0x82 && bits[0] != 0x86) {
65 if(bits[1] != bits_len - 2) {
69 if(!all_printable_chars(bits + 2, bits_len - 2)) {
79 std::ostringstream output;
86 BER_Decoder dec(std::span<const uint8_t>{in, len}, decoder_limits);
87 decode(output, dec, 0);
90void ASN1_Formatter::decode(std::ostream& output,
BER_Decoder& decoder,
size_t level)
const {
93 const bool recurse_deeper = (m_max_depth == 0 || level < m_max_depth);
98 const size_t length = obj.
length();
102 std::vector<uint8_t> bits;
111 output <<
format(type_tag, class_tag, level, length,
"");
112 decode(output, cons_info, level + 1);
114 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
117 bool success_parsing_cs =
false;
119 if(m_print_context_specific) {
121 if(possibly_a_general_name(bits.data(), bits.size())) {
123 success_parsing_cs =
true;
124 }
else if(recurse_deeper) {
125 std::vector<uint8_t> inner_bits;
126 data.decode(inner_bits, type_tag);
128 BER_Decoder inner(inner_bits, decoder.
limits());
129 std::ostringstream inner_data;
130 decode(inner_data, inner, level + 1);
131 output << inner_data.str();
132 success_parsing_cs =
true;
137 if(!success_parsing_cs) {
138 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, bits));
144 const std::string name = oid.human_name_or_empty();
145 const std::string oid_str = oid.to_string();
148 output <<
format(type_tag, class_tag, level, length, oid_str);
150 output <<
format(type_tag, class_tag, level, length,
fmt(
"{} [{}]", name, oid_str));
161 output <<
format(type_tag, class_tag, level, length,
format_bn(number));
163 bool boolean =
false;
164 data.decode(
boolean);
165 output <<
format(type_tag, class_tag, level, length, (
boolean ?
"true" :
"false"));
167 output <<
format(type_tag, class_tag, level, length,
"");
169 std::vector<uint8_t> decoded_bits;
170 data.decode(decoded_bits, type_tag);
171 bool printing_octet_string_worked =
false;
175 BER_Decoder inner(decoded_bits, decoder.
limits());
177 std::ostringstream inner_data;
178 decode(inner_data, inner, level + 1);
180 output <<
format(type_tag, class_tag, level, length,
"");
181 output << inner_data.str();
182 printing_octet_string_worked =
true;
186 if(!printing_octet_string_worked) {
187 output <<
format(type_tag, class_tag, level, length,
format_bin(type_tag, class_tag, decoded_bits));
192 output <<
format(type_tag, class_tag, level, length, str.value());
196 output <<
format(type_tag, class_tag, level, length, time.readable_string());
198 output <<
"Unknown ASN.1 tag class=" <<
static_cast<int>(class_tag) <<
" type=" <<
static_cast<int>(type_tag)
217 std::ostringstream oss;
223 oss <<
"[" << std::to_string(
static_cast<uint32_t
>(type_tag)) <<
"]";
237std::string ASN1_Pretty_Printer::format(
238 ASN1_Type type_tag,
ASN1_Class class_tag,
size_t level,
size_t length, std::string_view value)
const {
239 bool should_skip =
false;
241 if(value.length() > m_print_limit) {
246 value.length() > m_print_binary_limit) {
250 level += m_initial_level;
252 std::ostringstream oss;
254 oss <<
" d=" << std::setw(2) << level <<
", l=" << std::setw(4) << length <<
":" << std::string(level + 1,
' ')
255 << format_type(type_tag, class_tag);
257 if(!value.empty() && !should_skip) {
258 const size_t current_pos =
static_cast<size_t>(oss.tellp());
259 const size_t spaces_to_align = (current_pos >= m_value_column) ? 1 : (m_value_column - current_pos);
261 oss << std::string(spaces_to_align,
' ') << value;
269std::string ASN1_Pretty_Printer::format_bin(
ASN1_Type ,
271 const std::vector<uint8_t>& vec)
const {
272 if(all_printable_chars(vec.data(), vec.size())) {
279std::string ASN1_Pretty_Printer::format_bn(
const BigInt& bn)
const {
281 return bn.to_dec_string();
283 return bn.to_hex_string();
static bool is_string_type(ASN1_Type tag)
static Limits BER(size_t max_nested_indef=16)
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)