p_lib.c 66 KB

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