p_lib.c 71 KB

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