/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 | 10.4k | { |
18 | 10.4k | std::vector<uint8_t> output; |
19 | 10.4k | DER_Encoder der(output); |
20 | 10.4k | this->encode_into(der); |
21 | 10.4k | return output; |
22 | 10.4k | } |
23 | | |
24 | | /* |
25 | | * Check a type invariant on BER data |
26 | | */ |
27 | | void BER_Object::assert_is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_, |
28 | | const std::string& descr) const |
29 | 642k | { |
30 | 642k | if(this->is_a(type_tag_, class_tag_) == false) |
31 | 12.3k | { |
32 | 12.3k | std::stringstream msg; |
33 | 12.3k | |
34 | 12.3k | msg << "Tag mismatch when decoding " << descr << " got "; |
35 | 12.3k | |
36 | 12.3k | if(class_tag == NO_OBJECT && type_tag == NO_OBJECT) |
37 | 1.93k | { |
38 | 1.93k | msg << "EOF"; |
39 | 1.93k | } |
40 | 10.3k | else |
41 | 10.3k | { |
42 | 10.3k | if(class_tag == UNIVERSAL || class_tag == CONSTRUCTED) |
43 | 6.87k | { |
44 | 6.87k | msg << asn1_tag_to_string(type_tag); |
45 | 6.87k | } |
46 | 3.50k | else |
47 | 3.50k | { |
48 | 3.50k | msg << std::to_string(type_tag); |
49 | 3.50k | } |
50 | 10.3k | |
51 | 10.3k | msg << "/" << asn1_class_to_string(class_tag); |
52 | 10.3k | } |
53 | 12.3k | |
54 | 12.3k | msg << " expected "; |
55 | 12.3k | |
56 | 12.3k | if(class_tag_ == UNIVERSAL || class_tag_ == CONSTRUCTED) |
57 | 12.1k | { |
58 | 12.1k | msg << asn1_tag_to_string(type_tag_); |
59 | 12.1k | } |
60 | 200 | else |
61 | 200 | { |
62 | 200 | msg << std::to_string(type_tag_); |
63 | 200 | } |
64 | 12.3k | |
65 | 12.3k | msg << "/" << asn1_class_to_string(class_tag_); |
66 | 12.3k | |
67 | 12.3k | throw BER_Decoding_Error(msg.str()); |
68 | 12.3k | } |
69 | 642k | } |
70 | | |
71 | | bool BER_Object::is_a(ASN1_Tag type_tag_, ASN1_Tag class_tag_) const |
72 | 906k | { |
73 | 906k | return (type_tag == type_tag_ && class_tag == class_tag_); |
74 | 906k | } |
75 | | |
76 | | bool BER_Object::is_a(int type_tag_, ASN1_Tag class_tag_) const |
77 | 174k | { |
78 | 174k | return is_a(ASN1_Tag(type_tag_), class_tag_); |
79 | 174k | } |
80 | | |
81 | | void BER_Object::set_tagging(ASN1_Tag t, ASN1_Tag c) |
82 | 1.27M | { |
83 | 1.27M | type_tag = t; |
84 | 1.27M | class_tag = c; |
85 | 1.27M | } |
86 | | |
87 | | std::string asn1_class_to_string(ASN1_Tag type) |
88 | 22.6k | { |
89 | 22.6k | switch(type) |
90 | 22.6k | { |
91 | 7.94k | case UNIVERSAL: |
92 | 7.94k | return "UNIVERSAL"; |
93 | 11.0k | case CONSTRUCTED: |
94 | 11.0k | return "CONSTRUCTED"; |
95 | 899 | case CONTEXT_SPECIFIC: |
96 | 899 | return "CONTEXT_SPECIFIC"; |
97 | 835 | case APPLICATION: |
98 | 835 | return "APPLICATION"; |
99 | 482 | case CONSTRUCTED | CONTEXT_SPECIFIC: |
100 | 482 | return "PRIVATE"; |
101 | 0 | case Botan::NO_OBJECT: |
102 | 0 | return "NO_OBJECT"; |
103 | 1.49k | default: |
104 | 1.49k | return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")"; |
105 | 22.6k | } |
106 | 22.6k | } |
107 | | |
108 | | std::string asn1_tag_to_string(ASN1_Tag type) |
109 | 18.9k | { |
110 | 18.9k | switch(type) |
111 | 18.9k | { |
112 | 7.38k | case Botan::SEQUENCE: |
113 | 7.38k | return "SEQUENCE"; |
114 | 0 | |
115 | 969 | case Botan::SET: |
116 | 969 | return "SET"; |
117 | 0 | |
118 | 260 | case Botan::PRINTABLE_STRING: |
119 | 260 | return "PRINTABLE STRING"; |
120 | 0 | |
121 | 306 | case Botan::NUMERIC_STRING: |
122 | 306 | return "NUMERIC STRING"; |
123 | 0 | |
124 | 149 | case Botan::IA5_STRING: |
125 | 149 | return "IA5 STRING"; |
126 | 0 | |
127 | 156 | case Botan::T61_STRING: |
128 | 156 | return "T61 STRING"; |
129 | 0 | |
130 | 151 | case Botan::UTF8_STRING: |
131 | 151 | return "UTF8 STRING"; |
132 | 0 | |
133 | 308 | case Botan::VISIBLE_STRING: |
134 | 308 | return "VISIBLE STRING"; |
135 | 0 | |
136 | 65 | case Botan::BMP_STRING: |
137 | 65 | return "BMP STRING"; |
138 | 0 | |
139 | 62 | case Botan::UNIVERSAL_STRING: |
140 | 62 | return "UNIVERSAL STRING"; |
141 | 0 | |
142 | 102 | case Botan::UTC_TIME: |
143 | 102 | return "UTC TIME"; |
144 | 0 | |
145 | 213 | case Botan::GENERALIZED_TIME: |
146 | 213 | return "GENERALIZED TIME"; |
147 | 0 | |
148 | 1.81k | case Botan::OCTET_STRING: |
149 | 1.81k | return "OCTET STRING"; |
150 | 0 | |
151 | 2.21k | case Botan::BIT_STRING: |
152 | 2.21k | return "BIT STRING"; |
153 | 0 | |
154 | 1.31k | case Botan::ENUMERATED: |
155 | 1.31k | return "ENUMERATED"; |
156 | 0 | |
157 | 869 | case Botan::INTEGER: |
158 | 869 | return "INTEGER"; |
159 | 0 | |
160 | 157 | case Botan::NULL_TAG: |
161 | 157 | return "NULL"; |
162 | 0 | |
163 | 202 | case Botan::OBJECT_ID: |
164 | 202 | return "OBJECT"; |
165 | 0 | |
166 | 291 | case Botan::BOOLEAN: |
167 | 291 | return "BOOLEAN"; |
168 | 0 | |
169 | 6 | case Botan::NO_OBJECT: |
170 | 6 | return "NO_OBJECT"; |
171 | 0 | |
172 | 1.99k | default: |
173 | 1.99k | return "TAG(" + std::to_string(static_cast<size_t>(type)) + ")"; |
174 | 18.9k | } |
175 | 18.9k | } |
176 | | |
177 | | /* |
178 | | * BER Decoding Exceptions |
179 | | */ |
180 | | BER_Decoding_Error::BER_Decoding_Error(const std::string& str) : |
181 | 27.3k | Decoding_Error("BER: " + str) {} |
182 | | |
183 | | BER_Bad_Tag::BER_Bad_Tag(const std::string& str, ASN1_Tag tag) : |
184 | 2.23k | 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 | 41.0k | { |
197 | 41.0k | return ASN1::put_in_sequence(contents.data(), contents.size()); |
198 | 41.0k | } |
199 | | |
200 | | std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) |
201 | 41.0k | { |
202 | 41.0k | std::vector<uint8_t> output; |
203 | 41.0k | DER_Encoder(output) |
204 | 41.0k | .start_cons(SEQUENCE) |
205 | 41.0k | .raw_bytes(bits, len) |
206 | 41.0k | .end_cons(); |
207 | 41.0k | return output; |
208 | 41.0k | } |
209 | | |
210 | | /* |
211 | | * Convert a BER object into a string object |
212 | | */ |
213 | | std::string to_string(const BER_Object& obj) |
214 | 160k | { |
215 | 160k | return std::string(cast_uint8_ptr_to_char(obj.bits()), |
216 | 160k | obj.length()); |
217 | 160k | } |
218 | | |
219 | | /* |
220 | | * Do heuristic tests for BER data |
221 | | */ |
222 | | bool maybe_BER(DataSource& source) |
223 | 29.9k | { |
224 | 29.9k | uint8_t first_u8; |
225 | 29.9k | if(!source.peek_byte(first_u8)) |
226 | 14 | { |
227 | 14 | BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF"); |
228 | 14 | throw Stream_IO_Error("ASN1::maybe_BER: Source was empty"); |
229 | 29.9k | } |
230 | 29.9k | |
231 | 29.9k | if(first_u8 == (SEQUENCE | CONSTRUCTED)) |
232 | 23.9k | return true; |
233 | 6.01k | return false; |
234 | 6.01k | } |
235 | | |
236 | | } |
237 | | |
238 | | } |