2 * Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
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
11 * Test CMP DER parsing.
14 #include <openssl/bio.h>
15 #include <openssl/cmp.h>
16 #include "../crypto/cmp/cmp_local.h"
17 #include <openssl/err.h>
21 int FuzzerInitialize(int *argc, char ***argv)
23 OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
25 CRYPTO_free_ex_index(0, -1);
30 static int num_responses;
32 static OSSL_CMP_MSG *transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req)
34 if (num_responses++ > 2)
35 return NULL; /* prevent loops due to repeated pollRep */
36 return OSSL_CMP_MSG_dup((OSSL_CMP_MSG *)
37 OSSL_CMP_CTX_get_transfer_cb_arg(ctx));
40 static int print_noop(const char *func, const char *file, int line,
41 OSSL_CMP_severity level, const char *msg)
46 static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *rep,
47 int invalid_protection, int expected_type)
52 static void cmp_client_process_response(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
54 X509_NAME *name = X509_NAME_new();
55 ASN1_INTEGER *serial = ASN1_INTEGER_new();
57 ctx->unprotectedSend = 1; /* satisfy ossl_cmp_msg_protect() */
58 ctx->disableConfirm = 1; /* check just one response message */
59 ctx->popoMethod = OSSL_CRMF_POPO_NONE; /* satisfy ossl_cmp_certReq_new() */
60 ctx->oldCert = X509_new(); /* satisfy crm_new() and ossl_cmp_rr_new() */
61 if (!OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)"",
62 0) /* prevent too unspecific error */
63 || ctx->oldCert == NULL
64 || name == NULL || !X509_set_issuer_name(ctx->oldCert, name)
65 || serial == NULL || !X509_set_serialNumber(ctx->oldCert, serial))
68 (void)OSSL_CMP_CTX_set_transfer_cb(ctx, transfer_cb);
69 (void)OSSL_CMP_CTX_set_transfer_cb_arg(ctx, msg);
70 (void)OSSL_CMP_CTX_set_log_cb(ctx, print_noop);
72 switch (msg->body != NULL ? msg->body->type : -1) {
73 case OSSL_CMP_PKIBODY_IP:
74 (void)OSSL_CMP_exec_IR_ses(ctx);
76 case OSSL_CMP_PKIBODY_CP:
77 (void)OSSL_CMP_exec_CR_ses(ctx);
78 (void)OSSL_CMP_exec_P10CR_ses(ctx);
80 case OSSL_CMP_PKIBODY_KUP:
81 (void)OSSL_CMP_exec_KUR_ses(ctx);
83 case OSSL_CMP_PKIBODY_POLLREP:
84 ctx->status = OSSL_CMP_PKISTATUS_waiting;
85 (void)OSSL_CMP_try_certreq(ctx, OSSL_CMP_PKIBODY_CR, NULL);
87 case OSSL_CMP_PKIBODY_RP:
88 (void)OSSL_CMP_exec_RR_ses(ctx);
90 case OSSL_CMP_PKIBODY_GENP:
91 sk_OSSL_CMP_ITAV_pop_free(OSSL_CMP_exec_GENM_ses(ctx),
95 (void)ossl_cmp_msg_check_received(ctx, msg, allow_unprotected, 0);
100 ASN1_INTEGER_free(serial);
103 static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
104 const OSSL_CMP_MSG *cert_req,
106 const OSSL_CRMF_MSG *crm,
107 const X509_REQ *p10cr,
109 STACK_OF(X509) **chainOut,
110 STACK_OF(X509) **caPubs)
112 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
116 static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
117 const OSSL_CMP_MSG *rr,
118 const X509_NAME *issuer,
119 const ASN1_INTEGER *serial)
121 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
125 static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
126 const OSSL_CMP_MSG *genm,
127 const STACK_OF(OSSL_CMP_ITAV) *in,
128 STACK_OF(OSSL_CMP_ITAV) **out)
130 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
134 static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
135 const OSSL_CMP_PKISI *statusInfo,
136 const ASN1_INTEGER *errorCode,
137 const OSSL_CMP_PKIFREETEXT *errorDetails)
139 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
142 static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
143 const OSSL_CMP_MSG *certConf, int certReqId,
144 const ASN1_OCTET_STRING *certHash,
145 const OSSL_CMP_PKISI *si)
147 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
151 static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
152 const OSSL_CMP_MSG *pollReq, int certReqId,
153 OSSL_CMP_MSG **certReq, int64_t *check_after)
155 CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
159 int FuzzerTestOneInput(const uint8_t *buf, size_t len)
167 in = BIO_new(BIO_s_mem());
168 OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
169 msg = d2i_OSSL_CMP_MSG_bio(in, NULL);
171 BIO *out = BIO_new(BIO_s_null());
172 OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new();
173 OSSL_CMP_CTX *client_ctx = OSSL_CMP_CTX_new();
175 i2d_OSSL_CMP_MSG_bio(out, msg);
176 ASN1_item_print(out, (ASN1_VALUE *)msg, 4,
177 ASN1_ITEM_rptr(OSSL_CMP_MSG), NULL);
180 if (client_ctx != NULL)
181 cmp_client_process_response(client_ctx, msg);
183 && OSSL_CMP_CTX_set_log_cb(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx),
185 && OSSL_CMP_SRV_CTX_init(srv_ctx, NULL, process_cert_request,
186 process_rr, process_genm, process_error,
187 process_certConf, process_pollReq))
188 OSSL_CMP_MSG_free(OSSL_CMP_SRV_process_request(srv_ctx, msg));
190 OSSL_CMP_CTX_free(client_ctx);
191 OSSL_CMP_SRV_CTX_free(srv_ctx);
192 OSSL_CMP_MSG_free(msg);
201 void FuzzerCleanup(void)