1 /* Copyright (c) 2016, Google Inc.
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.
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. */
20 #include <gtest/gtest.h>
22 #include <openssl/asn1.h>
23 #include <openssl/asn1t.h>
24 #include <openssl/bytestring.h>
25 #include <openssl/err.h>
26 #include <openssl/mem.h>
28 #include "../test/test_util.h"
31 // kTag128 is an ASN.1 structure with a universal tag with number 128.
32 static const uint8_t kTag128[] = {
33 0x1f, 0x81, 0x00, 0x01, 0x00,
36 // kTag258 is an ASN.1 structure with a universal tag with number 258.
37 static const uint8_t kTag258[] = {
38 0x1f, 0x82, 0x02, 0x01, 0x00,
41 static_assert(V_ASN1_NEG_INTEGER == 258,
42 "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
44 // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
45 // which will not fit in an int.
46 static const uint8_t kTagOverflow[] = {
47 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
50 TEST(ASN1Test, LargeTags) {
51 const uint8_t *p = kTag258;
52 bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
53 EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type;
57 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
58 EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type;
62 obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
64 EXPECT_EQ(128, obj->type);
65 const uint8_t kZero = 0;
66 EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data,
67 obj->value.asn1_string->length));
70 TEST(ASN1Test, IntegerSetting) {
71 bssl::UniquePtr<ASN1_INTEGER> by_bn(M_ASN1_INTEGER_new());
72 bssl::UniquePtr<ASN1_INTEGER> by_long(M_ASN1_INTEGER_new());
73 bssl::UniquePtr<ASN1_INTEGER> by_uint64(M_ASN1_INTEGER_new());
74 bssl::UniquePtr<BIGNUM> bn(BN_new());
76 const std::vector<int64_t> kValues = {
77 LONG_MIN, -2, -1, 0, 1, 2, 0xff, 0x100, 0xffff, 0x10000, LONG_MAX,
79 for (const auto &i : kValues) {
82 ASSERT_EQ(1, ASN1_INTEGER_set(by_long.get(), i));
83 const uint64_t abs = i < 0 ? (0 - (uint64_t) i) : i;
84 ASSERT_TRUE(BN_set_u64(bn.get(), abs));
85 BN_set_negative(bn.get(), i < 0);
86 ASSERT_TRUE(BN_to_ASN1_INTEGER(bn.get(), by_bn.get()));
88 EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_long.get()));
91 ASSERT_EQ(1, ASN1_INTEGER_set_uint64(by_uint64.get(), i));
92 EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_uint64.get()));
97 typedef struct asn1_linked_list_st {
98 struct asn1_linked_list_st *next;
101 DECLARE_ASN1_ITEM(ASN1_LINKED_LIST)
102 DECLARE_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
104 ASN1_SEQUENCE(ASN1_LINKED_LIST) = {
105 ASN1_OPT(ASN1_LINKED_LIST, next, ASN1_LINKED_LIST),
106 } ASN1_SEQUENCE_END(ASN1_LINKED_LIST)
108 IMPLEMENT_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
110 static bool MakeLinkedList(bssl::UniquePtr<uint8_t> *out, size_t *out_len,
113 std::vector<CBB> cbbs(count);
114 if (!CBB_init(cbb.get(), 2 * count) ||
115 !CBB_add_asn1(cbb.get(), &cbbs[0], CBS_ASN1_SEQUENCE)) {
118 for (size_t i = 1; i < count; i++) {
119 if (!CBB_add_asn1(&cbbs[i - 1], &cbbs[i], CBS_ASN1_SEQUENCE)) {
124 if (!CBB_finish(cbb.get(), &ptr, out_len)) {
131 TEST(ASN1Test, Recursive) {
132 bssl::UniquePtr<uint8_t> data;
135 // Sanity-check that MakeLinkedList can be parsed.
136 ASSERT_TRUE(MakeLinkedList(&data, &len, 5));
137 const uint8_t *ptr = data.get();
138 ASN1_LINKED_LIST *list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
140 ASN1_LINKED_LIST_free(list);
142 // Excessively deep structures are rejected.
143 ASSERT_TRUE(MakeLinkedList(&data, &len, 100));
145 list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
147 // Note checking the error queue here does not work. The error "stack trace"
148 // is too deep, so the |ASN1_R_NESTED_TOO_DEEP| entry drops off the queue.
149 ASN1_LINKED_LIST_free(list);