Botan 3.0.0-alpha0
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 <sstream>
13
14namespace Botan {
15
16std::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*/
27void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag,
28 const std::string& descr) const
29 {
30 if(this->is_a(expected_type_tag, expected_class_tag) == false)
31 {
32 std::stringstream msg;
33
34 msg << "Tag mismatch when decoding " << descr << " got ";
35
36 if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject)
37 {
38 msg << "EOF";
39 }
40 else
41 {
42 if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed)
43 {
44 msg << asn1_tag_to_string(m_type_tag);
45 }
46 else
47 {
48 msg << std::to_string(static_cast<uint32_t>(m_type_tag));
49 }
50
51 msg << "/" << asn1_class_to_string(m_class_tag);
52 }
53
54 msg << " expected ";
55
56 if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed)
57 {
58 msg << asn1_tag_to_string(expected_type_tag);
59 }
60 else
61 {
62 msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
63 }
64
65 msg << "/" << asn1_class_to_string(expected_class_tag);
66
67 throw BER_Decoding_Error(msg.str());
68 }
69 }
70
71bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const
72 {
73 return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
74 }
75
76bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const
77 {
78 return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
79 }
80
81void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag)
82 {
83 m_type_tag = type_tag;
84 m_class_tag = class_tag;
85 }
86
88 {
89 switch(type)
90 {
92 return "UNIVERSAL";
94 return "CONSTRUCTED";
96 return "CONTEXT_SPECIFIC";
98 return "APPLICATION";
100 return "PRIVATE";
102 return "NO_OBJECT";
103 default:
104 return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
105 }
106 }
107
109 {
110 switch(type)
111 {
113 return "SEQUENCE";
114
115 case ASN1_Type::Set:
116 return "SET";
117
119 return "PRINTABLE STRING";
120
122 return "NUMERIC STRING";
123
125 return "IA5 STRING";
126
128 return "T61 STRING";
129
131 return "UTF8 STRING";
132
134 return "VISIBLE STRING";
135
137 return "BMP STRING";
138
140 return "UNIVERSAL STRING";
141
143 return "UTC TIME";
144
146 return "GENERALIZED TIME";
147
149 return "OCTET STRING";
150
152 return "BIT STRING";
153
155 return "ENUMERATED";
156
158 return "INTEGER";
159
160 case ASN1_Type::Null:
161 return "NULL";
162
164 return "OBJECT";
165
167 return "BOOLEAN";
168
170 return "NO_OBJECT";
171
172 default:
173 return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
174 }
175 }
176
177/*
178* BER Decoding Exceptions
179*/
181 Decoding_Error("BER: " + str) {}
182
183BER_Bad_Tag::BER_Bad_Tag(const std::string& str, uint32_t tagging) :
184 BER_Decoding_Error(str + ": " + std::to_string(tagging)) {}
185
186namespace ASN1 {
187
188/*
189* Put some arbitrary bytes into a SEQUENCE
190*/
191std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents)
192 {
193 return ASN1::put_in_sequence(contents.data(), contents.size());
194 }
195
196std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
197 {
198 std::vector<uint8_t> output;
199 DER_Encoder(output)
201 .raw_bytes(bits, len)
202 .end_cons();
203 return output;
204 }
205
206/*
207* Convert a BER object into a string object
208*/
209std::string to_string(const BER_Object& obj)
210 {
211 return std::string(cast_uint8_ptr_to_char(obj.bits()),
212 obj.length());
213 }
214
215/*
216* Do heuristic tests for BER data
217*/
219 {
220 uint8_t first_u8;
221 if(!source.peek_byte(first_u8))
222 {
223 BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
224 throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
225 }
226
227 const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
228 if(first_u8 == cons_seq)
229 return true;
230 return false;
231 }
232
233}
234
235}
#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:16
BER_Bad_Tag(const std::string &msg, uint32_t tagging)
Definition: asn1_obj.cpp:183
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:180
size_t length() const
Definition: asn1_obj.h:153
const uint8_t * bits() const
Definition: asn1_obj.h:151
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, const std::string &descr="object") const
Definition: asn1_obj.cpp:27
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition: asn1_obj.cpp:71
ASN1_Type type_tag() const
Definition: asn1_obj.h:145
ASN1_Class class_tag() const
Definition: asn1_obj.h:146
DER_Encoder & start_sequence()
Definition: der_enc.h:66
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:231
DER_Encoder & end_cons()
Definition: der_enc.cpp:194
size_t peek_byte(uint8_t &out) const
Definition: data_src.cpp:31
size_t read_byte(uint8_t &out)
Definition: data_src.cpp:23
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition: asn1_obj.cpp:191
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:218
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:209
Definition: alg_id.cpp:13
std::string asn1_tag_to_string(ASN1_Type type)
Definition: asn1_obj.cpp:108
ASN1_Class
Definition: asn1_obj.h:24
ASN1_Type
Definition: asn1_obj.h:39
std::string asn1_class_to_string(ASN1_Class type)
Definition: asn1_obj.cpp:87
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:188
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
Definition: bigint.h:1077
MechanismType type