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

randombit / botan / 12548935066

30 Dec 2024 04:04PM UTC coverage: 91.262% (+0.05%) from 91.208%
12548935066

Pull #4507

github

web-flow
Merge 3aca433ed into 9b8f3cc80
Pull Request #4507: Remove support for Kyber r3 key exchange in TLS

93394 of 102336 relevant lines covered (91.26%)

11408572.15 hits per line

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

82.28
/src/lib/asn1/der_enc.cpp
1
/*
2
* DER Encoder
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/der_enc.h>
9

10
#include <botan/asn1_obj.h>
11
#include <botan/bigint.h>
12
#include <botan/internal/bit_ops.h>
13
#include <botan/internal/fmt.h>
14
#include <botan/internal/loadstor.h>
15
#include <algorithm>
16

17
namespace Botan {
18

19
namespace {
20

21
/*
22
* DER encode an ASN.1 type tag
23
*/
24
void encode_tag(std::vector<uint8_t>& encoded_tag, ASN1_Type type_tag_e, ASN1_Class class_tag_e) {
427,565✔
25
   const uint32_t type_tag = static_cast<uint32_t>(type_tag_e);
427,565✔
26
   const uint32_t class_tag = static_cast<uint32_t>(class_tag_e);
427,565✔
27

28
   if((class_tag | 0xE0) != 0xE0) {
427,565✔
29
      throw Encoding_Error(fmt("DER_Encoder: Invalid class tag {}", std::to_string(class_tag)));
×
30
   }
31

32
   if(type_tag <= 30) {
427,565✔
33
      encoded_tag.push_back(static_cast<uint8_t>(type_tag | class_tag));
426,456✔
34
   } else {
35
      size_t blocks = high_bit(static_cast<uint32_t>(type_tag)) + 6;
1,109✔
36
      blocks = (blocks - (blocks % 7)) / 7;
1,109✔
37

38
      BOTAN_ASSERT_NOMSG(blocks > 0);
1,109✔
39

40
      encoded_tag.push_back(static_cast<uint8_t>(class_tag | 0x1F));
1,109✔
41
      for(size_t i = 0; i != blocks - 1; ++i) {
2,273✔
42
         encoded_tag.push_back(0x80 | ((type_tag >> 7 * (blocks - i - 1)) & 0x7F));
1,164✔
43
      }
44
      encoded_tag.push_back(type_tag & 0x7F);
1,109✔
45
   }
46
}
427,565✔
47

48
/*
49
* DER encode an ASN.1 length field
50
*/
51
void encode_length(std::vector<uint8_t>& encoded_length, size_t length) {
427,565✔
52
   if(length <= 127) {
427,565✔
53
      encoded_length.push_back(static_cast<uint8_t>(length));
320,053✔
54
   } else {
55
      const size_t bytes_needed = significant_bytes(length);
107,512✔
56

57
      encoded_length.push_back(static_cast<uint8_t>(0x80 | bytes_needed));
107,512✔
58

59
      for(size_t i = sizeof(length) - bytes_needed; i < sizeof(length); ++i) {
306,618✔
60
         encoded_length.push_back(get_byte_var(i, length));
199,106✔
61
      }
62
   }
63
}
427,565✔
64

65
}  // namespace
66

67
DER_Encoder::DER_Encoder(secure_vector<uint8_t>& vec) {
×
68
   m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); };
×
69
}
×
70

71
DER_Encoder::DER_Encoder(std::vector<uint8_t>& vec) {
183,910✔
72
   m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); };
551,730✔
73
}
183,910✔
74

75
/*
76
* Push the encoded SEQUENCE/SET to the encoder stream
77
*/
78
void DER_Encoder::DER_Sequence::push_contents(DER_Encoder& der) {
220,562✔
79
   const auto real_class_tag = m_class_tag | ASN1_Class::Constructed;
220,562✔
80

81
   if(m_type_tag == ASN1_Type::Set) {
220,562✔
82
      std::sort(m_set_contents.begin(), m_set_contents.end());
1,819✔
83
      for(const auto& set_elem : m_set_contents) {
3,638✔
84
         m_contents += set_elem;
1,819✔
85
      }
86
      m_set_contents.clear();
1,819✔
87
   }
88

89
   der.add_object(m_type_tag, real_class_tag, m_contents.data(), m_contents.size());
220,562✔
90
   m_contents.clear();
220,562✔
91
}
220,562✔
92

93
/*
94
* Add an encoded value to the SEQUENCE/SET
95
*/
96
void DER_Encoder::DER_Sequence::add_bytes(const uint8_t data[], size_t length) {
142,351✔
97
   if(m_type_tag == ASN1_Type::Set) {
142,351✔
98
      m_set_contents.push_back(secure_vector<uint8_t>(data, data + length));
434✔
99
   } else {
100
      m_contents += std::make_pair(data, length);
142,134✔
101
   }
102
}
142,351✔
103

