p_lib.c 56 KB

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