p_lib.c 70 KB

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