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