1 /*
2 * Copyright 2015-2024 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 #include <openssl/ui.h>
27
28 #include "testutil.h"
29 #include "internal/nelem.h"
30 #include "crypto/evp.h"
31 #include "../crypto/evp/evp_local.h"
32
33 /* Defined in tls-provider.c */
34 int tls_provider_init(const OSSL_CORE_HANDLE *handle,
35 const OSSL_DISPATCH *in,
36 const OSSL_DISPATCH **out,
37 void **provctx);
38
39 static OSSL_LIB_CTX *mainctx = NULL;
40 static OSSL_PROVIDER *nullprov = NULL;
41
42 /*
43 * kExampleRSAKeyDER is an RSA private key in ASN.1, DER format. Of course, you
44 * should never use this key anywhere but in an example.
45 */
46 static const unsigned char kExampleRSAKeyDER[] = {
47 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81, 0x00, 0xf8,
48 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5, 0xb4, 0x59,
49 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e, 0xd3, 0x37,
50 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34, 0x75, 0x71,
51 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde, 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e, 0x99, 0x5d, 0x08, 0xf1,
97 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
98 };
99
100 /*
101 * kExampleRSAKeyPKCS8 is kExampleRSAKeyDER encoded in a PKCS #8
102 * PrivateKeyInfo.
103 */
104 static const unsigned char kExampleRSAKeyPKCS8[] = {
105 0x30, 0x82, 0x02, 0x76, 0x02, 0x01, 0x00, 0x30, 0x0d, 0x06, 0x09, 0x2a,
106 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01, 0x05, 0x00, 0x04, 0x82,
107 0x02, 0x60, 0x30, 0x82, 0x02, 0x5c, 0x02, 0x01, 0x00, 0x02, 0x81, 0x81,
108 0x00, 0xf8, 0xb8, 0x6c, 0x83, 0xb4, 0xbc, 0xd9, 0xa8, 0x57, 0xc0, 0xa5,
109 0xb4, 0x59, 0x76, 0x8c, 0x54, 0x1d, 0x79, 0xeb, 0x22, 0x52, 0x04, 0x7e,
110 0xd3, 0x37, 0xeb, 0x41, 0xfd, 0x83, 0xf9, 0xf0, 0xa6, 0x85, 0x15, 0x34,
111 0x75, 0x71, 0x5a, 0x84, 0xa8, 0x3c, 0xd2, 0xef, 0x5a, 0x4e, 0xd3, 0xde,
112 0x97, 0x8a, 0xdd, 0xff, 0xbb, 0xcf, 0x0a, 0xaa, 0x86, 0x92, 0xbe, 0xb8,
113 0x50, 0xe4, 0xcd, 0x6f, 0x80, 0x33, 0x30, 0x76, 0x13, 0x8f, 0xca, 0x7b,
114 0xdc, 0xec, 0x5a, 0xca, 0x63, 0xc7, 0x03, 0x25, 0xef, 0xa8, 0x8a, 0x83,
115 0x58, 0x76, 0x20, 0xfa, 0x16, 0x77, 0xd7, 0x79, 0x92, 0x63, 0x01, 0x48,
116 0x1a, 0xd8, 0x7b, 0x67, 0xf1, 0x52, 0x55, 0x49, 0x4e, 0xd6, 0x6e, 0x4a,
117 0x5c, 0xd7, 0x7a, 0x37, 0x36, 0x0c, 0xde, 0xdd, 0x8f, 0x44, 0xe8, 0xc2,
118 0xa7, 0x2c, 0x2b, 0xb5, 0xaf, 0x64, 0x4b, 0x61, 0x07, 0x02, 0x03, 0x01,
119 0x00, 0x01, 0x02, 0x81, 0x80, 0x74, 0x88, 0x64, 0x3f, 0x69, 0x45, 0x3a,
120 0x6d, 0xc7, 0x7f, 0xb9, 0xa3, 0xc0, 0x6e, 0xec, 0xdc, 0xd4, 0x5a, 0xb5,
121 0x32, 0x85, 0x5f, 0x19, 0xd4, 0xf8, 0xd4, 0x3f, 0x3c, 0xfa, 0xc2, 0xf6,
122 0x5f, 0xee, 0xe6, 0xba, 0x87, 0x74, 0x2e, 0xc7, 0x0c, 0xd4, 0x42, 0xb8,
123 0x66, 0x85, 0x9c, 0x7b, 0x24, 0x61, 0xaa, 0x16, 0x11, 0xf6, 0xb5, 0xb6,
124 0xa4, 0x0a, 0xc9, 0x55, 0x2e, 0x81, 0xa5, 0x47, 0x61, 0xcb, 0x25, 0x8f,
125 0xc2, 0x15, 0x7b, 0x0e, 0x7c, 0x36, 0x9f, 0x3a, 0xda, 0x58, 0x86, 0x1c,
126 0x5b, 0x83, 0x79, 0xe6, 0x2b, 0xcc, 0xe6, 0xfa, 0x2c, 0x61, 0xf2, 0x78,
127 0x80, 0x1b, 0xe2, 0xf3, 0x9d, 0x39, 0x2b, 0x65, 0x57, 0x91, 0x3d, 0x71,
128 0x99, 0x73, 0xa5, 0xc2, 0x79, 0x20, 0x8c, 0x07, 0x4f, 0xe5, 0xb4, 0x60,
129 0x1f, 0x99, 0xa2, 0xb1, 0x4f, 0x0c, 0xef, 0xbc, 0x59, 0x53, 0x00, 0x7d,
130 0xb1, 0x02, 0x41, 0x00, 0xfc, 0x7e, 0x23, 0x65, 0x70, 0xf8, 0xce, 0xd3,
131 0x40, 0x41, 0x80, 0x6a, 0x1d, 0x01, 0xd6, 0x01, 0xff, 0xb6, 0x1b, 0x3d,
132 0x3d, 0x59, 0x09, 0x33, 0x79, 0xc0, 0x4f, 0xde, 0x96, 0x27, 0x4b, 0x18,
133 0xc6, 0xd9, 0x78, 0xf1, 0xf4, 0x35, 0x46, 0xe9, 0x7c, 0x42, 0x7a, 0x5d,
134 0x9f, 0xef, 0x54, 0xb8, 0xf7, 0x9f, 0xc4, 0x33, 0x6c, 0xf3, 0x8c, 0x32,
135 0x46, 0x87, 0x67, 0x30, 0x7b, 0xa7, 0xac, 0xe3, 0x02, 0x41, 0x00, 0xfc,
136 0x2c, 0xdf, 0x0c, 0x0d, 0x88, 0xf5, 0xb1, 0x92, 0xa8, 0x93, 0x47, 0x63,
137 0x55, 0xf5, 0xca, 0x58, 0x43, 0xba, 0x1c, 0xe5, 0x9e, 0xb6, 0x95, 0x05,
138 0xcd, 0xb5, 0x82, 0xdf, 0xeb, 0x04, 0x53, 0x9d, 0xbd, 0xc2, 0x38, 0x16,
139 0xb3, 0x62, 0xdd, 0xa1, 0x46, 0xdb, 0x6d, 0x97, 0x93, 0x9f, 0x8a, 0xc3,
140 0x9b, 0x64, 0x7e, 0x42, 0xe3, 0x32, 0x57, 0x19, 0x1b, 0xd5, 0x6e, 0x85,
141 0xfa, 0xb8, 0x8d, 0x02, 0x41, 0x00, 0xbc, 0x3d, 0xde, 0x6d, 0xd6, 0x97,
142 0xe8, 0xba, 0x9e, 0x81, 0x37, 0x17, 0xe5, 0xa0, 0x64, 0xc9, 0x00, 0xb7,
143 0xe7, 0xfe, 0xf4, 0x29, 0xd9, 0x2e, 0x43, 0x6b, 0x19, 0x20, 0xbd, 0x99,
144 0x75, 0xe7, 0x76, 0xf8, 0xd3, 0xae, 0xaf, 0x7e, 0xb8, 0xeb, 0x81, 0xf4,
145 0x9d, 0xfe, 0x07, 0x2b, 0x0b, 0x63, 0x0b, 0x5a, 0x55, 0x90, 0x71, 0x7d,
146 0xf1, 0xdb, 0xd9, 0xb1, 0x41, 0x41, 0x68, 0x2f, 0x4e, 0x39, 0x02, 0x40,
147 0x5a, 0x34, 0x66, 0xd8, 0xf5, 0xe2, 0x7f, 0x18, 0xb5, 0x00, 0x6e, 0x26,
148 0x84, 0x27, 0x14, 0x93, 0xfb, 0xfc, 0xc6, 0x0f, 0x5e, 0x27, 0xe6, 0xe1,
149 0xe9, 0xc0, 0x8a, 0xe4, 0x34, 0xda, 0xe9, 0xa2, 0x4b, 0x73, 0xbc, 0x8c,
150 0xb9, 0xba, 0x13, 0x6c, 0x7a, 0x2b, 0x51, 0x84, 0xa3, 0x4a, 0xe0, 0x30,
151 0x10, 0x06, 0x7e, 0xed, 0x17, 0x5a, 0x14, 0x00, 0xc9, 0xef, 0x85, 0xea,
152 0x52, 0x2c, 0xbc, 0x65, 0x02, 0x40, 0x51, 0xe3, 0xf2, 0x83, 0x19, 0x9b,
153 0xc4, 0x1e, 0x2f, 0x50, 0x3d, 0xdf, 0x5a, 0xa2, 0x18, 0xca, 0x5f, 0x2e,
154 0x49, 0xaf, 0x6f, 0xcc, 0xfa, 0x65, 0x77, 0x94, 0xb5, 0xa1, 0x0a, 0xa9,
155 0xd1, 0x8a, 0x39, 0x37, 0xf4, 0x0b, 0xa0, 0xd7, 0x82, 0x27, 0x5e, 0xae,
156 0x17, 0x17, 0xa1, 0x1e, 0x54, 0x34, 0xbf, 0x6e, 0xc4, 0x8e, 0x99, 0x5d,
157 0x08, 0xf1, 0x2d, 0x86, 0x9d, 0xa5, 0x20, 0x1b, 0xe5, 0xdf,
158 };
159
160 #ifndef OPENSSL_NO_DH
161 static const unsigned char kExampleDHPrivateKeyDER[] = {
162 0x30, 0x82, 0x02, 0x26, 0x02, 0x01, 0x00, 0x30, 0x82, 0x01, 0x17, 0x06,
163 0x09, 0x2A, 0x86, 0x48, 0x86, 0xF7, 0x0D, 0x01, 0x03, 0x01, 0x30, 0x82,
164 0x01, 0x08, 0x02, 0x82, 0x01, 0x01, 0x00, 0xD8, 0x4B, 0x0F, 0x0E, 0x6B,
165 0x79, 0xE9, 0x23, 0x4E, 0xE4, 0xBE, 0x9A, 0x8F, 0x7A, 0x5C, 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xAE, 0x5A, 0xEB, 0xB3, 0x3D, 0x30, 0x39, 0x69, 0xDB,
196 0xF2, 0x4F, 0x94, 0xB7, 0x71, 0xAF, 0xBA, 0x5C, 0x1F, 0xF8, 0x6B, 0xE5,
197 0xD1, 0xB1, 0x00, 0x81, 0xE2, 0x6D, 0xEC, 0x65, 0xF7, 0x7E, 0xCE, 0x03,
198 0x84, 0x68, 0x42, 0x6A, 0x8B, 0x47, 0x8E, 0x4A, 0x88, 0xDE, 0x82, 0xDD,
199 0xAF, 0xA9, 0x6F, 0x18, 0xF7, 0xC6, 0xE2, 0xB9, 0x97, 0xCE, 0x47, 0x8F,
200 0x85, 0x19, 0x61, 0x42, 0x67, 0x21, 0x7D, 0x13, 0x6E, 0xB5, 0x5A, 0x62,
201 0xF3, 0x08, 0xE2, 0x70, 0x3B, 0x0E, 0x85, 0x3C, 0xA1, 0xD3, 0xED, 0x7A,
202 0x43, 0xD6, 0xDE, 0x30, 0x5C, 0x48, 0xB2, 0x99, 0xAB, 0x3E, 0x65, 0xA6,
203 0x66, 0x80, 0x22, 0xFF, 0x92, 0xC1, 0x42, 0x1C, 0x30, 0x87, 0x74, 0x1E,
204 0x53, 0x57, 0x7C, 0xF8, 0x77, 0x51, 0xF1, 0x74, 0x16, 0xF4, 0x45, 0x26,
205 0x77, 0x0A, 0x05, 0x96, 0x13, 0x12, 0x06, 0x86, 0x2B, 0xB8, 0x49, 0x82,
206 0x69, 0x43, 0x0A, 0x57, 0xA7, 0x30, 0x19, 0x4C, 0xB8, 0x47, 0x82, 0x6E,
207 0x64, 0x7A, 0x06, 0x13, 0x5A, 0x82, 0x98, 0xD6, 0x7A, 0x09, 0xEC, 0x03,
208 0x8D, 0x03
209 };
210 #endif /* OPENSSL_NO_DH */
211
212 #ifndef OPENSSL_NO_EC
213 /*
214 * kExampleECKeyDER is a sample EC private key encoded as an ECPrivateKey
215 * structure.
216 */
217 static const unsigned char kExampleECKeyDER[] = {
218 0x30, 0x77, 0x02, 0x01, 0x01, 0x04, 0x20, 0x07, 0x0f, 0x08, 0x72, 0x7a,
219 0xd4, 0xa0, 0x4a, 0x9c, 0xdd, 0x59, 0xc9, 0x4d, 0x89, 0x68, 0x77, 0x08,
220 0xb5, 0x6f, 0xc9, 0x5d, 0x30, 0x77, 0x0e, 0xe8, 0xd1, 0xc9, 0xce, 0x0a,
221 0x8b, 0xb4, 0x6a, 0xa0, 0x0a, 0x06, 0x08, 0x2a, 0x86, 0x48, 0xce, 0x3d,
222 0x03, 0x01, 0x07, 0xa1, 0x44, 0x03, 0x42, 0x00, 0x04, 0xe6, 0x2b, 0x69,
223 0xe2, 0xbf, 0x65, 0x9f, 0x97, 0xbe, 0x2f, 0x1e, 0x0d, 0x94, 0x8a, 0x4c,
224 0xd5, 0x97, 0x6b, 0xb7, 0xa9, 0x1e, 0x0d, 0x46, 0xfb, 0xdd, 0xa9, 0xa9,
225 0x1e, 0x9d, 0xdc, 0xba, 0x5a, 0x01, 0xe7, 0xd6, 0x97, 0xa8, 0x0a, 0x18,
226 0xf9, 0xc3, 0xc4, 0xa3, 0x1e, 0x56, 0xe2, 0x7c, 0x83, 0x48, 0xdb, 0x16,
227 0x1a, 0x1c, 0xf5, 0x1d, 0x7e, 0xf1, 0x94, 0x2d, 0x4b, 0xcf, 0x72, 0x22,
228 0xc1,
229 };
230
231 /* P-384 sample EC private key in PKCS8 format (no public key) */
232 static const unsigned char kExampleECKey2DER[] = {
233 0x30, 0x4E, 0x02, 0x01, 0x00, 0x30, 0x10, 0x06, 0x07, 0x2A, 0x86, 0x48,
234 0xCE, 0x3D, 0x02, 0x01, 0x06, 0x05, 0x2B, 0x81, 0x04, 0x00, 0x22, 0x04,
235 0x37, 0x30, 0x35, 0x02, 0x01, 0x01, 0x04, 0x30, 0x73, 0xE3, 0x3A, 0x05,
236 0xF2, 0xB6, 0x99, 0x6D, 0x0C, 0x33, 0x7F, 0x15, 0x9E, 0x10, 0xA9, 0x17,
237 0x4C, 0x0A, 0x82, 0x57, 0x71, 0x13, 0x7A, 0xAC, 0x46, 0xA2, 0x5E, 0x1C,
238 0xE0, 0xC7, 0xB2, 0xF8, 0x20, 0x40, 0xC2, 0x27, 0xC8, 0xBE, 0x02, 0x7E,
239 0x96, 0x69, 0xE0, 0x04, 0xCB, 0x89, 0x0B, 0x42
240 };
241
242 # ifndef OPENSSL_NO_ECX
243 static const unsigned char kExampleECXKey2DER[] = {
244 0x30, 0x2E, 0x02, 0x01, 0x00, 0x30, 0x05, 0x06, 0x03, 0x2b, 0x65, 0x6e,
245 0x04, 0x22, 0x04, 0x20, 0xc8, 0xa9, 0xd5, 0xa9, 0x10, 0x91, 0xad, 0x85,
246 0x1c, 0x66, 0x8b, 0x07, 0x36, 0xc1, 0xc9, 0xa0, 0x29, 0x36, 0xc0, 0xd3,
247 0xad, 0x62, 0x67, 0x08, 0x58, 0x08, 0x80, 0x47, 0xba, 0x05, 0x74, 0x75
248 };
249 # endif
250 #endif
251
252 typedef struct APK_DATA_st {
253 const unsigned char *kder;
254 size_t size;
255 int evptype;
256 } APK_DATA;
257
258 static APK_DATA keydata[] = {
259 {kExampleRSAKeyDER, sizeof(kExampleRSAKeyDER), EVP_PKEY_RSA},
260 {kExampleRSAKeyPKCS8, sizeof(kExampleRSAKeyPKCS8), EVP_PKEY_RSA},
261 #ifndef OPENSSL_NO_EC
262 # ifndef OPENSSL_NO_ECX
263 {kExampleECXKey2DER, sizeof(kExampleECXKey2DER), EVP_PKEY_X25519},
264 # endif
265 {kExampleECKeyDER, sizeof(kExampleECKeyDER), EVP_PKEY_EC},
266 {kExampleECKey2DER, sizeof(kExampleECKey2DER), EVP_PKEY_EC},
267 #endif
268 #ifndef OPENSSL_NO_DH
269 {kExampleDHPrivateKeyDER, sizeof(kExampleDHPrivateKeyDER), EVP_PKEY_DH},
270 #endif
271 };
272
pkey_has_private(EVP_PKEY * key,const char * privtag,int use_octstring)273 static int pkey_has_private(EVP_PKEY *key, const char *privtag,
274 int use_octstring)
275 {
276 int ret = 0;
277
278 if (use_octstring) {
279 unsigned char buf[64];
280
281 ret = EVP_PKEY_get_octet_string_param(key, privtag, buf, sizeof(buf),
282 NULL);
283 } else {
284 BIGNUM *bn = NULL;
285
286 ret = EVP_PKEY_get_bn_param(key, privtag, &bn);
287 BN_free(bn);
288 }
289 return ret;
290 }
291
do_pkey_tofrom_data_select(EVP_PKEY * key,const char * keytype)292 static int do_pkey_tofrom_data_select(EVP_PKEY *key, const char *keytype)
293 {
294 int ret = 0;
295 OSSL_PARAM *pub_params = NULL, *keypair_params = NULL;
296 EVP_PKEY *fromkey = NULL, *fromkeypair = NULL;
297 EVP_PKEY_CTX *fromctx = NULL;
298 const char *privtag = strcmp(keytype, "RSA") == 0 ? "d" : "priv";
299 const int use_octstring = strcmp(keytype, "X25519") == 0;
300
301 /*
302 * Select only the public key component when using EVP_PKEY_todata() and
303 * check that the resulting param array does not contain a private key.
304 */
305 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_PUBLIC_KEY, &pub_params), 1)
306 || !TEST_ptr_null(OSSL_PARAM_locate(pub_params, privtag)))
307 goto end;
308 /*
309 * Select the keypair when using EVP_PKEY_todata() and check that
310 * the param array contains a private key.
311 */
312 if (!TEST_int_eq(EVP_PKEY_todata(key, EVP_PKEY_KEYPAIR, &keypair_params), 1)
313 || !TEST_ptr(OSSL_PARAM_locate(keypair_params, privtag)))
314 goto end;
315
316 /*
317 * Select only the public key when using EVP_PKEY_fromdata() and check that
318 * the resulting key does not contain a private key.
319 */
320 if (!TEST_ptr(fromctx = EVP_PKEY_CTX_new_from_name(mainctx, keytype, NULL))
321 || !TEST_int_eq(EVP_PKEY_fromdata_init(fromctx), 1)
322 || !TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkey, EVP_PKEY_PUBLIC_KEY,
323 keypair_params), 1)
324 || !TEST_false(pkey_has_private(fromkey, privtag, use_octstring)))
325 goto end;
326 /*
327 * Select the keypair when using EVP_PKEY_fromdata() and check that
328 * the resulting key contains a private key.
329 */
330 if (!TEST_int_eq(EVP_PKEY_fromdata(fromctx, &fromkeypair,
331 EVP_PKEY_KEYPAIR, keypair_params), 1)
332 || !TEST_true(pkey_has_private(fromkeypair, privtag, use_octstring)))
333 goto end;
334 ret = 1;
335 end:
336 EVP_PKEY_free(fromkeypair);
337 EVP_PKEY_free(fromkey);
338 EVP_PKEY_CTX_free(fromctx);
339 OSSL_PARAM_free(keypair_params);
340 OSSL_PARAM_free(pub_params);
341 return ret;
342 }
343
344 #ifndef OPENSSL_NO_DH
test_dh_tofrom_data_select(void)345 static int test_dh_tofrom_data_select(void)
346 {
347 int ret;
348 OSSL_PARAM params[2];
349 EVP_PKEY *key = NULL;
350 EVP_PKEY_CTX *gctx = NULL;
351 # ifndef OPENSSL_NO_DEPRECATED_3_0
352 const DH *dhkey;
353 const BIGNUM *privkey;
354 # endif
355
356 params[0] = OSSL_PARAM_construct_utf8_string("group", "ffdhe2048", 0);
357 params[1] = OSSL_PARAM_construct_end();
358 ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DHX", NULL))
359 && TEST_int_gt(EVP_PKEY_keygen_init(gctx), 0)
360 && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
361 && TEST_int_gt(EVP_PKEY_generate(gctx, &key), 0)
362 && TEST_true(do_pkey_tofrom_data_select(key, "DHX"));
363 # ifndef OPENSSL_NO_DEPRECATED_3_0
364 ret = ret && TEST_ptr(dhkey = EVP_PKEY_get0_DH(key))
365 && TEST_ptr(privkey = DH_get0_priv_key(dhkey))
366 && TEST_int_le(BN_num_bits(privkey), 225);
367 # endif
368 EVP_PKEY_free(key);
369 EVP_PKEY_CTX_free(gctx);
370 return ret;
371 }
372
test_dh_paramgen(void)373 static int test_dh_paramgen(void)
374 {
375 int ret;
376 OSSL_PARAM params[3];
377 EVP_PKEY *pkey = NULL;
378 EVP_PKEY_CTX *gctx = NULL;
379 unsigned int pbits = 512; /* minimum allowed for speed */
380
381 params[0] = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
382 params[1] = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_TYPE,
383 "generator", 0);
384 params[2] = OSSL_PARAM_construct_end();
385
386 ret = TEST_ptr(gctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
387 && TEST_int_gt(EVP_PKEY_paramgen_init(gctx), 0)
388 && TEST_true(EVP_PKEY_CTX_set_params(gctx, params))
389 && TEST_true(EVP_PKEY_paramgen(gctx, &pkey))
390 && TEST_ptr(pkey);
391
392 EVP_PKEY_CTX_free(gctx);
393 gctx = NULL;
394
395 ret = ret && TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
396 && TEST_int_eq(EVP_PKEY_param_check(gctx), 1)
397 && TEST_int_eq(EVP_PKEY_param_check_quick(gctx), 1);
398
399 EVP_PKEY_CTX_free(gctx);
400 EVP_PKEY_free(pkey);
401 return ret;
402 }
403
set_fromdata_string(EVP_PKEY_CTX * ctx,const char * name,char * value)404 static int set_fromdata_string(EVP_PKEY_CTX *ctx, const char *name, char *value)
405 {
406 int ret;
407 OSSL_PARAM params[2];
408 EVP_PKEY *pkey = NULL;
409
410 if (EVP_PKEY_fromdata_init(ctx) != 1)
411 return -1;
412 params[0] = OSSL_PARAM_construct_utf8_string(name, value, 0);
413 params[1] = OSSL_PARAM_construct_end();
414 ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
415 EVP_PKEY_free(pkey);
416 return ret;
417 }
418
set_fromdata_uint(EVP_PKEY_CTX * ctx,const char * name)419 static int set_fromdata_uint(EVP_PKEY_CTX *ctx, const char *name)
420 {
421 int ret;
422 unsigned int tmp = 0;
423 OSSL_PARAM params[2];
424 EVP_PKEY *pkey = NULL;
425
426 if (EVP_PKEY_fromdata_init(ctx) != 1)
427 return -1;
428 params[0] = OSSL_PARAM_construct_uint(name, &tmp);
429 params[1] = OSSL_PARAM_construct_end();
430 ret = EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params);
431 EVP_PKEY_free(pkey);
432 return ret;
433 }
434
test_dh_paramfromdata(void)435 static int test_dh_paramfromdata(void)
436 {
437 EVP_PKEY_CTX *ctx = NULL;
438 int ret = 0;
439
440 /* Test failure paths for FFC - mainly due to setting the wrong param type */
441 ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DH", NULL))
442 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_GROUP_NAME), 0)
443 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_GROUP_NAME, "bad"), 0)
444 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_P, "bad"), 0)
445 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_GINDEX, "bad"), 0)
446 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_PCOUNTER, "bad"), 0)
447 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_COFACTOR, "bad"), 0)
448 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_H, "bad"), 0)
449 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_SEED), 0)
450 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, "bad"), 0)
451 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_G, "bad"), 0)
452 && TEST_int_eq(set_fromdata_string(ctx, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, "bad"), 0)
453 && TEST_int_eq(set_fromdata_uint(ctx, OSSL_PKEY_PARAM_FFC_DIGEST), 0);
454
455 EVP_PKEY_CTX_free(ctx);
456 return ret;
457 }
458
459 #endif
460
461 /* Test that calling EVP_PKEY_Q_keygen() for a non-standard keytype works as expected */
test_new_keytype(void)462 static int test_new_keytype(void)
463 {
464 int ret = 0;
465 EVP_PKEY *key = NULL;
466 OSSL_PROVIDER *tlsprov = NULL;
467 EVP_PKEY_CTX *ctx = NULL;
468 size_t outlen, secretlen, secretlen2;
469 unsigned char *out = NULL, *secret = NULL, *secret2 = NULL;
470
471 /* without tls-provider key should not be create-able */
472 if (TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
473 goto err;
474 /* prepare & load tls-provider */
475 if (!TEST_true(OSSL_PROVIDER_add_builtin(mainctx, "tls-provider",
476 tls_provider_init))
477 || !TEST_ptr(tlsprov = OSSL_PROVIDER_load(mainctx, "tls-provider")))
478 goto err;
479 /* now try creating key again, should work this time */
480 if (!TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "XOR")))
481 goto err;
482 /* now do encaps/decaps to validate all is good */
483 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new(key, NULL))
484 || !TEST_int_eq(EVP_PKEY_encapsulate_init(ctx, NULL), 1)
485 || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, NULL, &outlen, NULL, &secretlen), 1))
486 goto err;
487 out = OPENSSL_malloc(outlen);
488 secret = OPENSSL_malloc(secretlen);
489 secret2 = OPENSSL_malloc(secretlen);
490 if (out == NULL || secret == NULL || secret2 == NULL
491 || !TEST_int_eq(EVP_PKEY_encapsulate(ctx, out, &outlen, secret, &secretlen), 1)
492 || !TEST_int_eq(EVP_PKEY_decapsulate_init(ctx, NULL), 1)
493 || !TEST_int_eq(EVP_PKEY_decapsulate(ctx, secret2, &secretlen2, out, outlen), 1)
494 || !TEST_mem_eq(secret, secretlen, secret2, secretlen2))
495 goto err;
496 ret = OSSL_PROVIDER_unload(tlsprov);
497
498 err:
499 OPENSSL_free(out);
500 OPENSSL_free(secret);
501 OPENSSL_free(secret2);
502 EVP_PKEY_CTX_free(ctx);
503 EVP_PKEY_free(key);
504 return ret;
505 }
506
507 #ifndef OPENSSL_NO_EC
508
test_ec_d2i_i2d_pubkey(void)509 static int test_ec_d2i_i2d_pubkey(void)
510 {
511 int ret = 0;
512 FILE *fp = NULL;
513 EVP_PKEY *key = NULL, *outkey = NULL;
514 static const char *filename = "pubkey.der";
515
516 if (!TEST_ptr(fp = fopen(filename, "wb"))
517 || !TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
518 || !TEST_true(i2d_PUBKEY_fp(fp, key))
519 || !TEST_int_eq(fclose(fp), 0))
520 goto err;
521 fp = NULL;
522
523 if (!TEST_ptr(fp = fopen(filename, "rb"))
524 || !TEST_ptr(outkey = d2i_PUBKEY_ex_fp(fp, NULL, mainctx, NULL))
525 || !TEST_int_eq(EVP_PKEY_eq(key, outkey), 1))
526 goto err;
527
528 ret = 1;
529
530 err:
531 EVP_PKEY_free(outkey);
532 EVP_PKEY_free(key);
533 fclose(fp);
534 return ret;
535 }
536
test_ec_tofrom_data_select(void)537 static int test_ec_tofrom_data_select(void)
538 {
539 int ret;
540 EVP_PKEY *key = NULL;
541
542 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "EC", "P-256"))
543 && TEST_true(do_pkey_tofrom_data_select(key, "EC"));
544 EVP_PKEY_free(key);
545 return ret;
546 }
547
548 # ifndef OPENSSL_NO_ECX
test_ecx_tofrom_data_select(void)549 static int test_ecx_tofrom_data_select(void)
550 {
551 int ret;
552 EVP_PKEY *key = NULL;
553
554 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "X25519"))
555 && TEST_true(do_pkey_tofrom_data_select(key, "X25519"));
556 EVP_PKEY_free(key);
557 return ret;
558 }
559 # endif
560 #endif
561
562 #ifndef OPENSSL_NO_SM2
test_sm2_tofrom_data_select(void)563 static int test_sm2_tofrom_data_select(void)
564 {
565 int ret;
566 EVP_PKEY *key = NULL;
567
568 ret = TEST_ptr(key = EVP_PKEY_Q_keygen(mainctx, NULL, "SM2"))
569 && TEST_true(do_pkey_tofrom_data_select(key, "SM2"));
570 EVP_PKEY_free(key);
571 return ret;
572 }
573 #endif
574
test_rsa_tofrom_data_select(void)575 static int test_rsa_tofrom_data_select(void)
576 {
577 int ret;
578 EVP_PKEY *key = NULL;
579 const unsigned char *pdata = kExampleRSAKeyDER;
580 int pdata_len = sizeof(kExampleRSAKeyDER);
581
582 ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
583 mainctx, NULL))
584 && TEST_true(do_pkey_tofrom_data_select(key, "RSA"));
585 EVP_PKEY_free(key);
586 return ret;
587 }
588
589 /* This is the equivalent of test_d2i_AutoPrivateKey in evp_extra_test */
test_d2i_AutoPrivateKey_ex(int i)590 static int test_d2i_AutoPrivateKey_ex(int i)
591 {
592 int ret = 0;
593 const unsigned char *p;
594 EVP_PKEY *pkey = NULL;
595 const APK_DATA *ak = &keydata[i];
596 const unsigned char *input = ak->kder;
597 size_t input_len = ak->size;
598 int expected_id = ak->evptype;
599 BIGNUM *p_bn = NULL;
600 BIGNUM *g_bn = NULL;
601 BIGNUM *priv_bn = NULL;
602
603 p = input;
604 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &p, (long)input_len,
605 mainctx, NULL))
606 || !TEST_ptr_eq(p, input + input_len)
607 || !TEST_int_eq(EVP_PKEY_get_id(pkey), expected_id))
608 goto done;
609
610 if (ak->evptype == EVP_PKEY_RSA) {
611 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_RSA_D,
612 &priv_bn)))
613 goto done;
614 } else if (ak->evptype == EVP_PKEY_X25519) {
615 unsigned char buffer[32];
616 size_t len;
617
618 if (!TEST_true(EVP_PKEY_get_octet_string_param(pkey,
619 OSSL_PKEY_PARAM_PRIV_KEY,
620 buffer, sizeof(buffer),
621 &len)))
622 goto done;
623 } else {
624 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_PRIV_KEY,
625 &priv_bn)))
626 goto done;
627 }
628
629 if (ak->evptype == EVP_PKEY_DH) {
630 if (!TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_P, &p_bn))
631 || !TEST_true(EVP_PKEY_get_bn_param(pkey, OSSL_PKEY_PARAM_FFC_G,
632 &g_bn)))
633 goto done;
634 }
635
636 ret = 1;
637 done:
638 BN_free(p_bn);
639 BN_free(g_bn);
640 BN_free(priv_bn);
641 EVP_PKEY_free(pkey);
642 return ret;
643 }
644
645 #ifndef OPENSSL_NO_DES
test_pkcs8key_nid_bio(void)646 static int test_pkcs8key_nid_bio(void)
647 {
648 int ret;
649 const int nid = NID_pbe_WithSHA1And3_Key_TripleDES_CBC;
650 static const char pwd[] = "PASSWORD";
651 EVP_PKEY *pkey = NULL, *pkey_dec = NULL;
652 BIO *in = NULL, *enc_bio = NULL;
653 char *enc_data = NULL;
654 long enc_datalen = 0;
655 OSSL_PROVIDER *provider = NULL;
656
657 ret = TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
658 && TEST_ptr(enc_bio = BIO_new(BIO_s_mem()))
659 && TEST_ptr(in = BIO_new_mem_buf(kExampleRSAKeyPKCS8,
660 sizeof(kExampleRSAKeyPKCS8)))
661 && TEST_ptr(pkey = d2i_PrivateKey_ex_bio(in, NULL, NULL, NULL))
662 && TEST_int_eq(i2d_PKCS8PrivateKey_nid_bio(enc_bio, pkey, nid,
663 pwd, sizeof(pwd) - 1,
664 NULL, NULL), 1)
665 && TEST_int_gt(enc_datalen = BIO_get_mem_data(enc_bio, &enc_data), 0)
666 && TEST_ptr(pkey_dec = d2i_PKCS8PrivateKey_bio(enc_bio, NULL, NULL,
667 (void *)pwd))
668 && TEST_true(EVP_PKEY_eq(pkey, pkey_dec));
669
670 EVP_PKEY_free(pkey_dec);
671 EVP_PKEY_free(pkey);
672 BIO_free(in);
673 BIO_free(enc_bio);
674 OSSL_PROVIDER_unload(provider);
675 return ret;
676 }
677 #endif /* OPENSSL_NO_DES */
678
test_alternative_default(void)679 static int test_alternative_default(void)
680 {
681 OSSL_LIB_CTX *oldctx;
682 EVP_MD *sha256;
683 int ok = 0;
684
685 /*
686 * setup_tests() loaded the "null" provider in the current default, so
687 * we know this fetch should fail.
688 */
689 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
690 goto err;
691
692 /*
693 * Now we switch to our main library context, and try again. Since no
694 * providers are loaded in this one, it should fall back to the default.
695 */
696 if (!TEST_ptr(oldctx = OSSL_LIB_CTX_set0_default(mainctx))
697 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
698 goto err;
699 EVP_MD_free(sha256);
700 sha256 = NULL;
701
702 /*
703 * Switching back should give us our main library context back, and
704 * fetching SHA2-256 should fail again.
705 */
706 if (!TEST_ptr_eq(OSSL_LIB_CTX_set0_default(oldctx), mainctx)
707 || !TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
708 goto err;
709
710 ok = 1;
711 err:
712 EVP_MD_free(sha256);
713 return ok;
714 }
715
test_provider_unload_effective(int testid)716 static int test_provider_unload_effective(int testid)
717 {
718 EVP_MD *sha256 = NULL;
719 OSSL_PROVIDER *provider = NULL;
720 int ok = 0;
721
722 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default"))
723 || !TEST_ptr(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
724 goto err;
725
726 if (testid > 0) {
727 OSSL_PROVIDER_unload(provider);
728 provider = NULL;
729 EVP_MD_free(sha256);
730 sha256 = NULL;
731 } else {
732 EVP_MD_free(sha256);
733 sha256 = NULL;
734 OSSL_PROVIDER_unload(provider);
735 provider = NULL;
736 }
737
738 /*
739 * setup_tests() loaded the "null" provider in the current default, and
740 * we unloaded it above after the load so we know this fetch should fail.
741 */
742 if (!TEST_ptr_null(sha256 = EVP_MD_fetch(NULL, "SHA2-256", NULL)))
743 goto err;
744
745 ok = 1;
746 err:
747 EVP_MD_free(sha256);
748 OSSL_PROVIDER_unload(provider);
749 return ok;
750 }
751
test_d2i_PrivateKey_ex(int testid)752 static int test_d2i_PrivateKey_ex(int testid)
753 {
754 int ok = 0;
755 OSSL_PROVIDER *provider = NULL;
756 BIO *key_bio = NULL;
757 EVP_PKEY *pkey = NULL;
758 int id = (testid == 0) ? 0 : 2;
759
760 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
761 goto err;
762 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[id].kder, (int)keydata[id].size)))
763 goto err;
764 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
765 goto err;
766
767 ERR_clear_error();
768 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
769 goto err;
770 ok = TEST_ptr(pkey = d2i_PrivateKey_bio(key_bio, NULL));
771 TEST_int_eq(ERR_peek_error(), 0);
772 test_openssl_errors();
773
774 err:
775 EVP_PKEY_free(pkey);
776 BIO_free(key_bio);
777 OSSL_PROVIDER_unload(provider);
778
779 return ok;
780 }
781
test_PEM_read_bio_negative(int testid)782 static int test_PEM_read_bio_negative(int testid)
783 {
784 int ok = 0;
785 OSSL_PROVIDER *provider = NULL;
786 BIO *key_bio = NULL;
787 EVP_PKEY *pkey = NULL;
788
789 if (!TEST_ptr(key_bio = BIO_new_mem_buf(keydata[testid].kder,
790 (int)keydata[testid].size)))
791 goto err;
792 ERR_clear_error();
793 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
794 goto err;
795 if (!TEST_int_ne(ERR_peek_error(), 0))
796 goto err;
797 if (!TEST_ptr(provider = OSSL_PROVIDER_load(NULL, "default")))
798 goto err;
799 if (!TEST_int_ge(BIO_seek(key_bio, 0), 0))
800 goto err;
801 ERR_clear_error();
802 if (!TEST_ptr_null(pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL)))
803 goto err;
804 if (!TEST_int_ne(ERR_peek_error(), 0))
805 goto err;
806
807 ok = 1;
808
809 err:
810 test_openssl_errors();
811 EVP_PKEY_free(pkey);
812 BIO_free(key_bio);
813 OSSL_PROVIDER_unload(provider);
814
815 return ok;
816 }
817
test_PEM_read_bio_negative_wrong_password(int testid)818 static int test_PEM_read_bio_negative_wrong_password(int testid)
819 {
820 int ok = 0;
821 OSSL_PROVIDER *provider = OSSL_PROVIDER_load(NULL, "default");
822 EVP_PKEY *read_pkey = NULL;
823 EVP_PKEY *write_pkey = EVP_RSA_gen(1024);
824 BIO *key_bio = BIO_new(BIO_s_mem());
825 const UI_METHOD *undo_ui_method = NULL;
826 const UI_METHOD *ui_method = NULL;
827 if (testid > 0)
828 ui_method = UI_null();
829
830 if (!TEST_ptr(provider))
831 goto err;
832 if (!TEST_ptr(key_bio))
833 goto err;
834 if (!TEST_ptr(write_pkey))
835 goto err;
836 undo_ui_method = UI_get_default_method();
837 UI_set_default_method(ui_method);
838
839 if (/* Output Encrypted private key in PEM form */
840 !TEST_true(PEM_write_bio_PrivateKey(key_bio, write_pkey, EVP_aes_256_cbc(),
841 NULL, 0, NULL, "pass")))
842 goto err;
843
844 ERR_clear_error();
845 read_pkey = PEM_read_bio_PrivateKey(key_bio, NULL, NULL, NULL);
846 if (!TEST_ptr_null(read_pkey))
847 goto err;
848
849 if (!TEST_int_eq(ERR_GET_REASON(ERR_get_error()), PEM_R_PROBLEMS_GETTING_PASSWORD))
850 goto err;
851 ok = 1;
852
853 err:
854 test_openssl_errors();
855 EVP_PKEY_free(read_pkey);
856 EVP_PKEY_free(write_pkey);
857 BIO_free(key_bio);
858 OSSL_PROVIDER_unload(provider);
859 UI_set_default_method(undo_ui_method);
860
861 return ok;
862 }
863
do_fromdata_key_is_equal(const OSSL_PARAM params[],const EVP_PKEY * expected,const char * type)864 static int do_fromdata_key_is_equal(const OSSL_PARAM params[],
865 const EVP_PKEY *expected, const char *type)
866 {
867 EVP_PKEY_CTX *ctx = NULL;
868 EVP_PKEY *pkey = NULL;
869 int ret;
870
871 ret = TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, type, NULL))
872 && TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
873 && TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey,
874 EVP_PKEY_KEYPAIR,
875 (OSSL_PARAM *)params), 1)
876 && TEST_true(EVP_PKEY_eq(pkey, expected));
877 EVP_PKEY_CTX_free(ctx);
878 EVP_PKEY_free(pkey);
879 return ret;
880 }
881
882 #ifndef OPENSSL_NO_DSA
883 /*
884 * This data was generated using:
885 * > openssl genpkey \
886 * -genparam -algorithm DSA -pkeyopt type:fips186_4 -text \
887 * -pkeyopt gindex:5 -out dsa_param.pem
888 * > openssl genpkey \
889 * -paramfile dsa_param.pem -pkeyopt type:fips186_4 -out dsa_priv.pem
890 */
891 static const unsigned char dsa_key[] = {
892 0x30, 0x82, 0x03, 0x4e, 0x02, 0x01, 0x00, 0x02, 0x82, 0x01, 0x01, 0x00,
893 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66,
894 0x17, 0xda, 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21,
895 0x3d, 0x85, 0xa3, 0x4a, 0xf0, 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37,
896 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1, 0xf6, 0xc4, 0x42, 0x23,
897 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6, 0xe8,
898 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b,
899 0xa8, 0xad, 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21,
900 0x55, 0x22, 0xce, 0xa2, 0xe9, 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03,
901 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a, 0x07, 0x80, 0xfc, 0x4e,
902 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22, 0x55,
903 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2,
904 0x62, 0x12, 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec,
905 0x70, 0xce, 0x0f, 0x8c, 0xff, 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32,
906 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb, 0x28, 0xe2, 0xc1, 0x1a,
907 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa, 0x69,
908 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44,
909 0xfb, 0x9a, 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12,
910 0x79, 0xbd, 0xa0, 0x70, 0x47, 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07,
911 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f, 0x1a, 0xd1, 0xa2, 0x96,
912 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c, 0x40,
913 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c,
914 0x5d, 0xbe, 0xfd, 0x1b, 0x02, 0x1d, 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02,
915 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73, 0xf3, 0x7d, 0x31,
916 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31, 0x02,
917 0x82, 0x01, 0x01, 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5,
918 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c, 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47,
919 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3, 0xd4, 0xd2, 0xe3,
920 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
921 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c,
922 0x2a, 0x04, 0x7c, 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2,
923 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb, 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63,
924 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2, 0x5b, 0x40, 0x6d,
925 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
926 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50,
927 0xd0, 0x44, 0x57, 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80,
928 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a, 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37,
929 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28, 0x05, 0xb5, 0xd8,
930 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
931 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97,
932 0xb7, 0xeb, 0x67, 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9,
933 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1, 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12,
934 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e, 0x28, 0xa9, 0xfc,
935 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
936 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01,
937 0x56, 0xfb, 0x78, 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7,
938 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01, 0x9f, 0x34, 0x02, 0x82, 0x01, 0x00,
939 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30,
940 0xeb, 0x8e, 0x44, 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38,
941 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8, 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2,
942 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52, 0x50, 0x15, 0x9c,
943 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
944 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3,
945 0xd9, 0xd4, 0x66, 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f,
946 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b, 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e,
947 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27, 0xb7, 0x5f, 0x65,
948 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
949 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41,
950 0xaf, 0x88, 0x23, 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d,
951 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63, 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07,
952 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7, 0x86, 0x99, 0x29,
953 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
954 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee,
955 0x6b, 0x11, 0xdd, 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f,
956 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55, 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d,
957 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5, 0x47, 0xa9, 0xb5,
958 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
959 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf,
960 0x07, 0x48, 0x45, 0xfd, 0x02, 0x1c, 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b,
961 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3, 0x43, 0xb8, 0xa4,
962 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21,
963 };
964 static const unsigned char dsa_p[] = {
965 0x00, 0xda, 0xb3, 0x46, 0x4d, 0x54, 0x57, 0xc7, 0xb4, 0x61, 0xa0, 0x6f, 0x66, 0x17, 0xda,
966 0xeb, 0x90, 0xf0, 0xa3, 0xd1, 0x29, 0xc9, 0x5f, 0xf2, 0x21, 0x3d, 0x85, 0xa3, 0x4a, 0xf0,
967 0xf8, 0x36, 0x39, 0x1b, 0xe3, 0xee, 0x37, 0x70, 0x06, 0x9b, 0xe8, 0xe3, 0x0a, 0xd2, 0xf1,
968 0xf6, 0xc4, 0x42, 0x23, 0x1f, 0x74, 0x78, 0xc2, 0x16, 0xf5, 0xce, 0xd6, 0xab, 0xa0, 0xc6,
969 0xe8, 0x99, 0x3d, 0xf8, 0x8b, 0xfb, 0x47, 0xf8, 0x5e, 0x05, 0x68, 0x6d, 0x8b, 0xa8, 0xad,
970 0xa1, 0xc2, 0x3a, 0x4e, 0xe0, 0xad, 0xec, 0x38, 0x75, 0x21, 0x55, 0x22, 0xce, 0xa2, 0xe9,
971 0xe5, 0x3b, 0xd7, 0x44, 0xeb, 0x5a, 0x03, 0x59, 0xa0, 0xc5, 0x7a, 0x92, 0x59, 0x7d, 0x7a,
972 0x07, 0x80, 0xfc, 0x4e, 0xf8, 0x56, 0x7e, 0xf1, 0x06, 0xe0, 0xba, 0xb2, 0xe7, 0x5b, 0x22,
973 0x55, 0xee, 0x4b, 0x42, 0x61, 0x67, 0x2c, 0x43, 0x9a, 0x38, 0x2b, 0x17, 0xc2, 0x62, 0x12,
974 0x8b, 0x0b, 0x22, 0x8c, 0x0c, 0x1c, 0x1c, 0x92, 0xb1, 0xec, 0x70, 0xce, 0x0f, 0x8c, 0xff,
975 0x8d, 0x21, 0xf9, 0x19, 0x68, 0x4d, 0x32, 0x59, 0x78, 0x42, 0x1d, 0x0c, 0xc5, 0x1a, 0xcb,
976 0x28, 0xe2, 0xc1, 0x1a, 0x35, 0xf1, 0x42, 0x0a, 0x19, 0x39, 0xfa, 0x83, 0xd1, 0xb4, 0xaa,
977 0x69, 0x0f, 0xc2, 0x8e, 0xf9, 0x59, 0x2c, 0xee, 0x11, 0xfc, 0x3e, 0x4b, 0x44, 0xfb, 0x9a,
978 0x32, 0xc8, 0x78, 0x23, 0x56, 0x85, 0x49, 0x21, 0x43, 0x12, 0x79, 0xbd, 0xa0, 0x70, 0x47,
979 0x2f, 0xae, 0xb6, 0xd7, 0x6c, 0xc6, 0x07, 0x76, 0xa9, 0x8a, 0xa2, 0x16, 0x02, 0x89, 0x1f,
980 0x1a, 0xd1, 0xa2, 0x96, 0x56, 0xd1, 0x1f, 0x10, 0xe1, 0xe5, 0x9f, 0x3f, 0xdd, 0x09, 0x0c,
981 0x40, 0x90, 0x71, 0xef, 0x14, 0x41, 0x02, 0x82, 0x3a, 0x6b, 0xe1, 0xf8, 0x2c, 0x5d, 0xbe,
982 0xfd, 0x1b
983 };
984 static const unsigned char dsa_q[] = {
985 0x00, 0xe0, 0x20, 0xe0, 0x7c, 0x02, 0x16, 0xa7, 0x6c, 0x6a, 0x19, 0xba, 0xd5, 0x83, 0x73,
986 0xf3, 0x7d, 0x31, 0xef, 0xa7, 0xe1, 0x5d, 0x5b, 0x7f, 0xf3, 0xfc, 0xda, 0x84, 0x31
987 };
988 static const unsigned char dsa_g[] = {
989 0x00, 0x83, 0xdb, 0xa1, 0xbc, 0x3e, 0xc7, 0x29, 0xa5, 0x6a, 0x5c, 0x2c, 0xe8, 0x7a, 0x8c,
990 0x7e, 0xe8, 0xb8, 0x3e, 0x13, 0x47, 0xcd, 0x36, 0x7e, 0x79, 0x30, 0x7a, 0x28, 0x03, 0xd3,
991 0xd4, 0xd2, 0xe3, 0xee, 0x3b, 0x46, 0xda, 0xe0, 0x71, 0xe6, 0xcf, 0x46, 0x86, 0x0a, 0x37,
992 0x57, 0xb6, 0xe9, 0xcf, 0xa1, 0x78, 0x19, 0xb8, 0x72, 0x9f, 0x30, 0x8c, 0x2a, 0x04, 0x7c,
993 0x2f, 0x0c, 0x27, 0xa7, 0xb3, 0x23, 0xe0, 0x46, 0xf2, 0x75, 0x0c, 0x03, 0x4c, 0xad, 0xfb,
994 0xc1, 0xcb, 0x28, 0xcd, 0xa0, 0x63, 0xdb, 0x44, 0x88, 0xe0, 0xda, 0x6c, 0x5b, 0x89, 0xb2,
995 0x5b, 0x40, 0x6d, 0xeb, 0x78, 0x7a, 0xd5, 0xaf, 0x40, 0x52, 0x46, 0x63, 0x92, 0x13, 0x0d,
996 0xee, 0xee, 0xf9, 0x53, 0xca, 0x2d, 0x4e, 0x3b, 0x13, 0xd8, 0x0f, 0x50, 0xd0, 0x44, 0x57,
997 0x67, 0x0f, 0x45, 0x8f, 0x21, 0x30, 0x97, 0x9e, 0x80, 0xd9, 0xd0, 0x91, 0xb7, 0xc9, 0x5a,
998 0x69, 0xda, 0xeb, 0xd5, 0xea, 0x37, 0xf6, 0xb3, 0xbe, 0x1f, 0x24, 0xf1, 0x55, 0x14, 0x28,
999 0x05, 0xb5, 0xd8, 0x84, 0x0f, 0x62, 0x85, 0xaa, 0xec, 0x77, 0x64, 0xfd, 0x80, 0x7c, 0x41,
1000 0x00, 0x88, 0xa3, 0x79, 0x7d, 0x4f, 0x6f, 0xe3, 0x76, 0xf4, 0xb5, 0x97, 0xb7, 0xeb, 0x67,
1001 0x28, 0xba, 0x07, 0x1a, 0x59, 0x32, 0xc1, 0x53, 0xd9, 0x05, 0x6b, 0x63, 0x93, 0xce, 0xa1,
1002 0xd9, 0x7a, 0xb2, 0xff, 0x1c, 0x12, 0x0a, 0x9a, 0xe5, 0x51, 0x1e, 0xba, 0xfc, 0x95, 0x2e,
1003 0x28, 0xa9, 0xfc, 0x4c, 0xed, 0x7b, 0x05, 0xca, 0x67, 0xe0, 0x2d, 0xd7, 0x54, 0xb3, 0x05,
1004 0x1c, 0x23, 0x2b, 0x35, 0x2e, 0x19, 0x48, 0x59, 0x0e, 0x58, 0xa8, 0x01, 0x56, 0xfb, 0x78,
1005 0x90, 0xba, 0x08, 0x77, 0x94, 0x45, 0x05, 0x13, 0xc7, 0x6b, 0x96, 0xd2, 0xa3, 0xa6, 0x01,
1006 0x9f, 0x34
1007 };
1008 static const unsigned char dsa_priv[] = {
1009 0x2f, 0x68, 0x44, 0xcb, 0xfb, 0x6b, 0xcb, 0x8d, 0x02, 0x49, 0x7c, 0xee, 0xd2, 0xa6, 0xd3,
1010 0x43, 0xb8, 0xa4, 0x09, 0xb7, 0xc1, 0xd4, 0x4b, 0xc3, 0x66, 0xa7, 0xe0, 0x21
1011 };
1012 static const unsigned char dsa_pub[] = {
1013 0x16, 0x1a, 0xb4, 0x6d, 0x9f, 0x16, 0x6c, 0xcc, 0x91, 0x66, 0xfe, 0x30, 0xeb, 0x8e, 0x44,
1014 0xba, 0x2b, 0x7a, 0xc9, 0xa8, 0x95, 0xf2, 0xa6, 0x38, 0xd8, 0xaf, 0x3e, 0x91, 0x68, 0xe8,
1015 0x52, 0xf3, 0x97, 0x37, 0x70, 0xf2, 0x47, 0xa3, 0xf4, 0x62, 0x26, 0xf5, 0x3b, 0x71, 0x52,
1016 0x50, 0x15, 0x9c, 0x6d, 0xa6, 0x6d, 0x92, 0x4c, 0x48, 0x76, 0x31, 0x54, 0x48, 0xa5, 0x99,
1017 0x7a, 0xd4, 0x61, 0xf7, 0x21, 0x44, 0xe7, 0xd8, 0x82, 0xc3, 0x50, 0xd3, 0xd9, 0xd4, 0x66,
1018 0x20, 0xab, 0x70, 0x4c, 0x97, 0x9b, 0x8d, 0xac, 0x1f, 0x78, 0x27, 0x1e, 0x47, 0xf8, 0x3b,
1019 0xd1, 0x55, 0x73, 0xf3, 0xb4, 0x8e, 0x6d, 0x45, 0x40, 0x54, 0xc6, 0xd8, 0x95, 0x15, 0x27,
1020 0xb7, 0x5f, 0x65, 0xaa, 0xcb, 0x24, 0xc9, 0x49, 0x87, 0x32, 0xad, 0xcb, 0xf8, 0x35, 0x63,
1021 0x56, 0x72, 0x7c, 0x4e, 0x6c, 0xad, 0x5f, 0x26, 0x8c, 0xd2, 0x80, 0x41, 0xaf, 0x88, 0x23,
1022 0x20, 0x03, 0xa4, 0xd5, 0x3c, 0x53, 0x54, 0xb0, 0x3d, 0xed, 0x0e, 0x9e, 0x53, 0x0a, 0x63,
1023 0x5f, 0xfd, 0x28, 0x57, 0x09, 0x07, 0x73, 0xf4, 0x0c, 0xd4, 0x71, 0x5d, 0x6b, 0xa0, 0xd7,
1024 0x86, 0x99, 0x29, 0x9b, 0xca, 0xfb, 0xcc, 0xd6, 0x2f, 0xfe, 0xbe, 0x94, 0xef, 0x1a, 0x0e,
1025 0x55, 0x84, 0xa7, 0xaf, 0x7b, 0xfa, 0xed, 0x77, 0x61, 0x28, 0x22, 0xee, 0x6b, 0x11, 0xdd,
1026 0xb0, 0x17, 0x1e, 0x06, 0xe4, 0x29, 0x4c, 0xc2, 0x3f, 0xd6, 0x75, 0xb6, 0x08, 0x04, 0x55,
1027 0x13, 0x48, 0x4f, 0x44, 0xea, 0x8d, 0xaf, 0xcb, 0xac, 0x22, 0xc4, 0x6a, 0xb3, 0x86, 0xe5,
1028 0x47, 0xa9, 0xb5, 0x72, 0x17, 0x23, 0x11, 0x81, 0x7f, 0x00, 0x00, 0x67, 0x5c, 0xf4, 0x58,
1029 0xcc, 0xe2, 0x46, 0xce, 0xf5, 0x6d, 0xd8, 0x18, 0x91, 0xc4, 0x20, 0xbf, 0x07, 0x48, 0x45,
1030 0xfd
1031 };
1032
do_check_params(OSSL_PARAM key_params[],int expected)1033 static int do_check_params(OSSL_PARAM key_params[], int expected)
1034 {
1035 EVP_PKEY_CTX *gen_ctx = NULL, *check_ctx = NULL;
1036 EVP_PKEY *pkey = NULL;
1037 int ret;
1038
1039 ret = TEST_ptr(gen_ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
1040 && TEST_int_eq(EVP_PKEY_fromdata_init(gen_ctx), 1)
1041 && TEST_int_eq(EVP_PKEY_fromdata(gen_ctx, &pkey,
1042 EVP_PKEY_KEYPAIR, key_params), 1)
1043 && TEST_ptr(check_ctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey,
1044 NULL))
1045 && TEST_int_eq(EVP_PKEY_param_check(check_ctx), expected);
1046 EVP_PKEY_CTX_free(check_ctx);
1047 EVP_PKEY_CTX_free(gen_ctx);
1048 EVP_PKEY_free(pkey);
1049 return ret;
1050 }
1051
do_check_bn(OSSL_PARAM params[],const char * key,const unsigned char * expected,size_t expected_len)1052 static int do_check_bn(OSSL_PARAM params[], const char *key,
1053 const unsigned char *expected, size_t expected_len)
1054 {
1055 OSSL_PARAM *p;
1056 BIGNUM *bn = NULL;
1057 unsigned char buffer[256 + 1];
1058 int ret, len;
1059
1060 ret = TEST_ptr(p = OSSL_PARAM_locate(params, key))
1061 && TEST_true(OSSL_PARAM_get_BN(p, &bn))
1062 && TEST_int_gt(len = BN_bn2binpad(bn, buffer, (int)expected_len), 0)
1063 && TEST_mem_eq(expected, expected_len, buffer, len);
1064 BN_free(bn);
1065 return ret;
1066 }
1067
do_check_int(OSSL_PARAM params[],const char * key,int expected)1068 static int do_check_int(OSSL_PARAM params[], const char *key, int expected)
1069 {
1070 OSSL_PARAM *p;
1071 int val = 0;
1072
1073 return TEST_ptr(p = OSSL_PARAM_locate(params, key))
1074 && TEST_true(OSSL_PARAM_get_int(p, &val))
1075 && TEST_int_eq(val, expected);
1076 }
1077
test_dsa_tofrom_data_select(void)1078 static int test_dsa_tofrom_data_select(void)
1079 {
1080 int ret;
1081 EVP_PKEY *key = NULL;
1082 const unsigned char *pkeydata = dsa_key;
1083
1084 ret = TEST_ptr(key = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
1085 mainctx, NULL))
1086 && TEST_true(do_pkey_tofrom_data_select(key, "DSA"));
1087
1088 EVP_PKEY_free(key);
1089 return ret;
1090 }
1091
test_dsa_todata(void)1092 static int test_dsa_todata(void)
1093 {
1094 EVP_PKEY *pkey = NULL;
1095 OSSL_PARAM *to_params = NULL, *all_params = NULL;
1096 OSSL_PARAM gen_params[4];
1097 int ret = 0;
1098 const unsigned char *pkeydata = dsa_key;
1099
1100 unsigned char dsa_seed[] = {
1101 0xbc, 0x8a, 0x81, 0x64, 0x9e, 0x9d, 0x63, 0xa7, 0xa3, 0x5d, 0x87, 0xdd,
1102 0x32, 0xf3, 0xc1, 0x9f, 0x18, 0x22, 0xeb, 0x73, 0x63, 0xad, 0x5e, 0x7b,
1103 0x90, 0xc1, 0xe3, 0xe0
1104 };
1105 int dsa_pcounter = 319;
1106 int dsa_gindex = 5;
1107
1108 gen_params[0] = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_FFC_SEED,
1109 (void*)dsa_seed,
1110 sizeof(dsa_seed));
1111 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
1112 &dsa_gindex);
1113 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
1114 &dsa_pcounter);
1115 gen_params[3] = OSSL_PARAM_construct_end();
1116
1117 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pkeydata, sizeof(dsa_key),
1118 mainctx, NULL))
1119 || !TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, &to_params), 1)
1120 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_P, dsa_p, sizeof(dsa_p))
1121 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_Q, dsa_q, sizeof(dsa_q))
1122 || !do_check_bn(to_params, OSSL_PKEY_PARAM_FFC_G, dsa_g, sizeof(dsa_g))
1123 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PUB_KEY, dsa_pub,
1124 sizeof(dsa_pub))
1125 || !do_check_bn(to_params, OSSL_PKEY_PARAM_PRIV_KEY, dsa_priv,
1126 sizeof(dsa_priv))
1127 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_GINDEX, -1)
1128 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_PCOUNTER, -1)
1129 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_H, 0)
1130 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_PQ, 1)
1131 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_G, 1)
1132 || !do_check_int(to_params, OSSL_PKEY_PARAM_FFC_VALIDATE_LEGACY, 0)
1133 || !TEST_ptr_null(OSSL_PARAM_locate(to_params, OSSL_PKEY_PARAM_FFC_SEED)))
1134 goto err;
1135
1136 if (!do_fromdata_key_is_equal(to_params, pkey, "DSA"))
1137 goto err;
1138
1139 if (!TEST_ptr(all_params = OSSL_PARAM_merge(to_params, gen_params))
1140 || !do_check_params(all_params, 1))
1141 goto err;
1142 gen_params[1] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_GINDEX,
1143 &dsa_gindex);
1144 gen_params[2] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_FFC_PCOUNTER,
1145 &dsa_pcounter);
1146 /*
1147 * Check that modifying the shallow copy values used in OSSL_PARAM_merge()
1148 * results in an invalid key. This also verifies that the fips186-4
1149 * validation code is running.
1150 */
1151 dsa_gindex++;
1152 if (!do_check_params(all_params, 0))
1153 goto err;
1154 dsa_gindex--;
1155 dsa_pcounter++;
1156 if (!do_check_params(all_params, 0))
1157 goto err;
1158 dsa_pcounter--;
1159 dsa_seed[0] = 0xb0;
1160 if (!do_check_params(all_params, 0))
1161 goto err;
1162
1163 ret = 1;
1164 err:
1165 EVP_PKEY_free(pkey);
1166 OSSL_PARAM_free(all_params);
1167 OSSL_PARAM_free(to_params);
1168 return ret;
1169 }
1170
1171 /*
1172 * Test that OSSL_PKEY_PARAM_FFC_DIGEST_PROPS is set properly when using fromdata
1173 * This test:
1174 * checks for failure when the property query is bad (tstid == 0)
1175 * checks for success when the property query is valid (tstid == 1)
1176 */
test_dsa_fromdata_digest_prop(int tstid)1177 static int test_dsa_fromdata_digest_prop(int tstid)
1178 {
1179 EVP_PKEY_CTX *ctx = NULL, *gctx = NULL;
1180 EVP_PKEY *pkey = NULL, *pkey2 = NULL;
1181 OSSL_PARAM params[4], *p = params;
1182 int ret = 0;
1183 int expected = (tstid == 0 ? 0 : 1);
1184 unsigned int pbits = 512; /* minimum allowed for speed */
1185
1186 *p++ = OSSL_PARAM_construct_uint(OSSL_PKEY_PARAM_FFC_PBITS, &pbits);
1187 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST, "SHA512", 0);
1188 /* Setting a bad prop query here should fail during paramgen - when it tries to do a fetch */
1189 *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_FFC_DIGEST_PROPS,
1190 tstid == 0 ? "provider=unknown" : "provider=default", 0);
1191 *p++ = OSSL_PARAM_construct_end();
1192
1193 if (!TEST_ptr(ctx = EVP_PKEY_CTX_new_from_name(mainctx, "DSA", NULL))
1194 || !TEST_int_eq(EVP_PKEY_fromdata_init(ctx), 1)
1195 || !TEST_int_eq(EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEY_PARAMETERS, params), 1))
1196 goto err;
1197
1198 if (!TEST_ptr(gctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1199 || !TEST_int_eq(EVP_PKEY_paramgen_init(gctx), 1)
1200 || !TEST_int_eq(EVP_PKEY_paramgen(gctx, &pkey2), expected))
1201 goto err;
1202
1203 ret = 1;
1204 err:
1205 EVP_PKEY_free(pkey2);
1206 EVP_PKEY_free(pkey);
1207 EVP_PKEY_CTX_free(ctx);
1208 EVP_PKEY_CTX_free(gctx);
1209 return ret;
1210 }
1211 #endif /* OPENSSL_NO_DSA */
1212
test_pkey_todata_null(void)1213 static int test_pkey_todata_null(void)
1214 {
1215 OSSL_PARAM *params = NULL;
1216 EVP_PKEY *pkey = NULL;
1217 int ret = 0;
1218 const unsigned char *pdata = keydata[0].kder;
1219
1220 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, (long)keydata[0].size,
1221 mainctx, NULL))
1222 && TEST_int_eq(EVP_PKEY_todata(NULL, EVP_PKEY_KEYPAIR, ¶ms), 0)
1223 && TEST_int_eq(EVP_PKEY_todata(pkey, EVP_PKEY_KEYPAIR, NULL), 0);
1224 EVP_PKEY_free(pkey);
1225 return ret;
1226 }
1227
1228 static OSSL_CALLBACK test_pkey_export_cb;
1229
test_pkey_export_cb(const OSSL_PARAM params[],void * arg)1230 static int test_pkey_export_cb(const OSSL_PARAM params[], void *arg)
1231 {
1232 if (arg == NULL)
1233 return 0;
1234 return do_fromdata_key_is_equal(params, (EVP_PKEY *)arg, "RSA");
1235 }
1236
test_pkey_export_null(void)1237 static int test_pkey_export_null(void)
1238 {
1239 EVP_PKEY *pkey = NULL;
1240 int ret = 0;
1241 const unsigned char *pdata = keydata[0].kder;
1242
1243 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, (long)keydata[0].size,
1244 mainctx, NULL))
1245 && TEST_int_eq(EVP_PKEY_export(NULL, EVP_PKEY_KEYPAIR,
1246 test_pkey_export_cb, NULL), 0)
1247 && TEST_int_eq(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR, NULL, NULL), 0);
1248 EVP_PKEY_free(pkey);
1249 return ret;
1250 }
1251
test_pkey_export(void)1252 static int test_pkey_export(void)
1253 {
1254 EVP_PKEY *pkey = NULL;
1255 #ifndef OPENSSL_NO_DEPRECATED_3_0
1256 RSA *rsa = NULL;
1257 #endif
1258 int ret = 1;
1259 const unsigned char *pdata = keydata[0].kder;
1260 int pdata_len = (int)keydata[0].size;
1261
1262 if (!TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, pdata_len,
1263 mainctx, NULL))
1264 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1265 test_pkey_export_cb, pkey))
1266 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1267 test_pkey_export_cb, NULL)))
1268 ret = 0;
1269 EVP_PKEY_free(pkey);
1270
1271 #ifndef OPENSSL_NO_DEPRECATED_3_0
1272 /* Now, try with a legacy key */
1273 pdata = keydata[0].kder;
1274 pdata_len = (int)keydata[0].size;
1275 if (!TEST_ptr(pkey = EVP_PKEY_new())
1276 || !TEST_ptr(rsa = d2i_RSAPrivateKey(NULL, &pdata, pdata_len))
1277 || !TEST_true(EVP_PKEY_set1_RSA(pkey, rsa))
1278 || !TEST_true(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1279 test_pkey_export_cb, pkey))
1280 || !TEST_false(EVP_PKEY_export(pkey, EVP_PKEY_KEYPAIR,
1281 test_pkey_export_cb, NULL)))
1282 ret = 0;
1283 RSA_free(rsa);
1284 EVP_PKEY_free(pkey);
1285 #endif
1286 return ret;
1287 }
1288
test_rsa_pss_sign(void)1289 static int test_rsa_pss_sign(void)
1290 {
1291 EVP_PKEY *pkey = NULL;
1292 EVP_PKEY_CTX *pctx = NULL;
1293 int ret = 0;
1294 const unsigned char *pdata = keydata[0].kder;
1295 const char *mdname = "SHA2-256";
1296 OSSL_PARAM sig_params[3];
1297 unsigned char mdbuf[256 / 8] = { 0 };
1298 int padding = RSA_PKCS1_PSS_PADDING;
1299 unsigned char *sig = NULL;
1300 size_t sig_len = 0;
1301
1302 sig_params[0] = OSSL_PARAM_construct_int(OSSL_PKEY_PARAM_PAD_MODE,
1303 &padding);
1304 sig_params[1] = OSSL_PARAM_construct_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST,
1305 (char *)mdname, 0);
1306 sig_params[2] = OSSL_PARAM_construct_end();
1307
1308 ret = TEST_ptr(pkey = d2i_AutoPrivateKey_ex(NULL, &pdata, (long)keydata[0].size,
1309 mainctx, NULL))
1310 && TEST_ptr(pctx = EVP_PKEY_CTX_new_from_pkey(mainctx, pkey, NULL))
1311 && TEST_int_gt(EVP_PKEY_sign_init_ex(pctx, sig_params), 0)
1312 && TEST_int_gt(EVP_PKEY_sign(pctx, NULL, &sig_len, mdbuf,
1313 sizeof(mdbuf)), 0)
1314 && TEST_size_t_gt(sig_len, 0)
1315 && TEST_ptr(sig = OPENSSL_malloc(sig_len))
1316 && TEST_int_gt(EVP_PKEY_sign(pctx, sig, &sig_len, mdbuf,
1317 sizeof(mdbuf)), 0);
1318
1319 EVP_PKEY_CTX_free(pctx);
1320 OPENSSL_free(sig);
1321 EVP_PKEY_free(pkey);
1322
1323 return ret;
1324 }
1325
test_evp_md_ctx_dup(void)1326 static int test_evp_md_ctx_dup(void)
1327 {
1328 EVP_MD_CTX *mdctx;
1329 EVP_MD_CTX *copyctx = NULL;
1330 int ret;
1331
1332 /* test copying freshly initialized context */
1333 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1334 && TEST_ptr(copyctx = EVP_MD_CTX_dup(mdctx));
1335
1336 EVP_MD_CTX_free(mdctx);
1337 EVP_MD_CTX_free(copyctx);
1338 return ret;
1339 }
1340
test_evp_md_ctx_copy(void)1341 static int test_evp_md_ctx_copy(void)
1342 {
1343 EVP_MD_CTX *mdctx = NULL;
1344 EVP_MD_CTX *copyctx = NULL;
1345 int ret;
1346
1347 /* test copying freshly initialized context */
1348 ret = TEST_ptr(mdctx = EVP_MD_CTX_new())
1349 && TEST_ptr(copyctx = EVP_MD_CTX_new())
1350 && TEST_true(EVP_MD_CTX_copy_ex(copyctx, mdctx));
1351
1352 EVP_MD_CTX_free(mdctx);
1353 EVP_MD_CTX_free(copyctx);
1354 return ret;
1355 }
1356
test_evp_md_ctx_copy2(void)1357 static int test_evp_md_ctx_copy2(void)
1358 {
1359 int ret = 0;
1360 EVP_MD *md = NULL;
1361 OSSL_LIB_CTX *ctx = NULL;
1362 EVP_MD_CTX *inctx = NULL, *outctx = NULL;
1363 void *origin_algctx = NULL;
1364
1365 if (!TEST_ptr(ctx = OSSL_LIB_CTX_new())
1366 || !TEST_ptr(md = EVP_MD_fetch(ctx, "sha256", NULL)))
1367 goto end;
1368
1369 inctx = EVP_MD_CTX_new();
1370 outctx = EVP_MD_CTX_new();
1371
1372 if (!TEST_ptr(inctx) || !TEST_ptr(outctx))
1373 goto end;
1374
1375 /* init inctx and outctx, now the contexts are from same providers */
1376 if (!TEST_true(EVP_DigestInit_ex2(inctx, md, NULL)))
1377 goto end;
1378 if (!TEST_true(EVP_DigestInit_ex2(outctx, md, NULL)))
1379 goto end;
1380
1381 /*
1382 * Test the EVP_MD_CTX_copy_ex function. After copying,
1383 * outctx->algctx should be the same as the original.
1384 */
1385 origin_algctx = outctx->algctx;
1386 ret = TEST_true(EVP_MD_CTX_copy_ex(outctx, inctx))
1387 && TEST_true(outctx->algctx == origin_algctx);
1388
1389 end:
1390 EVP_MD_free(md);
1391 EVP_MD_CTX_free(inctx);
1392 EVP_MD_CTX_free(outctx);
1393 OSSL_LIB_CTX_free(ctx);
1394 return ret;
1395 }
1396
1397 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
test_evp_pbe_alg_add(void)1398 static int test_evp_pbe_alg_add(void)
1399 {
1400 int ret = 0;
1401 int cipher_nid = 0, md_nid = 0;
1402 EVP_PBE_KEYGEN_EX *keygen_ex = NULL;
1403 EVP_PBE_KEYGEN *keygen = NULL;
1404
1405 if (!TEST_true(EVP_PBE_alg_add(NID_pbeWithMD5AndDES_CBC, EVP_des_cbc(), EVP_md5(),
1406 PKCS5_PBE_keyivgen)))
1407 goto err;
1408
1409 if (!TEST_true(EVP_PBE_find_ex(EVP_PBE_TYPE_OUTER, NID_pbeWithMD5AndDES_CBC,
1410 &cipher_nid, &md_nid, &keygen, &keygen_ex)))
1411 goto err;
1412
1413 if (!TEST_true(keygen != NULL))
1414 goto err;
1415 if (!TEST_true(keygen_ex == NULL))
1416 goto err;
1417
1418 ret = 1;
1419
1420 err:
1421 return ret;
1422 }
1423 #endif
1424
1425 /*
1426 * Currently, EVP_<OBJ>_fetch doesn't support
1427 * colon separated alternative names for lookup
1428 * so add a test here to ensure that when one is provided
1429 * libcrypto returns an error
1430 */
evp_test_name_parsing(void)1431 static int evp_test_name_parsing(void)
1432 {
1433 EVP_MD *md;
1434
1435 if (!TEST_ptr_null(md = EVP_MD_fetch(mainctx, "SHA256:BogusName", NULL))) {
1436 EVP_MD_free(md);
1437 return 0;
1438 }
1439
1440 return 1;
1441 }
1442
setup_tests(void)1443 int setup_tests(void)
1444 {
1445 if (!test_get_libctx(&mainctx, &nullprov, NULL, NULL, NULL)) {
1446 OSSL_LIB_CTX_free(mainctx);
1447 mainctx = NULL;
1448 return 0;
1449 }
1450
1451 ADD_TEST(evp_test_name_parsing);
1452 ADD_TEST(test_alternative_default);
1453 ADD_ALL_TESTS(test_d2i_AutoPrivateKey_ex, OSSL_NELEM(keydata));
1454 ADD_TEST(test_new_keytype);
1455 #ifndef OPENSSL_NO_EC
1456 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 2);
1457 ADD_TEST(test_ec_tofrom_data_select);
1458 # ifndef OPENSSL_NO_ECX
1459 ADD_TEST(test_ecx_tofrom_data_select);
1460 # endif
1461 ADD_TEST(test_ec_d2i_i2d_pubkey);
1462 #else
1463 ADD_ALL_TESTS(test_d2i_PrivateKey_ex, 1);
1464 #endif
1465 #ifndef OPENSSL_NO_SM2
1466 ADD_TEST(test_sm2_tofrom_data_select);
1467 #endif
1468 #ifndef OPENSSL_NO_DSA
1469 ADD_TEST(test_dsa_todata);
1470 ADD_TEST(test_dsa_tofrom_data_select);
1471 ADD_ALL_TESTS(test_dsa_fromdata_digest_prop, 2);
1472 #endif
1473 #ifndef OPENSSL_NO_DH
1474 ADD_TEST(test_dh_tofrom_data_select);
1475 ADD_TEST(test_dh_paramgen);
1476 ADD_TEST(test_dh_paramfromdata);
1477 #endif
1478 ADD_TEST(test_rsa_tofrom_data_select);
1479
1480 ADD_TEST(test_pkey_todata_null);
1481 ADD_TEST(test_pkey_export_null);
1482 ADD_TEST(test_pkey_export);
1483 #ifndef OPENSSL_NO_DES
1484 ADD_TEST(test_pkcs8key_nid_bio);
1485 #endif
1486 ADD_ALL_TESTS(test_PEM_read_bio_negative, OSSL_NELEM(keydata));
1487 ADD_ALL_TESTS(test_PEM_read_bio_negative_wrong_password, 2);
1488 ADD_TEST(test_rsa_pss_sign);
1489 ADD_TEST(test_evp_md_ctx_dup);
1490 ADD_TEST(test_evp_md_ctx_copy);
1491 ADD_TEST(test_evp_md_ctx_copy2);
1492 ADD_ALL_TESTS(test_provider_unload_effective, 2);
1493 #if !defined OPENSSL_NO_DES && !defined OPENSSL_NO_MD5
1494 ADD_TEST(test_evp_pbe_alg_add);
1495 #endif
1496 return 1;
1497 }
1498
cleanup_tests(void)1499 void cleanup_tests(void)
1500 {
1501 OSSL_LIB_CTX_free(mainctx);
1502 OSSL_PROVIDER_unload(nullprov);
1503 }
1504