asn1.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. * https://www.openssl.org/source/license.html
  8. * or in the file LICENSE in the source distribution.
  9. */
  10. /*
  11. * Fuzz ASN.1 parsing for various data structures. Specify which on the
  12. * command line:
  13. *
  14. * asn1 <data structure>
  15. */
  16. /* We need to use some deprecated APIs */
  17. #define OPENSSL_SUPPRESS_DEPRECATED
  18. #include <stdio.h>
  19. #include <string.h>
  20. #include <openssl/asn1.h>
  21. #include <openssl/asn1t.h>
  22. #include <openssl/dh.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/ec.h>
  25. #include <openssl/ocsp.h>
  26. #include <openssl/pkcs12.h>
  27. #include <openssl/rsa.h>
  28. #include <openssl/ts.h>
  29. #include <openssl/x509v3.h>
  30. #include <openssl/cms.h>
  31. #include <openssl/ess.h>
  32. #include <openssl/err.h>
  33. #include <openssl/rand.h>
  34. #include <openssl/bio.h>
  35. #include <openssl/evp.h>
  36. #include <openssl/ssl.h>
  37. #include "internal/nelem.h"
  38. #include "fuzzer.h"
  39. static ASN1_ITEM_EXP *item_type[] = {
  40. ASN1_ITEM_ref(ACCESS_DESCRIPTION),
  41. #ifndef OPENSSL_NO_RFC3779
  42. ASN1_ITEM_ref(ASIdentifierChoice),
  43. ASN1_ITEM_ref(ASIdentifiers),
  44. ASN1_ITEM_ref(ASIdOrRange),
  45. #endif
  46. ASN1_ITEM_ref(ASN1_ANY),
  47. ASN1_ITEM_ref(ASN1_BIT_STRING),
  48. ASN1_ITEM_ref(ASN1_BMPSTRING),
  49. ASN1_ITEM_ref(ASN1_BOOLEAN),
  50. ASN1_ITEM_ref(ASN1_ENUMERATED),
  51. ASN1_ITEM_ref(ASN1_FBOOLEAN),
  52. ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
  53. ASN1_ITEM_ref(ASN1_GENERALSTRING),
  54. ASN1_ITEM_ref(ASN1_IA5STRING),
  55. ASN1_ITEM_ref(ASN1_INTEGER),
  56. ASN1_ITEM_ref(ASN1_NULL),
  57. ASN1_ITEM_ref(ASN1_OBJECT),
  58. ASN1_ITEM_ref(ASN1_OCTET_STRING),
  59. ASN1_ITEM_ref(ASN1_OCTET_STRING_NDEF),
  60. ASN1_ITEM_ref(ASN1_PRINTABLE),
  61. ASN1_ITEM_ref(ASN1_PRINTABLESTRING),
  62. ASN1_ITEM_ref(ASN1_SEQUENCE),
  63. ASN1_ITEM_ref(ASN1_SEQUENCE_ANY),
  64. ASN1_ITEM_ref(ASN1_SET_ANY),
  65. ASN1_ITEM_ref(ASN1_T61STRING),
  66. ASN1_ITEM_ref(ASN1_TBOOLEAN),
  67. ASN1_ITEM_ref(ASN1_TIME),
  68. ASN1_ITEM_ref(ASN1_UNIVERSALSTRING),
  69. ASN1_ITEM_ref(ASN1_UTCTIME),
  70. ASN1_ITEM_ref(ASN1_UTF8STRING),
  71. ASN1_ITEM_ref(ASN1_VISIBLESTRING),
  72. #ifndef OPENSSL_NO_RFC3779
  73. ASN1_ITEM_ref(ASRange),
  74. #endif
  75. ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
  76. ASN1_ITEM_ref(AUTHORITY_KEYID),
  77. ASN1_ITEM_ref(BASIC_CONSTRAINTS),
  78. ASN1_ITEM_ref(BIGNUM),
  79. ASN1_ITEM_ref(CBIGNUM),
  80. ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  81. #ifndef OPENSSL_NO_CMS
  82. ASN1_ITEM_ref(CMS_ContentInfo),
  83. ASN1_ITEM_ref(CMS_ReceiptRequest),
  84. ASN1_ITEM_ref(CRL_DIST_POINTS),
  85. #endif
  86. #ifndef OPENSSL_NO_DH
  87. ASN1_ITEM_ref(DHparams),
  88. #endif
  89. ASN1_ITEM_ref(DIRECTORYSTRING),
  90. ASN1_ITEM_ref(DISPLAYTEXT),
  91. ASN1_ITEM_ref(DIST_POINT),
  92. ASN1_ITEM_ref(DIST_POINT_NAME),
  93. #if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  94. ASN1_ITEM_ref(ECPARAMETERS),
  95. ASN1_ITEM_ref(ECPKPARAMETERS),
  96. #endif
  97. ASN1_ITEM_ref(EDIPARTYNAME),
  98. ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
  99. ASN1_ITEM_ref(GENERAL_NAME),
  100. ASN1_ITEM_ref(GENERAL_NAMES),
  101. ASN1_ITEM_ref(GENERAL_SUBTREE),
  102. #ifndef OPENSSL_NO_RFC3779
  103. ASN1_ITEM_ref(IPAddressChoice),
  104. ASN1_ITEM_ref(IPAddressFamily),
  105. ASN1_ITEM_ref(IPAddressOrRange),
  106. ASN1_ITEM_ref(IPAddressRange),
  107. #endif
  108. ASN1_ITEM_ref(ISSUING_DIST_POINT),
  109. #ifndef OPENSSL_NO_DEPRECATED_3_0
  110. ASN1_ITEM_ref(LONG),
  111. #endif
  112. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  113. ASN1_ITEM_ref(NETSCAPE_CERT_SEQUENCE),
  114. ASN1_ITEM_ref(NETSCAPE_SPKAC),
  115. ASN1_ITEM_ref(NETSCAPE_SPKI),
  116. ASN1_ITEM_ref(NOTICEREF),
  117. #ifndef OPENSSL_NO_OCSP
  118. ASN1_ITEM_ref(OCSP_BASICRESP),
  119. ASN1_ITEM_ref(OCSP_CERTID),
  120. ASN1_ITEM_ref(OCSP_CERTSTATUS),
  121. ASN1_ITEM_ref(OCSP_CRLID),
  122. ASN1_ITEM_ref(OCSP_ONEREQ),
  123. ASN1_ITEM_ref(OCSP_REQINFO),
  124. ASN1_ITEM_ref(OCSP_REQUEST),
  125. ASN1_ITEM_ref(OCSP_RESPBYTES),
  126. ASN1_ITEM_ref(OCSP_RESPDATA),
  127. ASN1_ITEM_ref(OCSP_RESPID),
  128. ASN1_ITEM_ref(OCSP_RESPONSE),
  129. ASN1_ITEM_ref(OCSP_REVOKEDINFO),
  130. ASN1_ITEM_ref(OCSP_SERVICELOC),
  131. ASN1_ITEM_ref(OCSP_SIGNATURE),
  132. ASN1_ITEM_ref(OCSP_SINGLERESP),
  133. #endif
  134. ASN1_ITEM_ref(OTHERNAME),
  135. ASN1_ITEM_ref(PBE2PARAM),
  136. ASN1_ITEM_ref(PBEPARAM),
  137. ASN1_ITEM_ref(PBKDF2PARAM),
  138. ASN1_ITEM_ref(PKCS12),
  139. ASN1_ITEM_ref(PKCS12_AUTHSAFES),
  140. ASN1_ITEM_ref(PKCS12_BAGS),
  141. ASN1_ITEM_ref(PKCS12_MAC_DATA),
  142. ASN1_ITEM_ref(PKCS12_SAFEBAG),
  143. ASN1_ITEM_ref(PKCS12_SAFEBAGS),
  144. ASN1_ITEM_ref(PKCS7),
  145. ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
  146. ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
  147. ASN1_ITEM_ref(PKCS7_DIGEST),
  148. ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
  149. ASN1_ITEM_ref(PKCS7_ENCRYPT),
  150. ASN1_ITEM_ref(PKCS7_ENVELOPE),
  151. ASN1_ITEM_ref(PKCS7_ISSUER_AND_SERIAL),
  152. ASN1_ITEM_ref(PKCS7_RECIP_INFO),
  153. ASN1_ITEM_ref(PKCS7_SIGNED),
  154. ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
  155. ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
  156. ASN1_ITEM_ref(PKCS8_PRIV_KEY_INFO),
  157. ASN1_ITEM_ref(PKEY_USAGE_PERIOD),
  158. ASN1_ITEM_ref(POLICY_CONSTRAINTS),
  159. ASN1_ITEM_ref(POLICYINFO),
  160. ASN1_ITEM_ref(POLICY_MAPPING),
  161. ASN1_ITEM_ref(POLICY_MAPPINGS),
  162. ASN1_ITEM_ref(POLICYQUALINFO),
  163. ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
  164. ASN1_ITEM_ref(PROXY_POLICY),
  165. ASN1_ITEM_ref(RSA_OAEP_PARAMS),
  166. ASN1_ITEM_ref(RSA_PSS_PARAMS),
  167. #ifndef OPENSSL_NO_DEPRECATED_3_0
  168. ASN1_ITEM_ref(RSAPrivateKey),
  169. ASN1_ITEM_ref(RSAPublicKey),
  170. #endif
  171. ASN1_ITEM_ref(SXNET),
  172. ASN1_ITEM_ref(SXNETID),
  173. ASN1_ITEM_ref(USERNOTICE),
  174. ASN1_ITEM_ref(X509),
  175. ASN1_ITEM_ref(X509_ALGOR),
  176. ASN1_ITEM_ref(X509_ALGORS),
  177. ASN1_ITEM_ref(X509_ATTRIBUTE),
  178. ASN1_ITEM_ref(X509_CERT_AUX),
  179. ASN1_ITEM_ref(X509_CINF),
  180. ASN1_ITEM_ref(X509_CRL),
  181. ASN1_ITEM_ref(X509_CRL_INFO),
  182. ASN1_ITEM_ref(X509_EXTENSION),
  183. ASN1_ITEM_ref(X509_EXTENSIONS),
  184. ASN1_ITEM_ref(X509_NAME),
  185. ASN1_ITEM_ref(X509_NAME_ENTRY),
  186. ASN1_ITEM_ref(X509_PUBKEY),
  187. ASN1_ITEM_ref(X509_REQ),
  188. ASN1_ITEM_ref(X509_REQ_INFO),
  189. ASN1_ITEM_ref(X509_REVOKED),
  190. ASN1_ITEM_ref(X509_SIG),
  191. ASN1_ITEM_ref(X509_VAL),
  192. #ifndef OPENSSL_NO_DEPRECATED_3_0
  193. ASN1_ITEM_ref(ZLONG),
  194. #endif
  195. ASN1_ITEM_ref(INT32),
  196. ASN1_ITEM_ref(ZINT32),
  197. ASN1_ITEM_ref(UINT32),
  198. ASN1_ITEM_ref(ZUINT32),
  199. ASN1_ITEM_ref(INT64),
  200. ASN1_ITEM_ref(ZINT64),
  201. ASN1_ITEM_ref(UINT64),
  202. ASN1_ITEM_ref(ZUINT64),
  203. NULL
  204. };
  205. static ASN1_PCTX *pctx;
  206. #define DO_TEST(TYPE, D2I, I2D, PRINT) { \
  207. const unsigned char *p = buf; \
  208. unsigned char *der = NULL; \
  209. TYPE *type = D2I(NULL, &p, len); \
  210. \
  211. if (type != NULL) { \
  212. int len2; \
  213. BIO *bio = BIO_new(BIO_s_null()); \
  214. \
  215. PRINT(bio, type); \
  216. BIO_free(bio); \
  217. len2 = I2D(type, &der); \
  218. if (len2 != 0) {} \
  219. OPENSSL_free(der); \
  220. TYPE ## _free(type); \
  221. } \
  222. }
  223. #define DO_TEST_PRINT_OFFSET(TYPE, D2I, I2D, PRINT) { \
  224. const unsigned char *p = buf; \
  225. unsigned char *der = NULL; \
  226. TYPE *type = D2I(NULL, &p, len); \
  227. \
  228. if (type != NULL) { \
  229. BIO *bio = BIO_new(BIO_s_null()); \
  230. \
  231. PRINT(bio, type, 0); \
  232. BIO_free(bio); \
  233. I2D(type, &der); \
  234. OPENSSL_free(der); \
  235. TYPE ## _free(type); \
  236. } \
  237. }
  238. #define DO_TEST_PRINT_PCTX(TYPE, D2I, I2D, PRINT) { \
  239. const unsigned char *p = buf; \
  240. unsigned char *der = NULL; \
  241. TYPE *type = D2I(NULL, &p, len); \
  242. \
  243. if (type != NULL) { \
  244. BIO *bio = BIO_new(BIO_s_null()); \
  245. \
  246. PRINT(bio, type, 0, pctx); \
  247. BIO_free(bio); \
  248. I2D(type, &der); \
  249. OPENSSL_free(der); \
  250. TYPE ## _free(type); \
  251. } \
  252. }
  253. #define DO_TEST_NO_PRINT(TYPE, D2I, I2D) { \
  254. const unsigned char *p = buf; \
  255. unsigned char *der = NULL; \
  256. TYPE *type = D2I(NULL, &p, len); \
  257. \
  258. if (type != NULL) { \
  259. BIO *bio = BIO_new(BIO_s_null()); \
  260. \
  261. BIO_free(bio); \
  262. I2D(type, &der); \
  263. OPENSSL_free(der); \
  264. TYPE ## _free(type); \
  265. } \
  266. }
  267. int FuzzerInitialize(int *argc, char ***argv)
  268. {
  269. FuzzerSetRand();
  270. pctx = ASN1_PCTX_new();
  271. ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
  272. ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
  273. ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
  274. ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
  275. ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
  276. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  277. OPENSSL_init_ssl(OPENSSL_INIT_LOAD_SSL_STRINGS, NULL);
  278. ERR_clear_error();
  279. CRYPTO_free_ex_index(0, -1);
  280. return 1;
  281. }
  282. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  283. {
  284. int n;
  285. for (n = 0; item_type[n] != NULL; ++n) {
  286. const uint8_t *b = buf;
  287. unsigned char *der = NULL;
  288. const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
  289. ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
  290. if (o != NULL) {
  291. BIO *bio = BIO_new(BIO_s_null());
  292. ASN1_item_print(bio, o, 4, i, pctx);
  293. BIO_free(bio);
  294. ASN1_item_i2d(o, &der, i);
  295. OPENSSL_free(der);
  296. ASN1_item_free(o, i);
  297. }
  298. }
  299. #ifndef OPENSSL_NO_TS
  300. DO_TEST(TS_REQ, d2i_TS_REQ, i2d_TS_REQ, TS_REQ_print_bio);
  301. DO_TEST(TS_MSG_IMPRINT, d2i_TS_MSG_IMPRINT, i2d_TS_MSG_IMPRINT, TS_MSG_IMPRINT_print_bio);
  302. DO_TEST(TS_RESP, d2i_TS_RESP, i2d_TS_RESP, TS_RESP_print_bio);
  303. DO_TEST(TS_STATUS_INFO, d2i_TS_STATUS_INFO, i2d_TS_STATUS_INFO, TS_STATUS_INFO_print_bio);
  304. DO_TEST(TS_TST_INFO, d2i_TS_TST_INFO, i2d_TS_TST_INFO, TS_TST_INFO_print_bio);
  305. DO_TEST_NO_PRINT(TS_ACCURACY, d2i_TS_ACCURACY, i2d_TS_ACCURACY);
  306. #endif
  307. DO_TEST_NO_PRINT(ESS_ISSUER_SERIAL, d2i_ESS_ISSUER_SERIAL, i2d_ESS_ISSUER_SERIAL);
  308. DO_TEST_NO_PRINT(ESS_CERT_ID, d2i_ESS_CERT_ID, i2d_ESS_CERT_ID);
  309. DO_TEST_NO_PRINT(ESS_SIGNING_CERT, d2i_ESS_SIGNING_CERT, i2d_ESS_SIGNING_CERT);
  310. DO_TEST_NO_PRINT(ESS_CERT_ID_V2, d2i_ESS_CERT_ID_V2, i2d_ESS_CERT_ID_V2);
  311. DO_TEST_NO_PRINT(ESS_SIGNING_CERT_V2, d2i_ESS_SIGNING_CERT_V2, i2d_ESS_SIGNING_CERT_V2);
  312. #if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  313. DO_TEST_NO_PRINT(DH, d2i_DHparams, i2d_DHparams);
  314. DO_TEST_NO_PRINT(DH, d2i_DHxparams, i2d_DHxparams);
  315. #endif
  316. #ifndef OPENSSL_NO_DSA
  317. DO_TEST_NO_PRINT(DSA_SIG, d2i_DSA_SIG, i2d_DSA_SIG);
  318. # ifndef OPENSSL_NO_DEPRECATED_3_0
  319. DO_TEST_NO_PRINT(DSA, d2i_DSAPrivateKey, i2d_DSAPrivateKey);
  320. DO_TEST_NO_PRINT(DSA, d2i_DSAPublicKey, i2d_DSAPublicKey);
  321. DO_TEST_NO_PRINT(DSA, d2i_DSAparams, i2d_DSAparams);
  322. # endif
  323. #endif
  324. #ifndef OPENSSL_NO_DEPRECATED_3_0
  325. DO_TEST_NO_PRINT(RSA, d2i_RSAPublicKey, i2d_RSAPublicKey);
  326. #endif
  327. #ifndef OPENSSL_NO_EC
  328. # ifndef OPENSSL_NO_DEPRECATED_3_0
  329. DO_TEST_PRINT_OFFSET(EC_GROUP, d2i_ECPKParameters, i2d_ECPKParameters, ECPKParameters_print);
  330. DO_TEST_PRINT_OFFSET(EC_KEY, d2i_ECPrivateKey, i2d_ECPrivateKey, EC_KEY_print);
  331. DO_TEST(EC_KEY, d2i_ECParameters, i2d_ECParameters, ECParameters_print);
  332. DO_TEST_NO_PRINT(ECDSA_SIG, d2i_ECDSA_SIG, i2d_ECDSA_SIG);
  333. # endif
  334. #endif
  335. DO_TEST_PRINT_PCTX(EVP_PKEY, d2i_AutoPrivateKey, i2d_PrivateKey, EVP_PKEY_print_private);
  336. DO_TEST(SSL_SESSION, d2i_SSL_SESSION, i2d_SSL_SESSION, SSL_SESSION_print);
  337. ERR_clear_error();
  338. return 0;
  339. }
  340. void FuzzerCleanup(void)
  341. {
  342. ASN1_PCTX_free(pctx);
  343. FuzzerClearRand();
  344. }