Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

asn1_obj.cpp
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 #include <botan/asn1_obj.h>
9 #include <botan/der_enc.h>
10 #include <botan/data_src.h>
11 #include <botan/internal/stl_util.h>
12 #include <sstream>
13
14 namespace Botan {
15
16 std::vector<uint8_t> ASN1_Object::BER_encode() const
17 {
18 std::vector<uint8_t> output;
19 DER_Encoder der(output);
20 this->encode_into(der);
21 return output;
22 }
23
24 /*
25 * Check a type invariant on BER data
26 */
27 void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_,
28 const std::string& descr) const
29 {
30 if(this->is_a(type_tag_, class_tag_) == false)
31 {
32 std::stringstream msg;
33
34 msg << "Tag mismatch when decoding " << descr << " got ";
35
36 if(class_tag == NO_OBJECT && type_tag == NO_OBJECT)
37 {
38 msg << "EOF";
39 }
40 else
41 {
42 if(class_tag == UNIVERSAL || class_tag == CONSTRUCTED)
43 {
44 msg << asn1_tag_to_string(type_tag);
45 }
46 else
47 {
48 msg << std::to_string(type_tag);
49 }
50
51 msg << "/" << asn1_class_to_string(class_tag);
52 }
53
54 msg << " expected ";
55
56 if(class_tag_ == UNIVERSAL || class_tag_ == CONSTRUCTED)
57 {
58 msg << asn1_tag_to_string(type_tag_);
59 }
60 else
61 {
62 msg << std::to_string(type_tag_);
63 }
64
65 msg << "/" << asn1_class_to_string(class_tag_);
66
67 throw BER_Decoding_Error(msg.str());
68 }
69 }
70
71 bool BER_Object::is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) const
72 {
73 return (type_tag == type_tag_ && class_tag == class_tag_);
74 }
75
76 bool BER_Object::is_a(int type_tag_, ASN1_Tag class_tag_) const
77 {
78 return is_a(ASN1_Tag(type_tag_), class_tag_);
79 }
80
81 void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c)
82 {
83 type_tag = t;
84 class_tag = c;
85 }
86
87 std::string asn1_class_to_string(ASN1_Tag type)
88 {
89 switch(type)
90 {
91 case UNIVERSAL:
92 return "UNIVERSAL";
93 case CONSTRUCTED:
94 return "CONSTRUCTED";
95 case CONTEXT_SPECIFIC:
96 return "CONTEXT_SPECIFIC";
97 case APPLICATION:
98 return "APPLICATION";
99 case CONSTRUCTED | CONTEXT_SPECIFIC:
100 return "PRIVATE";
101 case Botan::NO_OBJECT:
102 return "NO_OBJECT";
103 default:
104 return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
105 }
106 }
107
108 std::string asn1_tag_to_string(ASN1_Tag type)
109 {
110 switch(type)
111 {
112 case Botan::SEQUENCE:
113 return "SEQUENCE";
114
115 case Botan::SET:
116 return "SET";
117
118 case Botan::PRINTABLE_STRING:
119 return "PRINTABLE STRING";
120
121 case Botan::NUMERIC_STRING:
122 return "NUMERIC STRING";
123
124 case Botan::IA5_STRING:
125 return "IA5 STRING";
126
127 case Botan::T61_STRING:
128 return "T61 STRING";
129
130 case Botan::UTF8_STRING:
131 return "UTF8 STRING";
132
133 case Botan::VISIBLE_STRING:
134 return "VISIBLE STRING";
135
136 case Botan::BMP_STRING:
137 return "BMP STRING";
138
139 case Botan::UNIVERSAL_STRING:
140 return "UNIVERSAL STRING";
141
142 case Botan::UTC_TIME:
143 return "UTC TIME";
144
145 case Botan::GENERALIZED_TIME:
146 return "GENERALIZED TIME";
147
148 case Botan::OCTET_STRING:
149 return "OCTET STRING";
150
151 case Botan::BIT_STRING:
152 return "BIT STRING";
153
154 case Botan::ENUMERATED:
155 return "ENUMERATED";
156
157 case Botan::INTEGER:
158 return "INTEGER";
159
160 case Botan::NULL_TAG:
161 return "NULL";
162
163 case Botan::OBJECT_ID:
164 return "OBJECT";
165
166 case Botan::BOOLEAN:
167 return "BOOLEAN";
168
169 case Botan::NO_OBJECT:
170 return "NO_OBJECT";
171
172 default:
173 return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")";
174 }
175 }
176
177 /*
178 * BER Decoding Exceptions
179 */
180 BER_Decoding_Error::BER_Decoding_Error(const std::string& str) :
181 Decoding_Error("BER: " + str) {}
182
183 BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
184 BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
185
186 BER_Bad_Tag::BER_Bad_Tag(const std::string& str,
187 ASN1_Tag tag1, ASN1_Tag tag2) :
188 BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {}
189
190 namespace ASN1 {
191
192 /*
193 * Put some arbitrary bytes into a SEQUENCE
194 */
195 std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
196 {
197 return ASN1::put_in_sequence(contents.data(), contents.size());
198 }
199
200 std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
201 {
202 std::vector<uint8_t> output;
203 DER_Encoder(output)
204 .start_cons(SEQUENCE)
205 .raw_bytes(bits, len)
206 .end_cons();
207 return output;
208 }
209
210 /*
211 * Convert a BER object into a string object
212 */
213 std::string to_string(const BER_Object& obj)
214 {
215 return std::string(cast_uint8_ptr_to_char(obj.bits()),
216 obj.length());
217 }
218
219 /*
220 * Do heuristic tests for BER data
221 */
222 bool maybe_BER(DataSource& source)
223 {
224 uint8_t first_u8;
225 if(!source.peek_byte(first_u8))
226 {
227 BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
228 throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
229 }
230
231 if(first_u8 == (SEQUENCE | CONSTRUCTED))
232 return true;
233 return false;
234 }
235
236 }
237
238 }
Botan::asn1_tag_to_string
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:108
Botan::asn1_class_to_string
std::string asn1_class_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:87
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
Botan::BOOLEAN
Definition: asn1_obj.h:33
Botan::VISIBLE_STRING
Definition: asn1_obj.h:48
std
Definition: bigint.h:1135
Botan::CONSTRUCTED
Definition: asn1_obj.h:28
Botan::ASN1::maybe_BER
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:222
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::DER_Encoder::end_cons
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
Botan::BER_Object::is_a
bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const
Definition: asn1_obj.cpp:71
Botan::DataSource
Definition: data_src.h:21
Botan::DER_Encoder::raw_bytes
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:228
Botan::BER_Object::assert_is_a
void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string &descr="object") const
Definition: asn1_obj.cpp:27
Botan::to_string
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
Botan::Decoding_Error
Definition: exceptn.h:199
Botan::BER_Object::bits
const uint8_t * bits() const
Definition: asn1_obj.h:117
Botan::SEQUENCE
Definition: asn1_obj.h:40
Botan::DER_Encoder
Definition: der_enc.h:23
Botan::Stream_IO_Error
Definition: exceptn.h:306
Botan::BMP_STRING
Definition: asn1_obj.h:50
Botan::BER_Object::class_tag
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:134
Botan::UNIVERSAL
Definition: asn1_obj.h:24
BOTAN_ASSERT_EQUAL
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:81
Botan::SET
Definition: asn1_obj.h:41
Botan::ASN1_Object::BER_encode
std::vector< uint8_t > BER_encode() const
Definition: asn1_obj.cpp:16
Botan::OBJECT_ID
Definition: asn1_obj.h:38
Botan::BER_Bad_Tag::BER_Bad_Tag
BER_Bad_Tag(const std::string &msg, ASN1_Tag tag)
Definition: asn1_obj.cpp:183
Botan::IA5_STRING
Definition: asn1_obj.h:47
Botan::GENERALIZED_TIME
Definition: asn1_obj.h:53
Botan
Definition: alg_id.cpp:13
Botan::DataSource::read_byte
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23
Botan::UTF8_STRING
Definition: asn1_obj.h:43
Botan::OCTET_STRING
Definition: asn1_obj.h:36
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::APPLICATION
Definition: asn1_obj.h:25
Botan::NO_OBJECT
Definition: asn1_obj.h:56
Botan::ASN1_Object::encode_into
virtual void encode_into(DER_Encoder &to) const =0
Botan::BER_Decoding_Error
Definition: asn1_obj.h:172
Botan::DataSource::peek_byte
size_t peek_byte(uint8_t &out) const
Definition: data_src.cpp:31
Botan::cast_uint8_ptr_to_char
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:194
Botan::BIT_STRING
Definition: asn1_obj.h:35
Botan::DER_Encoder::start_cons
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
Botan::PRINTABLE_STRING
Definition: asn1_obj.h:45
Botan::CONTEXT_SPECIFIC
Definition: asn1_obj.h:26
Botan::BER_Decoding_Error::BER_Decoding_Error
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:180
Botan::BER_Object
Definition: asn1_obj.h:97
Botan::T61_STRING
Definition: asn1_obj.h:46
Botan::UTC_TIME
Definition: asn1_obj.h:52