Coverage Report

Created: 2026-11-04 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/build/include/public/botan/asn1_print.h
Line
Count
Source
1
/*
2
* (C) 2014,2015,2017 Jack Lloyd
3
*
4
* Botan is released under the Simplified BSD License (see license.txt)
5
*/
6
7
#ifndef BOTAN_ASN1_PRINT_H_
8
#define BOTAN_ASN1_PRINT_H_
9
10
#include <botan/asn1_obj.h>
11
#include <iosfwd>
12
#include <string>
13
#include <vector>
14
15
namespace Botan {
16
17
class BigInt;
18
class BER_Decoder;
19
20
/**
21
* Format ASN.1 data and call a virtual to format
22
*/
23
class BOTAN_PUBLIC_API(2, 4) ASN1_Formatter /* NOLINT(*-special-member-functions) */ {
24
   public:
25
0
      virtual ~ASN1_Formatter() = default;
26
27
      /**
28
      * Create a formatter with the given settings
29
      *
30
      * @param print_context_specific if true, try to parse nested context specific data.
31
      * @param max_depth do not recurse more than this many times. If zero, recursion
32
      *        is unbounded.
33
      * @param require_der if true then non-canonical BER data is rejected
34
      */
35
      ASN1_Formatter(bool print_context_specific, size_t max_depth, bool require_der = false) :
36
9.39k
            m_print_context_specific(print_context_specific), m_max_depth(max_depth), m_require_der(require_der) {}
37
38
      /**
39
      * Format the given ASN.1 data, writing the result to a stream
40
      *
41
      * @param out the stream to write the formatted output to
42
      * @param in the DER or BER encoded data
43
      * @param len the length of in in bytes
44
      */
45
      void print_to_stream(std::ostream& out, const uint8_t in[], size_t len) const;
46
47
      /**
48
      * Return the given ASN.1 data in formatted form
49
      *
50
      * @param in the DER or BER encoded data
51
      * @param len the length of in in bytes
52
      */
53
      std::string print(const uint8_t in[], size_t len) const;
54
55
      /**
56
      * Return the given ASN.1 data in formatted form
57
      *
58
      * @param vec the DER or BER encoded data
59
      */
60
      template <typename Alloc>
61
      std::string print(const std::vector<uint8_t, Alloc>& vec) const {
62
         return print(vec.data(), vec.size());
63
      }
64
65
   protected:
66
      /**
67
      * This is called for each element
68
      */
69
      virtual std::string format(
70
         ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const = 0;
71
72
      /**
73
      * This is called to format binary elements that we don't know how to
74
      * convert to a string. The result will be passed as value to format; the
75
      * tags are included as a hint to aid decoding.
76
      *
77
      * TODO(Botan4) change the vector to a span
78
      */
79
      virtual std::string format_bin(ASN1_Type type_tag,
80
                                     ASN1_Class class_tag,
81
                                     const std::vector<uint8_t>& vec) const = 0;
82
83
      /**
84
      * This is called to format integers
85
      */
86
      virtual std::string format_bn(const BigInt& bn) const = 0;
87
88
   private:
89
      void decode(std::ostream& output, BER_Decoder& decoder, size_t level) const;
90
91
      const bool m_print_context_specific;
92
      const size_t m_max_depth;
93
      const bool m_require_der;
94
};
95
96
/**
97
* Format ASN.1 data into human readable output. The exact form of the output for
98
* any particular input is not guaranteed and may change from release to release.
99
*/
100
class BOTAN_PUBLIC_API(2, 4) ASN1_Pretty_Printer final : public ASN1_Formatter {
101
   public:
102
      /**
103
      * Create a pretty printer with the given settings
104
      *
105
      * @param print_limit strings larger than this are not printed
106
      * @param print_binary_limit binary strings larger than this are not printed
107
      * @param print_context_specific if true, try to parse nested context specific data.
108
      * @param initial_level the initial depth (0 or 1 are the only reasonable values)
109
      * @param value_column ASN.1 values are lined up at this column in output
110
      * @param max_depth do not recurse more than this many times. If zero, recursion
111
      *        is unbounded.
112
      * @param require_der if true then non-canonical BER data is rejected
113
      */
114
      explicit ASN1_Pretty_Printer(size_t print_limit = 4096,
115
                                   size_t print_binary_limit = 2048,
116
                                   bool print_context_specific = true,
117
                                   size_t initial_level = 0,
118
                                   size_t value_column = 60,
119
                                   size_t max_depth = 64,
120
                                   bool require_der = false) :
121
            ASN1_Formatter(print_context_specific, max_depth, require_der),
122
            m_print_limit(print_limit),
123
            m_print_binary_limit(print_binary_limit),
124
            m_initial_level(initial_level),
125
0
            m_value_column(value_column) {}
126
127
   private:
128
      std::string format(
129
         ASN1_Type type_tag, ASN1_Class class_tag, size_t level, size_t length, std::string_view value) const override;
130
131
      std::string format_bin(ASN1_Type type_tag, ASN1_Class class_tag, const std::vector<uint8_t>& vec) const override;
132
133
      std::string format_bn(const BigInt& bn) const override;
134
135
      const size_t m_print_limit;
136
      const size_t m_print_binary_limit;
137
      const size_t m_initial_level;
138
      const size_t m_value_column;
139
};
140
141
}  // namespace Botan
142
143
#endif