asn1_decode_test.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /*
  2. * Copyright 2017-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <openssl/rand.h>
  12. #include <openssl/asn1t.h>
  13. #include <openssl/obj_mac.h>
  14. #include "internal/numbers.h"
  15. #include "testutil.h"
  16. #ifdef __GNUC__
  17. # pragma GCC diagnostic ignored "-Wunused-function"
  18. #endif
  19. #ifdef __clang__
  20. # pragma clang diagnostic ignored "-Wunused-function"
  21. #endif
  22. /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
  23. static unsigned char t_invalid_zero[] = {
  24. 0x30, 0x02, /* SEQUENCE tag + length */
  25. 0x02, 0x00 /* INTEGER tag + length */
  26. };
  27. #ifndef OPENSSL_NO_DEPRECATED_3_0
  28. /* LONG case ************************************************************* */
  29. typedef struct {
  30. long test_long;
  31. } ASN1_LONG_DATA;
  32. ASN1_SEQUENCE(ASN1_LONG_DATA) = {
  33. ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
  34. } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
  35. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
  36. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
  37. static int test_long(void)
  38. {
  39. const unsigned char *p = t_invalid_zero;
  40. ASN1_LONG_DATA *dectst =
  41. d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
  42. if (dectst == NULL)
  43. return 0; /* Fail */
  44. ASN1_LONG_DATA_free(dectst);
  45. return 1;
  46. }
  47. #endif
  48. /* INT32 case ************************************************************* */
  49. typedef struct {
  50. int32_t test_int32;
  51. } ASN1_INT32_DATA;
  52. ASN1_SEQUENCE(ASN1_INT32_DATA) = {
  53. ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
  54. } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
  55. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
  56. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
  57. static int test_int32(void)
  58. {
  59. const unsigned char *p = t_invalid_zero;
  60. ASN1_INT32_DATA *dectst =
  61. d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  62. if (dectst == NULL)
  63. return 0; /* Fail */
  64. ASN1_INT32_DATA_free(dectst);
  65. return 1;
  66. }
  67. /* UINT32 case ************************************************************* */
  68. typedef struct {
  69. uint32_t test_uint32;
  70. } ASN1_UINT32_DATA;
  71. ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
  72. ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
  73. } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
  74. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
  75. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
  76. static int test_uint32(void)
  77. {
  78. const unsigned char *p = t_invalid_zero;
  79. ASN1_UINT32_DATA *dectst =
  80. d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  81. if (dectst == NULL)
  82. return 0; /* Fail */
  83. ASN1_UINT32_DATA_free(dectst);
  84. return 1;
  85. }
  86. /* INT64 case ************************************************************* */
  87. typedef struct {
  88. int64_t test_int64;
  89. } ASN1_INT64_DATA;
  90. ASN1_SEQUENCE(ASN1_INT64_DATA) = {
  91. ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
  92. } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
  93. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
  94. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
  95. static int test_int64(void)
  96. {
  97. const unsigned char *p = t_invalid_zero;
  98. ASN1_INT64_DATA *dectst =
  99. d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  100. if (dectst == NULL)
  101. return 0; /* Fail */
  102. ASN1_INT64_DATA_free(dectst);
  103. return 1;
  104. }
  105. /* UINT64 case ************************************************************* */
  106. typedef struct {
  107. uint64_t test_uint64;
  108. } ASN1_UINT64_DATA;
  109. ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
  110. ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
  111. } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
  112. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
  113. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
  114. static int test_uint64(void)
  115. {
  116. const unsigned char *p = t_invalid_zero;
  117. ASN1_UINT64_DATA *dectst =
  118. d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  119. if (dectst == NULL)
  120. return 0; /* Fail */
  121. ASN1_UINT64_DATA_free(dectst);
  122. return 1;
  123. }
  124. typedef struct {
  125. ASN1_STRING *invalidDirString;
  126. } INVALIDTEMPLATE;
  127. ASN1_SEQUENCE(INVALIDTEMPLATE) = {
  128. /*
  129. * DirectoryString is a CHOICE type so it must use explicit tagging -
  130. * but we deliberately use implicit here, which makes this template invalid.
  131. */
  132. ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
  133. } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
  134. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
  135. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
  136. /* Empty sequence for invalid template test */
  137. static unsigned char t_invalid_template[] = {
  138. 0x30, 0x03, /* SEQUENCE tag + length */
  139. 0x0c, 0x01, 0x41 /* UTF8String, length 1, "A" */
  140. };
  141. static int test_invalid_template(void)
  142. {
  143. const unsigned char *p = t_invalid_template;
  144. INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
  145. sizeof(t_invalid_template));
  146. /* We expect a NULL pointer return */
  147. if (TEST_ptr_null(tmp))
  148. return 1;
  149. INVALIDTEMPLATE_free(tmp);
  150. return 0;
  151. }
  152. static int test_reuse_asn1_object(void)
  153. {
  154. static unsigned char cn_der[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
  155. static unsigned char oid_der[] = {
  156. 0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
  157. };
  158. int ret = 0;
  159. ASN1_OBJECT *obj;
  160. unsigned char const *p = oid_der;
  161. /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
  162. if (!TEST_ptr(obj = ASN1_OBJECT_create(NID_undef, cn_der, sizeof(cn_der),
  163. "C", "countryName")))
  164. goto err;
  165. /* reuse obj - this should not leak sn and ln */
  166. if (!TEST_ptr(d2i_ASN1_OBJECT(&obj, &p, sizeof(oid_der))))
  167. goto err;
  168. ret = 1;
  169. err:
  170. ASN1_OBJECT_free(obj);
  171. return ret;
  172. }
  173. int setup_tests(void)
  174. {
  175. #ifndef OPENSSL_NO_DEPRECATED_3_0
  176. ADD_TEST(test_long);
  177. #endif
  178. ADD_TEST(test_int32);
  179. ADD_TEST(test_uint32);
  180. ADD_TEST(test_int64);
  181. ADD_TEST(test_uint64);
  182. ADD_TEST(test_invalid_template);
  183. ADD_TEST(test_reuse_asn1_object);
  184. return 1;
  185. }