1 /* $OpenBSD: evp_pkey_cleanup.c,v 1.5 2024/02/29 20:02:00 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_local.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_ED25519,
37 EVP_PKEY_HMAC,
38 EVP_PKEY_RSA,
39 EVP_PKEY_RSA_PSS,
40 EVP_PKEY_X25519,
41 };
42
43 static const size_t N_PKEY_IDS = sizeof(pkey_ids) / sizeof(pkey_ids[0]);
44
45 static int
test_evp_pkey_ctx_cleanup(int nid)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
main(void)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 return failed;
83 }
84