p_lib.c 70 KB

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