104
void DER_Encoder::DER_Sequence::add_bytes(const uint8_t hdr[], size_t hdr_len, const uint8_t val[], size_t val_len) {
237,399✔
105
   if(m_type_tag == ASN1_Type::Set) {
237,399✔
106
      secure_vector<uint8_t> m;
1,602✔
107
      m.reserve(hdr_len + val_len);
1,602✔
108
      m += std::make_pair(hdr, hdr_len);
1,602✔
109
      m += std::make_pair(val, val_len);
1,602✔
110
      m_set_contents.push_back(std::move(m));
1,602✔
111
   } else {
1,602✔
112
      m_contents += std::make_pair(hdr, hdr_len);
235,797✔
113
      m_contents += std::make_pair(val, val_len);
235,797✔
114
   }
115
}
237,399✔
116

117
/*
118
* Return the type and class taggings
119
*/
120
uint32_t DER_Encoder::DER_Sequence::tag_of() const {
×
121
   return m_type_tag | m_class_tag;
×
122
}
123

124
/*
125
* DER_Sequence Constructor
126
*/
127
DER_Encoder::DER_Sequence::DER_Sequence(ASN1_Type t1, ASN1_Class t2) : m_type_tag(t1), m_class_tag(t2) {}
220,562✔
128

129
/*
130
* Return the encoded contents
131
*/
132
secure_vector<uint8_t> DER_Encoder::get_contents() {
5,295✔
133
   if(!m_subsequences.empty()) {
5,295✔
134
      throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
×
135
   }
136

137
   if(m_append_output) {
5,295✔
138
      throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
×
139
   }
140

141
   secure_vector<uint8_t> output;
5,295✔
142
   std::swap(output, m_default_outbuf);
5,295✔
143
   return output;
5,295✔
144
}
145

146
std::vector<uint8_t> DER_Encoder::get_contents_unlocked() {
×
147
   if(!m_subsequences.empty()) {
×
148
      throw Invalid_State("DER_Encoder: Sequence hasn't been marked done");
×
149
   }
150

151
   if(m_append_output) {
×
152
      throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
×
153
   }
154

155
   std::vector<uint8_t> output(m_default_outbuf.begin(), m_default_outbuf.end());
×
156
   m_default_outbuf.clear();
×
157
   return output;
×
158
}
159

160
/*
161
* Start a new ASN.1 SEQUENCE/SET/EXPLICIT
162
*/
163
DER_Encoder& DER_Encoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
220,562✔
164
   m_subsequences.push_back(DER_Sequence(type_tag, class_tag));
441,124✔
165
   return (*this);
220,562✔
166
}
167

168
/*
169
* Finish the current ASN.1 SEQUENCE/SET/EXPLICIT
170
*/
171
DER_Encoder& DER_Encoder::end_cons() {
220,562✔
172
   if(m_subsequences.empty()) {
220,562✔
173
      throw Invalid_State("DER_Encoder::end_cons: No such sequence");
×
174
   }
175

176
   DER_Sequence last_seq = std::move(m_subsequences[m_subsequences.size() - 1]);
220,562✔
177
   m_subsequences.pop_back();
220,562✔
178
   last_seq.push_contents(*this);
220,562✔
179

180
   return (*this);
220,562✔
181
}
220,562✔
182

183
/*
184
* Start a new ASN.1 EXPLICIT encoding
185
*/
186
DER_Encoder& DER_Encoder::start_explicit(uint16_t type_no) {
985✔
187
   ASN1_Type type_tag = static_cast<ASN1_Type>(type_no);
985✔
188

189
   // This would confuse DER_Sequence
190
   if(type_tag == ASN1_Type::Set) {
985✔
191
      throw Internal_Error("DER_Encoder.start_explicit(SET) not supported");
×
192
   }
193

194
   return start_cons(type_tag, ASN1_Class::ContextSpecific);
985✔
195
}
196

197
/*
198
* Finish the current ASN.1 EXPLICIT encoding
199
*/
200
DER_Encoder& DER_Encoder::end_explicit() {
985✔
201
   return end_cons();
985✔
202
}
203

204
/*
205
* Write raw bytes into the stream
206
*/
207
DER_Encoder& DER_Encoder::raw_bytes(const uint8_t bytes[], size_t length) {
142,351✔
208
   if(!m_subsequences.empty()) {
142,351✔
209
      m_subsequences[m_subsequences.size() - 1].add_bytes(bytes, length);
142,351✔
210
   } else if(m_append_output) {
×
211
      m_append_output(bytes, length);
×
212
   } else {
213
      m_default_outbuf += std::make_pair(bytes, length);
×
214
   }
215

216
   return (*this);
142,351✔
217
}
218

