You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

151 lines
4.7 KiB

  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. #include <limits.h>
  15. #include <stdio.h>
  16. #include <vector>
  17. #include <gtest/gtest.h>
  18. #include <openssl/asn1.h>
  19. #include <openssl/asn1t.h>
  20. #include <openssl/bytestring.h>
  21. #include <openssl/err.h>
  22. #include <openssl/mem.h>
  23. #include "../test/test_util.h"
  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. // kTag258 is an ASN.1 structure with a universal tag with number 258.
  29. static const uint8_t kTag258[] = {
  30. 0x1f, 0x82, 0x02, 0x01, 0x00,
  31. };
  32. static_assert(V_ASN1_NEG_INTEGER == 258,
  33. "V_ASN1_NEG_INTEGER changed. Update kTag258 to collide with it.");
  34. // kTagOverflow is an ASN.1 structure with a universal tag with number 2^35-1,
  35. // which will not fit in an int.
  36. static const uint8_t kTagOverflow[] = {
  37. 0x1f, 0xff, 0xff, 0xff, 0xff, 0x7f, 0x01, 0x00,
  38. };
  39. TEST(ASN1Test, LargeTags) {
  40. const uint8_t *p = kTag258;
  41. bssl::UniquePtr<ASN1_TYPE> obj(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag258)));
  42. EXPECT_FALSE(obj) << "Parsed value with illegal tag" << obj->type;
  43. ERR_clear_error();
  44. p = kTagOverflow;
  45. obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTagOverflow)));
  46. EXPECT_FALSE(obj) << "Parsed value with tag overflow" << obj->type;
  47. ERR_clear_error();
  48. p = kTag128;
  49. obj.reset(d2i_ASN1_TYPE(NULL, &p, sizeof(kTag128)));
  50. ASSERT_TRUE(obj);
  51. EXPECT_EQ(128, obj->type);
  52. const uint8_t kZero = 0;
  53. EXPECT_EQ(Bytes(&kZero, 1), Bytes(obj->value.asn1_string->data,
  54. obj->value.asn1_string->length));
  55. }
  56. TEST(ASN1Test, IntegerSetting) {
  57. bssl::UniquePtr<ASN1_INTEGER> by_bn(M_ASN1_INTEGER_new());
  58. bssl::UniquePtr<ASN1_INTEGER> by_long(M_ASN1_INTEGER_new());
  59. bssl::UniquePtr<ASN1_INTEGER> by_uint64(M_ASN1_INTEGER_new());
  60. bssl::UniquePtr<BIGNUM> bn(BN_new());
  61. const std::vector<int64_t> kValues = {
  62. LONG_MIN, -2, -1, 0, 1, 2, 0xff, 0x100, 0xffff, 0x10000, LONG_MAX,
  63. };
  64. for (const auto &i : kValues) {
  65. SCOPED_TRACE(i);
  66. ASSERT_EQ(1, ASN1_INTEGER_set(by_long.get(), i));
  67. const uint64_t abs = i < 0 ? (0 - (uint64_t) i) : i;
  68. ASSERT_TRUE(BN_set_u64(bn.get(), abs));
  69. BN_set_negative(bn.get(), i < 0);
  70. ASSERT_TRUE(BN_to_ASN1_INTEGER(bn.get(), by_bn.get()));
  71. EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_long.get()));
  72. if (i >= 0) {
  73. ASSERT_EQ(1, ASN1_INTEGER_set_uint64(by_uint64.get(), i));
  74. EXPECT_EQ(0, ASN1_INTEGER_cmp(by_bn.get(), by_uint64.get()));
  75. }
  76. }
  77. }
  78. typedef struct asn1_linked_list_st {
  79. struct asn1_linked_list_st *next;
  80. } ASN1_LINKED_LIST;
  81. DECLARE_ASN1_ITEM(ASN1_LINKED_LIST)
  82. DECLARE_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
  83. ASN1_SEQUENCE(ASN1_LINKED_LIST) = {
  84. ASN1_OPT(ASN1_LINKED_LIST, next, ASN1_LINKED_LIST),
  85. } ASN1_SEQUENCE_END(ASN1_LINKED_LIST)
  86. IMPLEMENT_ASN1_FUNCTIONS(ASN1_LINKED_LIST)
  87. static bool MakeLinkedList(bssl::UniquePtr<uint8_t> *out, size_t *out_len,
  88. size_t count) {
  89. bssl::ScopedCBB cbb;
  90. std::vector<CBB> cbbs(count);
  91. if (!CBB_init(cbb.get(), 2 * count) ||
  92. !CBB_add_asn1(cbb.get(), &cbbs[0], CBS_ASN1_SEQUENCE)) {
  93. return false;
  94. }
  95. for (size_t i = 1; i < count; i++) {
  96. if (!CBB_add_asn1(&cbbs[i - 1], &cbbs[i], CBS_ASN1_SEQUENCE)) {
  97. return false;
  98. }
  99. }
  100. uint8_t *ptr;
  101. if (!CBB_finish(cbb.get(), &ptr, out_len)) {
  102. return false;
  103. }
  104. out->reset(ptr);
  105. return true;
  106. }
  107. TEST(ASN1Test, Recursive) {
  108. bssl::UniquePtr<uint8_t> data;
  109. size_t len;
  110. // Sanity-check that MakeLinkedList can be parsed.
  111. ASSERT_TRUE(MakeLinkedList(&data, &len, 5));
  112. const uint8_t *ptr = data.get();
  113. ASN1_LINKED_LIST *list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
  114. EXPECT_TRUE(list);
  115. ASN1_LINKED_LIST_free(list);
  116. // Excessively deep structures are rejected.
  117. ASSERT_TRUE(MakeLinkedList(&data, &len, 100));
  118. ptr = data.get();
  119. list = d2i_ASN1_LINKED_LIST(nullptr, &ptr, len);
  120. EXPECT_FALSE(list);
  121. // Note checking the error queue here does not work. The error "stack trace"
  122. // is too deep, so the |ASN1_R_NESTED_TOO_DEEP| entry drops off the queue.
  123. ASN1_LINKED_LIST_free(list);
  124. }