tls-provider.c 110 KB

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