2 * Copyright 2020-2023 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"
30 OSSL_provider_init_fn ossl_legacy_provider_init
;
33 /* Extended test macros to allow passing file & line number */
34 #define TEST_FL_ptr(a) test_ptr(file, line, #a, a)
35 #define TEST_FL_mem_eq(a, m, b, n) test_mem_eq(file, line, #a, #b, a, m, b, n)
36 #define TEST_FL_strn_eq(a, b, n) test_strn_eq(file, line, #a, #b, a, n, b, n)
37 #define TEST_FL_strn2_eq(a, m, b, n) test_strn_eq(file, line, #a, #b, a, m, b, n)
38 #define TEST_FL_int_eq(a, b) test_int_eq(file, line, #a, #b, a, b)
39 #define TEST_FL_int_ge(a, b) test_int_ge(file, line, #a, #b, a, b)
40 #define TEST_FL_int_gt(a, b) test_int_gt(file, line, #a, #b, a, b)
41 #define TEST_FL_long_gt(a, b) test_long_gt(file, line, #a, #b, a, b)
42 #define TEST_FL_true(a) test_true(file, line, #a, (a) != 0)
44 #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC)
45 # define OPENSSL_NO_KEYPARAMS
48 static int default_libctx
= 1;
49 static int is_fips
= 0;
50 static int is_fips_3_0_0
= 0;
52 static OSSL_LIB_CTX
*testctx
= NULL
;
53 static OSSL_LIB_CTX
*keyctx
= NULL
;
54 static char *testpropq
= NULL
;
56 static OSSL_PROVIDER
*nullprov
= NULL
;
57 static OSSL_PROVIDER
*deflprov
= NULL
;
58 static OSSL_PROVIDER
*keyprov
= NULL
;
61 static BN_CTX
*bnctx
= NULL
;
62 static OSSL_PARAM_BLD
*bld_prime_nc
= NULL
;
63 static OSSL_PARAM_BLD
*bld_prime
= NULL
;
64 static OSSL_PARAM
*ec_explicit_prime_params_nc
= NULL
;
65 static OSSL_PARAM
*ec_explicit_prime_params_explicit
= NULL
;
67 # ifndef OPENSSL_NO_EC2M
68 static OSSL_PARAM_BLD
*bld_tri_nc
= NULL
;
69 static OSSL_PARAM_BLD
*bld_tri
= NULL
;
70 static OSSL_PARAM
*ec_explicit_tri_params_nc
= NULL
;
71 static OSSL_PARAM
*ec_explicit_tri_params_explicit
= NULL
;
75 #ifndef OPENSSL_NO_KEYPARAMS
76 static EVP_PKEY
*make_template(const char *type
, OSSL_PARAM
*genparams
)
78 EVP_PKEY
*pkey
= NULL
;
79 EVP_PKEY_CTX
*ctx
= NULL
;
81 # ifndef OPENSSL_NO_DH
83 * Use 512-bit DH(X) keys with predetermined parameters for efficiency,
84 * for testing only. Use a minimum key size of 2048 for security purposes.
86 if (strcmp(type
, "DH") == 0)
87 return get_dh512(keyctx
);
89 if (strcmp(type
, "X9.42 DH") == 0)
90 return get_dhx512(keyctx
);
94 * No real need to check the errors other than for the cascade
95 * effect. |pkey| will simply remain NULL if something goes wrong.
97 (void)((ctx
= EVP_PKEY_CTX_new_from_name(keyctx
, type
, testpropq
)) != NULL
98 && EVP_PKEY_paramgen_init(ctx
) > 0
100 || EVP_PKEY_CTX_set_params(ctx
, genparams
) > 0)
101 && EVP_PKEY_generate(ctx
, &pkey
) > 0);
102 EVP_PKEY_CTX_free(ctx
);
108 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
109 static EVP_PKEY
*make_key(const char *type
, EVP_PKEY
*template,
110 OSSL_PARAM
*genparams
)
112 EVP_PKEY
*pkey
= NULL
;
115 ? EVP_PKEY_CTX_new_from_pkey(keyctx
, template, testpropq
)
116 : EVP_PKEY_CTX_new_from_name(keyctx
, type
, testpropq
);
119 * No real need to check the errors other than for the cascade
120 * effect. |pkey| will simply remain NULL if something goes wrong.
123 && EVP_PKEY_keygen_init(ctx
) > 0
124 && (genparams
== NULL
125 || EVP_PKEY_CTX_set_params(ctx
, genparams
) > 0)
126 && EVP_PKEY_keygen(ctx
, &pkey
) > 0);
127 EVP_PKEY_CTX_free(ctx
);
132 /* Main test driver */
134 typedef int (encoder
)(const char *file
, const int line
,
135 void **encoded
, long *encoded_len
,
136 void *object
, int selection
,
137 const char *output_type
, const char *output_structure
,
138 const char *pass
, const char *pcipher
);
139 typedef int (decoder
)(const char *file
, const int line
,
140 void **object
, void *encoded
, long encoded_len
,
141 const char *input_type
, const char *structure_type
,
142 const char *keytype
, int selection
, const char *pass
);
143 typedef int (tester
)(const char *file
, const int line
,
144 const void *data1
, size_t data1_len
,
145 const void *data2
, size_t data2_len
);
146 typedef int (checker
)(const char *file
, const int line
,
147 const char *type
, const void *data
, size_t data_len
);
148 typedef void (dumper
)(const char *label
, const void *data
, size_t data_len
);
150 #define FLAG_DECODE_WITH_TYPE 0x0001
151 #define FLAG_FAIL_IF_FIPS 0x0002
153 static int test_encode_decode(const char *file
, const int line
,
154 const char *type
, EVP_PKEY
*pkey
,
155 int selection
, const char *output_type
,
156 const char *output_structure
,
157 const char *pass
, const char *pcipher
,
158 encoder
*encode_cb
, decoder
*decode_cb
,
159 tester
*test_cb
, checker
*check_cb
,
160 dumper
*dump_cb
, int flags
)
162 void *encoded
= NULL
;
163 long encoded_len
= 0;
164 EVP_PKEY
*pkey2
= NULL
;
165 EVP_PKEY
*pkey3
= NULL
;
166 void *encoded2
= NULL
;
167 long encoded2_len
= 0;
171 * Encode |pkey|, decode the result into |pkey2|, and finish off by
172 * encoding |pkey2| as well. That last encoding is for checking and
175 if (!TEST_true(encode_cb(file
, line
, &encoded
, &encoded_len
, pkey
, selection
,
176 output_type
, output_structure
, pass
, pcipher
)))
179 if ((flags
& FLAG_FAIL_IF_FIPS
) != 0 && is_fips
&& !is_fips_3_0_0
) {
180 if (TEST_false(decode_cb(file
, line
, (void **)&pkey2
, encoded
,
181 encoded_len
, output_type
, output_structure
,
182 (flags
& FLAG_DECODE_WITH_TYPE
? type
: NULL
),
188 if (!TEST_true(check_cb(file
, line
, type
, encoded
, encoded_len
))
189 || !TEST_true(decode_cb(file
, line
, (void **)&pkey2
, encoded
, encoded_len
,
190 output_type
, output_structure
,
191 (flags
& FLAG_DECODE_WITH_TYPE
? type
: NULL
),
193 || ((output_structure
== NULL
194 || strcmp(output_structure
, "type-specific") != 0)
195 && !TEST_true(decode_cb(file
, line
, (void **)&pkey3
, encoded
, encoded_len
,
196 output_type
, output_structure
,
197 (flags
& FLAG_DECODE_WITH_TYPE
? type
: NULL
),
199 || !TEST_true(encode_cb(file
, line
, &encoded2
, &encoded2_len
, pkey2
, selection
,
200 output_type
, output_structure
, pass
, pcipher
)))
203 if (selection
== OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) {
204 if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey
, pkey2
), 1)
206 && !TEST_int_eq(EVP_PKEY_parameters_eq(pkey
, pkey3
), 1)))
209 if (!TEST_int_eq(EVP_PKEY_eq(pkey
, pkey2
), 1)
211 && !TEST_int_eq(EVP_PKEY_eq(pkey
, pkey3
), 1)))
216 * Double check the encoding, but only for unprotected keys,
217 * as protected keys have a random component, which makes the output
220 if ((pass
== NULL
&& pcipher
== NULL
)
221 && !test_cb(file
, line
, encoded
, encoded_len
, encoded2
, encoded2_len
))
227 if (encoded
!= NULL
&& encoded_len
!= 0)
228 dump_cb("|pkey| encoded", encoded
, encoded_len
);
229 if (encoded2
!= NULL
&& encoded2_len
!= 0)
230 dump_cb("|pkey2| encoded", encoded2
, encoded2_len
);
233 OPENSSL_free(encoded
);
234 OPENSSL_free(encoded2
);
235 EVP_PKEY_free(pkey2
);
236 EVP_PKEY_free(pkey3
);
240 /* Encoding and decoding methods */
242 static int encode_EVP_PKEY_prov(const char *file
, const int line
,
243 void **encoded
, long *encoded_len
,
244 void *object
, int selection
,
245 const char *output_type
,
246 const char *output_structure
,
247 const char *pass
, const char *pcipher
)
249 EVP_PKEY
*pkey
= object
;
250 OSSL_ENCODER_CTX
*ectx
= NULL
;
252 BUF_MEM
*mem_buf
= NULL
;
253 const unsigned char *upass
= (const unsigned char *)pass
;
256 if (!TEST_FL_ptr(ectx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
,
260 || !TEST_FL_int_gt(OSSL_ENCODER_CTX_get_num_encoders(ectx
), 0)
262 && !TEST_FL_true(OSSL_ENCODER_CTX_set_passphrase(ectx
, upass
,
265 && !TEST_FL_true(OSSL_ENCODER_CTX_set_cipher(ectx
, pcipher
, NULL
)))
266 || !TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
267 || !TEST_FL_true(OSSL_ENCODER_to_bio(ectx
, mem_ser
))
268 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
269 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
270 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
273 /* Detach the encoded output */
274 mem_buf
->data
= NULL
;
279 OSSL_ENCODER_CTX_free(ectx
);
283 static int decode_EVP_PKEY_prov(const char *file
, const int line
,
284 void **object
, void *encoded
, long encoded_len
,
285 const char *input_type
,
286 const char *structure_type
,
287 const char *keytype
, int selection
,
290 EVP_PKEY
*pkey
= NULL
, *testpkey
= NULL
;
291 OSSL_DECODER_CTX
*dctx
= NULL
;
292 BIO
*encoded_bio
= NULL
;
293 const unsigned char *upass
= (const unsigned char *)pass
;
298 if (strcmp(input_type
, "DER") == 0)
303 if (!TEST_FL_ptr(encoded_bio
= BIO_new_mem_buf(encoded
, encoded_len
)))
307 * We attempt the decode 3 times. The first time we provide the expected
308 * starting input type. The second time we provide NULL for the starting
309 * type. The third time we provide a bad starting input type.
310 * The bad starting input type should fail. The other two should succeed
311 * and produce the same result.
313 for (i
= 0; i
< 3; i
++) {
314 const char *testtype
= (i
== 0) ? input_type
315 : ((i
== 1) ? NULL
: badtype
);
317 if (!TEST_FL_ptr(dctx
= OSSL_DECODER_CTX_new_for_pkey(&testpkey
,
324 && !OSSL_DECODER_CTX_set_passphrase(dctx
, upass
, strlen(pass
)))
325 || !TEST_FL_int_gt(BIO_reset(encoded_bio
), 0)
326 /* We expect to fail when using a bad input type */
327 || !TEST_FL_int_eq(OSSL_DECODER_from_bio(dctx
, encoded_bio
),
330 OSSL_DECODER_CTX_free(dctx
);
337 if (selection
== OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) {
338 if (!TEST_FL_int_eq(EVP_PKEY_parameters_eq(pkey
, testpkey
), 1))
341 if (!TEST_FL_int_eq(EVP_PKEY_eq(pkey
, testpkey
), 1))
352 EVP_PKEY_free(testpkey
);
353 BIO_free(encoded_bio
);
354 OSSL_DECODER_CTX_free(dctx
);
358 static int encode_EVP_PKEY_legacy_PEM(const char *file
, const int line
,
359 void **encoded
, long *encoded_len
,
360 void *object
, ossl_unused
int selection
,
361 ossl_unused
const char *output_type
,
362 ossl_unused
const char *output_structure
,
363 const char *pass
, const char *pcipher
)
365 EVP_PKEY
*pkey
= object
;
366 EVP_CIPHER
*cipher
= NULL
;
368 BUF_MEM
*mem_buf
= NULL
;
369 const unsigned char *upass
= (const unsigned char *)pass
;
373 if (pcipher
!= NULL
&& pass
!= NULL
) {
374 passlen
= strlen(pass
);
375 if (!TEST_FL_ptr(cipher
= EVP_CIPHER_fetch(testctx
, pcipher
, testpropq
)))
378 if (!TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
379 || !TEST_FL_true(PEM_write_bio_PrivateKey_traditional(mem_ser
, pkey
,
383 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
384 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
385 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
388 /* Detach the encoded output */
389 mem_buf
->data
= NULL
;
394 EVP_CIPHER_free(cipher
);
398 static int encode_EVP_PKEY_MSBLOB(const char *file
, const int line
,
399 void **encoded
, long *encoded_len
,
400 void *object
, int selection
,
401 ossl_unused
const char *output_type
,
402 ossl_unused
const char *output_structure
,
403 ossl_unused
const char *pass
,
404 ossl_unused
const char *pcipher
)
406 EVP_PKEY
*pkey
= object
;
408 BUF_MEM
*mem_buf
= NULL
;
411 if (!TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem())))
414 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
415 if (!TEST_FL_int_ge(i2b_PrivateKey_bio(mem_ser
, pkey
), 0))
418 if (!TEST_FL_int_ge(i2b_PublicKey_bio(mem_ser
, pkey
), 0))
422 if (!TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
423 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
424 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
427 /* Detach the encoded output */
428 mem_buf
->data
= NULL
;
436 static pem_password_cb pass_pw
;
437 static int pass_pw(char *buf
, int size
, int rwflag
, void *userdata
)
439 OPENSSL_strlcpy(buf
, userdata
, size
);
440 return strlen(userdata
);
443 static int encode_EVP_PKEY_PVK(const char *file
, const int line
,
444 void **encoded
, long *encoded_len
,
445 void *object
, int selection
,
446 ossl_unused
const char *output_type
,
447 ossl_unused
const char *output_structure
,
449 ossl_unused
const char *pcipher
)
451 EVP_PKEY
*pkey
= object
;
453 BUF_MEM
*mem_buf
= NULL
;
454 int enc
= (pass
!= NULL
);
457 if (!TEST_FL_true(ossl_assert((selection
458 & OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0))
459 || !TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
460 || !TEST_FL_int_ge(i2b_PVK_bio_ex(mem_ser
, pkey
, enc
,
461 pass_pw
, (void *)pass
, testctx
, testpropq
), 0)
462 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
463 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
464 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
467 /* Detach the encoded output */
468 mem_buf
->data
= NULL
;
476 static int test_text(const char *file
, const int line
,
477 const void *data1
, size_t data1_len
,
478 const void *data2
, size_t data2_len
)
480 return TEST_FL_strn2_eq(data1
, data1_len
, data2
, data2_len
);
483 static int test_mem(const char *file
, const int line
,
484 const void *data1
, size_t data1_len
,
485 const void *data2
, size_t data2_len
)
487 return TEST_FL_mem_eq(data1
, data1_len
, data2
, data2_len
);
490 /* Test cases and their dumpers / checkers */
492 static void collect_name(const char *name
, void *arg
)
494 char **namelist
= arg
;
498 space
= strlen(name
);
499 if (*namelist
!= NULL
)
500 space
+= strlen(*namelist
) + 2 /* for comma and space */;
501 space
++; /* for terminating null byte */
503 new_namelist
= OPENSSL_realloc(*namelist
, space
);
504 if (new_namelist
== NULL
)
506 if (*namelist
!= NULL
) {
507 strcat(new_namelist
, ", ");
508 strcat(new_namelist
, name
);
510 strcpy(new_namelist
, name
);
512 *namelist
= new_namelist
;
515 static void dump_der(const char *label
, const void *data
, size_t data_len
)
517 test_output_memory(label
, data
, data_len
);
520 static void dump_pem(const char *label
, const void *data
, size_t data_len
)
522 test_output_string(label
, data
, data_len
- 1);
525 static int check_unprotected_PKCS8_DER(const char *file
, const int line
,
527 const void *data
, size_t data_len
)
529 const unsigned char *datap
= data
;
530 PKCS8_PRIV_KEY_INFO
*p8inf
=
531 d2i_PKCS8_PRIV_KEY_INFO(NULL
, &datap
, data_len
);
534 if (TEST_FL_ptr(p8inf
)) {
535 EVP_PKEY
*pkey
= EVP_PKCS82PKEY_ex(p8inf
, testctx
, testpropq
);
536 char *namelist
= NULL
;
538 if (TEST_FL_ptr(pkey
)) {
539 if (!(ok
= TEST_FL_true(EVP_PKEY_is_a(pkey
, type
)))) {
540 EVP_PKEY_type_names_do_all(pkey
, collect_name
, &namelist
);
541 if (namelist
!= NULL
)
542 TEST_note("%s isn't any of %s", type
, namelist
);
543 OPENSSL_free(namelist
);
545 ok
= ok
&& TEST_FL_true(evp_pkey_is_provided(pkey
));
549 PKCS8_PRIV_KEY_INFO_free(p8inf
);
553 static int test_unprotected_via_DER(const char *type
, EVP_PKEY
*key
, int fips
)
555 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
556 OSSL_KEYMGMT_SELECT_KEYPAIR
557 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
558 "DER", "PrivateKeyInfo", NULL
, NULL
,
559 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
560 test_mem
, check_unprotected_PKCS8_DER
,
561 dump_der
, fips
? 0 : FLAG_FAIL_IF_FIPS
);
564 static int check_unprotected_PKCS8_PEM(const char *file
, const int line
,
566 const void *data
, size_t data_len
)
568 static const char expected_pem_header
[] =
569 "-----BEGIN " PEM_STRING_PKCS8INF
"-----";
571 return TEST_FL_strn_eq(data
, expected_pem_header
,
572 sizeof(expected_pem_header
) - 1);
575 static int test_unprotected_via_PEM(const char *type
, EVP_PKEY
*key
, int fips
)
577 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
578 OSSL_KEYMGMT_SELECT_KEYPAIR
579 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
580 "PEM", "PrivateKeyInfo", NULL
, NULL
,
581 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
582 test_text
, check_unprotected_PKCS8_PEM
,
583 dump_pem
, fips
? 0 : FLAG_FAIL_IF_FIPS
);
586 #ifndef OPENSSL_NO_KEYPARAMS
587 static int check_params_DER(const char *file
, const int line
,
588 const char *type
, const void *data
, size_t data_len
)
590 const unsigned char *datap
= data
;
592 int itype
= NID_undef
;
593 EVP_PKEY
*pkey
= NULL
;
595 if (strcmp(type
, "DH") == 0)
597 else if (strcmp(type
, "X9.42 DH") == 0)
598 itype
= EVP_PKEY_DHX
;
599 else if (strcmp(type
, "DSA") == 0)
600 itype
= EVP_PKEY_DSA
;
601 else if (strcmp(type
, "EC") == 0)
604 if (itype
!= NID_undef
) {
605 pkey
= d2i_KeyParams(itype
, NULL
, &datap
, data_len
);
613 static int check_params_PEM(const char *file
, const int line
,
615 const void *data
, size_t data_len
)
617 static char expected_pem_header
[80];
620 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
621 sizeof(expected_pem_header
),
622 "-----BEGIN %s PARAMETERS-----", type
), 0)
623 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
));
626 static int test_params_via_DER(const char *type
, EVP_PKEY
*key
)
628 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
629 "DER", "type-specific", NULL
, NULL
,
630 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
631 test_mem
, check_params_DER
,
632 dump_der
, FLAG_DECODE_WITH_TYPE
);
635 static int test_params_via_PEM(const char *type
, EVP_PKEY
*key
)
637 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
638 "PEM", "type-specific", NULL
, NULL
,
639 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
640 test_text
, check_params_PEM
,
643 #endif /* !OPENSSL_NO_KEYPARAMS */
645 static int check_unprotected_legacy_PEM(const char *file
, const int line
,
647 const void *data
, size_t data_len
)
649 static char expected_pem_header
[80];
652 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
653 sizeof(expected_pem_header
),
654 "-----BEGIN %s PRIVATE KEY-----", type
), 0)
655 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
));
658 static int test_unprotected_via_legacy_PEM(const char *type
, EVP_PKEY
*key
)
660 if (!default_libctx
|| is_fips
)
661 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
663 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
664 OSSL_KEYMGMT_SELECT_KEYPAIR
665 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
666 "PEM", "type-specific", NULL
, NULL
,
667 encode_EVP_PKEY_legacy_PEM
, decode_EVP_PKEY_prov
,
668 test_text
, check_unprotected_legacy_PEM
,
672 static int check_MSBLOB(const char *file
, const int line
,
673 const char *type
, const void *data
, size_t data_len
)
675 const unsigned char *datap
= data
;
676 EVP_PKEY
*pkey
= b2i_PrivateKey(&datap
, data_len
);
677 int ok
= TEST_FL_ptr(pkey
);
683 static int test_unprotected_via_MSBLOB(const char *type
, EVP_PKEY
*key
)
685 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
686 OSSL_KEYMGMT_SELECT_KEYPAIR
687 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
688 "MSBLOB", NULL
, NULL
, NULL
,
689 encode_EVP_PKEY_MSBLOB
, decode_EVP_PKEY_prov
,
690 test_mem
, check_MSBLOB
,
694 static int check_PVK(const char *file
, const int line
,
695 const char *type
, const void *data
, size_t data_len
)
697 const unsigned char *in
= data
;
698 unsigned int saltlen
= 0, keylen
= 0;
699 int ok
= ossl_do_PVK_header(&in
, data_len
, 0, &saltlen
, &keylen
);
704 static int test_unprotected_via_PVK(const char *type
, EVP_PKEY
*key
)
706 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
707 OSSL_KEYMGMT_SELECT_KEYPAIR
708 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
709 "PVK", NULL
, NULL
, NULL
,
710 encode_EVP_PKEY_PVK
, decode_EVP_PKEY_prov
,
715 static const char *pass_cipher
= "AES-256-CBC";
716 static const char *pass
= "the holy handgrenade of antioch";
718 static int check_protected_PKCS8_DER(const char *file
, const int line
,
720 const void *data
, size_t data_len
)
722 const unsigned char *datap
= data
;
723 X509_SIG
*p8
= d2i_X509_SIG(NULL
, &datap
, data_len
);
724 int ok
= TEST_FL_ptr(p8
);
730 static int test_protected_via_DER(const char *type
, EVP_PKEY
*key
, int fips
)
732 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
733 OSSL_KEYMGMT_SELECT_KEYPAIR
734 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
735 "DER", "EncryptedPrivateKeyInfo",
737 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
738 test_mem
, check_protected_PKCS8_DER
,
739 dump_der
, fips
? 0 : FLAG_FAIL_IF_FIPS
);
742 static int check_protected_PKCS8_PEM(const char *file
, const int line
,
744 const void *data
, size_t data_len
)
746 static const char expected_pem_header
[] =
747 "-----BEGIN " PEM_STRING_PKCS8
"-----";
749 return TEST_FL_strn_eq(data
, expected_pem_header
,
750 sizeof(expected_pem_header
) - 1);
753 static int test_protected_via_PEM(const char *type
, EVP_PKEY
*key
, int fips
)
755 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
756 OSSL_KEYMGMT_SELECT_KEYPAIR
757 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
758 "PEM", "EncryptedPrivateKeyInfo",
760 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
761 test_text
, check_protected_PKCS8_PEM
,
762 dump_pem
, fips
? 0 : FLAG_FAIL_IF_FIPS
);
765 static int check_protected_legacy_PEM(const char *file
, const int line
,
767 const void *data
, size_t data_len
)
769 static char expected_pem_header
[80];
772 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
773 sizeof(expected_pem_header
),
774 "-----BEGIN %s PRIVATE KEY-----", type
), 0)
775 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
))
776 && TEST_FL_ptr(strstr(data
, "\nDEK-Info: "));
779 static int test_protected_via_legacy_PEM(const char *type
, EVP_PKEY
*key
)
781 if (!default_libctx
|| is_fips
)
782 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
784 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
785 OSSL_KEYMGMT_SELECT_KEYPAIR
786 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
787 "PEM", "type-specific", pass
, pass_cipher
,
788 encode_EVP_PKEY_legacy_PEM
, decode_EVP_PKEY_prov
,
789 test_text
, check_protected_legacy_PEM
,
793 #ifndef OPENSSL_NO_RC4
794 static int test_protected_via_PVK(const char *type
, EVP_PKEY
*key
)
797 OSSL_PROVIDER
*lgcyprov
= OSSL_PROVIDER_load(testctx
, "legacy");
798 if (lgcyprov
== NULL
)
799 return TEST_skip("Legacy provider not available");
801 ret
= test_encode_decode(__FILE__
, __LINE__
, type
, key
,
802 OSSL_KEYMGMT_SELECT_KEYPAIR
803 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
804 "PVK", NULL
, pass
, NULL
,
805 encode_EVP_PKEY_PVK
, decode_EVP_PKEY_prov
,
806 test_mem
, check_PVK
, dump_der
, 0);
807 OSSL_PROVIDER_unload(lgcyprov
);
812 static int check_public_DER(const char *file
, const int line
,
813 const char *type
, const void *data
, size_t data_len
)
815 const unsigned char *datap
= data
;
816 EVP_PKEY
*pkey
= d2i_PUBKEY_ex(NULL
, &datap
, data_len
, testctx
, testpropq
);
817 int ok
= (TEST_FL_ptr(pkey
) && TEST_FL_true(EVP_PKEY_is_a(pkey
, type
)));
823 static int test_public_via_DER(const char *type
, EVP_PKEY
*key
, int fips
)
825 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
826 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
827 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
828 "DER", "SubjectPublicKeyInfo", NULL
, NULL
,
829 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
830 test_mem
, check_public_DER
, dump_der
,
831 fips
? 0 : FLAG_FAIL_IF_FIPS
);
834 static int check_public_PEM(const char *file
, const int line
,
835 const char *type
, const void *data
, size_t data_len
)
837 static const char expected_pem_header
[] =
838 "-----BEGIN " PEM_STRING_PUBLIC
"-----";
841 TEST_FL_strn_eq(data
, expected_pem_header
,
842 sizeof(expected_pem_header
) - 1);
845 static int test_public_via_PEM(const char *type
, EVP_PKEY
*key
, int fips
)
847 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
848 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
849 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
850 "PEM", "SubjectPublicKeyInfo", NULL
, NULL
,
851 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
852 test_text
, check_public_PEM
, dump_pem
,
853 fips
? 0 : FLAG_FAIL_IF_FIPS
);
856 static int check_public_MSBLOB(const char *file
, const int line
,
858 const void *data
, size_t data_len
)
860 const unsigned char *datap
= data
;
861 EVP_PKEY
*pkey
= b2i_PublicKey(&datap
, data_len
);
862 int ok
= TEST_FL_ptr(pkey
);
868 static int test_public_via_MSBLOB(const char *type
, EVP_PKEY
*key
)
870 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
871 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
872 "MSBLOB", NULL
, NULL
, NULL
,
873 encode_EVP_PKEY_MSBLOB
, decode_EVP_PKEY_prov
,
874 test_mem
, check_public_MSBLOB
, dump_der
, 0);
877 #define KEYS(KEYTYPE) \
878 static EVP_PKEY *key_##KEYTYPE = NULL
879 #define MAKE_KEYS(KEYTYPE, KEYTYPEstr, params) \
881 && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, NULL, params))
882 #define FREE_KEYS(KEYTYPE) \
883 EVP_PKEY_free(key_##KEYTYPE); \
885 #define DOMAIN_KEYS(KEYTYPE) \
886 static EVP_PKEY *template_##KEYTYPE = NULL; \
887 static EVP_PKEY *key_##KEYTYPE = NULL
888 #define MAKE_DOMAIN_KEYS(KEYTYPE, KEYTYPEstr, params) \
890 && TEST_ptr(template_##KEYTYPE = \
891 make_template(KEYTYPEstr, params)) \
892 && TEST_ptr(key_##KEYTYPE = \
893 make_key(KEYTYPEstr, template_##KEYTYPE, NULL))
894 #define FREE_DOMAIN_KEYS(KEYTYPE) \
895 EVP_PKEY_free(template_##KEYTYPE); \
896 EVP_PKEY_free(key_##KEYTYPE)
898 #define IMPLEMENT_TEST_SUITE(KEYTYPE, KEYTYPEstr, fips) \
899 static int test_unprotected_##KEYTYPE##_via_DER(void) \
901 return test_unprotected_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \
903 static int test_unprotected_##KEYTYPE##_via_PEM(void) \
905 return test_unprotected_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \
907 static int test_protected_##KEYTYPE##_via_DER(void) \
909 return test_protected_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \
911 static int test_protected_##KEYTYPE##_via_PEM(void) \
913 return test_protected_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \
915 static int test_public_##KEYTYPE##_via_DER(void) \
917 return test_public_via_DER(KEYTYPEstr, key_##KEYTYPE, fips); \
919 static int test_public_##KEYTYPE##_via_PEM(void) \
921 return test_public_via_PEM(KEYTYPEstr, key_##KEYTYPE, fips); \
924 #define ADD_TEST_SUITE(KEYTYPE) \
925 ADD_TEST(test_unprotected_##KEYTYPE##_via_DER); \
926 ADD_TEST(test_unprotected_##KEYTYPE##_via_PEM); \
927 ADD_TEST(test_protected_##KEYTYPE##_via_DER); \
928 ADD_TEST(test_protected_##KEYTYPE##_via_PEM); \
929 ADD_TEST(test_public_##KEYTYPE##_via_DER); \
930 ADD_TEST(test_public_##KEYTYPE##_via_PEM)
932 #define IMPLEMENT_TEST_SUITE_PARAMS(KEYTYPE, KEYTYPEstr) \
933 static int test_params_##KEYTYPE##_via_DER(void) \
935 return test_params_via_DER(KEYTYPEstr, key_##KEYTYPE); \
937 static int test_params_##KEYTYPE##_via_PEM(void) \
939 return test_params_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
942 #define ADD_TEST_SUITE_PARAMS(KEYTYPE) \
943 ADD_TEST(test_params_##KEYTYPE##_via_DER); \
944 ADD_TEST(test_params_##KEYTYPE##_via_PEM)
946 #define IMPLEMENT_TEST_SUITE_LEGACY(KEYTYPE, KEYTYPEstr) \
947 static int test_unprotected_##KEYTYPE##_via_legacy_PEM(void) \
950 test_unprotected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
952 static int test_protected_##KEYTYPE##_via_legacy_PEM(void) \
955 test_protected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
958 #define ADD_TEST_SUITE_LEGACY(KEYTYPE) \
959 ADD_TEST(test_unprotected_##KEYTYPE##_via_legacy_PEM); \
960 ADD_TEST(test_protected_##KEYTYPE##_via_legacy_PEM)
962 #define IMPLEMENT_TEST_SUITE_MSBLOB(KEYTYPE, KEYTYPEstr) \
963 static int test_unprotected_##KEYTYPE##_via_MSBLOB(void) \
965 return test_unprotected_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
967 static int test_public_##KEYTYPE##_via_MSBLOB(void) \
969 return test_public_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
972 #define ADD_TEST_SUITE_MSBLOB(KEYTYPE) \
973 ADD_TEST(test_unprotected_##KEYTYPE##_via_MSBLOB); \
974 ADD_TEST(test_public_##KEYTYPE##_via_MSBLOB)
976 #define IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
977 static int test_unprotected_##KEYTYPE##_via_PVK(void) \
979 return test_unprotected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
981 # define ADD_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE) \
982 ADD_TEST(test_unprotected_##KEYTYPE##_via_PVK)
983 #ifndef OPENSSL_NO_RC4
984 # define IMPLEMENT_TEST_SUITE_PROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
985 static int test_protected_##KEYTYPE##_via_PVK(void) \
987 return test_protected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
989 # define ADD_TEST_SUITE_PROTECTED_PVK(KEYTYPE) \
990 ADD_TEST(test_protected_##KEYTYPE##_via_PVK)
993 #ifndef OPENSSL_NO_DH
995 IMPLEMENT_TEST_SUITE(DH
, "DH", 1)
996 IMPLEMENT_TEST_SUITE_PARAMS(DH
, "DH")
998 IMPLEMENT_TEST_SUITE(DHX
, "X9.42 DH", 1)
999 IMPLEMENT_TEST_SUITE_PARAMS(DHX
, "X9.42 DH")
1001 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
1002 * so no legacy tests.
1005 #ifndef OPENSSL_NO_DSA
1007 IMPLEMENT_TEST_SUITE(DSA
, "DSA", 1)
1008 IMPLEMENT_TEST_SUITE_PARAMS(DSA
, "DSA")
1009 IMPLEMENT_TEST_SUITE_LEGACY(DSA
, "DSA")
1010 IMPLEMENT_TEST_SUITE_MSBLOB(DSA
, "DSA")
1011 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(DSA
, "DSA")
1012 # ifndef OPENSSL_NO_RC4
1013 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(DSA
, "DSA")
1016 #ifndef OPENSSL_NO_EC
1018 IMPLEMENT_TEST_SUITE(EC
, "EC", 1)
1019 IMPLEMENT_TEST_SUITE_PARAMS(EC
, "EC")
1020 IMPLEMENT_TEST_SUITE_LEGACY(EC
, "EC")
1021 DOMAIN_KEYS(ECExplicitPrimeNamedCurve
);
1022 IMPLEMENT_TEST_SUITE(ECExplicitPrimeNamedCurve
, "EC", 1)
1023 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve
, "EC")
1024 DOMAIN_KEYS(ECExplicitPrime2G
);
1025 IMPLEMENT_TEST_SUITE(ECExplicitPrime2G
, "EC", 0)
1026 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrime2G
, "EC")
1027 # ifndef OPENSSL_NO_EC2M
1028 DOMAIN_KEYS(ECExplicitTriNamedCurve
);
1029 IMPLEMENT_TEST_SUITE(ECExplicitTriNamedCurve
, "EC", 1)
1030 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve
, "EC")
1031 DOMAIN_KEYS(ECExplicitTri2G
);
1032 IMPLEMENT_TEST_SUITE(ECExplicitTri2G
, "EC", 0)
1033 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTri2G
, "EC")
1036 IMPLEMENT_TEST_SUITE(ED25519
, "ED25519", 1)
1038 IMPLEMENT_TEST_SUITE(ED448
, "ED448", 1)
1040 IMPLEMENT_TEST_SUITE(X25519
, "X25519", 1)
1042 IMPLEMENT_TEST_SUITE(X448
, "X448", 1)
1044 * ED25519, ED448, X25519 and X448 have no support for
1045 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1049 IMPLEMENT_TEST_SUITE(RSA
, "RSA", 1)
1050 IMPLEMENT_TEST_SUITE_LEGACY(RSA
, "RSA")
1052 IMPLEMENT_TEST_SUITE(RSA_PSS
, "RSA-PSS", 1)
1054 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1055 * so no legacy tests.
1057 IMPLEMENT_TEST_SUITE_MSBLOB(RSA
, "RSA")
1058 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(RSA
, "RSA")
1059 #ifndef OPENSSL_NO_RC4
1060 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(RSA
, "RSA")
1063 #ifndef OPENSSL_NO_EC
1064 /* Explicit parameters that match a named curve */
1065 static int do_create_ec_explicit_prime_params(OSSL_PARAM_BLD
*bld
,
1066 const unsigned char *gen
,
1069 BIGNUM
*a
, *b
, *prime
, *order
;
1071 /* Curve prime256v1 */
1072 static const unsigned char prime_data
[] = {
1073 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1074 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1075 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1076 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1079 static const unsigned char a_data
[] = {
1080 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1081 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1082 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1083 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1086 static const unsigned char b_data
[] = {
1087 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
1088 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
1089 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
1090 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b
1092 static const unsigned char seed
[] = {
1093 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93,
1094 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, 0x26, 0xb7,
1095 0x81, 0x9f, 0x7e, 0x90
1097 static const unsigned char order_data
[] = {
1098 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1099 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1100 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e,
1101 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51
1103 return TEST_ptr(a
= BN_CTX_get(bnctx
))
1104 && TEST_ptr(b
= BN_CTX_get(bnctx
))
1105 && TEST_ptr(prime
= BN_CTX_get(bnctx
))
1106 && TEST_ptr(order
= BN_CTX_get(bnctx
))
1107 && TEST_ptr(BN_bin2bn(prime_data
, sizeof(prime_data
), prime
))
1108 && TEST_ptr(BN_bin2bn(a_data
, sizeof(a_data
), a
))
1109 && TEST_ptr(BN_bin2bn(b_data
, sizeof(b_data
), b
))
1110 && TEST_ptr(BN_bin2bn(order_data
, sizeof(order_data
), order
))
1111 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld
,
1112 OSSL_PKEY_PARAM_EC_FIELD_TYPE
, SN_X9_62_prime_field
,
1114 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, prime
))
1115 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, a
))
1116 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, b
))
1117 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
,
1118 OSSL_PKEY_PARAM_EC_ORDER
, order
))
1119 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1120 OSSL_PKEY_PARAM_EC_GENERATOR
, gen
, gen_len
))
1121 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1122 OSSL_PKEY_PARAM_EC_SEED
, seed
, sizeof(seed
)))
1123 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1127 static int create_ec_explicit_prime_params_namedcurve(OSSL_PARAM_BLD
*bld
)
1129 static const unsigned char prime256v1_gen
[] = {
1131 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
1132 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
1133 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
1134 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
1135 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b,
1136 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
1137 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
1138 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5
1140 return do_create_ec_explicit_prime_params(bld
, prime256v1_gen
,
1141 sizeof(prime256v1_gen
));
1144 static int create_ec_explicit_prime_params(OSSL_PARAM_BLD
*bld
)
1147 static const unsigned char prime256v1_gen2
[] = {
1149 0xe4, 0x97, 0x08, 0xbe, 0x7d, 0xfa, 0xa2, 0x9a,
1150 0xa3, 0x12, 0x6f, 0xe4, 0xe7, 0xd0, 0x25, 0xe3,
1151 0x4a, 0xc1, 0x03, 0x15, 0x8c, 0xd9, 0x33, 0xc6,
1152 0x97, 0x42, 0xf5, 0xdc, 0x97, 0xb9, 0xd7, 0x31,
1153 0xe9, 0x7d, 0x74, 0x3d, 0x67, 0x6a, 0x3b, 0x21,
1154 0x08, 0x9c, 0x31, 0x73, 0xf8, 0xc1, 0x27, 0xc9,
1155 0xd2, 0xa0, 0xa0, 0x83, 0x66, 0xe0, 0xc9, 0xda,
1156 0xa8, 0xc6, 0x56, 0x2b, 0x94, 0xb1, 0xae, 0x55
1158 return do_create_ec_explicit_prime_params(bld
, prime256v1_gen2
,
1159 sizeof(prime256v1_gen2
));
1162 # ifndef OPENSSL_NO_EC2M
1163 static int do_create_ec_explicit_trinomial_params(OSSL_PARAM_BLD
*bld
,
1164 const unsigned char *gen
,
1167 BIGNUM
*a
, *b
, *poly
, *order
, *cofactor
;
1168 /* sect233k1 characteristic-two-field tpBasis */
1169 static const unsigned char poly_data
[] = {
1170 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1171 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1172 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1174 static const unsigned char a_data
[] = {
1175 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1176 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1177 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1179 static const unsigned char b_data
[] = {
1180 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1181 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1182 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1184 static const unsigned char order_data
[] = {
1185 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1186 0x00, 0x00, 0x00, 0x06, 0x9D, 0x5B, 0xB9, 0x15, 0xBC, 0xD4, 0x6E, 0xFB,
1187 0x1A, 0xD5, 0xF1, 0x73, 0xAB, 0xDF
1189 static const unsigned char cofactor_data
[]= {
1192 return TEST_ptr(a
= BN_CTX_get(bnctx
))
1193 && TEST_ptr(b
= BN_CTX_get(bnctx
))
1194 && TEST_ptr(poly
= BN_CTX_get(bnctx
))
1195 && TEST_ptr(order
= BN_CTX_get(bnctx
))
1196 && TEST_ptr(cofactor
= BN_CTX_get(bnctx
))
1197 && TEST_ptr(BN_bin2bn(poly_data
, sizeof(poly_data
), poly
))
1198 && TEST_ptr(BN_bin2bn(a_data
, sizeof(a_data
), a
))
1199 && TEST_ptr(BN_bin2bn(b_data
, sizeof(b_data
), b
))
1200 && TEST_ptr(BN_bin2bn(order_data
, sizeof(order_data
), order
))
1201 && TEST_ptr(BN_bin2bn(cofactor_data
, sizeof(cofactor_data
), cofactor
))
1202 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld
,
1203 OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
1204 SN_X9_62_characteristic_two_field
, 0))
1205 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, poly
))
1206 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, a
))
1207 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, b
))
1208 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
,
1209 OSSL_PKEY_PARAM_EC_ORDER
, order
))
1210 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1211 OSSL_PKEY_PARAM_EC_GENERATOR
, gen
, gen_len
))
1212 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1216 static int create_ec_explicit_trinomial_params_namedcurve(OSSL_PARAM_BLD
*bld
)
1218 static const unsigned char gen
[] = {
1220 0x01, 0x72, 0x32, 0xBA, 0x85, 0x3A, 0x7E, 0x73, 0x1A, 0xF1, 0x29, 0xF2,
1221 0x2F, 0xF4, 0x14, 0x95, 0x63, 0xA4, 0x19, 0xC2, 0x6B, 0xF5, 0x0A, 0x4C,
1222 0x9D, 0x6E, 0xEF, 0xAD, 0x61, 0x26,
1223 0x01, 0xDB, 0x53, 0x7D, 0xEC, 0xE8, 0x19, 0xB7, 0xF7, 0x0F, 0x55, 0x5A,
1224 0x67, 0xC4, 0x27, 0xA8, 0xCD, 0x9B, 0xF1, 0x8A, 0xEB, 0x9B, 0x56, 0xE0,
1225 0xC1, 0x10, 0x56, 0xFA, 0xE6, 0xA3
1227 return do_create_ec_explicit_trinomial_params(bld
, gen
, sizeof(gen
));
1230 static int create_ec_explicit_trinomial_params(OSSL_PARAM_BLD
*bld
)
1232 static const unsigned char gen2
[] = {
1234 0x00, 0xd7, 0xba, 0xd0, 0x26, 0x6c, 0x31, 0x6a, 0x78, 0x76, 0x01, 0xd1,
1235 0x32, 0x4b, 0x8f, 0x30, 0x29, 0x2d, 0x78, 0x30, 0xca, 0x43, 0xaa, 0xf0,
1236 0xa2, 0x5a, 0xd4, 0x0f, 0xb3, 0xf4,
1237 0x00, 0x85, 0x4b, 0x1b, 0x8d, 0x50, 0x10, 0xa5, 0x1c, 0x80, 0xf7, 0x86,
1238 0x40, 0x62, 0x4c, 0x87, 0xd1, 0x26, 0x7a, 0x9c, 0x5c, 0xe9, 0x82, 0x29,
1239 0xd1, 0x67, 0x70, 0x41, 0xea, 0xcb
1241 return do_create_ec_explicit_trinomial_params(bld
, gen2
, sizeof(gen2
));
1243 # endif /* OPENSSL_NO_EC2M */
1244 #endif /* OPENSSL_NO_EC */
1246 typedef enum OPTION_choice
{
1257 const OPTIONS
*test_get_options(void)
1259 static const OPTIONS options
[] = {
1260 OPT_TEST_OPTIONS_DEFAULT_USAGE
,
1261 { "context", OPT_CONTEXT
, '-',
1262 "Explicitly use a non-default library context" },
1263 { "rsa", OPT_RSA_FILE
, '<',
1264 "PEM format RSA key file to encode/decode" },
1265 { "pss", OPT_RSA_PSS_FILE
, '<',
1266 "PEM format RSA-PSS key file to encode/decode" },
1267 { "config", OPT_CONFIG_FILE
, '<',
1268 "The configuration file to use for the library context" },
1269 { "provider", OPT_PROVIDER_NAME
, 's',
1270 "The provider to load (The default value is 'default')" },
1276 int setup_tests(void)
1278 const char *rsa_file
= NULL
;
1279 const char *rsa_pss_file
= NULL
;
1280 const char *prov_name
= "default";
1281 char *config_file
= NULL
;
1284 #ifndef OPENSSL_NO_DSA
1285 static size_t qbits
= 160; /* PVK only tolerates 160 Q bits */
1286 static size_t pbits
= 1024; /* With 160 Q bits, we MUST use 1024 P bits */
1287 OSSL_PARAM DSA_params
[] = {
1288 OSSL_PARAM_size_t("pbits", &pbits
),
1289 OSSL_PARAM_size_t("qbits", &qbits
),
1294 #ifndef OPENSSL_NO_EC
1295 static char groupname
[] = "prime256v1";
1296 OSSL_PARAM EC_params
[] = {
1297 OSSL_PARAM_utf8_string("group", groupname
, sizeof(groupname
) - 1),
1304 while ((o
= opt_next()) != OPT_EOF
) {
1309 case OPT_PROVIDER_NAME
:
1310 prov_name
= opt_arg();
1312 case OPT_CONFIG_FILE
:
1313 config_file
= opt_arg();
1316 rsa_file
= opt_arg();
1318 case OPT_RSA_PSS_FILE
:
1319 rsa_pss_file
= opt_arg();
1321 case OPT_TEST_CASES
:
1328 if (strcmp(prov_name
, "fips") == 0)
1331 if (default_libctx
) {
1332 if (!test_get_libctx(NULL
, NULL
, config_file
, &deflprov
, prov_name
))
1335 if (!test_get_libctx(&testctx
, &nullprov
, config_file
, &deflprov
, prov_name
))
1339 /* FIPS(3.0.0): provider imports explicit params but they won't work #17998 */
1340 is_fips_3_0_0
= fips_provider_version_eq(testctx
, 3, 0, 0);
1341 if (is_fips_3_0_0
< 0)
1344 #ifdef STATIC_LEGACY
1346 * This test is always statically linked against libcrypto. We must not
1347 * attempt to load legacy.so that might be dynamically linked against
1348 * libcrypto. Instead we use a built-in version of the legacy provider.
1350 if (!OSSL_PROVIDER_add_builtin(testctx
, "legacy", ossl_legacy_provider_init
))
1354 /* Separate provider/ctx for generating the test data */
1355 if (!TEST_ptr(keyctx
= OSSL_LIB_CTX_new()))
1357 if (!TEST_ptr(keyprov
= OSSL_PROVIDER_load(keyctx
, "default")))
1360 #ifndef OPENSSL_NO_EC
1361 if (!TEST_ptr(bnctx
= BN_CTX_new_ex(testctx
))
1362 || !TEST_ptr(bld_prime_nc
= OSSL_PARAM_BLD_new())
1363 || !TEST_ptr(bld_prime
= OSSL_PARAM_BLD_new())
1364 || !create_ec_explicit_prime_params_namedcurve(bld_prime_nc
)
1365 || !create_ec_explicit_prime_params(bld_prime
)
1366 || !TEST_ptr(ec_explicit_prime_params_nc
= OSSL_PARAM_BLD_to_param(bld_prime_nc
))
1367 || !TEST_ptr(ec_explicit_prime_params_explicit
= OSSL_PARAM_BLD_to_param(bld_prime
))
1368 # ifndef OPENSSL_NO_EC2M
1369 || !TEST_ptr(bld_tri_nc
= OSSL_PARAM_BLD_new())
1370 || !TEST_ptr(bld_tri
= OSSL_PARAM_BLD_new())
1371 || !create_ec_explicit_trinomial_params_namedcurve(bld_tri_nc
)
1372 || !create_ec_explicit_trinomial_params(bld_tri
)
1373 || !TEST_ptr(ec_explicit_tri_params_nc
= OSSL_PARAM_BLD_to_param(bld_tri_nc
))
1374 || !TEST_ptr(ec_explicit_tri_params_explicit
= OSSL_PARAM_BLD_to_param(bld_tri
))
1380 TEST_info("Generating keys...");
1382 #ifndef OPENSSL_NO_DH
1383 TEST_info("Generating DH keys...");
1384 MAKE_DOMAIN_KEYS(DH
, "DH", NULL
);
1385 MAKE_DOMAIN_KEYS(DHX
, "X9.42 DH", NULL
);
1387 #ifndef OPENSSL_NO_DSA
1388 TEST_info("Generating DSA keys...");
1389 MAKE_DOMAIN_KEYS(DSA
, "DSA", DSA_params
);
1391 #ifndef OPENSSL_NO_EC
1392 TEST_info("Generating EC keys...");
1393 MAKE_DOMAIN_KEYS(EC
, "EC", EC_params
);
1394 MAKE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve
, "EC", ec_explicit_prime_params_nc
);
1395 MAKE_DOMAIN_KEYS(ECExplicitPrime2G
, "EC", ec_explicit_prime_params_explicit
);
1396 # ifndef OPENSSL_NO_EC2M
1397 MAKE_DOMAIN_KEYS(ECExplicitTriNamedCurve
, "EC", ec_explicit_tri_params_nc
);
1398 MAKE_DOMAIN_KEYS(ECExplicitTri2G
, "EC", ec_explicit_tri_params_explicit
);
1400 MAKE_KEYS(ED25519
, "ED25519", NULL
);
1401 MAKE_KEYS(ED448
, "ED448", NULL
);
1402 MAKE_KEYS(X25519
, "X25519", NULL
);
1403 MAKE_KEYS(X448
, "X448", NULL
);
1405 TEST_info("Loading RSA key...");
1406 ok
= ok
&& TEST_ptr(key_RSA
= load_pkey_pem(rsa_file
, keyctx
));
1407 TEST_info("Loading RSA_PSS key...");
1408 ok
= ok
&& TEST_ptr(key_RSA_PSS
= load_pkey_pem(rsa_pss_file
, keyctx
));
1409 TEST_info("Generating keys done");
1412 #ifndef OPENSSL_NO_DH
1414 ADD_TEST_SUITE_PARAMS(DH
);
1415 ADD_TEST_SUITE(DHX
);
1416 ADD_TEST_SUITE_PARAMS(DHX
);
1418 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
1419 * so no legacy tests.
1422 #ifndef OPENSSL_NO_DSA
1423 ADD_TEST_SUITE(DSA
);
1424 ADD_TEST_SUITE_PARAMS(DSA
);
1425 ADD_TEST_SUITE_LEGACY(DSA
);
1426 ADD_TEST_SUITE_MSBLOB(DSA
);
1427 ADD_TEST_SUITE_UNPROTECTED_PVK(DSA
);
1428 # ifndef OPENSSL_NO_RC4
1429 ADD_TEST_SUITE_PROTECTED_PVK(DSA
);
1432 #ifndef OPENSSL_NO_EC
1434 ADD_TEST_SUITE_PARAMS(EC
);
1435 ADD_TEST_SUITE_LEGACY(EC
);
1436 ADD_TEST_SUITE(ECExplicitPrimeNamedCurve
);
1437 ADD_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve
);
1438 ADD_TEST_SUITE(ECExplicitPrime2G
);
1439 ADD_TEST_SUITE_LEGACY(ECExplicitPrime2G
);
1440 # ifndef OPENSSL_NO_EC2M
1441 ADD_TEST_SUITE(ECExplicitTriNamedCurve
);
1442 ADD_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve
);
1443 ADD_TEST_SUITE(ECExplicitTri2G
);
1444 ADD_TEST_SUITE_LEGACY(ECExplicitTri2G
);
1446 ADD_TEST_SUITE(ED25519
);
1447 ADD_TEST_SUITE(ED448
);
1448 ADD_TEST_SUITE(X25519
);
1449 ADD_TEST_SUITE(X448
);
1451 * ED25519, ED448, X25519 and X448 have no support for
1452 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1455 ADD_TEST_SUITE(RSA
);
1456 ADD_TEST_SUITE_LEGACY(RSA
);
1457 ADD_TEST_SUITE(RSA_PSS
);
1459 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1460 * so no legacy tests.
1462 ADD_TEST_SUITE_MSBLOB(RSA
);
1463 ADD_TEST_SUITE_UNPROTECTED_PVK(RSA
);
1464 # ifndef OPENSSL_NO_RC4
1465 ADD_TEST_SUITE_PROTECTED_PVK(RSA
);
1472 void cleanup_tests(void)
1474 #ifndef OPENSSL_NO_EC
1475 OSSL_PARAM_free(ec_explicit_prime_params_nc
);
1476 OSSL_PARAM_free(ec_explicit_prime_params_explicit
);
1477 OSSL_PARAM_BLD_free(bld_prime_nc
);
1478 OSSL_PARAM_BLD_free(bld_prime
);
1479 # ifndef OPENSSL_NO_EC2M
1480 OSSL_PARAM_free(ec_explicit_tri_params_nc
);
1481 OSSL_PARAM_free(ec_explicit_tri_params_explicit
);
1482 OSSL_PARAM_BLD_free(bld_tri_nc
);
1483 OSSL_PARAM_BLD_free(bld_tri
);
1486 #endif /* OPENSSL_NO_EC */
1488 #ifndef OPENSSL_NO_DH
1489 FREE_DOMAIN_KEYS(DH
);
1490 FREE_DOMAIN_KEYS(DHX
);
1492 #ifndef OPENSSL_NO_DSA
1493 FREE_DOMAIN_KEYS(DSA
);
1495 #ifndef OPENSSL_NO_EC
1496 FREE_DOMAIN_KEYS(EC
);
1497 FREE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve
);
1498 FREE_DOMAIN_KEYS(ECExplicitPrime2G
);
1499 # ifndef OPENSSL_NO_EC2M
1500 FREE_DOMAIN_KEYS(ECExplicitTriNamedCurve
);
1501 FREE_DOMAIN_KEYS(ECExplicitTri2G
);
1511 OSSL_PROVIDER_unload(nullprov
);
1512 OSSL_PROVIDER_unload(deflprov
);
1513 OSSL_PROVIDER_unload(keyprov
);
1514 OSSL_LIB_CTX_free(testctx
);
1515 OSSL_LIB_CTX_free(keyctx
);