ctrl_params_translate.c 102 KB

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