]>
Commit | Line | Data |
---|---|---|
8ae40cf5 | 1 | /* |
0c679f55 | 2 | * Copyright 2020-2025 The OpenSSL Project Authors. All Rights Reserved. |
8ae40cf5 RL |
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 | ||
318994a1 | 15 | #include <openssl/byteorder.h> |
8ae40cf5 RL |
16 | #include <openssl/core.h> |
17 | #include <openssl/core_dispatch.h> | |
18 | #include <openssl/core_names.h> | |
19 | #include <openssl/crypto.h> | |
20 | #include <openssl/params.h> | |
21 | #include <openssl/asn1.h> | |
22 | #include <openssl/err.h> | |
23 | #include <openssl/pem.h> | |
24 | #include <openssl/x509.h> | |
25 | #include <openssl/pkcs12.h> /* PKCS8_encrypt() */ | |
26 | #include <openssl/dh.h> | |
27 | #include <openssl/dsa.h> | |
28 | #include <openssl/ec.h> | |
2741128e | 29 | #include <openssl/proverr.h> |
8ae40cf5 RL |
30 | #include "internal/passphrase.h" |
31 | #include "internal/cryptlib.h" | |
32 | #include "crypto/ecx.h" | |
b818a998 | 33 | #include "crypto/ml_kem.h" |
8ae40cf5 | 34 | #include "crypto/rsa.h" |
df231a88 | 35 | #include "crypto/ml_dsa.h" |
a25bcde2 | 36 | #include "crypto/slh_dsa.h" |
8ae40cf5 | 37 | #include "prov/implementations.h" |
8ae40cf5 RL |
38 | #include "prov/bio.h" |
39 | #include "prov/provider_ctx.h" | |
40 | #include "prov/der_rsa.h" | |
41 | #include "endecoder_local.h" | |
5421423e | 42 | #include "ml_dsa_codecs.h" |
5b2d996f | 43 | #include "ml_kem_codecs.h" |
8ae40cf5 | 44 | |
a2e145f8 RL |
45 | #if defined(OPENSSL_NO_DH) && defined(OPENSSL_NO_DSA) && defined(OPENSSL_NO_EC) |
46 | # define OPENSSL_NO_KEYPARAMS | |
47 | #endif | |
48 | ||
35f6e7ea | 49 | typedef struct key2any_ctx_st { |
8ae40cf5 RL |
50 | PROV_CTX *provctx; |
51 | ||
78043fe8 TM |
52 | /* Set to 0 if parameters should not be saved (dsa only) */ |
53 | int save_parameters; | |
54 | ||
8ae40cf5 RL |
55 | /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */ |
56 | int cipher_intent; | |
57 | ||
58 | EVP_CIPHER *cipher; | |
59 | ||
60 | struct ossl_passphrase_data_st pwdata; | |
35f6e7ea | 61 | } KEY2ANY_CTX; |
8ae40cf5 | 62 | |
111dc4b0 | 63 | typedef int check_key_type_fn(const void *key, int nid); |
78043fe8 | 64 | typedef int key_to_paramstring_fn(const void *key, int nid, int save, |
8ae40cf5 | 65 | void **str, int *strtype); |
c319b627 RL |
66 | typedef int key_to_der_fn(BIO *out, const void *key, |
67 | int key_nid, const char *pemname, | |
35f6e7ea VD |
68 | key_to_paramstring_fn *p2s, |
69 | OSSL_i2d_of_void_ctx *k2d, KEY2ANY_CTX *ctx); | |
8ae40cf5 RL |
70 | typedef int write_bio_of_void_fn(BIO *bp, const void *x); |
71 | ||
576892d7 SL |
72 | |
73 | /* Free the blob allocated during key_to_paramstring_fn */ | |
74 | static void free_asn1_data(int type, void *data) | |
75 | { | |
1287dabd | 76 | switch (type) { |
576892d7 SL |
77 | case V_ASN1_OBJECT: |
78 | ASN1_OBJECT_free(data); | |
79 | break; | |
80 | case V_ASN1_SEQUENCE: | |
81 | ASN1_STRING_free(data); | |
82 | break; | |
83 | } | |
84 | } | |
85 | ||
8ae40cf5 RL |
86 | static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid, |
87 | void *params, int params_type, | |
35f6e7ea VD |
88 | OSSL_i2d_of_void_ctx *k2d, |
89 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
90 | { |
91 | /* der, derlen store the key DER output and its length */ | |
92 | unsigned char *der = NULL; | |
93 | int derlen; | |
94 | /* The final PKCS#8 info */ | |
95 | PKCS8_PRIV_KEY_INFO *p8info = NULL; | |
96 | ||
8ae40cf5 | 97 | if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL |
35f6e7ea | 98 | || (derlen = k2d(key, &der, (void *)ctx)) <= 0 |
8ae40cf5 RL |
99 | || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0, |
100 | params_type, params, der, derlen)) { | |
e077455e | 101 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
102 | PKCS8_PRIV_KEY_INFO_free(p8info); |
103 | OPENSSL_free(der); | |
104 | p8info = NULL; | |
105 | } | |
106 | ||
107 | return p8info; | |
108 | } | |
109 | ||
110 | static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info, | |
35f6e7ea | 111 | KEY2ANY_CTX *ctx) |
8ae40cf5 RL |
112 | { |
113 | X509_SIG *p8 = NULL; | |
114 | char kstr[PEM_BUFSIZE]; | |
115 | size_t klen = 0; | |
169eca60 | 116 | OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx); |
8ae40cf5 RL |
117 | |
118 | if (ctx->cipher == NULL) | |
119 | return NULL; | |
120 | ||
121 | if (!ossl_pw_get_passphrase(kstr, sizeof(kstr), &klen, NULL, 1, | |
122 | &ctx->pwdata)) { | |
f5f29796 | 123 | ERR_raise(ERR_LIB_PROV, PROV_R_UNABLE_TO_GET_PASSPHRASE); |
8ae40cf5 RL |
124 | return NULL; |
125 | } | |
126 | /* First argument == -1 means "standard" */ | |
6f9683d6 TM |
127 | p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, (int)klen, NULL, 0, 0, p8info, |
128 | libctx, NULL); | |
8ae40cf5 RL |
129 | OPENSSL_cleanse(kstr, klen); |
130 | return p8; | |
131 | } | |
132 | ||
133 | static X509_SIG *key_to_encp8(const void *key, int key_nid, | |
134 | void *params, int params_type, | |
35f6e7ea VD |
135 | OSSL_i2d_of_void_ctx *k2d, |
136 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
137 | { |
138 | PKCS8_PRIV_KEY_INFO *p8info = | |
35f6e7ea | 139 | key_to_p8info(key, key_nid, params, params_type, k2d, ctx); |
6a2b8ff3 | 140 | X509_SIG *p8 = NULL; |
8ae40cf5 | 141 | |
6a2b8ff3 | 142 | if (p8info == NULL) { |
576892d7 | 143 | free_asn1_data(params_type, params); |
6a2b8ff3 RL |
144 | } else { |
145 | p8 = p8info_to_encp8(p8info, ctx); | |
146 | PKCS8_PRIV_KEY_INFO_free(p8info); | |
147 | } | |
8ae40cf5 RL |
148 | return p8; |
149 | } | |
150 | ||
151 | static X509_PUBKEY *key_to_pubkey(const void *key, int key_nid, | |
152 | void *params, int params_type, | |
35f6e7ea VD |
153 | OSSL_i2d_of_void_ctx *k2d, |
154 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
155 | { |
156 | /* der, derlen store the key DER output and its length */ | |
157 | unsigned char *der = NULL; | |
158 | int derlen; | |
159 | /* The final X509_PUBKEY */ | |
160 | X509_PUBKEY *xpk = NULL; | |
161 | ||
162 | ||
163 | if ((xpk = X509_PUBKEY_new()) == NULL | |
35f6e7ea | 164 | || (derlen = k2d(key, &der, (void *)ctx)) <= 0 |
8ae40cf5 RL |
165 | || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid), |
166 | params_type, params, der, derlen)) { | |
e077455e | 167 | ERR_raise(ERR_LIB_PROV, ERR_R_X509_LIB); |
8ae40cf5 RL |
168 | X509_PUBKEY_free(xpk); |
169 | OPENSSL_free(der); | |
170 | xpk = NULL; | |
171 | } | |
172 | ||
173 | return xpk; | |
174 | } | |
175 | ||
c319b627 | 176 | /* |
6a2b8ff3 RL |
177 | * key_to_epki_* produce encoded output with the private key data in a |
178 | * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require | |
179 | * that there's an intent to encrypt, anything else is an error. | |
6a2b8ff3 | 180 | * |
e304aa87 | 181 | * key_to_pki_* primarily produce encoded output with the private key data |
6a2b8ff3 RL |
182 | * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if |
183 | * there is an intent to encrypt the data, the corresponding key_to_epki_* | |
184 | * function is used instead. | |
185 | * | |
186 | * key_to_spki_* produce encoded output with the public key data in an | |
187 | * X.509 SubjectPublicKeyInfo. | |
188 | * | |
189 | * Key parameters don't have any defined envelopment of this kind, but are | |
190 | * included in some manner in the output from the functions described above, | |
191 | * either in the AlgorithmIdentifier's parameter field, or as part of the | |
192 | * key data itself. | |
c319b627 | 193 | */ |
6a2b8ff3 RL |
194 | |
195 | static int key_to_epki_der_priv_bio(BIO *out, const void *key, | |
196 | int key_nid, | |
197 | ossl_unused const char *pemname, | |
198 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
199 | OSSL_i2d_of_void_ctx *k2d, |
200 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
201 | { |
202 | int ret = 0; | |
203 | void *str = NULL; | |
204 | int strtype = V_ASN1_UNDEF; | |
6a2b8ff3 RL |
205 | X509_SIG *p8; |
206 | ||
207 | if (!ctx->cipher_intent) | |
208 | return 0; | |
8ae40cf5 | 209 | |
78043fe8 TM |
210 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, |
211 | &str, &strtype)) | |
8ae40cf5 RL |
212 | return 0; |
213 | ||
6a2b8ff3 RL |
214 | p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); |
215 | if (p8 != NULL) | |
216 | ret = i2d_PKCS8_bio(out, p8); | |
8ae40cf5 | 217 | |
6a2b8ff3 | 218 | X509_SIG_free(p8); |
8ae40cf5 | 219 | |
6a2b8ff3 RL |
220 | return ret; |
221 | } | |
8ae40cf5 | 222 | |
6a2b8ff3 RL |
223 | static int key_to_epki_pem_priv_bio(BIO *out, const void *key, |
224 | int key_nid, | |
225 | ossl_unused const char *pemname, | |
226 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
227 | OSSL_i2d_of_void_ctx *k2d, |
228 | KEY2ANY_CTX *ctx) | |
6a2b8ff3 RL |
229 | { |
230 | int ret = 0; | |
231 | void *str = NULL; | |
232 | int strtype = V_ASN1_UNDEF; | |
233 | X509_SIG *p8; | |
8ae40cf5 | 234 | |
6a2b8ff3 RL |
235 | if (!ctx->cipher_intent) |
236 | return 0; | |
237 | ||
238 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, | |
239 | &str, &strtype)) | |
240 | return 0; | |
241 | ||
242 | p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx); | |
243 | if (p8 != NULL) | |
244 | ret = PEM_write_bio_PKCS8(out, p8); | |
245 | ||
246 | X509_SIG_free(p8); | |
8ae40cf5 RL |
247 | |
248 | return ret; | |
249 | } | |
250 | ||
6a2b8ff3 RL |
251 | static int key_to_pki_der_priv_bio(BIO *out, const void *key, |
252 | int key_nid, | |
253 | ossl_unused const char *pemname, | |
254 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
255 | OSSL_i2d_of_void_ctx *k2d, |
256 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
257 | { |
258 | int ret = 0; | |
259 | void *str = NULL; | |
260 | int strtype = V_ASN1_UNDEF; | |
6a2b8ff3 RL |
261 | PKCS8_PRIV_KEY_INFO *p8info; |
262 | ||
263 | if (ctx->cipher_intent) | |
264 | return key_to_epki_der_priv_bio(out, key, key_nid, pemname, | |
265 | p2s, k2d, ctx); | |
8ae40cf5 | 266 | |
78043fe8 TM |
267 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, |
268 | &str, &strtype)) | |
8ae40cf5 RL |
269 | return 0; |
270 | ||
35f6e7ea | 271 | p8info = key_to_p8info(key, key_nid, str, strtype, k2d, ctx); |
8ae40cf5 | 272 | |
6a2b8ff3 RL |
273 | if (p8info != NULL) |
274 | ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info); | |
275 | else | |
276 | free_asn1_data(strtype, str); | |
8ae40cf5 | 277 | |
6a2b8ff3 | 278 | PKCS8_PRIV_KEY_INFO_free(p8info); |
8ae40cf5 | 279 | |
6a2b8ff3 RL |
280 | return ret; |
281 | } | |
8ae40cf5 | 282 | |
6a2b8ff3 RL |
283 | static int key_to_pki_pem_priv_bio(BIO *out, const void *key, |
284 | int key_nid, | |
285 | ossl_unused const char *pemname, | |
286 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
287 | OSSL_i2d_of_void_ctx *k2d, |
288 | KEY2ANY_CTX *ctx) | |
6a2b8ff3 RL |
289 | { |
290 | int ret = 0; | |
291 | void *str = NULL; | |
292 | int strtype = V_ASN1_UNDEF; | |
293 | PKCS8_PRIV_KEY_INFO *p8info; | |
294 | ||
295 | if (ctx->cipher_intent) | |
296 | return key_to_epki_pem_priv_bio(out, key, key_nid, pemname, | |
297 | p2s, k2d, ctx); | |
298 | ||
299 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, | |
300 | &str, &strtype)) | |
301 | return 0; | |
302 | ||
35f6e7ea | 303 | p8info = key_to_p8info(key, key_nid, str, strtype, k2d, ctx); |
6a2b8ff3 RL |
304 | |
305 | if (p8info != NULL) | |
306 | ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info); | |
307 | else | |
308 | free_asn1_data(strtype, str); | |
309 | ||
310 | PKCS8_PRIV_KEY_INFO_free(p8info); | |
8ae40cf5 RL |
311 | |
312 | return ret; | |
313 | } | |
314 | ||
c319b627 RL |
315 | static int key_to_spki_der_pub_bio(BIO *out, const void *key, |
316 | int key_nid, | |
317 | ossl_unused const char *pemname, | |
318 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
319 | OSSL_i2d_of_void_ctx *k2d, |
320 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
321 | { |
322 | int ret = 0; | |
323 | void *str = NULL; | |
324 | int strtype = V_ASN1_UNDEF; | |
325 | X509_PUBKEY *xpk = NULL; | |
326 | ||
78043fe8 TM |
327 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, |
328 | &str, &strtype)) | |
8ae40cf5 RL |
329 | return 0; |
330 | ||
35f6e7ea | 331 | xpk = key_to_pubkey(key, key_nid, str, strtype, k2d, ctx); |
8ae40cf5 RL |
332 | |
333 | if (xpk != NULL) | |
334 | ret = i2d_X509_PUBKEY_bio(out, xpk); | |
335 | ||
336 | /* Also frees |str| */ | |
337 | X509_PUBKEY_free(xpk); | |
338 | return ret; | |
339 | } | |
340 | ||
c319b627 RL |
341 | static int key_to_spki_pem_pub_bio(BIO *out, const void *key, |
342 | int key_nid, | |
343 | ossl_unused const char *pemname, | |
344 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
345 | OSSL_i2d_of_void_ctx *k2d, |
346 | KEY2ANY_CTX *ctx) | |
8ae40cf5 RL |
347 | { |
348 | int ret = 0; | |
349 | void *str = NULL; | |
350 | int strtype = V_ASN1_UNDEF; | |
351 | X509_PUBKEY *xpk = NULL; | |
352 | ||
78043fe8 TM |
353 | if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters, |
354 | &str, &strtype)) | |
8ae40cf5 RL |
355 | return 0; |
356 | ||
35f6e7ea | 357 | xpk = key_to_pubkey(key, key_nid, str, strtype, k2d, ctx); |
8ae40cf5 RL |
358 | |
359 | if (xpk != NULL) | |
360 | ret = PEM_write_bio_X509_PUBKEY(out, xpk); | |
576892d7 SL |
361 | else |
362 | free_asn1_data(strtype, str); | |
8ae40cf5 RL |
363 | |
364 | /* Also frees |str| */ | |
365 | X509_PUBKEY_free(xpk); | |
366 | return ret; | |
367 | } | |
368 | ||
c319b627 RL |
369 | /* |
370 | * key_to_type_specific_* produce encoded output with type specific key data, | |
371 | * no envelopment; the same kind of output as the type specific i2d_ and | |
372 | * PEM_write_ functions, which is often a simple SEQUENCE of INTEGER. | |
373 | * | |
374 | * OpenSSL tries to discourage production of new keys in this form, because | |
375 | * of the ambiguity when trying to recognise them, but can't deny that PKCS#1 | |
376 | * et al still are live standards. | |
377 | * | |
378 | * Note that these functions completely ignore p2s, and rather rely entirely | |
379 | * on k2d to do the complete work. | |
380 | */ | |
381 | static int key_to_type_specific_der_bio(BIO *out, const void *key, | |
382 | int key_nid, | |
383 | ossl_unused const char *pemname, | |
384 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
385 | OSSL_i2d_of_void_ctx *k2d, |
386 | KEY2ANY_CTX *ctx) | |
c319b627 RL |
387 | { |
388 | unsigned char *der = NULL; | |
389 | int derlen; | |
390 | int ret; | |
391 | ||
35f6e7ea | 392 | if ((derlen = k2d(key, &der, (void *)ctx)) <= 0) { |
e077455e | 393 | ERR_raise(ERR_LIB_PROV, ERR_R_PROV_LIB); |
c319b627 RL |
394 | return 0; |
395 | } | |
396 | ||
397 | ret = BIO_write(out, der, derlen); | |
398 | OPENSSL_free(der); | |
399 | return ret > 0; | |
400 | } | |
401 | #define key_to_type_specific_der_priv_bio key_to_type_specific_der_bio | |
402 | #define key_to_type_specific_der_pub_bio key_to_type_specific_der_bio | |
403 | #define key_to_type_specific_der_param_bio key_to_type_specific_der_bio | |
404 | ||
405 | static int key_to_type_specific_pem_bio_cb(BIO *out, const void *key, | |
406 | int key_nid, const char *pemname, | |
407 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
408 | OSSL_i2d_of_void_ctx *k2d, |
409 | KEY2ANY_CTX *ctx, | |
c319b627 RL |
410 | pem_password_cb *cb, void *cbarg) |
411 | { | |
35f6e7ea VD |
412 | return PEM_ASN1_write_bio_ctx(k2d, (void *)ctx, pemname, out, key, |
413 | ctx->cipher, NULL, 0, cb, cbarg) > 0; | |
c319b627 RL |
414 | } |
415 | ||
416 | static int key_to_type_specific_pem_priv_bio(BIO *out, const void *key, | |
417 | int key_nid, const char *pemname, | |
418 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
419 | OSSL_i2d_of_void_ctx *k2d, |
420 | KEY2ANY_CTX *ctx) | |
c319b627 RL |
421 | { |
422 | return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, | |
423 | p2s, k2d, ctx, | |
424 | ossl_pw_pem_password, &ctx->pwdata); | |
425 | } | |
426 | ||
427 | static int key_to_type_specific_pem_pub_bio(BIO *out, const void *key, | |
428 | int key_nid, const char *pemname, | |
429 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
430 | OSSL_i2d_of_void_ctx *k2d, |
431 | KEY2ANY_CTX *ctx) | |
c319b627 RL |
432 | { |
433 | return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, | |
434 | p2s, k2d, ctx, NULL, NULL); | |
435 | } | |
436 | ||
a2e145f8 | 437 | #ifndef OPENSSL_NO_KEYPARAMS |
c319b627 RL |
438 | static int key_to_type_specific_pem_param_bio(BIO *out, const void *key, |
439 | int key_nid, const char *pemname, | |
440 | key_to_paramstring_fn *p2s, | |
35f6e7ea VD |
441 | OSSL_i2d_of_void_ctx *k2d, |
442 | KEY2ANY_CTX *ctx) | |
c319b627 RL |
443 | { |
444 | return key_to_type_specific_pem_bio_cb(out, key, key_nid, pemname, | |
445 | p2s, k2d, ctx, NULL, NULL); | |
446 | } | |
01b77081 | 447 | #endif |
c319b627 | 448 | |
8ae40cf5 RL |
449 | /* ---------------------------------------------------------------------- */ |
450 | ||
35f6e7ea VD |
451 | #define k2d_NOCTX(n, f) \ |
452 | static int \ | |
453 | n##_k2d(const void *key, unsigned char **pder, \ | |
454 | ossl_unused void *ctx) \ | |
455 | { \ | |
456 | return f(key, pder); \ | |
457 | } | |
458 | ||
459 | /* ---------------------------------------------------------------------- */ | |
460 | ||
8ae40cf5 | 461 | #ifndef OPENSSL_NO_DH |
78043fe8 | 462 | static int prepare_dh_params(const void *dh, int nid, int save, |
8ae40cf5 RL |
463 | void **pstr, int *pstrtype) |
464 | { | |
465 | ASN1_STRING *params = ASN1_STRING_new(); | |
466 | ||
467 | if (params == NULL) { | |
e077455e | 468 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
469 | return 0; |
470 | } | |
471 | ||
472 | if (nid == EVP_PKEY_DHX) | |
473 | params->length = i2d_DHxparams(dh, ¶ms->data); | |
474 | else | |
475 | params->length = i2d_DHparams(dh, ¶ms->data); | |
476 | ||
477 | if (params->length <= 0) { | |
e077455e | 478 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
479 | ASN1_STRING_free(params); |
480 | return 0; | |
481 | } | |
482 | params->type = V_ASN1_SEQUENCE; | |
483 | ||
484 | *pstr = params; | |
485 | *pstrtype = V_ASN1_SEQUENCE; | |
486 | return 1; | |
487 | } | |
488 | ||
35f6e7ea VD |
489 | static int dh_spki_pub_to_der(const void *dh, unsigned char **pder, |
490 | ossl_unused void *ctx) | |
8ae40cf5 RL |
491 | { |
492 | const BIGNUM *bn = NULL; | |
493 | ASN1_INTEGER *pub_key = NULL; | |
494 | int ret; | |
495 | ||
496 | if ((bn = DH_get0_pub_key(dh)) == NULL) { | |
497 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); | |
498 | return 0; | |
499 | } | |
500 | if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) { | |
501 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
502 | return 0; | |
503 | } | |
504 | ||
505 | ret = i2d_ASN1_INTEGER(pub_key, pder); | |
506 | ||
507 | ASN1_STRING_clear_free(pub_key); | |
508 | return ret; | |
509 | } | |
510 | ||
35f6e7ea VD |
511 | static int dh_pki_priv_to_der(const void *dh, unsigned char **pder, |
512 | ossl_unused void *ctx) | |
8ae40cf5 RL |
513 | { |
514 | const BIGNUM *bn = NULL; | |
515 | ASN1_INTEGER *priv_key = NULL; | |
516 | int ret; | |
517 | ||
518 | if ((bn = DH_get0_priv_key(dh)) == NULL) { | |
519 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY); | |
520 | return 0; | |
521 | } | |
522 | if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) { | |
523 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
524 | return 0; | |
525 | } | |
526 | ||
527 | ret = i2d_ASN1_INTEGER(priv_key, pder); | |
528 | ||
529 | ASN1_STRING_clear_free(priv_key); | |
530 | return ret; | |
531 | } | |
532 | ||
0195cdd2 RL |
533 | # define dh_epki_priv_to_der dh_pki_priv_to_der |
534 | ||
35f6e7ea VD |
535 | static int |
536 | dh_type_specific_params_to_der(const void *dh, unsigned char **pder, | |
537 | ossl_unused void *ctx) | |
8ae40cf5 | 538 | { |
c319b627 RL |
539 | if (DH_test_flags(dh, DH_FLAG_TYPE_DHX)) |
540 | return i2d_DHxparams(dh, pder); | |
541 | return i2d_DHparams(dh, pder); | |
8ae40cf5 RL |
542 | } |
543 | ||
c319b627 RL |
544 | /* |
545 | * DH doesn't have i2d_DHPrivateKey or i2d_DHPublicKey, so we can't make | |
546 | * corresponding functions here. | |
547 | */ | |
548 | # define dh_type_specific_priv_to_der NULL | |
549 | # define dh_type_specific_pub_to_der NULL | |
111dc4b0 | 550 | |
c319b627 | 551 | static int dh_check_key_type(const void *dh, int expected_type) |
111dc4b0 RL |
552 | { |
553 | int type = | |
c319b627 | 554 | DH_test_flags(dh, DH_FLAG_TYPE_DHX) ? EVP_PKEY_DHX : EVP_PKEY_DH; |
111dc4b0 RL |
555 | |
556 | return type == expected_type; | |
557 | } | |
558 | ||
559 | # define dh_evp_type EVP_PKEY_DH | |
560 | # define dhx_evp_type EVP_PKEY_DHX | |
c319b627 RL |
561 | # define dh_pem_type "DH" |
562 | # define dhx_pem_type "X9.42 DH" | |
8ae40cf5 RL |
563 | #endif |
564 | ||
565 | /* ---------------------------------------------------------------------- */ | |
566 | ||
567 | #ifndef OPENSSL_NO_DSA | |
78043fe8 TM |
568 | static int encode_dsa_params(const void *dsa, int nid, |
569 | void **pstr, int *pstrtype) | |
8ae40cf5 RL |
570 | { |
571 | ASN1_STRING *params = ASN1_STRING_new(); | |
572 | ||
573 | if (params == NULL) { | |
e077455e | 574 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
575 | return 0; |
576 | } | |
577 | ||
578 | params->length = i2d_DSAparams(dsa, ¶ms->data); | |
579 | ||
580 | if (params->length <= 0) { | |
e077455e | 581 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
582 | ASN1_STRING_free(params); |
583 | return 0; | |
584 | } | |
585 | ||
586 | *pstrtype = V_ASN1_SEQUENCE; | |
587 | *pstr = params; | |
588 | return 1; | |
589 | } | |
590 | ||
78043fe8 TM |
591 | static int prepare_dsa_params(const void *dsa, int nid, int save, |
592 | void **pstr, int *pstrtype) | |
8ae40cf5 RL |
593 | { |
594 | const BIGNUM *p = DSA_get0_p(dsa); | |
595 | const BIGNUM *q = DSA_get0_q(dsa); | |
596 | const BIGNUM *g = DSA_get0_g(dsa); | |
597 | ||
78043fe8 TM |
598 | if (save && p != NULL && q != NULL && g != NULL) |
599 | return encode_dsa_params(dsa, nid, pstr, pstrtype); | |
8ae40cf5 RL |
600 | |
601 | *pstr = NULL; | |
602 | *pstrtype = V_ASN1_UNDEF; | |
603 | return 1; | |
604 | } | |
605 | ||
35f6e7ea VD |
606 | static int dsa_spki_pub_to_der(const void *dsa, unsigned char **pder, |
607 | ossl_unused void *ctx) | |
8ae40cf5 RL |
608 | { |
609 | const BIGNUM *bn = NULL; | |
610 | ASN1_INTEGER *pub_key = NULL; | |
611 | int ret; | |
612 | ||
613 | if ((bn = DSA_get0_pub_key(dsa)) == NULL) { | |
614 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); | |
615 | return 0; | |
616 | } | |
617 | if ((pub_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) { | |
618 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
619 | return 0; | |
620 | } | |
621 | ||
622 | ret = i2d_ASN1_INTEGER(pub_key, pder); | |
623 | ||
624 | ASN1_STRING_clear_free(pub_key); | |
625 | return ret; | |
626 | } | |
627 | ||
35f6e7ea VD |
628 | static int dsa_pki_priv_to_der(const void *dsa, unsigned char **pder, |
629 | ossl_unused void *ctx) | |
8ae40cf5 RL |
630 | { |
631 | const BIGNUM *bn = NULL; | |
632 | ASN1_INTEGER *priv_key = NULL; | |
633 | int ret; | |
634 | ||
635 | if ((bn = DSA_get0_priv_key(dsa)) == NULL) { | |
636 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PRIVATE_KEY); | |
637 | return 0; | |
638 | } | |
639 | if ((priv_key = BN_to_ASN1_INTEGER(bn, NULL)) == NULL) { | |
640 | ERR_raise(ERR_LIB_PROV, PROV_R_BN_ERROR); | |
641 | return 0; | |
642 | } | |
643 | ||
644 | ret = i2d_ASN1_INTEGER(priv_key, pder); | |
645 | ||
646 | ASN1_STRING_clear_free(priv_key); | |
647 | return ret; | |
648 | } | |
649 | ||
35f6e7ea VD |
650 | k2d_NOCTX(dsa_prv, i2d_DSAPrivateKey) |
651 | k2d_NOCTX(dsa_pub, i2d_DSAPublicKey) | |
652 | k2d_NOCTX(dsa_param, i2d_DSAparams) | |
653 | ||
0195cdd2 RL |
654 | # define dsa_epki_priv_to_der dsa_pki_priv_to_der |
655 | ||
35f6e7ea VD |
656 | # define dsa_type_specific_priv_to_der dsa_prv_k2d |
657 | # define dsa_type_specific_pub_to_der dsa_pub_k2d | |
658 | # define dsa_type_specific_params_to_der dsa_param_k2d | |
111dc4b0 RL |
659 | |
660 | # define dsa_check_key_type NULL | |
661 | # define dsa_evp_type EVP_PKEY_DSA | |
c319b627 | 662 | # define dsa_pem_type "DSA" |
8ae40cf5 RL |
663 | #endif |
664 | ||
665 | /* ---------------------------------------------------------------------- */ | |
666 | ||
667 | #ifndef OPENSSL_NO_EC | |
8ae40cf5 RL |
668 | static int prepare_ec_explicit_params(const void *eckey, |
669 | void **pstr, int *pstrtype) | |
670 | { | |
671 | ASN1_STRING *params = ASN1_STRING_new(); | |
672 | ||
673 | if (params == NULL) { | |
e077455e | 674 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
675 | return 0; |
676 | } | |
677 | ||
678 | params->length = i2d_ECParameters(eckey, ¶ms->data); | |
679 | if (params->length <= 0) { | |
e077455e | 680 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
681 | ASN1_STRING_free(params); |
682 | return 0; | |
683 | } | |
684 | ||
685 | *pstrtype = V_ASN1_SEQUENCE; | |
686 | *pstr = params; | |
687 | return 1; | |
688 | } | |
689 | ||
c319b627 RL |
690 | /* |
691 | * This implements EcpkParameters, where the CHOICE is based on whether there | |
692 | * is a curve name (curve nid) to be found or not. See RFC 3279 for details. | |
c319b627 | 693 | */ |
78043fe8 | 694 | static int prepare_ec_params(const void *eckey, int nid, int save, |
8ae40cf5 RL |
695 | void **pstr, int *pstrtype) |
696 | { | |
697 | int curve_nid; | |
698 | const EC_GROUP *group = EC_KEY_get0_group(eckey); | |
699 | ASN1_OBJECT *params = NULL; | |
700 | ||
701 | if (group == NULL) | |
702 | return 0; | |
703 | curve_nid = EC_GROUP_get_curve_name(group); | |
704 | if (curve_nid != NID_undef) { | |
705 | params = OBJ_nid2obj(curve_nid); | |
706 | if (params == NULL) | |
707 | return 0; | |
708 | } | |
709 | ||
710 | if (curve_nid != NID_undef | |
711 | && (EC_GROUP_get_asn1_flag(group) & OPENSSL_EC_NAMED_CURVE)) { | |
c319b627 | 712 | /* The CHOICE came to namedCurve */ |
8ae40cf5 RL |
713 | if (OBJ_length(params) == 0) { |
714 | /* Some curves might not have an associated OID */ | |
715 | ERR_raise(ERR_LIB_PROV, PROV_R_MISSING_OID); | |
716 | ASN1_OBJECT_free(params); | |
717 | return 0; | |
718 | } | |
719 | *pstr = params; | |
720 | *pstrtype = V_ASN1_OBJECT; | |
721 | return 1; | |
722 | } else { | |
c319b627 | 723 | /* The CHOICE came to ecParameters */ |
8ae40cf5 RL |
724 | return prepare_ec_explicit_params(eckey, pstr, pstrtype); |
725 | } | |
726 | } | |
727 | ||
35f6e7ea VD |
728 | static int ec_spki_pub_to_der(const void *eckey, unsigned char **pder, |
729 | ossl_unused void *ctx) | |
8ae40cf5 | 730 | { |
6187d9ea MC |
731 | if (EC_KEY_get0_public_key(eckey) == NULL) { |
732 | ERR_raise(ERR_LIB_PROV, PROV_R_NOT_A_PUBLIC_KEY); | |
733 | return 0; | |
734 | } | |
8ae40cf5 RL |
735 | return i2o_ECPublicKey(eckey, pder); |
736 | } | |
737 | ||
35f6e7ea VD |
738 | static int ec_pki_priv_to_der(const void *veckey, unsigned char **pder, |
739 | ossl_unused void *ctx) | |
8ae40cf5 RL |
740 | { |
741 | EC_KEY *eckey = (EC_KEY *)veckey; | |
742 | unsigned int old_flags; | |
743 | int ret = 0; | |
744 | ||
745 | /* | |
746 | * For PKCS8 the curve name appears in the PKCS8_PRIV_KEY_INFO object | |
747 | * as the pkeyalg->parameter field. (For a named curve this is an OID) | |
748 | * The pkey field is an octet string that holds the encoded | |
749 | * ECPrivateKey SEQUENCE with the optional parameters field omitted. | |
750 | * We omit this by setting the EC_PKEY_NO_PARAMETERS flag. | |
751 | */ | |
752 | old_flags = EC_KEY_get_enc_flags(eckey); /* save old flags */ | |
753 | EC_KEY_set_enc_flags(eckey, old_flags | EC_PKEY_NO_PARAMETERS); | |
754 | ret = i2d_ECPrivateKey(eckey, pder); | |
755 | EC_KEY_set_enc_flags(eckey, old_flags); /* restore old flags */ | |
756 | return ret; /* return the length of the der encoded data */ | |
757 | } | |
111dc4b0 | 758 | |
35f6e7ea VD |
759 | k2d_NOCTX(ec_param, i2d_ECParameters) |
760 | k2d_NOCTX(ec_prv, i2d_ECPrivateKey) | |
761 | ||
0195cdd2 RL |
762 | # define ec_epki_priv_to_der ec_pki_priv_to_der |
763 | ||
35f6e7ea | 764 | # define ec_type_specific_params_to_der ec_param_k2d |
2d495192 | 765 | /* No ec_type_specific_pub_to_der, there simply is no such thing */ |
35f6e7ea | 766 | # define ec_type_specific_priv_to_der ec_prv_k2d |
c319b627 | 767 | |
111dc4b0 RL |
768 | # define ec_check_key_type NULL |
769 | # define ec_evp_type EVP_PKEY_EC | |
c319b627 | 770 | # define ec_pem_type "EC" |
f2db0528 RL |
771 | |
772 | # ifndef OPENSSL_NO_SM2 | |
1d490694 RL |
773 | /* |
774 | * Albeit SM2 is a slightly different algorithm than ECDSA, the key type | |
775 | * encoding (in all places where an AlgorithmIdentifier is produced, such | |
776 | * as PrivateKeyInfo and SubjectPublicKeyInfo) is the same as for ECC keys | |
777 | * according to the example in GM/T 0015-2012, appendix D.2. | |
778 | * This leaves the distinction of SM2 keys to the EC group (which is found | |
779 | * in AlgorithmIdentified.params). | |
780 | */ | |
781 | # define sm2_evp_type ec_evp_type | |
f2db0528 RL |
782 | # define sm2_pem_type "SM2" |
783 | # endif | |
8ae40cf5 RL |
784 | #endif |
785 | ||
786 | /* ---------------------------------------------------------------------- */ | |
787 | ||
4032cd9a | 788 | #ifndef OPENSSL_NO_ECX |
8ae40cf5 RL |
789 | # define prepare_ecx_params NULL |
790 | ||
35f6e7ea VD |
791 | static int ecx_spki_pub_to_der(const void *vecxkey, unsigned char **pder, |
792 | ossl_unused void *ctx) | |
8ae40cf5 RL |
793 | { |
794 | const ECX_KEY *ecxkey = vecxkey; | |
795 | unsigned char *keyblob; | |
796 | ||
797 | if (ecxkey == NULL) { | |
798 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); | |
799 | return 0; | |
800 | } | |
801 | ||
802 | keyblob = OPENSSL_memdup(ecxkey->pubkey, ecxkey->keylen); | |
e077455e | 803 | if (keyblob == NULL) |
8ae40cf5 | 804 | return 0; |
8ae40cf5 RL |
805 | |
806 | *pder = keyblob; | |
6f9683d6 | 807 | return (int)ecxkey->keylen; |
8ae40cf5 RL |
808 | } |
809 | ||
35f6e7ea VD |
810 | static int ecx_pki_priv_to_der(const void *vecxkey, unsigned char **pder, |
811 | ossl_unused void *ctx) | |
8ae40cf5 RL |
812 | { |
813 | const ECX_KEY *ecxkey = vecxkey; | |
814 | ASN1_OCTET_STRING oct; | |
815 | int keybloblen; | |
816 | ||
817 | if (ecxkey == NULL || ecxkey->privkey == NULL) { | |
818 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); | |
819 | return 0; | |
820 | } | |
821 | ||
822 | oct.data = ecxkey->privkey; | |
6f9683d6 | 823 | oct.length = (int)ecxkey->keylen; |
8ae40cf5 RL |
824 | oct.flags = 0; |
825 | ||
826 | keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder); | |
827 | if (keybloblen < 0) { | |
e077455e | 828 | ERR_raise(ERR_LIB_PROV, ERR_R_ASN1_LIB); |
8ae40cf5 RL |
829 | return 0; |
830 | } | |
831 | ||
832 | return keybloblen; | |
833 | } | |
834 | ||
0195cdd2 RL |
835 | # define ecx_epki_priv_to_der ecx_pki_priv_to_der |
836 | ||
c319b627 RL |
837 | /* |
838 | * ED25519, ED448, X25519 and X448 only has PKCS#8 / SubjectPublicKeyInfo | |
839 | * representation, so we don't define ecx_type_specific_[priv,pub,params]_to_der. | |
840 | */ | |
841 | ||
111dc4b0 RL |
842 | # define ecx_check_key_type NULL |
843 | ||
844 | # define ed25519_evp_type EVP_PKEY_ED25519 | |
845 | # define ed448_evp_type EVP_PKEY_ED448 | |
846 | # define x25519_evp_type EVP_PKEY_X25519 | |
847 | # define x448_evp_type EVP_PKEY_X448 | |
c319b627 RL |
848 | # define ed25519_pem_type "ED25519" |
849 | # define ed448_pem_type "ED448" | |
850 | # define x25519_pem_type "X25519" | |
851 | # define x448_pem_type "X448" | |
8ae40cf5 RL |
852 | #endif |
853 | ||
854 | /* ---------------------------------------------------------------------- */ | |
855 | ||
df231a88 | 856 | #ifndef OPENSSL_NO_ML_DSA |
c83e6c0a | 857 | static int ml_dsa_spki_pub_to_der(const void *vkey, unsigned char **pder, |
858 | ossl_unused void *ctx) | |
df231a88 | 859 | { |
5421423e | 860 | return ossl_ml_dsa_i2d_pubkey(vkey, pder); |
df231a88 | 861 | } |
862 | ||
c83e6c0a | 863 | static int ml_dsa_pki_priv_to_der(const void *vkey, unsigned char **pder, |
5421423e | 864 | void *vctx) |
df231a88 | 865 | { |
5421423e | 866 | KEY2ANY_CTX *ctx = vctx; |
df231a88 | 867 | |
5421423e | 868 | return ossl_ml_dsa_i2d_prvkey(vkey, pder, ctx->provctx); |
df231a88 | 869 | } |
870 | ||
871 | # define ml_dsa_epki_priv_to_der ml_dsa_pki_priv_to_der | |
872 | # define prepare_ml_dsa_params NULL | |
873 | # define ml_dsa_check_key_type NULL | |
874 | ||
875 | # define ml_dsa_44_evp_type EVP_PKEY_ML_DSA_44 | |
876 | # define ml_dsa_44_pem_type "ML-DSA-44" | |
877 | # define ml_dsa_65_evp_type EVP_PKEY_ML_DSA_65 | |
878 | # define ml_dsa_65_pem_type "ML-DSA-65" | |
879 | # define ml_dsa_87_evp_type EVP_PKEY_ML_DSA_87 | |
880 | # define ml_dsa_87_pem_type "ML-DSA-87" | |
881 | #endif /* OPENSSL_NO_ML_DSA */ | |
882 | ||
883 | /* ---------------------------------------------------------------------- */ | |
884 | ||
b818a998 VD |
885 | #ifndef OPENSSL_NO_ML_KEM |
886 | ||
887 | static int ml_kem_spki_pub_to_der(const void *vkey, unsigned char **pder, | |
888 | ossl_unused void *ctx) | |
889 | { | |
318994a1 | 890 | return ossl_ml_kem_i2d_pubkey(vkey, pder); |
b818a998 VD |
891 | } |
892 | ||
893 | static int ml_kem_pki_priv_to_der(const void *vkey, unsigned char **pder, | |
318994a1 | 894 | void *vctx) |
b818a998 | 895 | { |
318994a1 | 896 | KEY2ANY_CTX *ctx = vctx; |
b818a998 | 897 | |
5b2d996f | 898 | return ossl_ml_kem_i2d_prvkey(vkey, pder, ctx->provctx); |
b818a998 VD |
899 | } |
900 | ||
901 | # define ml_kem_epki_priv_to_der ml_kem_pki_priv_to_der | |
902 | # define prepare_ml_kem_params NULL | |
903 | # define ml_kem_check_key_type NULL | |
904 | ||
905 | # define ml_kem_512_evp_type EVP_PKEY_ML_KEM_512 | |
906 | # define ml_kem_512_pem_type "ML-KEM-512" | |
907 | # define ml_kem_768_evp_type EVP_PKEY_ML_KEM_768 | |
908 | # define ml_kem_768_pem_type "ML-KEM-768" | |
909 | # define ml_kem_1024_evp_type EVP_PKEY_ML_KEM_1024 | |
910 | # define ml_kem_1024_pem_type "ML-KEM-1024" | |
911 | #endif | |
912 | ||
913 | /* ---------------------------------------------------------------------- */ | |
914 | ||
8ae40cf5 RL |
915 | /* |
916 | * Helper functions to prepare RSA-PSS params for encoding. We would | |
917 | * have simply written the whole AlgorithmIdentifier, but existing libcrypto | |
918 | * functionality doesn't allow that. | |
919 | */ | |
920 | ||
78043fe8 | 921 | static int prepare_rsa_params(const void *rsa, int nid, int save, |
8ae40cf5 RL |
922 | void **pstr, int *pstrtype) |
923 | { | |
23b2fc0b | 924 | const RSA_PSS_PARAMS_30 *pss = ossl_rsa_get0_pss_params_30((RSA *)rsa); |
8ae40cf5 RL |
925 | |
926 | *pstr = NULL; | |
927 | ||
928 | switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) { | |
929 | case RSA_FLAG_TYPE_RSA: | |
930 | /* If plain RSA, the parameters shall be NULL */ | |
931 | *pstrtype = V_ASN1_NULL; | |
932 | return 1; | |
933 | case RSA_FLAG_TYPE_RSASSAPSS: | |
23b2fc0b | 934 | if (ossl_rsa_pss_params_30_is_unrestricted(pss)) { |
8ae40cf5 RL |
935 | *pstrtype = V_ASN1_UNDEF; |
936 | return 1; | |
937 | } else { | |
938 | ASN1_STRING *astr = NULL; | |
939 | WPACKET pkt; | |
940 | unsigned char *str = NULL; | |
941 | size_t str_sz = 0; | |
942 | int i; | |
943 | ||
944 | for (i = 0; i < 2; i++) { | |
945 | switch (i) { | |
946 | case 0: | |
947 | if (!WPACKET_init_null_der(&pkt)) | |
948 | goto err; | |
949 | break; | |
950 | case 1: | |
951 | if ((str = OPENSSL_malloc(str_sz)) == NULL | |
952 | || !WPACKET_init_der(&pkt, str, str_sz)) { | |
46def829 | 953 | WPACKET_cleanup(&pkt); |
8ae40cf5 RL |
954 | goto err; |
955 | } | |
956 | break; | |
957 | } | |
a55b00bd | 958 | if (!ossl_DER_w_RSASSA_PSS_params(&pkt, -1, pss) |
8ae40cf5 | 959 | || !WPACKET_finish(&pkt) |
46def829 BE |
960 | || !WPACKET_get_total_written(&pkt, &str_sz)) { |
961 | WPACKET_cleanup(&pkt); | |
8ae40cf5 | 962 | goto err; |
46def829 | 963 | } |
8ae40cf5 RL |
964 | WPACKET_cleanup(&pkt); |
965 | ||
966 | /* | |
967 | * If no PSS parameters are going to be written, there's no | |
968 | * point going for another iteration. | |
969 | * This saves us from getting |str| allocated just to have it | |
970 | * immediately de-allocated. | |
971 | */ | |
972 | if (str_sz == 0) | |
973 | break; | |
974 | } | |
975 | ||
976 | if ((astr = ASN1_STRING_new()) == NULL) | |
977 | goto err; | |
978 | *pstrtype = V_ASN1_SEQUENCE; | |
979 | ASN1_STRING_set0(astr, str, (int)str_sz); | |
980 | *pstr = astr; | |
981 | ||
982 | return 1; | |
983 | err: | |
984 | OPENSSL_free(str); | |
985 | return 0; | |
986 | } | |
987 | } | |
988 | ||
989 | /* Currently unsupported RSA key type */ | |
990 | return 0; | |
991 | } | |
992 | ||
35f6e7ea VD |
993 | k2d_NOCTX(rsa_prv, i2d_RSAPrivateKey) |
994 | k2d_NOCTX(rsa_pub, i2d_RSAPublicKey) | |
995 | ||
c319b627 RL |
996 | /* |
997 | * RSA is extremely simple, as PKCS#1 is used for the PKCS#8 |privateKey| | |
998 | * field as well as the SubjectPublicKeyInfo |subjectPublicKey| field. | |
999 | */ | |
6a2b8ff3 | 1000 | #define rsa_pki_priv_to_der rsa_type_specific_priv_to_der |
0195cdd2 | 1001 | #define rsa_epki_priv_to_der rsa_type_specific_priv_to_der |
c319b627 | 1002 | #define rsa_spki_pub_to_der rsa_type_specific_pub_to_der |
35f6e7ea VD |
1003 | #define rsa_type_specific_priv_to_der rsa_prv_k2d |
1004 | #define rsa_type_specific_pub_to_der rsa_pub_k2d | |
c319b627 | 1005 | #define rsa_type_specific_params_to_der NULL |
111dc4b0 RL |
1006 | |
1007 | static int rsa_check_key_type(const void *rsa, int expected_type) | |
1008 | { | |
1009 | switch (RSA_test_flags(rsa, RSA_FLAG_TYPE_MASK)) { | |
1010 | case RSA_FLAG_TYPE_RSA: | |
1011 | return expected_type == EVP_PKEY_RSA; | |
1012 | case RSA_FLAG_TYPE_RSASSAPSS: | |
1013 | return expected_type == EVP_PKEY_RSA_PSS; | |
1014 | } | |
1015 | ||
1016 | /* Currently unsupported RSA key type */ | |
1017 | return EVP_PKEY_NONE; | |
1018 | } | |
1019 | ||
1020 | #define rsa_evp_type EVP_PKEY_RSA | |
1021 | #define rsapss_evp_type EVP_PKEY_RSA_PSS | |
c319b627 RL |
1022 | #define rsa_pem_type "RSA" |
1023 | #define rsapss_pem_type "RSA-PSS" | |
8ae40cf5 RL |
1024 | |
1025 | /* ---------------------------------------------------------------------- */ | |
1026 | ||
a25bcde2 | 1027 | #ifndef OPENSSL_NO_SLH_DSA |
1028 | # define prepare_slh_dsa_params NULL | |
1029 | ||
67d52a55 | 1030 | static int slh_dsa_spki_pub_to_der(const void *vkey, unsigned char **pder, |
1031 | ossl_unused void *ctx) | |
a25bcde2 | 1032 | { |
1033 | const SLH_DSA_KEY *key = vkey; | |
1034 | uint8_t *key_blob; | |
1035 | size_t key_len; | |
1036 | ||
1037 | if (key == NULL) { | |
1038 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); | |
1039 | return 0; | |
1040 | } | |
1041 | key_len = ossl_slh_dsa_key_get_pub_len(key); | |
1042 | key_blob = OPENSSL_memdup(ossl_slh_dsa_key_get_pub(key), key_len); | |
1043 | if (key_blob == NULL) | |
1044 | return 0; | |
1045 | ||
1046 | *pder = key_blob; | |
6f9683d6 | 1047 | return (int)key_len; |
a25bcde2 | 1048 | } |
1049 | ||
67d52a55 | 1050 | static int slh_dsa_pki_priv_to_der(const void *vkey, unsigned char **pder, |
1051 | ossl_unused void *ctx) | |
a25bcde2 | 1052 | { |
1053 | const SLH_DSA_KEY *key = vkey; | |
8f86a75f | 1054 | size_t len; |
a25bcde2 | 1055 | |
8f86a75f | 1056 | if (ossl_slh_dsa_key_get_priv(key) == NULL) { |
a25bcde2 | 1057 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); |
1058 | return 0; | |
1059 | } | |
8f86a75f | 1060 | len = ossl_slh_dsa_key_get_priv_len(key); |
a25bcde2 | 1061 | |
8f86a75f | 1062 | if (pder != NULL |
1063 | && ((*pder = OPENSSL_memdup(ossl_slh_dsa_key_get_priv(key), len)) == NULL)) | |
a25bcde2 | 1064 | return 0; |
a25bcde2 | 1065 | |
6f9683d6 | 1066 | return (int)len; |
a25bcde2 | 1067 | } |
1068 | # define slh_dsa_epki_priv_to_der slh_dsa_pki_priv_to_der | |
1069 | ||
1070 | /* SLH_DSA only has PKCS#8 / SubjectPublicKeyInfo representations. */ | |
1071 | ||
1072 | # define slh_dsa_check_key_type NULL | |
1073 | # define slh_dsa_sha2_128s_evp_type EVP_PKEY_SLH_DSA_SHA2_128S | |
1074 | # define slh_dsa_sha2_128f_evp_type EVP_PKEY_SLH_DSA_SHA2_128F | |
1075 | # define slh_dsa_sha2_192s_evp_type EVP_PKEY_SLH_DSA_SHA2_192S | |
1076 | # define slh_dsa_sha2_192f_evp_type EVP_PKEY_SLH_DSA_SHA2_192F | |
1077 | # define slh_dsa_sha2_256s_evp_type EVP_PKEY_SLH_DSA_SHA2_256S | |
1078 | # define slh_dsa_sha2_256f_evp_type EVP_PKEY_SLH_DSA_SHA2_256F | |
1079 | # define slh_dsa_shake_128s_evp_type EVP_PKEY_SLH_DSA_SHAKE_128S | |
1080 | # define slh_dsa_shake_128f_evp_type EVP_PKEY_SLH_DSA_SHAKE_128F | |
1081 | # define slh_dsa_shake_192s_evp_type EVP_PKEY_SLH_DSA_SHAKE_192S | |
1082 | # define slh_dsa_shake_192f_evp_type EVP_PKEY_SLH_DSA_SHAKE_192F | |
1083 | # define slh_dsa_shake_256s_evp_type EVP_PKEY_SLH_DSA_SHAKE_256S | |
1084 | # define slh_dsa_shake_256f_evp_type EVP_PKEY_SLH_DSA_SHAKE_256F | |
1085 | # define slh_dsa_sha2_128s_input_type "SLH-DSA-SHA2-128s" | |
1086 | # define slh_dsa_sha2_128f_input_type "SLH-DSA-SHA2-128f" | |
1087 | # define slh_dsa_sha2_192s_input_type "SLH-DSA-SHA2-192s" | |
1088 | # define slh_dsa_sha2_192f_input_type "SLH-DSA-SHA2-192f" | |
1089 | # define slh_dsa_sha2_256s_input_type "SLH-DSA-SHA2-256s" | |
1090 | # define slh_dsa_sha2_256f_input_type "SLH-DSA-SHA2-256f" | |
1091 | # define slh_dsa_shake_128s_input_type "SLH-DSA-SHAKE-128s" | |
1092 | # define slh_dsa_shake_128f_input_type "SLH-DSA-SHAKE-128f" | |
1093 | # define slh_dsa_shake_192s_input_type "SLH-DSA-SHAKE-192s" | |
1094 | # define slh_dsa_shake_192f_input_type "SLH-DSA-SHAKE-192f" | |
1095 | # define slh_dsa_shake_256s_input_type "SLH-DSA-SHAKE-256s" | |
1096 | # define slh_dsa_shake_256f_input_type "SLH-DSA-SHAKE-256f" | |
1097 | # define slh_dsa_sha2_128s_pem_type "SLH-DSA-SHA2-128s" | |
1098 | # define slh_dsa_sha2_128f_pem_type "SLH-DSA-SHA2-128f" | |
1099 | # define slh_dsa_sha2_192s_pem_type "SLH-DSA-SHA2-192s" | |
1100 | # define slh_dsa_sha2_192f_pem_type "SLH-DSA-SHA2-192f" | |
1101 | # define slh_dsa_sha2_256s_pem_type "SLH-DSA-SHA2-256s" | |
1102 | # define slh_dsa_sha2_256f_pem_type "SLH-DSA-SHA2-256f" | |
1103 | # define slh_dsa_shake_128s_pem_type "SLH-DSA-SHAKE-128s" | |
1104 | # define slh_dsa_shake_128f_pem_type "SLH-DSA-SHAKE-128f" | |
1105 | # define slh_dsa_shake_192s_pem_type "SLH-DSA-SHAKE-192s" | |
1106 | # define slh_dsa_shake_192f_pem_type "SLH-DSA-SHAKE-192f" | |
1107 | # define slh_dsa_shake_256s_pem_type "SLH-DSA-SHAKE-256s" | |
1108 | # define slh_dsa_shake_256f_pem_type "SLH-DSA-SHAKE-256f" | |
8f86a75f | 1109 | #endif /* OPENSSL_NO_SLH_DSA */ |
a25bcde2 | 1110 | |
1111 | /* ---------------------------------------------------------------------- */ | |
1112 | ||
8ae40cf5 RL |
1113 | static OSSL_FUNC_decoder_newctx_fn key2any_newctx; |
1114 | static OSSL_FUNC_decoder_freectx_fn key2any_freectx; | |
1115 | ||
1116 | static void *key2any_newctx(void *provctx) | |
1117 | { | |
35f6e7ea | 1118 | KEY2ANY_CTX *ctx = OPENSSL_zalloc(sizeof(*ctx)); |
8ae40cf5 | 1119 | |
78043fe8 | 1120 | if (ctx != NULL) { |
8ae40cf5 | 1121 | ctx->provctx = provctx; |
78043fe8 TM |
1122 | ctx->save_parameters = 1; |
1123 | } | |
8ae40cf5 RL |
1124 | |
1125 | return ctx; | |
1126 | } | |
1127 | ||
1128 | static void key2any_freectx(void *vctx) | |
1129 | { | |
35f6e7ea | 1130 | KEY2ANY_CTX *ctx = vctx; |
8ae40cf5 RL |
1131 | |
1132 | ossl_pw_clear_passphrase_data(&ctx->pwdata); | |
1133 | EVP_CIPHER_free(ctx->cipher); | |
1134 | OPENSSL_free(ctx); | |
1135 | } | |
1136 | ||
1137 | static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx) | |
1138 | { | |
1139 | static const OSSL_PARAM settables[] = { | |
1140 | OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0), | |
1141 | OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0), | |
1142 | OSSL_PARAM_END, | |
1143 | }; | |
1144 | ||
1145 | return settables; | |
1146 | } | |
1147 | ||
1148 | static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[]) | |
1149 | { | |
35f6e7ea | 1150 | KEY2ANY_CTX *ctx = vctx; |
a829b735 | 1151 | OSSL_LIB_CTX *libctx = ossl_prov_ctx_get0_libctx(ctx->provctx); |
8ae40cf5 RL |
1152 | const OSSL_PARAM *cipherp = |
1153 | OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER); | |
1154 | const OSSL_PARAM *propsp = | |
1155 | OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES); | |
78043fe8 TM |
1156 | const OSSL_PARAM *save_paramsp = |
1157 | OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS); | |
8ae40cf5 RL |
1158 | |
1159 | if (cipherp != NULL) { | |
1160 | const char *ciphername = NULL; | |
1161 | const char *props = NULL; | |
1162 | ||
1163 | if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername)) | |
1164 | return 0; | |
1165 | if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props)) | |
1166 | return 0; | |
1167 | ||
1168 | EVP_CIPHER_free(ctx->cipher); | |
c319b627 | 1169 | ctx->cipher = NULL; |
8ae40cf5 RL |
1170 | ctx->cipher_intent = ciphername != NULL; |
1171 | if (ciphername != NULL | |
1172 | && ((ctx->cipher = | |
1173 | EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) | |
1174 | return 0; | |
1175 | } | |
78043fe8 TM |
1176 | |
1177 | if (save_paramsp != NULL) { | |
1178 | if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters)) | |
1179 | return 0; | |
1180 | } | |
8ae40cf5 RL |
1181 | return 1; |
1182 | } | |
1183 | ||
c319b627 RL |
1184 | static int key2any_check_selection(int selection, int selection_mask) |
1185 | { | |
1186 | /* | |
1187 | * The selections are kinda sorta "levels", i.e. each selection given | |
1188 | * here is assumed to include those following. | |
1189 | */ | |
1190 | int checks[] = { | |
1191 | OSSL_KEYMGMT_SELECT_PRIVATE_KEY, | |
1192 | OSSL_KEYMGMT_SELECT_PUBLIC_KEY, | |
1193 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | |
1194 | }; | |
1195 | size_t i; | |
1196 | ||
1197 | /* The decoder implementations made here support guessing */ | |
1198 | if (selection == 0) | |
1199 | return 1; | |
1200 | ||
1201 | for (i = 0; i < OSSL_NELEM(checks); i++) { | |
1202 | int check1 = (selection & checks[i]) != 0; | |
1203 | int check2 = (selection_mask & checks[i]) != 0; | |
1204 | ||
1205 | /* | |
1206 | * If the caller asked for the currently checked bit(s), return | |
1207 | * whether the decoder description says it's supported. | |
1208 | */ | |
1209 | if (check1) | |
1210 | return check2; | |
1211 | } | |
1212 | ||
1213 | /* This should be dead code, but just to be safe... */ | |
1214 | return 0; | |
1215 | } | |
1216 | ||
35f6e7ea | 1217 | static int key2any_encode(KEY2ANY_CTX *ctx, OSSL_CORE_BIO *cout, |
c319b627 | 1218 | const void *key, int type, const char *pemname, |
111dc4b0 RL |
1219 | check_key_type_fn *checker, |
1220 | key_to_der_fn *writer, | |
c319b627 | 1221 | OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg, |
8ae40cf5 | 1222 | key_to_paramstring_fn *key2paramstring, |
35f6e7ea | 1223 | OSSL_i2d_of_void_ctx *key2der) |
8ae40cf5 | 1224 | { |
8ae40cf5 RL |
1225 | int ret = 0; |
1226 | ||
111dc4b0 RL |
1227 | if (key == NULL) { |
1228 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_NULL_PARAMETER); | |
c319b627 RL |
1229 | } else if (writer != NULL |
1230 | && (checker == NULL || checker(key, type))) { | |
9500c823 | 1231 | BIO *out = ossl_bio_new_from_core_bio(ctx->provctx, cout); |
111dc4b0 RL |
1232 | |
1233 | if (out != NULL | |
c319b627 RL |
1234 | && (pwcb == NULL |
1235 | || ossl_pw_set_ossl_passphrase_cb(&ctx->pwdata, pwcb, pwcbarg))) | |
1236 | ret = | |
1237 | writer(out, key, type, pemname, key2paramstring, key2der, ctx); | |
8ae40cf5 | 1238 | |
111dc4b0 RL |
1239 | BIO_free(out); |
1240 | } else { | |
1241 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); | |
1242 | } | |
8ae40cf5 RL |
1243 | return ret; |
1244 | } | |
1245 | ||
c319b627 RL |
1246 | #define DO_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY |
1247 | #define DO_PRIVATE_KEY(impl, type, kind, output) \ | |
1248 | if ((selection & DO_PRIVATE_KEY_selection_mask) != 0) \ | |
1249 | return key2any_encode(ctx, cout, key, impl##_evp_type, \ | |
1250 | impl##_pem_type " PRIVATE KEY", \ | |
1251 | type##_check_key_type, \ | |
1252 | key_to_##kind##_##output##_priv_bio, \ | |
1253 | cb, cbarg, prepare_##type##_params, \ | |
1254 | type##_##kind##_priv_to_der); | |
1255 | ||
1256 | #define DO_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY | |
1257 | #define DO_PUBLIC_KEY(impl, type, kind, output) \ | |
1258 | if ((selection & DO_PUBLIC_KEY_selection_mask) != 0) \ | |
1259 | return key2any_encode(ctx, cout, key, impl##_evp_type, \ | |
1260 | impl##_pem_type " PUBLIC KEY", \ | |
1261 | type##_check_key_type, \ | |
1262 | key_to_##kind##_##output##_pub_bio, \ | |
1263 | cb, cbarg, prepare_##type##_params, \ | |
1264 | type##_##kind##_pub_to_der); | |
1265 | ||
1266 | #define DO_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS | |
1267 | #define DO_PARAMETERS(impl, type, kind, output) \ | |
1268 | if ((selection & DO_PARAMETERS_selection_mask) != 0) \ | |
1269 | return key2any_encode(ctx, cout, key, impl##_evp_type, \ | |
1270 | impl##_pem_type " PARAMETERS", \ | |
1271 | type##_check_key_type, \ | |
1272 | key_to_##kind##_##output##_param_bio, \ | |
1273 | NULL, NULL, NULL, \ | |
1274 | type##_##kind##_params_to_der); | |
1275 | ||
1276 | /*- | |
1277 | * Implement the kinds of output structure that can be produced. They are | |
1278 | * referred to by name, and for each name, the following macros are defined | |
1279 | * (braces not included): | |
1280 | * | |
c319b627 RL |
1281 | * DO_{kind}_selection_mask |
1282 | * | |
1283 | * A mask of selection bits that must not be zero. This is used as a | |
1284 | * selection criterion for each implementation. | |
1285 | * This mask must never be zero. | |
1286 | * | |
1287 | * DO_{kind} | |
1288 | * | |
1289 | * The performing macro. It must use the DO_ macros defined above, | |
1290 | * always in this order: | |
1291 | * | |
1292 | * - DO_PRIVATE_KEY | |
1293 | * - DO_PUBLIC_KEY | |
1294 | * - DO_PARAMETERS | |
1295 | * | |
1296 | * Any of those may be omitted, but the relative order must still be | |
1297 | * the same. | |
1298 | */ | |
8ae40cf5 | 1299 | |
6a2b8ff3 RL |
1300 | /* |
1301 | * PKCS#8 defines two structures for private keys only: | |
1302 | * - PrivateKeyInfo (raw unencrypted form) | |
1303 | * - EncryptedPrivateKeyInfo (encrypted wrapping) | |
1304 | * | |
1305 | * To allow a certain amount of flexibility, we allow the routines | |
1306 | * for PrivateKeyInfo to also produce EncryptedPrivateKeyInfo if a | |
1307 | * passphrase callback has been passed to them. | |
1308 | */ | |
1309 | #define DO_PrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask | |
1310 | #define DO_PrivateKeyInfo(impl, type, output) \ | |
1311 | DO_PRIVATE_KEY(impl, type, pki, output) | |
111dc4b0 | 1312 | |
0195cdd2 RL |
1313 | #define DO_EncryptedPrivateKeyInfo_selection_mask DO_PRIVATE_KEY_selection_mask |
1314 | #define DO_EncryptedPrivateKeyInfo(impl, type, output) \ | |
1315 | DO_PRIVATE_KEY(impl, type, epki, output) | |
1316 | ||
c319b627 | 1317 | /* SubjectPublicKeyInfo is a structure for public keys only */ |
c319b627 RL |
1318 | #define DO_SubjectPublicKeyInfo_selection_mask DO_PUBLIC_KEY_selection_mask |
1319 | #define DO_SubjectPublicKeyInfo(impl, type, output) \ | |
1320 | DO_PUBLIC_KEY(impl, type, spki, output) | |
8ae40cf5 | 1321 | |
c319b627 RL |
1322 | /* |
1323 | * "type-specific" is a uniform name for key type specific output for private | |
1324 | * and public keys as well as key parameters. This is used internally in | |
1325 | * libcrypto so it doesn't have to have special knowledge about select key | |
1326 | * types, but also when no better name has been found. If there are more | |
1327 | * expressive DO_ names above, those are preferred. | |
1328 | * | |
1329 | * Three forms exist: | |
1330 | * | |
1331 | * - type_specific_keypair Only supports private and public key | |
1332 | * - type_specific_params Only supports parameters | |
1333 | * - type_specific Supports all parts of an EVP_PKEY | |
1334 | * - type_specific_no_pub Supports all parts of an EVP_PKEY | |
1335 | * except public key | |
1336 | */ | |
c319b627 RL |
1337 | #define DO_type_specific_params_selection_mask DO_PARAMETERS_selection_mask |
1338 | #define DO_type_specific_params(impl, type, output) \ | |
1339 | DO_PARAMETERS(impl, type, type_specific, output) | |
c319b627 RL |
1340 | #define DO_type_specific_keypair_selection_mask \ |
1341 | ( DO_PRIVATE_KEY_selection_mask | DO_PUBLIC_KEY_selection_mask ) | |
1342 | #define DO_type_specific_keypair(impl, type, output) \ | |
1343 | DO_PRIVATE_KEY(impl, type, type_specific, output) \ | |
1344 | DO_PUBLIC_KEY(impl, type, type_specific, output) | |
c319b627 RL |
1345 | #define DO_type_specific_selection_mask \ |
1346 | ( DO_type_specific_keypair_selection_mask \ | |
1347 | | DO_type_specific_params_selection_mask ) | |
1348 | #define DO_type_specific(impl, type, output) \ | |
1349 | DO_type_specific_keypair(impl, type, output) \ | |
1350 | DO_type_specific_params(impl, type, output) | |
c319b627 RL |
1351 | #define DO_type_specific_no_pub_selection_mask \ |
1352 | ( DO_PRIVATE_KEY_selection_mask | DO_PARAMETERS_selection_mask) | |
1353 | #define DO_type_specific_no_pub(impl, type, output) \ | |
1354 | DO_PRIVATE_KEY(impl, type, type_specific, output) \ | |
1355 | DO_type_specific_params(impl, type, output) | |
8ae40cf5 | 1356 | |
c319b627 RL |
1357 | /* |
1358 | * Type specific aliases for the cases where we need to refer to them by | |
1359 | * type name. | |
1360 | * This only covers key types that are represented with i2d_{TYPE}PrivateKey, | |
1361 | * i2d_{TYPE}PublicKey and i2d_{TYPE}params / i2d_{TYPE}Parameters. | |
1362 | */ | |
c319b627 RL |
1363 | #define DO_RSA_selection_mask DO_type_specific_keypair_selection_mask |
1364 | #define DO_RSA(impl, type, output) DO_type_specific_keypair(impl, type, output) | |
1365 | ||
c319b627 RL |
1366 | #define DO_DH_selection_mask DO_type_specific_params_selection_mask |
1367 | #define DO_DH(impl, type, output) DO_type_specific_params(impl, type, output) | |
1368 | ||
c319b627 RL |
1369 | #define DO_DHX_selection_mask DO_type_specific_params_selection_mask |
1370 | #define DO_DHX(impl, type, output) DO_type_specific_params(impl, type, output) | |
1371 | ||
c319b627 RL |
1372 | #define DO_DSA_selection_mask DO_type_specific_selection_mask |
1373 | #define DO_DSA(impl, type, output) DO_type_specific(impl, type, output) | |
1374 | ||
2d495192 RL |
1375 | #define DO_EC_selection_mask DO_type_specific_no_pub_selection_mask |
1376 | #define DO_EC(impl, type, output) DO_type_specific_no_pub(impl, type, output) | |
c319b627 | 1377 | |
2d495192 RL |
1378 | #define DO_SM2_selection_mask DO_type_specific_no_pub_selection_mask |
1379 | #define DO_SM2(impl, type, output) DO_type_specific_no_pub(impl, type, output) | |
f2db0528 | 1380 | |
c319b627 | 1381 | /* PKCS#1 defines a structure for RSA private and public keys */ |
c319b627 RL |
1382 | #define DO_PKCS1_selection_mask DO_RSA_selection_mask |
1383 | #define DO_PKCS1(impl, type, output) DO_RSA(impl, type, output) | |
1384 | ||
1385 | /* PKCS#3 defines a structure for DH parameters */ | |
c319b627 RL |
1386 | #define DO_PKCS3_selection_mask DO_DH_selection_mask |
1387 | #define DO_PKCS3(impl, type, output) DO_DH(impl, type, output) | |
1388 | /* X9.42 defines a structure for DHx parameters */ | |
c319b627 RL |
1389 | #define DO_X9_42_selection_mask DO_DHX_selection_mask |
1390 | #define DO_X9_42(impl, type, output) DO_DHX(impl, type, output) | |
1391 | ||
1392 | /* X9.62 defines a structure for EC keys and parameters */ | |
c319b627 RL |
1393 | #define DO_X9_62_selection_mask DO_EC_selection_mask |
1394 | #define DO_X9_62(impl, type, output) DO_EC(impl, type, output) | |
8ae40cf5 | 1395 | |
c319b627 RL |
1396 | /* |
1397 | * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables. | |
1398 | * It takes the following arguments: | |
1399 | * | |
1400 | * impl This is the key type name that's being implemented. | |
1401 | * type This is the type name for the set of functions that implement | |
1402 | * the key type. For example, ed25519, ed448, x25519 and x448 | |
1403 | * are all implemented with the exact same set of functions. | |
c319b627 RL |
1404 | * kind What kind of support to implement. These translate into |
1405 | * the DO_##kind macros above. | |
1406 | * output The output type to implement. may be der or pem. | |
1407 | * | |
1408 | * The resulting OSSL_DISPATCH array gets the following name (expressed in | |
1409 | * C preprocessor terms) from those arguments: | |
1410 | * | |
1411 | * ossl_##impl##_to_##kind##_##output##_encoder_functions | |
1412 | */ | |
0cacf9be | 1413 | #define MAKE_ENCODER(impl, type, kind, output) \ |
111dc4b0 | 1414 | static OSSL_FUNC_encoder_import_object_fn \ |
c319b627 | 1415 | impl##_to_##kind##_##output##_import_object; \ |
111dc4b0 | 1416 | static OSSL_FUNC_encoder_free_object_fn \ |
c319b627 RL |
1417 | impl##_to_##kind##_##output##_free_object; \ |
1418 | static OSSL_FUNC_encoder_encode_fn \ | |
1419 | impl##_to_##kind##_##output##_encode; \ | |
111dc4b0 | 1420 | \ |
111dc4b0 | 1421 | static void * \ |
c319b627 RL |
1422 | impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \ |
1423 | const OSSL_PARAM params[]) \ | |
111dc4b0 | 1424 | { \ |
35f6e7ea | 1425 | KEY2ANY_CTX *ctx = vctx; \ |
c319b627 | 1426 | \ |
1be63951 | 1427 | return ossl_prov_import_key(ossl_##impl##_keymgmt_functions, \ |
111dc4b0 RL |
1428 | ctx->provctx, selection, params); \ |
1429 | } \ | |
c319b627 | 1430 | static void impl##_to_##kind##_##output##_free_object(void *key) \ |
111dc4b0 | 1431 | { \ |
1be63951 | 1432 | ossl_prov_free_key(ossl_##impl##_keymgmt_functions, key); \ |
111dc4b0 | 1433 | } \ |
c319b627 RL |
1434 | static int impl##_to_##kind##_##output##_does_selection(void *ctx, \ |
1435 | int selection) \ | |
1436 | { \ | |
1437 | return key2any_check_selection(selection, \ | |
1438 | DO_##kind##_selection_mask); \ | |
1439 | } \ | |
111dc4b0 | 1440 | static int \ |
c319b627 RL |
1441 | impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \ |
1442 | const void *key, \ | |
1443 | const OSSL_PARAM key_abstract[], \ | |
1444 | int selection, \ | |
1445 | OSSL_PASSPHRASE_CALLBACK *cb, \ | |
1446 | void *cbarg) \ | |
111dc4b0 RL |
1447 | { \ |
1448 | /* We don't deal with abstract objects */ \ | |
1449 | if (key_abstract != NULL) { \ | |
1450 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ | |
1451 | return 0; \ | |
1452 | } \ | |
c319b627 | 1453 | DO_##kind(impl, type, output) \ |
111dc4b0 RL |
1454 | \ |
1455 | ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT); \ | |
1456 | return 0; \ | |
1457 | } \ | |
c319b627 RL |
1458 | const OSSL_DISPATCH \ |
1459 | ossl_##impl##_to_##kind##_##output##_encoder_functions[] = { \ | |
111dc4b0 RL |
1460 | { OSSL_FUNC_ENCODER_NEWCTX, \ |
1461 | (void (*)(void))key2any_newctx }, \ | |
1462 | { OSSL_FUNC_ENCODER_FREECTX, \ | |
1463 | (void (*)(void))key2any_freectx }, \ | |
111dc4b0 RL |
1464 | { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \ |
1465 | (void (*)(void))key2any_settable_ctx_params }, \ | |
1466 | { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \ | |
1467 | (void (*)(void))key2any_set_ctx_params }, \ | |
c319b627 RL |
1468 | { OSSL_FUNC_ENCODER_DOES_SELECTION, \ |
1469 | (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \ | |
111dc4b0 | 1470 | { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \ |
c319b627 | 1471 | (void (*)(void))impl##_to_##kind##_##output##_import_object }, \ |
111dc4b0 | 1472 | { OSSL_FUNC_ENCODER_FREE_OBJECT, \ |
c319b627 | 1473 | (void (*)(void))impl##_to_##kind##_##output##_free_object }, \ |
111dc4b0 | 1474 | { OSSL_FUNC_ENCODER_ENCODE, \ |
c319b627 | 1475 | (void (*)(void))impl##_to_##kind##_##output##_encode }, \ |
1e6bd31e | 1476 | OSSL_DISPATCH_END \ |
8ae40cf5 RL |
1477 | } |
1478 | ||
c319b627 RL |
1479 | /* |
1480 | * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey, | |
1481 | * i2d_{TYPE}params, as they exist. | |
1482 | */ | |
0cacf9be | 1483 | MAKE_ENCODER(rsa, rsa, type_specific_keypair, der); |
8ae40cf5 | 1484 | #ifndef OPENSSL_NO_DH |
0cacf9be RL |
1485 | MAKE_ENCODER(dh, dh, type_specific_params, der); |
1486 | MAKE_ENCODER(dhx, dh, type_specific_params, der); | |
8ae40cf5 RL |
1487 | #endif |
1488 | #ifndef OPENSSL_NO_DSA | |
0cacf9be | 1489 | MAKE_ENCODER(dsa, dsa, type_specific, der); |
c319b627 RL |
1490 | #endif |
1491 | #ifndef OPENSSL_NO_EC | |
0cacf9be | 1492 | MAKE_ENCODER(ec, ec, type_specific_no_pub, der); |
f2db0528 | 1493 | # ifndef OPENSSL_NO_SM2 |
0cacf9be | 1494 | MAKE_ENCODER(sm2, ec, type_specific_no_pub, der); |
f2db0528 | 1495 | # endif |
c319b627 RL |
1496 | #endif |
1497 | ||
1498 | /* | |
1499 | * Replacements for PEM_write_bio_{TYPE}PrivateKey, | |
1500 | * PEM_write_bio_{TYPE}PublicKey, PEM_write_bio_{TYPE}params, as they exist. | |
1501 | */ | |
0cacf9be | 1502 | MAKE_ENCODER(rsa, rsa, type_specific_keypair, pem); |
c319b627 | 1503 | #ifndef OPENSSL_NO_DH |
0cacf9be RL |
1504 | MAKE_ENCODER(dh, dh, type_specific_params, pem); |
1505 | MAKE_ENCODER(dhx, dh, type_specific_params, pem); | |
c319b627 RL |
1506 | #endif |
1507 | #ifndef OPENSSL_NO_DSA | |
0cacf9be | 1508 | MAKE_ENCODER(dsa, dsa, type_specific, pem); |
c319b627 RL |
1509 | #endif |
1510 | #ifndef OPENSSL_NO_EC | |
0cacf9be | 1511 | MAKE_ENCODER(ec, ec, type_specific_no_pub, pem); |
f2db0528 | 1512 | # ifndef OPENSSL_NO_SM2 |
0cacf9be | 1513 | MAKE_ENCODER(sm2, ec, type_specific_no_pub, pem); |
f2db0528 | 1514 | # endif |
c319b627 RL |
1515 | #endif |
1516 | ||
1517 | /* | |
1518 | * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the | |
1519 | * implementations specified above, but are more specific. | |
1520 | * The SubjectPublicKeyInfo implementations also replace the | |
1521 | * PEM_write_bio_{TYPE}_PUBKEY functions. | |
1522 | * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(), | |
1523 | * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters(). | |
1524 | */ | |
0cacf9be RL |
1525 | MAKE_ENCODER(rsa, rsa, EncryptedPrivateKeyInfo, der); |
1526 | MAKE_ENCODER(rsa, rsa, EncryptedPrivateKeyInfo, pem); | |
1527 | MAKE_ENCODER(rsa, rsa, PrivateKeyInfo, der); | |
1528 | MAKE_ENCODER(rsa, rsa, PrivateKeyInfo, pem); | |
1529 | MAKE_ENCODER(rsa, rsa, SubjectPublicKeyInfo, der); | |
1530 | MAKE_ENCODER(rsa, rsa, SubjectPublicKeyInfo, pem); | |
1531 | MAKE_ENCODER(rsapss, rsa, EncryptedPrivateKeyInfo, der); | |
1532 | MAKE_ENCODER(rsapss, rsa, EncryptedPrivateKeyInfo, pem); | |
1533 | MAKE_ENCODER(rsapss, rsa, PrivateKeyInfo, der); | |
1534 | MAKE_ENCODER(rsapss, rsa, PrivateKeyInfo, pem); | |
1535 | MAKE_ENCODER(rsapss, rsa, SubjectPublicKeyInfo, der); | |
1536 | MAKE_ENCODER(rsapss, rsa, SubjectPublicKeyInfo, pem); | |
c319b627 | 1537 | #ifndef OPENSSL_NO_DH |
0cacf9be RL |
1538 | MAKE_ENCODER(dh, dh, EncryptedPrivateKeyInfo, der); |
1539 | MAKE_ENCODER(dh, dh, EncryptedPrivateKeyInfo, pem); | |
1540 | MAKE_ENCODER(dh, dh, PrivateKeyInfo, der); | |
1541 | MAKE_ENCODER(dh, dh, PrivateKeyInfo, pem); | |
1542 | MAKE_ENCODER(dh, dh, SubjectPublicKeyInfo, der); | |
1543 | MAKE_ENCODER(dh, dh, SubjectPublicKeyInfo, pem); | |
1544 | MAKE_ENCODER(dhx, dh, EncryptedPrivateKeyInfo, der); | |
1545 | MAKE_ENCODER(dhx, dh, EncryptedPrivateKeyInfo, pem); | |
1546 | MAKE_ENCODER(dhx, dh, PrivateKeyInfo, der); | |
1547 | MAKE_ENCODER(dhx, dh, PrivateKeyInfo, pem); | |
1548 | MAKE_ENCODER(dhx, dh, SubjectPublicKeyInfo, der); | |
1549 | MAKE_ENCODER(dhx, dh, SubjectPublicKeyInfo, pem); | |
c319b627 RL |
1550 | #endif |
1551 | #ifndef OPENSSL_NO_DSA | |
0cacf9be RL |
1552 | MAKE_ENCODER(dsa, dsa, EncryptedPrivateKeyInfo, der); |
1553 | MAKE_ENCODER(dsa, dsa, EncryptedPrivateKeyInfo, pem); | |
1554 | MAKE_ENCODER(dsa, dsa, PrivateKeyInfo, der); | |
1555 | MAKE_ENCODER(dsa, dsa, PrivateKeyInfo, pem); | |
1556 | MAKE_ENCODER(dsa, dsa, SubjectPublicKeyInfo, der); | |
1557 | MAKE_ENCODER(dsa, dsa, SubjectPublicKeyInfo, pem); | |
c319b627 RL |
1558 | #endif |
1559 | #ifndef OPENSSL_NO_EC | |
0cacf9be RL |
1560 | MAKE_ENCODER(ec, ec, EncryptedPrivateKeyInfo, der); |
1561 | MAKE_ENCODER(ec, ec, EncryptedPrivateKeyInfo, pem); | |
1562 | MAKE_ENCODER(ec, ec, PrivateKeyInfo, der); | |
1563 | MAKE_ENCODER(ec, ec, PrivateKeyInfo, pem); | |
1564 | MAKE_ENCODER(ec, ec, SubjectPublicKeyInfo, der); | |
1565 | MAKE_ENCODER(ec, ec, SubjectPublicKeyInfo, pem); | |
f2db0528 | 1566 | # ifndef OPENSSL_NO_SM2 |
0cacf9be RL |
1567 | MAKE_ENCODER(sm2, ec, EncryptedPrivateKeyInfo, der); |
1568 | MAKE_ENCODER(sm2, ec, EncryptedPrivateKeyInfo, pem); | |
1569 | MAKE_ENCODER(sm2, ec, PrivateKeyInfo, der); | |
1570 | MAKE_ENCODER(sm2, ec, PrivateKeyInfo, pem); | |
1571 | MAKE_ENCODER(sm2, ec, SubjectPublicKeyInfo, der); | |
1572 | MAKE_ENCODER(sm2, ec, SubjectPublicKeyInfo, pem); | |
f2db0528 | 1573 | # endif |
4032cd9a | 1574 | # ifndef OPENSSL_NO_ECX |
0cacf9be RL |
1575 | MAKE_ENCODER(ed25519, ecx, EncryptedPrivateKeyInfo, der); |
1576 | MAKE_ENCODER(ed25519, ecx, EncryptedPrivateKeyInfo, pem); | |
1577 | MAKE_ENCODER(ed25519, ecx, PrivateKeyInfo, der); | |
1578 | MAKE_ENCODER(ed25519, ecx, PrivateKeyInfo, pem); | |
1579 | MAKE_ENCODER(ed25519, ecx, SubjectPublicKeyInfo, der); | |
1580 | MAKE_ENCODER(ed25519, ecx, SubjectPublicKeyInfo, pem); | |
1581 | MAKE_ENCODER(ed448, ecx, EncryptedPrivateKeyInfo, der); | |
1582 | MAKE_ENCODER(ed448, ecx, EncryptedPrivateKeyInfo, pem); | |
1583 | MAKE_ENCODER(ed448, ecx, PrivateKeyInfo, der); | |
1584 | MAKE_ENCODER(ed448, ecx, PrivateKeyInfo, pem); | |
1585 | MAKE_ENCODER(ed448, ecx, SubjectPublicKeyInfo, der); | |
1586 | MAKE_ENCODER(ed448, ecx, SubjectPublicKeyInfo, pem); | |
1587 | MAKE_ENCODER(x25519, ecx, EncryptedPrivateKeyInfo, der); | |
1588 | MAKE_ENCODER(x25519, ecx, EncryptedPrivateKeyInfo, pem); | |
1589 | MAKE_ENCODER(x25519, ecx, PrivateKeyInfo, der); | |
1590 | MAKE_ENCODER(x25519, ecx, PrivateKeyInfo, pem); | |
1591 | MAKE_ENCODER(x25519, ecx, SubjectPublicKeyInfo, der); | |
1592 | MAKE_ENCODER(x25519, ecx, SubjectPublicKeyInfo, pem); | |
1593 | MAKE_ENCODER(x448, ecx, EncryptedPrivateKeyInfo, der); | |
1594 | MAKE_ENCODER(x448, ecx, EncryptedPrivateKeyInfo, pem); | |
1595 | MAKE_ENCODER(x448, ecx, PrivateKeyInfo, der); | |
1596 | MAKE_ENCODER(x448, ecx, PrivateKeyInfo, pem); | |
1597 | MAKE_ENCODER(x448, ecx, SubjectPublicKeyInfo, der); | |
1598 | MAKE_ENCODER(x448, ecx, SubjectPublicKeyInfo, pem); | |
4032cd9a | 1599 | # endif |
c319b627 | 1600 | #endif |
a25bcde2 | 1601 | #ifndef OPENSSL_NO_SLH_DSA |
67d52a55 | 1602 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, EncryptedPrivateKeyInfo, der); |
1603 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1604 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1605 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1606 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1607 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1608 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1609 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1610 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1611 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1612 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1613 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1614 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1615 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1616 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1617 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1618 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1619 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, EncryptedPrivateKeyInfo, der); | |
1620 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1621 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1622 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1623 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1624 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1625 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, EncryptedPrivateKeyInfo, pem); | |
1626 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, PrivateKeyInfo, der); | |
1627 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, PrivateKeyInfo, der); | |
1628 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, PrivateKeyInfo, der); | |
1629 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, PrivateKeyInfo, der); | |
1630 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, PrivateKeyInfo, der); | |
1631 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, PrivateKeyInfo, der); | |
1632 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, PrivateKeyInfo, pem); | |
1633 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, PrivateKeyInfo, pem); | |
1634 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, PrivateKeyInfo, pem); | |
1635 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, PrivateKeyInfo, pem); | |
1636 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, PrivateKeyInfo, pem); | |
1637 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, PrivateKeyInfo, pem); | |
1638 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, PrivateKeyInfo, der); | |
1639 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, PrivateKeyInfo, der); | |
1640 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, PrivateKeyInfo, der); | |
1641 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, PrivateKeyInfo, der); | |
1642 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, PrivateKeyInfo, der); | |
1643 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, PrivateKeyInfo, der); | |
1644 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, PrivateKeyInfo, pem); | |
1645 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, PrivateKeyInfo, pem); | |
1646 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, PrivateKeyInfo, pem); | |
1647 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, PrivateKeyInfo, pem); | |
1648 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, PrivateKeyInfo, pem); | |
1649 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, PrivateKeyInfo, pem); | |
1650 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, SubjectPublicKeyInfo, der); | |
1651 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, SubjectPublicKeyInfo, der); | |
1652 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, SubjectPublicKeyInfo, der); | |
1653 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, SubjectPublicKeyInfo, der); | |
1654 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, SubjectPublicKeyInfo, der); | |
1655 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, SubjectPublicKeyInfo, der); | |
1656 | MAKE_ENCODER(slh_dsa_sha2_128s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1657 | MAKE_ENCODER(slh_dsa_sha2_128f, slh_dsa, SubjectPublicKeyInfo, pem); | |
1658 | MAKE_ENCODER(slh_dsa_sha2_192s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1659 | MAKE_ENCODER(slh_dsa_sha2_192f, slh_dsa, SubjectPublicKeyInfo, pem); | |
1660 | MAKE_ENCODER(slh_dsa_sha2_256s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1661 | MAKE_ENCODER(slh_dsa_sha2_256f, slh_dsa, SubjectPublicKeyInfo, pem); | |
1662 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, SubjectPublicKeyInfo, der); | |
1663 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, SubjectPublicKeyInfo, der); | |
1664 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, SubjectPublicKeyInfo, der); | |
1665 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, SubjectPublicKeyInfo, der); | |
1666 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, SubjectPublicKeyInfo, der); | |
1667 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, SubjectPublicKeyInfo, der); | |
1668 | MAKE_ENCODER(slh_dsa_shake_128s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1669 | MAKE_ENCODER(slh_dsa_shake_128f, slh_dsa, SubjectPublicKeyInfo, pem); | |
1670 | MAKE_ENCODER(slh_dsa_shake_192s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1671 | MAKE_ENCODER(slh_dsa_shake_192f, slh_dsa, SubjectPublicKeyInfo, pem); | |
1672 | MAKE_ENCODER(slh_dsa_shake_256s, slh_dsa, SubjectPublicKeyInfo, pem); | |
1673 | MAKE_ENCODER(slh_dsa_shake_256f, slh_dsa, SubjectPublicKeyInfo, pem); | |
a25bcde2 | 1674 | #endif /* OPENSSL_NO_SLH_DSA */ |
c319b627 | 1675 | |
b818a998 VD |
1676 | #ifndef OPENSSL_NO_ML_KEM |
1677 | MAKE_ENCODER(ml_kem_512, ml_kem, EncryptedPrivateKeyInfo, der); | |
1678 | MAKE_ENCODER(ml_kem_512, ml_kem, EncryptedPrivateKeyInfo, pem); | |
1679 | MAKE_ENCODER(ml_kem_512, ml_kem, PrivateKeyInfo, der); | |
1680 | MAKE_ENCODER(ml_kem_512, ml_kem, PrivateKeyInfo, pem); | |
1681 | MAKE_ENCODER(ml_kem_512, ml_kem, SubjectPublicKeyInfo, der); | |
1682 | MAKE_ENCODER(ml_kem_512, ml_kem, SubjectPublicKeyInfo, pem); | |
1683 | ||
1684 | MAKE_ENCODER(ml_kem_768, ml_kem, EncryptedPrivateKeyInfo, der); | |
1685 | MAKE_ENCODER(ml_kem_768, ml_kem, EncryptedPrivateKeyInfo, pem); | |
1686 | MAKE_ENCODER(ml_kem_768, ml_kem, PrivateKeyInfo, der); | |
1687 | MAKE_ENCODER(ml_kem_768, ml_kem, PrivateKeyInfo, pem); | |
1688 | MAKE_ENCODER(ml_kem_768, ml_kem, SubjectPublicKeyInfo, der); | |
1689 | MAKE_ENCODER(ml_kem_768, ml_kem, SubjectPublicKeyInfo, pem); | |
1690 | ||
1691 | MAKE_ENCODER(ml_kem_1024, ml_kem, EncryptedPrivateKeyInfo, der); | |
1692 | MAKE_ENCODER(ml_kem_1024, ml_kem, EncryptedPrivateKeyInfo, pem); | |
1693 | MAKE_ENCODER(ml_kem_1024, ml_kem, PrivateKeyInfo, der); | |
1694 | MAKE_ENCODER(ml_kem_1024, ml_kem, PrivateKeyInfo, pem); | |
1695 | MAKE_ENCODER(ml_kem_1024, ml_kem, SubjectPublicKeyInfo, der); | |
1696 | MAKE_ENCODER(ml_kem_1024, ml_kem, SubjectPublicKeyInfo, pem); | |
1697 | #endif | |
1698 | ||
c319b627 RL |
1699 | /* |
1700 | * Support for key type specific output formats. Not all key types have | |
1701 | * this, we only aim to duplicate what is available in 1.1.1 as | |
1702 | * i2d_TYPEPrivateKey(), i2d_TYPEPublicKey() and i2d_TYPEparams(). | |
1703 | * For example, there are no publicly available i2d_ function for | |
1704 | * ED25519, ED448, X25519 or X448, and they therefore only have PKCS#8 | |
1705 | * and SubjectPublicKeyInfo implementations as implemented above. | |
1706 | */ | |
0cacf9be RL |
1707 | MAKE_ENCODER(rsa, rsa, RSA, der); |
1708 | MAKE_ENCODER(rsa, rsa, RSA, pem); | |
c319b627 | 1709 | #ifndef OPENSSL_NO_DH |
0cacf9be RL |
1710 | MAKE_ENCODER(dh, dh, DH, der); |
1711 | MAKE_ENCODER(dh, dh, DH, pem); | |
1712 | MAKE_ENCODER(dhx, dh, DHX, der); | |
1713 | MAKE_ENCODER(dhx, dh, DHX, pem); | |
c319b627 RL |
1714 | #endif |
1715 | #ifndef OPENSSL_NO_DSA | |
0cacf9be RL |
1716 | MAKE_ENCODER(dsa, dsa, DSA, der); |
1717 | MAKE_ENCODER(dsa, dsa, DSA, pem); | |
c319b627 RL |
1718 | #endif |
1719 | #ifndef OPENSSL_NO_EC | |
0cacf9be RL |
1720 | MAKE_ENCODER(ec, ec, EC, der); |
1721 | MAKE_ENCODER(ec, ec, EC, pem); | |
f2db0528 | 1722 | # ifndef OPENSSL_NO_SM2 |
0cacf9be RL |
1723 | MAKE_ENCODER(sm2, ec, SM2, der); |
1724 | MAKE_ENCODER(sm2, ec, SM2, pem); | |
f2db0528 | 1725 | # endif |
c319b627 RL |
1726 | #endif |
1727 | ||
1728 | /* Convenience structure names */ | |
0cacf9be RL |
1729 | MAKE_ENCODER(rsa, rsa, PKCS1, der); |
1730 | MAKE_ENCODER(rsa, rsa, PKCS1, pem); | |
1731 | MAKE_ENCODER(rsapss, rsa, PKCS1, der); | |
1732 | MAKE_ENCODER(rsapss, rsa, PKCS1, pem); | |
c319b627 | 1733 | #ifndef OPENSSL_NO_DH |
0cacf9be RL |
1734 | MAKE_ENCODER(dh, dh, PKCS3, der); /* parameters only */ |
1735 | MAKE_ENCODER(dh, dh, PKCS3, pem); /* parameters only */ | |
1736 | MAKE_ENCODER(dhx, dh, X9_42, der); /* parameters only */ | |
1737 | MAKE_ENCODER(dhx, dh, X9_42, pem); /* parameters only */ | |
8ae40cf5 RL |
1738 | #endif |
1739 | #ifndef OPENSSL_NO_EC | |
0cacf9be RL |
1740 | MAKE_ENCODER(ec, ec, X9_62, der); |
1741 | MAKE_ENCODER(ec, ec, X9_62, pem); | |
8ae40cf5 | 1742 | #endif |
df231a88 | 1743 | |
1744 | #ifndef OPENSSL_NO_ML_DSA | |
c83e6c0a | 1745 | MAKE_ENCODER(ml_dsa_44, ml_dsa, EncryptedPrivateKeyInfo, der); |
1746 | MAKE_ENCODER(ml_dsa_44, ml_dsa, EncryptedPrivateKeyInfo, pem); | |
1747 | MAKE_ENCODER(ml_dsa_44, ml_dsa, PrivateKeyInfo, der); | |
1748 | MAKE_ENCODER(ml_dsa_44, ml_dsa, PrivateKeyInfo, pem); | |
1749 | MAKE_ENCODER(ml_dsa_44, ml_dsa, SubjectPublicKeyInfo, der); | |
1750 | MAKE_ENCODER(ml_dsa_44, ml_dsa, SubjectPublicKeyInfo, pem); | |
1751 | ||
1752 | MAKE_ENCODER(ml_dsa_65, ml_dsa, EncryptedPrivateKeyInfo, der); | |
1753 | MAKE_ENCODER(ml_dsa_65, ml_dsa, EncryptedPrivateKeyInfo, pem); | |
1754 | MAKE_ENCODER(ml_dsa_65, ml_dsa, PrivateKeyInfo, der); | |
1755 | MAKE_ENCODER(ml_dsa_65, ml_dsa, PrivateKeyInfo, pem); | |
1756 | MAKE_ENCODER(ml_dsa_65, ml_dsa, SubjectPublicKeyInfo, der); | |
1757 | MAKE_ENCODER(ml_dsa_65, ml_dsa, SubjectPublicKeyInfo, pem); | |
1758 | ||
1759 | MAKE_ENCODER(ml_dsa_87, ml_dsa, EncryptedPrivateKeyInfo, der); | |
1760 | MAKE_ENCODER(ml_dsa_87, ml_dsa, EncryptedPrivateKeyInfo, pem); | |
1761 | MAKE_ENCODER(ml_dsa_87, ml_dsa, PrivateKeyInfo, der); | |
1762 | MAKE_ENCODER(ml_dsa_87, ml_dsa, PrivateKeyInfo, pem); | |
1763 | MAKE_ENCODER(ml_dsa_87, ml_dsa, SubjectPublicKeyInfo, der); | |
1764 | MAKE_ENCODER(ml_dsa_87, ml_dsa, SubjectPublicKeyInfo, pem); | |
df231a88 | 1765 | #endif /* OPENSSL_NO_ML_DSA */ |