p_lib.c 66 KB

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