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