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

randombit / botan / 5124576186

30 May 2023 06:27PM UTC coverage: 91.972% (-0.2%) from 92.218%
5124576186

push

github

randombit
Update support table

75605 of 82204 relevant lines covered (91.97%)

11895687.83 hits per line

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

83.12
/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) {
298,945✔
25
   const uint32_t type_tag = static_cast<uint32_t>(type_tag_e);
298,945✔
26
   const uint32_t class_tag = static_cast<uint32_t>(class_tag_e);
298,945✔
27

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

32
   if(type_tag <= 30) {
298,945✔
33
      encoded_tag.push_back(static_cast<uint8_t>(type_tag | class_tag));
297,841✔
34
   } else {
35
      size_t blocks = high_bit(static_cast<uint32_t>(type_tag)) + 6;
1,104✔
36
      blocks = (blocks - (blocks % 7)) / 7;
1,104✔
37

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

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

48
/*
49
* DER encode an ASN.1 length field
50
*/
51
void encode_length(std::vector<uint8_t>& encoded_length, size_t length) {
298,945✔
52
   if(length <= 127) {
298,945✔
53
      encoded_length.push_back(static_cast<uint8_t>(length));
216,673✔
54
   } else {
55
      const size_t bytes_needed = significant_bytes(length);
82,272✔
56

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

59
      for(size_t i = sizeof(length) - bytes_needed; i < sizeof(length); ++i) {
232,497✔
60
         encoded_length.push_back(get_byte_var(i, length));
150,225✔
61
      }
62
   }
63
}
298,945✔
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) {
121,447✔
72
   m_append_output = [&vec](const uint8_t b[], size_t l) { vec.insert(vec.end(), b, b + l); };
364,341✔
73
}
121,447✔
74

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

81
   if(m_type_tag == ASN1_Type::Set) {
157,879✔
82
      std::sort(m_set_contents.begin(), m_set_contents.end());
1,267✔
83
      for(const auto& set_elem : m_set_contents) {
2,534✔
84
         m_contents += set_elem;
1,267✔
85
      }
86
      m_set_contents.clear();
1,267✔
87
   }
88

89
   der.add_object(m_type_tag, real_class_tag, m_contents.data(), m_contents.size());
157,879✔
90
   m_contents.clear();
157,879✔
91
}
157,879✔
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) {
103,033✔
97
   if(m_type_tag == ASN1_Type::Set) {
103,033✔
98
      m_set_contents.push_back(secure_vector<uint8_t>(data, data + length));
308✔
99
   } else {
100
      m_contents += std::make_pair(data, length);
102,879✔
101
   }
102
}
103,033✔
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) {
173,350✔
105
   if(m_type_tag == ASN1_Type::Set) {
173,350✔
106
      secure_vector<uint8_t> m;
1,113✔
107
      m.reserve(hdr_len + val_len);
1,113✔
108
      m += std::make_pair(hdr, hdr_len);
1,113✔
109
      m += std::make_pair(val, val_len);
1,113✔
110
      m_set_contents.push_back(std::move(m));
1,113✔
111
   } else {
1,113✔
112
      m_contents += std::make_pair(hdr, hdr_len);
172,237✔
113
      m_contents += std::make_pair(val, val_len);
172,237✔
114
   }
115
}
173,350✔
116

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

122
/*
123
* DER_Sequence Constructor
124
*/
125
DER_Encoder::DER_Sequence::DER_Sequence(ASN1_Type t1, ASN1_Class t2) : m_type_tag(t1), m_class_tag(t2) {}
157,879✔
126

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

135
   if(m_append_output) {
3,454✔
136
      throw Invalid_State("DER_Encoder Cannot get contents when using output vector");
×
137
   }
138

139
   secure_vector<uint8_t> output;
3,454✔
140
   std::swap(output, m_default_outbuf);
3,454✔
141
   return output;
3,454✔
142
}
143

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

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

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

158
/*
159
* Start a new ASN.1 SEQUENCE/SET/EXPLICIT
160
*/
161
DER_Encoder& DER_Encoder::start_cons(ASN1_Type type_tag, ASN1_Class class_tag) {
157,879✔
162
   m_subsequences.push_back(DER_Sequence(type_tag, class_tag));
157,879✔
163
   return (*this);
157,879✔
164
}
165

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

174
   DER_Sequence last_seq = std::move(m_subsequences[m_subsequences.size() - 1]);
157,879✔
175
   m_subsequences.pop_back();
157,879✔
176
   last_seq.push_contents(*this);
157,879✔
177

178
   return (*this);
157,879✔
179
}
157,879✔
180

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

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

192
   return start_cons(type_tag, ASN1_Class::ContextSpecific);
713✔
193
}
194

195
/*
196
* Finish the current ASN.1 EXPLICIT encoding
197
*/
198
DER_Encoder& DER_Encoder::end_explicit() { return end_cons(); }
713✔
199

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

212
   return (*this);
103,033✔
213
}
214

