VirtualBox

source: vbox/trunk/src/libs/openssl-3.1.0/test/evp_extra_test2.c@ 100908

最後變更 在這個檔案從100908是 99366,由 vboxsync 提交於 22 月 前

openssl-3.1.0: Applied and adjusted our OpenSSL changes to 3.0.7. bugref:10418

檔案大小: 52.5 KB
 
1/*
2 * Copyright 2015-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10/* We need to use some deprecated APIs */
11#define OPENSSL_SUPPRESS_DEPRECATED
12
13/*
14 * Really these tests should be in evp_extra_test - but that doesn't
15 * yet support testing with a non-default libctx. Once it does we should move
16 * everything into one file. Consequently some things are duplicated between
17 * the two files.
18 */
19
20#include <openssl/evp.h>
21#include <openssl/pem.h>
22#include <openssl/provider.h>
23#include <openssl/rsa.h>
24#include <openssl/dh.h>
25#include <openssl/core_names.h>
26
27#include "testutil.h"
28#include "internal/nelem.h"
29
30static OSSL_LIB_CTX *mainctx = NULL;
31static OSSL_PROVIDER *nullprov = NULL;
32
33/*
34 * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
35 * should never use this key anywhere but in an example.
36 */
37static const unsigned char kExampleRSAKeyDER[] = {
38 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
39 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
40 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
41 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
42 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 0x97, 0x8a,
43 0xdd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, 0xf1,
88 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
89};
90
91/*
92 * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
93 * PrivateKeyInfo.
94 */
95static const unsigned char kExampleRSAKeyPKCS8[] = {
96 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
97 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
98 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
99 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
100 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
101 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
102 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
103 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
104 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
105 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
106 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
107 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
108 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
109 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
110 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
111 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
112 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
113 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
114 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
115 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
116 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
117 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
118 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
119 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
120 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
121 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
122 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
123 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
124 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
125 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
126 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
127 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
128 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
129 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
130 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
131 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
132 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
133 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
134 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
135 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
136 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
137 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
138 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
139 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
140 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
141 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
142 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
143 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
144 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
145 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
146 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
147 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
148 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
149};
150
151#ifndef OPENSSL_NO_DH
152static const unsigned char kExampleDHPrivateKeyDER[] = {
153 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
154 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
155 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
156 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 0xA3, 0x20,
157 0xD0, 0x86,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0x3D, 0x30, 0x39, 0x69, 0xDB,
187 0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
188 0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
189 0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
190 0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
191 0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
192 0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
193 0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
194 0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
195 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
196 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
197 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
198 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
199 0x8D, 0x03
200};
201#endif /* OPENSSL_NO_DH */
202
203#ifndef OPENSSL_NO_EC
204/*
205 * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
206 * structure.
207 */
208static const unsigned char kExampleECKeyDER[] = {
209 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
210 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
211 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
212 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
213 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
214 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
215 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
216 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
217 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
218 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
219 0xc1,
220};
221
222/* P-384 sample EC private key in PKCS8 format (no public key) */
223static const unsigned char kExampleECKey2DER[] = {
224 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
225 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
226 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
227 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
228 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
229 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
230 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
231};
232
233static const unsigned char kExampleECXKey2DER[] = {
234 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
235 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
236 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
237 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
238};
239#endif
240
241typedef struct APK_DATA_st {
242 const unsigned char *kder;
243 size_t size;
244 int evptype;
245} APK_DATA;
246
247static APK_DATA keydata[] = {
248 {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
249 {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
250#ifndef OPENSSL_NO_EC
251 {kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519},
252 {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC},
253 {kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC},
254#endif
255#ifndef OPENSSL_NO_DH
256 {kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH},
257#endif
258};
259
260static int pkey_has_private(EVP_PKEY *key, const char *privtag,
261 int use_octstring)
262{
263 int ret = 0;
264
265 if (use_octstring) {
266 unsigned char buf[64];
267
268 ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf),
269 NULL);
270 } else {
271 BIGNUM *bn = NULL;
272
273 ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
274 BN_free(bn);
275 }
276 return ret;
277}
278
279static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
280{
281 int ret = 0;
282 OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
283 EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
284 EVP_PKEY_CTX *fromctx = NULL;
285 const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv";
286 const int use_octstring = strcmp(keytype, "X25519") == 0;
287
288 /*
289 * Select only the public key component when using EVP_PKEY_todata() and
290 * check that the resulting param array does not contain a private key.
291 */
292 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
293 || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
294 goto end;
295 /*
296 * Select the keypair when using EVP_PKEY_todata() and check that
297 * the param array contains a private key.
298 */
299 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)
300 || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag)))
301 goto end;
302
303 /*
304 * Select only the public key when using EVP_PKEY_fromdata() and check that
305 * the resulting key does not contain a private key.
306 */
307 if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
308 || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
309 || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
310 keypair_params), 1)
311 || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
312 goto end;
313 /*
314 * Select the keypair when using EVP_PKEY_fromdata() and check that
315 * the resulting key contains a private key.
316 */
317 if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
318 EVP_PKEY_KEYPAIR, keypair_params), 1)
319 || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
320 goto end;
321 ret = 1;
322end:
323 EVP_PKEY_free(fromkeypair);
324 EVP_PKEY_free(fromkey);
325 EVP_PKEY_CTX_free(fromctx);
326 OSSL_PARAM_free(keypair_params);
327 OSSL_PARAM_free(pub_params);
328 return ret;
329}
330
331#ifndef OPENSSL_NO_DH
332static int test_dh_tofrom_data_select(void)
333{
334 int ret;
335 OSSL_PARAM params[2];
336 EVP_PKEY *key = NULL;
337 EVP_PKEY_CTX *gctx = NULL;
338# ifndef OPENSSL_NO_DEPRECATED_3_0
339 const DH *dhkey;
340 const BIGNUM *privkey;
341# endif
342
343 params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
344 params[1] = OSSL_PARAM_construct_end();
345 ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL))
346 && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
347 && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
348 && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0)
349 && TEST_true(do_pkey_tofrom_data_select(key, "DHX"));
350# ifndef OPENSSL_NO_DEPRECATED_3_0
351 ret = ret && TEST_ptr(dhkey = EVP_PKEY_get0_DH(key))
352 && TEST_ptr(privkey = DH_get0_priv_key(dhkey))
353 && TEST_int_le(BN_num_bits(privkey), 225);
354# endif
355 EVP_PKEY_free(key);
356 EVP_PKEY_CTX_free(gctx);
357 return ret;
358}
359
360static int test_dh_paramgen(void)
361{
362 int ret;
363 OSSL_PARAM params[3];
364 EVP_PKEY *pkey = NULL;
365 EVP_PKEY_CTX *gctx = NULL;
366 unsigned int pbits = 512; /* minimum allowed for speed */
367
368 params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
369 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
370 "generator", 0);
371 params[2] = OSSL_PARAM_construct_end();
372
373 ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
374 && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0)
375 && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
376 && TEST_true(EVP_PKEY_paramgen(gctx, &pkey))
377 && TEST_ptr(pkey);
378
379 EVP_PKEY_CTX_free(gctx);
380 gctx = NULL;
381
382 ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
383 && TEST_int_eq(EVP_PKEY_param_check(gctx), 1)
384 && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1);
385
386 EVP_PKEY_CTX_free(gctx);
387 EVP_PKEY_free(pkey);
388 return ret;
389}
390
391#endif
392
393#ifndef OPENSSL_NO_EC
394static int test_ec_tofrom_data_select(void)
395{
396 int ret;
397 EVP_PKEY *key = NULL;
398
399 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
400 && TEST_true(do_pkey_tofrom_data_select(key, "EC"));
401 EVP_PKEY_free(key);
402 return ret;
403}
404
405static int test_ecx_tofrom_data_select(void)
406{
407 int ret;
408 EVP_PKEY *key = NULL;
409
410 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519"))
411 && TEST_true(do_pkey_tofrom_data_select(key, "X25519"));
412 EVP_PKEY_free(key);
413 return ret;
414}
415#endif
416
417#ifndef OPENSSL_NO_SM2
418static int test_sm2_tofrom_data_select(void)
419{
420 int ret;
421 EVP_PKEY *key = NULL;
422
423 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "SM2"))
424 && TEST_true(do_pkey_tofrom_data_select(key, "SM2"));
425 EVP_PKEY_free(key);
426 return ret;
427}
428#endif
429
430static int test_rsa_tofrom_data_select(void)
431{
432 int ret;
433 EVP_PKEY *key = NULL;
434 const unsigned char *pdata = kExampleRSAKeyDER;
435 int pdata_len = sizeof(kExampleRSAKeyDER);
436
437 ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
438 mainctx, NULL))
439 && TEST_true(do_pkey_tofrom_data_select(key, "RSA"));
440 EVP_PKEY_free(key);
441 return ret;
442}
443
444/* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
445static int test_d2i_AutoPrivateKey_ex(int i)
446{
447 int ret = 0;
448 const unsigned char *p;
449 EVP_PKEY *pkey = NULL;
450 const APK_DATA *ak = &keydata[i];
451 const unsigned char *input = ak->kder;
452 size_t input_len = ak->size;
453 int expected_id = ak->evptype;
454 BIGNUM *p_bn = NULL;
455 BIGNUM *g_bn = NULL;
456 BIGNUM *priv_bn = NULL;
457
458 p = input;
459 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, input_len, mainctx,
460 NULL))
461 || !TEST_ptr_eq(p, input + input_len)
462 || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
463 goto done;
464
465 if (ak->evptype == EVP_PKEY_RSA) {
466 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
467 &priv_bn)))
468 goto done;
469 } else if (ak->evptype == EVP_PKEY_X25519) {
470 unsigned char buffer[32];
471 size_t len;
472
473 if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
474 OSSL_PKEY_PARAM_PRIV_KEY,
475 buffer, sizeof(buffer),
476 &len)))
477 goto done;
478 } else {
479 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
480 &priv_bn)))
481 goto done;
482 }
483
484 if (ak->evptype == EVP_PKEY_DH) {
485 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
486 || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
487 &g_bn)))
488 goto done;
489 }
490
491 ret = 1;
492done:
493 BN_free(p_bn);
494 BN_free(g_bn);
495 BN_free(priv_bn);
496 EVP_PKEY_free(pkey);
497 return ret;
498}
499
500#ifndef OPENSSL_NO_DES
501static int test_pkcs8key_nid_bio(void)
502{
503 int ret;
504 const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
505 static const char pwd[] = "PASSWORD";
506 EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
507 BIO *in = NULL, *enc_bio = NULL;
508 char *enc_data = NULL;
509 long enc_datalen = 0;
510 OSSL_PROVIDER *provider = NULL;
511
512 ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
513 && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
514 && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
515 sizeof(kExampleRSAKeyPKCS8)))
516 && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
517 && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
518 pwd, sizeof(pwd) - 1,
519 NULL, NULL), 1)
520 && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
521 && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
522 (void *)pwd))
523 && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
524
525 EVP_PKEY_free(pkey_dec);
526 EVP_PKEY_free(pkey);
527 BIO_free(in);
528 BIO_free(enc_bio);
529 OSSL_PROVIDER_unload(provider);
530 return ret;
531}
532#endif /* OPENSSL_NO_DES */
533
534static int test_alternative_default(void)
535{
536 OSSL_LIB_CTX *oldctx;
537 EVP_MD *sha256;
538 int ok = 0;
539
540 /*
541 * setup_tests() loaded the "null" provider in the current default, so
542 * we know this fetch should fail.
543 */
544 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
545 goto err;
546
547 /*
548 * Now we switch to our main library context, and try again. Since no
549 * providers are loaded in this one, it should fall back to the default.
550 */
551 if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
552 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
553 goto err;
554 EVP_MD_free(sha256);
555 sha256 = NULL;
556
557 /*
558 * Switching back should give us our main library context back, and
559 * fetching SHA2-256 should fail again.
560 */
561 if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
562 || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
563 goto err;
564
565 ok = 1;
566 err:
567 EVP_MD_free(sha256);
568 return ok;
569}
570
571static int test_provider_unload_effective(int testid)
572{
573 EVP_MD *sha256 = NULL;
574 OSSL_PROVIDER *provider = NULL;
575 int ok = 0;
576
577 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
578 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
579 goto err;
580
581 if (testid > 0) {
582 OSSL_PROVIDER_unload(provider);
583 provider = NULL;
584 EVP_MD_free(sha256);
585 sha256 = NULL;
586 } else {
587 EVP_MD_free(sha256);
588 sha256 = NULL;
589 OSSL_PROVIDER_unload(provider);
590 provider = NULL;
591 }
592
593 /*
594 * setup_tests() loaded the "null" provider in the current default, and
595 * we unloaded it above after the load so we know this fetch should fail.
596 */
597 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
598 goto err;
599
600 ok = 1;
601 err:
602 EVP_MD_free(sha256);
603 OSSL_PROVIDER_unload(provider);
604 return ok;
605}
606
607static int test_d2i_PrivateKey_ex(int testid)
608{
609 int ok = 0;
610 OSSL_PROVIDER *provider = NULL;
611 BIO *key_bio = NULL;
612 EVP_PKEY *pkey = NULL;
613 int id = (testid == 0) ? 0 : 2;
614
615 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
616 goto err;
617 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, keydata[id].size)))
618 goto err;
619 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
620 goto err;
621
622 ERR_clear_error();
623 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
624 goto err;
625 ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
626 TEST_int_eq(ERR_peek_error(), 0);
627 test_openssl_errors();
628
629 err:
630 EVP_PKEY_free(pkey);
631 BIO_free(key_bio);
632 OSSL_PROVIDER_unload(provider);
633
634 return ok;
635}
636
637static int test_PEM_read_bio_negative(int testid)
638{
639 int ok = 0;
640 OSSL_PROVIDER *provider = NULL;
641 BIO *key_bio = NULL;
642 EVP_PKEY *pkey = NULL;
643
644 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder, keydata[testid].size)))
645 goto err;
646 ERR_clear_error();
647 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
648 goto err;
649 if (!TEST_int_ne(ERR_peek_error(), 0))
650 goto err;
651 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
652 goto err;
653 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
654 goto err;
655 ERR_clear_error();
656 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
657 goto err;
658 if (!TEST_int_ne(ERR_peek_error(), 0))
659 goto err;
660
661 ok = 1;
662
663 err:
664 test_openssl_errors();
665 EVP_PKEY_free(pkey);
666 BIO_free(key_bio);
667 OSSL_PROVIDER_unload(provider);
668
669 return ok;
670}
671
672static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
673 const EVP_PKEY *expected, const char *type)
674{
675 EVP_PKEY_CTX *ctx = NULL;
676 EVP_PKEY *pkey = NULL;
677 int ret;
678
679 ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
680 && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
681 && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
682 EVP_PKEY_KEYPAIR,
683 (OSSL_PARAM *)params), 1)
684 && TEST_true(EVP_PKEY_eq(pkey, expected));
685 EVP_PKEY_CTX_free(ctx);
686 EVP_PKEY_free(pkey);
687 return ret;
688}
689
690#ifndef OPENSSL_NO_DSA
691/*
692 * This data was generated using:
693 * > openssl genpkey \
694 * -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
695 * -pkeyopt gindex:5 -out dsa_param.pem
696 * > openssl genpkey \
697 * -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
698 */
699static const unsigned char dsa_key[] = {
700 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
701 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66,
702 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21,
703 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37,
704 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23,
705 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8,
706 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b,
707 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21,
708 0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03,
709 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e,
710 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55,
711 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2,
712 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec,
713 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32,
714 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a,
715 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69,
716 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44,
717 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12,
718 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07,
719 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96,
720 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40,
721 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c,
722 0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d, 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02,
723 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31,
724 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02,
725 0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5,
726 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47,
727 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3,
728 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
729 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c,
730 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2,
731 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63,
732 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d,
733 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
734 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50,
735 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80,
736 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37,
737 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8,
738 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
739 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97,
740 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9,
741 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12,
742 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc,
743 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
744 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01,
745 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7,
746 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34, 0x02, 0x82, 0x01, 0x00,
747 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30,
748 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38,
749 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2,
750 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c,
751 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
752 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3,
753 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f,
754 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e,
755 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65,
756 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
757 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41,
758 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d,
759 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07,
760 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29,
761 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
762 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee,
763 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f,
764 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d,
765 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5,
766 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
767 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf,
768 0x07, 0x48, 0x45, 0xfd, 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b,
769 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4,
770 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21,
771};
772static const unsigned char dsa_p[] = {
773 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57,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x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
786 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
787 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
788 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
789 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
790 0xfd, 0x1b
791};
792static const unsigned char dsa_q[] = {
793 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
794 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
795};
796static const unsigned char dsa_g[] = {
797 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
798 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
799 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
800 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
801 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
802 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
803 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
804 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
805 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
806 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
807 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
808 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
809 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
810 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
811 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
812 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
813 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
814 0x9f, 0x34
815};
816static const unsigned char dsa_priv[] = {
817 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
818 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
819};
820static const unsigned char dsa_pub[] = {
821 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
822 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
823 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
824 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
825 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
826 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
827 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
828 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
829 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
830 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
831 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
832 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
833 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
834 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
835 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
836 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
837 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
838 0xfd
839};
840
841static int do_check_params(OSSL_PARAM key_params[], int expected)
842{
843 EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
844 EVP_PKEY *pkey = NULL;
845 int ret;
846
847 ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
848 && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
849 && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
850 EVP_PKEY_KEYPAIR, key_params), 1)
851 && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
852 NULL))
853 && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
854 EVP_PKEY_CTX_free(check_ctx);
855 EVP_PKEY_CTX_free(gen_ctx);
856 EVP_PKEY_free(pkey);
857 return ret;
858}
859
860static int do_check_bn(OSSL_PARAM params[], const char *key,
861 const unsigned char *expected, size_t expected_len)
862{
863 OSSL_PARAM *p;
864 BIGNUM *bn = NULL;
865 unsigned char buffer[256 + 1];
866 int ret, len;
867
868 ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
869 && TEST_true(OSSL_PARAM_get_BN(p, &bn))
870 && TEST_int_gt(len = BN_bn2binpad(bn, buffer, expected_len), 0)
871 && TEST_mem_eq(expected, expected_len, buffer, len);
872 BN_free(bn);
873 return ret;
874}
875
876static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
877{
878 OSSL_PARAM *p;
879 int val = 0;
880
881 return TEST_ptr(p = OSSL_PARAM_locate(params, key))
882 && TEST_true(OSSL_PARAM_get_int(p, &val))
883 && TEST_int_eq(val, expected);
884}
885
886static int test_dsa_tofrom_data_select(void)
887{
888 int ret;
889 EVP_PKEY *key = NULL;
890 const unsigned char *pkeydata = dsa_key;
891
892 ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
893 mainctx, NULL))
894 && TEST_true(do_pkey_tofrom_data_select(key, "DSA"));
895
896 EVP_PKEY_free(key);
897 return ret;
898}
899
900static int test_dsa_todata(void)
901{
902 EVP_PKEY *pkey = NULL;
903 OSSL_PARAM *to_params = NULL, *all_params = NULL;
904 OSSL_PARAM gen_params[4];
905 int ret = 0;
906 const unsigned char *pkeydata = dsa_key;
907
908 unsigned char dsa_seed[] = {
909 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
910 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
911 0x90, 0xc1, 0xe3, 0xe0
912 };
913 int dsa_pcounter = 319;
914 int dsa_gindex = 5;
915
916 gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
917 (void*)dsa_seed,
918 sizeof(dsa_seed));
919 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
920 &dsa_gindex);
921 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
922 &dsa_pcounter);
923 gen_params[3] = OSSL_PARAM_construct_end();
924
925 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
926 mainctx, NULL))
927 || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
928 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
929 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
930 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
931 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
932 sizeof(dsa_pub))
933 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
934 sizeof(dsa_priv))
935 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
936 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
937 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
938 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
939 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
940 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
941 || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
942 goto err;
943
944 if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
945 goto err;
946
947 if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
948 || !do_check_params(all_params, 1))
949 goto err;
950 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
951 &dsa_gindex);
952 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
953 &dsa_pcounter);
954 /*
955 * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
956 * results in an invalid key. This also verifies that the fips186-4
957 * validation code is running.
958 */
959 dsa_gindex++;
960 if (!do_check_params(all_params, 0))
961 goto err;
962 dsa_gindex--;
963 dsa_pcounter++;
964 if (!do_check_params(all_params, 0))
965 goto err;
966 dsa_pcounter--;
967 dsa_seed[0] = 0xb0;
968 if (!do_check_params(all_params, 0))
969 goto err;
970
971 ret = 1;
972err:
973 EVP_PKEY_free(pkey);
974 OSSL_PARAM_free(all_params);
975 OSSL_PARAM_free(to_params);
976 return ret;
977}
978
979/*
980 * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata
981 * This test:
982 * checks for failure when the property query is bad (tstid == 0)
983 * checks for success when the property query is valid (tstid == 1)
984 */
985static int test_dsa_fromdata_digest_prop(int tstid)
986{
987 EVP_PKEY_CTX *ctx = NULL, *gctx = NULL;
988 EVP_PKEY *pkey = NULL, *pkey2 = NULL;
989 OSSL_PARAM params[4], *p = params;
990 int ret = 0;
991 int expected = (tstid == 0 ? 0 : 1);
992 unsigned int pbits = 512; /* minimum allowed for speed */
993
994 *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
995 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0);
996 /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */
997 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
998 tstid == 0 ? "provider=unknown" : "provider=default", 0);
999 *p++ = OSSL_PARAM_construct_end();
1000
1001 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
1002 || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
1003 || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1))
1004 goto err;
1005
1006 if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1007 || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1)
1008 || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected))
1009 goto err;
1010
1011 ret = 1;
1012err:
1013 EVP_PKEY_free(pkey2);
1014 EVP_PKEY_free(pkey);
1015 EVP_PKEY_CTX_free(ctx);
1016 EVP_PKEY_CTX_free(gctx);
1017 return ret;
1018}
1019#endif /* OPENSSL_NO_DSA */
1020
1021static int test_pkey_todata_null(void)
1022{
1023 OSSL_PARAM *params = NULL;
1024 EVP_PKEY *pkey = NULL;
1025 int ret = 0;
1026 const unsigned char *pdata = keydata[0].kder;
1027
1028 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1029 mainctx, NULL))
1030 && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, &params), 0)
1031 && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
1032 EVP_PKEY_free(pkey);
1033 return ret;
1034}
1035
1036static OSSL_CALLBACK test_pkey_export_cb;
1037
1038static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
1039{
1040 if (arg == NULL)
1041 return 0;
1042 return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
1043}
1044
1045static int test_pkey_export_null(void)
1046{
1047 EVP_PKEY *pkey = NULL;
1048 int ret = 0;
1049 const unsigned char *pdata = keydata[0].kder;
1050
1051 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1052 mainctx, NULL))
1053 && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
1054 test_pkey_export_cb, NULL), 0)
1055 && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
1056 EVP_PKEY_free(pkey);
1057 return ret;
1058}
1059
1060static int test_pkey_export(void)
1061{
1062 EVP_PKEY *pkey = NULL;
1063#ifndef OPENSSL_NO_DEPRECATED_3_0
1064 RSA *rsa = NULL;
1065#endif
1066 int ret = 1;
1067 const unsigned char *pdata = keydata[0].kder;
1068 int pdata_len = keydata[0].size;
1069
1070 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
1071 mainctx, NULL))
1072 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1073 test_pkey_export_cb, pkey))
1074 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1075 test_pkey_export_cb, NULL)))
1076 ret = 0;
1077 EVP_PKEY_free(pkey);
1078
1079#ifndef OPENSSL_NO_DEPRECATED_3_0
1080 /* Now, try with a legacy key */
1081 pdata = keydata[0].kder;
1082 pdata_len = keydata[0].size;
1083 if (!TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
1084 || !TEST_ptr(pkey = EVP_PKEY_new())
1085 || !TEST_true(EVP_PKEY_assign_RSA(pkey, rsa))
1086 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1087 test_pkey_export_cb, pkey))
1088 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1089 test_pkey_export_cb, NULL)))
1090 ret = 0;
1091 EVP_PKEY_free(pkey);
1092#endif
1093 return ret;
1094}
1095
1096static int test_rsa_pss_sign(void)
1097{
1098 EVP_PKEY *pkey = NULL;
1099 EVP_PKEY_CTX *pctx = NULL;
1100 int ret = 0;
1101 const unsigned char *pdata = keydata[0].kder;
1102 const char *mdname = "SHA2-256";
1103 OSSL_PARAM sig_params[3];
1104 unsigned char mdbuf[256 / 8] = { 0 };
1105 int padding = RSA_PKCS1_PSS_PADDING;
1106 unsigned char *sig = NULL;
1107 size_t sig_len = 0;
1108
1109 sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
1110 &padding);
1111 sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
1112 (char *)mdname, 0);
1113 sig_params[2] = OSSL_PARAM_construct_end();
1114
1115 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, keydata[0].size,
1116 mainctx, NULL))
1117 && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1118 && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
1119 && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
1120 sizeof(mdbuf)), 0)
1121 && TEST_int_gt(sig_len, 0)
1122 && TEST_ptr(sig = OPENSSL_malloc(sig_len))
1123 && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
1124 sizeof(mdbuf)), 0);
1125
1126 EVP_PKEY_CTX_free(pctx);
1127 OPENSSL_free(sig);
1128 EVP_PKEY_free(pkey);
1129
1130 return ret;
1131}
1132
1133static int test_evp_md_ctx_dup(void)
1134{
1135 EVP_MD_CTX *mdctx;
1136 EVP_MD_CTX *copyctx = NULL;
1137 int ret;
1138
1139 /* test copying freshly initialized context */
1140 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1141 && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
1142
1143 EVP_MD_CTX_free(mdctx);
1144 EVP_MD_CTX_free(copyctx);
1145 return ret;
1146}
1147
1148static int test_evp_md_ctx_copy(void)
1149{
1150 EVP_MD_CTX *mdctx = NULL;
1151 EVP_MD_CTX *copyctx = NULL;
1152 int ret;
1153
1154 /* test copying freshly initialized context */
1155 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1156 && TEST_ptr(copyctx = EVP_MD_CTX_new())
1157 && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
1158
1159 EVP_MD_CTX_free(mdctx);
1160 EVP_MD_CTX_free(copyctx);
1161 return ret;
1162}
1163
1164#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
1165static int test_evp_pbe_alg_add(void)
1166{
1167 int ret = 0;
1168 int cipher_nid = 0, md_nid = 0;
1169 EVP_PBE_KEYGEN_EX *keygen_ex = NULL;
1170 EVP_PBE_KEYGEN *keygen = NULL;
1171
1172 if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(),
1173 PKCS5_PBE_keyivgen)))
1174 goto err;
1175
1176 if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
1177 &cipher_nid, &md_nid, &keygen, &keygen_ex)))
1178 goto err;
1179
1180 if (!TEST_true(keygen != NULL))
1181 goto err;
1182 if (!TEST_true(keygen_ex == NULL))
1183 goto err;
1184
1185 ret = 1;
1186
1187err:
1188 return ret;
1189}
1190#endif
1191
1192int setup_tests(void)
1193{
1194 if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
1195 OSSL_LIB_CTX_free(mainctx);
1196 mainctx = NULL;
1197 return 0;
1198 }
1199
1200 ADD_TEST(test_alternative_default);
1201 ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
1202#ifndef OPENSSL_NO_EC
1203 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
1204 ADD_TEST(test_ec_tofrom_data_select);
1205 ADD_TEST(test_ecx_tofrom_data_select);
1206#else
1207 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
1208#endif
1209#ifndef OPENSSL_NO_SM2
1210 ADD_TEST(test_sm2_tofrom_data_select);
1211#endif
1212#ifndef OPENSSL_NO_DSA
1213 ADD_TEST(test_dsa_todata);
1214 ADD_TEST(test_dsa_tofrom_data_select);
1215 ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2);
1216#endif
1217#ifndef OPENSSL_NO_DH
1218 ADD_TEST(test_dh_tofrom_data_select);
1219 ADD_TEST(test_dh_paramgen);
1220#endif
1221 ADD_TEST(test_rsa_tofrom_data_select);
1222
1223 ADD_TEST(test_pkey_todata_null);
1224 ADD_TEST(test_pkey_export_null);
1225 ADD_TEST(test_pkey_export);
1226#ifndef OPENSSL_NO_DES
1227 ADD_TEST(test_pkcs8key_nid_bio);
1228#endif
1229 ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
1230 ADD_TEST(test_rsa_pss_sign);
1231 ADD_TEST(test_evp_md_ctx_dup);
1232 ADD_TEST(test_evp_md_ctx_copy);
1233 ADD_ALL_TESTS(test_provider_unload_effective, 2);
1234#if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
1235 ADD_TEST(test_evp_pbe_alg_add);
1236#endif
1237 return 1;
1238}
1239
1240void cleanup_tests(void)
1241{
1242 OSSL_LIB_CTX_free(mainctx);
1243 OSSL_PROVIDER_unload(nullprov);
1244}
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette