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 <stdio.h>
     16 
     17 #include <gtest/gtest.h>
     18 #include <limits.h>
     19 
     20 #include <openssl/asn1.h>
     21 #include <openssl/err.h>
     22 
     23 #include "../test/test_util.h"
     24 
     25 
     26 // kTag128 is an ASN.1 structure with a universal tag with number 128.
     27 static const uint8_t kTag128[] = {
     28     0x1f, 0x81, 0x00, 0x01, 0x00,
     29 };
     30 
     31 // kTag258 is an ASN.1 structure with a universal tag with number 258.
     32 static const uint8_t kTag258[] = {
     33     0x1f, 0x82, 0x02, 0x01, 0x00,
     34 };
     35 
     36 static_assert(V_ASN1_NEG_INTEGER == 258,
     37               "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
     38 
     39 // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
     40 // which will not fit in an int.
     41 static const uint8_t kTagOverflow[] = {
     42     0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
     43 };
     44 
     45 TEST(ASN1Test, LargeTags) {
     46   const uint8_t *p = kTag258;
     47   bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
     48   EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type;
     49   ERR_clear_error();
     50 
     51   p = kTagOverflow;
     52   obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
     53   EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type;
     54   ERR_clear_error();
     55 
     56   p = kTag128;
     57   obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
     58   ASSERT_TRUE(obj);
     59   EXPECT_EQ(128, obj->type);
     60   const uint8_t kZero = 0;
     61   EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data,
     62                                     obj->value.asn1_string->length));
     63 }
     64 
     65 TEST(ASN1Test, IntegerSetting) {
     66   bssl::UniquePtr<ASN1_INTEGER> by_bn(M_ASN1_INTEGER_new());
     67   bssl::UniquePtr<ASN1_INTEGER> by_long(M_ASN1_INTEGER_new());
     68   bssl::UniquePtr<ASN1_INTEGER> by_uint64(M_ASN1_INTEGER_new());
     69   bssl::UniquePtr<BIGNUM> bn(BN_new());
     70 
     71   const std::vector<int64_t> kValues = {
     72       LONG_MIN, -2, -1, 0, 1, 2, 0xff, 0x100, 0xffff, 0x10000, LONG_MAX,
     73   };
     74   for (const auto &i : kValues) {
     75     SCOPED_TRACE(i);
     76 
     77     ASSERT_EQ(1, ASN1_INTEGER_set(by_long.get(), i));
     78     const uint64_t abs = i < 0 ? (0 - (uint64_t) i) : i;
     79     ASSERT_TRUE(BN_set_u64(bn.get(), abs));
     80     BN_set_negative(bn.get(), i < 0);
     81     ASSERT_TRUE(BN_to_ASN1_INTEGER(bn.get(), by_bn.get()));
     82 
     83     EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_long.get()));
     84 
     85     if (i >= 0) {
     86       ASSERT_EQ(1, ASN1_INTEGER_set_uint64(by_uint64.get(), i));
     87       EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_uint64.get()));
     88     }
     89   }
     90 }
     91