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

randombit / botan / 22061970273

16 Feb 2026 12:03PM UTC coverage: 90.044% (+0.007%) from 90.037%
22061970273

push

github

web-flow
Merge pull request #5345 from randombit/jack/min-test-rng-h

Minimize includes into test_rng.h

102336 of 113651 relevant lines covered (90.04%)

11395171.59 hits per line

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

95.58
/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,019✔
21
   std::vector<uint8_t> output;
27,019✔
22
   DER_Encoder der(output);
27,019✔
23
   this->encode_into(der);
27,019✔
24
   return output;
27,019✔
25
}
27,019✔
26

27
BER_Object::~BER_Object() {
4,799,092✔
28
   secure_scrub_memory(m_value);
4,799,092✔
29
}
4,799,092✔
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,331,409✔
35
   if(!this->is_a(expected_type_tag, expected_class_tag)) {
1,331,409✔
36
      std::stringstream msg;
3,583✔
37

38
      msg << "Tag mismatch when decoding " << descr << " got ";
3,583✔
39

40
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
3,583✔
41
         msg << "EOF";
1,090✔
42
      } else {
43
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
2,493✔
44
            msg << asn1_tag_to_string(m_type_tag);
3,812✔
45
         } else {
46
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
587✔
47
         }
48

49
         msg << "/" << asn1_class_to_string(m_class_tag);
4,986✔
50
      }
51

52
      msg << " expected ";
3,583✔
53

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

60
      msg << "/" << asn1_class_to_string(expected_class_tag);
7,166✔
61

62
      throw BER_Decoding_Error(msg.str());
7,166✔
63
   }
3,583✔
64
}
1,327,826✔
65

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

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

74
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
7,227,132✔
75
   m_type_tag = type_tag;
7,227,132✔
76
   m_class_tag = class_tag;
7,227,132✔
77
}
7,227,132✔
78

79
std::string asn1_class_to_string(ASN1_Class type) {
6,076✔
80
   switch(type) {
6,076✔
81
      case ASN1_Class::Universal:
2,605✔
82
         return "UNIVERSAL";
2,605✔
83
      case ASN1_Class::Constructed:
2,837✔
84
         return "CONSTRUCTED";
2,837✔
85
      case ASN1_Class::ContextSpecific:
128✔
86
         return "CONTEXT_SPECIFIC";
128✔
87
      case ASN1_Class::Application:
72✔
88
         return "APPLICATION";
72✔
89
      case ASN1_Class::Private:
43✔
90
         return "PRIVATE";
43✔
91
      case ASN1_Class::NoObject:
×
92
         return "NO_OBJECT";
×
93
      default:
391✔
94
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
1,173✔
95
   }
96
}
97

98
std::string asn1_tag_to_string(ASN1_Type type) {
6,270✔
99
   switch(type) {
6,270✔
100
      case ASN1_Type::Sequence:
2,337✔
101
         return "SEQUENCE";
2,337✔
102

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

157
      case ASN1_Type::NoObject:
1✔
158
         return "NO_OBJECT";
1✔
159

160
      default:
475✔
161
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,425✔
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)) {}
23,660✔
169

170
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
4,000✔
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) {
86,613✔
178
   return ASN1::put_in_sequence(contents.data(), contents.size());
86,613✔
179
}
180

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

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

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

204
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
63,058✔
205
   return first_u8 == cons_seq;
63,058✔
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