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 <gtest/gtest.h>
18
19 #include <openssl/asn1.h>
20 #include <openssl/err.h>
21
22 #include "../test/test_util.h"
23
24
25 // kTag128 is an ASN.1 structure with a universal tag with number 128.
26 static const uint8_t kTag128[] = {
27 0x1f, 0x81, 0x00, 0x01, 0x00,
28 };
29
30 // kTag258 is an ASN.1 structure with a universal tag with number 258.
31 static const uint8_t kTag258[] = {
32 0x1f, 0x82, 0x02, 0x01, 0x00,
33 };
34
35 static_assert(V_ASN1_NEG_INTEGER == 258,
36 "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
37
38 // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
39 // which will not fit in an int.
40 static const uint8_t kTagOverflow[] = {
41 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
42 };
43
TEST(ASN1Test,LargeTags)44 TEST(ASN1Test, LargeTags) {
45 const uint8_t *p = kTag258;
46 bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
47 EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type;
48 ERR_clear_error();
49
50 p = kTagOverflow;
51 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
52 EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type;
53 ERR_clear_error();
54
55 p = kTag128;
56 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
57 ASSERT_TRUE(obj);
58 EXPECT_EQ(128, obj->type);
59 const uint8_t kZero = 0;
60 EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data,
61 obj->value.asn1_string->length));
62 }
63