decode_der2key.c 33 KB

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