Botan 2.19.2
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_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 {
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
71bool 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
76bool 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
81void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c)
82 {
83 type_tag = t;
84 class_tag = c;
85 }
86
88 {
89 switch(type)
90 {
91 case UNIVERSAL:
92 return "UNIVERSAL";
93 case CONSTRUCTED:
94 return "CONSTRUCTED";
96 return "CONTEXT_SPECIFIC";
97 case APPLICATION:
98 return "APPLICATION";
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
109 {
110 switch(type)
111 {
112 case Botan::SEQUENCE:
113 return "SEQUENCE";
114
115 case Botan::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
142 case Botan::UTC_TIME:
143 return "UTC TIME";
144
146 return "GENERALIZED TIME";
147
149 return "OCTET STRING";
150
152 return "BIT STRING";
153
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*/
181 Decoding_Error("BER: " + str) {}
182
183BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) :
184 BER_Decoding_Error(str + ": " + std::to_string(tag)) {}
185
186BER_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
190namespace ASN1 {
191
192/*
193* Put some arbitrary bytes into a SEQUENCE
194*/
195std::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
200std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len)
201 {
202 std::vector<uint8_t> output;
203 DER_Encoder(output)
205 .raw_bytes(bits, len)
206 .end_cons();
207 return output;
208 }
209
210/*
211* Convert a BER object into a string object
212*/
213std::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*/
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}
#define BOTAN_ASSERT_EQUAL(expr1, expr2, assertion_made)
Definition: assert.h:81
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, ASN1_Tag tag)
Definition: asn1_obj.cpp:183
BER_Decoding_Error(const std::string &)
Definition: asn1_obj.cpp:180
size_t length() const
Definition: asn1_obj.h:121
const uint8_t * bits() const
Definition: asn1_obj.h:119
bool is_a(ASN1_Tag type_tag, ASN1_Tag class_tag) const
Definition: asn1_obj.cpp:71
BOTAN_DEPRECATED_PUBLIC_MEMBER_VARIABLES class_tag
Definition: asn1_obj.h:136
void assert_is_a(ASN1_Tag type_tag, ASN1_Tag class_tag, const std::string &descr="object") const
Definition: asn1_obj.cpp:27
DER_Encoder & start_cons(ASN1_Tag type_tag, ASN1_Tag class_tag=UNIVERSAL)
Definition: der_enc.cpp:181
DER_Encoder & raw_bytes(const uint8_t val[], size_t len)
Definition: der_enc.cpp:228
DER_Encoder & end_cons()
Definition: der_enc.cpp:191
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:195
bool maybe_BER(DataSource &source)
Definition: asn1_obj.cpp:222
std::string to_string(const BER_Object &obj)
Definition: asn1_obj.cpp:213
Definition: alg_id.cpp:13
const char * cast_uint8_ptr_to_char(const uint8_t *b)
Definition: mem_ops.h:195
ASN1_Tag
Definition: asn1_obj.h:25
@ UTC_TIME
Definition: asn1_obj.h:54
@ CONSTRUCTED
Definition: asn1_obj.h:30
@ GENERALIZED_TIME
Definition: asn1_obj.h:55
@ IA5_STRING
Definition: asn1_obj.h:49
@ UNIVERSAL_STRING
Definition: asn1_obj.h:51
@ BIT_STRING
Definition: asn1_obj.h:37
@ PRINTABLE_STRING
Definition: asn1_obj.h:47
@ T61_STRING
Definition: asn1_obj.h:48
@ UTF8_STRING
Definition: asn1_obj.h:45
@ SEQUENCE
Definition: asn1_obj.h:42
@ APPLICATION
Definition: asn1_obj.h:27
@ NUMERIC_STRING
Definition: asn1_obj.h:46
@ BOOLEAN
Definition: asn1_obj.h:35
@ CONTEXT_SPECIFIC
Definition: asn1_obj.h:28
@ BMP_STRING
Definition: asn1_obj.h:52
@ ENUMERATED
Definition: asn1_obj.h:41
@ NO_OBJECT
Definition: asn1_obj.h:58
@ OCTET_STRING
Definition: asn1_obj.h:38
@ NULL_TAG
Definition: asn1_obj.h:39
@ UNIVERSAL
Definition: asn1_obj.h:26
@ VISIBLE_STRING
Definition: asn1_obj.h:50
@ OBJECT_ID
Definition: asn1_obj.h:40
@ SET
Definition: asn1_obj.h:43
@ INTEGER
Definition: asn1_obj.h:36
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition: exceptn.cpp:11
std::string asn1_class_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:87
std::string asn1_tag_to_string(ASN1_Tag type)
Definition: asn1_obj.cpp:108
Definition: bigint.h:1143
MechanismType type