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