2
0

asn1.c 11 KB

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