xref: /openbsd-src/regress/lib/libcrypto/evp/evp_test.c (revision 3374c67d44f9b75b98444cbf63020f777792342e)
1 /*	$OpenBSD: evp_test.c,v 1.3 2022/11/26 16:08:56 tb Exp $ */
2 /*
3  * Copyright (c) 2022 Joel Sing <jsing@openbsd.org>
4  *
5  * Permission to use, copy, modify, and distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
18 #include <openssl/evp.h>
19 #include <openssl/ossl_typ.h>
20 
21 #include "evp_local.h"
22 
23 static int
24 evp_asn1_method_test(void)
25 {
26 	const EVP_PKEY_ASN1_METHOD *method;
27 	int count, pkey_id, i;
28 	int failed = 1;
29 
30 	if ((count = EVP_PKEY_asn1_get_count()) < 1) {
31 		fprintf(stderr, "FAIL: failed to get pkey asn1 method count\n");
32 		goto failure;
33 	}
34 	for (i = 0; i < count; i++) {
35 		if ((method = EVP_PKEY_asn1_get0(i)) == NULL) {
36 			fprintf(stderr, "FAIL: failed to get pkey %d\n", i);
37 			goto failure;
38 		}
39 	}
40 
41 	if ((method = EVP_PKEY_asn1_find(NULL, EVP_PKEY_RSA)) == NULL) {
42 		fprintf(stderr, "FAIL: failed to find RSA method\n");
43 		goto failure;
44 	}
45 	if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, method)) {
46 		fprintf(stderr, "FAIL: failed to get RSA method info\n");
47 		goto failure;
48 	}
49 	if (pkey_id != EVP_PKEY_RSA) {
50 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
51 		    pkey_id, EVP_PKEY_RSA);
52 		goto failure;
53 	}
54 
55 	if ((method = EVP_PKEY_asn1_find(NULL, EVP_PKEY_RSA_PSS)) == NULL) {
56 		fprintf(stderr, "FAIL: failed to find RSA-PSS method\n");
57 		goto failure;
58 	}
59 	if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, method)) {
60 		fprintf(stderr, "FAIL: failed to get RSA-PSS method info\n");
61 		goto failure;
62 	}
63 	if (pkey_id != EVP_PKEY_RSA_PSS) {
64 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
65 		    pkey_id, EVP_PKEY_RSA_PSS);
66 		goto failure;
67 	}
68 
69 	if ((method = EVP_PKEY_asn1_find_str(NULL, "RSA", -1)) == NULL) {
70 		fprintf(stderr, "FAIL: failed to find RSA method by str\n");
71 		goto failure;
72 	}
73 	if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, method)) {
74 		fprintf(stderr, "FAIL: failed to get RSA method info\n");
75 		goto failure;
76 	}
77 	if (pkey_id != EVP_PKEY_RSA) {
78 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
79 		    pkey_id, EVP_PKEY_RSA);
80 		goto failure;
81 	}
82 
83 	if ((method = EVP_PKEY_asn1_find_str(NULL, "RSA-PSS", -1)) == NULL) {
84 		fprintf(stderr, "FAIL: failed to find RSA-PSS method\n");
85 		goto failure;
86 	}
87 	if (!EVP_PKEY_asn1_get0_info(&pkey_id, NULL, NULL, NULL, NULL, method)) {
88 		fprintf(stderr, "FAIL: failed to get RSA-PSS method info\n");
89 		goto failure;
90 	}
91 	if (pkey_id != EVP_PKEY_RSA_PSS) {
92 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
93 		    pkey_id, EVP_PKEY_RSA_PSS);
94 		goto failure;
95 	}
96 
97 	failed = 0;
98 
99  failure:
100 
101 	return failed;
102 }
103 
104 static int
105 evp_pkey_method_test(void)
106 {
107 	const EVP_PKEY_METHOD *method;
108 	int pkey_id;
109 	int failed = 1;
110 
111 	if ((method = EVP_PKEY_meth_find(EVP_PKEY_RSA)) == NULL) {
112 		fprintf(stderr, "FAIL: failed to find RSA method\n");
113 		goto failure;
114 	}
115 	EVP_PKEY_meth_get0_info(&pkey_id, NULL, method);
116 	if (pkey_id != EVP_PKEY_RSA) {
117 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
118 		    pkey_id, EVP_PKEY_RSA);
119 		goto failure;
120 	}
121 
122 	if ((method = EVP_PKEY_meth_find(EVP_PKEY_RSA_PSS)) == NULL) {
123 		fprintf(stderr, "FAIL: failed to find RSA-PSS method\n");
124 		goto failure;
125 	}
126 	EVP_PKEY_meth_get0_info(&pkey_id, NULL, method);
127 	if (pkey_id != EVP_PKEY_RSA_PSS) {
128 		fprintf(stderr, "FAIL: method ID mismatch (%d != %d)\n",
129 		    pkey_id, EVP_PKEY_RSA_PSS);
130 		goto failure;
131 	}
132 
133 	failed = 0;
134 
135  failure:
136 
137 	return failed;
138 }
139 
140 int
141 main(int argc, char **argv)
142 {
143 	int failed = 0;
144 
145 	failed |= evp_asn1_method_test();
146 	failed |= evp_pkey_method_test();
147 
148 	return failed;
149 }
150