asn1.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222
  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 "fuzzer.h"
  29. static ASN1_ITEM_EXP *item_type[] = {
  30. ASN1_ITEM_ref(ACCESS_DESCRIPTION),
  31. #ifndef OPENSSL_NO_RFC3779
  32. ASN1_ITEM_ref(ASIdentifierChoice),
  33. ASN1_ITEM_ref(ASIdentifiers),
  34. ASN1_ITEM_ref(ASIdOrRange),
  35. #endif
  36. ASN1_ITEM_ref(ASN1_ANY),
  37. ASN1_ITEM_ref(ASN1_BIT_STRING),
  38. ASN1_ITEM_ref(ASN1_BMPSTRING),
  39. ASN1_ITEM_ref(ASN1_BOOLEAN),
  40. ASN1_ITEM_ref(ASN1_ENUMERATED),
  41. ASN1_ITEM_ref(ASN1_FBOOLEAN),
  42. ASN1_ITEM_ref(ASN1_GENERALIZEDTIME),
  43. ASN1_ITEM_ref(ASN1_GENERALSTRING),
  44. ASN1_ITEM_ref(ASN1_IA5STRING),
  45. ASN1_ITEM_ref(ASN1_INTEGER),
  46. ASN1_ITEM_ref(ASN1_NULL),
  47. ASN1_ITEM_ref(ASN1_OBJECT),
  48. ASN1_ITEM_ref(ASN1_OCTET_STRING),
  49. ASN1_ITEM_ref(ASN1_OCTET_STRING_NDEF),
  50. ASN1_ITEM_ref(ASN1_PRINTABLE),
  51. ASN1_ITEM_ref(ASN1_PRINTABLESTRING),
  52. ASN1_ITEM_ref(ASN1_SEQUENCE),
  53. ASN1_ITEM_ref(ASN1_SEQUENCE_ANY),
  54. ASN1_ITEM_ref(ASN1_SET_ANY),
  55. ASN1_ITEM_ref(ASN1_T61STRING),
  56. ASN1_ITEM_ref(ASN1_TBOOLEAN),
  57. ASN1_ITEM_ref(ASN1_TIME),
  58. ASN1_ITEM_ref(ASN1_UNIVERSALSTRING),
  59. ASN1_ITEM_ref(ASN1_UTCTIME),
  60. ASN1_ITEM_ref(ASN1_UTF8STRING),
  61. ASN1_ITEM_ref(ASN1_VISIBLESTRING),
  62. #ifndef OPENSSL_NO_RFC3779
  63. ASN1_ITEM_ref(ASRange),
  64. #endif
  65. ASN1_ITEM_ref(AUTHORITY_INFO_ACCESS),
  66. ASN1_ITEM_ref(AUTHORITY_KEYID),
  67. ASN1_ITEM_ref(BASIC_CONSTRAINTS),
  68. ASN1_ITEM_ref(BIGNUM),
  69. ASN1_ITEM_ref(CBIGNUM),
  70. ASN1_ITEM_ref(CERTIFICATEPOLICIES),
  71. #ifndef OPENSSL_NO_CMS
  72. ASN1_ITEM_ref(CMS_ContentInfo),
  73. ASN1_ITEM_ref(CMS_ReceiptRequest),
  74. ASN1_ITEM_ref(CRL_DIST_POINTS),
  75. #endif
  76. #ifndef OPENSSL_NO_DH
  77. ASN1_ITEM_ref(DHparams),
  78. #endif
  79. ASN1_ITEM_ref(DIRECTORYSTRING),
  80. ASN1_ITEM_ref(DISPLAYTEXT),
  81. ASN1_ITEM_ref(DIST_POINT),
  82. ASN1_ITEM_ref(DIST_POINT_NAME),
  83. #ifndef OPENSSL_NO_EC
  84. ASN1_ITEM_ref(ECPARAMETERS),
  85. ASN1_ITEM_ref(ECPKPARAMETERS),
  86. #endif
  87. ASN1_ITEM_ref(EDIPARTYNAME),
  88. ASN1_ITEM_ref(EXTENDED_KEY_USAGE),
  89. ASN1_ITEM_ref(GENERAL_NAME),
  90. ASN1_ITEM_ref(GENERAL_NAMES),
  91. ASN1_ITEM_ref(GENERAL_SUBTREE),
  92. #ifndef OPENSSL_NO_RFC3779
  93. ASN1_ITEM_ref(IPAddressChoice),
  94. ASN1_ITEM_ref(IPAddressFamily),
  95. ASN1_ITEM_ref(IPAddressOrRange),
  96. ASN1_ITEM_ref(IPAddressRange),
  97. #endif
  98. ASN1_ITEM_ref(ISSUING_DIST_POINT),
  99. ASN1_ITEM_ref(LONG),
  100. ASN1_ITEM_ref(NAME_CONSTRAINTS),
  101. ASN1_ITEM_ref(NETSCAPE_CERT_SEQUENCE),
  102. ASN1_ITEM_ref(NETSCAPE_SPKAC),
  103. ASN1_ITEM_ref(NETSCAPE_SPKI),
  104. ASN1_ITEM_ref(NOTICEREF),
  105. #ifndef OPENSSL_NO_OCSP
  106. ASN1_ITEM_ref(OCSP_BASICRESP),
  107. ASN1_ITEM_ref(OCSP_CERTID),
  108. ASN1_ITEM_ref(OCSP_CERTSTATUS),
  109. ASN1_ITEM_ref(OCSP_CRLID),
  110. ASN1_ITEM_ref(OCSP_ONEREQ),
  111. ASN1_ITEM_ref(OCSP_REQINFO),
  112. ASN1_ITEM_ref(OCSP_REQUEST),
  113. ASN1_ITEM_ref(OCSP_RESPBYTES),
  114. ASN1_ITEM_ref(OCSP_RESPDATA),
  115. ASN1_ITEM_ref(OCSP_RESPID),
  116. ASN1_ITEM_ref(OCSP_RESPONSE),
  117. ASN1_ITEM_ref(OCSP_REVOKEDINFO),
  118. ASN1_ITEM_ref(OCSP_SERVICELOC),
  119. ASN1_ITEM_ref(OCSP_SIGNATURE),
  120. ASN1_ITEM_ref(OCSP_SINGLERESP),
  121. #endif
  122. ASN1_ITEM_ref(OTHERNAME),
  123. ASN1_ITEM_ref(PBE2PARAM),
  124. ASN1_ITEM_ref(PBEPARAM),
  125. ASN1_ITEM_ref(PBKDF2PARAM),
  126. ASN1_ITEM_ref(PKCS12),
  127. ASN1_ITEM_ref(PKCS12_AUTHSAFES),
  128. ASN1_ITEM_ref(PKCS12_BAGS),
  129. ASN1_ITEM_ref(PKCS12_MAC_DATA),
  130. ASN1_ITEM_ref(PKCS12_SAFEBAG),
  131. ASN1_ITEM_ref(PKCS12_SAFEBAGS),
  132. ASN1_ITEM_ref(PKCS7),
  133. ASN1_ITEM_ref(PKCS7_ATTR_SIGN),
  134. ASN1_ITEM_ref(PKCS7_ATTR_VERIFY),
  135. ASN1_ITEM_ref(PKCS7_DIGEST),
  136. ASN1_ITEM_ref(PKCS7_ENC_CONTENT),
  137. ASN1_ITEM_ref(PKCS7_ENCRYPT),
  138. ASN1_ITEM_ref(PKCS7_ENVELOPE),
  139. ASN1_ITEM_ref(PKCS7_ISSUER_AND_SERIAL),
  140. ASN1_ITEM_ref(PKCS7_RECIP_INFO),
  141. ASN1_ITEM_ref(PKCS7_SIGNED),
  142. ASN1_ITEM_ref(PKCS7_SIGN_ENVELOPE),
  143. ASN1_ITEM_ref(PKCS7_SIGNER_INFO),
  144. ASN1_ITEM_ref(PKCS8_PRIV_KEY_INFO),
  145. ASN1_ITEM_ref(PKEY_USAGE_PERIOD),
  146. ASN1_ITEM_ref(POLICY_CONSTRAINTS),
  147. ASN1_ITEM_ref(POLICYINFO),
  148. ASN1_ITEM_ref(POLICY_MAPPING),
  149. ASN1_ITEM_ref(POLICY_MAPPINGS),
  150. ASN1_ITEM_ref(POLICYQUALINFO),
  151. ASN1_ITEM_ref(PROXY_CERT_INFO_EXTENSION),
  152. ASN1_ITEM_ref(PROXY_POLICY),
  153. ASN1_ITEM_ref(RSA_OAEP_PARAMS),
  154. ASN1_ITEM_ref(RSAPrivateKey),
  155. ASN1_ITEM_ref(RSA_PSS_PARAMS),
  156. ASN1_ITEM_ref(RSAPublicKey),
  157. ASN1_ITEM_ref(SXNET),
  158. ASN1_ITEM_ref(SXNETID),
  159. /*ASN1_ITEM_ref(TS_RESP), want to do this, but type is hidden, however d2i exists... */
  160. ASN1_ITEM_ref(USERNOTICE),
  161. ASN1_ITEM_ref(X509),
  162. ASN1_ITEM_ref(X509_ALGOR),
  163. ASN1_ITEM_ref(X509_ALGORS),
  164. ASN1_ITEM_ref(X509_ATTRIBUTE),
  165. ASN1_ITEM_ref(X509_CERT_AUX),
  166. ASN1_ITEM_ref(X509_CINF),
  167. ASN1_ITEM_ref(X509_CRL),
  168. ASN1_ITEM_ref(X509_CRL_INFO),
  169. ASN1_ITEM_ref(X509_EXTENSION),
  170. ASN1_ITEM_ref(X509_EXTENSIONS),
  171. ASN1_ITEM_ref(X509_NAME),
  172. ASN1_ITEM_ref(X509_NAME_ENTRY),
  173. ASN1_ITEM_ref(X509_PUBKEY),
  174. ASN1_ITEM_ref(X509_REQ),
  175. ASN1_ITEM_ref(X509_REQ_INFO),
  176. ASN1_ITEM_ref(X509_REVOKED),
  177. ASN1_ITEM_ref(X509_SIG),
  178. ASN1_ITEM_ref(X509_VAL),
  179. ASN1_ITEM_ref(ZLONG),
  180. NULL
  181. };
  182. int FuzzerInitialize(int *argc, char ***argv) {
  183. return 1;
  184. }
  185. int FuzzerTestOneInput(const uint8_t *buf, size_t len) {
  186. int n;
  187. ASN1_PCTX *pctx = ASN1_PCTX_new();
  188. ASN1_PCTX_set_flags(pctx, ASN1_PCTX_FLAGS_SHOW_ABSENT |
  189. ASN1_PCTX_FLAGS_SHOW_SEQUENCE | ASN1_PCTX_FLAGS_SHOW_SSOF |
  190. ASN1_PCTX_FLAGS_SHOW_TYPE | ASN1_PCTX_FLAGS_SHOW_FIELD_STRUCT_NAME);
  191. ASN1_PCTX_set_str_flags(pctx, ASN1_STRFLGS_UTF8_CONVERT |
  192. ASN1_STRFLGS_SHOW_TYPE | ASN1_STRFLGS_DUMP_ALL);
  193. for (n = 0; item_type[n] != NULL; ++n) {
  194. const uint8_t *b = buf;
  195. unsigned char *der = NULL;
  196. const ASN1_ITEM *i = ASN1_ITEM_ptr(item_type[n]);
  197. ASN1_VALUE *o = ASN1_item_d2i(NULL, &b, len, i);
  198. if (o != NULL) {
  199. BIO *bio = BIO_new(BIO_s_null());
  200. ASN1_item_print(bio, o, 4, i, pctx);
  201. BIO_free(bio);
  202. ASN1_item_i2d(o, &der, i);
  203. OPENSSL_free(der);
  204. ASN1_item_free(o, i);
  205. }
  206. }
  207. ASN1_PCTX_free(pctx);
  208. return 0;
  209. }