ctrl_params_translate.c 109 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952
  1. /*
  2. * Copyright 2021-2024 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. } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == NONE) {
  1110. ctx->action_type = GET;
  1111. }
  1112. if ((ret = default_check(state, translation, ctx)) <= 0)
  1113. return ret;
  1114. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
  1115. if (ctx->p1 < -1 || ctx->p1 > 1) {
  1116. /* Uses the same return value of pkey_ec_ctrl() */
  1117. return -2;
  1118. }
  1119. }
  1120. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1121. return ret;
  1122. if (state == POST_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1123. if (ctx->p1 < 0 || ctx->p1 > 1) {
  1124. /*
  1125. * The provider should return either 0 or 1, any other value is a
  1126. * provider error.
  1127. */
  1128. ctx->p1 = ret = -1;
  1129. }
  1130. } else if (state == PRE_PARAMS_TO_CTRL && ctx->action_type == GET) {
  1131. ctx->p1 = -2;
  1132. } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
  1133. ctx->p1 = ret;
  1134. }
  1135. return ret;
  1136. }
  1137. /* EVP_PKEY_CTRL_RSA_PADDING, EVP_PKEY_CTRL_GET_RSA_PADDING */
  1138. static int fix_rsa_padding_mode(enum state state,
  1139. const struct translation_st *translation,
  1140. struct translation_ctx_st *ctx)
  1141. {
  1142. static const OSSL_ITEM str_value_map[] = {
  1143. { RSA_PKCS1_PADDING, "pkcs1" },
  1144. { RSA_NO_PADDING, "none" },
  1145. { RSA_PKCS1_OAEP_PADDING, "oaep" },
  1146. { RSA_PKCS1_OAEP_PADDING, "oeap" },
  1147. { RSA_X931_PADDING, "x931" },
  1148. { RSA_PKCS1_PSS_PADDING, "pss" },
  1149. /* Special case, will pass directly as an integer */
  1150. { RSA_PKCS1_WITH_TLS_PADDING, NULL }
  1151. };
  1152. int ret;
  1153. if ((ret = default_check(state, translation, ctx)) <= 0)
  1154. return ret;
  1155. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1156. /*
  1157. * EVP_PKEY_CTRL_GET_RSA_PADDING returns the padding mode in the
  1158. * weirdest way for a ctrl. Instead of doing like all other ctrls
  1159. * that return a simple, i.e. just have that as a return value,
  1160. * this particular ctrl treats p2 as the address for the int to be
  1161. * returned. We must therefore remember |ctx->p2|, then make
  1162. * |ctx->p2| point at a buffer to be filled in with the name, and
  1163. * |ctx->p1| with its size. default_fixup_args() will take care
  1164. * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
  1165. * code section further down.
  1166. */
  1167. ctx->orig_p2 = ctx->p2;
  1168. ctx->p2 = ctx->name_buf;
  1169. ctx->p1 = sizeof(ctx->name_buf);
  1170. } else if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == SET) {
  1171. /*
  1172. * Ideally, we should use utf8 strings for the diverse padding modes.
  1173. * We only came here because someone called EVP_PKEY_CTX_ctrl(),
  1174. * though, and since that can reasonably be seen as legacy code
  1175. * that uses the diverse RSA macros for the padding mode, and we
  1176. * know that at least our providers can handle the numeric modes,
  1177. * we take the cheap route for now.
  1178. *
  1179. * The other solution would be to match |ctx->p1| against entries
  1180. * in str_value_map and pass the corresponding string. However,
  1181. * since we don't have a string for RSA_PKCS1_WITH_TLS_PADDING,
  1182. * we have to do this same hack at least for that one.
  1183. *
  1184. * Since the "official" data type for the RSA padding mode is utf8
  1185. * string, we cannot count on default_fixup_args(). Instead, we
  1186. * build the OSSL_PARAM item ourselves and return immediately.
  1187. */
  1188. ctx->params[0] = OSSL_PARAM_construct_int(translation->param_key,
  1189. &ctx->p1);
  1190. return 1;
  1191. } else if (state == POST_PARAMS_TO_CTRL && ctx->action_type == GET) {
  1192. size_t i;
  1193. /*
  1194. * The EVP_PKEY_CTX_get_params() caller may have asked for a utf8
  1195. * string, or may have asked for an integer of some sort. If they
  1196. * ask for an integer, we respond directly. If not, we translate
  1197. * the response from the ctrl function into a string.
  1198. */
  1199. switch (ctx->params->data_type) {
  1200. case OSSL_PARAM_INTEGER:
  1201. return OSSL_PARAM_get_int(ctx->params, &ctx->p1);
  1202. case OSSL_PARAM_UNSIGNED_INTEGER:
  1203. return OSSL_PARAM_get_uint(ctx->params, (unsigned int *)&ctx->p1);
  1204. default:
  1205. break;
  1206. }
  1207. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1208. if (ctx->p1 == (int)str_value_map[i].id)
  1209. break;
  1210. }
  1211. if (i == OSSL_NELEM(str_value_map)) {
  1212. ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
  1213. "[action:%d, state:%d] padding number %d",
  1214. ctx->action_type, state, ctx->p1);
  1215. return -2;
  1216. }
  1217. /*
  1218. * If we don't have a string, we can't do anything. The caller
  1219. * should have asked for a number...
  1220. */
  1221. if (str_value_map[i].ptr == NULL) {
  1222. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  1223. return -2;
  1224. }
  1225. ctx->p2 = str_value_map[i].ptr;
  1226. ctx->p1 = strlen(ctx->p2);
  1227. }
  1228. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1229. return ret;
  1230. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1231. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1232. size_t i;
  1233. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1234. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1235. break;
  1236. }
  1237. if (i == OSSL_NELEM(str_value_map)) {
  1238. ERR_raise_data(ERR_LIB_RSA, RSA_R_UNKNOWN_PADDING_TYPE,
  1239. "[action:%d, state:%d] padding name %s",
  1240. ctx->action_type, state, ctx->p1);
  1241. ctx->p1 = ret = -2;
  1242. } else if (state == POST_CTRL_TO_PARAMS) {
  1243. /* EVP_PKEY_CTRL_GET_RSA_PADDING weirdness explained further up */
  1244. *(int *)ctx->orig_p2 = str_value_map[i].id;
  1245. } else {
  1246. ctx->p1 = str_value_map[i].id;
  1247. }
  1248. ctx->p2 = NULL;
  1249. }
  1250. return ret;
  1251. }
  1252. /* EVP_PKEY_CTRL_RSA_PSS_SALTLEN, EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN */
  1253. static int fix_rsa_pss_saltlen(enum state state,
  1254. const struct translation_st *translation,
  1255. struct translation_ctx_st *ctx)
  1256. {
  1257. static const OSSL_ITEM str_value_map[] = {
  1258. { (unsigned int)RSA_PSS_SALTLEN_DIGEST, "digest" },
  1259. { (unsigned int)RSA_PSS_SALTLEN_MAX, "max" },
  1260. { (unsigned int)RSA_PSS_SALTLEN_AUTO, "auto" }
  1261. };
  1262. int ret;
  1263. if ((ret = default_check(state, translation, ctx)) <= 0)
  1264. return ret;
  1265. if (state == PRE_CTRL_TO_PARAMS && ctx->action_type == GET) {
  1266. /*
  1267. * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN returns the saltlen by filling
  1268. * in the int pointed at by p2. This is potentially as weird as
  1269. * the way EVP_PKEY_CTRL_GET_RSA_PADDING works, except that saltlen
  1270. * might be a negative value, so it wouldn't work as a legitimate
  1271. * return value.
  1272. * In any case, we must therefore remember |ctx->p2|, then make
  1273. * |ctx->p2| point at a buffer to be filled in with the name, and
  1274. * |ctx->p1| with its size. default_fixup_args() will take care
  1275. * of the rest for us, along with the POST_CTRL_TO_PARAMS && GET
  1276. * code section further down.
  1277. */
  1278. ctx->orig_p2 = ctx->p2;
  1279. ctx->p2 = ctx->name_buf;
  1280. ctx->p1 = sizeof(ctx->name_buf);
  1281. } else if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
  1282. || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
  1283. size_t i;
  1284. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1285. if (ctx->p1 == (int)str_value_map[i].id)
  1286. break;
  1287. }
  1288. if (i == OSSL_NELEM(str_value_map)) {
  1289. BIO_snprintf(ctx->name_buf, sizeof(ctx->name_buf), "%d", ctx->p1);
  1290. } else {
  1291. /* This won't truncate but it will quiet static analysers */
  1292. strncpy(ctx->name_buf, str_value_map[i].ptr, sizeof(ctx->name_buf) - 1);
  1293. ctx->name_buf[sizeof(ctx->name_buf) - 1] = '\0';
  1294. }
  1295. ctx->p2 = ctx->name_buf;
  1296. ctx->p1 = strlen(ctx->p2);
  1297. }
  1298. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1299. return ret;
  1300. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1301. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1302. size_t i;
  1303. int val;
  1304. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1305. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1306. break;
  1307. }
  1308. val = i == OSSL_NELEM(str_value_map) ? atoi(ctx->p2)
  1309. : (int)str_value_map[i].id;
  1310. if (state == POST_CTRL_TO_PARAMS) {
  1311. /*
  1312. * EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN weirdness explained further
  1313. * up
  1314. */
  1315. *(int *)ctx->orig_p2 = val;
  1316. } else {
  1317. ctx->p1 = val;
  1318. }
  1319. ctx->p2 = NULL;
  1320. }
  1321. return ret;
  1322. }
  1323. /* EVP_PKEY_CTRL_HKDF_MODE */
  1324. static int fix_hkdf_mode(enum state state,
  1325. const struct translation_st *translation,
  1326. struct translation_ctx_st *ctx)
  1327. {
  1328. static const OSSL_ITEM str_value_map[] = {
  1329. { EVP_KDF_HKDF_MODE_EXTRACT_AND_EXPAND, "EXTRACT_AND_EXPAND" },
  1330. { EVP_KDF_HKDF_MODE_EXTRACT_ONLY, "EXTRACT_ONLY" },
  1331. { EVP_KDF_HKDF_MODE_EXPAND_ONLY, "EXPAND_ONLY" }
  1332. };
  1333. int ret;
  1334. if ((ret = default_check(state, translation, ctx)) <= 0)
  1335. return ret;
  1336. if ((ctx->action_type == SET && state == PRE_CTRL_TO_PARAMS)
  1337. || (ctx->action_type == GET && state == POST_PARAMS_TO_CTRL)) {
  1338. size_t i;
  1339. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1340. if (ctx->p1 == (int)str_value_map[i].id)
  1341. break;
  1342. }
  1343. if (i == OSSL_NELEM(str_value_map))
  1344. return 0;
  1345. ctx->p2 = str_value_map[i].ptr;
  1346. ctx->p1 = strlen(ctx->p2);
  1347. }
  1348. if ((ret = default_fixup_args(state, translation, ctx)) <= 0)
  1349. return ret;
  1350. if ((ctx->action_type == SET && state == PRE_PARAMS_TO_CTRL)
  1351. || (ctx->action_type == GET && state == POST_CTRL_TO_PARAMS)) {
  1352. size_t i;
  1353. for (i = 0; i < OSSL_NELEM(str_value_map); i++) {
  1354. if (strcmp(ctx->p2, str_value_map[i].ptr) == 0)
  1355. break;
  1356. }
  1357. if (i == OSSL_NELEM(str_value_map))
  1358. return 0;
  1359. if (state == POST_CTRL_TO_PARAMS)
  1360. ret = str_value_map[i].id;
  1361. else
  1362. ctx->p1 = str_value_map[i].id;
  1363. ctx->p2 = NULL;
  1364. }
  1365. return 1;
  1366. }
  1367. /*-
  1368. * Payload getters
  1369. * ===============
  1370. *
  1371. * These all get the data they want, then call default_fixup_args() as
  1372. * a post-ctrl GET fixup. They all get NULL ctx, ctrl_cmd, ctrl_str,
  1373. * p1, sz
  1374. */
  1375. /* Pilfering DH, DSA and EC_KEY */
  1376. static int get_payload_group_name(enum state state,
  1377. const struct translation_st *translation,
  1378. struct translation_ctx_st *ctx)
  1379. {
  1380. EVP_PKEY *pkey = ctx->p2;
  1381. ctx->p2 = NULL;
  1382. switch (EVP_PKEY_get_base_id(pkey)) {
  1383. #ifndef OPENSSL_NO_DH
  1384. case EVP_PKEY_DH:
  1385. {
  1386. const DH *dh = EVP_PKEY_get0_DH(pkey);
  1387. int uid = DH_get_nid(dh);
  1388. if (uid != NID_undef) {
  1389. const DH_NAMED_GROUP *dh_group =
  1390. ossl_ffc_uid_to_dh_named_group(uid);
  1391. ctx->p2 = (char *)ossl_ffc_named_group_get_name(dh_group);
  1392. }
  1393. }
  1394. break;
  1395. #endif
  1396. #ifndef OPENSSL_NO_EC
  1397. case EVP_PKEY_EC:
  1398. {
  1399. const EC_GROUP *grp =
  1400. EC_KEY_get0_group(EVP_PKEY_get0_EC_KEY(pkey));
  1401. int nid = NID_undef;
  1402. if (grp != NULL)
  1403. nid = EC_GROUP_get_curve_name(grp);
  1404. if (nid != NID_undef)
  1405. ctx->p2 = (char *)OSSL_EC_curve_nid2name(nid);
  1406. }
  1407. break;
  1408. #endif
  1409. default:
  1410. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1411. return 0;
  1412. }
  1413. /*
  1414. * Quietly ignoring unknown groups matches the behaviour on the provider
  1415. * side.
  1416. */
  1417. if (ctx->p2 == NULL)
  1418. return 1;
  1419. ctx->p1 = strlen(ctx->p2);
  1420. return default_fixup_args(state, translation, ctx);
  1421. }
  1422. static int get_payload_private_key(enum state state,
  1423. const struct translation_st *translation,
  1424. struct translation_ctx_st *ctx)
  1425. {
  1426. EVP_PKEY *pkey = ctx->p2;
  1427. ctx->p2 = NULL;
  1428. if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  1429. return 0;
  1430. switch (EVP_PKEY_get_base_id(pkey)) {
  1431. #ifndef OPENSSL_NO_DH
  1432. case EVP_PKEY_DH:
  1433. {
  1434. const DH *dh = EVP_PKEY_get0_DH(pkey);
  1435. ctx->p2 = (BIGNUM *)DH_get0_priv_key(dh);
  1436. }
  1437. break;
  1438. #endif
  1439. #ifndef OPENSSL_NO_EC
  1440. case EVP_PKEY_EC:
  1441. {
  1442. const EC_KEY *ec = EVP_PKEY_get0_EC_KEY(pkey);
  1443. ctx->p2 = (BIGNUM *)EC_KEY_get0_private_key(ec);
  1444. }
  1445. break;
  1446. #endif
  1447. default:
  1448. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1449. return 0;
  1450. }
  1451. return default_fixup_args(state, translation, ctx);
  1452. }
  1453. static int get_payload_public_key(enum state state,
  1454. const struct translation_st *translation,
  1455. struct translation_ctx_st *ctx)
  1456. {
  1457. EVP_PKEY *pkey = ctx->p2;
  1458. unsigned char *buf = NULL;
  1459. int ret;
  1460. ctx->p2 = NULL;
  1461. switch (EVP_PKEY_get_base_id(pkey)) {
  1462. #ifndef OPENSSL_NO_DH
  1463. case EVP_PKEY_DHX:
  1464. case EVP_PKEY_DH:
  1465. switch (ctx->params->data_type) {
  1466. case OSSL_PARAM_OCTET_STRING:
  1467. ctx->sz = ossl_dh_key2buf(EVP_PKEY_get0_DH(pkey), &buf, 0, 1);
  1468. ctx->p2 = buf;
  1469. break;
  1470. case OSSL_PARAM_UNSIGNED_INTEGER:
  1471. ctx->p2 = (void *)DH_get0_pub_key(EVP_PKEY_get0_DH(pkey));
  1472. break;
  1473. default:
  1474. return 0;
  1475. }
  1476. break;
  1477. #endif
  1478. #ifndef OPENSSL_NO_DSA
  1479. case EVP_PKEY_DSA:
  1480. if (ctx->params->data_type == OSSL_PARAM_UNSIGNED_INTEGER) {
  1481. ctx->p2 = (void *)DSA_get0_pub_key(EVP_PKEY_get0_DSA(pkey));
  1482. break;
  1483. }
  1484. return 0;
  1485. #endif
  1486. #ifndef OPENSSL_NO_EC
  1487. case EVP_PKEY_EC:
  1488. if (ctx->params->data_type == OSSL_PARAM_OCTET_STRING) {
  1489. const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
  1490. BN_CTX *bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
  1491. const EC_GROUP *ecg = EC_KEY_get0_group(eckey);
  1492. const EC_POINT *point = EC_KEY_get0_public_key(eckey);
  1493. if (bnctx == NULL)
  1494. return 0;
  1495. ctx->sz = EC_POINT_point2buf(ecg, point,
  1496. POINT_CONVERSION_COMPRESSED,
  1497. &buf, bnctx);
  1498. ctx->p2 = buf;
  1499. BN_CTX_free(bnctx);
  1500. break;
  1501. }
  1502. return 0;
  1503. #endif
  1504. default:
  1505. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1506. return 0;
  1507. }
  1508. ret = default_fixup_args(state, translation, ctx);
  1509. OPENSSL_free(buf);
  1510. return ret;
  1511. }
  1512. static int get_payload_public_key_ec(enum state state,
  1513. const struct translation_st *translation,
  1514. struct translation_ctx_st *ctx)
  1515. {
  1516. #ifndef OPENSSL_NO_EC
  1517. EVP_PKEY *pkey = ctx->p2;
  1518. const EC_KEY *eckey = EVP_PKEY_get0_EC_KEY(pkey);
  1519. BN_CTX *bnctx;
  1520. const EC_POINT *point;
  1521. const EC_GROUP *ecg;
  1522. BIGNUM *x = NULL;
  1523. BIGNUM *y = NULL;
  1524. int ret = 0;
  1525. ctx->p2 = NULL;
  1526. if (eckey == NULL) {
  1527. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1528. return 0;
  1529. }
  1530. bnctx = BN_CTX_new_ex(ossl_ec_key_get_libctx(eckey));
  1531. if (bnctx == NULL)
  1532. return 0;
  1533. point = EC_KEY_get0_public_key(eckey);
  1534. ecg = EC_KEY_get0_group(eckey);
  1535. /* Caller should have requested a BN, fail if not */
  1536. if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  1537. goto out;
  1538. x = BN_CTX_get(bnctx);
  1539. y = BN_CTX_get(bnctx);
  1540. if (y == NULL)
  1541. goto out;
  1542. if (!EC_POINT_get_affine_coordinates(ecg, point, x, y, bnctx))
  1543. goto out;
  1544. if (strncmp(ctx->params->key, OSSL_PKEY_PARAM_EC_PUB_X, 2) == 0)
  1545. ctx->p2 = x;
  1546. else if (strncmp(ctx->params->key, OSSL_PKEY_PARAM_EC_PUB_Y, 2) == 0)
  1547. ctx->p2 = y;
  1548. else
  1549. goto out;
  1550. /* Return the payload */
  1551. ret = default_fixup_args(state, translation, ctx);
  1552. out:
  1553. BN_CTX_free(bnctx);
  1554. return ret;
  1555. #else
  1556. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1557. return 0;
  1558. #endif
  1559. }
  1560. static int get_payload_bn(enum state state,
  1561. const struct translation_st *translation,
  1562. struct translation_ctx_st *ctx, const BIGNUM *bn)
  1563. {
  1564. if (bn == NULL)
  1565. return 0;
  1566. if (ctx->params->data_type != OSSL_PARAM_UNSIGNED_INTEGER)
  1567. return 0;
  1568. ctx->p2 = (BIGNUM *)bn;
  1569. return default_fixup_args(state, translation, ctx);
  1570. }
  1571. static int get_dh_dsa_payload_p(enum state state,
  1572. const struct translation_st *translation,
  1573. struct translation_ctx_st *ctx)
  1574. {
  1575. const BIGNUM *bn = NULL;
  1576. EVP_PKEY *pkey = ctx->p2;
  1577. switch (EVP_PKEY_get_base_id(pkey)) {
  1578. #ifndef OPENSSL_NO_DH
  1579. case EVP_PKEY_DH:
  1580. bn = DH_get0_p(EVP_PKEY_get0_DH(pkey));
  1581. break;
  1582. #endif
  1583. #ifndef OPENSSL_NO_DSA
  1584. case EVP_PKEY_DSA:
  1585. bn = DSA_get0_p(EVP_PKEY_get0_DSA(pkey));
  1586. break;
  1587. #endif
  1588. default:
  1589. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1590. }
  1591. return get_payload_bn(state, translation, ctx, bn);
  1592. }
  1593. static int get_dh_dsa_payload_q(enum state state,
  1594. const struct translation_st *translation,
  1595. struct translation_ctx_st *ctx)
  1596. {
  1597. const BIGNUM *bn = NULL;
  1598. switch (EVP_PKEY_get_base_id(ctx->p2)) {
  1599. #ifndef OPENSSL_NO_DH
  1600. case EVP_PKEY_DH:
  1601. bn = DH_get0_q(EVP_PKEY_get0_DH(ctx->p2));
  1602. break;
  1603. #endif
  1604. #ifndef OPENSSL_NO_DSA
  1605. case EVP_PKEY_DSA:
  1606. bn = DSA_get0_q(EVP_PKEY_get0_DSA(ctx->p2));
  1607. break;
  1608. #endif
  1609. }
  1610. return get_payload_bn(state, translation, ctx, bn);
  1611. }
  1612. static int get_dh_dsa_payload_g(enum state state,
  1613. const struct translation_st *translation,
  1614. struct translation_ctx_st *ctx)
  1615. {
  1616. const BIGNUM *bn = NULL;
  1617. switch (EVP_PKEY_get_base_id(ctx->p2)) {
  1618. #ifndef OPENSSL_NO_DH
  1619. case EVP_PKEY_DH:
  1620. bn = DH_get0_g(EVP_PKEY_get0_DH(ctx->p2));
  1621. break;
  1622. #endif
  1623. #ifndef OPENSSL_NO_DSA
  1624. case EVP_PKEY_DSA:
  1625. bn = DSA_get0_g(EVP_PKEY_get0_DSA(ctx->p2));
  1626. break;
  1627. #endif
  1628. }
  1629. return get_payload_bn(state, translation, ctx, bn);
  1630. }
  1631. static int get_payload_int(enum state state,
  1632. const struct translation_st *translation,
  1633. struct translation_ctx_st *ctx,
  1634. const int val)
  1635. {
  1636. if (ctx->params->data_type != OSSL_PARAM_INTEGER)
  1637. return 0;
  1638. ctx->p1 = val;
  1639. ctx->p2 = NULL;
  1640. return default_fixup_args(state, translation, ctx);
  1641. }
  1642. static int get_ec_decoded_from_explicit_params(enum state state,
  1643. const struct translation_st *translation,
  1644. struct translation_ctx_st *ctx)
  1645. {
  1646. int val = 0;
  1647. EVP_PKEY *pkey = ctx->p2;
  1648. switch (EVP_PKEY_base_id(pkey)) {
  1649. #ifndef OPENSSL_NO_EC
  1650. case EVP_PKEY_EC:
  1651. val = EC_KEY_decoded_from_explicit_params(EVP_PKEY_get0_EC_KEY(pkey));
  1652. if (val < 0) {
  1653. ERR_raise(ERR_LIB_EVP, EVP_R_INVALID_KEY);
  1654. return 0;
  1655. }
  1656. break;
  1657. #endif
  1658. default:
  1659. ERR_raise(ERR_LIB_EVP, EVP_R_UNSUPPORTED_KEY_TYPE);
  1660. return 0;
  1661. }
  1662. return get_payload_int(state, translation, ctx, val);
  1663. }
  1664. static int get_rsa_payload_n(enum state state,
  1665. const struct translation_st *translation,
  1666. struct translation_ctx_st *ctx)
  1667. {
  1668. const BIGNUM *bn = NULL;
  1669. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
  1670. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
  1671. return 0;
  1672. bn = RSA_get0_n(EVP_PKEY_get0_RSA(ctx->p2));
  1673. return get_payload_bn(state, translation, ctx, bn);
  1674. }
  1675. static int get_rsa_payload_e(enum state state,
  1676. const struct translation_st *translation,
  1677. struct translation_ctx_st *ctx)
  1678. {
  1679. const BIGNUM *bn = NULL;
  1680. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
  1681. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
  1682. return 0;
  1683. bn = RSA_get0_e(EVP_PKEY_get0_RSA(ctx->p2));
  1684. return get_payload_bn(state, translation, ctx, bn);
  1685. }
  1686. static int get_rsa_payload_d(enum state state,
  1687. const struct translation_st *translation,
  1688. struct translation_ctx_st *ctx)
  1689. {
  1690. const BIGNUM *bn = NULL;
  1691. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA
  1692. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS)
  1693. return 0;
  1694. bn = RSA_get0_d(EVP_PKEY_get0_RSA(ctx->p2));
  1695. return get_payload_bn(state, translation, ctx, bn);
  1696. }
  1697. static int get_rsa_payload_factor(enum state state,
  1698. const struct translation_st *translation,
  1699. struct translation_ctx_st *ctx,
  1700. size_t factornum)
  1701. {
  1702. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1703. const BIGNUM *bn = NULL;
  1704. switch (factornum) {
  1705. case 0:
  1706. bn = RSA_get0_p(r);
  1707. break;
  1708. case 1:
  1709. bn = RSA_get0_q(r);
  1710. break;
  1711. default:
  1712. {
  1713. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1714. const BIGNUM *factors[10];
  1715. if (factornum - 2 < pnum
  1716. && RSA_get0_multi_prime_factors(r, factors))
  1717. bn = factors[factornum - 2];
  1718. }
  1719. break;
  1720. }
  1721. return get_payload_bn(state, translation, ctx, bn);
  1722. }
  1723. static int get_rsa_payload_exponent(enum state state,
  1724. const struct translation_st *translation,
  1725. struct translation_ctx_st *ctx,
  1726. size_t exponentnum)
  1727. {
  1728. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1729. const BIGNUM *bn = NULL;
  1730. switch (exponentnum) {
  1731. case 0:
  1732. bn = RSA_get0_dmp1(r);
  1733. break;
  1734. case 1:
  1735. bn = RSA_get0_dmq1(r);
  1736. break;
  1737. default:
  1738. {
  1739. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1740. const BIGNUM *exps[10], *coeffs[10];
  1741. if (exponentnum - 2 < pnum
  1742. && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
  1743. bn = exps[exponentnum - 2];
  1744. }
  1745. break;
  1746. }
  1747. return get_payload_bn(state, translation, ctx, bn);
  1748. }
  1749. static int get_rsa_payload_coefficient(enum state state,
  1750. const struct translation_st *translation,
  1751. struct translation_ctx_st *ctx,
  1752. size_t coefficientnum)
  1753. {
  1754. const RSA *r = EVP_PKEY_get0_RSA(ctx->p2);
  1755. const BIGNUM *bn = NULL;
  1756. switch (coefficientnum) {
  1757. case 0:
  1758. bn = RSA_get0_iqmp(r);
  1759. break;
  1760. default:
  1761. {
  1762. size_t pnum = RSA_get_multi_prime_extra_count(r);
  1763. const BIGNUM *exps[10], *coeffs[10];
  1764. if (coefficientnum - 1 < pnum
  1765. && RSA_get0_multi_prime_crt_params(r, exps, coeffs))
  1766. bn = coeffs[coefficientnum - 1];
  1767. }
  1768. break;
  1769. }
  1770. return get_payload_bn(state, translation, ctx, bn);
  1771. }
  1772. #define IMPL_GET_RSA_PAYLOAD_FACTOR(n) \
  1773. static int \
  1774. get_rsa_payload_f##n(enum state state, \
  1775. const struct translation_st *translation, \
  1776. struct translation_ctx_st *ctx) \
  1777. { \
  1778. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA \
  1779. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS) \
  1780. return 0; \
  1781. return get_rsa_payload_factor(state, translation, ctx, n - 1); \
  1782. }
  1783. #define IMPL_GET_RSA_PAYLOAD_EXPONENT(n) \
  1784. static int \
  1785. get_rsa_payload_e##n(enum state state, \
  1786. const struct translation_st *translation, \
  1787. struct translation_ctx_st *ctx) \
  1788. { \
  1789. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA \
  1790. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS) \
  1791. return 0; \
  1792. return get_rsa_payload_exponent(state, translation, ctx, \
  1793. n - 1); \
  1794. }
  1795. #define IMPL_GET_RSA_PAYLOAD_COEFFICIENT(n) \
  1796. static int \
  1797. get_rsa_payload_c##n(enum state state, \
  1798. const struct translation_st *translation, \
  1799. struct translation_ctx_st *ctx) \
  1800. { \
  1801. if (EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA \
  1802. && EVP_PKEY_get_base_id(ctx->p2) != EVP_PKEY_RSA_PSS) \
  1803. return 0; \
  1804. return get_rsa_payload_coefficient(state, translation, ctx, \
  1805. n - 1); \
  1806. }
  1807. IMPL_GET_RSA_PAYLOAD_FACTOR(1)
  1808. IMPL_GET_RSA_PAYLOAD_FACTOR(2)
  1809. IMPL_GET_RSA_PAYLOAD_FACTOR(3)
  1810. IMPL_GET_RSA_PAYLOAD_FACTOR(4)
  1811. IMPL_GET_RSA_PAYLOAD_FACTOR(5)
  1812. IMPL_GET_RSA_PAYLOAD_FACTOR(6)
  1813. IMPL_GET_RSA_PAYLOAD_FACTOR(7)
  1814. IMPL_GET_RSA_PAYLOAD_FACTOR(8)
  1815. IMPL_GET_RSA_PAYLOAD_FACTOR(9)
  1816. IMPL_GET_RSA_PAYLOAD_FACTOR(10)
  1817. IMPL_GET_RSA_PAYLOAD_EXPONENT(1)
  1818. IMPL_GET_RSA_PAYLOAD_EXPONENT(2)
  1819. IMPL_GET_RSA_PAYLOAD_EXPONENT(3)
  1820. IMPL_GET_RSA_PAYLOAD_EXPONENT(4)
  1821. IMPL_GET_RSA_PAYLOAD_EXPONENT(5)
  1822. IMPL_GET_RSA_PAYLOAD_EXPONENT(6)
  1823. IMPL_GET_RSA_PAYLOAD_EXPONENT(7)
  1824. IMPL_GET_RSA_PAYLOAD_EXPONENT(8)
  1825. IMPL_GET_RSA_PAYLOAD_EXPONENT(9)
  1826. IMPL_GET_RSA_PAYLOAD_EXPONENT(10)
  1827. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(1)
  1828. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(2)
  1829. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(3)
  1830. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(4)
  1831. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(5)
  1832. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(6)
  1833. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(7)
  1834. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(8)
  1835. IMPL_GET_RSA_PAYLOAD_COEFFICIENT(9)
  1836. static int fix_group_ecx(enum state state,
  1837. const struct translation_st *translation,
  1838. struct translation_ctx_st *ctx)
  1839. {
  1840. const char *value = NULL;
  1841. switch (state) {
  1842. case PRE_PARAMS_TO_CTRL:
  1843. if (!EVP_PKEY_CTX_IS_GEN_OP(ctx->pctx))
  1844. return 0;
  1845. ctx->action_type = NONE;
  1846. return 1;
  1847. case POST_PARAMS_TO_CTRL:
  1848. if (OSSL_PARAM_get_utf8_string_ptr(ctx->params, &value) == 0 ||
  1849. OPENSSL_strcasecmp(ctx->pctx->keytype, value) != 0) {
  1850. ERR_raise(ERR_LIB_EVP, ERR_R_PASSED_INVALID_ARGUMENT);
  1851. ctx->p1 = 0;
  1852. return 0;
  1853. }
  1854. ctx->p1 = 1;
  1855. return 1;
  1856. default:
  1857. return 0;
  1858. }
  1859. }
  1860. /*-
  1861. * The translation table itself
  1862. * ============================
  1863. */
  1864. static const struct translation_st evp_pkey_ctx_translations[] = {
  1865. /*
  1866. * DistID: we pass it to the backend as an octet string,
  1867. * but get it back as a pointer to an octet string.
  1868. *
  1869. * Note that the EVP_PKEY_CTRL_GET1_ID_LEN is purely for legacy purposes
  1870. * that has no separate counterpart in OSSL_PARAM terms, since we get
  1871. * the length of the DistID automatically when getting the DistID itself.
  1872. */
  1873. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  1874. EVP_PKEY_CTRL_SET1_ID, "distid", "hexdistid",
  1875. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_STRING, NULL },
  1876. { GET, -1, -1, -1,
  1877. EVP_PKEY_CTRL_GET1_ID, "distid", "hexdistid",
  1878. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, NULL },
  1879. { GET, -1, -1, -1,
  1880. EVP_PKEY_CTRL_GET1_ID_LEN, NULL, NULL,
  1881. OSSL_PKEY_PARAM_DIST_ID, OSSL_PARAM_OCTET_PTR, fix_distid_len },
  1882. /*-
  1883. * DH & DHX
  1884. * ========
  1885. */
  1886. /*
  1887. * EVP_PKEY_CTRL_DH_KDF_TYPE is used both for setting and getting. The
  1888. * fixup function has to handle this...
  1889. */
  1890. { NONE, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1891. EVP_PKEY_CTRL_DH_KDF_TYPE, NULL, NULL,
  1892. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING,
  1893. fix_dh_kdf_type },
  1894. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1895. EVP_PKEY_CTRL_DH_KDF_MD, NULL, NULL,
  1896. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1897. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1898. EVP_PKEY_CTRL_GET_DH_KDF_MD, NULL, NULL,
  1899. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1900. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1901. EVP_PKEY_CTRL_DH_KDF_OUTLEN, NULL, NULL,
  1902. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1903. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1904. EVP_PKEY_CTRL_GET_DH_KDF_OUTLEN, NULL, NULL,
  1905. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1906. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1907. EVP_PKEY_CTRL_DH_KDF_UKM, NULL, NULL,
  1908. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  1909. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1910. EVP_PKEY_CTRL_GET_DH_KDF_UKM, NULL, NULL,
  1911. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  1912. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1913. EVP_PKEY_CTRL_DH_KDF_OID, NULL, NULL,
  1914. OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
  1915. { GET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_DERIVE,
  1916. EVP_PKEY_CTRL_GET_DH_KDF_OID, NULL, NULL,
  1917. OSSL_KDF_PARAM_CEK_ALG, OSSL_PARAM_UTF8_STRING, fix_oid },
  1918. /* DHX Keygen Parameters that are shared with DH */
  1919. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1920. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
  1921. OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
  1922. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1923. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
  1924. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1925. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1926. EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
  1927. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, NULL },
  1928. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1929. EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
  1930. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
  1931. /* DH Keygen Parameters that are shared with DHX */
  1932. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1933. EVP_PKEY_CTRL_DH_PARAMGEN_TYPE, "dh_paramgen_type", NULL,
  1934. OSSL_PKEY_PARAM_FFC_TYPE, OSSL_PARAM_UTF8_STRING, fix_dh_paramgen_type },
  1935. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1936. EVP_PKEY_CTRL_DH_PARAMGEN_PRIME_LEN, "dh_paramgen_prime_len", NULL,
  1937. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1938. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1939. EVP_PKEY_CTRL_DH_NID, "dh_param", NULL,
  1940. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid },
  1941. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1942. EVP_PKEY_CTRL_DH_RFC5114, "dh_rfc5114", NULL,
  1943. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_dh_nid5114 },
  1944. /* DH specific Keygen Parameters */
  1945. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_PARAMGEN,
  1946. EVP_PKEY_CTRL_DH_PARAMGEN_GENERATOR, "dh_paramgen_generator", NULL,
  1947. OSSL_PKEY_PARAM_DH_GENERATOR, OSSL_PARAM_INTEGER, NULL },
  1948. /* DHX specific Keygen Parameters */
  1949. { SET, EVP_PKEY_DHX, 0, EVP_PKEY_OP_PARAMGEN,
  1950. EVP_PKEY_CTRL_DH_PARAMGEN_SUBPRIME_LEN, "dh_paramgen_subprime_len", NULL,
  1951. OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1952. { SET, EVP_PKEY_DH, 0, EVP_PKEY_OP_DERIVE,
  1953. EVP_PKEY_CTRL_DH_PAD, "dh_pad", NULL,
  1954. OSSL_EXCHANGE_PARAM_PAD, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1955. /*-
  1956. * DSA
  1957. * ===
  1958. */
  1959. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1960. EVP_PKEY_CTRL_DSA_PARAMGEN_BITS, "dsa_paramgen_bits", NULL,
  1961. OSSL_PKEY_PARAM_FFC_PBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1962. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1963. EVP_PKEY_CTRL_DSA_PARAMGEN_Q_BITS, "dsa_paramgen_q_bits", NULL,
  1964. OSSL_PKEY_PARAM_FFC_QBITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1965. { SET, EVP_PKEY_DSA, 0, EVP_PKEY_OP_PARAMGEN,
  1966. EVP_PKEY_CTRL_DSA_PARAMGEN_MD, "dsa_paramgen_md", NULL,
  1967. OSSL_PKEY_PARAM_FFC_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1968. /*-
  1969. * EC
  1970. * ==
  1971. */
  1972. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1973. EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
  1974. OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
  1975. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  1976. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
  1977. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  1978. fix_ec_paramgen_curve_nid },
  1979. /*
  1980. * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
  1981. * both for setting and getting. The fixup function has to handle this...
  1982. */
  1983. { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1984. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
  1985. OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
  1986. fix_ecdh_cofactor },
  1987. { NONE, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1988. EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
  1989. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
  1990. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1991. EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
  1992. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1993. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1994. EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
  1995. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  1996. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  1997. EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
  1998. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  1999. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  2000. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
  2001. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2002. { SET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  2003. EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
  2004. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  2005. { GET, EVP_PKEY_EC, 0, EVP_PKEY_OP_DERIVE,
  2006. EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
  2007. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  2008. /*-
  2009. * SM2
  2010. * ==
  2011. */
  2012. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  2013. EVP_PKEY_CTRL_EC_PARAM_ENC, "ec_param_enc", NULL,
  2014. OSSL_PKEY_PARAM_EC_ENCODING, OSSL_PARAM_UTF8_STRING, fix_ec_param_enc },
  2015. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_PARAMGEN | EVP_PKEY_OP_KEYGEN,
  2016. EVP_PKEY_CTRL_EC_PARAMGEN_CURVE_NID, "ec_paramgen_curve", NULL,
  2017. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  2018. fix_ec_paramgen_curve_nid },
  2019. /*
  2020. * EVP_PKEY_CTRL_EC_ECDH_COFACTOR and EVP_PKEY_CTRL_EC_KDF_TYPE are used
  2021. * both for setting and getting. The fixup function has to handle this...
  2022. */
  2023. { NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2024. EVP_PKEY_CTRL_EC_ECDH_COFACTOR, "ecdh_cofactor_mode", NULL,
  2025. OSSL_EXCHANGE_PARAM_EC_ECDH_COFACTOR_MODE, OSSL_PARAM_INTEGER,
  2026. fix_ecdh_cofactor },
  2027. { NONE, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2028. EVP_PKEY_CTRL_EC_KDF_TYPE, NULL, NULL,
  2029. OSSL_EXCHANGE_PARAM_KDF_TYPE, OSSL_PARAM_UTF8_STRING, fix_ec_kdf_type },
  2030. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2031. EVP_PKEY_CTRL_EC_KDF_MD, "ecdh_kdf_md", NULL,
  2032. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2033. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2034. EVP_PKEY_CTRL_GET_EC_KDF_MD, NULL, NULL,
  2035. OSSL_EXCHANGE_PARAM_KDF_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2036. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2037. EVP_PKEY_CTRL_EC_KDF_OUTLEN, NULL, NULL,
  2038. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2039. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2040. EVP_PKEY_CTRL_GET_EC_KDF_OUTLEN, NULL, NULL,
  2041. OSSL_EXCHANGE_PARAM_KDF_OUTLEN, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2042. { SET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2043. EVP_PKEY_CTRL_EC_KDF_UKM, NULL, NULL,
  2044. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_STRING, NULL },
  2045. { GET, EVP_PKEY_SM2, 0, EVP_PKEY_OP_DERIVE,
  2046. EVP_PKEY_CTRL_GET_EC_KDF_UKM, NULL, NULL,
  2047. OSSL_EXCHANGE_PARAM_KDF_UKM, OSSL_PARAM_OCTET_PTR, NULL },
  2048. /*-
  2049. * RSA
  2050. * ===
  2051. */
  2052. /*
  2053. * RSA padding modes are numeric with ctrls, strings with ctrl_strs,
  2054. * and can be both with OSSL_PARAM. We standardise on strings here,
  2055. * fix_rsa_padding_mode() does the work when the caller has a different
  2056. * idea.
  2057. */
  2058. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2059. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2060. EVP_PKEY_CTRL_RSA_PADDING, "rsa_padding_mode", NULL,
  2061. OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
  2062. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2063. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2064. EVP_PKEY_CTRL_GET_RSA_PADDING, NULL, NULL,
  2065. OSSL_PKEY_PARAM_PAD_MODE, OSSL_PARAM_UTF8_STRING, fix_rsa_padding_mode },
  2066. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2067. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2068. EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_mgf1_md", NULL,
  2069. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2070. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS,
  2071. EVP_PKEY_OP_TYPE_CRYPT | EVP_PKEY_OP_TYPE_SIG,
  2072. EVP_PKEY_CTRL_GET_RSA_MGF1_MD, NULL, NULL,
  2073. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2074. /*
  2075. * RSA-PSS saltlen is essentially numeric, but certain values can be
  2076. * expressed as keywords (strings) with ctrl_str. The corresponding
  2077. * OSSL_PARAM allows both forms.
  2078. * fix_rsa_pss_saltlen() takes care of the distinction.
  2079. */
  2080. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
  2081. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_saltlen", NULL,
  2082. OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
  2083. fix_rsa_pss_saltlen },
  2084. { GET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_TYPE_SIG,
  2085. EVP_PKEY_CTRL_GET_RSA_PSS_SALTLEN, NULL, NULL,
  2086. OSSL_PKEY_PARAM_RSA_PSS_SALTLEN, OSSL_PARAM_UTF8_STRING,
  2087. fix_rsa_pss_saltlen },
  2088. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2089. EVP_PKEY_CTRL_RSA_OAEP_MD, "rsa_oaep_md", NULL,
  2090. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2091. { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2092. EVP_PKEY_CTRL_GET_RSA_OAEP_MD, NULL, NULL,
  2093. OSSL_ASYM_CIPHER_PARAM_OAEP_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2094. /*
  2095. * The "rsa_oaep_label" ctrl_str expects the value to always be hex.
  2096. * This is accommodated by default_fixup_args() above, which mimics that
  2097. * expectation for any translation item where |ctrl_str| is NULL and
  2098. * |ctrl_hexstr| is non-NULL.
  2099. */
  2100. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2101. EVP_PKEY_CTRL_RSA_OAEP_LABEL, NULL, "rsa_oaep_label",
  2102. OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_STRING, NULL },
  2103. { GET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2104. EVP_PKEY_CTRL_GET_RSA_OAEP_LABEL, NULL, NULL,
  2105. OSSL_ASYM_CIPHER_PARAM_OAEP_LABEL, OSSL_PARAM_OCTET_PTR, NULL },
  2106. { SET, EVP_PKEY_RSA, 0, EVP_PKEY_OP_TYPE_CRYPT,
  2107. EVP_PKEY_CTRL_RSA_IMPLICIT_REJECTION, NULL,
  2108. "rsa_pkcs1_implicit_rejection",
  2109. OSSL_ASYM_CIPHER_PARAM_IMPLICIT_REJECTION, OSSL_PARAM_UNSIGNED_INTEGER,
  2110. NULL },
  2111. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2112. EVP_PKEY_CTRL_MD, "rsa_pss_keygen_md", NULL,
  2113. OSSL_ALG_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2114. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2115. EVP_PKEY_CTRL_RSA_MGF1_MD, "rsa_pss_keygen_mgf1_md", NULL,
  2116. OSSL_PKEY_PARAM_MGF1_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2117. { SET, EVP_PKEY_RSA_PSS, 0, EVP_PKEY_OP_TYPE_GEN,
  2118. EVP_PKEY_CTRL_RSA_PSS_SALTLEN, "rsa_pss_keygen_saltlen", NULL,
  2119. OSSL_SIGNATURE_PARAM_PSS_SALTLEN, OSSL_PARAM_INTEGER, NULL },
  2120. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  2121. EVP_PKEY_CTRL_RSA_KEYGEN_BITS, "rsa_keygen_bits", NULL,
  2122. OSSL_PKEY_PARAM_RSA_BITS, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2123. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  2124. EVP_PKEY_CTRL_RSA_KEYGEN_PUBEXP, "rsa_keygen_pubexp", NULL,
  2125. OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2126. { SET, EVP_PKEY_RSA, EVP_PKEY_RSA_PSS, EVP_PKEY_OP_KEYGEN,
  2127. EVP_PKEY_CTRL_RSA_KEYGEN_PRIMES, "rsa_keygen_primes", NULL,
  2128. OSSL_PKEY_PARAM_RSA_PRIMES, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2129. /*-
  2130. * SipHash
  2131. * ======
  2132. */
  2133. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2134. EVP_PKEY_CTRL_SET_DIGEST_SIZE, "digestsize", NULL,
  2135. OSSL_MAC_PARAM_SIZE, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2136. /*-
  2137. * TLS1-PRF
  2138. * ========
  2139. */
  2140. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2141. EVP_PKEY_CTRL_TLS_MD, "md", NULL,
  2142. OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2143. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2144. EVP_PKEY_CTRL_TLS_SECRET, "secret", "hexsecret",
  2145. OSSL_KDF_PARAM_SECRET, OSSL_PARAM_OCTET_STRING, NULL },
  2146. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2147. EVP_PKEY_CTRL_TLS_SEED, "seed", "hexseed",
  2148. OSSL_KDF_PARAM_SEED, OSSL_PARAM_OCTET_STRING, NULL },
  2149. /*-
  2150. * HKDF
  2151. * ====
  2152. */
  2153. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2154. EVP_PKEY_CTRL_HKDF_MD, "md", NULL,
  2155. OSSL_KDF_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2156. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2157. EVP_PKEY_CTRL_HKDF_SALT, "salt", "hexsalt",
  2158. OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
  2159. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2160. EVP_PKEY_CTRL_HKDF_KEY, "key", "hexkey",
  2161. OSSL_KDF_PARAM_KEY, OSSL_PARAM_OCTET_STRING, NULL },
  2162. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2163. EVP_PKEY_CTRL_HKDF_INFO, "info", "hexinfo",
  2164. OSSL_KDF_PARAM_INFO, OSSL_PARAM_OCTET_STRING, NULL },
  2165. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2166. EVP_PKEY_CTRL_HKDF_MODE, "mode", NULL,
  2167. OSSL_KDF_PARAM_MODE, OSSL_PARAM_INTEGER, fix_hkdf_mode },
  2168. /*-
  2169. * Scrypt
  2170. * ======
  2171. */
  2172. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2173. EVP_PKEY_CTRL_PASS, "pass", "hexpass",
  2174. OSSL_KDF_PARAM_PASSWORD, OSSL_PARAM_OCTET_STRING, NULL },
  2175. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2176. EVP_PKEY_CTRL_SCRYPT_SALT, "salt", "hexsalt",
  2177. OSSL_KDF_PARAM_SALT, OSSL_PARAM_OCTET_STRING, NULL },
  2178. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2179. EVP_PKEY_CTRL_SCRYPT_N, "N", NULL,
  2180. OSSL_KDF_PARAM_SCRYPT_N, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2181. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2182. EVP_PKEY_CTRL_SCRYPT_R, "r", NULL,
  2183. OSSL_KDF_PARAM_SCRYPT_R, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2184. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2185. EVP_PKEY_CTRL_SCRYPT_P, "p", NULL,
  2186. OSSL_KDF_PARAM_SCRYPT_P, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2187. { SET, -1, -1, EVP_PKEY_OP_DERIVE,
  2188. EVP_PKEY_CTRL_SCRYPT_MAXMEM_BYTES, "maxmem_bytes", NULL,
  2189. OSSL_KDF_PARAM_SCRYPT_MAXMEM, OSSL_PARAM_UNSIGNED_INTEGER, NULL },
  2190. { SET, -1, -1, EVP_PKEY_OP_KEYGEN | EVP_PKEY_OP_TYPE_CRYPT,
  2191. EVP_PKEY_CTRL_CIPHER, NULL, NULL,
  2192. OSSL_PKEY_PARAM_CIPHER, OSSL_PARAM_UTF8_STRING, fix_cipher },
  2193. { SET, -1, -1, EVP_PKEY_OP_KEYGEN,
  2194. EVP_PKEY_CTRL_SET_MAC_KEY, "key", "hexkey",
  2195. OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_OCTET_STRING, NULL },
  2196. { SET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2197. EVP_PKEY_CTRL_MD, NULL, NULL,
  2198. OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2199. { GET, -1, -1, EVP_PKEY_OP_TYPE_SIG,
  2200. EVP_PKEY_CTRL_GET_MD, NULL, NULL,
  2201. OSSL_SIGNATURE_PARAM_DIGEST, OSSL_PARAM_UTF8_STRING, fix_md },
  2202. /*-
  2203. * ECX
  2204. * ===
  2205. */
  2206. { SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
  2207. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2208. { SET, EVP_PKEY_X25519, EVP_PKEY_X25519, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
  2209. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2210. { SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_KEYGEN, -1, NULL, NULL,
  2211. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2212. { SET, EVP_PKEY_X448, EVP_PKEY_X448, EVP_PKEY_OP_PARAMGEN, -1, NULL, NULL,
  2213. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING, fix_group_ecx },
  2214. };
  2215. static const struct translation_st evp_pkey_translations[] = {
  2216. /*
  2217. * The following contain no ctrls, they are exclusively here to extract
  2218. * key payloads from legacy keys, using OSSL_PARAMs, and rely entirely
  2219. * on |fixup_args| to pass the actual data. The |fixup_args| should
  2220. * expect to get the EVP_PKEY pointer through |ctx->p2|.
  2221. */
  2222. /* DH, DSA & EC */
  2223. { GET, -1, -1, -1, 0, NULL, NULL,
  2224. OSSL_PKEY_PARAM_GROUP_NAME, OSSL_PARAM_UTF8_STRING,
  2225. get_payload_group_name },
  2226. { GET, -1, -1, -1, 0, NULL, NULL,
  2227. OSSL_PKEY_PARAM_PRIV_KEY, OSSL_PARAM_UNSIGNED_INTEGER,
  2228. get_payload_private_key },
  2229. { GET, -1, -1, -1, 0, NULL, NULL,
  2230. OSSL_PKEY_PARAM_PUB_KEY,
  2231. 0 /* no data type, let get_payload_public_key() handle that */,
  2232. get_payload_public_key },
  2233. { GET, -1, -1, -1, 0, NULL, NULL,
  2234. OSSL_PKEY_PARAM_EC_PUB_X, OSSL_PARAM_UNSIGNED_INTEGER,
  2235. get_payload_public_key_ec },
  2236. { GET, -1, -1, -1, 0, NULL, NULL,
  2237. OSSL_PKEY_PARAM_EC_PUB_Y, OSSL_PARAM_UNSIGNED_INTEGER,
  2238. get_payload_public_key_ec },
  2239. /* DH and DSA */
  2240. { GET, -1, -1, -1, 0, NULL, NULL,
  2241. OSSL_PKEY_PARAM_FFC_P, OSSL_PARAM_UNSIGNED_INTEGER,
  2242. get_dh_dsa_payload_p },
  2243. { GET, -1, -1, -1, 0, NULL, NULL,
  2244. OSSL_PKEY_PARAM_FFC_G, OSSL_PARAM_UNSIGNED_INTEGER,
  2245. get_dh_dsa_payload_g },
  2246. { GET, -1, -1, -1, 0, NULL, NULL,
  2247. OSSL_PKEY_PARAM_FFC_Q, OSSL_PARAM_UNSIGNED_INTEGER,
  2248. get_dh_dsa_payload_q },
  2249. /* RSA */
  2250. { GET, -1, -1, -1, 0, NULL, NULL,
  2251. OSSL_PKEY_PARAM_RSA_N, OSSL_PARAM_UNSIGNED_INTEGER,
  2252. get_rsa_payload_n },
  2253. { GET, -1, -1, -1, 0, NULL, NULL,
  2254. OSSL_PKEY_PARAM_RSA_E, OSSL_PARAM_UNSIGNED_INTEGER,
  2255. get_rsa_payload_e },
  2256. { GET, -1, -1, -1, 0, NULL, NULL,
  2257. OSSL_PKEY_PARAM_RSA_D, OSSL_PARAM_UNSIGNED_INTEGER,
  2258. get_rsa_payload_d },
  2259. { GET, -1, -1, -1, 0, NULL, NULL,
  2260. OSSL_PKEY_PARAM_RSA_FACTOR1, OSSL_PARAM_UNSIGNED_INTEGER,
  2261. get_rsa_payload_f1 },
  2262. { GET, -1, -1, -1, 0, NULL, NULL,
  2263. OSSL_PKEY_PARAM_RSA_FACTOR2, OSSL_PARAM_UNSIGNED_INTEGER,
  2264. get_rsa_payload_f2 },
  2265. { GET, -1, -1, -1, 0, NULL, NULL,
  2266. OSSL_PKEY_PARAM_RSA_FACTOR3, OSSL_PARAM_UNSIGNED_INTEGER,
  2267. get_rsa_payload_f3 },
  2268. { GET, -1, -1, -1, 0, NULL, NULL,
  2269. OSSL_PKEY_PARAM_RSA_FACTOR4, OSSL_PARAM_UNSIGNED_INTEGER,
  2270. get_rsa_payload_f4 },
  2271. { GET, -1, -1, -1, 0, NULL, NULL,
  2272. OSSL_PKEY_PARAM_RSA_FACTOR5, OSSL_PARAM_UNSIGNED_INTEGER,
  2273. get_rsa_payload_f5 },
  2274. { GET, -1, -1, -1, 0, NULL, NULL,
  2275. OSSL_PKEY_PARAM_RSA_FACTOR6, OSSL_PARAM_UNSIGNED_INTEGER,
  2276. get_rsa_payload_f6 },
  2277. { GET, -1, -1, -1, 0, NULL, NULL,
  2278. OSSL_PKEY_PARAM_RSA_FACTOR7, OSSL_PARAM_UNSIGNED_INTEGER,
  2279. get_rsa_payload_f7 },
  2280. { GET, -1, -1, -1, 0, NULL, NULL,
  2281. OSSL_PKEY_PARAM_RSA_FACTOR8, OSSL_PARAM_UNSIGNED_INTEGER,
  2282. get_rsa_payload_f8 },
  2283. { GET, -1, -1, -1, 0, NULL, NULL,
  2284. OSSL_PKEY_PARAM_RSA_FACTOR9, OSSL_PARAM_UNSIGNED_INTEGER,
  2285. get_rsa_payload_f9 },
  2286. { GET, -1, -1, -1, 0, NULL, NULL,
  2287. OSSL_PKEY_PARAM_RSA_FACTOR10, OSSL_PARAM_UNSIGNED_INTEGER,
  2288. get_rsa_payload_f10 },
  2289. { GET, -1, -1, -1, 0, NULL, NULL,
  2290. OSSL_PKEY_PARAM_RSA_EXPONENT1, OSSL_PARAM_UNSIGNED_INTEGER,
  2291. get_rsa_payload_e1 },
  2292. { GET, -1, -1, -1, 0, NULL, NULL,
  2293. OSSL_PKEY_PARAM_RSA_EXPONENT2, OSSL_PARAM_UNSIGNED_INTEGER,
  2294. get_rsa_payload_e2 },
  2295. { GET, -1, -1, -1, 0, NULL, NULL,
  2296. OSSL_PKEY_PARAM_RSA_EXPONENT3, OSSL_PARAM_UNSIGNED_INTEGER,
  2297. get_rsa_payload_e3 },
  2298. { GET, -1, -1, -1, 0, NULL, NULL,
  2299. OSSL_PKEY_PARAM_RSA_EXPONENT4, OSSL_PARAM_UNSIGNED_INTEGER,
  2300. get_rsa_payload_e4 },
  2301. { GET, -1, -1, -1, 0, NULL, NULL,
  2302. OSSL_PKEY_PARAM_RSA_EXPONENT5, OSSL_PARAM_UNSIGNED_INTEGER,
  2303. get_rsa_payload_e5 },
  2304. { GET, -1, -1, -1, 0, NULL, NULL,
  2305. OSSL_PKEY_PARAM_RSA_EXPONENT6, OSSL_PARAM_UNSIGNED_INTEGER,
  2306. get_rsa_payload_e6 },
  2307. { GET, -1, -1, -1, 0, NULL, NULL,
  2308. OSSL_PKEY_PARAM_RSA_EXPONENT7, OSSL_PARAM_UNSIGNED_INTEGER,
  2309. get_rsa_payload_e7 },
  2310. { GET, -1, -1, -1, 0, NULL, NULL,
  2311. OSSL_PKEY_PARAM_RSA_EXPONENT8, OSSL_PARAM_UNSIGNED_INTEGER,
  2312. get_rsa_payload_e8 },
  2313. { GET, -1, -1, -1, 0, NULL, NULL,
  2314. OSSL_PKEY_PARAM_RSA_EXPONENT9, OSSL_PARAM_UNSIGNED_INTEGER,
  2315. get_rsa_payload_e9 },
  2316. { GET, -1, -1, -1, 0, NULL, NULL,
  2317. OSSL_PKEY_PARAM_RSA_EXPONENT10, OSSL_PARAM_UNSIGNED_INTEGER,
  2318. get_rsa_payload_e10 },
  2319. { GET, -1, -1, -1, 0, NULL, NULL,
  2320. OSSL_PKEY_PARAM_RSA_COEFFICIENT1, OSSL_PARAM_UNSIGNED_INTEGER,
  2321. get_rsa_payload_c1 },
  2322. { GET, -1, -1, -1, 0, NULL, NULL,
  2323. OSSL_PKEY_PARAM_RSA_COEFFICIENT2, OSSL_PARAM_UNSIGNED_INTEGER,
  2324. get_rsa_payload_c2 },
  2325. { GET, -1, -1, -1, 0, NULL, NULL,
  2326. OSSL_PKEY_PARAM_RSA_COEFFICIENT3, OSSL_PARAM_UNSIGNED_INTEGER,
  2327. get_rsa_payload_c3 },
  2328. { GET, -1, -1, -1, 0, NULL, NULL,
  2329. OSSL_PKEY_PARAM_RSA_COEFFICIENT4, OSSL_PARAM_UNSIGNED_INTEGER,
  2330. get_rsa_payload_c4 },
  2331. { GET, -1, -1, -1, 0, NULL, NULL,
  2332. OSSL_PKEY_PARAM_RSA_COEFFICIENT5, OSSL_PARAM_UNSIGNED_INTEGER,
  2333. get_rsa_payload_c5 },
  2334. { GET, -1, -1, -1, 0, NULL, NULL,
  2335. OSSL_PKEY_PARAM_RSA_COEFFICIENT6, OSSL_PARAM_UNSIGNED_INTEGER,
  2336. get_rsa_payload_c6 },
  2337. { GET, -1, -1, -1, 0, NULL, NULL,
  2338. OSSL_PKEY_PARAM_RSA_COEFFICIENT7, OSSL_PARAM_UNSIGNED_INTEGER,
  2339. get_rsa_payload_c7 },
  2340. { GET, -1, -1, -1, 0, NULL, NULL,
  2341. OSSL_PKEY_PARAM_RSA_COEFFICIENT8, OSSL_PARAM_UNSIGNED_INTEGER,
  2342. get_rsa_payload_c8 },
  2343. { GET, -1, -1, -1, 0, NULL, NULL,
  2344. OSSL_PKEY_PARAM_RSA_COEFFICIENT9, OSSL_PARAM_UNSIGNED_INTEGER,
  2345. get_rsa_payload_c9 },
  2346. /* EC */
  2347. { GET, -1, -1, -1, 0, NULL, NULL,
  2348. OSSL_PKEY_PARAM_EC_DECODED_FROM_EXPLICIT_PARAMS, OSSL_PARAM_INTEGER,
  2349. get_ec_decoded_from_explicit_params },
  2350. };
  2351. static const struct translation_st *
  2352. lookup_translation(struct translation_st *tmpl,
  2353. const struct translation_st *translations,
  2354. size_t translations_num)
  2355. {
  2356. size_t i;
  2357. for (i = 0; i < translations_num; i++) {
  2358. const struct translation_st *item = &translations[i];
  2359. /*
  2360. * Sanity check the translation table item.
  2361. *
  2362. * 1. Either both keytypes are -1, or neither of them are.
  2363. * 2. TBA...
  2364. */
  2365. if (!ossl_assert((item->keytype1 == -1) == (item->keytype2 == -1)))
  2366. continue;
  2367. /*
  2368. * Base search criteria: check that the optype and keytypes match,
  2369. * if relevant. All callers must synthesise these bits somehow.
  2370. */
  2371. if (item->optype != -1 && (tmpl->optype & item->optype) == 0)
  2372. continue;
  2373. /*
  2374. * This expression is stunningly simple thanks to the sanity check
  2375. * above.
  2376. */
  2377. if (item->keytype1 != -1
  2378. && tmpl->keytype1 != item->keytype1
  2379. && tmpl->keytype2 != item->keytype2)
  2380. continue;
  2381. /*
  2382. * Done with the base search criteria, now we check the criteria for
  2383. * the individual types of translations:
  2384. * ctrl->params, ctrl_str->params, and params->ctrl
  2385. */
  2386. if (tmpl->ctrl_num != 0) {
  2387. if (tmpl->ctrl_num != item->ctrl_num)
  2388. continue;
  2389. } else if (tmpl->ctrl_str != NULL) {
  2390. const char *ctrl_str = NULL;
  2391. const char *ctrl_hexstr = NULL;
  2392. /*
  2393. * Search criteria that originates from a ctrl_str is only used
  2394. * for setting, never for getting. Therefore, we only look at
  2395. * the setter items.
  2396. */
  2397. if (item->action_type != NONE
  2398. && item->action_type != SET)
  2399. continue;
  2400. /*
  2401. * At least one of the ctrl cmd names must be match the ctrl
  2402. * cmd name in the template.
  2403. */
  2404. if (item->ctrl_str != NULL
  2405. && OPENSSL_strcasecmp(tmpl->ctrl_str, item->ctrl_str) == 0)
  2406. ctrl_str = tmpl->ctrl_str;
  2407. else if (item->ctrl_hexstr != NULL
  2408. && OPENSSL_strcasecmp(tmpl->ctrl_hexstr,
  2409. item->ctrl_hexstr) == 0)
  2410. ctrl_hexstr = tmpl->ctrl_hexstr;
  2411. else
  2412. continue;
  2413. /* Modify the template to signal which string matched */
  2414. tmpl->ctrl_str = ctrl_str;
  2415. tmpl->ctrl_hexstr = ctrl_hexstr;
  2416. } else if (tmpl->param_key != NULL) {
  2417. /*
  2418. * Search criteria that originates from an OSSL_PARAM setter or
  2419. * getter.
  2420. *
  2421. * Ctrls were fundamentally bidirectional, with only the ctrl
  2422. * command macro name implying direction (if you're lucky).
  2423. * A few ctrl commands were even taking advantage of the
  2424. * bidirectional nature, making the direction depend in the
  2425. * value of the numeric argument.
  2426. *
  2427. * OSSL_PARAM functions are fundamentally different, in that
  2428. * setters and getters are separated, so the data direction is
  2429. * implied by the function that's used. The same OSSL_PARAM
  2430. * key name can therefore be used in both directions. We must
  2431. * therefore take the action type into account in this case.
  2432. */
  2433. if ((item->action_type != NONE
  2434. && tmpl->action_type != item->action_type)
  2435. || (item->param_key != NULL
  2436. && OPENSSL_strcasecmp(tmpl->param_key,
  2437. item->param_key) != 0))
  2438. continue;
  2439. } else {
  2440. return NULL;
  2441. }
  2442. return item;
  2443. }
  2444. return NULL;
  2445. }
  2446. static const struct translation_st *
  2447. lookup_evp_pkey_ctx_translation(struct translation_st *tmpl)
  2448. {
  2449. return lookup_translation(tmpl, evp_pkey_ctx_translations,
  2450. OSSL_NELEM(evp_pkey_ctx_translations));
  2451. }
  2452. static const struct translation_st *
  2453. lookup_evp_pkey_translation(struct translation_st *tmpl)
  2454. {
  2455. return lookup_translation(tmpl, evp_pkey_translations,
  2456. OSSL_NELEM(evp_pkey_translations));
  2457. }
  2458. /* This must ONLY be called for provider side operations */
  2459. int evp_pkey_ctx_ctrl_to_param(EVP_PKEY_CTX *pctx,
  2460. int keytype, int optype,
  2461. int cmd, int p1, void *p2)
  2462. {
  2463. struct translation_ctx_st ctx = { 0, };
  2464. struct translation_st tmpl = { 0, };
  2465. const struct translation_st *translation = NULL;
  2466. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  2467. int ret;
  2468. fixup_args_fn *fixup = default_fixup_args;
  2469. if (keytype == -1)
  2470. keytype = pctx->legacy_keytype;
  2471. tmpl.ctrl_num = cmd;
  2472. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2473. tmpl.optype = optype;
  2474. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2475. if (translation == NULL) {
  2476. ERR_raise(ERR_LIB_EVP, EVP_R_COMMAND_NOT_SUPPORTED);
  2477. return -2;
  2478. }
  2479. if (pctx->pmeth != NULL
  2480. && pctx->pmeth->pkey_id != translation->keytype1
  2481. && pctx->pmeth->pkey_id != translation->keytype2)
  2482. return -1;
  2483. if (translation->fixup_args != NULL)
  2484. fixup = translation->fixup_args;
  2485. ctx.action_type = translation->action_type;
  2486. ctx.ctrl_cmd = cmd;
  2487. ctx.p1 = p1;
  2488. ctx.p2 = p2;
  2489. ctx.pctx = pctx;
  2490. ctx.params = params;
  2491. ret = fixup(PRE_CTRL_TO_PARAMS, translation, &ctx);
  2492. if (ret > 0) {
  2493. switch (ctx.action_type) {
  2494. default:
  2495. /* fixup_args is expected to make sure this is dead code */
  2496. break;
  2497. case GET:
  2498. ret = evp_pkey_ctx_get_params_strict(pctx, ctx.params);
  2499. break;
  2500. case SET:
  2501. ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
  2502. break;
  2503. }
  2504. }
  2505. /*
  2506. * In POST, we pass the return value as p1, allowing the fixup_args
  2507. * function to affect it by changing its value.
  2508. */
  2509. if (ret > 0) {
  2510. ctx.p1 = ret;
  2511. fixup(POST_CTRL_TO_PARAMS, translation, &ctx);
  2512. ret = ctx.p1;
  2513. }
  2514. cleanup_translation_ctx(POST_CTRL_TO_PARAMS, translation, &ctx);
  2515. return ret;
  2516. }
  2517. /* This must ONLY be called for provider side operations */
  2518. int evp_pkey_ctx_ctrl_str_to_param(EVP_PKEY_CTX *pctx,
  2519. const char *name, const char *value)
  2520. {
  2521. struct translation_ctx_st ctx = { 0, };
  2522. struct translation_st tmpl = { 0, };
  2523. const struct translation_st *translation = NULL;
  2524. OSSL_PARAM params[2] = { OSSL_PARAM_END, OSSL_PARAM_END };
  2525. int keytype = pctx->legacy_keytype;
  2526. int optype = pctx->operation == 0 ? -1 : pctx->operation;
  2527. int ret;
  2528. fixup_args_fn *fixup = default_fixup_args;
  2529. tmpl.action_type = SET;
  2530. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2531. tmpl.optype = optype;
  2532. tmpl.ctrl_str = name;
  2533. tmpl.ctrl_hexstr = name;
  2534. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2535. if (translation != NULL) {
  2536. if (translation->fixup_args != NULL)
  2537. fixup = translation->fixup_args;
  2538. ctx.action_type = translation->action_type;
  2539. ctx.ishex = (tmpl.ctrl_hexstr != NULL);
  2540. } else {
  2541. /* String controls really only support setting */
  2542. ctx.action_type = SET;
  2543. }
  2544. ctx.ctrl_str = name;
  2545. ctx.p1 = (int)strlen(value);
  2546. ctx.p2 = (char *)value;
  2547. ctx.pctx = pctx;
  2548. ctx.params = params;
  2549. ret = fixup(PRE_CTRL_STR_TO_PARAMS, translation, &ctx);
  2550. if (ret > 0) {
  2551. switch (ctx.action_type) {
  2552. default:
  2553. /* fixup_args is expected to make sure this is dead code */
  2554. break;
  2555. case GET:
  2556. /*
  2557. * this is dead code, but must be present, or some compilers
  2558. * will complain
  2559. */
  2560. break;
  2561. case SET:
  2562. ret = evp_pkey_ctx_set_params_strict(pctx, ctx.params);
  2563. break;
  2564. }
  2565. }
  2566. if (ret > 0)
  2567. ret = fixup(POST_CTRL_STR_TO_PARAMS, translation, &ctx);
  2568. cleanup_translation_ctx(CLEANUP_CTRL_STR_TO_PARAMS, translation, &ctx);
  2569. return ret;
  2570. }
  2571. /* This must ONLY be called for legacy operations */
  2572. static int evp_pkey_ctx_setget_params_to_ctrl(EVP_PKEY_CTX *pctx,
  2573. enum action action_type,
  2574. OSSL_PARAM *params)
  2575. {
  2576. int keytype = pctx->legacy_keytype;
  2577. int optype = pctx->operation == 0 ? -1 : pctx->operation;
  2578. for (; params != NULL && params->key != NULL; params++) {
  2579. struct translation_ctx_st ctx = { 0, };
  2580. struct translation_st tmpl = { 0, };
  2581. const struct translation_st *translation = NULL;
  2582. fixup_args_fn *fixup = default_fixup_args;
  2583. int ret;
  2584. ctx.action_type = tmpl.action_type = action_type;
  2585. tmpl.keytype1 = tmpl.keytype2 = keytype;
  2586. tmpl.optype = optype;
  2587. tmpl.param_key = params->key;
  2588. translation = lookup_evp_pkey_ctx_translation(&tmpl);
  2589. if (translation != NULL) {
  2590. if (translation->fixup_args != NULL)
  2591. fixup = translation->fixup_args;
  2592. ctx.ctrl_cmd = translation->ctrl_num;
  2593. }
  2594. ctx.pctx = pctx;
  2595. ctx.params = params;
  2596. ret = fixup(PRE_PARAMS_TO_CTRL, translation, &ctx);
  2597. if (ret > 0 && ctx.action_type != NONE)
  2598. ret = EVP_PKEY_CTX_ctrl(pctx, keytype, optype,
  2599. ctx.ctrl_cmd, ctx.p1, ctx.p2);
  2600. /*
  2601. * In POST, we pass the return value as p1, allowing the fixup_args
  2602. * function to put it to good use, or maybe affect it.
  2603. *
  2604. * NOTE: even though EVP_PKEY_CTX_ctrl return value is documented
  2605. * as return positive on Success and 0 or negative on falure. There
  2606. * maybe parameters (e.g. ecdh_cofactor), which actually return 0
  2607. * as success value. That is why we do POST_PARAMS_TO_CTRL for 0
  2608. * value as well
  2609. */
  2610. if (ret >= 0) {
  2611. ctx.p1 = ret;
  2612. fixup(POST_PARAMS_TO_CTRL, translation, &ctx);
  2613. ret = ctx.p1;
  2614. }
  2615. cleanup_translation_ctx(CLEANUP_PARAMS_TO_CTRL, translation, &ctx);
  2616. if (ret <= 0)
  2617. return 0;
  2618. }
  2619. return 1;
  2620. }
  2621. int evp_pkey_ctx_set_params_to_ctrl(EVP_PKEY_CTX *ctx, const OSSL_PARAM *params)
  2622. {
  2623. return evp_pkey_ctx_setget_params_to_ctrl(ctx, SET, (OSSL_PARAM *)params);
  2624. }
  2625. int evp_pkey_ctx_get_params_to_ctrl(EVP_PKEY_CTX *ctx, OSSL_PARAM *params)
  2626. {
  2627. return evp_pkey_ctx_setget_params_to_ctrl(ctx, GET, params);
  2628. }
  2629. /* This must ONLY be called for legacy EVP_PKEYs */
  2630. static int evp_pkey_setget_params_to_ctrl(const EVP_PKEY *pkey,
  2631. enum action action_type,
  2632. OSSL_PARAM *params)
  2633. {
  2634. int ret = 1;
  2635. for (; params != NULL && params->key != NULL; params++) {
  2636. struct translation_ctx_st ctx = { 0, };
  2637. struct translation_st tmpl = { 0, };
  2638. const struct translation_st *translation = NULL;
  2639. fixup_args_fn *fixup = default_fixup_args;
  2640. tmpl.action_type = action_type;
  2641. tmpl.param_key = params->key;
  2642. translation = lookup_evp_pkey_translation(&tmpl);
  2643. if (translation != NULL) {
  2644. if (translation->fixup_args != NULL)
  2645. fixup = translation->fixup_args;
  2646. ctx.action_type = translation->action_type;
  2647. }
  2648. ctx.p2 = (void *)pkey;
  2649. ctx.params = params;
  2650. /*
  2651. * EVP_PKEY doesn't have any ctrl function, so we rely completely
  2652. * on fixup_args to do the whole work. Also, we currently only
  2653. * support getting.
  2654. */
  2655. if (!ossl_assert(translation != NULL)
  2656. || !ossl_assert(translation->action_type == GET)
  2657. || !ossl_assert(translation->fixup_args != NULL)) {
  2658. return -2;
  2659. }
  2660. ret = fixup(PKEY, translation, &ctx);
  2661. cleanup_translation_ctx(PKEY, translation, &ctx);
  2662. }
  2663. return ret;
  2664. }
  2665. int evp_pkey_get_params_to_ctrl(const EVP_PKEY *pkey, OSSL_PARAM *params)
  2666. {
  2667. return evp_pkey_setget_params_to_ctrl(pkey, GET, params);
  2668. }