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"
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;
51 static OSSL_LIB_CTX
*testctx
= NULL
;
52 static OSSL_LIB_CTX
*keyctx
= NULL
;
53 static char *testpropq
= NULL
;
55 static OSSL_PROVIDER
*nullprov
= NULL
;
56 static OSSL_PROVIDER
*deflprov
= NULL
;
57 static OSSL_PROVIDER
*keyprov
= NULL
;
60 static BN_CTX
*bnctx
= NULL
;
61 static OSSL_PARAM_BLD
*bld_prime_nc
= NULL
;
62 static OSSL_PARAM_BLD
*bld_prime
= NULL
;
63 static OSSL_PARAM
*ec_explicit_prime_params_nc
= NULL
;
64 static OSSL_PARAM
*ec_explicit_prime_params_explicit
= NULL
;
66 # ifndef OPENSSL_NO_EC2M
67 static OSSL_PARAM_BLD
*bld_tri_nc
= NULL
;
68 static OSSL_PARAM_BLD
*bld_tri
= NULL
;
69 static OSSL_PARAM
*ec_explicit_tri_params_nc
= NULL
;
70 static OSSL_PARAM
*ec_explicit_tri_params_explicit
= NULL
;
74 #ifndef OPENSSL_NO_KEYPARAMS
75 static EVP_PKEY
*make_template(const char *type
, OSSL_PARAM
*genparams
)
77 EVP_PKEY
*pkey
= NULL
;
78 EVP_PKEY_CTX
*ctx
= NULL
;
80 # ifndef OPENSSL_NO_DH
82 * Use 512-bit DH(X) keys with predetermined parameters for efficiency,
83 * for testing only. Use a minimum key size of 2048 for security purposes.
85 if (strcmp(type
, "DH") == 0)
86 return get_dh512(keyctx
);
88 if (strcmp(type
, "X9.42 DH") == 0)
89 return get_dhx512(keyctx
);
93 * No real need to check the errors other than for the cascade
94 * effect. |pkey| will simply remain NULL if something goes wrong.
96 (void)((ctx
= EVP_PKEY_CTX_new_from_name(keyctx
, type
, testpropq
)) != NULL
97 && EVP_PKEY_paramgen_init(ctx
) > 0
99 || EVP_PKEY_CTX_set_params(ctx
, genparams
) > 0)
100 && EVP_PKEY_generate(ctx
, &pkey
) > 0);
101 EVP_PKEY_CTX_free(ctx
);
107 #if !defined(OPENSSL_NO_DH) || !defined(OPENSSL_NO_DSA) || !defined(OPENSSL_NO_EC)
108 static EVP_PKEY
*make_key(const char *type
, EVP_PKEY
*template,
109 OSSL_PARAM
*genparams
)
111 EVP_PKEY
*pkey
= NULL
;
114 ? EVP_PKEY_CTX_new_from_pkey(keyctx
, template, testpropq
)
115 : EVP_PKEY_CTX_new_from_name(keyctx
, type
, testpropq
);
118 * No real need to check the errors other than for the cascade
119 * effect. |pkey| will simply remain NULL if something goes wrong.
122 && EVP_PKEY_keygen_init(ctx
) > 0
123 && (genparams
== NULL
124 || EVP_PKEY_CTX_set_params(ctx
, genparams
) > 0)
125 && EVP_PKEY_keygen(ctx
, &pkey
) > 0);
126 EVP_PKEY_CTX_free(ctx
);
131 /* Main test driver */
133 typedef int (encoder
)(const char *file
, const int line
,
134 void **encoded
, long *encoded_len
,
135 void *object
, int selection
,
136 const char *output_type
, const char *output_structure
,
137 const char *pass
, const char *pcipher
);
138 typedef int (decoder
)(const char *file
, const int line
,
139 void **object
, void *encoded
, long encoded_len
,
140 const char *input_type
, const char *structure_type
,
141 const char *keytype
, int selection
, const char *pass
);
142 typedef int (tester
)(const char *file
, const int line
,
143 const void *data1
, size_t data1_len
,
144 const void *data2
, size_t data2_len
);
145 typedef int (checker
)(const char *file
, const int line
,
146 const char *type
, const void *data
, size_t data_len
);
147 typedef void (dumper
)(const char *label
, const void *data
, size_t data_len
);
149 #define FLAG_DECODE_WITH_TYPE 0x0001
151 static int test_encode_decode(const char *file
, const int line
,
152 const char *type
, EVP_PKEY
*pkey
,
153 int selection
, const char *output_type
,
154 const char *output_structure
,
155 const char *pass
, const char *pcipher
,
156 encoder
*encode_cb
, decoder
*decode_cb
,
157 tester
*test_cb
, checker
*check_cb
,
158 dumper
*dump_cb
, int flags
)
160 void *encoded
= NULL
;
161 long encoded_len
= 0;
162 EVP_PKEY
*pkey2
= NULL
;
163 void *encoded2
= NULL
;
164 long encoded2_len
= 0;
168 * Encode |pkey|, decode the result into |pkey2|, and finish off by
169 * encoding |pkey2| as well. That last encoding is for checking and
172 if (!TEST_true(encode_cb(file
, line
, &encoded
, &encoded_len
, pkey
, selection
,
173 output_type
, output_structure
, pass
, pcipher
))
174 || !TEST_true(check_cb(file
, line
, type
, encoded
, encoded_len
))
175 || !TEST_true(decode_cb(file
, line
, (void **)&pkey2
, encoded
, encoded_len
,
176 output_type
, output_structure
,
177 (flags
& FLAG_DECODE_WITH_TYPE
? type
: NULL
),
179 || !TEST_true(encode_cb(file
, line
, &encoded2
, &encoded2_len
, pkey2
, selection
,
180 output_type
, output_structure
, pass
, pcipher
)))
183 if (selection
== OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) {
184 if (!TEST_int_eq(EVP_PKEY_parameters_eq(pkey
, pkey2
), 1))
187 if (!TEST_int_eq(EVP_PKEY_eq(pkey
, pkey2
), 1))
192 * Double check the encoding, but only for unprotected keys,
193 * as protected keys have a random component, which makes the output
196 if ((pass
== NULL
&& pcipher
== NULL
)
197 && !test_cb(file
, line
, encoded
, encoded_len
, encoded2
, encoded2_len
))
203 if (encoded
!= NULL
&& encoded_len
!= 0)
204 dump_cb("|pkey| encoded", encoded
, encoded_len
);
205 if (encoded2
!= NULL
&& encoded2_len
!= 0)
206 dump_cb("|pkey2| encoded", encoded2
, encoded2_len
);
209 OPENSSL_free(encoded
);
210 OPENSSL_free(encoded2
);
211 EVP_PKEY_free(pkey2
);
215 /* Encoding and decoding methods */
217 static int encode_EVP_PKEY_prov(const char *file
, const int line
,
218 void **encoded
, long *encoded_len
,
219 void *object
, int selection
,
220 const char *output_type
,
221 const char *output_structure
,
222 const char *pass
, const char *pcipher
)
224 EVP_PKEY
*pkey
= object
;
225 OSSL_ENCODER_CTX
*ectx
= NULL
;
227 BUF_MEM
*mem_buf
= NULL
;
228 const unsigned char *upass
= (const unsigned char *)pass
;
231 if (!TEST_FL_ptr(ectx
= OSSL_ENCODER_CTX_new_for_pkey(pkey
, selection
,
235 || !TEST_FL_int_gt(OSSL_ENCODER_CTX_get_num_encoders(ectx
), 0)
237 && !TEST_FL_true(OSSL_ENCODER_CTX_set_passphrase(ectx
, upass
,
240 && !TEST_FL_true(OSSL_ENCODER_CTX_set_cipher(ectx
, pcipher
, NULL
)))
241 || !TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
242 || !TEST_FL_true(OSSL_ENCODER_to_bio(ectx
, mem_ser
))
243 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
244 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
245 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
248 /* Detach the encoded output */
249 mem_buf
->data
= NULL
;
254 OSSL_ENCODER_CTX_free(ectx
);
258 static int decode_EVP_PKEY_prov(const char *file
, const int line
,
259 void **object
, void *encoded
, long encoded_len
,
260 const char *input_type
,
261 const char *structure_type
,
262 const char *keytype
, int selection
,
265 EVP_PKEY
*pkey
= NULL
, *testpkey
= NULL
;
266 OSSL_DECODER_CTX
*dctx
= NULL
;
267 BIO
*encoded_bio
= NULL
;
268 const unsigned char *upass
= (const unsigned char *)pass
;
273 if (strcmp(input_type
, "DER") == 0)
278 if (!TEST_FL_ptr(encoded_bio
= BIO_new_mem_buf(encoded
, encoded_len
)))
282 * We attempt the decode 3 times. The first time we provide the expected
283 * starting input type. The second time we provide NULL for the starting
284 * type. The third time we provide a bad starting input type.
285 * The bad starting input type should fail. The other two should succeed
286 * and produce the same result.
288 for (i
= 0; i
< 3; i
++) {
289 const char *testtype
= (i
== 0) ? input_type
290 : ((i
== 1) ? NULL
: badtype
);
292 if (!TEST_FL_ptr(dctx
= OSSL_DECODER_CTX_new_for_pkey(&testpkey
,
299 && !OSSL_DECODER_CTX_set_passphrase(dctx
, upass
, strlen(pass
)))
300 || !TEST_FL_int_gt(BIO_reset(encoded_bio
), 0)
301 /* We expect to fail when using a bad input type */
302 || !TEST_FL_int_eq(OSSL_DECODER_from_bio(dctx
, encoded_bio
),
305 OSSL_DECODER_CTX_free(dctx
);
312 if (selection
== OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
) {
313 if (!TEST_FL_int_eq(EVP_PKEY_parameters_eq(pkey
, testpkey
), 1))
316 if (!TEST_FL_int_eq(EVP_PKEY_eq(pkey
, testpkey
), 1))
327 EVP_PKEY_free(testpkey
);
328 BIO_free(encoded_bio
);
329 OSSL_DECODER_CTX_free(dctx
);
333 static int encode_EVP_PKEY_legacy_PEM(const char *file
, const int line
,
334 void **encoded
, long *encoded_len
,
335 void *object
, ossl_unused
int selection
,
336 ossl_unused
const char *output_type
,
337 ossl_unused
const char *output_structure
,
338 const char *pass
, const char *pcipher
)
340 EVP_PKEY
*pkey
= object
;
341 EVP_CIPHER
*cipher
= NULL
;
343 BUF_MEM
*mem_buf
= NULL
;
344 const unsigned char *upass
= (const unsigned char *)pass
;
348 if (pcipher
!= NULL
&& pass
!= NULL
) {
349 passlen
= strlen(pass
);
350 if (!TEST_FL_ptr(cipher
= EVP_CIPHER_fetch(testctx
, pcipher
, testpropq
)))
353 if (!TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
354 || !TEST_FL_true(PEM_write_bio_PrivateKey_traditional(mem_ser
, pkey
,
358 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
359 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
360 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
363 /* Detach the encoded output */
364 mem_buf
->data
= NULL
;
369 EVP_CIPHER_free(cipher
);
373 static int encode_EVP_PKEY_MSBLOB(const char *file
, const int line
,
374 void **encoded
, long *encoded_len
,
375 void *object
, int selection
,
376 ossl_unused
const char *output_type
,
377 ossl_unused
const char *output_structure
,
378 ossl_unused
const char *pass
,
379 ossl_unused
const char *pcipher
)
381 EVP_PKEY
*pkey
= object
;
383 BUF_MEM
*mem_buf
= NULL
;
386 if (!TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem())))
389 if ((selection
& OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0) {
390 if (!TEST_FL_int_ge(i2b_PrivateKey_bio(mem_ser
, pkey
), 0))
393 if (!TEST_FL_int_ge(i2b_PublicKey_bio(mem_ser
, pkey
), 0))
397 if (!TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
398 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
399 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
402 /* Detach the encoded output */
403 mem_buf
->data
= NULL
;
411 static pem_password_cb pass_pw
;
412 static int pass_pw(char *buf
, int size
, int rwflag
, void *userdata
)
414 OPENSSL_strlcpy(buf
, userdata
, size
);
415 return strlen(userdata
);
418 static int encode_EVP_PKEY_PVK(const char *file
, const int line
,
419 void **encoded
, long *encoded_len
,
420 void *object
, int selection
,
421 ossl_unused
const char *output_type
,
422 ossl_unused
const char *output_structure
,
424 ossl_unused
const char *pcipher
)
426 EVP_PKEY
*pkey
= object
;
428 BUF_MEM
*mem_buf
= NULL
;
429 int enc
= (pass
!= NULL
);
432 if (!TEST_FL_true(ossl_assert((selection
433 & OSSL_KEYMGMT_SELECT_PRIVATE_KEY
) != 0))
434 || !TEST_FL_ptr(mem_ser
= BIO_new(BIO_s_mem()))
435 || !TEST_FL_int_ge(i2b_PVK_bio_ex(mem_ser
, pkey
, enc
,
436 pass_pw
, (void *)pass
, testctx
, testpropq
), 0)
437 || !TEST_FL_true(BIO_get_mem_ptr(mem_ser
, &mem_buf
) > 0)
438 || !TEST_FL_ptr(*encoded
= mem_buf
->data
)
439 || !TEST_FL_long_gt(*encoded_len
= mem_buf
->length
, 0))
442 /* Detach the encoded output */
443 mem_buf
->data
= NULL
;
451 static int test_text(const char *file
, const int line
,
452 const void *data1
, size_t data1_len
,
453 const void *data2
, size_t data2_len
)
455 return TEST_FL_strn2_eq(data1
, data1_len
, data2
, data2_len
);
458 static int test_mem(const char *file
, const int line
,
459 const void *data1
, size_t data1_len
,
460 const void *data2
, size_t data2_len
)
462 return TEST_FL_mem_eq(data1
, data1_len
, data2
, data2_len
);
465 /* Test cases and their dumpers / checkers */
467 static void collect_name(const char *name
, void *arg
)
469 char **namelist
= arg
;
473 space
= strlen(name
);
474 if (*namelist
!= NULL
)
475 space
+= strlen(*namelist
) + 2 /* for comma and space */;
476 space
++; /* for terminating null byte */
478 new_namelist
= OPENSSL_realloc(*namelist
, space
);
479 if (new_namelist
== NULL
)
481 if (*namelist
!= NULL
) {
482 strcat(new_namelist
, ", ");
483 strcat(new_namelist
, name
);
485 strcpy(new_namelist
, name
);
487 *namelist
= new_namelist
;
490 static void dump_der(const char *label
, const void *data
, size_t data_len
)
492 test_output_memory(label
, data
, data_len
);
495 static void dump_pem(const char *label
, const void *data
, size_t data_len
)
497 test_output_string(label
, data
, data_len
- 1);
500 static int check_unprotected_PKCS8_DER(const char *file
, const int line
,
502 const void *data
, size_t data_len
)
504 const unsigned char *datap
= data
;
505 PKCS8_PRIV_KEY_INFO
*p8inf
=
506 d2i_PKCS8_PRIV_KEY_INFO(NULL
, &datap
, data_len
);
509 if (TEST_FL_ptr(p8inf
)) {
510 EVP_PKEY
*pkey
= EVP_PKCS82PKEY_ex(p8inf
, testctx
, testpropq
);
511 char *namelist
= NULL
;
513 if (TEST_FL_ptr(pkey
)) {
514 if (!(ok
= TEST_FL_true(EVP_PKEY_is_a(pkey
, type
)))) {
515 EVP_PKEY_type_names_do_all(pkey
, collect_name
, &namelist
);
516 if (namelist
!= NULL
)
517 TEST_note("%s isn't any of %s", type
, namelist
);
518 OPENSSL_free(namelist
);
520 ok
= ok
&& TEST_FL_true(evp_pkey_is_provided(pkey
));
524 PKCS8_PRIV_KEY_INFO_free(p8inf
);
528 static int test_unprotected_via_DER(const char *type
, EVP_PKEY
*key
)
530 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
531 OSSL_KEYMGMT_SELECT_KEYPAIR
532 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
533 "DER", "PrivateKeyInfo", NULL
, NULL
,
534 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
535 test_mem
, check_unprotected_PKCS8_DER
,
539 static int check_unprotected_PKCS8_PEM(const char *file
, const int line
,
541 const void *data
, size_t data_len
)
543 static const char expected_pem_header
[] =
544 "-----BEGIN " PEM_STRING_PKCS8INF
"-----";
546 return TEST_FL_strn_eq(data
, expected_pem_header
,
547 sizeof(expected_pem_header
) - 1);
550 static int test_unprotected_via_PEM(const char *type
, EVP_PKEY
*key
)
552 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
553 OSSL_KEYMGMT_SELECT_KEYPAIR
554 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
555 "PEM", "PrivateKeyInfo", NULL
, NULL
,
556 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
557 test_text
, check_unprotected_PKCS8_PEM
,
561 #ifndef OPENSSL_NO_KEYPARAMS
562 static int check_params_DER(const char *file
, const int line
,
563 const char *type
, const void *data
, size_t data_len
)
565 const unsigned char *datap
= data
;
567 int itype
= NID_undef
;
568 EVP_PKEY
*pkey
= NULL
;
570 if (strcmp(type
, "DH") == 0)
572 else if (strcmp(type
, "X9.42 DH") == 0)
573 itype
= EVP_PKEY_DHX
;
574 else if (strcmp(type
, "DSA") == 0)
575 itype
= EVP_PKEY_DSA
;
576 else if (strcmp(type
, "EC") == 0)
579 if (itype
!= NID_undef
) {
580 pkey
= d2i_KeyParams(itype
, NULL
, &datap
, data_len
);
588 static int check_params_PEM(const char *file
, const int line
,
590 const void *data
, size_t data_len
)
592 static char expected_pem_header
[80];
595 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
596 sizeof(expected_pem_header
),
597 "-----BEGIN %s PARAMETERS-----", type
), 0)
598 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
));
601 static int test_params_via_DER(const char *type
, EVP_PKEY
*key
)
603 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
604 "DER", "type-specific", NULL
, NULL
,
605 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
606 test_mem
, check_params_DER
,
607 dump_der
, FLAG_DECODE_WITH_TYPE
);
610 static int test_params_via_PEM(const char *type
, EVP_PKEY
*key
)
612 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
613 "PEM", "type-specific", NULL
, NULL
,
614 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
615 test_text
, check_params_PEM
,
618 #endif /* !OPENSSL_NO_KEYPARAMS */
620 static int check_unprotected_legacy_PEM(const char *file
, const int line
,
622 const void *data
, size_t data_len
)
624 static char expected_pem_header
[80];
627 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
628 sizeof(expected_pem_header
),
629 "-----BEGIN %s PRIVATE KEY-----", type
), 0)
630 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
));
633 static int test_unprotected_via_legacy_PEM(const char *type
, EVP_PKEY
*key
)
635 if (!default_libctx
|| is_fips
)
636 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
638 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
639 OSSL_KEYMGMT_SELECT_KEYPAIR
640 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
641 "PEM", "type-specific", NULL
, NULL
,
642 encode_EVP_PKEY_legacy_PEM
, decode_EVP_PKEY_prov
,
643 test_text
, check_unprotected_legacy_PEM
,
647 static int check_MSBLOB(const char *file
, const int line
,
648 const char *type
, const void *data
, size_t data_len
)
650 const unsigned char *datap
= data
;
651 EVP_PKEY
*pkey
= b2i_PrivateKey(&datap
, data_len
);
652 int ok
= TEST_FL_ptr(pkey
);
658 static int test_unprotected_via_MSBLOB(const char *type
, EVP_PKEY
*key
)
660 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
661 OSSL_KEYMGMT_SELECT_KEYPAIR
662 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
663 "MSBLOB", NULL
, NULL
, NULL
,
664 encode_EVP_PKEY_MSBLOB
, decode_EVP_PKEY_prov
,
665 test_mem
, check_MSBLOB
,
669 static int check_PVK(const char *file
, const int line
,
670 const char *type
, const void *data
, size_t data_len
)
672 const unsigned char *in
= data
;
673 unsigned int saltlen
= 0, keylen
= 0;
674 int ok
= ossl_do_PVK_header(&in
, data_len
, 0, &saltlen
, &keylen
);
679 static int test_unprotected_via_PVK(const char *type
, EVP_PKEY
*key
)
681 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
682 OSSL_KEYMGMT_SELECT_KEYPAIR
683 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
684 "PVK", NULL
, NULL
, NULL
,
685 encode_EVP_PKEY_PVK
, decode_EVP_PKEY_prov
,
690 static const char *pass_cipher
= "AES-256-CBC";
691 static const char *pass
= "the holy handgrenade of antioch";
693 static int check_protected_PKCS8_DER(const char *file
, const int line
,
695 const void *data
, size_t data_len
)
697 const unsigned char *datap
= data
;
698 X509_SIG
*p8
= d2i_X509_SIG(NULL
, &datap
, data_len
);
699 int ok
= TEST_FL_ptr(p8
);
705 static int test_protected_via_DER(const char *type
, EVP_PKEY
*key
)
707 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
708 OSSL_KEYMGMT_SELECT_KEYPAIR
709 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
710 "DER", "EncryptedPrivateKeyInfo",
712 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
713 test_mem
, check_protected_PKCS8_DER
,
717 static int check_protected_PKCS8_PEM(const char *file
, const int line
,
719 const void *data
, size_t data_len
)
721 static const char expected_pem_header
[] =
722 "-----BEGIN " PEM_STRING_PKCS8
"-----";
724 return TEST_FL_strn_eq(data
, expected_pem_header
,
725 sizeof(expected_pem_header
) - 1);
728 static int test_protected_via_PEM(const char *type
, EVP_PKEY
*key
)
730 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
731 OSSL_KEYMGMT_SELECT_KEYPAIR
732 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
733 "PEM", "EncryptedPrivateKeyInfo",
735 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
736 test_text
, check_protected_PKCS8_PEM
,
740 static int check_protected_legacy_PEM(const char *file
, const int line
,
742 const void *data
, size_t data_len
)
744 static char expected_pem_header
[80];
747 TEST_FL_int_gt(BIO_snprintf(expected_pem_header
,
748 sizeof(expected_pem_header
),
749 "-----BEGIN %s PRIVATE KEY-----", type
), 0)
750 && TEST_FL_strn_eq(data
, expected_pem_header
, strlen(expected_pem_header
))
751 && TEST_FL_ptr(strstr(data
, "\nDEK-Info: "));
754 static int test_protected_via_legacy_PEM(const char *type
, EVP_PKEY
*key
)
756 if (!default_libctx
|| is_fips
)
757 return TEST_skip("Test not available if using a non-default library context or FIPS provider");
759 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
760 OSSL_KEYMGMT_SELECT_KEYPAIR
761 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
762 "PEM", "type-specific", pass
, pass_cipher
,
763 encode_EVP_PKEY_legacy_PEM
, decode_EVP_PKEY_prov
,
764 test_text
, check_protected_legacy_PEM
,
768 #ifndef OPENSSL_NO_RC4
769 static int test_protected_via_PVK(const char *type
, EVP_PKEY
*key
)
772 OSSL_PROVIDER
*lgcyprov
= OSSL_PROVIDER_load(testctx
, "legacy");
773 if (lgcyprov
== NULL
)
774 return TEST_skip("Legacy provider not available");
776 ret
= test_encode_decode(__FILE__
, __LINE__
, type
, key
,
777 OSSL_KEYMGMT_SELECT_KEYPAIR
778 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
779 "PVK", NULL
, pass
, NULL
,
780 encode_EVP_PKEY_PVK
, decode_EVP_PKEY_prov
,
781 test_mem
, check_PVK
, dump_der
, 0);
782 OSSL_PROVIDER_unload(lgcyprov
);
787 static int check_public_DER(const char *file
, const int line
,
788 const char *type
, const void *data
, size_t data_len
)
790 const unsigned char *datap
= data
;
791 EVP_PKEY
*pkey
= d2i_PUBKEY_ex(NULL
, &datap
, data_len
, testctx
, testpropq
);
792 int ok
= (TEST_FL_ptr(pkey
) && TEST_FL_true(EVP_PKEY_is_a(pkey
, type
)));
798 static int test_public_via_DER(const char *type
, EVP_PKEY
*key
)
800 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
801 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
802 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
803 "DER", "SubjectPublicKeyInfo", NULL
, NULL
,
804 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
805 test_mem
, check_public_DER
, dump_der
, 0);
808 static int check_public_PEM(const char *file
, const int line
,
809 const char *type
, const void *data
, size_t data_len
)
811 static const char expected_pem_header
[] =
812 "-----BEGIN " PEM_STRING_PUBLIC
"-----";
815 TEST_FL_strn_eq(data
, expected_pem_header
,
816 sizeof(expected_pem_header
) - 1);
819 static int test_public_via_PEM(const char *type
, EVP_PKEY
*key
)
821 return test_encode_decode(__FILE__
, __LINE__
, type
, key
,
822 OSSL_KEYMGMT_SELECT_PUBLIC_KEY
823 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
,
824 "PEM", "SubjectPublicKeyInfo", NULL
, NULL
,
825 encode_EVP_PKEY_prov
, decode_EVP_PKEY_prov
,
826 test_text
, check_public_PEM
, dump_pem
, 0);
829 static int check_public_MSBLOB(const char *file
, const int line
,
831 const void *data
, size_t data_len
)
833 const unsigned char *datap
= data
;
834 EVP_PKEY
*pkey
= b2i_PublicKey(&datap
, data_len
);
835 int ok
= TEST_FL_ptr(pkey
);
841 static int test_public_via_MSBLOB(const char *type
, EVP_PKEY
*key
)
843 return test_encode_decode(__FILE__
, __LINE__
, type
, key
, OSSL_KEYMGMT_SELECT_PUBLIC_KEY
844 | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
,
845 "MSBLOB", NULL
, NULL
, NULL
,
846 encode_EVP_PKEY_MSBLOB
, decode_EVP_PKEY_prov
,
847 test_mem
, check_public_MSBLOB
, dump_der
, 0);
850 #define KEYS(KEYTYPE) \
851 static EVP_PKEY *key_##KEYTYPE = NULL
852 #define MAKE_KEYS(KEYTYPE, KEYTYPEstr, params) \
854 && TEST_ptr(key_##KEYTYPE = make_key(KEYTYPEstr, NULL, params))
855 #define FREE_KEYS(KEYTYPE) \
856 EVP_PKEY_free(key_##KEYTYPE); \
858 #define DOMAIN_KEYS(KEYTYPE) \
859 static EVP_PKEY *template_##KEYTYPE = NULL; \
860 static EVP_PKEY *key_##KEYTYPE = NULL
861 #define MAKE_DOMAIN_KEYS(KEYTYPE, KEYTYPEstr, params) \
863 && TEST_ptr(template_##KEYTYPE = \
864 make_template(KEYTYPEstr, params)) \
865 && TEST_ptr(key_##KEYTYPE = \
866 make_key(KEYTYPEstr, template_##KEYTYPE, NULL))
867 #define FREE_DOMAIN_KEYS(KEYTYPE) \
868 EVP_PKEY_free(template_##KEYTYPE); \
869 EVP_PKEY_free(key_##KEYTYPE)
871 #define IMPLEMENT_TEST_SUITE(KEYTYPE, KEYTYPEstr) \
872 static int test_unprotected_##KEYTYPE##_via_DER(void) \
874 return test_unprotected_via_DER(KEYTYPEstr, key_##KEYTYPE); \
876 static int test_unprotected_##KEYTYPE##_via_PEM(void) \
878 return test_unprotected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
880 static int test_protected_##KEYTYPE##_via_DER(void) \
882 return test_protected_via_DER(KEYTYPEstr, key_##KEYTYPE); \
884 static int test_protected_##KEYTYPE##_via_PEM(void) \
886 return test_protected_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
888 static int test_public_##KEYTYPE##_via_DER(void) \
890 return test_public_via_DER(KEYTYPEstr, key_##KEYTYPE); \
892 static int test_public_##KEYTYPE##_via_PEM(void) \
894 return test_public_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
897 #define ADD_TEST_SUITE(KEYTYPE) \
898 ADD_TEST(test_unprotected_##KEYTYPE##_via_DER); \
899 ADD_TEST(test_unprotected_##KEYTYPE##_via_PEM); \
900 ADD_TEST(test_protected_##KEYTYPE##_via_DER); \
901 ADD_TEST(test_protected_##KEYTYPE##_via_PEM); \
902 ADD_TEST(test_public_##KEYTYPE##_via_DER); \
903 ADD_TEST(test_public_##KEYTYPE##_via_PEM)
905 #define IMPLEMENT_TEST_SUITE_PARAMS(KEYTYPE, KEYTYPEstr) \
906 static int test_params_##KEYTYPE##_via_DER(void) \
908 return test_params_via_DER(KEYTYPEstr, key_##KEYTYPE); \
910 static int test_params_##KEYTYPE##_via_PEM(void) \
912 return test_params_via_PEM(KEYTYPEstr, key_##KEYTYPE); \
915 #define ADD_TEST_SUITE_PARAMS(KEYTYPE) \
916 ADD_TEST(test_params_##KEYTYPE##_via_DER); \
917 ADD_TEST(test_params_##KEYTYPE##_via_PEM)
919 #define IMPLEMENT_TEST_SUITE_LEGACY(KEYTYPE, KEYTYPEstr) \
920 static int test_unprotected_##KEYTYPE##_via_legacy_PEM(void) \
923 test_unprotected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
925 static int test_protected_##KEYTYPE##_via_legacy_PEM(void) \
928 test_protected_via_legacy_PEM(KEYTYPEstr, key_##KEYTYPE); \
931 #define ADD_TEST_SUITE_LEGACY(KEYTYPE) \
932 ADD_TEST(test_unprotected_##KEYTYPE##_via_legacy_PEM); \
933 ADD_TEST(test_protected_##KEYTYPE##_via_legacy_PEM)
935 #define IMPLEMENT_TEST_SUITE_MSBLOB(KEYTYPE, KEYTYPEstr) \
936 static int test_unprotected_##KEYTYPE##_via_MSBLOB(void) \
938 return test_unprotected_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
940 static int test_public_##KEYTYPE##_via_MSBLOB(void) \
942 return test_public_via_MSBLOB(KEYTYPEstr, key_##KEYTYPE); \
945 #define ADD_TEST_SUITE_MSBLOB(KEYTYPE) \
946 ADD_TEST(test_unprotected_##KEYTYPE##_via_MSBLOB); \
947 ADD_TEST(test_public_##KEYTYPE##_via_MSBLOB)
949 #define IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
950 static int test_unprotected_##KEYTYPE##_via_PVK(void) \
952 return test_unprotected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
954 # define ADD_TEST_SUITE_UNPROTECTED_PVK(KEYTYPE) \
955 ADD_TEST(test_unprotected_##KEYTYPE##_via_PVK)
956 #ifndef OPENSSL_NO_RC4
957 # define IMPLEMENT_TEST_SUITE_PROTECTED_PVK(KEYTYPE, KEYTYPEstr) \
958 static int test_protected_##KEYTYPE##_via_PVK(void) \
960 return test_protected_via_PVK(KEYTYPEstr, key_##KEYTYPE); \
962 # define ADD_TEST_SUITE_PROTECTED_PVK(KEYTYPE) \
963 ADD_TEST(test_protected_##KEYTYPE##_via_PVK)
966 #ifndef OPENSSL_NO_DH
968 IMPLEMENT_TEST_SUITE(DH
, "DH")
969 IMPLEMENT_TEST_SUITE_PARAMS(DH
, "DH")
971 IMPLEMENT_TEST_SUITE(DHX
, "X9.42 DH")
972 IMPLEMENT_TEST_SUITE_PARAMS(DHX
, "X9.42 DH")
974 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
975 * so no legacy tests.
978 #ifndef OPENSSL_NO_DSA
980 IMPLEMENT_TEST_SUITE(DSA
, "DSA")
981 IMPLEMENT_TEST_SUITE_PARAMS(DSA
, "DSA")
982 IMPLEMENT_TEST_SUITE_LEGACY(DSA
, "DSA")
983 IMPLEMENT_TEST_SUITE_MSBLOB(DSA
, "DSA")
984 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(DSA
, "DSA")
985 # ifndef OPENSSL_NO_RC4
986 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(DSA
, "DSA")
989 #ifndef OPENSSL_NO_EC
991 IMPLEMENT_TEST_SUITE(EC
, "EC")
992 IMPLEMENT_TEST_SUITE_PARAMS(EC
, "EC")
993 IMPLEMENT_TEST_SUITE_LEGACY(EC
, "EC")
994 DOMAIN_KEYS(ECExplicitPrimeNamedCurve
);
995 IMPLEMENT_TEST_SUITE(ECExplicitPrimeNamedCurve
, "EC")
996 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve
, "EC")
997 DOMAIN_KEYS(ECExplicitPrime2G
);
998 IMPLEMENT_TEST_SUITE(ECExplicitPrime2G
, "EC")
999 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitPrime2G
, "EC")
1000 # ifndef OPENSSL_NO_EC2M
1001 DOMAIN_KEYS(ECExplicitTriNamedCurve
);
1002 IMPLEMENT_TEST_SUITE(ECExplicitTriNamedCurve
, "EC")
1003 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve
, "EC")
1004 DOMAIN_KEYS(ECExplicitTri2G
);
1005 IMPLEMENT_TEST_SUITE(ECExplicitTri2G
, "EC")
1006 IMPLEMENT_TEST_SUITE_LEGACY(ECExplicitTri2G
, "EC")
1009 IMPLEMENT_TEST_SUITE(ED25519
, "ED25519")
1011 IMPLEMENT_TEST_SUITE(ED448
, "ED448")
1013 IMPLEMENT_TEST_SUITE(X25519
, "X25519")
1015 IMPLEMENT_TEST_SUITE(X448
, "X448")
1017 * ED25519, ED448, X25519 and X448 have no support for
1018 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1022 IMPLEMENT_TEST_SUITE(RSA
, "RSA")
1023 IMPLEMENT_TEST_SUITE_LEGACY(RSA
, "RSA")
1025 IMPLEMENT_TEST_SUITE(RSA_PSS
, "RSA-PSS")
1027 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1028 * so no legacy tests.
1030 IMPLEMENT_TEST_SUITE_MSBLOB(RSA
, "RSA")
1031 IMPLEMENT_TEST_SUITE_UNPROTECTED_PVK(RSA
, "RSA")
1032 #ifndef OPENSSL_NO_RC4
1033 IMPLEMENT_TEST_SUITE_PROTECTED_PVK(RSA
, "RSA")
1036 #ifndef OPENSSL_NO_EC
1037 /* Explicit parameters that match a named curve */
1038 static int do_create_ec_explicit_prime_params(OSSL_PARAM_BLD
*bld
,
1039 const unsigned char *gen
,
1042 BIGNUM
*a
, *b
, *prime
, *order
;
1044 /* Curve prime256v1 */
1045 static const unsigned char prime_data
[] = {
1046 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1047 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1048 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1049 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1052 static const unsigned char a_data
[] = {
1053 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1054 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1055 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff,
1056 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1059 static const unsigned char b_data
[] = {
1060 0x5a, 0xc6, 0x35, 0xd8, 0xaa, 0x3a, 0x93, 0xe7,
1061 0xb3, 0xeb, 0xbd, 0x55, 0x76, 0x98, 0x86, 0xbc,
1062 0x65, 0x1d, 0x06, 0xb0, 0xcc, 0x53, 0xb0, 0xf6,
1063 0x3b, 0xce, 0x3c, 0x3e, 0x27, 0xd2, 0x60, 0x4b
1065 static const unsigned char seed
[] = {
1066 0xc4, 0x9d, 0x36, 0x08, 0x86, 0xe7, 0x04, 0x93,
1067 0x6a, 0x66, 0x78, 0xe1, 0x13, 0x9d, 0x26, 0xb7,
1068 0x81, 0x9f, 0x7e, 0x90
1070 static const unsigned char order_data
[] = {
1071 0x00, 0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00,
1072 0x00, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1073 0xff, 0xbc, 0xe6, 0xfa, 0xad, 0xa7, 0x17, 0x9e,
1074 0x84, 0xf3, 0xb9, 0xca, 0xc2, 0xfc, 0x63, 0x25, 0x51
1076 return TEST_ptr(a
= BN_CTX_get(bnctx
))
1077 && TEST_ptr(b
= BN_CTX_get(bnctx
))
1078 && TEST_ptr(prime
= BN_CTX_get(bnctx
))
1079 && TEST_ptr(order
= BN_CTX_get(bnctx
))
1080 && TEST_ptr(BN_bin2bn(prime_data
, sizeof(prime_data
), prime
))
1081 && TEST_ptr(BN_bin2bn(a_data
, sizeof(a_data
), a
))
1082 && TEST_ptr(BN_bin2bn(b_data
, sizeof(b_data
), b
))
1083 && TEST_ptr(BN_bin2bn(order_data
, sizeof(order_data
), order
))
1084 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld
,
1085 OSSL_PKEY_PARAM_EC_FIELD_TYPE
, SN_X9_62_prime_field
,
1087 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, prime
))
1088 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, a
))
1089 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, b
))
1090 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
,
1091 OSSL_PKEY_PARAM_EC_ORDER
, order
))
1092 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1093 OSSL_PKEY_PARAM_EC_GENERATOR
, gen
, gen_len
))
1094 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1095 OSSL_PKEY_PARAM_EC_SEED
, seed
, sizeof(seed
)))
1096 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1100 static int create_ec_explicit_prime_params_namedcurve(OSSL_PARAM_BLD
*bld
)
1102 static const unsigned char prime256v1_gen
[] = {
1104 0x6b, 0x17, 0xd1, 0xf2, 0xe1, 0x2c, 0x42, 0x47,
1105 0xf8, 0xbc, 0xe6, 0xe5, 0x63, 0xa4, 0x40, 0xf2,
1106 0x77, 0x03, 0x7d, 0x81, 0x2d, 0xeb, 0x33, 0xa0,
1107 0xf4, 0xa1, 0x39, 0x45, 0xd8, 0x98, 0xc2, 0x96,
1108 0x4f, 0xe3, 0x42, 0xe2, 0xfe, 0x1a, 0x7f, 0x9b,
1109 0x8e, 0xe7, 0xeb, 0x4a, 0x7c, 0x0f, 0x9e, 0x16,
1110 0x2b, 0xce, 0x33, 0x57, 0x6b, 0x31, 0x5e, 0xce,
1111 0xcb, 0xb6, 0x40, 0x68, 0x37, 0xbf, 0x51, 0xf5
1113 return do_create_ec_explicit_prime_params(bld
, prime256v1_gen
,
1114 sizeof(prime256v1_gen
));
1117 static int create_ec_explicit_prime_params(OSSL_PARAM_BLD
*bld
)
1120 static const unsigned char prime256v1_gen2
[] = {
1122 0xe4, 0x97, 0x08, 0xbe, 0x7d, 0xfa, 0xa2, 0x9a,
1123 0xa3, 0x12, 0x6f, 0xe4, 0xe7, 0xd0, 0x25, 0xe3,
1124 0x4a, 0xc1, 0x03, 0x15, 0x8c, 0xd9, 0x33, 0xc6,
1125 0x97, 0x42, 0xf5, 0xdc, 0x97, 0xb9, 0xd7, 0x31,
1126 0xe9, 0x7d, 0x74, 0x3d, 0x67, 0x6a, 0x3b, 0x21,
1127 0x08, 0x9c, 0x31, 0x73, 0xf8, 0xc1, 0x27, 0xc9,
1128 0xd2, 0xa0, 0xa0, 0x83, 0x66, 0xe0, 0xc9, 0xda,
1129 0xa8, 0xc6, 0x56, 0x2b, 0x94, 0xb1, 0xae, 0x55
1131 return do_create_ec_explicit_prime_params(bld
, prime256v1_gen2
,
1132 sizeof(prime256v1_gen2
));
1135 # ifndef OPENSSL_NO_EC2M
1136 static int do_create_ec_explicit_trinomial_params(OSSL_PARAM_BLD
*bld
,
1137 const unsigned char *gen
,
1140 BIGNUM
*a
, *b
, *poly
, *order
, *cofactor
;
1141 /* sect233k1 characteristic-two-field tpBasis */
1142 static const unsigned char poly_data
[] = {
1143 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1144 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, 0x00, 0x00,
1145 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1147 static const unsigned char a_data
[] = {
1148 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1149 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1150 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
1152 static const unsigned char b_data
[] = {
1153 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1154 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1155 0x00, 0x00, 0x00, 0x00, 0x00, 0x01
1157 static const unsigned char order_data
[] = {
1158 0x00, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1159 0x00, 0x00, 0x00, 0x06, 0x9D, 0x5B, 0xB9, 0x15, 0xBC, 0xD4, 0x6E, 0xFB,
1160 0x1A, 0xD5, 0xF1, 0x73, 0xAB, 0xDF
1162 static const unsigned char cofactor_data
[]= {
1165 return TEST_ptr(a
= BN_CTX_get(bnctx
))
1166 && TEST_ptr(b
= BN_CTX_get(bnctx
))
1167 && TEST_ptr(poly
= BN_CTX_get(bnctx
))
1168 && TEST_ptr(order
= BN_CTX_get(bnctx
))
1169 && TEST_ptr(cofactor
= BN_CTX_get(bnctx
))
1170 && TEST_ptr(BN_bin2bn(poly_data
, sizeof(poly_data
), poly
))
1171 && TEST_ptr(BN_bin2bn(a_data
, sizeof(a_data
), a
))
1172 && TEST_ptr(BN_bin2bn(b_data
, sizeof(b_data
), b
))
1173 && TEST_ptr(BN_bin2bn(order_data
, sizeof(order_data
), order
))
1174 && TEST_ptr(BN_bin2bn(cofactor_data
, sizeof(cofactor_data
), cofactor
))
1175 && TEST_true(OSSL_PARAM_BLD_push_utf8_string(bld
,
1176 OSSL_PKEY_PARAM_EC_FIELD_TYPE
,
1177 SN_X9_62_characteristic_two_field
, 0))
1178 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_P
, poly
))
1179 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_A
, a
))
1180 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_B
, b
))
1181 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
,
1182 OSSL_PKEY_PARAM_EC_ORDER
, order
))
1183 && TEST_true(OSSL_PARAM_BLD_push_octet_string(bld
,
1184 OSSL_PKEY_PARAM_EC_GENERATOR
, gen
, gen_len
))
1185 && TEST_true(OSSL_PARAM_BLD_push_BN(bld
, OSSL_PKEY_PARAM_EC_COFACTOR
,
1189 static int create_ec_explicit_trinomial_params_namedcurve(OSSL_PARAM_BLD
*bld
)
1191 static const unsigned char gen
[] = {
1193 0x01, 0x72, 0x32, 0xBA, 0x85, 0x3A, 0x7E, 0x73, 0x1A, 0xF1, 0x29, 0xF2,
1194 0x2F, 0xF4, 0x14, 0x95, 0x63, 0xA4, 0x19, 0xC2, 0x6B, 0xF5, 0x0A, 0x4C,
1195 0x9D, 0x6E, 0xEF, 0xAD, 0x61, 0x26,
1196 0x01, 0xDB, 0x53, 0x7D, 0xEC, 0xE8, 0x19, 0xB7, 0xF7, 0x0F, 0x55, 0x5A,
1197 0x67, 0xC4, 0x27, 0xA8, 0xCD, 0x9B, 0xF1, 0x8A, 0xEB, 0x9B, 0x56, 0xE0,
1198 0xC1, 0x10, 0x56, 0xFA, 0xE6, 0xA3
1200 return do_create_ec_explicit_trinomial_params(bld
, gen
, sizeof(gen
));
1203 static int create_ec_explicit_trinomial_params(OSSL_PARAM_BLD
*bld
)
1205 static const unsigned char gen2
[] = {
1207 0x00, 0xd7, 0xba, 0xd0, 0x26, 0x6c, 0x31, 0x6a, 0x78, 0x76, 0x01, 0xd1,
1208 0x32, 0x4b, 0x8f, 0x30, 0x29, 0x2d, 0x78, 0x30, 0xca, 0x43, 0xaa, 0xf0,
1209 0xa2, 0x5a, 0xd4, 0x0f, 0xb3, 0xf4,
1210 0x00, 0x85, 0x4b, 0x1b, 0x8d, 0x50, 0x10, 0xa5, 0x1c, 0x80, 0xf7, 0x86,
1211 0x40, 0x62, 0x4c, 0x87, 0xd1, 0x26, 0x7a, 0x9c, 0x5c, 0xe9, 0x82, 0x29,
1212 0xd1, 0x67, 0x70, 0x41, 0xea, 0xcb
1214 return do_create_ec_explicit_trinomial_params(bld
, gen2
, sizeof(gen2
));
1216 # endif /* OPENSSL_NO_EC2M */
1217 #endif /* OPENSSL_NO_EC */
1219 typedef enum OPTION_choice
{
1230 const OPTIONS
*test_get_options(void)
1232 static const OPTIONS options
[] = {
1233 OPT_TEST_OPTIONS_DEFAULT_USAGE
,
1234 { "context", OPT_CONTEXT
, '-',
1235 "Explicitly use a non-default library context" },
1236 { "rsa", OPT_RSA_FILE
, '<',
1237 "PEM format RSA key file to encode/decode" },
1238 { "pss", OPT_RSA_PSS_FILE
, '<',
1239 "PEM format RSA-PSS key file to encode/decode" },
1240 { "config", OPT_CONFIG_FILE
, '<',
1241 "The configuration file to use for the library context" },
1242 { "provider", OPT_PROVIDER_NAME
, 's',
1243 "The provider to load (The default value is 'default')" },
1249 int setup_tests(void)
1251 const char *rsa_file
= NULL
;
1252 const char *rsa_pss_file
= NULL
;
1253 const char *prov_name
= "default";
1254 char *config_file
= NULL
;
1257 #ifndef OPENSSL_NO_DSA
1258 static size_t qbits
= 160; /* PVK only tolerates 160 Q bits */
1259 static size_t pbits
= 1024; /* With 160 Q bits, we MUST use 1024 P bits */
1260 OSSL_PARAM DSA_params
[] = {
1261 OSSL_PARAM_size_t("pbits", &pbits
),
1262 OSSL_PARAM_size_t("qbits", &qbits
),
1267 #ifndef OPENSSL_NO_EC
1268 static char groupname
[] = "prime256v1";
1269 OSSL_PARAM EC_params
[] = {
1270 OSSL_PARAM_utf8_string("group", groupname
, sizeof(groupname
) - 1),
1277 while ((o
= opt_next()) != OPT_EOF
) {
1282 case OPT_PROVIDER_NAME
:
1283 prov_name
= opt_arg();
1285 case OPT_CONFIG_FILE
:
1286 config_file
= opt_arg();
1289 rsa_file
= opt_arg();
1291 case OPT_RSA_PSS_FILE
:
1292 rsa_pss_file
= opt_arg();
1294 case OPT_TEST_CASES
:
1301 if (strcmp(prov_name
, "fips") == 0)
1304 if (default_libctx
) {
1305 if (!test_get_libctx(NULL
, NULL
, config_file
, &deflprov
, prov_name
))
1308 if (!test_get_libctx(&testctx
, &nullprov
, config_file
, &deflprov
, prov_name
))
1312 #ifdef STATIC_LEGACY
1314 * This test is always statically linked against libcrypto. We must not
1315 * attempt to load legacy.so that might be dynamically linked against
1316 * libcrypto. Instead we use a built-in version of the legacy provider.
1318 if (!OSSL_PROVIDER_add_builtin(testctx
, "legacy", ossl_legacy_provider_init
))
1322 /* Separate provider/ctx for generating the test data */
1323 if (!TEST_ptr(keyctx
= OSSL_LIB_CTX_new()))
1325 if (!TEST_ptr(keyprov
= OSSL_PROVIDER_load(keyctx
, "default")))
1328 #ifndef OPENSSL_NO_EC
1329 if (!TEST_ptr(bnctx
= BN_CTX_new_ex(testctx
))
1330 || !TEST_ptr(bld_prime_nc
= OSSL_PARAM_BLD_new())
1331 || !TEST_ptr(bld_prime
= OSSL_PARAM_BLD_new())
1332 || !create_ec_explicit_prime_params_namedcurve(bld_prime_nc
)
1333 || !create_ec_explicit_prime_params(bld_prime
)
1334 || !TEST_ptr(ec_explicit_prime_params_nc
= OSSL_PARAM_BLD_to_param(bld_prime_nc
))
1335 || !TEST_ptr(ec_explicit_prime_params_explicit
= OSSL_PARAM_BLD_to_param(bld_prime
))
1336 # ifndef OPENSSL_NO_EC2M
1337 || !TEST_ptr(bld_tri_nc
= OSSL_PARAM_BLD_new())
1338 || !TEST_ptr(bld_tri
= OSSL_PARAM_BLD_new())
1339 || !create_ec_explicit_trinomial_params_namedcurve(bld_tri_nc
)
1340 || !create_ec_explicit_trinomial_params(bld_tri
)
1341 || !TEST_ptr(ec_explicit_tri_params_nc
= OSSL_PARAM_BLD_to_param(bld_tri_nc
))
1342 || !TEST_ptr(ec_explicit_tri_params_explicit
= OSSL_PARAM_BLD_to_param(bld_tri
))
1348 TEST_info("Generating keys...");
1350 #ifndef OPENSSL_NO_DH
1351 TEST_info("Generating DH keys...");
1352 MAKE_DOMAIN_KEYS(DH
, "DH", NULL
);
1353 MAKE_DOMAIN_KEYS(DHX
, "X9.42 DH", NULL
);
1355 #ifndef OPENSSL_NO_DSA
1356 TEST_info("Generating DSA keys...");
1357 MAKE_DOMAIN_KEYS(DSA
, "DSA", DSA_params
);
1359 #ifndef OPENSSL_NO_EC
1360 TEST_info("Generating EC keys...");
1361 MAKE_DOMAIN_KEYS(EC
, "EC", EC_params
);
1362 MAKE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve
, "EC", ec_explicit_prime_params_nc
);
1363 MAKE_DOMAIN_KEYS(ECExplicitPrime2G
, "EC", ec_explicit_prime_params_explicit
);
1364 # ifndef OPENSSL_NO_EC2M
1365 MAKE_DOMAIN_KEYS(ECExplicitTriNamedCurve
, "EC", ec_explicit_tri_params_nc
);
1366 MAKE_DOMAIN_KEYS(ECExplicitTri2G
, "EC", ec_explicit_tri_params_explicit
);
1368 MAKE_KEYS(ED25519
, "ED25519", NULL
);
1369 MAKE_KEYS(ED448
, "ED448", NULL
);
1370 MAKE_KEYS(X25519
, "X25519", NULL
);
1371 MAKE_KEYS(X448
, "X448", NULL
);
1373 TEST_info("Loading RSA key...");
1374 ok
= ok
&& TEST_ptr(key_RSA
= load_pkey_pem(rsa_file
, keyctx
));
1375 TEST_info("Loading RSA_PSS key...");
1376 ok
= ok
&& TEST_ptr(key_RSA_PSS
= load_pkey_pem(rsa_pss_file
, keyctx
));
1377 TEST_info("Generating keys done");
1380 #ifndef OPENSSL_NO_DH
1382 ADD_TEST_SUITE_PARAMS(DH
);
1383 ADD_TEST_SUITE(DHX
);
1384 ADD_TEST_SUITE_PARAMS(DHX
);
1386 * DH has no support for PEM_write_bio_PrivateKey_traditional(),
1387 * so no legacy tests.
1390 #ifndef OPENSSL_NO_DSA
1391 ADD_TEST_SUITE(DSA
);
1392 ADD_TEST_SUITE_PARAMS(DSA
);
1393 ADD_TEST_SUITE_LEGACY(DSA
);
1394 ADD_TEST_SUITE_MSBLOB(DSA
);
1395 ADD_TEST_SUITE_UNPROTECTED_PVK(DSA
);
1396 # ifndef OPENSSL_NO_RC4
1397 ADD_TEST_SUITE_PROTECTED_PVK(DSA
);
1400 #ifndef OPENSSL_NO_EC
1402 ADD_TEST_SUITE_PARAMS(EC
);
1403 ADD_TEST_SUITE_LEGACY(EC
);
1404 ADD_TEST_SUITE(ECExplicitPrimeNamedCurve
);
1405 ADD_TEST_SUITE_LEGACY(ECExplicitPrimeNamedCurve
);
1406 ADD_TEST_SUITE(ECExplicitPrime2G
);
1407 ADD_TEST_SUITE_LEGACY(ECExplicitPrime2G
);
1408 # ifndef OPENSSL_NO_EC2M
1409 ADD_TEST_SUITE(ECExplicitTriNamedCurve
);
1410 ADD_TEST_SUITE_LEGACY(ECExplicitTriNamedCurve
);
1411 ADD_TEST_SUITE(ECExplicitTri2G
);
1412 ADD_TEST_SUITE_LEGACY(ECExplicitTri2G
);
1414 ADD_TEST_SUITE(ED25519
);
1415 ADD_TEST_SUITE(ED448
);
1416 ADD_TEST_SUITE(X25519
);
1417 ADD_TEST_SUITE(X448
);
1419 * ED25519, ED448, X25519 and X448 have no support for
1420 * PEM_write_bio_PrivateKey_traditional(), so no legacy tests.
1423 ADD_TEST_SUITE(RSA
);
1424 ADD_TEST_SUITE_LEGACY(RSA
);
1425 ADD_TEST_SUITE(RSA_PSS
);
1427 * RSA-PSS has no support for PEM_write_bio_PrivateKey_traditional(),
1428 * so no legacy tests.
1430 ADD_TEST_SUITE_MSBLOB(RSA
);
1431 ADD_TEST_SUITE_UNPROTECTED_PVK(RSA
);
1432 # ifndef OPENSSL_NO_RC4
1433 ADD_TEST_SUITE_PROTECTED_PVK(RSA
);
1440 void cleanup_tests(void)
1442 #ifndef OPENSSL_NO_EC
1443 OSSL_PARAM_free(ec_explicit_prime_params_nc
);
1444 OSSL_PARAM_free(ec_explicit_prime_params_explicit
);
1445 OSSL_PARAM_BLD_free(bld_prime_nc
);
1446 OSSL_PARAM_BLD_free(bld_prime
);
1447 # ifndef OPENSSL_NO_EC2M
1448 OSSL_PARAM_free(ec_explicit_tri_params_nc
);
1449 OSSL_PARAM_free(ec_explicit_tri_params_explicit
);
1450 OSSL_PARAM_BLD_free(bld_tri_nc
);
1451 OSSL_PARAM_BLD_free(bld_tri
);
1454 #endif /* OPENSSL_NO_EC */
1456 #ifndef OPENSSL_NO_DH
1457 FREE_DOMAIN_KEYS(DH
);
1458 FREE_DOMAIN_KEYS(DHX
);
1460 #ifndef OPENSSL_NO_DSA
1461 FREE_DOMAIN_KEYS(DSA
);
1463 #ifndef OPENSSL_NO_EC
1464 FREE_DOMAIN_KEYS(EC
);
1465 FREE_DOMAIN_KEYS(ECExplicitPrimeNamedCurve
);
1466 FREE_DOMAIN_KEYS(ECExplicitPrime2G
);
1467 # ifndef OPENSSL_NO_EC2M
1468 FREE_DOMAIN_KEYS(ECExplicitTriNamedCurve
);
1469 FREE_DOMAIN_KEYS(ECExplicitTri2G
);
1479 OSSL_PROVIDER_unload(nullprov
);
1480 OSSL_PROVIDER_unload(deflprov
);
1481 OSSL_PROVIDER_unload(keyprov
);
1482 OSSL_LIB_CTX_free(testctx
);
1483 OSSL_LIB_CTX_free(keyctx
);