Home | History | Annotate | Download | only in asn1
      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 <limits.h>
     16 #include <stdio.h>
     17 
     18 #include <vector>
     19 
     20 #include <gtest/gtest.h>
     21 
     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>
     27 #include <openssl/obj.h>
     28 #include <openssl/span.h>
     29 
     30 #include "../test/test_util.h"
     31 
     32 
     33 // kTag128 is an ASN.1 structure with a universal tag with number 128.
     34 static const uint8_t kTag128[] = {
     35     0x1f, 0x81, 0x00, 0x01, 0x00,
     36 };
     37 
     38 // kTag258 is an ASN.1 structure with a universal tag with number 258.
     39 static const uint8_t kTag258[] = {
     40     0x1f, 0x82, 0x02, 0x01, 0x00,
     41 };
     42 
     43 static_assert(V_ASN1_NEG_INTEGER == 258,
     44               "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
     45 
     46 // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
     47 // which will not fit in an int.
     48 static const uint8_t kTagOverflow[] = {
     49     0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
     50 };
     51 
     52 TEST(ASN1Test, LargeTags) {
     53   const uint8_t *p = kTag258;
     54   bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
     55   EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type;
     56   ERR_clear_error();
     57 
     58   p = kTagOverflow;
     59   obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
     60   EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type;
     61   ERR_clear_error();
     62 
     63   p = kTag128;
     64   obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
     65   ASSERT_TRUE(obj);
     66   EXPECT_EQ(128, obj->type);
     67   const uint8_t kZero = 0;
     68   EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data,
     69                                     obj->value.asn1_string->length));
     70 }
     71 
     72 TEST(ASN1Test, IntegerSetting) {
     73   bssl::UniquePtr<ASN1_INTEGER> by_bn(M_ASN1_INTEGER_new());
     74   bssl::UniquePtr<ASN1_INTEGER> by_long(M_ASN1_INTEGER_new());
     75   bssl::UniquePtr<ASN1_INTEGER> by_uint64(M_ASN1_INTEGER_new());
     76   bssl::UniquePtr<BIGNUM> bn(BN_new());
     77 
     78   const std::vector<int64_t> kValues = {
     79       LONG_MIN, -2, -1, 0, 1, 2, 0xff, 0x100, 0xffff, 0x10000, LONG_MAX,
     80   };
     81   for (const auto &i : kValues) {
     82     SCOPED_TRACE(i);
     83 
     84     ASSERT_EQ(1, ASN1_INTEGER_set(by_long.get(), i));
     85     const uint64_t abs = i < 0 ? (0 - (uint64_t) i) : i;
     86     ASSERT_TRUE(BN_set_u64(bn.get(), abs));
     87     BN_set_negative(bn.get(), i < 0);
     88     ASSERT_TRUE(BN_to_ASN1_INTEGER(bn.get(), by_bn.get()));
     89 
     90     EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_long.get()));
     91 
     92     if (i >= 0) {
     93       ASSERT_EQ(1, ASN1_INTEGER_set_uint64(by_uint64.get(), i));
     94       EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_uint64.get()));
     95     }
     96   }
     97 }
     98 
     99 typedef struct asn1_linked_list_st {
    100   struct asn1_linked_list_st *next;
    101 } ASN1_LINKED_LIST;
    102 
    103 DECLARE_ASN1_ITEM(ASN1_LINKED_LIST)
    104 DECLARE_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
    105 
    106 ASN1_SEQUENCE(ASN1_LINKED_LIST) = {
    107   ASN1_OPT(ASN1_LINKED_LIST, next, ASN1_LINKED_LIST),
    108 } ASN1_SEQUENCE_END(ASN1_LINKED_LIST)
    109 
    110 IMPLEMENT_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
    111 
    112 static bool MakeLinkedList(bssl::UniquePtr<uint8_t> *out, size_t *out_len,
    113                            size_t count) {
    114   bssl::ScopedCBB cbb;
    115   std::vector<CBB> cbbs(count);
    116   if (!CBB_init(cbb.get(), 2 * count) ||
    117       !CBB_add_asn1(cbb.get(), &cbbs[0], CBS_ASN1_SEQUENCE)) {
    118     return false;
    119   }
    120   for (size_t i = 1; i < count; i++) {
    121     if (!CBB_add_asn1(&cbbs[i - 1], &cbbs[i], CBS_ASN1_SEQUENCE)) {
    122       return false;
    123     }
    124   }
    125   uint8_t *ptr;
    126   if (!CBB_finish(cbb.get(), &ptr, out_len)) {
    127     return false;
    128   }
    129   out->reset(ptr);
    130   return true;
    131 }
    132 
    133 TEST(ASN1Test, Recursive) {
    134   bssl::UniquePtr<uint8_t> data;
    135   size_t len;
    136 
    137   // Sanity-check that MakeLinkedList can be parsed.
    138   ASSERT_TRUE(MakeLinkedList(&data, &len, 5));
    139   const uint8_t *ptr = data.get();
    140   ASN1_LINKED_LIST *list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
    141   EXPECT_TRUE(list);
    142   ASN1_LINKED_LIST_free(list);
    143 
    144   // Excessively deep structures are rejected.
    145   ASSERT_TRUE(MakeLinkedList(&data, &len, 100));
    146   ptr = data.get();
    147   list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
    148   EXPECT_FALSE(list);
    149   // Note checking the error queue here does not work. The error "stack trace"
    150   // is too deep, so the |ASN1_R_NESTED_TOO_DEEP| entry drops off the queue.
    151   ASN1_LINKED_LIST_free(list);
    152 }
    153 
    154 template <typename T>
    155 void TestSerialize(T obj, int (*i2d_func)(T a, uint8_t **pp),
    156                    bssl::Span<const uint8_t> expected) {
    157   int len = static_cast<int>(expected.size());
    158   ASSERT_EQ(i2d_func(obj, nullptr), len);
    159 
    160   std::vector<uint8_t> buf(expected.size());
    161   uint8_t *ptr = buf.data();
    162   ASSERT_EQ(i2d_func(obj, &ptr), len);
    163   EXPECT_EQ(ptr, buf.data() + buf.size());
    164   EXPECT_EQ(Bytes(expected), Bytes(buf));
    165 
    166   // Test the allocating version.
    167   ptr = nullptr;
    168   ASSERT_EQ(i2d_func(obj, &ptr), len);
    169   EXPECT_EQ(Bytes(expected), Bytes(ptr, expected.size()));
    170   OPENSSL_free(ptr);
    171 }
    172 
    173 TEST(ASN1Test, SerializeObject) {
    174   static const uint8_t kDER[] = {0x06, 0x09, 0x2a, 0x86, 0x48, 0x86,
    175                                  0xf7, 0x0d, 0x01, 0x01, 0x01};
    176   const ASN1_OBJECT *obj = OBJ_nid2obj(NID_rsaEncryption);
    177   TestSerialize(const_cast<ASN1_OBJECT *>(obj), i2d_ASN1_OBJECT, kDER);
    178 }
    179 
    180 TEST(ASN1Test, SerializeBoolean) {
    181   static const uint8_t kTrue[] = {0x01, 0x01, 0xff};
    182   TestSerialize(0xff, i2d_ASN1_BOOLEAN, kTrue);
    183 
    184   static const uint8_t kFalse[] = {0x01, 0x01, 0x00};
    185   TestSerialize(0x00, i2d_ASN1_BOOLEAN, kFalse);
    186 }
    187