p_lib.c 69 KB

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