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