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

randombit / botan / 17127113192

21 Aug 2025 03:48AM UTC coverage: 90.666% (+0.01%) from 90.652%
17127113192

push

github

web-flow
Merge pull request #5061 from reneme/feature/ascon_hash256

100200 of 110516 relevant lines covered (90.67%)

12218609.82 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/data_src.h>
11
#include <botan/der_enc.h>
12
#include <botan/internal/fmt.h>
13
#include <botan/internal/mem_utils.h>
14
#include <botan/internal/stl_util.h>
15
#include <sstream>
16

17
namespace Botan {
18

19
std::vector<uint8_t> ASN1_Object::BER_encode() const {
28,057✔
20
   std::vector<uint8_t> output;
28,057✔
21
   DER_Encoder der(output);
28,057✔
22
   this->encode_into(der);
28,057✔
23
   return output;
28,057✔
24
}
28,057✔
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,279,334✔
30
   if(!this->is_a(expected_type_tag, expected_class_tag)) {
1,279,334✔
31
      std::stringstream msg;
3,584✔
32

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

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

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

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

49
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
3,584✔
50
         msg << asn1_tag_to_string(expected_type_tag);
7,076✔
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,168✔
56

57
      throw BER_Decoding_Error(msg.str());
7,168✔
58
   }
3,584✔
59
}
1,275,750✔
60

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

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

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

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

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

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

101
      case ASN1_Type::PrintableString:
62✔
102
         return "PRINTABLE STRING";
62✔
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:
10✔
117
         return "VISIBLE STRING";
10✔
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,650✔
141
         return "INTEGER";
1,650✔
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,660✔
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,448✔
173
   return ASN1::put_in_sequence(contents.data(), contents.size());
81,448✔
174
}
175

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

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

189
/*
190
* Do heuristic tests for BER data
191
*/
192
bool maybe_BER(DataSource& source) {
59,094✔
193
   uint8_t first_u8 = 0;
59,094✔
194
   if(source.peek_byte(first_u8) == 0) {
59,094✔
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,889✔
200
   return first_u8 == cons_seq;
58,889✔
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