p_lib.c 66 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344
  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_DEPRECATED_3_0
  564. int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
  565. {
  566. if (!evp_pkey_is_legacy(pkey)) {
  567. const char *name = OBJ_nid2sn(type);
  568. if (name != NULL && EVP_PKEY_is_a(pkey, name))
  569. return 1;
  570. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  571. return 0;
  572. }
  573. if (pkey->type == type) {
  574. return 1; /* it already is that type */
  575. }
  576. /*
  577. * The application is requesting to alias this to a different pkey type,
  578. * but not one that resolves to the base type.
  579. */
  580. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
  581. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  582. return 0;
  583. }
  584. pkey->type = type;
  585. return 1;
  586. }
  587. # endif
  588. # ifndef OPENSSL_NO_ENGINE
  589. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  590. {
  591. if (e != NULL) {
  592. if (!ENGINE_init(e)) {
  593. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  594. return 0;
  595. }
  596. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  597. ENGINE_finish(e);
  598. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  599. return 0;
  600. }
  601. }
  602. ENGINE_finish(pkey->pmeth_engine);
  603. pkey->pmeth_engine = e;
  604. return 1;
  605. }
  606. ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
  607. {
  608. return pkey->engine;
  609. }
  610. # endif
  611. # ifndef OPENSSL_NO_DEPRECATED_3_0
  612. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  613. {
  614. int alias = type;
  615. # ifndef OPENSSL_NO_EC
  616. if ((key != NULL) && (EVP_PKEY_type(type) == EVP_PKEY_EC)) {
  617. const EC_GROUP *group = EC_KEY_get0_group(key);
  618. if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
  619. alias = EVP_PKEY_SM2;
  620. }
  621. # endif
  622. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  623. return 0;
  624. if (!EVP_PKEY_set_alias_type(pkey, alias))
  625. return 0;
  626. pkey->pkey.ptr = key;
  627. return (key != NULL);
  628. }
  629. # endif
  630. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  631. {
  632. if (pkey == NULL)
  633. return NULL;
  634. if (!evp_pkey_is_provided(pkey))
  635. return pkey->pkey.ptr;
  636. return NULL;
  637. }
  638. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  639. {
  640. const ASN1_OCTET_STRING *os = NULL;
  641. if (pkey->type != EVP_PKEY_HMAC) {
  642. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
  643. return NULL;
  644. }
  645. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  646. if (os != NULL) {
  647. *len = os->length;
  648. return os->data;
  649. }
  650. return NULL;
  651. }
  652. # ifndef OPENSSL_NO_POLY1305
  653. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  654. {
  655. const ASN1_OCTET_STRING *os = NULL;
  656. if (pkey->type != EVP_PKEY_POLY1305) {
  657. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
  658. return NULL;
  659. }
  660. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  661. if (os != NULL) {
  662. *len = os->length;
  663. return os->data;
  664. }
  665. return NULL;
  666. }
  667. # endif
  668. # ifndef OPENSSL_NO_SIPHASH
  669. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  670. {
  671. const ASN1_OCTET_STRING *os = NULL;
  672. if (pkey->type != EVP_PKEY_SIPHASH) {
  673. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
  674. return NULL;
  675. }
  676. os = evp_pkey_get_legacy((EVP_PKEY *)pkey);
  677. if (os != NULL) {
  678. *len = os->length;
  679. return os->data;
  680. }
  681. return NULL;
  682. }
  683. # endif
  684. # ifndef OPENSSL_NO_DSA
  685. static DSA *evp_pkey_get0_DSA_int(const EVP_PKEY *pkey)
  686. {
  687. if (pkey->type != EVP_PKEY_DSA) {
  688. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
  689. return NULL;
  690. }
  691. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  692. }
  693. const DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
  694. {
  695. return evp_pkey_get0_DSA_int(pkey);
  696. }
  697. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  698. {
  699. int ret = EVP_PKEY_assign_DSA(pkey, key);
  700. if (ret)
  701. DSA_up_ref(key);
  702. return ret;
  703. }
  704. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  705. {
  706. DSA *ret = evp_pkey_get0_DSA_int(pkey);
  707. if (ret != NULL)
  708. DSA_up_ref(ret);
  709. return ret;
  710. }
  711. # endif /* OPENSSL_NO_DSA */
  712. # ifndef OPENSSL_NO_EC
  713. static const ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
  714. {
  715. if (EVP_PKEY_base_id(pkey) != type) {
  716. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
  717. return NULL;
  718. }
  719. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  720. }
  721. static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
  722. {
  723. ECX_KEY *ret = (ECX_KEY *)evp_pkey_get0_ECX_KEY(pkey, type);
  724. if (ret != NULL && !ossl_ecx_key_up_ref(ret))
  725. ret = NULL;
  726. return ret;
  727. }
  728. # define IMPLEMENT_ECX_VARIANT(NAME) \
  729. ECX_KEY *ossl_evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
  730. { \
  731. return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
  732. }
  733. IMPLEMENT_ECX_VARIANT(X25519)
  734. IMPLEMENT_ECX_VARIANT(X448)
  735. IMPLEMENT_ECX_VARIANT(ED25519)
  736. IMPLEMENT_ECX_VARIANT(ED448)
  737. # endif
  738. # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  739. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  740. {
  741. int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
  742. int ret = EVP_PKEY_assign(pkey, type, key);
  743. if (ret)
  744. DH_up_ref(key);
  745. return ret;
  746. }
  747. DH *evp_pkey_get0_DH_int(const EVP_PKEY *pkey)
  748. {
  749. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  750. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
  751. return NULL;
  752. }
  753. return evp_pkey_get_legacy((EVP_PKEY *)pkey);
  754. }
  755. const DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
  756. {
  757. return evp_pkey_get0_DH_int(pkey);
  758. }
  759. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  760. {
  761. DH *ret = evp_pkey_get0_DH_int(pkey);
  762. if (ret != NULL)
  763. DH_up_ref(ret);
  764. return ret;
  765. }
  766. # endif
  767. int EVP_PKEY_type(int type)
  768. {
  769. int ret;
  770. const EVP_PKEY_ASN1_METHOD *ameth;
  771. ENGINE *e;
  772. ameth = EVP_PKEY_asn1_find(&e, type);
  773. if (ameth)
  774. ret = ameth->pkey_id;
  775. else
  776. ret = NID_undef;
  777. # ifndef OPENSSL_NO_ENGINE
  778. ENGINE_finish(e);
  779. # endif
  780. return ret;
  781. }
  782. int EVP_PKEY_id(const EVP_PKEY *pkey)
  783. {
  784. return pkey->type;
  785. }
  786. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  787. {
  788. return EVP_PKEY_type(pkey->type);
  789. }
  790. /*
  791. * These hard coded cases are pure hackery to get around the fact
  792. * that names in crypto/objects/objects.txt are a mess. There is
  793. * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
  794. * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
  795. * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
  796. * "DSA" is accurate... but still, better be safe and hard-code
  797. * names that we know.
  798. * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
  799. * EVP_PKEY_EC, because of aliasing.
  800. * This should be cleaned away along with all other #legacy support.
  801. */
  802. static const OSSL_ITEM standard_name2type[] = {
  803. { EVP_PKEY_RSA, "RSA" },
  804. { EVP_PKEY_RSA_PSS, "RSA-PSS" },
  805. { EVP_PKEY_EC, "EC" },
  806. { EVP_PKEY_ED25519, "ED25519" },
  807. { EVP_PKEY_ED448, "ED448" },
  808. { EVP_PKEY_X25519, "X25519" },
  809. { EVP_PKEY_X448, "X448" },
  810. { EVP_PKEY_SM2, "SM2" },
  811. { EVP_PKEY_DH, "DH" },
  812. { EVP_PKEY_DHX, "X9.42 DH" },
  813. { EVP_PKEY_DHX, "DHX" },
  814. { EVP_PKEY_DSA, "DSA" },
  815. };
  816. int evp_pkey_name2type(const char *name)
  817. {
  818. int type;
  819. size_t i;
  820. for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
  821. if (strcasecmp(name, standard_name2type[i].ptr) == 0)
  822. return (int)standard_name2type[i].id;
  823. }
  824. if ((type = EVP_PKEY_type(OBJ_sn2nid(name))) != NID_undef)
  825. return type;
  826. return EVP_PKEY_type(OBJ_ln2nid(name));
  827. }
  828. const char *evp_pkey_type2name(int type)
  829. {
  830. size_t i;
  831. for (i = 0; i < OSSL_NELEM(standard_name2type); i++) {
  832. if (type == (int)standard_name2type[i].id)
  833. return standard_name2type[i].ptr;
  834. }
  835. return OBJ_nid2sn(type);
  836. }
  837. int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
  838. {
  839. if (pkey->keymgmt == NULL) {
  840. int type = evp_pkey_name2type(name);
  841. return pkey->type == type;
  842. }
  843. return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
  844. }
  845. int EVP_PKEY_typenames_do_all(const EVP_PKEY *pkey,
  846. void (*fn)(const char *name, void *data),
  847. void *data)
  848. {
  849. if (!evp_pkey_is_typed(pkey))
  850. return 0;
  851. if (!evp_pkey_is_provided(pkey)) {
  852. const char *name = OBJ_nid2sn(EVP_PKEY_id(pkey));
  853. fn(name, data);
  854. return 1;
  855. }
  856. return EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
  857. }
  858. int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
  859. {
  860. if (pkey->keymgmt == NULL) {
  861. switch (EVP_PKEY_base_id(pkey)) {
  862. case EVP_PKEY_RSA:
  863. return 1;
  864. # ifndef OPENSSL_NO_DSA
  865. case EVP_PKEY_DSA:
  866. return 1;
  867. # endif
  868. # ifndef OPENSSL_NO_EC
  869. case EVP_PKEY_ED25519:
  870. case EVP_PKEY_ED448:
  871. return 1;
  872. case EVP_PKEY_EC: /* Including SM2 */
  873. return EC_KEY_can_sign(pkey->pkey.ec);
  874. # endif
  875. default:
  876. break;
  877. }
  878. } else {
  879. const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
  880. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  881. const char *supported_sig =
  882. pkey->keymgmt->query_operation_name != NULL
  883. ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
  884. : evp_first_name(prov, pkey->keymgmt->name_id);
  885. EVP_SIGNATURE *signature = NULL;
  886. signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
  887. if (signature != NULL) {
  888. EVP_SIGNATURE_free(signature);
  889. return 1;
  890. }
  891. }
  892. return 0;
  893. }
  894. static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
  895. {
  896. BIO_set_indent(*out, saved_indent);
  897. if (pop_f_prefix) {
  898. BIO *next = BIO_pop(*out);
  899. BIO_free(*out);
  900. *out = next;
  901. }
  902. return 1;
  903. }
  904. static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
  905. long indent)
  906. {
  907. *pop_f_prefix = 0;
  908. *saved_indent = 0;
  909. if (indent > 0) {
  910. long i = BIO_get_indent(*out);
  911. *saved_indent = (i < 0 ? 0 : i);
  912. if (BIO_set_indent(*out, indent) <= 0) {
  913. if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
  914. return 0;
  915. *pop_f_prefix = 1;
  916. }
  917. if (BIO_set_indent(*out, indent) <= 0) {
  918. print_reset_indent(out, *pop_f_prefix, *saved_indent);
  919. return 0;
  920. }
  921. }
  922. return 1;
  923. }
  924. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  925. const char *kstr)
  926. {
  927. return BIO_indent(out, indent, 128)
  928. && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  929. kstr, OBJ_nid2ln(pkey->type)) > 0;
  930. }
  931. static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
  932. int selection /* For provided encoding */,
  933. const char *propquery /* For provided encoding */,
  934. int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
  935. int indent, ASN1_PCTX *pctx),
  936. ASN1_PCTX *legacy_pctx /* For legacy print */)
  937. {
  938. int pop_f_prefix;
  939. long saved_indent;
  940. OSSL_ENCODER_CTX *ctx = NULL;
  941. int ret = -2; /* default to unsupported */
  942. if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
  943. return 0;
  944. ctx = OSSL_ENCODER_CTX_new_for_pkey(pkey, selection, "TEXT", NULL,
  945. propquery);
  946. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
  947. ret = OSSL_ENCODER_to_bio(ctx, out);
  948. OSSL_ENCODER_CTX_free(ctx);
  949. if (ret != -2)
  950. goto end;
  951. /* legacy fallback */
  952. if (legacy_print != NULL)
  953. ret = legacy_print(out, pkey, 0, legacy_pctx);
  954. else
  955. ret = unsup_alg(out, pkey, 0, "Public Key");
  956. end:
  957. print_reset_indent(&out, pop_f_prefix, saved_indent);
  958. return ret;
  959. }
  960. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  961. int indent, ASN1_PCTX *pctx)
  962. {
  963. return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
  964. (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
  965. pctx);
  966. }
  967. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  968. int indent, ASN1_PCTX *pctx)
  969. {
  970. return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
  971. (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
  972. pctx);
  973. }
  974. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  975. int indent, ASN1_PCTX *pctx)
  976. {
  977. return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
  978. (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
  979. pctx);
  980. }
  981. # ifndef OPENSSL_NO_STDIO
  982. int EVP_PKEY_print_public_fp(FILE *fp, const EVP_PKEY *pkey,
  983. int indent, ASN1_PCTX *pctx)
  984. {
  985. int ret;
  986. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  987. if (b == NULL)
  988. return 0;
  989. ret = EVP_PKEY_print_public(b, pkey, indent, pctx);
  990. BIO_free(b);
  991. return ret;
  992. }
  993. int EVP_PKEY_print_private_fp(FILE *fp, const EVP_PKEY *pkey,
  994. int indent, ASN1_PCTX *pctx)
  995. {
  996. int ret;
  997. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  998. if (b == NULL)
  999. return 0;
  1000. ret = EVP_PKEY_print_private(b, pkey, indent, pctx);
  1001. BIO_free(b);
  1002. return ret;
  1003. }
  1004. int EVP_PKEY_print_params_fp(FILE *fp, const EVP_PKEY *pkey,
  1005. int indent, ASN1_PCTX *pctx)
  1006. {
  1007. int ret;
  1008. BIO *b = BIO_new_fp(fp, BIO_NOCLOSE);
  1009. if (b == NULL)
  1010. return 0;
  1011. ret = EVP_PKEY_print_params(b, pkey, indent, pctx);
  1012. BIO_free(b);
  1013. return ret;
  1014. }
  1015. # endif
  1016. static void mdname2nid(const char *mdname, void *data)
  1017. {
  1018. int *nid = (int *)data;
  1019. if (*nid != NID_undef)
  1020. return;
  1021. *nid = OBJ_sn2nid(mdname);
  1022. if (*nid == NID_undef)
  1023. *nid = OBJ_ln2nid(mdname);
  1024. }
  1025. static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
  1026. int arg1, void *arg2)
  1027. {
  1028. if (pkey->keymgmt == NULL)
  1029. return 0;
  1030. switch (op) {
  1031. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  1032. {
  1033. char mdname[80] = "";
  1034. int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
  1035. sizeof(mdname));
  1036. if (rv > 0) {
  1037. int mdnum;
  1038. OSSL_LIB_CTX *libctx = ossl_provider_libctx(pkey->keymgmt->prov);
  1039. /* Make sure the MD is in the namemap if available */
  1040. EVP_MD *md = EVP_MD_fetch(libctx, mdname, NULL);
  1041. OSSL_NAMEMAP *namemap = ossl_namemap_stored(libctx);
  1042. int nid = NID_undef;
  1043. /*
  1044. * The only reason to fetch the MD was to make sure it is in the
  1045. * namemap. We can immediately free it.
  1046. */
  1047. EVP_MD_free(md);
  1048. mdnum = ossl_namemap_name2num(namemap, mdname);
  1049. if (mdnum == 0)
  1050. return 0;
  1051. /*
  1052. * We have the namemap number - now we need to find the
  1053. * associated nid
  1054. */
  1055. if (!ossl_namemap_doall_names(namemap, mdnum, mdname2nid, &nid))
  1056. return 0;
  1057. *(int *)arg2 = nid;
  1058. }
  1059. return rv;
  1060. }
  1061. default:
  1062. return -2;
  1063. }
  1064. }
  1065. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  1066. {
  1067. if (pkey->ameth == NULL)
  1068. return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
  1069. if (pkey->ameth->pkey_ctrl == NULL)
  1070. return -2;
  1071. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  1072. }
  1073. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  1074. {
  1075. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  1076. }
  1077. int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
  1078. char *mdname, size_t mdname_sz)
  1079. {
  1080. if (pkey->ameth == NULL)
  1081. return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
  1082. pkey->keydata,
  1083. mdname, mdname_sz);
  1084. {
  1085. int nid = NID_undef;
  1086. int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
  1087. const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
  1088. if (rv > 0)
  1089. OPENSSL_strlcpy(mdname, name, mdname_sz);
  1090. return rv;
  1091. }
  1092. }
  1093. int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
  1094. size_t *gname_len)
  1095. {
  1096. return EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
  1097. gname, gname_sz, gname_len);
  1098. }
  1099. int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
  1100. {
  1101. int rv, default_nid;
  1102. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
  1103. if (rv == -2) {
  1104. /*
  1105. * If there is a mandatory default digest and this isn't it, then
  1106. * the answer is 'no'.
  1107. */
  1108. rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
  1109. if (rv == 2)
  1110. return (nid == default_nid);
  1111. /* zero is an error from EVP_PKEY_get_default_digest_nid() */
  1112. if (rv == 0)
  1113. return -1;
  1114. }
  1115. return rv;
  1116. }
  1117. int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
  1118. size_t publen)
  1119. {
  1120. if (pkey != NULL && evp_pkey_is_provided(pkey))
  1121. return
  1122. EVP_PKEY_set_octet_string_param(pkey,
  1123. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1124. (unsigned char *)pub, publen);
  1125. if (publen > INT_MAX)
  1126. return 0;
  1127. /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
  1128. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
  1129. (void *)pub) <= 0)
  1130. return 0;
  1131. return 1;
  1132. }
  1133. size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
  1134. {
  1135. int rv;
  1136. if (pkey != NULL && evp_pkey_is_provided(pkey)) {
  1137. size_t return_size = OSSL_PARAM_UNMODIFIED;
  1138. /*
  1139. * We know that this is going to fail, but it will give us a size
  1140. * to allocate.
  1141. */
  1142. EVP_PKEY_get_octet_string_param(pkey,
  1143. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1144. NULL, 0, &return_size);
  1145. if (return_size == OSSL_PARAM_UNMODIFIED)
  1146. return 0;
  1147. *ppub = OPENSSL_malloc(return_size);
  1148. if (*ppub == NULL)
  1149. return 0;
  1150. if (!EVP_PKEY_get_octet_string_param(pkey,
  1151. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1152. *ppub, return_size, NULL))
  1153. return 0;
  1154. return return_size;
  1155. }
  1156. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
  1157. if (rv <= 0)
  1158. return 0;
  1159. return rv;
  1160. }
  1161. #endif /* FIPS_MODULE */
  1162. /*- All methods below can also be used in FIPS_MODULE */
  1163. EVP_PKEY *EVP_PKEY_new(void)
  1164. {
  1165. EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
  1166. if (ret == NULL) {
  1167. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1168. return NULL;
  1169. }
  1170. ret->type = EVP_PKEY_NONE;
  1171. ret->save_type = EVP_PKEY_NONE;
  1172. ret->references = 1;
  1173. ret->save_parameters = 1;
  1174. ret->lock = CRYPTO_THREAD_lock_new();
  1175. if (ret->lock == NULL) {
  1176. EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1177. goto err;
  1178. }
  1179. #ifndef FIPS_MODULE
  1180. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
  1181. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1182. goto err;
  1183. }
  1184. #endif
  1185. return ret;
  1186. err:
  1187. CRYPTO_THREAD_lock_free(ret->lock);
  1188. OPENSSL_free(ret);
  1189. return NULL;
  1190. }
  1191. /*
  1192. * Setup a public key management method.
  1193. *
  1194. * For legacy keys, either |type| or |str| is expected to have the type
  1195. * information. In this case, the setup consists of finding an ASN1 method
  1196. * and potentially an ENGINE, and setting those fields in |pkey|.
  1197. *
  1198. * For provider side keys, |keymgmt| is expected to be non-NULL. In this
  1199. * case, the setup consists of setting the |keymgmt| field in |pkey|.
  1200. *
  1201. * If pkey is NULL just return 1 or 0 if the key management method exists.
  1202. */
  1203. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  1204. int len, EVP_KEYMGMT *keymgmt)
  1205. {
  1206. #ifndef FIPS_MODULE
  1207. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  1208. ENGINE **eptr = (e == NULL) ? &e : NULL;
  1209. #endif
  1210. /*
  1211. * The setups can't set both legacy and provider side methods.
  1212. * It is forbidden
  1213. */
  1214. if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
  1215. || !ossl_assert(e == NULL || keymgmt == NULL)) {
  1216. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1217. return 0;
  1218. }
  1219. if (pkey != NULL) {
  1220. int free_it = 0;
  1221. #ifndef FIPS_MODULE
  1222. free_it = free_it || pkey->pkey.ptr != NULL;
  1223. #endif
  1224. free_it = free_it || pkey->keydata != NULL;
  1225. if (free_it)
  1226. evp_pkey_free_it(pkey);
  1227. #ifndef FIPS_MODULE
  1228. /*
  1229. * If key type matches and a method exists then this lookup has
  1230. * succeeded once so just indicate success.
  1231. */
  1232. if (pkey->type != EVP_PKEY_NONE
  1233. && type == pkey->save_type
  1234. && pkey->ameth != NULL)
  1235. return 1;
  1236. # ifndef OPENSSL_NO_ENGINE
  1237. /* If we have ENGINEs release them */
  1238. ENGINE_finish(pkey->engine);
  1239. pkey->engine = NULL;
  1240. ENGINE_finish(pkey->pmeth_engine);
  1241. pkey->pmeth_engine = NULL;
  1242. # endif
  1243. #endif
  1244. }
  1245. #ifndef FIPS_MODULE
  1246. if (str != NULL)
  1247. ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
  1248. else if (type != EVP_PKEY_NONE)
  1249. ameth = EVP_PKEY_asn1_find(eptr, type);
  1250. # ifndef OPENSSL_NO_ENGINE
  1251. if (pkey == NULL && eptr != NULL)
  1252. ENGINE_finish(e);
  1253. # endif
  1254. #endif
  1255. {
  1256. int check = 1;
  1257. #ifndef FIPS_MODULE
  1258. check = check && ameth == NULL;
  1259. #endif
  1260. check = check && keymgmt == NULL;
  1261. if (check) {
  1262. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  1263. return 0;
  1264. }
  1265. }
  1266. if (pkey != NULL) {
  1267. if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
  1268. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1269. return 0;
  1270. }
  1271. pkey->keymgmt = keymgmt;
  1272. pkey->save_type = type;
  1273. pkey->type = type;
  1274. #ifndef FIPS_MODULE
  1275. /*
  1276. * If the internal "origin" key is provider side, don't save |ameth|.
  1277. * The main reason is that |ameth| is one factor to detect that the
  1278. * internal "origin" key is a legacy one.
  1279. */
  1280. if (keymgmt == NULL)
  1281. pkey->ameth = ameth;
  1282. pkey->engine = e;
  1283. /*
  1284. * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
  1285. * for any key type that has a legacy implementation, regardless of
  1286. * if the internal key is a legacy or a provider side one. When
  1287. * there is no legacy implementation for the key, the type becomes
  1288. * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
  1289. * with functions that expect legacy internal keys.
  1290. */
  1291. if (ameth != NULL)
  1292. pkey->type = ameth->pkey_id;
  1293. else
  1294. pkey->type = EVP_PKEY_KEYMGMT;
  1295. #endif
  1296. }
  1297. return 1;
  1298. }
  1299. #ifndef FIPS_MODULE
  1300. static void find_ameth(const char *name, void *data)
  1301. {
  1302. const char **str = data;
  1303. /*
  1304. * The error messages from pkey_set_type() are uninteresting here,
  1305. * and misleading.
  1306. */
  1307. ERR_set_mark();
  1308. if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
  1309. NULL)) {
  1310. if (str[0] == NULL)
  1311. str[0] = name;
  1312. else if (str[1] == NULL)
  1313. str[1] = name;
  1314. }
  1315. ERR_pop_to_mark();
  1316. }
  1317. #endif
  1318. int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
  1319. {
  1320. #ifndef FIPS_MODULE
  1321. # define EVP_PKEY_TYPE_STR str[0]
  1322. # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
  1323. /*
  1324. * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
  1325. * Ideally, only one should be found. If two (or more) are found, the
  1326. * match is ambiguous. This should never happen, but...
  1327. */
  1328. const char *str[2] = { NULL, NULL };
  1329. if (!EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str)
  1330. || str[1] != NULL) {
  1331. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1332. return 0;
  1333. }
  1334. #else
  1335. # define EVP_PKEY_TYPE_STR NULL
  1336. # define EVP_PKEY_TYPE_STRLEN -1
  1337. #endif
  1338. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
  1339. EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
  1340. keymgmt);
  1341. #undef EVP_PKEY_TYPE_STR
  1342. #undef EVP_PKEY_TYPE_STRLEN
  1343. }
  1344. int EVP_PKEY_up_ref(EVP_PKEY *pkey)
  1345. {
  1346. int i;
  1347. if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
  1348. return 0;
  1349. REF_PRINT_COUNT("EVP_PKEY", pkey);
  1350. REF_ASSERT_ISNT(i < 2);
  1351. return ((i > 1) ? 1 : 0);
  1352. }
  1353. #ifndef FIPS_MODULE
  1354. EVP_PKEY *EVP_PKEY_dup(EVP_PKEY *pkey)
  1355. {
  1356. EVP_PKEY *dup_pk;
  1357. if (pkey == NULL) {
  1358. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_NULL_PARAMETER);
  1359. return NULL;
  1360. }
  1361. if ((dup_pk = EVP_PKEY_new()) == NULL)
  1362. return NULL;
  1363. if (evp_pkey_is_blank(pkey))
  1364. goto done;
  1365. if (evp_pkey_is_provided(pkey)) {
  1366. if (!evp_keymgmt_util_copy(dup_pk, pkey,
  1367. OSSL_KEYMGMT_SELECT_ALL))
  1368. goto err;
  1369. goto done;
  1370. }
  1371. if (evp_pkey_is_legacy(pkey)) {
  1372. const EVP_PKEY_ASN1_METHOD *ameth = pkey->ameth;
  1373. if (ameth == NULL || ameth->copy == NULL) {
  1374. if (pkey->pkey.ptr == NULL /* empty key, just set type */
  1375. && EVP_PKEY_set_type(dup_pk, pkey->type) != 0)
  1376. goto done;
  1377. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1378. goto err;
  1379. }
  1380. if (!ameth->copy(dup_pk, pkey))
  1381. goto err;
  1382. goto done;
  1383. }
  1384. goto err;
  1385. done:
  1386. /* copy auxiliary data */
  1387. if (!CRYPTO_dup_ex_data(CRYPTO_EX_INDEX_EVP_PKEY,
  1388. &dup_pk->ex_data, &pkey->ex_data))
  1389. goto err;
  1390. if (pkey->attributes != NULL) {
  1391. if ((dup_pk->attributes = ossl_x509at_dup(pkey->attributes)) == NULL)
  1392. goto err;
  1393. }
  1394. return dup_pk;
  1395. err:
  1396. EVP_PKEY_free(dup_pk);
  1397. return NULL;
  1398. }
  1399. void evp_pkey_free_legacy(EVP_PKEY *x)
  1400. {
  1401. const EVP_PKEY_ASN1_METHOD *ameth = x->ameth;
  1402. ENGINE *tmpe = NULL;
  1403. if (ameth == NULL && x->legacy_cache_pkey.ptr != NULL)
  1404. ameth = EVP_PKEY_asn1_find(&tmpe, x->type);
  1405. if (ameth != NULL) {
  1406. if (x->legacy_cache_pkey.ptr != NULL) {
  1407. /*
  1408. * We should never have both a legacy origin key, and a key in the
  1409. * legacy cache.
  1410. */
  1411. assert(x->pkey.ptr == NULL);
  1412. /*
  1413. * For the purposes of freeing we make the legacy cache look like
  1414. * a legacy origin key.
  1415. */
  1416. x->pkey = x->legacy_cache_pkey;
  1417. x->legacy_cache_pkey.ptr = NULL;
  1418. }
  1419. if (ameth->pkey_free != NULL)
  1420. ameth->pkey_free(x);
  1421. x->pkey.ptr = NULL;
  1422. }
  1423. # ifndef OPENSSL_NO_ENGINE
  1424. ENGINE_finish(tmpe);
  1425. ENGINE_finish(x->engine);
  1426. x->engine = NULL;
  1427. ENGINE_finish(x->pmeth_engine);
  1428. x->pmeth_engine = NULL;
  1429. # endif
  1430. }
  1431. #endif /* FIPS_MODULE */
  1432. static void evp_pkey_free_it(EVP_PKEY *x)
  1433. {
  1434. /* internal function; x is never NULL */
  1435. evp_keymgmt_util_clear_operation_cache(x, 1);
  1436. #ifndef FIPS_MODULE
  1437. evp_pkey_free_legacy(x);
  1438. #endif
  1439. if (x->keymgmt != NULL) {
  1440. evp_keymgmt_freedata(x->keymgmt, x->keydata);
  1441. EVP_KEYMGMT_free(x->keymgmt);
  1442. x->keymgmt = NULL;
  1443. x->keydata = NULL;
  1444. }
  1445. x->type = EVP_PKEY_NONE;
  1446. }
  1447. void EVP_PKEY_free(EVP_PKEY *x)
  1448. {
  1449. int i;
  1450. if (x == NULL)
  1451. return;
  1452. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  1453. REF_PRINT_COUNT("EVP_PKEY", x);
  1454. if (i > 0)
  1455. return;
  1456. REF_ASSERT_ISNT(i < 0);
  1457. evp_pkey_free_it(x);
  1458. #ifndef FIPS_MODULE
  1459. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
  1460. #endif
  1461. CRYPTO_THREAD_lock_free(x->lock);
  1462. #ifndef FIPS_MODULE
  1463. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  1464. #endif
  1465. OPENSSL_free(x);
  1466. }
  1467. int EVP_PKEY_size(const EVP_PKEY *pkey)
  1468. {
  1469. int size = 0;
  1470. if (pkey != NULL) {
  1471. size = pkey->cache.size;
  1472. #ifndef FIPS_MODULE
  1473. if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
  1474. size = pkey->ameth->pkey_size(pkey);
  1475. #endif
  1476. }
  1477. return size < 0 ? 0 : size;
  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