ctrl_params_translate.c 108 KB

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