tls-provider.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269
  1. /*
  2. * Copyright 2019-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/core_names.h>
  11. #include <openssl/core_dispatch.h>
  12. #include <openssl/rand.h>
  13. #include <openssl/params.h>
  14. #include <openssl/err.h>
  15. #include <openssl/proverr.h>
  16. #include <openssl/pkcs12.h>
  17. #include <openssl/provider.h>
  18. #include <assert.h>
  19. #include <openssl/asn1.h>
  20. #include <openssl/asn1t.h>
  21. #include <openssl/core_object.h>
  22. #include "internal/asn1.h"
  23. /* For TLS1_3_VERSION */
  24. #include <openssl/ssl.h>
  25. #include "internal/nelem.h"
  26. #include "internal/refcount.h"
  27. /* error codes */
  28. /* xorprovider error codes */
  29. #define XORPROV_R_INVALID_DIGEST 1
  30. #define XORPROV_R_INVALID_SIZE 2
  31. #define XORPROV_R_INVALID_KEY 3
  32. #define XORPROV_R_UNSUPPORTED 4
  33. #define XORPROV_R_MISSING_OID 5
  34. #define XORPROV_R_OBJ_CREATE_ERR 6
  35. #define XORPROV_R_INVALID_ENCODING 7
  36. #define XORPROV_R_SIGN_ERROR 8
  37. #define XORPROV_R_LIB_CREATE_ERR 9
  38. #define XORPROV_R_NO_PRIVATE_KEY 10
  39. #define XORPROV_R_BUFFER_LENGTH_WRONG 11
  40. #define XORPROV_R_SIGNING_FAILED 12
  41. #define XORPROV_R_WRONG_PARAMETERS 13
  42. #define XORPROV_R_VERIFY_ERROR 14
  43. #define XORPROV_R_EVPINFO_MISSING 15
  44. static OSSL_FUNC_keymgmt_import_fn xor_import;
  45. static OSSL_FUNC_keymgmt_import_types_fn xor_import_types;
  46. static OSSL_FUNC_keymgmt_import_types_ex_fn xor_import_types_ex;
  47. static OSSL_FUNC_keymgmt_export_fn xor_export;
  48. static OSSL_FUNC_keymgmt_export_types_fn xor_export_types;
  49. static OSSL_FUNC_keymgmt_export_types_ex_fn xor_export_types_ex;
  50. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  51. const OSSL_DISPATCH *in,
  52. const OSSL_DISPATCH **out,
  53. void **provctx);
  54. #define XOR_KEY_SIZE 32
  55. /*
  56. * Top secret. This algorithm only works if no one knows what this number is.
  57. * Please don't tell anyone what it is.
  58. *
  59. * This algorithm is for testing only - don't really use it!
  60. */
  61. static const unsigned char private_constant[XOR_KEY_SIZE] = {
  62. 0xd3, 0x6b, 0x54, 0xec, 0x5b, 0xac, 0x89, 0x96, 0x8c, 0x2c, 0x66, 0xa5,
  63. 0x67, 0x0d, 0xe3, 0xdd, 0x43, 0x69, 0xbc, 0x83, 0x3d, 0x60, 0xc7, 0xb8,
  64. 0x2b, 0x1c, 0x5a, 0xfd, 0xb5, 0xcd, 0xd0, 0xf8
  65. };
  66. typedef struct xorkey_st {
  67. unsigned char privkey[XOR_KEY_SIZE];
  68. unsigned char pubkey[XOR_KEY_SIZE];
  69. int hasprivkey;
  70. int haspubkey;
  71. char *tls_name;
  72. CRYPTO_REF_COUNT references;
  73. } XORKEY;
  74. /* Key Management for the dummy XOR KEX, KEM and signature algorithms */
  75. static OSSL_FUNC_keymgmt_new_fn xor_newkey;
  76. static OSSL_FUNC_keymgmt_free_fn xor_freekey;
  77. static OSSL_FUNC_keymgmt_has_fn xor_has;
  78. static OSSL_FUNC_keymgmt_dup_fn xor_dup;
  79. static OSSL_FUNC_keymgmt_gen_init_fn xor_gen_init;
  80. static OSSL_FUNC_keymgmt_gen_set_params_fn xor_gen_set_params;
  81. static OSSL_FUNC_keymgmt_gen_settable_params_fn xor_gen_settable_params;
  82. static OSSL_FUNC_keymgmt_gen_fn xor_gen;
  83. static OSSL_FUNC_keymgmt_gen_cleanup_fn xor_gen_cleanup;
  84. static OSSL_FUNC_keymgmt_load_fn xor_load;
  85. static OSSL_FUNC_keymgmt_get_params_fn xor_get_params;
  86. static OSSL_FUNC_keymgmt_gettable_params_fn xor_gettable_params;
  87. static OSSL_FUNC_keymgmt_set_params_fn xor_set_params;
  88. static OSSL_FUNC_keymgmt_settable_params_fn xor_settable_params;
  89. /*
  90. * Dummy "XOR" Key Exchange algorithm. We just xor the private and public keys
  91. * together. Don't use this!
  92. */
  93. static OSSL_FUNC_keyexch_newctx_fn xor_newkemkexctx;
  94. static OSSL_FUNC_keyexch_init_fn xor_init;
  95. static OSSL_FUNC_keyexch_set_peer_fn xor_set_peer;
  96. static OSSL_FUNC_keyexch_derive_fn xor_derive;
  97. static OSSL_FUNC_keyexch_freectx_fn xor_freectx;
  98. static OSSL_FUNC_keyexch_dupctx_fn xor_dupctx;
  99. /*
  100. * Dummy "XOR" Key Encapsulation Method. We just build a KEM over the xor KEX.
  101. * Don't use this!
  102. */
  103. static OSSL_FUNC_kem_newctx_fn xor_newkemkexctx;
  104. static OSSL_FUNC_kem_freectx_fn xor_freectx;
  105. static OSSL_FUNC_kem_dupctx_fn xor_dupctx;
  106. static OSSL_FUNC_kem_encapsulate_init_fn xor_init;
  107. static OSSL_FUNC_kem_encapsulate_fn xor_encapsulate;
  108. static OSSL_FUNC_kem_decapsulate_init_fn xor_init;
  109. static OSSL_FUNC_kem_decapsulate_fn xor_decapsulate;
  110. /*
  111. * Common key management table access functions
  112. */
  113. static OSSL_FUNC_keymgmt_new_fn *
  114. xor_prov_get_keymgmt_new(const OSSL_DISPATCH *fns)
  115. {
  116. /* Pilfer the keymgmt dispatch table */
  117. for (; fns->function_id != 0; fns++)
  118. if (fns->function_id == OSSL_FUNC_KEYMGMT_NEW)
  119. return OSSL_FUNC_keymgmt_new(fns);
  120. return NULL;
  121. }
  122. static OSSL_FUNC_keymgmt_free_fn *
  123. xor_prov_get_keymgmt_free(const OSSL_DISPATCH *fns)
  124. {
  125. /* Pilfer the keymgmt dispatch table */
  126. for (; fns->function_id != 0; fns++)
  127. if (fns->function_id == OSSL_FUNC_KEYMGMT_FREE)
  128. return OSSL_FUNC_keymgmt_free(fns);
  129. return NULL;
  130. }
  131. static OSSL_FUNC_keymgmt_import_fn *
  132. xor_prov_get_keymgmt_import(const OSSL_DISPATCH *fns)
  133. {
  134. /* Pilfer the keymgmt dispatch table */
  135. for (; fns->function_id != 0; fns++)
  136. if (fns->function_id == OSSL_FUNC_KEYMGMT_IMPORT)
  137. return OSSL_FUNC_keymgmt_import(fns);
  138. return NULL;
  139. }
  140. static OSSL_FUNC_keymgmt_export_fn *
  141. xor_prov_get_keymgmt_export(const OSSL_DISPATCH *fns)
  142. {
  143. /* Pilfer the keymgmt dispatch table */
  144. for (; fns->function_id != 0; fns++)
  145. if (fns->function_id == OSSL_FUNC_KEYMGMT_EXPORT)
  146. return OSSL_FUNC_keymgmt_export(fns);
  147. return NULL;
  148. }
  149. static void *xor_prov_import_key(const OSSL_DISPATCH *fns, void *provctx,
  150. int selection, const OSSL_PARAM params[])
  151. {
  152. OSSL_FUNC_keymgmt_new_fn *kmgmt_new = xor_prov_get_keymgmt_new(fns);
  153. OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns);
  154. OSSL_FUNC_keymgmt_import_fn *kmgmt_import =
  155. xor_prov_get_keymgmt_import(fns);
  156. void *key = NULL;
  157. if (kmgmt_new != NULL && kmgmt_import != NULL && kmgmt_free != NULL) {
  158. if ((key = kmgmt_new(provctx)) == NULL
  159. || !kmgmt_import(key, selection, params)) {
  160. kmgmt_free(key);
  161. key = NULL;
  162. }
  163. }
  164. return key;
  165. }
  166. static void xor_prov_free_key(const OSSL_DISPATCH *fns, void *key)
  167. {
  168. OSSL_FUNC_keymgmt_free_fn *kmgmt_free = xor_prov_get_keymgmt_free(fns);
  169. if (kmgmt_free != NULL)
  170. kmgmt_free(key);
  171. }
  172. /*
  173. * We define 2 dummy TLS groups called "xorgroup" and "xorkemgroup" for test
  174. * purposes
  175. */
  176. struct tls_group_st {
  177. unsigned int group_id; /* for "tls-group-id", see provider-base(7) */
  178. unsigned int secbits;
  179. unsigned int mintls;
  180. unsigned int maxtls;
  181. unsigned int mindtls;
  182. unsigned int maxdtls;
  183. unsigned int is_kem; /* boolean */
  184. };
  185. #define XORGROUP_NAME "xorgroup"
  186. #define XORGROUP_NAME_INTERNAL "xorgroup-int"
  187. static struct tls_group_st xor_group = {
  188. 0, /* group_id, set by randomize_tls_alg_id() */
  189. 128, /* secbits */
  190. TLS1_3_VERSION, /* mintls */
  191. 0, /* maxtls */
  192. -1, /* mindtls */
  193. -1, /* maxdtls */
  194. 0 /* is_kem */
  195. };
  196. #define XORKEMGROUP_NAME "xorkemgroup"
  197. #define XORKEMGROUP_NAME_INTERNAL "xorkemgroup-int"
  198. static struct tls_group_st xor_kemgroup = {
  199. 0, /* group_id, set by randomize_tls_alg_id() */
  200. 128, /* secbits */
  201. TLS1_3_VERSION, /* mintls */
  202. 0, /* maxtls */
  203. -1, /* mindtls */
  204. -1, /* maxdtls */
  205. 1 /* is_kem */
  206. };
  207. #define ALGORITHM "XOR"
  208. static const OSSL_PARAM xor_group_params[] = {
  209. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
  210. XORGROUP_NAME, sizeof(XORGROUP_NAME)),
  211. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
  212. XORGROUP_NAME_INTERNAL,
  213. sizeof(XORGROUP_NAME_INTERNAL)),
  214. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
  215. sizeof(ALGORITHM)),
  216. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_group.group_id),
  217. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
  218. &xor_group.secbits),
  219. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_group.mintls),
  220. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_group.maxtls),
  221. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_group.mindtls),
  222. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_group.maxdtls),
  223. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_group.is_kem),
  224. OSSL_PARAM_END
  225. };
  226. static const OSSL_PARAM xor_kemgroup_params[] = {
  227. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME,
  228. XORKEMGROUP_NAME, sizeof(XORKEMGROUP_NAME)),
  229. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_NAME_INTERNAL,
  230. XORKEMGROUP_NAME_INTERNAL,
  231. sizeof(XORKEMGROUP_NAME_INTERNAL)),
  232. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_GROUP_ALG, ALGORITHM,
  233. sizeof(ALGORITHM)),
  234. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_ID, &xor_kemgroup.group_id),
  235. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_SECURITY_BITS,
  236. &xor_kemgroup.secbits),
  237. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_TLS, &xor_kemgroup.mintls),
  238. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_TLS, &xor_kemgroup.maxtls),
  239. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MIN_DTLS, &xor_kemgroup.mindtls),
  240. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_GROUP_MAX_DTLS, &xor_kemgroup.maxdtls),
  241. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_GROUP_IS_KEM, &xor_kemgroup.is_kem),
  242. OSSL_PARAM_END
  243. };
  244. #define NUM_DUMMY_GROUPS 50
  245. static char *dummy_group_names[NUM_DUMMY_GROUPS];
  246. /*
  247. * We define a dummy TLS sigalg called for test purposes
  248. */
  249. struct tls_sigalg_st {
  250. unsigned int code_point; /* for "tls-sigalg-alg", see provider-base(7) */
  251. unsigned int secbits;
  252. unsigned int mintls;
  253. unsigned int maxtls;
  254. };
  255. #define XORSIGALG_NAME "xorhmacsig"
  256. #define XORSIGALG_OID "1.3.6.1.4.1.16604.998888.1"
  257. #define XORSIGALG_HASH_NAME "xorhmacsha2sig"
  258. #define XORSIGALG_HASH "SHA256"
  259. #define XORSIGALG_HASH_OID "1.3.6.1.4.1.16604.998888.2"
  260. #define XORSIGALG12_NAME "xorhmacsig12"
  261. #define XORSIGALG12_OID "1.3.6.1.4.1.16604.998888.3"
  262. static struct tls_sigalg_st xor_sigalg = {
  263. 0, /* alg id, set by randomize_tls_alg_id() */
  264. 128, /* secbits */
  265. TLS1_3_VERSION, /* mintls */
  266. 0, /* maxtls */
  267. };
  268. static struct tls_sigalg_st xor_sigalg_hash = {
  269. 0, /* alg id, set by randomize_tls_alg_id() */
  270. 128, /* secbits */
  271. TLS1_3_VERSION, /* mintls */
  272. 0, /* maxtls */
  273. };
  274. static struct tls_sigalg_st xor_sigalg12 = {
  275. 0, /* alg id, set by randomize_tls_alg_id() */
  276. 128, /* secbits */
  277. TLS1_2_VERSION, /* mintls */
  278. TLS1_2_VERSION, /* maxtls */
  279. };
  280. static const OSSL_PARAM xor_sig_nohash_params[] = {
  281. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME,
  282. XORSIGALG_NAME, sizeof(XORSIGALG_NAME)),
  283. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME,
  284. XORSIGALG_NAME,
  285. sizeof(XORSIGALG_NAME)),
  286. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID,
  287. XORSIGALG_OID, sizeof(XORSIGALG_OID)),
  288. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT,
  289. &xor_sigalg.code_point),
  290. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS,
  291. &xor_sigalg.secbits),
  292. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS,
  293. &xor_sigalg.mintls),
  294. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS,
  295. &xor_sigalg.maxtls),
  296. OSSL_PARAM_END
  297. };
  298. static const OSSL_PARAM xor_sig_hash_params[] = {
  299. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME,
  300. XORSIGALG_HASH_NAME, sizeof(XORSIGALG_HASH_NAME)),
  301. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME,
  302. XORSIGALG_HASH_NAME,
  303. sizeof(XORSIGALG_HASH_NAME)),
  304. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_HASH_NAME,
  305. XORSIGALG_HASH, sizeof(XORSIGALG_HASH)),
  306. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID,
  307. XORSIGALG_HASH_OID, sizeof(XORSIGALG_HASH_OID)),
  308. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT,
  309. &xor_sigalg_hash.code_point),
  310. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS,
  311. &xor_sigalg_hash.secbits),
  312. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS,
  313. &xor_sigalg_hash.mintls),
  314. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS,
  315. &xor_sigalg_hash.maxtls),
  316. OSSL_PARAM_END
  317. };
  318. static const OSSL_PARAM xor_sig_12_params[] = {
  319. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_IANA_NAME,
  320. XORSIGALG12_NAME, sizeof(XORSIGALG12_NAME)),
  321. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_NAME,
  322. XORSIGALG12_NAME,
  323. sizeof(XORSIGALG12_NAME)),
  324. OSSL_PARAM_utf8_string(OSSL_CAPABILITY_TLS_SIGALG_OID,
  325. XORSIGALG12_OID, sizeof(XORSIGALG12_OID)),
  326. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_CODE_POINT,
  327. &xor_sigalg12.code_point),
  328. OSSL_PARAM_uint(OSSL_CAPABILITY_TLS_SIGALG_SECURITY_BITS,
  329. &xor_sigalg12.secbits),
  330. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MIN_TLS,
  331. &xor_sigalg12.mintls),
  332. OSSL_PARAM_int(OSSL_CAPABILITY_TLS_SIGALG_MAX_TLS,
  333. &xor_sigalg12.maxtls),
  334. OSSL_PARAM_END
  335. };
  336. static int tls_prov_get_capabilities(void *provctx, const char *capability,
  337. OSSL_CALLBACK *cb, void *arg)
  338. {
  339. int ret = 0;
  340. int i;
  341. const char *dummy_base = "dummy";
  342. const size_t dummy_name_max_size = strlen(dummy_base) + 3;
  343. if (strcmp(capability, "TLS-GROUP") == 0) {
  344. /* Register our 2 groups */
  345. ret = cb(xor_group_params, arg);
  346. ret &= cb(xor_kemgroup_params, arg);
  347. /*
  348. * Now register some dummy groups > GROUPLIST_INCREMENT (== 40) as defined
  349. * in ssl/t1_lib.c, to make sure we exercise the code paths for registering
  350. * large numbers of groups.
  351. */
  352. for (i = 0; i < NUM_DUMMY_GROUPS; i++) {
  353. OSSL_PARAM dummygroup[OSSL_NELEM(xor_group_params)];
  354. memcpy(dummygroup, xor_group_params, sizeof(xor_group_params));
  355. /* Give the dummy group a unique name */
  356. if (dummy_group_names[i] == NULL) {
  357. dummy_group_names[i] = OPENSSL_zalloc(dummy_name_max_size);
  358. if (dummy_group_names[i] == NULL)
  359. return 0;
  360. BIO_snprintf(dummy_group_names[i],
  361. dummy_name_max_size,
  362. "%s%d", dummy_base, i);
  363. }
  364. dummygroup[0].data = dummy_group_names[i];
  365. dummygroup[0].data_size = strlen(dummy_group_names[i]) + 1;
  366. ret &= cb(dummygroup, arg);
  367. }
  368. }
  369. if (strcmp(capability, "TLS-SIGALG") == 0) {
  370. ret = cb(xor_sig_nohash_params, arg);
  371. ret &= cb(xor_sig_hash_params, arg);
  372. ret &= cb(xor_sig_12_params, arg);
  373. }
  374. return ret;
  375. }
  376. typedef struct {
  377. OSSL_LIB_CTX *libctx;
  378. } PROV_XOR_CTX;
  379. static PROV_XOR_CTX *xor_newprovctx(OSSL_LIB_CTX *libctx)
  380. {
  381. PROV_XOR_CTX* prov_ctx = OPENSSL_malloc(sizeof(PROV_XOR_CTX));
  382. if (prov_ctx == NULL)
  383. return NULL;
  384. if (libctx == NULL) {
  385. OPENSSL_free(prov_ctx);
  386. return NULL;
  387. }
  388. prov_ctx->libctx = libctx;
  389. return prov_ctx;
  390. }
  391. #define PROV_XOR_LIBCTX_OF(provctx) (((PROV_XOR_CTX *)provctx)->libctx)
  392. /*
  393. * Dummy "XOR" Key Exchange and signature algorithm. We just xor the
  394. * private and public keys together. Don't use this!
  395. */
  396. typedef struct {
  397. XORKEY *key;
  398. XORKEY *peerkey;
  399. void *provctx;
  400. } PROV_XORKEMKEX_CTX;
  401. static void *xor_newkemkexctx(void *provctx)
  402. {
  403. PROV_XORKEMKEX_CTX *pxorctx = OPENSSL_zalloc(sizeof(PROV_XORKEMKEX_CTX));
  404. if (pxorctx == NULL)
  405. return NULL;
  406. pxorctx->provctx = provctx;
  407. return pxorctx;
  408. }
  409. static int xor_init(void *vpxorctx, void *vkey,
  410. ossl_unused const OSSL_PARAM params[])
  411. {
  412. PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx;
  413. if (pxorctx == NULL || vkey == NULL)
  414. return 0;
  415. pxorctx->key = vkey;
  416. return 1;
  417. }
  418. static int xor_set_peer(void *vpxorctx, void *vpeerkey)
  419. {
  420. PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx;
  421. if (pxorctx == NULL || vpeerkey == NULL)
  422. return 0;
  423. pxorctx->peerkey = vpeerkey;
  424. return 1;
  425. }
  426. static int xor_derive(void *vpxorctx, unsigned char *secret, size_t *secretlen,
  427. size_t outlen)
  428. {
  429. PROV_XORKEMKEX_CTX *pxorctx = (PROV_XORKEMKEX_CTX *)vpxorctx;
  430. int i;
  431. if (pxorctx->key == NULL || pxorctx->peerkey == NULL)
  432. return 0;
  433. *secretlen = XOR_KEY_SIZE;
  434. if (secret == NULL)
  435. return 1;
  436. if (outlen < XOR_KEY_SIZE)
  437. return 0;
  438. for (i = 0; i < XOR_KEY_SIZE; i++)
  439. secret[i] = pxorctx->key->privkey[i] ^ pxorctx->peerkey->pubkey[i];
  440. return 1;
  441. }
  442. static void xor_freectx(void *pxorctx)
  443. {
  444. OPENSSL_free(pxorctx);
  445. }
  446. static void *xor_dupctx(void *vpxorctx)
  447. {
  448. PROV_XORKEMKEX_CTX *srcctx = (PROV_XORKEMKEX_CTX *)vpxorctx;
  449. PROV_XORKEMKEX_CTX *dstctx;
  450. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  451. if (dstctx == NULL)
  452. return NULL;
  453. *dstctx = *srcctx;
  454. return dstctx;
  455. }
  456. static const OSSL_DISPATCH xor_keyexch_functions[] = {
  457. { OSSL_FUNC_KEYEXCH_NEWCTX, (void (*)(void))xor_newkemkexctx },
  458. { OSSL_FUNC_KEYEXCH_INIT, (void (*)(void))xor_init },
  459. { OSSL_FUNC_KEYEXCH_DERIVE, (void (*)(void))xor_derive },
  460. { OSSL_FUNC_KEYEXCH_SET_PEER, (void (*)(void))xor_set_peer },
  461. { OSSL_FUNC_KEYEXCH_FREECTX, (void (*)(void))xor_freectx },
  462. { OSSL_FUNC_KEYEXCH_DUPCTX, (void (*)(void))xor_dupctx },
  463. OSSL_DISPATCH_END
  464. };
  465. static const OSSL_ALGORITHM tls_prov_keyexch[] = {
  466. /*
  467. * Obviously this is not FIPS approved, but in order to test in conjunction
  468. * with the FIPS provider we pretend that it is.
  469. */
  470. { "XOR", "provider=tls-provider,fips=yes", xor_keyexch_functions },
  471. { NULL, NULL, NULL }
  472. };
  473. /*
  474. * Dummy "XOR" Key Encapsulation Method. We just build a KEM over the xor KEX.
  475. * Don't use this!
  476. */
  477. static int xor_encapsulate(void *vpxorctx,
  478. unsigned char *ct, size_t *ctlen,
  479. unsigned char *ss, size_t *sslen)
  480. {
  481. /*
  482. * We are building this around a KEX:
  483. *
  484. * 1. we generate ephemeral keypair
  485. * 2. we encode our ephemeral pubkey as the outgoing ct
  486. * 3. we derive using our ephemeral privkey in combination with the peer
  487. * pubkey from the ctx; the result is our ss.
  488. */
  489. int rv = 0;
  490. void *genctx = NULL, *derivectx = NULL;
  491. XORKEY *ourkey = NULL;
  492. PROV_XORKEMKEX_CTX *pxorctx = vpxorctx;
  493. if (ct == NULL || ss == NULL) {
  494. /* Just return sizes */
  495. if (ctlen == NULL && sslen == NULL)
  496. return 0;
  497. if (ctlen != NULL)
  498. *ctlen = XOR_KEY_SIZE;
  499. if (sslen != NULL)
  500. *sslen = XOR_KEY_SIZE;
  501. return 1;
  502. }
  503. /* 1. Generate keypair */
  504. genctx = xor_gen_init(pxorctx->provctx, OSSL_KEYMGMT_SELECT_KEYPAIR, NULL);
  505. if (genctx == NULL)
  506. goto end;
  507. ourkey = xor_gen(genctx, NULL, NULL);
  508. if (ourkey == NULL)
  509. goto end;
  510. /* 2. Encode ephemeral pubkey as ct */
  511. memcpy(ct, ourkey->pubkey, XOR_KEY_SIZE);
  512. *ctlen = XOR_KEY_SIZE;
  513. /* 3. Derive ss via KEX */
  514. derivectx = xor_newkemkexctx(pxorctx->provctx);
  515. if (derivectx == NULL
  516. || !xor_init(derivectx, ourkey, NULL)
  517. || !xor_set_peer(derivectx, pxorctx->key)
  518. || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE))
  519. goto end;
  520. rv = 1;
  521. end:
  522. xor_gen_cleanup(genctx);
  523. xor_freekey(ourkey);
  524. xor_freectx(derivectx);
  525. return rv;
  526. }
  527. static int xor_decapsulate(void *vpxorctx,
  528. unsigned char *ss, size_t *sslen,
  529. const unsigned char *ct, size_t ctlen)
  530. {
  531. /*
  532. * We are building this around a KEX:
  533. *
  534. * - ct is our peer's pubkey
  535. * - decapsulate is just derive.
  536. */
  537. int rv = 0;
  538. void *derivectx = NULL;
  539. XORKEY *peerkey = NULL;
  540. PROV_XORKEMKEX_CTX *pxorctx = vpxorctx;
  541. if (ss == NULL) {
  542. /* Just return size */
  543. if (sslen == NULL)
  544. return 0;
  545. *sslen = XOR_KEY_SIZE;
  546. return 1;
  547. }
  548. if (ctlen != XOR_KEY_SIZE)
  549. return 0;
  550. peerkey = xor_newkey(pxorctx->provctx);
  551. if (peerkey == NULL)
  552. goto end;
  553. memcpy(peerkey->pubkey, ct, XOR_KEY_SIZE);
  554. /* Derive ss via KEX */
  555. derivectx = xor_newkemkexctx(pxorctx->provctx);
  556. if (derivectx == NULL
  557. || !xor_init(derivectx, pxorctx->key, NULL)
  558. || !xor_set_peer(derivectx, peerkey)
  559. || !xor_derive(derivectx, ss, sslen, XOR_KEY_SIZE))
  560. goto end;
  561. rv = 1;
  562. end:
  563. xor_freekey(peerkey);
  564. xor_freectx(derivectx);
  565. return rv;
  566. }
  567. static const OSSL_DISPATCH xor_kem_functions[] = {
  568. { OSSL_FUNC_KEM_NEWCTX, (void (*)(void))xor_newkemkexctx },
  569. { OSSL_FUNC_KEM_FREECTX, (void (*)(void))xor_freectx },
  570. { OSSL_FUNC_KEM_DUPCTX, (void (*)(void))xor_dupctx },
  571. { OSSL_FUNC_KEM_ENCAPSULATE_INIT, (void (*)(void))xor_init },
  572. { OSSL_FUNC_KEM_ENCAPSULATE, (void (*)(void))xor_encapsulate },
  573. { OSSL_FUNC_KEM_DECAPSULATE_INIT, (void (*)(void))xor_init },
  574. { OSSL_FUNC_KEM_DECAPSULATE, (void (*)(void))xor_decapsulate },
  575. OSSL_DISPATCH_END
  576. };
  577. static const OSSL_ALGORITHM tls_prov_kem[] = {
  578. /*
  579. * Obviously this is not FIPS approved, but in order to test in conjunction
  580. * with the FIPS provider we pretend that it is.
  581. */
  582. { "XOR", "provider=tls-provider,fips=yes", xor_kem_functions },
  583. { NULL, NULL, NULL }
  584. };
  585. /* Key Management for the dummy XOR key exchange algorithm */
  586. static void *xor_newkey(void *provctx)
  587. {
  588. XORKEY *ret = OPENSSL_zalloc(sizeof(XORKEY));
  589. if (ret == NULL)
  590. return NULL;
  591. if (!CRYPTO_NEW_REF(&ret->references, 1)) {
  592. OPENSSL_free(ret);
  593. return NULL;
  594. }
  595. return ret;
  596. }
  597. static void xor_freekey(void *keydata)
  598. {
  599. XORKEY* key = (XORKEY *)keydata;
  600. int refcnt;
  601. if (key == NULL)
  602. return;
  603. if (CRYPTO_DOWN_REF(&key->references, &refcnt) <= 0)
  604. return;
  605. if (refcnt > 0)
  606. return;
  607. assert(refcnt == 0);
  608. if (key != NULL) {
  609. OPENSSL_free(key->tls_name);
  610. key->tls_name = NULL;
  611. }
  612. CRYPTO_FREE_REF(&key->references);
  613. OPENSSL_free(key);
  614. }
  615. static int xor_key_up_ref(XORKEY *key)
  616. {
  617. int refcnt;
  618. if (CRYPTO_UP_REF(&key->references, &refcnt) <= 0)
  619. return 0;
  620. assert(refcnt > 1);
  621. return (refcnt > 1);
  622. }
  623. static int xor_has(const void *vkey, int selection)
  624. {
  625. const XORKEY *key = vkey;
  626. int ok = 0;
  627. if (key != NULL) {
  628. ok = 1;
  629. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0)
  630. ok = ok && key->haspubkey;
  631. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0)
  632. ok = ok && key->hasprivkey;
  633. }
  634. return ok;
  635. }
  636. static void *xor_dup(const void *vfromkey, int selection)
  637. {
  638. XORKEY *tokey = xor_newkey(NULL);
  639. const XORKEY *fromkey = vfromkey;
  640. int ok = 0;
  641. if (tokey != NULL && fromkey != NULL) {
  642. ok = 1;
  643. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  644. if (fromkey->haspubkey) {
  645. memcpy(tokey->pubkey, fromkey->pubkey, XOR_KEY_SIZE);
  646. tokey->haspubkey = 1;
  647. } else {
  648. tokey->haspubkey = 0;
  649. }
  650. }
  651. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  652. if (fromkey->hasprivkey) {
  653. memcpy(tokey->privkey, fromkey->privkey, XOR_KEY_SIZE);
  654. tokey->hasprivkey = 1;
  655. } else {
  656. tokey->hasprivkey = 0;
  657. }
  658. }
  659. if (fromkey->tls_name != NULL)
  660. tokey->tls_name = OPENSSL_strdup(fromkey->tls_name);
  661. }
  662. if (!ok) {
  663. xor_freekey(tokey);
  664. tokey = NULL;
  665. }
  666. return tokey;
  667. }
  668. static ossl_inline int xor_get_params(void *vkey, OSSL_PARAM params[])
  669. {
  670. XORKEY *key = vkey;
  671. OSSL_PARAM *p;
  672. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_BITS)) != NULL
  673. && !OSSL_PARAM_set_int(p, XOR_KEY_SIZE))
  674. return 0;
  675. if ((p = OSSL_PARAM_locate(params, OSSL_PKEY_PARAM_SECURITY_BITS)) != NULL
  676. && !OSSL_PARAM_set_int(p, xor_group.secbits))
  677. return 0;
  678. if ((p = OSSL_PARAM_locate(params,
  679. OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY)) != NULL) {
  680. if (p->data_type != OSSL_PARAM_OCTET_STRING)
  681. return 0;
  682. p->return_size = XOR_KEY_SIZE;
  683. if (p->data != NULL && p->data_size >= XOR_KEY_SIZE)
  684. memcpy(p->data, key->pubkey, XOR_KEY_SIZE);
  685. }
  686. return 1;
  687. }
  688. static const OSSL_PARAM xor_params[] = {
  689. OSSL_PARAM_int(OSSL_PKEY_PARAM_BITS, NULL),
  690. OSSL_PARAM_int(OSSL_PKEY_PARAM_SECURITY_BITS, NULL),
  691. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  692. OSSL_PARAM_END
  693. };
  694. static const OSSL_PARAM *xor_gettable_params(void *provctx)
  695. {
  696. return xor_params;
  697. }
  698. static int xor_set_params(void *vkey, const OSSL_PARAM params[])
  699. {
  700. XORKEY *key = vkey;
  701. const OSSL_PARAM *p;
  702. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY);
  703. if (p != NULL) {
  704. if (p->data_type != OSSL_PARAM_OCTET_STRING
  705. || p->data_size != XOR_KEY_SIZE)
  706. return 0;
  707. memcpy(key->pubkey, p->data, XOR_KEY_SIZE);
  708. key->haspubkey = 1;
  709. }
  710. return 1;
  711. }
  712. static const OSSL_PARAM xor_known_settable_params[] = {
  713. OSSL_PARAM_octet_string(OSSL_PKEY_PARAM_ENCODED_PUBLIC_KEY, NULL, 0),
  714. OSSL_PARAM_END
  715. };
  716. static void *xor_load(const void *reference, size_t reference_sz)
  717. {
  718. XORKEY *key = NULL;
  719. if (reference_sz == sizeof(key)) {
  720. /* The contents of the reference is the address to our object */
  721. key = *(XORKEY **)reference;
  722. /* We grabbed, so we detach it */
  723. *(XORKEY **)reference = NULL;
  724. return key;
  725. }
  726. return NULL;
  727. }
  728. /* check one key is the "XOR complement" of the other */
  729. static int xor_recreate(const unsigned char *kd1, const unsigned char *kd2) {
  730. int i;
  731. for (i = 0; i < XOR_KEY_SIZE; i++) {
  732. if ((kd1[i] & 0xff) != ((kd2[i] ^ private_constant[i]) & 0xff))
  733. return 0;
  734. }
  735. return 1;
  736. }
  737. static int xor_match(const void *keydata1, const void *keydata2, int selection)
  738. {
  739. const XORKEY *key1 = keydata1;
  740. const XORKEY *key2 = keydata2;
  741. int ok = 1;
  742. if (key1->tls_name != NULL && key2->tls_name != NULL)
  743. ok = ok & (strcmp(key1->tls_name, key2->tls_name) == 0);
  744. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  745. if (key1->hasprivkey) {
  746. if (key2->hasprivkey)
  747. ok = ok & (CRYPTO_memcmp(key1->privkey, key2->privkey,
  748. XOR_KEY_SIZE) == 0);
  749. else
  750. ok = ok & xor_recreate(key1->privkey, key2->pubkey);
  751. } else {
  752. if (key2->hasprivkey)
  753. ok = ok & xor_recreate(key2->privkey, key1->pubkey);
  754. else
  755. ok = 0;
  756. }
  757. }
  758. if ((selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  759. if (key1->haspubkey) {
  760. if (key2->haspubkey)
  761. ok = ok & (CRYPTO_memcmp(key1->pubkey, key2->pubkey, XOR_KEY_SIZE) == 0);
  762. else
  763. ok = ok & xor_recreate(key1->pubkey, key2->privkey);
  764. } else {
  765. if (key2->haspubkey)
  766. ok = ok & xor_recreate(key2->pubkey, key1->privkey);
  767. else
  768. ok = 0;
  769. }
  770. }
  771. return ok;
  772. }
  773. static const OSSL_PARAM *xor_settable_params(void *provctx)
  774. {
  775. return xor_known_settable_params;
  776. }
  777. struct xor_gen_ctx {
  778. int selection;
  779. OSSL_LIB_CTX *libctx;
  780. };
  781. static void *xor_gen_init(void *provctx, int selection,
  782. const OSSL_PARAM params[])
  783. {
  784. struct xor_gen_ctx *gctx = NULL;
  785. if ((selection & (OSSL_KEYMGMT_SELECT_KEYPAIR
  786. | OSSL_KEYMGMT_SELECT_DOMAIN_PARAMETERS)) == 0)
  787. return NULL;
  788. if ((gctx = OPENSSL_zalloc(sizeof(*gctx))) != NULL)
  789. gctx->selection = selection;
  790. gctx->libctx = PROV_XOR_LIBCTX_OF(provctx);
  791. if (!xor_gen_set_params(gctx, params)) {
  792. OPENSSL_free(gctx);
  793. return NULL;
  794. }
  795. return gctx;
  796. }
  797. static int xor_gen_set_params(void *genctx, const OSSL_PARAM params[])
  798. {
  799. struct xor_gen_ctx *gctx = genctx;
  800. const OSSL_PARAM *p;
  801. if (gctx == NULL)
  802. return 0;
  803. p = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_GROUP_NAME);
  804. if (p != NULL) {
  805. if (p->data_type != OSSL_PARAM_UTF8_STRING
  806. || (strcmp(p->data, XORGROUP_NAME_INTERNAL) != 0
  807. && strcmp(p->data, XORKEMGROUP_NAME_INTERNAL) != 0))
  808. return 0;
  809. }
  810. return 1;
  811. }
  812. static const OSSL_PARAM *xor_gen_settable_params(ossl_unused void *genctx,
  813. ossl_unused void *provctx)
  814. {
  815. static OSSL_PARAM settable[] = {
  816. OSSL_PARAM_utf8_string(OSSL_PKEY_PARAM_GROUP_NAME, NULL, 0),
  817. OSSL_PARAM_END
  818. };
  819. return settable;
  820. }
  821. static void *xor_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  822. {
  823. struct xor_gen_ctx *gctx = genctx;
  824. XORKEY *key = xor_newkey(NULL);
  825. size_t i;
  826. if (key == NULL)
  827. return NULL;
  828. if ((gctx->selection & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0) {
  829. if (RAND_bytes_ex(gctx->libctx, key->privkey, XOR_KEY_SIZE, 0) <= 0) {
  830. OPENSSL_free(key);
  831. return NULL;
  832. }
  833. for (i = 0; i < XOR_KEY_SIZE; i++)
  834. key->pubkey[i] = key->privkey[i] ^ private_constant[i];
  835. key->hasprivkey = 1;
  836. key->haspubkey = 1;
  837. }
  838. return key;
  839. }
  840. /* IMPORT + EXPORT */
  841. static int xor_import(void *vkey, int select, const OSSL_PARAM params[])
  842. {
  843. XORKEY *key = vkey;
  844. const OSSL_PARAM *param_priv_key, *param_pub_key;
  845. unsigned char privkey[XOR_KEY_SIZE];
  846. unsigned char pubkey[XOR_KEY_SIZE];
  847. void *pprivkey = privkey, *ppubkey = pubkey;
  848. size_t priv_len = 0, pub_len = 0;
  849. int res = 0;
  850. if (key == NULL || (select & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  851. return 0;
  852. memset(privkey, 0, sizeof(privkey));
  853. memset(pubkey, 0, sizeof(pubkey));
  854. param_priv_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PRIV_KEY);
  855. param_pub_key = OSSL_PARAM_locate_const(params, OSSL_PKEY_PARAM_PUB_KEY);
  856. if ((param_priv_key != NULL
  857. && !OSSL_PARAM_get_octet_string(param_priv_key, &pprivkey,
  858. sizeof(privkey), &priv_len))
  859. || (param_pub_key != NULL
  860. && !OSSL_PARAM_get_octet_string(param_pub_key, &ppubkey,
  861. sizeof(pubkey), &pub_len)))
  862. goto err;
  863. if (priv_len > 0) {
  864. memcpy(key->privkey, privkey, priv_len);
  865. key->hasprivkey = 1;
  866. }
  867. if (pub_len > 0) {
  868. memcpy(key->pubkey, pubkey, pub_len);
  869. key->haspubkey = 1;
  870. }
  871. res = 1;
  872. err:
  873. return res;
  874. }
  875. static int xor_export(void *vkey, int select, OSSL_CALLBACK *param_cb,
  876. void *cbarg)
  877. {
  878. XORKEY *key = vkey;
  879. OSSL_PARAM params[3], *p = params;
  880. if (key == NULL || (select & OSSL_KEYMGMT_SELECT_KEYPAIR) == 0)
  881. return 0;
  882. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PRIV_KEY,
  883. key->privkey,
  884. sizeof(key->privkey));
  885. *p++ = OSSL_PARAM_construct_octet_string(OSSL_PKEY_PARAM_PUB_KEY,
  886. key->pubkey, sizeof(key->pubkey));
  887. *p++ = OSSL_PARAM_construct_end();
  888. return param_cb(params, cbarg);
  889. }
  890. static const OSSL_PARAM xor_key_types[] = {
  891. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PUB_KEY, NULL, 0),
  892. OSSL_PARAM_BN(OSSL_PKEY_PARAM_PRIV_KEY, NULL, 0),
  893. OSSL_PARAM_END
  894. };
  895. static const OSSL_PARAM *xor_import_types(int select)
  896. {
  897. return (select & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0 ? xor_key_types : NULL;
  898. }
  899. static const OSSL_PARAM *xor_import_types_ex(void *provctx, int select)
  900. {
  901. if (provctx == NULL)
  902. return NULL;
  903. return xor_import_types(select);
  904. }
  905. static const OSSL_PARAM *xor_export_types(int select)
  906. {
  907. return (select & OSSL_KEYMGMT_SELECT_KEYPAIR) != 0 ? xor_key_types : NULL;
  908. }
  909. static const OSSL_PARAM *xor_export_types_ex(void *provctx, int select)
  910. {
  911. if (provctx == NULL)
  912. return NULL;
  913. return xor_export_types(select);
  914. }
  915. static void xor_gen_cleanup(void *genctx)
  916. {
  917. OPENSSL_free(genctx);
  918. }
  919. static const OSSL_DISPATCH xor_keymgmt_functions[] = {
  920. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey },
  921. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
  922. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
  923. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  924. (void (*)(void))xor_gen_settable_params },
  925. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_gen },
  926. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
  927. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
  928. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
  929. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
  930. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
  931. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
  932. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup },
  933. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey },
  934. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import },
  935. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types },
  936. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES_EX, (void (*)(void))xor_import_types_ex },
  937. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export },
  938. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types },
  939. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES_EX, (void (*)(void))xor_export_types_ex },
  940. OSSL_DISPATCH_END
  941. };
  942. /* We're re-using most XOR keymgmt functions also for signature operations: */
  943. static void *xor_xorhmacsig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  944. {
  945. XORKEY *k = xor_gen(genctx, osslcb, cbarg);
  946. if (k == NULL)
  947. return NULL;
  948. k->tls_name = OPENSSL_strdup(XORSIGALG_NAME);
  949. if (k->tls_name == NULL) {
  950. xor_freekey(k);
  951. return NULL;
  952. }
  953. return k;
  954. }
  955. static void *xor_xorhmacsha2sig_gen(void *genctx, OSSL_CALLBACK *osslcb, void *cbarg)
  956. {
  957. XORKEY* k = xor_gen(genctx, osslcb, cbarg);
  958. if (k == NULL)
  959. return NULL;
  960. k->tls_name = OPENSSL_strdup(XORSIGALG_HASH_NAME);
  961. if (k->tls_name == NULL) {
  962. xor_freekey(k);
  963. return NULL;
  964. }
  965. return k;
  966. }
  967. static const OSSL_DISPATCH xor_xorhmacsig_keymgmt_functions[] = {
  968. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey },
  969. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
  970. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
  971. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  972. (void (*)(void))xor_gen_settable_params },
  973. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsig_gen },
  974. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
  975. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
  976. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
  977. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
  978. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
  979. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
  980. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup },
  981. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey },
  982. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import },
  983. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types },
  984. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export },
  985. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types },
  986. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load },
  987. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match },
  988. OSSL_DISPATCH_END
  989. };
  990. static const OSSL_DISPATCH xor_xorhmacsha2sig_keymgmt_functions[] = {
  991. { OSSL_FUNC_KEYMGMT_NEW, (void (*)(void))xor_newkey },
  992. { OSSL_FUNC_KEYMGMT_GEN_INIT, (void (*)(void))xor_gen_init },
  993. { OSSL_FUNC_KEYMGMT_GEN_SET_PARAMS, (void (*)(void))xor_gen_set_params },
  994. { OSSL_FUNC_KEYMGMT_GEN_SETTABLE_PARAMS,
  995. (void (*)(void))xor_gen_settable_params },
  996. { OSSL_FUNC_KEYMGMT_GEN, (void (*)(void))xor_xorhmacsha2sig_gen },
  997. { OSSL_FUNC_KEYMGMT_GEN_CLEANUP, (void (*)(void))xor_gen_cleanup },
  998. { OSSL_FUNC_KEYMGMT_GET_PARAMS, (void (*) (void))xor_get_params },
  999. { OSSL_FUNC_KEYMGMT_GETTABLE_PARAMS, (void (*) (void))xor_gettable_params },
  1000. { OSSL_FUNC_KEYMGMT_SET_PARAMS, (void (*) (void))xor_set_params },
  1001. { OSSL_FUNC_KEYMGMT_SETTABLE_PARAMS, (void (*) (void))xor_settable_params },
  1002. { OSSL_FUNC_KEYMGMT_HAS, (void (*)(void))xor_has },
  1003. { OSSL_FUNC_KEYMGMT_DUP, (void (*)(void))xor_dup },
  1004. { OSSL_FUNC_KEYMGMT_FREE, (void (*)(void))xor_freekey },
  1005. { OSSL_FUNC_KEYMGMT_IMPORT, (void (*)(void))xor_import },
  1006. { OSSL_FUNC_KEYMGMT_IMPORT_TYPES, (void (*)(void))xor_import_types },
  1007. { OSSL_FUNC_KEYMGMT_EXPORT, (void (*)(void))xor_export },
  1008. { OSSL_FUNC_KEYMGMT_EXPORT_TYPES, (void (*)(void))xor_export_types },
  1009. { OSSL_FUNC_KEYMGMT_LOAD, (void (*)(void))xor_load },
  1010. { OSSL_FUNC_KEYMGMT_MATCH, (void (*)(void))xor_match },
  1011. OSSL_DISPATCH_END
  1012. };
  1013. typedef enum {
  1014. KEY_OP_PUBLIC,
  1015. KEY_OP_PRIVATE,
  1016. KEY_OP_KEYGEN
  1017. } xor_key_op_t;
  1018. /* Re-create XORKEY from encoding(s): Same end-state as after key-gen */
  1019. static XORKEY *xor_key_op(const X509_ALGOR *palg,
  1020. const unsigned char *p, int plen,
  1021. xor_key_op_t op,
  1022. OSSL_LIB_CTX *libctx, const char *propq)
  1023. {
  1024. XORKEY *key = NULL;
  1025. int nid = NID_undef;
  1026. if (palg != NULL) {
  1027. int ptype;
  1028. /* Algorithm parameters must be absent */
  1029. X509_ALGOR_get0(NULL, &ptype, NULL, palg);
  1030. if (ptype != V_ASN1_UNDEF || palg->algorithm == NULL) {
  1031. ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING);
  1032. return 0;
  1033. }
  1034. nid = OBJ_obj2nid(palg->algorithm);
  1035. }
  1036. if (p == NULL || nid == EVP_PKEY_NONE || nid == NID_undef) {
  1037. ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING);
  1038. return 0;
  1039. }
  1040. key = xor_newkey(NULL);
  1041. if (key == NULL) {
  1042. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  1043. return 0;
  1044. }
  1045. if (XOR_KEY_SIZE != plen) {
  1046. ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_ENCODING);
  1047. goto err;
  1048. }
  1049. if (op == KEY_OP_PUBLIC) {
  1050. memcpy(key->pubkey, p, plen);
  1051. key->haspubkey = 1;
  1052. } else {
  1053. memcpy(key->privkey, p, plen);
  1054. key->hasprivkey = 1;
  1055. }
  1056. key->tls_name = OPENSSL_strdup(OBJ_nid2sn(nid));
  1057. if (key->tls_name == NULL)
  1058. goto err;
  1059. return key;
  1060. err:
  1061. xor_freekey(key);
  1062. return NULL;
  1063. }
  1064. static XORKEY *xor_key_from_x509pubkey(const X509_PUBKEY *xpk,
  1065. OSSL_LIB_CTX *libctx, const char *propq)
  1066. {
  1067. const unsigned char *p;
  1068. int plen;
  1069. X509_ALGOR *palg;
  1070. if (!xpk || (!X509_PUBKEY_get0_param(NULL, &p, &plen, &palg, xpk))) {
  1071. return NULL;
  1072. }
  1073. return xor_key_op(palg, p, plen, KEY_OP_PUBLIC, libctx, propq);
  1074. }
  1075. static XORKEY *xor_key_from_pkcs8(const PKCS8_PRIV_KEY_INFO *p8inf,
  1076. OSSL_LIB_CTX *libctx, const char *propq)
  1077. {
  1078. XORKEY *xork = NULL;
  1079. const unsigned char *p;
  1080. int plen;
  1081. ASN1_OCTET_STRING *oct = NULL;
  1082. const X509_ALGOR *palg;
  1083. if (!PKCS8_pkey_get0(NULL, &p, &plen, &palg, p8inf))
  1084. return 0;
  1085. oct = d2i_ASN1_OCTET_STRING(NULL, &p, plen);
  1086. if (oct == NULL) {
  1087. p = NULL;
  1088. plen = 0;
  1089. } else {
  1090. p = ASN1_STRING_get0_data(oct);
  1091. plen = ASN1_STRING_length(oct);
  1092. }
  1093. xork = xor_key_op(palg, p, plen, KEY_OP_PRIVATE,
  1094. libctx, propq);
  1095. ASN1_OCTET_STRING_free(oct);
  1096. return xork;
  1097. }
  1098. static const OSSL_ALGORITHM tls_prov_keymgmt[] = {
  1099. /*
  1100. * Obviously this is not FIPS approved, but in order to test in conjunction
  1101. * with the FIPS provider we pretend that it is.
  1102. */
  1103. { "XOR", "provider=tls-provider,fips=yes",
  1104. xor_keymgmt_functions },
  1105. { XORSIGALG_NAME, "provider=tls-provider,fips=yes",
  1106. xor_xorhmacsig_keymgmt_functions },
  1107. { XORSIGALG_HASH_NAME,
  1108. "provider=tls-provider,fips=yes",
  1109. xor_xorhmacsha2sig_keymgmt_functions },
  1110. { NULL, NULL, NULL }
  1111. };
  1112. struct key2any_ctx_st {
  1113. PROV_XOR_CTX *provctx;
  1114. /* Set to 0 if parameters should not be saved (dsa only) */
  1115. int save_parameters;
  1116. /* Set to 1 if intending to encrypt/decrypt, otherwise 0 */
  1117. int cipher_intent;
  1118. EVP_CIPHER *cipher;
  1119. OSSL_PASSPHRASE_CALLBACK *pwcb;
  1120. void *pwcbarg;
  1121. };
  1122. typedef int check_key_type_fn(const void *key, int nid);
  1123. typedef int key_to_paramstring_fn(const void *key, int nid, int save,
  1124. void **str, int *strtype);
  1125. typedef int key_to_der_fn(BIO *out, const void *key,
  1126. int key_nid, const char *pemname,
  1127. key_to_paramstring_fn *p2s, i2d_of_void *k2d,
  1128. struct key2any_ctx_st *ctx);
  1129. typedef int write_bio_of_void_fn(BIO *bp, const void *x);
  1130. /* Free the blob allocated during key_to_paramstring_fn */
  1131. static void free_asn1_data(int type, void *data)
  1132. {
  1133. switch(type) {
  1134. case V_ASN1_OBJECT:
  1135. ASN1_OBJECT_free(data);
  1136. break;
  1137. case V_ASN1_SEQUENCE:
  1138. ASN1_STRING_free(data);
  1139. break;
  1140. }
  1141. }
  1142. static PKCS8_PRIV_KEY_INFO *key_to_p8info(const void *key, int key_nid,
  1143. void *params, int params_type,
  1144. i2d_of_void *k2d)
  1145. {
  1146. /* der, derlen store the key DER output and its length */
  1147. unsigned char *der = NULL;
  1148. int derlen;
  1149. /* The final PKCS#8 info */
  1150. PKCS8_PRIV_KEY_INFO *p8info = NULL;
  1151. if ((p8info = PKCS8_PRIV_KEY_INFO_new()) == NULL
  1152. || (derlen = k2d(key, &der)) <= 0
  1153. || !PKCS8_pkey_set0(p8info, OBJ_nid2obj(key_nid), 0,
  1154. V_ASN1_UNDEF, NULL,
  1155. der, derlen)) {
  1156. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  1157. PKCS8_PRIV_KEY_INFO_free(p8info);
  1158. OPENSSL_free(der);
  1159. p8info = NULL;
  1160. }
  1161. return p8info;
  1162. }
  1163. static X509_SIG *p8info_to_encp8(PKCS8_PRIV_KEY_INFO *p8info,
  1164. struct key2any_ctx_st *ctx)
  1165. {
  1166. X509_SIG *p8 = NULL;
  1167. char kstr[PEM_BUFSIZE];
  1168. size_t klen = 0;
  1169. OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx);
  1170. if (ctx->cipher == NULL || ctx->pwcb == NULL)
  1171. return NULL;
  1172. if (!ctx->pwcb(kstr, PEM_BUFSIZE, &klen, NULL, ctx->pwcbarg)) {
  1173. ERR_raise(ERR_LIB_USER, PROV_R_UNABLE_TO_GET_PASSPHRASE);
  1174. return NULL;
  1175. }
  1176. /* First argument == -1 means "standard" */
  1177. p8 = PKCS8_encrypt_ex(-1, ctx->cipher, kstr, klen, NULL, 0, 0, p8info, libctx, NULL);
  1178. OPENSSL_cleanse(kstr, klen);
  1179. return p8;
  1180. }
  1181. static X509_SIG *key_to_encp8(const void *key, int key_nid,
  1182. void *params, int params_type,
  1183. i2d_of_void *k2d, struct key2any_ctx_st *ctx)
  1184. {
  1185. PKCS8_PRIV_KEY_INFO *p8info =
  1186. key_to_p8info(key, key_nid, params, params_type, k2d);
  1187. X509_SIG *p8 = NULL;
  1188. if (p8info == NULL) {
  1189. free_asn1_data(params_type, params);
  1190. } else {
  1191. p8 = p8info_to_encp8(p8info, ctx);
  1192. PKCS8_PRIV_KEY_INFO_free(p8info);
  1193. }
  1194. return p8;
  1195. }
  1196. static X509_PUBKEY *xorx_key_to_pubkey(const void *key, int key_nid,
  1197. void *params, int params_type,
  1198. i2d_of_void k2d)
  1199. {
  1200. /* der, derlen store the key DER output and its length */
  1201. unsigned char *der = NULL;
  1202. int derlen;
  1203. /* The final X509_PUBKEY */
  1204. X509_PUBKEY *xpk = NULL;
  1205. if ((xpk = X509_PUBKEY_new()) == NULL
  1206. || (derlen = k2d(key, &der)) <= 0
  1207. || !X509_PUBKEY_set0_param(xpk, OBJ_nid2obj(key_nid),
  1208. V_ASN1_UNDEF, NULL,
  1209. der, derlen)) {
  1210. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  1211. X509_PUBKEY_free(xpk);
  1212. OPENSSL_free(der);
  1213. xpk = NULL;
  1214. }
  1215. return xpk;
  1216. }
  1217. /*
  1218. * key_to_epki_* produce encoded output with the private key data in a
  1219. * EncryptedPrivateKeyInfo structure (defined by PKCS#8). They require
  1220. * that there's an intent to encrypt, anything else is an error.
  1221. *
  1222. * key_to_pki_* primarily produce encoded output with the private key data
  1223. * in a PrivateKeyInfo structure (also defined by PKCS#8). However, if
  1224. * there is an intent to encrypt the data, the corresponding key_to_epki_*
  1225. * function is used instead.
  1226. *
  1227. * key_to_spki_* produce encoded output with the public key data in an
  1228. * X.509 SubjectPublicKeyInfo.
  1229. *
  1230. * Key parameters don't have any defined envelopment of this kind, but are
  1231. * included in some manner in the output from the functions described above,
  1232. * either in the AlgorithmIdentifier's parameter field, or as part of the
  1233. * key data itself.
  1234. */
  1235. static int key_to_epki_der_priv_bio(BIO *out, const void *key,
  1236. int key_nid,
  1237. ossl_unused const char *pemname,
  1238. key_to_paramstring_fn *p2s,
  1239. i2d_of_void *k2d,
  1240. struct key2any_ctx_st *ctx)
  1241. {
  1242. int ret = 0;
  1243. void *str = NULL;
  1244. int strtype = V_ASN1_UNDEF;
  1245. X509_SIG *p8;
  1246. if (!ctx->cipher_intent)
  1247. return 0;
  1248. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1249. &str, &strtype))
  1250. return 0;
  1251. p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
  1252. if (p8 != NULL)
  1253. ret = i2d_PKCS8_bio(out, p8);
  1254. X509_SIG_free(p8);
  1255. return ret;
  1256. }
  1257. static int key_to_epki_pem_priv_bio(BIO *out, const void *key,
  1258. int key_nid,
  1259. ossl_unused const char *pemname,
  1260. key_to_paramstring_fn *p2s,
  1261. i2d_of_void *k2d,
  1262. struct key2any_ctx_st *ctx)
  1263. {
  1264. int ret = 0;
  1265. void *str = NULL;
  1266. int strtype = V_ASN1_UNDEF;
  1267. X509_SIG *p8;
  1268. if (!ctx->cipher_intent)
  1269. return 0;
  1270. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1271. &str, &strtype))
  1272. return 0;
  1273. p8 = key_to_encp8(key, key_nid, str, strtype, k2d, ctx);
  1274. if (p8 != NULL)
  1275. ret = PEM_write_bio_PKCS8(out, p8);
  1276. X509_SIG_free(p8);
  1277. return ret;
  1278. }
  1279. static int key_to_pki_der_priv_bio(BIO *out, const void *key,
  1280. int key_nid,
  1281. ossl_unused const char *pemname,
  1282. key_to_paramstring_fn *p2s,
  1283. i2d_of_void *k2d,
  1284. struct key2any_ctx_st *ctx)
  1285. {
  1286. int ret = 0;
  1287. void *str = NULL;
  1288. int strtype = V_ASN1_UNDEF;
  1289. PKCS8_PRIV_KEY_INFO *p8info;
  1290. if (ctx->cipher_intent)
  1291. return key_to_epki_der_priv_bio(out, key, key_nid, pemname,
  1292. p2s, k2d, ctx);
  1293. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1294. &str, &strtype))
  1295. return 0;
  1296. p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
  1297. if (p8info != NULL)
  1298. ret = i2d_PKCS8_PRIV_KEY_INFO_bio(out, p8info);
  1299. else
  1300. free_asn1_data(strtype, str);
  1301. PKCS8_PRIV_KEY_INFO_free(p8info);
  1302. return ret;
  1303. }
  1304. static int key_to_pki_pem_priv_bio(BIO *out, const void *key,
  1305. int key_nid,
  1306. ossl_unused const char *pemname,
  1307. key_to_paramstring_fn *p2s,
  1308. i2d_of_void *k2d,
  1309. struct key2any_ctx_st *ctx)
  1310. {
  1311. int ret = 0;
  1312. void *str = NULL;
  1313. int strtype = V_ASN1_UNDEF;
  1314. PKCS8_PRIV_KEY_INFO *p8info;
  1315. if (ctx->cipher_intent)
  1316. return key_to_epki_pem_priv_bio(out, key, key_nid, pemname,
  1317. p2s, k2d, ctx);
  1318. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1319. &str, &strtype))
  1320. return 0;
  1321. p8info = key_to_p8info(key, key_nid, str, strtype, k2d);
  1322. if (p8info != NULL)
  1323. ret = PEM_write_bio_PKCS8_PRIV_KEY_INFO(out, p8info);
  1324. else
  1325. free_asn1_data(strtype, str);
  1326. PKCS8_PRIV_KEY_INFO_free(p8info);
  1327. return ret;
  1328. }
  1329. static int key_to_spki_der_pub_bio(BIO *out, const void *key,
  1330. int key_nid,
  1331. ossl_unused const char *pemname,
  1332. key_to_paramstring_fn *p2s,
  1333. i2d_of_void *k2d,
  1334. struct key2any_ctx_st *ctx)
  1335. {
  1336. int ret = 0;
  1337. X509_PUBKEY *xpk = NULL;
  1338. void *str = NULL;
  1339. int strtype = V_ASN1_UNDEF;
  1340. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1341. &str, &strtype))
  1342. return 0;
  1343. xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d);
  1344. if (xpk != NULL)
  1345. ret = i2d_X509_PUBKEY_bio(out, xpk);
  1346. X509_PUBKEY_free(xpk);
  1347. return ret;
  1348. }
  1349. static int key_to_spki_pem_pub_bio(BIO *out, const void *key,
  1350. int key_nid,
  1351. ossl_unused const char *pemname,
  1352. key_to_paramstring_fn *p2s,
  1353. i2d_of_void *k2d,
  1354. struct key2any_ctx_st *ctx)
  1355. {
  1356. int ret = 0;
  1357. X509_PUBKEY *xpk = NULL;
  1358. void *str = NULL;
  1359. int strtype = V_ASN1_UNDEF;
  1360. if (p2s != NULL && !p2s(key, key_nid, ctx->save_parameters,
  1361. &str, &strtype))
  1362. return 0;
  1363. xpk = xorx_key_to_pubkey(key, key_nid, str, strtype, k2d);
  1364. if (xpk != NULL)
  1365. ret = PEM_write_bio_X509_PUBKEY(out, xpk);
  1366. else
  1367. free_asn1_data(strtype, str);
  1368. /* Also frees |str| */
  1369. X509_PUBKEY_free(xpk);
  1370. return ret;
  1371. }
  1372. /* ---------------------------------------------------------------------- */
  1373. static int prepare_xorx_params(const void *xorxkey, int nid, int save,
  1374. void **pstr, int *pstrtype)
  1375. {
  1376. ASN1_OBJECT *params = NULL;
  1377. XORKEY *k = (XORKEY*)xorxkey;
  1378. if (k->tls_name && OBJ_sn2nid(k->tls_name) != nid) {
  1379. ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY);
  1380. return 0;
  1381. }
  1382. if (nid == NID_undef) {
  1383. ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID);
  1384. return 0;
  1385. }
  1386. params = OBJ_nid2obj(nid);
  1387. if (params == NULL || OBJ_length(params) == 0) {
  1388. /* unexpected error */
  1389. ERR_raise(ERR_LIB_USER, XORPROV_R_MISSING_OID);
  1390. ASN1_OBJECT_free(params);
  1391. return 0;
  1392. }
  1393. *pstr = params;
  1394. *pstrtype = V_ASN1_OBJECT;
  1395. return 1;
  1396. }
  1397. static int xorx_spki_pub_to_der(const void *vecxkey, unsigned char **pder)
  1398. {
  1399. const XORKEY *xorxkey = vecxkey;
  1400. unsigned char *keyblob;
  1401. int retlen;
  1402. if (xorxkey == NULL) {
  1403. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER);
  1404. return 0;
  1405. }
  1406. keyblob = OPENSSL_memdup(xorxkey->pubkey, retlen = XOR_KEY_SIZE);
  1407. if (keyblob == NULL) {
  1408. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  1409. return 0;
  1410. }
  1411. *pder = keyblob;
  1412. return retlen;
  1413. }
  1414. static int xorx_pki_priv_to_der(const void *vecxkey, unsigned char **pder)
  1415. {
  1416. XORKEY *xorxkey = (XORKEY *)vecxkey;
  1417. unsigned char* buf = NULL;
  1418. ASN1_OCTET_STRING oct;
  1419. int keybloblen;
  1420. if (xorxkey == NULL) {
  1421. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER);
  1422. return 0;
  1423. }
  1424. buf = OPENSSL_secure_malloc(XOR_KEY_SIZE);
  1425. memcpy(buf, xorxkey->privkey, XOR_KEY_SIZE);
  1426. oct.data = buf;
  1427. oct.length = XOR_KEY_SIZE;
  1428. oct.flags = 0;
  1429. keybloblen = i2d_ASN1_OCTET_STRING(&oct, pder);
  1430. if (keybloblen < 0) {
  1431. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  1432. keybloblen = 0;
  1433. }
  1434. OPENSSL_secure_clear_free(buf, XOR_KEY_SIZE);
  1435. return keybloblen;
  1436. }
  1437. # define xorx_epki_priv_to_der xorx_pki_priv_to_der
  1438. /*
  1439. * XORX only has PKCS#8 / SubjectPublicKeyInfo
  1440. * representation, so we don't define xorx_type_specific_[priv,pub,params]_to_der.
  1441. */
  1442. # define xorx_check_key_type NULL
  1443. # define xorhmacsig_evp_type 0
  1444. # define xorhmacsig_input_type XORSIGALG_NAME
  1445. # define xorhmacsig_pem_type XORSIGALG_NAME
  1446. # define xorhmacsha2sig_evp_type 0
  1447. # define xorhmacsha2sig_input_type XORSIGALG_HASH_NAME
  1448. # define xorhmacsha2sig_pem_type XORSIGALG_HASH_NAME
  1449. /* ---------------------------------------------------------------------- */
  1450. static OSSL_FUNC_decoder_newctx_fn key2any_newctx;
  1451. static OSSL_FUNC_decoder_freectx_fn key2any_freectx;
  1452. static void *key2any_newctx(void *provctx)
  1453. {
  1454. struct key2any_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  1455. if (ctx != NULL) {
  1456. ctx->provctx = provctx;
  1457. ctx->save_parameters = 1;
  1458. }
  1459. return ctx;
  1460. }
  1461. static void key2any_freectx(void *vctx)
  1462. {
  1463. struct key2any_ctx_st *ctx = vctx;
  1464. EVP_CIPHER_free(ctx->cipher);
  1465. OPENSSL_free(ctx);
  1466. }
  1467. static const OSSL_PARAM *key2any_settable_ctx_params(ossl_unused void *provctx)
  1468. {
  1469. static const OSSL_PARAM settables[] = {
  1470. OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_CIPHER, NULL, 0),
  1471. OSSL_PARAM_utf8_string(OSSL_ENCODER_PARAM_PROPERTIES, NULL, 0),
  1472. OSSL_PARAM_END,
  1473. };
  1474. return settables;
  1475. }
  1476. static int key2any_set_ctx_params(void *vctx, const OSSL_PARAM params[])
  1477. {
  1478. struct key2any_ctx_st *ctx = vctx;
  1479. OSSL_LIB_CTX *libctx = PROV_XOR_LIBCTX_OF(ctx->provctx);
  1480. const OSSL_PARAM *cipherp =
  1481. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_CIPHER);
  1482. const OSSL_PARAM *propsp =
  1483. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_PROPERTIES);
  1484. const OSSL_PARAM *save_paramsp =
  1485. OSSL_PARAM_locate_const(params, OSSL_ENCODER_PARAM_SAVE_PARAMETERS);
  1486. if (cipherp != NULL) {
  1487. const char *ciphername = NULL;
  1488. const char *props = NULL;
  1489. if (!OSSL_PARAM_get_utf8_string_ptr(cipherp, &ciphername))
  1490. return 0;
  1491. if (propsp != NULL && !OSSL_PARAM_get_utf8_string_ptr(propsp, &props))
  1492. return 0;
  1493. EVP_CIPHER_free(ctx->cipher);
  1494. ctx->cipher = NULL;
  1495. ctx->cipher_intent = ciphername != NULL;
  1496. if (ciphername != NULL
  1497. && ((ctx->cipher =
  1498. EVP_CIPHER_fetch(libctx, ciphername, props)) == NULL)) {
  1499. return 0;
  1500. }
  1501. }
  1502. if (save_paramsp != NULL) {
  1503. if (!OSSL_PARAM_get_int(save_paramsp, &ctx->save_parameters)) {
  1504. return 0;
  1505. }
  1506. }
  1507. return 1;
  1508. }
  1509. static int key2any_check_selection(int selection, int selection_mask)
  1510. {
  1511. /*
  1512. * The selections are kinda sorta "levels", i.e. each selection given
  1513. * here is assumed to include those following.
  1514. */
  1515. int checks[] = {
  1516. OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  1517. OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  1518. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  1519. };
  1520. size_t i;
  1521. /* The decoder implementations made here support guessing */
  1522. if (selection == 0)
  1523. return 1;
  1524. for (i = 0; i < OSSL_NELEM(checks); i++) {
  1525. int check1 = (selection & checks[i]) != 0;
  1526. int check2 = (selection_mask & checks[i]) != 0;
  1527. /*
  1528. * If the caller asked for the currently checked bit(s), return
  1529. * whether the decoder description says it's supported.
  1530. */
  1531. if (check1)
  1532. return check2;
  1533. }
  1534. /* This should be dead code, but just to be safe... */
  1535. return 0;
  1536. }
  1537. static int key2any_encode(struct key2any_ctx_st *ctx, OSSL_CORE_BIO *cout,
  1538. const void *key, const char* typestr, const char *pemname,
  1539. key_to_der_fn *writer,
  1540. OSSL_PASSPHRASE_CALLBACK *pwcb, void *pwcbarg,
  1541. key_to_paramstring_fn *key2paramstring,
  1542. i2d_of_void *key2der)
  1543. {
  1544. int ret = 0;
  1545. int type = OBJ_sn2nid(typestr);
  1546. if (key == NULL || type <= 0) {
  1547. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_NULL_PARAMETER);
  1548. } else if (writer != NULL) {
  1549. BIO *out = BIO_new_from_core_bio(ctx->provctx->libctx, cout);
  1550. if (out != NULL) {
  1551. ctx->pwcb = pwcb;
  1552. ctx->pwcbarg = pwcbarg;
  1553. ret = writer(out, key, type, pemname, key2paramstring, key2der, ctx);
  1554. }
  1555. BIO_free(out);
  1556. } else {
  1557. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT);
  1558. }
  1559. return ret;
  1560. }
  1561. #define DO_ENC_PRIVATE_KEY_selection_mask OSSL_KEYMGMT_SELECT_PRIVATE_KEY
  1562. #define DO_ENC_PRIVATE_KEY(impl, type, kind, output) \
  1563. if ((selection & DO_ENC_PRIVATE_KEY_selection_mask) != 0) \
  1564. return key2any_encode(ctx, cout, key, impl##_pem_type, \
  1565. impl##_pem_type " PRIVATE KEY", \
  1566. key_to_##kind##_##output##_priv_bio, \
  1567. cb, cbarg, prepare_##type##_params, \
  1568. type##_##kind##_priv_to_der);
  1569. #define DO_ENC_PUBLIC_KEY_selection_mask OSSL_KEYMGMT_SELECT_PUBLIC_KEY
  1570. #define DO_ENC_PUBLIC_KEY(impl, type, kind, output) \
  1571. if ((selection & DO_ENC_PUBLIC_KEY_selection_mask) != 0) \
  1572. return key2any_encode(ctx, cout, key, impl##_pem_type, \
  1573. impl##_pem_type " PUBLIC KEY", \
  1574. key_to_##kind##_##output##_pub_bio, \
  1575. cb, cbarg, prepare_##type##_params, \
  1576. type##_##kind##_pub_to_der);
  1577. #define DO_ENC_PARAMETERS_selection_mask OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  1578. #define DO_ENC_PARAMETERS(impl, type, kind, output) \
  1579. if ((selection & DO_ENC_PARAMETERS_selection_mask) != 0) \
  1580. return key2any_encode(ctx, cout, key, impl##_pem_type, \
  1581. impl##_pem_type " PARAMETERS", \
  1582. key_to_##kind##_##output##_param_bio, \
  1583. NULL, NULL, NULL, \
  1584. type##_##kind##_params_to_der);
  1585. /*-
  1586. * Implement the kinds of output structure that can be produced. They are
  1587. * referred to by name, and for each name, the following macros are defined
  1588. * (braces not included):
  1589. *
  1590. * DO_{kind}_selection_mask
  1591. *
  1592. * A mask of selection bits that must not be zero. This is used as a
  1593. * selection criterion for each implementation.
  1594. * This mask must never be zero.
  1595. *
  1596. * DO_{kind}
  1597. *
  1598. * The performing macro. It must use the DO_ macros defined above,
  1599. * always in this order:
  1600. *
  1601. * - DO_PRIVATE_KEY
  1602. * - DO_PUBLIC_KEY
  1603. * - DO_PARAMETERS
  1604. *
  1605. * Any of those may be omitted, but the relative order must still be
  1606. * the same.
  1607. */
  1608. /*
  1609. * PKCS#8 defines two structures for private keys only:
  1610. * - PrivateKeyInfo (raw unencrypted form)
  1611. * - EncryptedPrivateKeyInfo (encrypted wrapping)
  1612. *
  1613. * To allow a certain amount of flexibility, we allow the routines
  1614. * for PrivateKeyInfo to also produce EncryptedPrivateKeyInfo if a
  1615. * passphrase callback has been passed to them.
  1616. */
  1617. #define DO_ENC_PrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask
  1618. #define DO_ENC_PrivateKeyInfo(impl, type, output) \
  1619. DO_ENC_PRIVATE_KEY(impl, type, pki, output)
  1620. #define DO_ENC_EncryptedPrivateKeyInfo_selection_mask DO_ENC_PRIVATE_KEY_selection_mask
  1621. #define DO_ENC_EncryptedPrivateKeyInfo(impl, type, output) \
  1622. DO_ENC_PRIVATE_KEY(impl, type, epki, output)
  1623. /* SubjectPublicKeyInfo is a structure for public keys only */
  1624. #define DO_ENC_SubjectPublicKeyInfo_selection_mask DO_ENC_PUBLIC_KEY_selection_mask
  1625. #define DO_ENC_SubjectPublicKeyInfo(impl, type, output) \
  1626. DO_ENC_PUBLIC_KEY(impl, type, spki, output)
  1627. /*
  1628. * MAKE_ENCODER is the single driver for creating OSSL_DISPATCH tables.
  1629. * It takes the following arguments:
  1630. *
  1631. * impl This is the key type name that's being implemented.
  1632. * type This is the type name for the set of functions that implement
  1633. * the key type. For example, ed25519, ed448, x25519 and x448
  1634. * are all implemented with the exact same set of functions.
  1635. * kind What kind of support to implement. These translate into
  1636. * the DO_##kind macros above.
  1637. * output The output type to implement. may be der or pem.
  1638. *
  1639. * The resulting OSSL_DISPATCH array gets the following name (expressed in
  1640. * C preprocessor terms) from those arguments:
  1641. *
  1642. * xor_##impl##_to_##kind##_##output##_encoder_functions
  1643. */
  1644. #define MAKE_ENCODER(impl, type, kind, output) \
  1645. static OSSL_FUNC_encoder_import_object_fn \
  1646. impl##_to_##kind##_##output##_import_object; \
  1647. static OSSL_FUNC_encoder_free_object_fn \
  1648. impl##_to_##kind##_##output##_free_object; \
  1649. static OSSL_FUNC_encoder_encode_fn \
  1650. impl##_to_##kind##_##output##_encode; \
  1651. \
  1652. static void * \
  1653. impl##_to_##kind##_##output##_import_object(void *vctx, int selection, \
  1654. const OSSL_PARAM params[]) \
  1655. { \
  1656. struct key2any_ctx_st *ctx = vctx; \
  1657. \
  1658. return xor_prov_import_key(xor_##impl##_keymgmt_functions, \
  1659. ctx->provctx, selection, params); \
  1660. } \
  1661. static void impl##_to_##kind##_##output##_free_object(void *key) \
  1662. { \
  1663. xor_prov_free_key(xor_##impl##_keymgmt_functions, key); \
  1664. } \
  1665. static int impl##_to_##kind##_##output##_does_selection(void *ctx, \
  1666. int selection) \
  1667. { \
  1668. return key2any_check_selection(selection, \
  1669. DO_ENC_##kind##_selection_mask); \
  1670. } \
  1671. static int \
  1672. impl##_to_##kind##_##output##_encode(void *ctx, OSSL_CORE_BIO *cout, \
  1673. const void *key, \
  1674. const OSSL_PARAM key_abstract[], \
  1675. int selection, \
  1676. OSSL_PASSPHRASE_CALLBACK *cb, \
  1677. void *cbarg) \
  1678. { \
  1679. /* We don't deal with abstract objects */ \
  1680. if (key_abstract != NULL) { \
  1681. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \
  1682. return 0; \
  1683. } \
  1684. DO_ENC_##kind(impl, type, output) \
  1685. \
  1686. ERR_raise(ERR_LIB_USER, ERR_R_PASSED_INVALID_ARGUMENT); \
  1687. return 0; \
  1688. } \
  1689. static const OSSL_DISPATCH \
  1690. xor_##impl##_to_##kind##_##output##_encoder_functions[] = { \
  1691. { OSSL_FUNC_ENCODER_NEWCTX, \
  1692. (void (*)(void))key2any_newctx }, \
  1693. { OSSL_FUNC_ENCODER_FREECTX, \
  1694. (void (*)(void))key2any_freectx }, \
  1695. { OSSL_FUNC_ENCODER_SETTABLE_CTX_PARAMS, \
  1696. (void (*)(void))key2any_settable_ctx_params }, \
  1697. { OSSL_FUNC_ENCODER_SET_CTX_PARAMS, \
  1698. (void (*)(void))key2any_set_ctx_params }, \
  1699. { OSSL_FUNC_ENCODER_DOES_SELECTION, \
  1700. (void (*)(void))impl##_to_##kind##_##output##_does_selection }, \
  1701. { OSSL_FUNC_ENCODER_IMPORT_OBJECT, \
  1702. (void (*)(void))impl##_to_##kind##_##output##_import_object }, \
  1703. { OSSL_FUNC_ENCODER_FREE_OBJECT, \
  1704. (void (*)(void))impl##_to_##kind##_##output##_free_object }, \
  1705. { OSSL_FUNC_ENCODER_ENCODE, \
  1706. (void (*)(void))impl##_to_##kind##_##output##_encode }, \
  1707. OSSL_DISPATCH_END \
  1708. }
  1709. /*
  1710. * Replacements for i2d_{TYPE}PrivateKey, i2d_{TYPE}PublicKey,
  1711. * i2d_{TYPE}params, as they exist.
  1712. */
  1713. /*
  1714. * PKCS#8 and SubjectPublicKeyInfo support. This may duplicate some of the
  1715. * implementations specified above, but are more specific.
  1716. * The SubjectPublicKeyInfo implementations also replace the
  1717. * PEM_write_bio_{TYPE}_PUBKEY functions.
  1718. * For PEM, these are expected to be used by PEM_write_bio_PrivateKey(),
  1719. * PEM_write_bio_PUBKEY() and PEM_write_bio_Parameters().
  1720. */
  1721. MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, der);
  1722. MAKE_ENCODER(xorhmacsig, xorx, EncryptedPrivateKeyInfo, pem);
  1723. MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, der);
  1724. MAKE_ENCODER(xorhmacsig, xorx, PrivateKeyInfo, pem);
  1725. MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, der);
  1726. MAKE_ENCODER(xorhmacsig, xorx, SubjectPublicKeyInfo, pem);
  1727. MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, der);
  1728. MAKE_ENCODER(xorhmacsha2sig, xorx, EncryptedPrivateKeyInfo, pem);
  1729. MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, der);
  1730. MAKE_ENCODER(xorhmacsha2sig, xorx, PrivateKeyInfo, pem);
  1731. MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, der);
  1732. MAKE_ENCODER(xorhmacsha2sig, xorx, SubjectPublicKeyInfo, pem);
  1733. static const OSSL_ALGORITHM tls_prov_encoder[] = {
  1734. #define ENCODER_PROVIDER "tls-provider"
  1735. #ifndef ENCODER_PROVIDER
  1736. # error Macro ENCODER_PROVIDER undefined
  1737. #endif
  1738. #define ENCODER_STRUCTURE_PKCS8 "pkcs8"
  1739. #define ENCODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo"
  1740. #define ENCODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo"
  1741. #define ENCODER_STRUCTURE_EncryptedPrivateKeyInfo "EncryptedPrivateKeyInfo"
  1742. #define ENCODER_STRUCTURE_PKCS1 "pkcs1"
  1743. #define ENCODER_STRUCTURE_PKCS3 "pkcs3"
  1744. /* Arguments are prefixed with '_' to avoid build breaks on certain platforms */
  1745. /*
  1746. * Obviously this is not FIPS approved, but in order to test in conjunction
  1747. * with the FIPS provider we pretend that it is.
  1748. */
  1749. #define ENCODER_TEXT(_name, _sym) \
  1750. { _name, \
  1751. "provider=" ENCODER_PROVIDER ",fips=yes,output=text", \
  1752. (xor_##_sym##_to_text_encoder_functions) }
  1753. #define ENCODER(_name, _sym, _fips, _output) \
  1754. { _name, \
  1755. "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output, \
  1756. (xor_##_sym##_to_##_output##_encoder_functions) }
  1757. #define ENCODER_w_structure(_name, _sym, _output, _structure) \
  1758. { _name, \
  1759. "provider=" ENCODER_PROVIDER ",fips=yes,output=" #_output \
  1760. ",structure=" ENCODER_STRUCTURE_##_structure, \
  1761. (xor_##_sym##_to_##_structure##_##_output##_encoder_functions) }
  1762. /*
  1763. * Entries for human text "encoders"
  1764. */
  1765. /*
  1766. * Entries for PKCS#8 and SubjectPublicKeyInfo.
  1767. * The "der" ones are added convenience for any user that wants to use
  1768. * OSSL_ENCODER directly.
  1769. * The "pem" ones also support PEM_write_bio_PrivateKey() and
  1770. * PEM_write_bio_PUBKEY().
  1771. */
  1772. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, PrivateKeyInfo),
  1773. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, PrivateKeyInfo),
  1774. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, EncryptedPrivateKeyInfo),
  1775. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, EncryptedPrivateKeyInfo),
  1776. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, der, SubjectPublicKeyInfo),
  1777. ENCODER_w_structure(XORSIGALG_NAME, xorhmacsig, pem, SubjectPublicKeyInfo),
  1778. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1779. der, PrivateKeyInfo),
  1780. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1781. pem, PrivateKeyInfo),
  1782. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1783. der, EncryptedPrivateKeyInfo),
  1784. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1785. pem, EncryptedPrivateKeyInfo),
  1786. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1787. der, SubjectPublicKeyInfo),
  1788. ENCODER_w_structure(XORSIGALG_HASH_NAME, xorhmacsha2sig,
  1789. pem, SubjectPublicKeyInfo),
  1790. #undef ENCODER_PROVIDER
  1791. { NULL, NULL, NULL }
  1792. };
  1793. struct der2key_ctx_st; /* Forward declaration */
  1794. typedef int check_key_fn(void *, struct der2key_ctx_st *ctx);
  1795. typedef void adjust_key_fn(void *, struct der2key_ctx_st *ctx);
  1796. typedef void free_key_fn(void *);
  1797. typedef void *d2i_PKCS8_fn(void **, const unsigned char **, long,
  1798. struct der2key_ctx_st *);
  1799. struct keytype_desc_st {
  1800. const char *keytype_name;
  1801. const OSSL_DISPATCH *fns; /* Keymgmt (to pilfer functions from) */
  1802. /* The input structure name */
  1803. const char *structure_name;
  1804. /*
  1805. * The EVP_PKEY_xxx type macro. Should be zero for type specific
  1806. * structures, non-zero when the outermost structure is PKCS#8 or
  1807. * SubjectPublicKeyInfo. This determines which of the function
  1808. * pointers below will be used.
  1809. */
  1810. int evp_type;
  1811. /* The selection mask for OSSL_FUNC_decoder_does_selection() */
  1812. int selection_mask;
  1813. /* For type specific decoders, we use the corresponding d2i */
  1814. d2i_of_void *d2i_private_key; /* From type-specific DER */
  1815. d2i_of_void *d2i_public_key; /* From type-specific DER */
  1816. d2i_of_void *d2i_key_params; /* From type-specific DER */
  1817. d2i_PKCS8_fn *d2i_PKCS8; /* Wrapped in a PrivateKeyInfo */
  1818. d2i_of_void *d2i_PUBKEY; /* Wrapped in a SubjectPublicKeyInfo */
  1819. /*
  1820. * For any key, we may need to check that the key meets expectations.
  1821. * This is useful when the same functions can decode several variants
  1822. * of a key.
  1823. */
  1824. check_key_fn *check_key;
  1825. /*
  1826. * For any key, we may need to make provider specific adjustments, such
  1827. * as ensure the key carries the correct library context.
  1828. */
  1829. adjust_key_fn *adjust_key;
  1830. /* {type}_free() */
  1831. free_key_fn *free_key;
  1832. };
  1833. /*
  1834. * Start blatant code steal. Alternative: Open up d2i_X509_PUBKEY_INTERNAL
  1835. * as per https://github.com/openssl/openssl/issues/16697 (TBD)
  1836. * Code from openssl/crypto/x509/x_pubkey.c as
  1837. * ossl_d2i_X509_PUBKEY_INTERNAL is presently not public
  1838. */
  1839. struct X509_pubkey_st {
  1840. X509_ALGOR *algor;
  1841. ASN1_BIT_STRING *public_key;
  1842. EVP_PKEY *pkey;
  1843. /* extra data for the callback, used by d2i_PUBKEY_ex */
  1844. OSSL_LIB_CTX *libctx;
  1845. char *propq;
  1846. };
  1847. ASN1_SEQUENCE(X509_PUBKEY_INTERNAL) = {
  1848. ASN1_SIMPLE(X509_PUBKEY, algor, X509_ALGOR),
  1849. ASN1_SIMPLE(X509_PUBKEY, public_key, ASN1_BIT_STRING)
  1850. } static_ASN1_SEQUENCE_END_name(X509_PUBKEY, X509_PUBKEY_INTERNAL)
  1851. static X509_PUBKEY *xorx_d2i_X509_PUBKEY_INTERNAL(const unsigned char **pp,
  1852. long len, OSSL_LIB_CTX *libctx)
  1853. {
  1854. X509_PUBKEY *xpub = OPENSSL_zalloc(sizeof(*xpub));
  1855. if (xpub == NULL)
  1856. return NULL;
  1857. return (X509_PUBKEY *)ASN1_item_d2i_ex((ASN1_VALUE **)&xpub, pp, len,
  1858. ASN1_ITEM_rptr(X509_PUBKEY_INTERNAL),
  1859. libctx, NULL);
  1860. }
  1861. /* end steal https://github.com/openssl/openssl/issues/16697 */
  1862. /*
  1863. * Context used for DER to key decoding.
  1864. */
  1865. struct der2key_ctx_st {
  1866. PROV_XOR_CTX *provctx;
  1867. struct keytype_desc_st *desc;
  1868. /* The selection that is passed to xor_der2key_decode() */
  1869. int selection;
  1870. /* Flag used to signal that a failure is fatal */
  1871. unsigned int flag_fatal : 1;
  1872. };
  1873. static int xor_read_der(PROV_XOR_CTX *provctx, OSSL_CORE_BIO *cin,
  1874. unsigned char **data, long *len)
  1875. {
  1876. BUF_MEM *mem = NULL;
  1877. BIO *in = BIO_new_from_core_bio(provctx->libctx, cin);
  1878. int ok = (asn1_d2i_read_bio(in, &mem) >= 0);
  1879. if (ok) {
  1880. *data = (unsigned char *)mem->data;
  1881. *len = (long)mem->length;
  1882. OPENSSL_free(mem);
  1883. }
  1884. BIO_free(in);
  1885. return ok;
  1886. }
  1887. typedef void *key_from_pkcs8_t(const PKCS8_PRIV_KEY_INFO *p8inf,
  1888. OSSL_LIB_CTX *libctx, const char *propq);
  1889. static void *xor_der2key_decode_p8(const unsigned char **input_der,
  1890. long input_der_len, struct der2key_ctx_st *ctx,
  1891. key_from_pkcs8_t *key_from_pkcs8)
  1892. {
  1893. PKCS8_PRIV_KEY_INFO *p8inf = NULL;
  1894. const X509_ALGOR *alg = NULL;
  1895. void *key = NULL;
  1896. if ((p8inf = d2i_PKCS8_PRIV_KEY_INFO(NULL, input_der, input_der_len)) != NULL
  1897. && PKCS8_pkey_get0(NULL, NULL, NULL, &alg, p8inf)
  1898. && OBJ_obj2nid(alg->algorithm) == ctx->desc->evp_type)
  1899. key = key_from_pkcs8(p8inf, PROV_XOR_LIBCTX_OF(ctx->provctx), NULL);
  1900. PKCS8_PRIV_KEY_INFO_free(p8inf);
  1901. return key;
  1902. }
  1903. static XORKEY *xor_d2i_PUBKEY(XORKEY **a,
  1904. const unsigned char **pp, long length)
  1905. {
  1906. XORKEY *key = NULL;
  1907. X509_PUBKEY *xpk;
  1908. xpk = xorx_d2i_X509_PUBKEY_INTERNAL(pp, length, NULL);
  1909. key = xor_key_from_x509pubkey(xpk, NULL, NULL);
  1910. if (key == NULL)
  1911. goto err_exit;
  1912. if (a != NULL) {
  1913. xor_freekey(*a);
  1914. *a = key;
  1915. }
  1916. err_exit:
  1917. X509_PUBKEY_free(xpk);
  1918. return key;
  1919. }
  1920. /* ---------------------------------------------------------------------- */
  1921. static OSSL_FUNC_decoder_freectx_fn der2key_freectx;
  1922. static OSSL_FUNC_decoder_decode_fn xor_der2key_decode;
  1923. static OSSL_FUNC_decoder_export_object_fn der2key_export_object;
  1924. static struct der2key_ctx_st *
  1925. der2key_newctx(void *provctx, struct keytype_desc_st *desc, const char* tls_name)
  1926. {
  1927. struct der2key_ctx_st *ctx = OPENSSL_zalloc(sizeof(*ctx));
  1928. if (ctx != NULL) {
  1929. ctx->provctx = provctx;
  1930. ctx->desc = desc;
  1931. if (desc->evp_type == 0) {
  1932. ctx->desc->evp_type = OBJ_sn2nid(tls_name);
  1933. }
  1934. }
  1935. return ctx;
  1936. }
  1937. static void der2key_freectx(void *vctx)
  1938. {
  1939. struct der2key_ctx_st *ctx = vctx;
  1940. OPENSSL_free(ctx);
  1941. }
  1942. static int der2key_check_selection(int selection,
  1943. const struct keytype_desc_st *desc)
  1944. {
  1945. /*
  1946. * The selections are kinda sorta "levels", i.e. each selection given
  1947. * here is assumed to include those following.
  1948. */
  1949. int checks[] = {
  1950. OSSL_KEYMGMT_SELECT_PRIVATE_KEY,
  1951. OSSL_KEYMGMT_SELECT_PUBLIC_KEY,
  1952. OSSL_KEYMGMT_SELECT_ALL_PARAMETERS
  1953. };
  1954. size_t i;
  1955. /* The decoder implementations made here support guessing */
  1956. if (selection == 0)
  1957. return 1;
  1958. for (i = 0; i < OSSL_NELEM(checks); i++) {
  1959. int check1 = (selection & checks[i]) != 0;
  1960. int check2 = (desc->selection_mask & checks[i]) != 0;
  1961. /*
  1962. * If the caller asked for the currently checked bit(s), return
  1963. * whether the decoder description says it's supported.
  1964. */
  1965. if (check1)
  1966. return check2;
  1967. }
  1968. /* This should be dead code, but just to be safe... */
  1969. return 0;
  1970. }
  1971. static int xor_der2key_decode(void *vctx, OSSL_CORE_BIO *cin, int selection,
  1972. OSSL_CALLBACK *data_cb, void *data_cbarg,
  1973. OSSL_PASSPHRASE_CALLBACK *pw_cb, void *pw_cbarg)
  1974. {
  1975. struct der2key_ctx_st *ctx = vctx;
  1976. unsigned char *der = NULL;
  1977. const unsigned char *derp;
  1978. long der_len = 0;
  1979. void *key = NULL;
  1980. int ok = 0;
  1981. ctx->selection = selection;
  1982. /*
  1983. * The caller is allowed to specify 0 as a selection mark, to have the
  1984. * structure and key type guessed. For type-specific structures, this
  1985. * is not recommended, as some structures are very similar.
  1986. * Note that 0 isn't the same as OSSL_KEYMGMT_SELECT_ALL, as the latter
  1987. * signifies a private key structure, where everything else is assumed
  1988. * to be present as well.
  1989. */
  1990. if (selection == 0)
  1991. selection = ctx->desc->selection_mask;
  1992. if ((selection & ctx->desc->selection_mask) == 0) {
  1993. ERR_raise(ERR_LIB_PROV, ERR_R_PASSED_INVALID_ARGUMENT);
  1994. return 0;
  1995. }
  1996. ok = xor_read_der(ctx->provctx, cin, &der, &der_len);
  1997. if (!ok)
  1998. goto next;
  1999. ok = 0; /* Assume that we fail */
  2000. if ((selection & OSSL_KEYMGMT_SELECT_PRIVATE_KEY) != 0) {
  2001. derp = der;
  2002. if (ctx->desc->d2i_PKCS8 != NULL) {
  2003. key = ctx->desc->d2i_PKCS8(NULL, &derp, der_len, ctx);
  2004. if (ctx->flag_fatal)
  2005. goto end;
  2006. } else if (ctx->desc->d2i_private_key != NULL) {
  2007. key = ctx->desc->d2i_private_key(NULL, &derp, der_len);
  2008. }
  2009. if (key == NULL && ctx->selection != 0)
  2010. goto next;
  2011. }
  2012. if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_PUBLIC_KEY) != 0) {
  2013. derp = der;
  2014. if (ctx->desc->d2i_PUBKEY != NULL)
  2015. key = ctx->desc->d2i_PUBKEY(NULL, &derp, der_len);
  2016. else
  2017. key = ctx->desc->d2i_public_key(NULL, &derp, der_len);
  2018. if (key == NULL && ctx->selection != 0)
  2019. goto next;
  2020. }
  2021. if (key == NULL && (selection & OSSL_KEYMGMT_SELECT_ALL_PARAMETERS) != 0) {
  2022. derp = der;
  2023. if (ctx->desc->d2i_key_params != NULL)
  2024. key = ctx->desc->d2i_key_params(NULL, &derp, der_len);
  2025. if (key == NULL && ctx->selection != 0)
  2026. goto next;
  2027. }
  2028. /*
  2029. * Last minute check to see if this was the correct type of key. This
  2030. * should never lead to a fatal error, i.e. the decoding itself was
  2031. * correct, it was just an unexpected key type. This is generally for
  2032. * classes of key types that have subtle variants, like RSA-PSS keys as
  2033. * opposed to plain RSA keys.
  2034. */
  2035. if (key != NULL
  2036. && ctx->desc->check_key != NULL
  2037. && !ctx->desc->check_key(key, ctx)) {
  2038. ctx->desc->free_key(key);
  2039. key = NULL;
  2040. }
  2041. if (key != NULL && ctx->desc->adjust_key != NULL)
  2042. ctx->desc->adjust_key(key, ctx);
  2043. next:
  2044. /*
  2045. * Indicated that we successfully decoded something, or not at all.
  2046. * Ending up "empty handed" is not an error.
  2047. */
  2048. ok = 1;
  2049. /*
  2050. * We free memory here so it's not held up during the callback, because
  2051. * we know the process is recursive and the allocated chunks of memory
  2052. * add up.
  2053. */
  2054. OPENSSL_free(der);
  2055. der = NULL;
  2056. if (key != NULL) {
  2057. OSSL_PARAM params[4];
  2058. int object_type = OSSL_OBJECT_PKEY;
  2059. params[0] =
  2060. OSSL_PARAM_construct_int(OSSL_OBJECT_PARAM_TYPE, &object_type);
  2061. params[1] =
  2062. OSSL_PARAM_construct_utf8_string(OSSL_OBJECT_PARAM_DATA_TYPE,
  2063. (char *)ctx->desc->keytype_name,
  2064. 0);
  2065. /* The address of the key becomes the octet string */
  2066. params[2] =
  2067. OSSL_PARAM_construct_octet_string(OSSL_OBJECT_PARAM_REFERENCE,
  2068. &key, sizeof(key));
  2069. params[3] = OSSL_PARAM_construct_end();
  2070. ok = data_cb(params, data_cbarg);
  2071. }
  2072. end:
  2073. ctx->desc->free_key(key);
  2074. OPENSSL_free(der);
  2075. return ok;
  2076. }
  2077. static int der2key_export_object(void *vctx,
  2078. const void *reference, size_t reference_sz,
  2079. OSSL_CALLBACK *export_cb, void *export_cbarg)
  2080. {
  2081. struct der2key_ctx_st *ctx = vctx;
  2082. OSSL_FUNC_keymgmt_export_fn *export =
  2083. xor_prov_get_keymgmt_export(ctx->desc->fns);
  2084. void *keydata;
  2085. if (reference_sz == sizeof(keydata) && export != NULL) {
  2086. /* The contents of the reference is the address to our object */
  2087. keydata = *(void **)reference;
  2088. return export(keydata, ctx->selection, export_cb, export_cbarg);
  2089. }
  2090. return 0;
  2091. }
  2092. /* ---------------------------------------------------------------------- */
  2093. static void *xorx_d2i_PKCS8(void **key, const unsigned char **der, long der_len,
  2094. struct der2key_ctx_st *ctx)
  2095. {
  2096. return xor_der2key_decode_p8(der, der_len, ctx,
  2097. (key_from_pkcs8_t *)xor_key_from_pkcs8);
  2098. }
  2099. static void xorx_key_adjust(void *key, struct der2key_ctx_st *ctx)
  2100. {
  2101. }
  2102. /* ---------------------------------------------------------------------- */
  2103. #define DO_PrivateKeyInfo(keytype) \
  2104. "PrivateKeyInfo", 0, \
  2105. ( OSSL_KEYMGMT_SELECT_PRIVATE_KEY ), \
  2106. NULL, \
  2107. NULL, \
  2108. NULL, \
  2109. xorx_d2i_PKCS8, \
  2110. NULL, \
  2111. NULL, \
  2112. xorx_key_adjust, \
  2113. (free_key_fn *)xor_freekey
  2114. #define DO_SubjectPublicKeyInfo(keytype) \
  2115. "SubjectPublicKeyInfo", 0, \
  2116. ( OSSL_KEYMGMT_SELECT_PUBLIC_KEY ), \
  2117. NULL, \
  2118. NULL, \
  2119. NULL, \
  2120. NULL, \
  2121. (d2i_of_void *)xor_d2i_PUBKEY, \
  2122. NULL, \
  2123. xorx_key_adjust, \
  2124. (free_key_fn *)xor_freekey
  2125. /*
  2126. * MAKE_DECODER is the single driver for creating OSSL_DISPATCH tables.
  2127. * It takes the following arguments:
  2128. *
  2129. * keytype_name The implementation key type as a string.
  2130. * keytype The implementation key type. This must correspond exactly
  2131. * to our existing keymgmt keytype names... in other words,
  2132. * there must exist an ossl_##keytype##_keymgmt_functions.
  2133. * type The type name for the set of functions that implement the
  2134. * decoder for the key type. This isn't necessarily the same
  2135. * as keytype. For example, the key types ed25519, ed448,
  2136. * x25519 and x448 are all handled by the same functions with
  2137. * the common type name ecx.
  2138. * kind The kind of support to implement. This translates into
  2139. * the DO_##kind macros above, to populate the keytype_desc_st
  2140. * structure.
  2141. */
  2142. #define MAKE_DECODER(keytype_name, keytype, type, kind) \
  2143. static struct keytype_desc_st kind##_##keytype##_desc = \
  2144. { keytype_name, xor_##keytype##_keymgmt_functions, \
  2145. DO_##kind(keytype) }; \
  2146. \
  2147. static OSSL_FUNC_decoder_newctx_fn kind##_der2##keytype##_newctx; \
  2148. \
  2149. static void *kind##_der2##keytype##_newctx(void *provctx) \
  2150. { \
  2151. return der2key_newctx(provctx, &kind##_##keytype##_desc, keytype_name );\
  2152. } \
  2153. static int kind##_der2##keytype##_does_selection(void *provctx, \
  2154. int selection) \
  2155. { \
  2156. return der2key_check_selection(selection, \
  2157. &kind##_##keytype##_desc); \
  2158. } \
  2159. static const OSSL_DISPATCH \
  2160. xor_##kind##_der_to_##keytype##_decoder_functions[] = { \
  2161. { OSSL_FUNC_DECODER_NEWCTX, \
  2162. (void (*)(void))kind##_der2##keytype##_newctx }, \
  2163. { OSSL_FUNC_DECODER_FREECTX, \
  2164. (void (*)(void))der2key_freectx }, \
  2165. { OSSL_FUNC_DECODER_DOES_SELECTION, \
  2166. (void (*)(void))kind##_der2##keytype##_does_selection }, \
  2167. { OSSL_FUNC_DECODER_DECODE, \
  2168. (void (*)(void))xor_der2key_decode }, \
  2169. { OSSL_FUNC_DECODER_EXPORT_OBJECT, \
  2170. (void (*)(void))der2key_export_object }, \
  2171. OSSL_DISPATCH_END \
  2172. }
  2173. MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, PrivateKeyInfo);
  2174. MAKE_DECODER(XORSIGALG_NAME, xorhmacsig, xor, SubjectPublicKeyInfo);
  2175. MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, PrivateKeyInfo);
  2176. MAKE_DECODER(XORSIGALG_HASH_NAME, xorhmacsha2sig, xor, SubjectPublicKeyInfo);
  2177. static const OSSL_ALGORITHM tls_prov_decoder[] = {
  2178. #define DECODER_PROVIDER "tls-provider"
  2179. #define DECODER_STRUCTURE_SubjectPublicKeyInfo "SubjectPublicKeyInfo"
  2180. #define DECODER_STRUCTURE_PrivateKeyInfo "PrivateKeyInfo"
  2181. /* Arguments are prefixed with '_' to avoid build breaks on certain platforms */
  2182. /*
  2183. * Obviously this is not FIPS approved, but in order to test in conjunction
  2184. * with the FIPS provider we pretend that it is.
  2185. */
  2186. #define DECODER(_name, _input, _output) \
  2187. { _name, \
  2188. "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input, \
  2189. (xor_##_input##_to_##_output##_decoder_functions) }
  2190. #define DECODER_w_structure(_name, _input, _structure, _output) \
  2191. { _name, \
  2192. "provider=" DECODER_PROVIDER ",fips=yes,input=" #_input \
  2193. ",structure=" DECODER_STRUCTURE_##_structure, \
  2194. (xor_##_structure##_##_input##_to_##_output##_decoder_functions) }
  2195. DECODER_w_structure(XORSIGALG_NAME, der, PrivateKeyInfo, xorhmacsig),
  2196. DECODER_w_structure(XORSIGALG_NAME, der, SubjectPublicKeyInfo, xorhmacsig),
  2197. DECODER_w_structure(XORSIGALG_HASH_NAME, der, PrivateKeyInfo, xorhmacsha2sig),
  2198. DECODER_w_structure(XORSIGALG_HASH_NAME, der, SubjectPublicKeyInfo, xorhmacsha2sig),
  2199. #undef DECODER_PROVIDER
  2200. { NULL, NULL, NULL }
  2201. };
  2202. #define OSSL_MAX_NAME_SIZE 50
  2203. #define OSSL_MAX_PROPQUERY_SIZE 256 /* Property query strings */
  2204. static OSSL_FUNC_signature_newctx_fn xor_sig_newctx;
  2205. static OSSL_FUNC_signature_sign_init_fn xor_sig_sign_init;
  2206. static OSSL_FUNC_signature_verify_init_fn xor_sig_verify_init;
  2207. static OSSL_FUNC_signature_sign_fn xor_sig_sign;
  2208. static OSSL_FUNC_signature_verify_fn xor_sig_verify;
  2209. static OSSL_FUNC_signature_digest_sign_init_fn xor_sig_digest_sign_init;
  2210. static OSSL_FUNC_signature_digest_sign_update_fn xor_sig_digest_signverify_update;
  2211. static OSSL_FUNC_signature_digest_sign_final_fn xor_sig_digest_sign_final;
  2212. static OSSL_FUNC_signature_digest_verify_init_fn xor_sig_digest_verify_init;
  2213. static OSSL_FUNC_signature_digest_verify_update_fn xor_sig_digest_signverify_update;
  2214. static OSSL_FUNC_signature_digest_verify_final_fn xor_sig_digest_verify_final;
  2215. static OSSL_FUNC_signature_freectx_fn xor_sig_freectx;
  2216. static OSSL_FUNC_signature_dupctx_fn xor_sig_dupctx;
  2217. static OSSL_FUNC_signature_get_ctx_params_fn xor_sig_get_ctx_params;
  2218. static OSSL_FUNC_signature_gettable_ctx_params_fn xor_sig_gettable_ctx_params;
  2219. static OSSL_FUNC_signature_set_ctx_params_fn xor_sig_set_ctx_params;
  2220. static OSSL_FUNC_signature_settable_ctx_params_fn xor_sig_settable_ctx_params;
  2221. static OSSL_FUNC_signature_get_ctx_md_params_fn xor_sig_get_ctx_md_params;
  2222. static OSSL_FUNC_signature_gettable_ctx_md_params_fn xor_sig_gettable_ctx_md_params;
  2223. static OSSL_FUNC_signature_set_ctx_md_params_fn xor_sig_set_ctx_md_params;
  2224. static OSSL_FUNC_signature_settable_ctx_md_params_fn xor_sig_settable_ctx_md_params;
  2225. static int xor_get_aid(unsigned char** oidbuf, const char *tls_name) {
  2226. X509_ALGOR *algor = X509_ALGOR_new();
  2227. int aidlen = 0;
  2228. X509_ALGOR_set0(algor, OBJ_txt2obj(tls_name, 0), V_ASN1_UNDEF, NULL);
  2229. aidlen = i2d_X509_ALGOR(algor, oidbuf);
  2230. X509_ALGOR_free(algor);
  2231. return(aidlen);
  2232. }
  2233. /*
  2234. * What's passed as an actual key is defined by the KEYMGMT interface.
  2235. */
  2236. typedef struct {
  2237. OSSL_LIB_CTX *libctx;
  2238. char *propq;
  2239. XORKEY *sig;
  2240. /*
  2241. * Flag to determine if the hash function can be changed (1) or not (0)
  2242. * Because it's dangerous to change during a DigestSign or DigestVerify
  2243. * operation, this flag is cleared by their Init function, and set again
  2244. * by their Final function.
  2245. */
  2246. unsigned int flag_allow_md : 1;
  2247. char mdname[OSSL_MAX_NAME_SIZE];
  2248. /* The Algorithm Identifier of the combined signature algorithm */
  2249. unsigned char *aid;
  2250. size_t aid_len;
  2251. /* main digest */
  2252. EVP_MD *md;
  2253. EVP_MD_CTX *mdctx;
  2254. int operation;
  2255. } PROV_XORSIG_CTX;
  2256. static void *xor_sig_newctx(void *provctx, const char *propq)
  2257. {
  2258. PROV_XORSIG_CTX *pxor_sigctx;
  2259. pxor_sigctx = OPENSSL_zalloc(sizeof(PROV_XORSIG_CTX));
  2260. if (pxor_sigctx == NULL)
  2261. return NULL;
  2262. pxor_sigctx->libctx = ((PROV_XOR_CTX*)provctx)->libctx;
  2263. pxor_sigctx->flag_allow_md = 0;
  2264. if (propq != NULL && (pxor_sigctx->propq = OPENSSL_strdup(propq)) == NULL) {
  2265. OPENSSL_free(pxor_sigctx);
  2266. pxor_sigctx = NULL;
  2267. ERR_raise(ERR_LIB_USER, ERR_R_MALLOC_FAILURE);
  2268. }
  2269. return pxor_sigctx;
  2270. }
  2271. static int xor_sig_setup_md(PROV_XORSIG_CTX *ctx,
  2272. const char *mdname, const char *mdprops)
  2273. {
  2274. EVP_MD *md;
  2275. if (mdprops == NULL)
  2276. mdprops = ctx->propq;
  2277. md = EVP_MD_fetch(ctx->libctx, mdname, mdprops);
  2278. if ((md == NULL) || (EVP_MD_nid(md)==NID_undef)) {
  2279. if (md == NULL)
  2280. ERR_raise_data(ERR_LIB_USER, XORPROV_R_INVALID_DIGEST,
  2281. "%s could not be fetched", mdname);
  2282. EVP_MD_free(md);
  2283. return 0;
  2284. }
  2285. EVP_MD_CTX_free(ctx->mdctx);
  2286. ctx->mdctx = NULL;
  2287. EVP_MD_free(ctx->md);
  2288. ctx->md = NULL;
  2289. OPENSSL_free(ctx->aid);
  2290. ctx->aid = NULL;
  2291. ctx->aid_len = xor_get_aid(&(ctx->aid), ctx->sig->tls_name);
  2292. ctx->mdctx = NULL;
  2293. ctx->md = md;
  2294. OPENSSL_strlcpy(ctx->mdname, mdname, sizeof(ctx->mdname));
  2295. return 1;
  2296. }
  2297. static int xor_sig_signverify_init(void *vpxor_sigctx, void *vxorsig,
  2298. int operation)
  2299. {
  2300. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2301. if (pxor_sigctx == NULL || vxorsig == NULL)
  2302. return 0;
  2303. xor_freekey(pxor_sigctx->sig);
  2304. if (!xor_key_up_ref(vxorsig))
  2305. return 0;
  2306. pxor_sigctx->sig = vxorsig;
  2307. pxor_sigctx->operation = operation;
  2308. if ((operation==EVP_PKEY_OP_SIGN && pxor_sigctx->sig == NULL)
  2309. || (operation==EVP_PKEY_OP_VERIFY && pxor_sigctx->sig == NULL)) {
  2310. ERR_raise(ERR_LIB_USER, XORPROV_R_INVALID_KEY);
  2311. return 0;
  2312. }
  2313. return 1;
  2314. }
  2315. static int xor_sig_sign_init(void *vpxor_sigctx, void *vxorsig,
  2316. const OSSL_PARAM params[])
  2317. {
  2318. return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_SIGN);
  2319. }
  2320. static int xor_sig_verify_init(void *vpxor_sigctx, void *vxorsig,
  2321. const OSSL_PARAM params[])
  2322. {
  2323. return xor_sig_signverify_init(vpxor_sigctx, vxorsig, EVP_PKEY_OP_VERIFY);
  2324. }
  2325. static int xor_sig_sign(void *vpxor_sigctx, unsigned char *sig, size_t *siglen,
  2326. size_t sigsize, const unsigned char *tbs, size_t tbslen)
  2327. {
  2328. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2329. XORKEY *xorkey = pxor_sigctx->sig;
  2330. size_t max_sig_len = EVP_MAX_MD_SIZE;
  2331. size_t xor_sig_len = 0;
  2332. int rv = 0;
  2333. if (xorkey == NULL || !xorkey->hasprivkey) {
  2334. ERR_raise(ERR_LIB_USER, XORPROV_R_NO_PRIVATE_KEY);
  2335. return rv;
  2336. }
  2337. if (sig == NULL) {
  2338. *siglen = max_sig_len;
  2339. return 1;
  2340. }
  2341. if (*siglen < max_sig_len) {
  2342. ERR_raise(ERR_LIB_USER, XORPROV_R_BUFFER_LENGTH_WRONG);
  2343. return rv;
  2344. }
  2345. /*
  2346. * create HMAC using XORKEY as key and hash as data:
  2347. * No real crypto, just for test, don't do this at home!
  2348. */
  2349. if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL,
  2350. xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen,
  2351. &sig[0], EVP_MAX_MD_SIZE, &xor_sig_len)) {
  2352. ERR_raise(ERR_LIB_USER, XORPROV_R_SIGNING_FAILED);
  2353. goto endsign;
  2354. }
  2355. *siglen = xor_sig_len;
  2356. rv = 1; /* success */
  2357. endsign:
  2358. return rv;
  2359. }
  2360. static int xor_sig_verify(void *vpxor_sigctx,
  2361. const unsigned char *sig, size_t siglen,
  2362. const unsigned char *tbs, size_t tbslen)
  2363. {
  2364. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2365. XORKEY *xorkey = pxor_sigctx->sig;
  2366. unsigned char resignature[EVP_MAX_MD_SIZE];
  2367. size_t resiglen;
  2368. int i;
  2369. if (xorkey == NULL || sig == NULL || tbs == NULL) {
  2370. ERR_raise(ERR_LIB_USER, XORPROV_R_WRONG_PARAMETERS);
  2371. return 0;
  2372. }
  2373. /*
  2374. * This is no real verify: just re-sign and compare:
  2375. * Don't do this at home! Not fit for real use!
  2376. */
  2377. /* First re-create private key from public key: */
  2378. for (i = 0; i < XOR_KEY_SIZE; i++)
  2379. xorkey->privkey[i] = xorkey->pubkey[i] ^ private_constant[i];
  2380. /* Now re-create signature */
  2381. if (!EVP_Q_mac(pxor_sigctx->libctx, "HMAC", NULL, "sha1", NULL,
  2382. xorkey->privkey, XOR_KEY_SIZE, tbs, tbslen,
  2383. &resignature[0], EVP_MAX_MD_SIZE, &resiglen)) {
  2384. ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR);
  2385. return 0;
  2386. }
  2387. /* Now compare with signature passed */
  2388. if (siglen != resiglen || memcmp(resignature, sig, siglen) != 0) {
  2389. ERR_raise(ERR_LIB_USER, XORPROV_R_VERIFY_ERROR);
  2390. return 0;
  2391. }
  2392. return 1;
  2393. }
  2394. static int xor_sig_digest_signverify_init(void *vpxor_sigctx, const char *mdname,
  2395. void *vxorsig, int operation)
  2396. {
  2397. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2398. char *rmdname = (char *)mdname;
  2399. if (rmdname == NULL)
  2400. rmdname = "sha256";
  2401. pxor_sigctx->flag_allow_md = 0;
  2402. if (!xor_sig_signverify_init(vpxor_sigctx, vxorsig, operation))
  2403. return 0;
  2404. if (!xor_sig_setup_md(pxor_sigctx, rmdname, NULL))
  2405. return 0;
  2406. pxor_sigctx->mdctx = EVP_MD_CTX_new();
  2407. if (pxor_sigctx->mdctx == NULL)
  2408. goto error;
  2409. if (!EVP_DigestInit_ex(pxor_sigctx->mdctx, pxor_sigctx->md, NULL))
  2410. goto error;
  2411. return 1;
  2412. error:
  2413. EVP_MD_CTX_free(pxor_sigctx->mdctx);
  2414. EVP_MD_free(pxor_sigctx->md);
  2415. pxor_sigctx->mdctx = NULL;
  2416. pxor_sigctx->md = NULL;
  2417. return 0;
  2418. }
  2419. static int xor_sig_digest_sign_init(void *vpxor_sigctx, const char *mdname,
  2420. void *vxorsig, const OSSL_PARAM params[])
  2421. {
  2422. return xor_sig_digest_signverify_init(vpxor_sigctx, mdname, vxorsig,
  2423. EVP_PKEY_OP_SIGN);
  2424. }
  2425. static int xor_sig_digest_verify_init(void *vpxor_sigctx, const char *mdname, void *vxorsig, const OSSL_PARAM params[])
  2426. {
  2427. return xor_sig_digest_signverify_init(vpxor_sigctx, mdname,
  2428. vxorsig, EVP_PKEY_OP_VERIFY);
  2429. }
  2430. int xor_sig_digest_signverify_update(void *vpxor_sigctx,
  2431. const unsigned char *data,
  2432. size_t datalen)
  2433. {
  2434. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2435. if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL)
  2436. return 0;
  2437. return EVP_DigestUpdate(pxor_sigctx->mdctx, data, datalen);
  2438. }
  2439. int xor_sig_digest_sign_final(void *vpxor_sigctx,
  2440. unsigned char *sig, size_t *siglen,
  2441. size_t sigsize)
  2442. {
  2443. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2444. unsigned char digest[EVP_MAX_MD_SIZE];
  2445. unsigned int dlen = 0;
  2446. if (sig != NULL) {
  2447. if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL)
  2448. return 0;
  2449. if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen))
  2450. return 0;
  2451. pxor_sigctx->flag_allow_md = 1;
  2452. }
  2453. return xor_sig_sign(vpxor_sigctx, sig, siglen, sigsize, digest, (size_t)dlen);
  2454. }
  2455. int xor_sig_digest_verify_final(void *vpxor_sigctx, const unsigned char *sig,
  2456. size_t siglen)
  2457. {
  2458. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2459. unsigned char digest[EVP_MAX_MD_SIZE];
  2460. unsigned int dlen = 0;
  2461. if (pxor_sigctx == NULL || pxor_sigctx->mdctx == NULL)
  2462. return 0;
  2463. if (!EVP_DigestFinal_ex(pxor_sigctx->mdctx, digest, &dlen))
  2464. return 0;
  2465. pxor_sigctx->flag_allow_md = 1;
  2466. return xor_sig_verify(vpxor_sigctx, sig, siglen, digest, (size_t)dlen);
  2467. }
  2468. static void xor_sig_freectx(void *vpxor_sigctx)
  2469. {
  2470. PROV_XORSIG_CTX *ctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2471. OPENSSL_free(ctx->propq);
  2472. EVP_MD_CTX_free(ctx->mdctx);
  2473. EVP_MD_free(ctx->md);
  2474. ctx->propq = NULL;
  2475. ctx->mdctx = NULL;
  2476. ctx->md = NULL;
  2477. xor_freekey(ctx->sig);
  2478. ctx->sig = NULL;
  2479. OPENSSL_free(ctx->aid);
  2480. OPENSSL_free(ctx);
  2481. }
  2482. static void *xor_sig_dupctx(void *vpxor_sigctx)
  2483. {
  2484. PROV_XORSIG_CTX *srcctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2485. PROV_XORSIG_CTX *dstctx;
  2486. dstctx = OPENSSL_zalloc(sizeof(*srcctx));
  2487. if (dstctx == NULL)
  2488. return NULL;
  2489. *dstctx = *srcctx;
  2490. dstctx->sig = NULL;
  2491. dstctx->md = NULL;
  2492. dstctx->mdctx = NULL;
  2493. dstctx->aid = NULL;
  2494. if ((srcctx->sig != NULL) && !xor_key_up_ref(srcctx->sig))
  2495. goto err;
  2496. dstctx->sig = srcctx->sig;
  2497. if (srcctx->md != NULL && !EVP_MD_up_ref(srcctx->md))
  2498. goto err;
  2499. dstctx->md = srcctx->md;
  2500. if (srcctx->mdctx != NULL) {
  2501. dstctx->mdctx = EVP_MD_CTX_new();
  2502. if (dstctx->mdctx == NULL
  2503. || !EVP_MD_CTX_copy_ex(dstctx->mdctx, srcctx->mdctx))
  2504. goto err;
  2505. }
  2506. return dstctx;
  2507. err:
  2508. xor_sig_freectx(dstctx);
  2509. return NULL;
  2510. }
  2511. static int xor_sig_get_ctx_params(void *vpxor_sigctx, OSSL_PARAM *params)
  2512. {
  2513. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2514. OSSL_PARAM *p;
  2515. if (pxor_sigctx == NULL || params == NULL)
  2516. return 0;
  2517. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_ALGORITHM_ID);
  2518. if (pxor_sigctx->aid == NULL)
  2519. pxor_sigctx->aid_len = xor_get_aid(&(pxor_sigctx->aid), pxor_sigctx->sig->tls_name);
  2520. if (p != NULL
  2521. && !OSSL_PARAM_set_octet_string(p, pxor_sigctx->aid, pxor_sigctx->aid_len))
  2522. return 0;
  2523. p = OSSL_PARAM_locate(params, OSSL_SIGNATURE_PARAM_DIGEST);
  2524. if (p != NULL && !OSSL_PARAM_set_utf8_string(p, pxor_sigctx->mdname))
  2525. return 0;
  2526. return 1;
  2527. }
  2528. static const OSSL_PARAM known_gettable_ctx_params[] = {
  2529. OSSL_PARAM_octet_string(OSSL_SIGNATURE_PARAM_ALGORITHM_ID, NULL, 0),
  2530. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
  2531. OSSL_PARAM_END
  2532. };
  2533. static const OSSL_PARAM *xor_sig_gettable_ctx_params(ossl_unused void *vpxor_sigctx, ossl_unused void *vctx)
  2534. {
  2535. return known_gettable_ctx_params;
  2536. }
  2537. static int xor_sig_set_ctx_params(void *vpxor_sigctx, const OSSL_PARAM params[])
  2538. {
  2539. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2540. const OSSL_PARAM *p;
  2541. if (pxor_sigctx == NULL || params == NULL)
  2542. return 0;
  2543. p = OSSL_PARAM_locate_const(params, OSSL_SIGNATURE_PARAM_DIGEST);
  2544. /* Not allowed during certain operations */
  2545. if (p != NULL && !pxor_sigctx->flag_allow_md)
  2546. return 0;
  2547. if (p != NULL) {
  2548. char mdname[OSSL_MAX_NAME_SIZE] = "", *pmdname = mdname;
  2549. char mdprops[OSSL_MAX_PROPQUERY_SIZE] = "", *pmdprops = mdprops;
  2550. const OSSL_PARAM *propsp =
  2551. OSSL_PARAM_locate_const(params,
  2552. OSSL_SIGNATURE_PARAM_PROPERTIES);
  2553. if (!OSSL_PARAM_get_utf8_string(p, &pmdname, sizeof(mdname)))
  2554. return 0;
  2555. if (propsp != NULL
  2556. && !OSSL_PARAM_get_utf8_string(propsp, &pmdprops, sizeof(mdprops)))
  2557. return 0;
  2558. if (!xor_sig_setup_md(pxor_sigctx, mdname, mdprops))
  2559. return 0;
  2560. }
  2561. return 1;
  2562. }
  2563. static const OSSL_PARAM known_settable_ctx_params[] = {
  2564. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_DIGEST, NULL, 0),
  2565. OSSL_PARAM_utf8_string(OSSL_SIGNATURE_PARAM_PROPERTIES, NULL, 0),
  2566. OSSL_PARAM_END
  2567. };
  2568. static const OSSL_PARAM *xor_sig_settable_ctx_params(ossl_unused void *vpsm2ctx,
  2569. ossl_unused void *provctx)
  2570. {
  2571. return known_settable_ctx_params;
  2572. }
  2573. static int xor_sig_get_ctx_md_params(void *vpxor_sigctx, OSSL_PARAM *params)
  2574. {
  2575. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2576. if (pxor_sigctx->mdctx == NULL)
  2577. return 0;
  2578. return EVP_MD_CTX_get_params(pxor_sigctx->mdctx, params);
  2579. }
  2580. static const OSSL_PARAM *xor_sig_gettable_ctx_md_params(void *vpxor_sigctx)
  2581. {
  2582. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2583. if (pxor_sigctx->md == NULL)
  2584. return 0;
  2585. return EVP_MD_gettable_ctx_params(pxor_sigctx->md);
  2586. }
  2587. static int xor_sig_set_ctx_md_params(void *vpxor_sigctx, const OSSL_PARAM params[])
  2588. {
  2589. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2590. if (pxor_sigctx->mdctx == NULL)
  2591. return 0;
  2592. return EVP_MD_CTX_set_params(pxor_sigctx->mdctx, params);
  2593. }
  2594. static const OSSL_PARAM *xor_sig_settable_ctx_md_params(void *vpxor_sigctx)
  2595. {
  2596. PROV_XORSIG_CTX *pxor_sigctx = (PROV_XORSIG_CTX *)vpxor_sigctx;
  2597. if (pxor_sigctx->md == NULL)
  2598. return 0;
  2599. return EVP_MD_settable_ctx_params(pxor_sigctx->md);
  2600. }
  2601. static const OSSL_DISPATCH xor_signature_functions[] = {
  2602. { OSSL_FUNC_SIGNATURE_NEWCTX, (void (*)(void))xor_sig_newctx },
  2603. { OSSL_FUNC_SIGNATURE_SIGN_INIT, (void (*)(void))xor_sig_sign_init },
  2604. { OSSL_FUNC_SIGNATURE_SIGN, (void (*)(void))xor_sig_sign },
  2605. { OSSL_FUNC_SIGNATURE_VERIFY_INIT, (void (*)(void))xor_sig_verify_init },
  2606. { OSSL_FUNC_SIGNATURE_VERIFY, (void (*)(void))xor_sig_verify },
  2607. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_INIT,
  2608. (void (*)(void))xor_sig_digest_sign_init },
  2609. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_UPDATE,
  2610. (void (*)(void))xor_sig_digest_signverify_update },
  2611. { OSSL_FUNC_SIGNATURE_DIGEST_SIGN_FINAL,
  2612. (void (*)(void))xor_sig_digest_sign_final },
  2613. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_INIT,
  2614. (void (*)(void))xor_sig_digest_verify_init },
  2615. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_UPDATE,
  2616. (void (*)(void))xor_sig_digest_signverify_update },
  2617. { OSSL_FUNC_SIGNATURE_DIGEST_VERIFY_FINAL,
  2618. (void (*)(void))xor_sig_digest_verify_final },
  2619. { OSSL_FUNC_SIGNATURE_FREECTX, (void (*)(void))xor_sig_freectx },
  2620. { OSSL_FUNC_SIGNATURE_DUPCTX, (void (*)(void))xor_sig_dupctx },
  2621. { OSSL_FUNC_SIGNATURE_GET_CTX_PARAMS, (void (*)(void))xor_sig_get_ctx_params },
  2622. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_PARAMS,
  2623. (void (*)(void))xor_sig_gettable_ctx_params },
  2624. { OSSL_FUNC_SIGNATURE_SET_CTX_PARAMS, (void (*)(void))xor_sig_set_ctx_params },
  2625. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_PARAMS,
  2626. (void (*)(void))xor_sig_settable_ctx_params },
  2627. { OSSL_FUNC_SIGNATURE_GET_CTX_MD_PARAMS,
  2628. (void (*)(void))xor_sig_get_ctx_md_params },
  2629. { OSSL_FUNC_SIGNATURE_GETTABLE_CTX_MD_PARAMS,
  2630. (void (*)(void))xor_sig_gettable_ctx_md_params },
  2631. { OSSL_FUNC_SIGNATURE_SET_CTX_MD_PARAMS,
  2632. (void (*)(void))xor_sig_set_ctx_md_params },
  2633. { OSSL_FUNC_SIGNATURE_SETTABLE_CTX_MD_PARAMS,
  2634. (void (*)(void))xor_sig_settable_ctx_md_params },
  2635. OSSL_DISPATCH_END
  2636. };
  2637. static const OSSL_ALGORITHM tls_prov_signature[] = {
  2638. /*
  2639. * Obviously this is not FIPS approved, but in order to test in conjunction
  2640. * with the FIPS provider we pretend that it is.
  2641. */
  2642. { XORSIGALG_NAME, "provider=tls-provider,fips=yes",
  2643. xor_signature_functions },
  2644. { XORSIGALG_HASH_NAME, "provider=tls-provider,fips=yes",
  2645. xor_signature_functions },
  2646. { XORSIGALG12_NAME, "provider=tls-provider,fips=yes",
  2647. xor_signature_functions },
  2648. { NULL, NULL, NULL }
  2649. };
  2650. static const OSSL_ALGORITHM *tls_prov_query(void *provctx, int operation_id,
  2651. int *no_cache)
  2652. {
  2653. *no_cache = 0;
  2654. switch (operation_id) {
  2655. case OSSL_OP_KEYMGMT:
  2656. return tls_prov_keymgmt;
  2657. case OSSL_OP_KEYEXCH:
  2658. return tls_prov_keyexch;
  2659. case OSSL_OP_KEM:
  2660. return tls_prov_kem;
  2661. case OSSL_OP_ENCODER:
  2662. return tls_prov_encoder;
  2663. case OSSL_OP_DECODER:
  2664. return tls_prov_decoder;
  2665. case OSSL_OP_SIGNATURE:
  2666. return tls_prov_signature;
  2667. }
  2668. return NULL;
  2669. }
  2670. static void tls_prov_teardown(void *provctx)
  2671. {
  2672. int i;
  2673. PROV_XOR_CTX *pctx = (PROV_XOR_CTX*)provctx;
  2674. OSSL_LIB_CTX_free(pctx->libctx);
  2675. for (i = 0; i < NUM_DUMMY_GROUPS; i++) {
  2676. OPENSSL_free(dummy_group_names[i]);
  2677. dummy_group_names[i] = NULL;
  2678. }
  2679. OPENSSL_free(pctx);
  2680. }
  2681. /* Functions we provide to the core */
  2682. static const OSSL_DISPATCH tls_prov_dispatch_table[] = {
  2683. { OSSL_FUNC_PROVIDER_TEARDOWN, (void (*)(void))tls_prov_teardown },
  2684. { OSSL_FUNC_PROVIDER_QUERY_OPERATION, (void (*)(void))tls_prov_query },
  2685. { OSSL_FUNC_PROVIDER_GET_CAPABILITIES, (void (*)(void))tls_prov_get_capabilities },
  2686. OSSL_DISPATCH_END
  2687. };
  2688. static
  2689. unsigned int randomize_tls_alg_id(OSSL_LIB_CTX *libctx)
  2690. {
  2691. /*
  2692. * Randomise the id we're going to use to ensure we don't interoperate
  2693. * with anything but ourselves.
  2694. */
  2695. unsigned int id;
  2696. static unsigned int mem[10] = { 0 };
  2697. static int in_mem = 0;
  2698. int i;
  2699. retry:
  2700. if (RAND_bytes_ex(libctx, (unsigned char *)&id, sizeof(id), 0) <= 0)
  2701. return 0;
  2702. /*
  2703. * Ensure id is within the IANA Reserved for private use range
  2704. * (65024-65279)
  2705. */
  2706. id %= 65279 - 65024;
  2707. id += 65024;
  2708. /* Ensure we did not already issue this id */
  2709. for (i = 0; i < in_mem; i++)
  2710. if (mem[i] == id)
  2711. goto retry;
  2712. /* Add this id to the list of ids issued by this function */
  2713. mem[in_mem++] = id;
  2714. return id;
  2715. }
  2716. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  2717. const OSSL_DISPATCH *in,
  2718. const OSSL_DISPATCH **out,
  2719. void **provctx)
  2720. {
  2721. OSSL_LIB_CTX *libctx = OSSL_LIB_CTX_new_from_dispatch(handle, in);
  2722. OSSL_FUNC_core_obj_create_fn *c_obj_create= NULL;
  2723. OSSL_FUNC_core_obj_add_sigid_fn *c_obj_add_sigid= NULL;
  2724. PROV_XOR_CTX *prov_ctx = xor_newprovctx(libctx);
  2725. if (libctx == NULL || prov_ctx == NULL)
  2726. return 0;
  2727. *provctx = prov_ctx;
  2728. /*
  2729. * Randomise the group_id and code_points we're going to use to ensure we
  2730. * don't interoperate with anything but ourselves.
  2731. */
  2732. xor_group.group_id = randomize_tls_alg_id(libctx);
  2733. xor_kemgroup.group_id = randomize_tls_alg_id(libctx);
  2734. xor_sigalg.code_point = randomize_tls_alg_id(libctx);
  2735. xor_sigalg_hash.code_point = randomize_tls_alg_id(libctx);
  2736. /* Retrieve registration functions */
  2737. for (; in->function_id != 0; in++) {
  2738. switch (in->function_id) {
  2739. case OSSL_FUNC_CORE_OBJ_CREATE:
  2740. c_obj_create = OSSL_FUNC_core_obj_create(in);
  2741. break;
  2742. case OSSL_FUNC_CORE_OBJ_ADD_SIGID:
  2743. c_obj_add_sigid = OSSL_FUNC_core_obj_add_sigid(in);
  2744. break;
  2745. /* Just ignore anything we don't understand */
  2746. default:
  2747. break;
  2748. }
  2749. }
  2750. /*
  2751. * Register algorithms manually as add_provider_sigalgs is
  2752. * only called during session establishment -- too late for
  2753. * key & cert generation...
  2754. */
  2755. if (!c_obj_create(handle, XORSIGALG_OID, XORSIGALG_NAME, XORSIGALG_NAME)) {
  2756. ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR);
  2757. return 0;
  2758. }
  2759. if (!c_obj_add_sigid(handle, XORSIGALG_OID, "", XORSIGALG_OID)) {
  2760. ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR);
  2761. return 0;
  2762. }
  2763. if (!c_obj_create(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH_NAME, NULL)) {
  2764. ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR);
  2765. return 0;
  2766. }
  2767. if (!c_obj_add_sigid(handle, XORSIGALG_HASH_OID, XORSIGALG_HASH, XORSIGALG_HASH_OID)) {
  2768. ERR_raise(ERR_LIB_USER, XORPROV_R_OBJ_CREATE_ERR);
  2769. return 0;
  2770. }
  2771. *out = tls_prov_dispatch_table;
  2772. return 1;
  2773. }