Botan  2.4.0
Crypto and TLS for C++11
asn1_print.h
Go to the documentation of this file.
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_DLL ASN1_Formatter
23  {
24  public:
25  virtual ~ASN1_Formatter() = default;
26 
27  /**
28  * @param print_context_specific if true, try to parse nested context specific data.
29  */
30  ASN1_Formatter(bool print_context_specific) :
31  m_print_context_specific(print_context_specific)
32  {}
33 
34  void print_to_stream(std::ostream& out,
35  const uint8_t in[],
36  size_t len) const;
37 
38  std::string print(const uint8_t in[], size_t len) const;
39 
40  template<typename Alloc>
41  std::string print(const std::vector<uint8_t, Alloc>& vec) const
42  {
43  return print(vec.data(), vec.size());
44  }
45 
46  protected:
47  /**
48  * This is called for each element
49  */
50  virtual std::string format(ASN1_Tag type_tag,
51  ASN1_Tag class_tag,
52  size_t level,
53  size_t length,
54  const std::string& value) const = 0;
55 
56  /**
57  * This is called to format binary elements that we don't know how to
58  * convert to a string The result will be passed as value to format; the
59  * tags are included as a hint to aid decoding.
60  */
61  virtual std::string format_bin(ASN1_Tag type_tag,
62  ASN1_Tag class_tag,
63  const std::vector<uint8_t>& vec) const = 0;
64 
65  private:
66  void decode(std::ostream& output,
67  BER_Decoder& decoder,
68  size_t level) const;
69 
70  const bool m_print_context_specific;
71  };
72 
73 /**
74 * Format ASN.1 data into human readable output. The exact form of the output for
75 * any particular input is not guaranteed and may change from release to release.
76 */
77 class BOTAN_DLL ASN1_Pretty_Printer final : public ASN1_Formatter
78  {
79  public:
80  /**
81  * @param print_limit strings larger than this are not printed
82  * @param print_binary_limit binary strings larger than this are not printed
83  * @param print_context_specific if true, try to parse nested context specific data.
84  * @param initial_level the initial depth (0 or 1 are the only reasonable values)
85  * @param value_column ASN.1 values are lined up at this column in output
86  */
87  ASN1_Pretty_Printer(size_t print_limit = 4096,
88  size_t print_binary_limit = 2048,
89  bool print_context_specific = true,
90  size_t initial_level = 0,
91  size_t value_column = 60) :
92  ASN1_Formatter(print_context_specific),
93  m_print_limit(print_limit),
94  m_print_binary_limit(print_binary_limit),
95  m_initial_level(initial_level),
96  m_value_column(value_column)
97  {}
98 
99  private:
100  std::string format(ASN1_Tag type_tag,
101  ASN1_Tag class_tag,
102  size_t level,
103  size_t length,
104  const std::string& value) const override;
105 
106  std::string format_bin(ASN1_Tag type_tag,
107  ASN1_Tag class_tag,
108  const std::vector<uint8_t>& vec) const override;
109 
110  const size_t m_print_limit;
111  const size_t m_print_binary_limit;
112  const size_t m_initial_level;
113  const size_t m_value_column;
114  };
115 
116 }
117 
118 #endif
ASN1_Tag
Definition: asn1_obj.h:22
ASN1_Pretty_Printer(size_t print_limit=4096, size_t print_binary_limit=2048, bool print_context_specific=true, size_t initial_level=0, size_t value_column=60)
Definition: asn1_print.h:87
ASN1_Formatter(bool print_context_specific)
Definition: asn1_print.h:30
secure_vector< uint8_t > decode(DataSource &source, std::string &label)
Definition: pem.cpp:68
Definition: alg_id.cpp:13
std::string print(const std::vector< uint8_t, Alloc > &vec) const
Definition: asn1_print.h:41