bytestring_test.cc
Go to the documentation of this file.
1 /* Copyright (c) 2014, Google Inc.
2  *
3  * Permission to use, copy, modify, and/or distribute this software for any
4  * purpose with or without fee is hereby granted, provided that the above
5  * copyright notice and this permission notice appear in all copies.
6  *
7  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
8  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
9  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
10  * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
11  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
12  * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
13  * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
14 
15 #include <stdio.h>
16 #include <stdlib.h>
17 #include <string.h>
18 
19 #include <vector>
20 
21 #include <gtest/gtest.h>
22 
23 #include <openssl/bytestring.h>
24 #include <openssl/crypto.h>
25 #include <openssl/span.h>
26 
27 #include "internal.h"
28 #include "../internal.h"
29 #include "../test/test_util.h"
30 
31 
32 TEST(CBSTest, Skip) {
33  static const uint8_t kData[] = {1, 2, 3};
34  CBS data;
35 
36  CBS_init(&data, kData, sizeof(kData));
37  EXPECT_EQ(3u, CBS_len(&data));
39  EXPECT_EQ(2u, CBS_len(&data));
41  EXPECT_EQ(0u, CBS_len(&data));
43 }
44 
45 TEST(CBSTest, GetUint) {
46  static const uint8_t kData[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10,
47  11, 12, 13, 14, 15, 16, 17, 18, 19, 20};
48  uint8_t u8;
49  uint16_t u16;
50  uint32_t u32;
51  uint64_t u64;
52  CBS data;
53 
54  CBS_init(&data, kData, sizeof(kData));
55  ASSERT_TRUE(CBS_get_u8(&data, &u8));
56  EXPECT_EQ(1u, u8);
57  ASSERT_TRUE(CBS_get_u16(&data, &u16));
58  EXPECT_EQ(0x203u, u16);
59  ASSERT_TRUE(CBS_get_u24(&data, &u32));
60  EXPECT_EQ(0x40506u, u32);
61  ASSERT_TRUE(CBS_get_u32(&data, &u32));
62  EXPECT_EQ(0x708090au, u32);
63  ASSERT_TRUE(CBS_get_u64(&data, &u64));
64  EXPECT_EQ(0xb0c0d0e0f101112u, u64);
66  EXPECT_EQ(0x14u, u8);
68  EXPECT_EQ(0x13u, u8);
71 
72  CBS_init(&data, kData, sizeof(kData));
74  EXPECT_EQ(0x0201u, u16);
76  EXPECT_EQ(0x06050403u, u32);
78  EXPECT_EQ(0x0e0d0c0b0a090807u, u64);
79 }
80 
81 TEST(CBSTest, GetPrefixed) {
82  static const uint8_t kData[] = {1, 2, 0, 2, 3, 4, 0, 0, 3, 3, 2, 1};
83  uint8_t u8;
84  uint16_t u16;
85  uint32_t u32;
86  CBS data, prefixed;
87 
88  CBS_init(&data, kData, sizeof(kData));
90  EXPECT_EQ(1u, CBS_len(&prefixed));
91  ASSERT_TRUE(CBS_get_u8(&prefixed, &u8));
92  EXPECT_EQ(2u, u8);
94  EXPECT_EQ(2u, CBS_len(&prefixed));
95  ASSERT_TRUE(CBS_get_u16(&prefixed, &u16));
96  EXPECT_EQ(0x304u, u16);
98  EXPECT_EQ(3u, CBS_len(&prefixed));
99  ASSERT_TRUE(CBS_get_u24(&prefixed, &u32));
100  EXPECT_EQ(0x30201u, u32);
101 }
102 
103 TEST(CBSTest, GetPrefixedBad) {
104  static const uint8_t kData1[] = {2, 1};
105  static const uint8_t kData2[] = {0, 2, 1};
106  static const uint8_t kData3[] = {0, 0, 2, 1};
107  CBS data, prefixed;
108 
109  CBS_init(&data, kData1, sizeof(kData1));
111 
112  CBS_init(&data, kData2, sizeof(kData2));
114 
115  CBS_init(&data, kData3, sizeof(kData3));
117 }
118 
119 TEST(CBSTest, GetUntilFirst) {
120  static const uint8_t kData[] = {0, 1, 2, 3, 0, 1, 2, 3};
121  CBS data;
122  CBS_init(&data, kData, sizeof(kData));
123 
124  CBS prefix;
126  EXPECT_EQ(CBS_data(&data), kData);
127  EXPECT_EQ(CBS_len(&data), sizeof(kData));
128 
130  EXPECT_EQ(CBS_len(&prefix), 0u);
131  EXPECT_EQ(CBS_data(&data), kData);
132  EXPECT_EQ(CBS_len(&data), sizeof(kData));
133 
135  EXPECT_EQ(CBS_data(&prefix), kData);
136  EXPECT_EQ(CBS_len(&prefix), 2u);
137  EXPECT_EQ(CBS_data(&data), kData + 2);
138  EXPECT_EQ(CBS_len(&data), sizeof(kData) - 2);
139 }
140 
141 TEST(CBSTest, GetASN1) {
142  static const uint8_t kData1[] = {0x30, 2, 1, 2};
143  static const uint8_t kData2[] = {0x30, 3, 1, 2};
144  static const uint8_t kData3[] = {0x30, 0x80};
145  static const uint8_t kData4[] = {0x30, 0x81, 1, 1};
146  static const uint8_t kData5[4 + 0x80] = {0x30, 0x82, 0, 0x80};
147  static const uint8_t kData6[] = {0xa1, 3, 0x4, 1, 1};
148  static const uint8_t kData7[] = {0xa1, 3, 0x4, 2, 1};
149  static const uint8_t kData8[] = {0xa1, 3, 0x2, 1, 1};
150  static const uint8_t kData9[] = {0xa1, 3, 0x2, 1, 0xff};
151 
152  CBS data, contents;
153  int present;
154  uint64_t value;
155 
156  CBS_init(&data, kData1, sizeof(kData1));
159 
161  EXPECT_EQ(Bytes("\x01\x02"), Bytes(CBS_data(&contents), CBS_len(&contents)));
162 
163  CBS_init(&data, kData2, sizeof(kData2));
164  // data is truncated
166 
167  CBS_init(&data, kData3, sizeof(kData3));
168  // zero byte length of length
170 
171  CBS_init(&data, kData4, sizeof(kData4));
172  // long form mistakenly used.
174 
175  CBS_init(&data, kData5, sizeof(kData5));
176  // length takes too many bytes.
178 
179  CBS_init(&data, kData1, sizeof(kData1));
180  // wrong tag.
182 
183  CBS_init(&data, NULL, 0);
184  // peek at empty data.
186 
187  CBS_init(&data, NULL, 0);
188  // optional elements at empty data.
190  &data, &contents, &present,
192  EXPECT_FALSE(present);
194  &data, &contents, &present,
196  EXPECT_FALSE(present);
199  &data, &contents, NULL,
204  EXPECT_EQ(42u, value);
205 
206  CBS_init(&data, kData6, sizeof(kData6));
207  // optional element.
209  &data, &contents, &present,
211  EXPECT_FALSE(present);
213  &data, &contents, &present,
215  EXPECT_TRUE(present);
216  EXPECT_EQ(Bytes("\x04\x01\x01"),
218 
219  CBS_init(&data, kData6, sizeof(kData6));
220  // optional octet string.
222  &data, &contents, &present,
224  EXPECT_FALSE(present);
227  &data, &contents, &present,
229  EXPECT_TRUE(present);
231 
232  CBS_init(&data, kData7, sizeof(kData7));
233  // invalid optional octet string.
235  &data, &contents, &present,
237 
238  CBS_init(&data, kData8, sizeof(kData8));
239  // optional integer.
242  EXPECT_EQ(42u, value);
245  EXPECT_EQ(1u, value);
246 
247  CBS_init(&data, kData9, sizeof(kData9));
248  // invalid optional integer.
251 
252  unsigned tag;
253  CBS_init(&data, kData1, sizeof(kData1));
256  EXPECT_EQ(Bytes("\x01\x02"), Bytes(CBS_data(&contents), CBS_len(&contents)));
257 
258  size_t header_len;
259  CBS_init(&data, kData1, sizeof(kData1));
262  EXPECT_EQ(2u, header_len);
263  EXPECT_EQ(Bytes("\x30\x02\x01\x02"),
265 }
266 
267 TEST(CBSTest, ParseASN1Tag) {
268  const struct {
269  bool ok;
270  unsigned tag;
271  std::vector<uint8_t> in;
272  } kTests[] = {
273  {true, CBS_ASN1_SEQUENCE, {0x30, 0}},
274  {true, CBS_ASN1_CONTEXT_SPECIFIC | CBS_ASN1_CONSTRUCTED | 4, {0xa4, 0}},
275  {true, CBS_ASN1_APPLICATION | 30, {0x5e, 0}},
276  {true, CBS_ASN1_APPLICATION | 31, {0x5f, 0x1f, 0}},
277  {true, CBS_ASN1_APPLICATION | 32, {0x5f, 0x20, 0}},
278  {true,
279  CBS_ASN1_PRIVATE | CBS_ASN1_CONSTRUCTED | 0x1fffffff,
280  {0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 0}},
281  // Tag number fits in unsigned but not |CBS_ASN1_TAG_NUMBER_MASK|.
282  {false, 0, {0xff, 0x82, 0xff, 0xff, 0xff, 0x7f, 0}},
283  // Tag number does not fit in unsigned.
284  {false, 0, {0xff, 0x90, 0x80, 0x80, 0x80, 0, 0}},
285  // Tag number is not minimally-encoded
286  {false, 0, {0x5f, 0x80, 0x1f, 0}},
287  // Tag number should have used short form.
288  {false, 0, {0x5f, 0x80, 0x1e, 0}},
289  };
290  for (const auto &t : kTests) {
291  SCOPED_TRACE(Bytes(t.in));
292  unsigned tag;
293  CBS cbs, child;
294  CBS_init(&cbs, t.in.data(), t.in.size());
295  ASSERT_EQ(t.ok, !!CBS_get_any_asn1(&cbs, &child, &tag));
296  if (t.ok) {
297  EXPECT_EQ(t.tag, tag);
298  EXPECT_EQ(0u, CBS_len(&child));
299  EXPECT_EQ(0u, CBS_len(&cbs));
300 
301  CBS_init(&cbs, t.in.data(), t.in.size());
303  EXPECT_FALSE(CBS_peek_asn1_tag(&cbs, t.tag + 1));
304 
305  EXPECT_TRUE(CBS_get_asn1(&cbs, &child, t.tag));
306  EXPECT_EQ(0u, CBS_len(&child));
307  EXPECT_EQ(0u, CBS_len(&cbs));
308 
309  CBS_init(&cbs, t.in.data(), t.in.size());
310  EXPECT_FALSE(CBS_get_asn1(&cbs, &child, t.tag + 1));
311  }
312  }
313 }
314 
315 TEST(CBSTest, GetOptionalASN1Bool) {
316  static const uint8_t kTrue[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0xff};
317  static const uint8_t kFalse[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x00};
318  static const uint8_t kInvalid[] = {0x0a, 3, CBS_ASN1_BOOLEAN, 1, 0x01};
319 
320  CBS data;
321  CBS_init(&data, NULL, 0);
322  int val = 2;
323  ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0));
324  EXPECT_EQ(0, val);
325 
326  CBS_init(&data, kTrue, sizeof(kTrue));
327  val = 2;
328  ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 0));
329  EXPECT_EQ(1, val);
330 
331  CBS_init(&data, kFalse, sizeof(kFalse));
332  val = 2;
333  ASSERT_TRUE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1));
334  EXPECT_EQ(0, val);
335 
336  CBS_init(&data, kInvalid, sizeof(kInvalid));
337  EXPECT_FALSE(CBS_get_optional_asn1_bool(&data, &val, 0x0a, 1));
338 }
339 
340 // Test that CBB_init may be used on an uninitialized input.
341 TEST(CBBTest, InitUninitialized) {
342  CBB cbb;
343  ASSERT_TRUE(CBB_init(&cbb, 100));
344  CBB_cleanup(&cbb);
345 }
346 
347 TEST(CBBTest, Basic) {
348  static const uint8_t kExpected[] = {
349  0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
350  0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14,
351  0x03, 0x02, 0x0a, 0x09, 0x08, 0x07, 0x12, 0x11, 0x10, 0x0f,
352  0x0e, 0x0d, 0x0c, 0x0b, 0x00, 0x00, 0x00, 0x00};
353  uint8_t *buf;
354  size_t buf_len;
355 
356  bssl::ScopedCBB cbb;
357  ASSERT_TRUE(CBB_init(cbb.get(), 100));
358  cbb.Reset();
359 
360  ASSERT_TRUE(CBB_init(cbb.get(), 0));
361  ASSERT_TRUE(CBB_add_zeros(cbb.get(), 0));
362  ASSERT_TRUE(CBB_add_u8(cbb.get(), 1));
363  ASSERT_TRUE(CBB_add_u16(cbb.get(), 0x203));
364  ASSERT_TRUE(CBB_add_u24(cbb.get(), 0x40506));
365  ASSERT_TRUE(CBB_add_u32(cbb.get(), 0x708090a));
366  ASSERT_TRUE(CBB_add_u64(cbb.get(), 0xb0c0d0e0f101112));
367  ASSERT_TRUE(CBB_add_bytes(cbb.get(), (const uint8_t *)"\x13\x14", 2));
368  ASSERT_TRUE(CBB_add_u16le(cbb.get(), 0x203));
369  ASSERT_TRUE(CBB_add_u32le(cbb.get(), 0x708090a));
370  ASSERT_TRUE(CBB_add_u64le(cbb.get(), 0xb0c0d0e0f101112));
371  ASSERT_TRUE(CBB_add_zeros(cbb.get(), 4));
372  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
373 
374  bssl::UniquePtr<uint8_t> scoper(buf);
375  EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len));
376 }
377 
378 TEST(CBBTest, Fixed) {
379  bssl::ScopedCBB cbb;
380  uint8_t buf[1];
381  uint8_t *out_buf;
382  size_t out_size;
383 
384  ASSERT_TRUE(CBB_init_fixed(cbb.get(), NULL, 0));
385  ASSERT_TRUE(CBB_finish(cbb.get(), &out_buf, &out_size));
386  EXPECT_EQ(NULL, out_buf);
387  EXPECT_EQ(0u, out_size);
388 
389  cbb.Reset();
390  ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, 1));
391  ASSERT_TRUE(CBB_add_u8(cbb.get(), 1));
392  ASSERT_TRUE(CBB_finish(cbb.get(), &out_buf, &out_size));
393  EXPECT_EQ(buf, out_buf);
394  EXPECT_EQ(1u, out_size);
395  EXPECT_EQ(1u, buf[0]);
396 
397  cbb.Reset();
398  ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, 1));
399  ASSERT_TRUE(CBB_add_u8(cbb.get(), 1));
400  EXPECT_FALSE(CBB_add_u8(cbb.get(), 2));
401 }
402 
403 // Test that calling CBB_finish on a child does nothing.
404 TEST(CBBTest, FinishChild) {
405  CBB child;
406  uint8_t *out_buf;
407  size_t out_size;
408 
409  bssl::ScopedCBB cbb;
410  ASSERT_TRUE(CBB_init(cbb.get(), 16));
412 
413  EXPECT_FALSE(CBB_finish(&child, &out_buf, &out_size));
414 
415  ASSERT_TRUE(CBB_finish(cbb.get(), &out_buf, &out_size));
416  bssl::UniquePtr<uint8_t> scoper(out_buf);
417  ASSERT_EQ(1u, out_size);
418  EXPECT_EQ(0u, out_buf[0]);
419 }
420 
421 TEST(CBBTest, Prefixed) {
422  static const uint8_t kExpected[] = {0, 1, 1, 0, 2, 2, 3, 0, 0, 3,
423  4, 5, 6, 5, 4, 1, 0, 1, 2};
424  uint8_t *buf;
425  size_t buf_len;
426  bssl::ScopedCBB cbb;
427  CBB contents, inner_contents, inner_inner_contents;
428  ASSERT_TRUE(CBB_init(cbb.get(), 0));
429  EXPECT_EQ(0u, CBB_len(cbb.get()));
434  ASSERT_TRUE(CBB_flush(cbb.get()));
435  EXPECT_EQ(3u, CBB_len(cbb.get()));
437  ASSERT_TRUE(CBB_add_u16(&contents, 0x203));
439  ASSERT_TRUE(CBB_add_u24(&contents, 0x40506));
441  ASSERT_TRUE(CBB_add_u8_length_prefixed(&contents, &inner_contents));
442  ASSERT_TRUE(CBB_add_u8(&inner_contents, 1));
443  ASSERT_TRUE(
444  CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents));
445  ASSERT_TRUE(CBB_add_u8(&inner_inner_contents, 2));
446  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
447 
448  bssl::UniquePtr<uint8_t> scoper(buf);
449  EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len));
450 }
451 
452 TEST(CBBTest, DiscardChild) {
453  bssl::ScopedCBB cbb;
454  CBB contents, inner_contents, inner_inner_contents;
455 
456  ASSERT_TRUE(CBB_init(cbb.get(), 0));
457  ASSERT_TRUE(CBB_add_u8(cbb.get(), 0xaa));
458 
459  // Discarding |cbb|'s children preserves the byte written.
460  CBB_discard_child(cbb.get());
461 
466  ASSERT_TRUE(CBB_add_u16(&contents, 0xcccc));
468  ASSERT_TRUE(CBB_add_u24(&contents, 0xdddddd));
471  ASSERT_TRUE(CBB_add_u8_length_prefixed(&contents, &inner_contents));
472  ASSERT_TRUE(CBB_add_u8(&inner_contents, 0x42));
473  ASSERT_TRUE(
474  CBB_add_u16_length_prefixed(&inner_contents, &inner_inner_contents));
475  ASSERT_TRUE(CBB_add_u8(&inner_inner_contents, 0x99));
476 
477  // Discard everything from |inner_contents| down.
479 
480  uint8_t *buf;
481  size_t buf_len;
482  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
483  bssl::UniquePtr<uint8_t> scoper(buf);
484 
485  static const uint8_t kExpected[] = {
486  0xaa,
487  0,
488  1, 0xbb,
489  0, 2, 0xcc, 0xcc,
490  0, 0, 3, 0xdd, 0xdd, 0xdd,
491  1, 0xff,
492  };
493  EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len));
494 }
495 
496 TEST(CBBTest, Misuse) {
497  bssl::ScopedCBB cbb;
498  CBB child, contents;
499  uint8_t *buf;
500  size_t buf_len;
501 
502  ASSERT_TRUE(CBB_init(cbb.get(), 0));
505  ASSERT_TRUE(CBB_add_u8(cbb.get(), 2));
506 
507  // Since we wrote to |cbb|, |child| is now invalid and attempts to write to
508  // it should fail.
515  EXPECT_FALSE(CBB_add_bytes(&child, (const uint8_t*) "a", 1));
516 
517  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
518  bssl::UniquePtr<uint8_t> scoper(buf);
519 
520  EXPECT_EQ(Bytes("\x01\x01\x02"), Bytes(buf, buf_len));
521 }
522 
523 TEST(CBBTest, ASN1) {
524  static const uint8_t kExpected[] = {
525  // SEQUENCE { 1 2 3 }
526  0x30, 3, 1, 2, 3,
527  // [4 CONSTRUCTED] { 4 5 6 }
528  0xa4, 3, 4, 5, 6,
529  // [APPLICATION 30 PRIMITIVE] { 7 8 9 }
530  0x5e, 3, 7, 8, 9,
531  // [APPLICATION 31 PRIMITIVE] { 10 11 12 }
532  0x5f, 0x1f, 3, 10, 11, 12,
533  // [PRIVATE 2^29-1 CONSTRUCTED] { 13 14 15 }
534  0xff, 0x81, 0xff, 0xff, 0xff, 0x7f, 3, 13, 14, 15,
535  };
536  uint8_t *buf;
537  size_t buf_len;
538  bssl::ScopedCBB cbb;
539  CBB contents, inner_contents;
540 
541  ASSERT_TRUE(CBB_init(cbb.get(), 0));
543  ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x01\x02\x03", 3));
544  ASSERT_TRUE(
545  CBB_add_asn1(cbb.get(), &contents,
547  ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x04\x05\x06", 3));
548  ASSERT_TRUE(
549  CBB_add_asn1(cbb.get(), &contents,
550  CBS_ASN1_APPLICATION | 30));
551  ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x07\x08\x09", 3));
552  ASSERT_TRUE(
553  CBB_add_asn1(cbb.get(), &contents,
554  CBS_ASN1_APPLICATION | 31));
555  ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x0a\x0b\x0c", 3));
556  ASSERT_TRUE(
557  CBB_add_asn1(cbb.get(), &contents,
558  CBS_ASN1_PRIVATE | CBS_ASN1_CONSTRUCTED | 0x1fffffff));
559  ASSERT_TRUE(CBB_add_bytes(&contents, (const uint8_t *)"\x0d\x0e\x0f", 3));
560  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
561  bssl::UniquePtr<uint8_t> scoper(buf);
562 
563  EXPECT_EQ(Bytes(kExpected), Bytes(buf, buf_len));
564 
565  std::vector<uint8_t> test_data(100000, 0x42);
566  ASSERT_TRUE(CBB_init(cbb.get(), 0));
568  ASSERT_TRUE(CBB_add_bytes(&contents, test_data.data(), 130));
569  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
570  scoper.reset(buf);
571 
572  ASSERT_EQ(3u + 130u, buf_len);
573  EXPECT_EQ(Bytes("\x30\x81\x82"), Bytes(buf, 3));
574  EXPECT_EQ(Bytes(test_data.data(), 130), Bytes(buf + 3, 130));
575 
576  ASSERT_TRUE(CBB_init(cbb.get(), 0));
578  ASSERT_TRUE(CBB_add_bytes(&contents, test_data.data(), 1000));
579  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
580  scoper.reset(buf);
581 
582  ASSERT_EQ(4u + 1000u, buf_len);
583  EXPECT_EQ(Bytes("\x30\x82\x03\xe8"), Bytes(buf, 4));
584  EXPECT_EQ(Bytes(test_data.data(), 1000), Bytes(buf + 4, 1000));
585 
586  ASSERT_TRUE(CBB_init(cbb.get(), 0));
588  ASSERT_TRUE(CBB_add_asn1(&contents, &inner_contents, CBS_ASN1_SEQUENCE));
589  ASSERT_TRUE(CBB_add_bytes(&inner_contents, test_data.data(), 100000));
590  ASSERT_TRUE(CBB_finish(cbb.get(), &buf, &buf_len));
591  scoper.reset(buf);
592 
593  ASSERT_EQ(5u + 5u + 100000u, buf_len);
594  EXPECT_EQ(Bytes("\x30\x83\x01\x86\xa5\x30\x83\x01\x86\xa0"), Bytes(buf, 10));
595  EXPECT_EQ(Bytes(test_data.data(), test_data.size()), Bytes(buf + 10, 100000));
596 }
597 
598 static void ExpectBerConvert(const char *name,
599  bssl::Span<const uint8_t> der_expected,
600  bssl::Span<const uint8_t> ber) {
602  CBS in, out;
603  uint8_t *storage;
604 
605  CBS_init(&in, ber.data(), ber.size());
607  bssl::UniquePtr<uint8_t> scoper(storage);
608 
609  EXPECT_EQ(Bytes(der_expected), Bytes(CBS_data(&out), CBS_len(&out)));
610  if (storage != nullptr) {
611  EXPECT_NE(Bytes(der_expected), Bytes(ber));
612  } else {
613  EXPECT_EQ(Bytes(der_expected), Bytes(ber));
614  }
615 }
616 
617 TEST(CBSTest, BerConvert) {
618  static const uint8_t kSimpleBER[] = {0x01, 0x01, 0x00};
619 
620  // kNonMinimalLengthBER has a non-minimally encoded length.
621  static const uint8_t kNonMinimalLengthBER[] = {0x02, 0x82, 0x00, 0x01, 0x01};
622  static const uint8_t kNonMinimalLengthDER[] = {0x02, 0x01, 0x01};
623 
624  // kIndefBER contains a SEQUENCE with an indefinite length.
625  static const uint8_t kIndefBER[] = {0x30, 0x80, 0x01, 0x01, 0x02, 0x00, 0x00};
626  static const uint8_t kIndefDER[] = {0x30, 0x03, 0x01, 0x01, 0x02};
627 
628  // kIndefBER2 contains a constructed [APPLICATION 31] with an indefinite
629  // length.
630  static const uint8_t kIndefBER2[] = {0x7f, 0x1f, 0x80, 0x01,
631  0x01, 0x02, 0x00, 0x00};
632  static const uint8_t kIndefDER2[] = {0x7f, 0x1f, 0x03, 0x01, 0x01, 0x02};
633 
634  // kOctetStringBER contains an indefinite length OCTET STRING with two parts.
635  // These parts need to be concatenated in DER form.
636  static const uint8_t kOctetStringBER[] = {0x24, 0x80, 0x04, 0x02, 0, 1,
637  0x04, 0x02, 2, 3, 0x00, 0x00};
638  static const uint8_t kOctetStringDER[] = {0x04, 0x04, 0, 1, 2, 3};
639 
640  // kNSSBER is part of a PKCS#12 message generated by NSS that uses indefinite
641  // length elements extensively.
642  static const uint8_t kNSSBER[] = {
643  0x30, 0x80, 0x02, 0x01, 0x03, 0x30, 0x80, 0x06, 0x09, 0x2a, 0x86, 0x48,
644  0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x80, 0x24, 0x80, 0x04, 0x04,
645  0x01, 0x02, 0x03, 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x30, 0x39,
646  0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05,
647  0x00, 0x04, 0x14, 0x84, 0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90,
648  0xc1, 0xb6, 0xe8, 0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04,
649  0x10, 0x38, 0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b,
650  0xf0, 0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0, 0x00, 0x00,
651  };
652 
653  static const uint8_t kNSSDER[] = {
654  0x30, 0x53, 0x02, 0x01, 0x03, 0x30, 0x13, 0x06, 0x09, 0x2a, 0x86,
655  0x48, 0x86, 0xf7, 0x0d, 0x01, 0x07, 0x01, 0xa0, 0x06, 0x04, 0x04,
656  0x01, 0x02, 0x03, 0x04, 0x30, 0x39, 0x30, 0x21, 0x30, 0x09, 0x06,
657  0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14, 0x84,
658  0x98, 0xfc, 0x66, 0x33, 0xee, 0xba, 0xe7, 0x90, 0xc1, 0xb6, 0xe8,
659  0x8f, 0xfe, 0x1d, 0xc5, 0xa5, 0x97, 0x93, 0x3e, 0x04, 0x10, 0x38,
660  0x62, 0xc6, 0x44, 0x12, 0xd5, 0x30, 0x00, 0xf8, 0xf2, 0x1b, 0xf0,
661  0x6e, 0x10, 0x9b, 0xb8, 0x02, 0x02, 0x07, 0xd0,
662  };
663 
664  // kConstructedStringBER contains a deeply-nested constructed OCTET STRING.
665  // The BER conversion collapses this to one level deep, but not completely.
666  static const uint8_t kConstructedStringBER[] = {
667  0xa0, 0x10, 0x24, 0x06, 0x04, 0x01, 0x00, 0x04, 0x01,
668  0x01, 0x24, 0x06, 0x04, 0x01, 0x02, 0x04, 0x01, 0x03,
669  };
670  static const uint8_t kConstructedStringDER[] = {
671  0xa0, 0x08, 0x04, 0x02, 0x00, 0x01, 0x04, 0x02, 0x02, 0x03,
672  };
673 
674  // kConstructedBitString contains a BER constructed BIT STRING. These are not
675  // supported and thus are left unchanged.
676  static const uint8_t kConstructedBitStringBER[] = {
677  0x23, 0x0a, 0x03, 0x03, 0x00, 0x12, 0x34, 0x03, 0x03, 0x00, 0x56, 0x78};
678 
679  ExpectBerConvert("kSimpleBER", kSimpleBER, kSimpleBER);
680  ExpectBerConvert("kNonMinimalLengthBER", kNonMinimalLengthDER,
681  kNonMinimalLengthBER);
682  ExpectBerConvert("kIndefBER", kIndefDER, kIndefBER);
683  ExpectBerConvert("kIndefBER2", kIndefDER2, kIndefBER2);
684  ExpectBerConvert("kOctetStringBER", kOctetStringDER, kOctetStringBER);
685  ExpectBerConvert("kNSSBER", kNSSDER, kNSSBER);
686  ExpectBerConvert("kConstructedStringBER", kConstructedStringDER,
687  kConstructedStringBER);
688  ExpectBerConvert("kConstructedBitStringBER", kConstructedBitStringBER,
689  kConstructedBitStringBER);
690 }
691 
692 struct BERTest {
693  const char *in_hex;
694  bool ok;
695  bool ber_found;
696  unsigned tag;
697 };
698 
699 static const BERTest kBERTests[] = {
700  // Trivial cases, also valid DER.
701  {"0000", true, false, 0},
702  {"0100", true, false, 1},
703  {"020101", true, false, 2},
704 
705  // Non-minimally encoded lengths.
706  {"02810101", true, true, 2},
707  {"0282000101", true, true, 2},
708  {"028300000101", true, true, 2},
709  {"02840000000101", true, true, 2},
710  // Technically valid BER, but not handled.
711  {"02850000000101", false, false, 0},
712 
713  {"0280", false, false, 0}, // Indefinite length, but not constructed.
714  {"2280", true, true, CBS_ASN1_CONSTRUCTED | 2}, // Indefinite length.
715  {"3f0000", false, false, 0}, // Invalid extended tag zero (X.690 8.1.2.4.2.c)
716  {"1f0100", false, false, 0}, // Should be a low-number tag form, even in BER.
717  {"1f4000", true, false, 0x40},
718  {"1f804000", false, false, 0}, // Non-minimal tags are invalid, even in BER.
719 };
720 
721 TEST(CBSTest, BERElementTest) {
722  for (const auto &test : kBERTests) {
723  SCOPED_TRACE(test.in_hex);
724 
725  std::vector<uint8_t> in_bytes;
726  ASSERT_TRUE(DecodeHex(&in_bytes, test.in_hex));
727  CBS in(in_bytes);
728  CBS out;
729  unsigned tag;
730  size_t header_len;
731  int ber_found;
732  int ok =
733  CBS_get_any_ber_asn1_element(&in, &out, &tag, &header_len, &ber_found);
734  ASSERT_TRUE((ok == 1) == test.ok);
735  if (!test.ok) {
736  continue;
737  }
738 
739  EXPECT_TRUE((ber_found == 1) == test.ber_found);
740  EXPECT_LE(header_len, in_bytes.size());
741  EXPECT_EQ(CBS_len(&out), in_bytes.size());
742  EXPECT_EQ(CBS_len(&in), 0u);
743  EXPECT_EQ(Bytes(out), Bytes(in_bytes));
744  EXPECT_EQ(tag, test.tag);
745  }
746 }
747 
749  const char *in;
750  size_t in_len;
751  bool ok;
752  const char *out;
753  size_t out_len;
754 };
755 
757  // A properly-encoded string.
758  {"\x80\x03\x61\x61\x61", 5, true, "aaa", 3},
759  // An implicit-tagged string.
760  {"\xa0\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, true, "aaa", 3},
761  // |CBS_get_asn1_implicit_string| only accepts one level deep of nesting.
762  {"\xa0\x0b\x24\x06\x04\x01\x61\x04\x01\x61\x04\x01\x61", 13, false, nullptr,
763  0},
764  // The outer tag must match.
765  {"\x81\x03\x61\x61\x61", 5, false, nullptr, 0},
766  {"\xa1\x09\x04\x01\x61\x04\x01\x61\x04\x01\x61", 11, false, nullptr, 0},
767  // The inner tag must match.
768  {"\xa1\x09\x0c\x01\x61\x0c\x01\x61\x0c\x01\x61", 11, false, nullptr, 0},
769 };
770 
771 TEST(CBSTest, ImplicitString) {
772  for (const auto &test : kImplicitStringTests) {
773  SCOPED_TRACE(Bytes(test.in, test.in_len));
774  uint8_t *storage = nullptr;
775  CBS in, out;
776  CBS_init(&in, reinterpret_cast<const uint8_t *>(test.in), test.in_len);
780  bssl::UniquePtr<uint8_t> scoper(storage);
781  EXPECT_EQ(test.ok, static_cast<bool>(ok));
782 
783  if (ok) {
784  EXPECT_EQ(Bytes(test.out, test.out_len),
785  Bytes(CBS_data(&out), CBS_len(&out)));
786  }
787  }
788 }
789 
792  const char *encoding;
793  size_t encoding_len;
794 };
795 
797  {0, "\x02\x01\x00", 3},
798  {1, "\x02\x01\x01", 3},
799  {127, "\x02\x01\x7f", 3},
800  {128, "\x02\x02\x00\x80", 4},
801  {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
802  {UINT64_C(0x0102030405060708),
803  "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08", 10},
804  {UINT64_C(0xffffffffffffffff),
805  "\x02\x09\x00\xff\xff\xff\xff\xff\xff\xff\xff", 11},
806 };
807 
809  const char *encoding;
810  size_t encoding_len;
811  bool overflow;
812 };
813 
815  // Bad tag.
816  {"\x03\x01\x00", 3, false},
817  // Empty contents.
818  {"\x02\x00", 2, false},
819  // Negative number.
820  {"\x02\x01\x80", 3, false},
821  // Overflow.
822  {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11, true},
823  // Leading zeros.
824  {"\x02\x02\x00\x01", 4, false},
825 };
826 
827 TEST(CBSTest, ASN1Uint64) {
828  for (const ASN1Uint64Test &test : kASN1Uint64Tests) {
829  SCOPED_TRACE(Bytes(test.encoding, test.encoding_len));
830  SCOPED_TRACE(test.value);
831  CBS cbs;
832  uint64_t value;
833  uint8_t *out;
834  size_t len;
835 
836  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
838  EXPECT_EQ(0u, CBS_len(&cbs));
839  EXPECT_EQ(test.value, value);
840 
841  CBS child;
842  int is_negative;
843  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
846  EXPECT_EQ(0, is_negative);
848 
849  bssl::ScopedCBB cbb;
850  ASSERT_TRUE(CBB_init(cbb.get(), 0));
851  ASSERT_TRUE(CBB_add_asn1_uint64(cbb.get(), test.value));
852  ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len));
853  bssl::UniquePtr<uint8_t> scoper(out);
854  EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len));
855  }
856 
858  SCOPED_TRACE(Bytes(test.encoding, test.encoding_len));
859  CBS cbs;
860  uint64_t value;
861 
862  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
864 
865  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
866  CBS child;
869  }
870  }
871 }
872 
875  const char *encoding;
876  size_t encoding_len;
877 };
878 
879 static const ASN1Int64Test kASN1Int64Tests[] = {
880  {0, "\x02\x01\x00", 3},
881  {1, "\x02\x01\x01", 3},
882  {-1, "\x02\x01\xff", 3},
883  {127, "\x02\x01\x7f", 3},
884  {-127, "\x02\x01\x81", 3},
885  {128, "\x02\x02\x00\x80", 4},
886  {-128, "\x02\x01\x80", 3},
887  {129, "\x02\x02\x00\x81", 4},
888  {-129, "\x02\x02\xff\x7f", 4},
889  {0xdeadbeef, "\x02\x05\x00\xde\xad\xbe\xef", 7},
890  {INT64_C(0x0102030405060708), "\x02\x08\x01\x02\x03\x04\x05\x06\x07\x08",
891  10},
892  {INT64_MIN, "\x02\x08\x80\x00\x00\x00\x00\x00\x00\x00", 10},
893  {INT64_MAX, "\x02\x08\x7f\xff\xff\xff\xff\xff\xff\xff", 10},
894 };
895 
897  const char *encoding;
898  size_t encoding_len;
899  bool overflow;
900 };
901 
903  // Bad tag.
904  {"\x03\x01\x00", 3, false},
905  // Empty contents.
906  {"\x02\x00", 2, false},
907  // Overflow.
908  {"\x02\x09\x01\x00\x00\x00\x00\x00\x00\x00\x00", 11, true},
909  // Underflow.
910  {"\x02\x09\x08\xff\xff\xff\xff\xff\xff\xff\xff", 11, true},
911  // Leading zeros.
912  {"\x02\x02\x00\x01", 4, false},
913  // Leading 0xff.
914  {"\x02\x02\xff\xff", 4, false},
915 };
916 
917 TEST(CBSTest, ASN1Int64) {
918  for (const ASN1Int64Test &test : kASN1Int64Tests) {
919  SCOPED_TRACE(Bytes(test.encoding, test.encoding_len));
920  SCOPED_TRACE(test.value);
921  CBS cbs;
922  int64_t value;
923  uint8_t *out;
924  size_t len;
925 
926  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
928  EXPECT_EQ(0u, CBS_len(&cbs));
929  EXPECT_EQ(test.value, value);
930 
931  CBS child;
932  int is_negative;
933  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
936  EXPECT_EQ(test.value < 0, !!is_negative);
938 
939  bssl::ScopedCBB cbb;
940  ASSERT_TRUE(CBB_init(cbb.get(), 0));
941  ASSERT_TRUE(CBB_add_asn1_int64(cbb.get(), test.value));
942  ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len));
943  bssl::UniquePtr<uint8_t> scoper(out);
944  EXPECT_EQ(Bytes(test.encoding, test.encoding_len), Bytes(out, len));
945  }
946 
948  SCOPED_TRACE(Bytes(test.encoding, test.encoding_len));
949  CBS cbs;
950  int64_t value;
951 
952  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
954 
955  CBS_init(&cbs, (const uint8_t *)test.encoding, test.encoding_len);
956  CBS child;
958  EXPECT_EQ(test.overflow, !!CBS_is_valid_asn1_integer(&child, NULL));
959  }
960  }
961 }
962 
963 TEST(CBBTest, Zero) {
964  CBB cbb;
965  CBB_zero(&cbb);
966  // Calling |CBB_cleanup| on a zero-state |CBB| must not crash.
967  CBB_cleanup(&cbb);
968 }
969 
970 TEST(CBBTest, Reserve) {
971  uint8_t buf[10];
972  uint8_t *ptr;
973  size_t len;
974  bssl::ScopedCBB cbb;
975  ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, sizeof(buf)));
976  // Too large.
977  EXPECT_FALSE(CBB_reserve(cbb.get(), &ptr, 11));
978 
979  cbb.Reset();
980  ASSERT_TRUE(CBB_init_fixed(cbb.get(), buf, sizeof(buf)));
981  // Successfully reserve the entire space.
982  ASSERT_TRUE(CBB_reserve(cbb.get(), &ptr, 10));
983  EXPECT_EQ(buf, ptr);
984  // Advancing under the maximum bytes is legal.
985  ASSERT_TRUE(CBB_did_write(cbb.get(), 5));
986  ASSERT_TRUE(CBB_finish(cbb.get(), NULL, &len));
987  EXPECT_EQ(5u, len);
988 }
989 
990 // Test that CBB errors are sticky; once on operation on CBB fails, all
991 // subsequent ones do.
992 TEST(CBBTest, StickyError) {
993  // Write an input that exceeds the limit for its length prefix.
994  bssl::ScopedCBB cbb;
995  CBB child;
996  static const uint8_t kZeros[256] = {0};
997  ASSERT_TRUE(CBB_init(cbb.get(), 0));
999  ASSERT_TRUE(CBB_add_bytes(&child, kZeros, sizeof(kZeros)));
1000  ASSERT_FALSE(CBB_flush(cbb.get()));
1001 
1002  // All future operations should fail.
1003  uint8_t *ptr;
1004  size_t len;
1005  EXPECT_FALSE(CBB_add_u8(cbb.get(), 0));
1006  EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len));
1007 
1008  // Write an input that cannot fit in a fixed CBB.
1009  cbb.Reset();
1010  uint8_t buf;
1011  ASSERT_TRUE(CBB_init_fixed(cbb.get(), &buf, 1));
1012  ASSERT_FALSE(CBB_add_bytes(cbb.get(), kZeros, sizeof(kZeros)));
1013 
1014  // All future operations should fail.
1015  EXPECT_FALSE(CBB_add_u8(cbb.get(), 0));
1016  EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len));
1017 
1018  // Write a u32 that cannot fit in a u24.
1019  cbb.Reset();
1020  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1021  ASSERT_FALSE(CBB_add_u24(cbb.get(), 1u << 24));
1022 
1023  // All future operations should fail.
1024  EXPECT_FALSE(CBB_add_u8(cbb.get(), 0));
1025  EXPECT_FALSE(CBB_finish(cbb.get(), &ptr, &len));
1026 }
1027 
1028 TEST(CBSTest, BitString) {
1029  static const std::vector<uint8_t> kValidBitStrings[] = {
1030  {0x00}, // 0 bits
1031  {0x07, 0x80}, // 1 bit
1032  {0x04, 0xf0}, // 4 bits
1033  {0x00, 0xff}, // 8 bits
1034  {0x06, 0xff, 0xff, 0xff, 0xff, 0xff, 0xc0}, // 42 bits
1035  };
1036  for (const auto& test : kValidBitStrings) {
1037  SCOPED_TRACE(Bytes(test.data(), test.size()));
1038  CBS cbs;
1039  CBS_init(&cbs, test.data(), test.size());
1041  }
1042 
1043  static const std::vector<uint8_t> kInvalidBitStrings[] = {
1044  // BIT STRINGs always have a leading byte.
1045  std::vector<uint8_t>{},
1046  // It's not possible to take an unused bit off the empty string.
1047  {0x01},
1048  // There can be at most 7 unused bits.
1049  {0x08, 0xff},
1050  {0xff, 0xff},
1051  // All unused bits must be cleared.
1052  {0x06, 0xff, 0xc1},
1053  };
1054  for (const auto& test : kInvalidBitStrings) {
1055  SCOPED_TRACE(Bytes(test.data(), test.size()));
1056  CBS cbs;
1057  CBS_init(&cbs, test.data(), test.size());
1059 
1060  // CBS_asn1_bitstring_has_bit returns false on invalid inputs.
1062  }
1063 
1064  static const struct {
1065  std::vector<uint8_t> in;
1066  unsigned bit;
1067  bool bit_set;
1068  } kBitTests[] = {
1069  // Basic tests.
1070  {{0x00}, 0, false},
1071  {{0x07, 0x80}, 0, true},
1072  {{0x06, 0x0f, 0x40}, 0, false},
1073  {{0x06, 0x0f, 0x40}, 1, false},
1074  {{0x06, 0x0f, 0x40}, 2, false},
1075  {{0x06, 0x0f, 0x40}, 3, false},
1076  {{0x06, 0x0f, 0x40}, 4, true},
1077  {{0x06, 0x0f, 0x40}, 5, true},
1078  {{0x06, 0x0f, 0x40}, 6, true},
1079  {{0x06, 0x0f, 0x40}, 7, true},
1080  {{0x06, 0x0f, 0x40}, 8, false},
1081  {{0x06, 0x0f, 0x40}, 9, true},
1082  // Out-of-bounds bits return 0.
1083  {{0x06, 0x0f, 0x40}, 10, false},
1084  {{0x06, 0x0f, 0x40}, 15, false},
1085  {{0x06, 0x0f, 0x40}, 16, false},
1086  {{0x06, 0x0f, 0x40}, 1000, false},
1087  };
1088  for (const auto& test : kBitTests) {
1089  SCOPED_TRACE(Bytes(test.in.data(), test.in.size()));
1090  SCOPED_TRACE(test.bit);
1091  CBS cbs;
1092  CBS_init(&cbs, test.in.data(), test.in.size());
1093  EXPECT_EQ(static_cast<int>(test.bit_set),
1095  }
1096 }
1097 
1098 TEST(CBBTest, AddOIDFromText) {
1099  const struct {
1100  const char *text;
1101  std::vector<uint8_t> der;
1102  } kValidOIDs[] = {
1103  // Some valid values.
1104  {"0.0", {0x00}},
1105  {"0.2.3.4", {0x2, 0x3, 0x4}},
1106  {"1.2.3.4", {0x2a, 0x3, 0x4}},
1107  {"2.2.3.4", {0x52, 0x3, 0x4}},
1108  {"1.2.840.113554.4.1.72585",
1109  {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09}},
1110  // Test edge cases around the first component.
1111  {"0.39", {0x27}},
1112  {"1.0", {0x28}},
1113  {"1.39", {0x4f}},
1114  {"2.0", {0x50}},
1115  {"2.1", {0x51}},
1116  {"2.40", {0x78}},
1117  // Edge cases near an overflow.
1118  {"1.2.18446744073709551615",
1119  {0x2a, 0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
1120  {"2.18446744073709551535",
1121  {0x81, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x7f}},
1122  };
1123 
1124  const char *kInvalidTexts[] = {
1125  // Invalid second component.
1126  "0.40",
1127  "1.40",
1128  // Invalid first component.
1129  "3.1",
1130  // The empty string is not an OID.
1131  "",
1132  // No empty components.
1133  ".1.2.3.4.5",
1134  "1..2.3.4.5",
1135  "1.2.3.4.5.",
1136  // There must be at least two components.
1137  "1",
1138  // No extra leading zeros.
1139  "00.1.2.3.4",
1140  "01.1.2.3.4",
1141  // Overflow for both components or 40*A + B.
1142  "1.2.18446744073709551616",
1143  "2.18446744073709551536",
1144  };
1145 
1146  const std::vector<uint8_t> kInvalidDER[] = {
1147  // The empty string is not an OID.
1148  {},
1149  // Non-minimal representation.
1150  {0x80, 0x01},
1151  // Overflow. This is the DER representation of
1152  // 1.2.840.113554.4.1.72585.18446744073709551616. (The final value is
1153  // 2^64.)
1154  {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x12, 0x04, 0x01, 0x84, 0xb7, 0x09,
1155  0x82, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80, 0x00},
1156  };
1157 
1158  for (const auto &t : kValidOIDs) {
1159  SCOPED_TRACE(t.text);
1160 
1161  bssl::ScopedCBB cbb;
1162  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1163  ASSERT_TRUE(CBB_add_asn1_oid_from_text(cbb.get(), t.text, strlen(t.text)));
1164  uint8_t *out;
1165  size_t len;
1166  ASSERT_TRUE(CBB_finish(cbb.get(), &out, &len));
1167  bssl::UniquePtr<uint8_t> free_out(out);
1168  EXPECT_EQ(Bytes(t.der), Bytes(out, len));
1169 
1170  CBS cbs;
1171  CBS_init(&cbs, t.der.data(), t.der.size());
1172  bssl::UniquePtr<char> text(CBS_asn1_oid_to_text(&cbs));
1173  ASSERT_TRUE(text.get());
1174  EXPECT_STREQ(t.text, text.get());
1175  }
1176 
1177  for (const char *t : kInvalidTexts) {
1178  SCOPED_TRACE(t);
1179  bssl::ScopedCBB cbb;
1180  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1181  EXPECT_FALSE(CBB_add_asn1_oid_from_text(cbb.get(), t, strlen(t)));
1182  }
1183 
1184  for (const auto &t : kInvalidDER) {
1185  SCOPED_TRACE(Bytes(t));
1186  CBS cbs;
1187  CBS_init(&cbs, t.data(), t.size());
1188  bssl::UniquePtr<char> text(CBS_asn1_oid_to_text(&cbs));
1189  EXPECT_FALSE(text);
1190  }
1191 }
1192 
1193 TEST(CBBTest, FlushASN1SetOf) {
1194  const struct {
1195  std::vector<uint8_t> in, out;
1196  } kValidInputs[] = {
1197  // No elements.
1198  {{}, {}},
1199  // One element.
1200  {{0x30, 0x00}, {0x30, 0x00}},
1201  // Two identical elements.
1202  {{0x30, 0x00, 0x30, 0x00}, {0x30, 0x00, 0x30, 0x00}},
1203  // clang-format off
1204  {{0x30, 0x02, 0x00, 0x00,
1205  0x30, 0x00,
1206  0x01, 0x00,
1207  0x30, 0x02, 0x00, 0x00,
1208  0x30, 0x03, 0x00, 0x00, 0x00,
1209  0x30, 0x00,
1210  0x30, 0x03, 0x00, 0x00, 0x01,
1211  0x30, 0x01, 0x00,
1212  0x01, 0x01, 0x00},
1213  {0x01, 0x00,
1214  0x01, 0x01, 0x00,
1215  0x30, 0x00,
1216  0x30, 0x00,
1217  0x30, 0x01, 0x00,
1218  0x30, 0x02, 0x00, 0x00,
1219  0x30, 0x02, 0x00, 0x00,
1220  0x30, 0x03, 0x00, 0x00, 0x00,
1221  0x30, 0x03, 0x00, 0x00, 0x01}},
1222  // clang-format on
1223  };
1224 
1225  for (const auto &t : kValidInputs) {
1226  SCOPED_TRACE(Bytes(t.in));
1227 
1228  bssl::ScopedCBB cbb;
1229  CBB child;
1230  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1231  ASSERT_TRUE(CBB_add_asn1(cbb.get(), &child, CBS_ASN1_SET));
1232  ASSERT_TRUE(CBB_add_bytes(&child, t.in.data(), t.in.size()));
1234  EXPECT_EQ(Bytes(t.out), Bytes(CBB_data(&child), CBB_len(&child)));
1235 
1236  // Running it again should be idempotent.
1238  EXPECT_EQ(Bytes(t.out), Bytes(CBB_data(&child), CBB_len(&child)));
1239 
1240  // The ASN.1 header remain intact.
1241  ASSERT_TRUE(CBB_flush(cbb.get()));
1242  EXPECT_EQ(0x31, CBB_data(cbb.get())[0]);
1243  }
1244 
1245  const std::vector<uint8_t> kInvalidInputs[] = {
1246  {0x30},
1247  {0x30, 0x01},
1248  {0x30, 0x00, 0x30, 0x00, 0x30, 0x01},
1249  };
1250 
1251  for (const auto &t : kInvalidInputs) {
1252  SCOPED_TRACE(Bytes(t));
1253 
1254  bssl::ScopedCBB cbb;
1255  CBB child;
1256  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1257  ASSERT_TRUE(CBB_add_asn1(cbb.get(), &child, CBS_ASN1_SET));
1258  ASSERT_TRUE(CBB_add_bytes(&child, t.data(), t.size()));
1260  }
1261 }
1262 
1263 template <class T>
1264 static std::vector<uint8_t> LiteralToBytes(const T *str) {
1265  std::vector<uint8_t> ret;
1266  for (; *str != 0; str++) {
1267  for (size_t i = 0; i < sizeof(T); i++) {
1268  ret.push_back(static_cast<uint8_t>(*str >> (8 * (sizeof(T) - 1 - i))));
1269  }
1270  }
1271  return ret;
1272 }
1273 
1274 static std::vector<uint32_t> LiteralToCodePoints(const char32_t *str) {
1275  std::vector<uint32_t> ret;
1276  for (; *str != 0; str++) {
1277  ret.push_back(static_cast<uint32_t>(*str));
1278  }
1279  return ret;
1280 }
1281 
1282 TEST(CBBTest, Unicode) {
1283  struct {
1284  int (*decode)(CBS *, uint32_t *);
1285  int (*encode)(CBB *, uint32_t);
1286  std::vector<uint8_t> in;
1287  std::vector<uint32_t> out;
1288  bool ok;
1289  } kTests[] = {
1291  // This test string captures all four cases in UTF-8.
1292  LiteralToBytes(u8"Hello, 世界! ¡Hola, 🌎!"),
1293  LiteralToCodePoints(U"Hello, 世界! ¡Hola, 🌎!"), true},
1294 
1295  // Some invalid inputs adapted from
1296  // http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt
1297  // 2.1 First possible sequence of a certain length. (5- and 6-bit
1298  // sequences no longer exist.)
1299  {cbs_get_utf8, cbb_add_utf8, {0xf8, 0x88, 0x80, 0x80, 0x80}, {}, false},
1300  {cbs_get_utf8,
1301  cbb_add_utf8,
1302  {0xfc, 0x84, 0x80, 0x80, 0x80, 0x80},
1303  {},
1304  false},
1305  // 3.1 Unexpected continuation bytes.
1306  {cbs_get_utf8, cbb_add_utf8, {0x80}, {}, false},
1307  {cbs_get_utf8, cbb_add_utf8, {0xbf}, {}, false},
1308  // 3.2 Lonely start characters.
1309  {cbs_get_utf8, cbb_add_utf8, {0xc0, ' '}, {}, false},
1310  {cbs_get_utf8, cbb_add_utf8, {0xe0, ' '}, {}, false},
1311  {cbs_get_utf8, cbb_add_utf8, {0xf0, ' '}, {}, false},
1312  // 3.3 Sequences with last continuation byte missing
1313  {cbs_get_utf8, cbb_add_utf8, {0xc0}, {}, false},
1314  {cbs_get_utf8, cbb_add_utf8, {0xe0, 0x80}, {}, false},
1315  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x80, 0x80}, {}, false},
1316  // Variation of the above with unexpected spaces.
1317  {cbs_get_utf8, cbb_add_utf8, {0xe0, 0x80, ' '}, {}, false},
1318  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x80, 0x80, ' '}, {}, false},
1319  // 4.1 Examples of an overlong ASCII character
1320  {cbs_get_utf8, cbb_add_utf8, {0xc0, 0xaf}, {}, false},
1321  {cbs_get_utf8, cbb_add_utf8, {0xe0, 0x80, 0xaf}, {}, false},
1322  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x80, 0x80, 0xaf}, {}, false},
1323  // 4.2 Maximum overlong sequences
1324  {cbs_get_utf8, cbb_add_utf8, {0xc1, 0xbf}, {}, false},
1325  {cbs_get_utf8, cbb_add_utf8, {0xe0, 0x9f, 0xbf}, {}, false},
1326  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x8f, 0xbf, 0xbf}, {}, false},
1327  // 4.3 Overlong representation of the NUL character
1328  {cbs_get_utf8, cbb_add_utf8, {0xc0, 0x80}, {}, false},
1329  {cbs_get_utf8, cbb_add_utf8, {0xe0, 0x80, 0x80}, {}, false},
1330  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x80, 0x80, 0x80}, {}, false},
1331  // 5.1 Single UTF-16 surrogates
1332  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xa0, 0x80}, {}, false},
1333  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xad, 0xbf}, {}, false},
1334  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xae, 0x80}, {}, false},
1335  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xb0, 0x80}, {}, false},
1336  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xbe, 0x80}, {}, false},
1337  {cbs_get_utf8, cbb_add_utf8, {0xed, 0xbf, 0xbf}, {}, false},
1338  // 5.2 Paired UTF-16 surrogates
1339  {cbs_get_utf8,
1340  cbb_add_utf8,
1341  {0xed, 0xa0, 0x80, 0xed, 0xb0, 0x80},
1342  {},
1343  false},
1344  {cbs_get_utf8,
1345  cbb_add_utf8,
1346  {0xed, 0xa0, 0x80, 0xed, 0xbf, 0xbf},
1347  {},
1348  false},
1349  {cbs_get_utf8,
1350  cbb_add_utf8,
1351  {0xed, 0xad, 0xbf, 0xed, 0xb0, 0x80},
1352  {},
1353  false},
1354  {cbs_get_utf8,
1355  cbb_add_utf8,
1356  {0xed, 0xad, 0xbf, 0xed, 0xbf, 0xbf},
1357  {},
1358  false},
1359  {cbs_get_utf8,
1360  cbb_add_utf8,
1361  {0xed, 0xae, 0x80, 0xed, 0xb0, 0x80},
1362  {},
1363  false},
1364  {cbs_get_utf8,
1365  cbb_add_utf8,
1366  {0xed, 0xae, 0x80, 0xed, 0xbf, 0xbf},
1367  {},
1368  false},
1369  {cbs_get_utf8,
1370  cbb_add_utf8,
1371  {0xed, 0xaf, 0xbf, 0xed, 0xb0, 0x80},
1372  {},
1373  false},
1374  {cbs_get_utf8,
1375  cbb_add_utf8,
1376  {0xed, 0xaf, 0xbf, 0xed, 0xbf, 0xbf},
1377  {},
1378  false},
1379  // 5.3 Noncharacter code positions
1380  {cbs_get_utf8, cbb_add_utf8, {0xef, 0xbf, 0xbe}, {}, false},
1381  {cbs_get_utf8, cbb_add_utf8, {0xef, 0xbf, 0xbf}, {}, false},
1382  {cbs_get_utf8, cbb_add_utf8, {0xef, 0xb7, 0x90}, {}, false},
1383  {cbs_get_utf8, cbb_add_utf8, {0xef, 0xb7, 0xaf}, {}, false},
1384  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x9f, 0xbf, 0xbe}, {}, false},
1385  {cbs_get_utf8, cbb_add_utf8, {0xf0, 0x9f, 0xbf, 0xbf}, {}, false},
1386 
1388  LiteralToCodePoints(U"¡Hola!"), true},
1389 
1390  // UCS-2 matches UTF-16 on the BMP.
1391  {cbs_get_ucs2_be, cbb_add_ucs2_be, LiteralToBytes(u"Hello, 世界!"),
1392  LiteralToCodePoints(U"Hello, 世界!"), true},
1393  // It does not support characters beyond the BMP.
1395  LiteralToBytes(u"Hello, 世界! ¡Hola, 🌎!"),
1396  LiteralToCodePoints(U"Hello, 世界! ¡Hola, "), false},
1397  // Unpaired surrogates and non-characters are also rejected.
1398  {cbs_get_ucs2_be, cbb_add_ucs2_be, {0xd8, 0x00}, {}, false},
1399  {cbs_get_ucs2_be, cbb_add_ucs2_be, {0xff, 0xfe}, {}, false},
1400 
1402  LiteralToBytes(U"Hello, 世界! ¡Hola, 🌎!"),
1403  LiteralToCodePoints(U"Hello, 世界! ¡Hola, 🌎!"), true},
1404  // Unpaired surrogates and non-characters are rejected.
1405  {cbs_get_utf32_be, cbb_add_utf32_be, {0x00, 0x00, 0xd8, 0x00}, {}, false},
1406  {cbs_get_utf32_be, cbb_add_utf32_be, {0x00, 0x00, 0xff, 0xfe}, {}, false},
1407 
1408  // Test that the NUL character can be encoded.
1409  {cbs_get_latin1, cbb_add_latin1, {0}, {0}, true},
1410  {cbs_get_utf8, cbb_add_utf8, {0}, {0}, true},
1411  {cbs_get_ucs2_be, cbb_add_ucs2_be, {0, 0}, {0}, true},
1412  {cbs_get_utf32_be, cbb_add_utf32_be, {0, 0, 0, 0}, {0}, true},
1413  };
1414  for (const auto &t : kTests) {
1415  SCOPED_TRACE(Bytes(t.in));
1416 
1417  // Test decoding.
1418  CBS cbs;
1419  CBS_init(&cbs, t.in.data(), t.in.size());
1420  std::vector<uint32_t> out;
1421  bool ok = true;
1422  while (CBS_len(&cbs) != 0) {
1423  uint32_t u;
1424  if (!t.decode(&cbs, &u)) {
1425  ok = false;
1426  break;
1427  }
1428  out.push_back(u);
1429  }
1430  EXPECT_EQ(t.ok, ok);
1431  EXPECT_EQ(t.out, out);
1432 
1433  // Test encoding.
1434  if (t.ok) {
1435  bssl::ScopedCBB cbb;
1436  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1437  for (uint32_t u : t.out) {
1438  ASSERT_TRUE(t.encode(cbb.get(), u));
1439  }
1440  EXPECT_EQ(Bytes(t.in), Bytes(CBB_data(cbb.get()), CBB_len(cbb.get())));
1441  }
1442  }
1443 
1444  static const uint32_t kBadCodePoints[] = {
1445  // Surrogate pairs.
1446  0xd800,
1447  0xdfff,
1448  // Non-characters.
1449  0xfffe,
1450  0xffff,
1451  0xfdd0,
1452  0x1fffe,
1453  0x1ffff,
1454  // Too big.
1455  0x110000,
1456  };
1457  bssl::ScopedCBB cbb;
1458  ASSERT_TRUE(CBB_init(cbb.get(), 0));
1459  for (uint32_t v : kBadCodePoints) {
1460  SCOPED_TRACE(v);
1461  EXPECT_FALSE(cbb_add_utf8(cbb.get(), v));
1462  EXPECT_FALSE(cbb_add_latin1(cbb.get(), v));
1463  EXPECT_FALSE(cbb_add_ucs2_be(cbb.get(), v));
1464  EXPECT_FALSE(cbb_add_utf32_be(cbb.get(), v));
1465  }
1466 
1467  // Additional values that are out of range.
1468  EXPECT_FALSE(cbb_add_latin1(cbb.get(), 0x100));
1469  EXPECT_FALSE(cbb_add_ucs2_be(cbb.get(), 0x10000));
1470 
1472  EXPECT_EQ(1u, cbb_get_utf8_len(0x7f));
1473  EXPECT_EQ(2u, cbb_get_utf8_len(0x80));
1474  EXPECT_EQ(2u, cbb_get_utf8_len(0x7ff));
1475  EXPECT_EQ(3u, cbb_get_utf8_len(0x800));
1476  EXPECT_EQ(3u, cbb_get_utf8_len(0xffff));
1477  EXPECT_EQ(4u, cbb_get_utf8_len(0x10000));
1478  EXPECT_EQ(4u, cbb_get_utf8_len(0x10ffff));
1479 }
xds_interop_client.str
str
Definition: xds_interop_client.py:487
EXPECT_FALSE
#define EXPECT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1970
ptr
char * ptr
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:45
CBS_get_asn1_uint64
#define CBS_get_asn1_uint64
Definition: boringssl_prefix_symbols.h:1066
kASN1InvalidUint64Tests
static const ASN1InvalidUint64Test kASN1InvalidUint64Tests[]
Definition: bytestring_test.cc:814
CBS_ASN1_INTEGER
#define CBS_ASN1_INTEGER
Definition: bytestring.h:207
CBS_get_u24_length_prefixed
#define CBS_get_u24_length_prefixed
Definition: boringssl_prefix_symbols.h:1077
cbs_get_latin1
#define cbs_get_latin1
Definition: boringssl_prefix_symbols.h:2929
CBB_flush
#define CBB_flush
Definition: boringssl_prefix_symbols.h:1045
CBB_data
#define CBB_data
Definition: boringssl_prefix_symbols.h:1040
kData8
static const char * kData8[]
Definition: crypto_test_data.cc:391
CBB_init
#define CBB_init
Definition: boringssl_prefix_symbols.h:1047
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
cbb_get_utf8_len
#define cbb_get_utf8_len
Definition: boringssl_prefix_symbols.h:2928
ASN1Int64Test::encoding_len
size_t encoding_len
Definition: bytestring_test.cc:876
CBS_get_u16
#define CBS_get_u16
Definition: boringssl_prefix_symbols.h:1073
Bytes
Definition: boringssl-with-bazel/src/crypto/test/test_util.h:38
cbs_st
Definition: bytestring.h:39
CBB_flush_asn1_set_of
#define CBB_flush_asn1_set_of
Definition: boringssl_prefix_symbols.h:1046
CBB_cleanup
#define CBB_cleanup
Definition: boringssl_prefix_symbols.h:1039
CBS_skip
#define CBS_skip
Definition: boringssl_prefix_symbols.h:1092
kData3
static const char * kData3[]
Definition: crypto_test_data.cc:343
CBB_zero
#define CBB_zero
Definition: boringssl_prefix_symbols.h:1051
BERTest
Definition: bytestring_test.cc:692
CBS_data
#define CBS_data
Definition: boringssl_prefix_symbols.h:1057
CBS_get_u32le
#define CBS_get_u32le
Definition: boringssl_prefix_symbols.h:1079
LiteralToCodePoints
static std::vector< uint32_t > LiteralToCodePoints(const char32_t *str)
Definition: bytestring_test.cc:1274
CBS_get_u64le
#define CBS_get_u64le
Definition: boringssl_prefix_symbols.h:1081
uint16_t
unsigned short uint16_t
Definition: stdint-msvc2008.h:79
CBS_get_any_asn1
#define CBS_get_any_asn1
Definition: boringssl_prefix_symbols.h:1058
test
Definition: spinlock_test.cc:36
INT64_MAX
#define INT64_MAX
Definition: stdint-msvc2008.h:139
CBS_get_last_u8
#define CBS_get_last_u8
Definition: boringssl_prefix_symbols.h:1068
string.h
kData5
static const char * kData5[]
Definition: crypto_test_data.cc:353
CBS_get_u32
#define CBS_get_u32
Definition: boringssl_prefix_symbols.h:1078
CBS_ASN1_OCTETSTRING
#define CBS_ASN1_OCTETSTRING
Definition: bytestring.h:209
CBB_add_u16_length_prefixed
#define CBB_add_u16_length_prefixed
Definition: boringssl_prefix_symbols.h:1028
CBB_add_u8
#define CBB_add_u8
Definition: boringssl_prefix_symbols.h:1036
buf
voidpf void * buf
Definition: bloaty/third_party/zlib/contrib/minizip/ioapi.h:136
CBB_add_asn1_int64
#define CBB_add_asn1_int64
Definition: boringssl_prefix_symbols.h:1021
CBS_ASN1_CONTEXT_SPECIFIC
#define CBS_ASN1_CONTEXT_SPECIFIC
Definition: bytestring.h:194
kData1
static const char * kData1[]
Definition: crypto_test_data.cc:223
CBS_ASN1_SET
#define CBS_ASN1_SET
Definition: bytestring.h:215
BERTest::ok
bool ok
Definition: bytestring_test.cc:694
CBS_get_u8_length_prefixed
#define CBS_get_u8_length_prefixed
Definition: boringssl_prefix_symbols.h:1083
INT64_C
#define INT64_C(val)
Definition: stdint-msvc2008.h:233
ASN1InvalidUint64Test
Definition: bytestring_test.cc:808
ASN1InvalidInt64Test::encoding
const char * encoding
Definition: bytestring_test.cc:897
CBS_len
#define CBS_len
Definition: boringssl_prefix_symbols.h:1089
u
OPENSSL_EXPORT pem_password_cb void * u
Definition: pem.h:351
kTests
static const HKDFTestVector kTests[]
Definition: hkdf_test.cc:41
CBB_add_u24
#define CBB_add_u24
Definition: boringssl_prefix_symbols.h:1030
setup.name
name
Definition: setup.py:542
CBS_ASN1_BOOLEAN
#define CBS_ASN1_BOOLEAN
Definition: bytestring.h:206
CBS_get_asn1
#define CBS_get_asn1
Definition: boringssl_prefix_symbols.h:1061
BERTest::tag
unsigned tag
Definition: bytestring_test.cc:696
EXPECT_LE
#define EXPECT_LE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2030
cbs
const CBS * cbs
Definition: third_party/boringssl-with-bazel/src/crypto/trust_token/internal.h:107
kASN1Int64Tests
static const ASN1Int64Test kASN1Int64Tests[]
Definition: bytestring_test.cc:879
ASN1Uint64Test::value
uint64_t value
Definition: bytestring_test.cc:791
ASN1Int64Test
Definition: bytestring_test.cc:873
CBS_is_valid_asn1_bitstring
#define CBS_is_valid_asn1_bitstring
Definition: boringssl_prefix_symbols.h:1087
CBS_init
#define CBS_init
Definition: boringssl_prefix_symbols.h:1085
CBS_is_valid_asn1_integer
#define CBS_is_valid_asn1_integer
Definition: boringssl_prefix_symbols.h:1088
CBB_add_u64le
#define CBB_add_u64le
Definition: boringssl_prefix_symbols.h:1035
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
kASN1InvalidInt64Tests
static const ASN1InvalidInt64Test kASN1InvalidInt64Tests[]
Definition: bytestring_test.cc:902
grpc._common.encode
def encode(s)
Definition: grpc/_common.py:68
T
#define T(upbtypeconst, upbtype, ctype, default_value)
cbb_add_ucs2_be
#define cbb_add_ucs2_be
Definition: boringssl_prefix_symbols.h:2925
EXPECT_EQ
#define EXPECT_EQ(a, b)
Definition: iomgr/time_averaged_stats_test.cc:27
BERTest::ber_found
bool ber_found
Definition: bytestring_test.cc:695
CBB_reserve
#define CBB_reserve
Definition: boringssl_prefix_symbols.h:1050
uint32_t
unsigned int uint32_t
Definition: stdint-msvc2008.h:80
CBS_get_until_first
#define CBS_get_until_first
Definition: boringssl_prefix_symbols.h:1084
CBB_did_write
#define CBB_did_write
Definition: boringssl_prefix_symbols.h:1041
bytestring.h
CBS_peek_asn1_tag
#define CBS_peek_asn1_tag
Definition: boringssl_prefix_symbols.h:1091
internal.h
in
const char * in
Definition: third_party/abseil-cpp/absl/strings/internal/str_format/parser_test.cc:391
ASN1InvalidUint64Test::overflow
bool overflow
Definition: bytestring_test.cc:811
CBS_ASN1_APPLICATION
#define CBS_ASN1_APPLICATION
Definition: bytestring.h:193
CBB_add_u16
#define CBB_add_u16
Definition: boringssl_prefix_symbols.h:1027
SCOPED_TRACE
#define SCOPED_TRACE(message)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2264
gen_server_registered_method_bad_client_test_body.text
def text
Definition: gen_server_registered_method_bad_client_test_body.py:50
xds_interop_client.int
int
Definition: xds_interop_client.py:113
CBS_get_any_asn1_element
#define CBS_get_any_asn1_element
Definition: boringssl_prefix_symbols.h:1059
int64_t
signed __int64 int64_t
Definition: stdint-msvc2008.h:89
CBB_add_u64
#define CBB_add_u64
Definition: boringssl_prefix_symbols.h:1034
tag
static void * tag(intptr_t t)
Definition: bad_client.cc:318
CBB_add_zeros
#define CBB_add_zeros
Definition: boringssl_prefix_symbols.h:1038
CBB_add_u8_length_prefixed
#define CBB_add_u8_length_prefixed
Definition: boringssl_prefix_symbols.h:1037
ASN1InvalidInt64Test::encoding_len
size_t encoding_len
Definition: bytestring_test.cc:898
cbb_add_utf32_be
#define cbb_add_utf32_be
Definition: boringssl_prefix_symbols.h:2926
EXPECT_NE
#define EXPECT_NE(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2028
ASN1InvalidInt64Test
Definition: bytestring_test.cc:896
CBB_finish
#define CBB_finish
Definition: boringssl_prefix_symbols.h:1043
kData7
static const char * kData7[]
Definition: crypto_test_data.cc:365
setup.v
v
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
CBS_get_u64
#define CBS_get_u64
Definition: boringssl_prefix_symbols.h:1080
uint64_t
unsigned __int64 uint64_t
Definition: stdint-msvc2008.h:90
CBB_add_asn1
#define CBB_add_asn1
Definition: boringssl_prefix_symbols.h:1019
CBS_get_u8
#define CBS_get_u8
Definition: boringssl_prefix_symbols.h:1082
CBS_get_optional_asn1_bool
#define CBS_get_optional_asn1_bool
Definition: boringssl_prefix_symbols.h:1070
crypto.h
CBS_get_any_ber_asn1_element
#define CBS_get_any_ber_asn1_element
Definition: boringssl_prefix_symbols.h:1060
CBB_add_u32le
#define CBB_add_u32le
Definition: boringssl_prefix_symbols.h:1033
googletest-filter-unittest.child
child
Definition: bloaty/third_party/googletest/googletest/test/googletest-filter-unittest.py:62
ImplicitStringTest::out_len
size_t out_len
Definition: bytestring_test.cc:753
ASN1Int64Test::encoding
const char * encoding
Definition: bytestring_test.cc:875
kASN1Uint64Tests
static const ASN1Uint64Test kASN1Uint64Tests[]
Definition: bytestring_test.cc:796
ASN1Uint64Test::encoding_len
size_t encoding_len
Definition: bytestring_test.cc:793
CBB_add_u32
#define CBB_add_u32
Definition: boringssl_prefix_symbols.h:1032
data
char data[kBufferLength]
Definition: abseil-cpp/absl/strings/internal/str_format/float_conversion.cc:1006
ImplicitStringTest::ok
bool ok
Definition: bytestring_test.cc:751
ASN1Int64Test::value
int64_t value
Definition: bytestring_test.cc:874
BERTest::in_hex
const char * in_hex
Definition: bytestring_test.cc:693
CBB_add_asn1_oid_from_text
#define CBB_add_asn1_oid_from_text
Definition: boringssl_prefix_symbols.h:1023
UINT64_C
#define UINT64_C(val)
Definition: stdint-msvc2008.h:238
ASN1InvalidUint64Test::encoding_len
size_t encoding_len
Definition: bytestring_test.cc:810
CBS_ASN1_CONSTRUCTED
#define CBS_ASN1_CONSTRUCTED
Definition: bytestring.h:188
kBERTests
static const BERTest kBERTests[]
Definition: bytestring_test.cc:699
ImplicitStringTest::in_len
size_t in_len
Definition: bytestring_test.cc:750
ExpectBerConvert
static void ExpectBerConvert(const char *name, bssl::Span< const uint8_t > der_expected, bssl::Span< const uint8_t > ber)
Definition: bytestring_test.cc:598
CBS_get_optional_asn1_uint64
#define CBS_get_optional_asn1_uint64
Definition: boringssl_prefix_symbols.h:1072
value
const char * value
Definition: hpack_parser_table.cc:165
ASN1InvalidUint64Test::encoding
const char * encoding
Definition: bytestring_test.cc:809
EXPECT_STREQ
#define EXPECT_STREQ(s1, s2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2095
absl::Skip
static PerThreadSynch * Skip(PerThreadSynch *x)
Definition: abseil-cpp/absl/synchronization/mutex.cc:837
INT64_MIN
#define INT64_MIN
Definition: stdint-msvc2008.h:138
CBB_add_bytes
#define CBB_add_bytes
Definition: boringssl_prefix_symbols.h:1025
absl::out_size
char int out_size
Definition: abseil-cpp/absl/synchronization/mutex.h:1048
contents
string_view contents
Definition: elf.cc:597
CBS_get_optional_asn1
#define CBS_get_optional_asn1
Definition: boringssl_prefix_symbols.h:1069
CBS_is_unsigned_asn1_integer
#define CBS_is_unsigned_asn1_integer
Definition: boringssl_prefix_symbols.h:1086
CBS_asn1_oid_to_text
#define CBS_asn1_oid_to_text
Definition: boringssl_prefix_symbols.h:1054
kImplicitStringTests
static const ImplicitStringTest kImplicitStringTests[]
Definition: bytestring_test.cc:756
CBS_get_optional_asn1_octet_string
#define CBS_get_optional_asn1_octet_string
Definition: boringssl_prefix_symbols.h:1071
DecodeHex
bool DecodeHex(std::vector< uint8_t > *out, const std::string &in)
Definition: boringssl-with-bazel/src/crypto/test/test_util.cc:58
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ASN1Uint64Test
Definition: bytestring_test.cc:790
TEST
TEST(CBSTest, Skip)
Definition: bytestring_test.cc:32
CBS_get_u16_length_prefixed
#define CBS_get_u16_length_prefixed
Definition: boringssl_prefix_symbols.h:1074
CBS_ASN1_PRIVATE
#define CBS_ASN1_PRIVATE
Definition: bytestring.h:195
grpc._common.decode
def decode(b)
Definition: grpc/_common.py:75
ASN1InvalidInt64Test::overflow
bool overflow
Definition: bytestring_test.cc:899
prefix
static const char prefix[]
Definition: head_of_line_blocking.cc:28
ASN1Uint64Test::encoding
const char * encoding
Definition: bytestring_test.cc:792
CBS_get_asn1_int64
#define CBS_get_asn1_int64
Definition: boringssl_prefix_symbols.h:1065
ASSERT_TRUE
#define ASSERT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1973
ASSERT_FALSE
#define ASSERT_FALSE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1976
ok
bool ok
Definition: async_end2end_test.cc:197
ImplicitStringTest
Definition: bytestring_test.cc:748
ImplicitStringTest::in
const char * in
Definition: bytestring_test.cc:749
cbb_add_latin1
#define cbb_add_latin1
Definition: boringssl_prefix_symbols.h:2924
CBS_get_u24
#define CBS_get_u24
Definition: boringssl_prefix_symbols.h:1076
cbb_add_utf8
#define cbb_add_utf8
Definition: boringssl_prefix_symbols.h:2927
CBS_get_u16le
#define CBS_get_u16le
Definition: boringssl_prefix_symbols.h:1075
EXPECT_TRUE
#define EXPECT_TRUE(condition)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:1967
WycheproofRawResult::kInvalid
@ kInvalid
ImplicitStringTest::out
const char * out
Definition: bytestring_test.cc:752
kData9
static const char * kData9[]
Definition: crypto_test_data.cc:404
CBB_len
#define CBB_len
Definition: boringssl_prefix_symbols.h:1049
span.h
len
int len
Definition: abseil-cpp/absl/base/internal/low_level_alloc_test.cc:46
kData4
static const char * kData4[]
Definition: crypto_test_data.cc:348
CBS_ASN1_SEQUENCE
#define CBS_ASN1_SEQUENCE
Definition: bytestring.h:214
kData2
static const char * kData2[]
Definition: crypto_test_data.cc:283
CBB_add_u16le
#define CBB_add_u16le
Definition: boringssl_prefix_symbols.h:1029
cbs_get_utf8
#define cbs_get_utf8
Definition: boringssl_prefix_symbols.h:2932
absl::string_view::data
constexpr const_pointer data() const noexcept
Definition: abseil-cpp/absl/strings/string_view.h:336
CBS_asn1_ber_to_der
#define CBS_asn1_ber_to_der
Definition: boringssl_prefix_symbols.h:1052
kData6
static const char * kData6[]
Definition: crypto_test_data.cc:360
absl::status_internal::storage
static ABSL_INTERNAL_ATOMIC_HOOK_ATTRIBUTES absl::base_internal::AtomicHook< StatusPayloadPrinter > storage
Definition: abseil-cpp/absl/status/status_payload_printer.cc:26
cbs_get_utf32_be
#define cbs_get_utf32_be
Definition: boringssl_prefix_symbols.h:2931
CBB_init_fixed
#define CBB_init_fixed
Definition: boringssl_prefix_symbols.h:1048
CBS_get_asn1_implicit_string
#define CBS_get_asn1_implicit_string
Definition: boringssl_prefix_symbols.h:1064
CBB_discard_child
#define CBB_discard_child
Definition: boringssl_prefix_symbols.h:1042
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
ASSERT_EQ
#define ASSERT_EQ(val1, val2)
Definition: bloaty/third_party/googletest/googletest/include/gtest/gtest.h:2056
CBS_asn1_bitstring_has_bit
#define CBS_asn1_bitstring_has_bit
Definition: boringssl_prefix_symbols.h:1053
cbs_get_ucs2_be
#define cbs_get_ucs2_be
Definition: boringssl_prefix_symbols.h:2930
LiteralToBytes
static std::vector< uint8_t > LiteralToBytes(const T *str)
Definition: bytestring_test.cc:1264
CBB_add_asn1_uint64
#define CBB_add_asn1_uint64
Definition: boringssl_prefix_symbols.h:1024
cbb_st
Definition: bytestring.h:375
CBB_add_u24_length_prefixed
#define CBB_add_u24_length_prefixed
Definition: boringssl_prefix_symbols.h:1031


grpc
Author(s):
autogenerated on Fri May 16 2025 02:57:51