p_lib.c 70 KB

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