1
0

sslapitest.c 421 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265102661026710268102691027010271102721027310274102751027610277102781027910280102811028210283102841028510286102871028810289102901029110292102931029410295102961029710298102991030010301103021030310304103051030610307103081030910310103111031210313103141031510316103171031810319103201032110322103231032410325103261032710328103291033010331103321033310334103351033610337103381033910340103411034210343103441034510346103471034810349103501035110352103531035410355103561035710358103591036010361103621036310364103651036610367103681036910370103711037210373103741037510376103771037810379103801038110382103831038410385103861038710388103891039010391103921039310394103951039610397103981039910400104011040210403104041040510406104071040810409104101041110412104131041410415104161041710418104191042010421104221042310424104251042610427104281042910430104311043210433104341043510436104371043810439104401044110442104431044410445104461044710448104491045010451104521045310454104551045610457104581045910460104611046210463104641046510466104671046810469104701047110472104731047410475104761047710478104791048010481104821048310484104851048610487104881048910490104911049210493104941049510496104971049810499105001050110502105031050410505105061050710508105091051010511105121051310514105151051610517105181051910520105211052210523105241052510526105271052810529105301053110532105331053410535105361053710538105391054010541105421054310544105451054610547105481054910550105511055210553105541055510556105571055810559105601056110562105631056410565105661056710568105691057010571105721057310574105751057610577105781057910580105811058210583105841058510586105871058810589105901059110592105931059410595105961059710598105991060010601106021060310604106051060610607106081060910610106111061210613106141061510616106171061810619106201062110622106231062410625106261062710628106291063010631106321063310634106351063610637106381063910640106411064210643106441064510646106471064810649106501065110652106531065410655106561065710658106591066010661106621066310664106651066610667106681066910670106711067210673106741067510676106771067810679106801068110682106831068410685106861068710688106891069010691106921069310694106951069610697106981069910700107011070210703107041070510706107071070810709107101071110712107131071410715107161071710718107191072010721107221072310724107251072610727107281072910730107311073210733107341073510736107371073810739107401074110742107431074410745107461074710748107491075010751107521075310754107551075610757107581075910760107611076210763107641076510766107671076810769107701077110772107731077410775107761077710778107791078010781107821078310784107851078610787107881078910790107911079210793107941079510796107971079810799108001080110802108031080410805108061080710808108091081010811108121081310814108151081610817108181081910820108211082210823108241082510826108271082810829108301083110832108331083410835108361083710838108391084010841108421084310844108451084610847108481084910850108511085210853108541085510856108571085810859108601086110862108631086410865108661086710868108691087010871108721087310874108751087610877108781087910880108811088210883108841088510886108871088810889108901089110892108931089410895108961089710898108991090010901109021090310904109051090610907109081090910910109111091210913109141091510916109171091810919109201092110922109231092410925109261092710928109291093010931109321093310934109351093610937109381093910940109411094210943109441094510946109471094810949109501095110952109531095410955109561095710958109591096010961109621096310964109651096610967109681096910970109711097210973109741097510976109771097810979109801098110982109831098410985109861098710988109891099010991109921099310994109951099610997109981099911000110011100211003110041100511006110071100811009110101101111012110131101411015110161101711018110191102011021110221102311024110251102611027110281102911030110311103211033110341103511036110371103811039110401104111042110431104411045110461104711048110491105011051110521105311054110551105611057110581105911060110611106211063110641106511066110671106811069110701107111072110731107411075110761107711078110791108011081110821108311084110851108611087110881108911090110911109211093110941109511096110971109811099111001110111102111031110411105111061110711108111091111011111111121111311114111151111611117111181111911120111211112211123111241112511126111271112811129111301113111132111331113411135111361113711138111391114011141111421114311144111451114611147111481114911150111511115211153111541115511156111571115811159111601116111162111631116411165111661116711168111691117011171111721117311174111751117611177111781117911180111811118211183111841118511186111871118811189111901119111192111931119411195111961119711198111991120011201112021120311204112051120611207112081120911210112111121211213112141121511216112171121811219112201122111222112231122411225112261122711228112291123011231112321123311234112351123611237112381123911240112411124211243112441124511246112471124811249112501125111252112531125411255112561125711258112591126011261112621126311264112651126611267112681126911270112711127211273112741127511276112771127811279112801128111282112831128411285112861128711288112891129011291112921129311294112951129611297112981129911300113011130211303113041130511306113071130811309113101131111312113131131411315113161131711318113191132011321113221132311324113251132611327113281132911330113311133211333113341133511336113371133811339113401134111342113431134411345113461134711348113491135011351113521135311354113551135611357113581135911360113611136211363113641136511366113671136811369113701137111372113731137411375113761137711378113791138011381113821138311384113851138611387113881138911390113911139211393113941139511396113971139811399114001140111402114031140411405114061140711408114091141011411114121141311414114151141611417114181141911420114211142211423114241142511426114271142811429114301143111432114331143411435114361143711438114391144011441114421144311444114451144611447114481144911450114511145211453114541145511456114571145811459114601146111462114631146411465114661146711468114691147011471114721147311474114751147611477114781147911480114811148211483114841148511486114871148811489114901149111492114931149411495114961149711498114991150011501115021150311504115051150611507115081150911510115111151211513115141151511516115171151811519115201152111522115231152411525115261152711528115291153011531115321153311534115351153611537115381153911540115411154211543115441154511546115471154811549115501155111552115531155411555115561155711558115591156011561115621156311564115651156611567115681156911570115711157211573115741157511576115771157811579115801158111582115831158411585115861158711588115891159011591115921159311594115951159611597115981159911600116011160211603116041160511606116071160811609116101161111612116131161411615116161161711618116191162011621116221162311624116251162611627116281162911630116311163211633116341163511636116371163811639116401164111642116431164411645116461164711648116491165011651116521165311654116551165611657116581165911660116611166211663116641166511666116671166811669116701167111672116731167411675116761167711678116791168011681116821168311684116851168611687116881168911690116911169211693116941169511696116971169811699117001170111702117031170411705117061170711708117091171011711117121171311714117151171611717117181171911720117211172211723117241172511726117271172811729117301173111732117331173411735117361173711738117391174011741117421174311744117451174611747117481174911750117511175211753117541175511756117571175811759117601176111762117631176411765117661176711768117691177011771117721177311774117751177611777117781177911780117811178211783117841178511786117871178811789117901179111792117931179411795117961179711798117991180011801118021180311804118051180611807118081180911810118111181211813118141181511816118171181811819118201182111822118231182411825118261182711828118291183011831118321183311834118351183611837118381183911840118411184211843118441184511846118471184811849118501185111852118531185411855118561185711858118591186011861118621186311864118651186611867118681186911870118711187211873118741187511876118771187811879118801188111882118831188411885118861188711888118891189011891118921189311894118951189611897118981189911900119011190211903119041190511906119071190811909119101191111912119131191411915119161191711918119191192011921119221192311924119251192611927119281192911930119311193211933119341193511936119371193811939119401194111942119431194411945119461194711948119491195011951119521195311954119551195611957119581195911960119611196211963119641196511966119671196811969119701197111972119731197411975119761197711978119791198011981119821198311984119851198611987119881198911990119911199211993119941199511996119971199811999120001200112002120031200412005120061200712008120091201012011120121201312014120151201612017120181201912020120211202212023120241202512026120271202812029120301203112032120331203412035120361203712038120391204012041120421204312044120451204612047120481204912050120511205212053120541205512056120571205812059120601206112062120631206412065120661206712068120691207012071120721207312074120751207612077120781207912080120811208212083120841208512086120871208812089120901209112092120931209412095120961209712098120991210012101121021210312104121051210612107121081210912110121111211212113121141211512116121171211812119121201212112122121231212412125121261212712128121291213012131121321213312134121351213612137121381213912140121411214212143121441214512146121471214812149121501215112152121531215412155121561215712158121591216012161121621216312164121651216612167121681216912170121711217212173121741217512176121771217812179121801218112182121831218412185121861218712188121891219012191121921219312194121951219612197121981219912200122011220212203122041220512206122071220812209122101221112212122131221412215122161221712218122191222012221122221222312224122251222612227122281222912230122311223212233122341223512236122371223812239122401224112242122431224412245122461224712248122491225012251122521225312254122551225612257122581225912260122611226212263122641226512266122671226812269122701227112272122731227412275122761227712278122791228012281122821228312284122851228612287122881228912290122911229212293122941229512296122971229812299123001230112302123031230412305123061230712308123091231012311123121231312314123151231612317123181231912320123211232212323123241232512326123271232812329123301233112332123331233412335123361233712338123391234012341123421234312344123451234612347123481234912350123511235212353123541235512356123571235812359123601236112362123631236412365123661236712368123691237012371123721237312374123751237612377123781237912380123811238212383123841238512386123871238812389123901239112392123931239412395123961239712398123991240012401124021240312404124051240612407124081240912410124111241212413124141241512416124171241812419124201242112422124231242412425124261242712428124291243012431124321243312434124351243612437124381243912440124411244212443124441244512446124471244812449124501245112452124531245412455124561245712458124591246012461124621246312464124651246612467124681246912470124711247212473124741247512476124771247812479124801248112482124831248412485124861248712488124891249012491124921249312494124951249612497124981249912500125011250212503125041250512506125071250812509125101251112512125131251412515125161251712518125191252012521125221252312524125251252612527125281252912530125311253212533125341253512536125371253812539125401254112542125431254412545125461254712548125491255012551125521255312554125551255612557125581255912560125611256212563125641256512566125671256812569125701257112572125731257412575125761257712578125791258012581125821258312584125851258612587125881258912590125911259212593125941259512596125971259812599126001260112602126031260412605126061260712608126091261012611126121261312614126151261612617126181261912620126211262212623126241262512626126271262812629126301263112632126331263412635126361263712638126391264012641126421264312644126451264612647126481264912650126511265212653126541265512656126571265812659126601266112662126631266412665126661266712668126691267012671126721267312674126751267612677126781267912680126811268212683126841268512686126871268812689126901269112692126931269412695126961269712698126991270012701127021270312704127051270612707127081270912710127111271212713127141271512716127171271812719127201272112722127231272412725127261272712728127291273012731127321273312734127351273612737127381273912740127411274212743127441274512746127471274812749127501275112752127531275412755
  1. /*
  2. * Copyright 2016-2025 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. /*
  10. * We need access to the deprecated low level HMAC APIs for legacy purposes
  11. * when the deprecated calls are not hidden
  12. */
  13. #ifndef OPENSSL_NO_DEPRECATED_3_0
  14. # define OPENSSL_SUPPRESS_DEPRECATED
  15. #endif
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <openssl/opensslconf.h>
  19. #include <openssl/bio.h>
  20. #include <openssl/crypto.h>
  21. #include <openssl/ssl.h>
  22. #include <openssl/ocsp.h>
  23. #include <openssl/srp.h>
  24. #include <openssl/txt_db.h>
  25. #include <openssl/aes.h>
  26. #include <openssl/rand.h>
  27. #include <openssl/core_names.h>
  28. #include <openssl/core_dispatch.h>
  29. #include <openssl/provider.h>
  30. #include <openssl/param_build.h>
  31. #include <openssl/x509v3.h>
  32. #include <openssl/dh.h>
  33. #include <openssl/engine.h>
  34. #include "helpers/ssltestlib.h"
  35. #include "testutil.h"
  36. #include "testutil/output.h"
  37. #include "internal/nelem.h"
  38. #include "internal/tlsgroups.h"
  39. #include "internal/ktls.h"
  40. #include "../ssl/ssl_local.h"
  41. #include "../ssl/record/methods/recmethod_local.h"
  42. #include "filterprov.h"
  43. #undef OSSL_NO_USABLE_TLS1_3
  44. #if defined(OPENSSL_NO_TLS1_3) \
  45. || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
  46. /*
  47. * If we don't have ec or dh then there are no built-in groups that are usable
  48. * with TLSv1.3
  49. */
  50. # define OSSL_NO_USABLE_TLS1_3
  51. #endif
  52. /* Defined in tls-provider.c */
  53. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  54. const OSSL_DISPATCH *in,
  55. const OSSL_DISPATCH **out,
  56. void **provctx);
  57. static OSSL_LIB_CTX *libctx = NULL;
  58. static OSSL_PROVIDER *defctxnull = NULL;
  59. #ifndef OSSL_NO_USABLE_TLS1_3
  60. static SSL_SESSION *clientpsk = NULL;
  61. static SSL_SESSION *serverpsk = NULL;
  62. static const char *pskid = "Identity";
  63. static const char *srvid;
  64. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  65. size_t *idlen, SSL_SESSION **sess);
  66. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  67. size_t identity_len, SSL_SESSION **sess);
  68. static int use_session_cb_cnt = 0;
  69. static int find_session_cb_cnt = 0;
  70. #endif
  71. static char *certsdir = NULL;
  72. static char *cert = NULL;
  73. static char *privkey = NULL;
  74. static char *cert2 = NULL;
  75. static char *privkey2 = NULL;
  76. static char *cert1024 = NULL;
  77. static char *privkey1024 = NULL;
  78. static char *cert3072 = NULL;
  79. static char *privkey3072 = NULL;
  80. static char *cert4096 = NULL;
  81. static char *privkey4096 = NULL;
  82. static char *cert8192 = NULL;
  83. static char *privkey8192 = NULL;
  84. static char *srpvfile = NULL;
  85. static char *tmpfilename = NULL;
  86. static char *dhfile = NULL;
  87. static int is_fips = 0;
  88. static int fips_ems_check = 0;
  89. #define LOG_BUFFER_SIZE 2048
  90. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  91. static size_t server_log_buffer_index = 0;
  92. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  93. static size_t client_log_buffer_index = 0;
  94. static int error_writing_log = 0;
  95. #ifndef OPENSSL_NO_OCSP
  96. static const unsigned char orespder[] = "Dummy OCSP Response";
  97. static int ocsp_server_called = 0;
  98. static int ocsp_client_called = 0;
  99. static int cdummyarg = 1;
  100. static X509 *ocspcert = NULL;
  101. #endif
  102. #define CLIENT_VERSION_LEN 2
  103. /*
  104. * This structure is used to validate that the correct number of log messages
  105. * of various types are emitted when emitting secret logs.
  106. */
  107. struct sslapitest_log_counts {
  108. unsigned int rsa_key_exchange_count;
  109. unsigned int master_secret_count;
  110. unsigned int client_early_secret_count;
  111. unsigned int client_handshake_secret_count;
  112. unsigned int server_handshake_secret_count;
  113. unsigned int client_application_secret_count;
  114. unsigned int server_application_secret_count;
  115. unsigned int early_exporter_secret_count;
  116. unsigned int exporter_secret_count;
  117. };
  118. static int hostname_cb(SSL *s, int *al, void *arg)
  119. {
  120. const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  121. if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
  122. || strcmp(hostname, "altgoodhost") == 0))
  123. return SSL_TLSEXT_ERR_OK;
  124. return SSL_TLSEXT_ERR_NOACK;
  125. }
  126. static void client_keylog_callback(const SSL *ssl, const char *line)
  127. {
  128. int line_length = strlen(line);
  129. /* If the log doesn't fit, error out. */
  130. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  131. TEST_info("Client log too full");
  132. error_writing_log = 1;
  133. return;
  134. }
  135. strcat(client_log_buffer, line);
  136. client_log_buffer_index += line_length;
  137. client_log_buffer[client_log_buffer_index++] = '\n';
  138. }
  139. static void server_keylog_callback(const SSL *ssl, const char *line)
  140. {
  141. int line_length = strlen(line);
  142. /* If the log doesn't fit, error out. */
  143. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  144. TEST_info("Server log too full");
  145. error_writing_log = 1;
  146. return;
  147. }
  148. strcat(server_log_buffer, line);
  149. server_log_buffer_index += line_length;
  150. server_log_buffer[server_log_buffer_index++] = '\n';
  151. }
  152. static int compare_hex_encoded_buffer(const char *hex_encoded,
  153. size_t hex_length,
  154. const uint8_t *raw,
  155. size_t raw_length)
  156. {
  157. size_t i, j;
  158. char hexed[3];
  159. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  160. return 1;
  161. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  162. BIO_snprintf(hexed, sizeof(hexed), "%02x", raw[i]);
  163. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  164. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  165. return 1;
  166. }
  167. return 0;
  168. }
  169. static int test_keylog_output(char *buffer, const SSL *ssl,
  170. const SSL_SESSION *session,
  171. struct sslapitest_log_counts *expected)
  172. {
  173. char *token = NULL;
  174. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  175. size_t client_random_size = SSL3_RANDOM_SIZE;
  176. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  177. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  178. unsigned int rsa_key_exchange_count = 0;
  179. unsigned int master_secret_count = 0;
  180. unsigned int client_early_secret_count = 0;
  181. unsigned int client_handshake_secret_count = 0;
  182. unsigned int server_handshake_secret_count = 0;
  183. unsigned int client_application_secret_count = 0;
  184. unsigned int server_application_secret_count = 0;
  185. unsigned int early_exporter_secret_count = 0;
  186. unsigned int exporter_secret_count = 0;
  187. for (token = strtok(buffer, " \n"); token != NULL;
  188. token = strtok(NULL, " \n")) {
  189. if (strcmp(token, "RSA") == 0) {
  190. /*
  191. * Premaster secret. Tokens should be: 16 ASCII bytes of
  192. * hex-encoded encrypted secret, then the hex-encoded pre-master
  193. * secret.
  194. */
  195. if (!TEST_ptr(token = strtok(NULL, " \n")))
  196. return 0;
  197. if (!TEST_size_t_eq(strlen(token), 16))
  198. return 0;
  199. if (!TEST_ptr(token = strtok(NULL, " \n")))
  200. return 0;
  201. /*
  202. * We can't sensibly check the log because the premaster secret is
  203. * transient, and OpenSSL doesn't keep hold of it once the master
  204. * secret is generated.
  205. */
  206. rsa_key_exchange_count++;
  207. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  208. /*
  209. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  210. * client random, then the hex-encoded master secret.
  211. */
  212. client_random_size = SSL_get_client_random(ssl,
  213. actual_client_random,
  214. SSL3_RANDOM_SIZE);
  215. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  216. return 0;
  217. if (!TEST_ptr(token = strtok(NULL, " \n")))
  218. return 0;
  219. if (!TEST_size_t_eq(strlen(token), 64))
  220. return 0;
  221. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  222. actual_client_random,
  223. client_random_size)))
  224. return 0;
  225. if (!TEST_ptr(token = strtok(NULL, " \n")))
  226. return 0;
  227. master_key_size = SSL_SESSION_get_master_key(session,
  228. actual_master_key,
  229. master_key_size);
  230. if (!TEST_size_t_ne(master_key_size, 0))
  231. return 0;
  232. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  233. actual_master_key,
  234. master_key_size)))
  235. return 0;
  236. master_secret_count++;
  237. } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
  238. || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  239. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  240. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  241. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
  242. || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
  243. || strcmp(token, "EXPORTER_SECRET") == 0) {
  244. /*
  245. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  246. * client random, and then the hex-encoded secret. In this case,
  247. * we treat all of these secrets identically and then just
  248. * distinguish between them when counting what we saw.
  249. */
  250. if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
  251. client_early_secret_count++;
  252. else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  253. client_handshake_secret_count++;
  254. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  255. server_handshake_secret_count++;
  256. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  257. client_application_secret_count++;
  258. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  259. server_application_secret_count++;
  260. else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
  261. early_exporter_secret_count++;
  262. else if (strcmp(token, "EXPORTER_SECRET") == 0)
  263. exporter_secret_count++;
  264. client_random_size = SSL_get_client_random(ssl,
  265. actual_client_random,
  266. SSL3_RANDOM_SIZE);
  267. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  268. return 0;
  269. if (!TEST_ptr(token = strtok(NULL, " \n")))
  270. return 0;
  271. if (!TEST_size_t_eq(strlen(token), 64))
  272. return 0;
  273. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  274. actual_client_random,
  275. client_random_size)))
  276. return 0;
  277. if (!TEST_ptr(token = strtok(NULL, " \n")))
  278. return 0;
  279. } else {
  280. TEST_info("Unexpected token %s\n", token);
  281. return 0;
  282. }
  283. }
  284. /* Got what we expected? */
  285. if (!TEST_size_t_eq(rsa_key_exchange_count,
  286. expected->rsa_key_exchange_count)
  287. || !TEST_size_t_eq(master_secret_count,
  288. expected->master_secret_count)
  289. || !TEST_size_t_eq(client_early_secret_count,
  290. expected->client_early_secret_count)
  291. || !TEST_size_t_eq(client_handshake_secret_count,
  292. expected->client_handshake_secret_count)
  293. || !TEST_size_t_eq(server_handshake_secret_count,
  294. expected->server_handshake_secret_count)
  295. || !TEST_size_t_eq(client_application_secret_count,
  296. expected->client_application_secret_count)
  297. || !TEST_size_t_eq(server_application_secret_count,
  298. expected->server_application_secret_count)
  299. || !TEST_size_t_eq(early_exporter_secret_count,
  300. expected->early_exporter_secret_count)
  301. || !TEST_size_t_eq(exporter_secret_count,
  302. expected->exporter_secret_count))
  303. return 0;
  304. return 1;
  305. }
  306. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  307. static int test_keylog(void)
  308. {
  309. SSL_CTX *cctx = NULL, *sctx = NULL;
  310. SSL *clientssl = NULL, *serverssl = NULL;
  311. int testresult = 0;
  312. struct sslapitest_log_counts expected;
  313. /* Clean up logging space */
  314. memset(&expected, 0, sizeof(expected));
  315. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  316. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  317. client_log_buffer_index = 0;
  318. server_log_buffer_index = 0;
  319. error_writing_log = 0;
  320. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  321. TLS_client_method(),
  322. TLS1_VERSION, 0,
  323. &sctx, &cctx, cert, privkey)))
  324. return 0;
  325. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  326. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  327. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  328. /* We also want to ensure that we use RSA-based key exchange. */
  329. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  330. goto end;
  331. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  332. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  333. goto end;
  334. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  335. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  336. == client_keylog_callback))
  337. goto end;
  338. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  339. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  340. == server_keylog_callback))
  341. goto end;
  342. /* Now do a handshake and check that the logs have been written to. */
  343. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  344. &clientssl, NULL, NULL))
  345. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  346. SSL_ERROR_NONE))
  347. || !TEST_false(error_writing_log)
  348. || !TEST_int_gt(client_log_buffer_index, 0)
  349. || !TEST_int_gt(server_log_buffer_index, 0))
  350. goto end;
  351. /*
  352. * Now we want to test that our output data was vaguely sensible. We
  353. * do that by using strtok and confirming that we have more or less the
  354. * data we expect. For both client and server, we expect to see one master
  355. * secret. The client should also see an RSA key exchange.
  356. */
  357. expected.rsa_key_exchange_count = 1;
  358. expected.master_secret_count = 1;
  359. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  360. SSL_get_session(clientssl), &expected)))
  361. goto end;
  362. expected.rsa_key_exchange_count = 0;
  363. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  364. SSL_get_session(serverssl), &expected)))
  365. goto end;
  366. testresult = 1;
  367. end:
  368. SSL_free(serverssl);
  369. SSL_free(clientssl);
  370. SSL_CTX_free(sctx);
  371. SSL_CTX_free(cctx);
  372. return testresult;
  373. }
  374. #endif
  375. #ifndef OSSL_NO_USABLE_TLS1_3
  376. static int test_keylog_no_master_key(void)
  377. {
  378. SSL_CTX *cctx = NULL, *sctx = NULL;
  379. SSL *clientssl = NULL, *serverssl = NULL;
  380. SSL_SESSION *sess = NULL;
  381. int testresult = 0;
  382. struct sslapitest_log_counts expected;
  383. unsigned char buf[1];
  384. size_t readbytes, written;
  385. /* Clean up logging space */
  386. memset(&expected, 0, sizeof(expected));
  387. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  388. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  389. client_log_buffer_index = 0;
  390. server_log_buffer_index = 0;
  391. error_writing_log = 0;
  392. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  393. TLS_client_method(), TLS1_VERSION, 0,
  394. &sctx, &cctx, cert, privkey))
  395. || !TEST_true(SSL_CTX_set_max_early_data(sctx,
  396. SSL3_RT_MAX_PLAIN_LENGTH)))
  397. return 0;
  398. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  399. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  400. goto end;
  401. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  402. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  403. == client_keylog_callback))
  404. goto end;
  405. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  406. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  407. == server_keylog_callback))
  408. goto end;
  409. /* Now do a handshake and check that the logs have been written to. */
  410. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  411. &clientssl, NULL, NULL))
  412. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  413. SSL_ERROR_NONE))
  414. || !TEST_false(error_writing_log))
  415. goto end;
  416. /*
  417. * Now we want to test that our output data was vaguely sensible. For this
  418. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  419. * TLSv1.3, but we do expect both client and server to emit keys.
  420. */
  421. expected.client_handshake_secret_count = 1;
  422. expected.server_handshake_secret_count = 1;
  423. expected.client_application_secret_count = 1;
  424. expected.server_application_secret_count = 1;
  425. expected.exporter_secret_count = 1;
  426. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  427. SSL_get_session(clientssl), &expected))
  428. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  429. SSL_get_session(serverssl),
  430. &expected)))
  431. goto end;
  432. /* Terminate old session and resume with early data. */
  433. sess = SSL_get1_session(clientssl);
  434. SSL_shutdown(clientssl);
  435. SSL_shutdown(serverssl);
  436. SSL_free(serverssl);
  437. SSL_free(clientssl);
  438. serverssl = clientssl = NULL;
  439. /* Reset key log */
  440. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  441. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  442. client_log_buffer_index = 0;
  443. server_log_buffer_index = 0;
  444. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  445. &clientssl, NULL, NULL))
  446. || !TEST_true(SSL_set_session(clientssl, sess))
  447. /* Here writing 0 length early data is enough. */
  448. || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  449. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  450. &readbytes),
  451. SSL_READ_EARLY_DATA_ERROR)
  452. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  453. SSL_EARLY_DATA_ACCEPTED)
  454. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  455. SSL_ERROR_NONE))
  456. || !TEST_true(SSL_session_reused(clientssl)))
  457. goto end;
  458. /* In addition to the previous entries, expect early secrets. */
  459. expected.client_early_secret_count = 1;
  460. expected.early_exporter_secret_count = 1;
  461. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  462. SSL_get_session(clientssl), &expected))
  463. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  464. SSL_get_session(serverssl),
  465. &expected)))
  466. goto end;
  467. testresult = 1;
  468. end:
  469. SSL_SESSION_free(sess);
  470. SSL_free(serverssl);
  471. SSL_free(clientssl);
  472. SSL_CTX_free(sctx);
  473. SSL_CTX_free(cctx);
  474. return testresult;
  475. }
  476. #endif
  477. static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
  478. {
  479. int res = X509_verify_cert(ctx);
  480. int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  481. SSL *ssl;
  482. /* this should not happen but check anyway */
  483. if (idx < 0
  484. || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
  485. return 0;
  486. if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
  487. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
  488. /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
  489. return SSL_set_retry_verify(ssl);
  490. return res;
  491. }
  492. static int test_client_cert_verify_cb(void)
  493. {
  494. /* server key, cert, chain, and root */
  495. char *skey = test_mk_file_path(certsdir, "leaf.key");
  496. char *leaf = test_mk_file_path(certsdir, "leaf.pem");
  497. char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
  498. char *int1 = test_mk_file_path(certsdir, "interCA.pem");
  499. char *root = test_mk_file_path(certsdir, "rootCA.pem");
  500. X509 *crt1 = NULL, *crt2 = NULL;
  501. STACK_OF(X509) *server_chain;
  502. SSL_CTX *cctx = NULL, *sctx = NULL;
  503. SSL *clientssl = NULL, *serverssl = NULL;
  504. int testresult = 0;
  505. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  506. TLS_client_method(), TLS1_VERSION, 0,
  507. &sctx, &cctx, NULL, NULL)))
  508. goto end;
  509. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
  510. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
  511. SSL_FILETYPE_PEM), 1)
  512. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  513. goto end;
  514. if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
  515. goto end;
  516. SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
  517. SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
  518. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  519. &clientssl, NULL, NULL)))
  520. goto end;
  521. /* attempt SSL_connect() with incomplete server chain */
  522. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  523. SSL_ERROR_WANT_RETRY_VERIFY)))
  524. goto end;
  525. /* application provides intermediate certs needed to verify server cert */
  526. if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
  527. || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
  528. || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
  529. goto end;
  530. /* add certs in reverse order to demonstrate real chain building */
  531. if (!TEST_true(sk_X509_push(server_chain, crt1)))
  532. goto end;
  533. crt1 = NULL;
  534. if (!TEST_true(sk_X509_push(server_chain, crt2)))
  535. goto end;
  536. crt2 = NULL;
  537. /* continue SSL_connect(), must now succeed with completed server chain */
  538. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  539. SSL_ERROR_NONE)))
  540. goto end;
  541. testresult = 1;
  542. end:
  543. X509_free(crt1);
  544. X509_free(crt2);
  545. if (clientssl != NULL) {
  546. SSL_shutdown(clientssl);
  547. SSL_free(clientssl);
  548. }
  549. if (serverssl != NULL) {
  550. SSL_shutdown(serverssl);
  551. SSL_free(serverssl);
  552. }
  553. SSL_CTX_free(sctx);
  554. SSL_CTX_free(cctx);
  555. OPENSSL_free(skey);
  556. OPENSSL_free(leaf);
  557. OPENSSL_free(int2);
  558. OPENSSL_free(int1);
  559. OPENSSL_free(root);
  560. return testresult;
  561. }
  562. static int test_ssl_build_cert_chain(void)
  563. {
  564. int ret = 0;
  565. SSL_CTX *ssl_ctx = NULL;
  566. SSL *ssl = NULL;
  567. char *skey = test_mk_file_path(certsdir, "leaf.key");
  568. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  569. if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  570. goto end;
  571. if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
  572. goto end;
  573. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  574. if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
  575. || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
  576. || !TEST_int_eq(SSL_check_private_key(ssl), 1))
  577. goto end;
  578. if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  579. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  580. goto end;
  581. ret = 1;
  582. end:
  583. SSL_free(ssl);
  584. SSL_CTX_free(ssl_ctx);
  585. OPENSSL_free(leaf_chain);
  586. OPENSSL_free(skey);
  587. return ret;
  588. }
  589. static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
  590. {
  591. static const char pass[] = "testpass";
  592. if (!TEST_int_eq(size, PEM_BUFSIZE))
  593. return -1;
  594. memcpy(buf, pass, sizeof(pass) - 1);
  595. return sizeof(pass) - 1;
  596. }
  597. static int test_ssl_ctx_build_cert_chain(void)
  598. {
  599. int ret = 0;
  600. SSL_CTX *ctx = NULL;
  601. char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
  602. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  603. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  604. goto end;
  605. SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
  606. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  607. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
  608. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
  609. SSL_FILETYPE_PEM), 1)
  610. || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
  611. goto end;
  612. if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  613. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  614. goto end;
  615. ret = 1;
  616. end:
  617. SSL_CTX_free(ctx);
  618. OPENSSL_free(leaf_chain);
  619. OPENSSL_free(skey);
  620. return ret;
  621. }
  622. #ifndef OPENSSL_NO_TLS1_2
  623. static int full_client_hello_callback(SSL *s, int *al, void *arg)
  624. {
  625. int *ctr = arg;
  626. const unsigned char *p;
  627. int *exts;
  628. /* We only configure two ciphers, but the SCSV is added automatically. */
  629. #ifdef OPENSSL_NO_EC
  630. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  631. #else
  632. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  633. 0x2c, 0x00, 0xff};
  634. #endif
  635. const int expected_extensions[] = {
  636. #ifndef OPENSSL_NO_EC
  637. 11, 10,
  638. #endif
  639. 35, 22, 23, 13};
  640. size_t len;
  641. /* Make sure we can defer processing and get called back. */
  642. if ((*ctr)++ == 0)
  643. return SSL_CLIENT_HELLO_RETRY;
  644. len = SSL_client_hello_get0_ciphers(s, &p);
  645. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  646. || !TEST_size_t_eq(
  647. SSL_client_hello_get0_compression_methods(s, &p), 1)
  648. || !TEST_int_eq(*p, 0))
  649. return SSL_CLIENT_HELLO_ERROR;
  650. if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
  651. return SSL_CLIENT_HELLO_ERROR;
  652. if (len != OSSL_NELEM(expected_extensions) ||
  653. memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
  654. printf("ClientHello callback expected extensions mismatch\n");
  655. OPENSSL_free(exts);
  656. return SSL_CLIENT_HELLO_ERROR;
  657. }
  658. OPENSSL_free(exts);
  659. return SSL_CLIENT_HELLO_SUCCESS;
  660. }
  661. static int test_client_hello_cb(void)
  662. {
  663. SSL_CTX *cctx = NULL, *sctx = NULL;
  664. SSL *clientssl = NULL, *serverssl = NULL;
  665. int testctr = 0, testresult = 0;
  666. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  667. TLS_client_method(), TLS1_VERSION, 0,
  668. &sctx, &cctx, cert, privkey)))
  669. goto end;
  670. SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
  671. /* The gimpy cipher list we configure can't do TLS 1.3. */
  672. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  673. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  674. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  675. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  676. &clientssl, NULL, NULL))
  677. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  678. SSL_ERROR_WANT_CLIENT_HELLO_CB))
  679. /*
  680. * Passing a -1 literal is a hack since
  681. * the real value was lost.
  682. * */
  683. || !TEST_int_eq(SSL_get_error(serverssl, -1),
  684. SSL_ERROR_WANT_CLIENT_HELLO_CB)
  685. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  686. SSL_ERROR_NONE)))
  687. goto end;
  688. testresult = 1;
  689. end:
  690. SSL_free(serverssl);
  691. SSL_free(clientssl);
  692. SSL_CTX_free(sctx);
  693. SSL_CTX_free(cctx);
  694. return testresult;
  695. }
  696. static int test_no_ems(void)
  697. {
  698. SSL_CTX *cctx = NULL, *sctx = NULL;
  699. SSL *clientssl = NULL, *serverssl = NULL;
  700. int testresult = 0, status;
  701. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  702. TLS1_VERSION, TLS1_2_VERSION,
  703. &sctx, &cctx, cert, privkey)) {
  704. printf("Unable to create SSL_CTX pair\n");
  705. goto end;
  706. }
  707. SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  708. if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
  709. printf("Unable to create SSL objects\n");
  710. goto end;
  711. }
  712. status = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  713. if (fips_ems_check) {
  714. if (status == 1) {
  715. printf("When FIPS uses the EMS check a connection that doesn't use EMS should fail\n");
  716. goto end;
  717. }
  718. } else {
  719. if (!status) {
  720. printf("Creating SSL connection failed\n");
  721. goto end;
  722. }
  723. if (SSL_get_extms_support(serverssl)) {
  724. printf("Server reports Extended Master Secret support\n");
  725. goto end;
  726. }
  727. if (SSL_get_extms_support(clientssl)) {
  728. printf("Client reports Extended Master Secret support\n");
  729. goto end;
  730. }
  731. }
  732. testresult = 1;
  733. end:
  734. SSL_free(serverssl);
  735. SSL_free(clientssl);
  736. SSL_CTX_free(sctx);
  737. SSL_CTX_free(cctx);
  738. return testresult;
  739. }
  740. /*
  741. * Very focused test to exercise a single case in the server-side state
  742. * machine, when the ChangeCipherState message needs to actually change
  743. * from one cipher to a different cipher (i.e., not changing from null
  744. * encryption to real encryption).
  745. */
  746. static int test_ccs_change_cipher(void)
  747. {
  748. SSL_CTX *cctx = NULL, *sctx = NULL;
  749. SSL *clientssl = NULL, *serverssl = NULL;
  750. SSL_SESSION *sess = NULL, *sesspre, *sesspost;
  751. int testresult = 0;
  752. int i;
  753. unsigned char buf;
  754. size_t readbytes;
  755. /*
  756. * Create a connection so we can resume and potentially (but not) use
  757. * a different cipher in the second connection.
  758. */
  759. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  760. TLS_client_method(),
  761. TLS1_VERSION, TLS1_2_VERSION,
  762. &sctx, &cctx, cert, privkey))
  763. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
  764. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  765. NULL, NULL))
  766. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  767. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  768. SSL_ERROR_NONE))
  769. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  770. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  771. goto end;
  772. shutdown_ssl_connection(serverssl, clientssl);
  773. serverssl = clientssl = NULL;
  774. /* Resume, preferring a different cipher. Our server will force the
  775. * same cipher to be used as the initial handshake. */
  776. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  777. NULL, NULL))
  778. || !TEST_true(SSL_set_session(clientssl, sess))
  779. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
  780. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  781. SSL_ERROR_NONE))
  782. || !TEST_true(SSL_session_reused(clientssl))
  783. || !TEST_true(SSL_session_reused(serverssl))
  784. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  785. || !TEST_ptr_eq(sesspre, sesspost)
  786. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  787. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  788. goto end;
  789. shutdown_ssl_connection(serverssl, clientssl);
  790. serverssl = clientssl = NULL;
  791. /*
  792. * Now create a fresh connection and try to renegotiate a different
  793. * cipher on it.
  794. */
  795. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  796. NULL, NULL))
  797. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  798. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  799. SSL_ERROR_NONE))
  800. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  801. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
  802. || !TEST_true(SSL_renegotiate(clientssl))
  803. || !TEST_true(SSL_renegotiate_pending(clientssl)))
  804. goto end;
  805. /* Actually drive the renegotiation. */
  806. for (i = 0; i < 3; i++) {
  807. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  808. if (!TEST_ulong_eq(readbytes, 0))
  809. goto end;
  810. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  811. SSL_ERROR_WANT_READ)) {
  812. goto end;
  813. }
  814. if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
  815. if (!TEST_ulong_eq(readbytes, 0))
  816. goto end;
  817. } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
  818. SSL_ERROR_WANT_READ)) {
  819. goto end;
  820. }
  821. }
  822. /* sesspre and sesspost should be different since the cipher changed. */
  823. if (!TEST_false(SSL_renegotiate_pending(clientssl))
  824. || !TEST_false(SSL_session_reused(clientssl))
  825. || !TEST_false(SSL_session_reused(serverssl))
  826. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  827. || !TEST_ptr_ne(sesspre, sesspost)
  828. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  829. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  830. goto end;
  831. shutdown_ssl_connection(serverssl, clientssl);
  832. serverssl = clientssl = NULL;
  833. testresult = 1;
  834. end:
  835. SSL_free(serverssl);
  836. SSL_free(clientssl);
  837. SSL_CTX_free(sctx);
  838. SSL_CTX_free(cctx);
  839. SSL_SESSION_free(sess);
  840. return testresult;
  841. }
  842. #endif
  843. static int execute_test_large_message(const SSL_METHOD *smeth,
  844. const SSL_METHOD *cmeth,
  845. int min_version, int max_version,
  846. int read_ahead)
  847. {
  848. SSL_CTX *cctx = NULL, *sctx = NULL;
  849. SSL *clientssl = NULL, *serverssl = NULL;
  850. int testresult = 0;
  851. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  852. max_version, &sctx, &cctx, cert,
  853. privkey)))
  854. goto end;
  855. #ifdef OPENSSL_NO_DTLS1_2
  856. if (smeth == DTLS_server_method()) {
  857. /*
  858. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  859. * level 0
  860. */
  861. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  862. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  863. "DEFAULT:@SECLEVEL=0")))
  864. goto end;
  865. }
  866. #endif
  867. if (read_ahead) {
  868. /*
  869. * Test that read_ahead works correctly when dealing with large
  870. * records
  871. */
  872. SSL_CTX_set_read_ahead(cctx, 1);
  873. }
  874. if (!ssl_ctx_add_large_cert_chain(libctx, sctx, cert))
  875. goto end;
  876. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  877. NULL, NULL))
  878. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  879. SSL_ERROR_NONE)))
  880. goto end;
  881. /*
  882. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  883. * doesn't leak.
  884. */
  885. if (!TEST_true(SSL_clear(serverssl)))
  886. goto end;
  887. testresult = 1;
  888. end:
  889. SSL_free(serverssl);
  890. SSL_free(clientssl);
  891. SSL_CTX_free(sctx);
  892. SSL_CTX_free(cctx);
  893. return testresult;
  894. }
  895. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
  896. !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
  897. /* sock must be connected */
  898. static int ktls_chk_platform(int sock)
  899. {
  900. if (!ktls_enable(sock))
  901. return 0;
  902. return 1;
  903. }
  904. static int ping_pong_query(SSL *clientssl, SSL *serverssl)
  905. {
  906. static char count = 1;
  907. unsigned char cbuf[16000] = {0};
  908. unsigned char sbuf[16000];
  909. size_t err = 0;
  910. char crec_wseq_before[SEQ_NUM_SIZE];
  911. char crec_wseq_after[SEQ_NUM_SIZE];
  912. char crec_rseq_before[SEQ_NUM_SIZE];
  913. char crec_rseq_after[SEQ_NUM_SIZE];
  914. char srec_wseq_before[SEQ_NUM_SIZE];
  915. char srec_wseq_after[SEQ_NUM_SIZE];
  916. char srec_rseq_before[SEQ_NUM_SIZE];
  917. char srec_rseq_after[SEQ_NUM_SIZE];
  918. SSL_CONNECTION *clientsc, *serversc;
  919. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  920. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  921. goto end;
  922. cbuf[0] = count++;
  923. memcpy(crec_wseq_before, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  924. memcpy(srec_wseq_before, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  925. memcpy(crec_rseq_before, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  926. memcpy(srec_rseq_before, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  927. if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
  928. goto end;
  929. while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
  930. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
  931. goto end;
  932. }
  933. }
  934. if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
  935. goto end;
  936. while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
  937. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
  938. goto end;
  939. }
  940. }
  941. memcpy(crec_wseq_after, &clientsc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  942. memcpy(srec_wseq_after, &serversc->rlayer.wrl->sequence, SEQ_NUM_SIZE);
  943. memcpy(crec_rseq_after, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  944. memcpy(srec_rseq_after, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  945. /* verify the payload */
  946. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  947. goto end;
  948. /*
  949. * If ktls is used then kernel sequences are used instead of
  950. * OpenSSL sequences
  951. */
  952. if (!BIO_get_ktls_send(clientsc->wbio)) {
  953. if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
  954. crec_wseq_after, SEQ_NUM_SIZE))
  955. goto end;
  956. } else {
  957. if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
  958. crec_wseq_after, SEQ_NUM_SIZE))
  959. goto end;
  960. }
  961. if (!BIO_get_ktls_send(serversc->wbio)) {
  962. if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
  963. srec_wseq_after, SEQ_NUM_SIZE))
  964. goto end;
  965. } else {
  966. if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
  967. srec_wseq_after, SEQ_NUM_SIZE))
  968. goto end;
  969. }
  970. if (!BIO_get_ktls_recv(clientsc->wbio)) {
  971. if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
  972. crec_rseq_after, SEQ_NUM_SIZE))
  973. goto end;
  974. } else {
  975. if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
  976. crec_rseq_after, SEQ_NUM_SIZE))
  977. goto end;
  978. }
  979. if (!BIO_get_ktls_recv(serversc->wbio)) {
  980. if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
  981. srec_rseq_after, SEQ_NUM_SIZE))
  982. goto end;
  983. } else {
  984. if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
  985. srec_rseq_after, SEQ_NUM_SIZE))
  986. goto end;
  987. }
  988. return 1;
  989. end:
  990. return 0;
  991. }
  992. static int execute_test_ktls(int cis_ktls, int sis_ktls,
  993. int tls_version, const char *cipher)
  994. {
  995. SSL_CTX *cctx = NULL, *sctx = NULL;
  996. SSL *clientssl = NULL, *serverssl = NULL;
  997. int ktls_used = 0, testresult = 0;
  998. int cfd = -1, sfd = -1;
  999. int rx_supported;
  1000. SSL_CONNECTION *clientsc, *serversc;
  1001. unsigned char *buf = NULL;
  1002. const size_t bufsz = SSL3_RT_MAX_PLAIN_LENGTH + 16;
  1003. int ret;
  1004. size_t offset = 0, i;
  1005. if (!TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL)))
  1006. goto end;
  1007. /* Skip this test if the platform does not support ktls */
  1008. if (!ktls_chk_platform(cfd)) {
  1009. testresult = TEST_skip("Kernel does not support KTLS");
  1010. goto end;
  1011. }
  1012. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1013. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1014. goto end;
  1015. }
  1016. /* Create a session based on SHA-256 */
  1017. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1018. TLS_client_method(),
  1019. tls_version, tls_version,
  1020. &sctx, &cctx, cert, privkey)))
  1021. goto end;
  1022. if (tls_version == TLS1_3_VERSION) {
  1023. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1024. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1025. goto end;
  1026. } else {
  1027. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1028. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1029. goto end;
  1030. }
  1031. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1032. &clientssl, sfd, cfd)))
  1033. goto end;
  1034. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  1035. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1036. goto end;
  1037. if (cis_ktls) {
  1038. if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
  1039. goto end;
  1040. }
  1041. if (sis_ktls) {
  1042. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1043. goto end;
  1044. }
  1045. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  1046. goto end;
  1047. /*
  1048. * The running kernel may not support a given cipher suite
  1049. * or direction, so just check that KTLS isn't used when it
  1050. * isn't enabled.
  1051. */
  1052. if (!cis_ktls) {
  1053. if (!TEST_false(BIO_get_ktls_send(clientsc->wbio)))
  1054. goto end;
  1055. } else {
  1056. if (BIO_get_ktls_send(clientsc->wbio))
  1057. ktls_used = 1;
  1058. }
  1059. if (!sis_ktls) {
  1060. if (!TEST_false(BIO_get_ktls_send(serversc->wbio)))
  1061. goto end;
  1062. } else {
  1063. if (BIO_get_ktls_send(serversc->wbio))
  1064. ktls_used = 1;
  1065. }
  1066. #if defined(OPENSSL_NO_KTLS_RX)
  1067. rx_supported = 0;
  1068. #else
  1069. rx_supported = 1;
  1070. #endif
  1071. if (!cis_ktls || !rx_supported) {
  1072. if (!TEST_false(BIO_get_ktls_recv(clientsc->rbio)))
  1073. goto end;
  1074. } else {
  1075. if (BIO_get_ktls_send(clientsc->rbio))
  1076. ktls_used = 1;
  1077. }
  1078. if (!sis_ktls || !rx_supported) {
  1079. if (!TEST_false(BIO_get_ktls_recv(serversc->rbio)))
  1080. goto end;
  1081. } else {
  1082. if (BIO_get_ktls_send(serversc->rbio))
  1083. ktls_used = 1;
  1084. }
  1085. if ((cis_ktls || sis_ktls) && !ktls_used) {
  1086. testresult = TEST_skip("KTLS not supported for %s cipher %s",
  1087. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1088. "TLS 1.2", cipher);
  1089. goto end;
  1090. }
  1091. if (!TEST_true(ping_pong_query(clientssl, serverssl)))
  1092. goto end;
  1093. buf = OPENSSL_zalloc(bufsz);
  1094. if (!TEST_ptr(buf))
  1095. goto end;
  1096. /*
  1097. * Write some data that exceeds the maximum record length. KTLS may choose
  1098. * to coalesce this data into a single buffer when we read it again.
  1099. */
  1100. while ((ret = SSL_write(clientssl, buf, bufsz)) != (int)bufsz) {
  1101. if (!TEST_true(SSL_get_error(clientssl, ret) == SSL_ERROR_WANT_WRITE))
  1102. goto end;
  1103. }
  1104. /* Now check that we can read all the data we wrote */
  1105. do {
  1106. ret = SSL_read(serverssl, buf + offset, bufsz - offset);
  1107. if (ret <= 0) {
  1108. if (!TEST_true(SSL_get_error(serverssl, ret) == SSL_ERROR_WANT_READ))
  1109. goto end;
  1110. } else {
  1111. offset += ret;
  1112. }
  1113. } while (offset < bufsz);
  1114. if (!TEST_true(offset == bufsz))
  1115. goto end;
  1116. for (i = 0; i < bufsz; i++)
  1117. if (!TEST_true(buf[i] == 0))
  1118. goto end;
  1119. testresult = 1;
  1120. end:
  1121. OPENSSL_free(buf);
  1122. if (clientssl) {
  1123. SSL_shutdown(clientssl);
  1124. SSL_free(clientssl);
  1125. }
  1126. if (serverssl) {
  1127. SSL_shutdown(serverssl);
  1128. SSL_free(serverssl);
  1129. }
  1130. SSL_CTX_free(sctx);
  1131. SSL_CTX_free(cctx);
  1132. serverssl = clientssl = NULL;
  1133. if (cfd != -1)
  1134. close(cfd);
  1135. if (sfd != -1)
  1136. close(sfd);
  1137. return testresult;
  1138. }
  1139. #define SENDFILE_SZ (16 * 4096)
  1140. #define SENDFILE_CHUNK (4 * 4096)
  1141. #define min(a,b) ((a) > (b) ? (b) : (a))
  1142. static int execute_test_ktls_sendfile(int tls_version, const char *cipher,
  1143. int zerocopy)
  1144. {
  1145. SSL_CTX *cctx = NULL, *sctx = NULL;
  1146. SSL *clientssl = NULL, *serverssl = NULL;
  1147. unsigned char *buf, *buf_dst;
  1148. BIO *out = NULL, *in = NULL;
  1149. int cfd = -1, sfd = -1, ffd, err;
  1150. ssize_t chunk_size = 0;
  1151. off_t chunk_off = 0;
  1152. int testresult = 0;
  1153. FILE *ffdp;
  1154. SSL_CONNECTION *serversc;
  1155. buf = OPENSSL_zalloc(SENDFILE_SZ);
  1156. buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
  1157. if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
  1158. || !TEST_true(create_test_sockets(&cfd, &sfd, SOCK_STREAM, NULL)))
  1159. goto end;
  1160. /* Skip this test if the platform does not support ktls */
  1161. if (!ktls_chk_platform(sfd)) {
  1162. testresult = TEST_skip("Kernel does not support KTLS");
  1163. goto end;
  1164. }
  1165. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1166. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1167. goto end;
  1168. }
  1169. /* Create a session based on SHA-256 */
  1170. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1171. TLS_client_method(),
  1172. tls_version, tls_version,
  1173. &sctx, &cctx, cert, privkey)))
  1174. goto end;
  1175. if (tls_version == TLS1_3_VERSION) {
  1176. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1177. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1178. goto end;
  1179. } else {
  1180. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1181. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1182. goto end;
  1183. }
  1184. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1185. &clientssl, sfd, cfd)))
  1186. goto end;
  1187. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1188. goto end;
  1189. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1190. goto end;
  1191. if (zerocopy) {
  1192. if (!TEST_true(SSL_set_options(serverssl,
  1193. SSL_OP_ENABLE_KTLS_TX_ZEROCOPY_SENDFILE)))
  1194. goto end;
  1195. }
  1196. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1197. SSL_ERROR_NONE)))
  1198. goto end;
  1199. if (!BIO_get_ktls_send(serversc->wbio)) {
  1200. testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
  1201. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1202. "TLS 1.2", cipher);
  1203. goto end;
  1204. }
  1205. if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
  1206. goto end;
  1207. out = BIO_new_file(tmpfilename, "wb");
  1208. if (!TEST_ptr(out))
  1209. goto end;
  1210. if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
  1211. goto end;
  1212. BIO_free(out);
  1213. out = NULL;
  1214. in = BIO_new_file(tmpfilename, "rb");
  1215. BIO_get_fp(in, &ffdp);
  1216. ffd = fileno(ffdp);
  1217. while (chunk_off < SENDFILE_SZ) {
  1218. chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
  1219. while ((err = SSL_sendfile(serverssl,
  1220. ffd,
  1221. chunk_off,
  1222. chunk_size,
  1223. 0)) != chunk_size) {
  1224. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
  1225. goto end;
  1226. }
  1227. while ((err = SSL_read(clientssl,
  1228. buf_dst + chunk_off,
  1229. chunk_size)) != chunk_size) {
  1230. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
  1231. goto end;
  1232. }
  1233. /* verify the payload */
  1234. if (!TEST_mem_eq(buf_dst + chunk_off,
  1235. chunk_size,
  1236. buf + chunk_off,
  1237. chunk_size))
  1238. goto end;
  1239. chunk_off += chunk_size;
  1240. }
  1241. testresult = 1;
  1242. end:
  1243. if (clientssl) {
  1244. SSL_shutdown(clientssl);
  1245. SSL_free(clientssl);
  1246. }
  1247. if (serverssl) {
  1248. SSL_shutdown(serverssl);
  1249. SSL_free(serverssl);
  1250. }
  1251. SSL_CTX_free(sctx);
  1252. SSL_CTX_free(cctx);
  1253. serverssl = clientssl = NULL;
  1254. BIO_free(out);
  1255. BIO_free(in);
  1256. if (cfd != -1)
  1257. close(cfd);
  1258. if (sfd != -1)
  1259. close(sfd);
  1260. OPENSSL_free(buf);
  1261. OPENSSL_free(buf_dst);
  1262. return testresult;
  1263. }
  1264. static struct ktls_test_cipher {
  1265. int tls_version;
  1266. const char *cipher;
  1267. } ktls_test_ciphers[] = {
  1268. # if !defined(OPENSSL_NO_TLS1_2)
  1269. # ifdef OPENSSL_KTLS_AES_GCM_128
  1270. { TLS1_2_VERSION, "AES128-GCM-SHA256" },
  1271. # endif
  1272. # ifdef OPENSSL_KTLS_AES_CCM_128
  1273. { TLS1_2_VERSION, "AES128-CCM"},
  1274. # endif
  1275. # ifdef OPENSSL_KTLS_AES_GCM_256
  1276. { TLS1_2_VERSION, "AES256-GCM-SHA384"},
  1277. # endif
  1278. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1279. # ifndef OPENSSL_NO_EC
  1280. { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
  1281. # endif
  1282. # endif
  1283. # endif
  1284. # if !defined(OSSL_NO_USABLE_TLS1_3)
  1285. # ifdef OPENSSL_KTLS_AES_GCM_128
  1286. { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
  1287. # endif
  1288. # ifdef OPENSSL_KTLS_AES_CCM_128
  1289. { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
  1290. # endif
  1291. # ifdef OPENSSL_KTLS_AES_GCM_256
  1292. { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
  1293. # endif
  1294. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1295. { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
  1296. # endif
  1297. # endif
  1298. };
  1299. #define NUM_KTLS_TEST_CIPHERS \
  1300. (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
  1301. static int test_ktls(int test)
  1302. {
  1303. struct ktls_test_cipher *cipher;
  1304. int cis_ktls, sis_ktls;
  1305. OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
  1306. cipher = &ktls_test_ciphers[test / 4];
  1307. cis_ktls = (test & 1) != 0;
  1308. sis_ktls = (test & 2) != 0;
  1309. return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
  1310. cipher->cipher);
  1311. }
  1312. static int test_ktls_sendfile(int test)
  1313. {
  1314. struct ktls_test_cipher *cipher;
  1315. int tst = test >> 1;
  1316. OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
  1317. cipher = &ktls_test_ciphers[tst];
  1318. return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher,
  1319. test & 1);
  1320. }
  1321. #endif
  1322. static int test_large_message_tls(void)
  1323. {
  1324. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1325. TLS1_VERSION, 0, 0);
  1326. }
  1327. static int test_large_message_tls_read_ahead(void)
  1328. {
  1329. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1330. TLS1_VERSION, 0, 1);
  1331. }
  1332. #ifndef OPENSSL_NO_DTLS
  1333. static int test_large_message_dtls(void)
  1334. {
  1335. # ifdef OPENSSL_NO_DTLS1_2
  1336. /* Not supported in the FIPS provider */
  1337. if (is_fips)
  1338. return 1;
  1339. # endif
  1340. /*
  1341. * read_ahead is not relevant to DTLS because DTLS always acts as if
  1342. * read_ahead is set.
  1343. */
  1344. return execute_test_large_message(DTLS_server_method(),
  1345. DTLS_client_method(),
  1346. DTLS1_VERSION, 0, 0);
  1347. }
  1348. #endif
  1349. /*
  1350. * Test we can successfully send the maximum amount of application data. We
  1351. * test each protocol version individually, each with and without EtM enabled.
  1352. * TLSv1.3 doesn't use EtM so technically it is redundant to test both but it is
  1353. * simpler this way. We also test all combinations with and without the
  1354. * SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS option which affects the size of the
  1355. * underlying buffer.
  1356. */
  1357. static int test_large_app_data(int tst)
  1358. {
  1359. SSL_CTX *cctx = NULL, *sctx = NULL;
  1360. SSL *clientssl = NULL, *serverssl = NULL;
  1361. int testresult = 0, prot;
  1362. unsigned char *msg, *buf = NULL;
  1363. size_t written, readbytes;
  1364. const SSL_METHOD *smeth = TLS_server_method();
  1365. const SSL_METHOD *cmeth = TLS_client_method();
  1366. switch (tst >> 2) {
  1367. case 0:
  1368. #ifndef OSSL_NO_USABLE_TLS1_3
  1369. prot = TLS1_3_VERSION;
  1370. break;
  1371. #else
  1372. return 1;
  1373. #endif
  1374. case 1:
  1375. #ifndef OPENSSL_NO_TLS1_2
  1376. prot = TLS1_2_VERSION;
  1377. break;
  1378. #else
  1379. return 1;
  1380. #endif
  1381. case 2:
  1382. #ifndef OPENSSL_NO_TLS1_1
  1383. prot = TLS1_1_VERSION;
  1384. break;
  1385. #else
  1386. return 1;
  1387. #endif
  1388. case 3:
  1389. #ifndef OPENSSL_NO_TLS1
  1390. prot = TLS1_VERSION;
  1391. break;
  1392. #else
  1393. return 1;
  1394. #endif
  1395. case 4:
  1396. #ifndef OPENSSL_NO_SSL3
  1397. prot = SSL3_VERSION;
  1398. break;
  1399. #else
  1400. return 1;
  1401. #endif
  1402. case 5:
  1403. #ifndef OPENSSL_NO_DTLS1_2
  1404. prot = DTLS1_2_VERSION;
  1405. smeth = DTLS_server_method();
  1406. cmeth = DTLS_client_method();
  1407. break;
  1408. #else
  1409. return 1;
  1410. #endif
  1411. case 6:
  1412. #ifndef OPENSSL_NO_DTLS1
  1413. prot = DTLS1_VERSION;
  1414. smeth = DTLS_server_method();
  1415. cmeth = DTLS_client_method();
  1416. break;
  1417. #else
  1418. return 1;
  1419. #endif
  1420. default:
  1421. /* Shouldn't happen */
  1422. return 0;
  1423. }
  1424. if ((prot < TLS1_2_VERSION || prot == DTLS1_VERSION) && is_fips)
  1425. return 1;
  1426. /* Maximal sized message of zeros */
  1427. msg = OPENSSL_zalloc(SSL3_RT_MAX_PLAIN_LENGTH);
  1428. if (!TEST_ptr(msg))
  1429. goto end;
  1430. buf = OPENSSL_malloc(SSL3_RT_MAX_PLAIN_LENGTH + 1);
  1431. if (!TEST_ptr(buf))
  1432. goto end;
  1433. /* Set whole buffer to all bits set */
  1434. memset(buf, 0xff, SSL3_RT_MAX_PLAIN_LENGTH + 1);
  1435. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, prot, prot,
  1436. &sctx, &cctx, cert, privkey)))
  1437. goto end;
  1438. if (prot < TLS1_2_VERSION || prot == DTLS1_VERSION) {
  1439. /* Older protocol versions need SECLEVEL=0 due to SHA1 usage */
  1440. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0"))
  1441. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  1442. "DEFAULT:@SECLEVEL=0")))
  1443. goto end;
  1444. }
  1445. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1446. &clientssl, NULL, NULL)))
  1447. goto end;
  1448. if ((tst & 1) != 0) {
  1449. /* Setting this option gives us a minimally sized underlying buffer */
  1450. if (!TEST_true(SSL_set_options(serverssl,
  1451. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS))
  1452. || !TEST_true(SSL_set_options(clientssl,
  1453. SSL_OP_DONT_INSERT_EMPTY_FRAGMENTS)))
  1454. goto end;
  1455. }
  1456. if ((tst & 2) != 0) {
  1457. /*
  1458. * Setting this option means the MAC is added before encryption
  1459. * giving us a larger record for the encryption process
  1460. */
  1461. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC))
  1462. || !TEST_true(SSL_set_options(clientssl,
  1463. SSL_OP_NO_ENCRYPT_THEN_MAC)))
  1464. goto end;
  1465. }
  1466. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  1467. goto end;
  1468. if (!TEST_true(SSL_write_ex(clientssl, msg, SSL3_RT_MAX_PLAIN_LENGTH,
  1469. &written))
  1470. || !TEST_size_t_eq(written, SSL3_RT_MAX_PLAIN_LENGTH))
  1471. goto end;
  1472. /* We provide a buffer slightly larger than what we are actually expecting */
  1473. if (!TEST_true(SSL_read_ex(serverssl, buf, SSL3_RT_MAX_PLAIN_LENGTH + 1,
  1474. &readbytes)))
  1475. goto end;
  1476. if (!TEST_mem_eq(msg, written, buf, readbytes))
  1477. goto end;
  1478. testresult = 1;
  1479. end:
  1480. OPENSSL_free(msg);
  1481. OPENSSL_free(buf);
  1482. SSL_free(serverssl);
  1483. SSL_free(clientssl);
  1484. SSL_CTX_free(sctx);
  1485. SSL_CTX_free(cctx);
  1486. return testresult;
  1487. }
  1488. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3) \
  1489. || !defined(OPENSSL_NO_DTLS)
  1490. static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
  1491. const SSL_METHOD *cmeth,
  1492. int min_version, int max_version)
  1493. {
  1494. size_t i;
  1495. SSL_CTX *cctx = NULL, *sctx = NULL;
  1496. SSL *clientssl = NULL, *serverssl = NULL;
  1497. int testresult = 0;
  1498. const unsigned char *zbuf;
  1499. SSL_CONNECTION *serversc;
  1500. TLS_RECORD *rr;
  1501. static unsigned char cbuf[16000];
  1502. static unsigned char sbuf[16000];
  1503. if (!TEST_true(create_ssl_ctx_pair(libctx,
  1504. smeth, cmeth,
  1505. min_version, max_version,
  1506. &sctx, &cctx, cert,
  1507. privkey)))
  1508. goto end;
  1509. # ifdef OPENSSL_NO_DTLS1_2
  1510. if (smeth == DTLS_server_method()) {
  1511. /* Not supported in the FIPS provider */
  1512. if (is_fips) {
  1513. testresult = 1;
  1514. goto end;
  1515. };
  1516. /*
  1517. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  1518. * level 0
  1519. */
  1520. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  1521. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  1522. "DEFAULT:@SECLEVEL=0")))
  1523. goto end;
  1524. }
  1525. # endif
  1526. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1527. NULL, NULL)))
  1528. goto end;
  1529. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
  1530. goto end;
  1531. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1532. SSL_ERROR_NONE)))
  1533. goto end;
  1534. for (i = 0; i < sizeof(cbuf); i++) {
  1535. cbuf[i] = i & 0xff;
  1536. }
  1537. if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
  1538. goto end;
  1539. if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1540. goto end;
  1541. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  1542. goto end;
  1543. /*
  1544. * Since we called SSL_peek(), we know the data in the record
  1545. * layer is a plaintext record. We can gather the pointer to check
  1546. * for zeroization after SSL_read().
  1547. */
  1548. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1549. goto end;
  1550. rr = serversc->rlayer.tlsrecs;
  1551. zbuf = &rr->data[rr->off];
  1552. if (!TEST_int_eq(rr->length, sizeof(cbuf)))
  1553. goto end;
  1554. /*
  1555. * After SSL_peek() the plaintext must still be stored in the
  1556. * record.
  1557. */
  1558. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1559. goto end;
  1560. memset(sbuf, 0, sizeof(sbuf));
  1561. if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1562. goto end;
  1563. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
  1564. goto end;
  1565. /* Check if rbuf is cleansed */
  1566. memset(cbuf, 0, sizeof(cbuf));
  1567. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1568. goto end;
  1569. testresult = 1;
  1570. end:
  1571. SSL_free(serverssl);
  1572. SSL_free(clientssl);
  1573. SSL_CTX_free(sctx);
  1574. SSL_CTX_free(cctx);
  1575. return testresult;
  1576. }
  1577. #endif /*
  1578. * !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  1579. * || !defined(OPENSSL_NO_DTLS)
  1580. */
  1581. static int test_cleanse_plaintext(void)
  1582. {
  1583. #if !defined(OPENSSL_NO_TLS1_2)
  1584. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1585. TLS_client_method(),
  1586. TLS1_2_VERSION,
  1587. TLS1_2_VERSION)))
  1588. return 0;
  1589. #endif
  1590. #if !defined(OSSL_NO_USABLE_TLS1_3)
  1591. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1592. TLS_client_method(),
  1593. TLS1_3_VERSION,
  1594. TLS1_3_VERSION)))
  1595. return 0;
  1596. #endif
  1597. #if !defined(OPENSSL_NO_DTLS)
  1598. if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
  1599. DTLS_client_method(),
  1600. DTLS1_VERSION,
  1601. 0)))
  1602. return 0;
  1603. #endif
  1604. return 1;
  1605. }
  1606. #ifndef OPENSSL_NO_OCSP
  1607. static int ocsp_server_cb(SSL *s, void *arg)
  1608. {
  1609. int *argi = (int *)arg;
  1610. unsigned char *copy = NULL;
  1611. STACK_OF(OCSP_RESPID) *ids = NULL;
  1612. OCSP_RESPID *id = NULL;
  1613. if (*argi == 2) {
  1614. /* In this test we are expecting exactly 1 OCSP_RESPID */
  1615. SSL_get_tlsext_status_ids(s, &ids);
  1616. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  1617. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1618. id = sk_OCSP_RESPID_value(ids, 0);
  1619. if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
  1620. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1621. } else if (*argi != 1) {
  1622. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1623. }
  1624. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  1625. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1626. if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
  1627. sizeof(orespder)))) {
  1628. OPENSSL_free(copy);
  1629. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1630. }
  1631. ocsp_server_called = 1;
  1632. return SSL_TLSEXT_ERR_OK;
  1633. }
  1634. static int ocsp_client_cb(SSL *s, void *arg)
  1635. {
  1636. int *argi = (int *)arg;
  1637. const unsigned char *respderin;
  1638. size_t len;
  1639. if (*argi != 1 && *argi != 2)
  1640. return 0;
  1641. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  1642. if (!TEST_mem_eq(orespder, len, respderin, len))
  1643. return 0;
  1644. ocsp_client_called = 1;
  1645. return 1;
  1646. }
  1647. static int test_tlsext_status_type(void)
  1648. {
  1649. SSL_CTX *cctx = NULL, *sctx = NULL;
  1650. SSL *clientssl = NULL, *serverssl = NULL;
  1651. int testresult = 0;
  1652. STACK_OF(OCSP_RESPID) *ids = NULL;
  1653. OCSP_RESPID *id = NULL;
  1654. BIO *certbio = NULL;
  1655. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  1656. TLS1_VERSION, 0,
  1657. &sctx, &cctx, cert, privkey))
  1658. return 0;
  1659. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  1660. goto end;
  1661. /* First just do various checks getting and setting tlsext_status_type */
  1662. clientssl = SSL_new(cctx);
  1663. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  1664. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  1665. TLSEXT_STATUSTYPE_ocsp))
  1666. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  1667. TLSEXT_STATUSTYPE_ocsp))
  1668. goto end;
  1669. SSL_free(clientssl);
  1670. clientssl = NULL;
  1671. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  1672. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  1673. goto end;
  1674. clientssl = SSL_new(cctx);
  1675. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  1676. goto end;
  1677. SSL_free(clientssl);
  1678. clientssl = NULL;
  1679. /*
  1680. * Now actually do a handshake and check OCSP information is exchanged and
  1681. * the callbacks get called
  1682. */
  1683. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  1684. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  1685. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  1686. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  1687. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1688. &clientssl, NULL, NULL))
  1689. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1690. SSL_ERROR_NONE))
  1691. || !TEST_true(ocsp_client_called)
  1692. || !TEST_true(ocsp_server_called))
  1693. goto end;
  1694. SSL_free(serverssl);
  1695. SSL_free(clientssl);
  1696. serverssl = NULL;
  1697. clientssl = NULL;
  1698. /* Try again but this time force the server side callback to fail */
  1699. ocsp_client_called = 0;
  1700. ocsp_server_called = 0;
  1701. cdummyarg = 0;
  1702. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1703. &clientssl, NULL, NULL))
  1704. /* This should fail because the callback will fail */
  1705. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  1706. SSL_ERROR_NONE))
  1707. || !TEST_false(ocsp_client_called)
  1708. || !TEST_false(ocsp_server_called))
  1709. goto end;
  1710. SSL_free(serverssl);
  1711. SSL_free(clientssl);
  1712. serverssl = NULL;
  1713. clientssl = NULL;
  1714. /*
  1715. * This time we'll get the client to send an OCSP_RESPID that it will
  1716. * accept.
  1717. */
  1718. ocsp_client_called = 0;
  1719. ocsp_server_called = 0;
  1720. cdummyarg = 2;
  1721. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1722. &clientssl, NULL, NULL)))
  1723. goto end;
  1724. /*
  1725. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  1726. * specific one. We'll use the server cert.
  1727. */
  1728. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  1729. || !TEST_ptr(id = OCSP_RESPID_new())
  1730. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  1731. || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
  1732. || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
  1733. || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
  1734. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  1735. goto end;
  1736. id = NULL;
  1737. SSL_set_tlsext_status_ids(clientssl, ids);
  1738. /* Control has been transferred */
  1739. ids = NULL;
  1740. BIO_free(certbio);
  1741. certbio = NULL;
  1742. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1743. SSL_ERROR_NONE))
  1744. || !TEST_true(ocsp_client_called)
  1745. || !TEST_true(ocsp_server_called))
  1746. goto end;
  1747. testresult = 1;
  1748. end:
  1749. SSL_free(serverssl);
  1750. SSL_free(clientssl);
  1751. SSL_CTX_free(sctx);
  1752. SSL_CTX_free(cctx);
  1753. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  1754. OCSP_RESPID_free(id);
  1755. BIO_free(certbio);
  1756. X509_free(ocspcert);
  1757. ocspcert = NULL;
  1758. return testresult;
  1759. }
  1760. #endif
  1761. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  1762. static int new_called, remove_called, get_called;
  1763. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  1764. {
  1765. new_called++;
  1766. /*
  1767. * sess has been up-refed for us, but we don't actually need it so free it
  1768. * immediately.
  1769. */
  1770. SSL_SESSION_free(sess);
  1771. return 1;
  1772. }
  1773. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  1774. {
  1775. remove_called++;
  1776. }
  1777. static SSL_SESSION *get_sess_val = NULL;
  1778. static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
  1779. int *copy)
  1780. {
  1781. get_called++;
  1782. *copy = 1;
  1783. return get_sess_val;
  1784. }
  1785. static int execute_test_session(int maxprot, int use_int_cache,
  1786. int use_ext_cache, long s_options)
  1787. {
  1788. SSL_CTX *sctx = NULL, *cctx = NULL;
  1789. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  1790. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  1791. # ifndef OPENSSL_NO_TLS1_1
  1792. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  1793. # endif
  1794. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  1795. int testresult = 0, numnewsesstick = 1;
  1796. new_called = remove_called = 0;
  1797. /* TLSv1.3 sends 2 NewSessionTickets */
  1798. if (maxprot == TLS1_3_VERSION)
  1799. numnewsesstick = 2;
  1800. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1801. TLS_client_method(), TLS1_VERSION, 0,
  1802. &sctx, &cctx, cert, privkey)))
  1803. return 0;
  1804. /*
  1805. * Only allow the max protocol version so we can force a connection failure
  1806. * later
  1807. */
  1808. SSL_CTX_set_min_proto_version(cctx, maxprot);
  1809. SSL_CTX_set_max_proto_version(cctx, maxprot);
  1810. /* Set up session cache */
  1811. if (use_ext_cache) {
  1812. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  1813. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  1814. }
  1815. if (use_int_cache) {
  1816. /* Also covers instance where both are set */
  1817. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  1818. } else {
  1819. SSL_CTX_set_session_cache_mode(cctx,
  1820. SSL_SESS_CACHE_CLIENT
  1821. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1822. }
  1823. if (s_options) {
  1824. SSL_CTX_set_options(sctx, s_options);
  1825. }
  1826. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1827. NULL, NULL))
  1828. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1829. SSL_ERROR_NONE))
  1830. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  1831. goto end;
  1832. /* Should fail because it should already be in the cache */
  1833. if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  1834. goto end;
  1835. if (use_ext_cache
  1836. && (!TEST_int_eq(new_called, numnewsesstick)
  1837. || !TEST_int_eq(remove_called, 0)))
  1838. goto end;
  1839. new_called = remove_called = 0;
  1840. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1841. &clientssl2, NULL, NULL))
  1842. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1843. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1844. SSL_ERROR_NONE))
  1845. || !TEST_true(SSL_session_reused(clientssl2)))
  1846. goto end;
  1847. if (maxprot == TLS1_3_VERSION) {
  1848. /*
  1849. * In TLSv1.3 we should have created a new session even though we have
  1850. * resumed. Since we attempted a resume we should also have removed the
  1851. * old ticket from the cache so that we try to only use tickets once.
  1852. */
  1853. if (use_ext_cache
  1854. && (!TEST_int_eq(new_called, 1)
  1855. || !TEST_int_eq(remove_called, 1)))
  1856. goto end;
  1857. } else {
  1858. /*
  1859. * In TLSv1.2 we expect to have resumed so no sessions added or
  1860. * removed.
  1861. */
  1862. if (use_ext_cache
  1863. && (!TEST_int_eq(new_called, 0)
  1864. || !TEST_int_eq(remove_called, 0)))
  1865. goto end;
  1866. }
  1867. SSL_SESSION_free(sess1);
  1868. if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
  1869. goto end;
  1870. shutdown_ssl_connection(serverssl2, clientssl2);
  1871. serverssl2 = clientssl2 = NULL;
  1872. new_called = remove_called = 0;
  1873. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1874. &clientssl2, NULL, NULL))
  1875. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1876. SSL_ERROR_NONE)))
  1877. goto end;
  1878. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  1879. goto end;
  1880. if (use_ext_cache
  1881. && (!TEST_int_eq(new_called, numnewsesstick)
  1882. || !TEST_int_eq(remove_called, 0)))
  1883. goto end;
  1884. new_called = remove_called = 0;
  1885. /*
  1886. * This should clear sess2 from the cache because it is a "bad" session.
  1887. * See SSL_set_session() documentation.
  1888. */
  1889. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  1890. goto end;
  1891. if (use_ext_cache
  1892. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1893. goto end;
  1894. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  1895. goto end;
  1896. if (use_int_cache) {
  1897. /* Should succeeded because it should not already be in the cache */
  1898. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  1899. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  1900. goto end;
  1901. }
  1902. new_called = remove_called = 0;
  1903. /* This shouldn't be in the cache so should fail */
  1904. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  1905. goto end;
  1906. if (use_ext_cache
  1907. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1908. goto end;
  1909. # if !defined(OPENSSL_NO_TLS1_1)
  1910. new_called = remove_called = 0;
  1911. /* Force a connection failure */
  1912. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  1913. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  1914. &clientssl3, NULL, NULL))
  1915. || !TEST_true(SSL_set_session(clientssl3, sess1))
  1916. /* This should fail because of the mismatched protocol versions */
  1917. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  1918. SSL_ERROR_NONE)))
  1919. goto end;
  1920. /* We should have automatically removed the session from the cache */
  1921. if (use_ext_cache
  1922. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1923. goto end;
  1924. /* Should succeed because it should not already be in the cache */
  1925. if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
  1926. goto end;
  1927. # endif
  1928. /* Now do some tests for server side caching */
  1929. if (use_ext_cache) {
  1930. SSL_CTX_sess_set_new_cb(cctx, NULL);
  1931. SSL_CTX_sess_set_remove_cb(cctx, NULL);
  1932. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  1933. SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
  1934. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  1935. get_sess_val = NULL;
  1936. }
  1937. SSL_CTX_set_session_cache_mode(cctx, 0);
  1938. /* Internal caching is the default on the server side */
  1939. if (!use_int_cache)
  1940. SSL_CTX_set_session_cache_mode(sctx,
  1941. SSL_SESS_CACHE_SERVER
  1942. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1943. SSL_free(serverssl1);
  1944. SSL_free(clientssl1);
  1945. serverssl1 = clientssl1 = NULL;
  1946. SSL_free(serverssl2);
  1947. SSL_free(clientssl2);
  1948. serverssl2 = clientssl2 = NULL;
  1949. SSL_SESSION_free(sess1);
  1950. sess1 = NULL;
  1951. SSL_SESSION_free(sess2);
  1952. sess2 = NULL;
  1953. SSL_CTX_set_max_proto_version(sctx, maxprot);
  1954. if (maxprot == TLS1_2_VERSION)
  1955. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  1956. new_called = remove_called = get_called = 0;
  1957. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1958. NULL, NULL))
  1959. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1960. SSL_ERROR_NONE))
  1961. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
  1962. || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
  1963. goto end;
  1964. if (use_int_cache) {
  1965. if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
  1966. /*
  1967. * In TLSv1.3 it should not have been added to the internal cache,
  1968. * except in the case where we also have an external cache (in that
  1969. * case it gets added to the cache in order to generate remove
  1970. * events after timeout).
  1971. */
  1972. if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
  1973. goto end;
  1974. } else {
  1975. /* Should fail because it should already be in the cache */
  1976. if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
  1977. goto end;
  1978. }
  1979. }
  1980. if (use_ext_cache) {
  1981. SSL_SESSION *tmp = sess2;
  1982. if (!TEST_int_eq(new_called, numnewsesstick)
  1983. || !TEST_int_eq(remove_called, 0)
  1984. || !TEST_int_eq(get_called, 0))
  1985. goto end;
  1986. /*
  1987. * Delete the session from the internal cache to force a lookup from
  1988. * the external cache. We take a copy first because
  1989. * SSL_CTX_remove_session() also marks the session as non-resumable.
  1990. */
  1991. if (use_int_cache && maxprot != TLS1_3_VERSION) {
  1992. if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
  1993. || !TEST_true(sess2->owner != NULL)
  1994. || !TEST_true(tmp->owner == NULL)
  1995. || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
  1996. goto end;
  1997. SSL_SESSION_free(sess2);
  1998. }
  1999. sess2 = tmp;
  2000. }
  2001. new_called = remove_called = get_called = 0;
  2002. get_sess_val = sess2;
  2003. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  2004. &clientssl2, NULL, NULL))
  2005. || !TEST_true(SSL_set_session(clientssl2, sess1))
  2006. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  2007. SSL_ERROR_NONE))
  2008. || !TEST_true(SSL_session_reused(clientssl2)))
  2009. goto end;
  2010. if (use_ext_cache) {
  2011. if (!TEST_int_eq(remove_called, 0))
  2012. goto end;
  2013. if (maxprot == TLS1_3_VERSION) {
  2014. if (!TEST_int_eq(new_called, 1)
  2015. || !TEST_int_eq(get_called, 0))
  2016. goto end;
  2017. } else {
  2018. if (!TEST_int_eq(new_called, 0)
  2019. || !TEST_int_eq(get_called, 1))
  2020. goto end;
  2021. }
  2022. }
  2023. /*
  2024. * Make a small cache, force out all other sessions but
  2025. * sess2, try to add sess1, which should succeed. Then
  2026. * make sure it's there by checking the owners. Despite
  2027. * the timeouts, sess1 should have kicked out sess2
  2028. */
  2029. /* Make sess1 expire before sess2 */
  2030. if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
  2031. || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
  2032. || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
  2033. || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
  2034. goto end;
  2035. if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
  2036. goto end;
  2037. /* Don't care about results - cache should only be sess2 at end */
  2038. SSL_CTX_add_session(sctx, sess1);
  2039. SSL_CTX_add_session(sctx, sess2);
  2040. /* Now add sess1, and make sure it remains, despite timeout */
  2041. if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
  2042. || !TEST_ptr(sess1->owner)
  2043. || !TEST_ptr_null(sess2->owner))
  2044. goto end;
  2045. testresult = 1;
  2046. end:
  2047. SSL_free(serverssl1);
  2048. SSL_free(clientssl1);
  2049. SSL_free(serverssl2);
  2050. SSL_free(clientssl2);
  2051. # ifndef OPENSSL_NO_TLS1_1
  2052. SSL_free(serverssl3);
  2053. SSL_free(clientssl3);
  2054. # endif
  2055. SSL_SESSION_free(sess1);
  2056. SSL_SESSION_free(sess2);
  2057. SSL_CTX_free(sctx);
  2058. SSL_CTX_free(cctx);
  2059. return testresult;
  2060. }
  2061. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  2062. static int test_session_with_only_int_cache(void)
  2063. {
  2064. #ifndef OSSL_NO_USABLE_TLS1_3
  2065. if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
  2066. return 0;
  2067. #endif
  2068. #ifndef OPENSSL_NO_TLS1_2
  2069. return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
  2070. #else
  2071. return 1;
  2072. #endif
  2073. }
  2074. static int test_session_with_only_ext_cache(void)
  2075. {
  2076. #ifndef OSSL_NO_USABLE_TLS1_3
  2077. if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
  2078. return 0;
  2079. #endif
  2080. #ifndef OPENSSL_NO_TLS1_2
  2081. return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
  2082. #else
  2083. return 1;
  2084. #endif
  2085. }
  2086. static int test_session_with_both_cache(void)
  2087. {
  2088. #ifndef OSSL_NO_USABLE_TLS1_3
  2089. if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
  2090. return 0;
  2091. #endif
  2092. #ifndef OPENSSL_NO_TLS1_2
  2093. return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
  2094. #else
  2095. return 1;
  2096. #endif
  2097. }
  2098. static int test_session_wo_ca_names(void)
  2099. {
  2100. #ifndef OSSL_NO_USABLE_TLS1_3
  2101. if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
  2102. return 0;
  2103. #endif
  2104. #ifndef OPENSSL_NO_TLS1_2
  2105. return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
  2106. #else
  2107. return 1;
  2108. #endif
  2109. }
  2110. #ifndef OSSL_NO_USABLE_TLS1_3
  2111. static SSL_SESSION *sesscache[6];
  2112. static int do_cache;
  2113. static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
  2114. {
  2115. if (do_cache) {
  2116. sesscache[new_called] = sess;
  2117. } else {
  2118. /* We don't need the reference to the session, so free it */
  2119. SSL_SESSION_free(sess);
  2120. }
  2121. new_called++;
  2122. return 1;
  2123. }
  2124. static int post_handshake_verify(SSL *sssl, SSL *cssl)
  2125. {
  2126. SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
  2127. if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
  2128. return 0;
  2129. /* Start handshake on the server and client */
  2130. if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
  2131. || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
  2132. || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
  2133. || !TEST_true(create_ssl_connection(sssl, cssl,
  2134. SSL_ERROR_NONE)))
  2135. return 0;
  2136. return 1;
  2137. }
  2138. static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
  2139. SSL_CTX **cctx)
  2140. {
  2141. int sess_id_ctx = 1;
  2142. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2143. TLS_client_method(), TLS1_VERSION, 0,
  2144. sctx, cctx, cert, privkey))
  2145. || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
  2146. || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
  2147. (void *)&sess_id_ctx,
  2148. sizeof(sess_id_ctx))))
  2149. return 0;
  2150. if (stateful)
  2151. SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
  2152. SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
  2153. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2154. SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
  2155. return 1;
  2156. }
  2157. static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
  2158. {
  2159. SSL *serverssl = NULL, *clientssl = NULL;
  2160. int i;
  2161. /* Test that we can resume with all the tickets we got given */
  2162. for (i = 0; i < idx * 2; i++) {
  2163. new_called = 0;
  2164. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2165. &clientssl, NULL, NULL))
  2166. || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
  2167. goto end;
  2168. SSL_set_post_handshake_auth(clientssl, 1);
  2169. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2170. SSL_ERROR_NONE)))
  2171. goto end;
  2172. /*
  2173. * Following a successful resumption we only get 1 ticket. After a
  2174. * failed one we should get idx tickets.
  2175. */
  2176. if (succ) {
  2177. if (!TEST_true(SSL_session_reused(clientssl))
  2178. || !TEST_int_eq(new_called, 1))
  2179. goto end;
  2180. } else {
  2181. if (!TEST_false(SSL_session_reused(clientssl))
  2182. || !TEST_int_eq(new_called, idx))
  2183. goto end;
  2184. }
  2185. new_called = 0;
  2186. /* After a post-handshake authentication we should get 1 new ticket */
  2187. if (succ
  2188. && (!post_handshake_verify(serverssl, clientssl)
  2189. || !TEST_int_eq(new_called, 1)))
  2190. goto end;
  2191. SSL_shutdown(clientssl);
  2192. SSL_shutdown(serverssl);
  2193. SSL_free(serverssl);
  2194. SSL_free(clientssl);
  2195. serverssl = clientssl = NULL;
  2196. SSL_SESSION_free(sesscache[i]);
  2197. sesscache[i] = NULL;
  2198. }
  2199. return 1;
  2200. end:
  2201. SSL_free(clientssl);
  2202. SSL_free(serverssl);
  2203. return 0;
  2204. }
  2205. static int test_tickets(int stateful, int idx)
  2206. {
  2207. SSL_CTX *sctx = NULL, *cctx = NULL;
  2208. SSL *serverssl = NULL, *clientssl = NULL;
  2209. int testresult = 0;
  2210. size_t j;
  2211. /* idx is the test number, but also the number of tickets we want */
  2212. new_called = 0;
  2213. do_cache = 1;
  2214. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2215. goto end;
  2216. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2217. &clientssl, NULL, NULL)))
  2218. goto end;
  2219. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2220. SSL_ERROR_NONE))
  2221. /* Check we got the number of tickets we were expecting */
  2222. || !TEST_int_eq(idx, new_called))
  2223. goto end;
  2224. SSL_shutdown(clientssl);
  2225. SSL_shutdown(serverssl);
  2226. SSL_free(serverssl);
  2227. SSL_free(clientssl);
  2228. SSL_CTX_free(sctx);
  2229. SSL_CTX_free(cctx);
  2230. clientssl = serverssl = NULL;
  2231. sctx = cctx = NULL;
  2232. /*
  2233. * Now we try to resume with the tickets we previously created. The
  2234. * resumption attempt is expected to fail (because we're now using a new
  2235. * SSL_CTX). We should see idx number of tickets issued again.
  2236. */
  2237. /* Stop caching sessions - just count them */
  2238. do_cache = 0;
  2239. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2240. goto end;
  2241. if (!check_resumption(idx, sctx, cctx, 0))
  2242. goto end;
  2243. /* Start again with caching sessions */
  2244. new_called = 0;
  2245. do_cache = 1;
  2246. SSL_CTX_free(sctx);
  2247. SSL_CTX_free(cctx);
  2248. sctx = cctx = NULL;
  2249. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2250. goto end;
  2251. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2252. &clientssl, NULL, NULL)))
  2253. goto end;
  2254. SSL_set_post_handshake_auth(clientssl, 1);
  2255. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2256. SSL_ERROR_NONE))
  2257. /* Check we got the number of tickets we were expecting */
  2258. || !TEST_int_eq(idx, new_called))
  2259. goto end;
  2260. /* After a post-handshake authentication we should get new tickets issued */
  2261. if (!post_handshake_verify(serverssl, clientssl)
  2262. || !TEST_int_eq(idx * 2, new_called))
  2263. goto end;
  2264. SSL_shutdown(clientssl);
  2265. SSL_shutdown(serverssl);
  2266. SSL_free(serverssl);
  2267. SSL_free(clientssl);
  2268. serverssl = clientssl = NULL;
  2269. /* Stop caching sessions - just count them */
  2270. do_cache = 0;
  2271. /*
  2272. * Check we can resume with all the tickets we created. This time around the
  2273. * resumptions should all be successful.
  2274. */
  2275. if (!check_resumption(idx, sctx, cctx, 1))
  2276. goto end;
  2277. testresult = 1;
  2278. end:
  2279. SSL_free(serverssl);
  2280. SSL_free(clientssl);
  2281. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  2282. SSL_SESSION_free(sesscache[j]);
  2283. sesscache[j] = NULL;
  2284. }
  2285. SSL_CTX_free(sctx);
  2286. SSL_CTX_free(cctx);
  2287. return testresult;
  2288. }
  2289. static int test_stateless_tickets(int idx)
  2290. {
  2291. return test_tickets(0, idx);
  2292. }
  2293. static int test_stateful_tickets(int idx)
  2294. {
  2295. return test_tickets(1, idx);
  2296. }
  2297. static int test_psk_tickets(void)
  2298. {
  2299. SSL_CTX *sctx = NULL, *cctx = NULL;
  2300. SSL *serverssl = NULL, *clientssl = NULL;
  2301. int testresult = 0;
  2302. int sess_id_ctx = 1;
  2303. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2304. TLS_client_method(), TLS1_VERSION, 0,
  2305. &sctx, &cctx, NULL, NULL))
  2306. || !TEST_true(SSL_CTX_set_session_id_context(sctx,
  2307. (void *)&sess_id_ctx,
  2308. sizeof(sess_id_ctx))))
  2309. goto end;
  2310. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  2311. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2312. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  2313. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  2314. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2315. use_session_cb_cnt = 0;
  2316. find_session_cb_cnt = 0;
  2317. srvid = pskid;
  2318. new_called = 0;
  2319. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2320. NULL, NULL)))
  2321. goto end;
  2322. clientpsk = serverpsk = create_a_psk(clientssl, SHA384_DIGEST_LENGTH);
  2323. if (!TEST_ptr(clientpsk))
  2324. goto end;
  2325. SSL_SESSION_up_ref(clientpsk);
  2326. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2327. SSL_ERROR_NONE))
  2328. || !TEST_int_eq(1, find_session_cb_cnt)
  2329. || !TEST_int_eq(1, use_session_cb_cnt)
  2330. /* We should always get 1 ticket when using external PSK */
  2331. || !TEST_int_eq(1, new_called))
  2332. goto end;
  2333. testresult = 1;
  2334. end:
  2335. SSL_free(serverssl);
  2336. SSL_free(clientssl);
  2337. SSL_CTX_free(sctx);
  2338. SSL_CTX_free(cctx);
  2339. SSL_SESSION_free(clientpsk);
  2340. SSL_SESSION_free(serverpsk);
  2341. clientpsk = serverpsk = NULL;
  2342. return testresult;
  2343. }
  2344. static int test_extra_tickets(int idx)
  2345. {
  2346. SSL_CTX *sctx = NULL, *cctx = NULL;
  2347. SSL *serverssl = NULL, *clientssl = NULL;
  2348. BIO *bretry = BIO_new(bio_s_always_retry());
  2349. BIO *tmp = NULL;
  2350. int testresult = 0;
  2351. int stateful = 0;
  2352. size_t nbytes;
  2353. unsigned char c, buf[1];
  2354. new_called = 0;
  2355. do_cache = 1;
  2356. if (idx >= 3) {
  2357. idx -= 3;
  2358. stateful = 1;
  2359. }
  2360. if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
  2361. goto end;
  2362. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  2363. /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
  2364. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2365. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2366. &clientssl, NULL, NULL)))
  2367. goto end;
  2368. /*
  2369. * Note that we have new_session_cb on both sctx and cctx, so new_called is
  2370. * incremented by both client and server.
  2371. */
  2372. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2373. SSL_ERROR_NONE))
  2374. /* Check we got the number of tickets we were expecting */
  2375. || !TEST_int_eq(idx * 2, new_called)
  2376. || !TEST_true(SSL_new_session_ticket(serverssl))
  2377. || !TEST_true(SSL_new_session_ticket(serverssl))
  2378. || !TEST_int_eq(idx * 2, new_called))
  2379. goto end;
  2380. /* Now try a (real) write to actually send the tickets */
  2381. c = '1';
  2382. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2383. || !TEST_size_t_eq(1, nbytes)
  2384. || !TEST_int_eq(idx * 2 + 2, new_called)
  2385. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2386. || !TEST_int_eq(idx * 2 + 4, new_called)
  2387. || !TEST_int_eq(sizeof(buf), nbytes)
  2388. || !TEST_int_eq(c, buf[0])
  2389. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2390. goto end;
  2391. /* Try with only requesting one new ticket, too */
  2392. c = '2';
  2393. new_called = 0;
  2394. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2395. || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
  2396. || !TEST_size_t_eq(sizeof(c), nbytes)
  2397. || !TEST_int_eq(1, new_called)
  2398. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2399. || !TEST_int_eq(2, new_called)
  2400. || !TEST_size_t_eq(sizeof(buf), nbytes)
  2401. || !TEST_int_eq(c, buf[0]))
  2402. goto end;
  2403. /* Do it again but use dummy writes to drive the ticket generation */
  2404. c = '3';
  2405. new_called = 0;
  2406. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2407. || !TEST_true(SSL_new_session_ticket(serverssl))
  2408. || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
  2409. || !TEST_size_t_eq(0, nbytes)
  2410. || !TEST_int_eq(2, new_called)
  2411. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2412. || !TEST_int_eq(4, new_called))
  2413. goto end;
  2414. /* Once more, but with SSL_do_handshake() to drive the ticket generation */
  2415. c = '4';
  2416. new_called = 0;
  2417. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2418. || !TEST_true(SSL_new_session_ticket(serverssl))
  2419. || !TEST_true(SSL_do_handshake(serverssl))
  2420. || !TEST_int_eq(2, new_called)
  2421. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2422. || !TEST_int_eq(4, new_called))
  2423. goto end;
  2424. /*
  2425. * Use the always-retry BIO to exercise the logic that forces ticket
  2426. * generation to wait until a record boundary.
  2427. */
  2428. c = '5';
  2429. new_called = 0;
  2430. tmp = SSL_get_wbio(serverssl);
  2431. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  2432. tmp = NULL;
  2433. goto end;
  2434. }
  2435. SSL_set0_wbio(serverssl, bretry);
  2436. bretry = NULL;
  2437. if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2438. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
  2439. || !TEST_size_t_eq(nbytes, 0))
  2440. goto end;
  2441. /* Restore a BIO that will let the write succeed */
  2442. SSL_set0_wbio(serverssl, tmp);
  2443. tmp = NULL;
  2444. /*
  2445. * These calls should just queue the request and not send anything
  2446. * even if we explicitly try to hit the state machine.
  2447. */
  2448. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2449. || !TEST_true(SSL_new_session_ticket(serverssl))
  2450. || !TEST_int_eq(0, new_called)
  2451. || !TEST_true(SSL_do_handshake(serverssl))
  2452. || !TEST_int_eq(0, new_called))
  2453. goto end;
  2454. /* Re-do the write; still no tickets sent */
  2455. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2456. || !TEST_size_t_eq(1, nbytes)
  2457. || !TEST_int_eq(0, new_called)
  2458. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2459. || !TEST_int_eq(0, new_called)
  2460. || !TEST_int_eq(sizeof(buf), nbytes)
  2461. || !TEST_int_eq(c, buf[0])
  2462. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2463. goto end;
  2464. /* Even trying to hit the state machine now will still not send tickets */
  2465. if (!TEST_true(SSL_do_handshake(serverssl))
  2466. || !TEST_int_eq(0, new_called))
  2467. goto end;
  2468. /* Now the *next* write should send the tickets */
  2469. c = '6';
  2470. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2471. || !TEST_size_t_eq(1, nbytes)
  2472. || !TEST_int_eq(2, new_called)
  2473. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2474. || !TEST_int_eq(4, new_called)
  2475. || !TEST_int_eq(sizeof(buf), nbytes)
  2476. || !TEST_int_eq(c, buf[0])
  2477. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2478. goto end;
  2479. SSL_shutdown(clientssl);
  2480. SSL_shutdown(serverssl);
  2481. testresult = 1;
  2482. end:
  2483. BIO_free(bretry);
  2484. BIO_free(tmp);
  2485. SSL_free(serverssl);
  2486. SSL_free(clientssl);
  2487. SSL_CTX_free(sctx);
  2488. SSL_CTX_free(cctx);
  2489. clientssl = serverssl = NULL;
  2490. sctx = cctx = NULL;
  2491. return testresult;
  2492. }
  2493. #endif
  2494. #define USE_NULL 0
  2495. #define USE_BIO_1 1
  2496. #define USE_BIO_2 2
  2497. #define USE_DEFAULT 3
  2498. #define CONNTYPE_CONNECTION_SUCCESS 0
  2499. #define CONNTYPE_CONNECTION_FAIL 1
  2500. #define CONNTYPE_NO_CONNECTION 2
  2501. #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  2502. #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
  2503. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
  2504. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
  2505. #else
  2506. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
  2507. #endif
  2508. #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
  2509. + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
  2510. + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
  2511. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  2512. {
  2513. switch (type) {
  2514. case USE_NULL:
  2515. *res = NULL;
  2516. break;
  2517. case USE_BIO_1:
  2518. *res = bio1;
  2519. break;
  2520. case USE_BIO_2:
  2521. *res = bio2;
  2522. break;
  2523. }
  2524. }
  2525. /*
  2526. * Tests calls to SSL_set_bio() under various conditions.
  2527. *
  2528. * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
  2529. * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
  2530. * then do more tests where we create a successful connection first using our
  2531. * standard connection setup functions, and then call SSL_set_bio() with
  2532. * various combinations of valid BIOs or NULL. We then repeat these tests
  2533. * following a failed connection. In this last case we are looking to check that
  2534. * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
  2535. */
  2536. static int test_ssl_set_bio(int idx)
  2537. {
  2538. SSL_CTX *sctx = NULL, *cctx = NULL;
  2539. BIO *bio1 = NULL;
  2540. BIO *bio2 = NULL;
  2541. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  2542. SSL *serverssl = NULL, *clientssl = NULL;
  2543. int initrbio, initwbio, newrbio, newwbio, conntype;
  2544. int testresult = 0;
  2545. if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
  2546. initrbio = idx % 3;
  2547. idx /= 3;
  2548. initwbio = idx % 3;
  2549. idx /= 3;
  2550. newrbio = idx % 3;
  2551. idx /= 3;
  2552. newwbio = idx % 3;
  2553. conntype = CONNTYPE_NO_CONNECTION;
  2554. } else {
  2555. idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
  2556. initrbio = initwbio = USE_DEFAULT;
  2557. newrbio = idx % 2;
  2558. idx /= 2;
  2559. newwbio = idx % 2;
  2560. idx /= 2;
  2561. conntype = idx % 2;
  2562. }
  2563. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2564. TLS_client_method(), TLS1_VERSION, 0,
  2565. &sctx, &cctx, cert, privkey)))
  2566. goto end;
  2567. if (conntype == CONNTYPE_CONNECTION_FAIL) {
  2568. /*
  2569. * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
  2570. * because we reduced the number of tests in the definition of
  2571. * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
  2572. * mismatched protocol versions we will force a connection failure.
  2573. */
  2574. SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
  2575. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2576. }
  2577. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2578. NULL, NULL)))
  2579. goto end;
  2580. if (initrbio == USE_BIO_1
  2581. || initwbio == USE_BIO_1
  2582. || newrbio == USE_BIO_1
  2583. || newwbio == USE_BIO_1) {
  2584. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  2585. goto end;
  2586. }
  2587. if (initrbio == USE_BIO_2
  2588. || initwbio == USE_BIO_2
  2589. || newrbio == USE_BIO_2
  2590. || newwbio == USE_BIO_2) {
  2591. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  2592. goto end;
  2593. }
  2594. if (initrbio != USE_DEFAULT) {
  2595. setupbio(&irbio, bio1, bio2, initrbio);
  2596. setupbio(&iwbio, bio1, bio2, initwbio);
  2597. SSL_set_bio(clientssl, irbio, iwbio);
  2598. /*
  2599. * We want to maintain our own refs to these BIO, so do an up ref for
  2600. * each BIO that will have ownership transferred in the SSL_set_bio()
  2601. * call
  2602. */
  2603. if (irbio != NULL)
  2604. BIO_up_ref(irbio);
  2605. if (iwbio != NULL && iwbio != irbio)
  2606. BIO_up_ref(iwbio);
  2607. }
  2608. if (conntype != CONNTYPE_NO_CONNECTION
  2609. && !TEST_true(create_ssl_connection(serverssl, clientssl,
  2610. SSL_ERROR_NONE)
  2611. == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
  2612. goto end;
  2613. setupbio(&nrbio, bio1, bio2, newrbio);
  2614. setupbio(&nwbio, bio1, bio2, newwbio);
  2615. /*
  2616. * We will (maybe) transfer ownership again so do more up refs.
  2617. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  2618. * already been set!
  2619. */
  2620. if (nrbio != NULL
  2621. && nrbio != irbio
  2622. && (nwbio != iwbio || nrbio != nwbio))
  2623. BIO_up_ref(nrbio);
  2624. if (nwbio != NULL
  2625. && nwbio != nrbio
  2626. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  2627. BIO_up_ref(nwbio);
  2628. SSL_set_bio(clientssl, nrbio, nwbio);
  2629. testresult = 1;
  2630. end:
  2631. BIO_free(bio1);
  2632. BIO_free(bio2);
  2633. /*
  2634. * This test is checking that the ref counting for SSL_set_bio is correct.
  2635. * If we get here and we did too many frees then we will fail in the above
  2636. * functions.
  2637. */
  2638. SSL_free(serverssl);
  2639. SSL_free(clientssl);
  2640. SSL_CTX_free(sctx);
  2641. SSL_CTX_free(cctx);
  2642. return testresult;
  2643. }
  2644. typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
  2645. static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
  2646. {
  2647. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  2648. SSL_CTX *ctx;
  2649. SSL *ssl = NULL;
  2650. int testresult = 0;
  2651. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  2652. || !TEST_ptr(ssl = SSL_new(ctx))
  2653. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  2654. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  2655. goto end;
  2656. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  2657. /*
  2658. * If anything goes wrong here then we could leak memory.
  2659. */
  2660. BIO_push(sslbio, membio1);
  2661. /* Verify changing the rbio/wbio directly does not cause leaks */
  2662. if (change_bio != NO_BIO_CHANGE) {
  2663. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
  2664. ssl = NULL;
  2665. goto end;
  2666. }
  2667. if (change_bio == CHANGE_RBIO)
  2668. SSL_set0_rbio(ssl, membio2);
  2669. else
  2670. SSL_set0_wbio(ssl, membio2);
  2671. }
  2672. ssl = NULL;
  2673. if (pop_ssl)
  2674. BIO_pop(sslbio);
  2675. else
  2676. BIO_pop(membio1);
  2677. testresult = 1;
  2678. end:
  2679. BIO_free(membio1);
  2680. BIO_free(sslbio);
  2681. SSL_free(ssl);
  2682. SSL_CTX_free(ctx);
  2683. return testresult;
  2684. }
  2685. static int test_ssl_bio_pop_next_bio(void)
  2686. {
  2687. return execute_test_ssl_bio(0, NO_BIO_CHANGE);
  2688. }
  2689. static int test_ssl_bio_pop_ssl_bio(void)
  2690. {
  2691. return execute_test_ssl_bio(1, NO_BIO_CHANGE);
  2692. }
  2693. static int test_ssl_bio_change_rbio(void)
  2694. {
  2695. return execute_test_ssl_bio(0, CHANGE_RBIO);
  2696. }
  2697. static int test_ssl_bio_change_wbio(void)
  2698. {
  2699. return execute_test_ssl_bio(0, CHANGE_WBIO);
  2700. }
  2701. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  2702. typedef struct {
  2703. /* The list of sig algs */
  2704. const int *list;
  2705. /* The length of the list */
  2706. size_t listlen;
  2707. /* A sigalgs list in string format */
  2708. const char *liststr;
  2709. /* Whether setting the list should succeed */
  2710. int valid;
  2711. /* Whether creating a connection with the list should succeed */
  2712. int connsuccess;
  2713. } sigalgs_list;
  2714. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  2715. # ifndef OPENSSL_NO_EC
  2716. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  2717. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  2718. # endif
  2719. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  2720. static const int invalidlist2[] = {NID_sha256, NID_undef};
  2721. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  2722. static const int invalidlist4[] = {NID_sha256};
  2723. static const sigalgs_list testsigalgs[] = {
  2724. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  2725. # ifndef OPENSSL_NO_EC
  2726. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  2727. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  2728. # endif
  2729. {NULL, 0, "RSA+SHA256", 1, 1},
  2730. {NULL, 0, "RSA+SHA256:?Invalid", 1, 1},
  2731. # ifndef OPENSSL_NO_EC
  2732. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  2733. {NULL, 0, "ECDSA+SHA512", 1, 0},
  2734. # endif
  2735. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  2736. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  2737. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  2738. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  2739. {NULL, 0, "RSA", 0, 0},
  2740. {NULL, 0, "SHA256", 0, 0},
  2741. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  2742. {NULL, 0, "Invalid", 0, 0}
  2743. };
  2744. static int test_set_sigalgs(int idx)
  2745. {
  2746. SSL_CTX *cctx = NULL, *sctx = NULL;
  2747. SSL *clientssl = NULL, *serverssl = NULL;
  2748. int testresult = 0;
  2749. const sigalgs_list *curr;
  2750. int testctx;
  2751. /* Should never happen */
  2752. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  2753. return 0;
  2754. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  2755. curr = testctx ? &testsigalgs[idx]
  2756. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  2757. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2758. TLS_client_method(), TLS1_VERSION, 0,
  2759. &sctx, &cctx, cert, privkey)))
  2760. return 0;
  2761. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2762. if (testctx) {
  2763. int ret;
  2764. if (curr->list != NULL)
  2765. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  2766. else
  2767. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  2768. if (!ret) {
  2769. if (curr->valid)
  2770. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  2771. else
  2772. testresult = 1;
  2773. goto end;
  2774. }
  2775. if (!curr->valid) {
  2776. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  2777. goto end;
  2778. }
  2779. }
  2780. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2781. &clientssl, NULL, NULL)))
  2782. goto end;
  2783. if (!testctx) {
  2784. int ret;
  2785. if (curr->list != NULL)
  2786. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  2787. else
  2788. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  2789. if (!ret) {
  2790. if (curr->valid)
  2791. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  2792. else
  2793. testresult = 1;
  2794. goto end;
  2795. }
  2796. if (!curr->valid)
  2797. goto end;
  2798. }
  2799. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  2800. SSL_ERROR_NONE),
  2801. curr->connsuccess))
  2802. goto end;
  2803. testresult = 1;
  2804. end:
  2805. SSL_free(serverssl);
  2806. SSL_free(clientssl);
  2807. SSL_CTX_free(sctx);
  2808. SSL_CTX_free(cctx);
  2809. return testresult;
  2810. }
  2811. #endif
  2812. #ifndef OSSL_NO_USABLE_TLS1_3
  2813. static int psk_client_cb_cnt = 0;
  2814. static int psk_server_cb_cnt = 0;
  2815. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  2816. size_t *idlen, SSL_SESSION **sess)
  2817. {
  2818. switch (++use_session_cb_cnt) {
  2819. case 1:
  2820. /* The first call should always have a NULL md */
  2821. if (md != NULL)
  2822. return 0;
  2823. break;
  2824. case 2:
  2825. /* The second call should always have an md */
  2826. if (md == NULL)
  2827. return 0;
  2828. break;
  2829. default:
  2830. /* We should only be called a maximum of twice */
  2831. return 0;
  2832. }
  2833. if (clientpsk != NULL)
  2834. SSL_SESSION_up_ref(clientpsk);
  2835. *sess = clientpsk;
  2836. *id = (const unsigned char *)pskid;
  2837. *idlen = strlen(pskid);
  2838. return 1;
  2839. }
  2840. #ifndef OPENSSL_NO_PSK
  2841. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
  2842. unsigned int max_id_len,
  2843. unsigned char *psk,
  2844. unsigned int max_psk_len)
  2845. {
  2846. unsigned int psklen = 0;
  2847. psk_client_cb_cnt++;
  2848. if (strlen(pskid) + 1 > max_id_len)
  2849. return 0;
  2850. /* We should only ever be called a maximum of twice per connection */
  2851. if (psk_client_cb_cnt > 2)
  2852. return 0;
  2853. if (clientpsk == NULL)
  2854. return 0;
  2855. /* We'll reuse the PSK we set up for TLSv1.3 */
  2856. if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
  2857. return 0;
  2858. psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
  2859. strncpy(id, pskid, max_id_len);
  2860. return psklen;
  2861. }
  2862. #endif /* OPENSSL_NO_PSK */
  2863. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  2864. size_t identity_len, SSL_SESSION **sess)
  2865. {
  2866. find_session_cb_cnt++;
  2867. /* We should only ever be called a maximum of twice per connection */
  2868. if (find_session_cb_cnt > 2)
  2869. return 0;
  2870. if (serverpsk == NULL)
  2871. return 0;
  2872. /* Identity should match that set by the client */
  2873. if (strlen(srvid) != identity_len
  2874. || strncmp(srvid, (const char *)identity, identity_len) != 0) {
  2875. /* No PSK found, continue but without a PSK */
  2876. *sess = NULL;
  2877. return 1;
  2878. }
  2879. SSL_SESSION_up_ref(serverpsk);
  2880. *sess = serverpsk;
  2881. return 1;
  2882. }
  2883. #ifndef OPENSSL_NO_PSK
  2884. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  2885. unsigned char *psk, unsigned int max_psk_len)
  2886. {
  2887. unsigned int psklen = 0;
  2888. psk_server_cb_cnt++;
  2889. /* We should only ever be called a maximum of twice per connection */
  2890. if (find_session_cb_cnt > 2)
  2891. return 0;
  2892. if (serverpsk == NULL)
  2893. return 0;
  2894. /* Identity should match that set by the client */
  2895. if (strcmp(srvid, identity) != 0) {
  2896. return 0;
  2897. }
  2898. /* We'll reuse the PSK we set up for TLSv1.3 */
  2899. if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
  2900. return 0;
  2901. psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
  2902. return psklen;
  2903. }
  2904. #endif /* OPENSSL_NO_PSK */
  2905. #define MSG1 "Hello"
  2906. #define MSG2 "World."
  2907. #define MSG3 "This"
  2908. #define MSG4 "is"
  2909. #define MSG5 "a"
  2910. #define MSG6 "test"
  2911. #define MSG7 "message."
  2912. static int artificial_ticket_time = 0;
  2913. static int sub_session_time(SSL_SESSION *sess)
  2914. {
  2915. OSSL_TIME tick_time;
  2916. tick_time = ossl_time_from_time_t(SSL_SESSION_get_time_ex(sess));
  2917. tick_time = ossl_time_subtract(tick_time, ossl_seconds2time(10));
  2918. return SSL_SESSION_set_time_ex(sess, ossl_time_to_time_t(tick_time)) != 0;
  2919. }
  2920. static int ed_gen_cb(SSL *s, void *arg)
  2921. {
  2922. SSL_SESSION *sess = SSL_get0_session(s);
  2923. if (sess == NULL)
  2924. return 0;
  2925. /*
  2926. * Artificially give the ticket some age. Just do it for the number of
  2927. * tickets we've been told to do.
  2928. */
  2929. if (artificial_ticket_time == 0)
  2930. return 1;
  2931. artificial_ticket_time--;
  2932. return sub_session_time(sess);
  2933. }
  2934. /*
  2935. * Helper method to setup objects for early data test. Caller frees objects on
  2936. * error.
  2937. */
  2938. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  2939. SSL **serverssl, SSL_SESSION **sess, int idx,
  2940. size_t mdsize)
  2941. {
  2942. int artificial = (artificial_ticket_time > 0);
  2943. if (*sctx == NULL
  2944. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2945. TLS_client_method(),
  2946. TLS1_VERSION, 0,
  2947. sctx, cctx, cert, privkey)))
  2948. return 0;
  2949. if (artificial)
  2950. SSL_CTX_set_session_ticket_cb(*sctx, ed_gen_cb, NULL, NULL);
  2951. if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
  2952. return 0;
  2953. if (idx == 1) {
  2954. /* When idx == 1 we repeat the tests with read_ahead set */
  2955. SSL_CTX_set_read_ahead(*cctx, 1);
  2956. SSL_CTX_set_read_ahead(*sctx, 1);
  2957. } else if (idx == 2) {
  2958. /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
  2959. SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
  2960. SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
  2961. use_session_cb_cnt = 0;
  2962. find_session_cb_cnt = 0;
  2963. srvid = pskid;
  2964. }
  2965. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  2966. NULL, NULL)))
  2967. return 0;
  2968. /*
  2969. * For one of the run throughs (doesn't matter which one), we'll try sending
  2970. * some SNI data in the initial ClientHello. This will be ignored (because
  2971. * there is no SNI cb set up by the server), so it should not impact
  2972. * early_data.
  2973. */
  2974. if (idx == 1
  2975. && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
  2976. return 0;
  2977. if (idx == 2) {
  2978. clientpsk = create_a_psk(*clientssl, mdsize);
  2979. if (!TEST_ptr(clientpsk)
  2980. /*
  2981. * We just choose an arbitrary value for max_early_data which
  2982. * should be big enough for testing purposes.
  2983. */
  2984. || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
  2985. 0x100))
  2986. || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2987. SSL_SESSION_free(clientpsk);
  2988. clientpsk = NULL;
  2989. return 0;
  2990. }
  2991. serverpsk = clientpsk;
  2992. if (sess != NULL) {
  2993. if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2994. SSL_SESSION_free(clientpsk);
  2995. SSL_SESSION_free(serverpsk);
  2996. clientpsk = serverpsk = NULL;
  2997. return 0;
  2998. }
  2999. *sess = clientpsk;
  3000. }
  3001. return 1;
  3002. }
  3003. if (sess == NULL)
  3004. return 1;
  3005. if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
  3006. SSL_ERROR_NONE)))
  3007. return 0;
  3008. *sess = SSL_get1_session(*clientssl);
  3009. SSL_shutdown(*clientssl);
  3010. SSL_shutdown(*serverssl);
  3011. SSL_free(*serverssl);
  3012. SSL_free(*clientssl);
  3013. *serverssl = *clientssl = NULL;
  3014. /*
  3015. * Artificially give the ticket some age to match the artificial age we
  3016. * gave it on the server side
  3017. */
  3018. if (artificial
  3019. && !TEST_true(sub_session_time(*sess)))
  3020. return 0;
  3021. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  3022. clientssl, NULL, NULL))
  3023. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  3024. return 0;
  3025. return 1;
  3026. }
  3027. static int check_early_data_timeout(OSSL_TIME timer)
  3028. {
  3029. int res = 0;
  3030. /*
  3031. * Early data is time sensitive. We have an approx 8 second allowance
  3032. * between writing the early data and reading it. If we exceed that time
  3033. * then this test will fail. This can sometimes (rarely) occur in normal CI
  3034. * operation. We can try and detect this and just ignore the result of this
  3035. * test if it has taken too long. We assume anything over 7 seconds is too
  3036. * long
  3037. */
  3038. timer = ossl_time_subtract(ossl_time_now(), timer);
  3039. if (ossl_time_compare(timer, ossl_seconds2time(7)) >= 0)
  3040. res = TEST_skip("Test took too long, ignoring result");
  3041. return res;
  3042. }
  3043. static int test_early_data_read_write(int idx)
  3044. {
  3045. SSL_CTX *cctx = NULL, *sctx = NULL;
  3046. SSL *clientssl = NULL, *serverssl = NULL;
  3047. int testresult = 0;
  3048. SSL_SESSION *sess = NULL;
  3049. unsigned char buf[20], data[1024];
  3050. size_t readbytes, written, eoedlen, rawread, rawwritten;
  3051. BIO *rbio;
  3052. OSSL_TIME timer;
  3053. /* Artificially give the next 2 tickets some age for non PSK sessions */
  3054. if (idx != 2)
  3055. artificial_ticket_time = 2;
  3056. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3057. &serverssl, &sess, idx,
  3058. SHA384_DIGEST_LENGTH))) {
  3059. artificial_ticket_time = 0;
  3060. goto end;
  3061. }
  3062. artificial_ticket_time = 0;
  3063. /* Write and read some early data */
  3064. timer = ossl_time_now();
  3065. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3066. &written))
  3067. || !TEST_size_t_eq(written, strlen(MSG1)))
  3068. goto end;
  3069. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3070. &readbytes),
  3071. SSL_READ_EARLY_DATA_SUCCESS)) {
  3072. testresult = check_early_data_timeout(timer);
  3073. goto end;
  3074. }
  3075. if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  3076. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3077. SSL_EARLY_DATA_ACCEPTED))
  3078. goto end;
  3079. /*
  3080. * Server should be able to write data, and client should be able to
  3081. * read it.
  3082. */
  3083. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  3084. &written))
  3085. || !TEST_size_t_eq(written, strlen(MSG2))
  3086. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3087. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3088. goto end;
  3089. /* Even after reading normal data, client should be able write early data */
  3090. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  3091. &written))
  3092. || !TEST_size_t_eq(written, strlen(MSG3)))
  3093. goto end;
  3094. /* Server should still be able read early data after writing data */
  3095. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3096. &readbytes),
  3097. SSL_READ_EARLY_DATA_SUCCESS)
  3098. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  3099. goto end;
  3100. /* Write more data from server and read it from client */
  3101. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  3102. &written))
  3103. || !TEST_size_t_eq(written, strlen(MSG4))
  3104. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3105. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  3106. goto end;
  3107. /*
  3108. * If client writes normal data it should mean writing early data is no
  3109. * longer possible.
  3110. */
  3111. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  3112. || !TEST_size_t_eq(written, strlen(MSG5))
  3113. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3114. SSL_EARLY_DATA_ACCEPTED))
  3115. goto end;
  3116. /*
  3117. * At this point the client has written EndOfEarlyData, ClientFinished and
  3118. * normal (fully protected) data. We are going to cause a delay between the
  3119. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  3120. * in the read BIO, and then just put back the EndOfEarlyData message.
  3121. */
  3122. rbio = SSL_get_rbio(serverssl);
  3123. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  3124. || !TEST_size_t_lt(rawread, sizeof(data))
  3125. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  3126. goto end;
  3127. /* Record length is in the 4th and 5th bytes of the record header */
  3128. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  3129. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  3130. || !TEST_size_t_eq(rawwritten, eoedlen))
  3131. goto end;
  3132. /* Server should be told that there is no more early data */
  3133. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3134. &readbytes),
  3135. SSL_READ_EARLY_DATA_FINISH)
  3136. || !TEST_size_t_eq(readbytes, 0))
  3137. goto end;
  3138. /*
  3139. * Server has not finished init yet, so should still be able to write early
  3140. * data.
  3141. */
  3142. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  3143. &written))
  3144. || !TEST_size_t_eq(written, strlen(MSG6)))
  3145. goto end;
  3146. /* Push the ClientFinished and the normal data back into the server rbio */
  3147. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  3148. &rawwritten))
  3149. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  3150. goto end;
  3151. /* Server should be able to read normal data */
  3152. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3153. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  3154. goto end;
  3155. /* Client and server should not be able to write/read early data now */
  3156. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  3157. &written)))
  3158. goto end;
  3159. ERR_clear_error();
  3160. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3161. &readbytes),
  3162. SSL_READ_EARLY_DATA_ERROR))
  3163. goto end;
  3164. ERR_clear_error();
  3165. /* Client should be able to read the data sent by the server */
  3166. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3167. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  3168. goto end;
  3169. /*
  3170. * Make sure we process the two NewSessionTickets. These arrive
  3171. * post-handshake. We attempt reads which we do not expect to return any
  3172. * data.
  3173. */
  3174. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3175. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
  3176. &readbytes)))
  3177. goto end;
  3178. /* Server should be able to write normal data */
  3179. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  3180. || !TEST_size_t_eq(written, strlen(MSG7))
  3181. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3182. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  3183. goto end;
  3184. SSL_SESSION_free(sess);
  3185. sess = SSL_get1_session(clientssl);
  3186. use_session_cb_cnt = 0;
  3187. find_session_cb_cnt = 0;
  3188. SSL_shutdown(clientssl);
  3189. SSL_shutdown(serverssl);
  3190. SSL_free(serverssl);
  3191. SSL_free(clientssl);
  3192. serverssl = clientssl = NULL;
  3193. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3194. &clientssl, NULL, NULL))
  3195. || !TEST_true(SSL_set_session(clientssl, sess)))
  3196. goto end;
  3197. /* Write and read some early data */
  3198. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3199. &written))
  3200. || !TEST_size_t_eq(written, strlen(MSG1))
  3201. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3202. &readbytes),
  3203. SSL_READ_EARLY_DATA_SUCCESS)
  3204. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3205. goto end;
  3206. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  3207. || !TEST_int_gt(SSL_accept(serverssl), 0))
  3208. goto end;
  3209. /* Client and server should not be able to write/read early data now */
  3210. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  3211. &written)))
  3212. goto end;
  3213. ERR_clear_error();
  3214. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3215. &readbytes),
  3216. SSL_READ_EARLY_DATA_ERROR))
  3217. goto end;
  3218. ERR_clear_error();
  3219. /* Client and server should be able to write/read normal data */
  3220. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  3221. || !TEST_size_t_eq(written, strlen(MSG5))
  3222. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3223. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  3224. goto end;
  3225. testresult = 1;
  3226. end:
  3227. SSL_SESSION_free(sess);
  3228. SSL_SESSION_free(clientpsk);
  3229. SSL_SESSION_free(serverpsk);
  3230. clientpsk = serverpsk = NULL;
  3231. SSL_free(serverssl);
  3232. SSL_free(clientssl);
  3233. SSL_CTX_free(sctx);
  3234. SSL_CTX_free(cctx);
  3235. return testresult;
  3236. }
  3237. static int allow_ed_cb_called = 0;
  3238. static int allow_early_data_cb(SSL *s, void *arg)
  3239. {
  3240. int *usecb = (int *)arg;
  3241. allow_ed_cb_called++;
  3242. if (*usecb == 1)
  3243. return 0;
  3244. return 1;
  3245. }
  3246. /*
  3247. * idx == 0: Standard early_data setup
  3248. * idx == 1: early_data setup using read_ahead
  3249. * usecb == 0: Don't use a custom early data callback
  3250. * usecb == 1: Use a custom early data callback and reject the early data
  3251. * usecb == 2: Use a custom early data callback and accept the early data
  3252. * confopt == 0: Configure anti-replay directly
  3253. * confopt == 1: Configure anti-replay using SSL_CONF
  3254. */
  3255. static int test_early_data_replay_int(int idx, int usecb, int confopt)
  3256. {
  3257. SSL_CTX *cctx = NULL, *sctx = NULL;
  3258. SSL *clientssl = NULL, *serverssl = NULL;
  3259. int testresult = 0;
  3260. SSL_SESSION *sess = NULL;
  3261. size_t readbytes, written;
  3262. unsigned char buf[20];
  3263. OSSL_TIME timer;
  3264. allow_ed_cb_called = 0;
  3265. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3266. TLS_client_method(), TLS1_VERSION, 0,
  3267. &sctx, &cctx, cert, privkey)))
  3268. return 0;
  3269. if (usecb > 0) {
  3270. if (confopt == 0) {
  3271. SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
  3272. } else {
  3273. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  3274. if (!TEST_ptr(confctx))
  3275. goto end;
  3276. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  3277. | SSL_CONF_FLAG_SERVER);
  3278. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  3279. if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
  3280. 2)) {
  3281. SSL_CONF_CTX_free(confctx);
  3282. goto end;
  3283. }
  3284. SSL_CONF_CTX_free(confctx);
  3285. }
  3286. SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
  3287. }
  3288. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3289. &serverssl, &sess, idx,
  3290. SHA384_DIGEST_LENGTH)))
  3291. goto end;
  3292. /*
  3293. * The server is configured to accept early data. Create a connection to
  3294. * "use up" the ticket
  3295. */
  3296. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3297. || !TEST_true(SSL_session_reused(clientssl)))
  3298. goto end;
  3299. SSL_shutdown(clientssl);
  3300. SSL_shutdown(serverssl);
  3301. SSL_free(serverssl);
  3302. SSL_free(clientssl);
  3303. serverssl = clientssl = NULL;
  3304. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3305. &clientssl, NULL, NULL))
  3306. || !TEST_true(SSL_set_session(clientssl, sess)))
  3307. goto end;
  3308. /* Write and read some early data */
  3309. timer = ossl_time_now();
  3310. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3311. &written))
  3312. || !TEST_size_t_eq(written, strlen(MSG1)))
  3313. goto end;
  3314. if (usecb <= 1) {
  3315. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3316. &readbytes),
  3317. SSL_READ_EARLY_DATA_FINISH)
  3318. /*
  3319. * The ticket was reused, so the we should have rejected the
  3320. * early data
  3321. */
  3322. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3323. SSL_EARLY_DATA_REJECTED))
  3324. goto end;
  3325. } else {
  3326. /* In this case the callback decides to accept the early data */
  3327. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3328. &readbytes),
  3329. SSL_READ_EARLY_DATA_SUCCESS)) {
  3330. testresult = check_early_data_timeout(timer);
  3331. goto end;
  3332. }
  3333. if (!TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
  3334. /*
  3335. * Server will have sent its flight so client can now send
  3336. * end of early data and complete its half of the handshake
  3337. */
  3338. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3339. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3340. &readbytes),
  3341. SSL_READ_EARLY_DATA_FINISH)
  3342. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3343. SSL_EARLY_DATA_ACCEPTED))
  3344. goto end;
  3345. }
  3346. /* Complete the connection */
  3347. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3348. || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
  3349. || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
  3350. goto end;
  3351. testresult = 1;
  3352. end:
  3353. SSL_SESSION_free(sess);
  3354. SSL_SESSION_free(clientpsk);
  3355. SSL_SESSION_free(serverpsk);
  3356. clientpsk = serverpsk = NULL;
  3357. SSL_free(serverssl);
  3358. SSL_free(clientssl);
  3359. SSL_CTX_free(sctx);
  3360. SSL_CTX_free(cctx);
  3361. return testresult;
  3362. }
  3363. static int test_early_data_replay(int idx)
  3364. {
  3365. int ret = 1, usecb, confopt;
  3366. for (usecb = 0; usecb < 3; usecb++) {
  3367. for (confopt = 0; confopt < 2; confopt++)
  3368. ret &= test_early_data_replay_int(idx, usecb, confopt);
  3369. }
  3370. return ret;
  3371. }
  3372. static const char *ciphersuites[] = {
  3373. "TLS_AES_128_CCM_8_SHA256",
  3374. "TLS_AES_128_GCM_SHA256",
  3375. "TLS_AES_256_GCM_SHA384",
  3376. "TLS_AES_128_CCM_SHA256",
  3377. #if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3378. "TLS_CHACHA20_POLY1305_SHA256"
  3379. #endif
  3380. };
  3381. /*
  3382. * Helper function to test that a server attempting to read early data can
  3383. * handle a connection from a client where the early data should be skipped.
  3384. * testtype: 0 == No HRR
  3385. * testtype: 1 == HRR
  3386. * testtype: 2 == HRR, invalid early_data sent after HRR
  3387. * testtype: 3 == recv_max_early_data set to 0
  3388. */
  3389. static int early_data_skip_helper(int testtype, int cipher, int idx)
  3390. {
  3391. SSL_CTX *cctx = NULL, *sctx = NULL;
  3392. SSL *clientssl = NULL, *serverssl = NULL;
  3393. int testresult = 0;
  3394. SSL_SESSION *sess = NULL;
  3395. unsigned char buf[20];
  3396. size_t readbytes, written;
  3397. if (is_fips && cipher == 4)
  3398. return 1;
  3399. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3400. TLS_client_method(),
  3401. TLS1_VERSION, 0,
  3402. &sctx, &cctx, cert, privkey)))
  3403. goto end;
  3404. if (cipher == 0) {
  3405. SSL_CTX_set_security_level(sctx, 0);
  3406. SSL_CTX_set_security_level(cctx, 0);
  3407. }
  3408. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, ciphersuites[cipher]))
  3409. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, ciphersuites[cipher])))
  3410. goto end;
  3411. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3412. &serverssl, &sess, idx,
  3413. cipher == 2 ? SHA384_DIGEST_LENGTH
  3414. : SHA256_DIGEST_LENGTH)))
  3415. goto end;
  3416. if (testtype == 1 || testtype == 2) {
  3417. /* Force an HRR to occur */
  3418. #if defined(OPENSSL_NO_EC)
  3419. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  3420. goto end;
  3421. #else
  3422. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
  3423. goto end;
  3424. #endif
  3425. } else if (idx == 2) {
  3426. /*
  3427. * We force early_data rejection by ensuring the PSK identity is
  3428. * unrecognised
  3429. */
  3430. srvid = "Dummy Identity";
  3431. } else {
  3432. /*
  3433. * Deliberately corrupt the creation time. We take 20 seconds off the
  3434. * time. It could be any value as long as it is not within tolerance.
  3435. * This should mean the ticket is rejected.
  3436. */
  3437. if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
  3438. goto end;
  3439. }
  3440. if (testtype == 3
  3441. && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
  3442. goto end;
  3443. /* Write some early data */
  3444. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3445. &written))
  3446. || !TEST_size_t_eq(written, strlen(MSG1)))
  3447. goto end;
  3448. /* Server should reject the early data */
  3449. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3450. &readbytes),
  3451. SSL_READ_EARLY_DATA_FINISH)
  3452. || !TEST_size_t_eq(readbytes, 0)
  3453. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3454. SSL_EARLY_DATA_REJECTED))
  3455. goto end;
  3456. switch (testtype) {
  3457. case 0:
  3458. /* Nothing to do */
  3459. break;
  3460. case 1:
  3461. /*
  3462. * Finish off the handshake. We perform the same writes and reads as
  3463. * further down but we expect them to fail due to the incomplete
  3464. * handshake.
  3465. */
  3466. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3467. || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
  3468. &readbytes)))
  3469. goto end;
  3470. break;
  3471. case 2:
  3472. {
  3473. BIO *wbio = SSL_get_wbio(clientssl);
  3474. /* A record that will appear as bad early_data */
  3475. const unsigned char bad_early_data[] = {
  3476. 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
  3477. };
  3478. /*
  3479. * We force the client to attempt a write. This will fail because
  3480. * we're still in the handshake. It will cause the second
  3481. * ClientHello to be sent.
  3482. */
  3483. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
  3484. &written)))
  3485. goto end;
  3486. /*
  3487. * Inject some early_data after the second ClientHello. This should
  3488. * cause the server to fail
  3489. */
  3490. if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
  3491. sizeof(bad_early_data), &written)))
  3492. goto end;
  3493. }
  3494. /* fallthrough */
  3495. case 3:
  3496. /*
  3497. * This client has sent more early_data than we are willing to skip
  3498. * (case 3) or sent invalid early_data (case 2) so the connection should
  3499. * abort.
  3500. */
  3501. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3502. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
  3503. goto end;
  3504. /* Connection has failed - nothing more to do */
  3505. testresult = 1;
  3506. goto end;
  3507. default:
  3508. TEST_error("Invalid test type");
  3509. goto end;
  3510. }
  3511. ERR_clear_error();
  3512. /*
  3513. * Should be able to send normal data despite rejection of early data. The
  3514. * early_data should be skipped.
  3515. */
  3516. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3517. || !TEST_size_t_eq(written, strlen(MSG2))
  3518. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3519. SSL_EARLY_DATA_REJECTED)
  3520. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3521. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3522. goto end;
  3523. /*
  3524. * Failure to decrypt early data records should not leave spurious errors
  3525. * on the error stack
  3526. */
  3527. if (!TEST_long_eq(ERR_peek_error(), 0))
  3528. goto end;
  3529. testresult = 1;
  3530. end:
  3531. SSL_SESSION_free(clientpsk);
  3532. SSL_SESSION_free(serverpsk);
  3533. clientpsk = serverpsk = NULL;
  3534. SSL_SESSION_free(sess);
  3535. SSL_free(serverssl);
  3536. SSL_free(clientssl);
  3537. SSL_CTX_free(sctx);
  3538. SSL_CTX_free(cctx);
  3539. return testresult;
  3540. }
  3541. /*
  3542. * Test that a server attempting to read early data can handle a connection
  3543. * from a client where the early data is not acceptable.
  3544. */
  3545. static int test_early_data_skip(int idx)
  3546. {
  3547. return early_data_skip_helper(0,
  3548. idx % OSSL_NELEM(ciphersuites),
  3549. idx / OSSL_NELEM(ciphersuites));
  3550. }
  3551. /*
  3552. * Test that a server attempting to read early data can handle a connection
  3553. * from a client where an HRR occurs.
  3554. */
  3555. static int test_early_data_skip_hrr(int idx)
  3556. {
  3557. return early_data_skip_helper(1,
  3558. idx % OSSL_NELEM(ciphersuites),
  3559. idx / OSSL_NELEM(ciphersuites));
  3560. }
  3561. /*
  3562. * Test that a server attempting to read early data can handle a connection
  3563. * from a client where an HRR occurs and correctly fails if early_data is sent
  3564. * after the HRR
  3565. */
  3566. static int test_early_data_skip_hrr_fail(int idx)
  3567. {
  3568. return early_data_skip_helper(2,
  3569. idx % OSSL_NELEM(ciphersuites),
  3570. idx / OSSL_NELEM(ciphersuites));
  3571. }
  3572. /*
  3573. * Test that a server attempting to read early data will abort if it tries to
  3574. * skip over too much.
  3575. */
  3576. static int test_early_data_skip_abort(int idx)
  3577. {
  3578. return early_data_skip_helper(3,
  3579. idx % OSSL_NELEM(ciphersuites),
  3580. idx / OSSL_NELEM(ciphersuites));
  3581. }
  3582. /*
  3583. * Test that a server attempting to read early data can handle a connection
  3584. * from a client that doesn't send any.
  3585. */
  3586. static int test_early_data_not_sent(int idx)
  3587. {
  3588. SSL_CTX *cctx = NULL, *sctx = NULL;
  3589. SSL *clientssl = NULL, *serverssl = NULL;
  3590. int testresult = 0;
  3591. SSL_SESSION *sess = NULL;
  3592. unsigned char buf[20];
  3593. size_t readbytes, written;
  3594. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3595. &serverssl, &sess, idx,
  3596. SHA384_DIGEST_LENGTH)))
  3597. goto end;
  3598. /* Write some data - should block due to handshake with server */
  3599. SSL_set_connect_state(clientssl);
  3600. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3601. goto end;
  3602. /* Server should detect that early data has not been sent */
  3603. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3604. &readbytes),
  3605. SSL_READ_EARLY_DATA_FINISH)
  3606. || !TEST_size_t_eq(readbytes, 0)
  3607. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3608. SSL_EARLY_DATA_NOT_SENT)
  3609. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3610. SSL_EARLY_DATA_NOT_SENT))
  3611. goto end;
  3612. /* Continue writing the message we started earlier */
  3613. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3614. || !TEST_size_t_eq(written, strlen(MSG1))
  3615. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3616. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3617. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  3618. || !TEST_size_t_eq(written, strlen(MSG2)))
  3619. goto end;
  3620. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3621. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3622. goto end;
  3623. testresult = 1;
  3624. end:
  3625. SSL_SESSION_free(sess);
  3626. SSL_SESSION_free(clientpsk);
  3627. SSL_SESSION_free(serverpsk);
  3628. clientpsk = serverpsk = NULL;
  3629. SSL_free(serverssl);
  3630. SSL_free(clientssl);
  3631. SSL_CTX_free(sctx);
  3632. SSL_CTX_free(cctx);
  3633. return testresult;
  3634. }
  3635. static const char *servalpn;
  3636. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  3637. unsigned char *outlen, const unsigned char *in,
  3638. unsigned int inlen, void *arg)
  3639. {
  3640. unsigned int protlen = 0;
  3641. const unsigned char *prot;
  3642. for (prot = in; prot < in + inlen; prot += protlen) {
  3643. protlen = *prot++;
  3644. if (in + inlen < prot + protlen)
  3645. return SSL_TLSEXT_ERR_NOACK;
  3646. if (protlen == strlen(servalpn)
  3647. && memcmp(prot, servalpn, protlen) == 0) {
  3648. *out = prot;
  3649. *outlen = protlen;
  3650. return SSL_TLSEXT_ERR_OK;
  3651. }
  3652. }
  3653. return SSL_TLSEXT_ERR_NOACK;
  3654. }
  3655. /* Test that a PSK can be used to send early_data */
  3656. static int test_early_data_psk(int idx)
  3657. {
  3658. SSL_CTX *cctx = NULL, *sctx = NULL;
  3659. SSL *clientssl = NULL, *serverssl = NULL;
  3660. int testresult = 0;
  3661. SSL_SESSION *sess = NULL;
  3662. unsigned char alpnlist[] = {
  3663. 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
  3664. 'l', 'p', 'n'
  3665. };
  3666. #define GOODALPNLEN 9
  3667. #define BADALPNLEN 8
  3668. #define GOODALPN (alpnlist)
  3669. #define BADALPN (alpnlist + GOODALPNLEN)
  3670. int err = 0;
  3671. unsigned char buf[20];
  3672. size_t readbytes, written;
  3673. int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
  3674. int edstatus = SSL_EARLY_DATA_ACCEPTED;
  3675. /* We always set this up with a final parameter of "2" for PSK */
  3676. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3677. &serverssl, &sess, 2,
  3678. SHA384_DIGEST_LENGTH)))
  3679. goto end;
  3680. servalpn = "goodalpn";
  3681. /*
  3682. * Note: There is no test for inconsistent SNI with late client detection.
  3683. * This is because servers do not acknowledge SNI even if they are using
  3684. * it in a resumption handshake - so it is not actually possible for a
  3685. * client to detect a problem.
  3686. */
  3687. switch (idx) {
  3688. case 0:
  3689. /* Set inconsistent SNI (early client detection) */
  3690. err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
  3691. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3692. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
  3693. goto end;
  3694. break;
  3695. case 1:
  3696. /* Set inconsistent ALPN (early client detection) */
  3697. err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
  3698. /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
  3699. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
  3700. GOODALPNLEN))
  3701. || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
  3702. BADALPNLEN)))
  3703. goto end;
  3704. break;
  3705. case 2:
  3706. /*
  3707. * Set invalid protocol version. Technically this affects PSKs without
  3708. * early_data too, but we test it here because it is similar to the
  3709. * SNI/ALPN consistency tests.
  3710. */
  3711. err = SSL_R_BAD_PSK;
  3712. if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
  3713. goto end;
  3714. break;
  3715. case 3:
  3716. /*
  3717. * Set inconsistent SNI (server side). In this case the connection
  3718. * will succeed and accept early_data. In TLSv1.3 on the server side SNI
  3719. * is associated with each handshake - not the session. Therefore it
  3720. * should not matter that we used a different server name last time.
  3721. */
  3722. SSL_SESSION_free(serverpsk);
  3723. serverpsk = SSL_SESSION_dup(clientpsk);
  3724. if (!TEST_ptr(serverpsk)
  3725. || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
  3726. goto end;
  3727. /* Fall through */
  3728. case 4:
  3729. /* Set consistent SNI */
  3730. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3731. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
  3732. || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  3733. hostname_cb)))
  3734. goto end;
  3735. break;
  3736. case 5:
  3737. /*
  3738. * Set inconsistent ALPN (server detected). In this case the connection
  3739. * will succeed but reject early_data.
  3740. */
  3741. servalpn = "badalpn";
  3742. edstatus = SSL_EARLY_DATA_REJECTED;
  3743. readearlyres = SSL_READ_EARLY_DATA_FINISH;
  3744. /* Fall through */
  3745. case 6:
  3746. /*
  3747. * Set consistent ALPN.
  3748. * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
  3749. * accepts a list of protos (each one length prefixed).
  3750. * SSL_set1_alpn_selected accepts a single protocol (not length
  3751. * prefixed)
  3752. */
  3753. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
  3754. GOODALPNLEN - 1))
  3755. || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
  3756. GOODALPNLEN)))
  3757. goto end;
  3758. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3759. break;
  3760. case 7:
  3761. /* Set inconsistent ALPN (late client detection) */
  3762. SSL_SESSION_free(serverpsk);
  3763. serverpsk = SSL_SESSION_dup(clientpsk);
  3764. if (!TEST_ptr(serverpsk)
  3765. || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
  3766. BADALPN + 1,
  3767. BADALPNLEN - 1))
  3768. || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
  3769. GOODALPN + 1,
  3770. GOODALPNLEN - 1))
  3771. || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
  3772. sizeof(alpnlist))))
  3773. goto end;
  3774. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3775. edstatus = SSL_EARLY_DATA_ACCEPTED;
  3776. readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
  3777. /* SSL_connect() call should fail */
  3778. connectres = -1;
  3779. break;
  3780. default:
  3781. TEST_error("Bad test index");
  3782. goto end;
  3783. }
  3784. SSL_set_connect_state(clientssl);
  3785. if (err != 0) {
  3786. if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3787. &written))
  3788. || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
  3789. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
  3790. goto end;
  3791. } else {
  3792. OSSL_TIME timer = ossl_time_now();
  3793. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3794. &written)))
  3795. goto end;
  3796. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3797. &readbytes), readearlyres)) {
  3798. testresult = check_early_data_timeout(timer);
  3799. goto end;
  3800. }
  3801. if ((readearlyres == SSL_READ_EARLY_DATA_SUCCESS
  3802. && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3803. || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
  3804. || !TEST_int_eq(SSL_connect(clientssl), connectres))
  3805. goto end;
  3806. }
  3807. testresult = 1;
  3808. end:
  3809. SSL_SESSION_free(sess);
  3810. SSL_SESSION_free(clientpsk);
  3811. SSL_SESSION_free(serverpsk);
  3812. clientpsk = serverpsk = NULL;
  3813. SSL_free(serverssl);
  3814. SSL_free(clientssl);
  3815. SSL_CTX_free(sctx);
  3816. SSL_CTX_free(cctx);
  3817. return testresult;
  3818. }
  3819. /*
  3820. * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
  3821. * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
  3822. * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
  3823. * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3824. * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
  3825. * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
  3826. */
  3827. static int test_early_data_psk_with_all_ciphers(int idx)
  3828. {
  3829. SSL_CTX *cctx = NULL, *sctx = NULL;
  3830. SSL *clientssl = NULL, *serverssl = NULL;
  3831. int testresult = 0;
  3832. SSL_SESSION *sess = NULL;
  3833. unsigned char buf[20];
  3834. size_t readbytes, written;
  3835. const SSL_CIPHER *cipher;
  3836. OSSL_TIME timer;
  3837. const char *cipher_str[] = {
  3838. TLS1_3_RFC_AES_128_GCM_SHA256,
  3839. TLS1_3_RFC_AES_256_GCM_SHA384,
  3840. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3841. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3842. # else
  3843. NULL,
  3844. # endif
  3845. TLS1_3_RFC_AES_128_CCM_SHA256,
  3846. TLS1_3_RFC_AES_128_CCM_8_SHA256
  3847. };
  3848. const unsigned char *cipher_bytes[] = {
  3849. TLS13_AES_128_GCM_SHA256_BYTES,
  3850. TLS13_AES_256_GCM_SHA384_BYTES,
  3851. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3852. TLS13_CHACHA20_POLY1305_SHA256_BYTES,
  3853. # else
  3854. NULL,
  3855. # endif
  3856. TLS13_AES_128_CCM_SHA256_BYTES,
  3857. TLS13_AES_128_CCM_8_SHA256_BYTES
  3858. };
  3859. if (cipher_str[idx] == NULL)
  3860. return 1;
  3861. /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
  3862. if (idx == 2 && is_fips == 1)
  3863. return 1;
  3864. /* We always set this up with a final parameter of "2" for PSK */
  3865. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3866. &serverssl, &sess, 2,
  3867. SHA384_DIGEST_LENGTH)))
  3868. goto end;
  3869. if (idx == 4) {
  3870. /* CCM8 ciphers are considered low security due to their short tag */
  3871. SSL_set_security_level(clientssl, 0);
  3872. SSL_set_security_level(serverssl, 0);
  3873. }
  3874. if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
  3875. || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
  3876. goto end;
  3877. /*
  3878. * 'setupearly_data_test' creates only one instance of SSL_SESSION
  3879. * and assigns to both client and server with incremented reference
  3880. * and the same instance is updated in 'sess'.
  3881. * So updating ciphersuite in 'sess' which will get reflected in
  3882. * PSK handshake using psk use sess and find sess cb.
  3883. */
  3884. cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
  3885. if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
  3886. goto end;
  3887. SSL_set_connect_state(clientssl);
  3888. timer = ossl_time_now();
  3889. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3890. &written)))
  3891. goto end;
  3892. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3893. &readbytes),
  3894. SSL_READ_EARLY_DATA_SUCCESS)) {
  3895. testresult = check_early_data_timeout(timer);
  3896. goto end;
  3897. }
  3898. if (!TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3899. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3900. SSL_EARLY_DATA_ACCEPTED)
  3901. || !TEST_int_eq(SSL_connect(clientssl), 1)
  3902. || !TEST_int_eq(SSL_accept(serverssl), 1))
  3903. goto end;
  3904. /* Send some normal data from client to server */
  3905. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3906. || !TEST_size_t_eq(written, strlen(MSG2)))
  3907. goto end;
  3908. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3909. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3910. goto end;
  3911. testresult = 1;
  3912. end:
  3913. SSL_SESSION_free(sess);
  3914. SSL_SESSION_free(clientpsk);
  3915. SSL_SESSION_free(serverpsk);
  3916. clientpsk = serverpsk = NULL;
  3917. if (clientssl != NULL)
  3918. SSL_shutdown(clientssl);
  3919. if (serverssl != NULL)
  3920. SSL_shutdown(serverssl);
  3921. SSL_free(serverssl);
  3922. SSL_free(clientssl);
  3923. SSL_CTX_free(sctx);
  3924. SSL_CTX_free(cctx);
  3925. return testresult;
  3926. }
  3927. /*
  3928. * Test that a server that doesn't try to read early data can handle a
  3929. * client sending some.
  3930. */
  3931. static int test_early_data_not_expected(int idx)
  3932. {
  3933. SSL_CTX *cctx = NULL, *sctx = NULL;
  3934. SSL *clientssl = NULL, *serverssl = NULL;
  3935. int testresult = 0;
  3936. SSL_SESSION *sess = NULL;
  3937. unsigned char buf[20];
  3938. size_t readbytes, written;
  3939. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3940. &serverssl, &sess, idx,
  3941. SHA384_DIGEST_LENGTH)))
  3942. goto end;
  3943. /* Write some early data */
  3944. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3945. &written)))
  3946. goto end;
  3947. /*
  3948. * Server should skip over early data and then block waiting for client to
  3949. * continue handshake
  3950. */
  3951. if (!TEST_int_le(SSL_accept(serverssl), 0)
  3952. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3953. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3954. SSL_EARLY_DATA_REJECTED)
  3955. || !TEST_int_gt(SSL_accept(serverssl), 0)
  3956. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3957. SSL_EARLY_DATA_REJECTED))
  3958. goto end;
  3959. /* Send some normal data from client to server */
  3960. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3961. || !TEST_size_t_eq(written, strlen(MSG2)))
  3962. goto end;
  3963. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3964. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3965. goto end;
  3966. testresult = 1;
  3967. end:
  3968. SSL_SESSION_free(sess);
  3969. SSL_SESSION_free(clientpsk);
  3970. SSL_SESSION_free(serverpsk);
  3971. clientpsk = serverpsk = NULL;
  3972. SSL_free(serverssl);
  3973. SSL_free(clientssl);
  3974. SSL_CTX_free(sctx);
  3975. SSL_CTX_free(cctx);
  3976. return testresult;
  3977. }
  3978. # ifndef OPENSSL_NO_TLS1_2
  3979. /*
  3980. * Test that a server attempting to read early data can handle a connection
  3981. * from a TLSv1.2 client.
  3982. */
  3983. static int test_early_data_tls1_2(int idx)
  3984. {
  3985. SSL_CTX *cctx = NULL, *sctx = NULL;
  3986. SSL *clientssl = NULL, *serverssl = NULL;
  3987. int testresult = 0;
  3988. unsigned char buf[20];
  3989. size_t readbytes, written;
  3990. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3991. &serverssl, NULL, idx,
  3992. SHA384_DIGEST_LENGTH)))
  3993. goto end;
  3994. /* Write some data - should block due to handshake with server */
  3995. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  3996. SSL_set_connect_state(clientssl);
  3997. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3998. goto end;
  3999. /*
  4000. * Server should do TLSv1.2 handshake. First it will block waiting for more
  4001. * messages from client after ServerDone. Then SSL_read_early_data should
  4002. * finish and detect that early data has not been sent
  4003. */
  4004. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  4005. &readbytes),
  4006. SSL_READ_EARLY_DATA_ERROR))
  4007. goto end;
  4008. /*
  4009. * Continue writing the message we started earlier. Will still block waiting
  4010. * for the CCS/Finished from server
  4011. */
  4012. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  4013. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  4014. &readbytes),
  4015. SSL_READ_EARLY_DATA_FINISH)
  4016. || !TEST_size_t_eq(readbytes, 0)
  4017. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  4018. SSL_EARLY_DATA_NOT_SENT))
  4019. goto end;
  4020. /* Continue writing the message we started earlier */
  4021. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  4022. || !TEST_size_t_eq(written, strlen(MSG1))
  4023. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  4024. SSL_EARLY_DATA_NOT_SENT)
  4025. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  4026. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  4027. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  4028. || !TEST_size_t_eq(written, strlen(MSG2))
  4029. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  4030. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  4031. goto end;
  4032. testresult = 1;
  4033. end:
  4034. SSL_SESSION_free(clientpsk);
  4035. SSL_SESSION_free(serverpsk);
  4036. clientpsk = serverpsk = NULL;
  4037. SSL_free(serverssl);
  4038. SSL_free(clientssl);
  4039. SSL_CTX_free(sctx);
  4040. SSL_CTX_free(cctx);
  4041. return testresult;
  4042. }
  4043. # endif /* OPENSSL_NO_TLS1_2 */
  4044. /*
  4045. * Test configuring the TLSv1.3 ciphersuites
  4046. *
  4047. * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
  4048. * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
  4049. * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
  4050. * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
  4051. * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  4052. * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  4053. * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
  4054. * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
  4055. * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
  4056. * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
  4057. */
  4058. static int test_set_ciphersuite(int idx)
  4059. {
  4060. SSL_CTX *cctx = NULL, *sctx = NULL;
  4061. SSL *clientssl = NULL, *serverssl = NULL;
  4062. int testresult = 0;
  4063. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4064. TLS_client_method(), TLS1_VERSION, 0,
  4065. &sctx, &cctx, cert, privkey))
  4066. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4067. "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
  4068. goto end;
  4069. if (idx >=4 && idx <= 7) {
  4070. /* SSL_CTX explicit cipher list */
  4071. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
  4072. goto end;
  4073. }
  4074. if (idx == 0 || idx == 4) {
  4075. /* Default ciphersuite */
  4076. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4077. "TLS_AES_128_GCM_SHA256")))
  4078. goto end;
  4079. } else if (idx == 1 || idx == 5) {
  4080. /* Non default ciphersuite */
  4081. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4082. "TLS_AES_128_CCM_SHA256")))
  4083. goto end;
  4084. }
  4085. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4086. &clientssl, NULL, NULL)))
  4087. goto end;
  4088. if (idx == 8 || idx == 9) {
  4089. /* SSL explicit cipher list */
  4090. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
  4091. goto end;
  4092. }
  4093. if (idx == 2 || idx == 6 || idx == 8) {
  4094. /* Default ciphersuite */
  4095. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  4096. "TLS_AES_128_GCM_SHA256")))
  4097. goto end;
  4098. } else if (idx == 3 || idx == 7 || idx == 9) {
  4099. /* Non default ciphersuite */
  4100. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  4101. "TLS_AES_128_CCM_SHA256")))
  4102. goto end;
  4103. }
  4104. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4105. goto end;
  4106. testresult = 1;
  4107. end:
  4108. SSL_free(serverssl);
  4109. SSL_free(clientssl);
  4110. SSL_CTX_free(sctx);
  4111. SSL_CTX_free(cctx);
  4112. return testresult;
  4113. }
  4114. static int test_ciphersuite_change(void)
  4115. {
  4116. SSL_CTX *cctx = NULL, *sctx = NULL;
  4117. SSL *clientssl = NULL, *serverssl = NULL;
  4118. SSL_SESSION *clntsess = NULL;
  4119. int testresult = 0;
  4120. const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
  4121. /* Create a session based on SHA-256 */
  4122. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4123. TLS_client_method(), TLS1_VERSION, 0,
  4124. &sctx, &cctx, cert, privkey))
  4125. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4126. "TLS_AES_128_GCM_SHA256:"
  4127. "TLS_AES_256_GCM_SHA384:"
  4128. "TLS_AES_128_CCM_SHA256"))
  4129. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4130. "TLS_AES_128_GCM_SHA256")))
  4131. goto end;
  4132. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4133. NULL, NULL))
  4134. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4135. SSL_ERROR_NONE)))
  4136. goto end;
  4137. clntsess = SSL_get1_session(clientssl);
  4138. /* Save for later */
  4139. aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
  4140. SSL_shutdown(clientssl);
  4141. SSL_shutdown(serverssl);
  4142. SSL_free(serverssl);
  4143. SSL_free(clientssl);
  4144. serverssl = clientssl = NULL;
  4145. /* Check we can resume a session with a different SHA-256 ciphersuite */
  4146. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4147. "TLS_AES_128_CCM_SHA256"))
  4148. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4149. &clientssl, NULL, NULL))
  4150. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4151. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4152. SSL_ERROR_NONE))
  4153. || !TEST_true(SSL_session_reused(clientssl)))
  4154. goto end;
  4155. SSL_SESSION_free(clntsess);
  4156. clntsess = SSL_get1_session(clientssl);
  4157. SSL_shutdown(clientssl);
  4158. SSL_shutdown(serverssl);
  4159. SSL_free(serverssl);
  4160. SSL_free(clientssl);
  4161. serverssl = clientssl = NULL;
  4162. /*
  4163. * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
  4164. * succeeds but does not resume.
  4165. */
  4166. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  4167. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4168. NULL, NULL))
  4169. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4170. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4171. SSL_ERROR_SSL))
  4172. || !TEST_false(SSL_session_reused(clientssl)))
  4173. goto end;
  4174. SSL_SESSION_free(clntsess);
  4175. clntsess = NULL;
  4176. SSL_shutdown(clientssl);
  4177. SSL_shutdown(serverssl);
  4178. SSL_free(serverssl);
  4179. SSL_free(clientssl);
  4180. serverssl = clientssl = NULL;
  4181. /* Create a session based on SHA384 */
  4182. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  4183. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4184. &clientssl, NULL, NULL))
  4185. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4186. SSL_ERROR_NONE)))
  4187. goto end;
  4188. clntsess = SSL_get1_session(clientssl);
  4189. SSL_shutdown(clientssl);
  4190. SSL_shutdown(serverssl);
  4191. SSL_free(serverssl);
  4192. SSL_free(clientssl);
  4193. serverssl = clientssl = NULL;
  4194. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4195. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
  4196. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4197. "TLS_AES_256_GCM_SHA384"))
  4198. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4199. NULL, NULL))
  4200. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4201. /*
  4202. * We use SSL_ERROR_WANT_READ below so that we can pause the
  4203. * connection after the initial ClientHello has been sent to
  4204. * enable us to make some session changes.
  4205. */
  4206. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4207. SSL_ERROR_WANT_READ)))
  4208. goto end;
  4209. /* Trick the client into thinking this session is for a different digest */
  4210. clntsess->cipher = aes_128_gcm_sha256;
  4211. clntsess->cipher_id = clntsess->cipher->id;
  4212. /*
  4213. * Continue the previously started connection. Server has selected a SHA-384
  4214. * ciphersuite, but client thinks the session is for SHA-256, so it should
  4215. * bail out.
  4216. */
  4217. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  4218. SSL_ERROR_SSL))
  4219. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
  4220. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
  4221. goto end;
  4222. testresult = 1;
  4223. end:
  4224. SSL_SESSION_free(clntsess);
  4225. SSL_free(serverssl);
  4226. SSL_free(clientssl);
  4227. SSL_CTX_free(sctx);
  4228. SSL_CTX_free(cctx);
  4229. return testresult;
  4230. }
  4231. /*
  4232. * Test TLSv1.3 Key exchange
  4233. * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
  4234. * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
  4235. * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
  4236. * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
  4237. * Test 4 = Test NID_X25519 with TLSv1.3 client and server
  4238. * Test 5 = Test NID_X448 with TLSv1.3 client and server
  4239. * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
  4240. * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
  4241. * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
  4242. * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
  4243. * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
  4244. * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
  4245. * Test 12 = Test all ECDHE with TLSv1.2 client and server
  4246. * Test 13 = Test all FFDHE with TLSv1.2 client and server
  4247. */
  4248. # ifndef OPENSSL_NO_EC
  4249. static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
  4250. NID_secp521r1,
  4251. # ifndef OPENSSL_NO_ECX
  4252. NID_X25519, NID_X448
  4253. # endif
  4254. };
  4255. # endif
  4256. # ifndef OPENSSL_NO_DH
  4257. static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
  4258. NID_ffdhe6144, NID_ffdhe8192};
  4259. # endif
  4260. static int test_key_exchange(int idx)
  4261. {
  4262. SSL_CTX *sctx = NULL, *cctx = NULL;
  4263. SSL *serverssl = NULL, *clientssl = NULL;
  4264. int testresult = 0;
  4265. int kexch_alg;
  4266. int *kexch_groups = &kexch_alg;
  4267. int kexch_groups_size = 1;
  4268. int max_version = TLS1_3_VERSION;
  4269. char *kexch_name0 = NULL;
  4270. switch (idx) {
  4271. # ifndef OPENSSL_NO_EC
  4272. # ifndef OPENSSL_NO_TLS1_2
  4273. case 12:
  4274. max_version = TLS1_2_VERSION;
  4275. # endif
  4276. /* Fall through */
  4277. case 0:
  4278. kexch_groups = ecdhe_kexch_groups;
  4279. kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
  4280. kexch_name0 = "secp256r1";
  4281. break;
  4282. case 1:
  4283. kexch_alg = NID_X9_62_prime256v1;
  4284. kexch_name0 = "secp256r1";
  4285. break;
  4286. case 2:
  4287. kexch_alg = NID_secp384r1;
  4288. kexch_name0 = "secp384r1";
  4289. break;
  4290. case 3:
  4291. kexch_alg = NID_secp521r1;
  4292. kexch_name0 = "secp521r1";
  4293. break;
  4294. # ifndef OPENSSL_NO_ECX
  4295. case 4:
  4296. if (is_fips)
  4297. return TEST_skip("X25519 might not be supported by fips provider.");
  4298. kexch_alg = NID_X25519;
  4299. kexch_name0 = "x25519";
  4300. break;
  4301. case 5:
  4302. if (is_fips)
  4303. return TEST_skip("X448 might not be supported by fips provider.");
  4304. kexch_alg = NID_X448;
  4305. kexch_name0 = "x448";
  4306. break;
  4307. # endif
  4308. # endif
  4309. # ifndef OPENSSL_NO_DH
  4310. # ifndef OPENSSL_NO_TLS1_2
  4311. case 13:
  4312. max_version = TLS1_2_VERSION;
  4313. kexch_name0 = "ffdhe2048";
  4314. # endif
  4315. /* Fall through */
  4316. case 6:
  4317. kexch_groups = ffdhe_kexch_groups;
  4318. kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
  4319. kexch_name0 = "ffdhe2048";
  4320. break;
  4321. case 7:
  4322. kexch_alg = NID_ffdhe2048;
  4323. kexch_name0 = "ffdhe2048";
  4324. break;
  4325. case 8:
  4326. kexch_alg = NID_ffdhe3072;
  4327. kexch_name0 = "ffdhe3072";
  4328. break;
  4329. case 9:
  4330. kexch_alg = NID_ffdhe4096;
  4331. kexch_name0 = "ffdhe4096";
  4332. break;
  4333. case 10:
  4334. kexch_alg = NID_ffdhe6144;
  4335. kexch_name0 = "ffdhe6144";
  4336. break;
  4337. case 11:
  4338. kexch_alg = NID_ffdhe8192;
  4339. kexch_name0 = "ffdhe8192";
  4340. break;
  4341. # endif
  4342. default:
  4343. /* We're skipping this test */
  4344. return 1;
  4345. }
  4346. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4347. TLS_client_method(), TLS1_VERSION,
  4348. max_version, &sctx, &cctx, cert,
  4349. privkey)))
  4350. goto end;
  4351. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4352. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4353. goto end;
  4354. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4355. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4356. goto end;
  4357. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4358. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4359. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4360. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4361. goto end;
  4362. /*
  4363. * Must include an EC ciphersuite so that we send supported groups in
  4364. * TLSv1.2
  4365. */
  4366. # ifndef OPENSSL_NO_TLS1_2
  4367. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4368. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4369. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4370. goto end;
  4371. # endif
  4372. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4373. NULL, NULL)))
  4374. goto end;
  4375. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
  4376. || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
  4377. goto end;
  4378. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4379. goto end;
  4380. /*
  4381. * If Handshake succeeds the negotiated kexch alg should be the first one in
  4382. * configured, except in the case of FFDHE groups (idx 13), which are
  4383. * TLSv1.3 only so we expect no shared group to exist.
  4384. */
  4385. if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
  4386. idx == 13 ? 0 : kexch_groups[0]))
  4387. goto end;
  4388. if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
  4389. kexch_name0))
  4390. goto end;
  4391. /* We don't implement RFC 7919 named groups for TLS 1.2. */
  4392. if (idx != 13) {
  4393. if (!TEST_str_eq(SSL_get0_group_name(serverssl), kexch_name0)
  4394. || !TEST_str_eq(SSL_get0_group_name(clientssl), kexch_name0))
  4395. goto end;
  4396. if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
  4397. goto end;
  4398. if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
  4399. goto end;
  4400. }
  4401. testresult = 1;
  4402. end:
  4403. SSL_free(serverssl);
  4404. SSL_free(clientssl);
  4405. SSL_CTX_free(sctx);
  4406. SSL_CTX_free(cctx);
  4407. return testresult;
  4408. }
  4409. # if !defined(OPENSSL_NO_TLS1_2) \
  4410. && !defined(OPENSSL_NO_EC) \
  4411. && !defined(OPENSSL_NO_DH)
  4412. static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
  4413. int isecdhe, int idx)
  4414. {
  4415. int kexch_alg;
  4416. int *kexch_groups = &kexch_alg;
  4417. int numec, numff;
  4418. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4419. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4420. if (isecdhe)
  4421. kexch_alg = ecdhe_kexch_groups[idx];
  4422. else
  4423. kexch_alg = ffdhe_kexch_groups[idx];
  4424. if (clientmulti) {
  4425. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
  4426. return 0;
  4427. if (isecdhe) {
  4428. if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
  4429. numec)))
  4430. return 0;
  4431. } else {
  4432. if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
  4433. numff)))
  4434. return 0;
  4435. }
  4436. } else {
  4437. if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
  4438. return 0;
  4439. if (isecdhe) {
  4440. if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
  4441. numec)))
  4442. return 0;
  4443. } else {
  4444. if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
  4445. numff)))
  4446. return 0;
  4447. }
  4448. }
  4449. return 1;
  4450. }
  4451. /*-
  4452. * Test the SSL_get_negotiated_group() API across a battery of scenarios.
  4453. * Run through both the ECDHE and FFDHE group lists used in the previous
  4454. * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
  4455. * confirming the expected result; then perform a resumption handshake
  4456. * while offering the same group list, and another resumption handshake
  4457. * offering a different group list. The returned value should be the
  4458. * negotiated group for the initial handshake; for TLS 1.3 resumption
  4459. * handshakes the returned value will be negotiated on the resumption
  4460. * handshake itself, but for TLS 1.2 resumption handshakes the value will
  4461. * be cached in the session from the original handshake, regardless of what
  4462. * was offered in the resumption ClientHello.
  4463. *
  4464. * Using E for the number of EC groups and F for the number of FF groups:
  4465. * E tests of ECDHE with TLS 1.3, server only has one group
  4466. * F tests of FFDHE with TLS 1.3, server only has one group
  4467. * E tests of ECDHE with TLS 1.2, server only has one group
  4468. * F tests of FFDHE with TLS 1.2, server only has one group
  4469. * E tests of ECDHE with TLS 1.3, client sends only one group
  4470. * F tests of FFDHE with TLS 1.3, client sends only one group
  4471. * E tests of ECDHE with TLS 1.2, client sends only one group
  4472. * F tests of FFDHE with TLS 1.2, client sends only one group
  4473. */
  4474. static int test_negotiated_group(int idx)
  4475. {
  4476. int clientmulti, istls13, isecdhe, numec, numff, numgroups;
  4477. int expectednid;
  4478. SSL_CTX *sctx = NULL, *cctx = NULL;
  4479. SSL *serverssl = NULL, *clientssl = NULL;
  4480. SSL_SESSION *origsess = NULL;
  4481. int testresult = 0;
  4482. int kexch_alg;
  4483. int max_version = TLS1_3_VERSION;
  4484. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4485. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4486. numgroups = numec + numff;
  4487. clientmulti = (idx < 2 * numgroups);
  4488. idx = idx % (2 * numgroups);
  4489. istls13 = (idx < numgroups);
  4490. idx = idx % numgroups;
  4491. isecdhe = (idx < numec);
  4492. if (!isecdhe)
  4493. idx -= numec;
  4494. /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
  4495. if (isecdhe)
  4496. kexch_alg = ecdhe_kexch_groups[idx];
  4497. else
  4498. kexch_alg = ffdhe_kexch_groups[idx];
  4499. /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
  4500. if (!istls13 && !isecdhe)
  4501. expectednid = NID_undef;
  4502. else
  4503. expectednid = kexch_alg;
  4504. if (is_fips && (kexch_alg == NID_X25519 || kexch_alg == NID_X448))
  4505. return TEST_skip("X25519 and X448 might not be available in fips provider.");
  4506. if (!istls13)
  4507. max_version = TLS1_2_VERSION;
  4508. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4509. TLS_client_method(), TLS1_VERSION,
  4510. max_version, &sctx, &cctx, cert,
  4511. privkey)))
  4512. goto end;
  4513. /*
  4514. * Force (EC)DHE ciphers for TLS 1.2.
  4515. * Be sure to enable auto tmp DH so that FFDHE can succeed.
  4516. */
  4517. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4518. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4519. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4520. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4521. goto end;
  4522. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4523. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4524. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4525. goto end;
  4526. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4527. NULL, NULL)))
  4528. goto end;
  4529. if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
  4530. idx)))
  4531. goto end;
  4532. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4533. goto end;
  4534. /* Initial handshake; always the configured one */
  4535. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4536. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4537. goto end;
  4538. if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
  4539. goto end;
  4540. SSL_shutdown(clientssl);
  4541. SSL_shutdown(serverssl);
  4542. SSL_free(serverssl);
  4543. SSL_free(clientssl);
  4544. serverssl = clientssl = NULL;
  4545. /* First resumption attempt; use the same config as initial handshake */
  4546. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4547. NULL, NULL))
  4548. || !TEST_true(SSL_set_session(clientssl, origsess))
  4549. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4550. isecdhe, idx)))
  4551. goto end;
  4552. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4553. || !TEST_true(SSL_session_reused(clientssl)))
  4554. goto end;
  4555. /* Still had better agree, since nothing changed... */
  4556. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4557. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4558. goto end;
  4559. SSL_shutdown(clientssl);
  4560. SSL_shutdown(serverssl);
  4561. SSL_free(serverssl);
  4562. SSL_free(clientssl);
  4563. serverssl = clientssl = NULL;
  4564. /*-
  4565. * Second resumption attempt
  4566. * The party that picks one group changes it, which we effectuate by
  4567. * changing 'idx' and updating what we expect.
  4568. */
  4569. if (idx == 0)
  4570. idx = 1;
  4571. else
  4572. idx--;
  4573. if (istls13) {
  4574. if (isecdhe)
  4575. expectednid = ecdhe_kexch_groups[idx];
  4576. else
  4577. expectednid = ffdhe_kexch_groups[idx];
  4578. /* Verify that we are changing what we expect. */
  4579. if (!TEST_int_ne(expectednid, kexch_alg))
  4580. goto end;
  4581. } else {
  4582. /* TLS 1.2 only supports named groups for ECDHE. */
  4583. if (isecdhe)
  4584. expectednid = kexch_alg;
  4585. else
  4586. expectednid = 0;
  4587. }
  4588. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4589. NULL, NULL))
  4590. || !TEST_true(SSL_set_session(clientssl, origsess))
  4591. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4592. isecdhe, idx)))
  4593. goto end;
  4594. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4595. || !TEST_true(SSL_session_reused(clientssl)))
  4596. goto end;
  4597. /* Check that we get what we expected */
  4598. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4599. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4600. goto end;
  4601. testresult = 1;
  4602. end:
  4603. SSL_free(serverssl);
  4604. SSL_free(clientssl);
  4605. SSL_CTX_free(sctx);
  4606. SSL_CTX_free(cctx);
  4607. SSL_SESSION_free(origsess);
  4608. return testresult;
  4609. }
  4610. # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
  4611. /*
  4612. * Test TLSv1.3 Cipher Suite
  4613. * Test 0 = Set TLS1.3 cipher on context
  4614. * Test 1 = Set TLS1.3 cipher on SSL
  4615. * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
  4616. * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
  4617. */
  4618. static int test_tls13_ciphersuite(int idx)
  4619. {
  4620. SSL_CTX *sctx = NULL, *cctx = NULL;
  4621. SSL *serverssl = NULL, *clientssl = NULL;
  4622. static const struct {
  4623. const char *ciphername;
  4624. int fipscapable;
  4625. int low_security;
  4626. } t13_ciphers[] = {
  4627. { TLS1_3_RFC_AES_128_GCM_SHA256, 1, 0 },
  4628. { TLS1_3_RFC_AES_256_GCM_SHA384, 1, 0 },
  4629. { TLS1_3_RFC_AES_128_CCM_SHA256, 1, 0 },
  4630. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4631. { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4632. { TLS1_3_RFC_AES_256_GCM_SHA384
  4633. ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4634. # endif
  4635. /* CCM8 ciphers are considered low security due to their short tag */
  4636. { TLS1_3_RFC_AES_128_CCM_8_SHA256
  4637. ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1, 1 }
  4638. };
  4639. const char *t13_cipher = NULL;
  4640. const char *t12_cipher = NULL;
  4641. const char *negotiated_scipher;
  4642. const char *negotiated_ccipher;
  4643. int set_at_ctx = 0;
  4644. int set_at_ssl = 0;
  4645. int testresult = 0;
  4646. int max_ver;
  4647. size_t i;
  4648. switch (idx) {
  4649. case 0:
  4650. set_at_ctx = 1;
  4651. break;
  4652. case 1:
  4653. set_at_ssl = 1;
  4654. break;
  4655. case 2:
  4656. set_at_ctx = 1;
  4657. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4658. break;
  4659. case 3:
  4660. set_at_ssl = 1;
  4661. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4662. break;
  4663. }
  4664. for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
  4665. # ifdef OPENSSL_NO_TLS1_2
  4666. if (max_ver == TLS1_2_VERSION)
  4667. continue;
  4668. # endif
  4669. for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
  4670. if (is_fips && !t13_ciphers[i].fipscapable)
  4671. continue;
  4672. t13_cipher = t13_ciphers[i].ciphername;
  4673. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4674. TLS_client_method(),
  4675. TLS1_VERSION, max_ver,
  4676. &sctx, &cctx, cert, privkey)))
  4677. goto end;
  4678. if (t13_ciphers[i].low_security) {
  4679. SSL_CTX_set_security_level(sctx, 0);
  4680. SSL_CTX_set_security_level(cctx, 0);
  4681. }
  4682. if (set_at_ctx) {
  4683. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
  4684. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
  4685. goto end;
  4686. if (t12_cipher != NULL) {
  4687. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
  4688. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  4689. t12_cipher)))
  4690. goto end;
  4691. }
  4692. }
  4693. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4694. &clientssl, NULL, NULL)))
  4695. goto end;
  4696. if (set_at_ssl) {
  4697. if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
  4698. || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
  4699. goto end;
  4700. if (t12_cipher != NULL) {
  4701. if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
  4702. || !TEST_true(SSL_set_cipher_list(clientssl,
  4703. t12_cipher)))
  4704. goto end;
  4705. }
  4706. }
  4707. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4708. SSL_ERROR_NONE)))
  4709. goto end;
  4710. negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4711. serverssl));
  4712. negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4713. clientssl));
  4714. if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
  4715. goto end;
  4716. /*
  4717. * TEST_strn_eq is used below because t13_cipher can contain
  4718. * multiple ciphersuites
  4719. */
  4720. if (max_ver == TLS1_3_VERSION
  4721. && !TEST_strn_eq(t13_cipher, negotiated_scipher,
  4722. strlen(negotiated_scipher)))
  4723. goto end;
  4724. # ifndef OPENSSL_NO_TLS1_2
  4725. /* Below validation is not done when t12_cipher is NULL */
  4726. if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
  4727. && !TEST_str_eq(t12_cipher, negotiated_scipher))
  4728. goto end;
  4729. # endif
  4730. SSL_free(serverssl);
  4731. serverssl = NULL;
  4732. SSL_free(clientssl);
  4733. clientssl = NULL;
  4734. SSL_CTX_free(sctx);
  4735. sctx = NULL;
  4736. SSL_CTX_free(cctx);
  4737. cctx = NULL;
  4738. }
  4739. }
  4740. testresult = 1;
  4741. end:
  4742. SSL_free(serverssl);
  4743. SSL_free(clientssl);
  4744. SSL_CTX_free(sctx);
  4745. SSL_CTX_free(cctx);
  4746. return testresult;
  4747. }
  4748. /*
  4749. * Test TLSv1.3 PSKs
  4750. * Test 0 = Test new style callbacks
  4751. * Test 1 = Test both new and old style callbacks
  4752. * Test 2 = Test old style callbacks
  4753. * Test 3 = Test old style callbacks with no certificate
  4754. */
  4755. static int test_tls13_psk(int idx)
  4756. {
  4757. SSL_CTX *sctx = NULL, *cctx = NULL;
  4758. SSL *serverssl = NULL, *clientssl = NULL;
  4759. const SSL_CIPHER *cipher = NULL;
  4760. const unsigned char key[] = {
  4761. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4762. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4763. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  4764. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  4765. };
  4766. int testresult = 0;
  4767. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4768. TLS_client_method(), TLS1_VERSION, 0,
  4769. &sctx, &cctx, idx == 3 ? NULL : cert,
  4770. idx == 3 ? NULL : privkey)))
  4771. goto end;
  4772. if (idx != 3) {
  4773. /*
  4774. * We use a ciphersuite with SHA256 to ease testing old style PSK
  4775. * callbacks which will always default to SHA256. This should not be
  4776. * necessary if we have no cert/priv key. In that case the server should
  4777. * prefer SHA256 automatically.
  4778. */
  4779. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4780. "TLS_AES_128_GCM_SHA256")))
  4781. goto end;
  4782. } else {
  4783. /*
  4784. * As noted above the server should prefer SHA256 automatically. However
  4785. * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
  4786. * code works even if we are testing with only the FIPS provider loaded.
  4787. */
  4788. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4789. "TLS_AES_256_GCM_SHA384:"
  4790. "TLS_AES_128_GCM_SHA256")))
  4791. goto end;
  4792. }
  4793. /*
  4794. * Test 0: New style callbacks only
  4795. * Test 1: New and old style callbacks (only the new ones should be used)
  4796. * Test 2: Old style callbacks only
  4797. */
  4798. if (idx == 0 || idx == 1) {
  4799. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  4800. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  4801. }
  4802. #ifndef OPENSSL_NO_PSK
  4803. if (idx >= 1) {
  4804. SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
  4805. SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
  4806. }
  4807. #endif
  4808. srvid = pskid;
  4809. use_session_cb_cnt = 0;
  4810. find_session_cb_cnt = 0;
  4811. psk_client_cb_cnt = 0;
  4812. psk_server_cb_cnt = 0;
  4813. if (idx != 3) {
  4814. /*
  4815. * Check we can create a connection if callback decides not to send a
  4816. * PSK
  4817. */
  4818. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4819. NULL, NULL))
  4820. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4821. SSL_ERROR_NONE))
  4822. || !TEST_false(SSL_session_reused(clientssl))
  4823. || !TEST_false(SSL_session_reused(serverssl)))
  4824. goto end;
  4825. if (idx == 0 || idx == 1) {
  4826. if (!TEST_true(use_session_cb_cnt == 1)
  4827. || !TEST_true(find_session_cb_cnt == 0)
  4828. /*
  4829. * If no old style callback then below should be 0
  4830. * otherwise 1
  4831. */
  4832. || !TEST_true(psk_client_cb_cnt == idx)
  4833. || !TEST_true(psk_server_cb_cnt == 0))
  4834. goto end;
  4835. } else {
  4836. if (!TEST_true(use_session_cb_cnt == 0)
  4837. || !TEST_true(find_session_cb_cnt == 0)
  4838. || !TEST_true(psk_client_cb_cnt == 1)
  4839. || !TEST_true(psk_server_cb_cnt == 0))
  4840. goto end;
  4841. }
  4842. shutdown_ssl_connection(serverssl, clientssl);
  4843. serverssl = clientssl = NULL;
  4844. use_session_cb_cnt = psk_client_cb_cnt = 0;
  4845. }
  4846. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4847. NULL, NULL)))
  4848. goto end;
  4849. /* Create the PSK */
  4850. cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
  4851. clientpsk = SSL_SESSION_new();
  4852. if (!TEST_ptr(clientpsk)
  4853. || !TEST_ptr(cipher)
  4854. || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
  4855. sizeof(key)))
  4856. || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
  4857. || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
  4858. TLS1_3_VERSION))
  4859. || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
  4860. goto end;
  4861. serverpsk = clientpsk;
  4862. /* Check we can create a connection and the PSK is used */
  4863. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4864. || !TEST_true(SSL_session_reused(clientssl))
  4865. || !TEST_true(SSL_session_reused(serverssl)))
  4866. goto end;
  4867. if (idx == 0 || idx == 1) {
  4868. if (!TEST_true(use_session_cb_cnt == 1)
  4869. || !TEST_true(find_session_cb_cnt == 1)
  4870. || !TEST_true(psk_client_cb_cnt == 0)
  4871. || !TEST_true(psk_server_cb_cnt == 0))
  4872. goto end;
  4873. } else {
  4874. if (!TEST_true(use_session_cb_cnt == 0)
  4875. || !TEST_true(find_session_cb_cnt == 0)
  4876. || !TEST_true(psk_client_cb_cnt == 1)
  4877. || !TEST_true(psk_server_cb_cnt == 1))
  4878. goto end;
  4879. }
  4880. shutdown_ssl_connection(serverssl, clientssl);
  4881. serverssl = clientssl = NULL;
  4882. use_session_cb_cnt = find_session_cb_cnt = 0;
  4883. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4884. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4885. NULL, NULL)))
  4886. goto end;
  4887. /* Force an HRR */
  4888. #if defined(OPENSSL_NO_EC)
  4889. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  4890. goto end;
  4891. #else
  4892. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-384")))
  4893. goto end;
  4894. #endif
  4895. /*
  4896. * Check we can create a connection, the PSK is used and the callbacks are
  4897. * called twice.
  4898. */
  4899. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4900. || !TEST_true(SSL_session_reused(clientssl))
  4901. || !TEST_true(SSL_session_reused(serverssl)))
  4902. goto end;
  4903. if (idx == 0 || idx == 1) {
  4904. if (!TEST_true(use_session_cb_cnt == 2)
  4905. || !TEST_true(find_session_cb_cnt == 2)
  4906. || !TEST_true(psk_client_cb_cnt == 0)
  4907. || !TEST_true(psk_server_cb_cnt == 0))
  4908. goto end;
  4909. } else {
  4910. if (!TEST_true(use_session_cb_cnt == 0)
  4911. || !TEST_true(find_session_cb_cnt == 0)
  4912. || !TEST_true(psk_client_cb_cnt == 2)
  4913. || !TEST_true(psk_server_cb_cnt == 2))
  4914. goto end;
  4915. }
  4916. shutdown_ssl_connection(serverssl, clientssl);
  4917. serverssl = clientssl = NULL;
  4918. use_session_cb_cnt = find_session_cb_cnt = 0;
  4919. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4920. if (idx != 3) {
  4921. /*
  4922. * Check that if the server rejects the PSK we can still connect, but with
  4923. * a full handshake
  4924. */
  4925. srvid = "Dummy Identity";
  4926. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4927. NULL, NULL))
  4928. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4929. SSL_ERROR_NONE))
  4930. || !TEST_false(SSL_session_reused(clientssl))
  4931. || !TEST_false(SSL_session_reused(serverssl)))
  4932. goto end;
  4933. if (idx == 0 || idx == 1) {
  4934. if (!TEST_true(use_session_cb_cnt == 1)
  4935. || !TEST_true(find_session_cb_cnt == 1)
  4936. || !TEST_true(psk_client_cb_cnt == 0)
  4937. /*
  4938. * If no old style callback then below should be 0
  4939. * otherwise 1
  4940. */
  4941. || !TEST_true(psk_server_cb_cnt == idx))
  4942. goto end;
  4943. } else {
  4944. if (!TEST_true(use_session_cb_cnt == 0)
  4945. || !TEST_true(find_session_cb_cnt == 0)
  4946. || !TEST_true(psk_client_cb_cnt == 1)
  4947. || !TEST_true(psk_server_cb_cnt == 1))
  4948. goto end;
  4949. }
  4950. shutdown_ssl_connection(serverssl, clientssl);
  4951. serverssl = clientssl = NULL;
  4952. }
  4953. testresult = 1;
  4954. end:
  4955. SSL_SESSION_free(clientpsk);
  4956. SSL_SESSION_free(serverpsk);
  4957. clientpsk = serverpsk = NULL;
  4958. SSL_free(serverssl);
  4959. SSL_free(clientssl);
  4960. SSL_CTX_free(sctx);
  4961. SSL_CTX_free(cctx);
  4962. return testresult;
  4963. }
  4964. #ifndef OSSL_NO_USABLE_TLS1_3
  4965. /*
  4966. * Test TLS1.3 connection establishment succeeds with various configurations of
  4967. * the options `SSL_OP_ALLOW_NO_DHE_KEX` and `SSL_OP_PREFER_NO_DHE_KEX`.
  4968. * The verification of whether the right KEX mode is chosen is not covered by
  4969. * this test but by `test_tls13kexmodes`.
  4970. *
  4971. * Tests (idx & 1): Server has `SSL_OP_ALLOW_NO_DHE_KEX` set.
  4972. * Tests (idx & 2): Server has `SSL_OP_PREFER_NO_DHE_KEX` set.
  4973. * Tests (idx & 4): Client has `SSL_OP_ALLOW_NO_DHE_KEX` set.
  4974. */
  4975. static int test_tls13_no_dhe_kex(const int idx)
  4976. {
  4977. SSL_CTX *sctx = NULL, *cctx = NULL;
  4978. SSL *serverssl = NULL, *clientssl = NULL;
  4979. int testresult = 0;
  4980. size_t j;
  4981. SSL_SESSION *saved_session;
  4982. int server_allow_no_dhe = (idx & 1) != 0;
  4983. int server_prefer_no_dhe = (idx & 2) != 0;
  4984. int client_allow_no_dhe = (idx & 4) != 0;
  4985. uint64_t server_options = 0
  4986. | (server_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0)
  4987. | (server_prefer_no_dhe ? SSL_OP_PREFER_NO_DHE_KEX : 0);
  4988. uint64_t client_options = 0
  4989. | (client_allow_no_dhe ? SSL_OP_ALLOW_NO_DHE_KEX : 0);
  4990. new_called = 0;
  4991. do_cache = 1;
  4992. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4993. TLS_client_method(), TLS1_3_VERSION, 0,
  4994. &sctx, &cctx, cert, privkey)))
  4995. goto end;
  4996. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  4997. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  4998. SSL_CTX_set_options(sctx, server_options);
  4999. SSL_CTX_set_options(cctx, client_options);
  5000. SSL_CTX_sess_set_new_cb(cctx, new_cachesession_cb);
  5001. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5002. &clientssl, NULL, NULL)))
  5003. goto end;
  5004. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5005. SSL_ERROR_NONE))
  5006. /* Check we got the number of tickets we were expecting */
  5007. || !TEST_int_eq(2, new_called))
  5008. goto end;
  5009. /* We'll reuse the last ticket. */
  5010. saved_session = sesscache[new_called - 1];
  5011. SSL_shutdown(clientssl);
  5012. SSL_shutdown(serverssl);
  5013. SSL_free(serverssl);
  5014. SSL_free(clientssl);
  5015. SSL_CTX_free(cctx);
  5016. clientssl = serverssl = NULL;
  5017. cctx = NULL;
  5018. /*
  5019. * Now we resume with the last ticket we created.
  5020. */
  5021. /* The server context already exists, so we only create the client. */
  5022. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5023. TLS_client_method(), TLS1_3_VERSION, 0,
  5024. NULL, &cctx, cert, privkey)))
  5025. goto end;
  5026. SSL_CTX_set_options(cctx, client_options);
  5027. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5028. &clientssl, NULL, NULL))
  5029. || !TEST_true(SSL_set_session(clientssl, saved_session)))
  5030. goto end;
  5031. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5032. SSL_ERROR_NONE)))
  5033. goto end;
  5034. /*
  5035. * Make sure, the session was resumed.
  5036. */
  5037. if (!TEST_true(SSL_session_reused(clientssl)))
  5038. goto end;
  5039. SSL_shutdown(clientssl);
  5040. SSL_shutdown(serverssl);
  5041. testresult = 1;
  5042. end:
  5043. SSL_free(serverssl);
  5044. SSL_free(clientssl);
  5045. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  5046. SSL_SESSION_free(sesscache[j]);
  5047. sesscache[j] = NULL;
  5048. }
  5049. SSL_CTX_free(sctx);
  5050. SSL_CTX_free(cctx);
  5051. return testresult;
  5052. }
  5053. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5054. static unsigned char cookie_magic_value[] = "cookie magic";
  5055. static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  5056. unsigned int *cookie_len)
  5057. {
  5058. /*
  5059. * Not suitable as a real cookie generation function but good enough for
  5060. * testing!
  5061. */
  5062. memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
  5063. *cookie_len = sizeof(cookie_magic_value) - 1;
  5064. return 1;
  5065. }
  5066. static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  5067. unsigned int cookie_len)
  5068. {
  5069. if (cookie_len == sizeof(cookie_magic_value) - 1
  5070. && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
  5071. return 1;
  5072. return 0;
  5073. }
  5074. static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  5075. size_t *cookie_len)
  5076. {
  5077. unsigned int temp;
  5078. int res = generate_cookie_callback(ssl, cookie, &temp);
  5079. *cookie_len = temp;
  5080. return res;
  5081. }
  5082. static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  5083. size_t cookie_len)
  5084. {
  5085. return verify_cookie_callback(ssl, cookie, cookie_len);
  5086. }
  5087. static int test_stateless(void)
  5088. {
  5089. SSL_CTX *sctx = NULL, *cctx = NULL;
  5090. SSL *serverssl = NULL, *clientssl = NULL;
  5091. int testresult = 0;
  5092. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5093. TLS_client_method(), TLS1_VERSION, 0,
  5094. &sctx, &cctx, cert, privkey)))
  5095. goto end;
  5096. /* The arrival of CCS messages can confuse the test */
  5097. SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  5098. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5099. NULL, NULL))
  5100. /* Send the first ClientHello */
  5101. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5102. SSL_ERROR_WANT_READ))
  5103. /*
  5104. * This should fail with a -1 return because we have no callbacks
  5105. * set up
  5106. */
  5107. || !TEST_int_eq(SSL_stateless(serverssl), -1))
  5108. goto end;
  5109. /* Fatal error so abandon the connection from this client */
  5110. SSL_free(clientssl);
  5111. clientssl = NULL;
  5112. /* Set up the cookie generation and verification callbacks */
  5113. SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
  5114. SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
  5115. /*
  5116. * Create a new connection from the client (we can reuse the server SSL
  5117. * object).
  5118. */
  5119. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5120. NULL, NULL))
  5121. /* Send the first ClientHello */
  5122. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5123. SSL_ERROR_WANT_READ))
  5124. /* This should fail because there is no cookie */
  5125. || !TEST_int_eq(SSL_stateless(serverssl), 0))
  5126. goto end;
  5127. /* Abandon the connection from this client */
  5128. SSL_free(clientssl);
  5129. clientssl = NULL;
  5130. /*
  5131. * Now create a connection from a new client but with the same server SSL
  5132. * object
  5133. */
  5134. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5135. NULL, NULL))
  5136. /* Send the first ClientHello */
  5137. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5138. SSL_ERROR_WANT_READ))
  5139. /* This should fail because there is no cookie */
  5140. || !TEST_int_eq(SSL_stateless(serverssl), 0)
  5141. /* Send the second ClientHello */
  5142. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  5143. SSL_ERROR_WANT_READ))
  5144. /* This should succeed because a cookie is now present */
  5145. || !TEST_int_eq(SSL_stateless(serverssl), 1)
  5146. /* Complete the connection */
  5147. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5148. SSL_ERROR_NONE)))
  5149. goto end;
  5150. shutdown_ssl_connection(serverssl, clientssl);
  5151. serverssl = clientssl = NULL;
  5152. testresult = 1;
  5153. end:
  5154. SSL_free(serverssl);
  5155. SSL_free(clientssl);
  5156. SSL_CTX_free(sctx);
  5157. SSL_CTX_free(cctx);
  5158. return testresult;
  5159. }
  5160. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5161. static int clntaddoldcb = 0;
  5162. static int clntparseoldcb = 0;
  5163. static int srvaddoldcb = 0;
  5164. static int srvparseoldcb = 0;
  5165. static int clntaddnewcb = 0;
  5166. static int clntparsenewcb = 0;
  5167. static int srvaddnewcb = 0;
  5168. static int srvparsenewcb = 0;
  5169. static int snicb = 0;
  5170. #define TEST_EXT_TYPE1 0xff00
  5171. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  5172. size_t *outlen, int *al, void *add_arg)
  5173. {
  5174. int *server = (int *)add_arg;
  5175. unsigned char *data;
  5176. if (SSL_is_server(s))
  5177. srvaddoldcb++;
  5178. else
  5179. clntaddoldcb++;
  5180. if (*server != SSL_is_server(s)
  5181. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  5182. return -1;
  5183. *data = 1;
  5184. *out = data;
  5185. *outlen = sizeof(char);
  5186. return 1;
  5187. }
  5188. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  5189. void *add_arg)
  5190. {
  5191. OPENSSL_free((unsigned char *)out);
  5192. }
  5193. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  5194. size_t inlen, int *al, void *parse_arg)
  5195. {
  5196. int *server = (int *)parse_arg;
  5197. if (SSL_is_server(s))
  5198. srvparseoldcb++;
  5199. else
  5200. clntparseoldcb++;
  5201. if (*server != SSL_is_server(s)
  5202. || inlen != sizeof(char)
  5203. || *in != 1)
  5204. return -1;
  5205. return 1;
  5206. }
  5207. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5208. const unsigned char **out, size_t *outlen, X509 *x,
  5209. size_t chainidx, int *al, void *add_arg)
  5210. {
  5211. int *server = (int *)add_arg;
  5212. unsigned char *data;
  5213. if (SSL_is_server(s))
  5214. srvaddnewcb++;
  5215. else
  5216. clntaddnewcb++;
  5217. if (*server != SSL_is_server(s)
  5218. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  5219. return -1;
  5220. *data = 1;
  5221. *out = data;
  5222. *outlen = sizeof(*data);
  5223. return 1;
  5224. }
  5225. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5226. const unsigned char *out, void *add_arg)
  5227. {
  5228. OPENSSL_free((unsigned char *)out);
  5229. }
  5230. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  5231. const unsigned char *in, size_t inlen, X509 *x,
  5232. size_t chainidx, int *al, void *parse_arg)
  5233. {
  5234. int *server = (int *)parse_arg;
  5235. if (SSL_is_server(s))
  5236. srvparsenewcb++;
  5237. else
  5238. clntparsenewcb++;
  5239. if (*server != SSL_is_server(s)
  5240. || inlen != sizeof(char) || *in != 1)
  5241. return -1;
  5242. return 1;
  5243. }
  5244. static int sni_cb(SSL *s, int *al, void *arg)
  5245. {
  5246. SSL_CTX *ctx = (SSL_CTX *)arg;
  5247. if (SSL_set_SSL_CTX(s, ctx) == NULL) {
  5248. *al = SSL_AD_INTERNAL_ERROR;
  5249. return SSL_TLSEXT_ERR_ALERT_FATAL;
  5250. }
  5251. snicb++;
  5252. return SSL_TLSEXT_ERR_OK;
  5253. }
  5254. static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  5255. {
  5256. return 1;
  5257. }
  5258. /*
  5259. * Custom call back tests.
  5260. * Test 0: Old style callbacks in TLSv1.2
  5261. * Test 1: New style callbacks in TLSv1.2
  5262. * Test 2: New style callbacks in TLSv1.2 with SNI
  5263. * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
  5264. * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  5265. * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
  5266. */
  5267. static int test_custom_exts(int tst)
  5268. {
  5269. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5270. SSL *clientssl = NULL, *serverssl = NULL;
  5271. int testresult = 0;
  5272. static int server = 1;
  5273. static int client = 0;
  5274. SSL_SESSION *sess = NULL;
  5275. unsigned int context;
  5276. #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  5277. /* Skip tests for TLSv1.2 and below in this case */
  5278. if (tst < 3)
  5279. return 1;
  5280. #endif
  5281. /* Reset callback counters */
  5282. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  5283. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  5284. snicb = 0;
  5285. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5286. TLS_client_method(), TLS1_VERSION, 0,
  5287. &sctx, &cctx, cert, privkey)))
  5288. goto end;
  5289. if (tst == 2
  5290. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
  5291. TLS1_VERSION, 0,
  5292. &sctx2, NULL, cert, privkey)))
  5293. goto end;
  5294. if (tst < 3) {
  5295. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  5296. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  5297. if (sctx2 != NULL)
  5298. SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
  5299. }
  5300. if (tst == 5) {
  5301. context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  5302. | SSL_EXT_TLS1_3_CERTIFICATE;
  5303. SSL_CTX_set_verify(sctx,
  5304. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  5305. verify_cb);
  5306. if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
  5307. SSL_FILETYPE_PEM), 1)
  5308. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
  5309. SSL_FILETYPE_PEM), 1)
  5310. || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
  5311. goto end;
  5312. } else if (tst == 4) {
  5313. context = SSL_EXT_CLIENT_HELLO
  5314. | SSL_EXT_TLS1_2_SERVER_HELLO
  5315. | SSL_EXT_TLS1_3_SERVER_HELLO
  5316. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  5317. | SSL_EXT_TLS1_3_CERTIFICATE
  5318. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  5319. } else {
  5320. context = SSL_EXT_CLIENT_HELLO
  5321. | SSL_EXT_TLS1_2_SERVER_HELLO
  5322. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  5323. }
  5324. /* Create a client side custom extension */
  5325. if (tst == 0) {
  5326. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  5327. old_add_cb, old_free_cb,
  5328. &client, old_parse_cb,
  5329. &client)))
  5330. goto end;
  5331. } else {
  5332. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  5333. new_add_cb, new_free_cb,
  5334. &client, new_parse_cb, &client)))
  5335. goto end;
  5336. }
  5337. /* Should not be able to add duplicates */
  5338. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  5339. old_add_cb, old_free_cb,
  5340. &client, old_parse_cb,
  5341. &client))
  5342. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  5343. context, new_add_cb,
  5344. new_free_cb, &client,
  5345. new_parse_cb, &client)))
  5346. goto end;
  5347. /* Create a server side custom extension */
  5348. if (tst == 0) {
  5349. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  5350. old_add_cb, old_free_cb,
  5351. &server, old_parse_cb,
  5352. &server)))
  5353. goto end;
  5354. } else {
  5355. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  5356. new_add_cb, new_free_cb,
  5357. &server, new_parse_cb, &server)))
  5358. goto end;
  5359. if (sctx2 != NULL
  5360. && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
  5361. context, new_add_cb,
  5362. new_free_cb, &server,
  5363. new_parse_cb, &server)))
  5364. goto end;
  5365. }
  5366. /* Should not be able to add duplicates */
  5367. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  5368. old_add_cb, old_free_cb,
  5369. &server, old_parse_cb,
  5370. &server))
  5371. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  5372. context, new_add_cb,
  5373. new_free_cb, &server,
  5374. new_parse_cb, &server)))
  5375. goto end;
  5376. if (tst == 2) {
  5377. /* Set up SNI */
  5378. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  5379. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  5380. goto end;
  5381. }
  5382. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5383. &clientssl, NULL, NULL))
  5384. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5385. SSL_ERROR_NONE)))
  5386. goto end;
  5387. if (tst == 0) {
  5388. if (clntaddoldcb != 1
  5389. || clntparseoldcb != 1
  5390. || srvaddoldcb != 1
  5391. || srvparseoldcb != 1)
  5392. goto end;
  5393. } else if (tst == 1 || tst == 2 || tst == 3) {
  5394. if (clntaddnewcb != 1
  5395. || clntparsenewcb != 1
  5396. || srvaddnewcb != 1
  5397. || srvparsenewcb != 1
  5398. || (tst != 2 && snicb != 0)
  5399. || (tst == 2 && snicb != 1))
  5400. goto end;
  5401. } else if (tst == 5) {
  5402. if (clntaddnewcb != 1
  5403. || clntparsenewcb != 1
  5404. || srvaddnewcb != 1
  5405. || srvparsenewcb != 1)
  5406. goto end;
  5407. } else {
  5408. /* In this case there 2 NewSessionTicket messages created */
  5409. if (clntaddnewcb != 1
  5410. || clntparsenewcb != 5
  5411. || srvaddnewcb != 5
  5412. || srvparsenewcb != 1)
  5413. goto end;
  5414. }
  5415. sess = SSL_get1_session(clientssl);
  5416. SSL_shutdown(clientssl);
  5417. SSL_shutdown(serverssl);
  5418. SSL_free(serverssl);
  5419. SSL_free(clientssl);
  5420. serverssl = clientssl = NULL;
  5421. if (tst == 3 || tst == 5) {
  5422. /* We don't bother with the resumption aspects for these tests */
  5423. testresult = 1;
  5424. goto end;
  5425. }
  5426. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5427. NULL, NULL))
  5428. || !TEST_true(SSL_set_session(clientssl, sess))
  5429. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5430. SSL_ERROR_NONE)))
  5431. goto end;
  5432. /*
  5433. * For a resumed session we expect to add the ClientHello extension. For the
  5434. * old style callbacks we ignore it on the server side because they set
  5435. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  5436. * them.
  5437. */
  5438. if (tst == 0) {
  5439. if (clntaddoldcb != 2
  5440. || clntparseoldcb != 1
  5441. || srvaddoldcb != 1
  5442. || srvparseoldcb != 1)
  5443. goto end;
  5444. } else if (tst == 1 || tst == 2 || tst == 3) {
  5445. if (clntaddnewcb != 2
  5446. || clntparsenewcb != 2
  5447. || srvaddnewcb != 2
  5448. || srvparsenewcb != 2)
  5449. goto end;
  5450. } else {
  5451. /*
  5452. * No Certificate message extensions in the resumption handshake,
  5453. * 2 NewSessionTickets in the initial handshake, 1 in the resumption
  5454. */
  5455. if (clntaddnewcb != 2
  5456. || clntparsenewcb != 8
  5457. || srvaddnewcb != 8
  5458. || srvparsenewcb != 2)
  5459. goto end;
  5460. }
  5461. testresult = 1;
  5462. end:
  5463. SSL_SESSION_free(sess);
  5464. SSL_free(serverssl);
  5465. SSL_free(clientssl);
  5466. SSL_CTX_free(sctx2);
  5467. SSL_CTX_free(sctx);
  5468. SSL_CTX_free(cctx);
  5469. return testresult;
  5470. }
  5471. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  5472. #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
  5473. | SSL_EXT_CLIENT_HELLO \
  5474. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5475. | SSL_EXT_IGNORE_ON_RESUMPTION)
  5476. #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
  5477. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5478. | SSL_EXT_CLIENT_HELLO)
  5479. #define SERVERINFO_CUSTOM \
  5480. 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
  5481. 0x00, 0x03, \
  5482. 0x04, 0x05, 0x06 \
  5483. static const unsigned char serverinfo_custom_tls13[] = {
  5484. 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
  5485. SERVERINFO_CUSTOM
  5486. };
  5487. static const unsigned char serverinfo_custom_v2[] = {
  5488. 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff,
  5489. SERVERINFO_CUSTOM
  5490. };
  5491. static const unsigned char serverinfo_custom_v1[] = {
  5492. SERVERINFO_CUSTOM
  5493. };
  5494. static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
  5495. static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
  5496. static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
  5497. static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
  5498. unsigned int context,
  5499. const unsigned char *in,
  5500. size_t inlen, X509 *x,
  5501. size_t chainidx, int *al,
  5502. void *parse_arg)
  5503. {
  5504. const size_t len = serverinfo_custom_v1_len;
  5505. const unsigned char *si = &serverinfo_custom_v1[len - 3];
  5506. int *p_cb_result = (int*)parse_arg;
  5507. *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
  5508. return 1;
  5509. }
  5510. static int test_serverinfo_custom(const int idx)
  5511. {
  5512. SSL_CTX *sctx = NULL, *cctx = NULL;
  5513. SSL *clientssl = NULL, *serverssl = NULL;
  5514. int testresult = 0;
  5515. int cb_result = 0;
  5516. /*
  5517. * Following variables are set in the switch statement
  5518. * according to the test iteration.
  5519. * Default values do not make much sense: test would fail with them.
  5520. */
  5521. int serverinfo_version = 0;
  5522. int protocol_version = 0;
  5523. unsigned int extension_context = 0;
  5524. const unsigned char *si = NULL;
  5525. size_t si_len = 0;
  5526. const int call_use_serverinfo_ex = idx > 0;
  5527. switch (idx) {
  5528. case 0: /* FALLTHROUGH */
  5529. case 1:
  5530. serverinfo_version = SSL_SERVERINFOV1;
  5531. protocol_version = TLS1_2_VERSION;
  5532. extension_context = SYNTHV1CONTEXT;
  5533. si = serverinfo_custom_v1;
  5534. si_len = serverinfo_custom_v1_len;
  5535. break;
  5536. case 2:
  5537. serverinfo_version = SSL_SERVERINFOV2;
  5538. protocol_version = TLS1_2_VERSION;
  5539. extension_context = SYNTHV1CONTEXT;
  5540. si = serverinfo_custom_v2;
  5541. si_len = serverinfo_custom_v2_len;
  5542. break;
  5543. case 3:
  5544. serverinfo_version = SSL_SERVERINFOV2;
  5545. protocol_version = TLS1_3_VERSION;
  5546. extension_context = TLS13CONTEXT;
  5547. si = serverinfo_custom_tls13;
  5548. si_len = serverinfo_custom_tls13_len;
  5549. break;
  5550. }
  5551. if (!TEST_true(create_ssl_ctx_pair(libctx,
  5552. TLS_method(),
  5553. TLS_method(),
  5554. protocol_version,
  5555. protocol_version,
  5556. &sctx, &cctx, cert, privkey)))
  5557. goto end;
  5558. if (call_use_serverinfo_ex) {
  5559. if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
  5560. si, si_len)))
  5561. goto end;
  5562. } else {
  5563. if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
  5564. goto end;
  5565. }
  5566. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
  5567. extension_context,
  5568. NULL, NULL, NULL,
  5569. serverinfo_custom_parse_cb,
  5570. &cb_result))
  5571. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5572. NULL, NULL))
  5573. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5574. SSL_ERROR_NONE))
  5575. || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
  5576. goto end;
  5577. if (!TEST_true(cb_result))
  5578. goto end;
  5579. testresult = 1;
  5580. end:
  5581. SSL_free(serverssl);
  5582. SSL_free(clientssl);
  5583. SSL_CTX_free(sctx);
  5584. SSL_CTX_free(cctx);
  5585. return testresult;
  5586. }
  5587. #endif
  5588. /*
  5589. * Test that SSL_export_keying_material() produces expected results. There are
  5590. * no test vectors so all we do is test that both sides of the communication
  5591. * produce the same results for different protocol versions.
  5592. */
  5593. #define SMALL_LABEL_LEN 10
  5594. #define LONG_LABEL_LEN 249
  5595. static int test_export_key_mat(int tst)
  5596. {
  5597. int testresult = 0;
  5598. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5599. SSL *clientssl = NULL, *serverssl = NULL;
  5600. const char label[LONG_LABEL_LEN + 1] = "test label";
  5601. const unsigned char context[] = "context";
  5602. const unsigned char *emptycontext = NULL;
  5603. unsigned char longcontext[1280];
  5604. int test_longcontext = fips_provider_version_ge(libctx, 3, 3, 0);
  5605. unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80], ckeymat4[80];
  5606. unsigned char skeymat1[80], skeymat2[80], skeymat3[80], skeymat4[80];
  5607. size_t labellen;
  5608. const int protocols[] = {
  5609. TLS1_VERSION,
  5610. TLS1_1_VERSION,
  5611. TLS1_2_VERSION,
  5612. TLS1_3_VERSION,
  5613. TLS1_3_VERSION,
  5614. TLS1_3_VERSION
  5615. };
  5616. #ifdef OPENSSL_NO_TLS1
  5617. if (tst == 0)
  5618. return 1;
  5619. #endif
  5620. #ifdef OPENSSL_NO_TLS1_1
  5621. if (tst == 1)
  5622. return 1;
  5623. #endif
  5624. if (is_fips && (tst == 0 || tst == 1))
  5625. return 1;
  5626. #ifdef OPENSSL_NO_TLS1_2
  5627. if (tst == 2)
  5628. return 1;
  5629. #endif
  5630. #ifdef OSSL_NO_USABLE_TLS1_3
  5631. if (tst >= 3)
  5632. return 1;
  5633. #endif
  5634. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5635. TLS_client_method(), TLS1_VERSION, 0,
  5636. &sctx, &cctx, cert, privkey)))
  5637. goto end;
  5638. OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
  5639. SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
  5640. SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
  5641. if ((protocols[tst] < TLS1_2_VERSION) &&
  5642. (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
  5643. || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
  5644. goto end;
  5645. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5646. NULL)))
  5647. goto end;
  5648. /*
  5649. * Premature call of SSL_export_keying_material should just fail.
  5650. */
  5651. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5652. sizeof(ckeymat1), label,
  5653. SMALL_LABEL_LEN + 1, context,
  5654. sizeof(context) - 1, 1), 0))
  5655. goto end;
  5656. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5657. SSL_ERROR_NONE)))
  5658. goto end;
  5659. if (tst == 5) {
  5660. /*
  5661. * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
  5662. * go over that.
  5663. */
  5664. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5665. sizeof(ckeymat1), label,
  5666. LONG_LABEL_LEN + 1, context,
  5667. sizeof(context) - 1, 1), 0))
  5668. goto end;
  5669. testresult = 1;
  5670. goto end;
  5671. } else if (tst == 4) {
  5672. labellen = LONG_LABEL_LEN;
  5673. } else {
  5674. labellen = SMALL_LABEL_LEN;
  5675. }
  5676. memset(longcontext, 1, sizeof(longcontext));
  5677. if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
  5678. sizeof(ckeymat1), label,
  5679. labellen, context,
  5680. sizeof(context) - 1, 1), 1)
  5681. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
  5682. sizeof(ckeymat2), label,
  5683. labellen,
  5684. emptycontext,
  5685. 0, 1), 1)
  5686. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
  5687. sizeof(ckeymat3), label,
  5688. labellen,
  5689. NULL, 0, 0), 1)
  5690. || (test_longcontext
  5691. && !TEST_int_eq(SSL_export_keying_material(clientssl,
  5692. ckeymat4,
  5693. sizeof(ckeymat4), label,
  5694. labellen,
  5695. longcontext,
  5696. sizeof(longcontext), 1),
  5697. 1))
  5698. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
  5699. sizeof(skeymat1), label,
  5700. labellen,
  5701. context,
  5702. sizeof(context) -1, 1),
  5703. 1)
  5704. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
  5705. sizeof(skeymat2), label,
  5706. labellen,
  5707. emptycontext,
  5708. 0, 1), 1)
  5709. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
  5710. sizeof(skeymat3), label,
  5711. labellen,
  5712. NULL, 0, 0), 1)
  5713. || (test_longcontext
  5714. && !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat4,
  5715. sizeof(skeymat4), label,
  5716. labellen,
  5717. longcontext,
  5718. sizeof(longcontext), 1),
  5719. 1))
  5720. /*
  5721. * Check that both sides created the same key material with the
  5722. * same context.
  5723. */
  5724. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5725. sizeof(skeymat1))
  5726. /*
  5727. * Check that both sides created the same key material with an
  5728. * empty context.
  5729. */
  5730. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5731. sizeof(skeymat2))
  5732. /*
  5733. * Check that both sides created the same key material without a
  5734. * context.
  5735. */
  5736. || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
  5737. sizeof(skeymat3))
  5738. /*
  5739. * Check that both sides created the same key material with a
  5740. * long context.
  5741. */
  5742. || (test_longcontext
  5743. && !TEST_mem_eq(ckeymat4, sizeof(ckeymat4), skeymat4,
  5744. sizeof(skeymat4)))
  5745. /* Different contexts should produce different results */
  5746. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5747. sizeof(ckeymat2)))
  5748. goto end;
  5749. /*
  5750. * Check that an empty context and no context produce different results in
  5751. * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
  5752. */
  5753. if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5754. sizeof(ckeymat3)))
  5755. || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5756. sizeof(ckeymat3))))
  5757. goto end;
  5758. testresult = 1;
  5759. end:
  5760. SSL_free(serverssl);
  5761. SSL_free(clientssl);
  5762. SSL_CTX_free(sctx2);
  5763. SSL_CTX_free(sctx);
  5764. SSL_CTX_free(cctx);
  5765. return testresult;
  5766. }
  5767. #ifndef OSSL_NO_USABLE_TLS1_3
  5768. /*
  5769. * Test that SSL_export_keying_material_early() produces expected
  5770. * results. There are no test vectors so all we do is test that both
  5771. * sides of the communication produce the same results for different
  5772. * protocol versions.
  5773. */
  5774. static int test_export_key_mat_early(int idx)
  5775. {
  5776. static const char label[] = "test label";
  5777. static const unsigned char context[] = "context";
  5778. int testresult = 0;
  5779. SSL_CTX *cctx = NULL, *sctx = NULL;
  5780. SSL *clientssl = NULL, *serverssl = NULL;
  5781. SSL_SESSION *sess = NULL;
  5782. const unsigned char *emptycontext = NULL;
  5783. unsigned char ckeymat1[80], ckeymat2[80];
  5784. unsigned char skeymat1[80], skeymat2[80];
  5785. unsigned char buf[1];
  5786. size_t readbytes, written;
  5787. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
  5788. &sess, idx, SHA384_DIGEST_LENGTH)))
  5789. goto end;
  5790. /* Here writing 0 length early data is enough. */
  5791. if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  5792. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  5793. &readbytes),
  5794. SSL_READ_EARLY_DATA_ERROR)
  5795. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  5796. SSL_EARLY_DATA_ACCEPTED))
  5797. goto end;
  5798. if (!TEST_int_eq(SSL_export_keying_material_early(
  5799. clientssl, ckeymat1, sizeof(ckeymat1), label,
  5800. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5801. || !TEST_int_eq(SSL_export_keying_material_early(
  5802. clientssl, ckeymat2, sizeof(ckeymat2), label,
  5803. sizeof(label) - 1, emptycontext, 0), 1)
  5804. || !TEST_int_eq(SSL_export_keying_material_early(
  5805. serverssl, skeymat1, sizeof(skeymat1), label,
  5806. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5807. || !TEST_int_eq(SSL_export_keying_material_early(
  5808. serverssl, skeymat2, sizeof(skeymat2), label,
  5809. sizeof(label) - 1, emptycontext, 0), 1)
  5810. /*
  5811. * Check that both sides created the same key material with the
  5812. * same context.
  5813. */
  5814. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5815. sizeof(skeymat1))
  5816. /*
  5817. * Check that both sides created the same key material with an
  5818. * empty context.
  5819. */
  5820. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5821. sizeof(skeymat2))
  5822. /* Different contexts should produce different results */
  5823. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5824. sizeof(ckeymat2)))
  5825. goto end;
  5826. testresult = 1;
  5827. end:
  5828. SSL_SESSION_free(sess);
  5829. SSL_SESSION_free(clientpsk);
  5830. SSL_SESSION_free(serverpsk);
  5831. clientpsk = serverpsk = NULL;
  5832. SSL_free(serverssl);
  5833. SSL_free(clientssl);
  5834. SSL_CTX_free(sctx);
  5835. SSL_CTX_free(cctx);
  5836. return testresult;
  5837. }
  5838. #define NUM_KEY_UPDATE_MESSAGES 40
  5839. /*
  5840. * Test KeyUpdate.
  5841. */
  5842. static int test_key_update(void)
  5843. {
  5844. SSL_CTX *cctx = NULL, *sctx = NULL;
  5845. SSL *clientssl = NULL, *serverssl = NULL;
  5846. int testresult = 0, i, j;
  5847. char buf[20];
  5848. static char *mess = "A test message";
  5849. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5850. TLS_client_method(),
  5851. TLS1_3_VERSION,
  5852. 0,
  5853. &sctx, &cctx, cert, privkey))
  5854. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5855. NULL, NULL))
  5856. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5857. SSL_ERROR_NONE)))
  5858. goto end;
  5859. for (j = 0; j < 2; j++) {
  5860. /* Send lots of KeyUpdate messages */
  5861. for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
  5862. if (!TEST_true(SSL_key_update(clientssl,
  5863. (j == 0)
  5864. ? SSL_KEY_UPDATE_NOT_REQUESTED
  5865. : SSL_KEY_UPDATE_REQUESTED))
  5866. || !TEST_true(SSL_do_handshake(clientssl)))
  5867. goto end;
  5868. }
  5869. /* Check that sending and receiving app data is ok */
  5870. if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
  5871. || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
  5872. strlen(mess)))
  5873. goto end;
  5874. if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
  5875. || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
  5876. strlen(mess)))
  5877. goto end;
  5878. }
  5879. testresult = 1;
  5880. end:
  5881. SSL_free(serverssl);
  5882. SSL_free(clientssl);
  5883. SSL_CTX_free(sctx);
  5884. SSL_CTX_free(cctx);
  5885. return testresult;
  5886. }
  5887. /*
  5888. * Test we can handle a KeyUpdate (update requested) message while
  5889. * write data is pending in peer.
  5890. * Test 0: Client sends KeyUpdate while Server is writing
  5891. * Test 1: Server sends KeyUpdate while Client is writing
  5892. */
  5893. static int test_key_update_peer_in_write(int tst)
  5894. {
  5895. SSL_CTX *cctx = NULL, *sctx = NULL;
  5896. SSL *clientssl = NULL, *serverssl = NULL;
  5897. int testresult = 0;
  5898. char buf[20];
  5899. static char *mess = "A test message";
  5900. BIO *bretry = BIO_new(bio_s_always_retry());
  5901. BIO *tmp = NULL;
  5902. SSL *peerupdate = NULL, *peerwrite = NULL;
  5903. if (!TEST_ptr(bretry)
  5904. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5905. TLS_client_method(),
  5906. TLS1_3_VERSION,
  5907. 0,
  5908. &sctx, &cctx, cert, privkey))
  5909. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5910. NULL, NULL))
  5911. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5912. SSL_ERROR_NONE)))
  5913. goto end;
  5914. peerupdate = tst == 0 ? clientssl : serverssl;
  5915. peerwrite = tst == 0 ? serverssl : clientssl;
  5916. if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
  5917. || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
  5918. goto end;
  5919. /* Swap the writing endpoint's write BIO to force a retry */
  5920. tmp = SSL_get_wbio(peerwrite);
  5921. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  5922. tmp = NULL;
  5923. goto end;
  5924. }
  5925. SSL_set0_wbio(peerwrite, bretry);
  5926. bretry = NULL;
  5927. /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
  5928. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
  5929. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE)
  5930. || !TEST_true(SSL_want_write(peerwrite))
  5931. || !TEST_true(SSL_net_write_desired(peerwrite)))
  5932. goto end;
  5933. /* Reinstate the original writing endpoint's write BIO */
  5934. SSL_set0_wbio(peerwrite, tmp);
  5935. tmp = NULL;
  5936. /* Now read some data - we will read the key update */
  5937. if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
  5938. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ)
  5939. || !TEST_true(SSL_want_read(peerwrite))
  5940. || !TEST_true(SSL_net_read_desired(peerwrite)))
  5941. goto end;
  5942. /*
  5943. * Complete the write we started previously and read it from the other
  5944. * endpoint
  5945. */
  5946. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5947. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5948. goto end;
  5949. /* Write more data to ensure we send the KeyUpdate message back */
  5950. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5951. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5952. goto end;
  5953. if (!TEST_false(SSL_net_read_desired(peerwrite))
  5954. || !TEST_false(SSL_net_write_desired(peerwrite))
  5955. || !TEST_int_eq(SSL_want(peerwrite), SSL_NOTHING))
  5956. goto end;
  5957. testresult = 1;
  5958. end:
  5959. SSL_free(serverssl);
  5960. SSL_free(clientssl);
  5961. SSL_CTX_free(sctx);
  5962. SSL_CTX_free(cctx);
  5963. BIO_free(bretry);
  5964. BIO_free(tmp);
  5965. return testresult;
  5966. }
  5967. /*
  5968. * Test we can handle a KeyUpdate (update requested) message while
  5969. * peer read data is pending after peer accepted keyupdate(the msg header
  5970. * had been read 5 bytes).
  5971. * Test 0: Client sends KeyUpdate while Server is reading
  5972. * Test 1: Server sends KeyUpdate while Client is reading
  5973. */
  5974. static int test_key_update_peer_in_read(int tst)
  5975. {
  5976. SSL_CTX *cctx = NULL, *sctx = NULL;
  5977. SSL *clientssl = NULL, *serverssl = NULL;
  5978. int testresult = 0;
  5979. char prbuf[515], lwbuf[515] = {0};
  5980. static char *mess = "A test message";
  5981. BIO *lbio = NULL, *pbio = NULL;
  5982. SSL *local = NULL, *peer = NULL;
  5983. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5984. TLS_client_method(),
  5985. TLS1_3_VERSION,
  5986. 0,
  5987. &sctx, &cctx, cert, privkey))
  5988. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5989. NULL, NULL))
  5990. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5991. SSL_ERROR_NONE)))
  5992. goto end;
  5993. local = tst == 0 ? clientssl : serverssl;
  5994. peer = tst == 0 ? serverssl : clientssl;
  5995. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  5996. goto end;
  5997. SSL_set_bio(local, lbio, lbio);
  5998. SSL_set_bio(peer, pbio, pbio);
  5999. /*
  6000. * we first write keyupdate msg then appdata in local
  6001. * write data in local will fail with SSL_ERROR_WANT_WRITE,because
  6002. * lwbuf app data msg size + key updata msg size > 512(the size of
  6003. * the bio pair buffer)
  6004. */
  6005. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6006. || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
  6007. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  6008. goto end;
  6009. /*
  6010. * first read keyupdate msg in peer in peer
  6011. * then read appdata that we know will fail with SSL_ERROR_WANT_READ
  6012. */
  6013. if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
  6014. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
  6015. goto end;
  6016. /* Now write some data in peer - we will write the key update */
  6017. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
  6018. goto end;
  6019. /*
  6020. * write data in local previously that we will complete
  6021. * read data in peer previously that we will complete
  6022. */
  6023. if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
  6024. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
  6025. goto end;
  6026. /* check that sending and receiving appdata ok */
  6027. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  6028. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  6029. goto end;
  6030. testresult = 1;
  6031. end:
  6032. SSL_free(serverssl);
  6033. SSL_free(clientssl);
  6034. SSL_CTX_free(sctx);
  6035. SSL_CTX_free(cctx);
  6036. return testresult;
  6037. }
  6038. /*
  6039. * Test we can't send a KeyUpdate (update requested) message while
  6040. * local write data is pending.
  6041. * Test 0: Client sends KeyUpdate while Client is writing
  6042. * Test 1: Server sends KeyUpdate while Server is writing
  6043. */
  6044. static int test_key_update_local_in_write(int tst)
  6045. {
  6046. SSL_CTX *cctx = NULL, *sctx = NULL;
  6047. SSL *clientssl = NULL, *serverssl = NULL;
  6048. int testresult = 0;
  6049. char buf[20];
  6050. static char *mess = "A test message";
  6051. BIO *bretry = BIO_new(bio_s_always_retry());
  6052. BIO *tmp = NULL;
  6053. SSL *local = NULL, *peer = NULL;
  6054. if (!TEST_ptr(bretry)
  6055. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6056. TLS_client_method(),
  6057. TLS1_3_VERSION,
  6058. 0,
  6059. &sctx, &cctx, cert, privkey))
  6060. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6061. NULL, NULL))
  6062. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6063. SSL_ERROR_NONE)))
  6064. goto end;
  6065. local = tst == 0 ? clientssl : serverssl;
  6066. peer = tst == 0 ? serverssl : clientssl;
  6067. /* Swap the writing endpoint's write BIO to force a retry */
  6068. tmp = SSL_get_wbio(local);
  6069. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  6070. tmp = NULL;
  6071. goto end;
  6072. }
  6073. SSL_set0_wbio(local, bretry);
  6074. bretry = NULL;
  6075. /* write data in local will fail with SSL_ERROR_WANT_WRITE */
  6076. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
  6077. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  6078. goto end;
  6079. /* Reinstate the original writing endpoint's write BIO */
  6080. SSL_set0_wbio(local, tmp);
  6081. tmp = NULL;
  6082. /* SSL_key_update will fail, because writing in local*/
  6083. if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6084. || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
  6085. goto end;
  6086. ERR_clear_error();
  6087. /* write data in local previously that we will complete */
  6088. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
  6089. goto end;
  6090. /* SSL_key_update will succeed because there is no pending write data */
  6091. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6092. || !TEST_int_eq(SSL_do_handshake(local), 1))
  6093. goto end;
  6094. /*
  6095. * we write some appdata in local
  6096. * read data in peer - we will read the keyupdate msg
  6097. */
  6098. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  6099. || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
  6100. goto end;
  6101. /* Write more peer more data to ensure we send the keyupdate message back */
  6102. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  6103. || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
  6104. goto end;
  6105. testresult = 1;
  6106. end:
  6107. SSL_free(serverssl);
  6108. SSL_free(clientssl);
  6109. SSL_CTX_free(sctx);
  6110. SSL_CTX_free(cctx);
  6111. BIO_free(bretry);
  6112. BIO_free(tmp);
  6113. return testresult;
  6114. }
  6115. /*
  6116. * Test we can handle a KeyUpdate (update requested) message while
  6117. * local read data is pending(the msg header had been read 5 bytes).
  6118. * Test 0: Client sends KeyUpdate while Client is reading
  6119. * Test 1: Server sends KeyUpdate while Server is reading
  6120. */
  6121. static int test_key_update_local_in_read(int tst)
  6122. {
  6123. SSL_CTX *cctx = NULL, *sctx = NULL;
  6124. SSL *clientssl = NULL, *serverssl = NULL;
  6125. int testresult = 0;
  6126. char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
  6127. static char *mess = "A test message";
  6128. BIO *lbio = NULL, *pbio = NULL;
  6129. SSL *local = NULL, *peer = NULL;
  6130. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6131. TLS_client_method(),
  6132. TLS1_3_VERSION,
  6133. 0,
  6134. &sctx, &cctx, cert, privkey))
  6135. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6136. NULL, NULL))
  6137. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6138. SSL_ERROR_NONE)))
  6139. goto end;
  6140. local = tst == 0 ? clientssl : serverssl;
  6141. peer = tst == 0 ? serverssl : clientssl;
  6142. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  6143. goto end;
  6144. SSL_set_bio(local, lbio, lbio);
  6145. SSL_set_bio(peer, pbio, pbio);
  6146. /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
  6147. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
  6148. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
  6149. goto end;
  6150. /* read appdata in local will fail with SSL_ERROR_WANT_READ */
  6151. if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
  6152. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
  6153. goto end;
  6154. /* SSL_do_handshake will send keyupdate msg */
  6155. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  6156. || !TEST_int_eq(SSL_do_handshake(local), 1))
  6157. goto end;
  6158. /*
  6159. * write data in peer previously that we will complete
  6160. * read data in local previously that we will complete
  6161. */
  6162. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
  6163. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
  6164. goto end;
  6165. /*
  6166. * write data in local
  6167. * read data in peer - we will read the key update
  6168. */
  6169. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  6170. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  6171. goto end;
  6172. /* Write more peer data to ensure we send the keyupdate message back */
  6173. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  6174. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
  6175. goto end;
  6176. testresult = 1;
  6177. end:
  6178. SSL_free(serverssl);
  6179. SSL_free(clientssl);
  6180. SSL_CTX_free(sctx);
  6181. SSL_CTX_free(cctx);
  6182. return testresult;
  6183. }
  6184. #endif /* OSSL_NO_USABLE_TLS1_3 */
  6185. /*
  6186. * Test clearing a connection via SSL_clear(), or resetting it via
  6187. * SSL_set_connect_state()/SSL_set_accept_state()
  6188. * Test 0: SSL_set_connect_state, TLSv1.3
  6189. * Test 1: SSL_set_connect_state, TLSv1.2
  6190. * Test 2: SSL_set_accept_state, TLSv1.3
  6191. * Test 3: SSL_set_accept_state, TLSv1.2
  6192. * Test 4: SSL_clear (client), TLSv1.3
  6193. * Test 5: SSL_clear (client), TLSv1.2
  6194. * Test 6: SSL_clear (server), TLSv1.3
  6195. * Test 7: SSL_clear (server), TLSv1.2
  6196. */
  6197. static int test_ssl_clear(int idx)
  6198. {
  6199. SSL_CTX *cctx = NULL, *sctx = NULL;
  6200. SSL *clientssl = NULL, *serverssl = NULL;
  6201. SSL *writer, *reader;
  6202. int testresult = 0;
  6203. int tls12test, servertest, cleartest;
  6204. size_t written, readbytes;
  6205. const char *msg = "Hello World";
  6206. unsigned char buf[5];
  6207. tls12test = idx & 1;
  6208. idx >>= 1;
  6209. servertest = idx & 1;
  6210. idx >>= 1;
  6211. cleartest = idx & 1;
  6212. #ifdef OPENSSL_NO_TLS1_2
  6213. if (tls12test == 1)
  6214. return TEST_skip("No TLSv1.2 in this build");
  6215. #endif
  6216. /* Create an initial connection */
  6217. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6218. TLS_client_method(), TLS1_VERSION, 0,
  6219. &sctx, &cctx, cert, privkey))
  6220. || (tls12test
  6221. && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
  6222. TLS1_2_VERSION)))
  6223. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  6224. &clientssl, NULL, NULL))
  6225. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6226. SSL_ERROR_NONE)))
  6227. goto end;
  6228. if (servertest) {
  6229. writer = clientssl;
  6230. reader = serverssl;
  6231. } else {
  6232. writer = serverssl;
  6233. reader = clientssl;
  6234. }
  6235. /* Write some data */
  6236. if (!TEST_true(SSL_write_ex(writer, msg, strlen(msg), &written))
  6237. || written != strlen(msg))
  6238. goto end;
  6239. /*
  6240. * Read a partial record. The remaining buffered data should be cleared by
  6241. * the subsequent clear/reset
  6242. */
  6243. if (!TEST_true(SSL_read_ex(reader, buf, sizeof(buf), &readbytes))
  6244. || readbytes != sizeof(buf))
  6245. goto end;
  6246. SSL_shutdown(clientssl);
  6247. SSL_shutdown(serverssl);
  6248. /* Reset/clear one SSL object in order to reuse it. We free the other one */
  6249. if (servertest) {
  6250. if (cleartest) {
  6251. if (!TEST_true(SSL_clear(serverssl)))
  6252. goto end;
  6253. } else {
  6254. SSL_set_accept_state(serverssl);
  6255. }
  6256. SSL_free(clientssl);
  6257. clientssl = NULL;
  6258. } else {
  6259. if (cleartest) {
  6260. if (!TEST_true(SSL_clear(clientssl)))
  6261. goto end;
  6262. } else {
  6263. SSL_set_connect_state(clientssl);
  6264. }
  6265. SSL_free(serverssl);
  6266. serverssl = NULL;
  6267. }
  6268. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6269. NULL, NULL))
  6270. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6271. SSL_ERROR_NONE))
  6272. || !TEST_true(servertest || SSL_session_reused(clientssl)))
  6273. goto end;
  6274. SSL_shutdown(clientssl);
  6275. SSL_shutdown(serverssl);
  6276. testresult = 1;
  6277. end:
  6278. SSL_free(serverssl);
  6279. SSL_free(clientssl);
  6280. SSL_CTX_free(sctx);
  6281. SSL_CTX_free(cctx);
  6282. return testresult;
  6283. }
  6284. /* Parse CH and retrieve any MFL extension value if present */
  6285. static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
  6286. {
  6287. long len;
  6288. unsigned char *data;
  6289. PACKET pkt, pkt2, pkt3;
  6290. unsigned int MFL_code = 0, type = 0;
  6291. if (!TEST_uint_gt(len = BIO_get_mem_data(bio, (char **) &data), 0))
  6292. goto end;
  6293. memset(&pkt, 0, sizeof(pkt));
  6294. memset(&pkt2, 0, sizeof(pkt2));
  6295. memset(&pkt3, 0, sizeof(pkt3));
  6296. if (!TEST_long_gt(len, 0)
  6297. || !TEST_true(PACKET_buf_init(&pkt, data, len))
  6298. /* Skip the record header */
  6299. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  6300. /* Skip the handshake message header */
  6301. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  6302. /* Skip client version and random */
  6303. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  6304. + SSL3_RANDOM_SIZE))
  6305. /* Skip session id */
  6306. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  6307. /* Skip ciphers */
  6308. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  6309. /* Skip compression */
  6310. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  6311. /* Extensions len */
  6312. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  6313. goto end;
  6314. /* Loop through all extensions */
  6315. while (PACKET_remaining(&pkt2)) {
  6316. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  6317. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  6318. goto end;
  6319. if (type == TLSEXT_TYPE_max_fragment_length) {
  6320. if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
  6321. || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
  6322. goto end;
  6323. *mfl_codemfl_code = MFL_code;
  6324. return 1;
  6325. }
  6326. }
  6327. end:
  6328. return 0;
  6329. }
  6330. /* Maximum-Fragment-Length TLS extension mode to test */
  6331. static const unsigned char max_fragment_len_test[] = {
  6332. TLSEXT_max_fragment_length_512,
  6333. TLSEXT_max_fragment_length_1024,
  6334. TLSEXT_max_fragment_length_2048,
  6335. TLSEXT_max_fragment_length_4096
  6336. };
  6337. static int test_max_fragment_len_ext(int idx_tst)
  6338. {
  6339. SSL_CTX *ctx = NULL;
  6340. SSL *con = NULL;
  6341. int testresult = 0, MFL_mode = 0;
  6342. BIO *rbio, *wbio;
  6343. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
  6344. TLS1_VERSION, 0, NULL, &ctx, NULL,
  6345. NULL)))
  6346. return 0;
  6347. if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
  6348. ctx, max_fragment_len_test[idx_tst])))
  6349. goto end;
  6350. con = SSL_new(ctx);
  6351. if (!TEST_ptr(con))
  6352. goto end;
  6353. rbio = BIO_new(BIO_s_mem());
  6354. wbio = BIO_new(BIO_s_mem());
  6355. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  6356. BIO_free(rbio);
  6357. BIO_free(wbio);
  6358. goto end;
  6359. }
  6360. SSL_set_bio(con, rbio, wbio);
  6361. if (!TEST_int_le(SSL_connect(con), 0)) {
  6362. /* This shouldn't succeed because we don't have a server! */
  6363. goto end;
  6364. }
  6365. if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
  6366. /* no MFL in client hello */
  6367. goto end;
  6368. if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
  6369. goto end;
  6370. testresult = 1;
  6371. end:
  6372. SSL_free(con);
  6373. SSL_CTX_free(ctx);
  6374. return testresult;
  6375. }
  6376. #ifndef OSSL_NO_USABLE_TLS1_3
  6377. static int test_pha_key_update(void)
  6378. {
  6379. SSL_CTX *cctx = NULL, *sctx = NULL;
  6380. SSL *clientssl = NULL, *serverssl = NULL;
  6381. int testresult = 0;
  6382. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6383. TLS_client_method(), TLS1_VERSION, 0,
  6384. &sctx, &cctx, cert, privkey)))
  6385. return 0;
  6386. if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
  6387. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
  6388. || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
  6389. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
  6390. goto end;
  6391. SSL_CTX_set_post_handshake_auth(cctx, 1);
  6392. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6393. NULL, NULL)))
  6394. goto end;
  6395. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6396. SSL_ERROR_NONE)))
  6397. goto end;
  6398. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  6399. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  6400. goto end;
  6401. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  6402. goto end;
  6403. /* Start handshake on the server */
  6404. if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
  6405. goto end;
  6406. /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
  6407. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6408. SSL_ERROR_NONE)))
  6409. goto end;
  6410. SSL_shutdown(clientssl);
  6411. SSL_shutdown(serverssl);
  6412. testresult = 1;
  6413. end:
  6414. SSL_free(serverssl);
  6415. SSL_free(clientssl);
  6416. SSL_CTX_free(sctx);
  6417. SSL_CTX_free(cctx);
  6418. return testresult;
  6419. }
  6420. #endif
  6421. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  6422. static SRP_VBASE *vbase = NULL;
  6423. static int ssl_srp_cb(SSL *s, int *ad, void *arg)
  6424. {
  6425. int ret = SSL3_AL_FATAL;
  6426. char *username;
  6427. SRP_user_pwd *user = NULL;
  6428. username = SSL_get_srp_username(s);
  6429. if (username == NULL) {
  6430. *ad = SSL_AD_INTERNAL_ERROR;
  6431. goto err;
  6432. }
  6433. user = SRP_VBASE_get1_by_user(vbase, username);
  6434. if (user == NULL) {
  6435. *ad = SSL_AD_INTERNAL_ERROR;
  6436. goto err;
  6437. }
  6438. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  6439. user->info) <= 0) {
  6440. *ad = SSL_AD_INTERNAL_ERROR;
  6441. goto err;
  6442. }
  6443. ret = 0;
  6444. err:
  6445. SRP_user_pwd_free(user);
  6446. return ret;
  6447. }
  6448. static int create_new_vfile(char *userid, char *password, const char *filename)
  6449. {
  6450. char *gNid = NULL;
  6451. OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
  6452. TXT_DB *db = NULL;
  6453. int ret = 0;
  6454. BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
  6455. size_t i;
  6456. if (!TEST_ptr(dummy) || !TEST_ptr(row))
  6457. goto end;
  6458. gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
  6459. &row[DB_srpverifier], NULL, NULL, libctx, NULL);
  6460. if (!TEST_ptr(gNid))
  6461. goto end;
  6462. /*
  6463. * The only way to create an empty TXT_DB is to provide a BIO with no data
  6464. * in it!
  6465. */
  6466. db = TXT_DB_read(dummy, DB_NUMBER);
  6467. if (!TEST_ptr(db))
  6468. goto end;
  6469. out = BIO_new_file(filename, "w");
  6470. if (!TEST_ptr(out))
  6471. goto end;
  6472. row[DB_srpid] = OPENSSL_strdup(userid);
  6473. row[DB_srptype] = OPENSSL_strdup("V");
  6474. row[DB_srpgN] = OPENSSL_strdup(gNid);
  6475. if (!TEST_ptr(row[DB_srpid])
  6476. || !TEST_ptr(row[DB_srptype])
  6477. || !TEST_ptr(row[DB_srpgN])
  6478. || !TEST_true(TXT_DB_insert(db, row)))
  6479. goto end;
  6480. row = NULL;
  6481. if (TXT_DB_write(out, db) <= 0)
  6482. goto end;
  6483. ret = 1;
  6484. end:
  6485. if (row != NULL) {
  6486. for (i = 0; i < DB_NUMBER; i++)
  6487. OPENSSL_free(row[i]);
  6488. }
  6489. OPENSSL_free(row);
  6490. BIO_free(dummy);
  6491. BIO_free(out);
  6492. TXT_DB_free(db);
  6493. return ret;
  6494. }
  6495. static int create_new_vbase(char *userid, char *password)
  6496. {
  6497. BIGNUM *verifier = NULL, *salt = NULL;
  6498. const SRP_gN *lgN = NULL;
  6499. SRP_user_pwd *user_pwd = NULL;
  6500. int ret = 0;
  6501. lgN = SRP_get_default_gN(NULL);
  6502. if (!TEST_ptr(lgN))
  6503. goto end;
  6504. if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
  6505. lgN->N, lgN->g, libctx, NULL)))
  6506. goto end;
  6507. user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
  6508. if (!TEST_ptr(user_pwd))
  6509. goto end;
  6510. user_pwd->N = lgN->N;
  6511. user_pwd->g = lgN->g;
  6512. user_pwd->id = OPENSSL_strdup(userid);
  6513. if (!TEST_ptr(user_pwd->id))
  6514. goto end;
  6515. user_pwd->v = verifier;
  6516. user_pwd->s = salt;
  6517. verifier = salt = NULL;
  6518. if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
  6519. goto end;
  6520. user_pwd = NULL;
  6521. ret = 1;
  6522. end:
  6523. SRP_user_pwd_free(user_pwd);
  6524. BN_free(salt);
  6525. BN_free(verifier);
  6526. return ret;
  6527. }
  6528. /*
  6529. * SRP tests
  6530. *
  6531. * Test 0: Simple successful SRP connection, new vbase
  6532. * Test 1: Connection failure due to bad password, new vbase
  6533. * Test 2: Simple successful SRP connection, vbase loaded from existing file
  6534. * Test 3: Connection failure due to bad password, vbase loaded from existing
  6535. * file
  6536. * Test 4: Simple successful SRP connection, vbase loaded from new file
  6537. * Test 5: Connection failure due to bad password, vbase loaded from new file
  6538. */
  6539. static int test_srp(int tst)
  6540. {
  6541. char *userid = "test", *password = "password", *tstsrpfile;
  6542. SSL_CTX *cctx = NULL, *sctx = NULL;
  6543. SSL *clientssl = NULL, *serverssl = NULL;
  6544. int ret, testresult = 0;
  6545. vbase = SRP_VBASE_new(NULL);
  6546. if (!TEST_ptr(vbase))
  6547. goto end;
  6548. if (tst == 0 || tst == 1) {
  6549. if (!TEST_true(create_new_vbase(userid, password)))
  6550. goto end;
  6551. } else {
  6552. if (tst == 4 || tst == 5) {
  6553. if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
  6554. goto end;
  6555. tstsrpfile = tmpfilename;
  6556. } else {
  6557. tstsrpfile = srpvfile;
  6558. }
  6559. if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
  6560. goto end;
  6561. }
  6562. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6563. TLS_client_method(), TLS1_VERSION, 0,
  6564. &sctx, &cctx, cert, privkey)))
  6565. goto end;
  6566. if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
  6567. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
  6568. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
  6569. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
  6570. || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
  6571. goto end;
  6572. if (tst % 2 == 1) {
  6573. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
  6574. goto end;
  6575. } else {
  6576. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
  6577. goto end;
  6578. }
  6579. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6580. NULL, NULL)))
  6581. goto end;
  6582. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  6583. if (ret) {
  6584. if (!TEST_true(tst % 2 == 0))
  6585. goto end;
  6586. } else {
  6587. if (!TEST_true(tst % 2 == 1))
  6588. goto end;
  6589. }
  6590. testresult = 1;
  6591. end:
  6592. SRP_VBASE_free(vbase);
  6593. vbase = NULL;
  6594. SSL_free(serverssl);
  6595. SSL_free(clientssl);
  6596. SSL_CTX_free(sctx);
  6597. SSL_CTX_free(cctx);
  6598. return testresult;
  6599. }
  6600. #endif
  6601. static int info_cb_failed = 0;
  6602. static int info_cb_offset = 0;
  6603. static int info_cb_this_state = -1;
  6604. static struct info_cb_states_st {
  6605. int where;
  6606. const char *statestr;
  6607. } info_cb_states[][60] = {
  6608. {
  6609. /* TLSv1.2 server followed by resumption */
  6610. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6611. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6612. {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
  6613. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
  6614. {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
  6615. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6616. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6617. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6618. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
  6619. {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6620. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
  6621. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6622. {SSL_CB_EXIT, NULL}, {0, NULL},
  6623. }, {
  6624. /* TLSv1.2 client followed by resumption */
  6625. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6626. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6627. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
  6628. {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
  6629. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
  6630. {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6631. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6632. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6633. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6634. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6635. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6636. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6637. }, {
  6638. /* TLSv1.3 server followed by resumption */
  6639. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6640. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6641. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
  6642. {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  6643. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  6644. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6645. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6646. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6647. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6648. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6649. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  6650. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6651. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6652. }, {
  6653. /* TLSv1.3 client followed by resumption */
  6654. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6655. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6656. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
  6657. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  6658. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6659. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6660. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
  6661. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  6662. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6663. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  6664. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6665. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6666. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6667. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
  6668. {SSL_CB_EXIT, NULL}, {0, NULL},
  6669. }, {
  6670. /* TLSv1.3 server, early_data */
  6671. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6672. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6673. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6674. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6675. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6676. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
  6677. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6678. {SSL_CB_EXIT, NULL}, {0, NULL},
  6679. }, {
  6680. /* TLSv1.3 client, early_data */
  6681. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6682. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
  6683. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6684. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6685. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6686. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
  6687. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6688. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6689. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6690. }, {
  6691. /* TLSv1.3 server, certificate compression, followed by resumption */
  6692. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6693. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6694. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSCC"},
  6695. {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  6696. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  6697. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6698. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6699. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6700. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6701. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6702. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  6703. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6704. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6705. }, {
  6706. /* TLSv1.3 client, certificate compression, followed by resumption */
  6707. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6708. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6709. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSCC"},
  6710. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  6711. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6712. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6713. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
  6714. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  6715. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6716. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  6717. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6718. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6719. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6720. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
  6721. {SSL_CB_EXIT, NULL}, {0, NULL},
  6722. }, {
  6723. {0, NULL},
  6724. }
  6725. };
  6726. static void sslapi_info_callback(const SSL *s, int where, int ret)
  6727. {
  6728. struct info_cb_states_st *state = info_cb_states[info_cb_offset];
  6729. /* We do not ever expect a connection to fail in this test */
  6730. if (!TEST_false(ret == 0)) {
  6731. info_cb_failed = 1;
  6732. return;
  6733. }
  6734. /*
  6735. * Do some sanity checks. We never expect these things to happen in this
  6736. * test
  6737. */
  6738. if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
  6739. || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
  6740. || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
  6741. info_cb_failed = 1;
  6742. return;
  6743. }
  6744. /* Now check we're in the right state */
  6745. if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
  6746. info_cb_failed = 1;
  6747. return;
  6748. }
  6749. if ((where & SSL_CB_LOOP) != 0
  6750. && !TEST_int_eq(strcmp(SSL_state_string(s),
  6751. state[info_cb_this_state].statestr), 0)) {
  6752. info_cb_failed = 1;
  6753. return;
  6754. }
  6755. /*
  6756. * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
  6757. */
  6758. if ((where & SSL_CB_HANDSHAKE_DONE)
  6759. && SSL_in_init((SSL *)s) != 0) {
  6760. info_cb_failed = 1;
  6761. return;
  6762. }
  6763. }
  6764. /*
  6765. * Test the info callback gets called when we expect it to.
  6766. *
  6767. * Test 0: TLSv1.2, server
  6768. * Test 1: TLSv1.2, client
  6769. * Test 2: TLSv1.3, server
  6770. * Test 3: TLSv1.3, client
  6771. * Test 4: TLSv1.3, server, early_data
  6772. * Test 5: TLSv1.3, client, early_data
  6773. * Test 6: TLSv1.3, server, compressed certificate
  6774. * Test 7: TLSv1.3, client, compressed certificate
  6775. */
  6776. static int test_info_callback(int tst)
  6777. {
  6778. SSL_CTX *cctx = NULL, *sctx = NULL;
  6779. SSL *clientssl = NULL, *serverssl = NULL;
  6780. SSL_SESSION *clntsess = NULL;
  6781. int testresult = 0;
  6782. int tlsvers;
  6783. if (tst < 2) {
  6784. /* We need either ECDHE or DHE for the TLSv1.2 test to work */
  6785. #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
  6786. || !defined(OPENSSL_NO_DH))
  6787. tlsvers = TLS1_2_VERSION;
  6788. #else
  6789. return 1;
  6790. #endif
  6791. } else {
  6792. #ifndef OSSL_NO_USABLE_TLS1_3
  6793. tlsvers = TLS1_3_VERSION;
  6794. #else
  6795. return 1;
  6796. #endif
  6797. }
  6798. /* Reset globals */
  6799. info_cb_failed = 0;
  6800. info_cb_this_state = -1;
  6801. info_cb_offset = tst;
  6802. #ifndef OSSL_NO_USABLE_TLS1_3
  6803. if (tst >= 4 && tst < 6) {
  6804. SSL_SESSION *sess = NULL;
  6805. size_t written, readbytes;
  6806. unsigned char buf[80];
  6807. OSSL_TIME timer;
  6808. /* early_data tests */
  6809. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  6810. &serverssl, &sess, 0,
  6811. SHA384_DIGEST_LENGTH)))
  6812. goto end;
  6813. /* We don't actually need this reference */
  6814. SSL_SESSION_free(sess);
  6815. SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
  6816. sslapi_info_callback);
  6817. /* Write and read some early data and then complete the connection */
  6818. timer = ossl_time_now();
  6819. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  6820. &written))
  6821. || !TEST_size_t_eq(written, strlen(MSG1)))
  6822. goto end;
  6823. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf,
  6824. sizeof(buf), &readbytes),
  6825. SSL_READ_EARLY_DATA_SUCCESS)) {
  6826. testresult = check_early_data_timeout(timer);
  6827. goto end;
  6828. }
  6829. if (!TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  6830. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  6831. SSL_EARLY_DATA_ACCEPTED)
  6832. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6833. SSL_ERROR_NONE))
  6834. || !TEST_false(info_cb_failed))
  6835. goto end;
  6836. testresult = 1;
  6837. goto end;
  6838. }
  6839. #endif
  6840. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6841. TLS_client_method(),
  6842. tlsvers, tlsvers, &sctx, &cctx, cert,
  6843. privkey)))
  6844. goto end;
  6845. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  6846. goto end;
  6847. /*
  6848. * For even numbered tests we check the server callbacks. For odd numbers we
  6849. * check the client.
  6850. */
  6851. SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
  6852. sslapi_info_callback);
  6853. if (tst >= 6) {
  6854. if (!SSL_CTX_compress_certs(sctx, 0))
  6855. goto end;
  6856. }
  6857. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  6858. &clientssl, NULL, NULL))
  6859. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6860. SSL_ERROR_NONE))
  6861. || !TEST_false(info_cb_failed))
  6862. goto end;
  6863. clntsess = SSL_get1_session(clientssl);
  6864. SSL_shutdown(clientssl);
  6865. SSL_shutdown(serverssl);
  6866. SSL_free(serverssl);
  6867. SSL_free(clientssl);
  6868. serverssl = clientssl = NULL;
  6869. /* Now do a resumption */
  6870. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6871. NULL))
  6872. || !TEST_true(SSL_set_session(clientssl, clntsess))
  6873. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6874. SSL_ERROR_NONE))
  6875. || !TEST_true(SSL_session_reused(clientssl))
  6876. || !TEST_false(info_cb_failed))
  6877. goto end;
  6878. testresult = 1;
  6879. end:
  6880. SSL_free(serverssl);
  6881. SSL_free(clientssl);
  6882. SSL_SESSION_free(clntsess);
  6883. SSL_CTX_free(sctx);
  6884. SSL_CTX_free(cctx);
  6885. return testresult;
  6886. }
  6887. static int test_ssl_pending(int tst)
  6888. {
  6889. SSL_CTX *cctx = NULL, *sctx = NULL;
  6890. SSL *clientssl = NULL, *serverssl = NULL;
  6891. int testresult = 0;
  6892. char msg[] = "A test message";
  6893. char buf[5];
  6894. size_t written, readbytes;
  6895. if (tst == 0) {
  6896. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6897. TLS_client_method(),
  6898. TLS1_VERSION, 0,
  6899. &sctx, &cctx, cert, privkey)))
  6900. goto end;
  6901. } else {
  6902. #ifndef OPENSSL_NO_DTLS
  6903. if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
  6904. DTLS_client_method(),
  6905. DTLS1_VERSION, 0,
  6906. &sctx, &cctx, cert, privkey)))
  6907. goto end;
  6908. # ifdef OPENSSL_NO_DTLS1_2
  6909. /* Not supported in the FIPS provider */
  6910. if (is_fips) {
  6911. testresult = 1;
  6912. goto end;
  6913. };
  6914. /*
  6915. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  6916. * level 0
  6917. */
  6918. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  6919. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  6920. "DEFAULT:@SECLEVEL=0")))
  6921. goto end;
  6922. # endif
  6923. #else
  6924. return 1;
  6925. #endif
  6926. }
  6927. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6928. NULL, NULL))
  6929. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6930. SSL_ERROR_NONE)))
  6931. goto end;
  6932. if (!TEST_int_eq(SSL_pending(clientssl), 0)
  6933. || !TEST_false(SSL_has_pending(clientssl))
  6934. || !TEST_int_eq(SSL_pending(serverssl), 0)
  6935. || !TEST_false(SSL_has_pending(serverssl))
  6936. || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  6937. || !TEST_size_t_eq(written, sizeof(msg))
  6938. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  6939. || !TEST_size_t_eq(readbytes, sizeof(buf))
  6940. || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
  6941. || !TEST_true(SSL_has_pending(clientssl)))
  6942. goto end;
  6943. testresult = 1;
  6944. end:
  6945. SSL_free(serverssl);
  6946. SSL_free(clientssl);
  6947. SSL_CTX_free(sctx);
  6948. SSL_CTX_free(cctx);
  6949. return testresult;
  6950. }
  6951. static struct {
  6952. unsigned int maxprot;
  6953. const char *clntciphers;
  6954. const char *clnttls13ciphers;
  6955. const char *srvrciphers;
  6956. const char *srvrtls13ciphers;
  6957. const char *shared;
  6958. const char *fipsshared;
  6959. } shared_ciphers_data[] = {
  6960. /*
  6961. * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
  6962. * TLSv1.3 is enabled but TLSv1.2 is disabled.
  6963. */
  6964. #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  6965. {
  6966. TLS1_2_VERSION,
  6967. "AES128-SHA:AES256-SHA",
  6968. NULL,
  6969. "AES256-SHA:DHE-RSA-AES128-SHA",
  6970. NULL,
  6971. "AES256-SHA",
  6972. "AES256-SHA"
  6973. },
  6974. # if !defined(OPENSSL_NO_CHACHA) \
  6975. && !defined(OPENSSL_NO_POLY1305) \
  6976. && !defined(OPENSSL_NO_EC)
  6977. {
  6978. TLS1_2_VERSION,
  6979. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6980. NULL,
  6981. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6982. NULL,
  6983. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6984. "AES128-SHA"
  6985. },
  6986. # endif
  6987. {
  6988. TLS1_2_VERSION,
  6989. "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
  6990. NULL,
  6991. "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
  6992. NULL,
  6993. "AES128-SHA:AES256-SHA",
  6994. "AES128-SHA:AES256-SHA"
  6995. },
  6996. {
  6997. TLS1_2_VERSION,
  6998. "AES128-SHA:AES256-SHA",
  6999. NULL,
  7000. "AES128-SHA:DHE-RSA-AES128-SHA",
  7001. NULL,
  7002. "AES128-SHA",
  7003. "AES128-SHA"
  7004. },
  7005. #endif
  7006. /*
  7007. * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
  7008. * enabled.
  7009. */
  7010. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
  7011. && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  7012. {
  7013. TLS1_3_VERSION,
  7014. "AES128-SHA:AES256-SHA",
  7015. NULL,
  7016. "AES256-SHA:AES128-SHA256",
  7017. NULL,
  7018. "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
  7019. "TLS_AES_128_GCM_SHA256:AES256-SHA",
  7020. "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
  7021. },
  7022. #endif
  7023. #ifndef OSSL_NO_USABLE_TLS1_3
  7024. {
  7025. TLS1_3_VERSION,
  7026. "AES128-SHA",
  7027. "TLS_AES_256_GCM_SHA384",
  7028. "AES256-SHA",
  7029. "TLS_AES_256_GCM_SHA384",
  7030. "TLS_AES_256_GCM_SHA384",
  7031. "TLS_AES_256_GCM_SHA384"
  7032. },
  7033. #endif
  7034. };
  7035. static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
  7036. {
  7037. SSL_CTX *cctx = NULL, *sctx = NULL;
  7038. SSL *clientssl = NULL, *serverssl = NULL;
  7039. int testresult = 0;
  7040. char buf[1024];
  7041. OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
  7042. if (!TEST_ptr(tmplibctx))
  7043. goto end;
  7044. /*
  7045. * Regardless of whether we're testing with the FIPS provider loaded into
  7046. * libctx, we want one peer to always use the full set of ciphersuites
  7047. * available. Therefore we use a separate libctx with the default provider
  7048. * loaded into it. We run the same tests twice - once with the client side
  7049. * having the full set of ciphersuites and once with the server side.
  7050. */
  7051. if (clnt) {
  7052. cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
  7053. if (!TEST_ptr(cctx))
  7054. goto end;
  7055. } else {
  7056. sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
  7057. if (!TEST_ptr(sctx))
  7058. goto end;
  7059. }
  7060. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7061. TLS_client_method(),
  7062. TLS1_VERSION,
  7063. shared_ciphers_data[tst].maxprot,
  7064. &sctx, &cctx, cert, privkey)))
  7065. goto end;
  7066. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7067. shared_ciphers_data[tst].clntciphers))
  7068. || (shared_ciphers_data[tst].clnttls13ciphers != NULL
  7069. && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  7070. shared_ciphers_data[tst].clnttls13ciphers)))
  7071. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  7072. shared_ciphers_data[tst].srvrciphers))
  7073. || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
  7074. && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  7075. shared_ciphers_data[tst].srvrtls13ciphers))))
  7076. goto end;
  7077. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7078. NULL, NULL))
  7079. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7080. SSL_ERROR_NONE)))
  7081. goto end;
  7082. if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
  7083. || !TEST_int_eq(strcmp(buf,
  7084. is_fips
  7085. ? shared_ciphers_data[tst].fipsshared
  7086. : shared_ciphers_data[tst].shared),
  7087. 0)) {
  7088. TEST_info("Shared ciphers are: %s\n", buf);
  7089. goto end;
  7090. }
  7091. testresult = 1;
  7092. end:
  7093. SSL_free(serverssl);
  7094. SSL_free(clientssl);
  7095. SSL_CTX_free(sctx);
  7096. SSL_CTX_free(cctx);
  7097. OSSL_LIB_CTX_free(tmplibctx);
  7098. return testresult;
  7099. }
  7100. static int test_ssl_get_shared_ciphers(int tst)
  7101. {
  7102. return int_test_ssl_get_shared_ciphers(tst, 0)
  7103. && int_test_ssl_get_shared_ciphers(tst, 1);
  7104. }
  7105. static const char *appdata = "Hello World";
  7106. static int gen_tick_called, dec_tick_called, tick_key_cb_called;
  7107. static int tick_key_renew = 0;
  7108. static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  7109. static int gen_tick_cb(SSL *s, void *arg)
  7110. {
  7111. gen_tick_called = 1;
  7112. return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
  7113. strlen(appdata));
  7114. }
  7115. static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
  7116. const unsigned char *keyname,
  7117. size_t keyname_length,
  7118. SSL_TICKET_STATUS status,
  7119. void *arg)
  7120. {
  7121. void *tickdata;
  7122. size_t tickdlen;
  7123. dec_tick_called = 1;
  7124. if (status == SSL_TICKET_EMPTY)
  7125. return SSL_TICKET_RETURN_IGNORE_RENEW;
  7126. if (!TEST_true(status == SSL_TICKET_SUCCESS
  7127. || status == SSL_TICKET_SUCCESS_RENEW))
  7128. return SSL_TICKET_RETURN_ABORT;
  7129. if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
  7130. &tickdlen))
  7131. || !TEST_size_t_eq(tickdlen, strlen(appdata))
  7132. || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
  7133. return SSL_TICKET_RETURN_ABORT;
  7134. if (tick_key_cb_called) {
  7135. /* Don't change what the ticket key callback wanted to do */
  7136. switch (status) {
  7137. case SSL_TICKET_NO_DECRYPT:
  7138. return SSL_TICKET_RETURN_IGNORE_RENEW;
  7139. case SSL_TICKET_SUCCESS:
  7140. return SSL_TICKET_RETURN_USE;
  7141. case SSL_TICKET_SUCCESS_RENEW:
  7142. return SSL_TICKET_RETURN_USE_RENEW;
  7143. default:
  7144. return SSL_TICKET_RETURN_ABORT;
  7145. }
  7146. }
  7147. return tick_dec_ret;
  7148. }
  7149. #ifndef OPENSSL_NO_DEPRECATED_3_0
  7150. static int tick_key_cb(SSL *s, unsigned char key_name[16],
  7151. unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
  7152. HMAC_CTX *hctx, int enc)
  7153. {
  7154. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  7155. const unsigned char tick_hmac_key[16] = "0123456789abcdef";
  7156. EVP_CIPHER *aes128cbc;
  7157. EVP_MD *sha256;
  7158. int ret;
  7159. tick_key_cb_called = 1;
  7160. if (tick_key_renew == -1)
  7161. return 0;
  7162. aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  7163. if (!TEST_ptr(aes128cbc))
  7164. return 0;
  7165. sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
  7166. if (!TEST_ptr(sha256)) {
  7167. EVP_CIPHER_free(aes128cbc);
  7168. return 0;
  7169. }
  7170. memset(iv, 0, AES_BLOCK_SIZE);
  7171. memset(key_name, 0, 16);
  7172. if (aes128cbc == NULL
  7173. || sha256 == NULL
  7174. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  7175. || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
  7176. NULL))
  7177. ret = -1;
  7178. else
  7179. ret = tick_key_renew ? 2 : 1;
  7180. EVP_CIPHER_free(aes128cbc);
  7181. EVP_MD_free(sha256);
  7182. return ret;
  7183. }
  7184. #endif
  7185. static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
  7186. unsigned char iv[EVP_MAX_IV_LENGTH],
  7187. EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
  7188. {
  7189. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  7190. unsigned char tick_hmac_key[16] = "0123456789abcdef";
  7191. OSSL_PARAM params[2];
  7192. EVP_CIPHER *aes128cbc;
  7193. int ret;
  7194. tick_key_cb_called = 1;
  7195. if (tick_key_renew == -1)
  7196. return 0;
  7197. aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  7198. if (!TEST_ptr(aes128cbc))
  7199. return 0;
  7200. memset(iv, 0, AES_BLOCK_SIZE);
  7201. memset(key_name, 0, 16);
  7202. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  7203. "SHA256", 0);
  7204. params[1] = OSSL_PARAM_construct_end();
  7205. if (aes128cbc == NULL
  7206. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  7207. || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
  7208. params))
  7209. ret = -1;
  7210. else
  7211. ret = tick_key_renew ? 2 : 1;
  7212. EVP_CIPHER_free(aes128cbc);
  7213. return ret;
  7214. }
  7215. /*
  7216. * Test the various ticket callbacks
  7217. * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
  7218. * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
  7219. * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
  7220. * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
  7221. * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
  7222. * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
  7223. * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
  7224. * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
  7225. * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
  7226. * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
  7227. * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
  7228. * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
  7229. * Test 12: TLSv1.2, old ticket key callback, no ticket
  7230. * Test 13: TLSv1.3, old ticket key callback, no ticket
  7231. * Test 14: TLSv1.2, ticket key callback, ticket, no renewal
  7232. * Test 15: TLSv1.3, ticket key callback, ticket, no renewal
  7233. * Test 16: TLSv1.2, ticket key callback, ticket, renewal
  7234. * Test 17: TLSv1.3, ticket key callback, ticket, renewal
  7235. * Test 18: TLSv1.2, ticket key callback, no ticket
  7236. * Test 19: TLSv1.3, ticket key callback, no ticket
  7237. */
  7238. static int test_ticket_callbacks(int tst)
  7239. {
  7240. SSL_CTX *cctx = NULL, *sctx = NULL;
  7241. SSL *clientssl = NULL, *serverssl = NULL;
  7242. SSL_SESSION *clntsess = NULL;
  7243. int testresult = 0;
  7244. #ifdef OPENSSL_NO_TLS1_2
  7245. if (tst % 2 == 0)
  7246. return 1;
  7247. #endif
  7248. #ifdef OSSL_NO_USABLE_TLS1_3
  7249. if (tst % 2 == 1)
  7250. return 1;
  7251. #endif
  7252. #ifdef OPENSSL_NO_DEPRECATED_3_0
  7253. if (tst >= 8 && tst <= 13)
  7254. return 1;
  7255. #endif
  7256. gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
  7257. /* Which tests the ticket key callback should request renewal for */
  7258. if (tst == 10 || tst == 11 || tst == 16 || tst == 17)
  7259. tick_key_renew = 1;
  7260. else if (tst == 12 || tst == 13 || tst == 18 || tst == 19)
  7261. tick_key_renew = -1; /* abort sending the ticket/0-length ticket */
  7262. else
  7263. tick_key_renew = 0;
  7264. /* Which tests the decrypt ticket callback should request renewal for */
  7265. switch (tst) {
  7266. case 0:
  7267. case 1:
  7268. tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
  7269. break;
  7270. case 2:
  7271. case 3:
  7272. tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
  7273. break;
  7274. case 4:
  7275. case 5:
  7276. tick_dec_ret = SSL_TICKET_RETURN_USE;
  7277. break;
  7278. case 6:
  7279. case 7:
  7280. tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
  7281. break;
  7282. default:
  7283. tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  7284. }
  7285. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7286. TLS_client_method(),
  7287. TLS1_VERSION,
  7288. ((tst % 2) == 0) ? TLS1_2_VERSION
  7289. : TLS1_3_VERSION,
  7290. &sctx, &cctx, cert, privkey)))
  7291. goto end;
  7292. /*
  7293. * We only want sessions to resume from tickets - not the session cache. So
  7294. * switch the cache off.
  7295. */
  7296. if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
  7297. goto end;
  7298. if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
  7299. NULL)))
  7300. goto end;
  7301. if (tst >= 14) {
  7302. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
  7303. goto end;
  7304. #ifndef OPENSSL_NO_DEPRECATED_3_0
  7305. } else if (tst >= 8) {
  7306. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
  7307. goto end;
  7308. #endif
  7309. }
  7310. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7311. NULL, NULL))
  7312. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7313. SSL_ERROR_NONE)))
  7314. goto end;
  7315. /*
  7316. * The decrypt ticket key callback in TLSv1.2 should be called even though
  7317. * we have no ticket yet, because it gets called with a status of
  7318. * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
  7319. * actually send any ticket data). This does not happen in TLSv1.3 because
  7320. * it is not valid to send empty ticket data in TLSv1.3.
  7321. */
  7322. if (!TEST_int_eq(gen_tick_called, 1)
  7323. || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
  7324. goto end;
  7325. gen_tick_called = dec_tick_called = 0;
  7326. clntsess = SSL_get1_session(clientssl);
  7327. SSL_shutdown(clientssl);
  7328. SSL_shutdown(serverssl);
  7329. SSL_free(serverssl);
  7330. SSL_free(clientssl);
  7331. serverssl = clientssl = NULL;
  7332. /* Now do a resumption */
  7333. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  7334. NULL))
  7335. || !TEST_true(SSL_set_session(clientssl, clntsess))
  7336. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7337. SSL_ERROR_NONE)))
  7338. goto end;
  7339. if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
  7340. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  7341. || tick_key_renew == -1) {
  7342. if (!TEST_false(SSL_session_reused(clientssl)))
  7343. goto end;
  7344. } else {
  7345. if (!TEST_true(SSL_session_reused(clientssl)))
  7346. goto end;
  7347. }
  7348. if (!TEST_int_eq(gen_tick_called,
  7349. (tick_key_renew
  7350. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  7351. || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
  7352. ? 1 : 0)
  7353. /* There is no ticket to decrypt in tests 13 and 19 */
  7354. || !TEST_int_eq(dec_tick_called, (tst == 13 || tst == 19) ? 0 : 1))
  7355. goto end;
  7356. testresult = 1;
  7357. end:
  7358. SSL_SESSION_free(clntsess);
  7359. SSL_free(serverssl);
  7360. SSL_free(clientssl);
  7361. SSL_CTX_free(sctx);
  7362. SSL_CTX_free(cctx);
  7363. return testresult;
  7364. }
  7365. /*
  7366. * Test incorrect shutdown.
  7367. * Test 0: client does not shutdown properly,
  7368. * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
  7369. * server should get SSL_ERROR_SSL
  7370. * Test 1: client does not shutdown properly,
  7371. * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
  7372. * server should get SSL_ERROR_ZERO_RETURN
  7373. */
  7374. static int test_incorrect_shutdown(int tst)
  7375. {
  7376. SSL_CTX *cctx = NULL, *sctx = NULL;
  7377. SSL *clientssl = NULL, *serverssl = NULL;
  7378. int testresult = 0;
  7379. char buf[80];
  7380. BIO *c2s;
  7381. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7382. TLS_client_method(), 0, 0,
  7383. &sctx, &cctx, cert, privkey)))
  7384. goto end;
  7385. if (tst == 1)
  7386. SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
  7387. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7388. NULL, NULL)))
  7389. goto end;
  7390. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  7391. SSL_ERROR_NONE)))
  7392. goto end;
  7393. c2s = SSL_get_rbio(serverssl);
  7394. BIO_set_mem_eof_return(c2s, 0);
  7395. if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
  7396. goto end;
  7397. if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
  7398. goto end;
  7399. if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
  7400. goto end;
  7401. testresult = 1;
  7402. end:
  7403. SSL_free(serverssl);
  7404. SSL_free(clientssl);
  7405. SSL_CTX_free(sctx);
  7406. SSL_CTX_free(cctx);
  7407. return testresult;
  7408. }
  7409. /*
  7410. * Test bi-directional shutdown.
  7411. * Test 0: TLSv1.2
  7412. * Test 1: TLSv1.2, server continues to read/write after client shutdown
  7413. * Test 2: TLSv1.3, no pending NewSessionTicket messages
  7414. * Test 3: TLSv1.3, pending NewSessionTicket messages
  7415. * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
  7416. * sends key update, client reads it
  7417. * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
  7418. * sends CertificateRequest, client reads and ignores it
  7419. * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
  7420. * doesn't read it
  7421. */
  7422. static int test_shutdown(int tst)
  7423. {
  7424. SSL_CTX *cctx = NULL, *sctx = NULL;
  7425. SSL *clientssl = NULL, *serverssl = NULL;
  7426. int testresult = 0;
  7427. char msg[] = "A test message";
  7428. char buf[80];
  7429. size_t written, readbytes;
  7430. SSL_SESSION *sess;
  7431. #ifdef OPENSSL_NO_TLS1_2
  7432. if (tst <= 1)
  7433. return 1;
  7434. #endif
  7435. #ifdef OSSL_NO_USABLE_TLS1_3
  7436. if (tst >= 2)
  7437. return 1;
  7438. #endif
  7439. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7440. TLS_client_method(),
  7441. TLS1_VERSION,
  7442. (tst <= 1) ? TLS1_2_VERSION
  7443. : TLS1_3_VERSION,
  7444. &sctx, &cctx, cert, privkey)))
  7445. goto end;
  7446. if (tst == 5)
  7447. SSL_CTX_set_post_handshake_auth(cctx, 1);
  7448. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7449. NULL, NULL)))
  7450. goto end;
  7451. if (tst == 3) {
  7452. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  7453. SSL_ERROR_NONE, 1, 0))
  7454. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7455. || !TEST_false(SSL_SESSION_is_resumable(sess)))
  7456. goto end;
  7457. } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  7458. SSL_ERROR_NONE))
  7459. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7460. || !TEST_true(SSL_SESSION_is_resumable(sess))) {
  7461. goto end;
  7462. }
  7463. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  7464. goto end;
  7465. if (tst >= 4) {
  7466. /*
  7467. * Reading on the server after the client has sent close_notify should
  7468. * fail and provide SSL_ERROR_ZERO_RETURN
  7469. */
  7470. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  7471. || !TEST_int_eq(SSL_get_error(serverssl, 0),
  7472. SSL_ERROR_ZERO_RETURN)
  7473. || !TEST_int_eq(SSL_get_shutdown(serverssl),
  7474. SSL_RECEIVED_SHUTDOWN)
  7475. /*
  7476. * Even though we're shutdown on receive we should still be
  7477. * able to write.
  7478. */
  7479. || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  7480. goto end;
  7481. if (tst == 4
  7482. && !TEST_true(SSL_key_update(serverssl,
  7483. SSL_KEY_UPDATE_REQUESTED)))
  7484. goto end;
  7485. if (tst == 5) {
  7486. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  7487. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  7488. goto end;
  7489. }
  7490. if ((tst == 4 || tst == 5)
  7491. && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  7492. goto end;
  7493. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  7494. goto end;
  7495. if (tst == 4 || tst == 5) {
  7496. /* Should still be able to read data from server */
  7497. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  7498. &readbytes))
  7499. || !TEST_size_t_eq(readbytes, sizeof(msg))
  7500. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
  7501. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  7502. &readbytes))
  7503. || !TEST_size_t_eq(readbytes, sizeof(msg))
  7504. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
  7505. goto end;
  7506. }
  7507. }
  7508. /* Writing on the client after sending close_notify shouldn't be possible */
  7509. if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
  7510. goto end;
  7511. if (tst < 4) {
  7512. /*
  7513. * For these tests the client has sent close_notify but it has not yet
  7514. * been received by the server. The server has not sent close_notify
  7515. * yet.
  7516. */
  7517. if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
  7518. /*
  7519. * Writing on the server after sending close_notify shouldn't
  7520. * be possible.
  7521. */
  7522. || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  7523. || !TEST_int_eq(SSL_shutdown(clientssl), 1)
  7524. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7525. || !TEST_true(SSL_SESSION_is_resumable(sess))
  7526. || !TEST_int_eq(SSL_shutdown(serverssl), 1))
  7527. goto end;
  7528. } else if (tst == 4 || tst == 5) {
  7529. /*
  7530. * In this test the client has sent close_notify and it has been
  7531. * received by the server which has responded with a close_notify. The
  7532. * client needs to read the close_notify sent by the server.
  7533. */
  7534. if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
  7535. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7536. || !TEST_true(SSL_SESSION_is_resumable(sess)))
  7537. goto end;
  7538. } else {
  7539. /*
  7540. * tst == 6
  7541. *
  7542. * The client has sent close_notify and is expecting a close_notify
  7543. * back, but instead there is application data first. The shutdown
  7544. * should fail with a fatal error.
  7545. */
  7546. if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
  7547. || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
  7548. goto end;
  7549. }
  7550. testresult = 1;
  7551. end:
  7552. SSL_free(serverssl);
  7553. SSL_free(clientssl);
  7554. SSL_CTX_free(sctx);
  7555. SSL_CTX_free(cctx);
  7556. return testresult;
  7557. }
  7558. /*
  7559. * Test that sending close_notify alerts works correctly in the case of a
  7560. * retryable write failure.
  7561. */
  7562. static int test_async_shutdown(void)
  7563. {
  7564. SSL_CTX *cctx = NULL, *sctx = NULL;
  7565. SSL *clientssl = NULL, *serverssl = NULL;
  7566. int testresult = 0;
  7567. BIO *bretry = BIO_new(bio_s_always_retry()), *tmp = NULL;
  7568. if (!TEST_ptr(bretry))
  7569. goto end;
  7570. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7571. TLS_client_method(),
  7572. 0, 0,
  7573. &sctx, &cctx, cert, privkey)))
  7574. goto end;
  7575. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  7576. NULL)))
  7577. goto end;
  7578. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7579. goto end;
  7580. /* Close write side of clientssl */
  7581. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  7582. goto end;
  7583. tmp = SSL_get_wbio(serverssl);
  7584. if (!TEST_true(BIO_up_ref(tmp))) {
  7585. tmp = NULL;
  7586. goto end;
  7587. }
  7588. SSL_set0_wbio(serverssl, bretry);
  7589. bretry = NULL;
  7590. /* First server shutdown should fail because of a retrable write failure */
  7591. if (!TEST_int_eq(SSL_shutdown(serverssl), -1)
  7592. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  7593. goto end;
  7594. /* Second server shutdown should fail for the same reason */
  7595. if (!TEST_int_eq(SSL_shutdown(serverssl), -1)
  7596. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  7597. goto end;
  7598. SSL_set0_wbio(serverssl, tmp);
  7599. tmp = NULL;
  7600. /* Third server shutdown should send close_notify */
  7601. if (!TEST_int_eq(SSL_shutdown(serverssl), 0))
  7602. goto end;
  7603. /* Fourth server shutdown should read close_notify from client and finish */
  7604. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  7605. goto end;
  7606. /* Client should also successfully fully shutdown */
  7607. if (!TEST_int_eq(SSL_shutdown(clientssl), 1))
  7608. goto end;
  7609. testresult = 1;
  7610. end:
  7611. SSL_free(serverssl);
  7612. SSL_free(clientssl);
  7613. SSL_CTX_free(sctx);
  7614. SSL_CTX_free(cctx);
  7615. BIO_free(bretry);
  7616. BIO_free(tmp);
  7617. return testresult;
  7618. }
  7619. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7620. static int cert_cb_cnt;
  7621. static int cert_cb(SSL *s, void *arg)
  7622. {
  7623. SSL_CTX *ctx = (SSL_CTX *)arg;
  7624. BIO *in = NULL;
  7625. EVP_PKEY *pkey = NULL;
  7626. X509 *x509 = NULL, *rootx = NULL;
  7627. STACK_OF(X509) *chain = NULL;
  7628. char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
  7629. int ret = 0;
  7630. if (cert_cb_cnt == 0) {
  7631. /* Suspend the handshake */
  7632. cert_cb_cnt++;
  7633. return -1;
  7634. } else if (cert_cb_cnt == 1) {
  7635. /*
  7636. * Update the SSL_CTX, set the certificate and private key and then
  7637. * continue the handshake normally.
  7638. */
  7639. if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
  7640. return 0;
  7641. if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
  7642. || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
  7643. SSL_FILETYPE_PEM))
  7644. || !TEST_true(SSL_check_private_key(s)))
  7645. return 0;
  7646. cert_cb_cnt++;
  7647. return 1;
  7648. } else if (cert_cb_cnt == 3) {
  7649. int rv;
  7650. rootfile = test_mk_file_path(certsdir, "rootcert.pem");
  7651. ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  7652. ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  7653. if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
  7654. goto out;
  7655. chain = sk_X509_new_null();
  7656. if (!TEST_ptr(chain))
  7657. goto out;
  7658. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7659. || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
  7660. || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
  7661. || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
  7662. || !TEST_true(sk_X509_push(chain, rootx)))
  7663. goto out;
  7664. rootx = NULL;
  7665. BIO_free(in);
  7666. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7667. || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
  7668. || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
  7669. || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
  7670. goto out;
  7671. BIO_free(in);
  7672. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7673. || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
  7674. || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  7675. NULL, NULL,
  7676. libctx, NULL)))
  7677. goto out;
  7678. rv = SSL_check_chain(s, x509, pkey, chain);
  7679. /*
  7680. * If the cert doesn't show as valid here (e.g., because we don't
  7681. * have any shared sigalgs), then we will not set it, and there will
  7682. * be no certificate at all on the SSL or SSL_CTX. This, in turn,
  7683. * will cause tls_choose_sigalgs() to fail the connection.
  7684. */
  7685. if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
  7686. == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
  7687. if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
  7688. goto out;
  7689. }
  7690. ret = 1;
  7691. }
  7692. /* Abort the handshake */
  7693. out:
  7694. OPENSSL_free(ecdsacert);
  7695. OPENSSL_free(ecdsakey);
  7696. OPENSSL_free(rootfile);
  7697. BIO_free(in);
  7698. EVP_PKEY_free(pkey);
  7699. X509_free(x509);
  7700. X509_free(rootx);
  7701. OSSL_STACK_OF_X509_free(chain);
  7702. return ret;
  7703. }
  7704. /*
  7705. * Test the certificate callback.
  7706. * Test 0: Callback fails
  7707. * Test 1: Success - no SSL_set_SSL_CTX() in the callback
  7708. * Test 2: Success - SSL_set_SSL_CTX() in the callback
  7709. * Test 3: Success - Call SSL_check_chain from the callback
  7710. * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
  7711. * chain
  7712. * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
  7713. */
  7714. static int test_cert_cb_int(int prot, int tst)
  7715. {
  7716. SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
  7717. SSL *clientssl = NULL, *serverssl = NULL;
  7718. int testresult = 0, ret;
  7719. #ifdef OPENSSL_NO_EC
  7720. /* We use an EC cert in these tests, so we skip in a no-ec build */
  7721. if (tst >= 3)
  7722. return 1;
  7723. #endif
  7724. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7725. TLS_client_method(),
  7726. TLS1_VERSION,
  7727. prot,
  7728. &sctx, &cctx, NULL, NULL)))
  7729. goto end;
  7730. if (tst == 0)
  7731. cert_cb_cnt = -1;
  7732. else if (tst >= 3)
  7733. cert_cb_cnt = 3;
  7734. else
  7735. cert_cb_cnt = 0;
  7736. if (tst == 2) {
  7737. snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7738. if (!TEST_ptr(snictx))
  7739. goto end;
  7740. }
  7741. SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
  7742. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7743. NULL, NULL)))
  7744. goto end;
  7745. if (tst == 4) {
  7746. /*
  7747. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7748. * the chain doesn't meet (the root uses an RSA cert)
  7749. */
  7750. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7751. "ecdsa_secp256r1_sha256")))
  7752. goto end;
  7753. } else if (tst == 5) {
  7754. /*
  7755. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7756. * the ee cert doesn't meet (the ee uses an ECDSA cert)
  7757. */
  7758. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7759. "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
  7760. goto end;
  7761. }
  7762. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  7763. if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
  7764. || (tst > 0
  7765. && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
  7766. goto end;
  7767. }
  7768. testresult = 1;
  7769. end:
  7770. SSL_free(serverssl);
  7771. SSL_free(clientssl);
  7772. SSL_CTX_free(sctx);
  7773. SSL_CTX_free(cctx);
  7774. SSL_CTX_free(snictx);
  7775. return testresult;
  7776. }
  7777. #endif
  7778. static int test_cert_cb(int tst)
  7779. {
  7780. int testresult = 1;
  7781. #ifndef OPENSSL_NO_TLS1_2
  7782. testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
  7783. #endif
  7784. #ifndef OSSL_NO_USABLE_TLS1_3
  7785. testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
  7786. #endif
  7787. return testresult;
  7788. }
  7789. static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
  7790. {
  7791. X509 *xcert;
  7792. EVP_PKEY *privpkey;
  7793. BIO *in = NULL;
  7794. BIO *priv_in = NULL;
  7795. /* Check that SSL_get0_peer_certificate() returns something sensible */
  7796. if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
  7797. return 0;
  7798. in = BIO_new_file(cert, "r");
  7799. if (!TEST_ptr(in))
  7800. return 0;
  7801. if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
  7802. || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
  7803. || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
  7804. || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
  7805. NULL, NULL,
  7806. libctx, NULL)))
  7807. goto err;
  7808. *x509 = xcert;
  7809. *pkey = privpkey;
  7810. BIO_free(in);
  7811. BIO_free(priv_in);
  7812. return 1;
  7813. err:
  7814. X509_free(xcert);
  7815. BIO_free(in);
  7816. BIO_free(priv_in);
  7817. return 0;
  7818. }
  7819. static int test_client_cert_cb(int tst)
  7820. {
  7821. SSL_CTX *cctx = NULL, *sctx = NULL;
  7822. SSL *clientssl = NULL, *serverssl = NULL;
  7823. int testresult = 0;
  7824. #ifdef OPENSSL_NO_TLS1_2
  7825. if (tst == 0)
  7826. return 1;
  7827. #endif
  7828. #ifdef OSSL_NO_USABLE_TLS1_3
  7829. if (tst == 1)
  7830. return 1;
  7831. #endif
  7832. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7833. TLS_client_method(),
  7834. TLS1_VERSION,
  7835. tst == 0 ? TLS1_2_VERSION
  7836. : TLS1_3_VERSION,
  7837. &sctx, &cctx, cert, privkey)))
  7838. goto end;
  7839. /*
  7840. * Test that setting a client_cert_cb results in a client certificate being
  7841. * sent.
  7842. */
  7843. SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
  7844. SSL_CTX_set_verify(sctx,
  7845. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  7846. verify_cb);
  7847. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7848. NULL, NULL))
  7849. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7850. SSL_ERROR_NONE)))
  7851. goto end;
  7852. testresult = 1;
  7853. end:
  7854. SSL_free(serverssl);
  7855. SSL_free(clientssl);
  7856. SSL_CTX_free(sctx);
  7857. SSL_CTX_free(cctx);
  7858. return testresult;
  7859. }
  7860. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7861. /*
  7862. * Test setting certificate authorities on both client and server.
  7863. *
  7864. * Test 0: SSL_CTX_set0_CA_list() only
  7865. * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
  7866. * Test 2: Only SSL_CTX_set_client_CA_list()
  7867. */
  7868. static int test_ca_names_int(int prot, int tst)
  7869. {
  7870. SSL_CTX *cctx = NULL, *sctx = NULL;
  7871. SSL *clientssl = NULL, *serverssl = NULL;
  7872. int testresult = 0;
  7873. size_t i;
  7874. X509_NAME *name[] = { NULL, NULL, NULL, NULL };
  7875. char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
  7876. STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
  7877. const STACK_OF(X509_NAME) *sktmp = NULL;
  7878. for (i = 0; i < OSSL_NELEM(name); i++) {
  7879. name[i] = X509_NAME_new();
  7880. if (!TEST_ptr(name[i])
  7881. || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
  7882. MBSTRING_ASC,
  7883. (unsigned char *)
  7884. strnames[i],
  7885. -1, -1, 0)))
  7886. goto end;
  7887. }
  7888. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7889. TLS_client_method(),
  7890. TLS1_VERSION,
  7891. prot,
  7892. &sctx, &cctx, cert, privkey)))
  7893. goto end;
  7894. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  7895. if (tst == 0 || tst == 1) {
  7896. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7897. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
  7898. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
  7899. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7900. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
  7901. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
  7902. goto end;
  7903. SSL_CTX_set0_CA_list(sctx, sk1);
  7904. SSL_CTX_set0_CA_list(cctx, sk2);
  7905. sk1 = sk2 = NULL;
  7906. }
  7907. if (tst == 1 || tst == 2) {
  7908. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7909. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
  7910. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
  7911. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7912. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
  7913. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
  7914. goto end;
  7915. SSL_CTX_set_client_CA_list(sctx, sk1);
  7916. SSL_CTX_set_client_CA_list(cctx, sk2);
  7917. sk1 = sk2 = NULL;
  7918. }
  7919. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7920. NULL, NULL))
  7921. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7922. SSL_ERROR_NONE)))
  7923. goto end;
  7924. /*
  7925. * We only expect certificate authorities to have been sent to the server
  7926. * if we are using TLSv1.3 and SSL_set0_CA_list() was used
  7927. */
  7928. sktmp = SSL_get0_peer_CA_list(serverssl);
  7929. if (prot == TLS1_3_VERSION
  7930. && (tst == 0 || tst == 1)) {
  7931. if (!TEST_ptr(sktmp)
  7932. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7933. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7934. name[0]), 0)
  7935. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7936. name[1]), 0))
  7937. goto end;
  7938. } else if (!TEST_ptr_null(sktmp)) {
  7939. goto end;
  7940. }
  7941. /*
  7942. * In all tests we expect certificate authorities to have been sent to the
  7943. * client. However, SSL_set_client_CA_list() should override
  7944. * SSL_set0_CA_list()
  7945. */
  7946. sktmp = SSL_get0_peer_CA_list(clientssl);
  7947. if (!TEST_ptr(sktmp)
  7948. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7949. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7950. name[tst == 0 ? 0 : 2]), 0)
  7951. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7952. name[tst == 0 ? 1 : 3]), 0))
  7953. goto end;
  7954. testresult = 1;
  7955. end:
  7956. SSL_free(serverssl);
  7957. SSL_free(clientssl);
  7958. SSL_CTX_free(sctx);
  7959. SSL_CTX_free(cctx);
  7960. for (i = 0; i < OSSL_NELEM(name); i++)
  7961. X509_NAME_free(name[i]);
  7962. sk_X509_NAME_pop_free(sk1, X509_NAME_free);
  7963. sk_X509_NAME_pop_free(sk2, X509_NAME_free);
  7964. return testresult;
  7965. }
  7966. #endif
  7967. static int test_ca_names(int tst)
  7968. {
  7969. int testresult = 1;
  7970. #ifndef OPENSSL_NO_TLS1_2
  7971. testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
  7972. #endif
  7973. #ifndef OSSL_NO_USABLE_TLS1_3
  7974. testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
  7975. #endif
  7976. return testresult;
  7977. }
  7978. #ifndef OPENSSL_NO_TLS1_2
  7979. static const char *multiblock_cipherlist_data[]=
  7980. {
  7981. "AES128-SHA",
  7982. "AES128-SHA256",
  7983. "AES256-SHA",
  7984. "AES256-SHA256",
  7985. };
  7986. /* Reduce the fragment size - so the multiblock test buffer can be small */
  7987. # define MULTIBLOCK_FRAGSIZE 512
  7988. static int test_multiblock_write(int test_index)
  7989. {
  7990. static const char *fetchable_ciphers[]=
  7991. {
  7992. "AES-128-CBC-HMAC-SHA1",
  7993. "AES-128-CBC-HMAC-SHA256",
  7994. "AES-256-CBC-HMAC-SHA1",
  7995. "AES-256-CBC-HMAC-SHA256"
  7996. };
  7997. const char *cipherlist = multiblock_cipherlist_data[test_index];
  7998. const SSL_METHOD *smeth = TLS_server_method();
  7999. const SSL_METHOD *cmeth = TLS_client_method();
  8000. int min_version = TLS1_VERSION;
  8001. int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
  8002. SSL_CTX *cctx = NULL, *sctx = NULL;
  8003. SSL *clientssl = NULL, *serverssl = NULL;
  8004. int testresult = 0;
  8005. /*
  8006. * Choose a buffer large enough to perform a multi-block operation
  8007. * i.e: write_len >= 4 * frag_size
  8008. * 9 * is chosen so that multiple multiblocks are used + some leftover.
  8009. */
  8010. unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
  8011. unsigned char buf[sizeof(msg)], *p = buf;
  8012. size_t readbytes, written, len;
  8013. EVP_CIPHER *ciph = NULL;
  8014. /*
  8015. * Check if the cipher exists before attempting to use it since it only has
  8016. * a hardware specific implementation.
  8017. */
  8018. ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
  8019. if (ciph == NULL) {
  8020. TEST_skip("Multiblock cipher is not available for %s", cipherlist);
  8021. return 1;
  8022. }
  8023. EVP_CIPHER_free(ciph);
  8024. /* Set up a buffer with some data that will be sent to the client */
  8025. RAND_bytes(msg, sizeof(msg));
  8026. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  8027. max_version, &sctx, &cctx, cert,
  8028. privkey)))
  8029. goto end;
  8030. if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
  8031. goto end;
  8032. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8033. NULL, NULL)))
  8034. goto end;
  8035. /* settings to force it to use AES-CBC-HMAC_SHA */
  8036. SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
  8037. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
  8038. goto end;
  8039. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8040. goto end;
  8041. if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  8042. || !TEST_size_t_eq(written, sizeof(msg)))
  8043. goto end;
  8044. len = written;
  8045. while (len > 0) {
  8046. if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
  8047. goto end;
  8048. p += readbytes;
  8049. len -= readbytes;
  8050. }
  8051. if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
  8052. goto end;
  8053. testresult = 1;
  8054. end:
  8055. SSL_free(serverssl);
  8056. SSL_free(clientssl);
  8057. SSL_CTX_free(sctx);
  8058. SSL_CTX_free(cctx);
  8059. return testresult;
  8060. }
  8061. #endif /* OPENSSL_NO_TLS1_2 */
  8062. static int test_session_timeout(int test)
  8063. {
  8064. /*
  8065. * Test session ordering and timeout
  8066. * Can't explicitly test performance of the new code,
  8067. * but can test to see if the ordering of the sessions
  8068. * are correct, and they are removed as expected
  8069. */
  8070. SSL_SESSION *early = NULL;
  8071. SSL_SESSION *middle = NULL;
  8072. SSL_SESSION *late = NULL;
  8073. SSL_CTX *ctx;
  8074. int testresult = 0;
  8075. long now = (long)time(NULL);
  8076. #define TIMEOUT 10
  8077. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  8078. || !TEST_ptr(early = SSL_SESSION_new())
  8079. || !TEST_ptr(middle = SSL_SESSION_new())
  8080. || !TEST_ptr(late = SSL_SESSION_new()))
  8081. goto end;
  8082. /* assign unique session ids */
  8083. early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8084. memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
  8085. middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8086. memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
  8087. late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  8088. memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
  8089. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8090. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  8091. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  8092. goto end;
  8093. /* Make sure they are all added */
  8094. if (!TEST_ptr(early->prev)
  8095. || !TEST_ptr(middle->prev)
  8096. || !TEST_ptr(late->prev))
  8097. goto end;
  8098. if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
  8099. || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
  8100. || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
  8101. goto end;
  8102. if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
  8103. || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
  8104. || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
  8105. goto end;
  8106. /* Make sure they are all still there */
  8107. if (!TEST_ptr(early->prev)
  8108. || !TEST_ptr(middle->prev)
  8109. || !TEST_ptr(late->prev))
  8110. goto end;
  8111. /* Make sure they are in the expected order */
  8112. if (!TEST_ptr_eq(late->next, middle)
  8113. || !TEST_ptr_eq(middle->next, early)
  8114. || !TEST_ptr_eq(early->prev, middle)
  8115. || !TEST_ptr_eq(middle->prev, late))
  8116. goto end;
  8117. /* This should remove "early" */
  8118. SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
  8119. if (!TEST_ptr_null(early->prev)
  8120. || !TEST_ptr(middle->prev)
  8121. || !TEST_ptr(late->prev))
  8122. goto end;
  8123. /* This should remove "middle" */
  8124. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
  8125. if (!TEST_ptr_null(early->prev)
  8126. || !TEST_ptr_null(middle->prev)
  8127. || !TEST_ptr(late->prev))
  8128. goto end;
  8129. /* This should remove "late" */
  8130. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
  8131. if (!TEST_ptr_null(early->prev)
  8132. || !TEST_ptr_null(middle->prev)
  8133. || !TEST_ptr_null(late->prev))
  8134. goto end;
  8135. /* Add them back in again */
  8136. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8137. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  8138. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  8139. goto end;
  8140. /* Make sure they are all added */
  8141. if (!TEST_ptr(early->prev)
  8142. || !TEST_ptr(middle->prev)
  8143. || !TEST_ptr(late->prev))
  8144. goto end;
  8145. /* This should remove all of them */
  8146. SSL_CTX_flush_sessions(ctx, 0);
  8147. if (!TEST_ptr_null(early->prev)
  8148. || !TEST_ptr_null(middle->prev)
  8149. || !TEST_ptr_null(late->prev))
  8150. goto end;
  8151. (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
  8152. | SSL_CTX_get_session_cache_mode(ctx));
  8153. /* make sure |now| is NOT equal to the current time */
  8154. now -= 10;
  8155. if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
  8156. || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  8157. || !TEST_long_ne(SSL_SESSION_get_time(early), now))
  8158. goto end;
  8159. testresult = 1;
  8160. end:
  8161. SSL_CTX_free(ctx);
  8162. SSL_SESSION_free(early);
  8163. SSL_SESSION_free(middle);
  8164. SSL_SESSION_free(late);
  8165. return testresult;
  8166. }
  8167. /*
  8168. * Test that a session cache overflow works as expected
  8169. * Test 0: TLSv1.3, timeout on new session later than old session
  8170. * Test 1: TLSv1.2, timeout on new session later than old session
  8171. * Test 2: TLSv1.3, timeout on new session earlier than old session
  8172. * Test 3: TLSv1.2, timeout on new session earlier than old session
  8173. */
  8174. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  8175. static int test_session_cache_overflow(int idx)
  8176. {
  8177. SSL_CTX *sctx = NULL, *cctx = NULL;
  8178. SSL *serverssl = NULL, *clientssl = NULL;
  8179. int testresult = 0;
  8180. SSL_SESSION *sess = NULL;
  8181. #ifdef OSSL_NO_USABLE_TLS1_3
  8182. /* If no TLSv1.3 available then do nothing in this case */
  8183. if (idx % 2 == 0)
  8184. return TEST_skip("No TLSv1.3 available");
  8185. #endif
  8186. #ifdef OPENSSL_NO_TLS1_2
  8187. /* If no TLSv1.2 available then do nothing in this case */
  8188. if (idx % 2 == 1)
  8189. return TEST_skip("No TLSv1.2 available");
  8190. #endif
  8191. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8192. TLS_client_method(), TLS1_VERSION,
  8193. (idx % 2 == 0) ? TLS1_3_VERSION
  8194. : TLS1_2_VERSION,
  8195. &sctx, &cctx, cert, privkey))
  8196. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET)))
  8197. goto end;
  8198. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  8199. get_sess_val = NULL;
  8200. SSL_CTX_sess_set_cache_size(sctx, 1);
  8201. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8202. NULL, NULL)))
  8203. goto end;
  8204. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8205. goto end;
  8206. if (idx > 1) {
  8207. sess = SSL_get_session(serverssl);
  8208. if (!TEST_ptr(sess))
  8209. goto end;
  8210. /*
  8211. * Cause this session to have a longer timeout than the next session to
  8212. * be added.
  8213. */
  8214. if (!TEST_true(SSL_SESSION_set_timeout(sess, LONG_MAX))) {
  8215. sess = NULL;
  8216. goto end;
  8217. }
  8218. sess = NULL;
  8219. }
  8220. SSL_shutdown(serverssl);
  8221. SSL_shutdown(clientssl);
  8222. SSL_free(serverssl);
  8223. SSL_free(clientssl);
  8224. serverssl = clientssl = NULL;
  8225. /*
  8226. * Session cache size is 1 and we already populated the cache with a session
  8227. * so the next connection should cause an overflow.
  8228. */
  8229. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8230. NULL, NULL)))
  8231. goto end;
  8232. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8233. goto end;
  8234. /*
  8235. * The session we just negotiated may have been already removed from the
  8236. * internal cache - but we will return it anyway from our external cache.
  8237. */
  8238. get_sess_val = SSL_get_session(serverssl);
  8239. if (!TEST_ptr(get_sess_val))
  8240. goto end;
  8241. sess = SSL_get1_session(clientssl);
  8242. if (!TEST_ptr(sess))
  8243. goto end;
  8244. SSL_shutdown(serverssl);
  8245. SSL_shutdown(clientssl);
  8246. SSL_free(serverssl);
  8247. SSL_free(clientssl);
  8248. serverssl = clientssl = NULL;
  8249. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8250. NULL, NULL)))
  8251. goto end;
  8252. if (!TEST_true(SSL_set_session(clientssl, sess)))
  8253. goto end;
  8254. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8255. goto end;
  8256. testresult = 1;
  8257. end:
  8258. SSL_free(serverssl);
  8259. SSL_free(clientssl);
  8260. SSL_CTX_free(sctx);
  8261. SSL_CTX_free(cctx);
  8262. SSL_SESSION_free(sess);
  8263. return testresult;
  8264. }
  8265. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  8266. /*
  8267. * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
  8268. * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
  8269. * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
  8270. * Test 3: Client does not set servername on initial handshake (TLSv1.2)
  8271. * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
  8272. * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
  8273. * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
  8274. * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
  8275. * Test 8: Client does not set servername on initial handshake(TLSv1.3)
  8276. * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
  8277. */
  8278. static int test_servername(int tst)
  8279. {
  8280. SSL_CTX *cctx = NULL, *sctx = NULL;
  8281. SSL *clientssl = NULL, *serverssl = NULL;
  8282. int testresult = 0;
  8283. SSL_SESSION *sess = NULL;
  8284. const char *sexpectedhost = NULL, *cexpectedhost = NULL;
  8285. #ifdef OPENSSL_NO_TLS1_2
  8286. if (tst <= 4)
  8287. return 1;
  8288. #endif
  8289. #ifdef OSSL_NO_USABLE_TLS1_3
  8290. if (tst >= 5)
  8291. return 1;
  8292. #endif
  8293. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8294. TLS_client_method(),
  8295. TLS1_VERSION,
  8296. (tst <= 4) ? TLS1_2_VERSION
  8297. : TLS1_3_VERSION,
  8298. &sctx, &cctx, cert, privkey))
  8299. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8300. NULL, NULL)))
  8301. goto end;
  8302. if (tst != 1 && tst != 6) {
  8303. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  8304. hostname_cb)))
  8305. goto end;
  8306. }
  8307. if (tst != 3 && tst != 8) {
  8308. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  8309. goto end;
  8310. sexpectedhost = cexpectedhost = "goodhost";
  8311. }
  8312. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8313. goto end;
  8314. if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
  8315. cexpectedhost)
  8316. || !TEST_str_eq(SSL_get_servername(serverssl,
  8317. TLSEXT_NAMETYPE_host_name),
  8318. sexpectedhost))
  8319. goto end;
  8320. /* Now repeat with a resumption handshake */
  8321. if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
  8322. || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
  8323. || !TEST_true(SSL_SESSION_is_resumable(sess))
  8324. || !TEST_int_eq(SSL_shutdown(serverssl), 0))
  8325. goto end;
  8326. SSL_free(clientssl);
  8327. SSL_free(serverssl);
  8328. clientssl = serverssl = NULL;
  8329. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  8330. NULL)))
  8331. goto end;
  8332. if (!TEST_true(SSL_set_session(clientssl, sess)))
  8333. goto end;
  8334. sexpectedhost = cexpectedhost = "goodhost";
  8335. if (tst == 2 || tst == 7) {
  8336. /* Set an inconsistent hostname */
  8337. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
  8338. goto end;
  8339. /*
  8340. * In TLSv1.2 we expect the hostname from the original handshake, in
  8341. * TLSv1.3 we expect the hostname from this handshake
  8342. */
  8343. if (tst == 7)
  8344. sexpectedhost = cexpectedhost = "altgoodhost";
  8345. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8346. TLSEXT_NAMETYPE_host_name),
  8347. "altgoodhost"))
  8348. goto end;
  8349. } else if (tst == 4 || tst == 9) {
  8350. /*
  8351. * A TLSv1.3 session does not associate a session with a servername,
  8352. * but a TLSv1.2 session does.
  8353. */
  8354. if (tst == 9)
  8355. sexpectedhost = cexpectedhost = NULL;
  8356. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8357. TLSEXT_NAMETYPE_host_name),
  8358. cexpectedhost))
  8359. goto end;
  8360. } else {
  8361. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  8362. goto end;
  8363. /*
  8364. * In a TLSv1.2 resumption where the hostname was not acknowledged
  8365. * we expect the hostname on the server to be empty. On the client we
  8366. * return what was requested in this case.
  8367. *
  8368. * Similarly if the client didn't set a hostname on an original TLSv1.2
  8369. * session but is now, the server hostname will be empty, but the client
  8370. * is as we set it.
  8371. */
  8372. if (tst == 1 || tst == 3)
  8373. sexpectedhost = NULL;
  8374. if (!TEST_str_eq(SSL_get_servername(clientssl,
  8375. TLSEXT_NAMETYPE_host_name),
  8376. "goodhost"))
  8377. goto end;
  8378. }
  8379. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8380. goto end;
  8381. if (!TEST_true(SSL_session_reused(clientssl))
  8382. || !TEST_true(SSL_session_reused(serverssl))
  8383. || !TEST_str_eq(SSL_get_servername(clientssl,
  8384. TLSEXT_NAMETYPE_host_name),
  8385. cexpectedhost)
  8386. || !TEST_str_eq(SSL_get_servername(serverssl,
  8387. TLSEXT_NAMETYPE_host_name),
  8388. sexpectedhost))
  8389. goto end;
  8390. testresult = 1;
  8391. end:
  8392. SSL_SESSION_free(sess);
  8393. SSL_free(serverssl);
  8394. SSL_free(clientssl);
  8395. SSL_CTX_free(sctx);
  8396. SSL_CTX_free(cctx);
  8397. return testresult;
  8398. }
  8399. static int test_unknown_sigalgs_groups(void)
  8400. {
  8401. int ret = 0;
  8402. SSL_CTX *ctx = NULL;
  8403. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  8404. goto end;
  8405. if (!TEST_int_gt(SSL_CTX_set1_sigalgs_list(ctx,
  8406. "RSA+SHA256:?nonexistent:?RSA+SHA512"),
  8407. 0))
  8408. goto end;
  8409. if (!TEST_size_t_eq(ctx->cert->conf_sigalgslen, 2)
  8410. || !TEST_int_eq(ctx->cert->conf_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256)
  8411. || !TEST_int_eq(ctx->cert->conf_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512))
  8412. goto end;
  8413. if (!TEST_int_gt(SSL_CTX_set1_client_sigalgs_list(ctx,
  8414. "RSA+SHA256:?nonexistent:?RSA+SHA512"),
  8415. 0))
  8416. goto end;
  8417. if (!TEST_size_t_eq(ctx->cert->client_sigalgslen, 2)
  8418. || !TEST_int_eq(ctx->cert->client_sigalgs[0], TLSEXT_SIGALG_rsa_pkcs1_sha256)
  8419. || !TEST_int_eq(ctx->cert->client_sigalgs[1], TLSEXT_SIGALG_rsa_pkcs1_sha512))
  8420. goto end;
  8421. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8422. "nonexistent"),
  8423. 0))
  8424. goto end;
  8425. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8426. "?nonexistent1:?nonexistent2:?nonexistent3"),
  8427. 0))
  8428. goto end;
  8429. #ifndef OPENSSL_NO_EC
  8430. if (!TEST_int_le(SSL_CTX_set1_groups_list(ctx,
  8431. "P-256:nonexistent"),
  8432. 0))
  8433. goto end;
  8434. if (!TEST_int_gt(SSL_CTX_set1_groups_list(ctx,
  8435. "P-384:?nonexistent:?P-521"),
  8436. 0))
  8437. goto end;
  8438. if (!TEST_size_t_eq(ctx->ext.supportedgroups_len, 2)
  8439. || !TEST_int_eq(ctx->ext.supportedgroups[0], OSSL_TLS_GROUP_ID_secp384r1)
  8440. || !TEST_int_eq(ctx->ext.supportedgroups[1], OSSL_TLS_GROUP_ID_secp521r1))
  8441. goto end;
  8442. #endif
  8443. ret = 1;
  8444. end:
  8445. SSL_CTX_free(ctx);
  8446. return ret;
  8447. }
  8448. #if !defined(OPENSSL_NO_EC) \
  8449. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  8450. /*
  8451. * Test that if signature algorithms are not available, then we do not offer or
  8452. * accept them.
  8453. * Test 0: Two RSA sig algs available: both RSA sig algs shared
  8454. * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
  8455. * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
  8456. * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
  8457. * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
  8458. * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
  8459. */
  8460. static int test_sigalgs_available(int idx)
  8461. {
  8462. SSL_CTX *cctx = NULL, *sctx = NULL;
  8463. SSL *clientssl = NULL, *serverssl = NULL;
  8464. int testresult = 0;
  8465. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  8466. OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
  8467. OSSL_PROVIDER *filterprov = NULL;
  8468. int sig, hash;
  8469. if (!TEST_ptr(tmpctx))
  8470. goto end;
  8471. if (idx != 0 && idx != 3) {
  8472. if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
  8473. filter_provider_init)))
  8474. goto end;
  8475. filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
  8476. if (!TEST_ptr(filterprov))
  8477. goto end;
  8478. if (idx < 3) {
  8479. /*
  8480. * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
  8481. * or accepted for the peer that uses this libctx. Note that libssl
  8482. * *requires* SHA2-256 to be available so we cannot disable that. We
  8483. * also need SHA1 for our certificate.
  8484. */
  8485. if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
  8486. "SHA2-256:SHA1")))
  8487. goto end;
  8488. } else {
  8489. if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
  8490. "ECDSA"))
  8491. # ifdef OPENSSL_NO_ECX
  8492. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT, "EC"))
  8493. # else
  8494. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
  8495. "EC:X25519:X448"))
  8496. # endif
  8497. )
  8498. goto end;
  8499. }
  8500. if (idx == 1 || idx == 4)
  8501. clientctx = tmpctx;
  8502. else
  8503. serverctx = tmpctx;
  8504. }
  8505. cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
  8506. sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
  8507. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  8508. goto end;
  8509. if (idx != 5) {
  8510. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8511. TLS_client_method(),
  8512. TLS1_VERSION,
  8513. 0,
  8514. &sctx, &cctx, cert, privkey)))
  8515. goto end;
  8516. } else {
  8517. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8518. TLS_client_method(),
  8519. TLS1_VERSION,
  8520. 0,
  8521. &sctx, &cctx, cert2, privkey2)))
  8522. goto end;
  8523. }
  8524. /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
  8525. if (idx < 4) {
  8526. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  8527. "ECDHE-RSA-AES128-GCM-SHA256")))
  8528. goto end;
  8529. } else {
  8530. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  8531. "ECDHE-ECDSA-AES128-GCM-SHA256")))
  8532. goto end;
  8533. }
  8534. if (idx < 3) {
  8535. if (!SSL_CTX_set1_sigalgs_list(cctx,
  8536. "rsa_pss_rsae_sha384"
  8537. ":rsa_pss_rsae_sha256")
  8538. || !SSL_CTX_set1_sigalgs_list(sctx,
  8539. "rsa_pss_rsae_sha384"
  8540. ":rsa_pss_rsae_sha256"))
  8541. goto end;
  8542. } else {
  8543. if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
  8544. || !SSL_CTX_set1_sigalgs_list(sctx,
  8545. "rsa_pss_rsae_sha256:ECDSA+SHA256"))
  8546. goto end;
  8547. }
  8548. if (idx != 5
  8549. && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
  8550. SSL_FILETYPE_PEM), 1)
  8551. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
  8552. privkey2,
  8553. SSL_FILETYPE_PEM), 1)
  8554. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
  8555. goto end;
  8556. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8557. NULL, NULL)))
  8558. goto end;
  8559. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8560. goto end;
  8561. /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
  8562. if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
  8563. NULL, NULL),
  8564. (idx == 0 || idx == 3) ? 2 : 1))
  8565. goto end;
  8566. if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
  8567. goto end;
  8568. if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
  8569. : NID_rsassaPss))
  8570. goto end;
  8571. testresult = filter_provider_check_clean_finish();
  8572. end:
  8573. SSL_free(serverssl);
  8574. SSL_free(clientssl);
  8575. SSL_CTX_free(sctx);
  8576. SSL_CTX_free(cctx);
  8577. OSSL_PROVIDER_unload(filterprov);
  8578. OSSL_LIB_CTX_free(tmpctx);
  8579. return testresult;
  8580. }
  8581. #endif /*
  8582. * !defined(OPENSSL_NO_EC) \
  8583. * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  8584. */
  8585. #ifndef OPENSSL_NO_TLS1_3
  8586. /* This test can run in TLSv1.3 even if ec and dh are disabled */
  8587. static int test_pluggable_group(int idx)
  8588. {
  8589. SSL_CTX *cctx = NULL, *sctx = NULL;
  8590. SSL *clientssl = NULL, *serverssl = NULL;
  8591. int testresult = 0;
  8592. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  8593. /* Check that we are not impacted by a provider without any groups */
  8594. OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
  8595. const char *group_name = idx == 0 ? "xorkemgroup" : "xorgroup";
  8596. if (!TEST_ptr(tlsprov))
  8597. goto end;
  8598. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8599. TLS_client_method(),
  8600. TLS1_3_VERSION,
  8601. TLS1_3_VERSION,
  8602. &sctx, &cctx, cert, privkey))
  8603. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8604. NULL, NULL)))
  8605. goto end;
  8606. /* ensure GROUPLIST_INCREMENT (=40) logic triggers: */
  8607. if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup:xorkemgroup:dummy1:dummy2:dummy3:dummy4:dummy5:dummy6:dummy7:dummy8:dummy9:dummy10:dummy11:dummy12:dummy13:dummy14:dummy15:dummy16:dummy17:dummy18:dummy19:dummy20:dummy21:dummy22:dummy23:dummy24:dummy25:dummy26:dummy27:dummy28:dummy29:dummy30:dummy31:dummy32:dummy33:dummy34:dummy35:dummy36:dummy37:dummy38:dummy39:dummy40:dummy41:dummy42:dummy43"))
  8608. /* removing a single algorithm from the list makes the test pass */
  8609. || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
  8610. goto end;
  8611. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8612. goto end;
  8613. if (!TEST_str_eq(group_name,
  8614. SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
  8615. goto end;
  8616. if (!TEST_str_eq(group_name, SSL_get0_group_name(serverssl))
  8617. || !TEST_str_eq(group_name, SSL_get0_group_name(clientssl)))
  8618. goto end;
  8619. testresult = 1;
  8620. end:
  8621. SSL_free(serverssl);
  8622. SSL_free(clientssl);
  8623. SSL_CTX_free(sctx);
  8624. SSL_CTX_free(cctx);
  8625. OSSL_PROVIDER_unload(tlsprov);
  8626. OSSL_PROVIDER_unload(legacyprov);
  8627. return testresult;
  8628. }
  8629. /*
  8630. * This function triggers encode, decode and sign functions
  8631. * of the artificial "xorhmacsig" algorithm implemented in tls-provider
  8632. * creating private key and certificate files for use in TLS testing.
  8633. */
  8634. static int create_cert_key(int idx, char *certfilename, char *privkeyfilename)
  8635. {
  8636. EVP_PKEY_CTX *evpctx = EVP_PKEY_CTX_new_from_name(libctx,
  8637. (idx == 0) ? "xorhmacsig" : "xorhmacsha2sig", NULL);
  8638. EVP_PKEY *pkey = NULL;
  8639. X509 *x509 = X509_new();
  8640. X509_NAME *name = NULL;
  8641. BIO *keybio = NULL, *certbio = NULL;
  8642. int ret = 1;
  8643. if (!TEST_ptr(evpctx)
  8644. || !TEST_true(EVP_PKEY_keygen_init(evpctx))
  8645. || !TEST_true(EVP_PKEY_generate(evpctx, &pkey))
  8646. || !TEST_ptr(pkey)
  8647. || !TEST_ptr(x509)
  8648. || !TEST_true(ASN1_INTEGER_set(X509_get_serialNumber(x509), 1))
  8649. || !TEST_true(X509_gmtime_adj(X509_getm_notBefore(x509), 0))
  8650. || !TEST_true(X509_gmtime_adj(X509_getm_notAfter(x509), 31536000L))
  8651. || !TEST_true(X509_set_pubkey(x509, pkey))
  8652. || !TEST_ptr(name = X509_get_subject_name(x509))
  8653. || !TEST_true(X509_NAME_add_entry_by_txt(name, "C", MBSTRING_ASC,
  8654. (unsigned char *)"CH", -1, -1, 0))
  8655. || !TEST_true(X509_NAME_add_entry_by_txt(name, "O", MBSTRING_ASC,
  8656. (unsigned char *)"test.org", -1, -1, 0))
  8657. || !TEST_true(X509_NAME_add_entry_by_txt(name, "CN", MBSTRING_ASC,
  8658. (unsigned char *)"localhost", -1, -1, 0))
  8659. || !TEST_true(X509_set_issuer_name(x509, name))
  8660. || !TEST_true(X509_sign(x509, pkey, EVP_sha1()))
  8661. || !TEST_ptr(keybio = BIO_new_file(privkeyfilename, "wb"))
  8662. || !TEST_true(PEM_write_bio_PrivateKey(keybio, pkey, NULL, NULL, 0, NULL, NULL))
  8663. || !TEST_ptr(certbio = BIO_new_file(certfilename, "wb"))
  8664. || !TEST_true(PEM_write_bio_X509(certbio, x509)))
  8665. ret = 0;
  8666. EVP_PKEY_free(pkey);
  8667. X509_free(x509);
  8668. EVP_PKEY_CTX_free(evpctx);
  8669. BIO_free(keybio);
  8670. BIO_free(certbio);
  8671. return ret;
  8672. }
  8673. /*
  8674. * Test that signature algorithms loaded via the provider interface can
  8675. * correctly establish a TLS (1.3) connection.
  8676. * Test 0: Signature algorithm with built-in hashing functionality: "xorhmacsig"
  8677. * Test 1: Signature algorithm using external SHA2 hashing: "xorhmacsha2sig"
  8678. * Test 2: Signature algorithm with built-in hashing configured via SSL_CONF_cmd
  8679. * Test 3: Test 0 using RPK
  8680. * Test 4: Test 1 using RPK
  8681. * Test 5: Test 2 using RPK
  8682. */
  8683. static int test_pluggable_signature(int idx)
  8684. {
  8685. static const unsigned char cert_type_rpk[] = { TLSEXT_cert_type_rpk, TLSEXT_cert_type_x509 };
  8686. SSL_CTX *cctx = NULL, *sctx = NULL;
  8687. SSL *clientssl = NULL, *serverssl = NULL;
  8688. int testresult = 0;
  8689. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  8690. OSSL_PROVIDER *defaultprov = OSSL_PROVIDER_load(libctx, "default");
  8691. char *certfilename = "tls-prov-cert.pem";
  8692. char *privkeyfilename = "tls-prov-key.pem";
  8693. int sigidx = idx % 3;
  8694. int rpkidx = idx / 3;
  8695. int do_conf_cmd = 0;
  8696. if (sigidx == 2) {
  8697. sigidx = 0;
  8698. do_conf_cmd = 1;
  8699. }
  8700. /* create key and certificate for the different algorithm types */
  8701. if (!TEST_ptr(tlsprov)
  8702. || !TEST_true(create_cert_key(sigidx, certfilename, privkeyfilename)))
  8703. goto end;
  8704. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8705. TLS_client_method(),
  8706. TLS1_3_VERSION,
  8707. TLS1_3_VERSION,
  8708. &sctx, &cctx, NULL, NULL)))
  8709. goto end;
  8710. if (do_conf_cmd) {
  8711. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  8712. if (!TEST_ptr(confctx))
  8713. goto end;
  8714. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  8715. | SSL_CONF_FLAG_SERVER
  8716. | SSL_CONF_FLAG_CERTIFICATE
  8717. | SSL_CONF_FLAG_REQUIRE_PRIVATE
  8718. | SSL_CONF_FLAG_SHOW_ERRORS);
  8719. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  8720. if (!TEST_int_gt(SSL_CONF_cmd(confctx, "Certificate", certfilename), 0)
  8721. || !TEST_int_gt(SSL_CONF_cmd(confctx, "PrivateKey", privkeyfilename), 0)
  8722. || !TEST_true(SSL_CONF_CTX_finish(confctx))) {
  8723. SSL_CONF_CTX_free(confctx);
  8724. goto end;
  8725. }
  8726. SSL_CONF_CTX_free(confctx);
  8727. } else {
  8728. if (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, certfilename,
  8729. SSL_FILETYPE_PEM), 1)
  8730. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
  8731. privkeyfilename,
  8732. SSL_FILETYPE_PEM), 1))
  8733. goto end;
  8734. }
  8735. if (!TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  8736. goto end;
  8737. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8738. NULL, NULL)))
  8739. goto end;
  8740. /* Enable RPK for server cert */
  8741. if (rpkidx) {
  8742. if (!TEST_true(SSL_set1_server_cert_type(serverssl, cert_type_rpk, sizeof(cert_type_rpk)))
  8743. || !TEST_true(SSL_set1_server_cert_type(clientssl, cert_type_rpk, sizeof(cert_type_rpk))))
  8744. goto end;
  8745. }
  8746. /* This is necessary to pass minimal setup w/o other groups configured */
  8747. if (!TEST_true(SSL_set1_groups_list(serverssl, "xorgroup"))
  8748. || !TEST_true(SSL_set1_groups_list(clientssl, "xorgroup")))
  8749. goto end;
  8750. /*
  8751. * If this connection gets established, it must have been completed
  8752. * via the tls-provider-implemented "hmacsig" algorithm, testing
  8753. * both sign and verify functions during handshake.
  8754. */
  8755. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8756. goto end;
  8757. /* If using RPK, make sure we got one */
  8758. if (rpkidx && !TEST_long_eq(SSL_get_verify_result(clientssl), X509_V_ERR_RPK_UNTRUSTED))
  8759. goto end;
  8760. testresult = 1;
  8761. end:
  8762. SSL_free(serverssl);
  8763. SSL_free(clientssl);
  8764. SSL_CTX_free(sctx);
  8765. SSL_CTX_free(cctx);
  8766. OSSL_PROVIDER_unload(tlsprov);
  8767. OSSL_PROVIDER_unload(defaultprov);
  8768. return testresult;
  8769. }
  8770. #endif
  8771. #ifndef OPENSSL_NO_TLS1_2
  8772. static int test_ssl_dup(void)
  8773. {
  8774. SSL_CTX *cctx = NULL, *sctx = NULL;
  8775. SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
  8776. int testresult = 0;
  8777. BIO *rbio = NULL, *wbio = NULL;
  8778. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8779. TLS_client_method(),
  8780. 0,
  8781. 0,
  8782. &sctx, &cctx, cert, privkey)))
  8783. goto end;
  8784. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8785. NULL, NULL)))
  8786. goto end;
  8787. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  8788. || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
  8789. goto end;
  8790. client2ssl = SSL_dup(clientssl);
  8791. rbio = SSL_get_rbio(clientssl);
  8792. if (!TEST_ptr(rbio)
  8793. || !TEST_true(BIO_up_ref(rbio)))
  8794. goto end;
  8795. SSL_set0_rbio(client2ssl, rbio);
  8796. rbio = NULL;
  8797. wbio = SSL_get_wbio(clientssl);
  8798. if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
  8799. goto end;
  8800. SSL_set0_wbio(client2ssl, wbio);
  8801. rbio = NULL;
  8802. if (!TEST_ptr(client2ssl)
  8803. /* Handshake not started so pointers should be different */
  8804. || !TEST_ptr_ne(clientssl, client2ssl))
  8805. goto end;
  8806. if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
  8807. || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
  8808. goto end;
  8809. if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
  8810. goto end;
  8811. SSL_free(clientssl);
  8812. clientssl = SSL_dup(client2ssl);
  8813. if (!TEST_ptr(clientssl)
  8814. /* Handshake has finished so pointers should be the same */
  8815. || !TEST_ptr_eq(clientssl, client2ssl))
  8816. goto end;
  8817. testresult = 1;
  8818. end:
  8819. SSL_free(serverssl);
  8820. SSL_free(clientssl);
  8821. SSL_free(client2ssl);
  8822. SSL_CTX_free(sctx);
  8823. SSL_CTX_free(cctx);
  8824. return testresult;
  8825. }
  8826. static int secret_cb(SSL *s, void *secretin, int *secret_len,
  8827. STACK_OF(SSL_CIPHER) *peer_ciphers,
  8828. const SSL_CIPHER **cipher, void *arg)
  8829. {
  8830. int i;
  8831. unsigned char *secret = secretin;
  8832. /* Just use a fixed master secret */
  8833. for (i = 0; i < *secret_len; i++)
  8834. secret[i] = 0xff;
  8835. /* We don't set a preferred cipher */
  8836. return 1;
  8837. }
  8838. /*
  8839. * Test the session_secret_cb which is designed for use with EAP-FAST
  8840. */
  8841. static int test_session_secret_cb(void)
  8842. {
  8843. SSL_CTX *cctx = NULL, *sctx = NULL;
  8844. SSL *clientssl = NULL, *serverssl = NULL;
  8845. SSL_SESSION *secret_sess = NULL;
  8846. int testresult = 0;
  8847. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8848. TLS_client_method(),
  8849. 0,
  8850. 0,
  8851. &sctx, &cctx, cert, privkey)))
  8852. goto end;
  8853. /* Create an initial connection and save the session */
  8854. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8855. NULL, NULL)))
  8856. goto end;
  8857. /* session_secret_cb does not support TLSv1.3 */
  8858. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  8859. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION)))
  8860. goto end;
  8861. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8862. goto end;
  8863. if (!TEST_ptr(secret_sess = SSL_get1_session(clientssl)))
  8864. goto end;
  8865. shutdown_ssl_connection(serverssl, clientssl);
  8866. serverssl = clientssl = NULL;
  8867. /* Resume the earlier session */
  8868. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8869. NULL, NULL)))
  8870. goto end;
  8871. /*
  8872. * No session ids for EAP-FAST - otherwise the state machine gets very
  8873. * confused.
  8874. */
  8875. if (!TEST_true(SSL_SESSION_set1_id(secret_sess, NULL, 0)))
  8876. goto end;
  8877. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  8878. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  8879. || !TEST_true(SSL_set_session_secret_cb(serverssl, secret_cb,
  8880. NULL))
  8881. || !TEST_true(SSL_set_session_secret_cb(clientssl, secret_cb,
  8882. NULL))
  8883. || !TEST_true(SSL_set_session(clientssl, secret_sess)))
  8884. goto end;
  8885. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8886. goto end;
  8887. testresult = 1;
  8888. end:
  8889. SSL_SESSION_free(secret_sess);
  8890. SSL_free(serverssl);
  8891. SSL_free(clientssl);
  8892. SSL_CTX_free(sctx);
  8893. SSL_CTX_free(cctx);
  8894. return testresult;
  8895. }
  8896. # ifndef OPENSSL_NO_DH
  8897. static EVP_PKEY *tmp_dh_params = NULL;
  8898. /* Helper function for the test_set_tmp_dh() tests */
  8899. static EVP_PKEY *get_tmp_dh_params(void)
  8900. {
  8901. if (tmp_dh_params == NULL) {
  8902. BIGNUM *p = NULL;
  8903. OSSL_PARAM_BLD *tmpl = NULL;
  8904. EVP_PKEY_CTX *pctx = NULL;
  8905. OSSL_PARAM *params = NULL;
  8906. EVP_PKEY *dhpkey = NULL;
  8907. p = BN_get_rfc3526_prime_2048(NULL);
  8908. if (!TEST_ptr(p))
  8909. goto end;
  8910. pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
  8911. if (!TEST_ptr(pctx)
  8912. || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
  8913. goto end;
  8914. tmpl = OSSL_PARAM_BLD_new();
  8915. if (!TEST_ptr(tmpl)
  8916. || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
  8917. OSSL_PKEY_PARAM_FFC_P,
  8918. p))
  8919. || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
  8920. OSSL_PKEY_PARAM_FFC_G,
  8921. 2)))
  8922. goto end;
  8923. params = OSSL_PARAM_BLD_to_param(tmpl);
  8924. if (!TEST_ptr(params)
  8925. || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
  8926. EVP_PKEY_KEY_PARAMETERS,
  8927. params), 1))
  8928. goto end;
  8929. tmp_dh_params = dhpkey;
  8930. end:
  8931. BN_free(p);
  8932. EVP_PKEY_CTX_free(pctx);
  8933. OSSL_PARAM_BLD_free(tmpl);
  8934. OSSL_PARAM_free(params);
  8935. }
  8936. if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
  8937. return NULL;
  8938. return tmp_dh_params;
  8939. }
  8940. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8941. /* Callback used by test_set_tmp_dh() */
  8942. static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
  8943. {
  8944. EVP_PKEY *dhpkey = get_tmp_dh_params();
  8945. DH *ret = NULL;
  8946. if (!TEST_ptr(dhpkey))
  8947. return NULL;
  8948. /*
  8949. * libssl does not free the returned DH, so we free it now knowing that even
  8950. * after we free dhpkey, there will still be a reference to the owning
  8951. * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
  8952. * of time we need it for.
  8953. */
  8954. ret = EVP_PKEY_get1_DH(dhpkey);
  8955. DH_free(ret);
  8956. EVP_PKEY_free(dhpkey);
  8957. return ret;
  8958. }
  8959. # endif
  8960. /*
  8961. * Test the various methods for setting temporary DH parameters
  8962. *
  8963. * Test 0: Default (no auto) setting
  8964. * Test 1: Explicit SSL_CTX auto off
  8965. * Test 2: Explicit SSL auto off
  8966. * Test 3: Explicit SSL_CTX auto on
  8967. * Test 4: Explicit SSL auto on
  8968. * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
  8969. * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
  8970. *
  8971. * The following are testing deprecated APIs, so we only run them if available
  8972. * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
  8973. * Test 8: Explicit SSL auto off, custom DH params via DH
  8974. * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
  8975. * Test 10: Explicit SSL auto off, custom DH params via callback
  8976. */
  8977. static int test_set_tmp_dh(int idx)
  8978. {
  8979. SSL_CTX *cctx = NULL, *sctx = NULL;
  8980. SSL *clientssl = NULL, *serverssl = NULL;
  8981. int testresult = 0;
  8982. int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
  8983. int expected = (idx <= 2) ? 0 : 1;
  8984. EVP_PKEY *dhpkey = NULL;
  8985. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8986. DH *dh = NULL;
  8987. # else
  8988. if (idx >= 7)
  8989. return 1;
  8990. # endif
  8991. if (idx >= 5 && idx <= 8) {
  8992. dhpkey = get_tmp_dh_params();
  8993. if (!TEST_ptr(dhpkey))
  8994. goto end;
  8995. }
  8996. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8997. if (idx == 7 || idx == 8) {
  8998. dh = EVP_PKEY_get1_DH(dhpkey);
  8999. if (!TEST_ptr(dh))
  9000. goto end;
  9001. }
  9002. # endif
  9003. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9004. TLS_client_method(),
  9005. 0,
  9006. 0,
  9007. &sctx, &cctx, cert, privkey)))
  9008. goto end;
  9009. if ((idx & 1) == 1) {
  9010. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
  9011. goto end;
  9012. }
  9013. if (idx == 5) {
  9014. if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
  9015. goto end;
  9016. dhpkey = NULL;
  9017. }
  9018. # ifndef OPENSSL_NO_DEPRECATED_3_0
  9019. else if (idx == 7) {
  9020. if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
  9021. goto end;
  9022. } else if (idx == 9) {
  9023. SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
  9024. }
  9025. # endif
  9026. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  9027. NULL, NULL)))
  9028. goto end;
  9029. if ((idx & 1) == 0 && idx != 0) {
  9030. if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
  9031. goto end;
  9032. }
  9033. if (idx == 6) {
  9034. if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
  9035. goto end;
  9036. dhpkey = NULL;
  9037. }
  9038. # ifndef OPENSSL_NO_DEPRECATED_3_0
  9039. else if (idx == 8) {
  9040. if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
  9041. goto end;
  9042. } else if (idx == 10) {
  9043. SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
  9044. }
  9045. # endif
  9046. if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  9047. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  9048. || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
  9049. goto end;
  9050. /*
  9051. * If autoon then we should succeed. Otherwise we expect failure because
  9052. * there are no parameters
  9053. */
  9054. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  9055. SSL_ERROR_NONE), expected))
  9056. goto end;
  9057. testresult = 1;
  9058. end:
  9059. # ifndef OPENSSL_NO_DEPRECATED_3_0
  9060. DH_free(dh);
  9061. # endif
  9062. SSL_free(serverssl);
  9063. SSL_free(clientssl);
  9064. SSL_CTX_free(sctx);
  9065. SSL_CTX_free(cctx);
  9066. EVP_PKEY_free(dhpkey);
  9067. return testresult;
  9068. }
  9069. /*
  9070. * Test the auto DH keys are appropriately sized
  9071. */
  9072. static int test_dh_auto(int idx)
  9073. {
  9074. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
  9075. SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9076. SSL *clientssl = NULL, *serverssl = NULL;
  9077. int testresult = 0;
  9078. EVP_PKEY *tmpkey = NULL;
  9079. char *thiscert = NULL, *thiskey = NULL;
  9080. size_t expdhsize = 0;
  9081. const char *ciphersuite = "DHE-RSA-AES128-SHA";
  9082. if (!TEST_ptr(sctx) || !TEST_ptr(cctx))
  9083. goto end;
  9084. switch (idx) {
  9085. case 0:
  9086. /* The FIPS provider doesn't support this DH size - so we ignore it */
  9087. if (is_fips) {
  9088. testresult = 1;
  9089. goto end;
  9090. }
  9091. thiscert = cert1024;
  9092. thiskey = privkey1024;
  9093. expdhsize = 1024;
  9094. SSL_CTX_set_security_level(sctx, 1);
  9095. SSL_CTX_set_security_level(cctx, 1);
  9096. break;
  9097. case 1:
  9098. /* 2048 bit prime */
  9099. thiscert = cert;
  9100. thiskey = privkey;
  9101. expdhsize = 2048;
  9102. break;
  9103. case 2:
  9104. thiscert = cert3072;
  9105. thiskey = privkey3072;
  9106. expdhsize = 3072;
  9107. break;
  9108. case 3:
  9109. thiscert = cert4096;
  9110. thiskey = privkey4096;
  9111. expdhsize = 4096;
  9112. break;
  9113. case 4:
  9114. thiscert = cert8192;
  9115. thiskey = privkey8192;
  9116. expdhsize = 8192;
  9117. break;
  9118. /* No certificate cases */
  9119. case 5:
  9120. /* The FIPS provider doesn't support this DH size - so we ignore it */
  9121. if (is_fips) {
  9122. testresult = 1;
  9123. goto end;
  9124. }
  9125. ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
  9126. expdhsize = 1024;
  9127. break;
  9128. case 6:
  9129. ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
  9130. expdhsize = 3072;
  9131. break;
  9132. default:
  9133. TEST_error("Invalid text index");
  9134. goto end;
  9135. }
  9136. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL,
  9137. NULL,
  9138. 0,
  9139. 0,
  9140. &sctx, &cctx, thiscert, thiskey)))
  9141. goto end;
  9142. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  9143. NULL, NULL)))
  9144. goto end;
  9145. if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
  9146. || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  9147. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  9148. || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
  9149. || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
  9150. goto end;
  9151. /*
  9152. * Send the server's first flight. At this point the server has created the
  9153. * temporary DH key but hasn't finished using it yet. Once used it is
  9154. * removed, so we cannot test it.
  9155. */
  9156. if (!TEST_int_le(SSL_connect(clientssl), 0)
  9157. || !TEST_int_le(SSL_accept(serverssl), 0))
  9158. goto end;
  9159. if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
  9160. goto end;
  9161. if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
  9162. goto end;
  9163. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9164. goto end;
  9165. testresult = 1;
  9166. end:
  9167. SSL_free(serverssl);
  9168. SSL_free(clientssl);
  9169. SSL_CTX_free(sctx);
  9170. SSL_CTX_free(cctx);
  9171. EVP_PKEY_free(tmpkey);
  9172. return testresult;
  9173. }
  9174. # endif /* OPENSSL_NO_DH */
  9175. #endif /* OPENSSL_NO_TLS1_2 */
  9176. #ifndef OSSL_NO_USABLE_TLS1_3
  9177. /*
  9178. * Test that setting an SNI callback works with TLSv1.3. Specifically we check
  9179. * that it works even without a certificate configured for the original
  9180. * SSL_CTX
  9181. */
  9182. static int test_sni_tls13(void)
  9183. {
  9184. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  9185. SSL *clientssl = NULL, *serverssl = NULL;
  9186. int testresult = 0;
  9187. /* Reset callback counter */
  9188. snicb = 0;
  9189. /* Create an initial SSL_CTX with no certificate configured */
  9190. sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9191. if (!TEST_ptr(sctx))
  9192. goto end;
  9193. /* Require TLSv1.3 as a minimum */
  9194. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9195. TLS_client_method(), TLS1_3_VERSION, 0,
  9196. &sctx2, &cctx, cert, privkey)))
  9197. goto end;
  9198. /* Set up SNI */
  9199. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  9200. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  9201. goto end;
  9202. /*
  9203. * Connection should still succeed because the final SSL_CTX has the right
  9204. * certificates configured.
  9205. */
  9206. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9207. &clientssl, NULL, NULL))
  9208. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  9209. SSL_ERROR_NONE)))
  9210. goto end;
  9211. /* We should have had the SNI callback called exactly once */
  9212. if (!TEST_int_eq(snicb, 1))
  9213. goto end;
  9214. testresult = 1;
  9215. end:
  9216. SSL_free(serverssl);
  9217. SSL_free(clientssl);
  9218. SSL_CTX_free(sctx2);
  9219. SSL_CTX_free(sctx);
  9220. SSL_CTX_free(cctx);
  9221. return testresult;
  9222. }
  9223. /*
  9224. * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
  9225. * 0 = TLSv1.2
  9226. * 1 = TLSv1.3
  9227. */
  9228. static int test_ticket_lifetime(int idx)
  9229. {
  9230. SSL_CTX *cctx = NULL, *sctx = NULL;
  9231. SSL *clientssl = NULL, *serverssl = NULL;
  9232. int testresult = 0;
  9233. int version = TLS1_3_VERSION;
  9234. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  9235. #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
  9236. if (idx == 0) {
  9237. #ifdef OPENSSL_NO_TLS1_2
  9238. return TEST_skip("TLS 1.2 is disabled.");
  9239. #else
  9240. version = TLS1_2_VERSION;
  9241. #endif
  9242. }
  9243. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9244. TLS_client_method(), version, version,
  9245. &sctx, &cctx, cert, privkey)))
  9246. goto end;
  9247. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9248. &clientssl, NULL, NULL)))
  9249. goto end;
  9250. /*
  9251. * Set the timeout to be more than 1 week
  9252. * make sure the returned value is the default
  9253. */
  9254. if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
  9255. SSL_get_default_timeout(serverssl)))
  9256. goto end;
  9257. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9258. goto end;
  9259. if (idx == 0) {
  9260. /* TLSv1.2 uses the set value */
  9261. if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
  9262. goto end;
  9263. } else {
  9264. /* TLSv1.3 uses the limited value */
  9265. if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
  9266. goto end;
  9267. }
  9268. testresult = 1;
  9269. end:
  9270. SSL_free(serverssl);
  9271. SSL_free(clientssl);
  9272. SSL_CTX_free(sctx);
  9273. SSL_CTX_free(cctx);
  9274. return testresult;
  9275. }
  9276. #endif
  9277. /*
  9278. * Test that setting an ALPN does not violate RFC
  9279. */
  9280. static int test_set_alpn(void)
  9281. {
  9282. SSL_CTX *ctx = NULL;
  9283. SSL *ssl = NULL;
  9284. int testresult = 0;
  9285. unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
  9286. unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
  9287. unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
  9288. unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
  9289. unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
  9290. unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
  9291. /* Create an initial SSL_CTX with no certificate configured */
  9292. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9293. if (!TEST_ptr(ctx))
  9294. goto end;
  9295. /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
  9296. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
  9297. goto end;
  9298. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
  9299. goto end;
  9300. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
  9301. goto end;
  9302. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
  9303. goto end;
  9304. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
  9305. goto end;
  9306. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
  9307. goto end;
  9308. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
  9309. goto end;
  9310. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
  9311. goto end;
  9312. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
  9313. goto end;
  9314. ssl = SSL_new(ctx);
  9315. if (!TEST_ptr(ssl))
  9316. goto end;
  9317. if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
  9318. goto end;
  9319. if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
  9320. goto end;
  9321. if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
  9322. goto end;
  9323. if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
  9324. goto end;
  9325. if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
  9326. goto end;
  9327. if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
  9328. goto end;
  9329. if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
  9330. goto end;
  9331. if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
  9332. goto end;
  9333. if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
  9334. goto end;
  9335. testresult = 1;
  9336. end:
  9337. SSL_free(ssl);
  9338. SSL_CTX_free(ctx);
  9339. return testresult;
  9340. }
  9341. /*
  9342. * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
  9343. */
  9344. static int test_set_verify_cert_store_ssl_ctx(void)
  9345. {
  9346. SSL_CTX *ctx = NULL;
  9347. int testresult = 0;
  9348. X509_STORE *store = NULL, *new_store = NULL,
  9349. *cstore = NULL, *new_cstore = NULL;
  9350. /* Create an initial SSL_CTX. */
  9351. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9352. if (!TEST_ptr(ctx))
  9353. goto end;
  9354. /* Retrieve verify store pointer. */
  9355. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9356. goto end;
  9357. /* Retrieve chain store pointer. */
  9358. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9359. goto end;
  9360. /* We haven't set any yet, so this should be NULL. */
  9361. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9362. goto end;
  9363. /* Create stores. We use separate stores so pointers are different. */
  9364. new_store = X509_STORE_new();
  9365. if (!TEST_ptr(new_store))
  9366. goto end;
  9367. new_cstore = X509_STORE_new();
  9368. if (!TEST_ptr(new_cstore))
  9369. goto end;
  9370. /* Set stores. */
  9371. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
  9372. goto end;
  9373. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
  9374. goto end;
  9375. /* Should be able to retrieve the same pointer. */
  9376. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9377. goto end;
  9378. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9379. goto end;
  9380. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  9381. goto end;
  9382. /* Should be able to unset again. */
  9383. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
  9384. goto end;
  9385. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
  9386. goto end;
  9387. /* Should now be NULL. */
  9388. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  9389. goto end;
  9390. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  9391. goto end;
  9392. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9393. goto end;
  9394. testresult = 1;
  9395. end:
  9396. X509_STORE_free(new_store);
  9397. X509_STORE_free(new_cstore);
  9398. SSL_CTX_free(ctx);
  9399. return testresult;
  9400. }
  9401. /*
  9402. * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
  9403. */
  9404. static int test_set_verify_cert_store_ssl(void)
  9405. {
  9406. SSL_CTX *ctx = NULL;
  9407. SSL *ssl = NULL;
  9408. int testresult = 0;
  9409. X509_STORE *store = NULL, *new_store = NULL,
  9410. *cstore = NULL, *new_cstore = NULL;
  9411. /* Create an initial SSL_CTX. */
  9412. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9413. if (!TEST_ptr(ctx))
  9414. goto end;
  9415. /* Create an SSL object. */
  9416. ssl = SSL_new(ctx);
  9417. if (!TEST_ptr(ssl))
  9418. goto end;
  9419. /* Retrieve verify store pointer. */
  9420. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9421. goto end;
  9422. /* Retrieve chain store pointer. */
  9423. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9424. goto end;
  9425. /* We haven't set any yet, so this should be NULL. */
  9426. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9427. goto end;
  9428. /* Create stores. We use separate stores so pointers are different. */
  9429. new_store = X509_STORE_new();
  9430. if (!TEST_ptr(new_store))
  9431. goto end;
  9432. new_cstore = X509_STORE_new();
  9433. if (!TEST_ptr(new_cstore))
  9434. goto end;
  9435. /* Set stores. */
  9436. if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
  9437. goto end;
  9438. if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
  9439. goto end;
  9440. /* Should be able to retrieve the same pointer. */
  9441. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9442. goto end;
  9443. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9444. goto end;
  9445. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  9446. goto end;
  9447. /* Should be able to unset again. */
  9448. if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
  9449. goto end;
  9450. if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
  9451. goto end;
  9452. /* Should now be NULL. */
  9453. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  9454. goto end;
  9455. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  9456. goto end;
  9457. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  9458. goto end;
  9459. testresult = 1;
  9460. end:
  9461. X509_STORE_free(new_store);
  9462. X509_STORE_free(new_cstore);
  9463. SSL_free(ssl);
  9464. SSL_CTX_free(ctx);
  9465. return testresult;
  9466. }
  9467. static int test_inherit_verify_param(void)
  9468. {
  9469. int testresult = 0;
  9470. SSL_CTX *ctx = NULL;
  9471. X509_VERIFY_PARAM *cp = NULL;
  9472. SSL *ssl = NULL;
  9473. X509_VERIFY_PARAM *sp = NULL;
  9474. int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  9475. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  9476. if (!TEST_ptr(ctx))
  9477. goto end;
  9478. cp = SSL_CTX_get0_param(ctx);
  9479. if (!TEST_ptr(cp))
  9480. goto end;
  9481. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
  9482. goto end;
  9483. X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
  9484. ssl = SSL_new(ctx);
  9485. if (!TEST_ptr(ssl))
  9486. goto end;
  9487. sp = SSL_get0_param(ssl);
  9488. if (!TEST_ptr(sp))
  9489. goto end;
  9490. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
  9491. goto end;
  9492. testresult = 1;
  9493. end:
  9494. SSL_free(ssl);
  9495. SSL_CTX_free(ctx);
  9496. return testresult;
  9497. }
  9498. static int test_load_dhfile(void)
  9499. {
  9500. #ifndef OPENSSL_NO_DH
  9501. int testresult = 0;
  9502. SSL_CTX *ctx = NULL;
  9503. SSL_CONF_CTX *cctx = NULL;
  9504. if (dhfile == NULL)
  9505. return 1;
  9506. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
  9507. || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
  9508. goto end;
  9509. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  9510. SSL_CONF_CTX_set_flags(cctx,
  9511. SSL_CONF_FLAG_CERTIFICATE
  9512. | SSL_CONF_FLAG_SERVER
  9513. | SSL_CONF_FLAG_FILE);
  9514. if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
  9515. goto end;
  9516. testresult = 1;
  9517. end:
  9518. SSL_CONF_CTX_free(cctx);
  9519. SSL_CTX_free(ctx);
  9520. return testresult;
  9521. #else
  9522. return TEST_skip("DH not supported by this build");
  9523. #endif
  9524. }
  9525. #ifndef OSSL_NO_USABLE_TLS1_3
  9526. /* Test that read_ahead works across a key change */
  9527. static int test_read_ahead_key_change(void)
  9528. {
  9529. SSL_CTX *cctx = NULL, *sctx = NULL;
  9530. SSL *clientssl = NULL, *serverssl = NULL;
  9531. int testresult = 0;
  9532. char *msg = "Hello World";
  9533. size_t written, readbytes;
  9534. char buf[80];
  9535. int i;
  9536. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9537. TLS_client_method(), TLS1_3_VERSION, 0,
  9538. &sctx, &cctx, cert, privkey)))
  9539. goto end;
  9540. SSL_CTX_set_read_ahead(sctx, 1);
  9541. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9542. &clientssl, NULL, NULL)))
  9543. goto end;
  9544. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9545. goto end;
  9546. /* Write some data, send a key update, write more data */
  9547. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9548. || !TEST_size_t_eq(written, strlen(msg)))
  9549. goto end;
  9550. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  9551. goto end;
  9552. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9553. || !TEST_size_t_eq(written, strlen(msg)))
  9554. goto end;
  9555. /*
  9556. * Since read_ahead is on the first read below should read the record with
  9557. * the first app data, the second record with the key update message, and
  9558. * the third record with the app data all in one go. We should be able to
  9559. * still process the read_ahead data correctly even though it crosses
  9560. * epochs
  9561. */
  9562. for (i = 0; i < 2; i++) {
  9563. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
  9564. &readbytes)))
  9565. goto end;
  9566. buf[readbytes] = '\0';
  9567. if (!TEST_str_eq(buf, msg))
  9568. goto end;
  9569. }
  9570. testresult = 1;
  9571. end:
  9572. SSL_free(serverssl);
  9573. SSL_free(clientssl);
  9574. SSL_CTX_free(sctx);
  9575. SSL_CTX_free(cctx);
  9576. return testresult;
  9577. }
  9578. static size_t record_pad_cb(SSL *s, int type, size_t len, void *arg)
  9579. {
  9580. int *called = arg;
  9581. switch ((*called)++) {
  9582. case 0:
  9583. /* Add some padding to first record */
  9584. return 512;
  9585. case 1:
  9586. /* Maximally pad the second record */
  9587. return SSL3_RT_MAX_PLAIN_LENGTH - len;
  9588. case 2:
  9589. /*
  9590. * Exceeding the maximum padding should be fine. It should just pad to
  9591. * the maximum anyway
  9592. */
  9593. return SSL3_RT_MAX_PLAIN_LENGTH + 1 - len;
  9594. case 3:
  9595. /*
  9596. * Very large padding should also be ok. Should just pad to the maximum
  9597. * allowed
  9598. */
  9599. return SIZE_MAX;
  9600. default:
  9601. return 0;
  9602. }
  9603. }
  9604. /*
  9605. * Test that setting record padding in TLSv1.3 works as expected
  9606. * Test 0: Record padding callback on the SSL_CTX
  9607. * Test 1: Record padding callback on the SSL
  9608. * Test 2: Record block padding on the SSL_CTX
  9609. * Test 3: Record block padding on the SSL
  9610. */
  9611. static int test_tls13_record_padding(int idx)
  9612. {
  9613. SSL_CTX *cctx = NULL, *sctx = NULL;
  9614. SSL *clientssl = NULL, *serverssl = NULL;
  9615. int testresult = 0;
  9616. char *msg = "Hello World";
  9617. size_t written, readbytes;
  9618. char buf[80];
  9619. int i;
  9620. int called = 0;
  9621. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9622. TLS_client_method(), TLS1_3_VERSION, 0,
  9623. &sctx, &cctx, cert, privkey)))
  9624. goto end;
  9625. if (idx == 0) {
  9626. SSL_CTX_set_record_padding_callback(cctx, record_pad_cb);
  9627. SSL_CTX_set_record_padding_callback_arg(cctx, &called);
  9628. if (!TEST_ptr_eq(SSL_CTX_get_record_padding_callback_arg(cctx), &called))
  9629. goto end;
  9630. } else if (idx == 2) {
  9631. /* Exceeding the max plain length should fail */
  9632. if (!TEST_false(SSL_CTX_set_block_padding(cctx,
  9633. SSL3_RT_MAX_PLAIN_LENGTH + 1)))
  9634. goto end;
  9635. if (!TEST_true(SSL_CTX_set_block_padding(cctx, 512)))
  9636. goto end;
  9637. }
  9638. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9639. &clientssl, NULL, NULL)))
  9640. goto end;
  9641. if (idx == 1) {
  9642. SSL_set_record_padding_callback(clientssl, record_pad_cb);
  9643. SSL_set_record_padding_callback_arg(clientssl, &called);
  9644. if (!TEST_ptr_eq(SSL_get_record_padding_callback_arg(clientssl), &called))
  9645. goto end;
  9646. } else if (idx == 3) {
  9647. /* Exceeding the max plain length should fail */
  9648. if (!TEST_false(SSL_set_block_padding(clientssl,
  9649. SSL3_RT_MAX_PLAIN_LENGTH + 1)))
  9650. goto end;
  9651. if (!TEST_true(SSL_set_block_padding(clientssl, 512)))
  9652. goto end;
  9653. }
  9654. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9655. goto end;
  9656. called = 0;
  9657. /*
  9658. * Write some data, then check we can read it. Do this four times to check
  9659. * we can continue to write and read padded data after the initial record
  9660. * padding has been added. We don't actually check that the padding has
  9661. * been applied to the record - just that we can continue to communicate
  9662. * normally and that the callback has been called (if appropriate).
  9663. */
  9664. for (i = 0; i < 4; i++) {
  9665. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  9666. || !TEST_size_t_eq(written, strlen(msg)))
  9667. goto end;
  9668. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
  9669. &readbytes))
  9670. || !TEST_size_t_eq(written, readbytes))
  9671. goto end;
  9672. buf[readbytes] = '\0';
  9673. if (!TEST_str_eq(buf, msg))
  9674. goto end;
  9675. }
  9676. if ((idx == 0 || idx == 1) && !TEST_int_eq(called, 4))
  9677. goto end;
  9678. testresult = 1;
  9679. end:
  9680. SSL_free(serverssl);
  9681. SSL_free(clientssl);
  9682. SSL_CTX_free(sctx);
  9683. SSL_CTX_free(cctx);
  9684. return testresult;
  9685. }
  9686. #endif /* OSSL_NO_USABLE_TLS1_3 */
  9687. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  9688. /*
  9689. * Test TLSv1.2 with a pipeline capable cipher. TLSv1.3 and DTLS do not
  9690. * support this yet. The only pipeline capable cipher that we have is in the
  9691. * dasync engine (providers don't support this yet), so we have to use
  9692. * deprecated APIs for this test.
  9693. *
  9694. * Test 0: Client has pipelining enabled, server does not
  9695. * Test 1: Server has pipelining enabled, client does not
  9696. * Test 2: Client has pipelining enabled, server does not: not enough data to
  9697. * fill all the pipelines
  9698. * Test 3: Client has pipelining enabled, server does not: not enough data to
  9699. * fill all the pipelines by more than a full pipeline's worth
  9700. * Test 4: Client has pipelining enabled, server does not: more data than all
  9701. * the available pipelines can take
  9702. * Test 5: Client has pipelining enabled, server does not: Maximum size pipeline
  9703. * Test 6: Repeat of test 0, but the engine is loaded late (after the SSL_CTX
  9704. * is created)
  9705. */
  9706. static int test_pipelining(int idx)
  9707. {
  9708. SSL_CTX *cctx = NULL, *sctx = NULL;
  9709. SSL *clientssl = NULL, *serverssl = NULL, *peera, *peerb;
  9710. int testresult = 0, numreads;
  9711. /* A 55 byte message */
  9712. unsigned char *msg = (unsigned char *)
  9713. "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz123";
  9714. size_t written, readbytes, offset, msglen, fragsize = 10, numpipes = 5;
  9715. size_t expectedreads;
  9716. unsigned char *buf = NULL;
  9717. ENGINE *e = NULL;
  9718. if (idx != 6) {
  9719. e = load_dasync();
  9720. if (e == NULL)
  9721. return 0;
  9722. }
  9723. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  9724. TLS_client_method(), 0,
  9725. TLS1_2_VERSION, &sctx, &cctx, cert,
  9726. privkey)))
  9727. goto end;
  9728. if (idx == 6) {
  9729. e = load_dasync();
  9730. if (e == NULL)
  9731. goto end;
  9732. /* Now act like test 0 */
  9733. idx = 0;
  9734. }
  9735. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9736. &clientssl, NULL, NULL)))
  9737. goto end;
  9738. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES128-SHA")))
  9739. goto end;
  9740. /* peera is always configured for pipelining, while peerb is not. */
  9741. if (idx == 1) {
  9742. peera = serverssl;
  9743. peerb = clientssl;
  9744. } else {
  9745. peera = clientssl;
  9746. peerb = serverssl;
  9747. }
  9748. if (idx == 5) {
  9749. numpipes = 2;
  9750. /* Maximum allowed fragment size */
  9751. fragsize = SSL3_RT_MAX_PLAIN_LENGTH;
  9752. msglen = fragsize * numpipes;
  9753. msg = OPENSSL_malloc(msglen);
  9754. if (!TEST_ptr(msg))
  9755. goto end;
  9756. if (!TEST_int_gt(RAND_bytes_ex(libctx, msg, msglen, 0), 0))
  9757. goto end;
  9758. } else if (idx == 4) {
  9759. msglen = 55;
  9760. } else {
  9761. msglen = 50;
  9762. }
  9763. if (idx == 2)
  9764. msglen -= 2; /* Send 2 less bytes */
  9765. else if (idx == 3)
  9766. msglen -= 12; /* Send 12 less bytes */
  9767. buf = OPENSSL_malloc(msglen);
  9768. if (!TEST_ptr(buf))
  9769. goto end;
  9770. if (idx == 5) {
  9771. /*
  9772. * Test that setting a split send fragment longer than the maximum
  9773. * allowed fails
  9774. */
  9775. if (!TEST_false(SSL_set_split_send_fragment(peera, fragsize + 1)))
  9776. goto end;
  9777. }
  9778. /*
  9779. * In the normal case. We have 5 pipelines with 10 bytes per pipeline
  9780. * (50 bytes in total). This is a ridiculously small number of bytes -
  9781. * but sufficient for our purposes
  9782. */
  9783. if (!TEST_true(SSL_set_max_pipelines(peera, numpipes))
  9784. || !TEST_true(SSL_set_split_send_fragment(peera, fragsize)))
  9785. goto end;
  9786. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9787. goto end;
  9788. /* Write some data from peera to peerb */
  9789. if (!TEST_true(SSL_write_ex(peera, msg, msglen, &written))
  9790. || !TEST_size_t_eq(written, msglen))
  9791. goto end;
  9792. /*
  9793. * If the pipelining code worked, then we expect all |numpipes| pipelines to
  9794. * have been used - except in test 3 where only |numpipes - 1| pipelines
  9795. * will be used. This will result in |numpipes| records (|numpipes - 1| for
  9796. * test 3) having been sent to peerb. Since peerb is not using read_ahead we
  9797. * expect this to be read in |numpipes| or |numpipes - 1| separate
  9798. * SSL_read_ex calls. In the case of test 4, there is then one additional
  9799. * read for left over data that couldn't fit in the previous pipelines
  9800. */
  9801. for (offset = 0, numreads = 0;
  9802. offset < msglen;
  9803. offset += readbytes, numreads++) {
  9804. if (!TEST_true(SSL_read_ex(peerb, buf + offset,
  9805. msglen - offset, &readbytes)))
  9806. goto end;
  9807. }
  9808. expectedreads = idx == 4 ? numpipes + 1
  9809. : (idx == 3 ? numpipes - 1 : numpipes);
  9810. if (!TEST_mem_eq(msg, msglen, buf, offset)
  9811. || !TEST_int_eq(numreads, expectedreads))
  9812. goto end;
  9813. /*
  9814. * Write some data from peerb to peera. We do this in up to |numpipes + 1|
  9815. * chunks to exercise the read pipelining code on peera.
  9816. */
  9817. for (offset = 0; offset < msglen; offset += fragsize) {
  9818. size_t sendlen = msglen - offset;
  9819. if (sendlen > fragsize)
  9820. sendlen = fragsize;
  9821. if (!TEST_true(SSL_write_ex(peerb, msg + offset, sendlen, &written))
  9822. || !TEST_size_t_eq(written, sendlen))
  9823. goto end;
  9824. }
  9825. /*
  9826. * The data was written in |numpipes|, |numpipes - 1| or |numpipes + 1|
  9827. * separate chunks (depending on which test we are running). If the
  9828. * pipelining is working then we expect peera to read up to numpipes chunks
  9829. * and process them in parallel, giving back the complete result in a single
  9830. * call to SSL_read_ex
  9831. */
  9832. if (!TEST_true(SSL_read_ex(peera, buf, msglen, &readbytes))
  9833. || !TEST_size_t_le(readbytes, msglen))
  9834. goto end;
  9835. if (idx == 4) {
  9836. size_t readbytes2;
  9837. if (!TEST_true(SSL_read_ex(peera, buf + readbytes,
  9838. msglen - readbytes, &readbytes2)))
  9839. goto end;
  9840. readbytes += readbytes2;
  9841. if (!TEST_size_t_le(readbytes, msglen))
  9842. goto end;
  9843. }
  9844. if (!TEST_mem_eq(msg, msglen, buf, readbytes))
  9845. goto end;
  9846. testresult = 1;
  9847. end:
  9848. SSL_free(serverssl);
  9849. SSL_free(clientssl);
  9850. SSL_CTX_free(sctx);
  9851. SSL_CTX_free(cctx);
  9852. if (e != NULL) {
  9853. ENGINE_unregister_ciphers(e);
  9854. ENGINE_finish(e);
  9855. ENGINE_free(e);
  9856. }
  9857. OPENSSL_free(buf);
  9858. if (fragsize == SSL3_RT_MAX_PLAIN_LENGTH)
  9859. OPENSSL_free(msg);
  9860. return testresult;
  9861. }
  9862. #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE) */
  9863. static int check_version_string(SSL *s, int version)
  9864. {
  9865. const char *verstr = NULL;
  9866. switch (version) {
  9867. case SSL3_VERSION:
  9868. verstr = "SSLv3";
  9869. break;
  9870. case TLS1_VERSION:
  9871. verstr = "TLSv1";
  9872. break;
  9873. case TLS1_1_VERSION:
  9874. verstr = "TLSv1.1";
  9875. break;
  9876. case TLS1_2_VERSION:
  9877. verstr = "TLSv1.2";
  9878. break;
  9879. case TLS1_3_VERSION:
  9880. verstr = "TLSv1.3";
  9881. break;
  9882. case DTLS1_VERSION:
  9883. verstr = "DTLSv1";
  9884. break;
  9885. case DTLS1_2_VERSION:
  9886. verstr = "DTLSv1.2";
  9887. }
  9888. return TEST_str_eq(verstr, SSL_get_version(s));
  9889. }
  9890. /*
  9891. * Test that SSL_version, SSL_get_version, SSL_is_quic, SSL_is_tls and
  9892. * SSL_is_dtls return the expected results for a (D)TLS connection. Compare with
  9893. * test_version() in quicapitest.c which does the same thing for QUIC
  9894. * connections.
  9895. */
  9896. static int test_version(int idx)
  9897. {
  9898. SSL_CTX *cctx = NULL, *sctx = NULL;
  9899. SSL *clientssl = NULL, *serverssl = NULL;
  9900. int testresult = 0, version;
  9901. const SSL_METHOD *servmeth = TLS_server_method();
  9902. const SSL_METHOD *clientmeth = TLS_client_method();
  9903. switch (idx) {
  9904. #if !defined(OPENSSL_NO_SSL3)
  9905. case 0:
  9906. version = SSL3_VERSION;
  9907. break;
  9908. #endif
  9909. #if !defined(OPENSSL_NO_TLS1)
  9910. case 1:
  9911. version = TLS1_VERSION;
  9912. break;
  9913. #endif
  9914. #if !defined(OPENSSL_NO_TLS1_2)
  9915. case 2:
  9916. version = TLS1_2_VERSION;
  9917. break;
  9918. #endif
  9919. #if !defined(OSSL_NO_USABLE_TLS1_3)
  9920. case 3:
  9921. version = TLS1_3_VERSION;
  9922. break;
  9923. #endif
  9924. #if !defined(OPENSSL_NO_DTLS1)
  9925. case 4:
  9926. version = DTLS1_VERSION;
  9927. break;
  9928. #endif
  9929. #if !defined(OPENSSL_NO_DTLS1_2)
  9930. case 5:
  9931. version = DTLS1_2_VERSION;
  9932. break;
  9933. #endif
  9934. /*
  9935. * NB we do not support QUIC in this test. That is covered by quicapitest.c
  9936. * We also don't support DTLS1_BAD_VER since we have no server support for
  9937. * that.
  9938. */
  9939. default:
  9940. TEST_skip("Unsupported protocol version");
  9941. return 1;
  9942. }
  9943. if (is_fips
  9944. && (version == SSL3_VERSION
  9945. || version == TLS1_VERSION
  9946. || version == DTLS1_VERSION)) {
  9947. TEST_skip("Protocol version not supported with FIPS");
  9948. return 1;
  9949. }
  9950. #if !defined(OPENSSL_NO_DTLS)
  9951. if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) {
  9952. servmeth = DTLS_server_method();
  9953. clientmeth = DTLS_client_method();
  9954. }
  9955. #endif
  9956. if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, version,
  9957. version, &sctx, &cctx, cert, privkey)))
  9958. goto end;
  9959. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  9960. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  9961. "DEFAULT:@SECLEVEL=0")))
  9962. goto end;
  9963. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  9964. &clientssl, NULL, NULL)))
  9965. goto end;
  9966. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  9967. goto end;
  9968. if (!TEST_int_eq(SSL_version(serverssl), version)
  9969. || !TEST_int_eq(SSL_version(clientssl), version)
  9970. || !TEST_true(check_version_string(serverssl, version))
  9971. || !TEST_true(check_version_string(clientssl, version)))
  9972. goto end;
  9973. if (version == DTLS1_VERSION || version == DTLS1_2_VERSION) {
  9974. if (!TEST_true(SSL_is_dtls(serverssl))
  9975. || !TEST_true(SSL_is_dtls(clientssl))
  9976. || !TEST_false(SSL_is_tls(serverssl))
  9977. || !TEST_false(SSL_is_tls(clientssl))
  9978. || !TEST_false(SSL_is_quic(serverssl))
  9979. || !TEST_false(SSL_is_quic(clientssl)))
  9980. goto end;
  9981. } else {
  9982. if (!TEST_true(SSL_is_tls(serverssl))
  9983. || !TEST_true(SSL_is_tls(clientssl))
  9984. || !TEST_false(SSL_is_dtls(serverssl))
  9985. || !TEST_false(SSL_is_dtls(clientssl))
  9986. || !TEST_false(SSL_is_quic(serverssl))
  9987. || !TEST_false(SSL_is_quic(clientssl)))
  9988. goto end;
  9989. }
  9990. testresult = 1;
  9991. end:
  9992. SSL_free(serverssl);
  9993. SSL_free(clientssl);
  9994. SSL_CTX_free(sctx);
  9995. SSL_CTX_free(cctx);
  9996. return testresult;
  9997. }
  9998. /*
  9999. * Test that the SSL_rstate_string*() APIs return sane results
  10000. */
  10001. static int test_rstate_string(void)
  10002. {
  10003. SSL_CTX *cctx = NULL, *sctx = NULL;
  10004. SSL *clientssl = NULL, *serverssl = NULL;
  10005. int testresult = 0, version;
  10006. const SSL_METHOD *servmeth = TLS_server_method();
  10007. const SSL_METHOD *clientmeth = TLS_client_method();
  10008. size_t written, readbytes;
  10009. unsigned char buf[2];
  10010. unsigned char dummyheader[SSL3_RT_HEADER_LENGTH] = {
  10011. SSL3_RT_APPLICATION_DATA,
  10012. TLS1_2_VERSION_MAJOR,
  10013. 0, /* To be filled in later */
  10014. 0,
  10015. 1
  10016. };
  10017. if (!TEST_true(create_ssl_ctx_pair(libctx, servmeth, clientmeth, 0,
  10018. 0, &sctx, &cctx, cert, privkey)))
  10019. goto end;
  10020. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  10021. &clientssl, NULL, NULL)))
  10022. goto end;
  10023. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH")
  10024. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header"))
  10025. goto end;
  10026. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10027. goto end;
  10028. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RH")
  10029. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read header"))
  10030. goto end;
  10031. /* Fill in the correct version for the record header */
  10032. version = SSL_version(serverssl);
  10033. if (version == TLS1_3_VERSION)
  10034. version = TLS1_2_VERSION;
  10035. dummyheader[2] = version & 0xff;
  10036. /*
  10037. * Send a dummy header. If we continued to read the body as well this
  10038. * would fail with a bad record mac, but we're not going to go that far.
  10039. */
  10040. if (!TEST_true(BIO_write_ex(SSL_get_rbio(serverssl), dummyheader,
  10041. sizeof(dummyheader), &written))
  10042. || !TEST_size_t_eq(written, SSL3_RT_HEADER_LENGTH))
  10043. goto end;
  10044. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes)))
  10045. goto end;
  10046. if (!TEST_str_eq(SSL_rstate_string(serverssl), "RB")
  10047. || !TEST_str_eq(SSL_rstate_string_long(serverssl), "read body"))
  10048. goto end;
  10049. testresult = 1;
  10050. end:
  10051. SSL_free(serverssl);
  10052. SSL_free(clientssl);
  10053. SSL_CTX_free(sctx);
  10054. SSL_CTX_free(cctx);
  10055. return testresult;
  10056. }
  10057. /*
  10058. * Force a write retry during handshaking. We test various combinations of
  10059. * scenarios. We test a large certificate message which will fill the buffering
  10060. * BIO used in the handshake. We try with client auth on and off. Finally we
  10061. * also try a BIO that indicates retry via a 0 return. BIO_write() is documented
  10062. * to indicate retry via -1 - but sometimes BIOs don't do that.
  10063. *
  10064. * Test 0: Standard certificate message
  10065. * Test 1: Large certificate message
  10066. * Test 2: Standard cert, verify peer
  10067. * Test 3: Large cert, verify peer
  10068. * Test 4: Standard cert, BIO returns 0 on retry
  10069. * Test 5: Large cert, BIO returns 0 on retry
  10070. * Test 6: Standard cert, verify peer, BIO returns 0 on retry
  10071. * Test 7: Large cert, verify peer, BIO returns 0 on retry
  10072. * Test 8-15: Repeat of above with TLSv1.2
  10073. */
  10074. static int test_handshake_retry(int idx)
  10075. {
  10076. SSL_CTX *cctx = NULL, *sctx = NULL;
  10077. SSL *clientssl = NULL, *serverssl = NULL;
  10078. int testresult = 0;
  10079. BIO *tmp = NULL, *bretry = BIO_new(bio_s_always_retry());
  10080. int maxversion = 0;
  10081. if (!TEST_ptr(bretry))
  10082. goto end;
  10083. #ifndef OPENSSL_NO_TLS1_2
  10084. if ((idx & 8) == 8)
  10085. maxversion = TLS1_2_VERSION;
  10086. #else
  10087. if ((idx & 8) == 8)
  10088. return TEST_skip("No TLSv1.2");
  10089. #endif
  10090. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10091. TLS_client_method(), 0, maxversion,
  10092. &sctx, &cctx, cert, privkey)))
  10093. goto end;
  10094. /*
  10095. * Add a large amount of data to fill the buffering BIO used by the SSL
  10096. * object
  10097. */
  10098. if ((idx & 1) == 1 && !ssl_ctx_add_large_cert_chain(libctx, sctx, cert))
  10099. goto end;
  10100. /*
  10101. * We don't actually configure a client cert, but neither do we fail if one
  10102. * isn't present.
  10103. */
  10104. if ((idx & 2) == 2)
  10105. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  10106. if ((idx & 4) == 4)
  10107. set_always_retry_err_val(0);
  10108. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  10109. &clientssl, NULL, NULL)))
  10110. goto end;
  10111. tmp = SSL_get_wbio(serverssl);
  10112. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  10113. tmp = NULL;
  10114. goto end;
  10115. }
  10116. SSL_set0_wbio(serverssl, bretry);
  10117. bretry = NULL;
  10118. if (!TEST_int_eq(SSL_connect(clientssl), -1))
  10119. goto end;
  10120. if (!TEST_int_eq(SSL_accept(serverssl), -1)
  10121. || !TEST_int_eq(SSL_get_error(serverssl, -1), SSL_ERROR_WANT_WRITE))
  10122. goto end;
  10123. /* Restore a BIO that will let the write succeed */
  10124. SSL_set0_wbio(serverssl, tmp);
  10125. tmp = NULL;
  10126. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10127. goto end;
  10128. testresult = 1;
  10129. end:
  10130. SSL_free(serverssl);
  10131. SSL_free(clientssl);
  10132. SSL_CTX_free(sctx);
  10133. SSL_CTX_free(cctx);
  10134. BIO_free(bretry);
  10135. BIO_free(tmp);
  10136. set_always_retry_err_val(-1);
  10137. return testresult;
  10138. }
  10139. /*
  10140. * Test that receiving retries when writing application data works as expected
  10141. */
  10142. static int test_data_retry(void)
  10143. {
  10144. SSL_CTX *cctx = NULL, *sctx = NULL;
  10145. SSL *clientssl = NULL, *serverssl = NULL;
  10146. int testresult = 0;
  10147. unsigned char inbuf[1200], outbuf[1200];
  10148. size_t i;
  10149. BIO *tmp = NULL;
  10150. BIO *bretry = BIO_new(bio_s_maybe_retry());
  10151. size_t written, readbytes, totread = 0;
  10152. if (!TEST_ptr(bretry))
  10153. goto end;
  10154. for (i = 0; i < sizeof(inbuf); i++)
  10155. inbuf[i] = (unsigned char)(0xff & i);
  10156. memset(outbuf, 0, sizeof(outbuf));
  10157. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10158. TLS_client_method(), 0, 0, &sctx, &cctx,
  10159. cert, privkey)))
  10160. goto end;
  10161. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  10162. NULL)))
  10163. goto end;
  10164. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10165. goto end;
  10166. /* Smallest possible max send fragment is 512 */
  10167. if (!TEST_true(SSL_set_max_send_fragment(clientssl, 512)))
  10168. goto end;
  10169. tmp = SSL_get_wbio(clientssl);
  10170. if (!TEST_ptr(tmp))
  10171. goto end;
  10172. if (!TEST_true(BIO_up_ref(tmp)))
  10173. goto end;
  10174. BIO_push(bretry, tmp);
  10175. tmp = NULL;
  10176. SSL_set0_wbio(clientssl, bretry);
  10177. if (!BIO_up_ref(bretry)) {
  10178. bretry = NULL;
  10179. goto end;
  10180. }
  10181. for (i = 0; i < 3; i++) {
  10182. /* We expect this call to make no progress and indicate retry */
  10183. if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10184. goto end;
  10185. if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE))
  10186. goto end;
  10187. /* Allow one write to progress, but the next one to signal retry */
  10188. if (!TEST_true(BIO_ctrl(bretry, MAYBE_RETRY_CTRL_SET_RETRY_AFTER_CNT, 1,
  10189. NULL)))
  10190. goto end;
  10191. if (i == 2)
  10192. break;
  10193. /*
  10194. * This call will hopefully make progress but will still indicate retry
  10195. * because there is more data than will fit into a single record.
  10196. */
  10197. if (!TEST_false(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10198. goto end;
  10199. if (!TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_WANT_WRITE))
  10200. goto end;
  10201. }
  10202. /* The final call should write the last chunk of data and succeed */
  10203. if (!TEST_true(SSL_write_ex(clientssl, inbuf, sizeof(inbuf), &written)))
  10204. goto end;
  10205. /* Read all the data available */
  10206. while (SSL_read_ex(serverssl, outbuf + totread, sizeof(outbuf) - totread,
  10207. &readbytes))
  10208. totread += readbytes;
  10209. if (!TEST_mem_eq(inbuf, sizeof(inbuf), outbuf, totread))
  10210. goto end;
  10211. testresult = 1;
  10212. end:
  10213. SSL_free(serverssl);
  10214. SSL_free(clientssl);
  10215. SSL_CTX_free(sctx);
  10216. SSL_CTX_free(cctx);
  10217. BIO_free_all(bretry);
  10218. BIO_free(tmp);
  10219. return testresult;
  10220. }
  10221. struct resume_servername_cb_data {
  10222. int i;
  10223. SSL_CTX *cctx;
  10224. SSL_CTX *sctx;
  10225. SSL_SESSION *sess;
  10226. int recurse;
  10227. };
  10228. /*
  10229. * Servername callback. We use it here to run another complete handshake using
  10230. * the same session - and mark the session as not_resuamble at the end
  10231. */
  10232. static int resume_servername_cb(SSL *s, int *ad, void *arg)
  10233. {
  10234. struct resume_servername_cb_data *cbdata = arg;
  10235. SSL *serverssl = NULL, *clientssl = NULL;
  10236. int ret = SSL_TLSEXT_ERR_ALERT_FATAL;
  10237. if (cbdata->recurse)
  10238. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10239. if ((cbdata->i % 3) != 1)
  10240. return SSL_TLSEXT_ERR_OK;
  10241. cbdata->recurse = 1;
  10242. if (!TEST_true(create_ssl_objects(cbdata->sctx, cbdata->cctx, &serverssl,
  10243. &clientssl, NULL, NULL))
  10244. || !TEST_true(SSL_set_session(clientssl, cbdata->sess)))
  10245. goto end;
  10246. ERR_set_mark();
  10247. /*
  10248. * We expect this to fail - because the servername cb will fail. This will
  10249. * mark the session as not_resumable.
  10250. */
  10251. if (!TEST_false(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))) {
  10252. ERR_clear_last_mark();
  10253. goto end;
  10254. }
  10255. ERR_pop_to_mark();
  10256. ret = SSL_TLSEXT_ERR_OK;
  10257. end:
  10258. SSL_free(serverssl);
  10259. SSL_free(clientssl);
  10260. cbdata->recurse = 0;
  10261. return ret;
  10262. }
  10263. /*
  10264. * Test multiple resumptions and cache size handling
  10265. * Test 0: TLSv1.3 (max_early_data set)
  10266. * Test 1: TLSv1.3 (SSL_OP_NO_TICKET set)
  10267. * Test 2: TLSv1.3 (max_early_data and SSL_OP_NO_TICKET set)
  10268. * Test 3: TLSv1.3 (SSL_OP_NO_TICKET, simultaneous resumes)
  10269. * Test 4: TLSv1.2
  10270. */
  10271. static int test_multi_resume(int idx)
  10272. {
  10273. SSL_CTX *sctx = NULL, *cctx = NULL;
  10274. SSL *serverssl = NULL, *clientssl = NULL;
  10275. SSL_SESSION *sess = NULL;
  10276. int max_version = TLS1_3_VERSION;
  10277. int i, testresult = 0;
  10278. struct resume_servername_cb_data cbdata;
  10279. #if defined(OPENSSL_NO_TLS1_2)
  10280. if (idx == 4)
  10281. return TEST_skip("TLSv1.2 is disabled in this build");
  10282. #else
  10283. if (idx == 4)
  10284. max_version = TLS1_2_VERSION;
  10285. #endif
  10286. #if defined(OSSL_NO_USABLE_TLS1_3)
  10287. if (idx != 4)
  10288. return TEST_skip("No usable TLSv1.3 in this build");
  10289. #endif
  10290. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10291. TLS_client_method(), TLS1_VERSION,
  10292. max_version, &sctx, &cctx, cert,
  10293. privkey)))
  10294. goto end;
  10295. /*
  10296. * TLSv1.3 only uses a session cache if either max_early_data > 0 (used for
  10297. * replay protection), or if SSL_OP_NO_TICKET is in use
  10298. */
  10299. if (idx == 0 || idx == 2) {
  10300. if (!TEST_true(SSL_CTX_set_max_early_data(sctx, 1024)))
  10301. goto end;
  10302. }
  10303. if (idx == 1 || idx == 2 || idx == 3)
  10304. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  10305. SSL_CTX_sess_set_cache_size(sctx, 5);
  10306. if (idx == 3) {
  10307. SSL_CTX_set_tlsext_servername_callback(sctx, resume_servername_cb);
  10308. SSL_CTX_set_tlsext_servername_arg(sctx, &cbdata);
  10309. cbdata.cctx = cctx;
  10310. cbdata.sctx = sctx;
  10311. cbdata.recurse = 0;
  10312. }
  10313. for (i = 0; i < 30; i++) {
  10314. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  10315. NULL, NULL))
  10316. || !TEST_true(SSL_set_session(clientssl, sess)))
  10317. goto end;
  10318. /*
  10319. * Check simultaneous resumes. We pause the connection part way through
  10320. * the handshake by (mis)using the servername_cb. The pause occurs after
  10321. * session resumption has already occurred, but before any session
  10322. * tickets have been issued. While paused we run another complete
  10323. * handshake resuming the same session.
  10324. */
  10325. if (idx == 3) {
  10326. cbdata.i = i;
  10327. cbdata.sess = sess;
  10328. }
  10329. /*
  10330. * Recreate a bug where dynamically changing the max_early_data value
  10331. * can cause sessions in the session cache which cannot be deleted.
  10332. */
  10333. if ((idx == 0 || idx == 2) && (i % 3) == 2)
  10334. SSL_set_max_early_data(serverssl, 0);
  10335. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10336. goto end;
  10337. if (sess == NULL || (idx == 0 && (i % 3) == 2)) {
  10338. if (!TEST_false(SSL_session_reused(clientssl)))
  10339. goto end;
  10340. } else {
  10341. if (!TEST_true(SSL_session_reused(clientssl)))
  10342. goto end;
  10343. }
  10344. SSL_SESSION_free(sess);
  10345. /* Do a full handshake, followed by two resumptions */
  10346. if ((i % 3) == 2) {
  10347. sess = NULL;
  10348. } else {
  10349. if (!TEST_ptr((sess = SSL_get1_session(clientssl))))
  10350. goto end;
  10351. }
  10352. SSL_shutdown(clientssl);
  10353. SSL_shutdown(serverssl);
  10354. SSL_free(serverssl);
  10355. SSL_free(clientssl);
  10356. serverssl = clientssl = NULL;
  10357. }
  10358. /* We should never exceed the session cache size limit */
  10359. if (!TEST_long_le(SSL_CTX_sess_number(sctx), 5))
  10360. goto end;
  10361. testresult = 1;
  10362. end:
  10363. SSL_free(serverssl);
  10364. SSL_free(clientssl);
  10365. SSL_CTX_free(sctx);
  10366. SSL_CTX_free(cctx);
  10367. SSL_SESSION_free(sess);
  10368. return testresult;
  10369. }
  10370. static struct next_proto_st {
  10371. int serverlen;
  10372. unsigned char server[40];
  10373. int clientlen;
  10374. unsigned char client[40];
  10375. int expected_ret;
  10376. size_t selectedlen;
  10377. unsigned char selected[40];
  10378. } next_proto_tests[] = {
  10379. {
  10380. 4, { 3, 'a', 'b', 'c' },
  10381. 4, { 3, 'a', 'b', 'c' },
  10382. OPENSSL_NPN_NEGOTIATED,
  10383. 3, { 'a', 'b', 'c' }
  10384. },
  10385. {
  10386. 7, { 3, 'a', 'b', 'c', 2, 'a', 'b' },
  10387. 4, { 3, 'a', 'b', 'c' },
  10388. OPENSSL_NPN_NEGOTIATED,
  10389. 3, { 'a', 'b', 'c' }
  10390. },
  10391. {
  10392. 7, { 2, 'a', 'b', 3, 'a', 'b', 'c', },
  10393. 4, { 3, 'a', 'b', 'c' },
  10394. OPENSSL_NPN_NEGOTIATED,
  10395. 3, { 'a', 'b', 'c' }
  10396. },
  10397. {
  10398. 4, { 3, 'a', 'b', 'c' },
  10399. 7, { 3, 'a', 'b', 'c', 2, 'a', 'b', },
  10400. OPENSSL_NPN_NEGOTIATED,
  10401. 3, { 'a', 'b', 'c' }
  10402. },
  10403. {
  10404. 4, { 3, 'a', 'b', 'c' },
  10405. 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
  10406. OPENSSL_NPN_NEGOTIATED,
  10407. 3, { 'a', 'b', 'c' }
  10408. },
  10409. {
  10410. 7, { 2, 'b', 'c', 3, 'a', 'b', 'c' },
  10411. 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
  10412. OPENSSL_NPN_NEGOTIATED,
  10413. 3, { 'a', 'b', 'c' }
  10414. },
  10415. {
  10416. 10, { 2, 'b', 'c', 3, 'a', 'b', 'c', 2, 'a', 'b' },
  10417. 7, { 2, 'a', 'b', 3, 'a', 'b', 'c'},
  10418. OPENSSL_NPN_NEGOTIATED,
  10419. 3, { 'a', 'b', 'c' }
  10420. },
  10421. {
  10422. 4, { 3, 'b', 'c', 'd' },
  10423. 4, { 3, 'a', 'b', 'c' },
  10424. OPENSSL_NPN_NO_OVERLAP,
  10425. 3, { 'a', 'b', 'c' }
  10426. },
  10427. {
  10428. 0, { 0 },
  10429. 4, { 3, 'a', 'b', 'c' },
  10430. OPENSSL_NPN_NO_OVERLAP,
  10431. 3, { 'a', 'b', 'c' }
  10432. },
  10433. {
  10434. -1, { 0 },
  10435. 4, { 3, 'a', 'b', 'c' },
  10436. OPENSSL_NPN_NO_OVERLAP,
  10437. 3, { 'a', 'b', 'c' }
  10438. },
  10439. {
  10440. 4, { 3, 'a', 'b', 'c' },
  10441. 0, { 0 },
  10442. OPENSSL_NPN_NO_OVERLAP,
  10443. 0, { 0 }
  10444. },
  10445. {
  10446. 4, { 3, 'a', 'b', 'c' },
  10447. -1, { 0 },
  10448. OPENSSL_NPN_NO_OVERLAP,
  10449. 0, { 0 }
  10450. },
  10451. {
  10452. 3, { 3, 'a', 'b', 'c' },
  10453. 4, { 3, 'a', 'b', 'c' },
  10454. OPENSSL_NPN_NO_OVERLAP,
  10455. 3, { 'a', 'b', 'c' }
  10456. },
  10457. {
  10458. 4, { 3, 'a', 'b', 'c' },
  10459. 3, { 3, 'a', 'b', 'c' },
  10460. OPENSSL_NPN_NO_OVERLAP,
  10461. 0, { 0 }
  10462. }
  10463. };
  10464. static int test_select_next_proto(int idx)
  10465. {
  10466. struct next_proto_st *np = &next_proto_tests[idx];
  10467. int ret = 0;
  10468. unsigned char *out, *client, *server;
  10469. unsigned char outlen;
  10470. unsigned int clientlen, serverlen;
  10471. if (np->clientlen == -1) {
  10472. client = NULL;
  10473. clientlen = 0;
  10474. } else {
  10475. client = np->client;
  10476. clientlen = (unsigned int)np->clientlen;
  10477. }
  10478. if (np->serverlen == -1) {
  10479. server = NULL;
  10480. serverlen = 0;
  10481. } else {
  10482. server = np->server;
  10483. serverlen = (unsigned int)np->serverlen;
  10484. }
  10485. if (!TEST_int_eq(SSL_select_next_proto(&out, &outlen, server, serverlen,
  10486. client, clientlen),
  10487. np->expected_ret))
  10488. goto err;
  10489. if (np->selectedlen == 0) {
  10490. if (!TEST_ptr_null(out) || !TEST_uchar_eq(outlen, 0))
  10491. goto err;
  10492. } else {
  10493. if (!TEST_mem_eq(out, outlen, np->selected, np->selectedlen))
  10494. goto err;
  10495. }
  10496. ret = 1;
  10497. err:
  10498. return ret;
  10499. }
  10500. static const unsigned char fooprot[] = {3, 'f', 'o', 'o' };
  10501. static const unsigned char barprot[] = {3, 'b', 'a', 'r' };
  10502. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
  10503. static int npn_advert_cb(SSL *ssl, const unsigned char **out,
  10504. unsigned int *outlen, void *arg)
  10505. {
  10506. int *idx = (int *)arg;
  10507. switch (*idx) {
  10508. default:
  10509. case 0:
  10510. *out = fooprot;
  10511. *outlen = sizeof(fooprot);
  10512. return SSL_TLSEXT_ERR_OK;
  10513. case 1:
  10514. *out = NULL;
  10515. *outlen = 0;
  10516. return SSL_TLSEXT_ERR_OK;
  10517. case 2:
  10518. return SSL_TLSEXT_ERR_NOACK;
  10519. }
  10520. }
  10521. static int npn_select_cb(SSL *s, unsigned char **out, unsigned char *outlen,
  10522. const unsigned char *in, unsigned int inlen, void *arg)
  10523. {
  10524. int *idx = (int *)arg;
  10525. switch (*idx) {
  10526. case 0:
  10527. case 1:
  10528. *out = (unsigned char *)(fooprot + 1);
  10529. *outlen = *fooprot;
  10530. return SSL_TLSEXT_ERR_OK;
  10531. case 3:
  10532. *out = (unsigned char *)(barprot + 1);
  10533. *outlen = *barprot;
  10534. return SSL_TLSEXT_ERR_OK;
  10535. case 4:
  10536. *outlen = 0;
  10537. return SSL_TLSEXT_ERR_OK;
  10538. default:
  10539. case 2:
  10540. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10541. }
  10542. }
  10543. /*
  10544. * Test the NPN callbacks
  10545. * Test 0: advert = foo, select = foo
  10546. * Test 1: advert = <empty>, select = foo
  10547. * Test 2: no advert
  10548. * Test 3: advert = foo, select = bar
  10549. * Test 4: advert = foo, select = <empty> (should fail)
  10550. */
  10551. static int test_npn(int idx)
  10552. {
  10553. SSL_CTX *sctx = NULL, *cctx = NULL;
  10554. SSL *serverssl = NULL, *clientssl = NULL;
  10555. int testresult = 0;
  10556. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10557. TLS_client_method(), 0, TLS1_2_VERSION,
  10558. &sctx, &cctx, cert, privkey)))
  10559. goto end;
  10560. SSL_CTX_set_next_protos_advertised_cb(sctx, npn_advert_cb, &idx);
  10561. SSL_CTX_set_next_proto_select_cb(cctx, npn_select_cb, &idx);
  10562. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  10563. NULL)))
  10564. goto end;
  10565. if (idx == 4) {
  10566. /* We don't allow empty selection of NPN, so this should fail */
  10567. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  10568. SSL_ERROR_NONE)))
  10569. goto end;
  10570. } else {
  10571. const unsigned char *prot;
  10572. unsigned int protlen;
  10573. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  10574. SSL_ERROR_NONE)))
  10575. goto end;
  10576. SSL_get0_next_proto_negotiated(serverssl, &prot, &protlen);
  10577. switch (idx) {
  10578. case 0:
  10579. case 1:
  10580. if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
  10581. goto end;
  10582. break;
  10583. case 2:
  10584. if (!TEST_uint_eq(protlen, 0))
  10585. goto end;
  10586. break;
  10587. case 3:
  10588. if (!TEST_mem_eq(prot, protlen, barprot + 1, *barprot))
  10589. goto end;
  10590. break;
  10591. default:
  10592. TEST_error("Should not get here");
  10593. goto end;
  10594. }
  10595. }
  10596. testresult = 1;
  10597. end:
  10598. SSL_free(serverssl);
  10599. SSL_free(clientssl);
  10600. SSL_CTX_free(sctx);
  10601. SSL_CTX_free(cctx);
  10602. return testresult;
  10603. }
  10604. #endif /* !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG) */
  10605. static int alpn_select_cb2(SSL *ssl, const unsigned char **out,
  10606. unsigned char *outlen, const unsigned char *in,
  10607. unsigned int inlen, void *arg)
  10608. {
  10609. int *idx = (int *)arg;
  10610. switch (*idx) {
  10611. case 0:
  10612. *out = (unsigned char *)(fooprot + 1);
  10613. *outlen = *fooprot;
  10614. return SSL_TLSEXT_ERR_OK;
  10615. case 2:
  10616. *out = (unsigned char *)(barprot + 1);
  10617. *outlen = *barprot;
  10618. return SSL_TLSEXT_ERR_OK;
  10619. case 3:
  10620. *outlen = 0;
  10621. return SSL_TLSEXT_ERR_OK;
  10622. default:
  10623. case 1:
  10624. return SSL_TLSEXT_ERR_ALERT_FATAL;
  10625. }
  10626. return 0;
  10627. }
  10628. /*
  10629. * Test the ALPN callbacks
  10630. * Test 0: client = foo, select = foo
  10631. * Test 1: client = <empty>, select = none
  10632. * Test 2: client = foo, select = bar (should fail)
  10633. * Test 3: client = foo, select = <empty> (should fail)
  10634. */
  10635. static int test_alpn(int idx)
  10636. {
  10637. SSL_CTX *sctx = NULL, *cctx = NULL;
  10638. SSL *serverssl = NULL, *clientssl = NULL;
  10639. int testresult = 0;
  10640. const unsigned char *prots = fooprot;
  10641. unsigned int protslen = sizeof(fooprot);
  10642. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  10643. TLS_client_method(), 0, 0,
  10644. &sctx, &cctx, cert, privkey)))
  10645. goto end;
  10646. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb2, &idx);
  10647. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  10648. NULL)))
  10649. goto end;
  10650. if (idx == 1) {
  10651. prots = NULL;
  10652. protslen = 0;
  10653. }
  10654. /* SSL_set_alpn_protos returns 0 for success! */
  10655. if (!TEST_false(SSL_set_alpn_protos(clientssl, prots, protslen)))
  10656. goto end;
  10657. if (idx == 2 || idx == 3) {
  10658. /* We don't allow empty selection of NPN, so this should fail */
  10659. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  10660. SSL_ERROR_NONE)))
  10661. goto end;
  10662. } else {
  10663. const unsigned char *prot;
  10664. unsigned int protlen;
  10665. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  10666. SSL_ERROR_NONE)))
  10667. goto end;
  10668. SSL_get0_alpn_selected(clientssl, &prot, &protlen);
  10669. switch (idx) {
  10670. case 0:
  10671. if (!TEST_mem_eq(prot, protlen, fooprot + 1, *fooprot))
  10672. goto end;
  10673. break;
  10674. case 1:
  10675. if (!TEST_uint_eq(protlen, 0))
  10676. goto end;
  10677. break;
  10678. default:
  10679. TEST_error("Should not get here");
  10680. goto end;
  10681. }
  10682. }
  10683. testresult = 1;
  10684. end:
  10685. SSL_free(serverssl);
  10686. SSL_free(clientssl);
  10687. SSL_CTX_free(sctx);
  10688. SSL_CTX_free(cctx);
  10689. return testresult;
  10690. }
  10691. static int test_no_renegotiation(int idx)
  10692. {
  10693. SSL_CTX *sctx = NULL, *cctx = NULL;
  10694. SSL *serverssl = NULL, *clientssl = NULL;
  10695. int testresult = 0, ret;
  10696. int max_proto;
  10697. const SSL_METHOD *sm, *cm;
  10698. unsigned char buf[5];
  10699. if (idx == 0) {
  10700. #ifndef OPENSSL_NO_TLS1_2
  10701. max_proto = TLS1_2_VERSION;
  10702. sm = TLS_server_method();
  10703. cm = TLS_client_method();
  10704. #else
  10705. return TEST_skip("TLSv1.2 is disabled in this build");
  10706. #endif
  10707. } else {
  10708. #ifndef OPENSSL_NO_DTLS1_2
  10709. max_proto = DTLS1_2_VERSION;
  10710. sm = DTLS_server_method();
  10711. cm = DTLS_client_method();
  10712. #else
  10713. return TEST_skip("DTLSv1.2 is disabled in this build");
  10714. #endif
  10715. }
  10716. if (!TEST_true(create_ssl_ctx_pair(libctx, sm, cm, 0, max_proto,
  10717. &sctx, &cctx, cert, privkey)))
  10718. goto end;
  10719. SSL_CTX_set_options(sctx, SSL_OP_NO_RENEGOTIATION);
  10720. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  10721. NULL)))
  10722. goto end;
  10723. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  10724. goto end;
  10725. if (!TEST_true(SSL_renegotiate(clientssl))
  10726. || !TEST_int_le(ret = SSL_connect(clientssl), 0)
  10727. || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_WANT_READ))
  10728. goto end;
  10729. /*
  10730. * We've not sent any application data, so we expect this to fail. It should
  10731. * also read the renegotiation attempt, and send back a no_renegotiation
  10732. * warning alert because we have renegotiation disabled.
  10733. */
  10734. if (!TEST_int_le(ret = SSL_read(serverssl, buf, sizeof(buf)), 0))
  10735. goto end;
  10736. if (!TEST_int_eq(SSL_get_error(serverssl, ret), SSL_ERROR_WANT_READ))
  10737. goto end;
  10738. /*
  10739. * The client should now see the no_renegotiation warning and fail the
  10740. * connection
  10741. */
  10742. if (!TEST_int_le(ret = SSL_connect(clientssl), 0)
  10743. || !TEST_int_eq(SSL_get_error(clientssl, ret), SSL_ERROR_SSL)
  10744. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), SSL_R_NO_RENEGOTIATION))
  10745. goto end;
  10746. testresult = 1;
  10747. end:
  10748. SSL_free(serverssl);
  10749. SSL_free(clientssl);
  10750. SSL_CTX_free(sctx);
  10751. SSL_CTX_free(cctx);
  10752. return testresult;
  10753. }
  10754. OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
  10755. int setup_tests(void)
  10756. {
  10757. char *modulename;
  10758. char *configfile;
  10759. libctx = OSSL_LIB_CTX_new();
  10760. if (!TEST_ptr(libctx))
  10761. return 0;
  10762. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  10763. /*
  10764. * Verify that the default and fips providers in the default libctx are not
  10765. * available
  10766. */
  10767. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  10768. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  10769. return 0;
  10770. if (!test_skip_common_options()) {
  10771. TEST_error("Error parsing test options\n");
  10772. return 0;
  10773. }
  10774. if (!TEST_ptr(certsdir = test_get_argument(0))
  10775. || !TEST_ptr(srpvfile = test_get_argument(1))
  10776. || !TEST_ptr(tmpfilename = test_get_argument(2))
  10777. || !TEST_ptr(modulename = test_get_argument(3))
  10778. || !TEST_ptr(configfile = test_get_argument(4))
  10779. || !TEST_ptr(dhfile = test_get_argument(5)))
  10780. return 0;
  10781. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  10782. return 0;
  10783. /* Check we have the expected provider available */
  10784. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  10785. return 0;
  10786. /* Check the default provider is not available */
  10787. if (strcmp(modulename, "default") != 0
  10788. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  10789. return 0;
  10790. if (strcmp(modulename, "fips") == 0) {
  10791. OSSL_PROVIDER *prov = NULL;
  10792. OSSL_PARAM params[2];
  10793. is_fips = 1;
  10794. prov = OSSL_PROVIDER_load(libctx, "fips");
  10795. if (prov != NULL) {
  10796. /* Query the fips provider to check if the check ems option is enabled */
  10797. params[0] =
  10798. OSSL_PARAM_construct_int(OSSL_PROV_PARAM_TLS1_PRF_EMS_CHECK,
  10799. &fips_ems_check);
  10800. params[1] = OSSL_PARAM_construct_end();
  10801. OSSL_PROVIDER_get_params(prov, params);
  10802. OSSL_PROVIDER_unload(prov);
  10803. }
  10804. }
  10805. /*
  10806. * We add, but don't load the test "tls-provider". We'll load it when we
  10807. * need it.
  10808. */
  10809. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
  10810. tls_provider_init)))
  10811. return 0;
  10812. if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
  10813. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  10814. TEST_error("not supported in this build");
  10815. return 0;
  10816. #else
  10817. int i, mcount, rcount, fcount;
  10818. for (i = 0; i < 4; i++)
  10819. test_export_key_mat(i);
  10820. CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
  10821. test_printf_stdout("malloc %d realloc %d free %d\n",
  10822. mcount, rcount, fcount);
  10823. return 1;
  10824. #endif
  10825. }
  10826. cert = test_mk_file_path(certsdir, "servercert.pem");
  10827. if (cert == NULL)
  10828. goto err;
  10829. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  10830. if (privkey == NULL)
  10831. goto err;
  10832. cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  10833. if (cert2 == NULL)
  10834. goto err;
  10835. privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  10836. if (privkey2 == NULL)
  10837. goto err;
  10838. cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
  10839. if (cert1024 == NULL)
  10840. goto err;
  10841. privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
  10842. if (privkey1024 == NULL)
  10843. goto err;
  10844. cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
  10845. if (cert3072 == NULL)
  10846. goto err;
  10847. privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
  10848. if (privkey3072 == NULL)
  10849. goto err;
  10850. cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
  10851. if (cert4096 == NULL)
  10852. goto err;
  10853. privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
  10854. if (privkey4096 == NULL)
  10855. goto err;
  10856. cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
  10857. if (cert8192 == NULL)
  10858. goto err;
  10859. privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
  10860. if (privkey8192 == NULL)
  10861. goto err;
  10862. if (fips_ems_check) {
  10863. #ifndef OPENSSL_NO_TLS1_2
  10864. ADD_TEST(test_no_ems);
  10865. #endif
  10866. return 1;
  10867. }
  10868. #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
  10869. # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  10870. ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
  10871. ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS * 2);
  10872. # endif
  10873. #endif
  10874. ADD_TEST(test_large_message_tls);
  10875. ADD_TEST(test_large_message_tls_read_ahead);
  10876. #ifndef OPENSSL_NO_DTLS
  10877. ADD_TEST(test_large_message_dtls);
  10878. #endif
  10879. ADD_ALL_TESTS(test_large_app_data, 28);
  10880. ADD_TEST(test_cleanse_plaintext);
  10881. #ifndef OPENSSL_NO_OCSP
  10882. ADD_TEST(test_tlsext_status_type);
  10883. #endif
  10884. ADD_TEST(test_session_with_only_int_cache);
  10885. ADD_TEST(test_session_with_only_ext_cache);
  10886. ADD_TEST(test_session_with_both_cache);
  10887. ADD_TEST(test_session_wo_ca_names);
  10888. #ifndef OSSL_NO_USABLE_TLS1_3
  10889. ADD_ALL_TESTS(test_stateful_tickets, 3);
  10890. ADD_ALL_TESTS(test_stateless_tickets, 3);
  10891. ADD_TEST(test_psk_tickets);
  10892. ADD_ALL_TESTS(test_extra_tickets, 6);
  10893. #endif
  10894. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  10895. ADD_TEST(test_ssl_bio_pop_next_bio);
  10896. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  10897. ADD_TEST(test_ssl_bio_change_rbio);
  10898. ADD_TEST(test_ssl_bio_change_wbio);
  10899. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  10900. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  10901. ADD_TEST(test_keylog);
  10902. #endif
  10903. #ifndef OSSL_NO_USABLE_TLS1_3
  10904. ADD_TEST(test_keylog_no_master_key);
  10905. #endif
  10906. ADD_TEST(test_client_cert_verify_cb);
  10907. ADD_TEST(test_ssl_build_cert_chain);
  10908. ADD_TEST(test_ssl_ctx_build_cert_chain);
  10909. #ifndef OPENSSL_NO_TLS1_2
  10910. ADD_TEST(test_client_hello_cb);
  10911. ADD_TEST(test_no_ems);
  10912. ADD_TEST(test_ccs_change_cipher);
  10913. #endif
  10914. #ifndef OSSL_NO_USABLE_TLS1_3
  10915. ADD_ALL_TESTS(test_early_data_read_write, 6);
  10916. /*
  10917. * We don't do replay tests for external PSK. Replay protection isn't used
  10918. * in that scenario.
  10919. */
  10920. ADD_ALL_TESTS(test_early_data_replay, 2);
  10921. ADD_ALL_TESTS(test_early_data_skip, OSSL_NELEM(ciphersuites) * 3);
  10922. ADD_ALL_TESTS(test_early_data_skip_hrr, OSSL_NELEM(ciphersuites) * 3);
  10923. ADD_ALL_TESTS(test_early_data_skip_hrr_fail, OSSL_NELEM(ciphersuites) * 3);
  10924. ADD_ALL_TESTS(test_early_data_skip_abort, OSSL_NELEM(ciphersuites) * 3);
  10925. ADD_ALL_TESTS(test_early_data_not_sent, 3);
  10926. ADD_ALL_TESTS(test_early_data_psk, 8);
  10927. ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
  10928. ADD_ALL_TESTS(test_early_data_not_expected, 3);
  10929. # ifndef OPENSSL_NO_TLS1_2
  10930. ADD_ALL_TESTS(test_early_data_tls1_2, 3);
  10931. # endif
  10932. #endif
  10933. #ifndef OSSL_NO_USABLE_TLS1_3
  10934. ADD_ALL_TESTS(test_set_ciphersuite, 10);
  10935. ADD_TEST(test_ciphersuite_change);
  10936. ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
  10937. # ifdef OPENSSL_NO_PSK
  10938. ADD_ALL_TESTS(test_tls13_psk, 1);
  10939. # else
  10940. ADD_ALL_TESTS(test_tls13_psk, 4);
  10941. # endif /* OPENSSL_NO_PSK */
  10942. #ifndef OSSL_NO_USABLE_TLS1_3
  10943. ADD_ALL_TESTS(test_tls13_no_dhe_kex, 8);
  10944. #endif /* OSSL_NO_USABLE_TLS1_3 */
  10945. # ifndef OPENSSL_NO_TLS1_2
  10946. /* Test with both TLSv1.3 and 1.2 versions */
  10947. ADD_ALL_TESTS(test_key_exchange, 14);
  10948. # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
  10949. ADD_ALL_TESTS(test_negotiated_group,
  10950. 4 * (OSSL_NELEM(ecdhe_kexch_groups)
  10951. + OSSL_NELEM(ffdhe_kexch_groups)));
  10952. # endif
  10953. # else
  10954. /* Test with only TLSv1.3 versions */
  10955. ADD_ALL_TESTS(test_key_exchange, 12);
  10956. # endif
  10957. ADD_ALL_TESTS(test_custom_exts, 6);
  10958. ADD_TEST(test_stateless);
  10959. ADD_TEST(test_pha_key_update);
  10960. #else
  10961. ADD_ALL_TESTS(test_custom_exts, 3);
  10962. #endif
  10963. ADD_ALL_TESTS(test_export_key_mat, 6);
  10964. #ifndef OSSL_NO_USABLE_TLS1_3
  10965. ADD_ALL_TESTS(test_export_key_mat_early, 3);
  10966. ADD_TEST(test_key_update);
  10967. ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
  10968. ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
  10969. ADD_ALL_TESTS(test_key_update_local_in_write, 2);
  10970. ADD_ALL_TESTS(test_key_update_local_in_read, 2);
  10971. #endif
  10972. ADD_ALL_TESTS(test_ssl_clear, 8);
  10973. ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
  10974. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  10975. ADD_ALL_TESTS(test_srp, 6);
  10976. #endif
  10977. #if !defined(OPENSSL_NO_COMP_ALG)
  10978. /* Add compression case */
  10979. ADD_ALL_TESTS(test_info_callback, 8);
  10980. #else
  10981. ADD_ALL_TESTS(test_info_callback, 6);
  10982. #endif
  10983. ADD_ALL_TESTS(test_ssl_pending, 2);
  10984. ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
  10985. ADD_ALL_TESTS(test_ticket_callbacks, 20);
  10986. ADD_ALL_TESTS(test_shutdown, 7);
  10987. ADD_TEST(test_async_shutdown);
  10988. ADD_ALL_TESTS(test_incorrect_shutdown, 2);
  10989. ADD_ALL_TESTS(test_cert_cb, 6);
  10990. ADD_ALL_TESTS(test_client_cert_cb, 2);
  10991. ADD_ALL_TESTS(test_ca_names, 3);
  10992. #ifndef OPENSSL_NO_TLS1_2
  10993. ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
  10994. #endif
  10995. ADD_ALL_TESTS(test_servername, 10);
  10996. ADD_TEST(test_unknown_sigalgs_groups);
  10997. #if !defined(OPENSSL_NO_EC) \
  10998. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  10999. ADD_ALL_TESTS(test_sigalgs_available, 6);
  11000. #endif
  11001. #ifndef OPENSSL_NO_TLS1_3
  11002. ADD_ALL_TESTS(test_pluggable_group, 2);
  11003. ADD_ALL_TESTS(test_pluggable_signature, 6);
  11004. #endif
  11005. #ifndef OPENSSL_NO_TLS1_2
  11006. ADD_TEST(test_ssl_dup);
  11007. ADD_TEST(test_session_secret_cb);
  11008. # ifndef OPENSSL_NO_DH
  11009. ADD_ALL_TESTS(test_set_tmp_dh, 11);
  11010. ADD_ALL_TESTS(test_dh_auto, 7);
  11011. # endif
  11012. #endif
  11013. #ifndef OSSL_NO_USABLE_TLS1_3
  11014. ADD_TEST(test_sni_tls13);
  11015. ADD_ALL_TESTS(test_ticket_lifetime, 2);
  11016. #endif
  11017. ADD_TEST(test_inherit_verify_param);
  11018. ADD_TEST(test_set_alpn);
  11019. ADD_TEST(test_set_verify_cert_store_ssl_ctx);
  11020. ADD_TEST(test_set_verify_cert_store_ssl);
  11021. ADD_ALL_TESTS(test_session_timeout, 1);
  11022. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  11023. ADD_ALL_TESTS(test_session_cache_overflow, 4);
  11024. #endif
  11025. ADD_TEST(test_load_dhfile);
  11026. #ifndef OSSL_NO_USABLE_TLS1_3
  11027. ADD_TEST(test_read_ahead_key_change);
  11028. ADD_ALL_TESTS(test_tls13_record_padding, 4);
  11029. #endif
  11030. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  11031. ADD_ALL_TESTS(test_serverinfo_custom, 4);
  11032. #endif
  11033. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DYNAMIC_ENGINE)
  11034. ADD_ALL_TESTS(test_pipelining, 7);
  11035. #endif
  11036. ADD_ALL_TESTS(test_version, 6);
  11037. ADD_TEST(test_rstate_string);
  11038. ADD_ALL_TESTS(test_handshake_retry, 16);
  11039. ADD_TEST(test_data_retry);
  11040. ADD_ALL_TESTS(test_multi_resume, 5);
  11041. ADD_ALL_TESTS(test_select_next_proto, OSSL_NELEM(next_proto_tests));
  11042. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_NEXTPROTONEG)
  11043. ADD_ALL_TESTS(test_npn, 5);
  11044. #endif
  11045. ADD_ALL_TESTS(test_alpn, 4);
  11046. ADD_ALL_TESTS(test_no_renegotiation, 2);
  11047. return 1;
  11048. err:
  11049. OPENSSL_free(cert);
  11050. OPENSSL_free(privkey);
  11051. OPENSSL_free(cert2);
  11052. OPENSSL_free(privkey2);
  11053. return 0;
  11054. }
  11055. void cleanup_tests(void)
  11056. {
  11057. # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
  11058. EVP_PKEY_free(tmp_dh_params);
  11059. #endif
  11060. OPENSSL_free(cert);
  11061. OPENSSL_free(privkey);
  11062. OPENSSL_free(cert2);
  11063. OPENSSL_free(privkey2);
  11064. OPENSSL_free(cert1024);
  11065. OPENSSL_free(privkey1024);
  11066. OPENSSL_free(cert3072);
  11067. OPENSSL_free(privkey3072);
  11068. OPENSSL_free(cert4096);
  11069. OPENSSL_free(privkey4096);
  11070. OPENSSL_free(cert8192);
  11071. OPENSSL_free(privkey8192);
  11072. bio_s_mempacket_test_free();
  11073. bio_s_always_retry_free();
  11074. bio_s_maybe_retry_free();
  11075. OSSL_PROVIDER_unload(defctxnull);
  11076. OSSL_LIB_CTX_free(libctx);
  11077. }