decode_der2key.c 31 KB

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