2 * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
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
11 #include <openssl/core_dispatch.h>
12 #include <openssl/evp.h>
13 #include <openssl/pem.h>
14 #include <openssl/rsa.h>
15 #include <openssl/x509.h>
16 #include <openssl/core_names.h>
17 #include <openssl/params.h>
18 #include <openssl/param_build.h>
19 #include <openssl/encoder.h>
20 #include <openssl/decoder.h>
22 #include "internal/cryptlib.h" /* ossl_assert */
23 #include "crypto/pem.h" /* For PVK and "blob" PEM headers */
24 #include "crypto/evp.h" /* For evp_pkey_is_provided() */
26 #include "helpers/predefined_dhparams.h"
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)
40 #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
41 # define OPENSSL_NO_KEYPARAMS
44 static int default_libctx = 1;
45 static int is_fips = 0;
47 static OSSL_LIB_CTX *testctx = NULL;
48 static OSSL_LIB_CTX *keyctx = NULL;
49 static char *testpropq = NULL;
51 static OSSL_PROVIDER *nullprov = NULL;
52 static OSSL_PROVIDER *deflprov = NULL;
53 static OSSL_PROVIDER *keyprov = NULL;
56 static BN_CTX *bnctx = NULL;
57 static OSSL_PARAM_BLD *bld_prime_nc = NULL;
58 static OSSL_PARAM_BLD *bld_prime = NULL;
59 static OSSL_PARAM *ec_explicit_prime_params_nc = NULL;
60 static OSSL_PARAM *ec_explicit_prime_params_explicit = NULL;
62 # ifndef OPENSSL_NO_EC2M
63 static OSSL_PARAM_BLD *bld_tri_nc = NULL;
64 static OSSL_PARAM_BLD *bld_tri = NULL;
65 static OSSL_PARAM *ec_explicit_tri_params_nc = NULL;
66 static OSSL_PARAM *ec_explicit_tri_params_explicit = NULL;
70 #ifndef OPENSSL_NO_KEYPARAMS
71 static EVP_PKEY *make_template(const char *type, OSSL_PARAM *genparams)
73 EVP_PKEY *pkey = NULL;
74 EVP_PKEY_CTX *ctx = NULL;
76 # ifndef OPENSSL_NO_DH
78 * Use 512-bit DH(X) keys with predetermined parameters for efficiency,
79 * for testing only. Use a minimum key size of 2048 for security purposes.
81 if (strcmp(type, "DH") == 0)
82 return get_dh512(keyctx);
84 if (strcmp(type, "X9.42 DH") == 0)
85 return get_dhx512(keyctx);
89 * No real need to check the errors other than for the cascade
90 * effect. |pkey| will simply remain NULL if something goes wrong.
92 (void)((ctx = EVP_PKEY_CTX_new_from_name(keyctx, type, testpropq)) != NULL
93 && EVP_PKEY_paramgen_init(ctx) > 0
95 || EVP_PKEY_CTX_set_params(ctx, genparams) > 0)
96 && EVP_PKEY_generate(ctx, &pkey) > 0);
97 EVP_PKEY_CTX_free(ctx);
103 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
104 static EVP_PKEY *make_key(const char *type, EVP_PKEY *template,
105 OSSL_PARAM *genparams)
107 EVP_PKEY *pkey = NULL;
110 ? EVP_PKEY_CTX_new_from_pkey(keyctx, template, testpropq)
111 : EVP_PKEY_CTX_new_from_name(keyctx, type, testpropq);
114 * No real need to check the errors other than for the cascade
115 * effect. |pkey| will simply remain NULL if something goes wrong.
118 && EVP_PKEY_keygen_init(ctx) > 0
119 && (genparams == NULL
120 || EVP_PKEY_CTX_set_params(ctx, genparams) > 0)
121 && EVP_PKEY_keygen(ctx, &pkey) > 0);
122 EVP_PKEY_CTX_free(ctx);
127 /* Main test driver */
129 typedef int (encoder)(const char *file, const int line,
130 void **encoded, long *encoded_len,
131 void *object, int selection,
132 const char *output_type, const char *output_structure,
133 const char *pass, const char *pcipher);
134 typedef int (decoder)(const char *file, const int line,
135 void **object, void *encoded, long encoded_len,
136 const char *input_type, const char *structure_type,
137 const char *keytype, int selection, const char *pass);
138 typedef int (tester)(const char *file, const int line,
139 const void *data1, size_t data1_len,
140 const void *data2, size_t data2_len);
141 typedef int (checker)(const char *file, const int line,
142 const char *type, const void *data, size_t data_len);
143 typedef void (dumper)(const char *label, const void *data, size_t data_len);
145 #define FLAG_DECODE_WITH_TYPE 0x0001
147 static int test_encode_decode(const char *file, const int line,
148 const char *type, EVP_PKEY *pkey,
149 int selection, const char *output_type,
150 const char *output_structure,
151 const char *pass, const char *pcipher,
152 encoder *encode_cb, decoder *decode_cb,
153 tester *test_cb, checker *check_cb,
154 dumper *dump_cb, int flags)
156 void *encoded = NULL;
157 long encoded_len = 0;
158 EVP_PKEY *pkey2 = NULL;
159 void *encoded2 = NULL;
160 long encoded2_len = 0;
164 * Encode |pkey|, decode the result into |pkey2|, and finish off by
165 * encoding |pkey2| as well. That last encoding is for checking and
168 if (!TEST_true(encode_cb(file, line, &encoded, &encoded_len, pkey, selection,
169 output_type, output_structure, pass, pcipher))
170 || !TEST_true(check_cb(file, line, type, encoded, encoded_len))
171 || !TEST_true(decode_cb(file, line, (void **)&pkey2, encoded, encoded_len,
172 output_type, output_structure,
173 (flags & FLAG_DECODE_WITH_TYPE ? type : NULL),
175 || !TEST_true(encode_cb(file, line, &encoded2, &encoded2_len, pkey2, selection,
176 output_type, output_structure, pass, pcipher)))
179 if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) {
180 if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey, pkey2), 1))
183 if (!TEST_int_eq(EVP_PKEY_eq(pkey, pkey2), 1))
188 * Double check the encoding, but only for unprotected keys,
189 * as protected keys have a random component, which makes the output
192 if ((pass == NULL && pcipher == NULL)
193 && !test_cb(file, line, encoded, encoded_len, encoded2, encoded2_len))
199 if (encoded != NULL && encoded_len != 0)
200 dump_cb("|pkey| encoded", encoded, encoded_len);
201 if (encoded2 != NULL && encoded2_len != 0)
202 dump_cb("|pkey2| encoded", encoded2, encoded2_len);
205 OPENSSL_free(encoded);
206 OPENSSL_free(encoded2);
207 EVP_PKEY_free(pkey2);
211 /* Encoding and decoding methods */
213 static int encode_EVP_PKEY_prov(const char *file, const int line,
214 void **encoded, long *encoded_len,
215 void *object, int selection,
216 const char *output_type,
217 const char *output_structure,
218 const char *pass, const char *pcipher)
220 EVP_PKEY *pkey = object;
221 OSSL_ENCODER_CTX *ectx = NULL;
223 BUF_MEM *mem_buf = NULL;
224 const unsigned char *upass = (const unsigned char *)pass;
227 if (!TEST_FL_ptr(ectx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection,
231 || !TEST_FL_int_gt(OSSL_ENCODER_CTX_get_num_encoders(ectx), 0)
233 && !TEST_FL_true(OSSL_ENCODER_CTX_set_passphrase(ectx, upass,
236 && !TEST_FL_true(OSSL_ENCODER_CTX_set_cipher(ectx, pcipher, NULL)))
237 || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem()))
238 || !TEST_FL_true(OSSL_ENCODER_to_bio(ectx, mem_ser))
239 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
240 || !TEST_FL_ptr(*encoded = mem_buf->data)
241 || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0))
244 /* Detach the encoded output */
245 mem_buf->data = NULL;
250 OSSL_ENCODER_CTX_free(ectx);
254 static int decode_EVP_PKEY_prov(const char *file, const int line,
255 void **object, void *encoded, long encoded_len,
256 const char *input_type,
257 const char *structure_type,
258 const char *keytype, int selection,
261 EVP_PKEY *pkey = NULL, *testpkey = NULL;
262 OSSL_DECODER_CTX *dctx = NULL;
263 BIO *encoded_bio = NULL;
264 const unsigned char *upass = (const unsigned char *)pass;
269 if (strcmp(input_type, "DER") == 0)
274 if (!TEST_FL_ptr(encoded_bio = BIO_new_mem_buf(encoded, encoded_len)))
278 * We attempt the decode 3 times. The first time we provide the expected
279 * starting input type. The second time we provide NULL for the starting
280 * type. The third time we provide a bad starting input type.
281 * The bad starting input type should fail. The other two should succeed
282 * and produce the same result.
284 for (i = 0; i < 3; i++) {
285 const char *testtype = (i == 0) ? input_type
286 : ((i == 1) ? NULL : badtype);
288 if (!TEST_FL_ptr(dctx = OSSL_DECODER_CTX_new_for_pkey(&testpkey,
295 && !OSSL_DECODER_CTX_set_passphrase(dctx, upass, strlen(pass)))
296 || !TEST_FL_int_gt(BIO_reset(encoded_bio), 0)
297 /* We expect to fail when using a bad input type */
298 || !TEST_FL_int_eq(OSSL_DECODER_from_bio(dctx, encoded_bio),
301 OSSL_DECODER_CTX_free(dctx);
308 if (selection == OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS) {
309 if (!TEST_FL_int_eq(EVP_PKEY_parameters_eq(pkey, testpkey), 1))
312 if (!TEST_FL_int_eq(EVP_PKEY_eq(pkey, testpkey), 1))
323 EVP_PKEY_free(testpkey);
324 BIO_free(encoded_bio);
325 OSSL_DECODER_CTX_free(dctx);
329 static int encode_EVP_PKEY_legacy_PEM(const char *file, const int line,
330 void **encoded, long *encoded_len,
331 void *object, ossl_unused int selection,
332 ossl_unused const char *output_type,
333 ossl_unused const char *output_structure,
334 const char *pass, const char *pcipher)
336 EVP_PKEY *pkey = object;
337 EVP_CIPHER *cipher = NULL;
339 BUF_MEM *mem_buf = NULL;
340 const unsigned char *upass = (const unsigned char *)pass;
344 if (pcipher != NULL && pass != NULL) {
345 passlen = strlen(pass);
346 if (!TEST_FL_ptr(cipher = EVP_CIPHER_fetch(testctx, pcipher, testpropq)))
349 if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem()))
350 || !TEST_FL_true(PEM_write_bio_PrivateKey_traditional(mem_ser, pkey,
354 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
355 || !TEST_FL_ptr(*encoded = mem_buf->data)
356 || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0))
359 /* Detach the encoded output */
360 mem_buf->data = NULL;
365 EVP_CIPHER_free(cipher);
369 static int encode_EVP_PKEY_MSBLOB(const char *file, const int line,
370 void **encoded, long *encoded_len,
371 void *object, int selection,
372 ossl_unused const char *output_type,
373 ossl_unused const char *output_structure,
374 ossl_unused const char *pass,
375 ossl_unused const char *pcipher)
377 EVP_PKEY *pkey = object;
379 BUF_MEM *mem_buf = NULL;
382 if (!TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem())))
385 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
386 if (!TEST_FL_int_ge(i2b_PrivateKey_bio(mem_ser, pkey), 0))
389 if (!TEST_FL_int_ge(i2b_PublicKey_bio(mem_ser, pkey), 0))
393 if (!TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
394 || !TEST_FL_ptr(*encoded = mem_buf->data)
395 || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0))
398 /* Detach the encoded output */
399 mem_buf->data = NULL;
407 static pem_password_cb pass_pw;
408 static int pass_pw(char *buf, int size, int rwflag, void *userdata)
410 OPENSSL_strlcpy(buf, userdata, size);
411 return strlen(userdata);
414 static int encode_EVP_PKEY_PVK(const char *file, const int line,
415 void **encoded, long *encoded_len,
416 void *object, int selection,
417 ossl_unused const char *output_type,
418 ossl_unused const char *output_structure,
420 ossl_unused const char *pcipher)
422 EVP_PKEY *pkey = object;
424 BUF_MEM *mem_buf = NULL;
425 int enc = (pass != NULL);
428 if (!TEST_FL_true(ossl_assert((selection
429 & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0))
430 || !TEST_FL_ptr(mem_ser = BIO_new(BIO_s_mem()))
431 || !TEST_FL_int_ge(i2b_PVK_bio_ex(mem_ser, pkey, enc,
432 pass_pw, (void *)pass, testctx, testpropq), 0)
433 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser, &mem_buf) > 0)
434 || !TEST_FL_ptr(*encoded = mem_buf->data)
435 || !TEST_FL_long_gt(*encoded_len = mem_buf->length, 0))
438 /* Detach the encoded output */
439 mem_buf->data = NULL;
447 static int test_text(const char *file, const int line,
448 const void *data1, size_t data1_len,
449 const void *data2, size_t data2_len)
451 return TEST_FL_strn2_eq(data1, data1_len, data2, data2_len);
454 static int test_mem(const char *file, const int line,
455 const void *data1, size_t data1_len,
456 const void *data2, size_t data2_len)
458 return TEST_FL_mem_eq(data1, data1_len, data2, data2_len);
461 /* Test cases and their dumpers / checkers */
463 static void collect_name(const char *name, void *arg)
465 char **namelist = arg;
469 space = strlen(name);
470 if (*namelist != NULL)
471 space += strlen(*namelist) + 2 /* for comma and space */;
472 space++; /* for terminating null byte */
474 new_namelist = OPENSSL_realloc(*namelist, space);
475 if (new_namelist == NULL)
477 if (*namelist != NULL) {
478 strcat(new_namelist, ", ");
479 strcat(new_namelist, name);
481 strcpy(new_namelist, name);
483 *namelist = new_namelist;
486 static void dump_der(const char *label, const void *data, size_t data_len)
488 test_output_memory(label, data, data_len);
491 static void dump_pem(const char *label, const void *data, size_t data_len)
493 test_output_string(label, data, data_len - 1);
496 static int check_unprotected_PKCS8_DER(const char *file, const int line,
498 const void *data, size_t data_len)
500 const unsigned char *datap = data;
501 PKCS8_PRIV_KEY_INFO *p8inf =
502 d2i_PKCS8_PRIV_KEY_INFO(NULL, &datap, data_len);
505 if (TEST_FL_ptr(p8inf)) {
506 EVP_PKEY *pkey = EVP_PKCS82PKEY_ex(p8inf, testctx, testpropq);
507 char *namelist = NULL;
509 if (TEST_FL_ptr(pkey)) {
510 if (!(ok = TEST_FL_true(EVP_PKEY_is_a(pkey, type)))) {
511 EVP_PKEY_type_names_do_all(pkey, collect_name, &namelist);
512 if (namelist != NULL)
513 TEST_note("%s isn't any of %s", type, namelist);
514 OPENSSL_free(namelist);
516 ok = ok && TEST_FL_true(evp_pkey_is_provided(pkey));
520 PKCS8_PRIV_KEY_INFO_free(p8inf);
524 static int test_unprotected_via_DER(const char *type, EVP_PKEY *key)
526 return test_encode_decode(__FILE__, __LINE__, type, key,
527 OSSL_KEYMGMT_SELECT_KEYPAIR
528 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
529 "DER", "pkcs8", NULL, NULL,
530 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
531 test_mem, check_unprotected_PKCS8_DER,
535 static int check_unprotected_PKCS8_PEM(const char *file, const int line,
537 const void *data, size_t data_len)
539 static const char expected_pem_header[] =
540 "-----BEGIN " PEM_STRING_PKCS8INF "-----";
542 return TEST_FL_strn_eq(data, expected_pem_header,
543 sizeof(expected_pem_header) - 1);
546 static int test_unprotected_via_PEM(const char *type, EVP_PKEY *key)
548 return test_encode_decode(__FILE__, __LINE__, type, key,
549 OSSL_KEYMGMT_SELECT_KEYPAIR
550 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
551 "PEM", "pkcs8", NULL, NULL,
552 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
553 test_text, check_unprotected_PKCS8_PEM,
557 #ifndef OPENSSL_NO_KEYPARAMS
558 static int check_params_DER(const char *file, const int line,
559 const char *type, const void *data, size_t data_len)
561 const unsigned char *datap = data;
563 int itype = NID_undef;
564 EVP_PKEY *pkey = NULL;
566 if (strcmp(type, "DH") == 0)
568 else if (strcmp(type, "X9.42 DH") == 0)
569 itype = EVP_PKEY_DHX;
570 else if (strcmp(type, "DSA") == 0)
571 itype = EVP_PKEY_DSA;
572 else if (strcmp(type, "EC") == 0)
575 if (itype != NID_undef) {
576 pkey = d2i_KeyParams(itype, NULL, &datap, data_len);
584 static int check_params_PEM(const char *file, const int line,
586 const void *data, size_t data_len)
588 static char expected_pem_header[80];
591 TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
592 sizeof(expected_pem_header),
593 "-----BEGIN %s PARAMETERS-----", type), 0)
594 && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header));
597 static int test_params_via_DER(const char *type, EVP_PKEY *key)
599 return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
600 "DER", "type-specific", NULL, NULL,
601 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
602 test_mem, check_params_DER,
603 dump_der, FLAG_DECODE_WITH_TYPE);
606 static int test_params_via_PEM(const char *type, EVP_PKEY *key)
608 return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
609 "PEM", "type-specific", NULL, NULL,
610 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
611 test_text, check_params_PEM,
614 #endif /* !OPENSSL_NO_KEYPARAMS */
616 static int check_unprotected_legacy_PEM(const char *file, const int line,
618 const void *data, size_t data_len)
620 static char expected_pem_header[80];
623 TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
624 sizeof(expected_pem_header),
625 "-----BEGIN %s PRIVATE KEY-----", type), 0)
626 && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header));
629 static int test_unprotected_via_legacy_PEM(const char *type, EVP_PKEY *key)
631 if (!default_libctx || is_fips)
632 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
634 return test_encode_decode(__FILE__, __LINE__, type, key,
635 OSSL_KEYMGMT_SELECT_KEYPAIR
636 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
637 "PEM", "type-specific", NULL, NULL,
638 encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov,
639 test_text, check_unprotected_legacy_PEM,
643 static int check_MSBLOB(const char *file, const int line,
644 const char *type, const void *data, size_t data_len)
646 const unsigned char *datap = data;
647 EVP_PKEY *pkey = b2i_PrivateKey(&datap, data_len);
648 int ok = TEST_FL_ptr(pkey);
654 static int test_unprotected_via_MSBLOB(const char *type, EVP_PKEY *key)
656 return test_encode_decode(__FILE__, __LINE__, type, key,
657 OSSL_KEYMGMT_SELECT_KEYPAIR
658 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
659 "MSBLOB", NULL, NULL, NULL,
660 encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov,
661 test_mem, check_MSBLOB,
665 static int check_PVK(const char *file, const int line,
666 const char *type, const void *data, size_t data_len)
668 const unsigned char *in = data;
669 unsigned int saltlen = 0, keylen = 0;
670 int ok = ossl_do_PVK_header(&in, data_len, 0, &saltlen, &keylen);
675 static int test_unprotected_via_PVK(const char *type, EVP_PKEY *key)
677 return test_encode_decode(__FILE__, __LINE__, type, key,
678 OSSL_KEYMGMT_SELECT_KEYPAIR
679 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
680 "PVK", NULL, NULL, NULL,
681 encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov,
686 static const char *pass_cipher = "AES-256-CBC";
687 static const char *pass = "the holy handgrenade of antioch";
689 static int check_protected_PKCS8_DER(const char *file, const int line,
691 const void *data, size_t data_len)
693 const unsigned char *datap = data;
694 X509_SIG *p8 = d2i_X509_SIG(NULL, &datap, data_len);
695 int ok = TEST_FL_ptr(p8);
701 static int test_protected_via_DER(const char *type, EVP_PKEY *key)
703 return test_encode_decode(__FILE__, __LINE__, type, key,
704 OSSL_KEYMGMT_SELECT_KEYPAIR
705 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
706 "DER", "pkcs8", pass, pass_cipher,
707 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
708 test_mem, check_protected_PKCS8_DER,
712 static int check_protected_PKCS8_PEM(const char *file, const int line,
714 const void *data, size_t data_len)
716 static const char expected_pem_header[] =
717 "-----BEGIN " PEM_STRING_PKCS8 "-----";
719 return TEST_FL_strn_eq(data, expected_pem_header,
720 sizeof(expected_pem_header) - 1);
723 static int test_protected_via_PEM(const char *type, EVP_PKEY *key)
725 return test_encode_decode(__FILE__, __LINE__, type, key,
726 OSSL_KEYMGMT_SELECT_KEYPAIR
727 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
728 "PEM", "pkcs8", pass, pass_cipher,
729 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
730 test_text, check_protected_PKCS8_PEM,
734 static int check_protected_legacy_PEM(const char *file, const int line,
736 const void *data, size_t data_len)
738 static char expected_pem_header[80];
741 TEST_FL_int_gt(BIO_snprintf(expected_pem_header,
742 sizeof(expected_pem_header),
743 "-----BEGIN %s PRIVATE KEY-----", type), 0)
744 && TEST_FL_strn_eq(data, expected_pem_header, strlen(expected_pem_header))
745 && TEST_FL_ptr(strstr(data, "\nDEK-Info: "));
748 static int test_protected_via_legacy_PEM(const char *type, EVP_PKEY *key)
750 if (!default_libctx || is_fips)
751 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
753 return test_encode_decode(__FILE__, __LINE__, type, key,
754 OSSL_KEYMGMT_SELECT_KEYPAIR
755 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
756 "PEM", "type-specific", pass, pass_cipher,
757 encode_EVP_PKEY_legacy_PEM, decode_EVP_PKEY_prov,
758 test_text, check_protected_legacy_PEM,
762 #ifndef OPENSSL_NO_RC4
763 static int test_protected_via_PVK(const char *type, EVP_PKEY *key)
766 OSSL_PROVIDER *lgcyprov = OSSL_PROVIDER_load(testctx, "legacy");
767 if (lgcyprov == NULL)
768 return TEST_skip("Legacy provider not available");
770 ret = test_encode_decode(__FILE__, __LINE__, type, key,
771 OSSL_KEYMGMT_SELECT_KEYPAIR
772 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
773 "PVK", NULL, pass, NULL,
774 encode_EVP_PKEY_PVK, decode_EVP_PKEY_prov,
775 test_mem, check_PVK, dump_der, 0);
776 OSSL_PROVIDER_unload(lgcyprov);
781 static int check_public_DER(const char *file, const int line,
782 const char *type, const void *data, size_t data_len)
784 const unsigned char *datap = data;
785 EVP_PKEY *pkey = d2i_PUBKEY_ex(NULL, &datap, data_len, testctx, testpropq);
786 int ok = (TEST_FL_ptr(pkey) && TEST_FL_true(EVP_PKEY_is_a(pkey, type)));
792 static int test_public_via_DER(const char *type, EVP_PKEY *key)
794 return test_encode_decode(__FILE__, __LINE__, type, key,
795 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
796 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
797 "DER", "SubjectPublicKeyInfo", NULL, NULL,
798 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
799 test_mem, check_public_DER, dump_der, 0);
802 static int check_public_PEM(const char *file, const int line,
803 const char *type, const void *data, size_t data_len)
805 static const char expected_pem_header[] =
806 "-----BEGIN " PEM_STRING_PUBLIC "-----";
809 TEST_FL_strn_eq(data, expected_pem_header,
810 sizeof(expected_pem_header) - 1);
813 static int test_public_via_PEM(const char *type, EVP_PKEY *key)
815 return test_encode_decode(__FILE__, __LINE__, type, key,
816 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
817 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS,
818 "PEM", "SubjectPublicKeyInfo", NULL, NULL,
819 encode_EVP_PKEY_prov, decode_EVP_PKEY_prov,
820 test_text, check_public_PEM, dump_pem, 0);
823 static int check_public_MSBLOB(const char *file, const int line,
825 const void *data, size_t data_len)
827 const unsigned char *datap = data;
828 EVP_PKEY *pkey = b2i_PublicKey(&datap, data_len);
829 int ok = TEST_FL_ptr(pkey);
835 static int test_public_via_MSBLOB(const char *type, EVP_PKEY *key)
837 return test_encode_decode(__FILE__, __LINE__, type, key, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
838 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS,
839 "MSBLOB", NULL, NULL, NULL,
840 encode_EVP_PKEY_MSBLOB, decode_EVP_PKEY_prov,
841 test_mem, check_public_MSBLOB, dump_der, 0);
844 #define KEYS(KEYTYPE) \
845 static EVP_PKEY *key_##KEYTYPE = NULL
846 #define MAKE_KEYS(KEYTYPE, KEYTYPEstr, params) \
848 && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, NULL, params))
849 #define FREE_KEYS(KEYTYPE) \
850 EVP_PKEY_free(key_##KEYTYPE); \
852 #define DOMAIN_KEYS(KEYTYPE) \
853 static EVP_PKEY *template_##KEYTYPE = NULL; \
854 static EVP_PKEY *key_##KEYTYPE = NULL
855 #define MAKE_DOMAIN_KEYS(KEYTYPE, KEYTYPEstr, params) \
857 && TEST_ptr(template_##KEYTYPE = \
858 make_template(KEYTYPEstr, params)) \
859 && TEST_ptr(key_##KEYTYPE = \
860 make_key(KEYTYPEstr, template_##KEYTYPE, NULL))
861 #define FREE_DOMAIN_KEYS(KEYTYPE) \
862 EVP_PKEY_free(template_##KEYTYPE); \
863 EVP_PKEY_free(key_##KEYTYPE)
865 #define IMPLEMENT_TEST_SUITE(KEYTYPE, KEYTYPEstr) \
866 static int test_unprotected_##KEYTYPE##_via_DER(void) \
868 return test_unprotected_via_DER(KEYTYPEstr, key_##KEYTYPE); \
870 static int test_unprotected_##KEYTYPE##_via_PEM(void) \
872 return test_unprotected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
874 static int test_protected_##KEYTYPE##_via_DER(void) \
876 return test_protected_via_DER(KEYTYPEstr, key_##KEYTYPE); \
878 static int test_protected_##KEYTYPE##_via_PEM(void) \
880 return test_protected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
882 static int test_public_##KEYTYPE##_via_DER(void) \
884 return test_public_via_DER(KEYTYPEstr, key_##KEYTYPE); \
886 static int test_public_##KEYTYPE##_via_PEM(void) \
888 return test_public_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
891 #define ADD_TEST_SUITE(KEYTYPE) \
892 ADD_TEST(test_unprotected_##KEYTYPE##_via_DER); \
893 ADD_TEST(test_unprotected_##KEYTYPE##_via_PEM); \
894 ADD_TEST(test_protected_##KEYTYPE##_via_DER); \
895 ADD_TEST(test_protected_##KEYTYPE##_via_PEM); \
896 ADD_TEST(test_public_##KEYTYPE##_via_DER); \
897 ADD_TEST(test_public_##KEYTYPE##_via_PEM)
899 #define IMPLEMENT_TEST_SUITE_PARAMS(KEYTYPE, KEYTYPEstr) \
900 static int test_params_##KEYTYPE##_via_DER(void) \
902 return test_params_via_DER(KEYTYPEstr, key_##KEYTYPE); \
904 static int test_params_##KEYTYPE##_via_PEM(void) \
906 return test_params_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
909 #define ADD_TEST_SUITE_PARAMS(KEYTYPE) \
910 ADD_TEST(test_params_##KEYTYPE##_via_DER); \
911 ADD_TEST(test_params_##KEYTYPE##_via_PEM)
913 #define IMPLEMENT_TEST_SUITE_LEGACY(KEYTYPE, KEYTYPEstr) \
914 static int test_unprotected_##KEYTYPE##_via_legacy_PEM(void) \
917 test_unprotected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
919 static int test_protected_##KEYTYPE##_via_legacy_PEM(void) \
922 test_protected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
925 #define ADD_TEST_SUITE_LEGACY(KEYTYPE) \
926 ADD_TEST(test_unprotected_##KEYTYPE##_via_legacy_PEM); \
927 ADD_TEST(test_protected_##KEYTYPE##_via_legacy_PEM)
929 #define IMPLEMENT_TEST_SUITE_MSBLOB(KEYTYPE, KEYTYPEstr) \
930 static int test_unprotected_##KEYTYPE##_via_MSBLOB(void) \
932 return test_unprotected_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
934 static int test_public_##KEYTYPE##_via_MSBLOB(void) \
936 return test_public_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
939 #define ADD_TEST_SUITE_MSBLOB(KEYTYPE) \
940 ADD_TEST(test_unprotected_##KEYTYPE##_via_MSBLOB); \
941 ADD_TEST(test_public_##KEYTYPE##_via_MSBLOB)
943 #define IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
944 static int test_unprotected_##KEYTYPE##_via_PVK(void) \
946 return test_unprotected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
948 # define ADD_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE) \
949 ADD_TEST(test_unprotected_##KEYTYPE##_via_PVK)
950 #ifndef OPENSSL_NO_RC4
951 # define IMPLEMENT_TEST_SUITE_PROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
952 static int test_protected_##KEYTYPE##_via_PVK(void) \
954 return test_protected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
956 # define ADD_TEST_SUITE_PROTECTED_PVK(KEYTYPE) \
957 ADD_TEST(test_protected_##KEYTYPE##_via_PVK)
960 #ifndef OPENSSL_NO_DH
962 IMPLEMENT_TEST_SUITE(DH, "DH")
963 IMPLEMENT_TEST_SUITE_PARAMS(DH, "DH")
965 IMPLEMENT_TEST_SUITE(DHX, "X9.42 DH")
966 IMPLEMENT_TEST_SUITE_PARAMS(DHX, "X9.42 DH")
968 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
969 * so no legacy tests.
972 #ifndef OPENSSL_NO_DSA
974 IMPLEMENT_TEST_SUITE(DSA, "DSA")
975 IMPLEMENT_TEST_SUITE_PARAMS(DSA, "DSA")
976 IMPLEMENT_TEST_SUITE_LEGACY(DSA, "DSA")
977 IMPLEMENT_TEST_SUITE_MSBLOB(DSA, "DSA")
978 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(DSA, "DSA")
979 # ifndef OPENSSL_NO_RC4
980 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(DSA, "DSA")
983 #ifndef OPENSSL_NO_EC
985 IMPLEMENT_TEST_SUITE(EC, "EC")
986 IMPLEMENT_TEST_SUITE_PARAMS(EC, "EC")
987 IMPLEMENT_TEST_SUITE_LEGACY(EC, "EC")
988 DOMAIN_KEYS(ECExplicitPrimeNamedCurve);
989 IMPLEMENT_TEST_SUITE(ECExplicitPrimeNamedCurve, "EC")
990 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve, "EC")
991 DOMAIN_KEYS(ECExplicitPrime2G);
992 IMPLEMENT_TEST_SUITE(ECExplicitPrime2G, "EC")
993 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrime2G, "EC")
994 # ifndef OPENSSL_NO_EC2M
995 DOMAIN_KEYS(ECExplicitTriNamedCurve);
996 IMPLEMENT_TEST_SUITE(ECExplicitTriNamedCurve, "EC")
997 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve, "EC")
998 DOMAIN_KEYS(ECExplicitTri2G);
999 IMPLEMENT_TEST_SUITE(ECExplicitTri2G, "EC")
1000 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTri2G, "EC")
1003 IMPLEMENT_TEST_SUITE(ED25519, "ED25519")
1005 IMPLEMENT_TEST_SUITE(ED448, "ED448")
1007 IMPLEMENT_TEST_SUITE(X25519, "X25519")
1009 IMPLEMENT_TEST_SUITE(X448, "X448")
1011 * ED25519, ED448, X25519 and X448 have no support for
1012 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1016 IMPLEMENT_TEST_SUITE(RSA, "RSA")
1017 IMPLEMENT_TEST_SUITE_LEGACY(RSA, "RSA")
1019 IMPLEMENT_TEST_SUITE(RSA_PSS, "RSA-PSS")
1021 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1022 * so no legacy tests.
1024 IMPLEMENT_TEST_SUITE_MSBLOB(RSA, "RSA")
1025 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(RSA, "RSA")
1026 #ifndef OPENSSL_NO_RC4
1027 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(RSA, "RSA")
1030 #ifndef OPENSSL_NO_EC
1031 /* Explicit parameters that match a named curve */
1032 static int do_create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld,
1033 const unsigned char *gen,
1036 BIGNUM *a, *b, *prime, *order;
1038 /* Curve prime256v1 */
1039 static const unsigned char prime_data[] = {
1040 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1041 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1042 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1043 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1046 static const unsigned char a_data[] = {
1047 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1048 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1049 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1050 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1053 static const unsigned char b_data[] = {
1054 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
1055 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
1056 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
1057 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b
1059 static const unsigned char seed[] = {
1060 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93,
1061 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, 0x26, 0xb7,
1062 0x81, 0x9f, 0x7e, 0x90
1064 static const unsigned char order_data[] = {
1065 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1066 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1067 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e,
1068 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51
1070 return TEST_ptr(a = BN_CTX_get(bnctx))
1071 && TEST_ptr(b = BN_CTX_get(bnctx))
1072 && TEST_ptr(prime = BN_CTX_get(bnctx))
1073 && TEST_ptr(order = BN_CTX_get(bnctx))
1074 && TEST_ptr(BN_bin2bn(prime_data, sizeof(prime_data), prime))
1075 && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a))
1076 && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b))
1077 && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order))
1078 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
1079 OSSL_PKEY_PARAM_EC_FIELD_TYPE, SN_X9_62_prime_field,
1081 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, prime))
1082 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
1083 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b))
1084 && TEST_true(OSSL_PARAM_BLD_push_BN(bld,
1085 OSSL_PKEY_PARAM_EC_ORDER, order))
1086 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
1087 OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len))
1088 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
1089 OSSL_PKEY_PARAM_EC_SEED, seed, sizeof(seed)))
1090 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1094 static int create_ec_explicit_prime_params_namedcurve(OSSL_PARAM_BLD *bld)
1096 static const unsigned char prime256v1_gen[] = {
1098 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
1099 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
1100 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
1101 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
1102 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b,
1103 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
1104 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
1105 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5
1107 return do_create_ec_explicit_prime_params(bld, prime256v1_gen,
1108 sizeof(prime256v1_gen));
1111 static int create_ec_explicit_prime_params(OSSL_PARAM_BLD *bld)
1114 static const unsigned char prime256v1_gen2[] = {
1116 0xe4, 0x97, 0x08, 0xbe, 0x7d, 0xfa, 0xa2, 0x9a,
1117 0xa3, 0x12, 0x6f, 0xe4, 0xe7, 0xd0, 0x25, 0xe3,
1118 0x4a, 0xc1, 0x03, 0x15, 0x8c, 0xd9, 0x33, 0xc6,
1119 0x97, 0x42, 0xf5, 0xdc, 0x97, 0xb9, 0xd7, 0x31,
1120 0xe9, 0x7d, 0x74, 0x3d, 0x67, 0x6a, 0x3b, 0x21,
1121 0x08, 0x9c, 0x31, 0x73, 0xf8, 0xc1, 0x27, 0xc9,
1122 0xd2, 0xa0, 0xa0, 0x83, 0x66, 0xe0, 0xc9, 0xda,
1123 0xa8, 0xc6, 0x56, 0x2b, 0x94, 0xb1, 0xae, 0x55
1125 return do_create_ec_explicit_prime_params(bld, prime256v1_gen2,
1126 sizeof(prime256v1_gen2));
1129 # ifndef OPENSSL_NO_EC2M
1130 static int do_create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld,
1131 const unsigned char *gen,
1134 BIGNUM *a, *b, *poly, *order, *cofactor;
1135 /* sect233k1 characteristic-two-field tpBasis */
1136 static const unsigned char poly_data[] = {
1137 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1138 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1139 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1141 static const unsigned char a_data[] = {
1142 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1143 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1146 static const unsigned char b_data[] = {
1147 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1149 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1151 static const unsigned char order_data[] = {
1152 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1153 0x00, 0x00, 0x00, 0x06, 0x9D, 0x5B, 0xB9, 0x15, 0xBC, 0xD4, 0x6E, 0xFB,
1154 0x1A, 0xD5, 0xF1, 0x73, 0xAB, 0xDF
1156 static const unsigned char cofactor_data[]= {
1159 return TEST_ptr(a = BN_CTX_get(bnctx))
1160 && TEST_ptr(b = BN_CTX_get(bnctx))
1161 && TEST_ptr(poly = BN_CTX_get(bnctx))
1162 && TEST_ptr(order = BN_CTX_get(bnctx))
1163 && TEST_ptr(cofactor = BN_CTX_get(bnctx))
1164 && TEST_ptr(BN_bin2bn(poly_data, sizeof(poly_data), poly))
1165 && TEST_ptr(BN_bin2bn(a_data, sizeof(a_data), a))
1166 && TEST_ptr(BN_bin2bn(b_data, sizeof(b_data), b))
1167 && TEST_ptr(BN_bin2bn(order_data, sizeof(order_data), order))
1168 && TEST_ptr(BN_bin2bn(cofactor_data, sizeof(cofactor_data), cofactor))
1169 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld,
1170 OSSL_PKEY_PARAM_EC_FIELD_TYPE,
1171 SN_X9_62_characteristic_two_field, 0))
1172 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_P, poly))
1173 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_A, a))
1174 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_B, b))
1175 && TEST_true(OSSL_PARAM_BLD_push_BN(bld,
1176 OSSL_PKEY_PARAM_EC_ORDER, order))
1177 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld,
1178 OSSL_PKEY_PARAM_EC_GENERATOR, gen, gen_len))
1179 && TEST_true(OSSL_PARAM_BLD_push_BN(bld, OSSL_PKEY_PARAM_EC_COFACTOR,
1183 static int create_ec_explicit_trinomial_params_namedcurve(OSSL_PARAM_BLD *bld)
1185 static const unsigned char gen[] = {
1187 0x01, 0x72, 0x32, 0xBA, 0x85, 0x3A, 0x7E, 0x73, 0x1A, 0xF1, 0x29, 0xF2,
1188 0x2F, 0xF4, 0x14, 0x95, 0x63, 0xA4, 0x19, 0xC2, 0x6B, 0xF5, 0x0A, 0x4C,
1189 0x9D, 0x6E, 0xEF, 0xAD, 0x61, 0x26,
1190 0x01, 0xDB, 0x53, 0x7D, 0xEC, 0xE8, 0x19, 0xB7, 0xF7, 0x0F, 0x55, 0x5A,
1191 0x67, 0xC4, 0x27, 0xA8, 0xCD, 0x9B, 0xF1, 0x8A, 0xEB, 0x9B, 0x56, 0xE0,
1192 0xC1, 0x10, 0x56, 0xFA, 0xE6, 0xA3
1194 return do_create_ec_explicit_trinomial_params(bld, gen, sizeof(gen));
1197 static int create_ec_explicit_trinomial_params(OSSL_PARAM_BLD *bld)
1199 static const unsigned char gen2[] = {
1201 0x00, 0xd7, 0xba, 0xd0, 0x26, 0x6c, 0x31, 0x6a, 0x78, 0x76, 0x01, 0xd1,
1202 0x32, 0x4b, 0x8f, 0x30, 0x29, 0x2d, 0x78, 0x30, 0xca, 0x43, 0xaa, 0xf0,
1203 0xa2, 0x5a, 0xd4, 0x0f, 0xb3, 0xf4,
1204 0x00, 0x85, 0x4b, 0x1b, 0x8d, 0x50, 0x10, 0xa5, 0x1c, 0x80, 0xf7, 0x86,
1205 0x40, 0x62, 0x4c, 0x87, 0xd1, 0x26, 0x7a, 0x9c, 0x5c, 0xe9, 0x82, 0x29,
1206 0xd1, 0x67, 0x70, 0x41, 0xea, 0xcb
1208 return do_create_ec_explicit_trinomial_params(bld, gen2, sizeof(gen2));
1210 # endif /* OPENSSL_NO_EC2M */
1211 #endif /* OPENSSL_NO_EC */
1213 typedef enum OPTION_choice {
1224 const OPTIONS *test_get_options(void)
1226 static const OPTIONS options[] = {
1227 OPT_TEST_OPTIONS_DEFAULT_USAGE,
1228 { "context", OPT_CONTEXT, '-',
1229 "Explicitly use a non-default library context" },
1230 { "rsa", OPT_RSA_FILE, '<',
1231 "PEM format RSA key file to encode/decode" },
1232 { "pss", OPT_RSA_PSS_FILE, '<',
1233 "PEM format RSA-PSS key file to encode/decode" },
1234 { "config", OPT_CONFIG_FILE, '<',
1235 "The configuration file to use for the library context" },
1236 { "provider", OPT_PROVIDER_NAME, 's',
1237 "The provider to load (The default value is 'default')" },
1243 int setup_tests(void)
1245 const char *rsa_file = NULL;
1246 const char *rsa_pss_file = NULL;
1247 const char *prov_name = "default";
1248 char *config_file = NULL;
1251 #ifndef OPENSSL_NO_DSA
1252 static size_t qbits = 160; /* PVK only tolerates 160 Q bits */
1253 static size_t pbits = 1024; /* With 160 Q bits, we MUST use 1024 P bits */
1254 OSSL_PARAM DSA_params[] = {
1255 OSSL_PARAM_size_t("pbits", &pbits),
1256 OSSL_PARAM_size_t("qbits", &qbits),
1261 #ifndef OPENSSL_NO_EC
1262 static char groupname[] = "prime256v1";
1263 OSSL_PARAM EC_params[] = {
1264 OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1),
1271 while ((o = opt_next()) != OPT_EOF) {
1276 case OPT_PROVIDER_NAME:
1277 prov_name = opt_arg();
1279 case OPT_CONFIG_FILE:
1280 config_file = opt_arg();
1283 rsa_file = opt_arg();
1285 case OPT_RSA_PSS_FILE:
1286 rsa_pss_file = opt_arg();
1288 case OPT_TEST_CASES:
1295 if (strcmp(prov_name, "fips") == 0)
1298 if (default_libctx) {
1299 if (!test_get_libctx(NULL, NULL, config_file, &deflprov, prov_name))
1302 if (!test_get_libctx(&testctx, &nullprov, config_file, &deflprov, prov_name))
1306 /* Separate provider/ctx for generating the test data */
1307 if (!TEST_ptr(keyctx = OSSL_LIB_CTX_new()))
1309 if (!TEST_ptr(keyprov = OSSL_PROVIDER_load(keyctx, "default")))
1312 #ifndef OPENSSL_NO_EC
1313 if (!TEST_ptr(bnctx = BN_CTX_new_ex(testctx))
1314 || !TEST_ptr(bld_prime_nc = OSSL_PARAM_BLD_new())
1315 || !TEST_ptr(bld_prime = OSSL_PARAM_BLD_new())
1316 || !create_ec_explicit_prime_params_namedcurve(bld_prime_nc)
1317 || !create_ec_explicit_prime_params(bld_prime)
1318 || !TEST_ptr(ec_explicit_prime_params_nc = OSSL_PARAM_BLD_to_param(bld_prime_nc))
1319 || !TEST_ptr(ec_explicit_prime_params_explicit = OSSL_PARAM_BLD_to_param(bld_prime))
1320 # ifndef OPENSSL_NO_EC2M
1321 || !TEST_ptr(bld_tri_nc = OSSL_PARAM_BLD_new())
1322 || !TEST_ptr(bld_tri = OSSL_PARAM_BLD_new())
1323 || !create_ec_explicit_trinomial_params_namedcurve(bld_tri_nc)
1324 || !create_ec_explicit_trinomial_params(bld_tri)
1325 || !TEST_ptr(ec_explicit_tri_params_nc = OSSL_PARAM_BLD_to_param(bld_tri_nc))
1326 || !TEST_ptr(ec_explicit_tri_params_explicit = OSSL_PARAM_BLD_to_param(bld_tri))
1332 TEST_info("Generating keys...");
1334 #ifndef OPENSSL_NO_DH
1335 TEST_info("Generating DH keys...");
1336 MAKE_DOMAIN_KEYS(DH, "DH", NULL);
1337 MAKE_DOMAIN_KEYS(DHX, "X9.42 DH", NULL);
1339 #ifndef OPENSSL_NO_DSA
1340 TEST_info("Generating DSA keys...");
1341 MAKE_DOMAIN_KEYS(DSA, "DSA", DSA_params);
1343 #ifndef OPENSSL_NO_EC
1344 TEST_info("Generating EC keys...");
1345 MAKE_DOMAIN_KEYS(EC, "EC", EC_params);
1346 MAKE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve, "EC", ec_explicit_prime_params_nc);
1347 MAKE_DOMAIN_KEYS(ECExplicitPrime2G, "EC", ec_explicit_prime_params_explicit);
1348 # ifndef OPENSSL_NO_EC2M
1349 MAKE_DOMAIN_KEYS(ECExplicitTriNamedCurve, "EC", ec_explicit_tri_params_nc);
1350 MAKE_DOMAIN_KEYS(ECExplicitTri2G, "EC", ec_explicit_tri_params_explicit);
1352 MAKE_KEYS(ED25519, "ED25519", NULL);
1353 MAKE_KEYS(ED448, "ED448", NULL);
1354 MAKE_KEYS(X25519, "X25519", NULL);
1355 MAKE_KEYS(X448, "X448", NULL);
1357 TEST_info("Loading RSA key...");
1358 ok = ok && TEST_ptr(key_RSA = load_pkey_pem(rsa_file, keyctx));
1359 TEST_info("Loading RSA_PSS key...");
1360 ok = ok && TEST_ptr(key_RSA_PSS = load_pkey_pem(rsa_pss_file, keyctx));
1361 TEST_info("Generating keys done");
1364 #ifndef OPENSSL_NO_DH
1366 ADD_TEST_SUITE_PARAMS(DH);
1367 ADD_TEST_SUITE(DHX);
1368 ADD_TEST_SUITE_PARAMS(DHX);
1370 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
1371 * so no legacy tests.
1374 #ifndef OPENSSL_NO_DSA
1375 ADD_TEST_SUITE(DSA);
1376 ADD_TEST_SUITE_PARAMS(DSA);
1377 ADD_TEST_SUITE_LEGACY(DSA);
1378 ADD_TEST_SUITE_MSBLOB(DSA);
1379 ADD_TEST_SUITE_UNPROTECTED_PVK(DSA);
1380 # ifndef OPENSSL_NO_RC4
1381 ADD_TEST_SUITE_PROTECTED_PVK(DSA);
1384 #ifndef OPENSSL_NO_EC
1386 ADD_TEST_SUITE_PARAMS(EC);
1387 ADD_TEST_SUITE_LEGACY(EC);
1388 ADD_TEST_SUITE(ECExplicitPrimeNamedCurve);
1389 ADD_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve);
1390 ADD_TEST_SUITE(ECExplicitPrime2G);
1391 ADD_TEST_SUITE_LEGACY(ECExplicitPrime2G);
1392 # ifndef OPENSSL_NO_EC2M
1393 ADD_TEST_SUITE(ECExplicitTriNamedCurve);
1394 ADD_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve);
1395 ADD_TEST_SUITE(ECExplicitTri2G);
1396 ADD_TEST_SUITE_LEGACY(ECExplicitTri2G);
1398 ADD_TEST_SUITE(ED25519);
1399 ADD_TEST_SUITE(ED448);
1400 ADD_TEST_SUITE(X25519);
1401 ADD_TEST_SUITE(X448);
1403 * ED25519, ED448, X25519 and X448 have no support for
1404 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1407 ADD_TEST_SUITE(RSA);
1408 ADD_TEST_SUITE_LEGACY(RSA);
1409 ADD_TEST_SUITE(RSA_PSS);
1411 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1412 * so no legacy tests.
1414 ADD_TEST_SUITE_MSBLOB(RSA);
1415 ADD_TEST_SUITE_UNPROTECTED_PVK(RSA);
1416 # ifndef OPENSSL_NO_RC4
1417 ADD_TEST_SUITE_PROTECTED_PVK(RSA);
1424 void cleanup_tests(void)
1426 #ifndef OPENSSL_NO_EC
1427 OSSL_PARAM_free(ec_explicit_prime_params_nc);
1428 OSSL_PARAM_free(ec_explicit_prime_params_explicit);
1429 OSSL_PARAM_BLD_free(bld_prime_nc);
1430 OSSL_PARAM_BLD_free(bld_prime);
1431 # ifndef OPENSSL_NO_EC2M
1432 OSSL_PARAM_free(ec_explicit_tri_params_nc);
1433 OSSL_PARAM_free(ec_explicit_tri_params_explicit);
1434 OSSL_PARAM_BLD_free(bld_tri_nc);
1435 OSSL_PARAM_BLD_free(bld_tri);
1438 #endif /* OPENSSL_NO_EC */
1440 #ifndef OPENSSL_NO_DH
1441 FREE_DOMAIN_KEYS(DH);
1442 FREE_DOMAIN_KEYS(DHX);
1444 #ifndef OPENSSL_NO_DSA
1445 FREE_DOMAIN_KEYS(DSA);
1447 #ifndef OPENSSL_NO_EC
1448 FREE_DOMAIN_KEYS(EC);
1449 FREE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve);
1450 FREE_DOMAIN_KEYS(ECExplicitPrime2G);
1451 # ifndef OPENSSL_NO_EC2M
1452 FREE_DOMAIN_KEYS(ECExplicitTriNamedCurve);
1453 FREE_DOMAIN_KEYS(ECExplicitTri2G);
1463 OSSL_PROVIDER_unload(nullprov);
1464 OSSL_PROVIDER_unload(deflprov);
1465 OSSL_PROVIDER_unload(keyprov);
1466 OSSL_LIB_CTX_free(testctx);
1467 OSSL_LIB_CTX_free(keyctx);