/src/botan/src/lib/asn1/asn1_obj.cpp
Line | Count | Source (jump to first uncovered line) |
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 | | |
14 | | namespace Botan { |
15 | | |
16 | | std::vector<uint8_t> ASN1_Object::BER_encode() const |
17 | 11.6k | { |
18 | 11.6k | std::vector<uint8_t> output; |
19 | 11.6k | DER_Encoder der(output); |
20 | 11.6k | this->encode_into(der); |
21 | 11.6k | return output; |
22 | 11.6k | } |
23 | | |
24 | | /* |
25 | | * Check a type invariant on BER data |
26 | | */ |
27 | | void BER_Object::assert_is_a(ASN1_Tag expected_type_tag, ASN1_Tag expected_class_tag, |
28 | | const std::string& descr) const |
29 | 733k | { |
30 | 733k | if(this->is_a(expected_type_tag, expected_class_tag) == false) |
31 | 15.5k | { |
32 | 15.5k | std::stringstream msg; |
33 | | |
34 | 15.5k | msg << "Tag mismatch when decoding " << descr << " got "; |
35 | | |
36 | 15.5k | if(m_class_tag == NO_OBJECT && m_type_tag == NO_OBJECT) |
37 | 2.05k | { |
38 | 2.05k | msg << "EOF"; |
39 | 2.05k | } |
40 | 13.4k | else |
41 | 13.4k | { |
42 | 13.4k | if(m_class_tag == UNIVERSAL || m_class_tag == CONSTRUCTED) |
43 | 8.97k | { |
44 | 8.97k | msg << asn1_tag_to_string(m_type_tag); |
45 | 8.97k | } |
46 | 4.47k | else |
47 | 4.47k | { |
48 | 4.47k | msg << std::to_string(m_type_tag); |
49 | 4.47k | } |
50 | | |
51 | 13.4k | msg << "/" << asn1_class_to_string(m_class_tag); |
52 | 13.4k | } |
53 | | |
54 | 15.5k | msg << " expected "; |
55 | | |
56 | 15.5k | if(expected_class_tag == UNIVERSAL || expected_class_tag == CONSTRUCTED) |
57 | 15.1k | { |
58 | 15.1k | msg << asn1_tag_to_string(expected_type_tag); |
59 | 15.1k | } |
60 | 326 | else |
61 | 326 | { |
62 | 326 | msg << std::to_string(expected_type_tag); |
63 | 326 | } |
64 | | |
65 | 15.5k | msg << "/" << asn1_class_to_string(expected_class_tag); |
66 | | |
67 | 15.5k | throw BER_Decoding_Error(msg.str()); |
68 | 15.5k | } |
69 | 733k | } |
70 | | |
71 | | bool BER_Object::is_a(ASN1_Tag expected_type_tag, ASN1_Tag expected_class_tag) const |
72 | 1.10M | { |
73 | 1.10M | return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag); |
74 | 1.10M | } |
75 | | |
76 | | bool BER_Object::is_a(int expected_type_tag, ASN1_Tag expected_class_tag) const |
77 | 266k | { |
78 | 266k | return is_a(ASN1_Tag(expected_type_tag), expected_class_tag); |
79 | 266k | } |
80 | | |
81 | | void BER_Object::set_tagging(ASN1_Tag type_tag, ASN1_Tag class_tag) |
82 | 1.45M | { |
83 | 1.45M | m_type_tag = type_tag; |
84 | 1.45M | m_class_tag = class_tag; |
85 | 1.45M | } |
86 | | |
87 | | std::string asn1_class_to_string(ASN1_Tag type) |
88 | 28.9k | { |
89 | 28.9k | switch(type) |
90 | 28.9k | { |
91 | 9.92k | case UNIVERSAL: |
92 | 9.92k | return "UNIVERSAL"; |
93 | 14.2k | case CONSTRUCTED: |
94 | 14.2k | return "CONSTRUCTED"; |
95 | 1.17k | case CONTEXT_SPECIFIC: |
96 | 1.17k | return "CONTEXT_SPECIFIC"; |
97 | 858 | case APPLICATION: |
98 | 858 | return "APPLICATION"; |
99 | 652 | case CONSTRUCTED | CONTEXT_SPECIFIC: |
100 | 652 | return "PRIVATE"; |
101 | 0 | case Botan::NO_OBJECT: |
102 | 0 | return "NO_OBJECT"; |
103 | 2.11k | default: |
104 | 2.11k | return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")"; |
105 | 28.9k | } |
106 | 28.9k | } |
107 | | |
108 | | std::string asn1_tag_to_string(ASN1_Tag type) |
109 | 24.1k | { |
110 | 24.1k | switch(type) |
111 | 24.1k | { |
112 | 9.26k | case Botan::SEQUENCE: |
113 | 9.26k | return "SEQUENCE"; |
114 | | |
115 | 1.20k | case Botan::SET: |
116 | 1.20k | return "SET"; |
117 | | |
118 | 227 | case Botan::PRINTABLE_STRING: |
119 | 227 | return "PRINTABLE STRING"; |
120 | | |
121 | 350 | case Botan::NUMERIC_STRING: |
122 | 350 | return "NUMERIC STRING"; |
123 | | |
124 | 261 | case Botan::IA5_STRING: |
125 | 261 | return "IA5 STRING"; |
126 | | |
127 | 311 | case Botan::T61_STRING: |
128 | 311 | return "T61 STRING"; |
129 | | |
130 | 148 | case Botan::UTF8_STRING: |
131 | 148 | return "UTF8 STRING"; |
132 | | |
133 | 402 | case Botan::VISIBLE_STRING: |
134 | 402 | return "VISIBLE STRING"; |
135 | | |
136 | 91 | case Botan::BMP_STRING: |
137 | 91 | return "BMP STRING"; |
138 | | |
139 | 77 | case Botan::UNIVERSAL_STRING: |
140 | 77 | return "UNIVERSAL STRING"; |
141 | | |
142 | 115 | case Botan::UTC_TIME: |
143 | 115 | return "UTC TIME"; |
144 | | |
145 | 168 | case Botan::GENERALIZED_TIME: |
146 | 168 | return "GENERALIZED TIME"; |
147 | | |
148 | 2.30k | case Botan::OCTET_STRING: |
149 | 2.30k | return "OCTET STRING"; |
150 | | |
151 | 2.39k | case Botan::BIT_STRING: |
152 | 2.39k | return "BIT STRING"; |
153 | | |
154 | 1.85k | case Botan::ENUMERATED: |
155 | 1.85k | return "ENUMERATED"; |
156 | | |
157 | 1.26k | case Botan::INTEGER: |
158 | 1.26k | return "INTEGER"; |
159 | | |
160 | 167 | case Botan::NULL_TAG: |
161 | 167 | return "NULL"; |
162 | | |
163 | 286 | case Botan::OBJECT_ID: |
164 | 286 | return "OBJECT"; |
165 | | |
166 | 387 | case Botan::BOOLEAN: |
167 | 387 | return "BOOLEAN"; |
168 | | |
169 | 6 | case Botan::NO_OBJECT: |
170 | 6 | return "NO_OBJECT"; |
171 | | |
172 | 2.86k | default: |
173 | 2.86k | return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")"; |
174 | 24.1k | } |
175 | 24.1k | } |
176 | | |
177 | | /* |
178 | | * BER Decoding Exceptions |
179 | | */ |
180 | | BER_Decoding_Error::BER_Decoding_Error(const std::string& str) : |
181 | 32.7k | Decoding_Error("BER: " + str) {} |
182 | | |
183 | | BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) : |
184 | 2.24k | BER_Decoding_Error(str + ": " + std::to_string(tag)) {} |
185 | | |
186 | | BER_Bad_Tag::BER_Bad_Tag(const std::string& str, |
187 | | ASN1_Tag tag1, ASN1_Tag tag2) : |
188 | 0 | BER_Decoding_Error(str + ": " + std::to_string(tag1) + "/" + std::to_string(tag2)) {} |
189 | | |
190 | | namespace ASN1 { |
191 | | |
192 | | /* |
193 | | * Put some arbitrary bytes into a SEQUENCE |
194 | | */ |
195 | | std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) |
196 | 46.0k | { |
197 | 46.0k | return ASN1::put_in_sequence(contents.data(), contents.size()); |
198 | 46.0k | } |
199 | | |
200 | | std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) |
201 | 46.0k | { |
202 | 46.0k | std::vector<uint8_t> output; |
203 | 46.0k | DER_Encoder(output) |
204 | 46.0k | .start_cons(SEQUENCE) |
205 | 46.0k | .raw_bytes(bits, len) |
206 | 46.0k | .end_cons(); |
207 | 46.0k | return output; |
208 | 46.0k | } |
209 | | |
210 | | /* |
211 | | * Convert a BER object into a string object |
212 | | */ |
213 | | std::string to_string(const BER_Object& obj) |
214 | 181k | { |
215 | 181k | return std::string(cast_uint8_ptr_to_char(obj.bits()), |
216 | 181k | obj.length()); |
217 | 181k | } |
218 | | |
219 | | /* |
220 | | * Do heuristic tests for BER data |
221 | | */ |
222 | | bool maybe_BER(DataSource& source) |
223 | 33.6k | { |
224 | 33.6k | uint8_t first_u8; |
225 | 33.6k | if(!source.peek_byte(first_u8)) |
226 | 11 | { |
227 | 11 | BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF"); |
228 | 11 | throw Stream_IO_Error("ASN1::maybe_BER: Source was empty"); |
229 | 11 | } |
230 | | |
231 | 33.6k | if(first_u8 == (SEQUENCE | CONSTRUCTED)) |
232 | 27.1k | return true; |
233 | 6.45k | return false; |
234 | 6.45k | } |
235 | | |
236 | | } |
237 | | |
238 | | } |