]>
Commit | Line | Data |
---|---|---|
5a23d78c | 1 | /* |
a28d06f3 | 2 | * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved. |
5a23d78c RL |
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 | ||
3ecbea6a | 10 | #include <string.h> |
ae12eac0 | 11 | #include <openssl/core_dispatch.h> |
5a23d78c RL |
12 | #include <openssl/evp.h> |
13 | #include <openssl/pem.h> | |
14 | #include <openssl/rsa.h> | |
15 | #include <openssl/x509.h> | |
c0f39ded | 16 | #include <openssl/core_names.h> |
7c664b1f | 17 | #include <openssl/params.h> |
c0f39ded | 18 | #include <openssl/param_build.h> |
ece9304c RL |
19 | #include <openssl/encoder.h> |
20 | #include <openssl/decoder.h> | |
5a23d78c | 21 | |
e2ac846e | 22 | #include "internal/cryptlib.h" /* ossl_assert */ |
0934cf48 | 23 | #include "crypto/pem.h" /* For PVK and "blob" PEM headers */ |
4f0831b8 | 24 | #include "crypto/evp.h" /* For evp_pkey_is_provided() */ |
e2ac846e | 25 | |
20f8bc72 | 26 | #include "helpers/predefined_dhparams.h" |
5a23d78c RL |
27 | #include "testutil.h" |
28 | ||
6511f686 JS |
29 | /* Extended test macros to allow passing file & line number */ |
30 | #define TEST_FL_ptr(a) test_ptr(file, line, #a, a) | |
31 | #define TEST_FL_mem_eq(a, m, b, n) test_mem_eq(file, line, #a, #b, a, m, b, n) | |
32 | #define TEST_FL_strn_eq(a, b, n) test_strn_eq(file, line, #a, #b, a, n, b, n) | |
33 | #define TEST_FL_strn2_eq(a, m, b, n) test_strn_eq(file, line, #a, #b, a, m, b, n) | |
34 | #define TEST_FL_int_eq(a, b) test_int_eq(file, line, #a, #b, a, b) | |
35 | #define TEST_FL_int_ge(a, b) test_int_ge(file, line, #a, #b, a, b) | |
36 | #define TEST_FL_int_gt(a, b) test_int_gt(file, line, #a, #b, a, b) | |
37 | #define TEST_FL_long_gt(a, b) test_long_gt(file, line, #a, #b, a, b) | |
38 | #define TEST_FL_true(a) test_true(file, line, #a, (a) != 0) | |
39 | ||
a2e145f8 RL |
40 | #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC) |
41 | # define OPENSSL_NO_KEYPARAMS | |
42 | #endif | |
43 | ||
c0f39ded SL |
44 | #ifndef OPENSSL_NO_EC |
45 | static BN_CTX *bnctx = NULL; | |
46 | static OSSL_PARAM_BLD *bld_prime_nc = NULL; | |
47 | static OSSL_PARAM_BLD *bld_prime = NULL; | |
48 | static OSSL_PARAM *ec_explicit_prime_params_nc = NULL; | |
49 | static OSSL_PARAM *ec_explicit_prime_params_explicit = NULL; | |
50 | ||
51 | # ifndef OPENSSL_NO_EC2M | |
52 | static OSSL_PARAM_BLD *bld_tri_nc = NULL; | |
53 | static OSSL_PARAM_BLD *bld_tri = NULL; | |
54 | static OSSL_PARAM *ec_explicit_tri_params_nc = NULL; | |
55 | static OSSL_PARAM *ec_explicit_tri_params_explicit = NULL; | |
56 | # endif | |
57 | #endif | |
58 | ||
a2e145f8 | 59 | #ifndef OPENSSL_NO_KEYPARAMS |
7c664b1f RL |
60 | static EVP_PKEY *make_template(const char *type, OSSL_PARAM *genparams) |
61 | { | |
62 | EVP_PKEY *pkey = NULL; | |
5658470c DDO |
63 | EVP_PKEY_CTX *ctx = NULL; |
64 | ||
821d6f8c | 65 | # ifndef OPENSSL_NO_DH |
1234aa7e DDO |
66 | /* |
67 | * Use 512-bit DH(X) keys with predetermined parameters for efficiency, | |
68 | * for testing only. Use a minimum key size of 2048 for security purposes. | |
69 | */ | |
5658470c DDO |
70 | if (strcmp(type, "DH") == 0) |
71 | return get_dh512(NULL); | |
72 | if (strcmp(type, "X9.42 DH") == 0) | |
73 | return get_dhx512(NULL); | |
821d6f8c | 74 | # endif |
7c664b1f RL |
75 | |
76 | /* | |
77 | * No real need to check the errors other than for the cascade | |
78 | * effect. |pkey| will simply remain NULL if something goes wrong. | |
79 | */ | |
5658470c | 80 | (void)((ctx = EVP_PKEY_CTX_new_from_name(NULL, type, NULL)) != NULL |
7c664b1f RL |
81 | && EVP_PKEY_paramgen_init(ctx) > 0 |
82 | && (genparams == NULL | |
83 | || EVP_PKEY_CTX_set_params(ctx, genparams) > 0) | |
84 | && EVP_PKEY_gen(ctx, &pkey) > 0); | |
85 | EVP_PKEY_CTX_free(ctx); | |
86 | ||
87 | return pkey; | |
88 | } | |
821d6f8c | 89 | #endif |
5a23d78c | 90 | |
7c664b1f | 91 | static EVP_PKEY *make_key(const char *type, EVP_PKEY *template, |
1251cddf | 92 | OSSL_PARAM *genparams) |
5a23d78c RL |
93 | { |
94 | EVP_PKEY *pkey = NULL; | |
7c664b1f RL |
95 | EVP_PKEY_CTX *ctx = |
96 | template != NULL | |
97 | ? EVP_PKEY_CTX_new(template, NULL) | |
98 | : EVP_PKEY_CTX_new_from_name(NULL, type, NULL); | |
5a23d78c RL |
99 | |
100 | /* | |
101 | * No real need to check the errors other than for the cascade | |
7c664b1f | 102 | * effect. |pkey| will simply remain NULL if something goes wrong. |
5a23d78c RL |
103 | */ |
104 | (void)(ctx != NULL | |
105 | && EVP_PKEY_keygen_init(ctx) > 0 | |
7c664b1f RL |
106 | && (genparams == NULL |
107 | || EVP_PKEY_CTX_set_params(ctx, genparams) > 0) | |
5a23d78c RL |
108 | && EVP_PKEY_keygen(ctx, &pkey) > 0); |
109 | EVP_PKEY_CTX_free(ctx); | |
5a23d78c RL |
110 | return pkey; |
111 | } | |
112 | ||
113 | /* Main test driver */ | |
114 | ||
6511f686 JS |
115 | typedef int (encoder)(const char *file, const int line, |
116 | void **encoded, long *encoded_len, | |
973a52ce RL |
117 | void *object, int selection, |
118 | const char *output_type, const char *output_structure, | |
ae12eac0 | 119 | const char *pass, const char *pcipher); |
6511f686 JS |
120 | typedef int (decoder)(const char *file, const int line, |
121 | void **object, void *encoded, long encoded_len, | |
ff1c10d9 RL |
122 | const char *input_type, const char *structure_type, |
123 | const char *keytype, int selection, const char *pass); | |
6511f686 JS |
124 | typedef int (tester)(const char *file, const int line, |
125 | const void *data1, size_t data1_len, | |
319d0b2b | 126 | const void *data2, size_t data2_len); |
6511f686 JS |
127 | typedef int (checker)(const char *file, const int line, |
128 | const char *type, const void *data, size_t data_len); | |
5a23d78c RL |
129 | typedef void (dumper)(const char *label, const void *data, size_t data_len); |
130 | ||
b565a17d MC |
131 | #define FLAG_DECODE_WITH_TYPE 0x0001 |
132 | ||
6511f686 JS |
133 | static int test_encode_decode(const char *file, const int line, |
134 | const char *type, EVP_PKEY *pkey, | |
973a52ce RL |
135 | int selection, const char *output_type, |
136 | const char *output_structure, | |
1251cddf RL |
137 | const char *pass, const char *pcipher, |
138 | encoder *encode_cb, decoder *decode_cb, | |
139 | tester *test_cb, checker *check_cb, | |
b565a17d | 140 | dumper *dump_cb, int flags) |
5a23d78c | 141 | { |
ece9304c RL |
142 | void *encoded = NULL; |
143 | long encoded_len = 0; | |
5a23d78c | 144 | EVP_PKEY *pkey2 = NULL; |
ece9304c RL |
145 | void *encoded2 = NULL; |
146 | long encoded2_len = 0; | |
5a23d78c RL |
147 | int ok = 0; |
148 | ||
ae12eac0 RL |
149 | /* |
150 | * Encode |pkey|, decode the result into |pkey2|, and finish off by | |
151 | * encoding |pkey2| as well. That last encoding is for checking and | |
152 | * dumping purposes. | |
153 | */ | |
6511f686 | 154 | if (!TEST_true(encode_cb(file, line, &encoded, &encoded_len, pkey, selection, |
973a52ce | 155 | output_type, output_structure, pass, pcipher)) |
6511f686 JS |
156 | || !TEST_true(check_cb(file, line, type, encoded, encoded_len)) |
157 | || !TEST_true(decode_cb(file, line, (void **)&pkey2, encoded, encoded_len, | |
ff1c10d9 | 158 | output_type, output_structure, |
b565a17d | 159 | (flags & FLAG_DECODE_WITH_TYPE ? type : NULL), |
ff1c10d9 | 160 | selection, pass)) |
6511f686 | 161 | || !TEST_true(encode_cb(file, line, &encoded2, &encoded2_len, pkey2, selection, |
973a52ce | 162 | output_type, output_structure, pass, pcipher))) |
ae12eac0 RL |
163 | goto end; |
164 | ||
b565a17d MC |
165 | if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) { |
166 | if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey2), 1)) | |
167 | goto end; | |
168 | } else { | |
169 | if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1)) | |
170 | goto end; | |
171 | } | |
5a23d78c RL |
172 | |
173 | /* | |
ece9304c | 174 | * Double check the encoding, but only for unprotected keys, |
3ecbea6a RL |
175 | * as protected keys have a random component, which makes the output |
176 | * differ. | |
5a23d78c | 177 | */ |
3ecbea6a | 178 | if ((pass == NULL && pcipher == NULL) |
6511f686 | 179 | && !test_cb(file, line, encoded, encoded_len, encoded2, encoded2_len)) |
5a23d78c RL |
180 | goto end; |
181 | ||
182 | ok = 1; | |
183 | end: | |
319d0b2b | 184 | if (!ok) { |
ece9304c | 185 | if (encoded != NULL && encoded_len != 0) |
ae12eac0 | 186 | dump_cb("|pkey| encoded", encoded, encoded_len); |
ece9304c | 187 | if (encoded2 != NULL && encoded2_len != 0) |
ae12eac0 | 188 | dump_cb("|pkey2| encoded", encoded2, encoded2_len); |
319d0b2b | 189 | } |
5a23d78c | 190 | |
ece9304c RL |
191 | OPENSSL_free(encoded); |
192 | OPENSSL_free(encoded2); | |
5a23d78c RL |
193 | EVP_PKEY_free(pkey2); |
194 | return ok; | |
195 | } | |
196 | ||
973a52ce | 197 | /* Encoding and decoding methods */ |
5a23d78c | 198 | |
6511f686 JS |
199 | static int encode_EVP_PKEY_prov(const char *file, const int line, |
200 | void **encoded, long *encoded_len, | |
973a52ce RL |
201 | void *object, int selection, |
202 | const char *output_type, | |
203 | const char *output_structure, | |
ae12eac0 | 204 | const char *pass, const char *pcipher) |
5a23d78c RL |
205 | { |
206 | EVP_PKEY *pkey = object; | |
ece9304c | 207 | OSSL_ENCODER_CTX *ectx = NULL; |
5a23d78c RL |
208 | BIO *mem_ser = NULL; |
209 | BUF_MEM *mem_buf = NULL; | |
3ecbea6a | 210 | const unsigned char *upass = (const unsigned char *)pass; |
5a23d78c RL |
211 | int ok = 0; |
212 | ||
6511f686 | 213 | if (!TEST_FL_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, |
fe75766c TM |
214 | output_type, |
215 | output_structure, | |
216 | NULL)) | |
6511f686 | 217 | || !TEST_FL_int_gt(OSSL_ENCODER_CTX_get_num_encoders(ectx), 0) |
3ecbea6a | 218 | || (pass != NULL |
6511f686 | 219 | && !TEST_FL_true(OSSL_ENCODER_CTX_set_passphrase(ectx, upass, |
ae12eac0 | 220 | strlen(pass)))) |
3ecbea6a | 221 | || (pcipher != NULL |
6511f686 JS |
222 | && !TEST_FL_true(OSSL_ENCODER_CTX_set_cipher(ectx, pcipher, NULL))) |
223 | || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) | |
224 | || !TEST_FL_true(OSSL_ENCODER_to_bio(ectx, mem_ser)) | |
225 | || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) | |
226 | || !TEST_FL_ptr(*encoded = mem_buf->data) | |
227 | || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) | |
5a23d78c RL |
228 | goto end; |
229 | ||
ece9304c | 230 | /* Detach the encoded output */ |
5a23d78c RL |
231 | mem_buf->data = NULL; |
232 | mem_buf->length = 0; | |
233 | ok = 1; | |
234 | end: | |
235 | BIO_free(mem_ser); | |
ece9304c | 236 | OSSL_ENCODER_CTX_free(ectx); |
5a23d78c RL |
237 | return ok; |
238 | } | |
239 | ||
6511f686 JS |
240 | static int decode_EVP_PKEY_prov(const char *file, const int line, |
241 | void **object, void *encoded, long encoded_len, | |
ff1c10d9 RL |
242 | const char *input_type, |
243 | const char *structure_type, | |
244 | const char *keytype, int selection, | |
245 | const char *pass) | |
5a23d78c | 246 | { |
b565a17d | 247 | EVP_PKEY *pkey = NULL, *testpkey = NULL; |
ece9304c | 248 | OSSL_DECODER_CTX *dctx = NULL; |
b565a17d | 249 | BIO *encoded_bio = NULL; |
3ecbea6a | 250 | const unsigned char *upass = (const unsigned char *)pass; |
5a23d78c | 251 | int ok = 0; |
b565a17d MC |
252 | int i; |
253 | const char *badtype; | |
5a23d78c | 254 | |
b565a17d MC |
255 | if (strcmp(input_type, "DER") == 0) |
256 | badtype = "PEM"; | |
257 | else | |
258 | badtype = "DER"; | |
259 | ||
6511f686 | 260 | if (!TEST_FL_ptr(encoded_bio = BIO_new_mem_buf(encoded, encoded_len))) |
5a23d78c | 261 | goto end; |
b565a17d MC |
262 | |
263 | /* | |
264 | * We attempt the decode 3 times. The first time we provide the expected | |
265 | * starting input type. The second time we provide NULL for the starting | |
266 | * type. The third time we provide a bad starting input type. | |
267 | * The bad starting input type should fail. The other two should succeed | |
268 | * and produce the same result. | |
269 | */ | |
270 | for (i = 0; i < 3; i++) { | |
271 | const char *testtype = (i == 0) ? input_type | |
272 | : ((i == 1) ? NULL : badtype); | |
273 | ||
6511f686 | 274 | if (!TEST_FL_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&testpkey, |
fe75766c | 275 | testtype, |
ff1c10d9 | 276 | structure_type, |
fe75766c TM |
277 | keytype, |
278 | selection, | |
279 | NULL, NULL)) | |
b565a17d MC |
280 | || (pass != NULL |
281 | && !OSSL_DECODER_CTX_set_passphrase(dctx, upass, strlen(pass))) | |
6511f686 | 282 | || !TEST_FL_int_gt(BIO_reset(encoded_bio), 0) |
b565a17d | 283 | /* We expect to fail when using a bad input type */ |
6511f686 | 284 | || !TEST_FL_int_eq(OSSL_DECODER_from_bio(dctx, encoded_bio), |
b565a17d MC |
285 | (i == 2) ? 0 : 1)) |
286 | goto end; | |
287 | OSSL_DECODER_CTX_free(dctx); | |
288 | dctx = NULL; | |
289 | ||
290 | if (i == 0) { | |
291 | pkey = testpkey; | |
292 | testpkey = NULL; | |
293 | } else if (i == 1) { | |
294 | if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) { | |
6511f686 | 295 | if (!TEST_FL_int_eq(EVP_PKEY_parameters_eq(pkey, testpkey), 1)) |
b565a17d MC |
296 | goto end; |
297 | } else { | |
6511f686 | 298 | if (!TEST_FL_int_eq(EVP_PKEY_eq(pkey, testpkey), 1)) |
b565a17d MC |
299 | goto end; |
300 | } | |
301 | } | |
302 | } | |
5a23d78c RL |
303 | ok = 1; |
304 | *object = pkey; | |
b565a17d MC |
305 | pkey = NULL; |
306 | ||
5a23d78c | 307 | end: |
b565a17d MC |
308 | EVP_PKEY_free(pkey); |
309 | EVP_PKEY_free(testpkey); | |
310 | BIO_free(encoded_bio); | |
ece9304c | 311 | OSSL_DECODER_CTX_free(dctx); |
5a23d78c RL |
312 | return ok; |
313 | } | |
314 | ||
6511f686 JS |
315 | static int encode_EVP_PKEY_legacy_PEM(const char *file, const int line, |
316 | void **encoded, long *encoded_len, | |
973a52ce RL |
317 | void *object, ossl_unused int selection, |
318 | ossl_unused const char *output_type, | |
319 | ossl_unused const char *output_structure, | |
ae12eac0 | 320 | const char *pass, const char *pcipher) |
e2ac846e RL |
321 | { |
322 | EVP_PKEY *pkey = object; | |
323 | EVP_CIPHER *cipher = NULL; | |
324 | BIO *mem_ser = NULL; | |
325 | BUF_MEM *mem_buf = NULL; | |
326 | const unsigned char *upass = (const unsigned char *)pass; | |
327 | size_t passlen = 0; | |
328 | int ok = 0; | |
329 | ||
330 | if (pcipher != NULL && pass != NULL) { | |
331 | passlen = strlen(pass); | |
6511f686 | 332 | if (!TEST_FL_ptr(cipher = EVP_CIPHER_fetch(NULL, pcipher, NULL))) |
e2ac846e RL |
333 | goto end; |
334 | } | |
6511f686 JS |
335 | if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) |
336 | || !TEST_FL_true(PEM_write_bio_PrivateKey_traditional(mem_ser, pkey, | |
e2ac846e RL |
337 | cipher, |
338 | upass, passlen, | |
339 | NULL, NULL)) | |
6511f686 JS |
340 | || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) |
341 | || !TEST_FL_ptr(*encoded = mem_buf->data) | |
342 | || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) | |
e2ac846e RL |
343 | goto end; |
344 | ||
ece9304c | 345 | /* Detach the encoded output */ |
e2ac846e RL |
346 | mem_buf->data = NULL; |
347 | mem_buf->length = 0; | |
348 | ok = 1; | |
349 | end: | |
350 | BIO_free(mem_ser); | |
351 | EVP_CIPHER_free(cipher); | |
352 | return ok; | |
353 | } | |
354 | ||
6511f686 JS |
355 | static int encode_EVP_PKEY_MSBLOB(const char *file, const int line, |
356 | void **encoded, long *encoded_len, | |
973a52ce | 357 | void *object, int selection, |
ae12eac0 | 358 | ossl_unused const char *output_type, |
973a52ce | 359 | ossl_unused const char *output_structure, |
1251cddf | 360 | ossl_unused const char *pass, |
ae12eac0 | 361 | ossl_unused const char *pcipher) |
a7922e20 RL |
362 | { |
363 | EVP_PKEY *pkey = object; | |
364 | BIO *mem_ser = NULL; | |
365 | BUF_MEM *mem_buf = NULL; | |
366 | int ok = 0; | |
367 | ||
6511f686 | 368 | if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem()))) |
a7922e20 RL |
369 | goto end; |
370 | ||
973a52ce | 371 | if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) { |
6511f686 | 372 | if (!TEST_FL_int_ge(i2b_PrivateKey_bio(mem_ser, pkey), 0)) |
973a52ce RL |
373 | goto end; |
374 | } else { | |
6511f686 | 375 | if (!TEST_FL_int_ge(i2b_PublicKey_bio(mem_ser, pkey), 0)) |
973a52ce RL |
376 | goto end; |
377 | } | |
a7922e20 | 378 | |
6511f686 JS |
379 | if (!TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) |
380 | || !TEST_FL_ptr(*encoded = mem_buf->data) | |
381 | || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) | |
a7922e20 RL |
382 | goto end; |
383 | ||
ece9304c | 384 | /* Detach the encoded output */ |
a7922e20 RL |
385 | mem_buf->data = NULL; |
386 | mem_buf->length = 0; | |
387 | ok = 1; | |
388 | end: | |
389 | BIO_free(mem_ser); | |
390 | return ok; | |
391 | } | |
392 | ||
a7922e20 RL |
393 | static pem_password_cb pass_pw; |
394 | static int pass_pw(char *buf, int size, int rwflag, void *userdata) | |
395 | { | |
396 | OPENSSL_strlcpy(buf, userdata, size); | |
397 | return strlen(userdata); | |
398 | } | |
399 | ||
6511f686 JS |
400 | static int encode_EVP_PKEY_PVK(const char *file, const int line, |
401 | void **encoded, long *encoded_len, | |
973a52ce | 402 | void *object, int selection, |
ae12eac0 | 403 | ossl_unused const char *output_type, |
973a52ce | 404 | ossl_unused const char *output_structure, |
1251cddf | 405 | const char *pass, |
ae12eac0 | 406 | ossl_unused const char *pcipher) |
a7922e20 RL |
407 | { |
408 | EVP_PKEY *pkey = object; | |
409 | BIO *mem_ser = NULL; | |
410 | BUF_MEM *mem_buf = NULL; | |
411 | int enc = (pass != NULL); | |
412 | int ok = 0; | |
413 | ||
6511f686 | 414 | if (!TEST_FL_true(ossl_assert((selection |
973a52ce | 415 | & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)) |
6511f686 JS |
416 | || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())) |
417 | || !TEST_FL_int_ge(i2b_PVK_bio(mem_ser, pkey, enc, | |
a7922e20 | 418 | pass_pw, (void *)pass), 0) |
6511f686 JS |
419 | || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0) |
420 | || !TEST_FL_ptr(*encoded = mem_buf->data) | |
421 | || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0)) | |
a7922e20 RL |
422 | goto end; |
423 | ||
ece9304c | 424 | /* Detach the encoded output */ |
a7922e20 RL |
425 | mem_buf->data = NULL; |
426 | mem_buf->length = 0; | |
427 | ok = 1; | |
428 | end: | |
429 | BIO_free(mem_ser); | |
430 | return ok; | |
431 | } | |
a7922e20 | 432 | |
6511f686 JS |
433 | static int test_text(const char *file, const int line, |
434 | const void *data1, size_t data1_len, | |
319d0b2b RL |
435 | const void *data2, size_t data2_len) |
436 | { | |
6511f686 | 437 | return TEST_FL_strn2_eq(data1, data1_len, data2, data2_len); |
319d0b2b RL |
438 | } |
439 | ||
6511f686 JS |
440 | static int test_mem(const char *file, const int line, |
441 | const void *data1, size_t data1_len, | |
319d0b2b RL |
442 | const void *data2, size_t data2_len) |
443 | { | |
6511f686 | 444 | return TEST_FL_mem_eq(data1, data1_len, data2, data2_len); |
319d0b2b RL |
445 | } |
446 | ||
5a23d78c RL |
447 | /* Test cases and their dumpers / checkers */ |
448 | ||
ae12eac0 RL |
449 | static void collect_name(const char *name, void *arg) |
450 | { | |
451 | char **namelist = arg; | |
452 | char *new_namelist; | |
453 | size_t space; | |
454 | ||
455 | space = strlen(name); | |
456 | if (*namelist != NULL) | |
457 | space += strlen(*namelist) + 2 /* for comma and space */; | |
458 | space++; /* for terminating null byte */ | |
459 | ||
460 | new_namelist = OPENSSL_realloc(*namelist, space); | |
461 | if (new_namelist == NULL) | |
462 | return; | |
463 | if (*namelist != NULL) { | |
464 | strcat(new_namelist, ", "); | |
465 | strcat(new_namelist, name); | |
466 | } else { | |
467 | strcpy(new_namelist, name); | |
468 | } | |
469 | *namelist = new_namelist; | |
470 | } | |
471 | ||
5a23d78c RL |
472 | static void dump_der(const char *label, const void *data, size_t data_len) |
473 | { | |
474 | test_output_memory(label, data, data_len); | |
475 | } | |
476 | ||
477 | static void dump_pem(const char *label, const void *data, size_t data_len) | |
478 | { | |
479 | test_output_string(label, data, data_len - 1); | |
480 | } | |
481 | ||
6511f686 JS |
482 | static int check_unprotected_PKCS8_DER(const char *file, const int line, |
483 | const char *type, | |
3ecbea6a | 484 | const void *data, size_t data_len) |
5a23d78c RL |
485 | { |
486 | const unsigned char *datap = data; | |
487 | PKCS8_PRIV_KEY_INFO *p8inf = | |
488 | d2i_PKCS8_PRIV_KEY_INFO(NULL, &datap, data_len); | |
489 | int ok = 0; | |
490 | ||
6511f686 | 491 | if (TEST_FL_ptr(p8inf)) { |
5a23d78c | 492 | EVP_PKEY *pkey = EVP_PKCS82PKEY(p8inf); |
ae12eac0 RL |
493 | char *namelist = NULL; |
494 | ||
6511f686 JS |
495 | if (TEST_FL_ptr(pkey)) { |
496 | if (!(ok = TEST_FL_true(EVP_PKEY_is_a(pkey, type)))) { | |
ae12eac0 RL |
497 | EVP_PKEY_typenames_do_all(pkey, collect_name, &namelist); |
498 | if (namelist != NULL) | |
499 | TEST_note("%s isn't any of %s", type, namelist); | |
500 | OPENSSL_free(namelist); | |
501 | } | |
4f0831b8 | 502 | ok = ok && TEST_FL_true(evp_pkey_is_provided(pkey)); |
ae12eac0 RL |
503 | EVP_PKEY_free(pkey); |
504 | } | |
5a23d78c RL |
505 | } |
506 | PKCS8_PRIV_KEY_INFO_free(p8inf); | |
507 | return ok; | |
508 | } | |
509 | ||
7c664b1f | 510 | static int test_unprotected_via_DER(const char *type, EVP_PKEY *key) |
846f96f8 | 511 | { |
6511f686 | 512 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 513 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 514 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 515 | "DER", "pkcs8", NULL, NULL, |
1251cddf | 516 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
ae12eac0 | 517 | test_mem, check_unprotected_PKCS8_DER, |
b565a17d | 518 | dump_der, 0); |
846f96f8 RL |
519 | } |
520 | ||
6511f686 JS |
521 | static int check_unprotected_PKCS8_PEM(const char *file, const int line, |
522 | const char *type, | |
3ecbea6a | 523 | const void *data, size_t data_len) |
5a23d78c | 524 | { |
ae12eac0 RL |
525 | static const char expected_pem_header[] = |
526 | "-----BEGIN " PEM_STRING_PKCS8INF "-----"; | |
5a23d78c | 527 | |
6511f686 | 528 | return TEST_FL_strn_eq(data, expected_pem_header, |
ae12eac0 | 529 | sizeof(expected_pem_header) - 1); |
5a23d78c RL |
530 | } |
531 | ||
7c664b1f | 532 | static int test_unprotected_via_PEM(const char *type, EVP_PKEY *key) |
5a23d78c | 533 | { |
6511f686 | 534 | return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 535 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 536 | "PEM", "pkcs8", NULL, NULL, |
1251cddf | 537 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
ae12eac0 | 538 | test_text, check_unprotected_PKCS8_PEM, |
b565a17d MC |
539 | dump_pem, 0); |
540 | } | |
541 | ||
a2e145f8 | 542 | #ifndef OPENSSL_NO_KEYPARAMS |
6511f686 JS |
543 | static int check_params_DER(const char *file, const int line, |
544 | const char *type, const void *data, size_t data_len) | |
b565a17d MC |
545 | { |
546 | const unsigned char *datap = data; | |
547 | int ok = 0; | |
548 | int itype = NID_undef; | |
549 | EVP_PKEY *pkey = NULL; | |
550 | ||
551 | if (strcmp(type, "DH") == 0) | |
552 | itype = EVP_PKEY_DH; | |
553 | else if (strcmp(type, "X9.42 DH") == 0) | |
554 | itype = EVP_PKEY_DHX; | |
555 | else if (strcmp(type, "DSA") == 0) | |
556 | itype = EVP_PKEY_DSA; | |
557 | else if (strcmp(type, "EC") == 0) | |
558 | itype = EVP_PKEY_EC; | |
559 | ||
560 | if (itype != NID_undef) { | |
561 | pkey = d2i_KeyParams(itype, NULL, &datap, data_len); | |
562 | ok = (pkey != NULL); | |
563 | EVP_PKEY_free(pkey); | |
564 | } | |
565 | ||
566 | return ok; | |
567 | } | |
568 | ||
6511f686 JS |
569 | static int check_params_PEM(const char *file, const int line, |
570 | const char *type, | |
571 | const void *data, size_t data_len) | |
b565a17d MC |
572 | { |
573 | static char expected_pem_header[80]; | |
574 | ||
575 | return | |
6511f686 | 576 | TEST_FL_int_gt(BIO_snprintf(expected_pem_header, |
b565a17d MC |
577 | sizeof(expected_pem_header), |
578 | "-----BEGIN %s PARAMETERS-----", type), 0) | |
6511f686 | 579 | && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)); |
b565a17d MC |
580 | } |
581 | ||
582 | static int test_params_via_DER(const char *type, EVP_PKEY *key) | |
583 | { | |
6511f686 | 584 | return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 585 | "DER", "type-specific", NULL, NULL, |
b565a17d MC |
586 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
587 | test_mem, check_params_DER, | |
588 | dump_der, FLAG_DECODE_WITH_TYPE); | |
589 | } | |
590 | ||
591 | static int test_params_via_PEM(const char *type, EVP_PKEY *key) | |
592 | { | |
6511f686 | 593 | return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 594 | "PEM", "type-specific", NULL, NULL, |
b565a17d MC |
595 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
596 | test_text, check_params_PEM, | |
597 | dump_pem, 0); | |
e2ac846e | 598 | } |
a2e145f8 | 599 | #endif /* !OPENSSL_NO_KEYPARAMS */ |
e2ac846e | 600 | |
6511f686 JS |
601 | static int check_unprotected_legacy_PEM(const char *file, const int line, |
602 | const char *type, | |
e2ac846e RL |
603 | const void *data, size_t data_len) |
604 | { | |
ae12eac0 | 605 | static char expected_pem_header[80]; |
e2ac846e | 606 | |
846f96f8 | 607 | return |
6511f686 | 608 | TEST_FL_int_gt(BIO_snprintf(expected_pem_header, |
ae12eac0 | 609 | sizeof(expected_pem_header), |
846f96f8 | 610 | "-----BEGIN %s PRIVATE KEY-----", type), 0) |
6511f686 | 611 | && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)); |
e2ac846e RL |
612 | } |
613 | ||
7c664b1f | 614 | static int test_unprotected_via_legacy_PEM(const char *type, EVP_PKEY *key) |
846f96f8 | 615 | { |
6511f686 | 616 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 617 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 618 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 619 | "PEM", "type-specific", NULL, NULL, |
1251cddf | 620 | encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov, |
ae12eac0 | 621 | test_text, check_unprotected_legacy_PEM, |
b565a17d | 622 | dump_pem, 0); |
3ecbea6a RL |
623 | } |
624 | ||
6511f686 JS |
625 | static int check_MSBLOB(const char *file, const int line, |
626 | const char *type, const void *data, size_t data_len) | |
a7922e20 RL |
627 | { |
628 | const unsigned char *datap = data; | |
629 | EVP_PKEY *pkey = b2i_PrivateKey(&datap, data_len); | |
6511f686 | 630 | int ok = TEST_FL_ptr(pkey); |
a7922e20 RL |
631 | |
632 | EVP_PKEY_free(pkey); | |
633 | return ok; | |
634 | } | |
635 | ||
636 | static int test_unprotected_via_MSBLOB(const char *type, EVP_PKEY *key) | |
637 | { | |
6511f686 | 638 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 639 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 640 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 641 | "MSBLOB", NULL, NULL, NULL, |
1251cddf | 642 | encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov, |
ae12eac0 | 643 | test_mem, check_MSBLOB, |
b565a17d | 644 | dump_der, 0); |
a7922e20 RL |
645 | } |
646 | ||
6511f686 JS |
647 | static int check_PVK(const char *file, const int line, |
648 | const char *type, const void *data, size_t data_len) | |
a7922e20 RL |
649 | { |
650 | const unsigned char *in = data; | |
651 | unsigned int saltlen = 0, keylen = 0; | |
652 | int ok = ossl_do_PVK_header(&in, data_len, 0, &saltlen, &keylen); | |
653 | ||
654 | return ok; | |
655 | } | |
656 | ||
657 | static int test_unprotected_via_PVK(const char *type, EVP_PKEY *key) | |
658 | { | |
6511f686 | 659 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 660 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 661 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 662 | "PVK", NULL, NULL, NULL, |
1251cddf | 663 | encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov, |
ae12eac0 | 664 | test_mem, check_PVK, |
b565a17d | 665 | dump_der, 0); |
a7922e20 | 666 | } |
a7922e20 | 667 | |
3ecbea6a RL |
668 | static const char *pass_cipher = "AES-256-CBC"; |
669 | static const char *pass = "the holy handgrenade of antioch"; | |
670 | ||
6511f686 JS |
671 | static int check_protected_PKCS8_DER(const char *file, const int line, |
672 | const char *type, | |
3ecbea6a RL |
673 | const void *data, size_t data_len) |
674 | { | |
675 | const unsigned char *datap = data; | |
676 | X509_SIG *p8 = d2i_X509_SIG(NULL, &datap, data_len); | |
6511f686 | 677 | int ok = TEST_FL_ptr(p8); |
3ecbea6a RL |
678 | |
679 | X509_SIG_free(p8); | |
680 | return ok; | |
681 | } | |
682 | ||
7c664b1f | 683 | static int test_protected_via_DER(const char *type, EVP_PKEY *key) |
846f96f8 | 684 | { |
6511f686 | 685 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 686 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 687 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 688 | "DER", "pkcs8", pass, pass_cipher, |
1251cddf | 689 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
ae12eac0 | 690 | test_mem, check_protected_PKCS8_DER, |
b565a17d | 691 | dump_der, 0); |
3ecbea6a RL |
692 | } |
693 | ||
6511f686 JS |
694 | static int check_protected_PKCS8_PEM(const char *file, const int line, |
695 | const char *type, | |
3ecbea6a RL |
696 | const void *data, size_t data_len) |
697 | { | |
ae12eac0 RL |
698 | static const char expected_pem_header[] = |
699 | "-----BEGIN " PEM_STRING_PKCS8 "-----"; | |
3ecbea6a | 700 | |
6511f686 | 701 | return TEST_FL_strn_eq(data, expected_pem_header, |
ae12eac0 | 702 | sizeof(expected_pem_header) - 1); |
3ecbea6a RL |
703 | } |
704 | ||
7c664b1f | 705 | static int test_protected_via_PEM(const char *type, EVP_PKEY *key) |
3ecbea6a | 706 | { |
6511f686 | 707 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 708 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 709 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 710 | "PEM", "pkcs8", pass, pass_cipher, |
1251cddf | 711 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
ae12eac0 | 712 | test_text, check_protected_PKCS8_PEM, |
b565a17d | 713 | dump_pem, 0); |
e2ac846e RL |
714 | } |
715 | ||
6511f686 JS |
716 | static int check_protected_legacy_PEM(const char *file, const int line, |
717 | const char *type, | |
e2ac846e RL |
718 | const void *data, size_t data_len) |
719 | { | |
ae12eac0 | 720 | static char expected_pem_header[80]; |
e2ac846e RL |
721 | |
722 | return | |
6511f686 | 723 | TEST_FL_int_gt(BIO_snprintf(expected_pem_header, |
ae12eac0 | 724 | sizeof(expected_pem_header), |
846f96f8 | 725 | "-----BEGIN %s PRIVATE KEY-----", type), 0) |
6511f686 JS |
726 | && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header)) |
727 | && TEST_FL_ptr(strstr(data, "\nDEK-Info: ")); | |
e2ac846e RL |
728 | } |
729 | ||
7c664b1f | 730 | static int test_protected_via_legacy_PEM(const char *type, EVP_PKEY *key) |
e2ac846e | 731 | { |
6511f686 | 732 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 733 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 734 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 735 | "PEM", "type-specific", pass, pass_cipher, |
1251cddf | 736 | encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov, |
ae12eac0 | 737 | test_text, check_protected_legacy_PEM, |
b565a17d | 738 | dump_pem, 0); |
846f96f8 RL |
739 | } |
740 | ||
2e1bc081 | 741 | #ifndef OPENSSL_NO_RC4 |
a7922e20 RL |
742 | static int test_protected_via_PVK(const char *type, EVP_PKEY *key) |
743 | { | |
6511f686 | 744 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 745 | OSSL_KEYMGMT_SELECT_KEYPAIR |
ae12eac0 | 746 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 747 | "PVK", NULL, pass, NULL, |
1251cddf | 748 | encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov, |
b565a17d | 749 | test_mem, check_PVK, dump_der, 0); |
a7922e20 RL |
750 | } |
751 | #endif | |
752 | ||
6511f686 JS |
753 | static int check_public_DER(const char *file, const int line, |
754 | const char *type, const void *data, size_t data_len) | |
3ff8159a RL |
755 | { |
756 | const unsigned char *datap = data; | |
757 | EVP_PKEY *pkey = d2i_PUBKEY(NULL, &datap, data_len); | |
6511f686 | 758 | int ok = (TEST_FL_ptr(pkey) && TEST_FL_true(EVP_PKEY_is_a(pkey, type))); |
3ff8159a RL |
759 | |
760 | EVP_PKEY_free(pkey); | |
761 | return ok; | |
762 | } | |
763 | ||
7c664b1f | 764 | static int test_public_via_DER(const char *type, EVP_PKEY *key) |
3ff8159a | 765 | { |
6511f686 | 766 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 767 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY |
ae12eac0 | 768 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 769 | "DER", "SubjectPublicKeyInfo", NULL, NULL, |
1251cddf | 770 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
b565a17d | 771 | test_mem, check_public_DER, dump_der, 0); |
3ff8159a RL |
772 | } |
773 | ||
6511f686 JS |
774 | static int check_public_PEM(const char *file, const int line, |
775 | const char *type, const void *data, size_t data_len) | |
3ff8159a | 776 | { |
ae12eac0 RL |
777 | static const char expected_pem_header[] = |
778 | "-----BEGIN " PEM_STRING_PUBLIC "-----"; | |
3ff8159a RL |
779 | |
780 | return | |
6511f686 | 781 | TEST_FL_strn_eq(data, expected_pem_header, |
ae12eac0 | 782 | sizeof(expected_pem_header) - 1); |
3ff8159a RL |
783 | } |
784 | ||
7c664b1f | 785 | static int test_public_via_PEM(const char *type, EVP_PKEY *key) |
3ff8159a | 786 | { |
6511f686 | 787 | return test_encode_decode(__FILE__, __LINE__, type, key, |
973a52ce | 788 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY |
ae12eac0 | 789 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce | 790 | "PEM", "SubjectPublicKeyInfo", NULL, NULL, |
1251cddf | 791 | encode_EVP_PKEY_prov, decode_EVP_PKEY_prov, |
b565a17d | 792 | test_text, check_public_PEM, dump_pem, 0); |
3ff8159a RL |
793 | } |
794 | ||
6511f686 JS |
795 | static int check_public_MSBLOB(const char *file, const int line, |
796 | const char *type, | |
a7922e20 RL |
797 | const void *data, size_t data_len) |
798 | { | |
799 | const unsigned char *datap = data; | |
800 | EVP_PKEY *pkey = b2i_PublicKey(&datap, data_len); | |
6511f686 | 801 | int ok = TEST_FL_ptr(pkey); |
a7922e20 RL |
802 | |
803 | EVP_PKEY_free(pkey); | |
804 | return ok; | |
805 | } | |
806 | ||
807 | static int test_public_via_MSBLOB(const char *type, EVP_PKEY *key) | |
808 | { | |
6511f686 | 809 | return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_PUBLIC_KEY |
ae12eac0 | 810 | | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS, |
973a52ce RL |
811 | "MSBLOB", NULL, NULL, NULL, |
812 | encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov, | |
b565a17d | 813 | test_mem, check_public_MSBLOB, dump_der, 0); |
a7922e20 | 814 | } |
a7922e20 | 815 | |
7c664b1f | 816 | #define KEYS(KEYTYPE) \ |
1251cddf | 817 | static EVP_PKEY *key_##KEYTYPE = NULL |
7c664b1f RL |
818 | #define MAKE_KEYS(KEYTYPE, KEYTYPEstr, params) \ |
819 | ok = ok \ | |
1251cddf | 820 | && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, NULL, params)) |
7c664b1f RL |
821 | #define FREE_KEYS(KEYTYPE) \ |
822 | EVP_PKEY_free(key_##KEYTYPE); \ | |
7c664b1f RL |
823 | |
824 | #define DOMAIN_KEYS(KEYTYPE) \ | |
825 | static EVP_PKEY *template_##KEYTYPE = NULL; \ | |
1251cddf | 826 | static EVP_PKEY *key_##KEYTYPE = NULL |
7c664b1f RL |
827 | #define MAKE_DOMAIN_KEYS(KEYTYPE, KEYTYPEstr, params) \ |
828 | ok = ok \ | |
829 | && TEST_ptr(template_##KEYTYPE = \ | |
830 | make_template(KEYTYPEstr, params)) \ | |
831 | && TEST_ptr(key_##KEYTYPE = \ | |
1251cddf | 832 | make_key(KEYTYPEstr, template_##KEYTYPE, NULL)) |
7c664b1f RL |
833 | #define FREE_DOMAIN_KEYS(KEYTYPE) \ |
834 | EVP_PKEY_free(template_##KEYTYPE); \ | |
1251cddf | 835 | EVP_PKEY_free(key_##KEYTYPE) |
7c664b1f RL |
836 | |
837 | #define IMPLEMENT_TEST_SUITE(KEYTYPE, KEYTYPEstr) \ | |
838 | static int test_unprotected_##KEYTYPE##_via_DER(void) \ | |
839 | { \ | |
840 | return test_unprotected_via_DER(KEYTYPEstr, key_##KEYTYPE); \ | |
841 | } \ | |
842 | static int test_unprotected_##KEYTYPE##_via_PEM(void) \ | |
843 | { \ | |
844 | return test_unprotected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
845 | } \ | |
7c664b1f RL |
846 | static int test_protected_##KEYTYPE##_via_DER(void) \ |
847 | { \ | |
848 | return test_protected_via_DER(KEYTYPEstr, key_##KEYTYPE); \ | |
849 | } \ | |
850 | static int test_protected_##KEYTYPE##_via_PEM(void) \ | |
851 | { \ | |
852 | return test_protected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
853 | } \ | |
7c664b1f RL |
854 | static int test_public_##KEYTYPE##_via_DER(void) \ |
855 | { \ | |
856 | return test_public_via_DER(KEYTYPEstr, key_##KEYTYPE); \ | |
857 | } \ | |
858 | static int test_public_##KEYTYPE##_via_PEM(void) \ | |
859 | { \ | |
860 | return test_public_via_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
861 | } | |
862 | ||
863 | #define ADD_TEST_SUITE(KEYTYPE) \ | |
864 | ADD_TEST(test_unprotected_##KEYTYPE##_via_DER); \ | |
865 | ADD_TEST(test_unprotected_##KEYTYPE##_via_PEM); \ | |
7c664b1f RL |
866 | ADD_TEST(test_protected_##KEYTYPE##_via_DER); \ |
867 | ADD_TEST(test_protected_##KEYTYPE##_via_PEM); \ | |
7c664b1f RL |
868 | ADD_TEST(test_public_##KEYTYPE##_via_DER); \ |
869 | ADD_TEST(test_public_##KEYTYPE##_via_PEM) | |
870 | ||
b565a17d MC |
871 | #define IMPLEMENT_TEST_SUITE_PARAMS(KEYTYPE, KEYTYPEstr) \ |
872 | static int test_params_##KEYTYPE##_via_DER(void) \ | |
873 | { \ | |
874 | return test_params_via_DER(KEYTYPEstr, key_##KEYTYPE); \ | |
875 | } \ | |
876 | static int test_params_##KEYTYPE##_via_PEM(void) \ | |
877 | { \ | |
878 | return test_params_via_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
879 | } | |
880 | ||
881 | #define ADD_TEST_SUITE_PARAMS(KEYTYPE) \ | |
882 | ADD_TEST(test_params_##KEYTYPE##_via_DER); \ | |
883 | ADD_TEST(test_params_##KEYTYPE##_via_PEM) | |
884 | ||
bddfea02 RL |
885 | #define IMPLEMENT_TEST_SUITE_LEGACY(KEYTYPE, KEYTYPEstr) \ |
886 | static int test_unprotected_##KEYTYPE##_via_legacy_PEM(void) \ | |
887 | { \ | |
1251cddf RL |
888 | return \ |
889 | test_unprotected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
bddfea02 RL |
890 | } \ |
891 | static int test_protected_##KEYTYPE##_via_legacy_PEM(void) \ | |
892 | { \ | |
1251cddf RL |
893 | return \ |
894 | test_protected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \ | |
bddfea02 RL |
895 | } |
896 | ||
2e1bc081 RL |
897 | #define ADD_TEST_SUITE_LEGACY(KEYTYPE) \ |
898 | ADD_TEST(test_unprotected_##KEYTYPE##_via_legacy_PEM); \ | |
bddfea02 RL |
899 | ADD_TEST(test_protected_##KEYTYPE##_via_legacy_PEM) |
900 | ||
2e1bc081 | 901 | #define IMPLEMENT_TEST_SUITE_MSBLOB(KEYTYPE, KEYTYPEstr) \ |
a7922e20 RL |
902 | static int test_unprotected_##KEYTYPE##_via_MSBLOB(void) \ |
903 | { \ | |
904 | return test_unprotected_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \ | |
905 | } \ | |
906 | static int test_public_##KEYTYPE##_via_MSBLOB(void) \ | |
907 | { \ | |
908 | return test_public_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \ | |
909 | } | |
910 | ||
2e1bc081 RL |
911 | #define ADD_TEST_SUITE_MSBLOB(KEYTYPE) \ |
912 | ADD_TEST(test_unprotected_##KEYTYPE##_via_MSBLOB); \ | |
a7922e20 RL |
913 | ADD_TEST(test_public_##KEYTYPE##_via_MSBLOB) |
914 | ||
5faec149 | 915 | #define IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE, KEYTYPEstr) \ |
a7922e20 RL |
916 | static int test_unprotected_##KEYTYPE##_via_PVK(void) \ |
917 | { \ | |
918 | return test_unprotected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \ | |
5faec149 RL |
919 | } |
920 | # define ADD_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE) \ | |
921 | ADD_TEST(test_unprotected_##KEYTYPE##_via_PVK) | |
922 | #ifndef OPENSSL_NO_RC4 | |
923 | # define IMPLEMENT_TEST_SUITE_PROTECTED_PVK(KEYTYPE, KEYTYPEstr) \ | |
a7922e20 RL |
924 | static int test_protected_##KEYTYPE##_via_PVK(void) \ |
925 | { \ | |
926 | return test_protected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \ | |
927 | } | |
5faec149 | 928 | # define ADD_TEST_SUITE_PROTECTED_PVK(KEYTYPE) \ |
a7922e20 | 929 | ADD_TEST(test_protected_##KEYTYPE##_via_PVK) |
a7922e20 RL |
930 | #endif |
931 | ||
7c664b1f RL |
932 | #ifndef OPENSSL_NO_DH |
933 | DOMAIN_KEYS(DH); | |
934 | IMPLEMENT_TEST_SUITE(DH, "DH") | |
b565a17d | 935 | IMPLEMENT_TEST_SUITE_PARAMS(DH, "DH") |
31d2daec SL |
936 | DOMAIN_KEYS(DHX); |
937 | IMPLEMENT_TEST_SUITE(DHX, "X9.42 DH") | |
b565a17d | 938 | IMPLEMENT_TEST_SUITE_PARAMS(DHX, "X9.42 DH") |
bddfea02 RL |
939 | /* |
940 | * DH has no support for PEM_write_bio_PrivateKey_traditional(), | |
941 | * so no legacy tests. | |
942 | */ | |
7c664b1f RL |
943 | #endif |
944 | #ifndef OPENSSL_NO_DSA | |
945 | DOMAIN_KEYS(DSA); | |
946 | IMPLEMENT_TEST_SUITE(DSA, "DSA") | |
b565a17d | 947 | IMPLEMENT_TEST_SUITE_PARAMS(DSA, "DSA") |
bddfea02 | 948 | IMPLEMENT_TEST_SUITE_LEGACY(DSA, "DSA") |
a7922e20 | 949 | IMPLEMENT_TEST_SUITE_MSBLOB(DSA, "DSA") |
5faec149 RL |
950 | IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(DSA, "DSA") |
951 | # ifndef OPENSSL_NO_RC4 | |
952 | IMPLEMENT_TEST_SUITE_PROTECTED_PVK(DSA, "DSA") | |
953 | # endif | |
7c664b1f RL |
954 | #endif |
955 | #ifndef OPENSSL_NO_EC | |
956 | DOMAIN_KEYS(EC); | |
957 | IMPLEMENT_TEST_SUITE(EC, "EC") | |
b565a17d | 958 | IMPLEMENT_TEST_SUITE_PARAMS(EC, "EC") |
bddfea02 | 959 | IMPLEMENT_TEST_SUITE_LEGACY(EC, "EC") |
c0f39ded SL |
960 | DOMAIN_KEYS(ECExplicitPrimeNamedCurve); |
961 | IMPLEMENT_TEST_SUITE(ECExplicitPrimeNamedCurve, "EC") | |
bddfea02 | 962 | IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve, "EC") |
c0f39ded SL |
963 | DOMAIN_KEYS(ECExplicitPrime2G); |
964 | IMPLEMENT_TEST_SUITE(ECExplicitPrime2G, "EC") | |
bddfea02 | 965 | IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrime2G, "EC") |
c0f39ded SL |
966 | # ifndef OPENSSL_NO_EC2M |
967 | DOMAIN_KEYS(ECExplicitTriNamedCurve); | |
968 | IMPLEMENT_TEST_SUITE(ECExplicitTriNamedCurve, "EC") | |
bddfea02 | 969 | IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve, "EC") |
c0f39ded SL |
970 | DOMAIN_KEYS(ECExplicitTri2G); |
971 | IMPLEMENT_TEST_SUITE(ECExplicitTri2G, "EC") | |
bddfea02 | 972 | IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTri2G, "EC") |
c0f39ded | 973 | # endif |
7c664b1f RL |
974 | KEYS(ED25519); |
975 | IMPLEMENT_TEST_SUITE(ED25519, "ED25519") | |
976 | KEYS(ED448); | |
977 | IMPLEMENT_TEST_SUITE(ED448, "ED448") | |
978 | KEYS(X25519); | |
979 | IMPLEMENT_TEST_SUITE(X25519, "X25519") | |
980 | KEYS(X448); | |
981 | IMPLEMENT_TEST_SUITE(X448, "X448") | |
bddfea02 RL |
982 | /* |
983 | * ED25519, ED448, X25519 and X448 have no support for | |
984 | * PEM_write_bio_PrivateKey_traditional(), so no legacy tests. | |
985 | */ | |
7c664b1f RL |
986 | #endif |
987 | KEYS(RSA); | |
988 | IMPLEMENT_TEST_SUITE(RSA, "RSA") | |
bddfea02 | 989 | IMPLEMENT_TEST_SUITE_LEGACY(RSA, "RSA") |
7c664b1f RL |
990 | KEYS(RSA_PSS); |
991 | IMPLEMENT_TEST_SUITE(RSA_PSS, "RSA-PSS") | |
bddfea02 RL |
992 | /* |
993 | * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(), | |
994 | * so no legacy tests. | |
995 | */ | |
a7922e20 | 996 | IMPLEMENT_TEST_SUITE_MSBLOB(RSA, "RSA") |
5faec149 RL |
997 | IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(RSA, "RSA") |
998 | #ifndef OPENSSL_NO_RC4 | |
999 | IMPLEMENT_TEST_SUITE_PROTECTED_PVK(RSA, "RSA") | |
1000 | #endif | |
3ff8159a | 1001 | |
c0f39ded SL |
1002 | #ifndef OPENSSL_NO_EC |
1003 | /* Explicit parameters that match a named curve */ | |
1004 | static int do_create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld, | |
1005 | const unsigned char *gen, | |
1006 | size_t gen_len) | |
1007 | { | |
1008 | BIGNUM *a, *b, *prime, *order; | |
1009 | ||
1010 | /* Curve prime256v1 */ | |
1011 | static const unsigned char prime_data[] = { | |
1012 | 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, | |
1013 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1014 | 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, | |
1015 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
1016 | 0xff | |
1017 | }; | |
1018 | static const unsigned char a_data[] = { | |
1019 | 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, | |
1020 | 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1021 | 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, | |
1022 | 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
1023 | 0xfc | |
1024 | }; | |
1025 | static const unsigned char b_data[] = { | |
1026 | 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7, | |
1027 | 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc, | |
1028 | 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6, | |
1029 | 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b | |
1030 | }; | |
1031 | static const unsigned char seed[] = { | |
1032 | 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93, | |
1033 | 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, 0x26, 0xb7, | |
1034 | 0x81, 0x9f, 0x7e, 0x90 | |
1035 | }; | |
1036 | static const unsigned char order_data[] = { | |
1037 | 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, | |
1038 | 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, | |
1039 | 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e, | |
1040 | 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51 | |
1041 | }; | |
1042 | return TEST_ptr(a = BN_CTX_get(bnctx)) | |
1043 | && TEST_ptr(b = BN_CTX_get(bnctx)) | |
1044 | && TEST_ptr(prime = BN_CTX_get(bnctx)) | |
1045 | && TEST_ptr(order = BN_CTX_get(bnctx)) | |
1046 | && TEST_ptr(BN_bin2bn(prime_data, sizeof(prime_data), prime)) | |
1047 | && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a)) | |
1048 | && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b)) | |
1049 | && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order)) | |
1050 | && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, | |
1051 | OSSL_PKEY_PARAM_EC_FIELD_TYPE, SN_X9_62_prime_field, | |
1052 | 0)) | |
1053 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, prime)) | |
1054 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) | |
1055 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)) | |
1056 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, | |
1057 | OSSL_PKEY_PARAM_EC_ORDER, order)) | |
1058 | && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, | |
1059 | OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len)) | |
1060 | && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, | |
1061 | OSSL_PKEY_PARAM_EC_SEED, seed, sizeof(seed))) | |
1062 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, | |
1063 | BN_value_one())); | |
1064 | } | |
1065 | ||
1066 | static int create_ec_explicit_prime_params_namedcurve(OSSL_PARAM_BLD *bld) | |
1067 | { | |
1068 | static const unsigned char prime256v1_gen[] = { | |
1069 | 0x04, | |
1070 | 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47, | |
1071 | 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2, | |
1072 | 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0, | |
1073 | 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96, | |
1074 | 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b, | |
1075 | 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16, | |
1076 | 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce, | |
1077 | 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5 | |
1078 | }; | |
1079 | return do_create_ec_explicit_prime_params(bld, prime256v1_gen, | |
1080 | sizeof(prime256v1_gen)); | |
1081 | } | |
1082 | ||
1083 | static int create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld) | |
1084 | { | |
1085 | /* 2G */ | |
1086 | static const unsigned char prime256v1_gen2[] = { | |
1087 | 0x04, | |
1088 | 0xe4, 0x97, 0x08, 0xbe, 0x7d, 0xfa, 0xa2, 0x9a, | |
1089 | 0xa3, 0x12, 0x6f, 0xe4, 0xe7, 0xd0, 0x25, 0xe3, | |
1090 | 0x4a, 0xc1, 0x03, 0x15, 0x8c, 0xd9, 0x33, 0xc6, | |
1091 | 0x97, 0x42, 0xf5, 0xdc, 0x97, 0xb9, 0xd7, 0x31, | |
1092 | 0xe9, 0x7d, 0x74, 0x3d, 0x67, 0x6a, 0x3b, 0x21, | |
1093 | 0x08, 0x9c, 0x31, 0x73, 0xf8, 0xc1, 0x27, 0xc9, | |
1094 | 0xd2, 0xa0, 0xa0, 0x83, 0x66, 0xe0, 0xc9, 0xda, | |
1095 | 0xa8, 0xc6, 0x56, 0x2b, 0x94, 0xb1, 0xae, 0x55 | |
1096 | }; | |
1097 | return do_create_ec_explicit_prime_params(bld, prime256v1_gen2, | |
1098 | sizeof(prime256v1_gen2)); | |
1099 | } | |
1100 | ||
1101 | # ifndef OPENSSL_NO_EC2M | |
1102 | static int do_create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld, | |
1103 | const unsigned char *gen, | |
1104 | size_t gen_len) | |
1105 | { | |
1106 | BIGNUM *a, *b, *poly, *order, *cofactor; | |
1107 | /* sect233k1 characteristic-two-field tpBasis */ | |
1108 | static const unsigned char poly_data[] = { | |
1109 | 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1110 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00, | |
1111 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, | |
1112 | }; | |
1113 | static const unsigned char a_data[] = { | |
1114 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1115 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1116 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 | |
1117 | }; | |
1118 | static const unsigned char b_data[] = { | |
1119 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1120 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1121 | 0x00, 0x00, 0x00, 0x00, 0x00, 0x01 | |
1122 | }; | |
1123 | static const unsigned char order_data[] = { | |
1124 | 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, | |
1125 | 0x00, 0x00, 0x00, 0x06, 0x9D, 0x5B, 0xB9, 0x15, 0xBC, 0xD4, 0x6E, 0xFB, | |
1126 | 0x1A, 0xD5, 0xF1, 0x73, 0xAB, 0xDF | |
1127 | }; | |
1128 | static const unsigned char cofactor_data[]= { | |
1129 | 0x4 | |
1130 | }; | |
1131 | return TEST_ptr(a = BN_CTX_get(bnctx)) | |
1132 | && TEST_ptr(b = BN_CTX_get(bnctx)) | |
1133 | && TEST_ptr(poly = BN_CTX_get(bnctx)) | |
1134 | && TEST_ptr(order = BN_CTX_get(bnctx)) | |
1135 | && TEST_ptr(cofactor = BN_CTX_get(bnctx)) | |
1136 | && TEST_ptr(BN_bin2bn(poly_data, sizeof(poly_data), poly)) | |
1137 | && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a)) | |
1138 | && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b)) | |
1139 | && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order)) | |
1140 | && TEST_ptr(BN_bin2bn(cofactor_data, sizeof(cofactor_data), cofactor)) | |
1141 | && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld, | |
1142 | OSSL_PKEY_PARAM_EC_FIELD_TYPE, | |
1143 | SN_X9_62_characteristic_two_field, 0)) | |
1144 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, poly)) | |
1145 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a)) | |
1146 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b)) | |
1147 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, | |
1148 | OSSL_PKEY_PARAM_EC_ORDER, order)) | |
1149 | && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld, | |
1150 | OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len)) | |
1151 | && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR, | |
1152 | cofactor)); | |
1153 | } | |
1154 | ||
1155 | static int create_ec_explicit_trinomial_params_namedcurve(OSSL_PARAM_BLD *bld) | |
1156 | { | |
1157 | static const unsigned char gen[] = { | |
1158 | 0x04, | |
1159 | 0x01, 0x72, 0x32, 0xBA, 0x85, 0x3A, 0x7E, 0x73, 0x1A, 0xF1, 0x29, 0xF2, | |
1160 | 0x2F, 0xF4, 0x14, 0x95, 0x63, 0xA4, 0x19, 0xC2, 0x6B, 0xF5, 0x0A, 0x4C, | |
1161 | 0x9D, 0x6E, 0xEF, 0xAD, 0x61, 0x26, | |
1162 | 0x01, 0xDB, 0x53, 0x7D, 0xEC, 0xE8, 0x19, 0xB7, 0xF7, 0x0F, 0x55, 0x5A, | |
1163 | 0x67, 0xC4, 0x27, 0xA8, 0xCD, 0x9B, 0xF1, 0x8A, 0xEB, 0x9B, 0x56, 0xE0, | |
1164 | 0xC1, 0x10, 0x56, 0xFA, 0xE6, 0xA3 | |
1165 | }; | |
1166 | return do_create_ec_explicit_trinomial_params(bld, gen, sizeof(gen)); | |
1167 | } | |
1168 | ||
1169 | static int create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld) | |
1170 | { | |
1171 | static const unsigned char gen2[] = { | |
1172 | 0x04, | |
1173 | 0x00, 0xd7, 0xba, 0xd0, 0x26, 0x6c, 0x31, 0x6a, 0x78, 0x76, 0x01, 0xd1, | |
1174 | 0x32, 0x4b, 0x8f, 0x30, 0x29, 0x2d, 0x78, 0x30, 0xca, 0x43, 0xaa, 0xf0, | |
1175 | 0xa2, 0x5a, 0xd4, 0x0f, 0xb3, 0xf4, | |
1176 | 0x00, 0x85, 0x4b, 0x1b, 0x8d, 0x50, 0x10, 0xa5, 0x1c, 0x80, 0xf7, 0x86, | |
1177 | 0x40, 0x62, 0x4c, 0x87, 0xd1, 0x26, 0x7a, 0x9c, 0x5c, 0xe9, 0x82, 0x29, | |
1178 | 0xd1, 0x67, 0x70, 0x41, 0xea, 0xcb | |
1179 | }; | |
1180 | return do_create_ec_explicit_trinomial_params(bld, gen2, sizeof(gen2)); | |
1181 | } | |
1182 | # endif /* OPENSSL_NO_EC2M */ | |
1183 | #endif /* OPENSSL_NO_EC */ | |
1184 | ||
5a23d78c RL |
1185 | int setup_tests(void) |
1186 | { | |
5faec149 RL |
1187 | # ifndef OPENSSL_NO_RC4 |
1188 | int use_legacy = OSSL_PROVIDER_available(NULL, "legacy"); | |
1189 | #endif | |
7c664b1f RL |
1190 | int ok = 1; |
1191 | ||
a7922e20 RL |
1192 | #ifndef OPENSSL_NO_DSA |
1193 | static size_t qbits = 160; /* PVK only tolerates 160 Q bits */ | |
1194 | static size_t pbits = 1024; /* With 160 Q bits, we MUST use 1024 P bits */ | |
1195 | OSSL_PARAM DSA_params[] = { | |
1196 | OSSL_PARAM_size_t("pbits", &pbits), | |
1197 | OSSL_PARAM_size_t("qbits", &qbits), | |
1198 | OSSL_PARAM_END | |
1199 | }; | |
1200 | #endif | |
1201 | ||
7c664b1f RL |
1202 | #ifndef OPENSSL_NO_EC |
1203 | static char groupname[] = "prime256v1"; | |
1204 | OSSL_PARAM EC_params[] = { | |
1205 | OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1), | |
1206 | OSSL_PARAM_END | |
1207 | }; | |
1208 | #endif | |
1209 | ||
1210 | /* 7 is the default magic number */ | |
1211 | static unsigned int rsapss_min_saltlen = 7; | |
1212 | OSSL_PARAM RSA_PSS_params[] = { | |
1213 | OSSL_PARAM_uint("saltlen", &rsapss_min_saltlen), | |
1214 | OSSL_PARAM_END | |
1215 | }; | |
1216 | ||
c0f39ded SL |
1217 | #ifndef OPENSSL_NO_EC |
1218 | if (!TEST_ptr(bnctx = BN_CTX_new_ex(NULL)) | |
1219 | || !TEST_ptr(bld_prime_nc = OSSL_PARAM_BLD_new()) | |
1220 | || !TEST_ptr(bld_prime = OSSL_PARAM_BLD_new()) | |
1221 | || !create_ec_explicit_prime_params_namedcurve(bld_prime_nc) | |
1222 | || !create_ec_explicit_prime_params(bld_prime) | |
1223 | || !TEST_ptr(ec_explicit_prime_params_nc = OSSL_PARAM_BLD_to_param(bld_prime_nc)) | |
1224 | || !TEST_ptr(ec_explicit_prime_params_explicit = OSSL_PARAM_BLD_to_param(bld_prime)) | |
1225 | # ifndef OPENSSL_NO_EC2M | |
1226 | || !TEST_ptr(bld_tri_nc = OSSL_PARAM_BLD_new()) | |
1227 | || !TEST_ptr(bld_tri = OSSL_PARAM_BLD_new()) | |
1228 | || !create_ec_explicit_trinomial_params_namedcurve(bld_tri_nc) | |
1229 | || !create_ec_explicit_trinomial_params(bld_tri) | |
1230 | || !TEST_ptr(ec_explicit_tri_params_nc = OSSL_PARAM_BLD_to_param(bld_tri_nc)) | |
1231 | || !TEST_ptr(ec_explicit_tri_params_explicit = OSSL_PARAM_BLD_to_param(bld_tri)) | |
1232 | # endif | |
1233 | ) | |
1234 | return 0; | |
1235 | #endif | |
1236 | ||
e2ac846e | 1237 | TEST_info("Generating keys..."); |
c0f39ded | 1238 | |
7c664b1f RL |
1239 | #ifndef OPENSSL_NO_DH |
1240 | MAKE_DOMAIN_KEYS(DH, "DH", NULL); | |
31d2daec | 1241 | MAKE_DOMAIN_KEYS(DHX, "X9.42 DH", NULL); |
5658470c | 1242 | TEST_info("Generating keys...DH done"); |
7c664b1f RL |
1243 | #endif |
1244 | #ifndef OPENSSL_NO_DSA | |
a7922e20 | 1245 | MAKE_DOMAIN_KEYS(DSA, "DSA", DSA_params); |
5658470c | 1246 | TEST_info("Generating keys...DSA done"); |
7c664b1f RL |
1247 | #endif |
1248 | #ifndef OPENSSL_NO_EC | |
1249 | MAKE_DOMAIN_KEYS(EC, "EC", EC_params); | |
c0f39ded SL |
1250 | MAKE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve, "EC", ec_explicit_prime_params_nc); |
1251 | MAKE_DOMAIN_KEYS(ECExplicitPrime2G, "EC", ec_explicit_prime_params_explicit); | |
1252 | # ifndef OPENSSL_NO_EC2M | |
1253 | MAKE_DOMAIN_KEYS(ECExplicitTriNamedCurve, "EC", ec_explicit_tri_params_nc); | |
1254 | MAKE_DOMAIN_KEYS(ECExplicitTri2G, "EC", ec_explicit_tri_params_explicit); | |
1255 | # endif | |
7c664b1f RL |
1256 | MAKE_KEYS(ED25519, "ED25519", NULL); |
1257 | MAKE_KEYS(ED448, "ED448", NULL); | |
1258 | MAKE_KEYS(X25519, "X25519", NULL); | |
1259 | MAKE_KEYS(X448, "X448", NULL); | |
5658470c | 1260 | TEST_info("Generating keys...EC done"); |
7c664b1f RL |
1261 | #endif |
1262 | MAKE_KEYS(RSA, "RSA", NULL); | |
5658470c | 1263 | TEST_info("Generating keys...RSA done"); |
7c664b1f | 1264 | MAKE_KEYS(RSA_PSS, "RSA-PSS", RSA_PSS_params); |
5658470c | 1265 | TEST_info("Generating keys...RSA_PSS done"); |
5a23d78c | 1266 | |
7c664b1f RL |
1267 | if (ok) { |
1268 | #ifndef OPENSSL_NO_DH | |
1269 | ADD_TEST_SUITE(DH); | |
b565a17d | 1270 | ADD_TEST_SUITE_PARAMS(DH); |
31d2daec | 1271 | ADD_TEST_SUITE(DHX); |
b565a17d | 1272 | ADD_TEST_SUITE_PARAMS(DHX); |
bddfea02 RL |
1273 | /* |
1274 | * DH has no support for PEM_write_bio_PrivateKey_traditional(), | |
1275 | * so no legacy tests. | |
1276 | */ | |
7c664b1f RL |
1277 | #endif |
1278 | #ifndef OPENSSL_NO_DSA | |
1279 | ADD_TEST_SUITE(DSA); | |
b565a17d | 1280 | ADD_TEST_SUITE_PARAMS(DSA); |
bddfea02 | 1281 | ADD_TEST_SUITE_LEGACY(DSA); |
a7922e20 | 1282 | ADD_TEST_SUITE_MSBLOB(DSA); |
5faec149 RL |
1283 | ADD_TEST_SUITE_UNPROTECTED_PVK(DSA); |
1284 | # ifndef OPENSSL_NO_RC4 | |
1285 | if (use_legacy) { | |
1286 | ADD_TEST_SUITE_PROTECTED_PVK(DSA); | |
1287 | } | |
1288 | # endif | |
7c664b1f RL |
1289 | #endif |
1290 | #ifndef OPENSSL_NO_EC | |
1291 | ADD_TEST_SUITE(EC); | |
b565a17d | 1292 | ADD_TEST_SUITE_PARAMS(EC); |
bddfea02 | 1293 | ADD_TEST_SUITE_LEGACY(EC); |
c0f39ded | 1294 | ADD_TEST_SUITE(ECExplicitPrimeNamedCurve); |
bddfea02 | 1295 | ADD_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve); |
c0f39ded | 1296 | ADD_TEST_SUITE(ECExplicitPrime2G); |
bddfea02 | 1297 | ADD_TEST_SUITE_LEGACY(ECExplicitPrime2G); |
c0f39ded SL |
1298 | # ifndef OPENSSL_NO_EC2M |
1299 | ADD_TEST_SUITE(ECExplicitTriNamedCurve); | |
bddfea02 | 1300 | ADD_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve); |
c0f39ded | 1301 | ADD_TEST_SUITE(ECExplicitTri2G); |
bddfea02 | 1302 | ADD_TEST_SUITE_LEGACY(ECExplicitTri2G); |
c0f39ded | 1303 | # endif |
7c664b1f RL |
1304 | ADD_TEST_SUITE(ED25519); |
1305 | ADD_TEST_SUITE(ED448); | |
1306 | ADD_TEST_SUITE(X25519); | |
1307 | ADD_TEST_SUITE(X448); | |
bddfea02 RL |
1308 | /* |
1309 | * ED25519, ED448, X25519 and X448 have no support for | |
1310 | * PEM_write_bio_PrivateKey_traditional(), so no legacy tests. | |
1311 | */ | |
7c664b1f RL |
1312 | #endif |
1313 | ADD_TEST_SUITE(RSA); | |
bddfea02 | 1314 | ADD_TEST_SUITE_LEGACY(RSA); |
7c664b1f | 1315 | ADD_TEST_SUITE(RSA_PSS); |
bddfea02 RL |
1316 | /* |
1317 | * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(), | |
1318 | * so no legacy tests. | |
1319 | */ | |
a7922e20 | 1320 | ADD_TEST_SUITE_MSBLOB(RSA); |
5faec149 RL |
1321 | ADD_TEST_SUITE_UNPROTECTED_PVK(RSA); |
1322 | # ifndef OPENSSL_NO_RC4 | |
1323 | if (use_legacy) { | |
1324 | ADD_TEST_SUITE_PROTECTED_PVK(RSA); | |
1325 | } | |
1326 | # endif | |
7c664b1f | 1327 | } |
5a23d78c RL |
1328 | |
1329 | return 1; | |
1330 | } | |
7c664b1f RL |
1331 | |
1332 | void cleanup_tests(void) | |
1333 | { | |
c0f39ded | 1334 | #ifndef OPENSSL_NO_EC |
3f883c7c SL |
1335 | OSSL_PARAM_free(ec_explicit_prime_params_nc); |
1336 | OSSL_PARAM_free(ec_explicit_prime_params_explicit); | |
c0f39ded SL |
1337 | OSSL_PARAM_BLD_free(bld_prime_nc); |
1338 | OSSL_PARAM_BLD_free(bld_prime); | |
1339 | # ifndef OPENSSL_NO_EC2M | |
3f883c7c SL |
1340 | OSSL_PARAM_free(ec_explicit_tri_params_nc); |
1341 | OSSL_PARAM_free(ec_explicit_tri_params_explicit); | |
c0f39ded SL |
1342 | OSSL_PARAM_BLD_free(bld_tri_nc); |
1343 | OSSL_PARAM_BLD_free(bld_tri); | |
1344 | # endif | |
1345 | BN_CTX_free(bnctx); | |
1346 | #endif /* OPENSSL_NO_EC */ | |
1347 | ||
7c664b1f RL |
1348 | #ifndef OPENSSL_NO_DH |
1349 | FREE_DOMAIN_KEYS(DH); | |
31d2daec | 1350 | FREE_DOMAIN_KEYS(DHX); |
7c664b1f RL |
1351 | #endif |
1352 | #ifndef OPENSSL_NO_DSA | |
1353 | FREE_DOMAIN_KEYS(DSA); | |
1354 | #endif | |
1355 | #ifndef OPENSSL_NO_EC | |
1356 | FREE_DOMAIN_KEYS(EC); | |
c0f39ded SL |
1357 | FREE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve); |
1358 | FREE_DOMAIN_KEYS(ECExplicitPrime2G); | |
1359 | # ifndef OPENSSL_NO_EC2M | |
1360 | FREE_DOMAIN_KEYS(ECExplicitTriNamedCurve); | |
1361 | FREE_DOMAIN_KEYS(ECExplicitTri2G); | |
1362 | # endif | |
7c664b1f RL |
1363 | FREE_KEYS(ED25519); |
1364 | FREE_KEYS(ED448); | |
1365 | FREE_KEYS(X25519); | |
1366 | FREE_KEYS(X448); | |
1367 | #endif | |
1368 | FREE_KEYS(RSA); | |
1369 | FREE_KEYS(RSA_PSS); | |
1370 | } |