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