ctrl_params_translate.c 108 KB

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