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