decode_der2key.c 32 KB

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