p_lib.c 70 KB

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