p_lib.c 66 KB

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