ctrl_params_translate.c 103 KB

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