decode_der2key.c 33 KB

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