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

randombit / botan / 22855031980

09 Mar 2026 01:10PM UTC coverage: 90.173% (-0.01%) from 90.184%
22855031980

Pull #5425

github

web-flow
Merge 058be2a86 into cdaa9c0ff
Pull Request #5425: BigInt encoding cleanups

103789 of 115100 relevant lines covered (90.17%)

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

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

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

40
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
3,581✔
41
         msg << "EOF";
1,088✔
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,810✔
45
         } else {
46
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
588✔
47
         }
48

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

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

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

62
      throw BER_Decoding_Error(msg.str());
7,162✔
63
   }
3,581✔
64
}
1,328,576✔
65

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

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

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

79
std::string asn1_class_to_string(ASN1_Class type) {
6,074✔
80
   switch(type) {
6,074✔
81
      case ASN1_Class::Universal:
2,604✔
82
         return "UNIVERSAL";
2,604✔
83
      case ASN1_Class::Constructed:
2,835✔
84
         return "CONSTRUCTED";
2,835✔
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:
392✔
94
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
1,176✔
95
   }
96
}
97

98
std::string asn1_tag_to_string(ASN1_Type type) {
6,267✔
99
   switch(type) {
6,267✔
100
      case ASN1_Type::Sequence:
2,335✔
101
         return "SEQUENCE";
2,335✔
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:
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:
339✔
137
         return "OCTET STRING";
339✔
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,648✔
146
         return "INTEGER";
1,648✔
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:
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,656✔
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,676✔
178
   return ASN1::put_in_sequence(contents.data(), contents.size());
86,676✔
179
}
180

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

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

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