xref: /netbsd-src/crypto/external/bsd/openssl/dist/test/endecoder_legacy_test.c (revision b0d1725196a7921d003d2c66a14f186abda4176b)
1*b0d17251Schristos /*
2*b0d17251Schristos  * Copyright 2020-2021 The OpenSSL Project Authors. All Rights Reserved.
3*b0d17251Schristos  *
4*b0d17251Schristos  * Licensed under the Apache License 2.0 (the "License").  You may not use
5*b0d17251Schristos  * this file except in compliance with the License.  You can obtain a copy
6*b0d17251Schristos  * in the file LICENSE in the source distribution or at
7*b0d17251Schristos  * https://www.openssl.org/source/license.html
8*b0d17251Schristos  */
9*b0d17251Schristos 
10*b0d17251Schristos /*
11*b0d17251Schristos  * This program tests the following known key type specific function against
12*b0d17251Schristos  * the corresponding OSSL_ENCODER implementation:
13*b0d17251Schristos  *
14*b0d17251Schristos  * - i2d_{TYPE}PrivateKey()
15*b0d17251Schristos  * - i2d_{TYPE}PublicKey(),
16*b0d17251Schristos  * - i2d_{TYPE}params(),
17*b0d17251Schristos  * - i2d_{TYPE}_PUBKEY(),
18*b0d17251Schristos  * - PEM_write_bio_{TYPE}PrivateKey()
19*b0d17251Schristos  * - PEM_write_bio_{TYPE}PublicKey()
20*b0d17251Schristos  * - PEM_write_bio_{TYPE}params()
21*b0d17251Schristos  * - PEM_write_bio_{TYPE}_PUBKEY()
22*b0d17251Schristos  *
23*b0d17251Schristos  * as well as the following functions against the corresponding OSSL_DECODER
24*b0d17251Schristos  * implementation.
25*b0d17251Schristos  *
26*b0d17251Schristos  * - d2i_{TYPE}PrivateKey()
27*b0d17251Schristos  * - d2i_{TYPE}PublicKey(),
28*b0d17251Schristos  * - d2i_{TYPE}params(),
29*b0d17251Schristos  * - d2i_{TYPE}_PUBKEY(),
30*b0d17251Schristos  * - PEM_read_bio_{TYPE}PrivateKey()
31*b0d17251Schristos  * - PEM_read_bio_{TYPE}PublicKey()
32*b0d17251Schristos  * - PEM_read_bio_{TYPE}params()
33*b0d17251Schristos  * - PEM_read_bio_{TYPE}_PUBKEY()
34*b0d17251Schristos  */
35*b0d17251Schristos 
36*b0d17251Schristos #include <stdlib.h>
37*b0d17251Schristos #include <string.h>
38*b0d17251Schristos 
39*b0d17251Schristos /*
40*b0d17251Schristos  * We test deprecated functions, so we need to suppress deprecation warnings.
41*b0d17251Schristos  */
42*b0d17251Schristos #define OPENSSL_SUPPRESS_DEPRECATED
43*b0d17251Schristos 
44*b0d17251Schristos #include <openssl/bio.h>
45*b0d17251Schristos #include <openssl/evp.h>
46*b0d17251Schristos #include <openssl/asn1.h>
47*b0d17251Schristos #include <openssl/pem.h>
48*b0d17251Schristos #include <openssl/params.h>
49*b0d17251Schristos #include <openssl/encoder.h>
50*b0d17251Schristos #include <openssl/decoder.h>
51*b0d17251Schristos #include <openssl/dh.h>
52*b0d17251Schristos #include <openssl/dsa.h>
53*b0d17251Schristos #ifndef OPENSSL_NO_DEPRECATED_3_0
54*b0d17251Schristos # include <openssl/rsa.h>
55*b0d17251Schristos #endif
56*b0d17251Schristos #include "internal/nelem.h"
57*b0d17251Schristos #include "crypto/evp.h"
58*b0d17251Schristos 
59*b0d17251Schristos #include "testutil.h"
60*b0d17251Schristos 
61*b0d17251Schristos typedef int PEM_write_bio_of_void_protected(BIO *out, const void *obj,
62*b0d17251Schristos                                             const EVP_CIPHER *enc,
63*b0d17251Schristos                                             unsigned char *kstr, int klen,
64*b0d17251Schristos                                             pem_password_cb *cb, void *u);
65*b0d17251Schristos typedef int PEM_write_bio_of_void_unprotected(BIO *out, const void *obj);
66*b0d17251Schristos typedef void *PEM_read_bio_of_void(BIO *out, void **obj,
67*b0d17251Schristos                                    pem_password_cb *cb, void *u);
68*b0d17251Schristos typedef int EVP_PKEY_print_fn(BIO *out, const EVP_PKEY *pkey,
69*b0d17251Schristos                               int indent, ASN1_PCTX *pctx);
70*b0d17251Schristos typedef int EVP_PKEY_eq_fn(const EVP_PKEY *a, const EVP_PKEY *b);
71*b0d17251Schristos 
72*b0d17251Schristos static struct test_stanza_st {
73*b0d17251Schristos     const char *keytype;
74*b0d17251Schristos     const char *structure[2];
75*b0d17251Schristos     int evp_type;
76*b0d17251Schristos 
77*b0d17251Schristos     i2d_of_void *i2d_PrivateKey;
78*b0d17251Schristos     i2d_of_void *i2d_PublicKey;
79*b0d17251Schristos     i2d_of_void *i2d_params;
80*b0d17251Schristos     i2d_of_void *i2d_PUBKEY;
81*b0d17251Schristos     PEM_write_bio_of_void_protected *pem_write_bio_PrivateKey;
82*b0d17251Schristos     PEM_write_bio_of_void_unprotected *pem_write_bio_PublicKey;
83*b0d17251Schristos     PEM_write_bio_of_void_unprotected *pem_write_bio_params;
84*b0d17251Schristos     PEM_write_bio_of_void_unprotected *pem_write_bio_PUBKEY;
85*b0d17251Schristos 
86*b0d17251Schristos     d2i_of_void *d2i_PrivateKey;
87*b0d17251Schristos     d2i_of_void *d2i_PublicKey;
88*b0d17251Schristos     d2i_of_void *d2i_params;
89*b0d17251Schristos     d2i_of_void *d2i_PUBKEY;
90*b0d17251Schristos     PEM_read_bio_of_void *pem_read_bio_PrivateKey;
91*b0d17251Schristos     PEM_read_bio_of_void *pem_read_bio_PublicKey;
92*b0d17251Schristos     PEM_read_bio_of_void *pem_read_bio_params;
93*b0d17251Schristos     PEM_read_bio_of_void *pem_read_bio_PUBKEY;
94*b0d17251Schristos } test_stanzas[] = {
95*b0d17251Schristos #ifndef OPENSSL_NO_DH
96*b0d17251Schristos     { "DH", { "DH", "type-specific" }, EVP_PKEY_DH,
97*b0d17251Schristos       NULL,                      /* No i2d_DHPrivateKey */
98*b0d17251Schristos       NULL,                      /* No i2d_DHPublicKey */
99*b0d17251Schristos       (i2d_of_void *)i2d_DHparams,
100*b0d17251Schristos       NULL,                      /* No i2d_DH_PUBKEY */
101*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DHPrivateKey */
102*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DHPublicKey */
103*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHparams,
104*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DH_PUBKEY */
105*b0d17251Schristos       NULL,                      /* No d2i_DHPrivateKey */
106*b0d17251Schristos       NULL,                      /* No d2i_DHPublicKey */
107*b0d17251Schristos       (d2i_of_void *)d2i_DHparams,
108*b0d17251Schristos       NULL,                      /* No d2i_DH_PUBKEY */
109*b0d17251Schristos       NULL,                      /* No PEM_read_bio_DHPrivateKey */
110*b0d17251Schristos       NULL,                      /* No PEM_read_bio_DHPublicKey */
111*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_DHparams,
112*b0d17251Schristos       NULL },                    /* No PEM_read_bio_DH_PUBKEY */
113*b0d17251Schristos     { "DHX", { "DHX", "type-specific" }, EVP_PKEY_DHX,
114*b0d17251Schristos       NULL,                      /* No i2d_DHxPrivateKey */
115*b0d17251Schristos       NULL,                      /* No i2d_DHxPublicKey */
116*b0d17251Schristos       (i2d_of_void *)i2d_DHxparams,
117*b0d17251Schristos       NULL,                      /* No i2d_DHx_PUBKEY */
118*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DHxPrivateKey */
119*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DHxPublicKey */
120*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DHxparams,
121*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DHx_PUBKEY */
122*b0d17251Schristos       NULL,                      /* No d2i_DHxPrivateKey */
123*b0d17251Schristos       NULL,                      /* No d2i_DHxPublicKey */
124*b0d17251Schristos       (d2i_of_void *)d2i_DHxparams,
125*b0d17251Schristos       NULL,                      /* No d2i_DHx_PUBKEY */
126*b0d17251Schristos       NULL,                      /* No PEM_read_bio_DHxPrivateKey */
127*b0d17251Schristos       NULL,                      /* No PEM_read_bio_DHxPublicKey */
128*b0d17251Schristos       NULL,                      /* No PEM_read_bio_DHxparams */
129*b0d17251Schristos       NULL },                    /* No PEM_read_bio_DHx_PUBKEY */
130*b0d17251Schristos #endif
131*b0d17251Schristos #ifndef OPENSSL_NO_DSA
132*b0d17251Schristos     { "DSA", { "DSA", "type-specific" }, EVP_PKEY_DSA,
133*b0d17251Schristos       (i2d_of_void *)i2d_DSAPrivateKey,
134*b0d17251Schristos       (i2d_of_void *)i2d_DSAPublicKey,
135*b0d17251Schristos       (i2d_of_void *)i2d_DSAparams,
136*b0d17251Schristos       (i2d_of_void *)i2d_DSA_PUBKEY,
137*b0d17251Schristos       (PEM_write_bio_of_void_protected *)PEM_write_bio_DSAPrivateKey,
138*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DSAPublicKey */
139*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSAparams,
140*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_DSA_PUBKEY,
141*b0d17251Schristos       (d2i_of_void *)d2i_DSAPrivateKey,
142*b0d17251Schristos       (d2i_of_void *)d2i_DSAPublicKey,
143*b0d17251Schristos       (d2i_of_void *)d2i_DSAparams,
144*b0d17251Schristos       (d2i_of_void *)d2i_DSA_PUBKEY,
145*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_DSAPrivateKey,
146*b0d17251Schristos       NULL,                      /* No PEM_write_bio_DSAPublicKey */
147*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_DSAparams,
148*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_DSA_PUBKEY },
149*b0d17251Schristos #endif
150*b0d17251Schristos #ifndef OPENSSL_NO_EC
151*b0d17251Schristos     { "EC", { "EC", "type-specific" }, EVP_PKEY_EC,
152*b0d17251Schristos       (i2d_of_void *)i2d_ECPrivateKey,
153*b0d17251Schristos       NULL,                      /* No i2d_ECPublicKey */
154*b0d17251Schristos       (i2d_of_void *)i2d_ECParameters,
155*b0d17251Schristos       (i2d_of_void *)i2d_EC_PUBKEY,
156*b0d17251Schristos       (PEM_write_bio_of_void_protected *)PEM_write_bio_ECPrivateKey,
157*b0d17251Schristos       NULL,                      /* No PEM_write_bio_ECPublicKey */
158*b0d17251Schristos       NULL,                      /* No PEM_write_bio_ECParameters */
159*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_EC_PUBKEY,
160*b0d17251Schristos       (d2i_of_void *)d2i_ECPrivateKey,
161*b0d17251Schristos       NULL,                      /* No d2i_ECPublicKey */
162*b0d17251Schristos       (d2i_of_void *)d2i_ECParameters,
163*b0d17251Schristos       (d2i_of_void *)d2i_EC_PUBKEY,
164*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_ECPrivateKey,
165*b0d17251Schristos       NULL,                      /* No PEM_read_bio_ECPublicKey */
166*b0d17251Schristos       NULL,                      /* No PEM_read_bio_ECParameters */
167*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_EC_PUBKEY, },
168*b0d17251Schristos #endif
169*b0d17251Schristos     { "RSA", { "RSA", "type-specific" }, EVP_PKEY_RSA,
170*b0d17251Schristos       (i2d_of_void *)i2d_RSAPrivateKey,
171*b0d17251Schristos       (i2d_of_void *)i2d_RSAPublicKey,
172*b0d17251Schristos       NULL,                      /* No i2d_RSAparams */
173*b0d17251Schristos       (i2d_of_void *)i2d_RSA_PUBKEY,
174*b0d17251Schristos       (PEM_write_bio_of_void_protected *)PEM_write_bio_RSAPrivateKey,
175*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSAPublicKey,
176*b0d17251Schristos       NULL,                      /* No PEM_write_bio_RSAparams */
177*b0d17251Schristos       (PEM_write_bio_of_void_unprotected *)PEM_write_bio_RSA_PUBKEY,
178*b0d17251Schristos       (d2i_of_void *)d2i_RSAPrivateKey,
179*b0d17251Schristos       (d2i_of_void *)d2i_RSAPublicKey,
180*b0d17251Schristos       NULL,                      /* No d2i_RSAparams */
181*b0d17251Schristos       (d2i_of_void *)d2i_RSA_PUBKEY,
182*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_RSAPrivateKey,
183*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_RSAPublicKey,
184*b0d17251Schristos       NULL,                      /* No PEM_read_bio_RSAparams */
185*b0d17251Schristos       (PEM_read_bio_of_void *)PEM_read_bio_RSA_PUBKEY }
186*b0d17251Schristos };
187*b0d17251Schristos 
188*b0d17251Schristos /*
189*b0d17251Schristos  * Keys that we're going to test with.  We initialize this with the intended
190*b0d17251Schristos  * key types, and generate the keys themselves on program setup.
191*b0d17251Schristos  * They must all be downgradable with EVP_PKEY_get0()
192*b0d17251Schristos  */
193*b0d17251Schristos 
194*b0d17251Schristos #ifndef OPENSSL_NO_DH
195*b0d17251Schristos static const OSSL_PARAM DH_params[] = { OSSL_PARAM_END };
196*b0d17251Schristos static const OSSL_PARAM DHX_params[] = { OSSL_PARAM_END };
197*b0d17251Schristos #endif
198*b0d17251Schristos #ifndef OPENSSL_NO_DSA
199*b0d17251Schristos static size_t qbits = 160;  /* PVK only tolerates 160 Q bits */
200*b0d17251Schristos static size_t pbits = 1024; /* With 160 Q bits, we MUST use 1024 P bits */
201*b0d17251Schristos static const OSSL_PARAM DSA_params[] = {
202*b0d17251Schristos     OSSL_PARAM_size_t("pbits", &pbits),
203*b0d17251Schristos     OSSL_PARAM_size_t("qbits", &qbits),
204*b0d17251Schristos     OSSL_PARAM_END
205*b0d17251Schristos };
206*b0d17251Schristos #endif
207*b0d17251Schristos #ifndef OPENSSL_NO_EC
208*b0d17251Schristos static char groupname[] = "prime256v1";
209*b0d17251Schristos static const OSSL_PARAM EC_params[] = {
210*b0d17251Schristos     OSSL_PARAM_utf8_string("group", groupname, sizeof(groupname) - 1),
211*b0d17251Schristos     OSSL_PARAM_END
212*b0d17251Schristos };
213*b0d17251Schristos #endif
214*b0d17251Schristos 
215*b0d17251Schristos static struct key_st {
216*b0d17251Schristos     const char *keytype;
217*b0d17251Schristos     int evp_type;
218*b0d17251Schristos     /* non-NULL if a template EVP_PKEY must be generated first */
219*b0d17251Schristos     const OSSL_PARAM *template_params;
220*b0d17251Schristos 
221*b0d17251Schristos     EVP_PKEY *key;
222*b0d17251Schristos } keys[] = {
223*b0d17251Schristos #ifndef OPENSSL_NO_DH
224*b0d17251Schristos     { "DH", EVP_PKEY_DH, DH_params, NULL },
225*b0d17251Schristos     { "DHX", EVP_PKEY_DHX, DHX_params, NULL },
226*b0d17251Schristos #endif
227*b0d17251Schristos #ifndef OPENSSL_NO_DSA
228*b0d17251Schristos     { "DSA", EVP_PKEY_DSA, DSA_params, NULL },
229*b0d17251Schristos #endif
230*b0d17251Schristos #ifndef OPENSSL_NO_EC
231*b0d17251Schristos     { "EC", EVP_PKEY_EC, EC_params, NULL },
232*b0d17251Schristos #endif
233*b0d17251Schristos #ifndef OPENSSL_NO_DEPRECATED_3_0
234*b0d17251Schristos     { "RSA", EVP_PKEY_RSA, NULL, NULL },
235*b0d17251Schristos #endif
236*b0d17251Schristos };
237*b0d17251Schristos 
make_key(const char * type,const OSSL_PARAM * gen_template_params)238*b0d17251Schristos static EVP_PKEY *make_key(const char *type,
239*b0d17251Schristos                           const OSSL_PARAM *gen_template_params)
240*b0d17251Schristos {
241*b0d17251Schristos     EVP_PKEY *template = NULL;
242*b0d17251Schristos     EVP_PKEY *pkey = NULL;
243*b0d17251Schristos     EVP_PKEY_CTX *ctx = NULL;
244*b0d17251Schristos     OSSL_PARAM *gen_template_params_noconst =
245*b0d17251Schristos         (OSSL_PARAM *)gen_template_params;
246*b0d17251Schristos 
247*b0d17251Schristos     if (gen_template_params != NULL
248*b0d17251Schristos         && ((ctx = EVP_PKEY_CTX_new_from_name(NULL, type, NULL)) == NULL
249*b0d17251Schristos             || EVP_PKEY_paramgen_init(ctx) <= 0
250*b0d17251Schristos             || (gen_template_params[0].key != NULL
251*b0d17251Schristos                 && EVP_PKEY_CTX_set_params(ctx, gen_template_params_noconst) <= 0)
252*b0d17251Schristos             || EVP_PKEY_generate(ctx, &template) <= 0))
253*b0d17251Schristos         goto end;
254*b0d17251Schristos     EVP_PKEY_CTX_free(ctx);
255*b0d17251Schristos 
256*b0d17251Schristos     /*
257*b0d17251Schristos      * No real need to check the errors other than for the cascade
258*b0d17251Schristos      * effect.  |pkey| will simply remain NULL if something goes wrong.
259*b0d17251Schristos      */
260*b0d17251Schristos     ctx =
261*b0d17251Schristos         template != NULL
262*b0d17251Schristos         ? EVP_PKEY_CTX_new(template, NULL)
263*b0d17251Schristos         : EVP_PKEY_CTX_new_from_name(NULL, type, NULL);
264*b0d17251Schristos 
265*b0d17251Schristos     (void)(ctx != NULL
266*b0d17251Schristos            && EVP_PKEY_keygen_init(ctx) > 0
267*b0d17251Schristos            && EVP_PKEY_keygen(ctx, &pkey) > 0);
268*b0d17251Schristos 
269*b0d17251Schristos  end:
270*b0d17251Schristos     EVP_PKEY_free(template);
271*b0d17251Schristos     EVP_PKEY_CTX_free(ctx);
272*b0d17251Schristos     return pkey;
273*b0d17251Schristos }
274*b0d17251Schristos 
lookup_key(const char * type)275*b0d17251Schristos static struct key_st *lookup_key(const char *type)
276*b0d17251Schristos {
277*b0d17251Schristos     size_t i;
278*b0d17251Schristos 
279*b0d17251Schristos     for (i = 0; i < OSSL_NELEM(keys); i++) {
280*b0d17251Schristos         if (strcmp(keys[i].keytype, type) == 0)
281*b0d17251Schristos             return &keys[i];
282*b0d17251Schristos     }
283*b0d17251Schristos     return NULL;
284*b0d17251Schristos }
285*b0d17251Schristos 
test_membio_str_eq(BIO * bio_provided,BIO * bio_legacy)286*b0d17251Schristos static int test_membio_str_eq(BIO *bio_provided, BIO *bio_legacy)
287*b0d17251Schristos {
288*b0d17251Schristos     char *str_provided = NULL, *str_legacy = NULL;
289*b0d17251Schristos     long len_provided = BIO_get_mem_data(bio_provided, &str_provided);
290*b0d17251Schristos     long len_legacy = BIO_get_mem_data(bio_legacy, &str_legacy);
291*b0d17251Schristos 
292*b0d17251Schristos     return TEST_long_ge(len_legacy, 0)
293*b0d17251Schristos            && TEST_long_ge(len_provided, 0)
294*b0d17251Schristos            && TEST_strn2_eq(str_provided, len_provided,
295*b0d17251Schristos                             str_legacy, len_legacy);
296*b0d17251Schristos }
297*b0d17251Schristos 
test_protected_PEM(const char * keytype,int evp_type,const void * legacy_key,PEM_write_bio_of_void_protected * pem_write_bio,PEM_read_bio_of_void * pem_read_bio,EVP_PKEY_eq_fn * evp_pkey_eq,EVP_PKEY_print_fn * evp_pkey_print,EVP_PKEY * provided_pkey,int selection,const char * structure)298*b0d17251Schristos static int test_protected_PEM(const char *keytype, int evp_type,
299*b0d17251Schristos                               const void *legacy_key,
300*b0d17251Schristos                               PEM_write_bio_of_void_protected *pem_write_bio,
301*b0d17251Schristos                               PEM_read_bio_of_void *pem_read_bio,
302*b0d17251Schristos                               EVP_PKEY_eq_fn *evp_pkey_eq,
303*b0d17251Schristos                               EVP_PKEY_print_fn *evp_pkey_print,
304*b0d17251Schristos                               EVP_PKEY *provided_pkey, int selection,
305*b0d17251Schristos                               const char *structure)
306*b0d17251Schristos {
307*b0d17251Schristos     int ok = 0;
308*b0d17251Schristos     BIO *membio_legacy = NULL;
309*b0d17251Schristos     BIO *membio_provided = NULL;
310*b0d17251Schristos     OSSL_ENCODER_CTX *ectx = NULL;
311*b0d17251Schristos     OSSL_DECODER_CTX *dctx = NULL;
312*b0d17251Schristos     void *decoded_legacy_key = NULL;
313*b0d17251Schristos     EVP_PKEY *decoded_legacy_pkey = NULL;
314*b0d17251Schristos     EVP_PKEY *decoded_provided_pkey = NULL;
315*b0d17251Schristos 
316*b0d17251Schristos     /* Set up the BIOs, so we have them */
317*b0d17251Schristos     if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
318*b0d17251Schristos         || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
319*b0d17251Schristos         goto end;
320*b0d17251Schristos 
321*b0d17251Schristos     if (!TEST_ptr(ectx =
322*b0d17251Schristos                   OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
323*b0d17251Schristos                                                 "PEM", structure,
324*b0d17251Schristos                                                 NULL))
325*b0d17251Schristos         || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
326*b0d17251Schristos         || !TEST_true(pem_write_bio(membio_legacy, legacy_key,
327*b0d17251Schristos                                    NULL, NULL, 0, NULL, NULL))
328*b0d17251Schristos         || !test_membio_str_eq(membio_provided, membio_legacy))
329*b0d17251Schristos         goto end;
330*b0d17251Schristos 
331*b0d17251Schristos     if (pem_read_bio != NULL) {
332*b0d17251Schristos         /* Now try decoding the results and compare the resulting keys */
333*b0d17251Schristos 
334*b0d17251Schristos         if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
335*b0d17251Schristos             || !TEST_ptr(dctx =
336*b0d17251Schristos                          OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
337*b0d17251Schristos                                                        "PEM", structure,
338*b0d17251Schristos                                                        keytype, selection,
339*b0d17251Schristos                                                        NULL, NULL))
340*b0d17251Schristos             || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
341*b0d17251Schristos             || !TEST_ptr(decoded_legacy_key =
342*b0d17251Schristos                          pem_read_bio(membio_legacy, NULL, NULL, NULL))
343*b0d17251Schristos             || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
344*b0d17251Schristos                                           decoded_legacy_key)))
345*b0d17251Schristos             goto end;
346*b0d17251Schristos 
347*b0d17251Schristos         if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
348*b0d17251Schristos                                      decoded_legacy_pkey), 0)) {
349*b0d17251Schristos             TEST_info("decoded_provided_pkey:");
350*b0d17251Schristos             evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
351*b0d17251Schristos             TEST_info("decoded_legacy_pkey:");
352*b0d17251Schristos             evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
353*b0d17251Schristos         }
354*b0d17251Schristos     }
355*b0d17251Schristos     ok = 1;
356*b0d17251Schristos  end:
357*b0d17251Schristos     EVP_PKEY_free(decoded_legacy_pkey);
358*b0d17251Schristos     EVP_PKEY_free(decoded_provided_pkey);
359*b0d17251Schristos     OSSL_ENCODER_CTX_free(ectx);
360*b0d17251Schristos     OSSL_DECODER_CTX_free(dctx);
361*b0d17251Schristos     BIO_free(membio_provided);
362*b0d17251Schristos     BIO_free(membio_legacy);
363*b0d17251Schristos     return ok;
364*b0d17251Schristos }
365*b0d17251Schristos 
test_unprotected_PEM(const char * keytype,int evp_type,const void * legacy_key,PEM_write_bio_of_void_unprotected * pem_write_bio,PEM_read_bio_of_void * pem_read_bio,EVP_PKEY_eq_fn * evp_pkey_eq,EVP_PKEY_print_fn * evp_pkey_print,EVP_PKEY * provided_pkey,int selection,const char * structure)366*b0d17251Schristos static int test_unprotected_PEM(const char *keytype, int evp_type,
367*b0d17251Schristos                                 const void *legacy_key,
368*b0d17251Schristos                                 PEM_write_bio_of_void_unprotected *pem_write_bio,
369*b0d17251Schristos                                 PEM_read_bio_of_void *pem_read_bio,
370*b0d17251Schristos                                 EVP_PKEY_eq_fn *evp_pkey_eq,
371*b0d17251Schristos                                 EVP_PKEY_print_fn *evp_pkey_print,
372*b0d17251Schristos                                 EVP_PKEY *provided_pkey, int selection,
373*b0d17251Schristos                                 const char *structure)
374*b0d17251Schristos {
375*b0d17251Schristos     int ok = 0;
376*b0d17251Schristos     BIO *membio_legacy = NULL;
377*b0d17251Schristos     BIO *membio_provided = NULL;
378*b0d17251Schristos     OSSL_ENCODER_CTX *ectx = NULL;
379*b0d17251Schristos     OSSL_DECODER_CTX *dctx = NULL;
380*b0d17251Schristos     void *decoded_legacy_key = NULL;
381*b0d17251Schristos     EVP_PKEY *decoded_legacy_pkey = NULL;
382*b0d17251Schristos     EVP_PKEY *decoded_provided_pkey = NULL;
383*b0d17251Schristos 
384*b0d17251Schristos     /* Set up the BIOs, so we have them */
385*b0d17251Schristos     if (!TEST_ptr(membio_legacy = BIO_new(BIO_s_mem()))
386*b0d17251Schristos         || !TEST_ptr(membio_provided = BIO_new(BIO_s_mem())))
387*b0d17251Schristos         goto end;
388*b0d17251Schristos 
389*b0d17251Schristos     if (!TEST_ptr(ectx =
390*b0d17251Schristos                   OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
391*b0d17251Schristos                                                 "PEM", structure,
392*b0d17251Schristos                                                 NULL))
393*b0d17251Schristos         || !TEST_true(OSSL_ENCODER_to_bio(ectx, membio_provided))
394*b0d17251Schristos         || !TEST_true(pem_write_bio(membio_legacy, legacy_key))
395*b0d17251Schristos         || !test_membio_str_eq(membio_provided, membio_legacy))
396*b0d17251Schristos         goto end;
397*b0d17251Schristos 
398*b0d17251Schristos     if (pem_read_bio != NULL) {
399*b0d17251Schristos         /* Now try decoding the results and compare the resulting keys */
400*b0d17251Schristos 
401*b0d17251Schristos         if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
402*b0d17251Schristos             || !TEST_ptr(dctx =
403*b0d17251Schristos                          OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
404*b0d17251Schristos                                                        "PEM", structure,
405*b0d17251Schristos                                                        keytype, selection,
406*b0d17251Schristos                                                        NULL, NULL))
407*b0d17251Schristos             || !TEST_true(OSSL_DECODER_from_bio(dctx, membio_provided))
408*b0d17251Schristos             || !TEST_ptr(decoded_legacy_key =
409*b0d17251Schristos                          pem_read_bio(membio_legacy, NULL, NULL, NULL))
410*b0d17251Schristos             || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
411*b0d17251Schristos                                           decoded_legacy_key)))
412*b0d17251Schristos             goto end;
413*b0d17251Schristos 
414*b0d17251Schristos         if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
415*b0d17251Schristos                                      decoded_legacy_pkey), 0)) {
416*b0d17251Schristos             TEST_info("decoded_provided_pkey:");
417*b0d17251Schristos             evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
418*b0d17251Schristos             TEST_info("decoded_legacy_pkey:");
419*b0d17251Schristos             evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
420*b0d17251Schristos         }
421*b0d17251Schristos     }
422*b0d17251Schristos     ok = 1;
423*b0d17251Schristos  end:
424*b0d17251Schristos     EVP_PKEY_free(decoded_legacy_pkey);
425*b0d17251Schristos     EVP_PKEY_free(decoded_provided_pkey);
426*b0d17251Schristos     OSSL_ENCODER_CTX_free(ectx);
427*b0d17251Schristos     OSSL_DECODER_CTX_free(dctx);
428*b0d17251Schristos     BIO_free(membio_provided);
429*b0d17251Schristos     BIO_free(membio_legacy);
430*b0d17251Schristos     return ok;
431*b0d17251Schristos }
432*b0d17251Schristos 
test_DER(const char * keytype,int evp_type,const void * legacy_key,i2d_of_void * i2d,d2i_of_void * d2i,EVP_PKEY_eq_fn * evp_pkey_eq,EVP_PKEY_print_fn * evp_pkey_print,EVP_PKEY * provided_pkey,int selection,const char * structure)433*b0d17251Schristos static int test_DER(const char *keytype, int evp_type,
434*b0d17251Schristos                     const void *legacy_key, i2d_of_void *i2d, d2i_of_void *d2i,
435*b0d17251Schristos                     EVP_PKEY_eq_fn *evp_pkey_eq,
436*b0d17251Schristos                     EVP_PKEY_print_fn *evp_pkey_print,
437*b0d17251Schristos                     EVP_PKEY *provided_pkey, int selection,
438*b0d17251Schristos                     const char *structure)
439*b0d17251Schristos {
440*b0d17251Schristos     int ok = 0;
441*b0d17251Schristos     unsigned char *der_legacy = NULL;
442*b0d17251Schristos     const unsigned char *pder_legacy = NULL;
443*b0d17251Schristos     size_t der_legacy_len = 0;
444*b0d17251Schristos     unsigned char *der_provided = NULL;
445*b0d17251Schristos     const unsigned char *pder_provided = NULL;
446*b0d17251Schristos     size_t der_provided_len = 0;
447*b0d17251Schristos     size_t tmp_size;
448*b0d17251Schristos     OSSL_ENCODER_CTX *ectx = NULL;
449*b0d17251Schristos     OSSL_DECODER_CTX *dctx = NULL;
450*b0d17251Schristos     void *decoded_legacy_key = NULL;
451*b0d17251Schristos     EVP_PKEY *decoded_legacy_pkey = NULL;
452*b0d17251Schristos     EVP_PKEY *decoded_provided_pkey = NULL;
453*b0d17251Schristos 
454*b0d17251Schristos     if (!TEST_ptr(ectx =
455*b0d17251Schristos                  OSSL_ENCODER_CTX_new_for_pkey(provided_pkey, selection,
456*b0d17251Schristos                                                "DER", structure,
457*b0d17251Schristos                                                NULL))
458*b0d17251Schristos         || !TEST_true(OSSL_ENCODER_to_data(ectx,
459*b0d17251Schristos                                           &der_provided, &der_provided_len))
460*b0d17251Schristos         || !TEST_size_t_gt(der_legacy_len = i2d(legacy_key, &der_legacy), 0)
461*b0d17251Schristos         || !TEST_mem_eq(der_provided, der_provided_len,
462*b0d17251Schristos                         der_legacy, der_legacy_len))
463*b0d17251Schristos         goto end;
464*b0d17251Schristos 
465*b0d17251Schristos     if (d2i != NULL) {
466*b0d17251Schristos         /* Now try decoding the results and compare the resulting keys */
467*b0d17251Schristos 
468*b0d17251Schristos         if (!TEST_ptr(decoded_legacy_pkey = EVP_PKEY_new())
469*b0d17251Schristos             || !TEST_ptr(dctx =
470*b0d17251Schristos                          OSSL_DECODER_CTX_new_for_pkey(&decoded_provided_pkey,
471*b0d17251Schristos                                                        "DER", structure,
472*b0d17251Schristos                                                        keytype, selection,
473*b0d17251Schristos                                                        NULL, NULL))
474*b0d17251Schristos             || !TEST_true((pder_provided = der_provided,
475*b0d17251Schristos                            tmp_size = der_provided_len,
476*b0d17251Schristos                            OSSL_DECODER_from_data(dctx, &pder_provided,
477*b0d17251Schristos                                                   &tmp_size)))
478*b0d17251Schristos             || !TEST_ptr((pder_legacy = der_legacy,
479*b0d17251Schristos                           decoded_legacy_key = d2i(NULL, &pder_legacy,
480*b0d17251Schristos                                                    (long)der_legacy_len)))
481*b0d17251Schristos             || !TEST_true(EVP_PKEY_assign(decoded_legacy_pkey, evp_type,
482*b0d17251Schristos                                           decoded_legacy_key)))
483*b0d17251Schristos             goto end;
484*b0d17251Schristos 
485*b0d17251Schristos         if (!TEST_int_gt(evp_pkey_eq(decoded_provided_pkey,
486*b0d17251Schristos                                      decoded_legacy_pkey), 0)) {
487*b0d17251Schristos             TEST_info("decoded_provided_pkey:");
488*b0d17251Schristos             evp_pkey_print(bio_out, decoded_provided_pkey, 0, NULL);
489*b0d17251Schristos             TEST_info("decoded_legacy_pkey:");
490*b0d17251Schristos             evp_pkey_print(bio_out, decoded_legacy_pkey, 0, NULL);
491*b0d17251Schristos         }
492*b0d17251Schristos     }
493*b0d17251Schristos     ok = 1;
494*b0d17251Schristos  end:
495*b0d17251Schristos     EVP_PKEY_free(decoded_legacy_pkey);
496*b0d17251Schristos     EVP_PKEY_free(decoded_provided_pkey);
497*b0d17251Schristos     OSSL_ENCODER_CTX_free(ectx);
498*b0d17251Schristos     OSSL_DECODER_CTX_free(dctx);
499*b0d17251Schristos     OPENSSL_free(der_provided);
500*b0d17251Schristos     OPENSSL_free(der_legacy);
501*b0d17251Schristos     return ok;
502*b0d17251Schristos }
503*b0d17251Schristos 
test_key(int idx)504*b0d17251Schristos static int test_key(int idx)
505*b0d17251Schristos {
506*b0d17251Schristos     struct test_stanza_st *test_stanza = NULL;
507*b0d17251Schristos     struct key_st *key = NULL;
508*b0d17251Schristos     int ok = 0;
509*b0d17251Schristos     size_t i;
510*b0d17251Schristos     EVP_PKEY *pkey = NULL, *downgraded_pkey = NULL;
511*b0d17251Schristos     const void *legacy_obj = NULL;
512*b0d17251Schristos 
513*b0d17251Schristos     /* Get the test data */
514*b0d17251Schristos     if (!TEST_ptr(test_stanza = &test_stanzas[idx])
515*b0d17251Schristos         || !TEST_ptr(key = lookup_key(test_stanza->keytype)))
516*b0d17251Schristos         goto end;
517*b0d17251Schristos 
518*b0d17251Schristos     /* Set up the keys */
519*b0d17251Schristos     if (!TEST_ptr(pkey = key->key)
520*b0d17251Schristos         || !TEST_true(evp_pkey_copy_downgraded(&downgraded_pkey, pkey))
521*b0d17251Schristos         || !TEST_ptr(downgraded_pkey)
522*b0d17251Schristos         || !TEST_int_eq(EVP_PKEY_get_id(downgraded_pkey), key->evp_type)
523*b0d17251Schristos         || !TEST_ptr(legacy_obj = EVP_PKEY_get0(downgraded_pkey)))
524*b0d17251Schristos         goto end;
525*b0d17251Schristos 
526*b0d17251Schristos     ok = 1;
527*b0d17251Schristos 
528*b0d17251Schristos     /* Test PrivateKey to PEM */
529*b0d17251Schristos     if (test_stanza->pem_write_bio_PrivateKey != NULL) {
530*b0d17251Schristos         int selection = OSSL_KEYMGMT_SELECT_ALL;
531*b0d17251Schristos 
532*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
533*b0d17251Schristos             const char *structure = test_stanza->structure[i];
534*b0d17251Schristos 
535*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PrivateKey for %s, %s",
536*b0d17251Schristos                       test_stanza->keytype, structure);
537*b0d17251Schristos             if (!test_protected_PEM(key->keytype, key->evp_type, legacy_obj,
538*b0d17251Schristos                                     test_stanza->pem_write_bio_PrivateKey,
539*b0d17251Schristos                                     test_stanza->pem_read_bio_PrivateKey,
540*b0d17251Schristos                                     EVP_PKEY_eq, EVP_PKEY_print_private,
541*b0d17251Schristos                                     pkey, selection, structure))
542*b0d17251Schristos                 ok = 0;
543*b0d17251Schristos         }
544*b0d17251Schristos     }
545*b0d17251Schristos 
546*b0d17251Schristos     /* Test PublicKey to PEM */
547*b0d17251Schristos     if (test_stanza->pem_write_bio_PublicKey != NULL) {
548*b0d17251Schristos         int selection =
549*b0d17251Schristos             OSSL_KEYMGMT_SELECT_PUBLIC_KEY
550*b0d17251Schristos             | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
551*b0d17251Schristos 
552*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
553*b0d17251Schristos             const char *structure = test_stanza->structure[i];
554*b0d17251Schristos 
555*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}PublicKey for %s, %s",
556*b0d17251Schristos                       test_stanza->keytype, structure);
557*b0d17251Schristos             if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
558*b0d17251Schristos                                       test_stanza->pem_write_bio_PublicKey,
559*b0d17251Schristos                                       test_stanza->pem_read_bio_PublicKey,
560*b0d17251Schristos                                       EVP_PKEY_eq, EVP_PKEY_print_public,
561*b0d17251Schristos                                       pkey, selection, structure))
562*b0d17251Schristos                 ok = 0;
563*b0d17251Schristos         }
564*b0d17251Schristos     }
565*b0d17251Schristos 
566*b0d17251Schristos     /* Test params to PEM */
567*b0d17251Schristos     if (test_stanza->pem_write_bio_params != NULL) {
568*b0d17251Schristos         int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
569*b0d17251Schristos 
570*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
571*b0d17251Schristos             const char *structure = test_stanza->structure[i];
572*b0d17251Schristos 
573*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}params for %s, %s",
574*b0d17251Schristos                       test_stanza->keytype, structure);
575*b0d17251Schristos             if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
576*b0d17251Schristos                                       test_stanza->pem_write_bio_params,
577*b0d17251Schristos                                       test_stanza->pem_read_bio_params,
578*b0d17251Schristos                                       EVP_PKEY_parameters_eq,
579*b0d17251Schristos                                       EVP_PKEY_print_params,
580*b0d17251Schristos                                       pkey, selection, structure))
581*b0d17251Schristos                 ok = 0;
582*b0d17251Schristos         }
583*b0d17251Schristos     }
584*b0d17251Schristos 
585*b0d17251Schristos     /* Test PUBKEY to PEM */
586*b0d17251Schristos     if (test_stanza->pem_write_bio_PUBKEY != NULL) {
587*b0d17251Schristos         int selection =
588*b0d17251Schristos             OSSL_KEYMGMT_SELECT_PUBLIC_KEY
589*b0d17251Schristos             | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
590*b0d17251Schristos         const char *structure = "SubjectPublicKeyInfo";
591*b0d17251Schristos 
592*b0d17251Schristos         TEST_info("Test OSSL_ENCODER against PEM_write_bio_{TYPE}_PUBKEY for %s, %s",
593*b0d17251Schristos                   test_stanza->keytype, structure);
594*b0d17251Schristos         if (!test_unprotected_PEM(key->keytype, key->evp_type, legacy_obj,
595*b0d17251Schristos                                   test_stanza->pem_write_bio_PUBKEY,
596*b0d17251Schristos                                   test_stanza->pem_read_bio_PUBKEY,
597*b0d17251Schristos                                   EVP_PKEY_eq, EVP_PKEY_print_public,
598*b0d17251Schristos                                   pkey, selection, structure))
599*b0d17251Schristos             ok = 0;
600*b0d17251Schristos     }
601*b0d17251Schristos 
602*b0d17251Schristos 
603*b0d17251Schristos     /* Test PrivateKey to DER */
604*b0d17251Schristos     if (test_stanza->i2d_PrivateKey != NULL) {
605*b0d17251Schristos         int selection = OSSL_KEYMGMT_SELECT_ALL;
606*b0d17251Schristos 
607*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
608*b0d17251Schristos             const char *structure = test_stanza->structure[i];
609*b0d17251Schristos 
610*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PrivateKey for %s, %s",
611*b0d17251Schristos                       test_stanza->keytype, structure);
612*b0d17251Schristos             if (!test_DER(key->keytype, key->evp_type, legacy_obj,
613*b0d17251Schristos                           test_stanza->i2d_PrivateKey,
614*b0d17251Schristos                           test_stanza->d2i_PrivateKey,
615*b0d17251Schristos                           EVP_PKEY_eq, EVP_PKEY_print_private,
616*b0d17251Schristos                           pkey, selection, structure))
617*b0d17251Schristos                 ok = 0;
618*b0d17251Schristos         }
619*b0d17251Schristos     }
620*b0d17251Schristos 
621*b0d17251Schristos     /* Test PublicKey to DER */
622*b0d17251Schristos     if (test_stanza->i2d_PublicKey != NULL) {
623*b0d17251Schristos         int selection =
624*b0d17251Schristos             OSSL_KEYMGMT_SELECT_PUBLIC_KEY
625*b0d17251Schristos             | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
626*b0d17251Schristos 
627*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
628*b0d17251Schristos             const char *structure = test_stanza->structure[i];
629*b0d17251Schristos 
630*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against i2d_{TYPE}PublicKey for %s, %s",
631*b0d17251Schristos                       test_stanza->keytype, structure);
632*b0d17251Schristos             if (!test_DER(key->keytype, key->evp_type, legacy_obj,
633*b0d17251Schristos                           test_stanza->i2d_PublicKey,
634*b0d17251Schristos                           test_stanza->d2i_PublicKey,
635*b0d17251Schristos                           EVP_PKEY_eq, EVP_PKEY_print_public,
636*b0d17251Schristos                           pkey, selection, structure))
637*b0d17251Schristos                 ok = 0;
638*b0d17251Schristos         }
639*b0d17251Schristos     }
640*b0d17251Schristos 
641*b0d17251Schristos     /* Test params to DER */
642*b0d17251Schristos     if (test_stanza->i2d_params != NULL) {
643*b0d17251Schristos         int selection = OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
644*b0d17251Schristos 
645*b0d17251Schristos         for (i = 0; i < OSSL_NELEM(test_stanza->structure); i++) {
646*b0d17251Schristos             const char *structure = test_stanza->structure[i];
647*b0d17251Schristos 
648*b0d17251Schristos             TEST_info("Test OSSL_ENCODER against i2d_{TYPE}params for %s, %s",
649*b0d17251Schristos                       test_stanza->keytype, structure);
650*b0d17251Schristos             if (!test_DER(key->keytype, key->evp_type, legacy_obj,
651*b0d17251Schristos                           test_stanza->i2d_params, test_stanza->d2i_params,
652*b0d17251Schristos                           EVP_PKEY_parameters_eq, EVP_PKEY_print_params,
653*b0d17251Schristos                           pkey, selection, structure))
654*b0d17251Schristos                 ok = 0;
655*b0d17251Schristos         }
656*b0d17251Schristos     }
657*b0d17251Schristos 
658*b0d17251Schristos     /* Test PUBKEY to DER */
659*b0d17251Schristos     if (test_stanza->i2d_PUBKEY != NULL) {
660*b0d17251Schristos         int selection =
661*b0d17251Schristos             OSSL_KEYMGMT_SELECT_PUBLIC_KEY
662*b0d17251Schristos             | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS;
663*b0d17251Schristos         const char *structure = "SubjectPublicKeyInfo";
664*b0d17251Schristos 
665*b0d17251Schristos         TEST_info("Test OSSL_ENCODER against i2d_{TYPE}_PUBKEY for %s, %s",
666*b0d17251Schristos                   test_stanza->keytype, structure);
667*b0d17251Schristos         if (!test_DER(key->keytype, key->evp_type, legacy_obj,
668*b0d17251Schristos                       test_stanza->i2d_PUBKEY, test_stanza->d2i_PUBKEY,
669*b0d17251Schristos                       EVP_PKEY_eq, EVP_PKEY_print_public,
670*b0d17251Schristos                       pkey, selection, structure))
671*b0d17251Schristos             ok = 0;
672*b0d17251Schristos     }
673*b0d17251Schristos  end:
674*b0d17251Schristos     EVP_PKEY_free(downgraded_pkey);
675*b0d17251Schristos     return ok;
676*b0d17251Schristos }
677*b0d17251Schristos 
678*b0d17251Schristos #define USAGE "rsa-key.pem dh-key.pem\n"
OPT_TEST_DECLARE_USAGE(USAGE)679*b0d17251Schristos OPT_TEST_DECLARE_USAGE(USAGE)
680*b0d17251Schristos 
681*b0d17251Schristos int setup_tests(void)
682*b0d17251Schristos {
683*b0d17251Schristos     size_t i;
684*b0d17251Schristos 
685*b0d17251Schristos     if (!test_skip_common_options()) {
686*b0d17251Schristos         TEST_error("Error parsing test options\n");
687*b0d17251Schristos         return 0;
688*b0d17251Schristos     }
689*b0d17251Schristos     if (test_get_argument_count() != 2) {
690*b0d17251Schristos         TEST_error("usage: endecoder_legacy_test %s", USAGE);
691*b0d17251Schristos         return 0;
692*b0d17251Schristos     }
693*b0d17251Schristos 
694*b0d17251Schristos     TEST_info("Generating keys...");
695*b0d17251Schristos 
696*b0d17251Schristos     for (i = 0; i < OSSL_NELEM(keys); i++) {
697*b0d17251Schristos #ifndef OPENSSL_NO_DH
698*b0d17251Schristos         if (strcmp(keys[i].keytype, "DH") == 0) {
699*b0d17251Schristos             if (!TEST_ptr(keys[i].key =
700*b0d17251Schristos                           load_pkey_pem(test_get_argument(1), NULL)))
701*b0d17251Schristos                 return  0;
702*b0d17251Schristos             continue;
703*b0d17251Schristos         }
704*b0d17251Schristos #endif
705*b0d17251Schristos #ifndef OPENSSL_NO_DEPRECATED_3_0
706*b0d17251Schristos         if (strcmp(keys[i].keytype, "RSA") == 0) {
707*b0d17251Schristos             if (!TEST_ptr(keys[i].key =
708*b0d17251Schristos                           load_pkey_pem(test_get_argument(0), NULL)))
709*b0d17251Schristos                 return  0;
710*b0d17251Schristos             continue;
711*b0d17251Schristos         }
712*b0d17251Schristos #endif
713*b0d17251Schristos         TEST_info("Generating %s key...", keys[i].keytype);
714*b0d17251Schristos         if (!TEST_ptr(keys[i].key =
715*b0d17251Schristos                       make_key(keys[i].keytype, keys[i].template_params)))
716*b0d17251Schristos             return 0;
717*b0d17251Schristos     }
718*b0d17251Schristos 
719*b0d17251Schristos     TEST_info("Generating keys done");
720*b0d17251Schristos 
721*b0d17251Schristos     ADD_ALL_TESTS(test_key, OSSL_NELEM(test_stanzas));
722*b0d17251Schristos     return 1;
723*b0d17251Schristos }
724*b0d17251Schristos 
cleanup_tests(void)725*b0d17251Schristos void cleanup_tests(void)
726*b0d17251Schristos {
727*b0d17251Schristos     size_t i;
728*b0d17251Schristos 
729*b0d17251Schristos     for (i = 0; i < OSSL_NELEM(keys); i++)
730*b0d17251Schristos         EVP_PKEY_free(keys[i].key);
731*b0d17251Schristos }
732