Add CMP fuzzing to fuzz/cmp.c, including a couple of helpers in crypto/cmp/

Reviewed-by: Kurt Roeckx <kurt@roeckx.be>
Reviewed-by: Matt Caswell <matt@openssl.org>
(Merged from https://github.com/openssl/openssl/pull/11386)
This commit is contained in:
Dr. David von Oheimb 2020-03-24 10:33:16 +01:00
parent a81151bd56
commit e599d0aecd
4571 changed files with 322 additions and 9 deletions

View File

@ -764,10 +764,17 @@ X509 *OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
goto end; goto end;
rrep = rp->body->value.rp; rrep = rp->body->value.rp;
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) { if (sk_OSSL_CMP_PKISI_num(rrep->status) != num_RevDetails) {
CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT); CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
goto end; goto end;
} }
#else
if (sk_OSSL_CMP_PKISI_num(rrep->status) < 1) {
CMPerr(0, CMP_R_WRONG_RP_COMPONENT_COUNT);
goto end;
}
#endif
/* evaluate PKIStatus field */ /* evaluate PKIStatus field */
si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid); si = ossl_cmp_revrepcontent_get_pkisi(rrep, rsid);
@ -822,15 +829,19 @@ X509 *OSSL_CMP_exec_RR_ses(OSSL_CMP_CTX *ctx)
goto err; goto err;
} }
if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) { if (X509_NAME_cmp(issuer, OSSL_CRMF_CERTID_get0_issuer(cid)) != 0) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_WRONG_CERTID_IN_RP); CMPerr(0, CMP_R_WRONG_CERTID_IN_RP);
result = NULL; result = NULL;
goto err; goto err;
#endif
} }
if (ASN1_INTEGER_cmp(serial, if (ASN1_INTEGER_cmp(serial,
OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) { OSSL_CRMF_CERTID_get0_serialNumber(cid)) != 0) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_WRONG_SERIAL_IN_RP); CMPerr(0, CMP_R_WRONG_SERIAL_IN_RP);
result = NULL; result = NULL;
goto err; goto err;
#endif
} }
} }

View File

@ -219,8 +219,13 @@ static OSSL_CRMF_MSG *crm_new(OSSL_CMP_CTX *ctx, int bodytype, int rid)
if (rkey == NULL) if (rkey == NULL)
rkey = ctx->pkey; /* default is independent of ctx->oldClCert */ rkey = ctx->pkey; /* default is independent of ctx->oldClCert */
if (rkey == NULL if (rkey == NULL) {
|| (bodytype == OSSL_CMP_PKIBODY_KUR && refcert == NULL)) { #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_NULL_ARGUMENT);
return NULL;
#endif
}
if (bodytype == OSSL_CMP_PKIBODY_KUR && refcert == NULL) {
CMPerr(0, CMP_R_INVALID_ARGS); CMPerr(0, CMP_R_INVALID_ARGS);
return NULL; return NULL;
} }

View File

@ -492,11 +492,13 @@ OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
default: default:
/* transactionID should be already initialized */ /* transactionID should be already initialized */
if (ctx->transactionID == NULL) { if (ctx->transactionID == NULL) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_UNEXPECTED_PKIBODY); CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
/* ignore any (extra) error in next two function calls: */ /* ignore any (extra) error in next two function calls: */
(void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID); (void)OSSL_CMP_CTX_set1_transactionID(ctx, hdr->transactionID);
(void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce); (void)ossl_cmp_ctx_set1_recipNonce(ctx, hdr->senderNonce);
goto err; goto err;
#endif
} }
} }
@ -547,6 +549,7 @@ OSSL_CMP_MSG *OSSL_CMP_SRV_process_request(OSSL_CMP_SRV_CTX *srv_ctx,
default: default:
/* TODO possibly support further request message types */ /* TODO possibly support further request message types */
CMPerr(0, CMP_R_UNEXPECTED_PKIBODY); CMPerr(0, CMP_R_UNEXPECTED_PKIBODY);
break;
} }
err: err:

