ctrl_params_translate.c 100 KB

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