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