asn1_decode_test.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  1. /*
  2. * Copyright 2017-2024 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/asn1.h>
  13. #include <openssl/asn1t.h>
  14. #include <openssl/obj_mac.h>
  15. #include "internal/numbers.h"
  16. #include "testutil.h"
  17. #ifdef __GNUC__
  18. # pragma GCC diagnostic ignored "-Wunused-function"
  19. #endif
  20. #ifdef __clang__
  21. # pragma clang diagnostic ignored "-Wunused-function"
  22. #endif
  23. /* Badly coded ASN.1 INTEGER zero wrapped in a sequence */
  24. static unsigned char t_invalid_zero[] = {
  25. 0x30, 0x02, /* SEQUENCE tag + length */
  26. 0x02, 0x00 /* INTEGER tag + length */
  27. };
  28. #ifndef OPENSSL_NO_DEPRECATED_3_0
  29. /* LONG case ************************************************************* */
  30. typedef struct {
  31. long test_long;
  32. } ASN1_LONG_DATA;
  33. ASN1_SEQUENCE(ASN1_LONG_DATA) = {
  34. ASN1_EMBED(ASN1_LONG_DATA, test_long, LONG),
  35. } static_ASN1_SEQUENCE_END(ASN1_LONG_DATA)
  36. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_LONG_DATA)
  37. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_LONG_DATA)
  38. static int test_long(void)
  39. {
  40. const unsigned char *p = t_invalid_zero;
  41. ASN1_LONG_DATA *dectst =
  42. d2i_ASN1_LONG_DATA(NULL, &p, sizeof(t_invalid_zero));
  43. if (dectst == NULL)
  44. return 0; /* Fail */
  45. ASN1_LONG_DATA_free(dectst);
  46. return 1;
  47. }
  48. #endif
  49. /* INT32 case ************************************************************* */
  50. typedef struct {
  51. int32_t test_int32;
  52. } ASN1_INT32_DATA;
  53. ASN1_SEQUENCE(ASN1_INT32_DATA) = {
  54. ASN1_EMBED(ASN1_INT32_DATA, test_int32, INT32),
  55. } static_ASN1_SEQUENCE_END(ASN1_INT32_DATA)
  56. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT32_DATA)
  57. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT32_DATA)
  58. static int test_int32(void)
  59. {
  60. const unsigned char *p = t_invalid_zero;
  61. ASN1_INT32_DATA *dectst =
  62. d2i_ASN1_INT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  63. if (dectst == NULL)
  64. return 0; /* Fail */
  65. ASN1_INT32_DATA_free(dectst);
  66. return 1;
  67. }
  68. /* UINT32 case ************************************************************* */
  69. typedef struct {
  70. uint32_t test_uint32;
  71. } ASN1_UINT32_DATA;
  72. ASN1_SEQUENCE(ASN1_UINT32_DATA) = {
  73. ASN1_EMBED(ASN1_UINT32_DATA, test_uint32, UINT32),
  74. } static_ASN1_SEQUENCE_END(ASN1_UINT32_DATA)
  75. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT32_DATA)
  76. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT32_DATA)
  77. static int test_uint32(void)
  78. {
  79. const unsigned char *p = t_invalid_zero;
  80. ASN1_UINT32_DATA *dectst =
  81. d2i_ASN1_UINT32_DATA(NULL, &p, sizeof(t_invalid_zero));
  82. if (dectst == NULL)
  83. return 0; /* Fail */
  84. ASN1_UINT32_DATA_free(dectst);
  85. return 1;
  86. }
  87. /* INT64 case ************************************************************* */
  88. typedef struct {
  89. int64_t test_int64;
  90. } ASN1_INT64_DATA;
  91. ASN1_SEQUENCE(ASN1_INT64_DATA) = {
  92. ASN1_EMBED(ASN1_INT64_DATA, test_int64, INT64),
  93. } static_ASN1_SEQUENCE_END(ASN1_INT64_DATA)
  94. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_INT64_DATA)
  95. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_INT64_DATA)
  96. static int test_int64(void)
  97. {
  98. const unsigned char *p = t_invalid_zero;
  99. ASN1_INT64_DATA *dectst =
  100. d2i_ASN1_INT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  101. if (dectst == NULL)
  102. return 0; /* Fail */
  103. ASN1_INT64_DATA_free(dectst);
  104. return 1;
  105. }
  106. /* UINT64 case ************************************************************* */
  107. typedef struct {
  108. uint64_t test_uint64;
  109. } ASN1_UINT64_DATA;
  110. ASN1_SEQUENCE(ASN1_UINT64_DATA) = {
  111. ASN1_EMBED(ASN1_UINT64_DATA, test_uint64, UINT64),
  112. } static_ASN1_SEQUENCE_END(ASN1_UINT64_DATA)
  113. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(ASN1_UINT64_DATA)
  114. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(ASN1_UINT64_DATA)
  115. static int test_uint64(void)
  116. {
  117. const unsigned char *p = t_invalid_zero;
  118. ASN1_UINT64_DATA *dectst =
  119. d2i_ASN1_UINT64_DATA(NULL, &p, sizeof(t_invalid_zero));
  120. if (dectst == NULL)
  121. return 0; /* Fail */
  122. ASN1_UINT64_DATA_free(dectst);
  123. return 1;
  124. }
  125. /* GeneralizedTime underflow *********************************************** */
  126. static int test_gentime(void)
  127. {
  128. /* Underflowing GeneralizedTime 161208193400Z (YYMMDDHHMMSSZ) */
  129. const unsigned char der[] = {
  130. 0x18, 0x0d, 0x31, 0x36, 0x31, 0x32, 0x30, 0x38, 0x31,
  131. 0x39, 0x33, 0x34, 0x30, 0x30, 0x5a,
  132. };
  133. const unsigned char *p;
  134. int der_len, rc = 1;
  135. ASN1_GENERALIZEDTIME *gentime;
  136. p = der;
  137. der_len = sizeof(der);
  138. gentime = d2i_ASN1_GENERALIZEDTIME(NULL, &p, der_len);
  139. if (!TEST_ptr_null(gentime))
  140. rc = 0; /* fail */
  141. ASN1_GENERALIZEDTIME_free(gentime);
  142. return rc;
  143. }
  144. /* UTCTime underflow ******************************************************* */
  145. static int test_utctime(void)
  146. {
  147. /* Underflowing UTCTime 0205104700Z (MMDDHHMMSSZ) */
  148. const unsigned char der[] = {
  149. 0x17, 0x0b, 0x30, 0x32, 0x30, 0x35, 0x31, 0x30,
  150. 0x34, 0x37, 0x30, 0x30, 0x5a,
  151. };
  152. const unsigned char *p;
  153. int der_len, rc = 1;
  154. ASN1_UTCTIME *utctime;
  155. p = der;
  156. der_len = sizeof(der);
  157. utctime = d2i_ASN1_UTCTIME(NULL, &p, der_len);
  158. if (!TEST_ptr_null(utctime))
  159. rc = 0; /* fail */
  160. ASN1_UTCTIME_free(utctime);
  161. return rc;
  162. }
  163. /* Invalid template ******************************************************** */
  164. typedef struct {
  165. ASN1_STRING *invalidDirString;
  166. } INVALIDTEMPLATE;
  167. ASN1_SEQUENCE(INVALIDTEMPLATE) = {
  168. /*
  169. * DirectoryString is a CHOICE type so it must use explicit tagging -
  170. * but we deliberately use implicit here, which makes this template invalid.
  171. */
  172. ASN1_IMP(INVALIDTEMPLATE, invalidDirString, DIRECTORYSTRING, 12)
  173. } static_ASN1_SEQUENCE_END(INVALIDTEMPLATE)
  174. IMPLEMENT_STATIC_ASN1_ENCODE_FUNCTIONS(INVALIDTEMPLATE)
  175. IMPLEMENT_STATIC_ASN1_ALLOC_FUNCTIONS(INVALIDTEMPLATE)
  176. /* Empty sequence for invalid template test */
  177. static unsigned char t_invalid_template[] = {
  178. 0x30, 0x03, /* SEQUENCE tag + length */
  179. 0x0c, 0x01, 0x41 /* UTF8String, length 1, "A" */
  180. };
  181. static int test_invalid_template(void)
  182. {
  183. const unsigned char *p = t_invalid_template;
  184. INVALIDTEMPLATE *tmp = d2i_INVALIDTEMPLATE(NULL, &p,
  185. sizeof(t_invalid_template));
  186. /* We expect a NULL pointer return */
  187. if (TEST_ptr_null(tmp))
  188. return 1;
  189. INVALIDTEMPLATE_free(tmp);
  190. return 0;
  191. }
  192. static int test_reuse_asn1_object(void)
  193. {
  194. static unsigned char cn_der[] = { 0x06, 0x03, 0x55, 0x04, 0x06 };
  195. static unsigned char oid_der[] = {
  196. 0x06, 0x06, 0x2a, 0x03, 0x04, 0x05, 0x06, 0x07
  197. };
  198. int ret = 0;
  199. ASN1_OBJECT *obj;
  200. unsigned char const *p = oid_der;
  201. /* Create an object that owns dynamically allocated 'sn' and 'ln' fields */
  202. if (!TEST_ptr(obj = ASN1_OBJECT_create(NID_undef, cn_der, sizeof(cn_der),
  203. "C", "countryName")))
  204. goto err;
  205. /* reuse obj - this should not leak sn and ln */
  206. if (!TEST_ptr(d2i_ASN1_OBJECT(&obj, &p, sizeof(oid_der))))
  207. goto err;
  208. ret = 1;
  209. err:
  210. ASN1_OBJECT_free(obj);
  211. return ret;
  212. }
  213. int setup_tests(void)
  214. {
  215. #ifndef OPENSSL_NO_DEPRECATED_3_0
  216. ADD_TEST(test_long);
  217. #endif
  218. ADD_TEST(test_int32);
  219. ADD_TEST(test_uint32);
  220. ADD_TEST(test_int64);
  221. ADD_TEST(test_uint64);
  222. ADD_TEST(test_gentime);
  223. ADD_TEST(test_utctime);
  224. ADD_TEST(test_invalid_template);
  225. ADD_TEST(test_reuse_asn1_object);
  226. return 1;
  227. }