p_lib.c 71 KB

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