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

randombit / botan / 26674642667

30 May 2026 02:04AM UTC coverage: 89.361% (-0.003%) from 89.364%
26674642667

push

github

randombit
Fix a typo [ci skip]

109992 of 123087 relevant lines covered (89.36%)

11187326.18 hits per line

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

93.81
/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/mem_ops.h>
14
#include <botan/internal/fmt.h>
15
#include <botan/internal/mem_utils.h>
16
#include <sstream>
17

18
namespace Botan {
19

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

27
BER_Object::~BER_Object() {
4,241,278✔
28
   secure_scrub_memory(m_value);
4,241,278✔
29
}
4,241,278✔
30

31
/*
32
* Check a type invariant on BER data
33
*/
34
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
1,174,050✔
35
   if(!this->is_a(expected_type_tag, expected_class_tag)) {
1,174,050✔
36
      std::stringstream msg;
1,847✔
37

38
      msg << "Tag mismatch when decoding " << descr << " got ";
1,847✔
39

40
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
1,847✔
41
         msg << "EOF";
99✔
42
      } else {
43
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
1,748✔
44
            msg << asn1_tag_to_string(m_type_tag);
2,962✔
45
         } else {
46
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
267✔
47
         }
48

49
         msg << "/" << asn1_class_to_string(m_class_tag);
3,496✔
50
      }
51

52
      msg << " expected ";
1,847✔
53

54
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
1,847✔
55
         msg << asn1_tag_to_string(expected_type_tag);
3,678✔
56
      } else {
57
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
8✔
58
      }
59

60
      msg << "/" << asn1_class_to_string(expected_class_tag);
3,694✔
61

62
      throw BER_Decoding_Error(msg.str());
3,694✔
63
   }
1,847✔
64
}
1,172,203✔
65

66
bool BER_Object::is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag) const {
1,501,309✔
67
   return (m_type_tag == expected_type_tag && m_class_tag == expected_class_tag);
1,501,309✔
68
}
69

70
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
112,161✔
71
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
112,161✔
72
}
73

74
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
6,894,922✔
75
   m_type_tag = type_tag;
6,894,922✔
76
   m_class_tag = class_tag;
6,894,922✔
77
}
6,894,922✔
78

79
std::string asn1_class_to_string(ASN1_Class type) {
3,595✔
80
   switch(type) {
3,595✔
81
      case ASN1_Class::Universal:
1,851✔
82
         return "UNIVERSAL";
1,851✔
83
      case ASN1_Class::Constructed:
1,469✔
84
         return "CONSTRUCTED";
1,469✔
85
      case ASN1_Class::ContextSpecific:
21✔
86
         return "CONTEXT_SPECIFIC";
21✔
87
      case ASN1_Class::Application:
49✔
88
         return "APPLICATION";
49✔
89
      case ASN1_Class::Private:
15✔
90
         return "PRIVATE";
15✔
91
      case ASN1_Class::NoObject:
×
92
         return "NO_OBJECT";
×
93
      default:
190✔
94
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
570✔
95
   }
96
}
97

98
std::string asn1_tag_to_string(ASN1_Type type) {
4,754✔
99
   switch(type) {
4,754✔
100
      case ASN1_Type::Sequence:
1,255✔
101
         return "SEQUENCE";
1,255✔
102

103
      case ASN1_Type::Set:
285✔
104
         return "SET";
285✔
105

106
      case ASN1_Type::PrintableString:
522✔
107
         return "PRINTABLE STRING";
522✔
108

109
      case ASN1_Type::NumericString:
56✔
110
         return "NUMERIC STRING";
56✔
111

112
      case ASN1_Type::Ia5String:
52✔
113
         return "IA5 STRING";
52✔
114

115
      case ASN1_Type::TeletexString:
4✔
116
         return "T61 STRING";
4✔
117

118
      case ASN1_Type::Utf8String:
77✔
119
         return "UTF8 STRING";
77✔
120

121
      case ASN1_Type::VisibleString:
11✔
122
         return "VISIBLE STRING";
11✔
123

124
      case ASN1_Type::BmpString:
1✔
125
         return "BMP STRING";
1✔
126

127
      case ASN1_Type::UniversalString:
×
128
         return "UNIVERSAL STRING";
×
129

130
      case ASN1_Type::UtcTime:
138✔
131
         return "UTC TIME";
138✔
132

133
      case ASN1_Type::GeneralizedTime:
11✔
134
         return "GENERALIZED TIME";
11✔
135

136
      case ASN1_Type::OctetString:
165✔
137
         return "OCTET STRING";
165✔
138

139
      case ASN1_Type::BitString:
120✔
140
         return "BIT STRING";
120✔
141

142
      case ASN1_Type::Enumerated:
11✔
143
         return "ENUMERATED";
11✔
144

145
      case ASN1_Type::Integer:
1,261✔
146
         return "INTEGER";
1,261✔
147

148
      case ASN1_Type::Null:
96✔
149
         return "NULL";
96✔
150

151
      case ASN1_Type::ObjectId:
175✔
152
         return "OBJECT";
175✔
153

154
      case ASN1_Type::Boolean:
84✔
155
         return "BOOLEAN";
84✔
156

157
      case ASN1_Type::NoObject:
×
158
         return "NO_OBJECT";
×
159

160
      default:
430✔
161
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,290✔
162
   }
163
}
164

165
/*
166
* BER Decoding Exceptions
167
*/
168
BER_Decoding_Error::BER_Decoding_Error(std::string_view err) : Decoding_Error(fmt("BER: {}", err)) {}
33,666✔
169

170
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
2,986✔
171

172
namespace ASN1 {
173

174
/*
175
* Put some arbitrary bytes into a SEQUENCE
176
*/
177
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
77,865✔
178
   return ASN1::put_in_sequence(contents.data(), contents.size());
77,865✔
179
}
180

181
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
79,441✔
182
   std::vector<uint8_t> output;
79,441✔
183
   DER_Encoder(output).start_sequence().raw_bytes(bits, len).end_cons();
158,882✔
184
   return output;
79,441✔
185
}
×
186

187
/*
188
* Convert a BER object into a string object
189
*/
190
std::string to_string(const BER_Object& obj) {
209,420✔
191
   return bytes_to_string(obj.data());
209,420✔
192
}
193

194
/*
195
* Do heuristic tests for BER data
196
*/
197
bool maybe_BER(DataSource& source) {
56,374✔
198
   uint8_t first_u8 = 0;
56,374✔
199
   if(source.peek_byte(first_u8) == 0) {
56,374✔
200
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
6✔
201
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
6✔
202
   }
203

204
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
56,368✔
205
   return first_u8 == cons_seq;
56,368✔
206
}
207

208
}  // namespace ASN1
209

210
}  // 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