]> git.ipfire.org Git - thirdparty/openssl.git/blob - providers/implementations/encode_decode/decode_der2key.c
threads_pthread.c: change inline to ossl_inline
[thirdparty/openssl.git] / providers / implementations / encode_decode / decode_der2key.c
1 /*
2 * Copyright 2020-2023 The OpenSSL Project Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License 2.0 (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
8 */
9
10 /*
11 * low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14 #include "internal/deprecated.h"
15
16 #include <openssl/core_dispatch.h>
17 #include <openssl/core_names.h>
18 #include <openssl/core_object.h>
19 #include <openssl/crypto.h>
20 #include <openssl/err.h>
21 #include <openssl/params.h>
22 #include <openssl/pem.h> /* PEM_BUFSIZE and public PEM functions */
23 #include <openssl/pkcs12.h>
24 #include <openssl/x509.h>
25 #include <openssl/proverr.h>
26 #include "internal/cryptlib.h" /* ossl_assert() */
27 #include "internal/asn1.h"
28 #include "crypto/dh.h"
29 #include "crypto/dsa.h"
30 #include "crypto/ec.h"
31 #include "crypto/evp.h"
32 #include "crypto/ecx.h"
33 #include "crypto/rsa.h"
34 #include "crypto/x509.h"
35 #include "prov/bio.h"
36 #include "prov/implementations.h"
37 #include "endecoder_local.h"
38 #include "internal/nelem.h"
39
40 struct der2key_ctx_st; /* Forward declaration */
41 typedef int check_key_fn(void *, struct der2key_ctx_st *ctx);
42 typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx);
43 typedef void free_key_fn(void *);
44 typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long,
45 struct der2key_ctx_st *);
46 struct keytype_desc_st {
47 const char *keytype_name;
48 const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
49
50 /* The input structure name */
51 const char *structure_name;
52
53 /*
54 * The EVP_PKEY_xxx type macro. Should be zero for type specific
55 * structures, non-zero when the outermost structure is PKCS#8 or
56 * SubjectPublicKeyInfo. This determines which of the function
57 * pointers below will be used.
58 */
59 int evp_type;
60
61 /* The selection mask for OSSL_FUNC_decoder_does_selection() */
62 int selection_mask;
63
64 /* For type specific decoders, we use the corresponding d2i */
65 d2i_of_void *d2i_private_key; /* From type-specific DER */
66 d2i_of_void *d2i_public_key; /* From type-specific DER */
67 d2i_of_void *d2i_key_params; /* From type-specific DER */
68 d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */
69 d2i_of_void *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */
70
71 /*
72 * For any key, we may need to check that the key meets expectations.
73 * This is useful when the same functions can decode several variants
74 * of a key.
75 */
76 check_key_fn *check_key;
77
78 /*
79 * For any key, we may need to make provider specific adjustments, such
80 * as ensure the key carries the correct library context.
81 */
82 adjust_key_fn *adjust_key;
83 /* {type}_free() */
84 free_key_fn *free_key;
85 };
86
87 /*
88 * Context used for DER to key decoding.
89 */
90 struct der2key_ctx_st {
91 PROV_CTX *provctx;
92 char propq[OSSL_MAX_PROPQUERY_SIZE];
93 const struct keytype_desc_st *desc;
94 /* The selection that is passed to der2key_decode() */
95 int selection;
96 /* Flag used to signal that a failure is fatal */
97 unsigned int flag_fatal : 1;
98 };
99
100 typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
101 OSSL_LIB_CTX *libctx, const char *propq);
102 static void *der2key_decode_p8(const unsigned char **input_der,
103 long input_der_len, struct der2key_ctx_st *ctx,
104 key_from_pkcs8_t *key_from_pkcs8)
105 {
106 PKCS8_PRIV_KEY_INFO *p8inf = NULL;
107 const X509_ALGOR *alg = NULL;
108 void *key = NULL;
109
110 if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL
111 && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
112 && OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type)
113 key = key_from_pkcs8(p8inf, PROV_LIBCTX_OF(ctx->provctx), ctx->propq);
114 PKCS8_PRIV_KEY_INFO_free(p8inf);
115
116 return key;
117 }
118
119 /* ---------------------------------------------------------------------- */
120
121 static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
122 static OSSL_FUNC_decoder_decode_fn der2key_decode;
123 static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
124 static OSSL_FUNC_decoder_settable_ctx_params_fn der2key_settable_ctx_params;
125 static OSSL_FUNC_decoder_set_ctx_params_fn der2key_set_ctx_params;
126
127 static struct der2key_ctx_st *
128 der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
129 {
130 struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
131
132 if (ctx != NULL) {
133 ctx->provctx = provctx;
134 ctx->desc = desc;
135 }
136 return ctx;
137 }
138
139 static const OSSL_PARAM *der2key_settable_ctx_params(ossl_unused void *provctx)
140 {
141 static const OSSL_PARAM settables[] = {
142 OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
143 OSSL_PARAM_END
144 };
145 return settables;
146 }
147
148 static int der2key_set_ctx_params(void *vctx, const OSSL_PARAM params[])
149 {
150 struct der2key_ctx_st *ctx = vctx;
151 const OSSL_PARAM *p;
152 char *str = ctx->propq;
153
154 p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);
155 if (p != NULL && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))
156 return 0;
157
158 return 1;
159 }
160
161 static void der2key_freectx(void *vctx)
162 {
163 struct der2key_ctx_st *ctx = vctx;
164
165 OPENSSL_free(ctx);
166 }
167
168 static int der2key_check_selection(int selection,
169 const struct keytype_desc_st *desc)
170 {
171 /*
172 * The selections are kinda sorta "levels", i.e. each selection given
173 * here is assumed to include those following.
174 */
175 int checks[] = {
176 OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
177 OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
178 OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
179 };
180 size_t i;
181
182 /* The decoder implementations made here support guessing */
183 if (selection == 0)
184 return 1;
185
186 for (i = 0; i < OSSL_NELEM(checks); i++) {
187 int check1 = (selection & checks[i]) != 0;
188 int check2 = (desc->selection_mask & checks[i]) != 0;
189
190 /*
191 * If the caller asked for the currently checked bit(s), return
192 * whether the decoder description says it's supported.
193 */
194 if (check1)
195 return check2;
196 }
197
198 /* This should be dead code, but just to be safe... */
199 return 0;
200 }
201
202 static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
203 OSSL_CALLBACK *data_cb, void *data_cbarg,
204 OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
205 {
206 struct der2key_ctx_st *ctx = vctx;
207 unsigned char *der = NULL;
208 const unsigned char *derp;
209 long der_len = 0;
210 void *key = NULL;
211 int ok = 0;
212
213 ctx->selection = selection;
214 /*
215 * The caller is allowed to specify 0 as a selection mark, to have the
216 * structure and key type guessed. For type-specific structures, this
217 * is not recommended, as some structures are very similar.
218 * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
219 * signifies a private key structure, where everything else is assumed
220 * to be present as well.
221 */
222 if (selection == 0)
223 selection = ctx->desc->selection_mask;
224 if ((selection & ctx->desc->selection_mask) == 0) {
225 ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
226 return 0;
227 }
228
229 ok = ossl_read_der(ctx->provctx, cin, &der, &der_len);
230 if (!ok)
231 goto next;
232
233 ok = 0; /* Assume that we fail */
234
235 ERR_set_mark();
236 if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
237 derp = der;
238 if (ctx->desc->d2i_PKCS8 != NULL) {
239 key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx);
240 if (ctx->flag_fatal) {
241 ERR_clear_last_mark();
242 goto end;
243 }
244 } else if (ctx->desc->d2i_private_key != NULL) {
245 key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
246 }
247 if (key == NULL && ctx->selection != 0) {
248 ERR_clear_last_mark();
249 goto next;
250 }
251 }
252 if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
253 derp = der;
254 if (ctx->desc->d2i_PUBKEY != NULL)
255 key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len);
256 else if (ctx->desc->d2i_public_key != NULL)
257 key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
258 if (key == NULL && ctx->selection != 0) {
259 ERR_clear_last_mark();
260 goto next;
261 }
262 }
263 if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
264 derp = der;
265 if (ctx->desc->d2i_key_params != NULL)
266 key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
267 if (key == NULL && ctx->selection != 0) {
268 ERR_clear_last_mark();
269 goto next;
270 }
271 }
272 if (key == NULL)
273 ERR_clear_last_mark();
274 else
275 ERR_pop_to_mark();
276
277 /*
278 * Last minute check to see if this was the correct type of key. This
279 * should never lead to a fatal error, i.e. the decoding itself was
280 * correct, it was just an unexpected key type. This is generally for
281 * classes of key types that have subtle variants, like RSA-PSS keys as
282 * opposed to plain RSA keys.
283 */
284 if (key != NULL
285 && ctx->desc->check_key != NULL
286 && !ctx->desc->check_key(key, ctx)) {
287 ctx->desc->free_key(key);
288 key = NULL;
289 }
290
291 if (key != NULL && ctx->desc->adjust_key != NULL)
292 ctx->desc->adjust_key(key, ctx);
293
294 next:
295 /*
296 * Indicated that we successfully decoded something, or not at all.
297 * Ending up "empty handed" is not an error.
298 */
299 ok = 1;
300
301 /*
302 * We free memory here so it's not held up during the callback, because
303 * we know the process is recursive and the allocated chunks of memory
304 * add up.
305 */
306 OPENSSL_free(der);
307 der = NULL;
308
309 if (key != NULL) {
310 OSSL_PARAM params[4];
311 int object_type = OSSL_OBJECT_PKEY;
312
313 params[0] =
314 OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
315 params[1] =
316 OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
317 (char *)ctx->desc->keytype_name,
318 0);
319 /* The address of the key becomes the octet string */
320 params[2] =
321 OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
322 &key, sizeof(key));
323 params[3] = OSSL_PARAM_construct_end();
324
325 ok = data_cb(params, data_cbarg);
326 }
327
328 end:
329 ctx->desc->free_key(key);
330 OPENSSL_free(der);
331
332 return ok;
333 }
334
335 static int der2key_export_object(void *vctx,
336 const void *reference, size_t reference_sz,
337 OSSL_CALLBACK *export_cb, void *export_cbarg)
338 {
339 struct der2key_ctx_st *ctx = vctx;
340 OSSL_FUNC_keymgmt_export_fn *export =
341 ossl_prov_get_keymgmt_export(ctx->desc->fns);
342 void *keydata;
343
344 if (reference_sz == sizeof(keydata) && export != NULL) {
345 int selection = ctx->selection;
346
347 if (selection == 0)
348 selection = OSSL_KEYMGMT_SELECT_ALL;
349 /* The contents of the reference is the address to our object */
350 keydata = *(void **)reference;
351
352 return export(keydata, selection, export_cb, export_cbarg);
353 }
354 return 0;
355 }
356
357 /* ---------------------------------------------------------------------- */
358
359 #ifndef OPENSSL_NO_DH
360 # define dh_evp_type EVP_PKEY_DH
361 # define dh_d2i_private_key NULL
362 # define dh_d2i_public_key NULL
363 # define dh_d2i_key_params (d2i_of_void *)d2i_DHparams
364
365 static void *dh_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
366 struct der2key_ctx_st *ctx)
367 {
368 return der2key_decode_p8(der, der_len, ctx,
369 (key_from_pkcs8_t *)ossl_dh_key_from_pkcs8);
370 }
371
372 # define dh_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DH_PUBKEY
373 # define dh_free (free_key_fn *)DH_free
374 # define dh_check NULL
375
376 static void dh_adjust(void *key, struct der2key_ctx_st *ctx)
377 {
378 ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
379 }
380
381 # define dhx_evp_type EVP_PKEY_DHX
382 # define dhx_d2i_private_key NULL
383 # define dhx_d2i_public_key NULL
384 # define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams
385 # define dhx_d2i_PKCS8 dh_d2i_PKCS8
386 # define dhx_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DHx_PUBKEY
387 # define dhx_free (free_key_fn *)DH_free
388 # define dhx_check NULL
389 # define dhx_adjust dh_adjust
390 #endif
391
392 /* ---------------------------------------------------------------------- */
393
394 #ifndef OPENSSL_NO_DSA
395 # define dsa_evp_type EVP_PKEY_DSA
396 # define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey
397 # define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey
398 # define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams
399
400 static void *dsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
401 struct der2key_ctx_st *ctx)
402 {
403 return der2key_decode_p8(der, der_len, ctx,
404 (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
405 }
406
407 # define dsa_d2i_PUBKEY (d2i_of_void *)ossl_d2i_DSA_PUBKEY
408 # define dsa_free (free_key_fn *)DSA_free
409 # define dsa_check NULL
410
411 static void dsa_adjust(void *key, struct der2key_ctx_st *ctx)
412 {
413 ossl_dsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
414 }
415 #endif
416
417 /* ---------------------------------------------------------------------- */
418
419 #ifndef OPENSSL_NO_EC
420 # define ec_evp_type EVP_PKEY_EC
421 # define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
422 # define ec_d2i_public_key NULL
423 # define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters
424
425 static void *ec_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
426 struct der2key_ctx_st *ctx)
427 {
428 return der2key_decode_p8(der, der_len, ctx,
429 (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
430 }
431
432 # define ec_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
433 # define ec_free (free_key_fn *)EC_KEY_free
434
435 static int ec_check(void *key, struct der2key_ctx_st *ctx)
436 {
437 /* We're trying to be clever by comparing two truths */
438
439 int sm2 = (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0;
440
441 return sm2 == (ctx->desc->evp_type == EVP_PKEY_SM2);
442 }
443
444 static void ec_adjust(void *key, struct der2key_ctx_st *ctx)
445 {
446 ossl_ec_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
447 }
448
449 # ifndef OPENSSL_NO_ECX
450 /*
451 * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
452 * so no d2i functions to be had.
453 */
454
455 static void *ecx_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
456 struct der2key_ctx_st *ctx)
457 {
458 return der2key_decode_p8(der, der_len, ctx,
459 (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8);
460 }
461
462 static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx)
463 {
464 ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
465 }
466
467 # define ed25519_evp_type EVP_PKEY_ED25519
468 # define ed25519_d2i_private_key NULL
469 # define ed25519_d2i_public_key NULL
470 # define ed25519_d2i_key_params NULL
471 # define ed25519_d2i_PKCS8 ecx_d2i_PKCS8
472 # define ed25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED25519_PUBKEY
473 # define ed25519_free (free_key_fn *)ossl_ecx_key_free
474 # define ed25519_check NULL
475 # define ed25519_adjust ecx_key_adjust
476
477 # define ed448_evp_type EVP_PKEY_ED448
478 # define ed448_d2i_private_key NULL
479 # define ed448_d2i_public_key NULL
480 # define ed448_d2i_key_params NULL
481 # define ed448_d2i_PKCS8 ecx_d2i_PKCS8
482 # define ed448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_ED448_PUBKEY
483 # define ed448_free (free_key_fn *)ossl_ecx_key_free
484 # define ed448_check NULL
485 # define ed448_adjust ecx_key_adjust
486
487 # define x25519_evp_type EVP_PKEY_X25519
488 # define x25519_d2i_private_key NULL
489 # define x25519_d2i_public_key NULL
490 # define x25519_d2i_key_params NULL
491 # define x25519_d2i_PKCS8 ecx_d2i_PKCS8
492 # define x25519_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X25519_PUBKEY
493 # define x25519_free (free_key_fn *)ossl_ecx_key_free
494 # define x25519_check NULL
495 # define x25519_adjust ecx_key_adjust
496
497 # define x448_evp_type EVP_PKEY_X448
498 # define x448_d2i_private_key NULL
499 # define x448_d2i_public_key NULL
500 # define x448_d2i_key_params NULL
501 # define x448_d2i_PKCS8 ecx_d2i_PKCS8
502 # define x448_d2i_PUBKEY (d2i_of_void *)ossl_d2i_X448_PUBKEY
503 # define x448_free (free_key_fn *)ossl_ecx_key_free
504 # define x448_check NULL
505 # define x448_adjust ecx_key_adjust
506 # endif /* OPENSSL_NO_ECX */
507
508 # ifndef OPENSSL_NO_SM2
509 # define sm2_evp_type EVP_PKEY_SM2
510 # define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
511 # define sm2_d2i_public_key NULL
512 # define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters
513
514 static void *sm2_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
515 struct der2key_ctx_st *ctx)
516 {
517 return der2key_decode_p8(der, der_len, ctx,
518 (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
519 }
520
521 # define sm2_d2i_PUBKEY (d2i_of_void *)d2i_EC_PUBKEY
522 # define sm2_free (free_key_fn *)EC_KEY_free
523 # define sm2_check ec_check
524 # define sm2_adjust ec_adjust
525 # endif
526 #endif
527
528 /* ---------------------------------------------------------------------- */
529
530 #define rsa_evp_type EVP_PKEY_RSA
531 #define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
532 #define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
533 #define rsa_d2i_key_params NULL
534
535 static void *rsa_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
536 struct der2key_ctx_st *ctx)
537 {
538 return der2key_decode_p8(der, der_len, ctx,
539 (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8);
540 }
541
542 #define rsa_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
543 #define rsa_free (free_key_fn *)RSA_free
544
545 static int rsa_check(void *key, struct der2key_ctx_st *ctx)
546 {
547 switch (RSA_test_flags(key, RSA_FLAG_TYPE_MASK)) {
548 case RSA_FLAG_TYPE_RSA:
549 return ctx->desc->evp_type == EVP_PKEY_RSA;
550 case RSA_FLAG_TYPE_RSASSAPSS:
551 return ctx->desc->evp_type == EVP_PKEY_RSA_PSS;
552 }
553
554 /* Currently unsupported RSA key type */
555 return 0;
556 }
557
558 static void rsa_adjust(void *key, struct der2key_ctx_st *ctx)
559 {
560 ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
561 }
562
563 #define rsapss_evp_type EVP_PKEY_RSA_PSS
564 #define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
565 #define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
566 #define rsapss_d2i_key_params NULL
567 #define rsapss_d2i_PKCS8 rsa_d2i_PKCS8
568 #define rsapss_d2i_PUBKEY (d2i_of_void *)d2i_RSA_PUBKEY
569 #define rsapss_free (free_key_fn *)RSA_free
570 #define rsapss_check rsa_check
571 #define rsapss_adjust rsa_adjust
572
573 /* ---------------------------------------------------------------------- */
574
575 /*
576 * The DO_ macros help define the selection mask and the method functions
577 * for each kind of object we want to decode.
578 */
579 #define DO_type_specific_keypair(keytype) \
580 "type-specific", keytype##_evp_type, \
581 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
582 keytype##_d2i_private_key, \
583 keytype##_d2i_public_key, \
584 NULL, \
585 NULL, \
586 NULL, \
587 keytype##_check, \
588 keytype##_adjust, \
589 keytype##_free
590
591 #define DO_type_specific_pub(keytype) \
592 "type-specific", keytype##_evp_type, \
593 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
594 NULL, \
595 keytype##_d2i_public_key, \
596 NULL, \
597 NULL, \
598 NULL, \
599 keytype##_check, \
600 keytype##_adjust, \
601 keytype##_free
602
603 #define DO_type_specific_priv(keytype) \
604 "type-specific", keytype##_evp_type, \
605 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
606 keytype##_d2i_private_key, \
607 NULL, \
608 NULL, \
609 NULL, \
610 NULL, \
611 keytype##_check, \
612 keytype##_adjust, \
613 keytype##_free
614
615 #define DO_type_specific_params(keytype) \
616 "type-specific", keytype##_evp_type, \
617 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
618 NULL, \
619 NULL, \
620 keytype##_d2i_key_params, \
621 NULL, \
622 NULL, \
623 keytype##_check, \
624 keytype##_adjust, \
625 keytype##_free
626
627 #define DO_type_specific(keytype) \
628 "type-specific", keytype##_evp_type, \
629 ( OSSL_KEYMGMT_SELECT_ALL ), \
630 keytype##_d2i_private_key, \
631 keytype##_d2i_public_key, \
632 keytype##_d2i_key_params, \
633 NULL, \
634 NULL, \
635 keytype##_check, \
636 keytype##_adjust, \
637 keytype##_free
638
639 #define DO_type_specific_no_pub(keytype) \
640 "type-specific", keytype##_evp_type, \
641 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
642 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
643 keytype##_d2i_private_key, \
644 NULL, \
645 keytype##_d2i_key_params, \
646 NULL, \
647 NULL, \
648 keytype##_check, \
649 keytype##_adjust, \
650 keytype##_free
651
652 #define DO_PrivateKeyInfo(keytype) \
653 "PrivateKeyInfo", keytype##_evp_type, \
654 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
655 NULL, \
656 NULL, \
657 NULL, \
658 keytype##_d2i_PKCS8, \
659 NULL, \
660 keytype##_check, \
661 keytype##_adjust, \
662 keytype##_free
663
664 #define DO_SubjectPublicKeyInfo(keytype) \
665 "SubjectPublicKeyInfo", keytype##_evp_type, \
666 ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
667 NULL, \
668 NULL, \
669 NULL, \
670 NULL, \
671 keytype##_d2i_PUBKEY, \
672 keytype##_check, \
673 keytype##_adjust, \
674 keytype##_free
675
676 #define DO_DH(keytype) \
677 "DH", keytype##_evp_type, \
678 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
679 NULL, \
680 NULL, \
681 keytype##_d2i_key_params, \
682 NULL, \
683 NULL, \
684 keytype##_check, \
685 keytype##_adjust, \
686 keytype##_free
687
688 #define DO_DHX(keytype) \
689 "DHX", keytype##_evp_type, \
690 ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
691 NULL, \
692 NULL, \
693 keytype##_d2i_key_params, \
694 NULL, \
695 NULL, \
696 keytype##_check, \
697 keytype##_adjust, \
698 keytype##_free
699
700 #define DO_DSA(keytype) \
701 "DSA", keytype##_evp_type, \
702 ( OSSL_KEYMGMT_SELECT_ALL ), \
703 keytype##_d2i_private_key, \
704 keytype##_d2i_public_key, \
705 keytype##_d2i_key_params, \
706 NULL, \
707 NULL, \
708 keytype##_check, \
709 keytype##_adjust, \
710 keytype##_free
711
712 #define DO_EC(keytype) \
713 "EC", keytype##_evp_type, \
714 ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
715 | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
716 keytype##_d2i_private_key, \
717 NULL, \
718 keytype##_d2i_key_params, \
719 NULL, \
720 NULL, \
721 keytype##_check, \
722 keytype##_adjust, \
723 keytype##_free
724
725 #define DO_RSA(keytype) \
726 "RSA", keytype##_evp_type, \
727 ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
728 keytype##_d2i_private_key, \
729 keytype##_d2i_public_key, \
730 NULL, \
731 NULL, \
732 NULL, \
733 keytype##_check, \
734 keytype##_adjust, \
735 keytype##_free
736
737 /*
738 * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
739 * It takes the following arguments:
740 *
741 * keytype_name The implementation key type as a string.
742 * keytype The implementation key type. This must correspond exactly
743 * to our existing keymgmt keytype names... in other words,
744 * there must exist an ossl_##keytype##_keymgmt_functions.
745 * type The type name for the set of functions that implement the
746 * decoder for the key type. This isn't necessarily the same
747 * as keytype. For example, the key types ed25519, ed448,
748 * x25519 and x448 are all handled by the same functions with
749 * the common type name ecx.
750 * kind The kind of support to implement. This translates into
751 * the DO_##kind macros above, to populate the keytype_desc_st
752 * structure.
753 */
754 #define MAKE_DECODER(keytype_name, keytype, type, kind) \
755 static const struct keytype_desc_st kind##_##keytype##_desc = \
756 { keytype_name, ossl_##keytype##_keymgmt_functions, \
757 DO_##kind(keytype) }; \
758 \
759 static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \
760 \
761 static void *kind##_der2##keytype##_newctx(void *provctx) \
762 { \
763 return der2key_newctx(provctx, &kind##_##keytype##_desc); \
764 } \
765 static int kind##_der2##keytype##_does_selection(void *provctx, \
766 int selection) \
767 { \
768 return der2key_check_selection(selection, \
769 &kind##_##keytype##_desc); \
770 } \
771 const OSSL_DISPATCH \
772 ossl_##kind##_der_to_##keytype##_decoder_functions[] = { \
773 { OSSL_FUNC_DECODER_NEWCTX, \
774 (void (*)(void))kind##_der2##keytype##_newctx }, \
775 { OSSL_FUNC_DECODER_FREECTX, \
776 (void (*)(void))der2key_freectx }, \
777 { OSSL_FUNC_DECODER_DOES_SELECTION, \
778 (void (*)(void))kind##_der2##keytype##_does_selection }, \
779 { OSSL_FUNC_DECODER_DECODE, \
780 (void (*)(void))der2key_decode }, \
781 { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
782 (void (*)(void))der2key_export_object }, \
783 { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \
784 (void (*)(void))der2key_settable_ctx_params }, \
785 { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \
786 (void (*)(void))der2key_set_ctx_params }, \
787 OSSL_DISPATCH_END \
788 }
789
790 #ifndef OPENSSL_NO_DH
791 MAKE_DECODER("DH", dh, dh, PrivateKeyInfo);
792 MAKE_DECODER("DH", dh, dh, SubjectPublicKeyInfo);
793 MAKE_DECODER("DH", dh, dh, type_specific_params);
794 MAKE_DECODER("DH", dh, dh, DH);
795 MAKE_DECODER("DHX", dhx, dhx, PrivateKeyInfo);
796 MAKE_DECODER("DHX", dhx, dhx, SubjectPublicKeyInfo);
797 MAKE_DECODER("DHX", dhx, dhx, type_specific_params);
798 MAKE_DECODER("DHX", dhx, dhx, DHX);
799 #endif
800 #ifndef OPENSSL_NO_DSA
801 MAKE_DECODER("DSA", dsa, dsa, PrivateKeyInfo);
802 MAKE_DECODER("DSA", dsa, dsa, SubjectPublicKeyInfo);
803 MAKE_DECODER("DSA", dsa, dsa, type_specific);
804 MAKE_DECODER("DSA", dsa, dsa, DSA);
805 #endif
806 #ifndef OPENSSL_NO_EC
807 MAKE_DECODER("EC", ec, ec, PrivateKeyInfo);
808 MAKE_DECODER("EC", ec, ec, SubjectPublicKeyInfo);
809 MAKE_DECODER("EC", ec, ec, type_specific_no_pub);
810 MAKE_DECODER("EC", ec, ec, EC);
811 # ifndef OPENSSL_NO_ECX
812 MAKE_DECODER("X25519", x25519, ecx, PrivateKeyInfo);
813 MAKE_DECODER("X25519", x25519, ecx, SubjectPublicKeyInfo);
814 MAKE_DECODER("X448", x448, ecx, PrivateKeyInfo);
815 MAKE_DECODER("X448", x448, ecx, SubjectPublicKeyInfo);
816 MAKE_DECODER("ED25519", ed25519, ecx, PrivateKeyInfo);
817 MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo);
818 MAKE_DECODER("ED448", ed448, ecx, PrivateKeyInfo);
819 MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo);
820 # endif
821 # ifndef OPENSSL_NO_SM2
822 MAKE_DECODER("SM2", sm2, ec, PrivateKeyInfo);
823 MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo);
824 MAKE_DECODER("SM2", sm2, sm2, type_specific_no_pub);
825 # endif
826 #endif
827 MAKE_DECODER("RSA", rsa, rsa, PrivateKeyInfo);
828 MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo);
829 MAKE_DECODER("RSA", rsa, rsa, type_specific_keypair);
830 MAKE_DECODER("RSA", rsa, rsa, RSA);
831 MAKE_DECODER("RSA-PSS", rsapss, rsapss, PrivateKeyInfo);
832 MAKE_DECODER("RSA-PSS", rsapss, rsapss, SubjectPublicKeyInfo);