Skip to content

Commit 6ad6eaf

Browse files
author
tb
committed
Add a simple test to ensure that pmeth->cleanup() can cope with NULL
pkey_ctx->data.
1 parent 2779ff1 commit 6ad6eaf

File tree

2 files changed

+93
-2
lines changed

2 files changed

+93
-2
lines changed
Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,23 @@
1-
# $OpenBSD: Makefile,v 1.6 2022/01/14 09:38:50 tb Exp $
1+
# $OpenBSD: Makefile,v 1.7 2022/03/30 08:57:26 tb Exp $
22

3-
PROGS= evptest evp_pkey_check
3+
PROGS= evptest evp_pkey_check evp_pkey_cleanup
44
LDADD= -lcrypto
55
DPADD= ${LIBCRYPTO}
66
WARNINGS= Yes
77
CFLAGS+= -DLIBRESSL_INTERNAL -DLIBRESSL_CRYPTO_INTERNAL -Werror
8+
CFLAGS+= -I${.CURDIR}/../../../../lib/libcrypto/evp
89

910
REGRESS_TARGETS+= regress-evptest
1011
REGRESS_TARGETS+= regress-evp_pkey_check
12+
REGRESS_TARGETS+= regress-evp_pkey_cleanup
1113

1214
regress-evptest: evptest
1315
./evptest ${.CURDIR}/evptests.txt
1416

1517
regress-evp_pkey_check: evp_pkey_check
1618
./evp_pkey_check
1719

20+
regress-evp_pkey_cleanup: evp_pkey_cleanup
21+
./evp_pkey_cleanup
22+
1823
.include <bsd.regress.mk>
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/* $OpenBSD: evp_pkey_cleanup.c,v 1.1 2022/03/30 08:57:26 tb Exp $ */
2+
3+
/*
4+
* Copyright (c) 2022 Theo Buehler <tb@openbsd.org>
5+
*
6+
* Permission to use, copy, modify, and distribute this software for any
7+
* purpose with or without fee is hereby granted, provided that the above
8+
* copyright notice and this permission notice appear in all copies.
9+
*
10+
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11+
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12+
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13+
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14+
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15+
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16+
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17+
*/
18+
19+
#include <stdio.h>
20+
21+
#include <openssl/evp.h>
22+
23+
#include "evp_locl.h"
24+
25+
struct pkey_cleanup_test {
26+
const char *name;
27+
int nid;
28+
void (*free)(void);
29+
};
30+
31+
int pkey_ids[] = {
32+
EVP_PKEY_CMAC,
33+
EVP_PKEY_DH,
34+
EVP_PKEY_DSA,
35+
EVP_PKEY_EC,
36+
EVP_PKEY_GOSTIMIT,
37+
EVP_PKEY_GOSTR01,
38+
EVP_PKEY_HMAC,
39+
EVP_PKEY_RSA,
40+
EVP_PKEY_RSA_PSS,
41+
};
42+
43+
static const size_t N_PKEY_IDS = sizeof(pkey_ids) / sizeof(pkey_ids[0]);
44+
45+
static int
46+
test_evp_pkey_ctx_cleanup(int nid)
47+
{
48+
EVP_PKEY_CTX *pkey_ctx = NULL;
49+
void *data;
50+
int failed = 1;
51+
52+
if ((pkey_ctx = EVP_PKEY_CTX_new_id(nid, NULL)) == NULL) {
53+
fprintf(stderr, "EVP_PKEY_CTX_new_id(%d, NULL) failed\n", nid);
54+
goto err;
55+
}
56+
57+
data = EVP_PKEY_CTX_get_data(pkey_ctx);
58+
59+
EVP_PKEY_CTX_set_data(pkey_ctx, NULL);
60+
if (pkey_ctx->pmeth->cleanup != NULL)
61+
pkey_ctx->pmeth->cleanup(pkey_ctx);
62+
63+
EVP_PKEY_CTX_set_data(pkey_ctx, data);
64+
65+
failed = 0;
66+
67+
err:
68+
EVP_PKEY_CTX_free(pkey_ctx);
69+
70+
return failed;
71+
}
72+
73+
int
74+
main(void)
75+
{
76+
size_t i;
77+
int failed = 0;
78+
79+
for (i = 0; i < N_PKEY_IDS; i++)
80+
failed |= test_evp_pkey_ctx_cleanup(pkey_ids[i]);
81+
82+
if (!failed)
83+
printf("SUCCESS\n");
84+
85+
return failed;
86+
}

0 commit comments

Comments
 (0)