cmp.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. /*
  2. * Copyright 2007-2020 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. /*
  10. * Test CMP DER parsing.
  11. */
  12. #include <openssl/bio.h>
  13. #include <openssl/cmp.h>
  14. #include "../crypto/cmp/cmp_local.h"
  15. #include <openssl/err.h>
  16. #include "fuzzer.h"
  17. #include "rand.inc"
  18. int FuzzerInitialize(int *argc, char ***argv)
  19. {
  20. OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
  21. ERR_clear_error();
  22. CRYPTO_free_ex_index(0, -1);
  23. FuzzerSetRand();
  24. return 1;
  25. }
  26. static int num_responses;
  27. static OSSL_CMP_MSG *transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req)
  28. {
  29. if (num_responses++ > 2)
  30. return NULL; /* prevent loops due to repeated pollRep */
  31. return OSSL_CMP_MSG_dup((OSSL_CMP_MSG *)
  32. OSSL_CMP_CTX_get_transfer_cb_arg(ctx));
  33. }
  34. static int print_noop(const char *func, const char *file, int line,
  35. OSSL_CMP_severity level, const char *msg)
  36. {
  37. return 1;
  38. }
  39. static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *rep,
  40. int invalid_protection, int expected_type)
  41. {
  42. return 1;
  43. }
  44. static void cmp_client_process_response(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
  45. {
  46. X509_NAME *name = X509_NAME_new();
  47. ASN1_INTEGER *serial = ASN1_INTEGER_new();
  48. ctx->unprotectedSend = 1; /* satisfy ossl_cmp_msg_protect() */
  49. ctx->disableConfirm = 1; /* check just one response message */
  50. ctx->popoMethod = OSSL_CRMF_POPO_NONE; /* satisfy ossl_cmp_certReq_new() */
  51. ctx->oldCert = X509_new(); /* satisfy crm_new() and ossl_cmp_rr_new() */
  52. if (!OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)"",
  53. 0) /* prevent too unspecific error */
  54. || ctx->oldCert == NULL
  55. || name == NULL || !X509_set_issuer_name(ctx->oldCert, name)
  56. || serial == NULL || !X509_set_serialNumber(ctx->oldCert, serial))
  57. goto err;
  58. (void)OSSL_CMP_CTX_set_transfer_cb(ctx, transfer_cb);
  59. (void)OSSL_CMP_CTX_set_transfer_cb_arg(ctx, msg);
  60. (void)OSSL_CMP_CTX_set_log_cb(ctx, print_noop);
  61. num_responses = 0;
  62. switch (msg->body != NULL ? msg->body->type : -1) {
  63. case OSSL_CMP_PKIBODY_IP:
  64. (void)OSSL_CMP_exec_IR_ses(ctx);
  65. break;
  66. case OSSL_CMP_PKIBODY_CP:
  67. (void)OSSL_CMP_exec_CR_ses(ctx);
  68. (void)OSSL_CMP_exec_P10CR_ses(ctx);
  69. break;
  70. case OSSL_CMP_PKIBODY_KUP:
  71. (void)OSSL_CMP_exec_KUR_ses(ctx);
  72. break;
  73. case OSSL_CMP_PKIBODY_POLLREP:
  74. ctx->status = OSSL_CMP_PKISTATUS_waiting;
  75. (void)OSSL_CMP_try_certreq(ctx, OSSL_CMP_PKIBODY_CR, NULL, NULL);
  76. break;
  77. case OSSL_CMP_PKIBODY_RP:
  78. (void)OSSL_CMP_exec_RR_ses(ctx);
  79. break;
  80. case OSSL_CMP_PKIBODY_GENP:
  81. sk_OSSL_CMP_ITAV_pop_free(OSSL_CMP_exec_GENM_ses(ctx),
  82. OSSL_CMP_ITAV_free);
  83. break;
  84. default:
  85. (void)ossl_cmp_msg_check_update(ctx, msg, allow_unprotected, 0);
  86. break;
  87. }
  88. err:
  89. X509_NAME_free(name);
  90. ASN1_INTEGER_free(serial);
  91. }
  92. static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
  93. const OSSL_CMP_MSG *cert_req,
  94. int certReqId,
  95. const OSSL_CRMF_MSG *crm,
  96. const X509_REQ *p10cr,
  97. X509 **certOut,
  98. STACK_OF(X509) **chainOut,
  99. STACK_OF(X509) **caPubs)
  100. {
  101. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  102. return NULL;
  103. }
  104. static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
  105. const OSSL_CMP_MSG *rr,
  106. const X509_NAME *issuer,
  107. const ASN1_INTEGER *serial)
  108. {
  109. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  110. return NULL;
  111. }
  112. static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
  113. const OSSL_CMP_MSG *genm,
  114. const STACK_OF(OSSL_CMP_ITAV) *in,
  115. STACK_OF(OSSL_CMP_ITAV) **out)
  116. {
  117. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  118. return 0;
  119. }
  120. static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
  121. const OSSL_CMP_PKISI *statusInfo,
  122. const ASN1_INTEGER *errorCode,
  123. const OSSL_CMP_PKIFREETEXT *errorDetails)
  124. {
  125. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  126. }
  127. static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
  128. const OSSL_CMP_MSG *certConf, int certReqId,
  129. const ASN1_OCTET_STRING *certHash,
  130. const OSSL_CMP_PKISI *si)
  131. {
  132. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  133. return 0;
  134. }
  135. static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
  136. const OSSL_CMP_MSG *pollReq, int certReqId,
  137. OSSL_CMP_MSG **certReq, int64_t *check_after)
  138. {
  139. ERR_raise(ERR_LIB_CMP, CMP_R_ERROR_PROCESSING_MESSAGE);
  140. return 0;
  141. }
  142. int FuzzerTestOneInput(const uint8_t *buf, size_t len)
  143. {
  144. OSSL_CMP_MSG *msg;
  145. BIO *in;
  146. if (len == 0)
  147. return 0;
  148. in = BIO_new(BIO_s_mem());
  149. OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
  150. msg = d2i_OSSL_CMP_MSG_bio(in, NULL);
  151. if (msg != NULL) {
  152. BIO *out = BIO_new(BIO_s_null());
  153. OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new(NULL, NULL);
  154. OSSL_CMP_CTX *client_ctx = OSSL_CMP_CTX_new(NULL, NULL);
  155. i2d_OSSL_CMP_MSG_bio(out, msg);
  156. ASN1_item_print(out, (ASN1_VALUE *)msg, 4,
  157. ASN1_ITEM_rptr(OSSL_CMP_MSG), NULL);
  158. BIO_free(out);
  159. if (client_ctx != NULL)
  160. cmp_client_process_response(client_ctx, msg);
  161. if (srv_ctx != NULL
  162. && OSSL_CMP_CTX_set_log_cb(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx),
  163. print_noop)
  164. && OSSL_CMP_SRV_CTX_init(srv_ctx, NULL, process_cert_request,
  165. process_rr, process_genm, process_error,
  166. process_certConf, process_pollReq))
  167. OSSL_CMP_MSG_free(OSSL_CMP_SRV_process_request(srv_ctx, msg));
  168. OSSL_CMP_CTX_free(client_ctx);
  169. OSSL_CMP_SRV_CTX_free(srv_ctx);
  170. OSSL_CMP_MSG_free(msg);
  171. }
  172. BIO_free(in);
  173. ERR_clear_error();
  174. return 0;
  175. }
  176. void FuzzerCleanup(void)
  177. {
  178. }