p_lib.c 67 KB

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