]> git.ipfire.org Git - thirdparty/openssl.git/blame - providers/implementations/encode_decode/encode_key2any.c
appveyor.yml: Move printing of env variables such that locally defined ones are shown...
[thirdparty/openssl.git] / providers / implementations / encode_decode / encode_key2any.c
CommitLineData
8ae40cf5
RL
1/*
2 * Copyright 2020 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/*
11 * Low level APIs are deprecated for public use, but still ok for internal use.
12 */
13#include "internal/deprecated.h"
14
15#include <openssl/core.h>
16#include <openssl/core_dispatch.h>
17#include <openssl/core_names.h>
18#include <openssl/crypto.h>
19#include <openssl/params.h>
20#include <openssl/asn1.h>
21#include <openssl/err.h>
22#include <openssl/pem.h>
23#include <openssl/x509.h>
24#include <openssl/pkcs12.h> /* PKCS8_encrypt() */
25#include <openssl/dh.h>
26#include <openssl/dsa.h>
27#include <openssl/ec.h>
28#include "internal/passphrase.h"
29#include "internal/cryptlib.h"
30#include "crypto/ecx.h"
31#include "crypto/rsa.h"
32#include "prov/implementations.h"
33#include "prov/providercommonerr.h"
34#include "prov/bio.h"
35#include "prov/provider_ctx.h"
36#include "prov/der_rsa.h"
37#include "endecoder_local.h"
38
39struct key2any_ctx_st {
40 PROV_CTX *provctx;
41
42 /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
43 int cipher_intent;
44
45 EVP_CIPHER *cipher;
46
47 struct ossl_passphrase_data_st pwdata;
48};
49
111dc4b0 50typedef int check_key_type_fn(const void *key, int nid);
8ae40cf5
RL
51typedef int key_to_paramstring_fn(const void *key, int nid,
52 void **str, int *strtype);
c319b627
RL
53typedef int key_to_der_fn(BIO *out, const void *key,
54 int key_nid, const char *pemname,
8ae40cf5
RL
55 key_to_paramstring_fn *p2s, i2d_of_void *k2d,
56 struct key2any_ctx_st *ctx);
57typedef int write_bio_of_void_fn(BIO *bp, const void *x);
58
59static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid,
60 void *params, int params_type,
61 i2d_of_void *k2d)
62{
63 /* der, derlen store the key DER output and its length */
64 unsigned char *der = NULL;
65 int derlen;
66 /* The final PKCS#8 info */
67 PKCS8_PRIV_KEY_INFO *p8info = NULL;
68
69
70 if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
71 || (derlen = k2d(key, &der)) <= 0
72 || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0,
73 params_type, params, der, derlen)) {
74 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
75 PKCS8_PRIV_KEY_INFO_free(p8info);
76 OPENSSL_free(der);
77 p8info = NULL;
78 }
79
80 return p8info;
81}
82
83static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info,
84 struct key2any_ctx_st *ctx)
85{
86 X509_SIG *p8 = NULL;
87 char kstr[PEM_BUFSIZE];
88 size_t klen = 0;
89
90 if (ctx->cipher == NULL)
91 return NULL;
92
93 if (!ossl_pw_get_passphrase(kstr, sizeof(kstr), &klen, NULL, 1,
94 &ctx->pwdata)) {
95 ERR_raise(ERR_LIB_PROV, PROV_R_READ_KEY);
96 return NULL;
97 }
98 /* First argument == -1 means "standard" */
99 p8 = PKCS8_encrypt(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info);
100 OPENSSL_cleanse(kstr, klen);
101 return p8;
102}
103
104static X509_SIG *key_to_encp8(const void *key, int key_nid,
105 void *params, int params_type,
106 i2d_of_void *k2d, struct key2any_ctx_st *ctx)
107{
108 PKCS8_PRIV_KEY_INFO *p8info =
109 key_to_p8info(key, key_nid, params, params_type, k2d);
110 X509_SIG *p8 = p8info_to_encp8(p8info, ctx);
111
112 PKCS8_PRIV_KEY_INFO_free(p8info);
113 return p8;
114}
115
116static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid,
117 void *params, int params_type,
118 i2d_of_void k2d)
119{
120 /* der, derlen store the key DER output and its length */
121 unsigned char *der = NULL;
122 int derlen;
123 /* The final X509_PUBKEY */
124 X509_PUBKEY *xpk = NULL;
125
126
127 if ((xpk = X509_PUBKEY_new()) == NULL
128 || (derlen = k2d(key, &der)) <= 0
129 || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid),
130 params_type, params, der, derlen)) {
131 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
132 X509_PUBKEY_free(xpk);
133 OPENSSL_free(der);
134 xpk = NULL;
135 }
136
137 return xpk;
138}
139
c319b627
RL
140/*
141 * key_to_pkcs8_* produce encoded output with the key data pkcs8
142 * in a structure. For private keys, that structure is PKCS#8, and for
143 * public keys, it's X.509 SubjectPublicKeyInfo. Parameters don't have
144 * any defined envelopment of that kind.
145 */
146static int key_to_pkcs8_der_priv_bio(BIO *out, const void *key,
147 int key_nid,
148 ossl_unused const char *pemname,
149 key_to_paramstring_fn *p2s,
150 i2d_of_void *k2d,
151 struct key2any_ctx_st *ctx)
8ae40cf5
RL
152{
153 int ret = 0;
154 void *str = NULL;
155 int strtype = V_ASN1_UNDEF;
156
157 if (p2s != NULL && !p2s(key, key_nid, &str, &strtype))
158 return 0;
159
160 if (ctx->cipher_intent) {
161 X509_SIG *p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
162
163 if (p8 != NULL)
164 ret = i2d_PKCS8_bio(out, p8);
165
166 X509_SIG_free(p8);
167 } else {
168 PKCS8_PRIV_KEY_INFO *p8info =
169 key_to_p8info(key, key_nid, str, strtype, k2d);
170
171 if (p8info != NULL)
172 ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
173
174 PKCS8_PRIV_KEY_INFO_free(p8info);
175 }
176
177 return ret;
178}
179
c319b627
RL
180static int key_to_pkcs8_pem_priv_bio(BIO *out, const void *key,
181 int key_nid,
182 ossl_unused const char *pemname,
183 key_to_paramstring_fn *p2s,
184 i2d_of_void *k2d,
185 struct key2any_ctx_st *ctx)
8ae40cf5
RL
186{
187 int ret = 0;
188 void *str = NULL;
189 int strtype = V_ASN1_UNDEF;
190
191 if (p2s != NULL && !p2s(key, key_nid, &str, &strtype))
192 return 0;
193
194 if (ctx->cipher_intent) {
195 X509_SIG *p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
196
197 if (p8 != NULL)
198 ret = PEM_write_bio_PKCS8(out, p8);
199
200 X509_SIG_free(p8);
201 } else {
202 PKCS8_PRIV_KEY_INFO *p8info =
203 key_to_p8info(key, key_nid, str, strtype, k2d);
204
205 if (p8info != NULL)
206 ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
207
208 PKCS8_PRIV_KEY_INFO_free(p8info);
209 }
210
211 return ret;
212}
213
c319b627
RL
214static int key_to_spki_der_pub_bio(BIO *out, const void *key,
215 int key_nid,
216 ossl_unused const char *pemname,
217 key_to_paramstring_fn *p2s,
218 i2d_of_void *k2d,
219 struct key2any_ctx_st *ctx)
8ae40cf5
RL
220{
221 int ret = 0;
222 void *str = NULL;
223 int strtype = V_ASN1_UNDEF;
224 X509_PUBKEY *xpk = NULL;
225
226 if (p2s != NULL && !p2s(key, key_nid, &str, &strtype))
227 return 0;
228
229 xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
230
231 if (xpk != NULL)
232 ret = i2d_X509_PUBKEY_bio(out, xpk);
233
234 /* Also frees |str| */
235 X509_PUBKEY_free(xpk);
236 return ret;
237}
238
c319b627
RL
239static int key_to_spki_pem_pub_bio(BIO *out, const void *key,
240 int key_nid,
241 ossl_unused const char *pemname,
242 key_to_paramstring_fn *p2s,
243 i2d_of_void *k2d,
244 struct key2any_ctx_st *ctx)
8ae40cf5
RL
245{
246 int ret = 0;
247 void *str = NULL;
248 int strtype = V_ASN1_UNDEF;
249 X509_PUBKEY *xpk = NULL;
250
251 if (p2s != NULL && !p2s(key, key_nid, &str, &strtype))
252 return 0;
253
254 xpk = key_to_pubkey(key, key_nid, str, strtype, k2d);
255
256 if (xpk != NULL)
257 ret = PEM_write_bio_X509_PUBKEY(out, xpk);
258
259 /* Also frees |str| */
260 X509_PUBKEY_free(xpk);
261 return ret;
262}
263
c319b627
RL
264/*
265 * key_to_type_specific_* produce encoded output with type specific key data,
266 * no envelopment; the same kind of output as the type specific i2d_ and
267 * PEM_write_ functions, which is often a simple SEQUENCE of INTEGER.
268 *
269 * OpenSSL tries to discourage production of new keys in this form, because
270 * of the ambiguity when trying to recognise them, but can't deny that PKCS#1
271 * et al still are live standards.
272 *
273 * Note that these functions completely ignore p2s, and rather rely entirely
274 * on k2d to do the complete work.
275 */
276static int key_to_type_specific_der_bio(BIO *out, const void *key,
277 int key_nid,
278 ossl_unused const char *pemname,
279 key_to_paramstring_fn *p2s,
280 i2d_of_void *k2d,
281 struct key2any_ctx_st *ctx)
282{
283 unsigned char *der = NULL;
284 int derlen;
285 int ret;
286
287 if ((derlen = k2d(key, &der)) <= 0) {
288 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
289 return 0;
290 }
291
292 ret = BIO_write(out, der, derlen);
293 OPENSSL_free(der);
294 return ret > 0;
295}
296#define key_to_type_specific_der_priv_bio key_to_type_specific_der_bio
297#define key_to_type_specific_der_pub_bio key_to_type_specific_der_bio
298#define key_to_type_specific_der_param_bio key_to_type_specific_der_bio
299
300static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key,
301 int key_nid, const char *pemname,
302 key_to_paramstring_fn *p2s,
303 i2d_of_void *k2d,
304 struct key2any_ctx_st *ctx,
305 pem_password_cb *cb, void *cbarg)
306{
307 return
308 PEM_ASN1_write_bio(k2d, pemname, out, key, ctx->cipher,
309 NULL, 0, ossl_pw_pem_password, &ctx->pwdata) > 0;
310}
311
312static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key,
313 int key_nid, const char *pemname,
314 key_to_paramstring_fn *p2s,
315 i2d_of_void *k2d,
316 struct key2any_ctx_st *ctx)
317{
318 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
319 p2s, k2d, ctx,
320 ossl_pw_pem_password, &ctx->pwdata);
321}
322
323static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key,
324 int key_nid, const char *pemname,
325 key_to_paramstring_fn *p2s,
326 i2d_of_void *k2d,
327 struct key2any_ctx_st *ctx)
328{
329 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
330 p2s, k2d, ctx, NULL, NULL);
331}
332
333static int key_to_type_specific_pem_param_bio(BIO *out, const void *key,
334 int key_nid, const char *pemname,
335 key_to_paramstring_fn *p2s,
336 i2d_of_void *k2d,
337 struct key2any_ctx_st *ctx)
338{
339 return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname,
340 p2s, k2d, ctx, NULL, NULL);
341}
342
111dc4b0
RL
343#define der_output_type "DER"
344#define pem_output_type "PEM"
345
8ae40cf5
RL
346/* ---------------------------------------------------------------------- */
347
348#ifndef OPENSSL_NO_DH
8ae40cf5
RL
349static int prepare_dh_params(const void *dh, int nid,
350 void **pstr, int *pstrtype)
351{
352 ASN1_STRING *params = ASN1_STRING_new();
353
354 if (params == NULL) {
355 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
356 return 0;
357 }
358
359 if (nid == EVP_PKEY_DHX)
360 params->length = i2d_DHxparams(dh, &params->data);
361 else
362 params->length = i2d_DHparams(dh, &params->data);
363
364 if (params->length <= 0) {
365 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
366 ASN1_STRING_free(params);
367 return 0;
368 }
369 params->type = V_ASN1_SEQUENCE;
370
371 *pstr = params;
372 *pstrtype = V_ASN1_SEQUENCE;
373 return 1;
374}
375
c319b627 376static int dh_spki_pub_to_der(const void *dh, unsigned char **pder)
8ae40cf5
RL
377{
378 const BIGNUM *bn = NULL;
379 ASN1_INTEGER *pub_key = NULL;
380 int ret;
381
382 if ((bn = DH_get0_pub_key(dh)) == NULL) {
383 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
384 return 0;
385 }
386 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
387 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
388 return 0;
389 }
390
391 ret = i2d_ASN1_INTEGER(pub_key, pder);
392
393 ASN1_STRING_clear_free(pub_key);
394 return ret;
395}
396
c319b627 397static int dh_pkcs8_priv_to_der(const void *dh, unsigned char **pder)
8ae40cf5
RL
398{
399 const BIGNUM *bn = NULL;
400 ASN1_INTEGER *priv_key = NULL;
401 int ret;
402
403 if ((bn = DH_get0_priv_key(dh)) == NULL) {
404 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
405 return 0;
406 }
407 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
408 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
409 return 0;
410 }
411
412 ret = i2d_ASN1_INTEGER(priv_key, pder);
413
414 ASN1_STRING_clear_free(priv_key);
415 return ret;
416}
417
c319b627 418static int dh_type_specific_params_to_der(const void *dh, unsigned char **pder)
8ae40cf5 419{
c319b627
RL
420 if (DH_test_flags(dh, DH_FLAG_TYPE_DHX))
421 return i2d_DHxparams(dh, pder);
422 return i2d_DHparams(dh, pder);
8ae40cf5
RL
423}
424
c319b627
RL
425/*
426 * DH doesn't have i2d_DHPrivateKey or i2d_DHPublicKey, so we can't make
427 * corresponding functions here.
428 */
429# define dh_type_specific_priv_to_der NULL
430# define dh_type_specific_pub_to_der NULL
111dc4b0 431
c319b627 432static int dh_check_key_type(const void *dh, int expected_type)
111dc4b0
RL
433{
434 int type =
c319b627 435 DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH;
111dc4b0
RL
436
437 return type == expected_type;
438}
439
440# define dh_evp_type EVP_PKEY_DH
441# define dhx_evp_type EVP_PKEY_DHX
442# define dh_input_type "DH"
443# define dhx_input_type "DHX"
c319b627
RL
444# define dh_pem_type "DH"
445# define dhx_pem_type "X9.42 DH"
8ae40cf5
RL
446#endif
447
448/* ---------------------------------------------------------------------- */
449
450#ifndef OPENSSL_NO_DSA
8ae40cf5
RL
451static int prepare_some_dsa_params(const void *dsa, int nid,
452 void **pstr, int *pstrtype)
453{
454 ASN1_STRING *params = ASN1_STRING_new();
455
456 if (params == NULL) {
457 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
458 return 0;
459 }
460
461 params->length = i2d_DSAparams(dsa, &params->data);
462
463 if (params->length <= 0) {
464 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
465 ASN1_STRING_free(params);
466 return 0;
467 }
468
469 *pstrtype = V_ASN1_SEQUENCE;
470 *pstr = params;
471 return 1;
472}
473
474static int prepare_all_dsa_params(const void *dsa, int nid,
475 void **pstr, int *pstrtype)
476{
477 const BIGNUM *p = DSA_get0_p(dsa);
478 const BIGNUM *q = DSA_get0_q(dsa);
479 const BIGNUM *g = DSA_get0_g(dsa);
480
481 if (p != NULL && q != NULL && g != NULL)
482 return prepare_some_dsa_params(dsa, nid, pstr, pstrtype);
483
484 *pstr = NULL;
485 *pstrtype = V_ASN1_UNDEF;
486 return 1;
487}
488
489static int prepare_dsa_params(const void *dsa, int nid,
490 void **pstr, int *pstrtype)
491{
492 /*
493 * TODO(v3.0) implement setting save_parameters, see dsa_pub_encode()
494 * in crypto/dsa/dsa_ameth.c
495 */
496 int save_parameters = 1;
497
498 return save_parameters
499 ? prepare_all_dsa_params(dsa, nid, pstr, pstrtype)
500 : prepare_some_dsa_params(dsa, nid, pstr, pstrtype);
501}
502
c319b627 503static int dsa_spki_pub_to_der(const void *dsa, unsigned char **pder)
8ae40cf5
RL
504{
505 const BIGNUM *bn = NULL;
506 ASN1_INTEGER *pub_key = NULL;
507 int ret;
508
509 if ((bn = DSA_get0_pub_key(dsa)) == NULL) {
510 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY);
511 return 0;
512 }
513 if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
514 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
515 return 0;
516 }
517
518 ret = i2d_ASN1_INTEGER(pub_key, pder);
519
520 ASN1_STRING_clear_free(pub_key);
521 return ret;
522}
523
c319b627 524static int dsa_pkcs8_priv_to_der(const void *dsa, unsigned char **pder)
8ae40cf5
RL
525{
526 const BIGNUM *bn = NULL;
527 ASN1_INTEGER *priv_key = NULL;
528 int ret;
529
530 if ((bn = DSA_get0_priv_key(dsa)) == NULL) {
531 ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY);
532 return 0;
533 }
534 if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) {
535 ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR);
536 return 0;
537 }
538
539 ret = i2d_ASN1_INTEGER(priv_key, pder);
540
541 ASN1_STRING_clear_free(priv_key);
542 return ret;
543}
544
c319b627
RL
545# define dsa_type_specific_priv_to_der (i2d_of_void *)i2d_DSAPrivateKey
546# define dsa_type_specific_pub_to_der (i2d_of_void *)i2d_DSAPublicKey
547# define dsa_type_specific_params_to_der (i2d_of_void *)i2d_DSAparams
111dc4b0
RL
548
549# define dsa_check_key_type NULL
550# define dsa_evp_type EVP_PKEY_DSA
551# define dsa_input_type "DSA"
c319b627 552# define dsa_pem_type "DSA"
8ae40cf5
RL
553#endif
554
555/* ---------------------------------------------------------------------- */
556
557#ifndef OPENSSL_NO_EC
8ae40cf5
RL
558static int prepare_ec_explicit_params(const void *eckey,
559 void **pstr, int *pstrtype)
560{
561 ASN1_STRING *params = ASN1_STRING_new();
562
563 if (params == NULL) {
564 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
565 return 0;
566 }
567
568 params->length = i2d_ECParameters(eckey, &params->data);
569 if (params->length <= 0) {
570 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
571 ASN1_STRING_free(params);
572 return 0;
573 }
574
575 *pstrtype = V_ASN1_SEQUENCE;
576 *pstr = params;
577 return 1;
578}
579
c319b627
RL
580/*
581 * This implements EcpkParameters, where the CHOICE is based on whether there
582 * is a curve name (curve nid) to be found or not. See RFC 3279 for details.
583 * TODO: shouldn't we use i2d_ECPKParameters()?
584 */
8ae40cf5
RL
585static int prepare_ec_params(const void *eckey, int nid,
586 void **pstr, int *pstrtype)
587{
588 int curve_nid;
589 const EC_GROUP *group = EC_KEY_get0_group(eckey);
590 ASN1_OBJECT *params = NULL;
591
592 if (group == NULL)
593 return 0;
594 curve_nid = EC_GROUP_get_curve_name(group);
595 if (curve_nid != NID_undef) {
596 params = OBJ_nid2obj(curve_nid);
597 if (params == NULL)
598 return 0;
599 }
600
601 if (curve_nid != NID_undef
602 && (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE)) {
c319b627 603 /* The CHOICE came to namedCurve */
8ae40cf5
RL
604 if (OBJ_length(params) == 0) {
605 /* Some curves might not have an associated OID */
606 ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_OID);
607 ASN1_OBJECT_free(params);
608 return 0;
609 }
610 *pstr = params;
611 *pstrtype = V_ASN1_OBJECT;
612 return 1;
613 } else {
c319b627 614 /* The CHOICE came to ecParameters */
8ae40cf5
RL
615 return prepare_ec_explicit_params(eckey, pstr, pstrtype);
616 }
617}
618
c319b627 619static int ec_spki_pub_to_der(const void *eckey, unsigned char **pder)
8ae40cf5
RL
620{
621 return i2o_ECPublicKey(eckey, pder);
622}
623
c319b627 624static int ec_pkcs8_priv_to_der(const void *veckey, unsigned char **pder)
8ae40cf5
RL
625{
626 EC_KEY *eckey = (EC_KEY *)veckey;
627 unsigned int old_flags;
628 int ret = 0;
629
630 /*
631 * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object
632 * as the pkeyalg->parameter field. (For a named curve this is an OID)
633 * The pkey field is an octet string that holds the encoded
634 * ECPrivateKey SEQUENCE with the optional parameters field omitted.
635 * We omit this by setting the EC_PKEY_NO_PARAMETERS flag.
636 */
637 old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */
638 EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS);
639 ret = i2d_ECPrivateKey(eckey, pder);
640 EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */
641 return ret; /* return the length of the der encoded data */
642}
111dc4b0 643
c319b627
RL
644# define ec_type_specific_params_to_der (i2d_of_void *)i2d_ECParameters
645# define ec_type_specific_pub_to_der (i2d_of_void *)i2o_ECPublicKey
646# define ec_type_specific_priv_to_der (i2d_of_void *)i2d_ECPrivateKey
647
111dc4b0
RL
648# define ec_check_key_type NULL
649# define ec_evp_type EVP_PKEY_EC
650# define ec_input_type "EC"
c319b627 651# define ec_pem_type "EC"
8ae40cf5
RL
652#endif
653
654/* ---------------------------------------------------------------------- */
655
656#ifndef OPENSSL_NO_EC
8ae40cf5
RL
657# define prepare_ecx_params NULL
658
c319b627 659static int ecx_spki_pub_to_der(const void *vecxkey, unsigned char **pder)
8ae40cf5
RL
660{
661 const ECX_KEY *ecxkey = vecxkey;
662 unsigned char *keyblob;
663
664 if (ecxkey == NULL) {
665 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
666 return 0;
667 }
668
669 keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen);
670 if (keyblob == NULL) {
671 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
672 return 0;
673 }
674
675 *pder = keyblob;
676 return ecxkey->keylen;
677}
678
c319b627 679static int ecx_pkcs8_priv_to_der(const void *vecxkey, unsigned char **pder)
8ae40cf5
RL
680{
681 const ECX_KEY *ecxkey = vecxkey;
682 ASN1_OCTET_STRING oct;
683 int keybloblen;
684
685 if (ecxkey == NULL || ecxkey->privkey == NULL) {
686 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
687 return 0;
688 }
689
690 oct.data = ecxkey->privkey;
691 oct.length = ecxkey->keylen;
692 oct.flags = 0;
693
694 keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
695 if (keybloblen < 0) {
696 ERR_raise(ERR_LIB_PROV, ERR_R_MALLOC_FAILURE);
697 return 0;
698 }
699
700 return keybloblen;
701}
702
c319b627
RL
703/*
704 * ED25519, ED448, X25519 and X448 only has PKCS#8 / SubjectPublicKeyInfo
705 * representation, so we don't define ecx_type_specific_[priv,pub,params]_to_der.
706 */
707
111dc4b0
RL
708# define ecx_check_key_type NULL
709
710# define ed25519_evp_type EVP_PKEY_ED25519
711# define ed448_evp_type EVP_PKEY_ED448
712# define x25519_evp_type EVP_PKEY_X25519
713# define x448_evp_type EVP_PKEY_X448
714# define ed25519_input_type "ED25519"
715# define ed448_input_type "ED448"
716# define x25519_input_type "X25519"
717# define x448_input_type "X448"
c319b627
RL
718# define ed25519_pem_type "ED25519"
719# define ed448_pem_type "ED448"
720# define x25519_pem_type "X25519"
721# define x448_pem_type "X448"
8ae40cf5
RL
722#endif
723
724/* ---------------------------------------------------------------------- */
725
8ae40cf5
RL
726/*
727 * Helper functions to prepare RSA-PSS params for encoding. We would
728 * have simply written the whole AlgorithmIdentifier, but existing libcrypto
729 * functionality doesn't allow that.
730 */
731
732static int prepare_rsa_params(const void *rsa, int nid,
733 void **pstr, int *pstrtype)
734{
23b2fc0b 735 const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30((RSA *)rsa);
8ae40cf5
RL
736
737 *pstr = NULL;
738
739 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
740 case RSA_FLAG_TYPE_RSA:
741 /* If plain RSA, the parameters shall be NULL */
742 *pstrtype = V_ASN1_NULL;
743 return 1;
744 case RSA_FLAG_TYPE_RSASSAPSS:
23b2fc0b 745 if (ossl_rsa_pss_params_30_is_unrestricted(pss)) {
8ae40cf5
RL
746 *pstrtype = V_ASN1_UNDEF;
747 return 1;
748 } else {
749 ASN1_STRING *astr = NULL;
750 WPACKET pkt;
751 unsigned char *str = NULL;
752 size_t str_sz = 0;
753 int i;
754
755 for (i = 0; i < 2; i++) {
756 switch (i) {
757 case 0:
758 if (!WPACKET_init_null_der(&pkt))
759 goto err;
760 break;
761 case 1:
762 if ((str = OPENSSL_malloc(str_sz)) == NULL
763 || !WPACKET_init_der(&pkt, str, str_sz)) {
764 goto err;
765 }
766 break;
767 }
a55b00bd 768 if (!ossl_DER_w_RSASSA_PSS_params(&pkt, -1, pss)
8ae40cf5
RL
769 || !WPACKET_finish(&pkt)
770 || !WPACKET_get_total_written(&pkt, &str_sz))
771 goto err;
772 WPACKET_cleanup(&pkt);
773
774 /*
775 * If no PSS parameters are going to be written, there's no
776 * point going for another iteration.
777 * This saves us from getting |str| allocated just to have it
778 * immediately de-allocated.
779 */
780 if (str_sz == 0)
781 break;
782 }
783
784 if ((astr = ASN1_STRING_new()) == NULL)
785 goto err;
786 *pstrtype = V_ASN1_SEQUENCE;
787 ASN1_STRING_set0(astr, str, (int)str_sz);
788 *pstr = astr;
789
790 return 1;
791 err:
792 OPENSSL_free(str);
793 return 0;
794 }
795 }
796
797 /* Currently unsupported RSA key type */
798 return 0;
799}
800
c319b627
RL
801/*
802 * RSA is extremely simple, as PKCS#1 is used for the PKCS#8 |privateKey|
803 * field as well as the SubjectPublicKeyInfo |subjectPublicKey| field.
804 */
805#define rsa_pkcs8_priv_to_der rsa_type_specific_priv_to_der
806#define rsa_spki_pub_to_der rsa_type_specific_pub_to_der
807#define rsa_type_specific_priv_to_der (i2d_of_void *)i2d_RSAPrivateKey
808#define rsa_type_specific_pub_to_der (i2d_of_void *)i2d_RSAPublicKey
809#define rsa_type_specific_params_to_der NULL
111dc4b0
RL
810
811static int rsa_check_key_type(const void *rsa, int expected_type)
812{
813 switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) {
814 case RSA_FLAG_TYPE_RSA:
815 return expected_type == EVP_PKEY_RSA;
816 case RSA_FLAG_TYPE_RSASSAPSS:
817 return expected_type == EVP_PKEY_RSA_PSS;
818 }
819
820 /* Currently unsupported RSA key type */
821 return EVP_PKEY_NONE;
822}
823
824#define rsa_evp_type EVP_PKEY_RSA
825#define rsapss_evp_type EVP_PKEY_RSA_PSS
826#define rsa_input_type "RSA"
827#define rsapss_input_type "RSA-PSS"
c319b627
RL
828#define rsa_pem_type "RSA"
829#define rsapss_pem_type "RSA-PSS"
8ae40cf5
RL
830
831/* ---------------------------------------------------------------------- */
832
833static OSSL_FUNC_decoder_newctx_fn key2any_newctx;
834static OSSL_FUNC_decoder_freectx_fn key2any_freectx;
835
836static void *key2any_newctx(void *provctx)
837{
838 struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
839
840 if (ctx != NULL)
841 ctx->provctx = provctx;
842
843 return ctx;
844}
845
846static void key2any_freectx(void *vctx)
847{
848 struct key2any_ctx_st *ctx = vctx;
849
850 ossl_pw_clear_passphrase_data(&ctx->pwdata);
851 EVP_CIPHER_free(ctx->cipher);
852 OPENSSL_free(ctx);
853}
854
c319b627 855static const OSSL_PARAM *key2any_gettable_params(void *provctx, int structure)
111dc4b0
RL
856{
857 static const OSSL_PARAM gettables[] = {
c319b627
RL
858 { OSSL_ENCODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
859 { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
860 OSSL_PARAM_END,
861 };
862
863 static const OSSL_PARAM gettables_w_structure[] = {
864 { OSSL_ENCODER_PARAM_INPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
111dc4b0 865 { OSSL_ENCODER_PARAM_OUTPUT_TYPE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
c319b627 866 { OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE, OSSL_PARAM_UTF8_PTR, NULL, 0, 0 },
111dc4b0
RL
867 OSSL_PARAM_END,
868 };
869
c319b627 870 return structure ? gettables_w_structure : gettables;
111dc4b0
RL
871}
872
873static int key2any_get_params(OSSL_PARAM params[], const char *input_type,
c319b627
RL
874 const char *output_type,
875 const char *output_struct)
111dc4b0
RL
876{
877 OSSL_PARAM *p;
878
879 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_INPUT_TYPE);
880 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, input_type))
881 return 0;
882
883 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_TYPE);
884 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, output_type))
885 return 0;
886
c319b627
RL
887 if (output_struct != NULL) {
888 p = OSSL_PARAM_locate(params, OSSL_ENCODER_PARAM_OUTPUT_STRUCTURE);
889 if (p != NULL && !OSSL_PARAM_set_utf8_ptr(p, output_struct))
890 return 0;
891 }
892
111dc4b0
RL
893 return 1;
894}
895
8ae40cf5
RL
896static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx)
897{
898 static const OSSL_PARAM settables[] = {
899 OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0),
900 OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0),
901 OSSL_PARAM_END,
902 };
903
904 return settables;
905}
906
907static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[])
908{
909 struct key2any_ctx_st *ctx = vctx;
a829b735 910 OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx);
8ae40cf5
RL
911 const OSSL_PARAM *cipherp =
912 OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER);
913 const OSSL_PARAM *propsp =
914 OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES);
915
916 if (cipherp != NULL) {
917 const char *ciphername = NULL;
918 const char *props = NULL;
919
920 if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername))
921 return 0;
922 if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props))
923 return 0;
924
925 EVP_CIPHER_free(ctx->cipher);
c319b627 926 ctx->cipher = NULL;
8ae40cf5
RL
927 ctx->cipher_intent = ciphername != NULL;
928 if (ciphername != NULL
929 && ((ctx->cipher =
930 EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL))
931 return 0;
932 }
933 return 1;
934}
935
c319b627
RL
936static int key2any_check_selection(int selection, int selection_mask)
937{
938 /*
939 * The selections are kinda sorta "levels", i.e. each selection given
940 * here is assumed to include those following.
941 */
942 int checks[] = {
943 OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
944 OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
945 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
946 };
947 size_t i;
948
949 /* The decoder implementations made here support guessing */
950 if (selection == 0)
951 return 1;
952
953 for (i = 0; i < OSSL_NELEM(checks); i++) {
954 int check1 = (selection & checks[i]) != 0;
955 int check2 = (selection_mask & checks[i]) != 0;
956
957 /*
958 * If the caller asked for the currently checked bit(s), return
959 * whether the decoder description says it's supported.
960 */
961 if (check1)
962 return check2;
963 }
964
965 /* This should be dead code, but just to be safe... */
966 return 0;
967}
968
111dc4b0 969static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout,
c319b627 970 const void *key, int type, const char *pemname,
111dc4b0
RL
971 check_key_type_fn *checker,
972 key_to_der_fn *writer,
c319b627 973 OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg,
8ae40cf5
RL
974 key_to_paramstring_fn *key2paramstring,
975 i2d_of_void *key2der)
976{
8ae40cf5
RL
977 int ret = 0;
978
111dc4b0
RL
979 if (key == NULL) {
980 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER);
c319b627
RL
981 } else if (writer != NULL
982 && (checker == NULL || checker(key, type))) {
111dc4b0
RL
983 BIO *out = bio_new_from_core_bio(ctx->provctx, cout);
984
985 if (out != NULL
c319b627
RL
986 && (pwcb == NULL
987 || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pwcb, pwcbarg)))
988 ret =
989 writer(out, key, type, pemname, key2paramstring, key2der, ctx);
8ae40cf5 990
111dc4b0
RL
991 BIO_free(out);
992 } else {
993 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
994 }
8ae40cf5
RL
995 return ret;
996}
997
c319b627
RL
998#define DO_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY
999#define DO_PRIVATE_KEY(impl, type, kind, output) \
1000 if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \
1001 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1002 impl##_pem_type " PRIVATE KEY", \
1003 type##_check_key_type, \
1004 key_to_##kind##_##output##_priv_bio, \
1005 cb, cbarg, prepare_##type##_params, \
1006 type##_##kind##_priv_to_der);
1007
1008#define DO_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY
1009#define DO_PUBLIC_KEY(impl, type, kind, output) \
1010 if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \
1011 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1012 impl##_pem_type " PUBLIC KEY", \
1013 type##_check_key_type, \
1014 key_to_##kind##_##output##_pub_bio, \
1015 cb, cbarg, prepare_##type##_params, \
1016 type##_##kind##_pub_to_der);
1017
1018#define DO_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
1019#define DO_PARAMETERS(impl, type, kind, output) \
1020 if ((selection & DO_PARAMETERS_selection_mask) != 0) \
1021 return key2any_encode(ctx, cout, key, impl##_evp_type, \
1022 impl##_pem_type " PARAMETERS", \
1023 type##_check_key_type, \
1024 key_to_##kind##_##output##_param_bio, \
1025 NULL, NULL, NULL, \
1026 type##_##kind##_params_to_der);
1027
1028/*-
1029 * Implement the kinds of output structure that can be produced. They are
1030 * referred to by name, and for each name, the following macros are defined
1031 * (braces not included):
1032 *
1033 * {kind}_output_structure
1034 *
1035 * A string that names the output structure. This is used as a selection
1036 * criterion for each implementation. It may be NULL, which means that
1037 * there is only one possible output structure for the implemented output
1038 * type.
1039 *
1040 * DO_{kind}_selection_mask
1041 *
1042 * A mask of selection bits that must not be zero. This is used as a
1043 * selection criterion for each implementation.
1044 * This mask must never be zero.
1045 *
1046 * DO_{kind}
1047 *
1048 * The performing macro. It must use the DO_ macros defined above,
1049 * always in this order:
1050 *
1051 * - DO_PRIVATE_KEY
1052 * - DO_PUBLIC_KEY
1053 * - DO_PARAMETERS
1054 *
1055 * Any of those may be omitted, but the relative order must still be
1056 * the same.
1057 */
8ae40cf5 1058
c319b627
RL
1059/* PKCS#8 is a structure for private keys only */
1060#define PKCS8_output_structure "pkcs8"
1061#define DO_PKCS8_selection_mask DO_PRIVATE_KEY_selection_mask
1062#define DO_PKCS8(impl, type, output) \
1063 DO_PRIVATE_KEY(impl, type, pkcs8, output)
111dc4b0 1064
c319b627
RL
1065/* SubjectPublicKeyInfo is a structure for public keys only */
1066#define SubjectPublicKeyInfo_output_structure "SubjectPublicKeyInfo"
1067#define DO_SubjectPublicKeyInfo_selection_mask DO_PUBLIC_KEY_selection_mask
1068#define DO_SubjectPublicKeyInfo(impl, type, output) \
1069 DO_PUBLIC_KEY(impl, type, spki, output)
8ae40cf5 1070
c319b627
RL
1071/*
1072 * "type-specific" is a uniform name for key type specific output for private
1073 * and public keys as well as key parameters. This is used internally in
1074 * libcrypto so it doesn't have to have special knowledge about select key
1075 * types, but also when no better name has been found. If there are more
1076 * expressive DO_ names above, those are preferred.
1077 *
1078 * Three forms exist:
1079 *
1080 * - type_specific_keypair Only supports private and public key
1081 * - type_specific_params Only supports parameters
1082 * - type_specific Supports all parts of an EVP_PKEY
1083 * - type_specific_no_pub Supports all parts of an EVP_PKEY
1084 * except public key
1085 */
1086#define type_specific_params_output_structure "type-specific"
1087#define DO_type_specific_params_selection_mask DO_PARAMETERS_selection_mask
1088#define DO_type_specific_params(impl, type, output) \
1089 DO_PARAMETERS(impl, type, type_specific, output)
1090#define type_specific_keypair_output_structure "type-specific"
1091#define DO_type_specific_keypair_selection_mask \
1092 ( DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask )
1093#define DO_type_specific_keypair(impl, type, output) \
1094 DO_PRIVATE_KEY(impl, type, type_specific, output) \
1095 DO_PUBLIC_KEY(impl, type, type_specific, output)
1096#define type_specific_output_structure "type-specific"
1097#define DO_type_specific_selection_mask \
1098 ( DO_type_specific_keypair_selection_mask \
1099 | DO_type_specific_params_selection_mask )
1100#define DO_type_specific(impl, type, output) \
1101 DO_type_specific_keypair(impl, type, output) \
1102 DO_type_specific_params(impl, type, output)
1103#define type_specific_no_pub_output_structure "type-specific"
1104#define DO_type_specific_no_pub_selection_mask \
1105 ( DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask)
1106#define DO_type_specific_no_pub(impl, type, output) \
1107 DO_PRIVATE_KEY(impl, type, type_specific, output) \
1108 DO_type_specific_params(impl, type, output)
8ae40cf5 1109
c319b627
RL
1110/*
1111 * Type specific aliases for the cases where we need to refer to them by
1112 * type name.
1113 * This only covers key types that are represented with i2d_{TYPE}PrivateKey,
1114 * i2d_{TYPE}PublicKey and i2d_{TYPE}params / i2d_{TYPE}Parameters.
1115 */
1116#define RSA_output_structure "rsa"
1117#define DO_RSA_selection_mask DO_type_specific_keypair_selection_mask
1118#define DO_RSA(impl, type, output) DO_type_specific_keypair(impl, type, output)
1119
1120#define DH_output_structure "dh"
1121#define DO_DH_selection_mask DO_type_specific_params_selection_mask
1122#define DO_DH(impl, type, output) DO_type_specific_params(impl, type, output)
1123
1124#define DHX_output_structure "dhx"
1125#define DO_DHX_selection_mask DO_type_specific_params_selection_mask
1126#define DO_DHX(impl, type, output) DO_type_specific_params(impl, type, output)
1127
1128#define DSA_output_structure "dsa"
1129#define DO_DSA_selection_mask DO_type_specific_selection_mask
1130#define DO_DSA(impl, type, output) DO_type_specific(impl, type, output)
1131
1132#define EC_output_structure "ec"
1133#define DO_EC_selection_mask DO_type_specific_selection_mask
1134#define DO_EC(impl, type, output) DO_type_specific(impl, type, output)
1135
1136/* PKCS#1 defines a structure for RSA private and public keys */
1137#define PKCS1_output_structure "pkcs1"
1138#define DO_PKCS1_selection_mask DO_RSA_selection_mask
1139#define DO_PKCS1(impl, type, output) DO_RSA(impl, type, output)
1140
1141/* PKCS#3 defines a structure for DH parameters */
1142#define PKCS3_output_structure "pkcs3"
1143#define DO_PKCS3_selection_mask DO_DH_selection_mask
1144#define DO_PKCS3(impl, type, output) DO_DH(impl, type, output)
1145/* X9.42 defines a structure for DHx parameters */
1146#define X9_42_output_structure "X9.42"
1147#define DO_X9_42_selection_mask DO_DHX_selection_mask
1148#define DO_X9_42(impl, type, output) DO_DHX(impl, type, output)
1149
1150/* X9.62 defines a structure for EC keys and parameters */
1151#define X9_62_output_structure "X9.62"
1152#define DO_X9_62_selection_mask DO_EC_selection_mask
1153#define DO_X9_62(impl, type, output) DO_EC(impl, type, output)
8ae40cf5 1154
c319b627
RL
1155/*
1156 * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables.
1157 * It takes the following arguments:
1158 *
1159 * impl This is the key type name that's being implemented.
1160 * type This is the type name for the set of functions that implement
1161 * the key type. For example, ed25519, ed448, x25519 and x448
1162 * are all implemented with the exact same set of functions.
1163 * evp_type The corresponding EVP_PKEY_xxx type macro for each key.
1164 * Necessary because we currently use EVP_PKEY with legacy
1165 * native keys internally. This will need to be refactored
1166 * when that legacy support goes away.
1167 * kind What kind of support to implement. These translate into
1168 * the DO_##kind macros above.
1169 * output The output type to implement. may be der or pem.
1170 *
1171 * The resulting OSSL_DISPATCH array gets the following name (expressed in
1172 * C preprocessor terms) from those arguments:
1173 *
1174 * ossl_##impl##_to_##kind##_##output##_encoder_functions
1175 */
1176#define MAKE_ENCODER(impl, type, evp_type, kind, output) \
1177 static OSSL_FUNC_encoder_gettable_params_fn \
1178 impl##_to_##kind##_##output##_gettable_params; \
111dc4b0 1179 static OSSL_FUNC_encoder_get_params_fn \
c319b627 1180 impl##_to_##kind##_##output##_get_params; \
111dc4b0 1181 static OSSL_FUNC_encoder_import_object_fn \
c319b627 1182 impl##_to_##kind##_##output##_import_object; \
111dc4b0 1183 static OSSL_FUNC_encoder_free_object_fn \
c319b627
RL
1184 impl##_to_##kind##_##output##_free_object; \
1185 static OSSL_FUNC_encoder_encode_fn \
1186 impl##_to_##kind##_##output##_encode; \
111dc4b0 1187 \
c319b627
RL
1188 static const OSSL_PARAM * \
1189 impl##_to_##kind##_##output##_gettable_params(void *provctx) \
1190 { \
1191 return key2any_gettable_params(provctx, \
1192 kind##_output_structure != NULL); \
1193 } \
1194 static int \
1195 impl##_to_##kind##_##output##_get_params(OSSL_PARAM params[]) \
111dc4b0
RL
1196 { \
1197 return key2any_get_params(params, impl##_input_type, \
c319b627
RL
1198 output##_output_type, \
1199 kind##_output_structure); \
111dc4b0
RL
1200 } \
1201 static void * \
c319b627
RL
1202 impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \
1203 const OSSL_PARAM params[]) \
111dc4b0
RL
1204 { \
1205 struct key2any_ctx_st *ctx = vctx; \
c319b627 1206 \
1be63951 1207 return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \
111dc4b0
RL
1208 ctx->provctx, selection, params); \
1209 } \
c319b627 1210 static void impl##_to_##kind##_##output##_free_object(void *key) \
111dc4b0 1211 { \
1be63951 1212 ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \
111dc4b0 1213 } \
c319b627
RL
1214 static int impl##_to_##kind##_##output##_does_selection(void *ctx, \
1215 int selection) \
1216 { \
1217 return key2any_check_selection(selection, \
1218 DO_##kind##_selection_mask); \
1219 } \
111dc4b0 1220 static int \
c319b627
RL
1221 impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \
1222 const void *key, \
1223 const OSSL_PARAM key_abstract[], \
1224 int selection, \
1225 OSSL_PASSPHRASE_CALLBACK *cb, \
1226 void *cbarg) \
111dc4b0
RL
1227 { \
1228 /* We don't deal with abstract objects */ \
1229 if (key_abstract != NULL) { \
1230 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
1231 return 0; \
1232 } \
c319b627 1233 DO_##kind(impl, type, output) \
111dc4b0
RL
1234 \
1235 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \
1236 return 0; \
1237 } \
c319b627
RL
1238 const OSSL_DISPATCH \
1239 ossl_##impl##_to_##kind##_##output##_encoder_functions[] = { \
111dc4b0
RL
1240 { OSSL_FUNC_ENCODER_NEWCTX, \
1241 (void (*)(void))key2any_newctx }, \
1242 { OSSL_FUNC_ENCODER_FREECTX, \
1243 (void (*)(void))key2any_freectx }, \
1244 { OSSL_FUNC_ENCODER_GETTABLE_PARAMS, \
c319b627 1245 (void (*)(void))impl##_to_##kind##_##output##_gettable_params }, \
111dc4b0 1246 { OSSL_FUNC_ENCODER_GET_PARAMS, \
c319b627 1247 (void (*)(void))impl##_to_##kind##_##output##_get_params }, \
111dc4b0
RL
1248 { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
1249 (void (*)(void))key2any_settable_ctx_params }, \
1250 { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
1251 (void (*)(void))key2any_set_ctx_params }, \
c319b627
RL
1252 { OSSL_FUNC_ENCODER_DOES_SELECTION, \
1253 (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \
111dc4b0 1254 { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
c319b627 1255 (void (*)(void))impl##_to_##kind##_##output##_import_object }, \
111dc4b0 1256 { OSSL_FUNC_ENCODER_FREE_OBJECT, \
c319b627 1257 (void (*)(void))impl##_to_##kind##_##output##_free_object }, \
111dc4b0 1258 { OSSL_FUNC_ENCODER_ENCODE, \
c319b627 1259 (void (*)(void))impl##_to_##kind##_##output##_encode }, \
111dc4b0 1260 { 0, NULL } \
8ae40cf5
RL
1261 }
1262
c319b627
RL
1263/*
1264 * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey,
1265 * i2d_{TYPE}params, as they exist.
1266 */
1267MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, der);
8ae40cf5 1268#ifndef OPENSSL_NO_DH
c319b627
RL
1269MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, der);
1270MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, der);
8ae40cf5
RL
1271#endif
1272#ifndef OPENSSL_NO_DSA
c319b627
RL
1273MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, der);
1274#endif
1275#ifndef OPENSSL_NO_EC
1276MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, der);
1277#endif
1278
1279/*
1280 * Replacements for PEM_write_bio_{TYPE}PrivateKey,
1281 * PEM_write_bio_{TYPE}PublicKey, PEM_write_bio_{TYPE}params, as they exist.
1282 */
1283MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, type_specific_keypair, pem);
1284#ifndef OPENSSL_NO_DH
1285MAKE_ENCODER(dh, dh, EVP_PKEY_DH, type_specific_params, pem);
1286MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, type_specific_params, pem);
1287#endif
1288#ifndef OPENSSL_NO_DSA
1289MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, type_specific, pem);
1290#endif
1291#ifndef OPENSSL_NO_EC
1292MAKE_ENCODER(ec, ec, EVP_PKEY_EC, type_specific_no_pub, pem);
1293#endif
1294
1295/*
1296 * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the
1297 * implementations specified above, but are more specific.
1298 * The SubjectPublicKeyInfo implementations also replace the
1299 * PEM_write_bio_{TYPE}_PUBKEY functions.
1300 * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(),
1301 * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters().
1302 */
1303MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS8, der);
1304MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS8, pem);
1305MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, der);
1306MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, SubjectPublicKeyInfo, pem);
1307MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS8, der);
1308MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS8, pem);
1309MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, der);
1310MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, SubjectPublicKeyInfo, pem);
1311#ifndef OPENSSL_NO_DH
1312MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS8, der);
1313MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS8, pem);
1314MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, der);
1315MAKE_ENCODER(dh, dh, EVP_PKEY_DH, SubjectPublicKeyInfo, pem);
1316MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PKCS8, der);
1317MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, PKCS8, pem);
1318MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, der);
1319MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, SubjectPublicKeyInfo, pem);
1320#endif
1321#ifndef OPENSSL_NO_DSA
1322MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PKCS8, der);
1323MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, PKCS8, pem);
1324MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, der);
1325MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, SubjectPublicKeyInfo, pem);
1326#endif
1327#ifndef OPENSSL_NO_EC
1328MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PKCS8, der);
1329MAKE_ENCODER(ec, ec, EVP_PKEY_EC, PKCS8, pem);
1330MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, der);
1331MAKE_ENCODER(ec, ec, EVP_PKEY_EC, SubjectPublicKeyInfo, pem);
1332MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PKCS8, der);
1333MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, PKCS8, pem);
1334MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, der);
1335MAKE_ENCODER(ed25519, ecx, EVP_PKEY_ED25519, SubjectPublicKeyInfo, pem);
1336MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PKCS8, der);
1337MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, PKCS8, pem);
1338MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
1339MAKE_ENCODER(ed448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
1340MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PKCS8, der);
1341MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, PKCS8, pem);
1342MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, der);
1343MAKE_ENCODER(x25519, ecx, EVP_PKEY_X25519, SubjectPublicKeyInfo, pem);
1344MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PKCS8, der);
1345MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, PKCS8, pem);
1346MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, der);
1347MAKE_ENCODER(x448, ecx, EVP_PKEY_ED448, SubjectPublicKeyInfo, pem);
1348#endif
1349
1350/*
1351 * Support for key type specific output formats. Not all key types have
1352 * this, we only aim to duplicate what is available in 1.1.1 as
1353 * i2d_TYPEPrivateKey(), i2d_TYPEPublicKey() and i2d_TYPEparams().
1354 * For example, there are no publicly available i2d_ function for
1355 * ED25519, ED448, X25519 or X448, and they therefore only have PKCS#8
1356 * and SubjectPublicKeyInfo implementations as implemented above.
1357 */
1358MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, der);
1359MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, RSA, pem);
1360#ifndef OPENSSL_NO_DH
1361MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, der);
1362MAKE_ENCODER(dh, dh, EVP_PKEY_DH, DH, pem);
1363MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, der);
1364MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, DHX, pem);
1365#endif
1366#ifndef OPENSSL_NO_DSA
1367MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, der);
1368MAKE_ENCODER(dsa, dsa, EVP_PKEY_DSA, DSA, pem);
1369#endif
1370#ifndef OPENSSL_NO_EC
1371MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, der);
1372MAKE_ENCODER(ec, ec, EVP_PKEY_EC, EC, pem);
1373#endif
1374
1375/* Convenience structure names */
1376MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, der);
1377MAKE_ENCODER(rsa, rsa, EVP_PKEY_RSA, PKCS1, pem);
1378MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, der);
1379MAKE_ENCODER(rsapss, rsa, EVP_PKEY_RSA_PSS, PKCS1, pem);
1380#ifndef OPENSSL_NO_DH
1381MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, der); /* parameters only */
1382MAKE_ENCODER(dh, dh, EVP_PKEY_DH, PKCS3, pem); /* parameters only */
1383MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, der); /* parameters only */
1384MAKE_ENCODER(dhx, dh, EVP_PKEY_DHX, X9_42, pem); /* parameters only */
8ae40cf5
RL
1385#endif
1386#ifndef OPENSSL_NO_EC
c319b627
RL
1387MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, der);
1388MAKE_ENCODER(ec, ec, EVP_PKEY_EC, X9_62, pem);
8ae40cf5 1389#endif