ctrl_params_translate.c 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869
  1. /*
  2. * Copyright 2021-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. * Some ctrls depend on deprecated functionality. We trust that this is
  11. * functionality that remains internally even when 'no-deprecated' is
  12. * configured. When we drop #legacy EVP_PKEYs, this source should be
  13. * possible to drop as well.
  14. */
  15. #include "internal/deprecated.h"
  16. #include <string.h>
  17. /* The following includes get us all the EVP_PKEY_CTRL macros */
  18. #include <openssl/dh.h>
  19. #include <openssl/dsa.h>
  20. #include <openssl/ec.h>
  21. #include <openssl/rsa.h>
  22. #include <openssl/kdf.h>
  23. /* This include gets us all the OSSL_PARAM key string macros */
  24. #include <openssl/core_names.h>
  25. #include <openssl/err.h>
  26. #include <openssl/evperr.h>
  27. #include <openssl/params.h>
  28. #include "internal/nelem.h"
  29. #include "internal/cryptlib.h"
  30. #include "internal/ffc.h"
  31. #include "crypto/evp.h"
  32. #include "crypto/dh.h"
  33. #include "crypto/ec.h"
  34. struct translation_ctx_st; /* Forwarding */
  35. struct translation_st; /* Forwarding */
  36. /*
  37. * The fixup_args functions are called with the following parameters:
  38. *
  39. * |state| The state we're called in, explained further at the
  40. * end of this comment.
  41. * |translation| The translation item, to be pilfered for data as
  42. * necessary.
  43. * |ctx| The translation context, which contains copies of
  44. * the following arguments, applicable according to
  45. * the caller. All of the attributes in this context
  46. * may be freely modified by the fixup_args function.
  47. * For cleanup, call cleanup_translation_ctx().
  48. *
  49. * The |state| tells the fixup_args function something about the caller and
  50. * what they may expect:
  51. *
  52. * PKEY The fixup_args function has been called
  53. * from an EVP_PKEY payload getter / setter,
  54. * and is fully responsible for getting or
  55. * setting the requested data. With this
  56. * state, the fixup_args function is expected
  57. * to use or modify |*params|, depending on
  58. * |action_type|.
  59. *
  60. * PRE_CTRL_TO_PARAMS The fixup_args function has been called
  61. * POST_CTRL_TO_PARAMS from EVP_PKEY_CTX_ctrl(), to help with
  62. * translating the ctrl data to an OSSL_PARAM
  63. * element or back. The calling sequence is
  64. * as follows:
  65. *
  66. * 1. fixup_args(PRE_CTRL_TO_PARAMS, ...)
  67. * 2. EVP_PKEY_CTX_set_params() or
  68. * EVP_PKEY_CTX_get_params()
  69. * 3. fixup_args(POST_CTRL_TO_PARAMS, ...)
  70. *
  71. * With the PRE_CTRL_TO_PARAMS state, the
  72. * fixup_args function is expected to modify
  73. * the passed |*params| in whatever way
  74. * necessary, when |action_type == SET|.
  75. * With the POST_CTRL_TO_PARAMS state, the
  76. * fixup_args function is expected to modify
  77. * the passed |p2| in whatever way necessary,
  78. * when |action_type == GET|.
  79. *
  80. * The return value from the fixup_args call
  81. * with the POST_CTRL_TO_PARAMS state becomes
  82. * the return value back to EVP_PKEY_CTX_ctrl().
  83. *
  84. * CLEANUP_CTRL_TO_PARAMS The cleanup_args functions has been called
  85. * from EVP_PKEY_CTX_ctrl(), to clean up what
  86. * the fixup_args function has done, if needed.
  87. *
  88. *
  89. * PRE_CTRL_STR_TO_PARAMS The fixup_args function has been called
  90. * POST_CTRL_STR_TO_PARAMS from EVP_PKEY_CTX_ctrl_str(), to help with
  91. * translating the ctrl_str data to an
  92. * OSSL_PARAM element or back. The calling
  93. * sequence is as follows:
  94. *
  95. * 1. fixup_args(PRE_CTRL_STR_TO_PARAMS, ...)
  96. * 2. EVP_PKEY_CTX_set_params() or
  97. * EVP_PKEY_CTX_get_params()
  98. * 3. fixup_args(POST_CTRL_STR_TO_PARAMS, ...)
  99. *
  100. * With the PRE_CTRL_STR_TO_PARAMS state,
  101. * the fixup_args function is expected to
  102. * modify the passed |*params| in whatever
  103. * way necessary, when |action_type == SET|.
  104. * With the POST_CTRL_STR_TO_PARAMS state,
  105. * the fixup_args function is only expected
  106. * to return a value.
  107. *
  108. * CLEANUP_CTRL_STR_TO_PARAMS The cleanup_args functions has been called
  109. * from EVP_PKEY_CTX_ctrl_str(), to clean up
  110. * what the fixup_args function has done, if
  111. * needed.
  112. *
  113. * PRE_PARAMS_TO_CTRL The fixup_args function has been called
  114. * POST_PARAMS_TO_CTRL from EVP_PKEY_CTX_get_params() or
  115. * EVP_PKEY_CTX_set_params(), to help with
  116. * translating the OSSL_PARAM data to the
  117. * corresponding EVP_PKEY_CTX_ctrl() arguments
  118. * or the other way around. The calling
  119. * sequence is as follows:
  120. *
  121. * 1. fixup_args(PRE_PARAMS_TO_CTRL, ...)
  122. * 2. EVP_PKEY_CTX_ctrl()
  123. * 3. fixup_args(POST_PARAMS_TO_CTRL, ...)
  124. *
  125. * With the PRE_PARAMS_TO_CTRL state, the
  126. * fixup_args function is expected to modify
  127. * the passed |p1| and |p2| in whatever way
  128. * necessary, when |action_type == SET|.
  129. * With the POST_PARAMS_TO_CTRL state, the
  130. * fixup_args function is expected to
  131. * modify the passed |*params| in whatever
  132. * way necessary, when |action_type == GET|.
  133. *
  134. * CLEANUP_PARAMS_TO_CTRL The cleanup_args functions has been called
  135. * from EVP_PKEY_CTX_get_params() or
  136. * EVP_PKEY_CTX_set_params(), to clean up what
  137. * the fixup_args function has done, if needed.
  138. */
  139. enum state {
  140. PKEY,
  141. PRE_CTRL_TO_PARAMS, POST_CTRL_TO_PARAMS, CLEANUP_CTRL_TO_PARAMS,
  142. PRE_CTRL_STR_TO_PARAMS, POST_CTRL_STR_TO_PARAMS, CLEANUP_CTRL_STR_TO_PARAMS,
  143. PRE_PARAMS_TO_CTRL, POST_PARAMS_TO_CTRL, CLEANUP_PARAMS_TO_CTRL
  144. };
  145. enum action {
  146. NONE = 0, GET = 1, SET = 2
  147. };
  148. typedef int fixup_args_fn(enum state state,
  149. const struct translation_st *translation,
  150. struct translation_ctx_st *ctx);
  151. typedef int cleanup_args_fn(enum state state,
  152. const struct translation_st *translation,
  153. struct translation_ctx_st *ctx);
  154. struct translation_ctx_st {
  155. /*
  156. * The EVP_PKEY_CTX, for calls on that structure, to be pilfered for data
  157. * as necessary.
  158. */
  159. EVP_PKEY_CTX *pctx;
  160. /*
  161. * The action type (GET or SET). This may be 0 in some cases, and should
  162. * be modified by the fixup_args function in the PRE states. It should
  163. * otherwise remain untouched once set.
  164. */
  165. enum action action_type;
  166. /*
  167. * For ctrl to params translation, the actual ctrl command number used.
  168. * For params to ctrl translation, 0.
  169. */
  170. int ctrl_cmd;
  171. /*
  172. * For ctrl_str to params translation, the actual ctrl command string
  173. * used. In this case, the (string) value is always passed as |p2|.
  174. * For params to ctrl translation, this is NULL. Along with it is also
  175. * and indicator whether it matched |ctrl_str| or |ctrl_hexstr| in the
  176. * translation item.
  177. */
  178. const char *ctrl_str;
  179. int ishex;
  180. /* the ctrl-style int argument. */
  181. int p1;
  182. /* the ctrl-style void* argument. */
  183. void *p2;
  184. /* a size, for passing back the |p2| size where applicable */
  185. size_t sz;
  186. /* pointer to the OSSL_PARAM-style params array. */
  187. OSSL_PARAM *params;
  188. /*-
  189. * The following are used entirely internally by the fixup_args functions
  190. * and should not be touched by the callers, at all.
  191. */
  192. /*
  193. * Copy of the ctrl-style void* argument, if the fixup_args function
  194. * needs to manipulate |p2| but wants to remember original.
  195. */
  196. void *orig_p2;
  197. /* Diverse types of storage for the needy. */
  198. char name_buf[OSSL_MAX_NAME_SIZE];
  199. void *allocated_buf;
  200. void *bufp;
  201. size_t buflen;
  202. };
  203. struct translation_st {
  204. /*-
  205. * What this table item does.
  206. *
  207. * If the item has this set to 0, it means that both GET and SET are
  208. * supported, and |fixup_args| will determine which it is. This is to
  209. * support translations of ctrls where the action type depends on the
  210. * value of |p1| or |p2| (ctrls are really bi-directional, but are
  211. * seldom used that way).
  212. *
  213. * This can be also used in the lookup template when it looks up by
  214. * OSSL_PARAM key, to indicate if a setter or a getter called.
  215. */
  216. enum action action_type;
  217. /*-
  218. * Conditions, for params->ctrl translations.
  219. *
  220. * In table item, |keytype1| and |keytype2| can be set to -1 to indicate
  221. * that this item supports all key types (or rather, that |fixup_args|
  222. * will check and return an error if it's not supported).
  223. * Any of these may be set to 0 to indicate that they are unset.
  224. */
  225. int keytype1; /* The EVP_PKEY_XXX type, i.e. NIDs. #legacy */
  226. int keytype2; /* Another EVP_PKEY_XXX type, used for aliases */
  227. int optype; /* The operation type */
  228. /*
  229. * Lookup and translation attributes
  230. *
  231. * |ctrl_num|, |ctrl_str|, |ctrl_hexstr| and |param_key| are lookup
  232. * attributes.
  233. *
  234. * |ctrl_num| may be 0 or that |param_key| may be NULL in the table item,
  235. * but not at the same time. If they are, they are simply not used for
  236. * lookup.
  237. * When |ctrl_num| == 0, no ctrl will be called. Likewise, when
  238. * |param_key| == NULL, no OSSL_PARAM setter/getter will be called.
  239. * In that case the treatment of the translation item relies entirely on
  240. * |fixup_args|, which is then assumed to have side effects.
  241. *
  242. * As a special case, it's possible to set |ctrl_hexstr| and assign NULL
  243. * to |ctrl_str|. That will signal to default_fixup_args() that the
  244. * value must always be interpreted as hex.
  245. */
  246. int ctrl_num; /* EVP_PKEY_CTRL_xxx */
  247. const char *ctrl_str; /* The corresponding ctrl string */
  248. const char *ctrl_hexstr; /* The alternative "hex{str}" ctrl string */
  249. const char *param_key; /* The corresponding OSSL_PARAM key */
  250. /*
  251. * The appropriate OSSL_PARAM data type. This may be 0 to indicate that
  252. * this OSSL_PARAM may have more than one data type, depending on input
  253. * material. In this case, |fixup_args| is expected to check and handle
  254. * it.
  255. */
  256. unsigned int param_data_type;
  257. /*
  258. * Fixer functions
  259. *
  260. * |fixup_args| is always called before (for SET) or after (for GET)
  261. * the actual ctrl / OSSL_PARAM function.
  262. */
  263. fixup_args_fn *fixup_args;
  264. };
  265. /*-
  266. * Fixer function implementations
  267. * ==============================
  268. */
  269. /*
  270. * default_check isn't a fixer per se, but rather a helper function to
  271. * perform certain standard checks.
  272. */
  273. static int default_check(enum state state,
  274. const struct translation_st *translation,
  275. const struct translation_ctx_st *ctx)
  276. {
  277. switch (state) {
  278. default:
  279. break;
  280. case PRE_CTRL_TO_PARAMS:
  281. if (!ossl_assert(translation != NULL)) {
  282. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  283. return -2;
  284. }
  285. if (!ossl_assert(translation->param_key != 0)
  286. || !ossl_assert(translation->param_data_type != 0)) {
  287. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  288. return -1;
  289. }
  290. break;
  291. case PRE_CTRL_STR_TO_PARAMS:
  292. /*
  293. * For ctrl_str to params translation, we allow direct use of
  294. * OSSL_PARAM keys as ctrl_str keys. Therefore, it's possible that
  295. * we end up with |translation == NULL|, which is fine. The fixup
  296. * function will have to deal with it carefully.
  297. */
  298. if (translation != NULL) {
  299. if (!ossl_assert(translation->action_type != GET)) {
  300. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  301. return -2;
  302. }
  303. if (!ossl_assert(translation->param_key != NULL)
  304. || !ossl_assert(translation->param_data_type != 0)) {
  305. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  306. return 0;
  307. }
  308. }
  309. break;
  310. case PRE_PARAMS_TO_CTRL:
  311. case POST_PARAMS_TO_CTRL:
  312. if (!ossl_assert(translation != NULL)) {
  313. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  314. return -2;
  315. }
  316. if (!ossl_assert(translation->ctrl_num != 0)
  317. || !ossl_assert(translation->param_data_type != 0)) {
  318. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  319. return -1;
  320. }
  321. }
  322. /* Nothing else to check */
  323. return 1;
  324. }
  325. /*-
  326. * default_fixup_args fixes up all sorts of arguments, governed by the
  327. * diverse attributes in the translation item. It covers all "standard"
  328. * base ctrl functionality, meaning it can handle basic conversion of
  329. * data between p1+p2 (SET) or return value+p2 (GET) as long as the values
  330. * don't have extra semantics (such as NIDs, OIDs, that sort of stuff).
  331. * Extra semantics must be handled via specific fixup_args functions.
  332. *
  333. * The following states and action type combinations have standard handling
  334. * done in this function:
  335. *
  336. * PRE_CTRL_TO_PARAMS, 0 - ERROR. action type must be
  337. * determined by a fixup function.
  338. * PRE_CTRL_TO_PARAMS, SET | GET - |p1| and |p2| are converted to an
  339. * OSSL_PARAM according to the data
  340. * type given in |translattion|.
  341. * For OSSL_PARAM_UNSIGNED_INTEGER,
  342. * a BIGNUM passed as |p2| is accepted.
  343. * POST_CTRL_TO_PARAMS, GET - If the OSSL_PARAM data type is a
  344. * STRING or PTR type, |p1| is set
  345. * to the OSSL_PARAM return size, and
  346. * |p2| is set to the string.
  347. * PRE_CTRL_STR_TO_PARAMS, !SET - ERROR. That combination is not
  348. * supported.
  349. * PRE_CTRL_STR_TO_PARAMS, SET - |p2| is taken as a string, and is
  350. * converted to an OSSL_PARAM in a
  351. * standard manner, guided by the
  352. * param key and data type from
  353. * |translation|.
  354. * PRE_PARAMS_TO_CTRL, SET - the OSSL_PARAM is converted to
  355. * |p1| and |p2| according to the
  356. * data type given in |translation|
  357. * For OSSL_PARAM_UNSIGNED_INTEGER,
  358. * if |p2| is non-NULL, then |*p2|
  359. * is assigned a BIGNUM, otherwise
  360. * |p1| is assigned an unsigned int.
  361. * POST_PARAMS_TO_CTRL, GET - |p1| and |p2| are converted to
  362. * an OSSL_PARAM, in the same manner
  363. * as for the combination of
  364. * PRE_CTRL_TO_PARAMS, SET.
  365. */
  366. static int default_fixup_args(enum state state,
  367. const struct translation_st *translation,
  368. struct translation_ctx_st *ctx)
  369. {
  370. int ret;
  371. if ((ret = default_check(state, translation, ctx)) <= 0)
  372. return ret;
  373. switch (state) {
  374. default:
  375. /* For states this function should never have been called with */
  376. ERR_raise_data(ERR_LIB_EVP, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED,
  377. "[action:%d, state:%d]", ctx->action_type, state);
  378. return 0;
  379. /*
  380. * PRE_CTRL_TO_PARAMS and POST_CTRL_TO_PARAMS handle ctrl to params
  381. * translations. PRE_CTRL_TO_PARAMS is responsible for preparing
  382. * |*params|, and POST_CTRL_TO_PARAMS is responsible for bringing the
  383. * result back to |*p2| and the return value.
  384. */
  385. case PRE_CTRL_TO_PARAMS:
  386. /* This is ctrl to params translation, so we need an OSSL_PARAM key */
  387. if (ctx->action_type == NONE) {
  388. /*
  389. * No action type is an error here. That's a case for a
  390. * special fixup function.
  391. */
  392. ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
  393. "[action:%d, state:%d]", ctx->action_type, state);
  394. return 0;
  395. }
  396. if (translation->optype != 0) {
  397. if ((EVP_PKEY_CTX_IS_SIGNATURE_OP(ctx->pctx)
  398. && ctx->pctx->op.sig.algctx == NULL)
  399. || (EVP_PKEY_CTX_IS_DERIVE_OP(ctx->pctx)
  400. && ctx->pctx->op.kex.algctx == NULL)
  401. || (EVP_PKEY_CTX_IS_ASYM_CIPHER_OP(ctx->pctx)
  402. && ctx->pctx->op.ciph.algctx == NULL)
  403. || (EVP_PKEY_CTX_IS_KEM_OP(ctx->pctx)
  404. && ctx->pctx->op.encap.algctx == NULL)
  405. /*
  406. * The following may be unnecessary, but we have them
  407. * for good measure...
  408. */
  409. || (EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx)
  410. && ctx->pctx->op.keymgmt.genctx == NULL)
  411. || (EVP_PKEY_CTX_IS_FROMDATA_OP(ctx->pctx)
  412. && ctx->pctx->op.keymgmt.genctx == NULL)) {
  413. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  414. /* Uses the same return values as EVP_PKEY_CTX_ctrl */
  415. return -2;
  416. }
  417. }
  418. /*
  419. * OSSL_PARAM_construct_TYPE() works equally well for both SET and GET.
  420. */
  421. switch (translation->param_data_type) {
  422. case OSSL_PARAM_INTEGER:
  423. *ctx->params = OSSL_PARAM_construct_int(translation->param_key,
  424. &ctx->p1);
  425. break;
  426. case OSSL_PARAM_UNSIGNED_INTEGER:
  427. /*
  428. * BIGNUMs are passed via |p2|. For all ctrl's that just want
  429. * to pass a simple integer via |p1|, |p2| is expected to be
  430. * NULL.
  431. *
  432. * Note that this allocates a buffer, which the cleanup function
  433. * must deallocate.
  434. */
  435. if (ctx->p2 != NULL) {
  436. if (ctx->action_type == SET) {
  437. ctx->buflen = BN_num_bytes(ctx->p2);
  438. if ((ctx->allocated_buf =
  439. OPENSSL_malloc(ctx->buflen)) == NULL) {
  440. ERR_raise(ERR_LIB_EVP, ERR_R_MALLOC_FAILURE);
  441. return 0;
  442. }
  443. if (BN_bn2nativepad(ctx->p2,
  444. ctx->allocated_buf, ctx->buflen) < 0) {
  445. OPENSSL_free(ctx->allocated_buf);
  446. ctx->allocated_buf = NULL;
  447. return 0;
  448. }
  449. *ctx->params =
  450. OSSL_PARAM_construct_BN(translation->param_key,
  451. ctx->allocated_buf,
  452. ctx->buflen);
  453. } else {
  454. /*
  455. * No support for getting a BIGNUM by ctrl, this needs
  456. * fixup_args function support.
  457. */
  458. ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
  459. "[action:%d, state:%d] trying to get a "
  460. "BIGNUM via ctrl call",
  461. ctx->action_type, state);
  462. return 0;
  463. }
  464. } else {
  465. *ctx->params =
  466. OSSL_PARAM_construct_uint(translation->param_key,
  467. (unsigned int *)&ctx->p1);
  468. }
  469. break;
  470. case OSSL_PARAM_UTF8_STRING:
  471. *ctx->params =
  472. OSSL_PARAM_construct_utf8_string(translation->param_key,
  473. ctx->p2, (size_t)ctx->p1);
  474. break;
  475. case OSSL_PARAM_UTF8_PTR:
  476. *ctx->params =
  477. OSSL_PARAM_construct_utf8_ptr(translation->param_key,
  478. ctx->p2, (size_t)ctx->p1);
  479. break;
  480. case OSSL_PARAM_OCTET_STRING:
  481. *ctx->params =
  482. OSSL_PARAM_construct_octet_string(translation->param_key,
  483. ctx->p2, (size_t)ctx->p1);
  484. break;
  485. case OSSL_PARAM_OCTET_PTR:
  486. *ctx->params =
  487. OSSL_PARAM_construct_octet_ptr(translation->param_key,
  488. ctx->p2, (size_t)ctx->p1);
  489. break;
  490. }
  491. break;
  492. case POST_CTRL_TO_PARAMS:
  493. /*
  494. * Because EVP_PKEY_CTX_ctrl() returns the length of certain objects
  495. * as its return value, we need to ensure that we do it here as well,
  496. * for the OSSL_PARAM data types where this makes sense.
  497. */
  498. if (ctx->action_type == GET) {
  499. switch (translation->param_data_type) {
  500. case OSSL_PARAM_UTF8_STRING:
  501. case OSSL_PARAM_UTF8_PTR:
  502. case OSSL_PARAM_OCTET_STRING:
  503. case OSSL_PARAM_OCTET_PTR:
  504. ctx->p1 = (int)ctx->params[0].return_size;
  505. break;
  506. }
  507. }
  508. break;
  509. /*
  510. * PRE_CTRL_STR_TO_PARAMS and POST_CTRL_STR_TO_PARAMS handle ctrl_str to
  511. * params translations. PRE_CTRL_TO_PARAMS is responsible for preparing
  512. * |*params|, and POST_CTRL_TO_PARAMS currently has nothing to do, since
  513. * there's no support for getting data via ctrl_str calls.
  514. */
  515. case PRE_CTRL_STR_TO_PARAMS:
  516. {
  517. /* This is ctrl_str to params translation */
  518. const char *tmp_ctrl_str = ctx->ctrl_str;
  519. const char *orig_ctrl_str = ctx->ctrl_str;
  520. const char *orig_value = ctx->p2;
  521. const OSSL_PARAM *settable = NULL;
  522. int exists = 0;
  523. /* Only setting is supported here */
  524. if (ctx->action_type != SET) {
  525. ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
  526. "[action:%d, state:%d] only setting allowed",
  527. ctx->action_type, state);
  528. return 0;
  529. }
  530. /*
  531. * If no translation exists, we simply pass the control string
  532. * unmodified.
  533. */
  534. if (translation != NULL) {
  535. tmp_ctrl_str = ctx->ctrl_str = translation->param_key;
  536. if (ctx->ishex) {
  537. strcpy(ctx->name_buf, "hex");
  538. if (OPENSSL_strlcat(ctx->name_buf, tmp_ctrl_str,
  539. sizeof(ctx->name_buf)) <= 3) {
  540. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  541. return -1;
  542. }
  543. tmp_ctrl_str = ctx->name_buf;
  544. }
  545. }
  546. settable = EVP_PKEY_CTX_settable_params(ctx->pctx);
  547. if (!OSSL_PARAM_allocate_from_text(ctx->params, settable,
  548. tmp_ctrl_str,
  549. ctx->p2, strlen(ctx->p2),
  550. &exists)) {
  551. if (!exists) {
  552. ERR_raise_data(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED,
  553. "[action:%d, state:%d] name=%s, value=%s",
  554. ctx->action_type, state,
  555. orig_ctrl_str, orig_value);
  556. return -2;
  557. }
  558. return 0;
  559. }
  560. ctx->allocated_buf = ctx->params->data;
  561. ctx->buflen = ctx->params->data_size;
  562. }
  563. break;
  564. case POST_CTRL_STR_TO_PARAMS:
  565. /* Nothing to be done */
  566. break;
  567. /*
  568. * PRE_PARAMS_TO_CTRL and POST_PARAMS_TO_CTRL handle params to ctrl
  569. * translations. PRE_PARAMS_TO_CTRL is responsible for preparing
  570. * |p1| and |p2|, and POST_PARAMS_TO_CTRL is responsible for bringing
  571. * the EVP_PKEY_CTX_ctrl() return value (passed as |p1|) and |p2| back
  572. * to |*params|.
  573. *
  574. * PKEY is treated just like POST_PARAMS_TO_CTRL, making it easy
  575. * for the related fixup_args functions to just set |p1| and |p2|
  576. * appropriately and leave it to this section of code to fix up
  577. * |ctx->params| accordingly.
  578. */
  579. case PKEY:
  580. case POST_PARAMS_TO_CTRL:
  581. ret = ctx->p1;
  582. /* FALLTHRU */
  583. case PRE_PARAMS_TO_CTRL:
  584. {
  585. /* This is params to ctrl translation */
  586. if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
  587. /* For the PRE state, only setting needs some work to be done */
  588. /* When setting, we populate |p1| and |p2| from |*params| */
  589. switch (translation->param_data_type) {
  590. case OSSL_PARAM_INTEGER:
  591. return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
  592. case OSSL_PARAM_UNSIGNED_INTEGER:
  593. if (ctx->p2 != NULL) {
  594. /* BIGNUM passed down with p2 */
  595. if (!OSSL_PARAM_get_BN(ctx->params, ctx->p2))
  596. return 0;
  597. } else {
  598. /* Normal C unsigned int passed down */
  599. if (!OSSL_PARAM_get_uint(ctx->params,
  600. (unsigned int *)&ctx->p1))
  601. return 0;
  602. }
  603. return 1;
  604. case OSSL_PARAM_UTF8_STRING:
  605. return OSSL_PARAM_get_utf8_string(ctx->params,
  606. ctx->p2, ctx->sz);
  607. case OSSL_PARAM_OCTET_STRING:
  608. return OSSL_PARAM_get_octet_string(ctx->params,
  609. &ctx->p2, ctx->sz,
  610. (size_t *)&ctx->p1);
  611. case OSSL_PARAM_OCTET_PTR:
  612. return OSSL_PARAM_get_octet_ptr(ctx->params,
  613. ctx->p2, &ctx->sz);
  614. default:
  615. ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
  616. "[action:%d, state:%d] "
  617. "unknown OSSL_PARAM data type %d",
  618. ctx->action_type, state,
  619. translation->param_data_type);
  620. return 0;
  621. }
  622. } else if ((state == POST_PARAMS_TO_CTRL || state == PKEY)
  623. && ctx->action_type == GET) {
  624. /* For the POST state, only getting needs some work to be done */
  625. unsigned int param_data_type = translation->param_data_type;
  626. size_t size = (size_t)ctx->p1;
  627. if (state == PKEY)
  628. size = ctx->sz;
  629. if (param_data_type == 0) {
  630. /* we must have a fixup_args function to work */
  631. if (!ossl_assert(translation->fixup_args != NULL)) {
  632. ERR_raise(ERR_LIB_EVP, ERR_R_INTERNAL_ERROR);
  633. return 0;
  634. }
  635. param_data_type = ctx->params->data_type;
  636. }
  637. /* When getting, we populate |*params| from |p1| and |p2| */
  638. switch (param_data_type) {
  639. case OSSL_PARAM_INTEGER:
  640. return OSSL_PARAM_set_int(ctx->params, ctx->p1);
  641. case OSSL_PARAM_UNSIGNED_INTEGER:
  642. if (ctx->p2 != NULL) {
  643. /* BIGNUM passed back */
  644. return OSSL_PARAM_set_BN(ctx->params, ctx->p2);
  645. } else {
  646. /* Normal C unsigned int passed back */
  647. return OSSL_PARAM_set_uint(ctx->params,
  648. (unsigned int)ctx->p1);
  649. }
  650. return 0;
  651. case OSSL_PARAM_UTF8_STRING:
  652. return OSSL_PARAM_set_utf8_string(ctx->params, ctx->p2);
  653. case OSSL_PARAM_OCTET_STRING:
  654. return OSSL_PARAM_set_octet_string(ctx->params, ctx->p2,
  655. size);
  656. case OSSL_PARAM_OCTET_PTR:
  657. return OSSL_PARAM_set_octet_ptr(ctx->params, *(void **)ctx->p2,
  658. size);
  659. default:
  660. ERR_raise_data(ERR_LIB_EVP, ERR_R_UNSUPPORTED,
  661. "[action:%d, state:%d] "
  662. "unsupported OSSL_PARAM data type %d",
  663. ctx->action_type, state,
  664. translation->param_data_type);
  665. return 0;
  666. }
  667. } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
  668. if (translation->param_data_type == OSSL_PARAM_OCTET_PTR)
  669. ctx->p2 = &ctx->bufp;
  670. }
  671. }
  672. /* Any other combination is simply pass-through */
  673. break;
  674. }
  675. return ret;
  676. }
  677. static int
  678. cleanup_translation_ctx(enum state state,
  679. const struct translation_st *translation,
  680. struct translation_ctx_st *ctx)
  681. {
  682. if (ctx->allocated_buf != NULL)
  683. OPENSSL_free(ctx->allocated_buf);
  684. ctx->allocated_buf = NULL;
  685. return 1;
  686. }
  687. /*
  688. * fix_cipher_md fixes up an EVP_CIPHER / EVP_MD to its name on SET,
  689. * and cipher / md name to EVP_MD on GET.
  690. */
  691. static const char *get_cipher_name(void *cipher)
  692. {
  693. return EVP_CIPHER_get0_name(cipher);
  694. }
  695. static const char *get_md_name(void *md)
  696. {
  697. return EVP_MD_get0_name(md);
  698. }
  699. static const void *get_cipher_by_name(OSSL_LIB_CTX *libctx, const char *name)
  700. {
  701. return evp_get_cipherbyname_ex(libctx, name);
  702. }
  703. static const void *get_md_by_name(OSSL_LIB_CTX *libctx, const char *name)
  704. {
  705. return evp_get_digestbyname_ex(libctx, name);
  706. }
  707. static int fix_cipher_md(enum state state,
  708. const struct translation_st *translation,
  709. struct translation_ctx_st *ctx,
  710. const char *(*get_name)(void *algo),
  711. const void *(*get_algo_by_name)(OSSL_LIB_CTX *libctx,
  712. const char *name))
  713. {
  714. int ret = 1;
  715. if ((ret = default_check(state, translation, ctx)) <= 0)
  716. return ret;
  717. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
  718. /*
  719. * |ctx->p2| contains the address to an EVP_CIPHER or EVP_MD pointer
  720. * to be filled in. We need to remember it, then make |ctx->p2|
  721. * point at a buffer to be filled in with the name, and |ctx->p1|
  722. * with its size. default_fixup_args() will take care of the rest
  723. * for us.
  724. */
  725. ctx->orig_p2 = ctx->p2;
  726. ctx->p2 = ctx->name_buf;
  727. ctx->p1 = sizeof(ctx->name_buf);
  728. } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
  729. /*
  730. * In different parts of OpenSSL, this ctrl command is used
  731. * differently. Some calls pass a NID as p1, others pass an
  732. * EVP_CIPHER pointer as p2...
  733. */
  734. ctx->p2 = (char *)(ctx->p2 == NULL
  735. ? OBJ_nid2sn(ctx->p1)
  736. : get_name(ctx->p2));
  737. ctx->p1 = strlen(ctx->p2);
  738. } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
  739. ctx->p2 = (ctx->p2 == NULL ? "" : (char *)get_name(ctx->p2));
  740. ctx->p1 = strlen(ctx->p2);
  741. }
  742. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  743. return ret;
  744. if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
  745. /*
  746. * Here's how we re-use |ctx->orig_p2| that was set in the
  747. * PRE_CTRL_TO_PARAMS state above.
  748. */
  749. *(void **)ctx->orig_p2 =
  750. (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
  751. ctx->p1 = 1;
  752. } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET) {
  753. ctx->p2 = (void *)get_algo_by_name(ctx->pctx->libctx, ctx->p2);
  754. ctx->p1 = 0;
  755. }
  756. return ret;
  757. }
  758. static int fix_cipher(enum state state,
  759. const struct translation_st *translation,
  760. struct translation_ctx_st *ctx)
  761. {
  762. return fix_cipher_md(state, translation, ctx,
  763. get_cipher_name, get_cipher_by_name);
  764. }
  765. static int fix_md(enum state state,
  766. const struct translation_st *translation,
  767. struct translation_ctx_st *ctx)
  768. {
  769. return fix_cipher_md(state, translation, ctx,
  770. get_md_name, get_md_by_name);
  771. }
  772. static int fix_distid_len(enum state state,
  773. const struct translation_st *translation,
  774. struct translation_ctx_st *ctx)
  775. {
  776. int ret = default_fixup_args(state, translation, ctx);
  777. if (ret > 0) {
  778. ret = 0;
  779. if ((state == POST_CTRL_TO_PARAMS
  780. || state == POST_CTRL_STR_TO_PARAMS) && ctx->action_type == GET) {
  781. *(size_t *)ctx->p2 = ctx->sz;
  782. ret = 1;
  783. }
  784. }
  785. return ret;
  786. }
  787. struct kdf_type_map_st {
  788. int kdf_type_num;
  789. const char *kdf_type_str;
  790. };
  791. static int fix_kdf_type(enum state state,
  792. const struct translation_st *translation,
  793. struct translation_ctx_st *ctx,
  794. const struct kdf_type_map_st *kdf_type_map)
  795. {
  796. /*
  797. * The EVP_PKEY_CTRL_DH_KDF_TYPE ctrl command is a bit special, in
  798. * that it's used both for setting a value, and for getting it, all
  799. * depending on the value if |p1|; if |p1| is -2, the backend is
  800. * supposed to place the current kdf type in |p2|, and if not, |p1|
  801. * is interpreted as the new kdf type.
  802. */
  803. int ret = 0;
  804. if ((ret = default_check(state, translation, ctx)) <= 0)
  805. return ret;
  806. if (state == PRE_CTRL_TO_PARAMS) {
  807. /*
  808. * In |translations|, the initial value for |ctx->action_type| must
  809. * be NONE.
  810. */
  811. if (!ossl_assert(ctx->action_type == NONE))
  812. return 0;
  813. /* The action type depends on the value of *p1 */
  814. if (ctx->p1 == -2) {
  815. /*
  816. * The OSSL_PARAMS getter needs space to store a copy of the kdf
  817. * type string. We use |ctx->name_buf|, which has enough space
  818. * allocated.
  819. *
  820. * (this wouldn't be needed if the OSSL_xxx_PARAM_KDF_TYPE
  821. * had the data type OSSL_PARAM_UTF8_PTR)
  822. */
  823. ctx->p2 = ctx->name_buf;
  824. ctx->p1 = sizeof(ctx->name_buf);
  825. ctx->action_type = GET;
  826. } else {
  827. ctx->action_type = SET;
  828. }
  829. }
  830. if ((ret = default_check(state, translation, ctx)) <= 0)
  831. return ret;
  832. if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
  833. || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
  834. ret = -2;
  835. /* Convert KDF type numbers to strings */
  836. for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
  837. if (ctx->p1 == kdf_type_map->kdf_type_num) {
  838. ctx->p2 = (char *)kdf_type_map->kdf_type_str;
  839. ret = 1;
  840. break;
  841. }
  842. if (ret <= 0)
  843. goto end;
  844. ctx->p1 = strlen(ctx->p2);
  845. }
  846. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  847. return ret;
  848. if ((state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)
  849. || (state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)) {
  850. ctx->p1 = ret = -1;
  851. /* Convert KDF type strings to numbers */
  852. for (; kdf_type_map->kdf_type_str != NULL; kdf_type_map++)
  853. if (OPENSSL_strcasecmp(ctx->p2, kdf_type_map->kdf_type_str) == 0) {
  854. ctx->p1 = kdf_type_map->kdf_type_num;
  855. ret = 1;
  856. break;
  857. }
  858. ctx->p2 = NULL;
  859. } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
  860. ctx->p1 = -2;
  861. }
  862. end:
  863. return ret;
  864. }
  865. /* EVP_PKEY_CTRL_DH_KDF_TYPE */
  866. static int fix_dh_kdf_type(enum state state,
  867. const struct translation_st *translation,
  868. struct translation_ctx_st *ctx)
  869. {
  870. static const struct kdf_type_map_st kdf_type_map[] = {
  871. { EVP_PKEY_DH_KDF_NONE, "" },
  872. { EVP_PKEY_DH_KDF_X9_42, OSSL_KDF_NAME_X942KDF_ASN1 },
  873. { 0, NULL }
  874. };
  875. return fix_kdf_type(state, translation, ctx, kdf_type_map);
  876. }
  877. /* EVP_PKEY_CTRL_EC_KDF_TYPE */
  878. static int fix_ec_kdf_type(enum state state,
  879. const struct translation_st *translation,
  880. struct translation_ctx_st *ctx)
  881. {
  882. static const struct kdf_type_map_st kdf_type_map[] = {
  883. { EVP_PKEY_ECDH_KDF_NONE, "" },
  884. { EVP_PKEY_ECDH_KDF_X9_63, OSSL_KDF_NAME_X963KDF },
  885. { 0, NULL }
  886. };
  887. return fix_kdf_type(state, translation, ctx, kdf_type_map);
  888. }
  889. /* EVP_PKEY_CTRL_DH_KDF_OID, EVP_PKEY_CTRL_GET_DH_KDF_OID, ...??? */
  890. static int fix_oid(enum state state,
  891. const struct translation_st *translation,
  892. struct translation_ctx_st *ctx)
  893. {
  894. int ret;
  895. if ((ret = default_check(state, translation, ctx)) <= 0)
  896. return ret;
  897. if ((state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET)
  898. || (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET)) {
  899. /*
  900. * We're translating from ctrl to params and setting the OID, or
  901. * we're translating from params to ctrl and getting the OID.
  902. * Either way, |ctx->p2| points at an ASN1_OBJECT, and needs to have
  903. * that replaced with the corresponding name.
  904. * default_fixup_args() will then be able to convert that to the
  905. * corresponding OSSL_PARAM.
  906. */
  907. OBJ_obj2txt(ctx->name_buf, sizeof(ctx->name_buf), ctx->p2, 0);
  908. ctx->p2 = (char *)ctx->name_buf;
  909. ctx->p1 = 0; /* let default_fixup_args() figure out the length */
  910. }
  911. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  912. return ret;
  913. if ((state == PRE_PARAMS_TO_CTRL && ctx->action_type == SET)
  914. || (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET)) {
  915. /*
  916. * We're translating from ctrl to params and setting the OID name,
  917. * or we're translating from params to ctrl and getting the OID
  918. * name. Either way, default_fixup_args() has placed the OID name
  919. * in |ctx->p2|, all we need to do now is to replace that with the
  920. * corresponding ASN1_OBJECT.
  921. */
  922. ctx->p2 = (ASN1_OBJECT *)OBJ_txt2obj(ctx->p2, 0);
  923. }
  924. return ret;
  925. }
  926. /* EVP_PKEY_CTRL_DH_NID */
  927. static int fix_dh_nid(enum state state,
  928. const struct translation_st *translation,
  929. struct translation_ctx_st *ctx)
  930. {
  931. int ret;
  932. if ((ret = default_check(state, translation, ctx)) <= 0)
  933. return ret;
  934. /* This is only settable */
  935. if (ctx->action_type != SET)
  936. return 0;
  937. if (state == PRE_CTRL_TO_PARAMS) {
  938. if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
  939. (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
  940. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  941. return 0;
  942. }
  943. ctx->p1 = 0;
  944. }
  945. return default_fixup_args(state, translation, ctx);
  946. }
  947. /* EVP_PKEY_CTRL_DH_RFC5114 */
  948. static int fix_dh_nid5114(enum state state,
  949. const struct translation_st *translation,
  950. struct translation_ctx_st *ctx)
  951. {
  952. int ret;
  953. if ((ret = default_check(state, translation, ctx)) <= 0)
  954. return ret;
  955. /* This is only settable */
  956. if (ctx->action_type != SET)
  957. return 0;
  958. switch (state) {
  959. case PRE_CTRL_TO_PARAMS:
  960. if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
  961. (ossl_ffc_uid_to_dh_named_group(ctx->p1))) == NULL) {
  962. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  963. return 0;
  964. }
  965. ctx->p1 = 0;
  966. break;
  967. case PRE_CTRL_STR_TO_PARAMS:
  968. if (ctx->p2 == NULL)
  969. return 0;
  970. if ((ctx->p2 = (char *)ossl_ffc_named_group_get_name
  971. (ossl_ffc_uid_to_dh_named_group(atoi(ctx->p2)))) == NULL) {
  972. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  973. return 0;
  974. }
  975. ctx->p1 = 0;
  976. break;
  977. default:
  978. break;
  979. }
  980. return default_fixup_args(state, translation, ctx);
  981. }
  982. /* EVP_PKEY_CTRL_DH_PARAMGEN_TYPE */
  983. static int fix_dh_paramgen_type(enum state state,
  984. const struct translation_st *translation,
  985. struct translation_ctx_st *ctx)
  986. {
  987. int ret;
  988. if ((ret = default_check(state, translation, ctx)) <= 0)
  989. return ret;
  990. /* This is only settable */
  991. if (ctx->action_type != SET)
  992. return 0;
  993. if (state == PRE_CTRL_STR_TO_PARAMS) {
  994. if ((ctx->p2 = (char *)ossl_dh_gen_type_id2name(atoi(ctx->p2)))
  995. == NULL) {
  996. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_VALUE);
  997. return 0;
  998. }
  999. ctx->p1 = strlen(ctx->p2);
  1000. }
  1001. return default_fixup_args(state, translation, ctx);
  1002. }
  1003. /* EVP_PKEY_CTRL_EC_PARAM_ENC */
  1004. static int fix_ec_param_enc(enum state state,
  1005. const struct translation_st *translation,
  1006. struct translation_ctx_st *ctx)
  1007. {
  1008. int ret;
  1009. if ((ret = default_check(state, translation, ctx)) <= 0)
  1010. return ret;
  1011. /* This is currently only settable */
  1012. if (ctx->action_type != SET)
  1013. return 0;
  1014. if (state == PRE_CTRL_TO_PARAMS) {
  1015. switch (ctx->p1) {
  1016. case OPENSSL_EC_EXPLICIT_CURVE:
  1017. ctx->p2 = OSSL_PKEY_EC_ENCODING_EXPLICIT;
  1018. break;
  1019. case OPENSSL_EC_NAMED_CURVE:
  1020. ctx->p2 = OSSL_PKEY_EC_ENCODING_GROUP;
  1021. break;
  1022. default:
  1023. ret = -2;
  1024. goto end;
  1025. }
  1026. ctx->p1 = 0;
  1027. }
  1028. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1029. return ret;
  1030. if (state == PRE_PARAMS_TO_CTRL) {
  1031. if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_EXPLICIT) == 0)
  1032. ctx->p1 = OPENSSL_EC_EXPLICIT_CURVE;
  1033. else if (strcmp(ctx->p2, OSSL_PKEY_EC_ENCODING_GROUP) == 0)
  1034. ctx->p1 = OPENSSL_EC_NAMED_CURVE;
  1035. else
  1036. ctx->p1 = ret = -2;
  1037. ctx->p2 = NULL;
  1038. }
  1039. end:
  1040. if (ret == -2)
  1041. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1042. return ret;
  1043. }
  1044. /* EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID */
  1045. static int fix_ec_paramgen_curve_nid(enum state state,
  1046. const struct translation_st *translation,
  1047. struct translation_ctx_st *ctx)
  1048. {
  1049. char *p2 = NULL;
  1050. int ret;
  1051. if ((ret = default_check(state, translation, ctx)) <= 0)
  1052. return ret;
  1053. /* This is currently only settable */
  1054. if (ctx->action_type != SET)
  1055. return 0;
  1056. if (state == PRE_CTRL_TO_PARAMS) {
  1057. ctx->p2 = (char *)OBJ_nid2sn(ctx->p1);
  1058. ctx->p1 = 0;
  1059. } else if (state == PRE_PARAMS_TO_CTRL) {
  1060. /*
  1061. * We're translating from params to ctrl and setting the curve name.
  1062. * The ctrl function needs it to be a NID, but meanwhile, we need
  1063. * space to get the curve name from the param. |ctx->name_buf| is
  1064. * sufficient for that.
  1065. * The double indirection is necessary for default_fixup_args()'s
  1066. * call of OSSL_PARAM_get_utf8_string() to be done correctly.
  1067. */
  1068. p2 = ctx->name_buf;
  1069. ctx->p2 = &p2;
  1070. ctx->sz = sizeof(ctx->name_buf);
  1071. }
  1072. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1073. return ret;
  1074. if (state == PRE_PARAMS_TO_CTRL) {
  1075. ctx->p1 = OBJ_sn2nid(p2);
  1076. ctx->p2 = NULL;
  1077. }
  1078. return ret;
  1079. }
  1080. /* EVP_PKEY_CTRL_EC_ECDH_COFACTOR */
  1081. static int fix_ecdh_cofactor(enum state state,
  1082. const struct translation_st *translation,
  1083. struct translation_ctx_st *ctx)
  1084. {
  1085. /*
  1086. * The EVP_PKEY_CTRL_EC_ECDH_COFACTOR ctrl command is a bit special, in
  1087. * that it's used both for setting a value, and for getting it, all
  1088. * depending on the value if |ctx->p1|; if |ctx->p1| is -2, the backend is
  1089. * supposed to place the current cofactor mode in |ctx->p2|, and if not,
  1090. * |ctx->p1| is interpreted as the new cofactor mode.
  1091. */
  1092. int ret = 0;
  1093. if (state == PRE_CTRL_TO_PARAMS) {
  1094. /*
  1095. * The initial value for |ctx->action_type| must be zero.
  1096. * evp_pkey_ctrl_to_params() takes it from the translation item.
  1097. */
  1098. if (!ossl_assert(ctx->action_type == NONE))
  1099. return 0;
  1100. /* The action type depends on the value of ctx->p1 */
  1101. if (ctx->p1 == -2)
  1102. ctx->action_type = GET;
  1103. else
  1104. ctx->action_type = SET;
  1105. } else if (state == PRE_CTRL_STR_TO_PARAMS) {
  1106. ctx->action_type = SET;
  1107. } else if (state == PRE_PARAMS_TO_CTRL) {
  1108. /* The initial value for |ctx->action_type| must not be zero. */
  1109. if (!ossl_assert(ctx->action_type != NONE))
  1110. return 0;
  1111. }
  1112. if ((ret = default_check(state, translation, ctx)) <= 0)
  1113. return ret;
  1114. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
  1115. if (ctx->p1 < -1 || ctx->p1 > 1) {
  1116. /* Uses the same return value of pkey_ec_ctrl() */
  1117. return -2;
  1118. }
  1119. }
  1120. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1121. return ret;
  1122. if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1123. if (ctx->p1 < 0 || ctx->p1 > 1) {
  1124. /*
  1125. * The provider should return either 0 or 1, any other value is a
  1126. * provider error.
  1127. */
  1128. ctx->p1 = ret = -1;
  1129. }
  1130. } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
  1131. ctx->p1 = -2;
  1132. }
  1133. return ret;
  1134. }
  1135. /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
  1136. static int fix_rsa_padding_mode(enum state state,
  1137. const struct translation_st *translation,
  1138. struct translation_ctx_st *ctx)
  1139. {
  1140. static const OSSL_ITEM str_value_map[] = {
  1141. { RSA_PKCS1_PADDING, "pkcs1" },
  1142. { RSA_NO_PADDING, "none" },
  1143. { RSA_PKCS1_OAEP_PADDING, "oaep" },
  1144. { RSA_PKCS1_OAEP_PADDING, "oeap" },
  1145. { RSA_X931_PADDING, "x931" },
  1146. { RSA_PKCS1_PSS_PADDING, "pss" },
  1147. /* Special case, will pass directly as an integer */
  1148. { RSA_PKCS1_WITH_TLS_PADDING, NULL }
  1149. };
  1150. int ret;
  1151. if ((ret = default_check(state, translation, ctx)) <= 0)
  1152. return ret;
  1153. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1154. /*
  1155. * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
  1156. * weirdest way for a ctrl. Instead of doing like all other ctrls
  1157. * that return a simple, i.e. just have that as a return value,
  1158. * this particular ctrl treats p2 as the address for the int to be
  1159. * returned. We must therefore remember |ctx->p2|, then make
  1160. * |ctx->p2| point at a buffer to be filled in with the name, and
  1161. * |ctx->p1| with its size. default_fixup_args() will take care
  1162. * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
  1163. * code section further down.
  1164. */
  1165. ctx->orig_p2 = ctx->p2;
  1166. ctx->p2 = ctx->name_buf;
  1167. ctx->p1 = sizeof(ctx->name_buf);
  1168. } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
  1169. /*
  1170. * Ideally, we should use utf8 strings for the diverse padding modes.
  1171. * We only came here because someone called EVP_PKEY_CTX_ctrl(),
  1172. * though, and since that can reasonably be seen as legacy code
  1173. * that uses the diverse RSA macros for the padding mode, and we
  1174. * know that at least our providers can handle the numeric modes,
  1175. * we take the cheap route for now.
  1176. *
  1177. * The other solution would be to match |ctx->p1| against entries
  1178. * in str_value_map and pass the corresponding string. However,
  1179. * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
  1180. * we have to do this same hack at least for that one.
  1181. *
  1182. * Since the "official" data type for the RSA padding mode is utf8
  1183. * string, we cannot count on default_fixup_args(). Instead, we
  1184. * build the OSSL_PARAM item ourselves and return immediately.
  1185. */
  1186. ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
  1187. &ctx->p1);
  1188. return 1;
  1189. } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
  1190. size_t i;
  1191. /*
  1192. * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
  1193. * string, or may have asked for an integer of some sort. If they
  1194. * ask for an integer, we respond directly. If not, we translate
  1195. * the response from the ctrl function into a string.
  1196. */
  1197. switch (ctx->params->data_type) {
  1198. case OSSL_PARAM_INTEGER:
  1199. return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
  1200. case OSSL_PARAM_UNSIGNED_INTEGER:
  1201. return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
  1202. default:
  1203. break;
  1204. }
  1205. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1206. if (ctx->p1 == (int)str_value_map[i].id)
  1207. break;
  1208. }
  1209. if (i == OSSL_NELEM(str_value_map)) {
  1210. ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
  1211. "[action:%d, state:%d] padding number %d",
  1212. ctx->action_type, state, ctx->p1);
  1213. return -2;
  1214. }
  1215. /*
  1216. * If we don't have a string, we can't do anything. The caller
  1217. * should have asked for a number...
  1218. */
  1219. if (str_value_map[i].ptr == NULL) {
  1220. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1221. return -2;
  1222. }
  1223. ctx->p2 = str_value_map[i].ptr;
  1224. ctx->p1 = strlen(ctx->p2);
  1225. }
  1226. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1227. return ret;
  1228. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1229. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1230. size_t i;
  1231. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1232. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1233. break;
  1234. }
  1235. if (i == OSSL_NELEM(str_value_map)) {
  1236. ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
  1237. "[action:%d, state:%d] padding name %s",
  1238. ctx->action_type, state, ctx->p1);
  1239. ctx->p1 = ret = -2;
  1240. } else if (state == POST_CTRL_TO_PARAMS) {
  1241. /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
  1242. *(int *)ctx->orig_p2 = str_value_map[i].id;
  1243. } else {
  1244. ctx->p1 = str_value_map[i].id;
  1245. }
  1246. ctx->p2 = NULL;
  1247. }
  1248. return ret;
  1249. }
  1250. /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
  1251. static int fix_rsa_pss_saltlen(enum state state,
  1252. const struct translation_st *translation,
  1253. struct translation_ctx_st *ctx)
  1254. {
  1255. static const OSSL_ITEM str_value_map[] = {
  1256. { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
  1257. { (unsigned int)RSA_PSS_SALTLEN_MAX, "max" },
  1258. { (unsigned int)RSA_PSS_SALTLEN_AUTO, "auto" }
  1259. };
  1260. int ret;
  1261. if ((ret = default_check(state, translation, ctx)) <= 0)
  1262. return ret;
  1263. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1264. /*
  1265. * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
  1266. * in the int pointed at by p2. This is potentially as weird as
  1267. * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
  1268. * might be a negative value, so it wouldn't work as a legitimate
  1269. * return value.
  1270. * In any case, we must therefore remember |ctx->p2|, then make
  1271. * |ctx->p2| point at a buffer to be filled in with the name, and
  1272. * |ctx->p1| with its size. default_fixup_args() will take care
  1273. * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
  1274. * code section further down.
  1275. */
  1276. ctx->orig_p2 = ctx->p2;
  1277. ctx->p2 = ctx->name_buf;
  1278. ctx->p1 = sizeof(ctx->name_buf);
  1279. } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
  1280. || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
  1281. size_t i;
  1282. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1283. if (ctx->p1 == (int)str_value_map[i].id)
  1284. break;
  1285. }
  1286. if (i == OSSL_NELEM(str_value_map)) {
  1287. BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
  1288. } else {
  1289. /* This won't truncate but it will quiet static analysers */
  1290. strncpy(ctx->name_buf, str_value_map[i].ptr, sizeof(ctx->name_buf) - 1);
  1291. ctx->name_buf[sizeof(ctx->name_buf) - 1] = '\0';
  1292. }
  1293. ctx->p2 = ctx->name_buf;
  1294. ctx->p1 = strlen(ctx->p2);
  1295. }
  1296. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1297. return ret;
  1298. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1299. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1300. size_t i;
  1301. int val;
  1302. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1303. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1304. break;
  1305. }
  1306. val = i == OSSL_NELEM(str_value_map) ? atoi(ctx->p2)
  1307. : (int)str_value_map[i].id;
  1308. if (state == POST_CTRL_TO_PARAMS) {
  1309. /*
  1310. * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
  1311. * up
  1312. */
  1313. *(int *)ctx->orig_p2 = val;
  1314. } else {
  1315. ctx->p1 = val;
  1316. }
  1317. ctx->p2 = NULL;
  1318. }
  1319. return ret;
  1320. }
  1321. /* EVP_PKEY_CTRL_HKDF_MODE */
  1322. static int fix_hkdf_mode(enum state state,
  1323. const struct translation_st *translation,
  1324. struct translation_ctx_st *ctx)
  1325. {
  1326. static const OSSL_ITEM str_value_map[] = {
  1327. { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
  1328. { EVP_KDF_HKDF_MODE_EXTRACT_ONLY, "EXTRACT_ONLY" },
  1329. { EVP_KDF_HKDF_MODE_EXPAND_ONLY, "EXPAND_ONLY" }
  1330. };
  1331. int ret;
  1332. if ((ret = default_check(state, translation, ctx)) <= 0)
  1333. return ret;
  1334. if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
  1335. || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
  1336. size_t i;
  1337. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1338. if (ctx->p1 == (int)str_value_map[i].id)
  1339. break;
  1340. }
  1341. if (i == OSSL_NELEM(str_value_map))
  1342. return 0;
  1343. ctx->p2 = str_value_map[i].ptr;
  1344. ctx->p1 = strlen(ctx->p2);
  1345. }
  1346. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1347. return ret;
  1348. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1349. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1350. size_t i;
  1351. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1352. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1353. break;
  1354. }
  1355. if (i == OSSL_NELEM(str_value_map))
  1356. return 0;
  1357. if (state == POST_CTRL_TO_PARAMS)
  1358. ret = str_value_map[i].id;
  1359. else
  1360. ctx->p1 = str_value_map[i].id;
  1361. ctx->p2 = NULL;
  1362. }
  1363. return 1;
  1364. }
  1365. /*-
  1366. * Payload getters
  1367. * ===============
  1368. *
  1369. * These all get the data they want, then call default_fixup_args() as
  1370. * a post-ctrl GET fixup. They all get NULL ctx, ctrl_cmd, ctrl_str,
  1371. * p1, sz
  1372. */
  1373. /* Pilfering DH, DSA and EC_KEY */
  1374. static int get_payload_group_name(enum state state,
  1375. const struct translation_st *translation,
  1376. struct translation_ctx_st *ctx)
  1377. {
  1378. EVP_PKEY *pkey = ctx->p2;
  1379. ctx->p2 = NULL;
  1380. switch (EVP_PKEY_get_base_id(pkey)) {
  1381. #ifndef OPENSSL_NO_DH
  1382. case EVP_PKEY_DH:
  1383. {
  1384. const DH *dh = EVP_PKEY_get0_DH(pkey);
  1385. int uid = DH_get_nid(dh);
  1386. if (uid != NID_undef) {
  1387. const DH_NAMED_GROUP *dh_group =
  1388. ossl_ffc_uid_to_dh_named_group(uid);
  1389. ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
  1390. }
  1391. }
  1392. break;
  1393. #endif
  1394. #ifndef OPENSSL_NO_EC
  1395. case EVP_PKEY_EC:
  1396. {
  1397. const EC_GROUP *grp =
  1398. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
  1399. int nid = NID_undef;
  1400. if (grp != NULL)
  1401. nid = EC_GROUP_get_curve_name(grp);
  1402. if (nid != NID_undef)
  1403. ctx->p2 = (char *)OSSL_EC_curve_nid2name(nid);
  1404. }
  1405. break;
  1406. #endif
  1407. default:
  1408. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1409. return 0;
  1410. }
  1411. /*
  1412. * Quietly ignoring unknown groups matches the behaviour on the provider
  1413. * side.
  1414. */
  1415. if (ctx->p2 == NULL)
  1416. return 1;
  1417. ctx->p1 = strlen(ctx->p2);
  1418. return default_fixup_args(state, translation, ctx);
  1419. }
  1420. static int get_payload_private_key(enum state state,
  1421. const struct translation_st *translation,
  1422. struct translation_ctx_st *ctx)
  1423. {
  1424. EVP_PKEY *pkey = ctx->p2;
  1425. ctx->p2 = NULL;
  1426. if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  1427. return 0;
  1428. switch (EVP_PKEY_get_base_id(pkey)) {
  1429. #ifndef OPENSSL_NO_DH
  1430. case EVP_PKEY_DH:
  1431. {
  1432. const DH *dh = EVP_PKEY_get0_DH(pkey);
  1433. ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
  1434. }
  1435. break;
  1436. #endif
  1437. #ifndef OPENSSL_NO_EC
  1438. case EVP_PKEY_EC:
  1439. {
  1440. const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  1441. ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
  1442. }
  1443. break;
  1444. #endif
  1445. default:
  1446. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1447. return 0;
  1448. }
  1449. return default_fixup_args(state, translation, ctx);
  1450. }
  1451. static int get_payload_public_key(enum state state,
  1452. const struct translation_st *translation,
  1453. struct translation_ctx_st *ctx)
  1454. {
  1455. EVP_PKEY *pkey = ctx->p2;
  1456. unsigned char *buf = NULL;
  1457. int ret;
  1458. ctx->p2 = NULL;
  1459. switch (EVP_PKEY_get_base_id(pkey)) {
  1460. #ifndef OPENSSL_NO_DH
  1461. case EVP_PKEY_DHX:
  1462. case EVP_PKEY_DH:
  1463. switch (ctx->params->data_type) {
  1464. case OSSL_PARAM_OCTET_STRING:
  1465. ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
  1466. ctx->p2 = buf;
  1467. break;
  1468. case OSSL_PARAM_UNSIGNED_INTEGER:
  1469. ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
  1470. break;
  1471. default:
  1472. return 0;
  1473. }
  1474. break;
  1475. #endif
  1476. #ifndef OPENSSL_NO_DSA
  1477. case EVP_PKEY_DSA:
  1478. if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  1479. ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
  1480. break;
  1481. }
  1482. return 0;
  1483. #endif
  1484. #ifndef OPENSSL_NO_EC
  1485. case EVP_PKEY_EC:
  1486. if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
  1487. const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
  1488. BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
  1489. const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
  1490. const EC_POINT *point = EC_KEY_get0_public_key(eckey);
  1491. if (bnctx == NULL)
  1492. return 0;
  1493. ctx->sz = EC_POINT_point2buf(ecg, point,
  1494. POINT_CONVERSION_COMPRESSED,
  1495. &buf, bnctx);
  1496. ctx->p2 = buf;
  1497. BN_CTX_free(bnctx);
  1498. break;
  1499. }
  1500. return 0;
  1501. #endif
  1502. default:
  1503. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1504. return 0;
  1505. }
  1506. ret = default_fixup_args(state, translation, ctx);
  1507. OPENSSL_free(buf);
  1508. return ret;
  1509. }
  1510. static int get_payload_bn(enum state state,
  1511. const struct translation_st *translation,
  1512. struct translation_ctx_st *ctx, const BIGNUM *bn)
  1513. {
  1514. if (bn == NULL)
  1515. return 0;
  1516. if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  1517. return 0;
  1518. ctx->p2 = (BIGNUM *)bn;
  1519. return default_fixup_args(state, translation, ctx);
  1520. }
  1521. static int get_dh_dsa_payload_p(enum state state,
  1522. const struct translation_st *translation,
  1523. struct translation_ctx_st *ctx)
  1524. {
  1525. const BIGNUM *bn = NULL;
  1526. EVP_PKEY *pkey = ctx->p2;
  1527. switch (EVP_PKEY_get_base_id(pkey)) {
  1528. #ifndef OPENSSL_NO_DH
  1529. case EVP_PKEY_DH:
  1530. bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
  1531. break;
  1532. #endif
  1533. #ifndef OPENSSL_NO_DSA
  1534. case EVP_PKEY_DSA:
  1535. bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
  1536. break;
  1537. #endif
  1538. default:
  1539. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1540. }
  1541. return get_payload_bn(state, translation, ctx, bn);
  1542. }
  1543. static int get_dh_dsa_payload_q(enum state state,
  1544. const struct translation_st *translation,
  1545. struct translation_ctx_st *ctx)
  1546. {
  1547. const BIGNUM *bn = NULL;
  1548. switch (EVP_PKEY_get_base_id(ctx->p2)) {
  1549. #ifndef OPENSSL_NO_DH
  1550. case EVP_PKEY_DH:
  1551. bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
  1552. break;
  1553. #endif
  1554. #ifndef OPENSSL_NO_DSA
  1555. case EVP_PKEY_DSA:
  1556. bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
  1557. break;
  1558. #endif
  1559. }
  1560. return get_payload_bn(state, translation, ctx, bn);
  1561. }
  1562. static int get_dh_dsa_payload_g(enum state state,
  1563. const struct translation_st *translation,
  1564. struct translation_ctx_st *ctx)
  1565. {
  1566. const BIGNUM *bn = NULL;
  1567. switch (EVP_PKEY_get_base_id(ctx->p2)) {
  1568. #ifndef OPENSSL_NO_DH
  1569. case EVP_PKEY_DH:
  1570. bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
  1571. break;
  1572. #endif
  1573. #ifndef OPENSSL_NO_DSA
  1574. case EVP_PKEY_DSA:
  1575. bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
  1576. break;
  1577. #endif
  1578. }
  1579. return get_payload_bn(state, translation, ctx, bn);
  1580. }
  1581. static int get_payload_int(enum state state,
  1582. const struct translation_st *translation,
  1583. struct translation_ctx_st *ctx,
  1584. const int val)
  1585. {
  1586. if (ctx->params->data_type != OSSL_PARAM_INTEGER)
  1587. return 0;
  1588. ctx->p1 = val;
  1589. ctx->p2 = NULL;
  1590. return default_fixup_args(state, translation, ctx);
  1591. }
  1592. static int get_ec_decoded_from_explicit_params(enum state state,
  1593. const struct translation_st *translation,
  1594. struct translation_ctx_st *ctx)
  1595. {
  1596. int val = 0;
  1597. EVP_PKEY *pkey = ctx->p2;
  1598. switch (EVP_PKEY_base_id(pkey)) {
  1599. #ifndef OPENSSL_NO_EC
  1600. case EVP_PKEY_EC:
  1601. val = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey));
  1602. if (val < 0) {
  1603. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
  1604. return 0;
  1605. }
  1606. break;
  1607. #endif
  1608. default:
  1609. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1610. return 0;
  1611. }
  1612. return get_payload_int(state, translation, ctx, val);
  1613. }
  1614. static int get_rsa_payload_n(enum state state,
  1615. const struct translation_st *translation,
  1616. struct translation_ctx_st *ctx)
  1617. {
  1618. const BIGNUM *bn = NULL;
  1619. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
  1620. return 0;
  1621. bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
  1622. return get_payload_bn(state, translation, ctx, bn);
  1623. }
  1624. static int get_rsa_payload_e(enum state state,
  1625. const struct translation_st *translation,
  1626. struct translation_ctx_st *ctx)
  1627. {
  1628. const BIGNUM *bn = NULL;
  1629. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
  1630. return 0;
  1631. bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
  1632. return get_payload_bn(state, translation, ctx, bn);
  1633. }
  1634. static int get_rsa_payload_d(enum state state,
  1635. const struct translation_st *translation,
  1636. struct translation_ctx_st *ctx)
  1637. {
  1638. const BIGNUM *bn = NULL;
  1639. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA)
  1640. return 0;
  1641. bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
  1642. return get_payload_bn(state, translation, ctx, bn);
  1643. }
  1644. static int get_rsa_payload_factor(enum state state,
  1645. const struct translation_st *translation,
  1646. struct translation_ctx_st *ctx,
  1647. size_t factornum)
  1648. {
  1649. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1650. const BIGNUM *bn = NULL;
  1651. switch (factornum) {
  1652. case 0:
  1653. bn = RSA_get0_p(r);
  1654. break;
  1655. case 1:
  1656. bn = RSA_get0_q(r);
  1657. break;
  1658. default:
  1659. {
  1660. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1661. const BIGNUM *factors[10];
  1662. if (factornum - 2 < pnum
  1663. && RSA_get0_multi_prime_factors(r, factors))
  1664. bn = factors[factornum - 2];
  1665. }
  1666. break;
  1667. }
  1668. return get_payload_bn(state, translation, ctx, bn);
  1669. }
  1670. static int get_rsa_payload_exponent(enum state state,
  1671. const struct translation_st *translation,
  1672. struct translation_ctx_st *ctx,
  1673. size_t exponentnum)
  1674. {
  1675. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1676. const BIGNUM *bn = NULL;
  1677. switch (exponentnum) {
  1678. case 0:
  1679. bn = RSA_get0_dmp1(r);
  1680. break;
  1681. case 1:
  1682. bn = RSA_get0_dmq1(r);
  1683. break;
  1684. default:
  1685. {
  1686. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1687. const BIGNUM *exps[10], *coeffs[10];
  1688. if (exponentnum - 2 < pnum
  1689. && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
  1690. bn = exps[exponentnum - 2];
  1691. }
  1692. break;
  1693. }
  1694. return get_payload_bn(state, translation, ctx, bn);
  1695. }
  1696. static int get_rsa_payload_coefficient(enum state state,
  1697. const struct translation_st *translation,
  1698. struct translation_ctx_st *ctx,
  1699. size_t coefficientnum)
  1700. {
  1701. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1702. const BIGNUM *bn = NULL;
  1703. switch (coefficientnum) {
  1704. case 0:
  1705. bn = RSA_get0_iqmp(r);
  1706. break;
  1707. default:
  1708. {
  1709. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1710. const BIGNUM *exps[10], *coeffs[10];
  1711. if (coefficientnum - 1 < pnum
  1712. && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
  1713. bn = coeffs[coefficientnum - 1];
  1714. }
  1715. break;
  1716. }
  1717. return get_payload_bn(state, translation, ctx, bn);
  1718. }
  1719. #define IMPL_GET_RSA_PAYLOAD_FACTOR(n) \
  1720. static int \
  1721. get_rsa_payload_f##n(enum state state, \
  1722. const struct translation_st *translation, \
  1723. struct translation_ctx_st *ctx) \
  1724. { \
  1725. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
  1726. return 0; \
  1727. return get_rsa_payload_factor(state, translation, ctx, n - 1); \
  1728. }
  1729. #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n) \
  1730. static int \
  1731. get_rsa_payload_e##n(enum state state, \
  1732. const struct translation_st *translation, \
  1733. struct translation_ctx_st *ctx) \
  1734. { \
  1735. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
  1736. return 0; \
  1737. return get_rsa_payload_exponent(state, translation, ctx, \
  1738. n - 1); \
  1739. }
  1740. #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n) \
  1741. static int \
  1742. get_rsa_payload_c##n(enum state state, \
  1743. const struct translation_st *translation, \
  1744. struct translation_ctx_st *ctx) \
  1745. { \
  1746. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA) \
  1747. return 0; \
  1748. return get_rsa_payload_coefficient(state, translation, ctx, \
  1749. n - 1); \
  1750. }
  1751. IMPL_GET_RSA_PAYLOAD_FACTOR(1)
  1752. IMPL_GET_RSA_PAYLOAD_FACTOR(2)
  1753. IMPL_GET_RSA_PAYLOAD_FACTOR(3)
  1754. IMPL_GET_RSA_PAYLOAD_FACTOR(4)
  1755. IMPL_GET_RSA_PAYLOAD_FACTOR(5)
  1756. IMPL_GET_RSA_PAYLOAD_FACTOR(6)
  1757. IMPL_GET_RSA_PAYLOAD_FACTOR(7)
  1758. IMPL_GET_RSA_PAYLOAD_FACTOR(8)
  1759. IMPL_GET_RSA_PAYLOAD_FACTOR(9)
  1760. IMPL_GET_RSA_PAYLOAD_FACTOR(10)
  1761. IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
  1762. IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
  1763. IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
  1764. IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
  1765. IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
  1766. IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
  1767. IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
  1768. IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
  1769. IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
  1770. IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
  1771. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
  1772. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
  1773. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
  1774. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
  1775. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
  1776. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
  1777. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
  1778. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
  1779. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
  1780. static int fix_group_ecx(enum state state,
  1781. const struct translation_st *translation,
  1782. struct translation_ctx_st *ctx)
  1783. {
  1784. const char *value = NULL;
  1785. switch (state) {
  1786. case PRE_PARAMS_TO_CTRL:
  1787. if (!EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx))
  1788. return 0;
  1789. ctx->action_type = NONE;
  1790. return 1;
  1791. case POST_PARAMS_TO_CTRL:
  1792. if (OSSL_PARAM_get_utf8_string_ptr(ctx->params, &value) == 0 ||
  1793. OPENSSL_strcasecmp(ctx->pctx->keytype, value) != 0) {
  1794. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
  1795. ctx->p1 = 0;
  1796. return 0;
  1797. }
  1798. ctx->p1 = 1;
  1799. return 1;
  1800. default:
  1801. return 0;
  1802. }
  1803. }
  1804. /*-
  1805. * The translation table itself
  1806. * ============================
  1807. */
  1808. static const struct translation_st evp_pkey_ctx_translations[] = {
  1809. /*
  1810. * DistID: we pass it to the backend as an octet string,
  1811. * but get it back as a pointer to an octet string.
  1812. *
  1813. * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
  1814. * that has no separate counterpart in OSSL_PARAM terms, since we get
  1815. * the length of the DistID automatically when getting the DistID itself.
  1816. */
  1817. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  1818. EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
  1819. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
  1820. { GET, -1, -1, -1,
  1821. EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
  1822. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
  1823. { GET, -1, -1, -1,
  1824. EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
  1825. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
  1826. /*-
  1827. * DH & DHX
  1828. * ========
  1829. */
  1830. /*
  1831. * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting. The
  1832. * fixup function has to handle this...
  1833. */
  1834. { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1835. EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
  1836. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
  1837. fix_dh_kdf_type },
  1838. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1839. EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
  1840. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1841. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1842. EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
  1843. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1844. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1845. EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
  1846. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1847. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1848. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
  1849. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1850. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1851. EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
  1852. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  1853. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1854. EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
  1855. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  1856. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1857. EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
  1858. OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
  1859. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1860. EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
  1861. OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
  1862. /* DHX Keygen Parameters that are shared with DH */
  1863. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1864. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
  1865. OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
  1866. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1867. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
  1868. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1869. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1870. EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
  1871. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
  1872. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1873. EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
  1874. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
  1875. /* DH Keygen Parameters that are shared with DHX */
  1876. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1877. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
  1878. OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
  1879. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1880. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
  1881. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1882. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1883. EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
  1884. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
  1885. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1886. EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
  1887. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
  1888. /* DH specific Keygen Parameters */
  1889. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1890. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
  1891. OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
  1892. /* DHX specific Keygen Parameters */
  1893. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1894. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
  1895. OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1896. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
  1897. EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
  1898. OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1899. /*-
  1900. * DSA
  1901. * ===
  1902. */
  1903. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1904. EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
  1905. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1906. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1907. EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
  1908. OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1909. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1910. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
  1911. OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1912. /*-
  1913. * EC
  1914. * ==
  1915. */
  1916. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1917. EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
  1918. OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
  1919. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1920. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
  1921. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  1922. fix_ec_paramgen_curve_nid },
  1923. /*
  1924. * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
  1925. * both for setting and getting. The fixup function has to handle this...
  1926. */
  1927. { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1928. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
  1929. OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
  1930. fix_ecdh_cofactor },
  1931. { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1932. EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
  1933. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
  1934. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1935. EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
  1936. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1937. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1938. EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
  1939. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1940. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1941. EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
  1942. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1943. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1944. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
  1945. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1946. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1947. EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
  1948. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  1949. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1950. EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
  1951. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  1952. /*-
  1953. * SM2
  1954. * ==
  1955. */
  1956. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1957. EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
  1958. OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
  1959. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1960. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
  1961. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  1962. fix_ec_paramgen_curve_nid },
  1963. /*
  1964. * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
  1965. * both for setting and getting. The fixup function has to handle this...
  1966. */
  1967. { NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1968. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
  1969. OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
  1970. fix_ecdh_cofactor },
  1971. { NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1972. EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
  1973. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
  1974. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1975. EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
  1976. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1977. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1978. EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
  1979. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1980. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1981. EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
  1982. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1983. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1984. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
  1985. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1986. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1987. EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
  1988. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  1989. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  1990. EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
  1991. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  1992. /*-
  1993. * RSA
  1994. * ===
  1995. */
  1996. /*
  1997. * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
  1998. * and can be both with OSSL_PARAM. We standardise on strings here,
  1999. * fix_rsa_padding_mode() does the work when the caller has a different
  2000. * idea.
  2001. */
  2002. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2003. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2004. EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
  2005. OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
  2006. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2007. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2008. EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
  2009. OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
  2010. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2011. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2012. EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
  2013. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2014. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2015. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2016. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
  2017. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2018. /*
  2019. * RSA-PSS saltlen is essentially numeric, but certain values can be
  2020. * expressed as keywords (strings) with ctrl_str. The corresponding
  2021. * OSSL_PARAM allows both forms.
  2022. * fix_rsa_pss_saltlen() takes care of the distinction.
  2023. */
  2024. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
  2025. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
  2026. OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
  2027. fix_rsa_pss_saltlen },
  2028. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
  2029. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
  2030. OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
  2031. fix_rsa_pss_saltlen },
  2032. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2033. EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
  2034. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2035. { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2036. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
  2037. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2038. /*
  2039. * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
  2040. * This is accommodated by default_fixup_args() above, which mimics that
  2041. * expectation for any translation item where |ctrl_str| is NULL and
  2042. * |ctrl_hexstr| is non-NULL.
  2043. */
  2044. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2045. EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
  2046. OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
  2047. { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2048. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
  2049. OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR, NULL },
  2050. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2051. EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
  2052. OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2053. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2054. EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
  2055. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2056. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2057. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
  2058. OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
  2059. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  2060. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
  2061. OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2062. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
  2063. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
  2064. OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2065. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_KEYGEN,
  2066. EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
  2067. OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2068. /*-
  2069. * SipHash
  2070. * ======
  2071. */
  2072. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2073. EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
  2074. OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2075. /*-
  2076. * TLS1-PRF
  2077. * ========
  2078. */
  2079. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2080. EVP_PKEY_CTRL_TLS_MD, "md", NULL,
  2081. OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2082. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2083. EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
  2084. OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
  2085. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2086. EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
  2087. OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
  2088. /*-
  2089. * HKDF
  2090. * ====
  2091. */
  2092. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2093. EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
  2094. OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2095. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2096. EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
  2097. OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
  2098. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2099. EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
  2100. OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
  2101. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2102. EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
  2103. OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
  2104. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2105. EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
  2106. OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
  2107. /*-
  2108. * Scrypt
  2109. * ======
  2110. */
  2111. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2112. EVP_PKEY_CTRL_PASS, "pass", "hexpass",
  2113. OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
  2114. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2115. EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
  2116. OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
  2117. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2118. EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
  2119. OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2120. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2121. EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
  2122. OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2123. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2124. EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
  2125. OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2126. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2127. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
  2128. OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2129. { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
  2130. EVP_PKEY_CTRL_CIPHER, NULL, NULL,
  2131. OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
  2132. { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
  2133. EVP_PKEY_CTRL_SET_MAC_KEY, "key", "hexkey",
  2134. OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
  2135. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2136. EVP_PKEY_CTRL_MD, NULL, NULL,
  2137. OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2138. { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2139. EVP_PKEY_CTRL_GET_MD, NULL, NULL,
  2140. OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2141. /*-
  2142. * ECX
  2143. * ===
  2144. */
  2145. { SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
  2146. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2147. { SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
  2148. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2149. { SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
  2150. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2151. { SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
  2152. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2153. };
  2154. static const struct translation_st evp_pkey_translations[] = {
  2155. /*
  2156. * The following contain no ctrls, they are exclusively here to extract
  2157. * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
  2158. * on |fixup_args| to pass the actual data. The |fixup_args| should
  2159. * expect to get the EVP_PKEY pointer through |ctx->p2|.
  2160. */
  2161. /* DH, DSA & EC */
  2162. { GET, -1, -1, -1, 0, NULL, NULL,
  2163. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  2164. get_payload_group_name },
  2165. { GET, -1, -1, -1, 0, NULL, NULL,
  2166. OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
  2167. get_payload_private_key },
  2168. { GET, -1, -1, -1, 0, NULL, NULL,
  2169. OSSL_PKEY_PARAM_PUB_KEY,
  2170. 0 /* no data type, let get_payload_public_key() handle that */,
  2171. get_payload_public_key },
  2172. /* DH and DSA */
  2173. { GET, -1, -1, -1, 0, NULL, NULL,
  2174. OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
  2175. get_dh_dsa_payload_p },
  2176. { GET, -1, -1, -1, 0, NULL, NULL,
  2177. OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
  2178. get_dh_dsa_payload_g },
  2179. { GET, -1, -1, -1, 0, NULL, NULL,
  2180. OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
  2181. get_dh_dsa_payload_q },
  2182. /* RSA */
  2183. { GET, -1, -1, -1, 0, NULL, NULL,
  2184. OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
  2185. get_rsa_payload_n },
  2186. { GET, -1, -1, -1, 0, NULL, NULL,
  2187. OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
  2188. get_rsa_payload_e },
  2189. { GET, -1, -1, -1, 0, NULL, NULL,
  2190. OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
  2191. get_rsa_payload_d },
  2192. { GET, -1, -1, -1, 0, NULL, NULL,
  2193. OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
  2194. get_rsa_payload_f1 },
  2195. { GET, -1, -1, -1, 0, NULL, NULL,
  2196. OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
  2197. get_rsa_payload_f2 },
  2198. { GET, -1, -1, -1, 0, NULL, NULL,
  2199. OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
  2200. get_rsa_payload_f3 },
  2201. { GET, -1, -1, -1, 0, NULL, NULL,
  2202. OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
  2203. get_rsa_payload_f4 },
  2204. { GET, -1, -1, -1, 0, NULL, NULL,
  2205. OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
  2206. get_rsa_payload_f5 },
  2207. { GET, -1, -1, -1, 0, NULL, NULL,
  2208. OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
  2209. get_rsa_payload_f6 },
  2210. { GET, -1, -1, -1, 0, NULL, NULL,
  2211. OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
  2212. get_rsa_payload_f7 },
  2213. { GET, -1, -1, -1, 0, NULL, NULL,
  2214. OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
  2215. get_rsa_payload_f8 },
  2216. { GET, -1, -1, -1, 0, NULL, NULL,
  2217. OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
  2218. get_rsa_payload_f9 },
  2219. { GET, -1, -1, -1, 0, NULL, NULL,
  2220. OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
  2221. get_rsa_payload_f10 },
  2222. { GET, -1, -1, -1, 0, NULL, NULL,
  2223. OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
  2224. get_rsa_payload_e1 },
  2225. { GET, -1, -1, -1, 0, NULL, NULL,
  2226. OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
  2227. get_rsa_payload_e2 },
  2228. { GET, -1, -1, -1, 0, NULL, NULL,
  2229. OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
  2230. get_rsa_payload_e3 },
  2231. { GET, -1, -1, -1, 0, NULL, NULL,
  2232. OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
  2233. get_rsa_payload_e4 },
  2234. { GET, -1, -1, -1, 0, NULL, NULL,
  2235. OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
  2236. get_rsa_payload_e5 },
  2237. { GET, -1, -1, -1, 0, NULL, NULL,
  2238. OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
  2239. get_rsa_payload_e6 },
  2240. { GET, -1, -1, -1, 0, NULL, NULL,
  2241. OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
  2242. get_rsa_payload_e7 },
  2243. { GET, -1, -1, -1, 0, NULL, NULL,
  2244. OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
  2245. get_rsa_payload_e8 },
  2246. { GET, -1, -1, -1, 0, NULL, NULL,
  2247. OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
  2248. get_rsa_payload_e9 },
  2249. { GET, -1, -1, -1, 0, NULL, NULL,
  2250. OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
  2251. get_rsa_payload_e10 },
  2252. { GET, -1, -1, -1, 0, NULL, NULL,
  2253. OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
  2254. get_rsa_payload_c1 },
  2255. { GET, -1, -1, -1, 0, NULL, NULL,
  2256. OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
  2257. get_rsa_payload_c2 },
  2258. { GET, -1, -1, -1, 0, NULL, NULL,
  2259. OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
  2260. get_rsa_payload_c3 },
  2261. { GET, -1, -1, -1, 0, NULL, NULL,
  2262. OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
  2263. get_rsa_payload_c4 },
  2264. { GET, -1, -1, -1, 0, NULL, NULL,
  2265. OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
  2266. get_rsa_payload_c5 },
  2267. { GET, -1, -1, -1, 0, NULL, NULL,
  2268. OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
  2269. get_rsa_payload_c6 },
  2270. { GET, -1, -1, -1, 0, NULL, NULL,
  2271. OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
  2272. get_rsa_payload_c7 },
  2273. { GET, -1, -1, -1, 0, NULL, NULL,
  2274. OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
  2275. get_rsa_payload_c8 },
  2276. { GET, -1, -1, -1, 0, NULL, NULL,
  2277. OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
  2278. get_rsa_payload_c9 },
  2279. /* EC */
  2280. { GET, -1, -1, -1, 0, NULL, NULL,
  2281. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, OSSL_PARAM_INTEGER,
  2282. get_ec_decoded_from_explicit_params },
  2283. };
  2284. static const struct translation_st *
  2285. lookup_translation(struct translation_st *tmpl,
  2286. const struct translation_st *translations,
  2287. size_t translations_num)
  2288. {
  2289. size_t i;
  2290. for (i = 0; i < translations_num; i++) {
  2291. const struct translation_st *item = &translations[i];
  2292. /*
  2293. * Sanity check the translation table item.
  2294. *
  2295. * 1. Either both keytypes are -1, or neither of them are.
  2296. * 2. TBA...
  2297. */
  2298. if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
  2299. continue;
  2300. /*
  2301. * Base search criteria: check that the optype and keytypes match,
  2302. * if relevant. All callers must synthesise these bits somehow.
  2303. */
  2304. if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
  2305. continue;
  2306. /*
  2307. * This expression is stunningly simple thanks to the sanity check
  2308. * above.
  2309. */
  2310. if (item->keytype1 != -1
  2311. && tmpl->keytype1 != item->keytype1
  2312. && tmpl->keytype2 != item->keytype2)
  2313. continue;
  2314. /*
  2315. * Done with the base search criteria, now we check the criteria for
  2316. * the individual types of translations:
  2317. * ctrl->params, ctrl_str->params, and params->ctrl
  2318. */
  2319. if (tmpl->ctrl_num != 0) {
  2320. if (tmpl->ctrl_num != item->ctrl_num)
  2321. continue;
  2322. } else if (tmpl->ctrl_str != NULL) {
  2323. const char *ctrl_str = NULL;
  2324. const char *ctrl_hexstr = NULL;
  2325. /*
  2326. * Search criteria that originates from a ctrl_str is only used
  2327. * for setting, never for getting. Therefore, we only look at
  2328. * the setter items.
  2329. */
  2330. if (item->action_type != NONE
  2331. && item->action_type != SET)
  2332. continue;
  2333. /*
  2334. * At least one of the ctrl cmd names must be match the ctrl
  2335. * cmd name in the template.
  2336. */
  2337. if (item->ctrl_str != NULL
  2338. && OPENSSL_strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
  2339. ctrl_str = tmpl->ctrl_str;
  2340. else if (item->ctrl_hexstr != NULL
  2341. && OPENSSL_strcasecmp(tmpl->ctrl_hexstr,
  2342. item->ctrl_hexstr) == 0)
  2343. ctrl_hexstr = tmpl->ctrl_hexstr;
  2344. else
  2345. continue;
  2346. /* Modify the template to signal which string matched */
  2347. tmpl->ctrl_str = ctrl_str;
  2348. tmpl->ctrl_hexstr = ctrl_hexstr;
  2349. } else if (tmpl->param_key != NULL) {
  2350. /*
  2351. * Search criteria that originates from an OSSL_PARAM setter or
  2352. * getter.
  2353. *
  2354. * Ctrls were fundamentally bidirectional, with only the ctrl
  2355. * command macro name implying direction (if you're lucky).
  2356. * A few ctrl commands were even taking advantage of the
  2357. * bidirectional nature, making the direction depend in the
  2358. * value of the numeric argument.
  2359. *
  2360. * OSSL_PARAM functions are fundamentally different, in that
  2361. * setters and getters are separated, so the data direction is
  2362. * implied by the function that's used. The same OSSL_PARAM
  2363. * key name can therefore be used in both directions. We must
  2364. * therefore take the action type into account in this case.
  2365. */
  2366. if ((item->action_type != NONE
  2367. && tmpl->action_type != item->action_type)
  2368. || (item->param_key != NULL
  2369. && OPENSSL_strcasecmp(tmpl->param_key,
  2370. item->param_key) != 0))
  2371. continue;
  2372. } else {
  2373. return NULL;
  2374. }
  2375. return item;
  2376. }
  2377. return NULL;
  2378. }
  2379. static const struct translation_st *
  2380. lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
  2381. {
  2382. return lookup_translation(tmpl, evp_pkey_ctx_translations,
  2383. OSSL_NELEM(evp_pkey_ctx_translations));
  2384. }
  2385. static const struct translation_st *
  2386. lookup_evp_pkey_translation(struct translation_st *tmpl)
  2387. {
  2388. return lookup_translation(tmpl, evp_pkey_translations,
  2389. OSSL_NELEM(evp_pkey_translations));
  2390. }
  2391. /* This must ONLY be called for provider side operations */
  2392. int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
  2393. int keytype, int optype,
  2394. int cmd, int p1, void *p2)
  2395. {
  2396. struct translation_ctx_st ctx = { 0, };
  2397. struct translation_st tmpl = { 0, };
  2398. const struct translation_st *translation = NULL;
  2399. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  2400. int ret;
  2401. fixup_args_fn *fixup = default_fixup_args;
  2402. if (keytype == -1)
  2403. keytype = pctx->legacy_keytype;
  2404. tmpl.ctrl_num = cmd;
  2405. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2406. tmpl.optype = optype;
  2407. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2408. if (translation == NULL) {
  2409. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  2410. return -2;
  2411. }
  2412. if (pctx->pmeth != NULL
  2413. && pctx->pmeth->pkey_id != translation->keytype1
  2414. && pctx->pmeth->pkey_id != translation->keytype2)
  2415. return -1;
  2416. if (translation->fixup_args != NULL)
  2417. fixup = translation->fixup_args;
  2418. ctx.action_type = translation->action_type;
  2419. ctx.ctrl_cmd = cmd;
  2420. ctx.p1 = p1;
  2421. ctx.p2 = p2;
  2422. ctx.pctx = pctx;
  2423. ctx.params = params;
  2424. ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
  2425. if (ret > 0) {
  2426. switch (ctx.action_type) {
  2427. default:
  2428. /* fixup_args is expected to make sure this is dead code */
  2429. break;
  2430. case GET:
  2431. ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
  2432. break;
  2433. case SET:
  2434. ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
  2435. break;
  2436. }
  2437. }
  2438. /*
  2439. * In POST, we pass the return value as p1, allowing the fixup_args
  2440. * function to affect it by changing its value.
  2441. */
  2442. if (ret > 0) {
  2443. ctx.p1 = ret;
  2444. fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
  2445. ret = ctx.p1;
  2446. }
  2447. cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
  2448. return ret;
  2449. }
  2450. /* This must ONLY be called for provider side operations */
  2451. int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
  2452. const char *name, const char *value)
  2453. {
  2454. struct translation_ctx_st ctx = { 0, };
  2455. struct translation_st tmpl = { 0, };
  2456. const struct translation_st *translation = NULL;
  2457. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  2458. int keytype = pctx->legacy_keytype;
  2459. int optype = pctx->operation == 0 ? -1 : pctx->operation;
  2460. int ret;
  2461. fixup_args_fn *fixup = default_fixup_args;
  2462. tmpl.action_type = SET;
  2463. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2464. tmpl.optype = optype;
  2465. tmpl.ctrl_str = name;
  2466. tmpl.ctrl_hexstr = name;
  2467. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2468. if (translation != NULL) {
  2469. if (translation->fixup_args != NULL)
  2470. fixup = translation->fixup_args;
  2471. ctx.action_type = translation->action_type;
  2472. ctx.ishex = (tmpl.ctrl_hexstr != NULL);
  2473. } else {
  2474. /* String controls really only support setting */
  2475. ctx.action_type = SET;
  2476. }
  2477. ctx.ctrl_str = name;
  2478. ctx.p1 = (int)strlen(value);
  2479. ctx.p2 = (char *)value;
  2480. ctx.pctx = pctx;
  2481. ctx.params = params;
  2482. ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
  2483. if (ret > 0) {
  2484. switch (ctx.action_type) {
  2485. default:
  2486. /* fixup_args is expected to make sure this is dead code */
  2487. break;
  2488. case GET:
  2489. /*
  2490. * this is dead code, but must be present, or some compilers
  2491. * will complain
  2492. */
  2493. break;
  2494. case SET:
  2495. ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
  2496. break;
  2497. }
  2498. }
  2499. if (ret > 0)
  2500. ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
  2501. cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
  2502. return ret;
  2503. }
  2504. /* This must ONLY be called for legacy operations */
  2505. static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
  2506. enum action action_type,
  2507. OSSL_PARAM *params)
  2508. {
  2509. int keytype = pctx->legacy_keytype;
  2510. int optype = pctx->operation == 0 ? -1 : pctx->operation;
  2511. for (; params != NULL && params->key != NULL; params++) {
  2512. struct translation_ctx_st ctx = { 0, };
  2513. struct translation_st tmpl = { 0, };
  2514. const struct translation_st *translation = NULL;
  2515. fixup_args_fn *fixup = default_fixup_args;
  2516. int ret;
  2517. tmpl.action_type = action_type;
  2518. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2519. tmpl.optype = optype;
  2520. tmpl.param_key = params->key;
  2521. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2522. if (translation != NULL) {
  2523. if (translation->fixup_args != NULL)
  2524. fixup = translation->fixup_args;
  2525. ctx.action_type = translation->action_type;
  2526. ctx.ctrl_cmd = translation->ctrl_num;
  2527. }
  2528. ctx.pctx = pctx;
  2529. ctx.params = params;
  2530. ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
  2531. if (ret > 0 && ctx.action_type != NONE)
  2532. ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
  2533. ctx.ctrl_cmd, ctx.p1, ctx.p2);
  2534. /*
  2535. * In POST, we pass the return value as p1, allowing the fixup_args
  2536. * function to put it to good use, or maybe affect it.
  2537. */
  2538. if (ret > 0) {
  2539. ctx.p1 = ret;
  2540. fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
  2541. ret = ctx.p1;
  2542. }
  2543. cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
  2544. if (ret <= 0)
  2545. return 0;
  2546. }
  2547. return 1;
  2548. }
  2549. int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
  2550. {
  2551. return evp_pkey_ctx_setget_params_to_ctrl(ctx, SET, (OSSL_PARAM *)params);
  2552. }
  2553. int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  2554. {
  2555. return evp_pkey_ctx_setget_params_to_ctrl(ctx, GET, params);
  2556. }
  2557. /* This must ONLY be called for legacy EVP_PKEYs */
  2558. static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
  2559. enum action action_type,
  2560. OSSL_PARAM *params)
  2561. {
  2562. int ret = 1;
  2563. for (; params != NULL && params->key != NULL; params++) {
  2564. struct translation_ctx_st ctx = { 0, };
  2565. struct translation_st tmpl = { 0, };
  2566. const struct translation_st *translation = NULL;
  2567. fixup_args_fn *fixup = default_fixup_args;
  2568. tmpl.action_type = action_type;
  2569. tmpl.param_key = params->key;
  2570. translation = lookup_evp_pkey_translation(&tmpl);
  2571. if (translation != NULL) {
  2572. if (translation->fixup_args != NULL)
  2573. fixup = translation->fixup_args;
  2574. ctx.action_type = translation->action_type;
  2575. }
  2576. ctx.p2 = (void *)pkey;
  2577. ctx.params = params;
  2578. /*
  2579. * EVP_PKEY doesn't have any ctrl function, so we rely completely
  2580. * on fixup_args to do the whole work. Also, we currently only
  2581. * support getting.
  2582. */
  2583. if (!ossl_assert(translation != NULL)
  2584. || !ossl_assert(translation->action_type == GET)
  2585. || !ossl_assert(translation->fixup_args != NULL)) {
  2586. return -2;
  2587. }
  2588. ret = fixup(PKEY, translation, &ctx);
  2589. cleanup_translation_ctx(PKEY, translation, &ctx);
  2590. }
  2591. return ret;
  2592. }
  2593. int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
  2594. {
  2595. return evp_pkey_setget_params_to_ctrl(pkey, GET, params);
  2596. }