aboutsummaryrefslogtreecommitdiffstats
path: root/src/lib/asn1/asn1_print.h
diff options
context:
space:
mode:
authorJack Lloyd <[email protected]>2017-12-28 08:03:31 -0500
committerJack Lloyd <[email protected]>2017-12-28 08:03:31 -0500
commit4c1ffbe1dc46dd0844c4674791bb8cc430a41a8e (patch)
treec9e38341af545ff1d7da4aa3c60530249bb646a9 /src/lib/asn1/asn1_print.h
parentdb23059fdb4e6127a75bd9c52dab722d52ff09d6 (diff)
Refactor ASN1_Pretty_Printer
Now the base class ASN1_Formatter parses the data and calls virtuals to format. This allows custom formatting, or in the case of the fuzzer skipping the overhead of formatting entirely.
Diffstat (limited to 'src/lib/asn1/asn1_print.h')
-rw-r--r--src/lib/asn1/asn1_print.h86
1 files changed, 63 insertions, 23 deletions
diff --git a/src/lib/asn1/asn1_print.h b/src/lib/asn1/asn1_print.h
index 0fd760e89..4f9fa8f4d 100644
--- a/src/lib/asn1/asn1_print.h
+++ b/src/lib/asn1/asn1_print.h
@@ -7,7 +7,7 @@
#ifndef BOTAN_ASN1_PRINT_H_
#define BOTAN_ASN1_PRINT_H_
-#include <botan/types.h>
+#include <botan/asn1_obj.h>
#include <string>
#include <vector>
#include <iosfwd>
@@ -17,27 +17,17 @@ namespace Botan {
class BER_Decoder;
/**
-* Format ASN.1 data into human readable strings
+* Format ASN.1 data and call a virtual to format
*/
-class BOTAN_DLL ASN1_Pretty_Printer
+class BOTAN_DLL ASN1_Formatter
{
public:
+ virtual ~ASN1_Formatter() = default;
+
/**
- * @param print_limit strings larger than this are not printed
- * @param print_binary_limit binary strings larger than this are not printed
* @param print_context_specific if true, try to parse nested context specific data.
- * @param initial_level the initial depth (0 or 1 are the only reasonable values)
- * @param value_column ASN.1 values are lined up at this column in output
*/
- ASN1_Pretty_Printer(size_t print_limit = 256,
- size_t print_binary_limit = 256,
- bool print_context_specific = true,
- size_t initial_level = 0,
- size_t value_column = 60) :
- m_print_limit(print_limit),
- m_print_binary_limit(print_binary_limit),
- m_initial_level(initial_level),
- m_value_column(value_column),
+ ASN1_Formatter(bool print_context_specific) :
m_print_context_specific(print_context_specific)
{}
@@ -53,23 +43,73 @@ class BOTAN_DLL ASN1_Pretty_Printer
return print(vec.data(), vec.size());
}
- private:
- void emit(std::ostream& out,
- const std::string& type,
- size_t level, size_t length,
- const std::string& value = "") const;
+ protected:
+ /**
+ * This is called for each element
+ */
+ virtual std::string format(ASN1_Tag type_tag,
+ ASN1_Tag class_tag,
+ size_t level,
+ size_t length,
+ const std::string& value) const = 0;
+
+ /**
+ * This is called to format binary elements that we don't know how to
+ * convert to a string The result will be passed as value to format; the
+ * tags are included as a hint to aid decoding.
+ */
+ virtual std::string format_bin(ASN1_Tag type_tag,
+ ASN1_Tag class_tag,
+ const std::vector<uint8_t>& vec) const = 0;
+ private:
void decode(std::ostream& output,
BER_Decoder& decoder,
size_t level) const;
- std::string format_binary(const std::vector<uint8_t>& in) const;
+ const bool m_print_context_specific;
+ };
+
+/**
+* Format ASN.1 data into human readable strings
+*/
+class BOTAN_DLL ASN1_Pretty_Printer final : public ASN1_Formatter
+ {
+ public:
+ /**
+ * @param print_limit strings larger than this are not printed
+ * @param print_binary_limit binary strings larger than this are not printed
+ * @param print_context_specific if true, try to parse nested context specific data.
+ * @param initial_level the initial depth (0 or 1 are the only reasonable values)
+ * @param value_column ASN.1 values are lined up at this column in output
+ */
+ ASN1_Pretty_Printer(size_t print_limit = 256,
+ size_t print_binary_limit = 256,
+ bool print_context_specific = true,
+ size_t initial_level = 0,
+ size_t value_column = 60) :
+ ASN1_Formatter(print_context_specific),
+ m_print_limit(print_limit),
+ m_print_binary_limit(print_binary_limit),
+ m_initial_level(initial_level),
+ m_value_column(value_column)
+ {}
+
+ private:
+ std::string format(ASN1_Tag type_tag,
+ ASN1_Tag class_tag,
+ size_t level,
+ size_t length,
+ const std::string& value) const override;
+
+ std::string format_bin(ASN1_Tag type_tag,
+ ASN1_Tag class_tag,
+ const std::vector<uint8_t>& vec) const override;
const size_t m_print_limit;
const size_t m_print_binary_limit;
const size_t m_initial_level;
const size_t m_value_column;
- const bool m_print_context_specific;
};
}