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

randombit / botan / 5464015689

05 Jul 2023 12:02PM UTC coverage: 91.656% (-0.08%) from 91.732%
5464015689

Pull #3609

github

web-flow
Merge 136c76107 into cf8d8a6ca
Pull Request #3609: [TLS 1.3] Hybrid PQ/T key establishment

78648 of 85808 relevant lines covered (91.66%)

12275301.23 hits per line

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

93.69
/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/stl_util.h>
14
#include <sstream>
15

16
namespace Botan {
17

18
std::vector<uint8_t> ASN1_Object::BER_encode() const {
20,237✔
19
   std::vector<uint8_t> output;
20,237✔
20
   DER_Encoder der(output);
20,237✔
21
   this->encode_into(der);
20,237✔
22
   return output;
20,237✔
23
}
20,237✔
24

25
/*
26
* Check a type invariant on BER data
27
*/
28
void BER_Object::assert_is_a(ASN1_Type expected_type_tag, ASN1_Class expected_class_tag, std::string_view descr) const {
1,007,649✔
29
   if(this->is_a(expected_type_tag, expected_class_tag) == false) {
1,007,649✔
30
      std::stringstream msg;
5,050✔
31

32
      msg << "Tag mismatch when decoding " << descr << " got ";
5,050✔
33

34
      if(m_class_tag == ASN1_Class::NoObject && m_type_tag == ASN1_Type::NoObject) {
5,050✔
35
         msg << "EOF";
2,294✔
36
      } else {
37
         if(m_class_tag == ASN1_Class::Universal || m_class_tag == ASN1_Class::Constructed) {
2,756✔
38
            msg << asn1_tag_to_string(m_type_tag);
4,269✔
39
         } else {
40
            msg << std::to_string(static_cast<uint32_t>(m_type_tag));
1,262✔
41
         }
42

43
         msg << "/" << asn1_class_to_string(m_class_tag);
5,686✔
44
      }
45

46
      msg << " expected ";
5,050✔
47

48
      if(expected_class_tag == ASN1_Class::Universal || expected_class_tag == ASN1_Class::Constructed) {
5,050✔
49
         msg << asn1_tag_to_string(expected_type_tag);
9,992✔
50
      } else {
51
         msg << std::to_string(static_cast<uint32_t>(expected_type_tag));
108✔
52
      }
53

54
      msg << "/" << asn1_class_to_string(expected_class_tag);
10,100✔
55

56
      throw BER_Decoding_Error(msg.str());
10,100✔
57
   }
5,050✔
58
}
1,002,599✔
59

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

64
bool BER_Object::is_a(int expected_type_tag, ASN1_Class expected_class_tag) const {
279,525✔
65
   return is_a(ASN1_Type(expected_type_tag), expected_class_tag);
279,525✔
66
}
67

68
void BER_Object::set_tagging(ASN1_Type type_tag, ASN1_Class class_tag) {
6,723,457✔
69
   m_type_tag = type_tag;
6,723,457✔
70
   m_class_tag = class_tag;
6,723,457✔
71
}
6,723,457✔
72

73
std::string asn1_class_to_string(ASN1_Class type) {
7,806✔
74
   switch(type) {
7,806✔
75
      case ASN1_Class::Universal:
2,870✔
76
         return "UNIVERSAL";
2,870✔
77
      case ASN1_Class::Constructed:
4,251✔
78
         return "CONSTRUCTED";
4,251✔
79
      case ASN1_Class::ContextSpecific:
174✔
80
         return "CONTEXT_SPECIFIC";
174✔
81
      case ASN1_Class::Application:
60✔
82
         return "APPLICATION";
60✔
83
      case ASN1_Class::Private:
56✔
84
         return "PRIVATE";
56✔
85
      case ASN1_Class::NoObject:
×
86
         return "NO_OBJECT";
×
87
      default:
395✔
88
         return "CLASS(" + std::to_string(static_cast<size_t>(type)) + ")";
790✔
89
   }
90
}
91

92
std::string asn1_tag_to_string(ASN1_Type type) {
7,946✔
93
   switch(type) {
7,946✔
94
      case ASN1_Type::Sequence:
3,505✔
95
         return "SEQUENCE";
3,505✔
96

97
      case ASN1_Type::Set:
665✔
98
         return "SET";
665✔
99

100
      case ASN1_Type::PrintableString:
65✔
101
         return "PRINTABLE STRING";
65✔
102

103
      case ASN1_Type::NumericString:
32✔
104
         return "NUMERIC STRING";
32✔
105

106
      case ASN1_Type::Ia5String:
15✔
107
         return "IA5 STRING";
15✔
108

109
      case ASN1_Type::TeletexString:
20✔
110
         return "T61 STRING";
20✔
111

112
      case ASN1_Type::Utf8String:
63✔
113
         return "UTF8 STRING";
63✔
114

115
      case ASN1_Type::VisibleString:
24✔
116
         return "VISIBLE STRING";
24✔
117

118
      case ASN1_Type::BmpString:
8✔
119
         return "BMP STRING";
8✔
120

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

124
      case ASN1_Type::UtcTime:
161✔
125
         return "UTC TIME";
161✔
126

127
      case ASN1_Type::GeneralizedTime:
15✔
128
         return "GENERALIZED TIME";
15✔
129

130
      case ASN1_Type::OctetString:
415✔
131
         return "OCTET STRING";
415✔
132

133
      case ASN1_Type::BitString:
258✔
134
         return "BIT STRING";
258✔
135

136
      case ASN1_Type::Enumerated:
64✔
137
         return "ENUMERATED";
64✔
138

139
      case ASN1_Type::Integer:
1,694✔
140
         return "INTEGER";
1,694✔
141

142
      case ASN1_Type::Null:
133✔
143
         return "NULL";
133✔
144

145
      case ASN1_Type::ObjectId:
212✔
146
         return "OBJECT";
212✔
147

148
      case ASN1_Type::Boolean:
115✔
149
         return "BOOLEAN";
115✔
150

151
      case ASN1_Type::NoObject:
×
152
         return "NO_OBJECT";
×
153

154
      default:
482✔
155
         return "TAG(" + std::to_string(static_cast<uint32_t>(type)) + ")";
964✔
156
   }
157
}
158

159
/*
160
* BER Decoding Exceptions
161
*/
162
BER_Decoding_Error::BER_Decoding_Error(std::string_view str) : Decoding_Error(fmt("BER: {}", str)) {}
25,986✔
163

164
BER_Bad_Tag::BER_Bad_Tag(std::string_view str, uint32_t tagging) : BER_Decoding_Error(fmt("{}: {}", str, tagging)) {}
4,308✔
165

166
namespace ASN1 {
167

168
/*
169
* Put some arbitrary bytes into a SEQUENCE
170
*/
171
std::vector<uint8_t> put_in_sequence(const std::vector<uint8_t>& contents) {
58,800✔
172
   return ASN1::put_in_sequence(contents.data(), contents.size());
58,800✔
173
}
174

175
std::vector<uint8_t> put_in_sequence(const uint8_t bits[], size_t len) {
58,964✔
176
   std::vector<uint8_t> output;
58,964✔
177
   DER_Encoder(output).start_sequence().raw_bytes(bits, len).end_cons();
117,928✔
178
   return output;
58,964✔
179
}
×
180

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

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

198
   const auto cons_seq = static_cast<uint8_t>(ASN1_Class::Constructed) | static_cast<uint8_t>(ASN1_Type::Sequence);
43,243✔
199
   if(first_u8 == cons_seq) {
43,243✔
200
      return true;
36,610✔
201
   }
202
   return false;
203
}
204

205
}  // namespace ASN1
206

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

© 2025 Coveralls, Inc