Coverage Report

Created: 2025-07-18 06:20

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