From 6ad6eafddd85b962abbeb9a404641ff8b3ea595e Mon Sep 17 00:00:00 2001 From: tb <> Date: Wed, 30 Mar 2022 08:57:26 +0000 Subject: [PATCH] Add a simple test to ensure that pmeth->cleanup() can cope with NULL pkey_ctx->data. --- src/regress/lib/libcrypto/evp/Makefile | 9 +- .../lib/libcrypto/evp/evp_pkey_cleanup.c | 86 +++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) create mode 100644 src/regress/lib/libcrypto/evp/evp_pkey_cleanup.c diff --git a/src/regress/lib/libcrypto/evp/Makefile b/src/regress/lib/libcrypto/evp/Makefile index 5abec99a983..f883584a17c 100644 --- a/src/regress/lib/libcrypto/evp/Makefile +++ b/src/regress/lib/libcrypto/evp/Makefile @@ -1,13 +1,15 @@ -# $OpenBSD: Makefile,v 1.6 2022/01/14 09:38:50 tb Exp $ +# $OpenBSD: Makefile,v 1.7 2022/03/30 08:57:26 tb Exp $ -PROGS= evptest evp_pkey_check +PROGS= evptest evp_pkey_check evp_pkey_cleanup LDADD= -lcrypto DPADD= ${LIBCRYPTO} WARNINGS= Yes CFLAGS+= -DLIBRESSL_INTERNAL -DLIBRESSL_CRYPTO_INTERNAL -Werror +CFLAGS+= -I${.CURDIR}/../../../../lib/libcrypto/evp REGRESS_TARGETS+= regress-evptest REGRESS_TARGETS+= regress-evp_pkey_check +REGRESS_TARGETS+= regress-evp_pkey_cleanup regress-evptest: evptest ./evptest ${.CURDIR}/evptests.txt @@ -15,4 +17,7 @@ regress-evptest: evptest regress-evp_pkey_check: evp_pkey_check ./evp_pkey_check +regress-evp_pkey_cleanup: evp_pkey_cleanup + ./evp_pkey_cleanup + .include diff --git a/src/regress/lib/libcrypto/evp/evp_pkey_cleanup.c b/src/regress/lib/libcrypto/evp/evp_pkey_cleanup.c new file mode 100644 index 00000000000..5f8cc20d749 --- /dev/null +++ b/src/regress/lib/libcrypto/evp/evp_pkey_cleanup.c @@ -0,0 +1,86 @@ +/* $OpenBSD: evp_pkey_cleanup.c,v 1.1 2022/03/30 08:57:26 tb Exp $ */ + +/* + * Copyright (c) 2022 Theo Buehler + * + * Permission to use, copy, modify, and distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#include + +#include + +#include "evp_locl.h" + +struct pkey_cleanup_test { + const char *name; + int nid; + void (*free)(void); +}; + +int pkey_ids[] = { + EVP_PKEY_CMAC, + EVP_PKEY_DH, + EVP_PKEY_DSA, + EVP_PKEY_EC, + EVP_PKEY_GOSTIMIT, + EVP_PKEY_GOSTR01, + EVP_PKEY_HMAC, + EVP_PKEY_RSA, + EVP_PKEY_RSA_PSS, +}; + +static const size_t N_PKEY_IDS = sizeof(pkey_ids) / sizeof(pkey_ids[0]); + +static int +test_evp_pkey_ctx_cleanup(int nid) +{ + EVP_PKEY_CTX *pkey_ctx = NULL; + void *data; + int failed = 1; + + if ((pkey_ctx = EVP_PKEY_CTX_new_id(nid, NULL)) == NULL) { + fprintf(stderr, "EVP_PKEY_CTX_new_id(%d, NULL) failed\n", nid); + goto err; + } + + data = EVP_PKEY_CTX_get_data(pkey_ctx); + + EVP_PKEY_CTX_set_data(pkey_ctx, NULL); + if (pkey_ctx->pmeth->cleanup != NULL) + pkey_ctx->pmeth->cleanup(pkey_ctx); + + EVP_PKEY_CTX_set_data(pkey_ctx, data); + + failed = 0; + + err: + EVP_PKEY_CTX_free(pkey_ctx); + + return failed; +} + +int +main(void) +{ + size_t i; + int failed = 0; + + for (i = 0; i < N_PKEY_IDS; i++) + failed |= test_evp_pkey_ctx_cleanup(pkey_ids[i]); + + if (!failed) + printf("SUCCESS\n"); + + return failed; +}