Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

asn1_obj.h
Go to the documentation of this file.
1 /*
2 * ASN.1 Internals
3 * (C) 1999-2007,2018 Jack Lloyd
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 */
7
8 #ifndef BOTAN_ASN1_H_
9 #define BOTAN_ASN1_H_
10
11 #include <botan/secmem.h>
12 #include <botan/exceptn.h>
13
14 namespace Botan {
15
16 class BER_Decoder;
17 class DER_Encoder;
18
19 /**
20 * ASN.1 Type and Class Tags
21 * This will become an enum class in a future major release
22 */
23 enum ASN1_Tag : uint32_t {
24 UNIVERSAL = 0x00,
25 APPLICATION = 0x40,
26 CONTEXT_SPECIFIC = 0x80,
27
28 CONSTRUCTED = 0x20,
29
30 PRIVATE = CONSTRUCTED | CONTEXT_SPECIFIC,
31
32 EOC = 0x00,
33 BOOLEAN = 0x01,
34 INTEGER = 0x02,
35 BIT_STRING = 0x03,
36 OCTET_STRING = 0x04,
37 NULL_TAG = 0x05,
38 OBJECT_ID = 0x06,
39 ENUMERATED = 0x0A,
40 SEQUENCE = 0x10,
41 SET = 0x11,
42
43 UTF8_STRING = 0x0C,
44 NUMERIC_STRING = 0x12,
45 PRINTABLE_STRING = 0x13,
46 T61_STRING = 0x14,
47 IA5_STRING = 0x16,
48 VISIBLE_STRING = 0x1A,
49 UNIVERSAL_STRING = 0x1C,
50 BMP_STRING = 0x1E,
51
52 UTC_TIME = 0x17,
53 GENERALIZED_TIME = 0x18,
54 UTC_OR_GENERALIZED_TIME = 0x19,
55
56 NO_OBJECT = 0xFF00,
57 DIRECTORY_STRING = 0xFF01
58 };
59
60 std::string BOTAN_UNSTABLE_API asn1_tag_to_string(ASN1_Tag type);
61 std::string BOTAN_UNSTABLE_API asn1_class_to_string(ASN1_Tag type);
62
63 /**
64 * Basic ASN.1 Object Interface
65 */
66 class BOTAN_PUBLIC_API(2,0) ASN1_Object
67 {
68 public:
69 /**
70 * Encode whatever this object is into to
71 * @param to the DER_Encoder that will be written to
72 */
73 virtual void encode_into(DER_Encoder& to) const = 0;
74
75 /**
76 * Decode whatever this object is from from
77 * @param from the BER_Decoder that will be read from
78 */
79 virtual void decode_from(BER_Decoder& from) = 0;
80
81 /**
82 * Return the encoding of this object. This is a convenience
83 * method when just one object needs to be serialized. Use
84 * DER_Encoder for complicated encodings.
85 */
86 std::vector<uint8_t> BER_encode() const;
87
88 ASN1_Object() = default;
89 ASN1_Object(const ASN1_Object&) = default;
90 ASN1_Object & operator=(const ASN1_Object&) = default;
91 virtual ~ASN1_Object() = default;
92 };
93
94 /**
95 * BER Encoded Object
96 */
97 class BOTAN_PUBLIC_API(2,0) BER_Object final
98 {
99 public:
100 BER_Object() : type_tag(NO_OBJECT), class_tag(UNIVERSAL) {}
101
102 BER_Object(const BER_Object& other) = default;
103
104 BER_Object& operator=(const BER_Object& other) = default;
105
106 BER_Object(BER_Object&& other) = default;
107
108 BER_Object& operator=(BER_Object&& other) = default;
109
110 bool is_set() const { return type_tag != NO_OBJECT; }
111
112 ASN1_Tag tagging() const { return ASN1_Tag(type() | get_class()); }
113
114 ASN1_Tag type() const { return type_tag; }
115 ASN1_Tag get_class() const { return class_tag; }
116
117 const uint8_t* bits() const { return value.data(); }
118
119 size_t length() const { return value.size(); }
120
121 void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag,
122 const std::string& descr = "object") const;
123
124 bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const;
125
126 bool is_a(int type_tag, ASN1_Tag class_tag) const;
127
128 BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES:
129 /*
130 * The following member variables are public for historical reasons, but
131 * will be made private in a future major release. Use the accessor
132 * functions above.
133 */
134 ASN1_Tag type_tag, class_tag;
135 secure_vector<uint8_t> value;
136
137 private:
138
139 friend class BER_Decoder;
140
141 void set_tagging(ASN1_Tag type_tag, ASN1_Tag class_tag);
142
143 uint8_t* mutable_bits(size_t length)
144 {
145 value.resize(length);
146 return value.data();
147 }
148 };
149
150 /*
151 * ASN.1 Utility Functions
152 */
153 class DataSource;
154
155 namespace ASN1 {
156
157 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& val);
158 std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len);
159 std::string to_string(const BER_Object& obj);
160
161 /**
162 * Heuristics tests; is this object possibly BER?
163 * @param src a data source that will be peeked at but not modified
164 */
165 bool maybe_BER(DataSource& src);
166
167 }
168
169 /**
170 * General BER Decoding Error Exception
171 */
172 class BOTAN_PUBLIC_API(2,0) BER_Decoding_Error : public Decoding_Error
173 {
174 public:
175 explicit BER_Decoding_Error(const std::string&);
176 };
177
178 /**
179 * Exception For Incorrect BER Taggings
180 */
181 class BOTAN_PUBLIC_API(2,0) BER_Bad_Tag final : public BER_Decoding_Error
182 {
183 public:
184 BER_Bad_Tag(const std::string& msg, ASN1_Tag tag);
185 BER_Bad_Tag(const std::string& msg, ASN1_Tag tag1, ASN1_Tag tag2);
186 };
187
188 }
189
190 #endif
Botan::asn1_tag_to_string
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:108
BOTAN_UNSTABLE_API
#define BOTAN_UNSTABLE_API
Definition: compiler.h:38
Botan::asn1_class_to_string
std::string asn1_class_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:87
Botan::ASN1_Object
Definition: asn1_obj.h:66
Botan::BER_Object::type
ASN1_Tag type() const
Definition: asn1_obj.h:114
Botan::NUMERIC_STRING
Definition: asn1_obj.h:44
Botan::BER_Object::length
size_t length() const
Definition: asn1_obj.h:119
Botan::UNIVERSAL_STRING
Definition: asn1_obj.h:49
Botan::INTEGER
Definition: asn1_obj.h:34
final
int(* final)(unsigned char *, CTX *)
Definition: commoncrypto_hash.cpp:28
BOTAN_PUBLIC_API
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:31
Botan::BOOLEAN
Definition: asn1_obj.h:33
Botan::VISIBLE_STRING
Definition: asn1_obj.h:48
Botan::CONSTRUCTED
Definition: asn1_obj.h:28
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:222
Botan::PRIVATE
Definition: asn1_obj.h:30
Botan::ASN1::to_string
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Botan::ASN1_Tag
ASN1_Tag
Definition: asn1_obj.h:23
type
MechanismType type
Definition: p11_mechanism.cpp:44
Botan::ENUMERATED
Definition: asn1_obj.h:39
Botan::Decoding_Error
Definition: exceptn.h:199
Botan::BER_Object::bits
const uint8_t * bits() const
Definition: asn1_obj.h:117
Botan::BER_Object::tagging
ASN1_Tag tagging() const
Definition: asn1_obj.h:112
Botan::SEQUENCE
Definition: asn1_obj.h:40
Botan::DER_Encoder
Definition: der_enc.h:23
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::BMP_STRING
Definition: asn1_obj.h:50
Botan::BER_Object::value
secure_vector< uint8_t > value
Definition: asn1_obj.h:135
Botan::BER_Object::class_tag
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:134
Botan::BER_Object::get_class
ASN1_Tag get_class() const
Definition: asn1_obj.h:115
Botan::UNIVERSAL
Definition: asn1_obj.h:24
Botan::SET
Definition: asn1_obj.h:41
Botan::OBJECT_ID
Definition: asn1_obj.h:38
Botan::IA5_STRING
Definition: asn1_obj.h:47
Botan::GENERALIZED_TIME
Definition: asn1_obj.h:53
Botan
Definition: alg_id.cpp:13
Botan::PKCS8::BER_encode
secure_vector< uint8_t > BER_encode(const Private_Key &key)
Definition: pkcs8.cpp:139
Botan::UTF8_STRING
Definition: asn1_obj.h:43
Botan::OCTET_STRING
Definition: asn1_obj.h:36
Botan::EOC
Definition: asn1_obj.h:32
Botan::ASN1::put_in_sequence
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:195
Botan::NULL_TAG
Definition: asn1_obj.h:37
Botan::BER_Object::BER_Object
BER_Object()
Definition: asn1_obj.h:100
Botan::BER_Object::is_set
bool is_set() const
Definition: asn1_obj.h:110
Botan::APPLICATION
Definition: asn1_obj.h:25
Botan::NO_OBJECT
Definition: asn1_obj.h:56
Botan::BER_Decoding_Error
Definition: asn1_obj.h:172
Botan::BER_Bad_Tag
Definition: asn1_obj.h:181
Botan::BIT_STRING
Definition: asn1_obj.h:35
Botan::PRINTABLE_STRING
Definition: asn1_obj.h:45
Botan::CONTEXT_SPECIFIC
Definition: asn1_obj.h:26
Botan::DIRECTORY_STRING
Definition: asn1_obj.h:57
Botan::UTC_OR_GENERALIZED_TIME
Definition: asn1_obj.h:54
Botan::BER_Decoder
Definition: ber_dec.h:21
Botan::BER_Object
Definition: asn1_obj.h:97
Botan::T61_STRING
Definition: asn1_obj.h:46
Botan::UTC_TIME
Definition: asn1_obj.h:52