Debian Codegraph @

external/boringssl/crypto/asn1/asn1_test.cc

raw
1 /* Copyright (c) 2016, 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
17 #include <openssl/asn1.h>
18 #include <openssl/crypto.h>
19 #include <openssl/err.h>
20
21 #include "../test/scoped_types.h"
22
23
24 // kTag128 is an ASN.1 structure with a universal tag with number 128.
25 static const uint8_t kTag128[] = {
26 0x1f, 0x81, 0x00, 0x01, 0x00,
27 };
28
29 // kTag258 is an ASN.1 structure with a universal tag with number 258.
30 static const uint8_t kTag258[] = {
31 0x1f, 0x82, 0x02, 0x01, 0x00,
32 };
33
34 static_assert(V_ASN1_NEG_INTEGER == 258,
35 "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
36
37 // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
38 // which will not fit in an int.
39 static const uint8_t kTagOverflow[] = {
40 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
41 };
42
43 static bool TestLargeTags() {
44 const uint8_t *p = kTag258;
45 ScopedASN1_TYPE obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
46 if (obj) {
47 fprintf(stderr, "Parsed value with illegal tag (type = %d).\n", obj->type);
48 return false;
49 }
50 ERR_clear_error();
51
52 p = kTagOverflow;
53 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
54 if (obj) {
55 fprintf(stderr, "Parsed value with tag overflow (type = %d).\n", obj->type);
56 return false;
57 }
58 ERR_clear_error();
59
60 p = kTag128;
61 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
62 if (!obj || obj->type != 128 || obj->value.asn1_string->length != 1 ||
63 obj->value.asn1_string->data[0] != 0) {
64 fprintf(stderr, "Failed to parse value with tag 128.\n");
65 ERR_print_errors_fp(stderr);
66 return false;
67 }
68
69 return true;
70 }
71
72 int main() {
73 CRYPTO_library_init();
74
75 if (!TestLargeTags()) {
76 return 1;
77 }
78
79 printf("PASS\n");
80 return 0;
81 }