ctrl_params_translate.c 109 KB

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