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