decode_der2key.c 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307
  1. /*
  2. * Copyright 2020-2025 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/byteorder.h>
  15. #include <openssl/core_dispatch.h>
  16. #include <openssl/core_names.h>
  17. #include <openssl/core_object.h>
  18. #include <openssl/crypto.h>
  19. #include <openssl/err.h>
  20. #include <openssl/params.h>
  21. #include <openssl/pem.h> /* PEM_BUFSIZE and public PEM functions */
  22. #include <openssl/pkcs12.h>
  23. #include <openssl/provider.h>
  24. #include <openssl/x509.h>
  25. #include <openssl/proverr.h>
  26. #include <openssl/asn1t.h>
  27. #include "internal/cryptlib.h" /* ossl_assert() */
  28. #include "crypto/dh.h"
  29. #include "crypto/dsa.h"
  30. #include "crypto/ec.h"
  31. #include "crypto/evp.h"
  32. #include "crypto/ecx.h"
  33. #include "crypto/rsa.h"
  34. #include "crypto/ml_dsa.h"
  35. #include "crypto/slh_dsa.h"
  36. #include "crypto/x509.h"
  37. #include "crypto/ml_kem.h"
  38. #include "openssl/obj_mac.h"
  39. #include "prov/bio.h"
  40. #include "prov/implementations.h"
  41. #include "endecoder_local.h"
  42. #include "internal/nelem.h"
  43. #include "ml_dsa_codecs.h"
  44. #include "ml_kem_codecs.h"
  45. #ifndef OPENSSL_NO_SLH_DSA
  46. typedef struct {
  47. ASN1_OBJECT *oid;
  48. } BARE_ALGOR;
  49. typedef struct {
  50. BARE_ALGOR algor;
  51. ASN1_BIT_STRING *pubkey;
  52. } BARE_PUBKEY;
  53. ASN1_SEQUENCE(BARE_ALGOR) = {
  54. ASN1_SIMPLE(BARE_ALGOR, oid, ASN1_OBJECT),
  55. } static_ASN1_SEQUENCE_END(BARE_ALGOR)
  56. ASN1_SEQUENCE(BARE_PUBKEY) = {
  57. ASN1_EMBED(BARE_PUBKEY, algor, BARE_ALGOR),
  58. ASN1_SIMPLE(BARE_PUBKEY, pubkey, ASN1_BIT_STRING)
  59. } static_ASN1_SEQUENCE_END(BARE_PUBKEY)
  60. #endif /* OPENSSL_NO_SLH_DSA */
  61. struct der2key_ctx_st; /* Forward declaration */
  62. typedef int check_key_fn(void *, struct der2key_ctx_st *ctx);
  63. typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx);
  64. typedef void free_key_fn(void *);
  65. typedef void *d2i_PKCS8_fn(const unsigned char **, long,
  66. struct der2key_ctx_st *);
  67. typedef void *d2i_PUBKEY_fn(const unsigned char **, long,
  68. struct der2key_ctx_st *);
  69. struct keytype_desc_st {
  70. const char *keytype_name;
  71. const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
  72. /* The input structure name */
  73. const char *structure_name;
  74. /*
  75. * The EVP_PKEY_xxx type macro. Should be zero for type specific
  76. * structures, non-zero when the outermost structure is PKCS#8 or
  77. * SubjectPublicKeyInfo. This determines which of the function
  78. * pointers below will be used.
  79. */
  80. int evp_type;
  81. /* The selection mask for OSSL_FUNC_decoder_does_selection() */
  82. int selection_mask;
  83. /* For type specific decoders, we use the corresponding d2i */
  84. d2i_of_void *d2i_private_key; /* From type-specific DER */
  85. d2i_of_void *d2i_public_key; /* From type-specific DER */
  86. d2i_of_void *d2i_key_params; /* From type-specific DER */
  87. d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */
  88. d2i_PUBKEY_fn *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */
  89. /*
  90. * For any key, we may need to check that the key meets expectations.
  91. * This is useful when the same functions can decode several variants
  92. * of a key.
  93. */
  94. check_key_fn *check_key;
  95. /*
  96. * For any key, we may need to make provider specific adjustments, such
  97. * as ensure the key carries the correct library context.
  98. */
  99. adjust_key_fn *adjust_key;
  100. /* {type}_free() */
  101. free_key_fn *free_key;
  102. };
  103. /*
  104. * Context used for DER to key decoding.
  105. */
  106. struct der2key_ctx_st {
  107. PROV_CTX *provctx;
  108. char propq[OSSL_MAX_PROPQUERY_SIZE];
  109. const struct keytype_desc_st *desc;
  110. /* The selection that is passed to der2key_decode() */
  111. int selection;
  112. /* Flag used to signal that a failure is fatal */
  113. unsigned int flag_fatal : 1;
  114. };
  115. typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
  116. OSSL_LIB_CTX *libctx, const char *propq);
  117. static void *der2key_decode_p8(const unsigned char **input_der,
  118. long input_der_len, struct der2key_ctx_st *ctx,
  119. key_from_pkcs8_t *key_from_pkcs8)
  120. {
  121. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  122. const X509_ALGOR *alg = NULL;
  123. void *key = NULL;
  124. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL
  125. && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
  126. && (OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type
  127. /* Allow decoding sm2 private key with id_ecPublicKey */
  128. || (OBJ_obj2nid(alg->algorithm) == NID_X9_62_id_ecPublicKey
  129. && ctx->desc->evp_type == NID_sm2)))
  130. key = key_from_pkcs8(p8inf, PROV_LIBCTX_OF(ctx->provctx), ctx->propq);
  131. PKCS8_PRIV_KEY_INFO_free(p8inf);
  132. return key;
  133. }
  134. /* ---------------------------------------------------------------------- */
  135. static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
  136. static OSSL_FUNC_decoder_decode_fn der2key_decode;
  137. static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
  138. static OSSL_FUNC_decoder_settable_ctx_params_fn der2key_settable_ctx_params;
  139. static OSSL_FUNC_decoder_set_ctx_params_fn der2key_set_ctx_params;
  140. static struct der2key_ctx_st *
  141. der2key_newctx(void *provctx, const struct keytype_desc_st *desc)
  142. {
  143. struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  144. if (ctx != NULL) {
  145. ctx->provctx = provctx;
  146. ctx->desc = desc;
  147. }
  148. return ctx;
  149. }
  150. static const OSSL_PARAM *der2key_settable_ctx_params(ossl_unused void *provctx)
  151. {
  152. static const OSSL_PARAM settables[] = {
  153. OSSL_PARAM_utf8_string(OSSL_DECODER_PARAM_PROPERTIES, NULL, 0),
  154. OSSL_PARAM_END
  155. };
  156. return settables;
  157. }
  158. static int der2key_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  159. {
  160. struct der2key_ctx_st *ctx = vctx;
  161. const OSSL_PARAM *p;
  162. char *str = ctx->propq;
  163. p = OSSL_PARAM_locate_const(params, OSSL_DECODER_PARAM_PROPERTIES);
  164. if (p != NULL && !OSSL_PARAM_get_utf8_string(p, &str, sizeof(ctx->propq)))
  165. return 0;
  166. return 1;
  167. }
  168. static void der2key_freectx(void *vctx)
  169. {
  170. struct der2key_ctx_st *ctx = vctx;
  171. OPENSSL_free(ctx);
  172. }
  173. static int der2key_check_selection(int selection,
  174. const struct keytype_desc_st *desc)
  175. {
  176. /*
  177. * The selections are kinda sorta "levels", i.e. each selection given
  178. * here is assumed to include those following.
  179. */
  180. int checks[] = {
  181. OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  182. OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  183. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  184. };
  185. size_t i;
  186. /* The decoder implementations made here support guessing */
  187. if (selection == 0)
  188. return 1;
  189. for (i = 0; i < OSSL_NELEM(checks); i++) {
  190. int check1 = (selection & checks[i]) != 0;
  191. int check2 = (desc->selection_mask & checks[i]) != 0;
  192. /*
  193. * If the caller asked for the currently checked bit(s), return
  194. * whether the decoder description says it's supported.
  195. */
  196. if (check1)
  197. return check2;
  198. }
  199. /* This should be dead code, but just to be safe... */
  200. return 0;
  201. }
  202. static int der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  203. OSSL_CALLBACK *data_cb, void *data_cbarg,
  204. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  205. {
  206. struct der2key_ctx_st *ctx = vctx;
  207. unsigned char *der = NULL;
  208. const unsigned char *derp;
  209. long der_len = 0;
  210. void *key = NULL;
  211. int ok = 0;
  212. ctx->selection = selection;
  213. /*
  214. * The caller is allowed to specify 0 as a selection mask, to have the
  215. * structure and key type guessed. For type-specific structures, this
  216. * is not recommended, as some structures are very similar.
  217. * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
  218. * signifies a private key structure, where everything else is assumed
  219. * to be present as well.
  220. */
  221. if (selection == 0)
  222. selection = ctx->desc->selection_mask;
  223. if ((selection & ctx->desc->selection_mask) == 0) {
  224. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  225. return 0;
  226. }
  227. ok = ossl_read_der(ctx->provctx, cin, &der, &der_len);
  228. if (!ok)
  229. goto next;
  230. ok = 0; /* Assume that we fail */
  231. ERR_set_mark();
  232. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  233. derp = der;
  234. if (ctx->desc->d2i_PKCS8 != NULL) {
  235. key = ctx->desc->d2i_PKCS8(&derp, der_len, ctx);
  236. if (ctx->flag_fatal) {
  237. ERR_clear_last_mark();
  238. goto end;
  239. }
  240. } else if (ctx->desc->d2i_private_key != NULL) {
  241. key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
  242. }
  243. if (key == NULL && ctx->selection != 0) {
  244. ERR_clear_last_mark();
  245. goto next;
  246. }
  247. }
  248. if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  249. derp = der;
  250. if (ctx->desc->d2i_PUBKEY != NULL)
  251. key = ctx->desc->d2i_PUBKEY(&derp, der_len, ctx);
  252. else if (ctx->desc->d2i_public_key != NULL)
  253. key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
  254. if (key == NULL && ctx->selection != 0) {
  255. ERR_clear_last_mark();
  256. goto next;
  257. }
  258. }
  259. if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
  260. derp = der;
  261. if (ctx->desc->d2i_key_params != NULL)
  262. key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
  263. if (key == NULL && ctx->selection != 0) {
  264. ERR_clear_last_mark();
  265. goto next;
  266. }
  267. }
  268. if (key == NULL)
  269. ERR_clear_last_mark();
  270. else
  271. ERR_pop_to_mark();
  272. /*
  273. * Last minute check to see if this was the correct type of key. This
  274. * should never lead to a fatal error, i.e. the decoding itself was
  275. * correct, it was just an unexpected key type. This is generally for
  276. * classes of key types that have subtle variants, like RSA-PSS keys as
  277. * opposed to plain RSA keys.
  278. */
  279. if (key != NULL
  280. && ctx->desc->check_key != NULL
  281. && !ctx->desc->check_key(key, ctx)) {
  282. ctx->desc->free_key(key);
  283. key = NULL;
  284. }
  285. if (key != NULL && ctx->desc->adjust_key != NULL)
  286. ctx->desc->adjust_key(key, ctx);
  287. next:
  288. /*
  289. * Indicated that we successfully decoded something, or not at all.
  290. * Ending up "empty handed" is not an error.
  291. */
  292. ok = 1;
  293. /*
  294. * We free memory here so it's not held up during the callback, because
  295. * we know the process is recursive and the allocated chunks of memory
  296. * add up.
  297. */
  298. OPENSSL_free(der);
  299. der = NULL;
  300. if (key != NULL) {
  301. OSSL_PARAM params[4];
  302. int object_type = OSSL_OBJECT_PKEY;
  303. params[0] =
  304. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  305. #ifndef OPENSSL_NO_SM2
  306. if (strcmp(ctx->desc->keytype_name, "EC") == 0
  307. && (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0)
  308. params[1] =
  309. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  310. "SM2", 0);
  311. else
  312. #endif
  313. params[1] =
  314. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  315. (char *)ctx->desc->keytype_name,
  316. 0);
  317. /* The address of the key becomes the octet string */
  318. params[2] =
  319. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  320. &key, sizeof(key));
  321. params[3] = OSSL_PARAM_construct_end();
  322. ok = data_cb(params, data_cbarg);
  323. }
  324. end:
  325. ctx->desc->free_key(key);
  326. OPENSSL_free(der);
  327. return ok;
  328. }
  329. static int der2key_export_object(void *vctx,
  330. const void *reference, size_t reference_sz,
  331. OSSL_CALLBACK *export_cb, void *export_cbarg)
  332. {
  333. struct der2key_ctx_st *ctx = vctx;
  334. OSSL_FUNC_keymgmt_export_fn *export =
  335. ossl_prov_get_keymgmt_export(ctx->desc->fns);
  336. void *keydata;
  337. if (reference_sz == sizeof(keydata) && export != NULL) {
  338. int selection = ctx->selection;
  339. if (selection == 0)
  340. selection = OSSL_KEYMGMT_SELECT_ALL;
  341. /* The contents of the reference is the address to our object */
  342. keydata = *(void **)reference;
  343. return export(keydata, selection, export_cb, export_cbarg);
  344. }
  345. return 0;
  346. }
  347. #define D2I_PUBKEY_NOCTX(n, f) \
  348. static void * \
  349. n##_d2i_PUBKEY(const unsigned char **der, long der_len, \
  350. ossl_unused struct der2key_ctx_st *ctx) \
  351. { \
  352. return f(NULL, der, der_len); \
  353. }
  354. /* ---------------------------------------------------------------------- */
  355. #ifndef OPENSSL_NO_DH
  356. # define dh_evp_type EVP_PKEY_DH
  357. # define dh_d2i_private_key NULL
  358. # define dh_d2i_public_key NULL
  359. # define dh_d2i_key_params (d2i_of_void *)d2i_DHparams
  360. # define dh_free (free_key_fn *)DH_free
  361. # define dh_check NULL
  362. static void *dh_d2i_PKCS8(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_dh_key_from_pkcs8);
  367. }
  368. D2I_PUBKEY_NOCTX(dh, ossl_d2i_DH_PUBKEY)
  369. D2I_PUBKEY_NOCTX(dhx, ossl_d2i_DHx_PUBKEY)
  370. static void dh_adjust(void *key, struct der2key_ctx_st *ctx)
  371. {
  372. ossl_dh_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  373. }
  374. # define dhx_evp_type EVP_PKEY_DHX
  375. # define dhx_d2i_private_key NULL
  376. # define dhx_d2i_public_key NULL
  377. # define dhx_d2i_key_params (d2i_of_void *)d2i_DHxparams
  378. # define dhx_d2i_PKCS8 dh_d2i_PKCS8
  379. # define dhx_free (free_key_fn *)DH_free
  380. # define dhx_check NULL
  381. # define dhx_adjust dh_adjust
  382. #endif
  383. /* ---------------------------------------------------------------------- */
  384. #ifndef OPENSSL_NO_DSA
  385. # define dsa_evp_type EVP_PKEY_DSA
  386. # define dsa_d2i_private_key (d2i_of_void *)d2i_DSAPrivateKey
  387. # define dsa_d2i_public_key (d2i_of_void *)d2i_DSAPublicKey
  388. # define dsa_d2i_key_params (d2i_of_void *)d2i_DSAparams
  389. # define dsa_free (free_key_fn *)DSA_free
  390. # define dsa_check NULL
  391. static void *dsa_d2i_PKCS8(const unsigned char **der, long der_len,
  392. struct der2key_ctx_st *ctx)
  393. {
  394. return der2key_decode_p8(der, der_len, ctx,
  395. (key_from_pkcs8_t *)ossl_dsa_key_from_pkcs8);
  396. }
  397. D2I_PUBKEY_NOCTX(dsa, ossl_d2i_DSA_PUBKEY)
  398. static void dsa_adjust(void *key, struct der2key_ctx_st *ctx)
  399. {
  400. ossl_dsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  401. }
  402. #endif
  403. /* ---------------------------------------------------------------------- */
  404. #ifndef OPENSSL_NO_EC
  405. # define ec_evp_type EVP_PKEY_EC
  406. # define ec_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
  407. # define ec_d2i_public_key NULL
  408. # define ec_d2i_key_params (d2i_of_void *)d2i_ECParameters
  409. # define ec_free (free_key_fn *)EC_KEY_free
  410. static void *ec_d2i_PKCS8(const unsigned char **der, long der_len,
  411. struct der2key_ctx_st *ctx)
  412. {
  413. return der2key_decode_p8(der, der_len, ctx,
  414. (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
  415. }
  416. D2I_PUBKEY_NOCTX(ec, d2i_EC_PUBKEY)
  417. static int ec_check(void *key, struct der2key_ctx_st *ctx)
  418. {
  419. /* We're trying to be clever by comparing two truths */
  420. int ret = 0;
  421. int sm2 = (EC_KEY_get_flags(key) & EC_FLAG_SM2_RANGE) != 0;
  422. if (sm2)
  423. ret = ctx->desc->evp_type == EVP_PKEY_SM2
  424. || ctx->desc->evp_type == NID_X9_62_id_ecPublicKey;
  425. else
  426. ret = ctx->desc->evp_type != EVP_PKEY_SM2;
  427. return ret;
  428. }
  429. static void ec_adjust(void *key, struct der2key_ctx_st *ctx)
  430. {
  431. ossl_ec_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  432. }
  433. # ifndef OPENSSL_NO_ECX
  434. /*
  435. * ED25519, ED448, X25519, X448 only implement PKCS#8 and SubjectPublicKeyInfo,
  436. * so no d2i functions to be had.
  437. */
  438. static void *ecx_d2i_PKCS8(const unsigned char **der, long der_len,
  439. struct der2key_ctx_st *ctx)
  440. {
  441. return der2key_decode_p8(der, der_len, ctx,
  442. (key_from_pkcs8_t *)ossl_ecx_key_from_pkcs8);
  443. }
  444. D2I_PUBKEY_NOCTX(ed25519, ossl_d2i_ED25519_PUBKEY)
  445. D2I_PUBKEY_NOCTX(ed448, ossl_d2i_ED448_PUBKEY)
  446. D2I_PUBKEY_NOCTX(x25519, ossl_d2i_X25519_PUBKEY)
  447. D2I_PUBKEY_NOCTX(x448, ossl_d2i_X448_PUBKEY)
  448. static void ecx_key_adjust(void *key, struct der2key_ctx_st *ctx)
  449. {
  450. ossl_ecx_key_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  451. }
  452. # define ed25519_evp_type EVP_PKEY_ED25519
  453. # define ed25519_d2i_private_key NULL
  454. # define ed25519_d2i_public_key NULL
  455. # define ed25519_d2i_key_params NULL
  456. # define ed25519_d2i_PKCS8 ecx_d2i_PKCS8
  457. # define ed25519_free (free_key_fn *)ossl_ecx_key_free
  458. # define ed25519_check NULL
  459. # define ed25519_adjust ecx_key_adjust
  460. # define ed448_evp_type EVP_PKEY_ED448
  461. # define ed448_d2i_private_key NULL
  462. # define ed448_d2i_public_key NULL
  463. # define ed448_d2i_key_params NULL
  464. # define ed448_d2i_PKCS8 ecx_d2i_PKCS8
  465. # define ed448_free (free_key_fn *)ossl_ecx_key_free
  466. # define ed448_check NULL
  467. # define ed448_adjust ecx_key_adjust
  468. # define x25519_evp_type EVP_PKEY_X25519
  469. # define x25519_d2i_private_key NULL
  470. # define x25519_d2i_public_key NULL
  471. # define x25519_d2i_key_params NULL
  472. # define x25519_d2i_PKCS8 ecx_d2i_PKCS8
  473. # define x25519_free (free_key_fn *)ossl_ecx_key_free
  474. # define x25519_check NULL
  475. # define x25519_adjust ecx_key_adjust
  476. # define x448_evp_type EVP_PKEY_X448
  477. # define x448_d2i_private_key NULL
  478. # define x448_d2i_public_key NULL
  479. # define x448_d2i_key_params NULL
  480. # define x448_d2i_PKCS8 ecx_d2i_PKCS8
  481. # define x448_free (free_key_fn *)ossl_ecx_key_free
  482. # define x448_check NULL
  483. # define x448_adjust ecx_key_adjust
  484. # endif /* OPENSSL_NO_ECX */
  485. # ifndef OPENSSL_NO_SM2
  486. # define sm2_evp_type EVP_PKEY_SM2
  487. # define sm2_d2i_private_key (d2i_of_void *)d2i_ECPrivateKey
  488. # define sm2_d2i_public_key NULL
  489. # define sm2_d2i_key_params (d2i_of_void *)d2i_ECParameters
  490. # define sm2_d2i_PUBKEY ec_d2i_PUBKEY
  491. # define sm2_free (free_key_fn *)EC_KEY_free
  492. # define sm2_check ec_check
  493. # define sm2_adjust ec_adjust
  494. static void *sm2_d2i_PKCS8(const unsigned char **der, long der_len,
  495. struct der2key_ctx_st *ctx)
  496. {
  497. return der2key_decode_p8(der, der_len, ctx,
  498. (key_from_pkcs8_t *)ossl_ec_key_from_pkcs8);
  499. }
  500. # endif
  501. #endif
  502. /* ---------------------------------------------------------------------- */
  503. #ifndef OPENSSL_NO_ML_KEM
  504. static void *
  505. ml_kem_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
  506. {
  507. ML_KEM_KEY *key;
  508. key = ossl_ml_kem_d2i_PKCS8(*der, der_len, ctx->desc->evp_type,
  509. ctx->provctx, ctx->propq);
  510. if (key != NULL)
  511. *der += der_len;
  512. return key;
  513. }
  514. static ossl_inline void *
  515. ml_kem_d2i_PUBKEY(const uint8_t **der, long der_len,
  516. struct der2key_ctx_st *ctx)
  517. {
  518. ML_KEM_KEY *key;
  519. key = ossl_ml_kem_d2i_PUBKEY(*der, der_len, ctx->desc->evp_type,
  520. ctx->provctx, ctx->propq);
  521. if (key != NULL)
  522. *der += der_len;
  523. return key;
  524. }
  525. # define ml_kem_512_evp_type EVP_PKEY_ML_KEM_512
  526. # define ml_kem_512_d2i_private_key NULL
  527. # define ml_kem_512_d2i_public_key NULL
  528. # define ml_kem_512_d2i_key_params NULL
  529. # define ml_kem_512_d2i_PUBKEY ml_kem_d2i_PUBKEY
  530. # define ml_kem_512_d2i_PKCS8 ml_kem_d2i_PKCS8
  531. # define ml_kem_512_free (free_key_fn *)ossl_ml_kem_key_free
  532. # define ml_kem_512_check NULL
  533. # define ml_kem_512_adjust NULL
  534. # define ml_kem_768_evp_type EVP_PKEY_ML_KEM_768
  535. # define ml_kem_768_d2i_private_key NULL
  536. # define ml_kem_768_d2i_public_key NULL
  537. # define ml_kem_768_d2i_key_params NULL
  538. # define ml_kem_768_d2i_PUBKEY ml_kem_d2i_PUBKEY
  539. # define ml_kem_768_d2i_PKCS8 ml_kem_d2i_PKCS8
  540. # define ml_kem_768_free (free_key_fn *)ossl_ml_kem_key_free
  541. # define ml_kem_768_check NULL
  542. # define ml_kem_768_adjust NULL
  543. # define ml_kem_1024_evp_type EVP_PKEY_ML_KEM_1024
  544. # define ml_kem_1024_d2i_private_key NULL
  545. # define ml_kem_1024_d2i_public_key NULL
  546. # define ml_kem_1024_d2i_PUBKEY ml_kem_d2i_PUBKEY
  547. # define ml_kem_1024_d2i_PKCS8 ml_kem_d2i_PKCS8
  548. # define ml_kem_1024_d2i_key_params NULL
  549. # define ml_kem_1024_free (free_key_fn *)ossl_ml_kem_key_free
  550. # define ml_kem_1024_check NULL
  551. # define ml_kem_1024_adjust NULL
  552. #endif
  553. #ifndef OPENSSL_NO_SLH_DSA
  554. static void *
  555. slh_dsa_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
  556. {
  557. SLH_DSA_KEY *key = NULL, *ret = NULL;
  558. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  559. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  560. const unsigned char *p;
  561. const X509_ALGOR *alg = NULL;
  562. int plen, ptype;
  563. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, der, der_len)) == NULL
  564. || !PKCS8_pkey_get0(NULL, &p, &plen, &alg, p8inf))
  565. goto end;
  566. /* Algorithm parameters must be absent. */
  567. if ((X509_ALGOR_get0(NULL, &ptype, NULL, alg), ptype != V_ASN1_UNDEF)) {
  568. ERR_raise_data(ERR_LIB_PROV, PROV_R_UNEXPECTED_KEY_PARAMETERS,
  569. "unexpected parameters with a PKCS#8 %s private key",
  570. ctx->desc->keytype_name);
  571. goto end;
  572. }
  573. if (OBJ_obj2nid(alg->algorithm) != ctx->desc->evp_type)
  574. goto end;
  575. if ((key = ossl_slh_dsa_key_new(libctx, ctx->propq,
  576. ctx->desc->keytype_name)) == NULL)
  577. goto end;
  578. if (!ossl_slh_dsa_set_priv(key, p, plen))
  579. goto end;
  580. ret = key;
  581. end:
  582. PKCS8_PRIV_KEY_INFO_free(p8inf);
  583. if (ret == NULL)
  584. ossl_slh_dsa_key_free(key);
  585. return ret;
  586. }
  587. static ossl_inline void *slh_dsa_d2i_PUBKEY(const uint8_t **der, long der_len,
  588. struct der2key_ctx_st *ctx)
  589. {
  590. int ok = 0;
  591. OSSL_LIB_CTX *libctx = PROV_LIBCTX_OF(ctx->provctx);
  592. SLH_DSA_KEY *ret = NULL;
  593. BARE_PUBKEY *spki = NULL;
  594. const uint8_t *end = *der;
  595. size_t len;
  596. ret = ossl_slh_dsa_key_new(libctx, ctx->propq, ctx->desc->keytype_name);
  597. if (ret == NULL)
  598. return NULL;
  599. len = ossl_slh_dsa_key_get_pub_len(ret);
  600. /*-
  601. * The DER ASN.1 encoding of SLH-DSA public keys prepends 18 bytes to the
  602. * encoded public key (since the largest public key size is 64 bytes):
  603. *
  604. * - 2 byte outer sequence tag and length
  605. * - 2 byte algorithm sequence tag and length
  606. * - 2 byte algorithm OID tag and length
  607. * - 9 byte algorithm OID
  608. * - 2 byte bit string tag and length
  609. * - 1 bitstring lead byte
  610. *
  611. * Check that we have the right OID, the bit string has no "bits left" and
  612. * that we consume all the input exactly.
  613. */
  614. if (der_len != 18 + (long)len) {
  615. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  616. "unexpected %s public key length: %ld != %ld",
  617. ctx->desc->keytype_name, der_len,
  618. 18 + (long)len);
  619. goto err;
  620. }
  621. if ((spki = OPENSSL_zalloc(sizeof(*spki))) == NULL)
  622. goto err;
  623. /* The spki storage is freed on error */
  624. if (ASN1_item_d2i_ex((ASN1_VALUE **)&spki, &end, der_len,
  625. ASN1_ITEM_rptr(BARE_PUBKEY), NULL, NULL) == NULL) {
  626. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  627. "malformed %s public key ASN.1 encoding",
  628. ossl_slh_dsa_key_get_name(ret));
  629. goto err;
  630. }
  631. /* The spki structure now owns some memory */
  632. if ((spki->pubkey->flags & 0x7) != 0 || end != *der + der_len) {
  633. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  634. "malformed %s public key ASN.1 encoding",
  635. ossl_slh_dsa_key_get_name(ret));
  636. goto err;
  637. }
  638. if (OBJ_cmp(OBJ_nid2obj(ctx->desc->evp_type), spki->algor.oid) != 0) {
  639. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  640. "unexpected algorithm OID for an %s public key",
  641. ossl_slh_dsa_key_get_name(ret));
  642. goto err;
  643. }
  644. if (!ossl_slh_dsa_set_pub(ret, spki->pubkey->data, spki->pubkey->length)) {
  645. ERR_raise_data(ERR_LIB_PROV, PROV_R_BAD_ENCODING,
  646. "failed to parse %s public key from the input data",
  647. ossl_slh_dsa_key_get_name(ret));
  648. goto err;
  649. }
  650. ok = 1;
  651. err:
  652. if (spki != NULL) {
  653. ASN1_OBJECT_free(spki->algor.oid);
  654. ASN1_BIT_STRING_free(spki->pubkey);
  655. OPENSSL_free(spki);
  656. }
  657. if (!ok) {
  658. ossl_slh_dsa_key_free(ret);
  659. ret = NULL;
  660. }
  661. return ret;
  662. }
  663. # define slh_dsa_sha2_128s_evp_type EVP_PKEY_SLH_DSA_SHA2_128S
  664. # define slh_dsa_sha2_128s_d2i_private_key NULL
  665. # define slh_dsa_sha2_128s_d2i_public_key NULL
  666. # define slh_dsa_sha2_128s_d2i_key_params NULL
  667. # define slh_dsa_sha2_128s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  668. # define slh_dsa_sha2_128s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  669. # define slh_dsa_sha2_128s_free (free_key_fn *)ossl_slh_dsa_key_free
  670. # define slh_dsa_sha2_128s_check NULL
  671. # define slh_dsa_sha2_128s_adjust NULL
  672. # define slh_dsa_sha2_128f_evp_type EVP_PKEY_SLH_DSA_SHA2_128F
  673. # define slh_dsa_sha2_128f_d2i_private_key NULL
  674. # define slh_dsa_sha2_128f_d2i_public_key NULL
  675. # define slh_dsa_sha2_128f_d2i_key_params NULL
  676. # define slh_dsa_sha2_128f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  677. # define slh_dsa_sha2_128f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  678. # define slh_dsa_sha2_128f_free (free_key_fn *)ossl_slh_dsa_key_free
  679. # define slh_dsa_sha2_128f_check NULL
  680. # define slh_dsa_sha2_128f_adjust NULL
  681. # define slh_dsa_sha2_192s_evp_type EVP_PKEY_SLH_DSA_SHA2_192S
  682. # define slh_dsa_sha2_192s_d2i_private_key NULL
  683. # define slh_dsa_sha2_192s_d2i_public_key NULL
  684. # define slh_dsa_sha2_192s_d2i_key_params NULL
  685. # define slh_dsa_sha2_192s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  686. # define slh_dsa_sha2_192s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  687. # define slh_dsa_sha2_192s_free (free_key_fn *)ossl_slh_dsa_key_free
  688. # define slh_dsa_sha2_192s_check NULL
  689. # define slh_dsa_sha2_192s_adjust NULL
  690. # define slh_dsa_sha2_192f_evp_type EVP_PKEY_SLH_DSA_SHA2_192F
  691. # define slh_dsa_sha2_192f_d2i_private_key NULL
  692. # define slh_dsa_sha2_192f_d2i_public_key NULL
  693. # define slh_dsa_sha2_192f_d2i_key_params NULL
  694. # define slh_dsa_sha2_192f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  695. # define slh_dsa_sha2_192f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  696. # define slh_dsa_sha2_192f_free (free_key_fn *)ossl_slh_dsa_key_free
  697. # define slh_dsa_sha2_192f_check NULL
  698. # define slh_dsa_sha2_192f_adjust NULL
  699. # define slh_dsa_sha2_256s_evp_type EVP_PKEY_SLH_DSA_SHA2_256S
  700. # define slh_dsa_sha2_256s_d2i_private_key NULL
  701. # define slh_dsa_sha2_256s_d2i_public_key NULL
  702. # define slh_dsa_sha2_256s_d2i_key_params NULL
  703. # define slh_dsa_sha2_256s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  704. # define slh_dsa_sha2_256s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  705. # define slh_dsa_sha2_256s_free (free_key_fn *)ossl_slh_dsa_key_free
  706. # define slh_dsa_sha2_256s_check NULL
  707. # define slh_dsa_sha2_256s_adjust NULL
  708. # define slh_dsa_sha2_256f_evp_type EVP_PKEY_SLH_DSA_SHA2_256F
  709. # define slh_dsa_sha2_256f_d2i_private_key NULL
  710. # define slh_dsa_sha2_256f_d2i_public_key NULL
  711. # define slh_dsa_sha2_256f_d2i_key_params NULL
  712. # define slh_dsa_sha2_256f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  713. # define slh_dsa_sha2_256f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  714. # define slh_dsa_sha2_256f_free (free_key_fn *)ossl_slh_dsa_key_free
  715. # define slh_dsa_sha2_256f_check NULL
  716. # define slh_dsa_sha2_256f_adjust NULL
  717. # define slh_dsa_shake_128s_evp_type EVP_PKEY_SLH_DSA_SHAKE_128S
  718. # define slh_dsa_shake_128s_d2i_private_key NULL
  719. # define slh_dsa_shake_128s_d2i_public_key NULL
  720. # define slh_dsa_shake_128s_d2i_key_params NULL
  721. # define slh_dsa_shake_128s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  722. # define slh_dsa_shake_128s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  723. # define slh_dsa_shake_128s_free (free_key_fn *)ossl_slh_dsa_key_free
  724. # define slh_dsa_shake_128s_check NULL
  725. # define slh_dsa_shake_128s_adjust NULL
  726. # define slh_dsa_shake_128f_evp_type EVP_PKEY_SLH_DSA_SHAKE_128F
  727. # define slh_dsa_shake_128f_d2i_private_key NULL
  728. # define slh_dsa_shake_128f_d2i_public_key NULL
  729. # define slh_dsa_shake_128f_d2i_key_params NULL
  730. # define slh_dsa_shake_128f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  731. # define slh_dsa_shake_128f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  732. # define slh_dsa_shake_128f_free (free_key_fn *)ossl_slh_dsa_key_free
  733. # define slh_dsa_shake_128f_check NULL
  734. # define slh_dsa_shake_128f_adjust NULL
  735. # define slh_dsa_shake_192s_evp_type EVP_PKEY_SLH_DSA_SHAKE_192S
  736. # define slh_dsa_shake_192s_d2i_private_key NULL
  737. # define slh_dsa_shake_192s_d2i_public_key NULL
  738. # define slh_dsa_shake_192s_d2i_key_params NULL
  739. # define slh_dsa_shake_192s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  740. # define slh_dsa_shake_192s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  741. # define slh_dsa_shake_192s_free (free_key_fn *)ossl_slh_dsa_key_free
  742. # define slh_dsa_shake_192s_check NULL
  743. # define slh_dsa_shake_192s_adjust NULL
  744. # define slh_dsa_shake_192f_evp_type EVP_PKEY_SLH_DSA_SHAKE_192F
  745. # define slh_dsa_shake_192f_d2i_private_key NULL
  746. # define slh_dsa_shake_192f_d2i_public_key NULL
  747. # define slh_dsa_shake_192f_d2i_key_params NULL
  748. # define slh_dsa_shake_192f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  749. # define slh_dsa_shake_192f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  750. # define slh_dsa_shake_192f_free (free_key_fn *)ossl_slh_dsa_key_free
  751. # define slh_dsa_shake_192f_check NULL
  752. # define slh_dsa_shake_192f_adjust NULL
  753. # define slh_dsa_shake_256s_evp_type EVP_PKEY_SLH_DSA_SHAKE_256S
  754. # define slh_dsa_shake_256s_d2i_private_key NULL
  755. # define slh_dsa_shake_256s_d2i_public_key NULL
  756. # define slh_dsa_shake_256s_d2i_key_params NULL
  757. # define slh_dsa_shake_256s_d2i_PKCS8 slh_dsa_d2i_PKCS8
  758. # define slh_dsa_shake_256s_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  759. # define slh_dsa_shake_256s_free (free_key_fn *)ossl_slh_dsa_key_free
  760. # define slh_dsa_shake_256s_check NULL
  761. # define slh_dsa_shake_256s_adjust NULL
  762. # define slh_dsa_shake_256f_evp_type EVP_PKEY_SLH_DSA_SHAKE_256F
  763. # define slh_dsa_shake_256f_d2i_private_key NULL
  764. # define slh_dsa_shake_256f_d2i_public_key NULL
  765. # define slh_dsa_shake_256f_d2i_key_params NULL
  766. # define slh_dsa_shake_256f_d2i_PKCS8 slh_dsa_d2i_PKCS8
  767. # define slh_dsa_shake_256f_d2i_PUBKEY slh_dsa_d2i_PUBKEY
  768. # define slh_dsa_shake_256f_free (free_key_fn *)ossl_slh_dsa_key_free
  769. # define slh_dsa_shake_256f_check NULL
  770. # define slh_dsa_shake_256f_adjust NULL
  771. #endif /* OPENSSL_NO_SLH_DSA */
  772. /* ---------------------------------------------------------------------- */
  773. #define rsa_evp_type EVP_PKEY_RSA
  774. #define rsa_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
  775. #define rsa_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
  776. #define rsa_d2i_key_params NULL
  777. #define rsa_free (free_key_fn *)RSA_free
  778. static void *rsa_d2i_PKCS8(const unsigned char **der, long der_len,
  779. struct der2key_ctx_st *ctx)
  780. {
  781. return der2key_decode_p8(der, der_len, ctx,
  782. (key_from_pkcs8_t *)ossl_rsa_key_from_pkcs8);
  783. }
  784. static void *
  785. rsa_d2i_PUBKEY(const unsigned char **der, long der_len,
  786. ossl_unused struct der2key_ctx_st *ctx)
  787. {
  788. return d2i_RSA_PUBKEY(NULL, der, der_len);
  789. }
  790. static int rsa_check(void *key, struct der2key_ctx_st *ctx)
  791. {
  792. int valid;
  793. switch (RSA_test_flags(key, RSA_FLAG_TYPE_MASK)) {
  794. case RSA_FLAG_TYPE_RSA:
  795. valid = (ctx->desc->evp_type == EVP_PKEY_RSA);
  796. break;
  797. case RSA_FLAG_TYPE_RSASSAPSS:
  798. valid = (ctx->desc->evp_type == EVP_PKEY_RSA_PSS);
  799. break;
  800. default:
  801. /* Currently unsupported RSA key type */
  802. valid = 0;
  803. }
  804. valid = (valid && ossl_rsa_check_factors(key));
  805. return valid;
  806. }
  807. static void rsa_adjust(void *key, struct der2key_ctx_st *ctx)
  808. {
  809. ossl_rsa_set0_libctx(key, PROV_LIBCTX_OF(ctx->provctx));
  810. }
  811. #define rsapss_evp_type EVP_PKEY_RSA_PSS
  812. #define rsapss_d2i_private_key (d2i_of_void *)d2i_RSAPrivateKey
  813. #define rsapss_d2i_public_key (d2i_of_void *)d2i_RSAPublicKey
  814. #define rsapss_d2i_key_params NULL
  815. #define rsapss_d2i_PKCS8 rsa_d2i_PKCS8
  816. #define rsapss_d2i_PUBKEY rsa_d2i_PUBKEY
  817. #define rsapss_free (free_key_fn *)RSA_free
  818. #define rsapss_check rsa_check
  819. #define rsapss_adjust rsa_adjust
  820. /* ---------------------------------------------------------------------- */
  821. #ifndef OPENSSL_NO_ML_DSA
  822. static void *
  823. ml_dsa_d2i_PKCS8(const uint8_t **der, long der_len, struct der2key_ctx_st *ctx)
  824. {
  825. ML_DSA_KEY *key;
  826. key = ossl_ml_dsa_d2i_PKCS8(*der, der_len, ctx->desc->evp_type,
  827. ctx->provctx, ctx->propq);
  828. if (key != NULL)
  829. *der += der_len;
  830. return key;
  831. }
  832. static ossl_inline void * ml_dsa_d2i_PUBKEY(const uint8_t **der, long der_len,
  833. struct der2key_ctx_st *ctx)
  834. {
  835. ML_DSA_KEY *key;
  836. key = ossl_ml_dsa_d2i_PUBKEY(*der, der_len, ctx->desc->evp_type,
  837. ctx->provctx, ctx->propq);
  838. if (key != NULL)
  839. *der += der_len;
  840. return key;
  841. }
  842. # define ml_dsa_44_evp_type EVP_PKEY_ML_DSA_44
  843. # define ml_dsa_44_d2i_private_key NULL
  844. # define ml_dsa_44_d2i_public_key NULL
  845. # define ml_dsa_44_d2i_key_params NULL
  846. # define ml_dsa_44_d2i_PUBKEY ml_dsa_d2i_PUBKEY
  847. # define ml_dsa_44_d2i_PKCS8 ml_dsa_d2i_PKCS8
  848. # define ml_dsa_44_free (free_key_fn *)ossl_ml_dsa_key_free
  849. # define ml_dsa_44_check NULL
  850. # define ml_dsa_44_adjust NULL
  851. # define ml_dsa_65_evp_type EVP_PKEY_ML_DSA_65
  852. # define ml_dsa_65_d2i_private_key NULL
  853. # define ml_dsa_65_d2i_public_key NULL
  854. # define ml_dsa_65_d2i_key_params NULL
  855. # define ml_dsa_65_d2i_PUBKEY ml_dsa_d2i_PUBKEY
  856. # define ml_dsa_65_d2i_PKCS8 ml_dsa_d2i_PKCS8
  857. # define ml_dsa_65_free (free_key_fn *)ossl_ml_dsa_key_free
  858. # define ml_dsa_65_check NULL
  859. # define ml_dsa_65_adjust NULL
  860. # define ml_dsa_87_evp_type EVP_PKEY_ML_DSA_87
  861. # define ml_dsa_87_d2i_private_key NULL
  862. # define ml_dsa_87_d2i_public_key NULL
  863. # define ml_dsa_87_d2i_PUBKEY ml_dsa_d2i_PUBKEY
  864. # define ml_dsa_87_d2i_PKCS8 ml_dsa_d2i_PKCS8
  865. # define ml_dsa_87_d2i_key_params NULL
  866. # define ml_dsa_87_free (free_key_fn *)ossl_ml_dsa_key_free
  867. # define ml_dsa_87_check NULL
  868. # define ml_dsa_87_adjust NULL
  869. #endif
  870. /* ---------------------------------------------------------------------- */
  871. /*
  872. * The DO_ macros help define the selection mask and the method functions
  873. * for each kind of object we want to decode.
  874. */
  875. #define DO_type_specific_keypair(keytype) \
  876. "type-specific", keytype##_evp_type, \
  877. ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
  878. keytype##_d2i_private_key, \
  879. keytype##_d2i_public_key, \
  880. NULL, \
  881. NULL, \
  882. NULL, \
  883. keytype##_check, \
  884. keytype##_adjust, \
  885. keytype##_free
  886. #define DO_type_specific_pub(keytype) \
  887. "type-specific", keytype##_evp_type, \
  888. ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
  889. NULL, \
  890. keytype##_d2i_public_key, \
  891. NULL, \
  892. NULL, \
  893. NULL, \
  894. keytype##_check, \
  895. keytype##_adjust, \
  896. keytype##_free
  897. #define DO_type_specific_priv(keytype) \
  898. "type-specific", keytype##_evp_type, \
  899. ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
  900. keytype##_d2i_private_key, \
  901. NULL, \
  902. NULL, \
  903. NULL, \
  904. NULL, \
  905. keytype##_check, \
  906. keytype##_adjust, \
  907. keytype##_free
  908. #define DO_type_specific_params(keytype) \
  909. "type-specific", keytype##_evp_type, \
  910. ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
  911. NULL, \
  912. NULL, \
  913. keytype##_d2i_key_params, \
  914. NULL, \
  915. NULL, \
  916. keytype##_check, \
  917. keytype##_adjust, \
  918. keytype##_free
  919. #define DO_type_specific(keytype) \
  920. "type-specific", keytype##_evp_type, \
  921. ( OSSL_KEYMGMT_SELECT_ALL ), \
  922. keytype##_d2i_private_key, \
  923. keytype##_d2i_public_key, \
  924. keytype##_d2i_key_params, \
  925. NULL, \
  926. NULL, \
  927. keytype##_check, \
  928. keytype##_adjust, \
  929. keytype##_free
  930. #define DO_type_specific_no_pub(keytype) \
  931. "type-specific", keytype##_evp_type, \
  932. ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
  933. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
  934. keytype##_d2i_private_key, \
  935. NULL, \
  936. keytype##_d2i_key_params, \
  937. NULL, \
  938. NULL, \
  939. keytype##_check, \
  940. keytype##_adjust, \
  941. keytype##_free
  942. #define DO_PrivateKeyInfo(keytype) \
  943. "PrivateKeyInfo", keytype##_evp_type, \
  944. ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
  945. NULL, \
  946. NULL, \
  947. NULL, \
  948. keytype##_d2i_PKCS8, \
  949. NULL, \
  950. keytype##_check, \
  951. keytype##_adjust, \
  952. keytype##_free
  953. #define DO_SubjectPublicKeyInfo(keytype) \
  954. "SubjectPublicKeyInfo", keytype##_evp_type, \
  955. ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
  956. NULL, \
  957. NULL, \
  958. NULL, \
  959. NULL, \
  960. keytype##_d2i_PUBKEY, \
  961. keytype##_check, \
  962. keytype##_adjust, \
  963. keytype##_free
  964. #define DO_DH(keytype) \
  965. "DH", keytype##_evp_type, \
  966. ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
  967. NULL, \
  968. NULL, \
  969. keytype##_d2i_key_params, \
  970. NULL, \
  971. NULL, \
  972. keytype##_check, \
  973. keytype##_adjust, \
  974. keytype##_free
  975. #define DO_DHX(keytype) \
  976. "DHX", keytype##_evp_type, \
  977. ( OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
  978. NULL, \
  979. NULL, \
  980. keytype##_d2i_key_params, \
  981. NULL, \
  982. NULL, \
  983. keytype##_check, \
  984. keytype##_adjust, \
  985. keytype##_free
  986. #define DO_DSA(keytype) \
  987. "DSA", keytype##_evp_type, \
  988. ( OSSL_KEYMGMT_SELECT_ALL ), \
  989. keytype##_d2i_private_key, \
  990. keytype##_d2i_public_key, \
  991. keytype##_d2i_key_params, \
  992. NULL, \
  993. NULL, \
  994. keytype##_check, \
  995. keytype##_adjust, \
  996. keytype##_free
  997. #define DO_EC(keytype) \
  998. "EC", keytype##_evp_type, \
  999. ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY \
  1000. | OSSL_KEYMGMT_SELECT_ALL_PARAMETERS ), \
  1001. keytype##_d2i_private_key, \
  1002. NULL, \
  1003. keytype##_d2i_key_params, \
  1004. NULL, \
  1005. NULL, \
  1006. keytype##_check, \
  1007. keytype##_adjust, \
  1008. keytype##_free
  1009. #define DO_RSA(keytype) \
  1010. "RSA", keytype##_evp_type, \
  1011. ( OSSL_KEYMGMT_SELECT_KEYPAIR ), \
  1012. keytype##_d2i_private_key, \
  1013. keytype##_d2i_public_key, \
  1014. NULL, \
  1015. NULL, \
  1016. NULL, \
  1017. keytype##_check, \
  1018. keytype##_adjust, \
  1019. keytype##_free
  1020. /*
  1021. * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
  1022. * It takes the following arguments:
  1023. *
  1024. * keytype_name The implementation key type as a string.
  1025. * keytype The implementation key type. This must correspond exactly
  1026. * to our existing keymgmt keytype names... in other words,
  1027. * there must exist an ossl_##keytype##_keymgmt_functions.
  1028. * type The type name for the set of functions that implement the
  1029. * decoder for the key type. This isn't necessarily the same
  1030. * as keytype. For example, the key types ed25519, ed448,
  1031. * x25519 and x448 are all handled by the same functions with
  1032. * the common type name ecx.
  1033. * kind The kind of support to implement. This translates into
  1034. * the DO_##kind macros above, to populate the keytype_desc_st
  1035. * structure.
  1036. */
  1037. #define MAKE_DECODER(keytype_name, keytype, type, kind) \
  1038. static const struct keytype_desc_st kind##_##keytype##_desc = \
  1039. { keytype_name, ossl_##keytype##_keymgmt_functions, \
  1040. DO_##kind(keytype) }; \
  1041. \
  1042. static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \
  1043. \
  1044. static void *kind##_der2##keytype##_newctx(void *provctx) \
  1045. { \
  1046. return der2key_newctx(provctx, &kind##_##keytype##_desc); \
  1047. } \
  1048. static int kind##_der2##keytype##_does_selection(void *provctx, \
  1049. int selection) \
  1050. { \
  1051. return der2key_check_selection(selection, \
  1052. &kind##_##keytype##_desc); \
  1053. } \
  1054. const OSSL_DISPATCH \
  1055. ossl_##kind##_der_to_##keytype##_decoder_functions[] = { \
  1056. { OSSL_FUNC_DECODER_NEWCTX, \
  1057. (void (*)(void))kind##_der2##keytype##_newctx }, \
  1058. { OSSL_FUNC_DECODER_FREECTX, \
  1059. (void (*)(void))der2key_freectx }, \
  1060. { OSSL_FUNC_DECODER_DOES_SELECTION, \
  1061. (void (*)(void))kind##_der2##keytype##_does_selection }, \
  1062. { OSSL_FUNC_DECODER_DECODE, \
  1063. (void (*)(void))der2key_decode }, \
  1064. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  1065. (void (*)(void))der2key_export_object }, \
  1066. { OSSL_FUNC_DECODER_SETTABLE_CTX_PARAMS, \
  1067. (void (*)(void))der2key_settable_ctx_params }, \
  1068. { OSSL_FUNC_DECODER_SET_CTX_PARAMS, \
  1069. (void (*)(void))der2key_set_ctx_params }, \
  1070. OSSL_DISPATCH_END \
  1071. }
  1072. #ifndef OPENSSL_NO_DH
  1073. MAKE_DECODER("DH", dh, dh, PrivateKeyInfo);
  1074. MAKE_DECODER("DH", dh, dh, SubjectPublicKeyInfo);
  1075. MAKE_DECODER("DH", dh, dh, type_specific_params);
  1076. MAKE_DECODER("DH", dh, dh, DH);
  1077. MAKE_DECODER("DHX", dhx, dhx, PrivateKeyInfo);
  1078. MAKE_DECODER("DHX", dhx, dhx, SubjectPublicKeyInfo);
  1079. MAKE_DECODER("DHX", dhx, dhx, type_specific_params);
  1080. MAKE_DECODER("DHX", dhx, dhx, DHX);
  1081. #endif
  1082. #ifndef OPENSSL_NO_DSA
  1083. MAKE_DECODER("DSA", dsa, dsa, PrivateKeyInfo);
  1084. MAKE_DECODER("DSA", dsa, dsa, SubjectPublicKeyInfo);
  1085. MAKE_DECODER("DSA", dsa, dsa, type_specific);
  1086. MAKE_DECODER("DSA", dsa, dsa, DSA);
  1087. #endif
  1088. #ifndef OPENSSL_NO_EC
  1089. MAKE_DECODER("EC", ec, ec, PrivateKeyInfo);
  1090. MAKE_DECODER("EC", ec, ec, SubjectPublicKeyInfo);
  1091. MAKE_DECODER("EC", ec, ec, type_specific_no_pub);
  1092. MAKE_DECODER("EC", ec, ec, EC);
  1093. # ifndef OPENSSL_NO_ECX
  1094. MAKE_DECODER("X25519", x25519, ecx, PrivateKeyInfo);
  1095. MAKE_DECODER("X25519", x25519, ecx, SubjectPublicKeyInfo);
  1096. MAKE_DECODER("X448", x448, ecx, PrivateKeyInfo);
  1097. MAKE_DECODER("X448", x448, ecx, SubjectPublicKeyInfo);
  1098. MAKE_DECODER("ED25519", ed25519, ecx, PrivateKeyInfo);
  1099. MAKE_DECODER("ED25519", ed25519, ecx, SubjectPublicKeyInfo);
  1100. MAKE_DECODER("ED448", ed448, ecx, PrivateKeyInfo);
  1101. MAKE_DECODER("ED448", ed448, ecx, SubjectPublicKeyInfo);
  1102. # endif
  1103. # ifndef OPENSSL_NO_SM2
  1104. MAKE_DECODER("SM2", sm2, ec, PrivateKeyInfo);
  1105. MAKE_DECODER("SM2", sm2, ec, SubjectPublicKeyInfo);
  1106. MAKE_DECODER("SM2", sm2, sm2, type_specific_no_pub);
  1107. # endif
  1108. #endif
  1109. #ifndef OPENSSL_NO_ML_KEM
  1110. MAKE_DECODER("ML-KEM-512", ml_kem_512, ml_kem_512, PrivateKeyInfo);
  1111. MAKE_DECODER("ML-KEM-512", ml_kem_512, ml_kem_512, SubjectPublicKeyInfo);
  1112. MAKE_DECODER("ML-KEM-768", ml_kem_768, ml_kem_768, PrivateKeyInfo);
  1113. MAKE_DECODER("ML-KEM-768", ml_kem_768, ml_kem_768, SubjectPublicKeyInfo);
  1114. MAKE_DECODER("ML-KEM-1024", ml_kem_1024, ml_kem_1024, PrivateKeyInfo);
  1115. MAKE_DECODER("ML-KEM-1024", ml_kem_1024, ml_kem_1024, SubjectPublicKeyInfo);
  1116. #endif
  1117. #ifndef OPENSSL_NO_SLH_DSA
  1118. MAKE_DECODER("SLH-DSA-SHA2-128s", slh_dsa_sha2_128s, slh_dsa, PrivateKeyInfo);
  1119. MAKE_DECODER("SLH-DSA-SHA2-128f", slh_dsa_sha2_128f, slh_dsa, PrivateKeyInfo);
  1120. MAKE_DECODER("SLH-DSA-SHA2-192s", slh_dsa_sha2_192s, slh_dsa, PrivateKeyInfo);
  1121. MAKE_DECODER("SLH-DSA-SHA2-192f", slh_dsa_sha2_192f, slh_dsa, PrivateKeyInfo);
  1122. MAKE_DECODER("SLH-DSA-SHA2-256s", slh_dsa_sha2_256s, slh_dsa, PrivateKeyInfo);
  1123. MAKE_DECODER("SLH-DSA-SHA2-256f", slh_dsa_sha2_256f, slh_dsa, PrivateKeyInfo);
  1124. MAKE_DECODER("SLH-DSA-SHAKE-128s", slh_dsa_shake_128s, slh_dsa, PrivateKeyInfo);
  1125. MAKE_DECODER("SLH-DSA-SHAKE-128f", slh_dsa_shake_128f, slh_dsa, PrivateKeyInfo);
  1126. MAKE_DECODER("SLH-DSA-SHAKE-192s", slh_dsa_shake_192s, slh_dsa, PrivateKeyInfo);
  1127. MAKE_DECODER("SLH-DSA-SHAKE-192f", slh_dsa_shake_192f, slh_dsa, PrivateKeyInfo);
  1128. MAKE_DECODER("SLH-DSA-SHAKE-256s", slh_dsa_shake_256s, slh_dsa, PrivateKeyInfo);
  1129. MAKE_DECODER("SLH-DSA-SHAKE-256f", slh_dsa_shake_256f, slh_dsa, PrivateKeyInfo);
  1130. MAKE_DECODER("SLH-DSA-SHA2-128s", slh_dsa_sha2_128s, slh_dsa, SubjectPublicKeyInfo);
  1131. MAKE_DECODER("SLH-DSA-SHA2-128f", slh_dsa_sha2_128f, slh_dsa, SubjectPublicKeyInfo);
  1132. MAKE_DECODER("SLH-DSA-SHA2-192s", slh_dsa_sha2_192s, slh_dsa, SubjectPublicKeyInfo);
  1133. MAKE_DECODER("SLH-DSA-SHA2-192f", slh_dsa_sha2_192f, slh_dsa, SubjectPublicKeyInfo);
  1134. MAKE_DECODER("SLH-DSA-SHA2-256s", slh_dsa_sha2_256s, slh_dsa, SubjectPublicKeyInfo);
  1135. MAKE_DECODER("SLH-DSA-SHA2-256f", slh_dsa_sha2_256f, slh_dsa, SubjectPublicKeyInfo);
  1136. MAKE_DECODER("SLH-DSA-SHAKE-128s", slh_dsa_shake_128s, slh_dsa, SubjectPublicKeyInfo);
  1137. MAKE_DECODER("SLH-DSA-SHAKE-128f", slh_dsa_shake_128f, slh_dsa, SubjectPublicKeyInfo);
  1138. MAKE_DECODER("SLH-DSA-SHAKE-192s", slh_dsa_shake_192s, slh_dsa, SubjectPublicKeyInfo);
  1139. MAKE_DECODER("SLH-DSA-SHAKE-192f", slh_dsa_shake_192f, slh_dsa, SubjectPublicKeyInfo);
  1140. MAKE_DECODER("SLH-DSA-SHAKE-256s", slh_dsa_shake_256s, slh_dsa, SubjectPublicKeyInfo);
  1141. MAKE_DECODER("SLH-DSA-SHAKE-256f", slh_dsa_shake_256f, slh_dsa, SubjectPublicKeyInfo);
  1142. #endif /* OPENSSL_NO_SLH_DSA */
  1143. MAKE_DECODER("RSA", rsa, rsa, PrivateKeyInfo);
  1144. MAKE_DECODER("RSA", rsa, rsa, SubjectPublicKeyInfo);
  1145. MAKE_DECODER("RSA", rsa, rsa, type_specific_keypair);
  1146. MAKE_DECODER("RSA", rsa, rsa, RSA);
  1147. MAKE_DECODER("RSA-PSS", rsapss, rsapss, PrivateKeyInfo);
  1148. MAKE_DECODER("RSA-PSS", rsapss, rsapss, SubjectPublicKeyInfo);
  1149. #ifndef OPENSSL_NO_ML_DSA
  1150. MAKE_DECODER("ML-DSA-44", ml_dsa_44, ml_dsa_44, PrivateKeyInfo);
  1151. MAKE_DECODER("ML-DSA-44", ml_dsa_44, ml_dsa_44, SubjectPublicKeyInfo);
  1152. MAKE_DECODER("ML-DSA-65", ml_dsa_65, ml_dsa_65, PrivateKeyInfo);
  1153. MAKE_DECODER("ML-DSA-65", ml_dsa_65, ml_dsa_65, SubjectPublicKeyInfo);
  1154. MAKE_DECODER("ML-DSA-87", ml_dsa_87, ml_dsa_87, PrivateKeyInfo);
  1155. MAKE_DECODER("ML-DSA-87", ml_dsa_87, ml_dsa_87, SubjectPublicKeyInfo);
  1156. #endif