Botan 3.13.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
10#include <botan/assert.h>
11#include <botan/data_src.h>
12#include <botan/der_enc.h>
13#include <botan/mem_ops.h>
14#include <botan/internal/fmt.h>
15#include <botan/internal/mem_utils.h>
16#include <sstream>
17#include <utility>
18
19namespace Botan {
20
21std::vector<uint8_t> ASN1_Object::BER_encode() const {
22 std::vector<uint8_t> output;
23 DER_Encoder der(output);
24 this->encode_into(der);
25 return output;
26}
27
28ASN1_BitString::ASN1_BitString(std::vector<uint8_t> bytes, size_t unused_bits) :
29 m_bytes(std::move(bytes)), m_unused_bits(unused_bits) {
30 if(m_unused_bits >= 8) {
31 throw Invalid_Argument("ASN1_BitString: Invalid unused bit count");
32 }
33
34 if(m_bytes.empty() && m_unused_bits != 0) {
35 throw Invalid_Argument("ASN1_BitString: Empty BIT STRING cannot have unused bits");
36 }
37
38 if(m_unused_bits > 0 && (m_bytes.back() & ((1U << m_unused_bits) - 1)) != 0) {
39 throw Invalid_Argument("ASN1_BitString: Unused bits must be zero");
40 }
41}
42
43ASN1_BitString::ASN1_BitString(std::span<const uint8_t> bytes, size_t unused_bits) :
44 ASN1_BitString(std::vector<uint8_t>(bytes.begin(), bytes.end()), unused_bits) {}
45
47 return 8 * m_bytes.size() - m_unused_bits;
48}
49
50bool ASN1_BitString::bit_at(size_t bit) const {
51 if(bit >= bit_length()) {
52 throw Invalid_Argument("ASN1_BitString: Bit index out of range");
53 }
54
55 return (m_bytes[bit / 8] & (0x80 >> (bit % 8))) != 0;
56}
57
61
62/*
63* Check a type invariant on BER data
64*/
65void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
66 if(!this->is_a(expected_type_tag, expected_class_tag)) {
67 std::stringstream msg;
68
69 msg << "Tag mismatch when decoding " << descr << " got ";
70
71 if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
72 msg << "EOF";
73 } else {
74 if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
75 msg << asn1_tag_to_string(m_type_tag);
76 } else {
77 msg << std::to_string(static_cast<uint32_t>(m_type_tag));
78 }
79
80 msg << "/" << asn1_class_to_string(m_class_tag);
81 }
82
83 msg << " expected ";
84
85 if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
86 msg << asn1_tag_to_string(expected_type_tag);
87 } else {
88 msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
89 }
90
91 msg << "/" << asn1_class_to_string(expected_class_tag);
92
93 throw BER_Decoding_Error(msg.str());
94 }
95}
96
97bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const {
98 return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
99}
100
101bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
102 return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
103}
104
105void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
106 m_type_tag = type_tag;
107 m_class_tag = class_tag;
108}
109
111 switch(type) {
113 return "UNIVERSAL";
115 return "CONSTRUCTED";
117 return "CONTEXT_SPECIFIC";
119 return "APPLICATION";
121 return "PRIVATE";
123 return "NO_OBJECT";
124 default:
125 return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
126 }
127}
128
129std::string asn1_tag_to_string(ASN1_Type type) {
130 switch(type) {
132 return "SEQUENCE";
133
134 case ASN1_Type::Set:
135 return "SET";
136
138 return "PRINTABLE STRING";
139
141 return "NUMERIC STRING";
142
144 return "IA5 STRING";
145
147 return "T61 STRING";
148
150 return "UTF8 STRING";
151
153 return "VISIBLE STRING";
154
156 return "BMP STRING";
157
159 return "UNIVERSAL STRING";
160
162 return "UTC TIME";
163
165 return "GENERALIZED TIME";
166
168 return "OCTET STRING";
169
171 return "BIT STRING";
172
174 return "ENUMERATED";
175
177 return "INTEGER";
178
179 case ASN1_Type::Null:
180 return "NULL";
181
183 return "OBJECT";
184
186 return "BOOLEAN";
187
189 return "NO_OBJECT";
190
191 default:
192 return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
193 }
194}
195
196/*
197* BER Decoding Exceptions
198*/
199BER_Decoding_Error::BER_Decoding_Error(std::string_view err) : Decoding_Error(fmt("BER: {}", err)) {}
200
201BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
202
203namespace ASN1 {
204
205/*
206* Put some arbitrary bytes into a SEQUENCE
207*/
208std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
209 return ASN1::put_in_sequence(contents.data(), contents.size());
210}
211
212std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
213 std::vector<uint8_t> output = der_sequence_header(len);
214 output.reserve(output.size() + len);
215 if(len > 0) {
216 output.insert(output.end(), bits, bits + len);
217 }
218 return output;
219}
220
221/*
222* Convert a BER object into a string object
223*/
224std::string to_string(const BER_Object& obj) {
225 return bytes_to_string(obj.data());
226}
227
228/*
229* Do heuristic tests for BER data
230*/
231bool maybe_BER(DataSource& source) {
232 uint8_t first_u8 = 0;
233 if(source.peek_byte(first_u8) == 0) {
234 BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
235 throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
236 }
237
238 const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
239 return first_u8 == cons_seq;
240}
241
242} // namespace ASN1
243
244} // namespace Botan
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition assert.h:88
bool bit_at(size_t bit) const
Definition asn1_obj.cpp:50
size_t unused_bits() const
Definition asn1_obj.h:211
std::span< const uint8_t > bytes() const
Definition asn1_obj.h:206
size_t bit_length() const
Definition asn1_obj.cpp:46
virtual void encode_into(DER_Encoder &to) const =0
std::vector< uint8_t > BER_encode() const
Definition asn1_obj.cpp:21
BER_Bad_Tag(std::string_view msg, uint32_t tagging)
Definition asn1_obj.cpp:201
BER_Decoding_Error(std::string_view err)
Definition asn1_obj.cpp:199
bool is_a(ASN1_Type type_tag, ASN1_Class class_tag) const
Definition asn1_obj.cpp:97
ASN1_Type type_tag() const
Definition asn1_obj.h:277
void assert_is_a(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view descr="object") const
Definition asn1_obj.cpp:65
std::span< const uint8_t > data() const
Definition asn1_obj.h:308
ASN1_Class class_tag() const
Definition asn1_obj.h:282
size_t peek_byte(uint8_t &out) const
Definition data_src.cpp:46
size_t read_byte(uint8_t &out)
Definition data_src.cpp:27
Decoding_Error(std::string_view name)
Definition exceptn.cpp:125
std::vector< uint8_t > put_in_sequence(const std::vector< uint8_t > &contents)
Definition asn1_obj.cpp:208
std::vector< uint8_t > der_sequence_header(size_t contents_len)
Definition der_enc.cpp:71
bool maybe_BER(DataSource &source)
Definition asn1_obj.cpp:231
std::string to_string(const BER_Object &obj)
Definition asn1_obj.cpp:224
std::string asn1_tag_to_string(ASN1_Type type)
Definition asn1_obj.cpp:129
ASN1_Class
Definition asn1_obj.h:32
std::string fmt(std::string_view format, const T &... args)
Definition fmt.h:53
ASN1_Type
Definition asn1_obj.h:47
std::string bytes_to_string(std::span< const uint8_t > bytes)
Definition mem_utils.h:76
void secure_scrub_memory(void *ptr, size_t n)
Definition mem_utils.cpp:25
std::string asn1_class_to_string(ASN1_Class type)
Definition asn1_obj.cpp:110