p_lib.c 61 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115
  1. /*
  2. * Copyright 1995-2020 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 <openssl/bn.h>
  18. #include <openssl/err.h>
  19. #include <openssl/objects.h>
  20. #include <openssl/evp.h>
  21. #include <openssl/x509.h>
  22. #include <openssl/rsa.h>
  23. #include <openssl/dsa.h>
  24. #include <openssl/dh.h>
  25. #include <openssl/ec.h>
  26. #include <openssl/cmac.h>
  27. #include <openssl/engine.h>
  28. #include <openssl/params.h>
  29. #include <openssl/param_build.h>
  30. #include <openssl/encoder.h>
  31. #include <openssl/core_names.h>
  32. #include "internal/ffc.h"
  33. #include "crypto/asn1.h"
  34. #include "crypto/evp.h"
  35. #include "crypto/ecx.h"
  36. #include "internal/provider.h"
  37. #include "evp_local.h"
  38. #include "crypto/ec.h"
  39. /* TODO remove this when the EVP_PKEY_is_a() #legacy support hack is removed */
  40. #include "e_os.h" /* strcasecmp on Windows */
  41. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  42. int len, EVP_KEYMGMT *keymgmt);
  43. static void evp_pkey_free_it(EVP_PKEY *key);
  44. #ifndef FIPS_MODULE
  45. /* The type of parameters selected in key parameter functions */
  46. # define SELECT_PARAMETERS OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS
  47. int EVP_PKEY_bits(const EVP_PKEY *pkey)
  48. {
  49. int size = 0;
  50. if (pkey != NULL) {
  51. size = pkey->cache.bits;
  52. if (pkey->ameth != NULL && pkey->ameth->pkey_bits != NULL)
  53. size = pkey->ameth->pkey_bits(pkey);
  54. }
  55. return size < 0 ? 0 : size;
  56. }
  57. int EVP_PKEY_security_bits(const EVP_PKEY *pkey)
  58. {
  59. int size = 0;
  60. if (pkey != NULL) {
  61. size = pkey->cache.security_bits;
  62. if (pkey->ameth != NULL && pkey->ameth->pkey_security_bits != NULL)
  63. size = pkey->ameth->pkey_security_bits(pkey);
  64. }
  65. return size < 0 ? 0 : size;
  66. }
  67. int EVP_PKEY_save_parameters(EVP_PKEY *pkey, int mode)
  68. {
  69. # ifndef OPENSSL_NO_DSA
  70. if (pkey->type == EVP_PKEY_DSA) {
  71. int ret = pkey->save_parameters;
  72. if (mode >= 0)
  73. pkey->save_parameters = mode;
  74. return ret;
  75. }
  76. # endif
  77. # ifndef OPENSSL_NO_EC
  78. if (pkey->type == EVP_PKEY_EC) {
  79. int ret = pkey->save_parameters;
  80. if (mode >= 0)
  81. pkey->save_parameters = mode;
  82. return ret;
  83. }
  84. # endif
  85. return 0;
  86. }
  87. int EVP_PKEY_set_ex_data(EVP_PKEY *key, int idx, void *arg)
  88. {
  89. return CRYPTO_set_ex_data(&key->ex_data, idx, arg);
  90. }
  91. void *EVP_PKEY_get_ex_data(const EVP_PKEY *key, int idx)
  92. {
  93. return CRYPTO_get_ex_data(&key->ex_data, idx);
  94. }
  95. int EVP_PKEY_copy_parameters(EVP_PKEY *to, const EVP_PKEY *from)
  96. {
  97. /*
  98. * TODO: clean up legacy stuff from this function when legacy support
  99. * 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_ex(const unsigned char *priv, size_t len,
  541. const char *cipher_name, OSSL_LIB_CTX *libctx,
  542. const char *propq)
  543. {
  544. return new_cmac_key_int(priv, len, cipher_name, NULL, libctx, propq, NULL);
  545. }
  546. EVP_PKEY *EVP_PKEY_new_CMAC_key(ENGINE *e, const unsigned char *priv,
  547. size_t len, const EVP_CIPHER *cipher)
  548. {
  549. return new_cmac_key_int(priv, len, NULL, cipher, NULL, NULL, e);
  550. }
  551. int EVP_PKEY_set_type(EVP_PKEY *pkey, int type)
  552. {
  553. return pkey_set_type(pkey, NULL, type, NULL, -1, NULL);
  554. }
  555. int EVP_PKEY_set_type_str(EVP_PKEY *pkey, const char *str, int len)
  556. {
  557. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE, str, len, NULL);
  558. }
  559. #ifndef OPENSSL_NO_DEPRECATED_3_0
  560. int EVP_PKEY_set_alias_type(EVP_PKEY *pkey, int type)
  561. {
  562. if (!evp_pkey_is_legacy(pkey)) {
  563. const char *name = OBJ_nid2sn(type);
  564. if (name != NULL && EVP_PKEY_is_a(pkey, name))
  565. return 1;
  566. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_OPERATION);
  567. return 0;
  568. }
  569. if (pkey->type == type) {
  570. return 1; /* it already is that type */
  571. }
  572. /*
  573. * The application is requesting to alias this to a different pkey type,
  574. * but not one that resolves to the base type.
  575. */
  576. if (EVP_PKEY_type(type) != EVP_PKEY_base_id(pkey)) {
  577. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  578. return 0;
  579. }
  580. pkey->type = type;
  581. return 1;
  582. }
  583. #endif
  584. # ifndef OPENSSL_NO_ENGINE
  585. int EVP_PKEY_set1_engine(EVP_PKEY *pkey, ENGINE *e)
  586. {
  587. if (e != NULL) {
  588. if (!ENGINE_init(e)) {
  589. ERR_raise(ERR_LIB_EVP, ERR_R_ENGINE_LIB);
  590. return 0;
  591. }
  592. if (ENGINE_get_pkey_meth(e, pkey->type) == NULL) {
  593. ENGINE_finish(e);
  594. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  595. return 0;
  596. }
  597. }
  598. ENGINE_finish(pkey->pmeth_engine);
  599. pkey->pmeth_engine = e;
  600. return 1;
  601. }
  602. ENGINE *EVP_PKEY_get0_engine(const EVP_PKEY *pkey)
  603. {
  604. return pkey->engine;
  605. }
  606. # endif
  607. int EVP_PKEY_assign(EVP_PKEY *pkey, int type, void *key)
  608. {
  609. int alias = type;
  610. #ifndef OPENSSL_NO_EC
  611. if ((key != NULL) && (EVP_PKEY_type(type) == EVP_PKEY_EC)) {
  612. const EC_GROUP *group = EC_KEY_get0_group(key);
  613. if (group != NULL && EC_GROUP_get_curve_name(group) == NID_sm2)
  614. alias = EVP_PKEY_SM2;
  615. }
  616. #endif
  617. if (pkey == NULL || !EVP_PKEY_set_type(pkey, type))
  618. return 0;
  619. if (!EVP_PKEY_set_alias_type(pkey, alias))
  620. return 0;
  621. pkey->pkey.ptr = key;
  622. return (key != NULL);
  623. }
  624. void *EVP_PKEY_get0(const EVP_PKEY *pkey)
  625. {
  626. if (pkey == NULL)
  627. return NULL;
  628. if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
  629. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
  630. return NULL;
  631. }
  632. return pkey->pkey.ptr;
  633. }
  634. const unsigned char *EVP_PKEY_get0_hmac(const EVP_PKEY *pkey, size_t *len)
  635. {
  636. ASN1_OCTET_STRING *os = NULL;
  637. if (pkey->type != EVP_PKEY_HMAC) {
  638. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_AN_HMAC_KEY);
  639. return NULL;
  640. }
  641. os = EVP_PKEY_get0(pkey);
  642. *len = os->length;
  643. return os->data;
  644. }
  645. # ifndef OPENSSL_NO_POLY1305
  646. const unsigned char *EVP_PKEY_get0_poly1305(const EVP_PKEY *pkey, size_t *len)
  647. {
  648. ASN1_OCTET_STRING *os = NULL;
  649. if (pkey->type != EVP_PKEY_POLY1305) {
  650. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_POLY1305_KEY);
  651. return NULL;
  652. }
  653. os = EVP_PKEY_get0(pkey);
  654. *len = os->length;
  655. return os->data;
  656. }
  657. # endif
  658. # ifndef OPENSSL_NO_SIPHASH
  659. const unsigned char *EVP_PKEY_get0_siphash(const EVP_PKEY *pkey, size_t *len)
  660. {
  661. ASN1_OCTET_STRING *os = NULL;
  662. if (pkey->type != EVP_PKEY_SIPHASH) {
  663. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_SIPHASH_KEY);
  664. return NULL;
  665. }
  666. os = EVP_PKEY_get0(pkey);
  667. *len = os->length;
  668. return os->data;
  669. }
  670. # endif
  671. # ifndef OPENSSL_NO_DSA
  672. DSA *EVP_PKEY_get0_DSA(const EVP_PKEY *pkey)
  673. {
  674. if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
  675. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
  676. return NULL;
  677. }
  678. if (pkey->type != EVP_PKEY_DSA) {
  679. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DSA_KEY);
  680. return NULL;
  681. }
  682. return pkey->pkey.dsa;
  683. }
  684. int EVP_PKEY_set1_DSA(EVP_PKEY *pkey, DSA *key)
  685. {
  686. int ret = EVP_PKEY_assign_DSA(pkey, key);
  687. if (ret)
  688. DSA_up_ref(key);
  689. return ret;
  690. }
  691. DSA *EVP_PKEY_get1_DSA(EVP_PKEY *pkey)
  692. {
  693. DSA *ret = EVP_PKEY_get0_DSA(pkey);
  694. if (ret != NULL)
  695. DSA_up_ref(ret);
  696. return ret;
  697. }
  698. # endif /* OPENSSL_NO_DSA */
  699. #endif /* FIPS_MODULE */
  700. #ifndef FIPS_MODULE
  701. # ifndef OPENSSL_NO_EC
  702. int EVP_PKEY_set1_EC_KEY(EVP_PKEY *pkey, EC_KEY *key)
  703. {
  704. int ret = EVP_PKEY_assign_EC_KEY(pkey, key);
  705. if (ret)
  706. EC_KEY_up_ref(key);
  707. return ret;
  708. }
  709. EC_KEY *EVP_PKEY_get0_EC_KEY(const EVP_PKEY *pkey)
  710. {
  711. if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
  712. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
  713. return NULL;
  714. }
  715. if (EVP_PKEY_base_id(pkey) != EVP_PKEY_EC) {
  716. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_EC_KEY);
  717. return NULL;
  718. }
  719. return pkey->pkey.ec;
  720. }
  721. EC_KEY *EVP_PKEY_get1_EC_KEY(EVP_PKEY *pkey)
  722. {
  723. EC_KEY *ret = EVP_PKEY_get0_EC_KEY(pkey);
  724. if (ret != NULL)
  725. EC_KEY_up_ref(ret);
  726. return ret;
  727. }
  728. static ECX_KEY *evp_pkey_get0_ECX_KEY(const EVP_PKEY *pkey, int type)
  729. {
  730. if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
  731. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
  732. return NULL;
  733. }
  734. if (EVP_PKEY_base_id(pkey) != type) {
  735. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_ECX_KEY);
  736. return NULL;
  737. }
  738. return pkey->pkey.ecx;
  739. }
  740. static ECX_KEY *evp_pkey_get1_ECX_KEY(EVP_PKEY *pkey, int type)
  741. {
  742. ECX_KEY *ret = evp_pkey_get0_ECX_KEY(pkey, type);
  743. if (ret != NULL)
  744. ecx_key_up_ref(ret);
  745. return ret;
  746. }
  747. # define IMPLEMENT_ECX_VARIANT(NAME) \
  748. ECX_KEY *evp_pkey_get1_##NAME(EVP_PKEY *pkey) \
  749. { \
  750. return evp_pkey_get1_ECX_KEY(pkey, EVP_PKEY_##NAME); \
  751. }
  752. IMPLEMENT_ECX_VARIANT(X25519)
  753. IMPLEMENT_ECX_VARIANT(X448)
  754. IMPLEMENT_ECX_VARIANT(ED25519)
  755. IMPLEMENT_ECX_VARIANT(ED448)
  756. # endif
  757. # if !defined(OPENSSL_NO_DH) && !defined(OPENSSL_NO_DEPRECATED_3_0)
  758. int EVP_PKEY_set1_DH(EVP_PKEY *pkey, DH *key)
  759. {
  760. int type = DH_get0_q(key) == NULL ? EVP_PKEY_DH : EVP_PKEY_DHX;
  761. int ret = EVP_PKEY_assign(pkey, type, key);
  762. if (ret)
  763. DH_up_ref(key);
  764. return ret;
  765. }
  766. DH *EVP_PKEY_get0_DH(const EVP_PKEY *pkey)
  767. {
  768. if (!evp_pkey_downgrade((EVP_PKEY *)pkey)) {
  769. ERR_raise(ERR_LIB_EVP, EVP_R_INACCESSIBLE_KEY);
  770. return NULL;
  771. }
  772. if (pkey->type != EVP_PKEY_DH && pkey->type != EVP_PKEY_DHX) {
  773. ERR_raise(ERR_LIB_EVP, EVP_R_EXPECTING_A_DH_KEY);
  774. return NULL;
  775. }
  776. return pkey->pkey.dh;
  777. }
  778. DH *EVP_PKEY_get1_DH(EVP_PKEY *pkey)
  779. {
  780. DH *ret = EVP_PKEY_get0_DH(pkey);
  781. if (ret != NULL)
  782. DH_up_ref(ret);
  783. return ret;
  784. }
  785. # endif
  786. int EVP_PKEY_type(int type)
  787. {
  788. int ret;
  789. const EVP_PKEY_ASN1_METHOD *ameth;
  790. ENGINE *e;
  791. ameth = EVP_PKEY_asn1_find(&e, type);
  792. if (ameth)
  793. ret = ameth->pkey_id;
  794. else
  795. ret = NID_undef;
  796. # ifndef OPENSSL_NO_ENGINE
  797. ENGINE_finish(e);
  798. # endif
  799. return ret;
  800. }
  801. int EVP_PKEY_id(const EVP_PKEY *pkey)
  802. {
  803. return pkey->type;
  804. }
  805. int EVP_PKEY_base_id(const EVP_PKEY *pkey)
  806. {
  807. return EVP_PKEY_type(pkey->type);
  808. }
  809. #ifndef FIPS_MODULE
  810. int evp_pkey_name2type(const char *name)
  811. {
  812. /*
  813. * These hard coded cases are pure hackery to get around the fact
  814. * that names in crypto/objects/objects.txt are a mess. There is
  815. * no "EC", and "RSA" leads to the NID for 2.5.8.1.1, an OID that's
  816. * fallen out in favor of { pkcs-1 1 }, i.e. 1.2.840.113549.1.1.1,
  817. * the NID of which is used for EVP_PKEY_RSA. Strangely enough,
  818. * "DSA" is accurate... but still, better be safe and hard-code
  819. * names that we know.
  820. * On a similar topic, EVP_PKEY_type(EVP_PKEY_SM2) will result in
  821. * EVP_PKEY_EC, because of aliasing.
  822. * TODO Clean this away along with all other #legacy support.
  823. */
  824. int type = NID_undef;
  825. if (strcasecmp(name, "RSA") == 0)
  826. type = EVP_PKEY_RSA;
  827. else if (strcasecmp(name, "RSA-PSS") == 0)
  828. type = EVP_PKEY_RSA_PSS;
  829. else if (strcasecmp(name, "EC") == 0)
  830. type = EVP_PKEY_EC;
  831. else if (strcasecmp(name, "ED25519") == 0)
  832. type = EVP_PKEY_ED25519;
  833. else if (strcasecmp(name, "ED448") == 0)
  834. type = EVP_PKEY_ED448;
  835. else if (strcasecmp(name, "X25519") == 0)
  836. type = EVP_PKEY_X25519;
  837. else if (strcasecmp(name, "X448") == 0)
  838. type = EVP_PKEY_X448;
  839. else if (strcasecmp(name, "SM2") == 0)
  840. type = EVP_PKEY_SM2;
  841. else if (strcasecmp(name, "DH") == 0)
  842. type = EVP_PKEY_DH;
  843. else if (strcasecmp(name, "X9.42 DH") == 0)
  844. type = EVP_PKEY_DHX;
  845. else if (strcasecmp(name, "DSA") == 0)
  846. type = EVP_PKEY_DSA;
  847. if (type == NID_undef)
  848. type = EVP_PKEY_type(OBJ_sn2nid(name));
  849. if (type == NID_undef)
  850. type = EVP_PKEY_type(OBJ_ln2nid(name));
  851. return type;
  852. }
  853. #endif
  854. int EVP_PKEY_is_a(const EVP_PKEY *pkey, const char *name)
  855. {
  856. #ifndef FIPS_MODULE
  857. if (pkey->keymgmt == NULL) {
  858. int type = evp_pkey_name2type(name);
  859. return pkey->type == type;
  860. }
  861. #endif
  862. return EVP_KEYMGMT_is_a(pkey->keymgmt, name);
  863. }
  864. void EVP_PKEY_typenames_do_all(const EVP_PKEY *pkey,
  865. void (*fn)(const char *name, void *data),
  866. void *data)
  867. {
  868. if (!evp_pkey_is_typed(pkey))
  869. return;
  870. if (!evp_pkey_is_provided(pkey)) {
  871. const char *name = OBJ_nid2sn(EVP_PKEY_id(pkey));
  872. fn(name, data);
  873. return;
  874. }
  875. EVP_KEYMGMT_names_do_all(pkey->keymgmt, fn, data);
  876. }
  877. int EVP_PKEY_can_sign(const EVP_PKEY *pkey)
  878. {
  879. if (pkey->keymgmt == NULL) {
  880. switch (EVP_PKEY_base_id(pkey)) {
  881. case EVP_PKEY_RSA:
  882. return 1;
  883. #ifndef OPENSSL_NO_DSA
  884. case EVP_PKEY_DSA:
  885. return 1;
  886. #endif
  887. #ifndef OPENSSL_NO_EC
  888. case EVP_PKEY_ED25519:
  889. case EVP_PKEY_ED448:
  890. return 1;
  891. case EVP_PKEY_EC: /* Including SM2 */
  892. return EC_KEY_can_sign(pkey->pkey.ec);
  893. #endif
  894. default:
  895. break;
  896. }
  897. } else {
  898. const OSSL_PROVIDER *prov = EVP_KEYMGMT_provider(pkey->keymgmt);
  899. OSSL_LIB_CTX *libctx = ossl_provider_libctx(prov);
  900. const char *supported_sig =
  901. pkey->keymgmt->query_operation_name != NULL
  902. ? pkey->keymgmt->query_operation_name(OSSL_OP_SIGNATURE)
  903. : evp_first_name(prov, pkey->keymgmt->name_id);
  904. EVP_SIGNATURE *signature = NULL;
  905. signature = EVP_SIGNATURE_fetch(libctx, supported_sig, NULL);
  906. if (signature != NULL) {
  907. EVP_SIGNATURE_free(signature);
  908. return 1;
  909. }
  910. }
  911. return 0;
  912. }
  913. static int print_reset_indent(BIO **out, int pop_f_prefix, long saved_indent)
  914. {
  915. BIO_set_indent(*out, saved_indent);
  916. if (pop_f_prefix) {
  917. BIO *next = BIO_pop(*out);
  918. BIO_free(*out);
  919. *out = next;
  920. }
  921. return 1;
  922. }
  923. static int print_set_indent(BIO **out, int *pop_f_prefix, long *saved_indent,
  924. long indent)
  925. {
  926. *pop_f_prefix = 0;
  927. *saved_indent = 0;
  928. if (indent > 0) {
  929. long i = BIO_get_indent(*out);
  930. *saved_indent = (i < 0 ? 0 : i);
  931. if (BIO_set_indent(*out, indent) <= 0) {
  932. if ((*out = BIO_push(BIO_new(BIO_f_prefix()), *out)) == NULL)
  933. return 0;
  934. *pop_f_prefix = 1;
  935. }
  936. if (BIO_set_indent(*out, indent) <= 0) {
  937. print_reset_indent(out, *pop_f_prefix, *saved_indent);
  938. return 0;
  939. }
  940. }
  941. return 1;
  942. }
  943. static int unsup_alg(BIO *out, const EVP_PKEY *pkey, int indent,
  944. const char *kstr)
  945. {
  946. return BIO_indent(out, indent, 128)
  947. && BIO_printf(out, "%s algorithm \"%s\" unsupported\n",
  948. kstr, OBJ_nid2ln(pkey->type)) > 0;
  949. }
  950. static int print_pkey(const EVP_PKEY *pkey, BIO *out, int indent,
  951. int selection /* For provided encoding */,
  952. const char *propquery /* For provided encoding */,
  953. int (*legacy_print)(BIO *out, const EVP_PKEY *pkey,
  954. int indent, ASN1_PCTX *pctx),
  955. ASN1_PCTX *legacy_pctx /* For legacy print */)
  956. {
  957. int pop_f_prefix;
  958. long saved_indent;
  959. OSSL_ENCODER_CTX *ctx = NULL;
  960. int ret = -2; /* default to unsupported */
  961. if (!print_set_indent(&out, &pop_f_prefix, &saved_indent, indent))
  962. return 0;
  963. ctx = OSSL_ENCODER_CTX_new_by_EVP_PKEY(pkey, selection, "TEXT", NULL,
  964. propquery);
  965. if (OSSL_ENCODER_CTX_get_num_encoders(ctx) != 0)
  966. ret = OSSL_ENCODER_to_bio(ctx, out);
  967. OSSL_ENCODER_CTX_free(ctx);
  968. if (ret != -2)
  969. goto end;
  970. /* legacy fallback */
  971. if (legacy_print != NULL)
  972. ret = legacy_print(out, pkey, 0, legacy_pctx);
  973. else
  974. ret = unsup_alg(out, pkey, 0, "Public Key");
  975. end:
  976. print_reset_indent(&out, pop_f_prefix, saved_indent);
  977. return ret;
  978. }
  979. int EVP_PKEY_print_public(BIO *out, const EVP_PKEY *pkey,
  980. int indent, ASN1_PCTX *pctx)
  981. {
  982. return print_pkey(pkey, out, indent, EVP_PKEY_PUBLIC_KEY, NULL,
  983. (pkey->ameth != NULL ? pkey->ameth->pub_print : NULL),
  984. pctx);
  985. }
  986. int EVP_PKEY_print_private(BIO *out, const EVP_PKEY *pkey,
  987. int indent, ASN1_PCTX *pctx)
  988. {
  989. return print_pkey(pkey, out, indent, EVP_PKEY_KEYPAIR, NULL,
  990. (pkey->ameth != NULL ? pkey->ameth->priv_print : NULL),
  991. pctx);
  992. }
  993. int EVP_PKEY_print_params(BIO *out, const EVP_PKEY *pkey,
  994. int indent, ASN1_PCTX *pctx)
  995. {
  996. return print_pkey(pkey, out, indent, EVP_PKEY_KEY_PARAMETERS, NULL,
  997. (pkey->ameth != NULL ? pkey->ameth->param_print : NULL),
  998. pctx);
  999. }
  1000. static int legacy_asn1_ctrl_to_param(EVP_PKEY *pkey, int op,
  1001. int arg1, void *arg2)
  1002. {
  1003. if (pkey->keymgmt == NULL)
  1004. return 0;
  1005. switch (op) {
  1006. case ASN1_PKEY_CTRL_DEFAULT_MD_NID:
  1007. {
  1008. char mdname[80] = "";
  1009. int rv = EVP_PKEY_get_default_digest_name(pkey, mdname,
  1010. sizeof(mdname));
  1011. if (rv > 0) {
  1012. int nid;
  1013. nid = OBJ_sn2nid(mdname);
  1014. if (nid == NID_undef)
  1015. nid = OBJ_ln2nid(mdname);
  1016. *(int *)arg2 = nid;
  1017. }
  1018. return rv;
  1019. }
  1020. default:
  1021. return -2;
  1022. }
  1023. }
  1024. static int evp_pkey_asn1_ctrl(EVP_PKEY *pkey, int op, int arg1, void *arg2)
  1025. {
  1026. if (pkey->ameth == NULL)
  1027. return legacy_asn1_ctrl_to_param(pkey, op, arg1, arg2);
  1028. if (pkey->ameth->pkey_ctrl == NULL)
  1029. return -2;
  1030. return pkey->ameth->pkey_ctrl(pkey, op, arg1, arg2);
  1031. }
  1032. int EVP_PKEY_get_default_digest_nid(EVP_PKEY *pkey, int *pnid)
  1033. {
  1034. return evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_DEFAULT_MD_NID, 0, pnid);
  1035. }
  1036. int EVP_PKEY_get_default_digest_name(EVP_PKEY *pkey,
  1037. char *mdname, size_t mdname_sz)
  1038. {
  1039. if (pkey->ameth == NULL)
  1040. return evp_keymgmt_util_get_deflt_digest_name(pkey->keymgmt,
  1041. pkey->keydata,
  1042. mdname, mdname_sz);
  1043. {
  1044. int nid = NID_undef;
  1045. int rv = EVP_PKEY_get_default_digest_nid(pkey, &nid);
  1046. const char *name = rv > 0 ? OBJ_nid2sn(nid) : NULL;
  1047. if (rv > 0)
  1048. OPENSSL_strlcpy(mdname, name, mdname_sz);
  1049. return rv;
  1050. }
  1051. }
  1052. int EVP_PKEY_get_group_name(const EVP_PKEY *pkey, char *gname, size_t gname_sz,
  1053. size_t *gname_len)
  1054. {
  1055. if (evp_pkey_is_legacy(pkey)) {
  1056. const char *name = NULL;
  1057. switch (EVP_PKEY_base_id(pkey)) {
  1058. #ifndef OPENSSL_NO_EC
  1059. case EVP_PKEY_EC:
  1060. {
  1061. EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  1062. int nid = EC_GROUP_get_curve_name(EC_KEY_get0_group(ec));
  1063. if (nid != NID_undef)
  1064. name = ec_curve_nid2name(nid);
  1065. }
  1066. break;
  1067. #endif
  1068. #ifndef OPENSSL_NO_DH
  1069. case EVP_PKEY_DH:
  1070. {
  1071. DH *dh = EVP_PKEY_get0_DH(pkey);
  1072. int uid = DH_get_nid(dh);
  1073. if (uid != NID_undef) {
  1074. const DH_NAMED_GROUP *dh_group =
  1075. ossl_ffc_uid_to_dh_named_group(uid);
  1076. name = ossl_ffc_named_group_get_name(dh_group);
  1077. }
  1078. }
  1079. break;
  1080. #endif
  1081. default:
  1082. break;
  1083. }
  1084. if (gname_len != NULL)
  1085. *gname_len = (name == NULL ? 0 : strlen(name));
  1086. if (name != NULL) {
  1087. if (gname != NULL)
  1088. OPENSSL_strlcpy(gname, name, gname_sz);
  1089. return 1;
  1090. }
  1091. } else if (evp_pkey_is_provided(pkey)) {
  1092. if (EVP_PKEY_get_utf8_string_param(pkey, OSSL_PKEY_PARAM_GROUP_NAME,
  1093. gname, gname_sz, gname_len))
  1094. return 1;
  1095. } else {
  1096. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
  1097. return 0;
  1098. }
  1099. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1100. return 0;
  1101. }
  1102. int EVP_PKEY_supports_digest_nid(EVP_PKEY *pkey, int nid)
  1103. {
  1104. int rv, default_nid;
  1105. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SUPPORTS_MD_NID, nid, NULL);
  1106. if (rv == -2) {
  1107. /*
  1108. * If there is a mandatory default digest and this isn't it, then
  1109. * the answer is 'no'.
  1110. */
  1111. rv = EVP_PKEY_get_default_digest_nid(pkey, &default_nid);
  1112. if (rv == 2)
  1113. return (nid == default_nid);
  1114. /* zero is an error from EVP_PKEY_get_default_digest_nid() */
  1115. if (rv == 0)
  1116. return -1;
  1117. }
  1118. return rv;
  1119. }
  1120. int EVP_PKEY_set1_encoded_public_key(EVP_PKEY *pkey, const unsigned char *pub,
  1121. size_t publen)
  1122. {
  1123. if (pkey->ameth == NULL) {
  1124. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1125. if (pkey->keymgmt == NULL || pkey->keydata == NULL)
  1126. return 0;
  1127. params[0] =
  1128. OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1129. (unsigned char *)pub, publen);
  1130. return evp_keymgmt_set_params(pkey->keymgmt, pkey->keydata, params);
  1131. }
  1132. if (publen > INT_MAX)
  1133. return 0;
  1134. /* Historically this function was EVP_PKEY_set1_tls_encodedpoint */
  1135. if (evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_SET1_TLS_ENCPT, publen,
  1136. (void *)pub) <= 0)
  1137. return 0;
  1138. return 1;
  1139. }
  1140. size_t EVP_PKEY_get1_encoded_public_key(EVP_PKEY *pkey, unsigned char **ppub)
  1141. {
  1142. int rv;
  1143. if (pkey->ameth == NULL) {
  1144. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  1145. if (pkey->keymgmt == NULL || pkey->keydata == NULL)
  1146. return 0;
  1147. params[0] =
  1148. OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1149. NULL, 0);
  1150. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
  1151. return 0;
  1152. *ppub = OPENSSL_malloc(params[0].return_size);
  1153. if (*ppub == NULL)
  1154. return 0;
  1155. params[0] =
  1156. OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY,
  1157. *ppub, params[0].return_size);
  1158. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
  1159. return 0;
  1160. return params[0].return_size;
  1161. }
  1162. rv = evp_pkey_asn1_ctrl(pkey, ASN1_PKEY_CTRL_GET1_TLS_ENCPT, 0, ppub);
  1163. if (rv <= 0)
  1164. return 0;
  1165. return rv;
  1166. }
  1167. #endif /* FIPS_MODULE */
  1168. /*- All methods below can also be used in FIPS_MODULE */
  1169. /*
  1170. * This reset function must be used very carefully, as it literally throws
  1171. * away everything in an EVP_PKEY without freeing them, and may cause leaks
  1172. * of memory, what have you.
  1173. * The only reason we have this is to have the same code for EVP_PKEY_new()
  1174. * and evp_pkey_downgrade().
  1175. */
  1176. static int evp_pkey_reset_unlocked(EVP_PKEY *pk)
  1177. {
  1178. if (pk == NULL)
  1179. return 0;
  1180. if (pk->lock != NULL) {
  1181. const size_t offset = (unsigned char *)&pk->lock - (unsigned char *)pk;
  1182. memset(pk, 0, offset);
  1183. memset((unsigned char *)pk + offset + sizeof(pk->lock),
  1184. 0,
  1185. sizeof(*pk) - offset - sizeof(pk->lock));
  1186. }
  1187. /* EVP_PKEY_new uses zalloc so no need to call memset if pk->lock is NULL */
  1188. pk->type = EVP_PKEY_NONE;
  1189. pk->save_type = EVP_PKEY_NONE;
  1190. pk->references = 1;
  1191. pk->save_parameters = 1;
  1192. return 1;
  1193. }
  1194. EVP_PKEY *EVP_PKEY_new(void)
  1195. {
  1196. EVP_PKEY *ret = OPENSSL_zalloc(sizeof(*ret));
  1197. if (ret == NULL) {
  1198. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1199. return NULL;
  1200. }
  1201. if (!evp_pkey_reset_unlocked(ret))
  1202. goto err;
  1203. ret->lock = CRYPTO_THREAD_lock_new();
  1204. if (ret->lock == NULL) {
  1205. EVPerr(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1206. goto err;
  1207. }
  1208. #ifndef FIPS_MODULE
  1209. if (!CRYPTO_new_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, ret, &ret->ex_data)) {
  1210. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1211. goto err;
  1212. }
  1213. #endif
  1214. return ret;
  1215. err:
  1216. CRYPTO_THREAD_lock_free(ret->lock);
  1217. OPENSSL_free(ret);
  1218. return NULL;
  1219. }
  1220. /*
  1221. * Setup a public key management method.
  1222. *
  1223. * For legacy keys, either |type| or |str| is expected to have the type
  1224. * information. In this case, the setup consists of finding an ASN1 method
  1225. * and potentially an ENGINE, and setting those fields in |pkey|.
  1226. *
  1227. * For provider side keys, |keymgmt| is expected to be non-NULL. In this
  1228. * case, the setup consists of setting the |keymgmt| field in |pkey|.
  1229. *
  1230. * If pkey is NULL just return 1 or 0 if the key management method exists.
  1231. */
  1232. static int pkey_set_type(EVP_PKEY *pkey, ENGINE *e, int type, const char *str,
  1233. int len, EVP_KEYMGMT *keymgmt)
  1234. {
  1235. #ifndef FIPS_MODULE
  1236. const EVP_PKEY_ASN1_METHOD *ameth = NULL;
  1237. ENGINE **eptr = (e == NULL) ? &e : NULL;
  1238. #endif
  1239. /*
  1240. * The setups can't set both legacy and provider side methods.
  1241. * It is forbidden
  1242. */
  1243. if (!ossl_assert(type == EVP_PKEY_NONE || keymgmt == NULL)
  1244. || !ossl_assert(e == NULL || keymgmt == NULL)) {
  1245. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1246. return 0;
  1247. }
  1248. if (pkey != NULL) {
  1249. int free_it = 0;
  1250. #ifndef FIPS_MODULE
  1251. free_it = free_it || pkey->pkey.ptr != NULL;
  1252. #endif
  1253. free_it = free_it || pkey->keydata != NULL;
  1254. if (free_it)
  1255. evp_pkey_free_it(pkey);
  1256. #ifndef FIPS_MODULE
  1257. /*
  1258. * If key type matches and a method exists then this lookup has
  1259. * succeeded once so just indicate success.
  1260. */
  1261. if (pkey->type != EVP_PKEY_NONE
  1262. && type == pkey->save_type
  1263. && pkey->ameth != NULL)
  1264. return 1;
  1265. # ifndef OPENSSL_NO_ENGINE
  1266. /* If we have ENGINEs release them */
  1267. ENGINE_finish(pkey->engine);
  1268. pkey->engine = NULL;
  1269. ENGINE_finish(pkey->pmeth_engine);
  1270. pkey->pmeth_engine = NULL;
  1271. # endif
  1272. #endif
  1273. }
  1274. #ifndef FIPS_MODULE
  1275. if (str != NULL)
  1276. ameth = EVP_PKEY_asn1_find_str(eptr, str, len);
  1277. else if (type != EVP_PKEY_NONE)
  1278. ameth = EVP_PKEY_asn1_find(eptr, type);
  1279. # ifndef OPENSSL_NO_ENGINE
  1280. if (pkey == NULL && eptr != NULL)
  1281. ENGINE_finish(e);
  1282. # endif
  1283. #endif
  1284. {
  1285. int check = 1;
  1286. #ifndef FIPS_MODULE
  1287. check = check && ameth == NULL;
  1288. #endif
  1289. check = check && keymgmt == NULL;
  1290. if (check) {
  1291. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_ALGORITHM);
  1292. return 0;
  1293. }
  1294. }
  1295. if (pkey != NULL) {
  1296. if (keymgmt != NULL && !EVP_KEYMGMT_up_ref(keymgmt)) {
  1297. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1298. return 0;
  1299. }
  1300. pkey->keymgmt = keymgmt;
  1301. pkey->save_type = type;
  1302. pkey->type = type;
  1303. #ifndef FIPS_MODULE
  1304. /*
  1305. * If the internal "origin" key is provider side, don't save |ameth|.
  1306. * The main reason is that |ameth| is one factor to detect that the
  1307. * internal "origin" key is a legacy one.
  1308. */
  1309. if (keymgmt == NULL)
  1310. pkey->ameth = ameth;
  1311. pkey->engine = e;
  1312. /*
  1313. * The EVP_PKEY_ASN1_METHOD |pkey_id| retains its legacy key purpose
  1314. * for any key type that has a legacy implementation, regardless of
  1315. * if the internal key is a legacy or a provider side one. When
  1316. * there is no legacy implementation for the key, the type becomes
  1317. * EVP_PKEY_KEYMGMT, which indicates that one should be cautious
  1318. * with functions that expect legacy internal keys.
  1319. */
  1320. if (ameth != NULL)
  1321. pkey->type = ameth->pkey_id;
  1322. else
  1323. pkey->type = EVP_PKEY_KEYMGMT;
  1324. #endif
  1325. }
  1326. return 1;
  1327. }
  1328. #ifndef FIPS_MODULE
  1329. static void find_ameth(const char *name, void *data)
  1330. {
  1331. const char **str = data;
  1332. /*
  1333. * The error messages from pkey_set_type() are uninteresting here,
  1334. * and misleading.
  1335. */
  1336. ERR_set_mark();
  1337. if (pkey_set_type(NULL, NULL, EVP_PKEY_NONE, name, strlen(name),
  1338. NULL)) {
  1339. if (str[0] == NULL)
  1340. str[0] = name;
  1341. else if (str[1] == NULL)
  1342. str[1] = name;
  1343. }
  1344. ERR_pop_to_mark();
  1345. }
  1346. #endif
  1347. int EVP_PKEY_set_type_by_keymgmt(EVP_PKEY *pkey, EVP_KEYMGMT *keymgmt)
  1348. {
  1349. #ifndef FIPS_MODULE
  1350. # define EVP_PKEY_TYPE_STR str[0]
  1351. # define EVP_PKEY_TYPE_STRLEN (str[0] == NULL ? -1 : (int)strlen(str[0]))
  1352. /*
  1353. * Find at most two strings that have an associated EVP_PKEY_ASN1_METHOD
  1354. * Ideally, only one should be found. If two (or more) are found, the
  1355. * match is ambiguous. This should never happen, but...
  1356. */
  1357. const char *str[2] = { NULL, NULL };
  1358. EVP_KEYMGMT_names_do_all(keymgmt, find_ameth, &str);
  1359. if (str[1] != NULL) {
  1360. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  1361. return 0;
  1362. }
  1363. #else
  1364. # define EVP_PKEY_TYPE_STR NULL
  1365. # define EVP_PKEY_TYPE_STRLEN -1
  1366. #endif
  1367. return pkey_set_type(pkey, NULL, EVP_PKEY_NONE,
  1368. EVP_PKEY_TYPE_STR, EVP_PKEY_TYPE_STRLEN,
  1369. keymgmt);
  1370. #undef EVP_PKEY_TYPE_STR
  1371. #undef EVP_PKEY_TYPE_STRLEN
  1372. }
  1373. int EVP_PKEY_up_ref(EVP_PKEY *pkey)
  1374. {
  1375. int i;
  1376. if (CRYPTO_UP_REF(&pkey->references, &i, pkey->lock) <= 0)
  1377. return 0;
  1378. REF_PRINT_COUNT("EVP_PKEY", pkey);
  1379. REF_ASSERT_ISNT(i < 2);
  1380. return ((i > 1) ? 1 : 0);
  1381. }
  1382. #ifndef FIPS_MODULE
  1383. void evp_pkey_free_legacy(EVP_PKEY *x)
  1384. {
  1385. if (x->ameth != NULL) {
  1386. if (x->ameth->pkey_free != NULL)
  1387. x->ameth->pkey_free(x);
  1388. x->pkey.ptr = NULL;
  1389. }
  1390. # ifndef OPENSSL_NO_ENGINE
  1391. ENGINE_finish(x->engine);
  1392. x->engine = NULL;
  1393. ENGINE_finish(x->pmeth_engine);
  1394. x->pmeth_engine = NULL;
  1395. # endif
  1396. }
  1397. #endif /* FIPS_MODULE */
  1398. static void evp_pkey_free_it(EVP_PKEY *x)
  1399. {
  1400. /* internal function; x is never NULL */
  1401. evp_keymgmt_util_clear_operation_cache(x);
  1402. #ifndef FIPS_MODULE
  1403. evp_pkey_free_legacy(x);
  1404. #endif
  1405. if (x->keymgmt != NULL) {
  1406. evp_keymgmt_freedata(x->keymgmt, x->keydata);
  1407. EVP_KEYMGMT_free(x->keymgmt);
  1408. x->keymgmt = NULL;
  1409. x->keydata = NULL;
  1410. }
  1411. x->type = EVP_PKEY_NONE;
  1412. }
  1413. void EVP_PKEY_free(EVP_PKEY *x)
  1414. {
  1415. int i;
  1416. if (x == NULL)
  1417. return;
  1418. CRYPTO_DOWN_REF(&x->references, &i, x->lock);
  1419. REF_PRINT_COUNT("EVP_PKEY", x);
  1420. if (i > 0)
  1421. return;
  1422. REF_ASSERT_ISNT(i < 0);
  1423. evp_pkey_free_it(x);
  1424. #ifndef FIPS_MODULE
  1425. CRYPTO_free_ex_data(CRYPTO_EX_INDEX_EVP_PKEY, x, &x->ex_data);
  1426. #endif
  1427. CRYPTO_THREAD_lock_free(x->lock);
  1428. #ifndef FIPS_MODULE
  1429. sk_X509_ATTRIBUTE_pop_free(x->attributes, X509_ATTRIBUTE_free);
  1430. #endif
  1431. OPENSSL_free(x);
  1432. }
  1433. int EVP_PKEY_size(const EVP_PKEY *pkey)
  1434. {
  1435. int size = 0;
  1436. if (pkey != NULL) {
  1437. size = pkey->cache.size;
  1438. #ifndef FIPS_MODULE
  1439. if (pkey->ameth != NULL && pkey->ameth->pkey_size != NULL)
  1440. size = pkey->ameth->pkey_size(pkey);
  1441. #endif
  1442. }
  1443. return size < 0 ? 0 : size;
  1444. }
  1445. void *evp_pkey_export_to_provider(EVP_PKEY *pk, OSSL_LIB_CTX *libctx,
  1446. EVP_KEYMGMT **keymgmt,
  1447. const char *propquery)
  1448. {
  1449. EVP_KEYMGMT *allocated_keymgmt = NULL;
  1450. EVP_KEYMGMT *tmp_keymgmt = NULL;
  1451. void *keydata = NULL;
  1452. int check;
  1453. if (pk == NULL)
  1454. return NULL;
  1455. /* No key data => nothing to export */
  1456. check = 1;
  1457. #ifndef FIPS_MODULE
  1458. check = check && pk->pkey.ptr == NULL;
  1459. #endif
  1460. check = check && pk->keydata == NULL;
  1461. if (check)
  1462. return NULL;
  1463. #ifndef FIPS_MODULE
  1464. if (pk->pkey.ptr != NULL) {
  1465. /*
  1466. * If the legacy key doesn't have an dirty counter or export function,
  1467. * give up
  1468. */
  1469. if (pk->ameth->dirty_cnt == NULL || pk->ameth->export_to == NULL)
  1470. return NULL;
  1471. }
  1472. #endif
  1473. if (keymgmt != NULL) {
  1474. tmp_keymgmt = *keymgmt;
  1475. *keymgmt = NULL;
  1476. }
  1477. /*
  1478. * If no keymgmt was given or found, get a default keymgmt. We do so by
  1479. * letting EVP_PKEY_CTX_new_from_pkey() do it for us, then we steal it.
  1480. */
  1481. if (tmp_keymgmt == NULL) {
  1482. EVP_PKEY_CTX *ctx = EVP_PKEY_CTX_new_from_pkey(libctx, pk, propquery);
  1483. tmp_keymgmt = ctx->keymgmt;
  1484. ctx->keymgmt = NULL;
  1485. EVP_PKEY_CTX_free(ctx);
  1486. }
  1487. /* If there's still no keymgmt to be had, give up */
  1488. if (tmp_keymgmt == NULL)
  1489. goto end;
  1490. #ifndef FIPS_MODULE
  1491. if (pk->pkey.ptr != NULL) {
  1492. size_t i = 0;
  1493. /*
  1494. * If the legacy "origin" hasn't changed since last time, we try
  1495. * to find our keymgmt in the operation cache. If it has changed,
  1496. * |i| remains zero, and we will clear the cache further down.
  1497. */
  1498. if (pk->ameth->dirty_cnt(pk) == pk->dirty_cnt_copy) {
  1499. i = evp_keymgmt_util_find_operation_cache_index(pk, tmp_keymgmt);
  1500. /*
  1501. * If |tmp_keymgmt| is present in the operation cache, it means
  1502. * that export doesn't need to be redone. In that case, we take
  1503. * token copies of the cached pointers, to have token success
  1504. * values to return.
  1505. */
  1506. if (i < OSSL_NELEM(pk->operation_cache)
  1507. && pk->operation_cache[i].keymgmt != NULL) {
  1508. keydata = pk->operation_cache[i].keydata;
  1509. goto end;
  1510. }
  1511. }
  1512. /*
  1513. * TODO(3.0) Right now, we assume we have ample space. We will have
  1514. * to think about a cache aging scheme, though, if |i| indexes outside
  1515. * the array.
  1516. */
  1517. if (!ossl_assert(i < OSSL_NELEM(pk->operation_cache)))
  1518. goto end;
  1519. /* Make sure that the keymgmt key type matches the legacy NID */
  1520. if (!ossl_assert(EVP_KEYMGMT_is_a(tmp_keymgmt, OBJ_nid2sn(pk->type))))
  1521. goto end;
  1522. if ((keydata = evp_keymgmt_newdata(tmp_keymgmt)) == NULL)
  1523. goto end;
  1524. if (!pk->ameth->export_to(pk, keydata, tmp_keymgmt, libctx, propquery)) {
  1525. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1526. keydata = NULL;
  1527. goto end;
  1528. }
  1529. /*
  1530. * If the dirty counter changed since last time, then clear the
  1531. * operation cache. In that case, we know that |i| is zero. Just
  1532. * in case this is a re-export, we increment then decrement the
  1533. * keymgmt reference counter.
  1534. */
  1535. if (!EVP_KEYMGMT_up_ref(tmp_keymgmt)) { /* refcnt++ */
  1536. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1537. keydata = NULL;
  1538. goto end;
  1539. }
  1540. if (pk->ameth->dirty_cnt(pk) != pk->dirty_cnt_copy)
  1541. evp_keymgmt_util_clear_operation_cache(pk);
  1542. EVP_KEYMGMT_free(tmp_keymgmt); /* refcnt-- */
  1543. /* Add the new export to the operation cache */
  1544. if (!evp_keymgmt_util_cache_keydata(pk, i, tmp_keymgmt, keydata)) {
  1545. evp_keymgmt_freedata(tmp_keymgmt, keydata);
  1546. keydata = NULL;
  1547. goto end;
  1548. }
  1549. /* Synchronize the dirty count */
  1550. pk->dirty_cnt_copy = pk->ameth->dirty_cnt(pk);
  1551. goto end;
  1552. }
  1553. #endif /* FIPS_MODULE */
  1554. keydata = evp_keymgmt_util_export_to_provider(pk, tmp_keymgmt);
  1555. end:
  1556. /*
  1557. * If nothing was exported, |tmp_keymgmt| might point at a freed
  1558. * EVP_KEYMGMT, so we clear it to be safe. It shouldn't be useful for
  1559. * the caller either way in that case.
  1560. */
  1561. if (keydata == NULL)
  1562. tmp_keymgmt = NULL;
  1563. if (keymgmt != NULL)
  1564. *keymgmt = tmp_keymgmt;
  1565. EVP_KEYMGMT_free(allocated_keymgmt);
  1566. return keydata;
  1567. }
  1568. #ifndef FIPS_MODULE
  1569. int evp_pkey_copy_downgraded(EVP_PKEY **dest, const EVP_PKEY *src)
  1570. {
  1571. if (!ossl_assert(dest != NULL))
  1572. return 0;
  1573. if (evp_pkey_is_assigned(src) && evp_pkey_is_provided(src)) {
  1574. EVP_KEYMGMT *keymgmt = src->keymgmt;
  1575. void *keydata = src->keydata;
  1576. int type = src->type;
  1577. const char *keytype = NULL;
  1578. keytype = evp_first_name(EVP_KEYMGMT_provider(keymgmt),
  1579. keymgmt->name_id);
  1580. /*
  1581. * If the type is EVP_PKEY_NONE, then we have a problem somewhere
  1582. * else in our code. If it's not one of the well known EVP_PKEY_xxx
  1583. * values, it should at least be EVP_PKEY_KEYMGMT at this point.
  1584. * TODO(3.0) remove this check when we're confident that the rest
  1585. * of the code treats this correctly.
  1586. */
  1587. if (!ossl_assert(type != EVP_PKEY_NONE)) {
  1588. ERR_raise_data(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR,
  1589. "keymgmt key type = %s but legacy type = EVP_PKEY_NONE",
  1590. keytype);
  1591. return 0;
  1592. }
  1593. /* Prefer the legacy key type name for error reporting */
  1594. if (type != EVP_PKEY_KEYMGMT)
  1595. keytype = OBJ_nid2sn(type);
  1596. /* Make sure we have a clean slate to copy into */
  1597. if (*dest == NULL)
  1598. *dest = EVP_PKEY_new();
  1599. else
  1600. evp_pkey_free_it(*dest);
  1601. if (EVP_PKEY_set_type(*dest, type)) {
  1602. /* If the key is typed but empty, we're done */
  1603. if (keydata == NULL)
  1604. return 1;
  1605. if ((*dest)->ameth->import_from == NULL) {
  1606. ERR_raise_data(ERR_LIB_EVP, EVP_R_NO_IMPORT_FUNCTION,
  1607. "key type = %s", keytype);
  1608. } else {
  1609. /*
  1610. * We perform the export in the same libctx as the keymgmt
  1611. * that we are using.
  1612. */
  1613. OSSL_LIB_CTX *libctx =
  1614. ossl_provider_libctx(keymgmt->prov);
  1615. EVP_PKEY_CTX *pctx =
  1616. EVP_PKEY_CTX_new_from_pkey(libctx, *dest, NULL);
  1617. if (pctx == NULL)
  1618. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  1619. if (pctx != NULL
  1620. && evp_keymgmt_export(keymgmt, keydata,
  1621. OSSL_KEYMGMT_SELECT_ALL,
  1622. (*dest)->ameth->import_from,
  1623. pctx)) {
  1624. /* Synchronize the dirty count */
  1625. (*dest)->dirty_cnt_copy = (*dest)->ameth->dirty_cnt(*dest);
  1626. EVP_PKEY_CTX_free(pctx);
  1627. return 1;
  1628. }
  1629. EVP_PKEY_CTX_free(pctx);
  1630. }
  1631. ERR_raise_data(ERR_LIB_EVP, EVP_R_KEYMGMT_EXPORT_FAILURE,
  1632. "key type = %s", keytype);
  1633. }
  1634. }
  1635. return 0;
  1636. }
  1637. int evp_pkey_downgrade(EVP_PKEY *pk)
  1638. {
  1639. EVP_PKEY tmp_copy; /* Stack allocated! */
  1640. int rv = 0;
  1641. if (!ossl_assert(pk != NULL))
  1642. return 0;
  1643. /*
  1644. * Throughout this whole function, we must ensure that we lock / unlock
  1645. * the exact same lock. Note that we do pass it around a bit.
  1646. */
  1647. if (!CRYPTO_THREAD_write_lock(pk->lock))
  1648. return 0;
  1649. /* If this isn't an assigned provider side key, we're done */
  1650. if (!evp_pkey_is_assigned(pk) || !evp_pkey_is_provided(pk)) {
  1651. rv = 1;
  1652. goto end;
  1653. }
  1654. /*
  1655. * To be able to downgrade, we steal the contents of |pk|, then reset
  1656. * it, and finally try to make it a downgraded copy. If any of that
  1657. * fails, we restore the copied contents into |pk|.
  1658. */
  1659. tmp_copy = *pk; /* |tmp_copy| now owns THE lock */
  1660. if (evp_pkey_reset_unlocked(pk)
  1661. && evp_pkey_copy_downgraded(&pk, &tmp_copy)) {
  1662. /* Restore the common attributes, then empty |tmp_copy| */
  1663. pk->references = tmp_copy.references;
  1664. pk->attributes = tmp_copy.attributes;
  1665. pk->save_parameters = tmp_copy.save_parameters;
  1666. pk->ex_data = tmp_copy.ex_data;
  1667. /* Ensure that stuff we've copied won't be freed */
  1668. tmp_copy.lock = NULL;
  1669. tmp_copy.attributes = NULL;
  1670. memset(&tmp_copy.ex_data, 0, sizeof(tmp_copy.ex_data));
  1671. /*
  1672. * Save the provider side data in the operation cache, so they'll
  1673. * find it again. |pk| is new, so it's safe to assume slot zero
  1674. * is free.
  1675. * Note that evp_keymgmt_util_cache_keydata() increments keymgmt's
  1676. * reference count, so we need to decrement it, or there will be a
  1677. * leak.
  1678. */
  1679. evp_keymgmt_util_cache_keydata(pk, 0, tmp_copy.keymgmt,
  1680. tmp_copy.keydata);
  1681. EVP_KEYMGMT_free(tmp_copy.keymgmt);
  1682. /*
  1683. * Clear keymgmt and keydata from |tmp_copy|, or they'll get
  1684. * inadvertently freed.
  1685. */
  1686. tmp_copy.keymgmt = NULL;
  1687. tmp_copy.keydata = NULL;
  1688. evp_pkey_free_it(&tmp_copy);
  1689. rv = 1;
  1690. } else {
  1691. /* Restore the original key */
  1692. *pk = tmp_copy;
  1693. }
  1694. end:
  1695. if (!CRYPTO_THREAD_unlock(pk->lock))
  1696. return 0;
  1697. return rv;
  1698. }
  1699. #endif /* FIPS_MODULE */
  1700. const OSSL_PARAM *EVP_PKEY_gettable_params(const EVP_PKEY *pkey)
  1701. {
  1702. if (pkey == NULL
  1703. || pkey->keymgmt == NULL
  1704. || pkey->keydata == NULL)
  1705. return 0;
  1706. return EVP_KEYMGMT_gettable_params(pkey->keymgmt);
  1707. }
  1708. int EVP_PKEY_get_bn_param(const EVP_PKEY *pkey, const char *key_name,
  1709. BIGNUM **bn)
  1710. {
  1711. int ret = 0;
  1712. OSSL_PARAM params[2];
  1713. unsigned char buffer[2048];
  1714. unsigned char *buf = NULL;
  1715. size_t buf_sz = 0;
  1716. if (pkey == NULL
  1717. || pkey->keymgmt == NULL
  1718. || pkey->keydata == NULL
  1719. || key_name == NULL
  1720. || bn == NULL)
  1721. return 0;
  1722. memset(buffer, 0, sizeof(buffer));
  1723. params[0] = OSSL_PARAM_construct_BN(key_name, buffer, sizeof(buffer));
  1724. params[1] = OSSL_PARAM_construct_end();
  1725. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)) {
  1726. if (!OSSL_PARAM_modified(params) || params[0].return_size == 0)
  1727. return 0;
  1728. buf_sz = params[0].return_size;
  1729. /*
  1730. * If it failed because the buffer was too small then allocate the
  1731. * required buffer size and retry.
  1732. */
  1733. buf = OPENSSL_zalloc(buf_sz);
  1734. if (buf == NULL)
  1735. return 0;
  1736. params[0].data = buf;
  1737. params[0].data_size = buf_sz;
  1738. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params))
  1739. goto err;
  1740. }
  1741. /* Fail if the param was not found */
  1742. if (!OSSL_PARAM_modified(params))
  1743. goto err;
  1744. ret = OSSL_PARAM_get_BN(params, bn);
  1745. err:
  1746. OPENSSL_free(buf);
  1747. return ret;
  1748. }
  1749. int EVP_PKEY_get_octet_string_param(const EVP_PKEY *pkey, const char *key_name,
  1750. unsigned char *buf, size_t max_buf_sz,
  1751. size_t *out_sz)
  1752. {
  1753. OSSL_PARAM params[2];
  1754. if (pkey == NULL
  1755. || pkey->keymgmt == NULL
  1756. || pkey->keydata == NULL
  1757. || key_name == NULL)
  1758. return 0;
  1759. params[0] = OSSL_PARAM_construct_octet_string(key_name, buf, max_buf_sz);
  1760. params[1] = OSSL_PARAM_construct_end();
  1761. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
  1762. || !OSSL_PARAM_modified(params))
  1763. return 0;
  1764. if (out_sz != NULL)
  1765. *out_sz = params[0].return_size;
  1766. return 1;
  1767. }
  1768. int EVP_PKEY_get_utf8_string_param(const EVP_PKEY *pkey, const char *key_name,
  1769. char *str, size_t max_buf_sz,
  1770. size_t *out_sz)
  1771. {
  1772. OSSL_PARAM params[2];
  1773. if (pkey == NULL
  1774. || pkey->keymgmt == NULL
  1775. || pkey->keydata == NULL
  1776. || key_name == NULL)
  1777. return 0;
  1778. params[0] = OSSL_PARAM_construct_utf8_string(key_name, str, max_buf_sz);
  1779. params[1] = OSSL_PARAM_construct_end();
  1780. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
  1781. || !OSSL_PARAM_modified(params))
  1782. return 0;
  1783. if (out_sz != NULL)
  1784. *out_sz = params[0].return_size;
  1785. return 1;
  1786. }
  1787. int EVP_PKEY_get_int_param(const EVP_PKEY *pkey, const char *key_name,
  1788. int *out)
  1789. {
  1790. OSSL_PARAM params[2];
  1791. if (pkey == NULL
  1792. || pkey->keymgmt == NULL
  1793. || pkey->keydata == NULL
  1794. || key_name == NULL)
  1795. return 0;
  1796. params[0] = OSSL_PARAM_construct_int(key_name, out);
  1797. params[1] = OSSL_PARAM_construct_end();
  1798. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
  1799. || !OSSL_PARAM_modified(params))
  1800. return 0;
  1801. return 1;
  1802. }
  1803. int EVP_PKEY_get_size_t_param(const EVP_PKEY *pkey, const char *key_name,
  1804. size_t *out)
  1805. {
  1806. OSSL_PARAM params[2];
  1807. if (pkey == NULL
  1808. || pkey->keymgmt == NULL
  1809. || pkey->keydata == NULL
  1810. || key_name == NULL)
  1811. return 0;
  1812. params[0] = OSSL_PARAM_construct_size_t(key_name, out);
  1813. params[1] = OSSL_PARAM_construct_end();
  1814. if (!evp_keymgmt_get_params(pkey->keymgmt, pkey->keydata, params)
  1815. || !OSSL_PARAM_modified(params))
  1816. return 0;
  1817. return 1;
  1818. }