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