219
/*
220
* Write the encoding of the byte(s)
221
*/
222
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length) {
427,565✔
223
   std::vector<uint8_t> hdr;
427,565✔
224
   encode_tag(hdr, type_tag, class_tag);
427,565✔
225
   encode_length(hdr, length);
427,565✔
226

227
   if(!m_subsequences.empty()) {
427,565✔
228
      m_subsequences[m_subsequences.size() - 1].add_bytes(hdr.data(), hdr.size(), rep, length);
237,399✔
229
   } else if(m_append_output) {
190,166✔
230
      m_append_output(hdr.data(), hdr.size());
183,910✔
231
      m_append_output(rep, length);
367,820✔
232
   } else {
233
      m_default_outbuf += hdr;
6,256✔
234
      m_default_outbuf += std::make_pair(rep, length);
6,256✔
235
   }
236

237
   return (*this);
427,565✔
238
}
427,565✔
239

240
/*
241
* Encode a NULL object
242
*/
243
DER_Encoder& DER_Encoder::encode_null() {
×
244
   return add_object(ASN1_Type::Null, ASN1_Class::Universal, nullptr, 0);
×
245
}
246

247
/*
248
* DER encode a BOOLEAN
249
*/
250
DER_Encoder& DER_Encoder::encode(bool is_true) {
4,590✔
251
   return encode(is_true, ASN1_Type::Boolean, ASN1_Class::Universal);
4,590✔
252
}
253

254
/*
255
* DER encode a small INTEGER
256
*/
257
DER_Encoder& DER_Encoder::encode(size_t n) {
29,370✔
258
   return encode(BigInt::from_u64(n), ASN1_Type::Integer, ASN1_Class::Universal);
52,097✔
259
}
260

261
/*
262
* DER encode a small INTEGER
263
*/
264
DER_Encoder& DER_Encoder::encode(const BigInt& n) {
20,988✔
265
   return encode(n, ASN1_Type::Integer, ASN1_Class::Universal);
20,988✔
266
}
267

268
/*
269
* Encode this object
270
*/
271
DER_Encoder& DER_Encoder::encode(const uint8_t bytes[], size_t length, ASN1_Type real_type) {
38,853✔
272
   return encode(bytes, length, real_type, real_type, ASN1_Class::Universal);
38,853✔
273
}
274

275
/*
276
* DER encode a BOOLEAN
277
*/
278
DER_Encoder& DER_Encoder::encode(bool is_true, ASN1_Type type_tag, ASN1_Class class_tag) {
4,590✔
279
   uint8_t val = is_true ? 0xFF : 0x00;
4,590✔
280
   return add_object(type_tag, class_tag, &val, 1);
4,590✔
281
}
282

283
/*
284
* DER encode a small INTEGER
285
*/
286
DER_Encoder& DER_Encoder::encode(size_t n, ASN1_Type type_tag, ASN1_Class class_tag) {
29✔
287
   return encode(BigInt::from_u64(n), type_tag, class_tag);
58✔
288
}
289

290
/*
291
* DER encode an INTEGER
292
*/
293
DER_Encoder& DER_Encoder::encode(const BigInt& n, ASN1_Type type_tag, ASN1_Class class_tag) {
50,387✔
294
   if(n == 0) {
50,387✔
295
      return add_object(type_tag, class_tag, 0);
7,018✔
296
   }
297

298
   const size_t extra_zero = (n.bits() % 8 == 0) ? 1 : 0;
43,369✔
299

300
   auto contents = n.serialize(n.bytes() + extra_zero);
43,369✔
301
   if(n < 0) {
43,369✔
302
      for(unsigned char& content : contents) {
×
303
         content = ~content;
×
304
      }
305
      for(size_t i = contents.size(); i > 0; --i) {
×
306
         if(++contents[i - 1]) {
×
307
            break;
308
         }
309
      }
310
   }
311

312
   return add_object(type_tag, class_tag, contents);
43,369✔
313
}
43,369✔
314

315
/*
316
* DER encode an OCTET STRING or BIT STRING
317
*/
318
DER_Encoder& DER_Encoder::encode(
39,259✔
319
   const uint8_t bytes[], size_t length, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag) {
320
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
39,259✔
321
      throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
×
322
   }
323

324
   if(real_type == ASN1_Type::BitString) {
39,259✔
325
      secure_vector<uint8_t> encoded;
24,054✔
326
      encoded.push_back(0);
24,054✔
327
      encoded += std::make_pair(bytes, length);
24,054✔
328
      return add_object(type_tag, class_tag, encoded);
24,054✔
329
   } else {
24,054✔
330
      return add_object(type_tag, class_tag, bytes, length);
15,205✔
331
   }
332
}
333

334
DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) {
100,915✔
335
   obj.encode_into(*this);
100,915✔
336
   return (*this);
100,915✔
337
}
338

339
/*
340
* Write the encoding of the byte(s)
341
*/
342
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view rep_str) {
5,490✔
343
   const uint8_t* rep = cast_char_ptr_to_uint8(rep_str.data());
5,490✔
344
   const size_t rep_len = rep_str.size();
5,490✔
345
   return add_object(type_tag, class_tag, rep, rep_len);
5,490✔
346
}
347

348
/*
349
* Write the encoding of the byte
350
*/
351
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, uint8_t rep) {
7,018✔
352
   return add_object(type_tag, class_tag, &rep, 1);
7,018✔
353
}
354

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