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

randombit / botan / 23169899665

16 Mar 2026 10:58PM UTC coverage: 89.682% (-0.08%) from 89.759%
23169899665

Pull #5453

github

web-flow
Merge 989eb1591 into 4f6f5bbaf
Pull Request #5453: Add Whirlpool implementation using AVX-512

104427 of 116442 relevant lines covered (89.68%)

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

27
BER_Object::~BER_Object() {
4,802,846✔
28
   secure_scrub_memory(m_value);
4,802,846✔
29
}
4,802,846✔
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,332,579✔
35
   if(!this->is_a(expected_type_tag, expected_class_tag)) {
1,332,579✔
36
      std::stringstream msg;
3,582✔
37

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

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

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

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

54
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
3,582✔
55
         msg << asn1_tag_to_string(expected_type_tag);
7,070✔
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,164✔
61

62
      throw BER_Decoding_Error(msg.str());
7,164✔
63
   }
3,582✔
64
}
1,328,997✔
65

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

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

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

79
std::string asn1_class_to_string(ASN1_Class type) {
6,074✔
80
   switch(type) {
6,074✔
81
      case ASN1_Class::Universal:
2,603✔
82
         return "UNIVERSAL";
2,603✔
83
      case ASN1_Class::Constructed:
2,839✔
84
         return "CONSTRUCTED";
2,839✔
85
      case ASN1_Class::ContextSpecific:
128✔
86
         return "CONTEXT_SPECIFIC";
128✔
87
      case ASN1_Class::Application:
71✔
88
         return "APPLICATION";
71✔
89
      case ASN1_Class::Private:
42✔
90
         return "PRIVATE";
42✔
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,338✔
101
         return "SEQUENCE";
2,338✔
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:
22✔
110
         return "NUMERIC STRING";
22✔
111

112
      case ASN1_Type::Ia5String:
16✔
113
         return "IA5 STRING";
16✔
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:
3✔
125
         return "BMP STRING";
3✔
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,647✔
146
         return "INTEGER";
1,647✔
147

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

151
      case ASN1_Type::ObjectId:
188✔
152
         return "OBJECT";
188✔
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:
474✔
161
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,422✔
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,664✔
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,715✔
178
   return ASN1::put_in_sequence(contents.data(), contents.size());
86,715✔
179
}
180

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

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

194
/*
195
* Do heuristic tests for BER data
196
*/
197
bool maybe_BER(DataSource& source) {
63,344✔
198
   uint8_t first_u8 = 0;
63,344✔
199
   if(source.peek_byte(first_u8) == 0) {
63,344✔
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,139✔
205
   return first_u8 == cons_seq;
63,139✔
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