Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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