Skip to content

Commit

Permalink
Add a simple test to ensure that pmeth->cleanup() can cope with NULL
Browse files Browse the repository at this point in the history
pkey_ctx->data.
  • Loading branch information
tb committed Mar 30, 2022
1 parent 2779ff1 commit 6ad6eaf
Show file tree
Hide file tree
Showing 2 changed files with 93 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/regress/lib/libcrypto/evp/Makefile
Original file line number Diff line number Diff line change
@@ -1,18 +1,23 @@
# $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

regress-evp_pkey_check: evp_pkey_check
./evp_pkey_check

regress-evp_pkey_cleanup: evp_pkey_cleanup
./evp_pkey_cleanup

.include <bsd.regress.mk>
86 changes: 86 additions & 0 deletions src/regress/lib/libcrypto/evp/evp_pkey_cleanup.c
Original file line number Diff line number Diff line change
@@ -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 <[email protected]>
*
* 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 <stdio.h>

#include <openssl/evp.h>

#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;
}

0 comments on commit 6ad6eaf

Please sign in to comment.