View File

@ -700,26 +700,34 @@ int ossl_cmp_msg_check_received(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg,
/* detect explicitly permitted exceptions for invalid protection */ /* detect explicitly permitted exceptions for invalid protection */
if (!OSSL_CMP_validate_msg(ctx, msg) if (!OSSL_CMP_validate_msg(ctx, msg)
&& (cb == NULL || (*cb)(ctx, msg, 1, cb_arg) <= 0)) { && (cb == NULL || (*cb)(ctx, msg, 1, cb_arg) <= 0)) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_ERROR_VALIDATING_PROTECTION); CMPerr(0, CMP_R_ERROR_VALIDATING_PROTECTION);
return -1; return -1;
#endif
} }
} else { } else {
/* detect explicitly permitted exceptions for missing protection */ /* detect explicitly permitted exceptions for missing protection */
if (cb == NULL || (*cb)(ctx, msg, 0, cb_arg) <= 0) { if (cb == NULL || (*cb)(ctx, msg, 0, cb_arg) <= 0) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_MISSING_PROTECTION); CMPerr(0, CMP_R_MISSING_PROTECTION);
return -1; return -1;
#endif
} }
} }
/* check CMP version number in header */ /* check CMP version number in header */
if (ossl_cmp_hdr_get_pvno(OSSL_CMP_MSG_get0_header(msg)) != OSSL_CMP_PVNO) { if (ossl_cmp_hdr_get_pvno(OSSL_CMP_MSG_get0_header(msg)) != OSSL_CMP_PVNO) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_UNEXPECTED_PVNO); CMPerr(0, CMP_R_UNEXPECTED_PVNO);
return -1; return -1;
#endif
} }
if ((rcvd_type = ossl_cmp_msg_get_bodytype(msg)) < 0) { if ((rcvd_type = ossl_cmp_msg_get_bodytype(msg)) < 0) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_PKIBODY_ERROR); CMPerr(0, CMP_R_PKIBODY_ERROR);
return -1; return -1;
#endif
} }
/* compare received transactionID with the expected one in previous msg */ /* compare received transactionID with the expected one in previous msg */
@ -727,8 +735,10 @@ int ossl_cmp_msg_check_received(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg,
&& (msg->header->transactionID == NULL && (msg->header->transactionID == NULL
|| ASN1_OCTET_STRING_cmp(ctx->transactionID, || ASN1_OCTET_STRING_cmp(ctx->transactionID,
msg->header->transactionID) != 0)) { msg->header->transactionID) != 0)) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_TRANSACTIONID_UNMATCHED); CMPerr(0, CMP_R_TRANSACTIONID_UNMATCHED);
return -1; return -1;
#endif
} }
/* compare received nonce with the one we sent */ /* compare received nonce with the one we sent */
@ -736,8 +746,10 @@ int ossl_cmp_msg_check_received(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *msg,
&& (msg->header->recipNonce == NULL && (msg->header->recipNonce == NULL
|| ASN1_OCTET_STRING_cmp(ctx->senderNonce, || ASN1_OCTET_STRING_cmp(ctx->senderNonce,
msg->header->recipNonce) != 0)) { msg->header->recipNonce) != 0)) {
#ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_RECIPNONCE_UNMATCHED); CMPerr(0, CMP_R_RECIPNONCE_UNMATCHED);
return -1; return -1;
#endif
} }
/* /*
@ -776,19 +788,27 @@ int ossl_cmp_verify_popo(const OSSL_CMP_MSG *msg, int accept_RAVerified)
{ {
X509_REQ *req = msg->body->value.p10cr; X509_REQ *req = msg->body->value.p10cr;
if (X509_REQ_verify(req, X509_REQ_get0_pubkey(req)) > 0) if (X509_REQ_verify(req, X509_REQ_get0_pubkey(req)) <= 0) {
return 1; #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
CMPerr(0, CMP_R_REQUEST_NOT_ACCEPTED); CMPerr(0, CMP_R_REQUEST_NOT_ACCEPTED);
return 0; return 0;
#endif
}
} }
break;
case OSSL_CMP_PKIBODY_IR: case OSSL_CMP_PKIBODY_IR:
case OSSL_CMP_PKIBODY_CR: case OSSL_CMP_PKIBODY_CR:
case OSSL_CMP_PKIBODY_KUR: case OSSL_CMP_PKIBODY_KUR:
return OSSL_CRMF_MSGS_verify_popo(msg->body->value.ir, if (!OSSL_CRMF_MSGS_verify_popo(msg->body->value.ir, OSSL_CMP_CERTREQID,
OSSL_CMP_CERTREQID, accept_RAVerified)) {
accept_RAVerified); #ifndef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
return 0;
#endif
}
break;
default: default:
CMPerr(0, CMP_R_PKIBODY_ERROR); CMPerr(0, CMP_R_PKIBODY_ERROR);
return 0; return 0;
} }
return 1;
} }

View File

@ -11,6 +11,10 @@
IF[{- !$disabled{"fuzz-afl"} || !$disabled{"fuzz-libfuzzer"} -}] IF[{- !$disabled{"fuzz-afl"} || !$disabled{"fuzz-libfuzzer"} -}]
PROGRAMS{noinst}=asn1 asn1parse bignum bndiv client conf crl server x509 PROGRAMS{noinst}=asn1 asn1parse bignum bndiv client conf crl server x509
IF[{- !$disabled{"cmp"} -}]
PROGRAMS{noinst}=cmp
ENDIF
IF[{- !$disabled{"cms"} -}] IF[{- !$disabled{"cms"} -}]
PROGRAMS{noinst}=cms PROGRAMS{noinst}=cms
ENDIF ENDIF
@ -39,6 +43,10 @@ IF[{- !$disabled{"fuzz-afl"} || !$disabled{"fuzz-libfuzzer"} -}]
INCLUDE[client]=../include {- $ex_inc -} INCLUDE[client]=../include {- $ex_inc -}
DEPEND[client]=../libcrypto ../libssl {- $ex_lib -} DEPEND[client]=../libcrypto ../libssl {- $ex_lib -}
SOURCE[cmp]=cmp.c driver.c
INCLUDE[cmp]=../include {- $ex_inc -}
DEPEND[cmp]=../libcrypto {- $ex_lib -}
SOURCE[cms]=cms.c driver.c SOURCE[cms]=cms.c driver.c
INCLUDE[cms]=../include {- $ex_inc -} INCLUDE[cms]=../include {- $ex_inc -}
DEPEND[cms]=../libcrypto {- $ex_lib -} DEPEND[cms]=../libcrypto {- $ex_lib -}
@ -67,6 +75,10 @@ ENDIF
IF[{- !$disabled{tests} -}] IF[{- !$disabled{tests} -}]
PROGRAMS{noinst}=asn1-test asn1parse-test bignum-test bndiv-test client-test conf-test crl-test server-test x509-test PROGRAMS{noinst}=asn1-test asn1parse-test bignum-test bndiv-test client-test conf-test crl-test server-test x509-test
IF[{- !$disabled{"cmp"} -}]
PROGRAMS{noinst}=cmp-test
ENDIF
IF[{- !$disabled{"cms"} -}] IF[{- !$disabled{"cms"} -}]
PROGRAMS{noinst}=cms-test PROGRAMS{noinst}=cms-test
ENDIF ENDIF
@ -95,6 +107,11 @@ IF[{- !$disabled{tests} -}]
INCLUDE[client-test]=../include INCLUDE[client-test]=../include
DEPEND[client-test]=../libcrypto ../libssl DEPEND[client-test]=../libcrypto ../libssl
SOURCE[cmp-test]=cmp.c test-corpus.c
INCLUDE[cmp-test]=../include
DEPEND[cmp-test]=../libcrypto.a
# referring to static lib allows using non-exported functions
SOURCE[cms-test]=cms.c test-corpus.c SOURCE[cms-test]=cms.c test-corpus.c
INCLUDE[cms-test]=../include INCLUDE[cms-test]=../include
DEPEND[cms-test]=../libcrypto DEPEND[cms-test]=../libcrypto

203
fuzz/cmp.c Normal file
View File

@ -0,0 +1,203 @@
/*
* Copyright 2007-2020 The OpenSSL Project Authors. All Rights Reserved.
*
* Licensed under the Apache License 2.0 (the "License"). You may not use
* this file except in compliance with the License. You can obtain a copy
* in the file LICENSE in the source distribution or at
* https://www.openssl.org/source/license.html
*/
/*
* Test CMP DER parsing.
*/
#include <openssl/bio.h>
#include <openssl/cmp.h>
#include "../crypto/cmp/cmp_local.h"
#include <openssl/err.h>
#include "fuzzer.h"
#include "rand.inc"
int FuzzerInitialize(int *argc, char ***argv)
{
OPENSSL_init_crypto(OPENSSL_INIT_LOAD_CRYPTO_STRINGS, NULL);
ERR_clear_error();
CRYPTO_free_ex_index(0, -1);
FuzzerSetRand();
return 1;
}
static int num_responses;
static OSSL_CMP_MSG *transfer_cb(OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *req)
{
if (num_responses++ > 2)
return NULL; /* prevent loops due to repeated pollRep */
return OSSL_CMP_MSG_dup((OSSL_CMP_MSG *)
OSSL_CMP_CTX_get_transfer_cb_arg(ctx));
}
static int print_noop(const char *func, const char *file, int line,
OSSL_CMP_severity level, const char *msg)
{
return 1;
}
static int allow_unprotected(const OSSL_CMP_CTX *ctx, const OSSL_CMP_MSG *rep,
int invalid_protection, int expected_type)
{
return 1;
}
static void cmp_client_process_response(OSSL_CMP_CTX *ctx, OSSL_CMP_MSG *msg)
{
X509_NAME *name = X509_NAME_new();
ASN1_INTEGER *serial = ASN1_INTEGER_new();
ctx->unprotectedSend = 1; /* satisfy ossl_cmp_msg_protect() */
ctx->disableConfirm = 1; /* check just one response message */
ctx->popoMethod = OSSL_CRMF_POPO_NONE; /* satisfy ossl_cmp_certReq_new() */
ctx->oldCert = X509_new(); /* satisfy crm_new() and ossl_cmp_rr_new() */
if (!OSSL_CMP_CTX_set1_secretValue(ctx, (unsigned char *)"",
0) /* prevent too unspecific error */
|| ctx->oldCert == NULL
|| name == NULL || !X509_set_issuer_name(ctx->oldCert, name)
|| serial == NULL || !X509_set_serialNumber(ctx->oldCert, serial))
goto err;
(void)OSSL_CMP_CTX_set_transfer_cb(ctx, transfer_cb);
(void)OSSL_CMP_CTX_set_transfer_cb_arg(ctx, msg);
(void)OSSL_CMP_CTX_set_log_cb(ctx, print_noop);
num_responses = 0;
switch (msg->body != NULL ? msg->body->type : -1) {
case OSSL_CMP_PKIBODY_IP:
(void)OSSL_CMP_exec_IR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_CP:
(void)OSSL_CMP_exec_CR_ses(ctx);
(void)OSSL_CMP_exec_P10CR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_KUP:
(void)OSSL_CMP_exec_KUR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_POLLREP:
ctx->status = OSSL_CMP_PKISTATUS_waiting;
(void)OSSL_CMP_try_certreq(ctx, OSSL_CMP_PKIBODY_CR, NULL);
break;
case OSSL_CMP_PKIBODY_RP:
(void)OSSL_CMP_exec_RR_ses(ctx);
break;
case OSSL_CMP_PKIBODY_GENP:
sk_OSSL_CMP_ITAV_pop_free(OSSL_CMP_exec_GENM_ses(ctx),
OSSL_CMP_ITAV_free);
break;
default:
(void)ossl_cmp_msg_check_received(ctx, msg, allow_unprotected, 0);
break;
}
err:
X509_NAME_free(name);
ASN1_INTEGER_free(serial);
}
static OSSL_CMP_PKISI *process_cert_request(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *cert_req,
int certReqId,
const OSSL_CRMF_MSG *crm,
const X509_REQ *p10cr,
X509 **certOut,
STACK_OF(X509) **chainOut,
STACK_OF(X509) **caPubs)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
return NULL;
}
static OSSL_CMP_PKISI *process_rr(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *rr,
const X509_NAME *issuer,
const ASN1_INTEGER *serial)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
return NULL;
}
static int process_genm(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *genm,
const STACK_OF(OSSL_CMP_ITAV) *in,
STACK_OF(OSSL_CMP_ITAV) **out)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
static void process_error(OSSL_CMP_SRV_CTX *srv_ctx, const OSSL_CMP_MSG *error,
const OSSL_CMP_PKISI *statusInfo,
const ASN1_INTEGER *errorCode,
const OSSL_CMP_PKIFREETEXT *errorDetails)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
}
static int process_certConf(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *certConf, int certReqId,
const ASN1_OCTET_STRING *certHash,
const OSSL_CMP_PKISI *si)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
static int process_pollReq(OSSL_CMP_SRV_CTX *srv_ctx,
const OSSL_CMP_MSG *pollReq, int certReqId,
OSSL_CMP_MSG **certReq, int64_t *check_after)
{
CMPerr(0, CMP_R_ERROR_PROCESSING_MESSAGE);
return 0;
}
int FuzzerTestOneInput(const uint8_t *buf, size_t len)
{
OSSL_CMP_MSG *msg;
BIO *in;
if (len == 0)
return 0;
in = BIO_new(BIO_s_mem());
OPENSSL_assert((size_t)BIO_write(in, buf, len) == len);
msg = d2i_OSSL_CMP_MSG_bio(in, NULL);
if (msg != NULL) {
BIO *out = BIO_new(BIO_s_null());
OSSL_CMP_SRV_CTX *srv_ctx = OSSL_CMP_SRV_CTX_new();
OSSL_CMP_CTX *client_ctx = OSSL_CMP_CTX_new();
i2d_OSSL_CMP_MSG_bio(out, msg);
ASN1_item_print(out, (ASN1_VALUE *)msg, 4,
ASN1_ITEM_rptr(OSSL_CMP_MSG), NULL);
BIO_free(out);
if (client_ctx != NULL)
cmp_client_process_response(client_ctx, msg);
if (srv_ctx != NULL
&& OSSL_CMP_CTX_set_log_cb(OSSL_CMP_SRV_CTX_get0_cmp_ctx(srv_ctx),
print_noop)
&& OSSL_CMP_SRV_CTX_init(srv_ctx, NULL, process_cert_request,
process_rr, process_genm, process_error,
process_certConf, process_pollReq))
OSSL_CMP_MSG_free(OSSL_CMP_SRV_process_request(srv_ctx, msg));
OSSL_CMP_CTX_free(client_ctx);
OSSL_CMP_SRV_CTX_free(srv_ctx);
OSSL_CMP_MSG_free(msg);
}
BIO_free(in);
ERR_clear_error();
return 0;
}
void FuzzerCleanup(void)
{
}

Some files were not shown because too many files have changed in this diff Show More