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