215
/*
216
* Write the encoding of the byte(s)
217
*/
218
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, const uint8_t rep[], size_t length) {
298,945✔
219
   std::vector<uint8_t> hdr;
298,945✔
220
   encode_tag(hdr, type_tag, class_tag);
298,945✔
221
   encode_length(hdr, length);
298,945✔
222

223
   if(!m_subsequences.empty()) {
298,945✔
224
      m_subsequences[m_subsequences.size() - 1].add_bytes(hdr.data(), hdr.size(), rep, length);
173,350✔
225
   } else if(m_append_output) {
125,595✔
226
      m_append_output(hdr.data(), hdr.size());
121,447✔
227
      m_append_output(rep, length);
242,894✔
228
   } else {
229
      m_default_outbuf += hdr;
4,148✔
230
      m_default_outbuf += std::make_pair(rep, length);
4,148✔
231
   }
232

233
   return (*this);
298,945✔
234
}
298,945✔
235

236
/*
237
* Encode a NULL object
238
*/
239
DER_Encoder& DER_Encoder::encode_null() { return add_object(ASN1_Type::Null, ASN1_Class::Universal, nullptr, 0); }
×
240

241
/*
242
* DER encode a BOOLEAN
243
*/
244
DER_Encoder& DER_Encoder::encode(bool is_true) { return encode(is_true, ASN1_Type::Boolean, ASN1_Class::Universal); }
4,185✔
245

246
/*
247
* DER encode a small INTEGER
248
*/
249
DER_Encoder& DER_Encoder::encode(size_t n) {
22,485✔
250
   return encode(BigInt::from_u64(n), ASN1_Type::Integer, ASN1_Class::Universal);
39,511✔
251
}
252

253
/*
254
* DER encode a small INTEGER
255
*/
256
DER_Encoder& DER_Encoder::encode(const BigInt& n) { return encode(n, ASN1_Type::Integer, ASN1_Class::Universal); }
17,968✔
257

258
/*
259
* Encode this object
260
*/
261
DER_Encoder& DER_Encoder::encode(const uint8_t bytes[], size_t length, ASN1_Type real_type) {
29,115✔
262
   return encode(bytes, length, real_type, real_type, ASN1_Class::Universal);
29,115✔
263
}
264

265
/*
266
* DER encode a BOOLEAN
267
*/
268
DER_Encoder& DER_Encoder::encode(bool is_true, ASN1_Type type_tag, ASN1_Class class_tag) {
4,185✔
269
   uint8_t val = is_true ? 0xFF : 0x00;
4,185✔
270
   return add_object(type_tag, class_tag, &val, 1);
4,185✔
271
}
272

273
/*
274
* DER encode a small INTEGER
275
*/
276
DER_Encoder& DER_Encoder::encode(size_t n, ASN1_Type type_tag, ASN1_Class class_tag) {
23✔
277
   return encode(BigInt::from_u64(n), type_tag, class_tag);
46✔
278
}
279

280
/*
281
* DER encode an INTEGER
282
*/
283
DER_Encoder& DER_Encoder::encode(const BigInt& n, ASN1_Type type_tag, ASN1_Class class_tag) {
40,476✔
284
   if(n == 0) {
40,476✔
285
      return add_object(type_tag, class_tag, 0);
5,839✔
286
   }
287

288
   const size_t extra_zero = (n.bits() % 8 == 0) ? 1 : 0;
34,637✔
289
   secure_vector<uint8_t> contents(extra_zero + n.bytes());
34,637✔
290
   n.binary_encode(&contents[extra_zero]);
34,637✔
291
   if(n < 0) {
34,637✔
292
      for(unsigned char& content : contents) {
×
293
         content = ~content;
×
294
      }
295
      for(size_t i = contents.size(); i > 0; --i) {
×
296
         if(++contents[i - 1]) {
×
297
            break;
298
         }
299
      }
300
   }
301

302
   return add_object(type_tag, class_tag, contents);
34,637✔
303
}
40,476✔
304

305
/*
306
* DER encode an OCTET STRING or BIT STRING
307
*/
308
DER_Encoder& DER_Encoder::encode(
29,411✔
309
   const uint8_t bytes[], size_t length, ASN1_Type real_type, ASN1_Type type_tag, ASN1_Class class_tag) {
310
   if(real_type != ASN1_Type::OctetString && real_type != ASN1_Type::BitString) {
29,411✔
311
      throw Invalid_Argument("DER_Encoder: Invalid tag for byte/bit string");
×
312
   }
313

314
   if(real_type == ASN1_Type::BitString) {
29,411✔
315
      secure_vector<uint8_t> encoded;
18,435✔
316
      encoded.push_back(0);
18,435✔
317
      encoded += std::make_pair(bytes, length);
18,435✔
318
      return add_object(type_tag, class_tag, encoded);
18,435✔
319
   } else {
18,435✔
320
      return add_object(type_tag, class_tag, bytes, length);
10,976✔
321
   }
322
}
323

324
DER_Encoder& DER_Encoder::encode(const ASN1_Object& obj) {
69,507✔
325
   obj.encode_into(*this);
69,507✔
326
   return (*this);
69,507✔
327
}
328

329
/*
330
* Write the encoding of the byte(s)
331
*/
332
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, std::string_view rep_str) {
4,459✔
333
   const uint8_t* rep = cast_char_ptr_to_uint8(rep_str.data());
4,459✔
334
   const size_t rep_len = rep_str.size();
4,459✔
335
   return add_object(type_tag, class_tag, rep, rep_len);
4,459✔
336
}
337

338
/*
339
* Write the encoding of the byte
340
*/
341
DER_Encoder& DER_Encoder::add_object(ASN1_Type type_tag, ASN1_Class class_tag, uint8_t rep) {
5,839✔
342
   return add_object(type_tag, class_tag, &rep, 1);
5,839✔
343
}
344

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