• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 19151109973

06 Nov 2025 09:48PM UTC coverage: 90.625% (-0.003%) from 90.628%
19151109973

push

github

randombit
Update for 3.10.0 release

100615 of 111023 relevant lines covered (90.63%)

12629131.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

95.45
/src/lib/asn1/asn1_obj.cpp
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/assert.h>
11
#include <botan/data_src.h>
12
#include <botan/der_enc.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/mem_utils.h>
15
#include <sstream>
16

17
namespace Botan {
18

19
std::vector<uint8_t> ASN1_Object::BER_encode() const {
27,992✔
20
   std::vector<uint8_t> output;
27,992✔
21
   DER_Encoder der(output);
27,992✔
22
   this->encode_into(der);
27,992✔
23
   return output;
27,992✔
24
}
27,992✔
25

26
/*
27
* Check a type invariant on BER data
28
*/
29
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
1,278,219✔
30
   if(!this->is_a(expected_type_tag, expected_class_tag)) {
1,278,219✔
31
      std::stringstream msg;
3,580✔
32

33
      msg << "Tag mismatch when decoding " << descr << " got ";
3,580✔
34

35
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
3,580✔
36
         msg << "EOF";
1,089✔
37
      } else {
38
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
2,491✔
39
            msg << asn1_tag_to_string(m_type_tag);
3,808✔
40
         } else {
41
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
587✔
42
         }
43

44
         msg << "/" << asn1_class_to_string(m_class_tag);
4,982✔
45
      }
46

47
      msg << " expected ";
3,580✔
48

49
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
3,580✔
50
         msg << asn1_tag_to_string(expected_type_tag);
7,068✔
51
      } else {
52
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
46✔
53
      }
54

55
      msg << "/" << asn1_class_to_string(expected_class_tag);
7,160✔
56

57
      throw BER_Decoding_Error(msg.str());
7,160✔
58
   }
3,580✔
59
}
1,274,639✔
60

61
bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const {
1,618,347✔
62
   return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
1,618,347✔
63
}
64

65
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
121,590✔
66
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
121,590✔
67
}
68

69
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
7,149,028✔
70
   m_type_tag = type_tag;
7,149,028✔
71
   m_class_tag = class_tag;
7,149,028✔
72
}
7,149,028✔
73

74
std::string asn1_class_to_string(ASN1_Class type) {
6,071✔
75
   switch(type) {
6,071✔
76
      case ASN1_Class::Universal:
2,601✔
77
         return "UNIVERSAL";
2,601✔
78
      case ASN1_Class::Constructed:
2,837✔
79
         return "CONSTRUCTED";
2,837✔
80
      case ASN1_Class::ContextSpecific:
128✔
81
         return "CONTEXT_SPECIFIC";
128✔
82
      case ASN1_Class::Application:
71✔
83
         return "APPLICATION";
71✔
84
      case ASN1_Class::Private:
43✔
85
         return "PRIVATE";
43✔
86
      case ASN1_Class::NoObject:
×
87
         return "NO_OBJECT";
×
88
      default:
391✔
89
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
1,173✔
90
   }
91
}
92

93
std::string asn1_tag_to_string(ASN1_Type type) {
6,266✔
94
   switch(type) {
6,266✔
95
      case ASN1_Type::Sequence:
2,337✔
96
         return "SEQUENCE";
2,337✔
97

98
      case ASN1_Type::Set:
457✔
99
         return "SET";
457✔
100

101
      case ASN1_Type::PrintableString:
63✔
102
         return "PRINTABLE STRING";
63✔
103

104
      case ASN1_Type::NumericString:
21✔
105
         return "NUMERIC STRING";
21✔
106

107
      case ASN1_Type::Ia5String:
16✔
108
         return "IA5 STRING";
16✔
109

110
      case ASN1_Type::TeletexString:
9✔
111
         return "T61 STRING";
9✔
112

113
      case ASN1_Type::Utf8String:
67✔
114
         return "UTF8 STRING";
67✔
115

116
      case ASN1_Type::VisibleString:
9✔
117
         return "VISIBLE STRING";
9✔
118

119
      case ASN1_Type::BmpString:
2✔
120
         return "BMP STRING";
2✔
121

122
      case ASN1_Type::UniversalString:
×
123
         return "UNIVERSAL STRING";
×
124

125
      case ASN1_Type::UtcTime:
143✔
126
         return "UTC TIME";
143✔
127

128
      case ASN1_Type::GeneralizedTime:
24✔
129
         return "GENERALIZED TIME";
24✔
130

131
      case ASN1_Type::OctetString:
339✔
132
         return "OCTET STRING";
339✔
133

134
      case ASN1_Type::BitString:
197✔
135
         return "BIT STRING";
197✔
136

137
      case ASN1_Type::Enumerated:
51✔
138
         return "ENUMERATED";
51✔
139

140
      case ASN1_Type::Integer:
1,647✔
141
         return "INTEGER";
1,647✔
142

143
      case ASN1_Type::Null:
130✔
144
         return "NULL";
130✔
145

146
      case ASN1_Type::ObjectId:
187✔
147
         return "OBJECT";
187✔
148

149
      case ASN1_Type::Boolean:
92✔
150
         return "BOOLEAN";
92✔
151

152
      case ASN1_Type::NoObject:
1✔
153
         return "NO_OBJECT";
1✔
154

155
      default:
474✔
156
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,422✔
157
   }
158
}
159

160
/*
161
* BER Decoding Exceptions
162
*/
163
BER_Decoding_Error::BER_Decoding_Error(std::string_view err) : Decoding_Error(fmt("BER: {}", err)) {}
23,652✔
164

165
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
4,000✔
166

167
namespace ASN1 {
168

169
/*
170
* Put some arbitrary bytes into a SEQUENCE
171
*/
172
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
81,387✔
173
   return ASN1::put_in_sequence(contents.data(), contents.size());
81,387✔
174
}
175

176
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
82,964✔
177
   std::vector<uint8_t> output;
82,964✔
178
   DER_Encoder(output).start_sequence().raw_bytes(bits, len).end_cons();
165,928✔
179
   return output;
82,964✔
180
}
×
181

182
/*
183
* Convert a BER object into a string object
184
*/
185
std::string to_string(const BER_Object& obj) {
239,617✔
186
   return bytes_to_string(obj.data());
239,617✔
187
}
188

189
/*
190
* Do heuristic tests for BER data
191
*/
192
bool maybe_BER(DataSource& source) {
59,044✔
193
   uint8_t first_u8 = 0;
59,044✔
194
   if(source.peek_byte(first_u8) == 0) {
59,044✔
195
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
205✔
196
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
205✔
197
   }
198

199
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
58,839✔
200
   return first_u8 == cons_seq;
58,839✔
201
}
202

203
}  // namespace ASN1
204

205
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc