Coverage Report

Created: 2021-02-21 07:20

/src/botan/build/include/botan/asn1_print.h
Line
Count
Source (jump to first uncovered line)
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 <string>
12
#include <vector>
13
#include <iosfwd>
14
15
namespace Botan {
16
17
class BER_Decoder;
18
19
/**
20
* Format ASN.1 data and call a virtual to format
21
*/
22
class BOTAN_PUBLIC_API(2,4) ASN1_Formatter
23
   {
24
   public:
25
2.83k
      virtual ~ASN1_Formatter() = default;
26
27
      /**
28
      * @param print_context_specific if true, try to parse nested context specific data.
29
      * @param max_depth do not recurse more than this many times. If zero, recursion
30
      *        is unbounded.
31
      */
32
      ASN1_Formatter(bool print_context_specific, size_t max_depth) :
33
         m_print_context_specific(print_context_specific),
34
         m_max_depth(max_depth)
35
2.83k
         {}
36
37
      void print_to_stream(std::ostream& out,
38
                           const uint8_t in[],
39
                           size_t len) const;
40
41
      std::string print(const uint8_t in[], size_t len) const;
42
43
      template<typename Alloc>
44
      std::string print(const std::vector<uint8_t, Alloc>& vec) const
45
         {
46
         return print(vec.data(), vec.size());
47
         }
48
49
   protected:
50
      /**
51
      * This is called for each element
52
      */
53
      virtual std::string format(ASN1_Type type_tag,
54
                                 ASN1_Class class_tag,
55
                                 size_t level,
56
                                 size_t length,
57
                                 const std::string& value) const = 0;
58
59
      /**
60
      * This is called to format binary elements that we don't know how to
61
      * convert to a string The result will be passed as value to format; the
62
      * tags are included as a hint to aid decoding.
63
      */
64
      virtual std::string format_bin(ASN1_Type type_tag,
65
                                     ASN1_Class class_tag,
66
                                     const std::vector<uint8_t>& vec) const = 0;
67
68
   private:
69
      void decode(std::ostream& output,
70
                  BER_Decoder& decoder,
71
                  size_t level) const;
72
73
      const bool m_print_context_specific;
74
      const size_t m_max_depth;
75
   };
76
77
/**
78
* Format ASN.1 data into human readable output. The exact form of the output for
79
* any particular input is not guaranteed and may change from release to release.
80
*/
81
class BOTAN_PUBLIC_API(2,4) ASN1_Pretty_Printer final : public ASN1_Formatter
82
   {
83
   public:
84
      /**
85
      * @param print_limit strings larger than this are not printed
86
      * @param print_binary_limit binary strings larger than this are not printed
87
      * @param print_context_specific if true, try to parse nested context specific data.
88
      * @param initial_level the initial depth (0 or 1 are the only reasonable values)
89
      * @param value_column ASN.1 values are lined up at this column in output
90
      * @param max_depth do not recurse more than this many times. If zero, recursion
91
      *        is unbounded.
92
      */
93
      ASN1_Pretty_Printer(size_t print_limit = 4096,
94
                          size_t print_binary_limit = 2048,
95
                          bool print_context_specific = true,
96
                          size_t initial_level = 0,
97
                          size_t value_column = 60,
98
                          size_t max_depth = 64) :
99
         ASN1_Formatter(print_context_specific, max_depth),
100
         m_print_limit(print_limit),
101
         m_print_binary_limit(print_binary_limit),
102
         m_initial_level(initial_level),
103
         m_value_column(value_column)
104
0
         {}
105
106
   private:
107
      std::string format(ASN1_Type type_tag,
108
                         ASN1_Class class_tag,
109
                         size_t level,
110
                         size_t length,
111
                         const std::string& value) const override;
112
113
      std::string format_bin(ASN1_Type type_tag,
114
                             ASN1_Class class_tag,
115
                             const std::vector<uint8_t>& vec) const override;
116
117
      const size_t m_print_limit;
118
      const size_t m_print_binary_limit;
119
      const size_t m_initial_level;
120
      const size_t m_value_column;
121
   };
122
123
}
124
125
#endif