p_lib.c 72 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554
  1. /*
  2. * Copyright 1995-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. * DSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <assert.h>
  15. #include <stdio.h>
  16. #include "internal/cryptlib.h"
  17. #include "internal/refcount.h"
  18. #include "internal/namemap.h"
  19. #include <openssl/bn.h>
  20. #include <openssl/err.h>
  21. #include <openssl/objects.h>
  22. #include <openssl/evp.h>
  23. #include <openssl/rsa.h>
  24. #include <openssl/dsa.h>
  25. #include <openssl/dh.h>
  26. #include <openssl/ec.h>
  27. #include <openssl/cmac.h>
  28. #ifndef FIPS_MODULE
  29. # include <openssl/engine.h>
  30. #endif
  31. #include <openssl/params.h>
  32. #include <openssl/param_build.h>
  33. #include <openssl/encoder.h>
  34. #include <openssl/core_names.h>
  35. #include "internal/numbers.h" /* includes SIZE_MAX */
  36. #include "internal/ffc.h"
  37. #include "crypto/evp.h"
  38. #include "crypto/dh.h"
  39. #include "crypto/dsa.h"
  40. #include "crypto/ec.h"
  41. #include "crypto/ecx.h"
  42. #include "crypto/rsa.h"
  43. #ifndef FIPS_MODULE
  44. # include "crypto/asn1.h"
  45. # include "crypto/x509.h"
  46. #endif
  47. #include "internal/provider.h"
  48. #include "evp_local.h"
  49. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  50. int len, EVP_KEYMGMT *keymgmt);
  51. static void evp_pkey_free_it(EVP_PKEY *key);
  52. /* The type of parameters selected in key parameter functions */
  53. # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  54. #ifndef FIPS_MODULE
  55. int EVP_PKEY_get_bits(const EVP_PKEY *pkey)
  56. {
  57. int size = 0;
  58. if (pkey != NULL) {
  59. size = pkey->cache.bits;
  60. if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
  61. size = pkey->ameth->pkey_bits(pkey);
  62. }
  63. if (size <= 0) {
  64. ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_BITS);
  65. return 0;
  66. }
  67. return size;
  68. }
  69. int EVP_PKEY_get_security_bits(const EVP_PKEY *pkey)
  70. {
  71. int size = 0;
  72. if (pkey != NULL) {
  73. size = pkey->cache.security_bits;
  74. if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
  75. size = pkey->ameth->pkey_security_bits(pkey);
  76. }
  77. if (size <= 0) {
  78. ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_SECURITY_BITS);
  79. return 0;
  80. }
  81. return size;
  82. }
  83. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  84. {
  85. # ifndef OPENSSL_NO_DSA
  86. if (pkey->type == EVP_PKEY_DSA) {
  87. int ret = pkey->save_parameters;
  88. if (mode >= 0)
  89. pkey->save_parameters = mode;
  90. return ret;
  91. }
  92. # endif
  93. # ifndef OPENSSL_NO_EC
  94. if (pkey->type == EVP_PKEY_EC) {
  95. int ret = pkey->save_parameters;
  96. if (mode >= 0)
  97. pkey->save_parameters = mode;
  98. return ret;
  99. }
  100. # endif
  101. return 0;
  102. }
  103. int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
  104. {
  105. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  106. }
  107. void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
  108. {
  109. return CRYPTO_get_ex_data(&key->ex_data, idx);
  110. }
  111. #endif /* !FIPS_MODULE */
  112. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  113. {
  114. /*
  115. * Clean up legacy stuff from this function when legacy support is gone.
  116. */
  117. EVP_PKEY *downgraded_from = NULL;
  118. int ok = 0;
  119. #ifndef FIPS_MODULE
  120. /*
  121. * If |to| is a legacy key and |from| isn't, we must make a downgraded
  122. * copy of |from|. If that fails, this function fails.
  123. */
  124. if (evp_pkey_is_legacy(to) && evp_pkey_is_provided(from)) {
  125. if (!evp_pkey_copy_downgraded(&downgraded_from, from))
  126. goto end;
  127. from = downgraded_from;
  128. }
  129. #endif /* !FIPS_MODULE */
  130. /*
  131. * Make sure |to| is typed. Content is less important at this early
  132. * stage.
  133. *
  134. * 1. If |to| is untyped, assign |from|'s key type to it.
  135. * 2. If |to| contains a legacy key, compare its |type| to |from|'s.
  136. * (|from| was already downgraded above)
  137. *
  138. * If |to| is a provided key, there's nothing more to do here, functions
  139. * like evp_keymgmt_util_copy() and evp_pkey_export_to_provider() called
  140. * further down help us find out if they are the same or not.
  141. */
  142. if (evp_pkey_is_blank(to)) {
  143. #ifndef FIPS_MODULE
  144. if (evp_pkey_is_legacy(from)) {
  145. if (EVP_PKEY_set_type(to, from->type) == 0)
  146. goto end;
  147. } else
  148. #endif /* !FIPS_MODULE */
  149. {
  150. if (EVP_PKEY_set_type_by_keymgmt(to, from->keymgmt) == 0)
  151. goto end;
  152. }
  153. }
  154. #ifndef FIPS_MODULE
  155. else if (evp_pkey_is_legacy(to)) {
  156. if (to->type != from->type) {
  157. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
  158. goto end;
  159. }
  160. }
  161. #endif /* !FIPS_MODULE */
  162. if (EVP_PKEY_missing_parameters(from)) {
  163. ERR_raise(ERR_LIB_EVP, EVP_R_MISSING_PARAMETERS);
  164. goto end;
  165. }
  166. if (!EVP_PKEY_missing_parameters(to)) {
  167. if (EVP_PKEY_parameters_eq(to, from) == 1)
  168. ok = 1;
  169. else
  170. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_PARAMETERS);
  171. goto end;
  172. }
  173. /* For purely provided keys, we just call the keymgmt utility */
  174. if (to->keymgmt != NULL && from->keymgmt != NULL) {
  175. ok = evp_keymgmt_util_copy(to, (EVP_PKEY *)from, SELECT_PARAMETERS);
  176. goto end;
  177. }
  178. #ifndef FIPS_MODULE
  179. /*
  180. * If |to| is provided, we know that |from| is legacy at this point.
  181. * Try exporting |from| to |to|'s keymgmt, then use evp_keymgmt_dup()
  182. * to copy the appropriate data to |to|'s keydata.
  183. * We cannot override existing data so do it only if there is no keydata
  184. * in |to| yet.
  185. */
  186. if (to->keymgmt != NULL && to->keydata == NULL) {
  187. EVP_KEYMGMT *to_keymgmt = to->keymgmt;
  188. void *from_keydata =
  189. evp_pkey_export_to_provider((EVP_PKEY *)from, NULL, &to_keymgmt,
  190. NULL);
  191. /*
  192. * If we get a NULL, it could be an internal error, or it could be
  193. * that there's a key mismatch. We're pretending the latter...
  194. */
  195. if (from_keydata == NULL)
  196. ERR_raise(ERR_LIB_EVP, EVP_R_DIFFERENT_KEY_TYPES);
  197. else
  198. ok = (to->keydata = evp_keymgmt_dup(to->keymgmt,
  199. from_keydata,
  200. SELECT_PARAMETERS)) != NULL;
  201. goto end;
  202. }
  203. /* Both keys are legacy */
  204. if (from->ameth != NULL && from->ameth->param_copy != NULL)
  205. ok = from->ameth->param_copy(to, from);
  206. #endif /* !FIPS_MODULE */
  207. end:
  208. EVP_PKEY_free(downgraded_from);
  209. return ok;
  210. }
  211. int EVP_PKEY_missing_parameters(const EVP_PKEY *pkey)
  212. {
  213. if (pkey != NULL) {
  214. #ifdef FIPS_MODULE
  215. return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
  216. #else
  217. if (pkey->keymgmt != NULL)
  218. return !evp_keymgmt_util_has((EVP_PKEY *)pkey, SELECT_PARAMETERS);
  219. if (pkey->ameth != NULL && pkey->ameth->param_missing != NULL)
  220. return pkey->ameth->param_missing(pkey);
  221. #endif /* FIPS_MODULE */
  222. }
  223. return 0;
  224. }
  225. /*
  226. * This function is called for any mixture of keys except pure legacy pair.
  227. * When legacy keys are gone, we replace a call to this functions with
  228. * a call to evp_keymgmt_util_match().
  229. */
  230. static int evp_pkey_cmp_any(const EVP_PKEY *a, const EVP_PKEY *b,
  231. int selection)
  232. {
  233. #ifdef FIPS_MODULE
  234. return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
  235. #else
  236. EVP_KEYMGMT *keymgmt1 = NULL, *keymgmt2 = NULL;
  237. void *keydata1 = NULL, *keydata2 = NULL, *tmp_keydata = NULL;
  238. /* If none of them are provided, this function shouldn't have been called */
  239. if (!ossl_assert(evp_pkey_is_provided(a) || evp_pkey_is_provided(b)))
  240. return -2;
  241. /* For purely provided keys, we just call the keymgmt utility */
  242. if (evp_pkey_is_provided(a) && evp_pkey_is_provided(b))
  243. return evp_keymgmt_util_match((EVP_PKEY *)a, (EVP_PKEY *)b, selection);
  244. /*
  245. * At this point, one of them is provided, the other not. This allows
  246. * us to compare types using legacy NIDs.
  247. */
  248. if (evp_pkey_is_legacy(a)
  249. && !EVP_KEYMGMT_is_a(b->keymgmt, OBJ_nid2sn(a->type)))
  250. return -1; /* not the same key type */
  251. if (evp_pkey_is_legacy(b)
  252. && !EVP_KEYMGMT_is_a(a->keymgmt, OBJ_nid2sn(b->type)))
  253. return -1; /* not the same key type */
  254. /*
  255. * We've determined that they both are the same keytype, so the next
  256. * step is to do a bit of cross export to ensure we have keydata for
  257. * both keys in the same keymgmt.
  258. */
  259. keymgmt1 = a->keymgmt;
  260. keydata1 = a->keydata;
  261. keymgmt2 = b->keymgmt;
  262. keydata2 = b->keydata;
  263. if (keymgmt2 != NULL && keymgmt2->match != NULL) {
  264. tmp_keydata =
  265. evp_pkey_export_to_provider((EVP_PKEY *)a, NULL, &keymgmt2, NULL);
  266. if (tmp_keydata != NULL) {
  267. keymgmt1 = keymgmt2;
  268. keydata1 = tmp_keydata;
  269. }
  270. }
  271. if (tmp_keydata == NULL && keymgmt1 != NULL && keymgmt1->match != NULL) {
  272. tmp_keydata =
  273. evp_pkey_export_to_provider((EVP_PKEY *)b, NULL, &keymgmt1, NULL);
  274. if (tmp_keydata != NULL) {
  275. keymgmt2 = keymgmt1;
  276. keydata2 = tmp_keydata;
  277. }
  278. }
  279. /* If we still don't have matching keymgmt implementations, we give up */
  280. if (keymgmt1 != keymgmt2)
  281. return -2;
  282. /* If the keymgmt implementations are NULL, the export failed */
  283. if (keymgmt1 == NULL)
  284. return -2;
  285. return evp_keymgmt_match(keymgmt1, keydata1, keydata2, selection);
  286. #endif /* FIPS_MODULE */
  287. }
  288. #ifndef FIPS_MODULE
  289. # ifndef OPENSSL_NO_DEPRECATED_3_0
  290. int EVP_PKEY_cmp_parameters(const EVP_PKEY *a, const EVP_PKEY *b)
  291. {
  292. return EVP_PKEY_parameters_eq(a, b);
  293. }
  294. # endif
  295. #endif /* FIPS_MODULE */
  296. int EVP_PKEY_parameters_eq(const EVP_PKEY *a, const EVP_PKEY *b)
  297. {
  298. #ifdef FIPS_MODULE
  299. return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
  300. #else
  301. /*
  302. * This will just call evp_keymgmt_util_match when legacy support
  303. * is gone.
  304. */
  305. if (a->keymgmt != NULL || b->keymgmt != NULL)
  306. return evp_pkey_cmp_any(a, b, SELECT_PARAMETERS);
  307. /* All legacy keys */
  308. if (a->type != b->type)
  309. return -1;
  310. if (a->ameth != NULL && a->ameth->param_cmp != NULL)
  311. return a->ameth->param_cmp(a, b);
  312. return -2;
  313. #endif /* !FIPS_MODULE */
  314. }
  315. #ifndef FIPS_MODULE
  316. # ifndef OPENSSL_NO_DEPRECATED_3_0
  317. int EVP_PKEY_cmp(const EVP_PKEY *a, const EVP_PKEY *b)
  318. {
  319. return EVP_PKEY_eq(a, b);
  320. }
  321. # endif
  322. #endif /* !FIPS_MODULE */
  323. int EVP_PKEY_eq(const EVP_PKEY *a, const EVP_PKEY *b)
  324. {
  325. /*
  326. * This will just call evp_keymgmt_util_match when legacy support
  327. * is gone.
  328. */
  329. /* Trivial shortcuts */
  330. if (a == b)
  331. return 1;
  332. if (a == NULL || b == NULL)
  333. return 0;
  334. #ifndef FIPS_MODULE
  335. if (a->keymgmt != NULL || b->keymgmt != NULL)
  336. #endif /* !FIPS_MODULE */
  337. {
  338. int selection = SELECT_PARAMETERS;
  339. if (evp_keymgmt_util_has((EVP_PKEY *)a, OSSL_KEYMGMT_SELECT_PUBLIC_KEY)
  340. && evp_keymgmt_util_has((EVP_PKEY *)b, OSSL_KEYMGMT_SELECT_PUBLIC_KEY))
  341. selection |= OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  342. else
  343. selection |= OSSL_KEYMGMT_SELECT_KEYPAIR;
  344. return evp_pkey_cmp_any(a, b, selection);
  345. }
  346. #ifndef FIPS_MODULE
  347. /* All legacy keys */
  348. if (a->type != b->type)
  349. return -1;
  350. if (a->ameth != NULL) {
  351. int ret;
  352. /* Compare parameters if the algorithm has them */
  353. if (a->ameth->param_cmp != NULL) {
  354. ret = a->ameth->param_cmp(a, b);
  355. if (ret <= 0)
  356. return ret;
  357. }
  358. if (a->ameth->pub_cmp != NULL)
  359. return a->ameth->pub_cmp(a, b);
  360. }
  361. return -2;
  362. #endif /* !FIPS_MODULE */
  363. }
  364. #ifndef FIPS_MODULE
  365. static EVP_PKEY *new_raw_key_int(OSSL_LIB_CTX *libctx,
  366. const char *strtype,
  367. const char *propq,
  368. int nidtype,
  369. ENGINE *e,
  370. const unsigned char *key,
  371. size_t len,
  372. int key_is_priv)
  373. {
  374. EVP_PKEY *pkey = NULL;
  375. EVP_PKEY_CTX *ctx = NULL;
  376. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  377. int result = 0;
  378. # ifndef OPENSSL_NO_ENGINE
  379. /* Check if there is an Engine for this type */
  380. if (e == NULL) {
  381. ENGINE *tmpe = NULL;
  382. if (strtype != NULL)
  383. ameth = EVP_PKEY_asn1_find_str(&tmpe, strtype, -1);
  384. else if (nidtype != EVP_PKEY_NONE)
  385. ameth = EVP_PKEY_asn1_find(&tmpe, nidtype);
  386. /* If tmpe is NULL then no engine is claiming to support this type */
  387. if (tmpe == NULL)
  388. ameth = NULL;
  389. ENGINE_finish(tmpe);
  390. }
  391. # endif
  392. if (e == NULL && ameth == NULL) {
  393. /*
  394. * No engine is claiming to support this type, so lets see if we have
  395. * a provider.
  396. */
  397. ctx = EVP_PKEY_CTX_new_from_name(libctx,
  398. strtype != NULL ? strtype
  399. : OBJ_nid2sn(nidtype),
  400. propq);
  401. if (ctx == NULL)
  402. goto err;
  403. /* May fail if no provider available */
  404. ERR_set_mark();
  405. if (EVP_PKEY_fromdata_init(ctx) == 1) {
  406. OSSL_PARAM params[] = { OSSL_PARAM_END, OSSL_PARAM_END };
  407. ERR_clear_last_mark();
  408. params[0] = OSSL_PARAM_construct_octet_string(
  409. key_is_priv ? OSSL_PKEY_PARAM_PRIV_KEY
  410. : OSSL_PKEY_PARAM_PUB_KEY,
  411. (void *)key, len);
  412. if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) != 1) {
  413. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  414. goto err;
  415. }
  416. EVP_PKEY_CTX_free(ctx);
  417. return pkey;
  418. }
  419. ERR_pop_to_mark();
  420. /* else not supported so fallback to legacy */
  421. }
  422. /* Legacy code path */
  423. pkey = EVP_PKEY_new();
  424. if (pkey == NULL) {
  425. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  426. goto err;
  427. }
  428. if (!pkey_set_type(pkey, e, nidtype, strtype, -1, NULL)) {
  429. /* ERR_raise(ERR_LIB_EVP, ...) already called */
  430. goto err;
  431. }
  432. if (!ossl_assert(pkey->ameth != NULL))
  433. goto err;
  434. if (key_is_priv) {
  435. if (pkey->ameth->set_priv_key == NULL) {
  436. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  437. goto err;
  438. }
  439. if (!pkey->ameth->set_priv_key(pkey, key, len)) {
  440. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  441. goto err;
  442. }
  443. } else {
  444. if (pkey->ameth->set_pub_key == NULL) {
  445. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  446. goto err;
  447. }
  448. if (!pkey->ameth->set_pub_key(pkey, key, len)) {
  449. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  450. goto err;
  451. }
  452. }
  453. result = 1;
  454. err:
  455. if (!result) {
  456. EVP_PKEY_free(pkey);
  457. pkey = NULL;
  458. }
  459. EVP_PKEY_CTX_free(ctx);
  460. return pkey;
  461. }
  462. EVP_PKEY *EVP_PKEY_new_raw_private_key_ex(OSSL_LIB_CTX *libctx,
  463. const char *keytype,
  464. const char *propq,
  465. const unsigned char *priv, size_t len)
  466. {
  467. return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, priv,
  468. len, 1);
  469. }
  470. EVP_PKEY *EVP_PKEY_new_raw_private_key(int type, ENGINE *e,
  471. const unsigned char *priv,
  472. size_t len)
  473. {
  474. return new_raw_key_int(NULL, NULL, NULL, type, e, priv, len, 1);
  475. }
  476. EVP_PKEY *EVP_PKEY_new_raw_public_key_ex(OSSL_LIB_CTX *libctx,
  477. const char *keytype, const char *propq,
  478. const unsigned char *pub, size_t len)
  479. {
  480. return new_raw_key_int(libctx, keytype, propq, EVP_PKEY_NONE, NULL, pub,
  481. len, 0);
  482. }
  483. EVP_PKEY *EVP_PKEY_new_raw_public_key(int type, ENGINE *e,
  484. const unsigned char *pub,
  485. size_t len)
  486. {
  487. return new_raw_key_int(NULL, NULL, NULL, type, e, pub, len, 0);
  488. }
  489. struct raw_key_details_st {
  490. unsigned char **key;
  491. size_t *len;
  492. int selection;
  493. };
  494. static OSSL_CALLBACK get_raw_key_details;
  495. static int get_raw_key_details(const OSSL_PARAM params[], void *arg)
  496. {
  497. const OSSL_PARAM *p = NULL;
  498. struct raw_key_details_st *raw_key = arg;
  499. if (raw_key->selection == OSSL_KEYMGMT_SELECT_PRIVATE_KEY) {
  500. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY))
  501. != NULL)
  502. return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
  503. raw_key->key == NULL ? 0 : *raw_key->len,
  504. raw_key->len);
  505. } else if (raw_key->selection == OSSL_KEYMGMT_SELECT_PUBLIC_KEY) {
  506. if ((p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY))
  507. != NULL)
  508. return OSSL_PARAM_get_octet_string(p, (void **)raw_key->key,
  509. raw_key->key == NULL ? 0 : *raw_key->len,
  510. raw_key->len);
  511. }
  512. return 0;
  513. }
  514. int EVP_PKEY_get_raw_private_key(const EVP_PKEY *pkey, unsigned char *priv,
  515. size_t *len)
  516. {
  517. if (pkey->keymgmt != NULL) {
  518. struct raw_key_details_st raw_key;
  519. raw_key.key = priv == NULL ? NULL : &priv;
  520. raw_key.len = len;
  521. raw_key.selection = OSSL_KEYMGMT_SELECT_PRIVATE_KEY;
  522. return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  523. get_raw_key_details, &raw_key);
  524. }
  525. if (pkey->ameth == NULL) {
  526. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  527. return 0;
  528. }
  529. if (pkey->ameth->get_priv_key == NULL) {
  530. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  531. return 0;
  532. }
  533. if (!pkey->ameth->get_priv_key(pkey, priv, len)) {
  534. ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
  535. return 0;
  536. }
  537. return 1;
  538. }
  539. int EVP_PKEY_get_raw_public_key(const EVP_PKEY *pkey, unsigned char *pub,
  540. size_t *len)
  541. {
  542. if (pkey->keymgmt != NULL) {
  543. struct raw_key_details_st raw_key;
  544. raw_key.key = pub == NULL ? NULL : &pub;
  545. raw_key.len = len;
  546. raw_key.selection = OSSL_KEYMGMT_SELECT_PUBLIC_KEY;
  547. return evp_keymgmt_util_export(pkey, OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  548. get_raw_key_details, &raw_key);
  549. }
  550. if (pkey->ameth == NULL) {
  551. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  552. return 0;
  553. }
  554. if (pkey->ameth->get_pub_key == NULL) {
  555. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  556. return 0;
  557. }
  558. if (!pkey->ameth->get_pub_key(pkey, pub, len)) {
  559. ERR_raise(ERR_LIB_EVP, EVP_R_GET_RAW_KEY_FAILED);
  560. return 0;
  561. }
  562. return 1;
  563. }
  564. static EVP_PKEY *new_cmac_key_int(const unsigned char *priv, size_t len,
  565. const char *cipher_name,
  566. const EVP_CIPHER *cipher,
  567. OSSL_LIB_CTX *libctx,
  568. const char *propq, ENGINE *e)
  569. {
  570. # ifndef OPENSSL_NO_CMAC
  571. # ifndef OPENSSL_NO_ENGINE
  572. const char *engine_id = e != NULL ? ENGINE_get_id(e) : NULL;
  573. # endif
  574. OSSL_PARAM params[5], *p = params;
  575. EVP_PKEY *pkey = NULL;
  576. EVP_PKEY_CTX *ctx;
  577. if (cipher != NULL)
  578. cipher_name = EVP_CIPHER_get0_name(cipher);
  579. if (cipher_name == NULL) {
  580. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  581. return NULL;
  582. }
  583. ctx = EVP_PKEY_CTX_new_from_name(libctx, "CMAC", propq);
  584. if (ctx == NULL)
  585. goto err;
  586. if (EVP_PKEY_fromdata_init(ctx) <= 0) {
  587. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  588. goto err;
  589. }
  590. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  591. (void *)priv, len);
  592. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_CIPHER,
  593. (char *)cipher_name, 0);
  594. if (propq != NULL)
  595. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_PROPERTIES,
  596. (char *)propq, 0);
  597. # ifndef OPENSSL_NO_ENGINE
  598. if (engine_id != NULL)
  599. *p++ = OSSL_PARAM_construct_utf8_string(OSSL_PKEY_PARAM_ENGINE,
  600. (char *)engine_id, 0);
  601. # endif
  602. *p = OSSL_PARAM_construct_end();
  603. if (EVP_PKEY_fromdata(ctx, &pkey, EVP_PKEY_KEYPAIR, params) <= 0) {
  604. ERR_raise(ERR_LIB_EVP, EVP_R_KEY_SETUP_FAILED);
  605. goto err;
  606. }
  607. err:
  608. EVP_PKEY_CTX_free(ctx);
  609. return pkey;
  610. # else
  611. ERR_raise(ERR_LIB_EVP, EVP_R_OPERATION_NOT_SUPPORTED_FOR_THIS_KEYTYPE);
  612. return NULL;
  613. # endif
  614. }
  615. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  616. size_t len, const EVP_CIPHER *cipher)
  617. {
  618. return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
  619. }
  620. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  621. {
  622. return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
  623. }
  624. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  625. {
  626. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
  627. }
  628. # ifndef OPENSSL_NO_ENGINE
  629. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  630. {
  631. if (e != NULL) {
  632. if (!ENGINE_init(e)) {
  633. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  634. return 0;
  635. }
  636. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  637. ENGINE_finish(e);
  638. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  639. return 0;
  640. }
  641. }
  642. ENGINE_finish(pkey->pmeth_engine);
  643. pkey->pmeth_engine = e;
  644. return 1;
  645. }
  646. ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
  647. {
  648. return pkey->engine;
  649. }
  650. # endif
  651. # ifndef OPENSSL_NO_DEPRECATED_3_0
  652. static void detect_foreign_key(EVP_PKEY *pkey)
  653. {
  654. switch (pkey->type) {
  655. case EVP_PKEY_RSA:
  656. case EVP_PKEY_RSA_PSS:
  657. pkey->foreign = pkey->pkey.rsa != NULL
  658. && ossl_rsa_is_foreign(pkey->pkey.rsa);
  659. break;
  660. # ifndef OPENSSL_NO_EC
  661. case EVP_PKEY_SM2:
  662. break;
  663. case EVP_PKEY_EC:
  664. pkey->foreign = pkey->pkey.ec != NULL
  665. && ossl_ec_key_is_foreign(pkey->pkey.ec);
  666. break;
  667. # endif
  668. # ifndef OPENSSL_NO_DSA
  669. case EVP_PKEY_DSA:
  670. pkey->foreign = pkey->pkey.dsa != NULL
  671. && ossl_dsa_is_foreign(pkey->pkey.dsa);
  672. break;
  673. #endif
  674. # ifndef OPENSSL_NO_DH
  675. case EVP_PKEY_DH:
  676. pkey->foreign = pkey->pkey.dh != NULL
  677. && ossl_dh_is_foreign(pkey->pkey.dh);
  678. break;
  679. #endif
  680. default:
  681. pkey->foreign = 0;
  682. break;
  683. }
  684. }
  685. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  686. {
  687. # ifndef OPENSSL_NO_EC
  688. int pktype;
  689. pktype = EVP_PKEY_type(type);
  690. if ((key != NULL) && (pktype == EVP_PKEY_EC || pktype == EVP_PKEY_SM2)) {
  691. const EC_GROUP *group = EC_KEY_get0_group(key);
  692. if (group != NULL) {
  693. int curve = EC_GROUP_get_curve_name(group);
  694. /*
  695. * Regardless of what is requested the SM2 curve must be SM2 type,
  696. * and non SM2 curves are EC type.
  697. */
  698. if (curve == NID_sm2 && pktype == EVP_PKEY_EC)
  699. type = EVP_PKEY_SM2;
  700. else if(curve != NID_sm2 && pktype == EVP_PKEY_SM2)
  701. type = EVP_PKEY_EC;
  702. }
  703. }
  704. # endif
  705. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  706. return 0;
  707. pkey->pkey.ptr = key;
  708. detect_foreign_key(pkey);
  709. return (key != NULL);
  710. }
  711. # endif
  712. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  713. {
  714. if (pkey == NULL)
  715. return NULL;
  716. if (!evp_pkey_is_provided(pkey))
  717. return pkey->pkey.ptr;
  718. return NULL;
  719. }
  720. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  721. {
  722. const ASN1_OCTET_STRING *os = NULL;
  723. if (pkey->type != EVP_PKEY_HMAC) {
  724. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
  725. return NULL;
  726. }
  727. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  728. if (os != NULL) {
  729. *len = os->length;
  730. return os->data;
  731. }
  732. return NULL;
  733. }
  734. # ifndef OPENSSL_NO_POLY1305
  735. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  736. {
  737. const ASN1_OCTET_STRING *os = NULL;
  738. if (pkey->type != EVP_PKEY_POLY1305) {
  739. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
  740. return NULL;
  741. }
  742. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  743. if (os != NULL) {
  744. *len = os->length;
  745. return os->data;
  746. }
  747. return NULL;
  748. }
  749. # endif
  750. # ifndef OPENSSL_NO_SIPHASH
  751. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  752. {
  753. const ASN1_OCTET_STRING *os = NULL;
  754. if (pkey->type != EVP_PKEY_SIPHASH) {
  755. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
  756. return NULL;
  757. }
  758. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  759. if (os != NULL) {
  760. *len = os->length;
  761. return os->data;
  762. }
  763. return NULL;
  764. }
  765. # endif
  766. # ifndef OPENSSL_NO_DSA
  767. static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
  768. {
  769. if (pkey->type != EVP_PKEY_DSA) {
  770. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
  771. return NULL;
  772. }
  773. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  774. }
  775. const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
  776. {
  777. return evp_pkey_get0_DSA_int(pkey);
  778. }
  779. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  780. {
  781. int ret;
  782. if (!DSA_up_ref(key))
  783. return 0;
  784. ret = EVP_PKEY_assign_DSA(pkey, key);
  785. if (!ret)
  786. DSA_free(key);
  787. return ret;
  788. }
  789. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  790. {
  791. DSA *ret = evp_pkey_get0_DSA_int(pkey);
  792. if (ret != NULL && !DSA_up_ref(ret))
  793. return NULL;
  794. return ret;
  795. }
  796. # endif /* OPENSSL_NO_DSA */
  797. # ifndef OPENSSL_NO_ECX
  798. static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
  799. {
  800. if (EVP_PKEY_get_base_id(pkey) != type) {
  801. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
  802. return NULL;
  803. }
  804. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  805. }
  806. static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
  807. {
  808. ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
  809. if (ret != NULL && !ossl_ecx_key_up_ref(ret))
  810. ret = NULL;
  811. return ret;
  812. }
  813. # define IMPLEMENT_ECX_VARIANT(NAME) \
  814. ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
  815. { \
  816. return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
  817. }
  818. IMPLEMENT_ECX_VARIANT(X25519)
  819. IMPLEMENT_ECX_VARIANT(X448)
  820. IMPLEMENT_ECX_VARIANT(ED25519)
  821. IMPLEMENT_ECX_VARIANT(ED448)
  822. # endif /* OPENSSL_NO_ECX */
  823. # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  824. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *dhkey)
  825. {
  826. int ret, type;
  827. /*
  828. * ossl_dh_is_named_safe_prime_group() returns 1 for named safe prime groups
  829. * related to ffdhe and modp (which cache q = (p - 1) / 2),
  830. * and returns 0 for all other dh parameter generation types including
  831. * RFC5114 named groups.
  832. *
  833. * The EVP_PKEY_DH type is used for dh parameter generation types:
  834. * - named safe prime groups related to ffdhe and modp
  835. * - safe prime generator
  836. *
  837. * The type EVP_PKEY_DHX is used for dh parameter generation types
  838. * - fips186-4 and fips186-2
  839. * - rfc5114 named groups.
  840. *
  841. * The EVP_PKEY_DH type is used to save PKCS#3 data than can be stored
  842. * without a q value.
  843. * The EVP_PKEY_DHX type is used to save X9.42 data that requires the
  844. * q value to be stored.
  845. */
  846. if (ossl_dh_is_named_safe_prime_group(dhkey))
  847. type = EVP_PKEY_DH;
  848. else
  849. type = DH_get0_q(dhkey) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
  850. if (!DH_up_ref(dhkey))
  851. return 0;
  852. ret = EVP_PKEY_assign(pkey, type, dhkey);
  853. if (!ret)
  854. DH_free(dhkey);
  855. return ret;
  856. }
  857. DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
  858. {
  859. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  860. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
  861. return NULL;
  862. }
  863. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  864. }
  865. const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
  866. {
  867. return evp_pkey_get0_DH_int(pkey);
  868. }
  869. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  870. {
  871. DH *ret = evp_pkey_get0_DH_int(pkey);
  872. if (ret != NULL && !DH_up_ref(ret))
  873. ret = NULL;
  874. return ret;
  875. }
  876. # endif
  877. int EVP_PKEY_type(int type)
  878. {
  879. int ret;
  880. const EVP_PKEY_ASN1_METHOD *ameth;
  881. ENGINE *e;
  882. ameth = EVP_PKEY_asn1_find(&e, type);
  883. if (ameth)
  884. ret = ameth->pkey_id;
  885. else
  886. ret = NID_undef;
  887. # ifndef OPENSSL_NO_ENGINE
  888. ENGINE_finish(e);
  889. # endif
  890. return ret;
  891. }
  892. int EVP_PKEY_get_id(const EVP_PKEY *pkey)
  893. {
  894. return pkey->type;
  895. }
  896. int EVP_PKEY_get_base_id(const EVP_PKEY *pkey)
  897. {
  898. return EVP_PKEY_type(pkey->type);
  899. }
  900. /*
  901. * These hard coded cases are pure hackery to get around the fact
  902. * that names in crypto/objects/objects.txt are a mess. There is
  903. * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
  904. * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
  905. * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
  906. * "DSA" is accurate... but still, better be safe and hard-code
  907. * names that we know.
  908. * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
  909. * EVP_PKEY_EC, because of aliasing.
  910. * This should be cleaned away along with all other #legacy support.
  911. */
  912. static const OSSL_ITEM standard_name2type[] = {
  913. { EVP_PKEY_RSA, "RSA" },
  914. { EVP_PKEY_RSA_PSS, "RSA-PSS" },
  915. { EVP_PKEY_EC, "EC" },
  916. { EVP_PKEY_ED25519, "ED25519" },
  917. { EVP_PKEY_ED448, "ED448" },
  918. { EVP_PKEY_X25519, "X25519" },
  919. { EVP_PKEY_X448, "X448" },
  920. { EVP_PKEY_SM2, "SM2" },
  921. { EVP_PKEY_DH, "DH" },
  922. { EVP_PKEY_DHX, "X9.42 DH" },
  923. { EVP_PKEY_DHX, "DHX" },
  924. { EVP_PKEY_DSA, "DSA" },
  925. };
  926. int evp_pkey_name2type(const char *name)
  927. {
  928. int type;
  929. size_t i;
  930. for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
  931. if (OPENSSL_strcasecmp(name, standard_name2type[i].ptr) == 0)
  932. return (int)standard_name2type[i].id;
  933. }
  934. if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
  935. return type;
  936. return EVP_PKEY_type(OBJ_ln2nid(name));
  937. }
  938. const char *evp_pkey_type2name(int type)
  939. {
  940. size_t i;
  941. for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
  942. if (type == (int)standard_name2type[i].id)
  943. return standard_name2type[i].ptr;
  944. }
  945. return OBJ_nid2sn(type);
  946. }
  947. int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
  948. {
  949. if (pkey == NULL)
  950. return 0;
  951. if (pkey->keymgmt == NULL)
  952. return pkey->type == evp_pkey_name2type(name);
  953. return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
  954. }
  955. int EVP_PKEY_type_names_do_all(const EVP_PKEY *pkey,
  956. void (*fn)(const char *name, void *data),
  957. void *data)
  958. {
  959. if (!evp_pkey_is_typed(pkey))
  960. return 0;
  961. if (!evp_pkey_is_provided(pkey)) {
  962. const char *name = OBJ_nid2sn(EVP_PKEY_get_id(pkey));
  963. fn(name, data);
  964. return 1;
  965. }
  966. return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
  967. }
  968. int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
  969. {
  970. if (pkey->keymgmt == NULL) {
  971. switch (EVP_PKEY_get_base_id(pkey)) {
  972. case EVP_PKEY_RSA:
  973. case EVP_PKEY_RSA_PSS:
  974. return 1;
  975. # ifndef OPENSSL_NO_DSA
  976. case EVP_PKEY_DSA:
  977. return 1;
  978. # endif
  979. # ifndef OPENSSL_NO_EC
  980. case EVP_PKEY_ED25519:
  981. case EVP_PKEY_ED448:
  982. return 1;
  983. case EVP_PKEY_EC: /* Including SM2 */
  984. return EC_KEY_can_sign(pkey->pkey.ec);
  985. # endif
  986. default:
  987. break;
  988. }
  989. } else {
  990. const OSSL_PROVIDER *prov = EVP_KEYMGMT_get0_provider(pkey->keymgmt);
  991. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  992. EVP_SIGNATURE *sig;
  993. const char *name;
  994. name = evp_keymgmt_util_query_operation_name(pkey->keymgmt,
  995. OSSL_OP_SIGNATURE);
  996. sig = EVP_SIGNATURE_fetch(libctx, name, NULL);
  997. if (sig != NULL) {
  998. EVP_SIGNATURE_free(sig);
  999. return 1;
  1000. }
  1001. }
  1002. return 0;
  1003. }
  1004. static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
  1005. {
  1006. BIO_set_indent(*out, saved_indent);
  1007. if (pop_f_prefix) {
  1008. BIO *next = BIO_pop(*out);
  1009. BIO_free(*out);
  1010. *out = next;
  1011. }
  1012. return 1;
  1013. }
  1014. static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
  1015. long indent)
  1016. {
  1017. *pop_f_prefix = 0;
  1018. *saved_indent = 0;
  1019. if (indent > 0) {
  1020. long i = BIO_get_indent(*out);
  1021. *saved_indent = (i < 0 ? 0 : i);
  1022. if (BIO_set_indent(*out, indent) <= 0) {
  1023. BIO *prefbio = BIO_new(BIO_f_prefix());
  1024. if (prefbio == NULL)
  1025. return 0;
  1026. *out = BIO_push(prefbio, *out);
  1027. *pop_f_prefix = 1;
  1028. }
  1029. if (BIO_set_indent(*out, indent) <= 0) {
  1030. print_reset_indent(out, *pop_f_prefix, *saved_indent);
  1031. return 0;
  1032. }
  1033. }
  1034. return 1;
  1035. }
  1036. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  1037. const char *kstr)
  1038. {
  1039. return BIO_indent(out, indent, 128)
  1040. && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  1041. kstr, OBJ_nid2ln(pkey->type)) > 0;
  1042. }
  1043. static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
  1044. int selection /* For provided encoding */,
  1045. const char *propquery /* For provided encoding */,
  1046. int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
  1047. int indent, ASN1_PCTX *pctx),
  1048. ASN1_PCTX *legacy_pctx /* For legacy print */)
  1049. {
  1050. int pop_f_prefix;
  1051. long saved_indent;
  1052. OSSL_ENCODER_CTX *ctx = NULL;
  1053. int ret = -2; /* default to unsupported */
  1054. if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
  1055. return 0;
  1056. ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
  1057. propquery);
  1058. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
  1059. ret = OSSL_ENCODER_to_bio(ctx, out);
  1060. OSSL_ENCODER_CTX_free(ctx);
  1061. if (ret != -2)
  1062. goto end;
  1063. /* legacy fallback */
  1064. if (legacy_print != NULL)
  1065. ret = legacy_print(out, pkey, 0, legacy_pctx);
  1066. else
  1067. ret = unsup_alg(out, pkey, 0, "Public Key");
  1068. end:
  1069. print_reset_indent(&out, pop_f_prefix, saved_indent);
  1070. return ret;
  1071. }
  1072. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  1073. int indent, ASN1_PCTX *pctx)
  1074. {
  1075. return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
  1076. (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
  1077. pctx);
  1078. }
  1079. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  1080. int indent, ASN1_PCTX *pctx)
  1081. {
  1082. return print_pkey(pkey, out, indent, EVP_PKEY_PRIVATE_KEY, NULL,
  1083. (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
  1084. pctx);
  1085. }
  1086. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  1087. int indent, ASN1_PCTX *pctx)
  1088. {
  1089. return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
  1090. (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
  1091. pctx);
  1092. }
  1093. # ifndef OPENSSL_NO_STDIO
  1094. int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
  1095. int indent, ASN1_PCTX *pctx)
  1096. {
  1097. int ret;
  1098. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  1099. if (b == NULL)
  1100. return 0;
  1101. ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
  1102. BIO_free(b);
  1103. return ret;
  1104. }
  1105. int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
  1106. int indent, ASN1_PCTX *pctx)
  1107. {
  1108. int ret;
  1109. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  1110. if (b == NULL)
  1111. return 0;
  1112. ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
  1113. BIO_free(b);
  1114. return ret;
  1115. }
  1116. int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
  1117. int indent, ASN1_PCTX *pctx)
  1118. {
  1119. int ret;
  1120. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  1121. if (b == NULL)
  1122. return 0;
  1123. ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
  1124. BIO_free(b);
  1125. return ret;
  1126. }
  1127. # endif
  1128. static void mdname2nid(const char *mdname, void *data)
  1129. {
  1130. int *nid = (int *)data;
  1131. if (*nid != NID_undef)
  1132. return;
  1133. *nid = OBJ_sn2nid(mdname);
  1134. if (*nid == NID_undef)
  1135. *nid = OBJ_ln2nid(mdname);
  1136. }
  1137. static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
  1138. int arg1, void *arg2)
  1139. {
  1140. if (pkey->keymgmt == NULL)
  1141. return 0;
  1142. switch (op) {
  1143. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  1144. {
  1145. char mdname[80] = "";
  1146. int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
  1147. sizeof(mdname));
  1148. if (rv > 0) {
  1149. int mdnum;
  1150. OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
  1151. /* Make sure the MD is in the namemap if available */
  1152. EVP_MD *md;
  1153. OSSL_NAMEMAP *namemap;
  1154. int nid = NID_undef;
  1155. (void)ERR_set_mark();
  1156. md = EVP_MD_fetch(libctx, mdname, NULL);
  1157. (void)ERR_pop_to_mark();
  1158. namemap = ossl_namemap_stored(libctx);
  1159. /*
  1160. * The only reason to fetch the MD was to make sure it is in the
  1161. * namemap. We can immediately free it.
  1162. */
  1163. EVP_MD_free(md);
  1164. mdnum = ossl_namemap_name2num(namemap, mdname);
  1165. if (mdnum == 0)
  1166. return 0;
  1167. /*
  1168. * We have the namemap number - now we need to find the
  1169. * associated nid
  1170. */
  1171. if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
  1172. return 0;
  1173. *(int *)arg2 = nid;
  1174. }
  1175. return rv;
  1176. }
  1177. default:
  1178. return -2;
  1179. }
  1180. }
  1181. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  1182. {
  1183. if (pkey->ameth == NULL)
  1184. return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
  1185. if (pkey->ameth->pkey_ctrl == NULL)
  1186. return -2;
  1187. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  1188. }
  1189. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  1190. {
  1191. if (pkey == NULL)
  1192. return 0;
  1193. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  1194. }
  1195. int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
  1196. char *mdname, size_t mdname_sz)
  1197. {
  1198. if (pkey->ameth == NULL)
  1199. return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
  1200. pkey->keydata,
  1201. mdname, mdname_sz);
  1202. {
  1203. int nid = NID_undef;
  1204. int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
  1205. const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
  1206. if (rv > 0)
  1207. OPENSSL_strlcpy(mdname, name, mdname_sz);
  1208. return rv;
  1209. }
  1210. }
  1211. int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
  1212. size_t *gname_len)
  1213. {
  1214. return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
  1215. gname, gname_sz, gname_len);
  1216. }
  1217. int EVP_PKEY_digestsign_supports_digest(EVP_PKEY *pkey, OSSL_LIB_CTX *libctx,
  1218. const char *name, const char *propq)
  1219. {
  1220. int rv;
  1221. EVP_MD_CTX *ctx = NULL;
  1222. if ((ctx = EVP_MD_CTX_new()) == NULL)
  1223. return -1;
  1224. ERR_set_mark();
  1225. rv = EVP_DigestSignInit_ex(ctx, NULL, name, libctx,
  1226. propq, pkey, NULL);
  1227. ERR_pop_to_mark();
  1228. EVP_MD_CTX_free(ctx);
  1229. return rv;
  1230. }
  1231. #endif /* !FIPS_MODULE */
  1232. int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
  1233. size_t publen)
  1234. {
  1235. if (pkey == NULL)
  1236. return 0;
  1237. #ifndef FIPS_MODULE
  1238. if (evp_pkey_is_provided(pkey))
  1239. #endif /* !FIPS_MODULE */
  1240. return
  1241. EVP_PKEY_set_octet_string_param(pkey,
  1242. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1243. (unsigned char *)pub, publen);
  1244. #ifndef FIPS_MODULE
  1245. if (publen > INT_MAX)
  1246. return 0;
  1247. /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
  1248. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
  1249. (void *)pub) <= 0)
  1250. return 0;
  1251. return 1;
  1252. #endif /* !FIPS_MODULE */
  1253. }
  1254. size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
  1255. {
  1256. if (pkey == NULL)
  1257. return 0;
  1258. #ifndef FIPS_MODULE
  1259. if (evp_pkey_is_provided(pkey))
  1260. #endif
  1261. {
  1262. size_t return_size = OSSL_PARAM_UNMODIFIED;
  1263. unsigned char *buf;
  1264. /*
  1265. * We know that this is going to fail, but it will give us a size
  1266. * to allocate.
  1267. */
  1268. EVP_PKEY_get_octet_string_param(pkey,
  1269. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1270. NULL, 0, &return_size);
  1271. if (return_size == OSSL_PARAM_UNMODIFIED)
  1272. return 0;
  1273. *ppub = NULL;
  1274. buf = OPENSSL_malloc(return_size);
  1275. if (buf == NULL)
  1276. return 0;
  1277. if (!EVP_PKEY_get_octet_string_param(pkey,
  1278. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1279. buf, return_size, NULL)) {
  1280. OPENSSL_free(buf);
  1281. return 0;
  1282. }
  1283. *ppub = buf;
  1284. return return_size;
  1285. }
  1286. #ifndef FIPS_MODULE
  1287. {
  1288. int rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
  1289. if (rv <= 0)
  1290. return 0;
  1291. return rv;
  1292. }
  1293. #endif /* !FIPS_MODULE */
  1294. }
  1295. /*- All methods below can also be used in FIPS_MODULE */
  1296. EVP_PKEY *EVP_PKEY_new(void)
  1297. {
  1298. EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
  1299. if (ret == NULL)
  1300. return NULL;
  1301. ret->type = EVP_PKEY_NONE;
  1302. ret->save_type = EVP_PKEY_NONE;
  1303. if (!CRYPTO_NEW_REF(&ret->references, 1))
  1304. goto err;
  1305. ret->lock = CRYPTO_THREAD_lock_new();
  1306. if (ret->lock == NULL) {
  1307. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  1308. goto err;
  1309. }
  1310. #ifndef FIPS_MODULE
  1311. ret->save_parameters = 1;
  1312. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
  1313. ERR_raise(ERR_LIB_EVP, ERR_R_CRYPTO_LIB);
  1314. goto err;
  1315. }
  1316. #endif
  1317. return ret;
  1318. err:
  1319. CRYPTO_FREE_REF(&ret->references);
  1320. CRYPTO_THREAD_lock_free(ret->lock);
  1321. OPENSSL_free(ret);
  1322. return NULL;
  1323. }
  1324. /*
  1325. * Setup a public key management method.
  1326. *
  1327. * For legacy keys, either |type| or |str| is expected to have the type
  1328. * information. In this case, the setup consists of finding an ASN1 method
  1329. * and potentially an ENGINE, and setting those fields in |pkey|.
  1330. *
  1331. * For provider side keys, |keymgmt| is expected to be non-NULL. In this
  1332. * case, the setup consists of setting the |keymgmt| field in |pkey|.
  1333. *
  1334. * If pkey is NULL just return 1 or 0 if the key management method exists.
  1335. */
  1336. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  1337. int len, EVP_KEYMGMT *keymgmt)
  1338. {
  1339. #ifndef FIPS_MODULE
  1340. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  1341. ENGINE **eptr = (e == NULL) ? &e : NULL;
  1342. #endif
  1343. /*
  1344. * The setups can't set both legacy and provider side methods.
  1345. * It is forbidden
  1346. */
  1347. if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
  1348. || !ossl_assert(e == NULL || keymgmt == NULL)) {
  1349. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1350. return 0;
  1351. }
  1352. if (pkey != NULL) {
  1353. int free_it = 0;
  1354. #ifndef FIPS_MODULE
  1355. free_it = free_it || pkey->pkey.ptr != NULL;
  1356. #endif
  1357. free_it = free_it || pkey->keydata != NULL;
  1358. if (free_it)
  1359. evp_pkey_free_it(pkey);
  1360. #ifndef FIPS_MODULE
  1361. /*
  1362. * If key type matches and a method exists then this lookup has
  1363. * succeeded once so just indicate success.
  1364. */
  1365. if (pkey->type != EVP_PKEY_NONE
  1366. && type == pkey->save_type
  1367. && pkey->ameth != NULL)
  1368. return 1;
  1369. # ifndef OPENSSL_NO_ENGINE
  1370. /* If we have ENGINEs release them */
  1371. ENGINE_finish(pkey->engine);
  1372. pkey->engine = NULL;
  1373. ENGINE_finish(pkey->pmeth_engine);
  1374. pkey->pmeth_engine = NULL;
  1375. # endif
  1376. #endif
  1377. }
  1378. #ifndef FIPS_MODULE
  1379. if (str != NULL)
  1380. ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
  1381. else if (type != EVP_PKEY_NONE)
  1382. ameth = EVP_PKEY_asn1_find(eptr, type);
  1383. # ifndef OPENSSL_NO_ENGINE
  1384. if (pkey == NULL && eptr != NULL)
  1385. ENGINE_finish(e);
  1386. # endif
  1387. #endif
  1388. {
  1389. int check = 1;
  1390. #ifndef FIPS_MODULE
  1391. check = check && ameth == NULL;
  1392. #endif
  1393. check = check && keymgmt == NULL;
  1394. if (check) {
  1395. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  1396. return 0;
  1397. }
  1398. }
  1399. if (pkey != NULL) {
  1400. if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
  1401. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1402. return 0;
  1403. }
  1404. pkey->keymgmt = keymgmt;
  1405. pkey->save_type = type;
  1406. pkey->type = type;
  1407. #ifndef FIPS_MODULE
  1408. /*
  1409. * If the internal "origin" key is provider side, don't save |ameth|.
  1410. * The main reason is that |ameth| is one factor to detect that the
  1411. * internal "origin" key is a legacy one.
  1412. */
  1413. if (keymgmt == NULL)
  1414. pkey->ameth = ameth;
  1415. /*
  1416. * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
  1417. * for any key type that has a legacy implementation, regardless of
  1418. * if the internal key is a legacy or a provider side one. When
  1419. * there is no legacy implementation for the key, the type becomes
  1420. * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
  1421. * with functions that expect legacy internal keys.
  1422. */
  1423. if (ameth != NULL) {
  1424. if (type == EVP_PKEY_NONE)
  1425. pkey->type = ameth->pkey_id;
  1426. } else {
  1427. pkey->type = EVP_PKEY_KEYMGMT;
  1428. }
  1429. # ifndef OPENSSL_NO_ENGINE
  1430. if (eptr == NULL && e != NULL && !ENGINE_init(e)) {
  1431. ERR_raise(ERR_LIB_EVP, EVP_R_INITIALIZATION_ERROR);
  1432. return 0;
  1433. }
  1434. # endif
  1435. pkey->engine = e;
  1436. #endif
  1437. }
  1438. return 1;
  1439. }
  1440. #ifndef FIPS_MODULE
  1441. static void find_ameth(const char *name, void *data)
  1442. {
  1443. const char **str = data;
  1444. /*
  1445. * The error messages from pkey_set_type() are uninteresting here,
  1446. * and misleading.
  1447. */
  1448. ERR_set_mark();
  1449. if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
  1450. NULL)) {
  1451. if (str[0] == NULL)
  1452. str[0] = name;
  1453. else if (str[1] == NULL)
  1454. str[1] = name;
  1455. }
  1456. ERR_pop_to_mark();
  1457. }
  1458. #endif
  1459. int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
  1460. {
  1461. #ifndef FIPS_MODULE
  1462. # define EVP_PKEY_TYPE_STR str[0]
  1463. # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
  1464. /*
  1465. * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
  1466. * Ideally, only one should be found. If two (or more) are found, the
  1467. * match is ambiguous. This should never happen, but...
  1468. */
  1469. const char *str[2] = { NULL, NULL };
  1470. if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
  1471. || str[1] != NULL) {
  1472. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1473. return 0;
  1474. }
  1475. #else
  1476. # define EVP_PKEY_TYPE_STR NULL
  1477. # define EVP_PKEY_TYPE_STRLEN -1
  1478. #endif
  1479. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
  1480. EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
  1481. keymgmt);
  1482. #undef EVP_PKEY_TYPE_STR
  1483. #undef EVP_PKEY_TYPE_STRLEN
  1484. }
  1485. int EVP_PKEY_up_ref(EVP_PKEY *pkey)
  1486. {
  1487. int i;
  1488. if (CRYPTO_UP_REF(&pkey->references, &i) <= 0)
  1489. return 0;
  1490. REF_PRINT_COUNT("EVP_PKEY", i, pkey);
  1491. REF_ASSERT_ISNT(i < 2);
  1492. return ((i > 1) ? 1 : 0);
  1493. }
  1494. EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
  1495. {
  1496. EVP_PKEY *dup_pk;
  1497. if (pkey == NULL) {
  1498. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  1499. return NULL;
  1500. }
  1501. if ((dup_pk = EVP_PKEY_new()) == NULL)
  1502. return NULL;
  1503. if (evp_pkey_is_blank(pkey))
  1504. goto done;
  1505. #ifndef FIPS_MODULE
  1506. if (evp_pkey_is_provided(pkey))
  1507. #endif /* !FIPS_MODULE */
  1508. {
  1509. if (!evp_keymgmt_util_copy(dup_pk, pkey,
  1510. OSSL_KEYMGMT_SELECT_ALL))
  1511. goto err;
  1512. goto done;
  1513. }
  1514. #ifndef FIPS_MODULE
  1515. if (evp_pkey_is_legacy(pkey)) {
  1516. const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
  1517. if (ameth == NULL || ameth->copy == NULL) {
  1518. if (pkey->pkey.ptr == NULL /* empty key, just set type */
  1519. && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
  1520. goto done;
  1521. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1522. goto err;
  1523. }
  1524. if (!ameth->copy(dup_pk, pkey))
  1525. goto err;
  1526. goto done;
  1527. }
  1528. #endif /* !FIPS_MODULE */
  1529. goto err;
  1530. done:
  1531. #ifndef FIPS_MODULE
  1532. /* copy auxiliary data */
  1533. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
  1534. &dup_pk->ex_data, &pkey->ex_data))
  1535. goto err;
  1536. if (pkey->attributes != NULL) {
  1537. if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
  1538. goto err;
  1539. }
  1540. #endif /* !FIPS_MODULE */
  1541. return dup_pk;
  1542. err:
  1543. EVP_PKEY_free(dup_pk);
  1544. return NULL;
  1545. }
  1546. #ifndef FIPS_MODULE
  1547. void evp_pkey_free_legacy(EVP_PKEY *x)
  1548. {
  1549. const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
  1550. ENGINE *tmpe = NULL;
  1551. if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
  1552. ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
  1553. if (ameth != NULL) {
  1554. if (x->legacy_cache_pkey.ptr != NULL) {
  1555. /*
  1556. * We should never have both a legacy origin key, and a key in the
  1557. * legacy cache.
  1558. */
  1559. assert(x->pkey.ptr == NULL);
  1560. /*
  1561. * For the purposes of freeing we make the legacy cache look like
  1562. * a legacy origin key.
  1563. */
  1564. x->pkey = x->legacy_cache_pkey;
  1565. x->legacy_cache_pkey.ptr = NULL;
  1566. }
  1567. if (ameth->pkey_free != NULL)
  1568. ameth->pkey_free(x);
  1569. x->pkey.ptr = NULL;
  1570. }
  1571. # ifndef OPENSSL_NO_ENGINE
  1572. ENGINE_finish(tmpe);
  1573. ENGINE_finish(x->engine);
  1574. x->engine = NULL;
  1575. ENGINE_finish(x->pmeth_engine);
  1576. x->pmeth_engine = NULL;
  1577. # endif
  1578. }
  1579. #endif /* FIPS_MODULE */
  1580. static void evp_pkey_free_it(EVP_PKEY *x)
  1581. {
  1582. /* internal function; x is never NULL */
  1583. evp_keymgmt_util_clear_operation_cache(x);
  1584. #ifndef FIPS_MODULE
  1585. evp_pkey_free_legacy(x);
  1586. #endif
  1587. if (x->keymgmt != NULL) {
  1588. evp_keymgmt_freedata(x->keymgmt, x->keydata);
  1589. EVP_KEYMGMT_free(x->keymgmt);
  1590. x->keymgmt = NULL;
  1591. x->keydata = NULL;
  1592. }
  1593. x->type = EVP_PKEY_NONE;
  1594. }
  1595. void EVP_PKEY_free(EVP_PKEY *x)
  1596. {
  1597. int i;
  1598. if (x == NULL)
  1599. return;
  1600. CRYPTO_DOWN_REF(&x->references, &i);
  1601. REF_PRINT_COUNT("EVP_PKEY", i, x);
  1602. if (i > 0)
  1603. return;
  1604. REF_ASSERT_ISNT(i < 0);
  1605. evp_pkey_free_it(x);
  1606. #ifndef FIPS_MODULE
  1607. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
  1608. #endif
  1609. CRYPTO_THREAD_lock_free(x->lock);
  1610. CRYPTO_FREE_REF(&x->references);
  1611. #ifndef FIPS_MODULE
  1612. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  1613. #endif
  1614. OPENSSL_free(x);
  1615. }
  1616. int EVP_PKEY_get_size(const EVP_PKEY *pkey)
  1617. {
  1618. int size = 0;
  1619. if (pkey != NULL) {
  1620. size = pkey->cache.size;
  1621. #ifndef FIPS_MODULE
  1622. if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
  1623. size = pkey->ameth->pkey_size(pkey);
  1624. #endif
  1625. }
  1626. if (size <= 0) {
  1627. ERR_raise(ERR_LIB_EVP, EVP_R_UNKNOWN_MAX_SIZE);
  1628. return 0;
  1629. }
  1630. return size;
  1631. }
  1632. const char *EVP_PKEY_get0_description(const EVP_PKEY *pkey)
  1633. {
  1634. if (!evp_pkey_is_assigned(pkey))
  1635. return NULL;
  1636. if (evp_pkey_is_provided(pkey) && pkey->keymgmt->description != NULL)
  1637. return pkey->keymgmt->description;
  1638. #ifndef FIPS_MODULE
  1639. if (pkey->ameth != NULL)
  1640. return pkey->ameth->info;
  1641. #endif
  1642. return NULL;
  1643. }
  1644. void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
  1645. EVP_KEYMGMT **keymgmt,
  1646. const char *propquery)
  1647. {
  1648. EVP_KEYMGMT *allocated_keymgmt = NULL;
  1649. EVP_KEYMGMT *tmp_keymgmt = NULL;
  1650. int selection = OSSL_KEYMGMT_SELECT_ALL;
  1651. void *keydata = NULL;
  1652. int check;
  1653. if (pk == NULL)
  1654. return NULL;
  1655. /* No key data => nothing to export */
  1656. check = 1;
  1657. #ifndef FIPS_MODULE
  1658. check = check && pk->pkey.ptr == NULL;
  1659. #endif
  1660. check = check && pk->keydata == NULL;
  1661. if (check)
  1662. return NULL;
  1663. #ifndef FIPS_MODULE
  1664. if (pk->pkey.ptr != NULL) {
  1665. /*
  1666. * If the legacy key doesn't have an dirty counter or export function,
  1667. * give up
  1668. */
  1669. if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
  1670. return NULL;
  1671. }
  1672. #endif
  1673. if (keymgmt != NULL) {
  1674. tmp_keymgmt = *keymgmt;
  1675. *keymgmt = NULL;
  1676. }
  1677. /*
  1678. * If no keymgmt was given or found, get a default keymgmt. We do so by
  1679. * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
  1680. */
  1681. if (tmp_keymgmt == NULL) {
  1682. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
  1683. if (ctx == NULL)
  1684. goto end;
  1685. allocated_keymgmt = tmp_keymgmt = ctx->keymgmt;
  1686. ctx->keymgmt = NULL;
  1687. EVP_PKEY_CTX_free(ctx);
  1688. }
  1689. /* If there's still no keymgmt to be had, give up */
  1690. if (tmp_keymgmt == NULL)
  1691. goto end;
  1692. #ifndef FIPS_MODULE
  1693. if (pk->pkey.ptr != NULL) {
  1694. OP_CACHE_ELEM *op;
  1695. /*
  1696. * If the legacy "origin" hasn't changed since last time, we try
  1697. * to find our keymgmt in the operation cache. If it has changed,
  1698. * |i| remains zero, and we will clear the cache further down.
  1699. */
  1700. if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
  1701. if (!CRYPTO_THREAD_read_lock(pk->lock))
  1702. goto end;
  1703. op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt,
  1704. selection);
  1705. /*
  1706. * If |tmp_keymgmt| is present in the operation cache, it means
  1707. * that export doesn't need to be redone. In that case, we take
  1708. * token copies of the cached pointers, to have token success
  1709. * values to return. It is possible (e.g. in a no-cached-fetch
  1710. * build), for op->keymgmt to be a different pointer to tmp_keymgmt
  1711. * even though the name/provider must be the same. In other words
  1712. * the keymgmt instance may be different but still equivalent, i.e.
  1713. * same algorithm/provider instance - but we make the simplifying
  1714. * assumption that the keydata can be used with either keymgmt
  1715. * instance. Not doing so introduces significant complexity and
  1716. * probably requires refactoring - since we would have to ripple
  1717. * the change in keymgmt instance up the call chain.
  1718. */
  1719. if (op != NULL && op->keymgmt != NULL) {
  1720. keydata = op->keydata;
  1721. CRYPTO_THREAD_unlock(pk->lock);
  1722. goto end;
  1723. }
  1724. CRYPTO_THREAD_unlock(pk->lock);
  1725. }
  1726. /* Make sure that the keymgmt key type matches the legacy NID */
  1727. if (!EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type)))
  1728. goto end;
  1729. if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
  1730. goto end;
  1731. if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt->import,
  1732. libctx, propquery)) {
  1733. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1734. keydata = NULL;
  1735. goto end;
  1736. }
  1737. /*
  1738. * If the dirty counter changed since last time, then clear the
  1739. * operation cache. In that case, we know that |i| is zero. Just
  1740. * in case this is a re-export, we increment then decrement the
  1741. * keymgmt reference counter.
  1742. */
  1743. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
  1744. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1745. keydata = NULL;
  1746. goto end;
  1747. }
  1748. if (!CRYPTO_THREAD_write_lock(pk->lock))
  1749. goto end;
  1750. if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy
  1751. && !evp_keymgmt_util_clear_operation_cache(pk)) {
  1752. CRYPTO_THREAD_unlock(pk->lock);
  1753. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1754. keydata = NULL;
  1755. EVP_KEYMGMT_free(tmp_keymgmt);
  1756. goto end;
  1757. }
  1758. EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
  1759. /* Check to make sure some other thread didn't get there first */
  1760. op = evp_keymgmt_util_find_operation_cache(pk, tmp_keymgmt, selection);
  1761. if (op != NULL && op->keymgmt != NULL) {
  1762. void *tmp_keydata = op->keydata;
  1763. CRYPTO_THREAD_unlock(pk->lock);
  1764. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1765. keydata = tmp_keydata;
  1766. goto end;
  1767. }
  1768. /* Add the new export to the operation cache */
  1769. if (!evp_keymgmt_util_cache_keydata(pk, tmp_keymgmt, keydata,
  1770. selection)) {
  1771. CRYPTO_THREAD_unlock(pk->lock);
  1772. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1773. keydata = NULL;
  1774. goto end;
  1775. }
  1776. /* Synchronize the dirty count */
  1777. pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
  1778. CRYPTO_THREAD_unlock(pk->lock);
  1779. goto end;
  1780. }
  1781. #endif /* FIPS_MODULE */
  1782. keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt, selection);
  1783. end:
  1784. /*
  1785. * If nothing was exported, |tmp_keymgmt| might point at a freed
  1786. * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
  1787. * the caller either way in that case.
  1788. */
  1789. if (keydata == NULL)
  1790. tmp_keymgmt = NULL;
  1791. if (keymgmt != NULL && tmp_keymgmt != NULL) {
  1792. *keymgmt = tmp_keymgmt;
  1793. allocated_keymgmt = NULL;
  1794. }
  1795. EVP_KEYMGMT_free(allocated_keymgmt);
  1796. return keydata;
  1797. }
  1798. #ifndef FIPS_MODULE
  1799. int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
  1800. {
  1801. EVP_PKEY *allocpkey = NULL;
  1802. if (!ossl_assert(dest != NULL))
  1803. return 0;
  1804. if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
  1805. EVP_KEYMGMT *keymgmt = src->keymgmt;
  1806. void *keydata = src->keydata;
  1807. int type = src->type;
  1808. const char *keytype = NULL;
  1809. keytype = EVP_KEYMGMT_get0_name(keymgmt);
  1810. /*
  1811. * If the type is EVP_PKEY_NONE, then we have a problem somewhere
  1812. * else in our code. If it's not one of the well known EVP_PKEY_xxx
  1813. * values, it should at least be EVP_PKEY_KEYMGMT at this point.
  1814. * The check is kept as a safety measure.
  1815. */
  1816. if (!ossl_assert(type != EVP_PKEY_NONE)) {
  1817. ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
  1818. "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
  1819. keytype);
  1820. return 0;
  1821. }
  1822. /* Prefer the legacy key type name for error reporting */
  1823. if (type != EVP_PKEY_KEYMGMT)
  1824. keytype = OBJ_nid2sn(type);
  1825. /* Make sure we have a clean slate to copy into */
  1826. if (*dest == NULL) {
  1827. allocpkey = *dest = EVP_PKEY_new();
  1828. if (*dest == NULL) {
  1829. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  1830. return 0;
  1831. }
  1832. } else {
  1833. evp_pkey_free_it(*dest);
  1834. }
  1835. if (EVP_PKEY_set_type(*dest, type)) {
  1836. /* If the key is typed but empty, we're done */
  1837. if (keydata == NULL)
  1838. return 1;
  1839. if ((*dest)->ameth->import_from == NULL) {
  1840. ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
  1841. "key type = %s", keytype);
  1842. } else {
  1843. /*
  1844. * We perform the export in the same libctx as the keymgmt
  1845. * that we are using.
  1846. */
  1847. OSSL_LIB_CTX *libctx =
  1848. ossl_provider_libctx(keymgmt->prov);
  1849. EVP_PKEY_CTX *pctx =
  1850. EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
  1851. if (pctx == NULL)
  1852. ERR_raise(ERR_LIB_EVP, ERR_R_EVP_LIB);
  1853. if (pctx != NULL
  1854. && evp_keymgmt_export(keymgmt, keydata,
  1855. OSSL_KEYMGMT_SELECT_ALL,
  1856. (*dest)->ameth->import_from,
  1857. pctx)) {
  1858. /* Synchronize the dirty count */
  1859. (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
  1860. EVP_PKEY_CTX_free(pctx);
  1861. return 1;
  1862. }
  1863. EVP_PKEY_CTX_free(pctx);
  1864. }
  1865. ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
  1866. "key type = %s", keytype);
  1867. }
  1868. }
  1869. if (allocpkey != NULL) {
  1870. EVP_PKEY_free(allocpkey);
  1871. *dest = NULL;
  1872. }
  1873. return 0;
  1874. }
  1875. void *evp_pkey_get_legacy(EVP_PKEY *pk)
  1876. {
  1877. EVP_PKEY *tmp_copy = NULL;
  1878. void *ret = NULL;
  1879. if (!ossl_assert(pk != NULL))
  1880. return NULL;
  1881. /*
  1882. * If this isn't an assigned provider side key, we just use any existing
  1883. * origin legacy key.
  1884. */
  1885. if (!evp_pkey_is_assigned(pk))
  1886. return NULL;
  1887. if (!evp_pkey_is_provided(pk))
  1888. return pk->pkey.ptr;
  1889. if (!CRYPTO_THREAD_read_lock(pk->lock))
  1890. return NULL;
  1891. ret = pk->legacy_cache_pkey.ptr;
  1892. if (!CRYPTO_THREAD_unlock(pk->lock))
  1893. return NULL;
  1894. if (ret != NULL)
  1895. return ret;
  1896. if (!evp_pkey_copy_downgraded(&tmp_copy, pk))
  1897. goto err;
  1898. if (!CRYPTO_THREAD_write_lock(pk->lock))
  1899. goto err;
  1900. /* Check again in case some other thread has updated it in the meantime */
  1901. ret = pk->legacy_cache_pkey.ptr;
  1902. if (ret == NULL) {
  1903. /* Steal the legacy key reference from the temporary copy */
  1904. ret = pk->legacy_cache_pkey.ptr = tmp_copy->pkey.ptr;
  1905. tmp_copy->pkey.ptr = NULL;
  1906. }
  1907. if (!CRYPTO_THREAD_unlock(pk->lock)) {
  1908. ret = NULL;
  1909. goto err;
  1910. }
  1911. err:
  1912. EVP_PKEY_free(tmp_copy);
  1913. return ret;
  1914. }
  1915. #endif /* FIPS_MODULE */
  1916. int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
  1917. BIGNUM **bn)
  1918. {
  1919. int ret = 0;
  1920. OSSL_PARAM params[2];
  1921. unsigned char buffer[2048];
  1922. unsigned char *buf = NULL;
  1923. size_t buf_sz = 0;
  1924. if (key_name == NULL
  1925. || bn == NULL)
  1926. return 0;
  1927. memset(buffer, 0, sizeof(buffer));
  1928. params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
  1929. params[1] = OSSL_PARAM_construct_end();
  1930. if (!EVP_PKEY_get_params(pkey, params)) {
  1931. if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
  1932. return 0;
  1933. buf_sz = params[0].return_size;
  1934. /*
  1935. * If it failed because the buffer was too small then allocate the
  1936. * required buffer size and retry.
  1937. */
  1938. buf = OPENSSL_zalloc(buf_sz);
  1939. if (buf == NULL)
  1940. return 0;
  1941. params[0].data = buf;
  1942. params[0].data_size = buf_sz;
  1943. if (!EVP_PKEY_get_params(pkey, params))
  1944. goto err;
  1945. }
  1946. /* Fail if the param was not found */
  1947. if (!OSSL_PARAM_modified(params))
  1948. goto err;
  1949. ret = OSSL_PARAM_get_BN(params, bn);
  1950. err:
  1951. if (buf != NULL) {
  1952. if (OSSL_PARAM_modified(params))
  1953. OPENSSL_clear_free(buf, buf_sz);
  1954. else
  1955. OPENSSL_free(buf);
  1956. } else if (OSSL_PARAM_modified(params)) {
  1957. OPENSSL_cleanse(buffer, params[0].data_size);
  1958. }
  1959. return ret;
  1960. }
  1961. int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
  1962. unsigned char *buf, size_t max_buf_sz,
  1963. size_t *out_len)
  1964. {
  1965. OSSL_PARAM params[2];
  1966. int ret1 = 0, ret2 = 0;
  1967. if (key_name == NULL)
  1968. return 0;
  1969. params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
  1970. params[1] = OSSL_PARAM_construct_end();
  1971. if ((ret1 = EVP_PKEY_get_params(pkey, params)))
  1972. ret2 = OSSL_PARAM_modified(params);
  1973. if (ret2 && out_len != NULL)
  1974. *out_len = params[0].return_size;
  1975. return ret1 && ret2;
  1976. }
  1977. int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
  1978. char *str, size_t max_buf_sz,
  1979. size_t *out_len)
  1980. {
  1981. OSSL_PARAM params[2];
  1982. int ret1 = 0, ret2 = 0;
  1983. if (key_name == NULL)
  1984. return 0;
  1985. params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
  1986. params[1] = OSSL_PARAM_construct_end();
  1987. if ((ret1 = EVP_PKEY_get_params(pkey, params)))
  1988. ret2 = OSSL_PARAM_modified(params);
  1989. if (ret2 && out_len != NULL)
  1990. *out_len = params[0].return_size;
  1991. if (ret2 && params[0].return_size == max_buf_sz)
  1992. /* There was no space for a NUL byte */
  1993. return 0;
  1994. /* Add a terminating NUL byte for good measure */
  1995. if (ret2 && str != NULL)
  1996. str[params[0].return_size] = '\0';
  1997. return ret1 && ret2;
  1998. }
  1999. int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
  2000. int *out)
  2001. {
  2002. OSSL_PARAM params[2];
  2003. if (key_name == NULL)
  2004. return 0;
  2005. params[0] = OSSL_PARAM_construct_int(key_name, out);
  2006. params[1] = OSSL_PARAM_construct_end();
  2007. return EVP_PKEY_get_params(pkey, params)
  2008. && OSSL_PARAM_modified(params);
  2009. }
  2010. int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
  2011. size_t *out)
  2012. {
  2013. OSSL_PARAM params[2];
  2014. if (key_name == NULL)
  2015. return 0;
  2016. params[0] = OSSL_PARAM_construct_size_t(key_name, out);
  2017. params[1] = OSSL_PARAM_construct_end();
  2018. return EVP_PKEY_get_params(pkey, params)
  2019. && OSSL_PARAM_modified(params);
  2020. }
  2021. int EVP_PKEY_set_int_param(EVP_PKEY *pkey, const char *key_name, int in)
  2022. {
  2023. OSSL_PARAM params[2];
  2024. if (key_name == NULL)
  2025. return 0;
  2026. params[0] = OSSL_PARAM_construct_int(key_name, &in);
  2027. params[1] = OSSL_PARAM_construct_end();
  2028. return EVP_PKEY_set_params(pkey, params);
  2029. }
  2030. int EVP_PKEY_set_size_t_param(EVP_PKEY *pkey, const char *key_name, size_t in)
  2031. {
  2032. OSSL_PARAM params[2];
  2033. if (key_name == NULL)
  2034. return 0;
  2035. params[0] = OSSL_PARAM_construct_size_t(key_name, &in);
  2036. params[1] = OSSL_PARAM_construct_end();
  2037. return EVP_PKEY_set_params(pkey, params);
  2038. }
  2039. int EVP_PKEY_set_bn_param(EVP_PKEY *pkey, const char *key_name,
  2040. const BIGNUM *bn)
  2041. {
  2042. OSSL_PARAM params[2];
  2043. unsigned char buffer[2048];
  2044. int bsize = 0;
  2045. if (key_name == NULL
  2046. || bn == NULL
  2047. || pkey == NULL
  2048. || !evp_pkey_is_assigned(pkey))
  2049. return 0;
  2050. bsize = BN_num_bytes(bn);
  2051. if (!ossl_assert(bsize <= (int)sizeof(buffer)))
  2052. return 0;
  2053. if (BN_bn2nativepad(bn, buffer, bsize) < 0)
  2054. return 0;
  2055. params[0] = OSSL_PARAM_construct_BN(key_name, buffer, bsize);
  2056. params[1] = OSSL_PARAM_construct_end();
  2057. return EVP_PKEY_set_params(pkey, params);
  2058. }
  2059. int EVP_PKEY_set_utf8_string_param(EVP_PKEY *pkey, const char *key_name,
  2060. const char *str)
  2061. {
  2062. OSSL_PARAM params[2];
  2063. if (key_name == NULL)
  2064. return 0;
  2065. params[0] = OSSL_PARAM_construct_utf8_string(key_name, (char *)str, 0);
  2066. params[1] = OSSL_PARAM_construct_end();
  2067. return EVP_PKEY_set_params(pkey, params);
  2068. }
  2069. int EVP_PKEY_set_octet_string_param(EVP_PKEY *pkey, const char *key_name,
  2070. const unsigned char *buf, size_t bsize)
  2071. {
  2072. OSSL_PARAM params[2];
  2073. if (key_name == NULL)
  2074. return 0;
  2075. params[0] = OSSL_PARAM_construct_octet_string(key_name,
  2076. (unsigned char *)buf, bsize);
  2077. params[1] = OSSL_PARAM_construct_end();
  2078. return EVP_PKEY_set_params(pkey, params);
  2079. }
  2080. const OSSL_PARAM *EVP_PKEY_settable_params(const EVP_PKEY *pkey)
  2081. {
  2082. return (pkey != NULL && evp_pkey_is_provided(pkey))
  2083. ? EVP_KEYMGMT_settable_params(pkey->keymgmt)
  2084. : NULL;
  2085. }
  2086. int EVP_PKEY_set_params(EVP_PKEY *pkey, OSSL_PARAM params[])
  2087. {
  2088. if (pkey != NULL) {
  2089. if (evp_pkey_is_provided(pkey)) {
  2090. pkey->dirty_cnt++;
  2091. return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
  2092. }
  2093. #ifndef FIPS_MODULE
  2094. /*
  2095. * We will hopefully never find the need to set individual data in
  2096. * EVP_PKEYs with a legacy internal key, but we can't be entirely
  2097. * sure. This bit of code can be enabled if we find the need. If
  2098. * not, it can safely be removed when #legacy support is removed.
  2099. */
  2100. # if 0
  2101. else if (evp_pkey_is_legacy(pkey)) {
  2102. return evp_pkey_set_params_to_ctrl(pkey, params);
  2103. }
  2104. # endif
  2105. #endif
  2106. }
  2107. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
  2108. return 0;
  2109. }
  2110. const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
  2111. {
  2112. return (pkey != NULL && evp_pkey_is_provided(pkey))
  2113. ? EVP_KEYMGMT_gettable_params(pkey->keymgmt)
  2114. : NULL;
  2115. }
  2116. int EVP_PKEY_get_params(const EVP_PKEY *pkey, OSSL_PARAM params[])
  2117. {
  2118. if (pkey != NULL) {
  2119. if (evp_pkey_is_provided(pkey))
  2120. return evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params) > 0;
  2121. #ifndef FIPS_MODULE
  2122. else if (evp_pkey_is_legacy(pkey))
  2123. return evp_pkey_get_params_to_ctrl(pkey, params) > 0;
  2124. #endif
  2125. }
  2126. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
  2127. return 0;
  2128. }
  2129. #ifndef FIPS_MODULE
  2130. int EVP_PKEY_get_ec_point_conv_form(const EVP_PKEY *pkey)
  2131. {
  2132. char name[80];
  2133. size_t name_len;
  2134. if (pkey == NULL)
  2135. return 0;
  2136. if (pkey->keymgmt == NULL
  2137. || pkey->keydata == NULL) {
  2138. # ifndef OPENSSL_NO_EC
  2139. /* Might work through the legacy route */
  2140. const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  2141. if (ec == NULL)
  2142. return 0;
  2143. return EC_KEY_get_conv_form(ec);
  2144. # else
  2145. return 0;
  2146. # endif
  2147. }
  2148. if (!EVP_PKEY_get_utf8_string_param(pkey,
  2149. OSSL_PKEY_PARAM_EC_POINT_CONVERSION_FORMAT,
  2150. name, sizeof(name), &name_len))
  2151. return 0;
  2152. if (strcmp(name, "uncompressed") == 0)
  2153. return POINT_CONVERSION_UNCOMPRESSED;
  2154. if (strcmp(name, "compressed") == 0)
  2155. return POINT_CONVERSION_COMPRESSED;
  2156. if (strcmp(name, "hybrid") == 0)
  2157. return POINT_CONVERSION_HYBRID;
  2158. return 0;
  2159. }
  2160. int EVP_PKEY_get_field_type(const EVP_PKEY *pkey)
  2161. {
  2162. char fstr[80];
  2163. size_t fstrlen;
  2164. if (pkey == NULL)
  2165. return 0;
  2166. if (pkey->keymgmt == NULL
  2167. || pkey->keydata == NULL) {
  2168. # ifndef OPENSSL_NO_EC
  2169. /* Might work through the legacy route */
  2170. const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  2171. const EC_GROUP *grp;
  2172. if (ec == NULL)
  2173. return 0;
  2174. grp = EC_KEY_get0_group(ec);
  2175. if (grp == NULL)
  2176. return 0;
  2177. return EC_GROUP_get_field_type(grp);
  2178. # else
  2179. return 0;
  2180. # endif
  2181. }
  2182. if (!EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_EC_FIELD_TYPE,
  2183. fstr, sizeof(fstr), &fstrlen))
  2184. return 0;
  2185. if (strcmp(fstr, SN_X9_62_prime_field) == 0)
  2186. return NID_X9_62_prime_field;
  2187. else if (strcmp(fstr, SN_X9_62_characteristic_two_field))
  2188. return NID_X9_62_characteristic_two_field;
  2189. return 0;
  2190. }
  2191. #endif