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

randombit / botan / 11448036146

21 Oct 2024 08:07PM UTC coverage: 91.132% (-0.008%) from 91.14%
11448036146

push

github

randombit
Update for 3.6.0 release

91047 of 99907 relevant lines covered (91.13%)

9369646.05 hits per line

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

95.5
/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/mem_ops.h>
13
#include <botan/internal/fmt.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 {
26,034✔
20
   std::vector<uint8_t> output;
26,034✔
21
   DER_Encoder der(output);
26,034✔
22
   this->encode_into(der);
26,034✔
23
   return output;
26,034✔
24
}
26,034✔
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,101,823✔
30
   if(this->is_a(expected_type_tag, expected_class_tag) == false) {
1,101,823✔
31
      std::stringstream msg;
3,335✔
32

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

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

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

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

49
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
3,335✔
50
         msg << asn1_tag_to_string(expected_type_tag);
6,580✔
51
      } else {
52
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
45✔
53
      }
54

55
      msg << "/" << asn1_class_to_string(expected_class_tag);
6,670✔
56

57
      throw BER_Decoding_Error(msg.str());
6,670✔
58
   }
3,335✔
59
}
1,098,488✔
60

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

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

69
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
6,880,416✔
70
   m_type_tag = type_tag;
6,880,416✔
71
   m_class_tag = class_tag;
6,880,416✔
72
}
6,880,416✔
73

74
std::string asn1_class_to_string(ASN1_Class type) {
5,573✔
75
   switch(type) {
5,573✔
76
      case ASN1_Class::Universal:
2,549✔
77
         return "UNIVERSAL";
2,549✔
78
      case ASN1_Class::Constructed:
2,513✔
79
         return "CONSTRUCTED";
2,513✔
80
      case ASN1_Class::ContextSpecific:
92✔
81
         return "CONTEXT_SPECIFIC";
92✔
82
      case ASN1_Class::Application:
45✔
83
         return "APPLICATION";
45✔
84
      case ASN1_Class::Private:
38✔
85
         return "PRIVATE";
38✔
86
      case ASN1_Class::NoObject:
×
87
         return "NO_OBJECT";
×
88
      default:
336✔
89
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
1,008✔
90
   }
91
}
92

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

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

101
      case ASN1_Type::PrintableString:
61✔
102
         return "PRINTABLE STRING";
61✔
103

104
      case ASN1_Type::NumericString:
20✔
105
         return "NUMERIC STRING";
20✔
106

107
      case ASN1_Type::Ia5String:
14✔
108
         return "IA5 STRING";
14✔
109

110
      case ASN1_Type::TeletexString:
7✔
111
         return "T61 STRING";
7✔
112

113
      case ASN1_Type::Utf8String:
62✔
114
         return "UTF8 STRING";
62✔
115

116
      case ASN1_Type::VisibleString:
8✔
117
         return "VISIBLE STRING";
8✔
118

119
      case ASN1_Type::BmpString:
1✔
120
         return "BMP STRING";
1✔
121

122
      case ASN1_Type::UniversalString:
×
123
         return "UNIVERSAL STRING";
×
124

125
      case ASN1_Type::UtcTime:
141✔
126
         return "UTC TIME";
141✔
127

128
      case ASN1_Type::GeneralizedTime:
14✔
129
         return "GENERALIZED TIME";
14✔
130

131
      case ASN1_Type::OctetString:
319✔
132
         return "OCTET STRING";
319✔
133

134
      case ASN1_Type::BitString:
190✔
135
         return "BIT STRING";
190✔
136

137
      case ASN1_Type::Enumerated:
51✔
138
         return "ENUMERATED";
51✔
139

140
      case ASN1_Type::Integer:
1,646✔
141
         return "INTEGER";
1,646✔
142

143
      case ASN1_Type::Null:
128✔
144
         return "NULL";
128✔
145

146
      case ASN1_Type::ObjectId:
185✔
147
         return "OBJECT";
185✔
148

149
      case ASN1_Type::Boolean:
85✔
150
         return "BOOLEAN";
85✔
151

152
      case ASN1_Type::NoObject:
1✔
153
         return "NO_OBJECT";
1✔
154

155
      default:
433✔
156
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
1,299✔
157
   }
158
}
159

160
/*
161
* BER Decoding Exceptions
162
*/
163
BER_Decoding_Error::BER_Decoding_Error(std::string_view str) : Decoding_Error(fmt("BER: {}", str)) {}
22,884✔
164

165
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
3,972✔
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) {
75,203✔
173
   return ASN1::put_in_sequence(contents.data(), contents.size());
75,203✔
174
}
175

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

182
/*
183
* Convert a BER object into a string object
184
*/
185
std::string to_string(const BER_Object& obj) {
209,810✔
186
   return std::string(cast_uint8_ptr_to_char(obj.bits()), obj.length());
209,810✔
187
}
188

189
/*
190
* Do heuristic tests for BER data
191
*/
192
bool maybe_BER(DataSource& source) {
54,057✔
193
   uint8_t first_u8;
54,057✔
194
   if(!source.peek_byte(first_u8)) {
54,057✔
195
      BOTAN_ASSERT_EQUAL(source.read_byte(first_u8), 0, "Expected EOF");
204✔
196
      throw Stream_IO_Error("ASN1::maybe_BER: Source was empty");
204✔
197
   }
198

199
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
53,853✔
200
   if(first_u8 == cons_seq) {
53,853✔
201
      return true;
45,293✔
202
   }
203
   return false;
204
}
205

206
}  // namespace ASN1
207

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