sslapitest.c 342 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988698969906991699269936994699569966997699869997000700170027003700470057006700770087009701070117012701370147015701670177018701970207021702270237024702570267027702870297030703170327033703470357036703770387039704070417042704370447045704670477048704970507051705270537054705570567057705870597060706170627063706470657066706770687069707070717072707370747075707670777078707970807081708270837084708570867087708870897090709170927093709470957096709770987099710071017102710371047105710671077108710971107111711271137114711571167117711871197120712171227123712471257126712771287129713071317132713371347135713671377138713971407141714271437144714571467147714871497150715171527153715471557156715771587159716071617162716371647165716671677168716971707171717271737174717571767177717871797180718171827183718471857186718771887189719071917192719371947195719671977198719972007201720272037204720572067207720872097210721172127213721472157216721772187219722072217222722372247225722672277228722972307231723272337234723572367237723872397240724172427243724472457246724772487249725072517252725372547255725672577258725972607261726272637264726572667267726872697270727172727273727472757276727772787279728072817282728372847285728672877288728972907291729272937294729572967297729872997300730173027303730473057306730773087309731073117312731373147315731673177318731973207321732273237324732573267327732873297330733173327333733473357336733773387339734073417342734373447345734673477348734973507351735273537354735573567357735873597360736173627363736473657366736773687369737073717372737373747375737673777378737973807381738273837384738573867387738873897390739173927393739473957396739773987399740074017402740374047405740674077408740974107411741274137414741574167417741874197420742174227423742474257426742774287429743074317432743374347435743674377438743974407441744274437444744574467447744874497450745174527453745474557456745774587459746074617462746374647465746674677468746974707471747274737474747574767477747874797480748174827483748474857486748774887489749074917492749374947495749674977498749975007501750275037504750575067507750875097510751175127513751475157516751775187519752075217522752375247525752675277528752975307531753275337534753575367537753875397540754175427543754475457546754775487549755075517552755375547555755675577558755975607561756275637564756575667567756875697570757175727573757475757576757775787579758075817582758375847585758675877588758975907591759275937594759575967597759875997600760176027603760476057606760776087609761076117612761376147615761676177618761976207621762276237624762576267627762876297630763176327633763476357636763776387639764076417642764376447645764676477648764976507651765276537654765576567657765876597660766176627663766476657666766776687669767076717672767376747675767676777678767976807681768276837684768576867687768876897690769176927693769476957696769776987699770077017702770377047705770677077708770977107711771277137714771577167717771877197720772177227723772477257726772777287729773077317732773377347735773677377738773977407741774277437744774577467747774877497750775177527753775477557756775777587759776077617762776377647765776677677768776977707771777277737774777577767777777877797780778177827783778477857786778777887789779077917792779377947795779677977798779978007801780278037804780578067807780878097810781178127813781478157816781778187819782078217822782378247825782678277828782978307831783278337834783578367837783878397840784178427843784478457846784778487849785078517852785378547855785678577858785978607861786278637864786578667867786878697870787178727873787478757876787778787879788078817882788378847885788678877888788978907891789278937894789578967897789878997900790179027903790479057906790779087909791079117912791379147915791679177918791979207921792279237924792579267927792879297930793179327933793479357936793779387939794079417942794379447945794679477948794979507951795279537954795579567957795879597960796179627963796479657966796779687969797079717972797379747975797679777978797979807981798279837984798579867987798879897990799179927993799479957996799779987999800080018002800380048005800680078008800980108011801280138014801580168017801880198020802180228023802480258026802780288029803080318032803380348035803680378038803980408041804280438044804580468047804880498050805180528053805480558056805780588059806080618062806380648065806680678068806980708071807280738074807580768077807880798080808180828083808480858086808780888089809080918092809380948095809680978098809981008101810281038104810581068107810881098110811181128113811481158116811781188119812081218122812381248125812681278128812981308131813281338134813581368137813881398140814181428143814481458146814781488149815081518152815381548155815681578158815981608161816281638164816581668167816881698170817181728173817481758176817781788179818081818182818381848185818681878188818981908191819281938194819581968197819881998200820182028203820482058206820782088209821082118212821382148215821682178218821982208221822282238224822582268227822882298230823182328233823482358236823782388239824082418242824382448245824682478248824982508251825282538254825582568257825882598260826182628263826482658266826782688269827082718272827382748275827682778278827982808281828282838284828582868287828882898290829182928293829482958296829782988299830083018302830383048305830683078308830983108311831283138314831583168317831883198320832183228323832483258326832783288329833083318332833383348335833683378338833983408341834283438344834583468347834883498350835183528353835483558356835783588359836083618362836383648365836683678368836983708371837283738374837583768377837883798380838183828383838483858386838783888389839083918392839383948395839683978398839984008401840284038404840584068407840884098410841184128413841484158416841784188419842084218422842384248425842684278428842984308431843284338434843584368437843884398440844184428443844484458446844784488449845084518452845384548455845684578458845984608461846284638464846584668467846884698470847184728473847484758476847784788479848084818482848384848485848684878488848984908491849284938494849584968497849884998500850185028503850485058506850785088509851085118512851385148515851685178518851985208521852285238524852585268527852885298530853185328533853485358536853785388539854085418542854385448545854685478548854985508551855285538554855585568557855885598560856185628563856485658566856785688569857085718572857385748575857685778578857985808581858285838584858585868587858885898590859185928593859485958596859785988599860086018602860386048605860686078608860986108611861286138614861586168617861886198620862186228623862486258626862786288629863086318632863386348635863686378638863986408641864286438644864586468647864886498650865186528653865486558656865786588659866086618662866386648665866686678668866986708671867286738674867586768677867886798680868186828683868486858686868786888689869086918692869386948695869686978698869987008701870287038704870587068707870887098710871187128713871487158716871787188719872087218722872387248725872687278728872987308731873287338734873587368737873887398740874187428743874487458746874787488749875087518752875387548755875687578758875987608761876287638764876587668767876887698770877187728773877487758776877787788779878087818782878387848785878687878788878987908791879287938794879587968797879887998800880188028803880488058806880788088809881088118812881388148815881688178818881988208821882288238824882588268827882888298830883188328833883488358836883788388839884088418842884388448845884688478848884988508851885288538854885588568857885888598860886188628863886488658866886788688869887088718872887388748875887688778878887988808881888288838884888588868887888888898890889188928893889488958896889788988899890089018902890389048905890689078908890989108911891289138914891589168917891889198920892189228923892489258926892789288929893089318932893389348935893689378938893989408941894289438944894589468947894889498950895189528953895489558956895789588959896089618962896389648965896689678968896989708971897289738974897589768977897889798980898189828983898489858986898789888989899089918992899389948995899689978998899990009001900290039004900590069007900890099010901190129013901490159016901790189019902090219022902390249025902690279028902990309031903290339034903590369037903890399040904190429043904490459046904790489049905090519052905390549055905690579058905990609061906290639064906590669067906890699070907190729073907490759076907790789079908090819082908390849085908690879088908990909091909290939094909590969097909890999100910191029103910491059106910791089109911091119112911391149115911691179118911991209121912291239124912591269127912891299130913191329133913491359136913791389139914091419142914391449145914691479148914991509151915291539154915591569157915891599160916191629163916491659166916791689169917091719172917391749175917691779178917991809181918291839184918591869187918891899190919191929193919491959196919791989199920092019202920392049205920692079208920992109211921292139214921592169217921892199220922192229223922492259226922792289229923092319232923392349235923692379238923992409241924292439244924592469247924892499250925192529253925492559256925792589259926092619262926392649265926692679268926992709271927292739274927592769277927892799280928192829283928492859286928792889289929092919292929392949295929692979298929993009301930293039304930593069307930893099310931193129313931493159316931793189319932093219322932393249325932693279328932993309331933293339334933593369337933893399340934193429343934493459346934793489349935093519352935393549355935693579358935993609361936293639364936593669367936893699370937193729373937493759376937793789379938093819382938393849385938693879388938993909391939293939394939593969397939893999400940194029403940494059406940794089409941094119412941394149415941694179418941994209421942294239424942594269427942894299430943194329433943494359436943794389439944094419442944394449445944694479448944994509451945294539454945594569457945894599460946194629463946494659466946794689469947094719472947394749475947694779478947994809481948294839484948594869487948894899490949194929493949494959496949794989499950095019502950395049505950695079508950995109511951295139514951595169517951895199520952195229523952495259526952795289529953095319532953395349535953695379538953995409541954295439544954595469547954895499550955195529553955495559556955795589559956095619562956395649565956695679568956995709571957295739574957595769577957895799580958195829583958495859586958795889589959095919592959395949595959695979598959996009601960296039604960596069607960896099610961196129613961496159616961796189619962096219622962396249625962696279628962996309631963296339634963596369637963896399640964196429643964496459646964796489649965096519652965396549655965696579658965996609661966296639664966596669667966896699670967196729673967496759676967796789679968096819682968396849685968696879688968996909691969296939694969596969697969896999700970197029703970497059706970797089709971097119712971397149715971697179718971997209721972297239724972597269727972897299730973197329733973497359736973797389739974097419742974397449745974697479748974997509751975297539754975597569757975897599760976197629763976497659766976797689769977097719772977397749775977697779778977997809781978297839784978597869787978897899790979197929793979497959796979797989799980098019802980398049805980698079808980998109811981298139814981598169817981898199820982198229823982498259826982798289829983098319832983398349835983698379838983998409841984298439844984598469847984898499850985198529853985498559856985798589859986098619862986398649865986698679868986998709871987298739874987598769877987898799880988198829883988498859886988798889889989098919892989398949895989698979898989999009901990299039904990599069907990899099910991199129913991499159916991799189919992099219922992399249925992699279928992999309931993299339934993599369937993899399940994199429943994499459946994799489949995099519952995399549955995699579958995999609961996299639964996599669967996899699970997199729973997499759976997799789979998099819982998399849985998699879988998999909991999299939994999599969997999899991000010001100021000310004100051000610007100081000910010100111001210013100141001510016100171001810019100201002110022100231002410025100261002710028100291003010031100321003310034100351003610037100381003910040100411004210043100441004510046100471004810049100501005110052100531005410055100561005710058100591006010061100621006310064100651006610067100681006910070100711007210073100741007510076100771007810079100801008110082100831008410085100861008710088100891009010091100921009310094100951009610097100981009910100101011010210103101041010510106101071010810109101101011110112101131011410115101161011710118101191012010121101221012310124101251012610127101281012910130101311013210133101341013510136101371013810139101401014110142101431014410145101461014710148101491015010151101521015310154101551015610157101581015910160101611016210163101641016510166101671016810169101701017110172101731017410175101761017710178101791018010181101821018310184101851018610187101881018910190101911019210193101941019510196101971019810199102001020110202102031020410205102061020710208102091021010211102121021310214102151021610217102181021910220102211022210223102241022510226102271022810229102301023110232102331023410235102361023710238102391024010241102421024310244102451024610247102481024910250102511025210253102541025510256102571025810259102601026110262102631026410265
  1. /*
  2. * Copyright 2016-2022 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 "helpers/ssltestlib.h"
  34. #include "testutil.h"
  35. #include "testutil/output.h"
  36. #include "internal/nelem.h"
  37. #include "internal/ktls.h"
  38. #include "../ssl/ssl_local.h"
  39. #include "../ssl/record/methods/recmethod_local.h"
  40. #include "filterprov.h"
  41. #undef OSSL_NO_USABLE_TLS1_3
  42. #if defined(OPENSSL_NO_TLS1_3) \
  43. || (defined(OPENSSL_NO_EC) && defined(OPENSSL_NO_DH))
  44. /*
  45. * If we don't have ec or dh then there are no built-in groups that are usable
  46. * with TLSv1.3
  47. */
  48. # define OSSL_NO_USABLE_TLS1_3
  49. #endif
  50. /* Defined in tls-provider.c */
  51. int tls_provider_init(const OSSL_CORE_HANDLE *handle,
  52. const OSSL_DISPATCH *in,
  53. const OSSL_DISPATCH **out,
  54. void **provctx);
  55. static OSSL_LIB_CTX *libctx = NULL;
  56. static OSSL_PROVIDER *defctxnull = NULL;
  57. #ifndef OSSL_NO_USABLE_TLS1_3
  58. static SSL_SESSION *clientpsk = NULL;
  59. static SSL_SESSION *serverpsk = NULL;
  60. static const char *pskid = "Identity";
  61. static const char *srvid;
  62. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  63. size_t *idlen, SSL_SESSION **sess);
  64. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  65. size_t identity_len, SSL_SESSION **sess);
  66. static int use_session_cb_cnt = 0;
  67. static int find_session_cb_cnt = 0;
  68. static SSL_SESSION *create_a_psk(SSL *ssl);
  69. #endif
  70. static char *certsdir = NULL;
  71. static char *cert = NULL;
  72. static char *privkey = NULL;
  73. static char *cert2 = NULL;
  74. static char *privkey2 = NULL;
  75. static char *cert1024 = NULL;
  76. static char *privkey1024 = NULL;
  77. static char *cert3072 = NULL;
  78. static char *privkey3072 = NULL;
  79. static char *cert4096 = NULL;
  80. static char *privkey4096 = NULL;
  81. static char *cert8192 = NULL;
  82. static char *privkey8192 = NULL;
  83. static char *srpvfile = NULL;
  84. static char *tmpfilename = NULL;
  85. static char *dhfile = NULL;
  86. static int is_fips = 0;
  87. #define LOG_BUFFER_SIZE 2048
  88. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  89. static size_t server_log_buffer_index = 0;
  90. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  91. static size_t client_log_buffer_index = 0;
  92. static int error_writing_log = 0;
  93. #ifndef OPENSSL_NO_OCSP
  94. static const unsigned char orespder[] = "Dummy OCSP Response";
  95. static int ocsp_server_called = 0;
  96. static int ocsp_client_called = 0;
  97. static int cdummyarg = 1;
  98. static X509 *ocspcert = NULL;
  99. #endif
  100. #define NUM_EXTRA_CERTS 40
  101. #define CLIENT_VERSION_LEN 2
  102. /*
  103. * This structure is used to validate that the correct number of log messages
  104. * of various types are emitted when emitting secret logs.
  105. */
  106. struct sslapitest_log_counts {
  107. unsigned int rsa_key_exchange_count;
  108. unsigned int master_secret_count;
  109. unsigned int client_early_secret_count;
  110. unsigned int client_handshake_secret_count;
  111. unsigned int server_handshake_secret_count;
  112. unsigned int client_application_secret_count;
  113. unsigned int server_application_secret_count;
  114. unsigned int early_exporter_secret_count;
  115. unsigned int exporter_secret_count;
  116. };
  117. static int hostname_cb(SSL *s, int *al, void *arg)
  118. {
  119. const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  120. if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
  121. || strcmp(hostname, "altgoodhost") == 0))
  122. return SSL_TLSEXT_ERR_OK;
  123. return SSL_TLSEXT_ERR_NOACK;
  124. }
  125. static void client_keylog_callback(const SSL *ssl, const char *line)
  126. {
  127. int line_length = strlen(line);
  128. /* If the log doesn't fit, error out. */
  129. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  130. TEST_info("Client log too full");
  131. error_writing_log = 1;
  132. return;
  133. }
  134. strcat(client_log_buffer, line);
  135. client_log_buffer_index += line_length;
  136. client_log_buffer[client_log_buffer_index++] = '\n';
  137. }
  138. static void server_keylog_callback(const SSL *ssl, const char *line)
  139. {
  140. int line_length = strlen(line);
  141. /* If the log doesn't fit, error out. */
  142. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  143. TEST_info("Server log too full");
  144. error_writing_log = 1;
  145. return;
  146. }
  147. strcat(server_log_buffer, line);
  148. server_log_buffer_index += line_length;
  149. server_log_buffer[server_log_buffer_index++] = '\n';
  150. }
  151. static int compare_hex_encoded_buffer(const char *hex_encoded,
  152. size_t hex_length,
  153. const uint8_t *raw,
  154. size_t raw_length)
  155. {
  156. size_t i, j;
  157. char hexed[3];
  158. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  159. return 1;
  160. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  161. sprintf(hexed, "%02x", raw[i]);
  162. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  163. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  164. return 1;
  165. }
  166. return 0;
  167. }
  168. static int test_keylog_output(char *buffer, const SSL *ssl,
  169. const SSL_SESSION *session,
  170. struct sslapitest_log_counts *expected)
  171. {
  172. char *token = NULL;
  173. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  174. size_t client_random_size = SSL3_RANDOM_SIZE;
  175. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  176. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  177. unsigned int rsa_key_exchange_count = 0;
  178. unsigned int master_secret_count = 0;
  179. unsigned int client_early_secret_count = 0;
  180. unsigned int client_handshake_secret_count = 0;
  181. unsigned int server_handshake_secret_count = 0;
  182. unsigned int client_application_secret_count = 0;
  183. unsigned int server_application_secret_count = 0;
  184. unsigned int early_exporter_secret_count = 0;
  185. unsigned int exporter_secret_count = 0;
  186. for (token = strtok(buffer, " \n"); token != NULL;
  187. token = strtok(NULL, " \n")) {
  188. if (strcmp(token, "RSA") == 0) {
  189. /*
  190. * Premaster secret. Tokens should be: 16 ASCII bytes of
  191. * hex-encoded encrypted secret, then the hex-encoded pre-master
  192. * secret.
  193. */
  194. if (!TEST_ptr(token = strtok(NULL, " \n")))
  195. return 0;
  196. if (!TEST_size_t_eq(strlen(token), 16))
  197. return 0;
  198. if (!TEST_ptr(token = strtok(NULL, " \n")))
  199. return 0;
  200. /*
  201. * We can't sensibly check the log because the premaster secret is
  202. * transient, and OpenSSL doesn't keep hold of it once the master
  203. * secret is generated.
  204. */
  205. rsa_key_exchange_count++;
  206. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  207. /*
  208. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  209. * client random, then the hex-encoded master secret.
  210. */
  211. client_random_size = SSL_get_client_random(ssl,
  212. actual_client_random,
  213. SSL3_RANDOM_SIZE);
  214. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  215. return 0;
  216. if (!TEST_ptr(token = strtok(NULL, " \n")))
  217. return 0;
  218. if (!TEST_size_t_eq(strlen(token), 64))
  219. return 0;
  220. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  221. actual_client_random,
  222. client_random_size)))
  223. return 0;
  224. if (!TEST_ptr(token = strtok(NULL, " \n")))
  225. return 0;
  226. master_key_size = SSL_SESSION_get_master_key(session,
  227. actual_master_key,
  228. master_key_size);
  229. if (!TEST_size_t_ne(master_key_size, 0))
  230. return 0;
  231. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  232. actual_master_key,
  233. master_key_size)))
  234. return 0;
  235. master_secret_count++;
  236. } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
  237. || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  238. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  239. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  240. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
  241. || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
  242. || strcmp(token, "EXPORTER_SECRET") == 0) {
  243. /*
  244. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  245. * client random, and then the hex-encoded secret. In this case,
  246. * we treat all of these secrets identically and then just
  247. * distinguish between them when counting what we saw.
  248. */
  249. if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
  250. client_early_secret_count++;
  251. else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  252. client_handshake_secret_count++;
  253. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  254. server_handshake_secret_count++;
  255. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  256. client_application_secret_count++;
  257. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  258. server_application_secret_count++;
  259. else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
  260. early_exporter_secret_count++;
  261. else if (strcmp(token, "EXPORTER_SECRET") == 0)
  262. exporter_secret_count++;
  263. client_random_size = SSL_get_client_random(ssl,
  264. actual_client_random,
  265. SSL3_RANDOM_SIZE);
  266. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  267. return 0;
  268. if (!TEST_ptr(token = strtok(NULL, " \n")))
  269. return 0;
  270. if (!TEST_size_t_eq(strlen(token), 64))
  271. return 0;
  272. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  273. actual_client_random,
  274. client_random_size)))
  275. return 0;
  276. if (!TEST_ptr(token = strtok(NULL, " \n")))
  277. return 0;
  278. } else {
  279. TEST_info("Unexpected token %s\n", token);
  280. return 0;
  281. }
  282. }
  283. /* Got what we expected? */
  284. if (!TEST_size_t_eq(rsa_key_exchange_count,
  285. expected->rsa_key_exchange_count)
  286. || !TEST_size_t_eq(master_secret_count,
  287. expected->master_secret_count)
  288. || !TEST_size_t_eq(client_early_secret_count,
  289. expected->client_early_secret_count)
  290. || !TEST_size_t_eq(client_handshake_secret_count,
  291. expected->client_handshake_secret_count)
  292. || !TEST_size_t_eq(server_handshake_secret_count,
  293. expected->server_handshake_secret_count)
  294. || !TEST_size_t_eq(client_application_secret_count,
  295. expected->client_application_secret_count)
  296. || !TEST_size_t_eq(server_application_secret_count,
  297. expected->server_application_secret_count)
  298. || !TEST_size_t_eq(early_exporter_secret_count,
  299. expected->early_exporter_secret_count)
  300. || !TEST_size_t_eq(exporter_secret_count,
  301. expected->exporter_secret_count))
  302. return 0;
  303. return 1;
  304. }
  305. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  306. static int test_keylog(void)
  307. {
  308. SSL_CTX *cctx = NULL, *sctx = NULL;
  309. SSL *clientssl = NULL, *serverssl = NULL;
  310. int testresult = 0;
  311. struct sslapitest_log_counts expected;
  312. /* Clean up logging space */
  313. memset(&expected, 0, sizeof(expected));
  314. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  315. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  316. client_log_buffer_index = 0;
  317. server_log_buffer_index = 0;
  318. error_writing_log = 0;
  319. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  320. TLS_client_method(),
  321. TLS1_VERSION, 0,
  322. &sctx, &cctx, cert, privkey)))
  323. return 0;
  324. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  325. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  326. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  327. /* We also want to ensure that we use RSA-based key exchange. */
  328. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  329. goto end;
  330. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  331. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  332. goto end;
  333. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  334. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  335. == client_keylog_callback))
  336. goto end;
  337. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  338. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  339. == server_keylog_callback))
  340. goto end;
  341. /* Now do a handshake and check that the logs have been written to. */
  342. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  343. &clientssl, NULL, NULL))
  344. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  345. SSL_ERROR_NONE))
  346. || !TEST_false(error_writing_log)
  347. || !TEST_int_gt(client_log_buffer_index, 0)
  348. || !TEST_int_gt(server_log_buffer_index, 0))
  349. goto end;
  350. /*
  351. * Now we want to test that our output data was vaguely sensible. We
  352. * do that by using strtok and confirming that we have more or less the
  353. * data we expect. For both client and server, we expect to see one master
  354. * secret. The client should also see a RSA key exchange.
  355. */
  356. expected.rsa_key_exchange_count = 1;
  357. expected.master_secret_count = 1;
  358. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  359. SSL_get_session(clientssl), &expected)))
  360. goto end;
  361. expected.rsa_key_exchange_count = 0;
  362. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  363. SSL_get_session(serverssl), &expected)))
  364. goto end;
  365. testresult = 1;
  366. end:
  367. SSL_free(serverssl);
  368. SSL_free(clientssl);
  369. SSL_CTX_free(sctx);
  370. SSL_CTX_free(cctx);
  371. return testresult;
  372. }
  373. #endif
  374. #ifndef OSSL_NO_USABLE_TLS1_3
  375. static int test_keylog_no_master_key(void)
  376. {
  377. SSL_CTX *cctx = NULL, *sctx = NULL;
  378. SSL *clientssl = NULL, *serverssl = NULL;
  379. SSL_SESSION *sess = NULL;
  380. int testresult = 0;
  381. struct sslapitest_log_counts expected;
  382. unsigned char buf[1];
  383. size_t readbytes, written;
  384. /* Clean up logging space */
  385. memset(&expected, 0, sizeof(expected));
  386. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  387. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  388. client_log_buffer_index = 0;
  389. server_log_buffer_index = 0;
  390. error_writing_log = 0;
  391. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  392. TLS_client_method(), TLS1_VERSION, 0,
  393. &sctx, &cctx, cert, privkey))
  394. || !TEST_true(SSL_CTX_set_max_early_data(sctx,
  395. SSL3_RT_MAX_PLAIN_LENGTH)))
  396. return 0;
  397. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  398. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  399. goto end;
  400. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  401. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  402. == client_keylog_callback))
  403. goto end;
  404. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  405. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  406. == server_keylog_callback))
  407. goto end;
  408. /* Now do a handshake and check that the logs have been written to. */
  409. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  410. &clientssl, NULL, NULL))
  411. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  412. SSL_ERROR_NONE))
  413. || !TEST_false(error_writing_log))
  414. goto end;
  415. /*
  416. * Now we want to test that our output data was vaguely sensible. For this
  417. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  418. * TLSv1.3, but we do expect both client and server to emit keys.
  419. */
  420. expected.client_handshake_secret_count = 1;
  421. expected.server_handshake_secret_count = 1;
  422. expected.client_application_secret_count = 1;
  423. expected.server_application_secret_count = 1;
  424. expected.exporter_secret_count = 1;
  425. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  426. SSL_get_session(clientssl), &expected))
  427. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  428. SSL_get_session(serverssl),
  429. &expected)))
  430. goto end;
  431. /* Terminate old session and resume with early data. */
  432. sess = SSL_get1_session(clientssl);
  433. SSL_shutdown(clientssl);
  434. SSL_shutdown(serverssl);
  435. SSL_free(serverssl);
  436. SSL_free(clientssl);
  437. serverssl = clientssl = NULL;
  438. /* Reset key log */
  439. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  440. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  441. client_log_buffer_index = 0;
  442. server_log_buffer_index = 0;
  443. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  444. &clientssl, NULL, NULL))
  445. || !TEST_true(SSL_set_session(clientssl, sess))
  446. /* Here writing 0 length early data is enough. */
  447. || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  448. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  449. &readbytes),
  450. SSL_READ_EARLY_DATA_ERROR)
  451. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  452. SSL_EARLY_DATA_ACCEPTED)
  453. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  454. SSL_ERROR_NONE))
  455. || !TEST_true(SSL_session_reused(clientssl)))
  456. goto end;
  457. /* In addition to the previous entries, expect early secrets. */
  458. expected.client_early_secret_count = 1;
  459. expected.early_exporter_secret_count = 1;
  460. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  461. SSL_get_session(clientssl), &expected))
  462. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  463. SSL_get_session(serverssl),
  464. &expected)))
  465. goto end;
  466. testresult = 1;
  467. end:
  468. SSL_SESSION_free(sess);
  469. SSL_free(serverssl);
  470. SSL_free(clientssl);
  471. SSL_CTX_free(sctx);
  472. SSL_CTX_free(cctx);
  473. return testresult;
  474. }
  475. #endif
  476. static int verify_retry_cb(X509_STORE_CTX *ctx, void *arg)
  477. {
  478. int res = X509_verify_cert(ctx);
  479. int idx = SSL_get_ex_data_X509_STORE_CTX_idx();
  480. SSL *ssl;
  481. /* this should not happen but check anyway */
  482. if (idx < 0
  483. || (ssl = X509_STORE_CTX_get_ex_data(ctx, idx)) == NULL)
  484. return 0;
  485. if (res == 0 && X509_STORE_CTX_get_error(ctx) ==
  486. X509_V_ERR_UNABLE_TO_GET_ISSUER_CERT_LOCALLY)
  487. /* indicate SSL_ERROR_WANT_RETRY_VERIFY */
  488. return SSL_set_retry_verify(ssl);
  489. return res;
  490. }
  491. static int test_client_cert_verify_cb(void)
  492. {
  493. /* server key, cert, chain, and root */
  494. char *skey = test_mk_file_path(certsdir, "leaf.key");
  495. char *leaf = test_mk_file_path(certsdir, "leaf.pem");
  496. char *int2 = test_mk_file_path(certsdir, "subinterCA.pem");
  497. char *int1 = test_mk_file_path(certsdir, "interCA.pem");
  498. char *root = test_mk_file_path(certsdir, "rootCA.pem");
  499. X509 *crt1 = NULL, *crt2 = NULL;
  500. STACK_OF(X509) *server_chain;
  501. SSL_CTX *cctx = NULL, *sctx = NULL;
  502. SSL *clientssl = NULL, *serverssl = NULL;
  503. int testresult = 0;
  504. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  505. TLS_client_method(), TLS1_VERSION, 0,
  506. &sctx, &cctx, NULL, NULL)))
  507. goto end;
  508. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(sctx, leaf), 1)
  509. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx, skey,
  510. SSL_FILETYPE_PEM), 1)
  511. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1))
  512. goto end;
  513. if (!TEST_true(SSL_CTX_load_verify_locations(cctx, root, NULL)))
  514. goto end;
  515. SSL_CTX_set_verify(cctx, SSL_VERIFY_PEER, NULL);
  516. SSL_CTX_set_cert_verify_callback(cctx, verify_retry_cb, NULL);
  517. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  518. &clientssl, NULL, NULL)))
  519. goto end;
  520. /* attempt SSL_connect() with incomplete server chain */
  521. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  522. SSL_ERROR_WANT_RETRY_VERIFY)))
  523. goto end;
  524. /* application provides intermediate certs needed to verify server cert */
  525. if (!TEST_ptr((crt1 = load_cert_pem(int1, libctx)))
  526. || !TEST_ptr((crt2 = load_cert_pem(int2, libctx)))
  527. || !TEST_ptr((server_chain = SSL_get_peer_cert_chain(clientssl))))
  528. goto end;
  529. /* add certs in reverse order to demonstrate real chain building */
  530. if (!TEST_true(sk_X509_push(server_chain, crt1)))
  531. goto end;
  532. crt1 = NULL;
  533. if (!TEST_true(sk_X509_push(server_chain, crt2)))
  534. goto end;
  535. crt2 = NULL;
  536. /* continue SSL_connect(), must now succeed with completed server chain */
  537. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  538. SSL_ERROR_NONE)))
  539. goto end;
  540. testresult = 1;
  541. end:
  542. X509_free(crt1);
  543. X509_free(crt2);
  544. if (clientssl != NULL) {
  545. SSL_shutdown(clientssl);
  546. SSL_free(clientssl);
  547. }
  548. if (serverssl != NULL) {
  549. SSL_shutdown(serverssl);
  550. SSL_free(serverssl);
  551. }
  552. SSL_CTX_free(sctx);
  553. SSL_CTX_free(cctx);
  554. OPENSSL_free(skey);
  555. OPENSSL_free(leaf);
  556. OPENSSL_free(int2);
  557. OPENSSL_free(int1);
  558. OPENSSL_free(root);
  559. return testresult;
  560. }
  561. static int test_ssl_build_cert_chain(void)
  562. {
  563. int ret = 0;
  564. SSL_CTX *ssl_ctx = NULL;
  565. SSL *ssl = NULL;
  566. char *skey = test_mk_file_path(certsdir, "leaf.key");
  567. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  568. if (!TEST_ptr(ssl_ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  569. goto end;
  570. if (!TEST_ptr(ssl = SSL_new(ssl_ctx)))
  571. goto end;
  572. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  573. if (!TEST_int_eq(SSL_use_certificate_chain_file(ssl, leaf_chain), 1)
  574. || !TEST_int_eq(SSL_use_PrivateKey_file(ssl, skey, SSL_FILETYPE_PEM), 1)
  575. || !TEST_int_eq(SSL_check_private_key(ssl), 1))
  576. goto end;
  577. if (!TEST_true(SSL_build_cert_chain(ssl, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  578. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  579. goto end;
  580. ret = 1;
  581. end:
  582. SSL_free(ssl);
  583. SSL_CTX_free(ssl_ctx);
  584. OPENSSL_free(leaf_chain);
  585. OPENSSL_free(skey);
  586. return ret;
  587. }
  588. static int get_password_cb(char *buf, int size, int rw_flag, void *userdata)
  589. {
  590. static const char pass[] = "testpass";
  591. if (!TEST_int_eq(size, PEM_BUFSIZE))
  592. return -1;
  593. memcpy(buf, pass, sizeof(pass) - 1);
  594. return sizeof(pass) - 1;
  595. }
  596. static int test_ssl_ctx_build_cert_chain(void)
  597. {
  598. int ret = 0;
  599. SSL_CTX *ctx = NULL;
  600. char *skey = test_mk_file_path(certsdir, "leaf-encrypted.key");
  601. char *leaf_chain = test_mk_file_path(certsdir, "leaf-chain.pem");
  602. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method())))
  603. goto end;
  604. SSL_CTX_set_default_passwd_cb(ctx, get_password_cb);
  605. /* leaf_chain contains leaf + subinterCA + interCA + rootCA */
  606. if (!TEST_int_eq(SSL_CTX_use_certificate_chain_file(ctx, leaf_chain), 1)
  607. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(ctx, skey,
  608. SSL_FILETYPE_PEM), 1)
  609. || !TEST_int_eq(SSL_CTX_check_private_key(ctx), 1))
  610. goto end;
  611. if (!TEST_true(SSL_CTX_build_cert_chain(ctx, SSL_BUILD_CHAIN_FLAG_NO_ROOT
  612. | SSL_BUILD_CHAIN_FLAG_CHECK)))
  613. goto end;
  614. ret = 1;
  615. end:
  616. SSL_CTX_free(ctx);
  617. OPENSSL_free(leaf_chain);
  618. OPENSSL_free(skey);
  619. return ret;
  620. }
  621. #ifndef OPENSSL_NO_TLS1_2
  622. static int full_client_hello_callback(SSL *s, int *al, void *arg)
  623. {
  624. int *ctr = arg;
  625. const unsigned char *p;
  626. int *exts;
  627. /* We only configure two ciphers, but the SCSV is added automatically. */
  628. #ifdef OPENSSL_NO_EC
  629. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  630. #else
  631. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  632. 0x2c, 0x00, 0xff};
  633. #endif
  634. const int expected_extensions[] = {
  635. #ifndef OPENSSL_NO_EC
  636. 11, 10,
  637. #endif
  638. 35, 22, 23, 13};
  639. size_t len;
  640. /* Make sure we can defer processing and get called back. */
  641. if ((*ctr)++ == 0)
  642. return SSL_CLIENT_HELLO_RETRY;
  643. len = SSL_client_hello_get0_ciphers(s, &p);
  644. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  645. || !TEST_size_t_eq(
  646. SSL_client_hello_get0_compression_methods(s, &p), 1)
  647. || !TEST_int_eq(*p, 0))
  648. return SSL_CLIENT_HELLO_ERROR;
  649. if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
  650. return SSL_CLIENT_HELLO_ERROR;
  651. if (len != OSSL_NELEM(expected_extensions) ||
  652. memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
  653. printf("ClientHello callback expected extensions mismatch\n");
  654. OPENSSL_free(exts);
  655. return SSL_CLIENT_HELLO_ERROR;
  656. }
  657. OPENSSL_free(exts);
  658. return SSL_CLIENT_HELLO_SUCCESS;
  659. }
  660. static int test_client_hello_cb(void)
  661. {
  662. SSL_CTX *cctx = NULL, *sctx = NULL;
  663. SSL *clientssl = NULL, *serverssl = NULL;
  664. int testctr = 0, testresult = 0;
  665. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  666. TLS_client_method(), TLS1_VERSION, 0,
  667. &sctx, &cctx, cert, privkey)))
  668. goto end;
  669. SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
  670. /* The gimpy cipher list we configure can't do TLS 1.3. */
  671. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  672. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  673. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  674. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  675. &clientssl, NULL, NULL))
  676. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  677. SSL_ERROR_WANT_CLIENT_HELLO_CB))
  678. /*
  679. * Passing a -1 literal is a hack since
  680. * the real value was lost.
  681. * */
  682. || !TEST_int_eq(SSL_get_error(serverssl, -1),
  683. SSL_ERROR_WANT_CLIENT_HELLO_CB)
  684. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  685. SSL_ERROR_NONE)))
  686. goto end;
  687. testresult = 1;
  688. end:
  689. SSL_free(serverssl);
  690. SSL_free(clientssl);
  691. SSL_CTX_free(sctx);
  692. SSL_CTX_free(cctx);
  693. return testresult;
  694. }
  695. static int test_no_ems(void)
  696. {
  697. SSL_CTX *cctx = NULL, *sctx = NULL;
  698. SSL *clientssl = NULL, *serverssl = NULL;
  699. int testresult = 0;
  700. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  701. TLS1_VERSION, TLS1_2_VERSION,
  702. &sctx, &cctx, cert, privkey)) {
  703. printf("Unable to create SSL_CTX pair\n");
  704. goto end;
  705. }
  706. SSL_CTX_set_options(sctx, SSL_OP_NO_EXTENDED_MASTER_SECRET);
  707. if (!create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL, NULL)) {
  708. printf("Unable to create SSL objects\n");
  709. goto end;
  710. }
  711. if (!create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)) {
  712. printf("Creating SSL connection failed\n");
  713. goto end;
  714. }
  715. if (SSL_get_extms_support(serverssl)) {
  716. printf("Server reports Extended Master Secret support\n");
  717. goto end;
  718. }
  719. if (SSL_get_extms_support(clientssl)) {
  720. printf("Client reports Extended Master Secret support\n");
  721. goto end;
  722. }
  723. testresult = 1;
  724. end:
  725. SSL_free(serverssl);
  726. SSL_free(clientssl);
  727. SSL_CTX_free(sctx);
  728. SSL_CTX_free(cctx);
  729. return testresult;
  730. }
  731. /*
  732. * Very focused test to exercise a single case in the server-side state
  733. * machine, when the ChangeCipherState message needs to actually change
  734. * from one cipher to a different cipher (i.e., not changing from null
  735. * encryption to real encryption).
  736. */
  737. static int test_ccs_change_cipher(void)
  738. {
  739. SSL_CTX *cctx = NULL, *sctx = NULL;
  740. SSL *clientssl = NULL, *serverssl = NULL;
  741. SSL_SESSION *sess = NULL, *sesspre, *sesspost;
  742. int testresult = 0;
  743. int i;
  744. unsigned char buf;
  745. size_t readbytes;
  746. /*
  747. * Create a connection so we can resume and potentially (but not) use
  748. * a different cipher in the second connection.
  749. */
  750. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  751. TLS_client_method(),
  752. TLS1_VERSION, TLS1_2_VERSION,
  753. &sctx, &cctx, cert, privkey))
  754. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
  755. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  756. NULL, NULL))
  757. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  758. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  759. SSL_ERROR_NONE))
  760. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  761. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  762. goto end;
  763. shutdown_ssl_connection(serverssl, clientssl);
  764. serverssl = clientssl = NULL;
  765. /* Resume, preferring a different cipher. Our server will force the
  766. * same cipher to be used as the initial handshake. */
  767. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  768. NULL, NULL))
  769. || !TEST_true(SSL_set_session(clientssl, sess))
  770. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
  771. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  772. SSL_ERROR_NONE))
  773. || !TEST_true(SSL_session_reused(clientssl))
  774. || !TEST_true(SSL_session_reused(serverssl))
  775. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  776. || !TEST_ptr_eq(sesspre, sesspost)
  777. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  778. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  779. goto end;
  780. shutdown_ssl_connection(serverssl, clientssl);
  781. serverssl = clientssl = NULL;
  782. /*
  783. * Now create a fresh connection and try to renegotiate a different
  784. * cipher on it.
  785. */
  786. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  787. NULL, NULL))
  788. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  789. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  790. SSL_ERROR_NONE))
  791. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  792. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
  793. || !TEST_true(SSL_renegotiate(clientssl))
  794. || !TEST_true(SSL_renegotiate_pending(clientssl)))
  795. goto end;
  796. /* Actually drive the renegotiation. */
  797. for (i = 0; i < 3; i++) {
  798. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  799. if (!TEST_ulong_eq(readbytes, 0))
  800. goto end;
  801. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  802. SSL_ERROR_WANT_READ)) {
  803. goto end;
  804. }
  805. if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
  806. if (!TEST_ulong_eq(readbytes, 0))
  807. goto end;
  808. } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
  809. SSL_ERROR_WANT_READ)) {
  810. goto end;
  811. }
  812. }
  813. /* sesspre and sesspost should be different since the cipher changed. */
  814. if (!TEST_false(SSL_renegotiate_pending(clientssl))
  815. || !TEST_false(SSL_session_reused(clientssl))
  816. || !TEST_false(SSL_session_reused(serverssl))
  817. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  818. || !TEST_ptr_ne(sesspre, sesspost)
  819. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  820. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  821. goto end;
  822. shutdown_ssl_connection(serverssl, clientssl);
  823. serverssl = clientssl = NULL;
  824. testresult = 1;
  825. end:
  826. SSL_free(serverssl);
  827. SSL_free(clientssl);
  828. SSL_CTX_free(sctx);
  829. SSL_CTX_free(cctx);
  830. SSL_SESSION_free(sess);
  831. return testresult;
  832. }
  833. #endif
  834. static int execute_test_large_message(const SSL_METHOD *smeth,
  835. const SSL_METHOD *cmeth,
  836. int min_version, int max_version,
  837. int read_ahead)
  838. {
  839. SSL_CTX *cctx = NULL, *sctx = NULL;
  840. SSL *clientssl = NULL, *serverssl = NULL;
  841. int testresult = 0;
  842. int i;
  843. BIO *certbio = NULL;
  844. X509 *chaincert = NULL;
  845. int certlen;
  846. if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
  847. goto end;
  848. if (!TEST_ptr(chaincert = X509_new_ex(libctx, NULL)))
  849. goto end;
  850. if (PEM_read_bio_X509(certbio, &chaincert, NULL, NULL) == NULL)
  851. goto end;
  852. BIO_free(certbio);
  853. certbio = NULL;
  854. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  855. max_version, &sctx, &cctx, cert,
  856. privkey)))
  857. goto end;
  858. #ifdef OPENSSL_NO_DTLS1_2
  859. if (smeth == DTLS_server_method()) {
  860. /*
  861. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  862. * level 0
  863. */
  864. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  865. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  866. "DEFAULT:@SECLEVEL=0")))
  867. goto end;
  868. }
  869. #endif
  870. if (read_ahead) {
  871. /*
  872. * Test that read_ahead works correctly when dealing with large
  873. * records
  874. */
  875. SSL_CTX_set_read_ahead(cctx, 1);
  876. }
  877. /*
  878. * We assume the supplied certificate is big enough so that if we add
  879. * NUM_EXTRA_CERTS it will make the overall message large enough. The
  880. * default buffer size is requested to be 16k, but due to the way BUF_MEM
  881. * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
  882. * test we need to have a message larger than that.
  883. */
  884. certlen = i2d_X509(chaincert, NULL);
  885. OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
  886. (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
  887. for (i = 0; i < NUM_EXTRA_CERTS; i++) {
  888. if (!X509_up_ref(chaincert))
  889. goto end;
  890. if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
  891. X509_free(chaincert);
  892. goto end;
  893. }
  894. }
  895. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  896. NULL, NULL))
  897. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  898. SSL_ERROR_NONE)))
  899. goto end;
  900. /*
  901. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  902. * doesn't leak.
  903. */
  904. if (!TEST_true(SSL_clear(serverssl)))
  905. goto end;
  906. testresult = 1;
  907. end:
  908. BIO_free(certbio);
  909. X509_free(chaincert);
  910. SSL_free(serverssl);
  911. SSL_free(clientssl);
  912. SSL_CTX_free(sctx);
  913. SSL_CTX_free(cctx);
  914. return testresult;
  915. }
  916. #if !defined(OPENSSL_NO_SOCK) && !defined(OPENSSL_NO_KTLS) && \
  917. !(defined(OSSL_NO_USABLE_TLS1_3) && defined(OPENSSL_NO_TLS1_2))
  918. /* sock must be connected */
  919. static int ktls_chk_platform(int sock)
  920. {
  921. if (!ktls_enable(sock))
  922. return 0;
  923. return 1;
  924. }
  925. static int ping_pong_query(SSL *clientssl, SSL *serverssl)
  926. {
  927. static char count = 1;
  928. unsigned char cbuf[16000] = {0};
  929. unsigned char sbuf[16000];
  930. size_t err = 0;
  931. char crec_wseq_before[SEQ_NUM_SIZE];
  932. char crec_wseq_after[SEQ_NUM_SIZE];
  933. char crec_rseq_before[SEQ_NUM_SIZE];
  934. char crec_rseq_after[SEQ_NUM_SIZE];
  935. char srec_wseq_before[SEQ_NUM_SIZE];
  936. char srec_wseq_after[SEQ_NUM_SIZE];
  937. char srec_rseq_before[SEQ_NUM_SIZE];
  938. char srec_rseq_after[SEQ_NUM_SIZE];
  939. SSL_CONNECTION *clientsc, *serversc;
  940. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  941. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  942. goto end;
  943. cbuf[0] = count++;
  944. memcpy(crec_wseq_before, &clientsc->rlayer.write_sequence, SEQ_NUM_SIZE);
  945. memcpy(crec_rseq_before, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  946. memcpy(srec_wseq_before, &serversc->rlayer.write_sequence, SEQ_NUM_SIZE);
  947. memcpy(srec_rseq_before, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  948. if (!TEST_true(SSL_write(clientssl, cbuf, sizeof(cbuf)) == sizeof(cbuf)))
  949. goto end;
  950. while ((err = SSL_read(serverssl, &sbuf, sizeof(sbuf))) != sizeof(sbuf)) {
  951. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_READ) {
  952. goto end;
  953. }
  954. }
  955. if (!TEST_true(SSL_write(serverssl, sbuf, sizeof(sbuf)) == sizeof(sbuf)))
  956. goto end;
  957. while ((err = SSL_read(clientssl, &cbuf, sizeof(cbuf))) != sizeof(cbuf)) {
  958. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ) {
  959. goto end;
  960. }
  961. }
  962. memcpy(crec_wseq_after, &clientsc->rlayer.write_sequence, SEQ_NUM_SIZE);
  963. memcpy(crec_rseq_after, &clientsc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  964. memcpy(srec_wseq_after, &serversc->rlayer.write_sequence, SEQ_NUM_SIZE);
  965. memcpy(srec_rseq_after, &serversc->rlayer.rrl->sequence, SEQ_NUM_SIZE);
  966. /* verify the payload */
  967. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  968. goto end;
  969. /*
  970. * If ktls is used then kernel sequences are used instead of
  971. * OpenSSL sequences
  972. */
  973. if (!BIO_get_ktls_send(clientsc->wbio)) {
  974. if (!TEST_mem_ne(crec_wseq_before, SEQ_NUM_SIZE,
  975. crec_wseq_after, SEQ_NUM_SIZE))
  976. goto end;
  977. } else {
  978. if (!TEST_mem_eq(crec_wseq_before, SEQ_NUM_SIZE,
  979. crec_wseq_after, SEQ_NUM_SIZE))
  980. goto end;
  981. }
  982. if (!BIO_get_ktls_send(serversc->wbio)) {
  983. if (!TEST_mem_ne(srec_wseq_before, SEQ_NUM_SIZE,
  984. srec_wseq_after, SEQ_NUM_SIZE))
  985. goto end;
  986. } else {
  987. if (!TEST_mem_eq(srec_wseq_before, SEQ_NUM_SIZE,
  988. srec_wseq_after, SEQ_NUM_SIZE))
  989. goto end;
  990. }
  991. if (!BIO_get_ktls_recv(clientsc->wbio)) {
  992. if (!TEST_mem_ne(crec_rseq_before, SEQ_NUM_SIZE,
  993. crec_rseq_after, SEQ_NUM_SIZE))
  994. goto end;
  995. } else {
  996. if (!TEST_mem_eq(crec_rseq_before, SEQ_NUM_SIZE,
  997. crec_rseq_after, SEQ_NUM_SIZE))
  998. goto end;
  999. }
  1000. if (!BIO_get_ktls_recv(serversc->wbio)) {
  1001. if (!TEST_mem_ne(srec_rseq_before, SEQ_NUM_SIZE,
  1002. srec_rseq_after, SEQ_NUM_SIZE))
  1003. goto end;
  1004. } else {
  1005. if (!TEST_mem_eq(srec_rseq_before, SEQ_NUM_SIZE,
  1006. srec_rseq_after, SEQ_NUM_SIZE))
  1007. goto end;
  1008. }
  1009. return 1;
  1010. end:
  1011. return 0;
  1012. }
  1013. static int execute_test_ktls(int cis_ktls, int sis_ktls,
  1014. int tls_version, const char *cipher)
  1015. {
  1016. SSL_CTX *cctx = NULL, *sctx = NULL;
  1017. SSL *clientssl = NULL, *serverssl = NULL;
  1018. int ktls_used = 0, testresult = 0;
  1019. int cfd = -1, sfd = -1;
  1020. int rx_supported;
  1021. SSL_CONNECTION *clientsc, *serversc;
  1022. if (!TEST_true(create_test_sockets(&cfd, &sfd)))
  1023. goto end;
  1024. /* Skip this test if the platform does not support ktls */
  1025. if (!ktls_chk_platform(cfd)) {
  1026. testresult = TEST_skip("Kernel does not support KTLS");
  1027. goto end;
  1028. }
  1029. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1030. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1031. goto end;
  1032. }
  1033. /* Create a session based on SHA-256 */
  1034. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1035. TLS_client_method(),
  1036. tls_version, tls_version,
  1037. &sctx, &cctx, cert, privkey)))
  1038. goto end;
  1039. if (tls_version == TLS1_3_VERSION) {
  1040. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1041. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1042. goto end;
  1043. } else {
  1044. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1045. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1046. goto end;
  1047. }
  1048. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1049. &clientssl, sfd, cfd)))
  1050. goto end;
  1051. if (!TEST_ptr(clientsc = SSL_CONNECTION_FROM_SSL_ONLY(clientssl))
  1052. || !TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1053. goto end;
  1054. if (cis_ktls) {
  1055. if (!TEST_true(SSL_set_options(clientssl, SSL_OP_ENABLE_KTLS)))
  1056. goto end;
  1057. }
  1058. if (sis_ktls) {
  1059. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1060. goto end;
  1061. }
  1062. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  1063. goto end;
  1064. /*
  1065. * The running kernel may not support a given cipher suite
  1066. * or direction, so just check that KTLS isn't used when it
  1067. * isn't enabled.
  1068. */
  1069. if (!cis_ktls) {
  1070. if (!TEST_false(BIO_get_ktls_send(clientsc->wbio)))
  1071. goto end;
  1072. } else {
  1073. if (BIO_get_ktls_send(clientsc->wbio))
  1074. ktls_used = 1;
  1075. }
  1076. if (!sis_ktls) {
  1077. if (!TEST_false(BIO_get_ktls_send(serversc->wbio)))
  1078. goto end;
  1079. } else {
  1080. if (BIO_get_ktls_send(serversc->wbio))
  1081. ktls_used = 1;
  1082. }
  1083. #if defined(OPENSSL_NO_KTLS_RX)
  1084. rx_supported = 0;
  1085. #else
  1086. rx_supported = 1;
  1087. #endif
  1088. if (!cis_ktls || !rx_supported) {
  1089. if (!TEST_false(BIO_get_ktls_recv(clientsc->rbio)))
  1090. goto end;
  1091. } else {
  1092. if (BIO_get_ktls_send(clientsc->rbio))
  1093. ktls_used = 1;
  1094. }
  1095. if (!sis_ktls || !rx_supported) {
  1096. if (!TEST_false(BIO_get_ktls_recv(serversc->rbio)))
  1097. goto end;
  1098. } else {
  1099. if (BIO_get_ktls_send(serversc->rbio))
  1100. ktls_used = 1;
  1101. }
  1102. if ((cis_ktls || sis_ktls) && !ktls_used) {
  1103. testresult = TEST_skip("KTLS not supported for %s cipher %s",
  1104. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1105. "TLS 1.2", cipher);
  1106. goto end;
  1107. }
  1108. if (!TEST_true(ping_pong_query(clientssl, serverssl)))
  1109. goto end;
  1110. testresult = 1;
  1111. end:
  1112. if (clientssl) {
  1113. SSL_shutdown(clientssl);
  1114. SSL_free(clientssl);
  1115. }
  1116. if (serverssl) {
  1117. SSL_shutdown(serverssl);
  1118. SSL_free(serverssl);
  1119. }
  1120. SSL_CTX_free(sctx);
  1121. SSL_CTX_free(cctx);
  1122. serverssl = clientssl = NULL;
  1123. if (cfd != -1)
  1124. close(cfd);
  1125. if (sfd != -1)
  1126. close(sfd);
  1127. return testresult;
  1128. }
  1129. #define SENDFILE_SZ (16 * 4096)
  1130. #define SENDFILE_CHUNK (4 * 4096)
  1131. #define min(a,b) ((a) > (b) ? (b) : (a))
  1132. static int execute_test_ktls_sendfile(int tls_version, const char *cipher)
  1133. {
  1134. SSL_CTX *cctx = NULL, *sctx = NULL;
  1135. SSL *clientssl = NULL, *serverssl = NULL;
  1136. unsigned char *buf, *buf_dst;
  1137. BIO *out = NULL, *in = NULL;
  1138. int cfd = -1, sfd = -1, ffd, err;
  1139. ssize_t chunk_size = 0;
  1140. off_t chunk_off = 0;
  1141. int testresult = 0;
  1142. FILE *ffdp;
  1143. SSL_CONNECTION *serversc;
  1144. buf = OPENSSL_zalloc(SENDFILE_SZ);
  1145. buf_dst = OPENSSL_zalloc(SENDFILE_SZ);
  1146. if (!TEST_ptr(buf) || !TEST_ptr(buf_dst)
  1147. || !TEST_true(create_test_sockets(&cfd, &sfd)))
  1148. goto end;
  1149. /* Skip this test if the platform does not support ktls */
  1150. if (!ktls_chk_platform(sfd)) {
  1151. testresult = TEST_skip("Kernel does not support KTLS");
  1152. goto end;
  1153. }
  1154. if (is_fips && strstr(cipher, "CHACHA") != NULL) {
  1155. testresult = TEST_skip("CHACHA is not supported in FIPS");
  1156. goto end;
  1157. }
  1158. /* Create a session based on SHA-256 */
  1159. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1160. TLS_client_method(),
  1161. tls_version, tls_version,
  1162. &sctx, &cctx, cert, privkey)))
  1163. goto end;
  1164. if (tls_version == TLS1_3_VERSION) {
  1165. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, cipher))
  1166. || !TEST_true(SSL_CTX_set_ciphersuites(sctx, cipher)))
  1167. goto end;
  1168. } else {
  1169. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipher))
  1170. || !TEST_true(SSL_CTX_set_cipher_list(sctx, cipher)))
  1171. goto end;
  1172. }
  1173. if (!TEST_true(create_ssl_objects2(sctx, cctx, &serverssl,
  1174. &clientssl, sfd, cfd)))
  1175. goto end;
  1176. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1177. goto end;
  1178. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_ENABLE_KTLS)))
  1179. goto end;
  1180. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1181. SSL_ERROR_NONE)))
  1182. goto end;
  1183. if (!BIO_get_ktls_send(serversc->wbio)) {
  1184. testresult = TEST_skip("Failed to enable KTLS for %s cipher %s",
  1185. tls_version == TLS1_3_VERSION ? "TLS 1.3" :
  1186. "TLS 1.2", cipher);
  1187. goto end;
  1188. }
  1189. if (!TEST_int_gt(RAND_bytes_ex(libctx, buf, SENDFILE_SZ, 0), 0))
  1190. goto end;
  1191. out = BIO_new_file(tmpfilename, "wb");
  1192. if (!TEST_ptr(out))
  1193. goto end;
  1194. if (BIO_write(out, buf, SENDFILE_SZ) != SENDFILE_SZ)
  1195. goto end;
  1196. BIO_free(out);
  1197. out = NULL;
  1198. in = BIO_new_file(tmpfilename, "rb");
  1199. BIO_get_fp(in, &ffdp);
  1200. ffd = fileno(ffdp);
  1201. while (chunk_off < SENDFILE_SZ) {
  1202. chunk_size = min(SENDFILE_CHUNK, SENDFILE_SZ - chunk_off);
  1203. while ((err = SSL_sendfile(serverssl,
  1204. ffd,
  1205. chunk_off,
  1206. chunk_size,
  1207. 0)) != chunk_size) {
  1208. if (SSL_get_error(serverssl, err) != SSL_ERROR_WANT_WRITE)
  1209. goto end;
  1210. }
  1211. while ((err = SSL_read(clientssl,
  1212. buf_dst + chunk_off,
  1213. chunk_size)) != chunk_size) {
  1214. if (SSL_get_error(clientssl, err) != SSL_ERROR_WANT_READ)
  1215. goto end;
  1216. }
  1217. /* verify the payload */
  1218. if (!TEST_mem_eq(buf_dst + chunk_off,
  1219. chunk_size,
  1220. buf + chunk_off,
  1221. chunk_size))
  1222. goto end;
  1223. chunk_off += chunk_size;
  1224. }
  1225. testresult = 1;
  1226. end:
  1227. if (clientssl) {
  1228. SSL_shutdown(clientssl);
  1229. SSL_free(clientssl);
  1230. }
  1231. if (serverssl) {
  1232. SSL_shutdown(serverssl);
  1233. SSL_free(serverssl);
  1234. }
  1235. SSL_CTX_free(sctx);
  1236. SSL_CTX_free(cctx);
  1237. serverssl = clientssl = NULL;
  1238. BIO_free(out);
  1239. BIO_free(in);
  1240. if (cfd != -1)
  1241. close(cfd);
  1242. if (sfd != -1)
  1243. close(sfd);
  1244. OPENSSL_free(buf);
  1245. OPENSSL_free(buf_dst);
  1246. return testresult;
  1247. }
  1248. static struct ktls_test_cipher {
  1249. int tls_version;
  1250. const char *cipher;
  1251. } ktls_test_ciphers[] = {
  1252. # if !defined(OPENSSL_NO_TLS1_2)
  1253. # ifdef OPENSSL_KTLS_AES_GCM_128
  1254. { TLS1_2_VERSION, "AES128-GCM-SHA256" },
  1255. # endif
  1256. # ifdef OPENSSL_KTLS_AES_CCM_128
  1257. { TLS1_2_VERSION, "AES128-CCM"},
  1258. # endif
  1259. # ifdef OPENSSL_KTLS_AES_GCM_256
  1260. { TLS1_2_VERSION, "AES256-GCM-SHA384"},
  1261. # endif
  1262. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1263. { TLS1_2_VERSION, "ECDHE-RSA-CHACHA20-POLY1305"},
  1264. # endif
  1265. # endif
  1266. # if !defined(OSSL_NO_USABLE_TLS1_3)
  1267. # ifdef OPENSSL_KTLS_AES_GCM_128
  1268. { TLS1_3_VERSION, "TLS_AES_128_GCM_SHA256" },
  1269. # endif
  1270. # ifdef OPENSSL_KTLS_AES_CCM_128
  1271. { TLS1_3_VERSION, "TLS_AES_128_CCM_SHA256" },
  1272. # endif
  1273. # ifdef OPENSSL_KTLS_AES_GCM_256
  1274. { TLS1_3_VERSION, "TLS_AES_256_GCM_SHA384" },
  1275. # endif
  1276. # ifdef OPENSSL_KTLS_CHACHA20_POLY1305
  1277. { TLS1_3_VERSION, "TLS_CHACHA20_POLY1305_SHA256" },
  1278. # endif
  1279. # endif
  1280. };
  1281. #define NUM_KTLS_TEST_CIPHERS \
  1282. (sizeof(ktls_test_ciphers) / sizeof(ktls_test_ciphers[0]))
  1283. static int test_ktls(int test)
  1284. {
  1285. struct ktls_test_cipher *cipher;
  1286. int cis_ktls, sis_ktls;
  1287. OPENSSL_assert(test / 4 < (int)NUM_KTLS_TEST_CIPHERS);
  1288. cipher = &ktls_test_ciphers[test / 4];
  1289. cis_ktls = (test & 1) != 0;
  1290. sis_ktls = (test & 2) != 0;
  1291. return execute_test_ktls(cis_ktls, sis_ktls, cipher->tls_version,
  1292. cipher->cipher);
  1293. }
  1294. static int test_ktls_sendfile(int tst)
  1295. {
  1296. struct ktls_test_cipher *cipher;
  1297. OPENSSL_assert(tst < (int)NUM_KTLS_TEST_CIPHERS);
  1298. cipher = &ktls_test_ciphers[tst];
  1299. return execute_test_ktls_sendfile(cipher->tls_version, cipher->cipher);
  1300. }
  1301. #endif
  1302. static int test_large_message_tls(void)
  1303. {
  1304. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1305. TLS1_VERSION, 0, 0);
  1306. }
  1307. static int test_large_message_tls_read_ahead(void)
  1308. {
  1309. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  1310. TLS1_VERSION, 0, 1);
  1311. }
  1312. #ifndef OPENSSL_NO_DTLS
  1313. static int test_large_message_dtls(void)
  1314. {
  1315. # ifdef OPENSSL_NO_DTLS1_2
  1316. /* Not supported in the FIPS provider */
  1317. if (is_fips)
  1318. return 1;
  1319. # endif
  1320. /*
  1321. * read_ahead is not relevant to DTLS because DTLS always acts as if
  1322. * read_ahead is set.
  1323. */
  1324. return execute_test_large_message(DTLS_server_method(),
  1325. DTLS_client_method(),
  1326. DTLS1_VERSION, 0, 0);
  1327. }
  1328. #endif
  1329. static int execute_cleanse_plaintext(const SSL_METHOD *smeth,
  1330. const SSL_METHOD *cmeth,
  1331. int min_version, int max_version)
  1332. {
  1333. size_t i;
  1334. SSL_CTX *cctx = NULL, *sctx = NULL;
  1335. SSL *clientssl = NULL, *serverssl = NULL;
  1336. int testresult = 0;
  1337. void *zbuf;
  1338. SSL_CONNECTION *serversc;
  1339. TLS_RECORD *rr;
  1340. static unsigned char cbuf[16000];
  1341. static unsigned char sbuf[16000];
  1342. if (!TEST_true(create_ssl_ctx_pair(libctx,
  1343. smeth, cmeth,
  1344. min_version, max_version,
  1345. &sctx, &cctx, cert,
  1346. privkey)))
  1347. goto end;
  1348. #ifdef OPENSSL_NO_DTLS1_2
  1349. if (smeth == DTLS_server_method()) {
  1350. # ifdef OPENSSL_NO_DTLS1_2
  1351. /* Not supported in the FIPS provider */
  1352. if (is_fips) {
  1353. testresult = 1;
  1354. goto end;
  1355. };
  1356. # endif
  1357. /*
  1358. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  1359. * level 0
  1360. */
  1361. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  1362. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  1363. "DEFAULT:@SECLEVEL=0")))
  1364. goto end;
  1365. }
  1366. #endif
  1367. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1368. NULL, NULL)))
  1369. goto end;
  1370. if (!TEST_true(SSL_set_options(serverssl, SSL_OP_CLEANSE_PLAINTEXT)))
  1371. goto end;
  1372. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1373. SSL_ERROR_NONE)))
  1374. goto end;
  1375. for (i = 0; i < sizeof(cbuf); i++) {
  1376. cbuf[i] = i & 0xff;
  1377. }
  1378. if (!TEST_int_eq(SSL_write(clientssl, cbuf, sizeof(cbuf)), sizeof(cbuf)))
  1379. goto end;
  1380. if (!TEST_int_eq(SSL_peek(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1381. goto end;
  1382. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(sbuf)))
  1383. goto end;
  1384. /*
  1385. * Since we called SSL_peek(), we know the data in the record
  1386. * layer is a plaintext record. We can gather the pointer to check
  1387. * for zeroization after SSL_read().
  1388. */
  1389. if (!TEST_ptr(serversc = SSL_CONNECTION_FROM_SSL_ONLY(serverssl)))
  1390. goto end;
  1391. rr = serversc->rlayer.tlsrecs;
  1392. zbuf = &rr->data[rr->off];
  1393. if (!TEST_int_eq(rr->length, sizeof(cbuf)))
  1394. goto end;
  1395. /*
  1396. * After SSL_peek() the plaintext must still be stored in the
  1397. * record.
  1398. */
  1399. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1400. goto end;
  1401. memset(sbuf, 0, sizeof(sbuf));
  1402. if (!TEST_int_eq(SSL_read(serverssl, &sbuf, sizeof(sbuf)), sizeof(sbuf)))
  1403. goto end;
  1404. if (!TEST_mem_eq(cbuf, sizeof(cbuf), sbuf, sizeof(cbuf)))
  1405. goto end;
  1406. /* Check if rbuf is cleansed */
  1407. memset(cbuf, 0, sizeof(cbuf));
  1408. if (!TEST_mem_eq(cbuf, sizeof(cbuf), zbuf, sizeof(cbuf)))
  1409. goto end;
  1410. testresult = 1;
  1411. end:
  1412. SSL_free(serverssl);
  1413. SSL_free(clientssl);
  1414. SSL_CTX_free(sctx);
  1415. SSL_CTX_free(cctx);
  1416. return testresult;
  1417. }
  1418. static int test_cleanse_plaintext(void)
  1419. {
  1420. #if !defined(OPENSSL_NO_TLS1_2)
  1421. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1422. TLS_client_method(),
  1423. TLS1_2_VERSION,
  1424. TLS1_2_VERSION)))
  1425. return 0;
  1426. #endif
  1427. #if !defined(OSSL_NO_USABLE_TLS1_3)
  1428. if (!TEST_true(execute_cleanse_plaintext(TLS_server_method(),
  1429. TLS_client_method(),
  1430. TLS1_3_VERSION,
  1431. TLS1_3_VERSION)))
  1432. return 0;
  1433. #endif
  1434. #if !defined(OPENSSL_NO_DTLS)
  1435. if (!TEST_true(execute_cleanse_plaintext(DTLS_server_method(),
  1436. DTLS_client_method(),
  1437. DTLS1_VERSION,
  1438. 0)))
  1439. return 0;
  1440. #endif
  1441. return 1;
  1442. }
  1443. #ifndef OPENSSL_NO_OCSP
  1444. static int ocsp_server_cb(SSL *s, void *arg)
  1445. {
  1446. int *argi = (int *)arg;
  1447. unsigned char *copy = NULL;
  1448. STACK_OF(OCSP_RESPID) *ids = NULL;
  1449. OCSP_RESPID *id = NULL;
  1450. if (*argi == 2) {
  1451. /* In this test we are expecting exactly 1 OCSP_RESPID */
  1452. SSL_get_tlsext_status_ids(s, &ids);
  1453. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  1454. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1455. id = sk_OCSP_RESPID_value(ids, 0);
  1456. if (id == NULL || !OCSP_RESPID_match_ex(id, ocspcert, libctx, NULL))
  1457. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1458. } else if (*argi != 1) {
  1459. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1460. }
  1461. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  1462. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1463. if (!TEST_true(SSL_set_tlsext_status_ocsp_resp(s, copy,
  1464. sizeof(orespder)))) {
  1465. OPENSSL_free(copy);
  1466. return SSL_TLSEXT_ERR_ALERT_FATAL;
  1467. }
  1468. ocsp_server_called = 1;
  1469. return SSL_TLSEXT_ERR_OK;
  1470. }
  1471. static int ocsp_client_cb(SSL *s, void *arg)
  1472. {
  1473. int *argi = (int *)arg;
  1474. const unsigned char *respderin;
  1475. size_t len;
  1476. if (*argi != 1 && *argi != 2)
  1477. return 0;
  1478. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  1479. if (!TEST_mem_eq(orespder, len, respderin, len))
  1480. return 0;
  1481. ocsp_client_called = 1;
  1482. return 1;
  1483. }
  1484. static int test_tlsext_status_type(void)
  1485. {
  1486. SSL_CTX *cctx = NULL, *sctx = NULL;
  1487. SSL *clientssl = NULL, *serverssl = NULL;
  1488. int testresult = 0;
  1489. STACK_OF(OCSP_RESPID) *ids = NULL;
  1490. OCSP_RESPID *id = NULL;
  1491. BIO *certbio = NULL;
  1492. if (!create_ssl_ctx_pair(libctx, TLS_server_method(), TLS_client_method(),
  1493. TLS1_VERSION, 0,
  1494. &sctx, &cctx, cert, privkey))
  1495. return 0;
  1496. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  1497. goto end;
  1498. /* First just do various checks getting and setting tlsext_status_type */
  1499. clientssl = SSL_new(cctx);
  1500. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  1501. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  1502. TLSEXT_STATUSTYPE_ocsp))
  1503. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  1504. TLSEXT_STATUSTYPE_ocsp))
  1505. goto end;
  1506. SSL_free(clientssl);
  1507. clientssl = NULL;
  1508. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  1509. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  1510. goto end;
  1511. clientssl = SSL_new(cctx);
  1512. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  1513. goto end;
  1514. SSL_free(clientssl);
  1515. clientssl = NULL;
  1516. /*
  1517. * Now actually do a handshake and check OCSP information is exchanged and
  1518. * the callbacks get called
  1519. */
  1520. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  1521. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  1522. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  1523. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  1524. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1525. &clientssl, NULL, NULL))
  1526. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  1527. SSL_ERROR_NONE))
  1528. || !TEST_true(ocsp_client_called)
  1529. || !TEST_true(ocsp_server_called))
  1530. goto end;
  1531. SSL_free(serverssl);
  1532. SSL_free(clientssl);
  1533. serverssl = NULL;
  1534. clientssl = NULL;
  1535. /* Try again but this time force the server side callback to fail */
  1536. ocsp_client_called = 0;
  1537. ocsp_server_called = 0;
  1538. cdummyarg = 0;
  1539. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1540. &clientssl, NULL, NULL))
  1541. /* This should fail because the callback will fail */
  1542. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  1543. SSL_ERROR_NONE))
  1544. || !TEST_false(ocsp_client_called)
  1545. || !TEST_false(ocsp_server_called))
  1546. goto end;
  1547. SSL_free(serverssl);
  1548. SSL_free(clientssl);
  1549. serverssl = NULL;
  1550. clientssl = NULL;
  1551. /*
  1552. * This time we'll get the client to send an OCSP_RESPID that it will
  1553. * accept.
  1554. */
  1555. ocsp_client_called = 0;
  1556. ocsp_server_called = 0;
  1557. cdummyarg = 2;
  1558. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1559. &clientssl, NULL, NULL)))
  1560. goto end;
  1561. /*
  1562. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  1563. * specific one. We'll use the server cert.
  1564. */
  1565. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  1566. || !TEST_ptr(id = OCSP_RESPID_new())
  1567. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  1568. || !TEST_ptr(ocspcert = X509_new_ex(libctx, NULL))
  1569. || !TEST_ptr(PEM_read_bio_X509(certbio, &ocspcert, NULL, NULL))
  1570. || !TEST_true(OCSP_RESPID_set_by_key_ex(id, ocspcert, libctx, NULL))
  1571. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  1572. goto end;
  1573. id = NULL;
  1574. SSL_set_tlsext_status_ids(clientssl, ids);
  1575. /* Control has been transferred */
  1576. ids = NULL;
  1577. BIO_free(certbio);
  1578. certbio = NULL;
  1579. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1580. SSL_ERROR_NONE))
  1581. || !TEST_true(ocsp_client_called)
  1582. || !TEST_true(ocsp_server_called))
  1583. goto end;
  1584. testresult = 1;
  1585. end:
  1586. SSL_free(serverssl);
  1587. SSL_free(clientssl);
  1588. SSL_CTX_free(sctx);
  1589. SSL_CTX_free(cctx);
  1590. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  1591. OCSP_RESPID_free(id);
  1592. BIO_free(certbio);
  1593. X509_free(ocspcert);
  1594. ocspcert = NULL;
  1595. return testresult;
  1596. }
  1597. #endif
  1598. #if !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  1599. static int new_called, remove_called, get_called;
  1600. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  1601. {
  1602. new_called++;
  1603. /*
  1604. * sess has been up-refed for us, but we don't actually need it so free it
  1605. * immediately.
  1606. */
  1607. SSL_SESSION_free(sess);
  1608. return 1;
  1609. }
  1610. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  1611. {
  1612. remove_called++;
  1613. }
  1614. static SSL_SESSION *get_sess_val = NULL;
  1615. static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
  1616. int *copy)
  1617. {
  1618. get_called++;
  1619. *copy = 1;
  1620. return get_sess_val;
  1621. }
  1622. static int execute_test_session(int maxprot, int use_int_cache,
  1623. int use_ext_cache, long s_options)
  1624. {
  1625. SSL_CTX *sctx = NULL, *cctx = NULL;
  1626. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  1627. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  1628. # ifndef OPENSSL_NO_TLS1_1
  1629. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  1630. # endif
  1631. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  1632. int testresult = 0, numnewsesstick = 1;
  1633. new_called = remove_called = 0;
  1634. /* TLSv1.3 sends 2 NewSessionTickets */
  1635. if (maxprot == TLS1_3_VERSION)
  1636. numnewsesstick = 2;
  1637. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1638. TLS_client_method(), TLS1_VERSION, 0,
  1639. &sctx, &cctx, cert, privkey)))
  1640. return 0;
  1641. /*
  1642. * Only allow the max protocol version so we can force a connection failure
  1643. * later
  1644. */
  1645. SSL_CTX_set_min_proto_version(cctx, maxprot);
  1646. SSL_CTX_set_max_proto_version(cctx, maxprot);
  1647. /* Set up session cache */
  1648. if (use_ext_cache) {
  1649. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  1650. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  1651. }
  1652. if (use_int_cache) {
  1653. /* Also covers instance where both are set */
  1654. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  1655. } else {
  1656. SSL_CTX_set_session_cache_mode(cctx,
  1657. SSL_SESS_CACHE_CLIENT
  1658. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1659. }
  1660. if (s_options) {
  1661. SSL_CTX_set_options(sctx, s_options);
  1662. }
  1663. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1664. NULL, NULL))
  1665. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1666. SSL_ERROR_NONE))
  1667. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  1668. goto end;
  1669. /* Should fail because it should already be in the cache */
  1670. if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  1671. goto end;
  1672. if (use_ext_cache
  1673. && (!TEST_int_eq(new_called, numnewsesstick)
  1674. || !TEST_int_eq(remove_called, 0)))
  1675. goto end;
  1676. new_called = remove_called = 0;
  1677. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1678. &clientssl2, NULL, NULL))
  1679. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1680. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1681. SSL_ERROR_NONE))
  1682. || !TEST_true(SSL_session_reused(clientssl2)))
  1683. goto end;
  1684. if (maxprot == TLS1_3_VERSION) {
  1685. /*
  1686. * In TLSv1.3 we should have created a new session even though we have
  1687. * resumed. Since we attempted a resume we should also have removed the
  1688. * old ticket from the cache so that we try to only use tickets once.
  1689. */
  1690. if (use_ext_cache
  1691. && (!TEST_int_eq(new_called, 1)
  1692. || !TEST_int_eq(remove_called, 1)))
  1693. goto end;
  1694. } else {
  1695. /*
  1696. * In TLSv1.2 we expect to have resumed so no sessions added or
  1697. * removed.
  1698. */
  1699. if (use_ext_cache
  1700. && (!TEST_int_eq(new_called, 0)
  1701. || !TEST_int_eq(remove_called, 0)))
  1702. goto end;
  1703. }
  1704. SSL_SESSION_free(sess1);
  1705. if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
  1706. goto end;
  1707. shutdown_ssl_connection(serverssl2, clientssl2);
  1708. serverssl2 = clientssl2 = NULL;
  1709. new_called = remove_called = 0;
  1710. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1711. &clientssl2, NULL, NULL))
  1712. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1713. SSL_ERROR_NONE)))
  1714. goto end;
  1715. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  1716. goto end;
  1717. if (use_ext_cache
  1718. && (!TEST_int_eq(new_called, numnewsesstick)
  1719. || !TEST_int_eq(remove_called, 0)))
  1720. goto end;
  1721. new_called = remove_called = 0;
  1722. /*
  1723. * This should clear sess2 from the cache because it is a "bad" session.
  1724. * See SSL_set_session() documentation.
  1725. */
  1726. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  1727. goto end;
  1728. if (use_ext_cache
  1729. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1730. goto end;
  1731. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  1732. goto end;
  1733. if (use_int_cache) {
  1734. /* Should succeeded because it should not already be in the cache */
  1735. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  1736. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  1737. goto end;
  1738. }
  1739. new_called = remove_called = 0;
  1740. /* This shouldn't be in the cache so should fail */
  1741. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  1742. goto end;
  1743. if (use_ext_cache
  1744. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1745. goto end;
  1746. # if !defined(OPENSSL_NO_TLS1_1)
  1747. new_called = remove_called = 0;
  1748. /* Force a connection failure */
  1749. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  1750. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  1751. &clientssl3, NULL, NULL))
  1752. || !TEST_true(SSL_set_session(clientssl3, sess1))
  1753. /* This should fail because of the mismatched protocol versions */
  1754. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  1755. SSL_ERROR_NONE)))
  1756. goto end;
  1757. /* We should have automatically removed the session from the cache */
  1758. if (use_ext_cache
  1759. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1760. goto end;
  1761. /* Should succeed because it should not already be in the cache */
  1762. if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
  1763. goto end;
  1764. # endif
  1765. /* Now do some tests for server side caching */
  1766. if (use_ext_cache) {
  1767. SSL_CTX_sess_set_new_cb(cctx, NULL);
  1768. SSL_CTX_sess_set_remove_cb(cctx, NULL);
  1769. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  1770. SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
  1771. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  1772. get_sess_val = NULL;
  1773. }
  1774. SSL_CTX_set_session_cache_mode(cctx, 0);
  1775. /* Internal caching is the default on the server side */
  1776. if (!use_int_cache)
  1777. SSL_CTX_set_session_cache_mode(sctx,
  1778. SSL_SESS_CACHE_SERVER
  1779. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1780. SSL_free(serverssl1);
  1781. SSL_free(clientssl1);
  1782. serverssl1 = clientssl1 = NULL;
  1783. SSL_free(serverssl2);
  1784. SSL_free(clientssl2);
  1785. serverssl2 = clientssl2 = NULL;
  1786. SSL_SESSION_free(sess1);
  1787. sess1 = NULL;
  1788. SSL_SESSION_free(sess2);
  1789. sess2 = NULL;
  1790. SSL_CTX_set_max_proto_version(sctx, maxprot);
  1791. if (maxprot == TLS1_2_VERSION)
  1792. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  1793. new_called = remove_called = get_called = 0;
  1794. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1795. NULL, NULL))
  1796. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1797. SSL_ERROR_NONE))
  1798. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
  1799. || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
  1800. goto end;
  1801. if (use_int_cache) {
  1802. if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
  1803. /*
  1804. * In TLSv1.3 it should not have been added to the internal cache,
  1805. * except in the case where we also have an external cache (in that
  1806. * case it gets added to the cache in order to generate remove
  1807. * events after timeout).
  1808. */
  1809. if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
  1810. goto end;
  1811. } else {
  1812. /* Should fail because it should already be in the cache */
  1813. if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
  1814. goto end;
  1815. }
  1816. }
  1817. if (use_ext_cache) {
  1818. SSL_SESSION *tmp = sess2;
  1819. if (!TEST_int_eq(new_called, numnewsesstick)
  1820. || !TEST_int_eq(remove_called, 0)
  1821. || !TEST_int_eq(get_called, 0))
  1822. goto end;
  1823. /*
  1824. * Delete the session from the internal cache to force a lookup from
  1825. * the external cache. We take a copy first because
  1826. * SSL_CTX_remove_session() also marks the session as non-resumable.
  1827. */
  1828. if (use_int_cache && maxprot != TLS1_3_VERSION) {
  1829. if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
  1830. || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
  1831. goto end;
  1832. SSL_SESSION_free(sess2);
  1833. }
  1834. sess2 = tmp;
  1835. }
  1836. new_called = remove_called = get_called = 0;
  1837. get_sess_val = sess2;
  1838. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1839. &clientssl2, NULL, NULL))
  1840. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1841. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1842. SSL_ERROR_NONE))
  1843. || !TEST_true(SSL_session_reused(clientssl2)))
  1844. goto end;
  1845. if (use_ext_cache) {
  1846. if (!TEST_int_eq(remove_called, 0))
  1847. goto end;
  1848. if (maxprot == TLS1_3_VERSION) {
  1849. if (!TEST_int_eq(new_called, 1)
  1850. || !TEST_int_eq(get_called, 0))
  1851. goto end;
  1852. } else {
  1853. if (!TEST_int_eq(new_called, 0)
  1854. || !TEST_int_eq(get_called, 1))
  1855. goto end;
  1856. }
  1857. }
  1858. /*
  1859. * Make a small cache, force out all other sessions but
  1860. * sess2, try to add sess1, which should succeed. Then
  1861. * make sure it's there by checking the owners. Despite
  1862. * the timeouts, sess1 should have kicked out sess2
  1863. */
  1864. /* Make sess1 expire before sess2 */
  1865. if (!TEST_long_gt(SSL_SESSION_set_time(sess1, 1000), 0)
  1866. || !TEST_long_gt(SSL_SESSION_set_timeout(sess1, 1000), 0)
  1867. || !TEST_long_gt(SSL_SESSION_set_time(sess2, 2000), 0)
  1868. || !TEST_long_gt(SSL_SESSION_set_timeout(sess2, 2000), 0))
  1869. goto end;
  1870. if (!TEST_long_ne(SSL_CTX_sess_set_cache_size(sctx, 1), 0))
  1871. goto end;
  1872. /* Don't care about results - cache should only be sess2 at end */
  1873. SSL_CTX_add_session(sctx, sess1);
  1874. SSL_CTX_add_session(sctx, sess2);
  1875. /* Now add sess1, and make sure it remains, despite timeout */
  1876. if (!TEST_true(SSL_CTX_add_session(sctx, sess1))
  1877. || !TEST_ptr(sess1->owner)
  1878. || !TEST_ptr_null(sess2->owner))
  1879. goto end;
  1880. testresult = 1;
  1881. end:
  1882. SSL_free(serverssl1);
  1883. SSL_free(clientssl1);
  1884. SSL_free(serverssl2);
  1885. SSL_free(clientssl2);
  1886. # ifndef OPENSSL_NO_TLS1_1
  1887. SSL_free(serverssl3);
  1888. SSL_free(clientssl3);
  1889. # endif
  1890. SSL_SESSION_free(sess1);
  1891. SSL_SESSION_free(sess2);
  1892. SSL_CTX_free(sctx);
  1893. SSL_CTX_free(cctx);
  1894. return testresult;
  1895. }
  1896. #endif /* !defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  1897. static int test_session_with_only_int_cache(void)
  1898. {
  1899. #ifndef OSSL_NO_USABLE_TLS1_3
  1900. if (!execute_test_session(TLS1_3_VERSION, 1, 0, 0))
  1901. return 0;
  1902. #endif
  1903. #ifndef OPENSSL_NO_TLS1_2
  1904. return execute_test_session(TLS1_2_VERSION, 1, 0, 0);
  1905. #else
  1906. return 1;
  1907. #endif
  1908. }
  1909. static int test_session_with_only_ext_cache(void)
  1910. {
  1911. #ifndef OSSL_NO_USABLE_TLS1_3
  1912. if (!execute_test_session(TLS1_3_VERSION, 0, 1, 0))
  1913. return 0;
  1914. #endif
  1915. #ifndef OPENSSL_NO_TLS1_2
  1916. return execute_test_session(TLS1_2_VERSION, 0, 1, 0);
  1917. #else
  1918. return 1;
  1919. #endif
  1920. }
  1921. static int test_session_with_both_cache(void)
  1922. {
  1923. #ifndef OSSL_NO_USABLE_TLS1_3
  1924. if (!execute_test_session(TLS1_3_VERSION, 1, 1, 0))
  1925. return 0;
  1926. #endif
  1927. #ifndef OPENSSL_NO_TLS1_2
  1928. return execute_test_session(TLS1_2_VERSION, 1, 1, 0);
  1929. #else
  1930. return 1;
  1931. #endif
  1932. }
  1933. static int test_session_wo_ca_names(void)
  1934. {
  1935. #ifndef OSSL_NO_USABLE_TLS1_3
  1936. if (!execute_test_session(TLS1_3_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES))
  1937. return 0;
  1938. #endif
  1939. #ifndef OPENSSL_NO_TLS1_2
  1940. return execute_test_session(TLS1_2_VERSION, 1, 0, SSL_OP_DISABLE_TLSEXT_CA_NAMES);
  1941. #else
  1942. return 1;
  1943. #endif
  1944. }
  1945. #ifndef OSSL_NO_USABLE_TLS1_3
  1946. static SSL_SESSION *sesscache[6];
  1947. static int do_cache;
  1948. static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
  1949. {
  1950. if (do_cache) {
  1951. sesscache[new_called] = sess;
  1952. } else {
  1953. /* We don't need the reference to the session, so free it */
  1954. SSL_SESSION_free(sess);
  1955. }
  1956. new_called++;
  1957. return 1;
  1958. }
  1959. static int post_handshake_verify(SSL *sssl, SSL *cssl)
  1960. {
  1961. SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
  1962. if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
  1963. return 0;
  1964. /* Start handshake on the server and client */
  1965. if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
  1966. || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
  1967. || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
  1968. || !TEST_true(create_ssl_connection(sssl, cssl,
  1969. SSL_ERROR_NONE)))
  1970. return 0;
  1971. return 1;
  1972. }
  1973. static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
  1974. SSL_CTX **cctx)
  1975. {
  1976. int sess_id_ctx = 1;
  1977. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  1978. TLS_client_method(), TLS1_VERSION, 0,
  1979. sctx, cctx, cert, privkey))
  1980. || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
  1981. || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
  1982. (void *)&sess_id_ctx,
  1983. sizeof(sess_id_ctx))))
  1984. return 0;
  1985. if (stateful)
  1986. SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
  1987. SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
  1988. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1989. SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
  1990. return 1;
  1991. }
  1992. static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
  1993. {
  1994. SSL *serverssl = NULL, *clientssl = NULL;
  1995. int i;
  1996. /* Test that we can resume with all the tickets we got given */
  1997. for (i = 0; i < idx * 2; i++) {
  1998. new_called = 0;
  1999. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2000. &clientssl, NULL, NULL))
  2001. || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
  2002. goto end;
  2003. SSL_set_post_handshake_auth(clientssl, 1);
  2004. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2005. SSL_ERROR_NONE)))
  2006. goto end;
  2007. /*
  2008. * Following a successful resumption we only get 1 ticket. After a
  2009. * failed one we should get idx tickets.
  2010. */
  2011. if (succ) {
  2012. if (!TEST_true(SSL_session_reused(clientssl))
  2013. || !TEST_int_eq(new_called, 1))
  2014. goto end;
  2015. } else {
  2016. if (!TEST_false(SSL_session_reused(clientssl))
  2017. || !TEST_int_eq(new_called, idx))
  2018. goto end;
  2019. }
  2020. new_called = 0;
  2021. /* After a post-handshake authentication we should get 1 new ticket */
  2022. if (succ
  2023. && (!post_handshake_verify(serverssl, clientssl)
  2024. || !TEST_int_eq(new_called, 1)))
  2025. goto end;
  2026. SSL_shutdown(clientssl);
  2027. SSL_shutdown(serverssl);
  2028. SSL_free(serverssl);
  2029. SSL_free(clientssl);
  2030. serverssl = clientssl = NULL;
  2031. SSL_SESSION_free(sesscache[i]);
  2032. sesscache[i] = NULL;
  2033. }
  2034. return 1;
  2035. end:
  2036. SSL_free(clientssl);
  2037. SSL_free(serverssl);
  2038. return 0;
  2039. }
  2040. static int test_tickets(int stateful, int idx)
  2041. {
  2042. SSL_CTX *sctx = NULL, *cctx = NULL;
  2043. SSL *serverssl = NULL, *clientssl = NULL;
  2044. int testresult = 0;
  2045. size_t j;
  2046. /* idx is the test number, but also the number of tickets we want */
  2047. new_called = 0;
  2048. do_cache = 1;
  2049. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2050. goto end;
  2051. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2052. &clientssl, NULL, NULL)))
  2053. goto end;
  2054. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2055. SSL_ERROR_NONE))
  2056. /* Check we got the number of tickets we were expecting */
  2057. || !TEST_int_eq(idx, new_called))
  2058. goto end;
  2059. SSL_shutdown(clientssl);
  2060. SSL_shutdown(serverssl);
  2061. SSL_free(serverssl);
  2062. SSL_free(clientssl);
  2063. SSL_CTX_free(sctx);
  2064. SSL_CTX_free(cctx);
  2065. clientssl = serverssl = NULL;
  2066. sctx = cctx = NULL;
  2067. /*
  2068. * Now we try to resume with the tickets we previously created. The
  2069. * resumption attempt is expected to fail (because we're now using a new
  2070. * SSL_CTX). We should see idx number of tickets issued again.
  2071. */
  2072. /* Stop caching sessions - just count them */
  2073. do_cache = 0;
  2074. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2075. goto end;
  2076. if (!check_resumption(idx, sctx, cctx, 0))
  2077. goto end;
  2078. /* Start again with caching sessions */
  2079. new_called = 0;
  2080. do_cache = 1;
  2081. SSL_CTX_free(sctx);
  2082. SSL_CTX_free(cctx);
  2083. sctx = cctx = NULL;
  2084. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  2085. goto end;
  2086. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2087. &clientssl, NULL, NULL)))
  2088. goto end;
  2089. SSL_set_post_handshake_auth(clientssl, 1);
  2090. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2091. SSL_ERROR_NONE))
  2092. /* Check we got the number of tickets we were expecting */
  2093. || !TEST_int_eq(idx, new_called))
  2094. goto end;
  2095. /* After a post-handshake authentication we should get new tickets issued */
  2096. if (!post_handshake_verify(serverssl, clientssl)
  2097. || !TEST_int_eq(idx * 2, new_called))
  2098. goto end;
  2099. SSL_shutdown(clientssl);
  2100. SSL_shutdown(serverssl);
  2101. SSL_free(serverssl);
  2102. SSL_free(clientssl);
  2103. serverssl = clientssl = NULL;
  2104. /* Stop caching sessions - just count them */
  2105. do_cache = 0;
  2106. /*
  2107. * Check we can resume with all the tickets we created. This time around the
  2108. * resumptions should all be successful.
  2109. */
  2110. if (!check_resumption(idx, sctx, cctx, 1))
  2111. goto end;
  2112. testresult = 1;
  2113. end:
  2114. SSL_free(serverssl);
  2115. SSL_free(clientssl);
  2116. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  2117. SSL_SESSION_free(sesscache[j]);
  2118. sesscache[j] = NULL;
  2119. }
  2120. SSL_CTX_free(sctx);
  2121. SSL_CTX_free(cctx);
  2122. return testresult;
  2123. }
  2124. static int test_stateless_tickets(int idx)
  2125. {
  2126. return test_tickets(0, idx);
  2127. }
  2128. static int test_stateful_tickets(int idx)
  2129. {
  2130. return test_tickets(1, idx);
  2131. }
  2132. static int test_psk_tickets(void)
  2133. {
  2134. SSL_CTX *sctx = NULL, *cctx = NULL;
  2135. SSL *serverssl = NULL, *clientssl = NULL;
  2136. int testresult = 0;
  2137. int sess_id_ctx = 1;
  2138. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2139. TLS_client_method(), TLS1_VERSION, 0,
  2140. &sctx, &cctx, NULL, NULL))
  2141. || !TEST_true(SSL_CTX_set_session_id_context(sctx,
  2142. (void *)&sess_id_ctx,
  2143. sizeof(sess_id_ctx))))
  2144. goto end;
  2145. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  2146. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  2147. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  2148. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  2149. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2150. use_session_cb_cnt = 0;
  2151. find_session_cb_cnt = 0;
  2152. srvid = pskid;
  2153. new_called = 0;
  2154. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2155. NULL, NULL)))
  2156. goto end;
  2157. clientpsk = serverpsk = create_a_psk(clientssl);
  2158. if (!TEST_ptr(clientpsk))
  2159. goto end;
  2160. SSL_SESSION_up_ref(clientpsk);
  2161. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2162. SSL_ERROR_NONE))
  2163. || !TEST_int_eq(1, find_session_cb_cnt)
  2164. || !TEST_int_eq(1, use_session_cb_cnt)
  2165. /* We should always get 1 ticket when using external PSK */
  2166. || !TEST_int_eq(1, new_called))
  2167. goto end;
  2168. testresult = 1;
  2169. end:
  2170. SSL_free(serverssl);
  2171. SSL_free(clientssl);
  2172. SSL_CTX_free(sctx);
  2173. SSL_CTX_free(cctx);
  2174. SSL_SESSION_free(clientpsk);
  2175. SSL_SESSION_free(serverpsk);
  2176. clientpsk = serverpsk = NULL;
  2177. return testresult;
  2178. }
  2179. static int test_extra_tickets(int idx)
  2180. {
  2181. SSL_CTX *sctx = NULL, *cctx = NULL;
  2182. SSL *serverssl = NULL, *clientssl = NULL;
  2183. BIO *bretry = BIO_new(bio_s_always_retry());
  2184. BIO *tmp = NULL;
  2185. int testresult = 0;
  2186. int stateful = 0;
  2187. size_t nbytes;
  2188. unsigned char c, buf[1];
  2189. new_called = 0;
  2190. do_cache = 1;
  2191. if (idx >= 3) {
  2192. idx -= 3;
  2193. stateful = 1;
  2194. }
  2195. if (!TEST_ptr(bretry) || !setup_ticket_test(stateful, idx, &sctx, &cctx))
  2196. goto end;
  2197. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  2198. /* setup_ticket_test() uses new_cachesession_cb which we don't need. */
  2199. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  2200. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2201. &clientssl, NULL, NULL)))
  2202. goto end;
  2203. /*
  2204. * Note that we have new_session_cb on both sctx and cctx, so new_called is
  2205. * incremented by both client and server.
  2206. */
  2207. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  2208. SSL_ERROR_NONE))
  2209. /* Check we got the number of tickets we were expecting */
  2210. || !TEST_int_eq(idx * 2, new_called)
  2211. || !TEST_true(SSL_new_session_ticket(serverssl))
  2212. || !TEST_true(SSL_new_session_ticket(serverssl))
  2213. || !TEST_int_eq(idx * 2, new_called))
  2214. goto end;
  2215. /* Now try a (real) write to actually send the tickets */
  2216. c = '1';
  2217. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2218. || !TEST_size_t_eq(1, nbytes)
  2219. || !TEST_int_eq(idx * 2 + 2, new_called)
  2220. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2221. || !TEST_int_eq(idx * 2 + 4, new_called)
  2222. || !TEST_int_eq(sizeof(buf), nbytes)
  2223. || !TEST_int_eq(c, buf[0])
  2224. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2225. goto end;
  2226. /* Try with only requesting one new ticket, too */
  2227. c = '2';
  2228. new_called = 0;
  2229. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2230. || !TEST_true(SSL_write_ex(serverssl, &c, sizeof(c), &nbytes))
  2231. || !TEST_size_t_eq(sizeof(c), nbytes)
  2232. || !TEST_int_eq(1, new_called)
  2233. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2234. || !TEST_int_eq(2, new_called)
  2235. || !TEST_size_t_eq(sizeof(buf), nbytes)
  2236. || !TEST_int_eq(c, buf[0]))
  2237. goto end;
  2238. /* Do it again but use dummy writes to drive the ticket generation */
  2239. c = '3';
  2240. new_called = 0;
  2241. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2242. || !TEST_true(SSL_new_session_ticket(serverssl))
  2243. || !TEST_true(SSL_write_ex(serverssl, &c, 0, &nbytes))
  2244. || !TEST_size_t_eq(0, nbytes)
  2245. || !TEST_int_eq(2, new_called)
  2246. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2247. || !TEST_int_eq(4, new_called))
  2248. goto end;
  2249. /* Once more, but with SSL_do_handshake() to drive the ticket generation */
  2250. c = '4';
  2251. new_called = 0;
  2252. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2253. || !TEST_true(SSL_new_session_ticket(serverssl))
  2254. || !TEST_true(SSL_do_handshake(serverssl))
  2255. || !TEST_int_eq(2, new_called)
  2256. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2257. || !TEST_int_eq(4, new_called))
  2258. goto end;
  2259. /*
  2260. * Use the always-retry BIO to exercise the logic that forces ticket
  2261. * generation to wait until a record boundary.
  2262. */
  2263. c = '5';
  2264. new_called = 0;
  2265. tmp = SSL_get_wbio(serverssl);
  2266. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  2267. tmp = NULL;
  2268. goto end;
  2269. }
  2270. SSL_set0_wbio(serverssl, bretry);
  2271. bretry = NULL;
  2272. if (!TEST_false(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2273. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_WANT_WRITE)
  2274. || !TEST_size_t_eq(nbytes, 0))
  2275. goto end;
  2276. /* Restore a BIO that will let the write succeed */
  2277. SSL_set0_wbio(serverssl, tmp);
  2278. tmp = NULL;
  2279. /*
  2280. * These calls should just queue the request and not send anything
  2281. * even if we explicitly try to hit the state machine.
  2282. */
  2283. if (!TEST_true(SSL_new_session_ticket(serverssl))
  2284. || !TEST_true(SSL_new_session_ticket(serverssl))
  2285. || !TEST_int_eq(0, new_called)
  2286. || !TEST_true(SSL_do_handshake(serverssl))
  2287. || !TEST_int_eq(0, new_called))
  2288. goto end;
  2289. /* Re-do the write; still no tickets sent */
  2290. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2291. || !TEST_size_t_eq(1, nbytes)
  2292. || !TEST_int_eq(0, new_called)
  2293. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2294. || !TEST_int_eq(0, new_called)
  2295. || !TEST_int_eq(sizeof(buf), nbytes)
  2296. || !TEST_int_eq(c, buf[0])
  2297. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2298. goto end;
  2299. /* Even trying to hit the state machine now will still not send tickets */
  2300. if (!TEST_true(SSL_do_handshake(serverssl))
  2301. || !TEST_int_eq(0, new_called))
  2302. goto end;
  2303. /* Now the *next* write should send the tickets */
  2304. c = '6';
  2305. if (!TEST_true(SSL_write_ex(serverssl, &c, 1, &nbytes))
  2306. || !TEST_size_t_eq(1, nbytes)
  2307. || !TEST_int_eq(2, new_called)
  2308. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes))
  2309. || !TEST_int_eq(4, new_called)
  2310. || !TEST_int_eq(sizeof(buf), nbytes)
  2311. || !TEST_int_eq(c, buf[0])
  2312. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &nbytes)))
  2313. goto end;
  2314. SSL_shutdown(clientssl);
  2315. SSL_shutdown(serverssl);
  2316. testresult = 1;
  2317. end:
  2318. BIO_free(bretry);
  2319. BIO_free(tmp);
  2320. SSL_free(serverssl);
  2321. SSL_free(clientssl);
  2322. SSL_CTX_free(sctx);
  2323. SSL_CTX_free(cctx);
  2324. clientssl = serverssl = NULL;
  2325. sctx = cctx = NULL;
  2326. return testresult;
  2327. }
  2328. #endif
  2329. #define USE_NULL 0
  2330. #define USE_BIO_1 1
  2331. #define USE_BIO_2 2
  2332. #define USE_DEFAULT 3
  2333. #define CONNTYPE_CONNECTION_SUCCESS 0
  2334. #define CONNTYPE_CONNECTION_FAIL 1
  2335. #define CONNTYPE_NO_CONNECTION 2
  2336. #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  2337. #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
  2338. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
  2339. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
  2340. #else
  2341. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
  2342. #endif
  2343. #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
  2344. + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
  2345. + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
  2346. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  2347. {
  2348. switch (type) {
  2349. case USE_NULL:
  2350. *res = NULL;
  2351. break;
  2352. case USE_BIO_1:
  2353. *res = bio1;
  2354. break;
  2355. case USE_BIO_2:
  2356. *res = bio2;
  2357. break;
  2358. }
  2359. }
  2360. /*
  2361. * Tests calls to SSL_set_bio() under various conditions.
  2362. *
  2363. * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
  2364. * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
  2365. * then do more tests where we create a successful connection first using our
  2366. * standard connection setup functions, and then call SSL_set_bio() with
  2367. * various combinations of valid BIOs or NULL. We then repeat these tests
  2368. * following a failed connection. In this last case we are looking to check that
  2369. * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
  2370. */
  2371. static int test_ssl_set_bio(int idx)
  2372. {
  2373. SSL_CTX *sctx = NULL, *cctx = NULL;
  2374. BIO *bio1 = NULL;
  2375. BIO *bio2 = NULL;
  2376. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  2377. SSL *serverssl = NULL, *clientssl = NULL;
  2378. int initrbio, initwbio, newrbio, newwbio, conntype;
  2379. int testresult = 0;
  2380. if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
  2381. initrbio = idx % 3;
  2382. idx /= 3;
  2383. initwbio = idx % 3;
  2384. idx /= 3;
  2385. newrbio = idx % 3;
  2386. idx /= 3;
  2387. newwbio = idx % 3;
  2388. conntype = CONNTYPE_NO_CONNECTION;
  2389. } else {
  2390. idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
  2391. initrbio = initwbio = USE_DEFAULT;
  2392. newrbio = idx % 2;
  2393. idx /= 2;
  2394. newwbio = idx % 2;
  2395. idx /= 2;
  2396. conntype = idx % 2;
  2397. }
  2398. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2399. TLS_client_method(), TLS1_VERSION, 0,
  2400. &sctx, &cctx, cert, privkey)))
  2401. goto end;
  2402. if (conntype == CONNTYPE_CONNECTION_FAIL) {
  2403. /*
  2404. * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
  2405. * because we reduced the number of tests in the definition of
  2406. * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
  2407. * mismatched protocol versions we will force a connection failure.
  2408. */
  2409. SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
  2410. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2411. }
  2412. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2413. NULL, NULL)))
  2414. goto end;
  2415. if (initrbio == USE_BIO_1
  2416. || initwbio == USE_BIO_1
  2417. || newrbio == USE_BIO_1
  2418. || newwbio == USE_BIO_1) {
  2419. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  2420. goto end;
  2421. }
  2422. if (initrbio == USE_BIO_2
  2423. || initwbio == USE_BIO_2
  2424. || newrbio == USE_BIO_2
  2425. || newwbio == USE_BIO_2) {
  2426. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  2427. goto end;
  2428. }
  2429. if (initrbio != USE_DEFAULT) {
  2430. setupbio(&irbio, bio1, bio2, initrbio);
  2431. setupbio(&iwbio, bio1, bio2, initwbio);
  2432. SSL_set_bio(clientssl, irbio, iwbio);
  2433. /*
  2434. * We want to maintain our own refs to these BIO, so do an up ref for
  2435. * each BIO that will have ownership transferred in the SSL_set_bio()
  2436. * call
  2437. */
  2438. if (irbio != NULL)
  2439. BIO_up_ref(irbio);
  2440. if (iwbio != NULL && iwbio != irbio)
  2441. BIO_up_ref(iwbio);
  2442. }
  2443. if (conntype != CONNTYPE_NO_CONNECTION
  2444. && !TEST_true(create_ssl_connection(serverssl, clientssl,
  2445. SSL_ERROR_NONE)
  2446. == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
  2447. goto end;
  2448. setupbio(&nrbio, bio1, bio2, newrbio);
  2449. setupbio(&nwbio, bio1, bio2, newwbio);
  2450. /*
  2451. * We will (maybe) transfer ownership again so do more up refs.
  2452. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  2453. * already been set!
  2454. */
  2455. if (nrbio != NULL
  2456. && nrbio != irbio
  2457. && (nwbio != iwbio || nrbio != nwbio))
  2458. BIO_up_ref(nrbio);
  2459. if (nwbio != NULL
  2460. && nwbio != nrbio
  2461. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  2462. BIO_up_ref(nwbio);
  2463. SSL_set_bio(clientssl, nrbio, nwbio);
  2464. testresult = 1;
  2465. end:
  2466. BIO_free(bio1);
  2467. BIO_free(bio2);
  2468. /*
  2469. * This test is checking that the ref counting for SSL_set_bio is correct.
  2470. * If we get here and we did too many frees then we will fail in the above
  2471. * functions.
  2472. */
  2473. SSL_free(serverssl);
  2474. SSL_free(clientssl);
  2475. SSL_CTX_free(sctx);
  2476. SSL_CTX_free(cctx);
  2477. return testresult;
  2478. }
  2479. typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
  2480. static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
  2481. {
  2482. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  2483. SSL_CTX *ctx;
  2484. SSL *ssl = NULL;
  2485. int testresult = 0;
  2486. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  2487. || !TEST_ptr(ssl = SSL_new(ctx))
  2488. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  2489. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  2490. goto end;
  2491. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  2492. /*
  2493. * If anything goes wrong here then we could leak memory.
  2494. */
  2495. BIO_push(sslbio, membio1);
  2496. /* Verify changing the rbio/wbio directly does not cause leaks */
  2497. if (change_bio != NO_BIO_CHANGE) {
  2498. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
  2499. ssl = NULL;
  2500. goto end;
  2501. }
  2502. if (change_bio == CHANGE_RBIO)
  2503. SSL_set0_rbio(ssl, membio2);
  2504. else
  2505. SSL_set0_wbio(ssl, membio2);
  2506. }
  2507. ssl = NULL;
  2508. if (pop_ssl)
  2509. BIO_pop(sslbio);
  2510. else
  2511. BIO_pop(membio1);
  2512. testresult = 1;
  2513. end:
  2514. BIO_free(membio1);
  2515. BIO_free(sslbio);
  2516. SSL_free(ssl);
  2517. SSL_CTX_free(ctx);
  2518. return testresult;
  2519. }
  2520. static int test_ssl_bio_pop_next_bio(void)
  2521. {
  2522. return execute_test_ssl_bio(0, NO_BIO_CHANGE);
  2523. }
  2524. static int test_ssl_bio_pop_ssl_bio(void)
  2525. {
  2526. return execute_test_ssl_bio(1, NO_BIO_CHANGE);
  2527. }
  2528. static int test_ssl_bio_change_rbio(void)
  2529. {
  2530. return execute_test_ssl_bio(0, CHANGE_RBIO);
  2531. }
  2532. static int test_ssl_bio_change_wbio(void)
  2533. {
  2534. return execute_test_ssl_bio(0, CHANGE_WBIO);
  2535. }
  2536. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  2537. typedef struct {
  2538. /* The list of sig algs */
  2539. const int *list;
  2540. /* The length of the list */
  2541. size_t listlen;
  2542. /* A sigalgs list in string format */
  2543. const char *liststr;
  2544. /* Whether setting the list should succeed */
  2545. int valid;
  2546. /* Whether creating a connection with the list should succeed */
  2547. int connsuccess;
  2548. } sigalgs_list;
  2549. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  2550. # ifndef OPENSSL_NO_EC
  2551. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  2552. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  2553. # endif
  2554. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  2555. static const int invalidlist2[] = {NID_sha256, NID_undef};
  2556. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  2557. static const int invalidlist4[] = {NID_sha256};
  2558. static const sigalgs_list testsigalgs[] = {
  2559. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  2560. # ifndef OPENSSL_NO_EC
  2561. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  2562. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  2563. # endif
  2564. {NULL, 0, "RSA+SHA256", 1, 1},
  2565. # ifndef OPENSSL_NO_EC
  2566. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  2567. {NULL, 0, "ECDSA+SHA512", 1, 0},
  2568. # endif
  2569. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  2570. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  2571. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  2572. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  2573. {NULL, 0, "RSA", 0, 0},
  2574. {NULL, 0, "SHA256", 0, 0},
  2575. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  2576. {NULL, 0, "Invalid", 0, 0}
  2577. };
  2578. static int test_set_sigalgs(int idx)
  2579. {
  2580. SSL_CTX *cctx = NULL, *sctx = NULL;
  2581. SSL *clientssl = NULL, *serverssl = NULL;
  2582. int testresult = 0;
  2583. const sigalgs_list *curr;
  2584. int testctx;
  2585. /* Should never happen */
  2586. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  2587. return 0;
  2588. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  2589. curr = testctx ? &testsigalgs[idx]
  2590. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  2591. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2592. TLS_client_method(), TLS1_VERSION, 0,
  2593. &sctx, &cctx, cert, privkey)))
  2594. return 0;
  2595. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  2596. if (testctx) {
  2597. int ret;
  2598. if (curr->list != NULL)
  2599. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  2600. else
  2601. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  2602. if (!ret) {
  2603. if (curr->valid)
  2604. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  2605. else
  2606. testresult = 1;
  2607. goto end;
  2608. }
  2609. if (!curr->valid) {
  2610. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  2611. goto end;
  2612. }
  2613. }
  2614. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2615. &clientssl, NULL, NULL)))
  2616. goto end;
  2617. if (!testctx) {
  2618. int ret;
  2619. if (curr->list != NULL)
  2620. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  2621. else
  2622. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  2623. if (!ret) {
  2624. if (curr->valid)
  2625. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  2626. else
  2627. testresult = 1;
  2628. goto end;
  2629. }
  2630. if (!curr->valid)
  2631. goto end;
  2632. }
  2633. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  2634. SSL_ERROR_NONE),
  2635. curr->connsuccess))
  2636. goto end;
  2637. testresult = 1;
  2638. end:
  2639. SSL_free(serverssl);
  2640. SSL_free(clientssl);
  2641. SSL_CTX_free(sctx);
  2642. SSL_CTX_free(cctx);
  2643. return testresult;
  2644. }
  2645. #endif
  2646. #ifndef OSSL_NO_USABLE_TLS1_3
  2647. static int psk_client_cb_cnt = 0;
  2648. static int psk_server_cb_cnt = 0;
  2649. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  2650. size_t *idlen, SSL_SESSION **sess)
  2651. {
  2652. switch (++use_session_cb_cnt) {
  2653. case 1:
  2654. /* The first call should always have a NULL md */
  2655. if (md != NULL)
  2656. return 0;
  2657. break;
  2658. case 2:
  2659. /* The second call should always have an md */
  2660. if (md == NULL)
  2661. return 0;
  2662. break;
  2663. default:
  2664. /* We should only be called a maximum of twice */
  2665. return 0;
  2666. }
  2667. if (clientpsk != NULL)
  2668. SSL_SESSION_up_ref(clientpsk);
  2669. *sess = clientpsk;
  2670. *id = (const unsigned char *)pskid;
  2671. *idlen = strlen(pskid);
  2672. return 1;
  2673. }
  2674. #ifndef OPENSSL_NO_PSK
  2675. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
  2676. unsigned int max_id_len,
  2677. unsigned char *psk,
  2678. unsigned int max_psk_len)
  2679. {
  2680. unsigned int psklen = 0;
  2681. psk_client_cb_cnt++;
  2682. if (strlen(pskid) + 1 > max_id_len)
  2683. return 0;
  2684. /* We should only ever be called a maximum of twice per connection */
  2685. if (psk_client_cb_cnt > 2)
  2686. return 0;
  2687. if (clientpsk == NULL)
  2688. return 0;
  2689. /* We'll reuse the PSK we set up for TLSv1.3 */
  2690. if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
  2691. return 0;
  2692. psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
  2693. strncpy(id, pskid, max_id_len);
  2694. return psklen;
  2695. }
  2696. #endif /* OPENSSL_NO_PSK */
  2697. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  2698. size_t identity_len, SSL_SESSION **sess)
  2699. {
  2700. find_session_cb_cnt++;
  2701. /* We should only ever be called a maximum of twice per connection */
  2702. if (find_session_cb_cnt > 2)
  2703. return 0;
  2704. if (serverpsk == NULL)
  2705. return 0;
  2706. /* Identity should match that set by the client */
  2707. if (strlen(srvid) != identity_len
  2708. || strncmp(srvid, (const char *)identity, identity_len) != 0) {
  2709. /* No PSK found, continue but without a PSK */
  2710. *sess = NULL;
  2711. return 1;
  2712. }
  2713. SSL_SESSION_up_ref(serverpsk);
  2714. *sess = serverpsk;
  2715. return 1;
  2716. }
  2717. #ifndef OPENSSL_NO_PSK
  2718. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  2719. unsigned char *psk, unsigned int max_psk_len)
  2720. {
  2721. unsigned int psklen = 0;
  2722. psk_server_cb_cnt++;
  2723. /* We should only ever be called a maximum of twice per connection */
  2724. if (find_session_cb_cnt > 2)
  2725. return 0;
  2726. if (serverpsk == NULL)
  2727. return 0;
  2728. /* Identity should match that set by the client */
  2729. if (strcmp(srvid, identity) != 0) {
  2730. return 0;
  2731. }
  2732. /* We'll reuse the PSK we set up for TLSv1.3 */
  2733. if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
  2734. return 0;
  2735. psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
  2736. return psklen;
  2737. }
  2738. #endif /* OPENSSL_NO_PSK */
  2739. #define MSG1 "Hello"
  2740. #define MSG2 "World."
  2741. #define MSG3 "This"
  2742. #define MSG4 "is"
  2743. #define MSG5 "a"
  2744. #define MSG6 "test"
  2745. #define MSG7 "message."
  2746. #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
  2747. #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
  2748. #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
  2749. #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
  2750. #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
  2751. static SSL_SESSION *create_a_psk(SSL *ssl)
  2752. {
  2753. const SSL_CIPHER *cipher = NULL;
  2754. const unsigned char key[] = {
  2755. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  2756. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  2757. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  2758. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  2759. 0x2c, 0x2d, 0x2e, 0x2f
  2760. };
  2761. SSL_SESSION *sess = NULL;
  2762. cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
  2763. sess = SSL_SESSION_new();
  2764. if (!TEST_ptr(sess)
  2765. || !TEST_ptr(cipher)
  2766. || !TEST_true(SSL_SESSION_set1_master_key(sess, key,
  2767. sizeof(key)))
  2768. || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
  2769. || !TEST_true(
  2770. SSL_SESSION_set_protocol_version(sess,
  2771. TLS1_3_VERSION))) {
  2772. SSL_SESSION_free(sess);
  2773. return NULL;
  2774. }
  2775. return sess;
  2776. }
  2777. /*
  2778. * Helper method to setup objects for early data test. Caller frees objects on
  2779. * error.
  2780. */
  2781. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  2782. SSL **serverssl, SSL_SESSION **sess, int idx)
  2783. {
  2784. if (*sctx == NULL
  2785. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  2786. TLS_client_method(),
  2787. TLS1_VERSION, 0,
  2788. sctx, cctx, cert, privkey)))
  2789. return 0;
  2790. if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
  2791. return 0;
  2792. if (idx == 1) {
  2793. /* When idx == 1 we repeat the tests with read_ahead set */
  2794. SSL_CTX_set_read_ahead(*cctx, 1);
  2795. SSL_CTX_set_read_ahead(*sctx, 1);
  2796. } else if (idx == 2) {
  2797. /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
  2798. SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
  2799. SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
  2800. use_session_cb_cnt = 0;
  2801. find_session_cb_cnt = 0;
  2802. srvid = pskid;
  2803. }
  2804. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  2805. NULL, NULL)))
  2806. return 0;
  2807. /*
  2808. * For one of the run throughs (doesn't matter which one), we'll try sending
  2809. * some SNI data in the initial ClientHello. This will be ignored (because
  2810. * there is no SNI cb set up by the server), so it should not impact
  2811. * early_data.
  2812. */
  2813. if (idx == 1
  2814. && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
  2815. return 0;
  2816. if (idx == 2) {
  2817. clientpsk = create_a_psk(*clientssl);
  2818. if (!TEST_ptr(clientpsk)
  2819. /*
  2820. * We just choose an arbitrary value for max_early_data which
  2821. * should be big enough for testing purposes.
  2822. */
  2823. || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
  2824. 0x100))
  2825. || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2826. SSL_SESSION_free(clientpsk);
  2827. clientpsk = NULL;
  2828. return 0;
  2829. }
  2830. serverpsk = clientpsk;
  2831. if (sess != NULL) {
  2832. if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  2833. SSL_SESSION_free(clientpsk);
  2834. SSL_SESSION_free(serverpsk);
  2835. clientpsk = serverpsk = NULL;
  2836. return 0;
  2837. }
  2838. *sess = clientpsk;
  2839. }
  2840. return 1;
  2841. }
  2842. if (sess == NULL)
  2843. return 1;
  2844. if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
  2845. SSL_ERROR_NONE)))
  2846. return 0;
  2847. *sess = SSL_get1_session(*clientssl);
  2848. SSL_shutdown(*clientssl);
  2849. SSL_shutdown(*serverssl);
  2850. SSL_free(*serverssl);
  2851. SSL_free(*clientssl);
  2852. *serverssl = *clientssl = NULL;
  2853. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  2854. clientssl, NULL, NULL))
  2855. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  2856. return 0;
  2857. return 1;
  2858. }
  2859. static int test_early_data_read_write(int idx)
  2860. {
  2861. SSL_CTX *cctx = NULL, *sctx = NULL;
  2862. SSL *clientssl = NULL, *serverssl = NULL;
  2863. int testresult = 0;
  2864. SSL_SESSION *sess = NULL;
  2865. unsigned char buf[20], data[1024];
  2866. size_t readbytes, written, eoedlen, rawread, rawwritten;
  2867. BIO *rbio;
  2868. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2869. &serverssl, &sess, idx)))
  2870. goto end;
  2871. /* Write and read some early data */
  2872. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2873. &written))
  2874. || !TEST_size_t_eq(written, strlen(MSG1))
  2875. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  2876. sizeof(buf), &readbytes),
  2877. SSL_READ_EARLY_DATA_SUCCESS)
  2878. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  2879. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2880. SSL_EARLY_DATA_ACCEPTED))
  2881. goto end;
  2882. /*
  2883. * Server should be able to write data, and client should be able to
  2884. * read it.
  2885. */
  2886. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  2887. &written))
  2888. || !TEST_size_t_eq(written, strlen(MSG2))
  2889. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2890. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2891. goto end;
  2892. /* Even after reading normal data, client should be able write early data */
  2893. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  2894. &written))
  2895. || !TEST_size_t_eq(written, strlen(MSG3)))
  2896. goto end;
  2897. /* Server should still be able read early data after writing data */
  2898. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2899. &readbytes),
  2900. SSL_READ_EARLY_DATA_SUCCESS)
  2901. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  2902. goto end;
  2903. /* Write more data from server and read it from client */
  2904. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  2905. &written))
  2906. || !TEST_size_t_eq(written, strlen(MSG4))
  2907. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2908. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  2909. goto end;
  2910. /*
  2911. * If client writes normal data it should mean writing early data is no
  2912. * longer possible.
  2913. */
  2914. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  2915. || !TEST_size_t_eq(written, strlen(MSG5))
  2916. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2917. SSL_EARLY_DATA_ACCEPTED))
  2918. goto end;
  2919. /*
  2920. * At this point the client has written EndOfEarlyData, ClientFinished and
  2921. * normal (fully protected) data. We are going to cause a delay between the
  2922. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  2923. * in the read BIO, and then just put back the EndOfEarlyData message.
  2924. */
  2925. rbio = SSL_get_rbio(serverssl);
  2926. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  2927. || !TEST_size_t_lt(rawread, sizeof(data))
  2928. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  2929. goto end;
  2930. /* Record length is in the 4th and 5th bytes of the record header */
  2931. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  2932. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  2933. || !TEST_size_t_eq(rawwritten, eoedlen))
  2934. goto end;
  2935. /* Server should be told that there is no more early data */
  2936. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2937. &readbytes),
  2938. SSL_READ_EARLY_DATA_FINISH)
  2939. || !TEST_size_t_eq(readbytes, 0))
  2940. goto end;
  2941. /*
  2942. * Server has not finished init yet, so should still be able to write early
  2943. * data.
  2944. */
  2945. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  2946. &written))
  2947. || !TEST_size_t_eq(written, strlen(MSG6)))
  2948. goto end;
  2949. /* Push the ClientFinished and the normal data back into the server rbio */
  2950. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  2951. &rawwritten))
  2952. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  2953. goto end;
  2954. /* Server should be able to read normal data */
  2955. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2956. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  2957. goto end;
  2958. /* Client and server should not be able to write/read early data now */
  2959. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  2960. &written)))
  2961. goto end;
  2962. ERR_clear_error();
  2963. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2964. &readbytes),
  2965. SSL_READ_EARLY_DATA_ERROR))
  2966. goto end;
  2967. ERR_clear_error();
  2968. /* Client should be able to read the data sent by the server */
  2969. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2970. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  2971. goto end;
  2972. /*
  2973. * Make sure we process the two NewSessionTickets. These arrive
  2974. * post-handshake. We attempt reads which we do not expect to return any
  2975. * data.
  2976. */
  2977. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2978. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
  2979. &readbytes)))
  2980. goto end;
  2981. /* Server should be able to write normal data */
  2982. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  2983. || !TEST_size_t_eq(written, strlen(MSG7))
  2984. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2985. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  2986. goto end;
  2987. SSL_SESSION_free(sess);
  2988. sess = SSL_get1_session(clientssl);
  2989. use_session_cb_cnt = 0;
  2990. find_session_cb_cnt = 0;
  2991. SSL_shutdown(clientssl);
  2992. SSL_shutdown(serverssl);
  2993. SSL_free(serverssl);
  2994. SSL_free(clientssl);
  2995. serverssl = clientssl = NULL;
  2996. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2997. &clientssl, NULL, NULL))
  2998. || !TEST_true(SSL_set_session(clientssl, sess)))
  2999. goto end;
  3000. /* Write and read some early data */
  3001. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3002. &written))
  3003. || !TEST_size_t_eq(written, strlen(MSG1))
  3004. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3005. &readbytes),
  3006. SSL_READ_EARLY_DATA_SUCCESS)
  3007. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3008. goto end;
  3009. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  3010. || !TEST_int_gt(SSL_accept(serverssl), 0))
  3011. goto end;
  3012. /* Client and server should not be able to write/read early data now */
  3013. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  3014. &written)))
  3015. goto end;
  3016. ERR_clear_error();
  3017. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3018. &readbytes),
  3019. SSL_READ_EARLY_DATA_ERROR))
  3020. goto end;
  3021. ERR_clear_error();
  3022. /* Client and server should be able to write/read normal data */
  3023. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  3024. || !TEST_size_t_eq(written, strlen(MSG5))
  3025. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3026. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  3027. goto end;
  3028. testresult = 1;
  3029. end:
  3030. SSL_SESSION_free(sess);
  3031. SSL_SESSION_free(clientpsk);
  3032. SSL_SESSION_free(serverpsk);
  3033. clientpsk = serverpsk = NULL;
  3034. SSL_free(serverssl);
  3035. SSL_free(clientssl);
  3036. SSL_CTX_free(sctx);
  3037. SSL_CTX_free(cctx);
  3038. return testresult;
  3039. }
  3040. static int allow_ed_cb_called = 0;
  3041. static int allow_early_data_cb(SSL *s, void *arg)
  3042. {
  3043. int *usecb = (int *)arg;
  3044. allow_ed_cb_called++;
  3045. if (*usecb == 1)
  3046. return 0;
  3047. return 1;
  3048. }
  3049. /*
  3050. * idx == 0: Standard early_data setup
  3051. * idx == 1: early_data setup using read_ahead
  3052. * usecb == 0: Don't use a custom early data callback
  3053. * usecb == 1: Use a custom early data callback and reject the early data
  3054. * usecb == 2: Use a custom early data callback and accept the early data
  3055. * confopt == 0: Configure anti-replay directly
  3056. * confopt == 1: Configure anti-replay using SSL_CONF
  3057. */
  3058. static int test_early_data_replay_int(int idx, int usecb, int confopt)
  3059. {
  3060. SSL_CTX *cctx = NULL, *sctx = NULL;
  3061. SSL *clientssl = NULL, *serverssl = NULL;
  3062. int testresult = 0;
  3063. SSL_SESSION *sess = NULL;
  3064. size_t readbytes, written;
  3065. unsigned char buf[20];
  3066. allow_ed_cb_called = 0;
  3067. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3068. TLS_client_method(), TLS1_VERSION, 0,
  3069. &sctx, &cctx, cert, privkey)))
  3070. return 0;
  3071. if (usecb > 0) {
  3072. if (confopt == 0) {
  3073. SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
  3074. } else {
  3075. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  3076. if (!TEST_ptr(confctx))
  3077. goto end;
  3078. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  3079. | SSL_CONF_FLAG_SERVER);
  3080. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  3081. if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
  3082. 2)) {
  3083. SSL_CONF_CTX_free(confctx);
  3084. goto end;
  3085. }
  3086. SSL_CONF_CTX_free(confctx);
  3087. }
  3088. SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
  3089. }
  3090. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3091. &serverssl, &sess, idx)))
  3092. goto end;
  3093. /*
  3094. * The server is configured to accept early data. Create a connection to
  3095. * "use up" the ticket
  3096. */
  3097. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3098. || !TEST_true(SSL_session_reused(clientssl)))
  3099. goto end;
  3100. SSL_shutdown(clientssl);
  3101. SSL_shutdown(serverssl);
  3102. SSL_free(serverssl);
  3103. SSL_free(clientssl);
  3104. serverssl = clientssl = NULL;
  3105. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3106. &clientssl, NULL, NULL))
  3107. || !TEST_true(SSL_set_session(clientssl, sess)))
  3108. goto end;
  3109. /* Write and read some early data */
  3110. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3111. &written))
  3112. || !TEST_size_t_eq(written, strlen(MSG1)))
  3113. goto end;
  3114. if (usecb <= 1) {
  3115. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3116. &readbytes),
  3117. SSL_READ_EARLY_DATA_FINISH)
  3118. /*
  3119. * The ticket was reused, so the we should have rejected the
  3120. * early data
  3121. */
  3122. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3123. SSL_EARLY_DATA_REJECTED))
  3124. goto end;
  3125. } else {
  3126. /* In this case the callback decides to accept the early data */
  3127. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3128. &readbytes),
  3129. SSL_READ_EARLY_DATA_SUCCESS)
  3130. || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
  3131. /*
  3132. * Server will have sent its flight so client can now send
  3133. * end of early data and complete its half of the handshake
  3134. */
  3135. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3136. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3137. &readbytes),
  3138. SSL_READ_EARLY_DATA_FINISH)
  3139. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3140. SSL_EARLY_DATA_ACCEPTED))
  3141. goto end;
  3142. }
  3143. /* Complete the connection */
  3144. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3145. || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
  3146. || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
  3147. goto end;
  3148. testresult = 1;
  3149. end:
  3150. SSL_SESSION_free(sess);
  3151. SSL_SESSION_free(clientpsk);
  3152. SSL_SESSION_free(serverpsk);
  3153. clientpsk = serverpsk = NULL;
  3154. SSL_free(serverssl);
  3155. SSL_free(clientssl);
  3156. SSL_CTX_free(sctx);
  3157. SSL_CTX_free(cctx);
  3158. return testresult;
  3159. }
  3160. static int test_early_data_replay(int idx)
  3161. {
  3162. int ret = 1, usecb, confopt;
  3163. for (usecb = 0; usecb < 3; usecb++) {
  3164. for (confopt = 0; confopt < 2; confopt++)
  3165. ret &= test_early_data_replay_int(idx, usecb, confopt);
  3166. }
  3167. return ret;
  3168. }
  3169. /*
  3170. * Helper function to test that a server attempting to read early data can
  3171. * handle a connection from a client where the early data should be skipped.
  3172. * testtype: 0 == No HRR
  3173. * testtype: 1 == HRR
  3174. * testtype: 2 == HRR, invalid early_data sent after HRR
  3175. * testtype: 3 == recv_max_early_data set to 0
  3176. */
  3177. static int early_data_skip_helper(int testtype, int idx)
  3178. {
  3179. SSL_CTX *cctx = NULL, *sctx = NULL;
  3180. SSL *clientssl = NULL, *serverssl = NULL;
  3181. int testresult = 0;
  3182. SSL_SESSION *sess = NULL;
  3183. unsigned char buf[20];
  3184. size_t readbytes, written;
  3185. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3186. &serverssl, &sess, idx)))
  3187. goto end;
  3188. if (testtype == 1 || testtype == 2) {
  3189. /* Force an HRR to occur */
  3190. #if defined(OPENSSL_NO_EC)
  3191. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  3192. goto end;
  3193. #else
  3194. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  3195. goto end;
  3196. #endif
  3197. } else if (idx == 2) {
  3198. /*
  3199. * We force early_data rejection by ensuring the PSK identity is
  3200. * unrecognised
  3201. */
  3202. srvid = "Dummy Identity";
  3203. } else {
  3204. /*
  3205. * Deliberately corrupt the creation time. We take 20 seconds off the
  3206. * time. It could be any value as long as it is not within tolerance.
  3207. * This should mean the ticket is rejected.
  3208. */
  3209. if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
  3210. goto end;
  3211. }
  3212. if (testtype == 3
  3213. && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
  3214. goto end;
  3215. /* Write some early data */
  3216. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3217. &written))
  3218. || !TEST_size_t_eq(written, strlen(MSG1)))
  3219. goto end;
  3220. /* Server should reject the early data */
  3221. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3222. &readbytes),
  3223. SSL_READ_EARLY_DATA_FINISH)
  3224. || !TEST_size_t_eq(readbytes, 0)
  3225. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3226. SSL_EARLY_DATA_REJECTED))
  3227. goto end;
  3228. switch (testtype) {
  3229. case 0:
  3230. /* Nothing to do */
  3231. break;
  3232. case 1:
  3233. /*
  3234. * Finish off the handshake. We perform the same writes and reads as
  3235. * further down but we expect them to fail due to the incomplete
  3236. * handshake.
  3237. */
  3238. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3239. || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
  3240. &readbytes)))
  3241. goto end;
  3242. break;
  3243. case 2:
  3244. {
  3245. BIO *wbio = SSL_get_wbio(clientssl);
  3246. /* A record that will appear as bad early_data */
  3247. const unsigned char bad_early_data[] = {
  3248. 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
  3249. };
  3250. /*
  3251. * We force the client to attempt a write. This will fail because
  3252. * we're still in the handshake. It will cause the second
  3253. * ClientHello to be sent.
  3254. */
  3255. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
  3256. &written)))
  3257. goto end;
  3258. /*
  3259. * Inject some early_data after the second ClientHello. This should
  3260. * cause the server to fail
  3261. */
  3262. if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
  3263. sizeof(bad_early_data), &written)))
  3264. goto end;
  3265. }
  3266. /* fallthrough */
  3267. case 3:
  3268. /*
  3269. * This client has sent more early_data than we are willing to skip
  3270. * (case 3) or sent invalid early_data (case 2) so the connection should
  3271. * abort.
  3272. */
  3273. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3274. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
  3275. goto end;
  3276. /* Connection has failed - nothing more to do */
  3277. testresult = 1;
  3278. goto end;
  3279. default:
  3280. TEST_error("Invalid test type");
  3281. goto end;
  3282. }
  3283. /*
  3284. * Should be able to send normal data despite rejection of early data. The
  3285. * early_data should be skipped.
  3286. */
  3287. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3288. || !TEST_size_t_eq(written, strlen(MSG2))
  3289. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3290. SSL_EARLY_DATA_REJECTED)
  3291. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3292. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3293. goto end;
  3294. testresult = 1;
  3295. end:
  3296. SSL_SESSION_free(clientpsk);
  3297. SSL_SESSION_free(serverpsk);
  3298. clientpsk = serverpsk = NULL;
  3299. SSL_SESSION_free(sess);
  3300. SSL_free(serverssl);
  3301. SSL_free(clientssl);
  3302. SSL_CTX_free(sctx);
  3303. SSL_CTX_free(cctx);
  3304. return testresult;
  3305. }
  3306. /*
  3307. * Test that a server attempting to read early data can handle a connection
  3308. * from a client where the early data is not acceptable.
  3309. */
  3310. static int test_early_data_skip(int idx)
  3311. {
  3312. return early_data_skip_helper(0, idx);
  3313. }
  3314. /*
  3315. * Test that a server attempting to read early data can handle a connection
  3316. * from a client where an HRR occurs.
  3317. */
  3318. static int test_early_data_skip_hrr(int idx)
  3319. {
  3320. return early_data_skip_helper(1, idx);
  3321. }
  3322. /*
  3323. * Test that a server attempting to read early data can handle a connection
  3324. * from a client where an HRR occurs and correctly fails if early_data is sent
  3325. * after the HRR
  3326. */
  3327. static int test_early_data_skip_hrr_fail(int idx)
  3328. {
  3329. return early_data_skip_helper(2, idx);
  3330. }
  3331. /*
  3332. * Test that a server attempting to read early data will abort if it tries to
  3333. * skip over too much.
  3334. */
  3335. static int test_early_data_skip_abort(int idx)
  3336. {
  3337. return early_data_skip_helper(3, idx);
  3338. }
  3339. /*
  3340. * Test that a server attempting to read early data can handle a connection
  3341. * from a client that doesn't send any.
  3342. */
  3343. static int test_early_data_not_sent(int idx)
  3344. {
  3345. SSL_CTX *cctx = NULL, *sctx = NULL;
  3346. SSL *clientssl = NULL, *serverssl = NULL;
  3347. int testresult = 0;
  3348. SSL_SESSION *sess = NULL;
  3349. unsigned char buf[20];
  3350. size_t readbytes, written;
  3351. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3352. &serverssl, &sess, idx)))
  3353. goto end;
  3354. /* Write some data - should block due to handshake with server */
  3355. SSL_set_connect_state(clientssl);
  3356. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3357. goto end;
  3358. /* Server should detect that early data has not been sent */
  3359. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3360. &readbytes),
  3361. SSL_READ_EARLY_DATA_FINISH)
  3362. || !TEST_size_t_eq(readbytes, 0)
  3363. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3364. SSL_EARLY_DATA_NOT_SENT)
  3365. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3366. SSL_EARLY_DATA_NOT_SENT))
  3367. goto end;
  3368. /* Continue writing the message we started earlier */
  3369. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3370. || !TEST_size_t_eq(written, strlen(MSG1))
  3371. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3372. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3373. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  3374. || !TEST_size_t_eq(written, strlen(MSG2)))
  3375. goto end;
  3376. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  3377. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3378. goto end;
  3379. testresult = 1;
  3380. end:
  3381. SSL_SESSION_free(sess);
  3382. SSL_SESSION_free(clientpsk);
  3383. SSL_SESSION_free(serverpsk);
  3384. clientpsk = serverpsk = NULL;
  3385. SSL_free(serverssl);
  3386. SSL_free(clientssl);
  3387. SSL_CTX_free(sctx);
  3388. SSL_CTX_free(cctx);
  3389. return testresult;
  3390. }
  3391. static const char *servalpn;
  3392. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  3393. unsigned char *outlen, const unsigned char *in,
  3394. unsigned int inlen, void *arg)
  3395. {
  3396. unsigned int protlen = 0;
  3397. const unsigned char *prot;
  3398. for (prot = in; prot < in + inlen; prot += protlen) {
  3399. protlen = *prot++;
  3400. if (in + inlen < prot + protlen)
  3401. return SSL_TLSEXT_ERR_NOACK;
  3402. if (protlen == strlen(servalpn)
  3403. && memcmp(prot, servalpn, protlen) == 0) {
  3404. *out = prot;
  3405. *outlen = protlen;
  3406. return SSL_TLSEXT_ERR_OK;
  3407. }
  3408. }
  3409. return SSL_TLSEXT_ERR_NOACK;
  3410. }
  3411. /* Test that a PSK can be used to send early_data */
  3412. static int test_early_data_psk(int idx)
  3413. {
  3414. SSL_CTX *cctx = NULL, *sctx = NULL;
  3415. SSL *clientssl = NULL, *serverssl = NULL;
  3416. int testresult = 0;
  3417. SSL_SESSION *sess = NULL;
  3418. unsigned char alpnlist[] = {
  3419. 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
  3420. 'l', 'p', 'n'
  3421. };
  3422. #define GOODALPNLEN 9
  3423. #define BADALPNLEN 8
  3424. #define GOODALPN (alpnlist)
  3425. #define BADALPN (alpnlist + GOODALPNLEN)
  3426. int err = 0;
  3427. unsigned char buf[20];
  3428. size_t readbytes, written;
  3429. int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
  3430. int edstatus = SSL_EARLY_DATA_ACCEPTED;
  3431. /* We always set this up with a final parameter of "2" for PSK */
  3432. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3433. &serverssl, &sess, 2)))
  3434. goto end;
  3435. servalpn = "goodalpn";
  3436. /*
  3437. * Note: There is no test for inconsistent SNI with late client detection.
  3438. * This is because servers do not acknowledge SNI even if they are using
  3439. * it in a resumption handshake - so it is not actually possible for a
  3440. * client to detect a problem.
  3441. */
  3442. switch (idx) {
  3443. case 0:
  3444. /* Set inconsistent SNI (early client detection) */
  3445. err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
  3446. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3447. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
  3448. goto end;
  3449. break;
  3450. case 1:
  3451. /* Set inconsistent ALPN (early client detection) */
  3452. err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
  3453. /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
  3454. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
  3455. GOODALPNLEN))
  3456. || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
  3457. BADALPNLEN)))
  3458. goto end;
  3459. break;
  3460. case 2:
  3461. /*
  3462. * Set invalid protocol version. Technically this affects PSKs without
  3463. * early_data too, but we test it here because it is similar to the
  3464. * SNI/ALPN consistency tests.
  3465. */
  3466. err = SSL_R_BAD_PSK;
  3467. if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
  3468. goto end;
  3469. break;
  3470. case 3:
  3471. /*
  3472. * Set inconsistent SNI (server side). In this case the connection
  3473. * will succeed and accept early_data. In TLSv1.3 on the server side SNI
  3474. * is associated with each handshake - not the session. Therefore it
  3475. * should not matter that we used a different server name last time.
  3476. */
  3477. SSL_SESSION_free(serverpsk);
  3478. serverpsk = SSL_SESSION_dup(clientpsk);
  3479. if (!TEST_ptr(serverpsk)
  3480. || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
  3481. goto end;
  3482. /* Fall through */
  3483. case 4:
  3484. /* Set consistent SNI */
  3485. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  3486. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
  3487. || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  3488. hostname_cb)))
  3489. goto end;
  3490. break;
  3491. case 5:
  3492. /*
  3493. * Set inconsistent ALPN (server detected). In this case the connection
  3494. * will succeed but reject early_data.
  3495. */
  3496. servalpn = "badalpn";
  3497. edstatus = SSL_EARLY_DATA_REJECTED;
  3498. readearlyres = SSL_READ_EARLY_DATA_FINISH;
  3499. /* Fall through */
  3500. case 6:
  3501. /*
  3502. * Set consistent ALPN.
  3503. * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
  3504. * accepts a list of protos (each one length prefixed).
  3505. * SSL_set1_alpn_selected accepts a single protocol (not length
  3506. * prefixed)
  3507. */
  3508. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
  3509. GOODALPNLEN - 1))
  3510. || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
  3511. GOODALPNLEN)))
  3512. goto end;
  3513. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3514. break;
  3515. case 7:
  3516. /* Set inconsistent ALPN (late client detection) */
  3517. SSL_SESSION_free(serverpsk);
  3518. serverpsk = SSL_SESSION_dup(clientpsk);
  3519. if (!TEST_ptr(serverpsk)
  3520. || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
  3521. BADALPN + 1,
  3522. BADALPNLEN - 1))
  3523. || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
  3524. GOODALPN + 1,
  3525. GOODALPNLEN - 1))
  3526. || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
  3527. sizeof(alpnlist))))
  3528. goto end;
  3529. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  3530. edstatus = SSL_EARLY_DATA_ACCEPTED;
  3531. readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
  3532. /* SSL_connect() call should fail */
  3533. connectres = -1;
  3534. break;
  3535. default:
  3536. TEST_error("Bad test index");
  3537. goto end;
  3538. }
  3539. SSL_set_connect_state(clientssl);
  3540. if (err != 0) {
  3541. if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3542. &written))
  3543. || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
  3544. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
  3545. goto end;
  3546. } else {
  3547. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3548. &written)))
  3549. goto end;
  3550. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3551. &readbytes), readearlyres)
  3552. || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
  3553. && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  3554. || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
  3555. || !TEST_int_eq(SSL_connect(clientssl), connectres))
  3556. goto end;
  3557. }
  3558. testresult = 1;
  3559. end:
  3560. SSL_SESSION_free(sess);
  3561. SSL_SESSION_free(clientpsk);
  3562. SSL_SESSION_free(serverpsk);
  3563. clientpsk = serverpsk = NULL;
  3564. SSL_free(serverssl);
  3565. SSL_free(clientssl);
  3566. SSL_CTX_free(sctx);
  3567. SSL_CTX_free(cctx);
  3568. return testresult;
  3569. }
  3570. /*
  3571. * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
  3572. * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
  3573. * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
  3574. * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3575. * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
  3576. * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
  3577. */
  3578. static int test_early_data_psk_with_all_ciphers(int idx)
  3579. {
  3580. SSL_CTX *cctx = NULL, *sctx = NULL;
  3581. SSL *clientssl = NULL, *serverssl = NULL;
  3582. int testresult = 0;
  3583. SSL_SESSION *sess = NULL;
  3584. unsigned char buf[20];
  3585. size_t readbytes, written;
  3586. const SSL_CIPHER *cipher;
  3587. const char *cipher_str[] = {
  3588. TLS1_3_RFC_AES_128_GCM_SHA256,
  3589. TLS1_3_RFC_AES_256_GCM_SHA384,
  3590. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3591. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3592. # else
  3593. NULL,
  3594. # endif
  3595. TLS1_3_RFC_AES_128_CCM_SHA256,
  3596. TLS1_3_RFC_AES_128_CCM_8_SHA256
  3597. };
  3598. const unsigned char *cipher_bytes[] = {
  3599. TLS13_AES_128_GCM_SHA256_BYTES,
  3600. TLS13_AES_256_GCM_SHA384_BYTES,
  3601. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3602. TLS13_CHACHA20_POLY1305_SHA256_BYTES,
  3603. # else
  3604. NULL,
  3605. # endif
  3606. TLS13_AES_128_CCM_SHA256_BYTES,
  3607. TLS13_AES_128_CCM_8_SHA256_BYTES
  3608. };
  3609. if (cipher_str[idx] == NULL)
  3610. return 1;
  3611. /* Skip ChaCha20Poly1305 as currently FIPS module does not support it */
  3612. if (idx == 2 && is_fips == 1)
  3613. return 1;
  3614. /* We always set this up with a final parameter of "2" for PSK */
  3615. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3616. &serverssl, &sess, 2)))
  3617. goto end;
  3618. if (idx == 4) {
  3619. /* CCM8 ciphers are considered low security due to their short tag */
  3620. SSL_set_security_level(clientssl, 0);
  3621. SSL_set_security_level(serverssl, 0);
  3622. }
  3623. if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
  3624. || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
  3625. goto end;
  3626. /*
  3627. * 'setupearly_data_test' creates only one instance of SSL_SESSION
  3628. * and assigns to both client and server with incremented reference
  3629. * and the same instance is updated in 'sess'.
  3630. * So updating ciphersuite in 'sess' which will get reflected in
  3631. * PSK handshake using psk use sess and find sess cb.
  3632. */
  3633. cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
  3634. if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
  3635. goto end;
  3636. SSL_set_connect_state(clientssl);
  3637. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3638. &written)))
  3639. goto end;
  3640. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3641. &readbytes),
  3642. SSL_READ_EARLY_DATA_SUCCESS)
  3643. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3644. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3645. SSL_EARLY_DATA_ACCEPTED)
  3646. || !TEST_int_eq(SSL_connect(clientssl), 1)
  3647. || !TEST_int_eq(SSL_accept(serverssl), 1))
  3648. goto end;
  3649. /* Send some normal data from client to server */
  3650. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3651. || !TEST_size_t_eq(written, strlen(MSG2)))
  3652. goto end;
  3653. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3654. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3655. goto end;
  3656. testresult = 1;
  3657. end:
  3658. SSL_SESSION_free(sess);
  3659. SSL_SESSION_free(clientpsk);
  3660. SSL_SESSION_free(serverpsk);
  3661. clientpsk = serverpsk = NULL;
  3662. if (clientssl != NULL)
  3663. SSL_shutdown(clientssl);
  3664. if (serverssl != NULL)
  3665. SSL_shutdown(serverssl);
  3666. SSL_free(serverssl);
  3667. SSL_free(clientssl);
  3668. SSL_CTX_free(sctx);
  3669. SSL_CTX_free(cctx);
  3670. return testresult;
  3671. }
  3672. /*
  3673. * Test that a server that doesn't try to read early data can handle a
  3674. * client sending some.
  3675. */
  3676. static int test_early_data_not_expected(int idx)
  3677. {
  3678. SSL_CTX *cctx = NULL, *sctx = NULL;
  3679. SSL *clientssl = NULL, *serverssl = NULL;
  3680. int testresult = 0;
  3681. SSL_SESSION *sess = NULL;
  3682. unsigned char buf[20];
  3683. size_t readbytes, written;
  3684. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3685. &serverssl, &sess, idx)))
  3686. goto end;
  3687. /* Write some early data */
  3688. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  3689. &written)))
  3690. goto end;
  3691. /*
  3692. * Server should skip over early data and then block waiting for client to
  3693. * continue handshake
  3694. */
  3695. if (!TEST_int_le(SSL_accept(serverssl), 0)
  3696. || !TEST_int_gt(SSL_connect(clientssl), 0)
  3697. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3698. SSL_EARLY_DATA_REJECTED)
  3699. || !TEST_int_gt(SSL_accept(serverssl), 0)
  3700. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3701. SSL_EARLY_DATA_REJECTED))
  3702. goto end;
  3703. /* Send some normal data from client to server */
  3704. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  3705. || !TEST_size_t_eq(written, strlen(MSG2)))
  3706. goto end;
  3707. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3708. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3709. goto end;
  3710. testresult = 1;
  3711. end:
  3712. SSL_SESSION_free(sess);
  3713. SSL_SESSION_free(clientpsk);
  3714. SSL_SESSION_free(serverpsk);
  3715. clientpsk = serverpsk = NULL;
  3716. SSL_free(serverssl);
  3717. SSL_free(clientssl);
  3718. SSL_CTX_free(sctx);
  3719. SSL_CTX_free(cctx);
  3720. return testresult;
  3721. }
  3722. # ifndef OPENSSL_NO_TLS1_2
  3723. /*
  3724. * Test that a server attempting to read early data can handle a connection
  3725. * from a TLSv1.2 client.
  3726. */
  3727. static int test_early_data_tls1_2(int idx)
  3728. {
  3729. SSL_CTX *cctx = NULL, *sctx = NULL;
  3730. SSL *clientssl = NULL, *serverssl = NULL;
  3731. int testresult = 0;
  3732. unsigned char buf[20];
  3733. size_t readbytes, written;
  3734. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  3735. &serverssl, NULL, idx)))
  3736. goto end;
  3737. /* Write some data - should block due to handshake with server */
  3738. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  3739. SSL_set_connect_state(clientssl);
  3740. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  3741. goto end;
  3742. /*
  3743. * Server should do TLSv1.2 handshake. First it will block waiting for more
  3744. * messages from client after ServerDone. Then SSL_read_early_data should
  3745. * finish and detect that early data has not been sent
  3746. */
  3747. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3748. &readbytes),
  3749. SSL_READ_EARLY_DATA_ERROR))
  3750. goto end;
  3751. /*
  3752. * Continue writing the message we started earlier. Will still block waiting
  3753. * for the CCS/Finished from server
  3754. */
  3755. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3756. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  3757. &readbytes),
  3758. SSL_READ_EARLY_DATA_FINISH)
  3759. || !TEST_size_t_eq(readbytes, 0)
  3760. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  3761. SSL_EARLY_DATA_NOT_SENT))
  3762. goto end;
  3763. /* Continue writing the message we started earlier */
  3764. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  3765. || !TEST_size_t_eq(written, strlen(MSG1))
  3766. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  3767. SSL_EARLY_DATA_NOT_SENT)
  3768. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  3769. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  3770. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  3771. || !TEST_size_t_eq(written, strlen(MSG2))
  3772. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  3773. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  3774. goto end;
  3775. testresult = 1;
  3776. end:
  3777. SSL_SESSION_free(clientpsk);
  3778. SSL_SESSION_free(serverpsk);
  3779. clientpsk = serverpsk = NULL;
  3780. SSL_free(serverssl);
  3781. SSL_free(clientssl);
  3782. SSL_CTX_free(sctx);
  3783. SSL_CTX_free(cctx);
  3784. return testresult;
  3785. }
  3786. # endif /* OPENSSL_NO_TLS1_2 */
  3787. /*
  3788. * Test configuring the TLSv1.3 ciphersuites
  3789. *
  3790. * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
  3791. * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
  3792. * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
  3793. * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
  3794. * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  3795. * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  3796. * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
  3797. * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
  3798. * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
  3799. * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
  3800. */
  3801. static int test_set_ciphersuite(int idx)
  3802. {
  3803. SSL_CTX *cctx = NULL, *sctx = NULL;
  3804. SSL *clientssl = NULL, *serverssl = NULL;
  3805. int testresult = 0;
  3806. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3807. TLS_client_method(), TLS1_VERSION, 0,
  3808. &sctx, &cctx, cert, privkey))
  3809. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3810. "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
  3811. goto end;
  3812. if (idx >=4 && idx <= 7) {
  3813. /* SSL_CTX explicit cipher list */
  3814. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
  3815. goto end;
  3816. }
  3817. if (idx == 0 || idx == 4) {
  3818. /* Default ciphersuite */
  3819. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3820. "TLS_AES_128_GCM_SHA256")))
  3821. goto end;
  3822. } else if (idx == 1 || idx == 5) {
  3823. /* Non default ciphersuite */
  3824. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3825. "TLS_AES_128_CCM_SHA256")))
  3826. goto end;
  3827. }
  3828. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3829. &clientssl, NULL, NULL)))
  3830. goto end;
  3831. if (idx == 8 || idx == 9) {
  3832. /* SSL explicit cipher list */
  3833. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
  3834. goto end;
  3835. }
  3836. if (idx == 2 || idx == 6 || idx == 8) {
  3837. /* Default ciphersuite */
  3838. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  3839. "TLS_AES_128_GCM_SHA256")))
  3840. goto end;
  3841. } else if (idx == 3 || idx == 7 || idx == 9) {
  3842. /* Non default ciphersuite */
  3843. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  3844. "TLS_AES_128_CCM_SHA256")))
  3845. goto end;
  3846. }
  3847. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  3848. goto end;
  3849. testresult = 1;
  3850. end:
  3851. SSL_free(serverssl);
  3852. SSL_free(clientssl);
  3853. SSL_CTX_free(sctx);
  3854. SSL_CTX_free(cctx);
  3855. return testresult;
  3856. }
  3857. static int test_ciphersuite_change(void)
  3858. {
  3859. SSL_CTX *cctx = NULL, *sctx = NULL;
  3860. SSL *clientssl = NULL, *serverssl = NULL;
  3861. SSL_SESSION *clntsess = NULL;
  3862. int testresult = 0;
  3863. const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
  3864. /* Create a session based on SHA-256 */
  3865. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  3866. TLS_client_method(), TLS1_VERSION, 0,
  3867. &sctx, &cctx, cert, privkey))
  3868. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3869. "TLS_AES_128_GCM_SHA256:"
  3870. "TLS_AES_256_GCM_SHA384:"
  3871. "TLS_AES_128_CCM_SHA256"))
  3872. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3873. "TLS_AES_128_GCM_SHA256")))
  3874. goto end;
  3875. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3876. NULL, NULL))
  3877. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3878. SSL_ERROR_NONE)))
  3879. goto end;
  3880. clntsess = SSL_get1_session(clientssl);
  3881. /* Save for later */
  3882. aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
  3883. SSL_shutdown(clientssl);
  3884. SSL_shutdown(serverssl);
  3885. SSL_free(serverssl);
  3886. SSL_free(clientssl);
  3887. serverssl = clientssl = NULL;
  3888. /* Check we can resume a session with a different SHA-256 ciphersuite */
  3889. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3890. "TLS_AES_128_CCM_SHA256"))
  3891. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3892. &clientssl, NULL, NULL))
  3893. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3894. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3895. SSL_ERROR_NONE))
  3896. || !TEST_true(SSL_session_reused(clientssl)))
  3897. goto end;
  3898. SSL_SESSION_free(clntsess);
  3899. clntsess = SSL_get1_session(clientssl);
  3900. SSL_shutdown(clientssl);
  3901. SSL_shutdown(serverssl);
  3902. SSL_free(serverssl);
  3903. SSL_free(clientssl);
  3904. serverssl = clientssl = NULL;
  3905. /*
  3906. * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
  3907. * succeeds but does not resume.
  3908. */
  3909. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  3910. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3911. NULL, NULL))
  3912. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3913. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3914. SSL_ERROR_SSL))
  3915. || !TEST_false(SSL_session_reused(clientssl)))
  3916. goto end;
  3917. SSL_SESSION_free(clntsess);
  3918. clntsess = NULL;
  3919. SSL_shutdown(clientssl);
  3920. SSL_shutdown(serverssl);
  3921. SSL_free(serverssl);
  3922. SSL_free(clientssl);
  3923. serverssl = clientssl = NULL;
  3924. /* Create a session based on SHA384 */
  3925. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  3926. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3927. &clientssl, NULL, NULL))
  3928. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3929. SSL_ERROR_NONE)))
  3930. goto end;
  3931. clntsess = SSL_get1_session(clientssl);
  3932. SSL_shutdown(clientssl);
  3933. SSL_shutdown(serverssl);
  3934. SSL_free(serverssl);
  3935. SSL_free(clientssl);
  3936. serverssl = clientssl = NULL;
  3937. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3938. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
  3939. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3940. "TLS_AES_256_GCM_SHA384"))
  3941. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3942. NULL, NULL))
  3943. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3944. /*
  3945. * We use SSL_ERROR_WANT_READ below so that we can pause the
  3946. * connection after the initial ClientHello has been sent to
  3947. * enable us to make some session changes.
  3948. */
  3949. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3950. SSL_ERROR_WANT_READ)))
  3951. goto end;
  3952. /* Trick the client into thinking this session is for a different digest */
  3953. clntsess->cipher = aes_128_gcm_sha256;
  3954. clntsess->cipher_id = clntsess->cipher->id;
  3955. /*
  3956. * Continue the previously started connection. Server has selected a SHA-384
  3957. * ciphersuite, but client thinks the session is for SHA-256, so it should
  3958. * bail out.
  3959. */
  3960. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  3961. SSL_ERROR_SSL))
  3962. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
  3963. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
  3964. goto end;
  3965. testresult = 1;
  3966. end:
  3967. SSL_SESSION_free(clntsess);
  3968. SSL_free(serverssl);
  3969. SSL_free(clientssl);
  3970. SSL_CTX_free(sctx);
  3971. SSL_CTX_free(cctx);
  3972. return testresult;
  3973. }
  3974. /*
  3975. * Test TLSv1.3 Key exchange
  3976. * Test 0 = Test all ECDHE Key exchange with TLSv1.3 client and server
  3977. * Test 1 = Test NID_X9_62_prime256v1 with TLSv1.3 client and server
  3978. * Test 2 = Test NID_secp384r1 with TLSv1.3 client and server
  3979. * Test 3 = Test NID_secp521r1 with TLSv1.3 client and server
  3980. * Test 4 = Test NID_X25519 with TLSv1.3 client and server
  3981. * Test 5 = Test NID_X448 with TLSv1.3 client and server
  3982. * Test 6 = Test all FFDHE Key exchange with TLSv1.3 client and server
  3983. * Test 7 = Test NID_ffdhe2048 with TLSv1.3 client and server
  3984. * Test 8 = Test NID_ffdhe3072 with TLSv1.3 client and server
  3985. * Test 9 = Test NID_ffdhe4096 with TLSv1.3 client and server
  3986. * Test 10 = Test NID_ffdhe6144 with TLSv1.3 client and server
  3987. * Test 11 = Test NID_ffdhe8192 with TLSv1.3 client and server
  3988. * Test 12 = Test all ECDHE with TLSv1.2 client and server
  3989. * Test 13 = Test all FFDHE with TLSv1.2 client and server
  3990. */
  3991. # ifndef OPENSSL_NO_EC
  3992. static int ecdhe_kexch_groups[] = {NID_X9_62_prime256v1, NID_secp384r1,
  3993. NID_secp521r1, NID_X25519, NID_X448};
  3994. # endif
  3995. # ifndef OPENSSL_NO_DH
  3996. static int ffdhe_kexch_groups[] = {NID_ffdhe2048, NID_ffdhe3072, NID_ffdhe4096,
  3997. NID_ffdhe6144, NID_ffdhe8192};
  3998. # endif
  3999. static int test_key_exchange(int idx)
  4000. {
  4001. SSL_CTX *sctx = NULL, *cctx = NULL;
  4002. SSL *serverssl = NULL, *clientssl = NULL;
  4003. int testresult = 0;
  4004. int kexch_alg;
  4005. int *kexch_groups = &kexch_alg;
  4006. int kexch_groups_size = 1;
  4007. int max_version = TLS1_3_VERSION;
  4008. char *kexch_name0 = NULL;
  4009. switch (idx) {
  4010. # ifndef OPENSSL_NO_EC
  4011. # ifndef OPENSSL_NO_TLS1_2
  4012. case 12:
  4013. max_version = TLS1_2_VERSION;
  4014. # endif
  4015. /* Fall through */
  4016. case 0:
  4017. kexch_groups = ecdhe_kexch_groups;
  4018. kexch_groups_size = OSSL_NELEM(ecdhe_kexch_groups);
  4019. kexch_name0 = "secp256r1";
  4020. break;
  4021. case 1:
  4022. kexch_alg = NID_X9_62_prime256v1;
  4023. kexch_name0 = "secp256r1";
  4024. break;
  4025. case 2:
  4026. kexch_alg = NID_secp384r1;
  4027. kexch_name0 = "secp384r1";
  4028. break;
  4029. case 3:
  4030. kexch_alg = NID_secp521r1;
  4031. kexch_name0 = "secp521r1";
  4032. break;
  4033. case 4:
  4034. kexch_alg = NID_X25519;
  4035. kexch_name0 = "x25519";
  4036. break;
  4037. case 5:
  4038. kexch_alg = NID_X448;
  4039. kexch_name0 = "x448";
  4040. break;
  4041. # endif
  4042. # ifndef OPENSSL_NO_DH
  4043. # ifndef OPENSSL_NO_TLS1_2
  4044. case 13:
  4045. max_version = TLS1_2_VERSION;
  4046. kexch_name0 = "ffdhe2048";
  4047. # endif
  4048. /* Fall through */
  4049. case 6:
  4050. kexch_groups = ffdhe_kexch_groups;
  4051. kexch_groups_size = OSSL_NELEM(ffdhe_kexch_groups);
  4052. kexch_name0 = "ffdhe2048";
  4053. break;
  4054. case 7:
  4055. kexch_alg = NID_ffdhe2048;
  4056. kexch_name0 = "ffdhe2048";
  4057. break;
  4058. case 8:
  4059. kexch_alg = NID_ffdhe3072;
  4060. kexch_name0 = "ffdhe3072";
  4061. break;
  4062. case 9:
  4063. kexch_alg = NID_ffdhe4096;
  4064. kexch_name0 = "ffdhe4096";
  4065. break;
  4066. case 10:
  4067. kexch_alg = NID_ffdhe6144;
  4068. kexch_name0 = "ffdhe6144";
  4069. break;
  4070. case 11:
  4071. kexch_alg = NID_ffdhe8192;
  4072. kexch_name0 = "ffdhe8192";
  4073. break;
  4074. # endif
  4075. default:
  4076. /* We're skipping this test */
  4077. return 1;
  4078. }
  4079. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4080. TLS_client_method(), TLS1_VERSION,
  4081. max_version, &sctx, &cctx, cert,
  4082. privkey)))
  4083. goto end;
  4084. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4085. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4086. goto end;
  4087. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4088. TLS1_3_RFC_AES_128_GCM_SHA256)))
  4089. goto end;
  4090. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4091. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4092. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4093. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4094. goto end;
  4095. /*
  4096. * Must include an EC ciphersuite so that we send supported groups in
  4097. * TLSv1.2
  4098. */
  4099. # ifndef OPENSSL_NO_TLS1_2
  4100. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4101. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4102. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4103. goto end;
  4104. # endif
  4105. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4106. NULL, NULL)))
  4107. goto end;
  4108. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, kexch_groups_size))
  4109. || !TEST_true(SSL_set1_groups(clientssl, kexch_groups, kexch_groups_size)))
  4110. goto end;
  4111. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4112. goto end;
  4113. /*
  4114. * If Handshake succeeds the negotiated kexch alg should be the first one in
  4115. * configured, except in the case of FFDHE groups (idx 13), which are
  4116. * TLSv1.3 only so we expect no shared group to exist.
  4117. */
  4118. if (!TEST_int_eq(SSL_get_shared_group(serverssl, 0),
  4119. idx == 13 ? 0 : kexch_groups[0]))
  4120. goto end;
  4121. if (!TEST_str_eq(SSL_group_to_name(serverssl, kexch_groups[0]),
  4122. kexch_name0))
  4123. goto end;
  4124. /* We don't implement RFC 7919 named groups for TLS 1.2. */
  4125. if (idx != 13) {
  4126. if (!TEST_int_eq(SSL_get_negotiated_group(serverssl), kexch_groups[0]))
  4127. goto end;
  4128. if (!TEST_int_eq(SSL_get_negotiated_group(clientssl), kexch_groups[0]))
  4129. goto end;
  4130. }
  4131. testresult = 1;
  4132. end:
  4133. SSL_free(serverssl);
  4134. SSL_free(clientssl);
  4135. SSL_CTX_free(sctx);
  4136. SSL_CTX_free(cctx);
  4137. return testresult;
  4138. }
  4139. # if !defined(OPENSSL_NO_TLS1_2) \
  4140. && !defined(OPENSSL_NO_EC) \
  4141. && !defined(OPENSSL_NO_DH)
  4142. static int set_ssl_groups(SSL *serverssl, SSL *clientssl, int clientmulti,
  4143. int isecdhe, int idx)
  4144. {
  4145. int kexch_alg;
  4146. int *kexch_groups = &kexch_alg;
  4147. int numec, numff;
  4148. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4149. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4150. if (isecdhe)
  4151. kexch_alg = ecdhe_kexch_groups[idx];
  4152. else
  4153. kexch_alg = ffdhe_kexch_groups[idx];
  4154. if (clientmulti) {
  4155. if (!TEST_true(SSL_set1_groups(serverssl, kexch_groups, 1)))
  4156. return 0;
  4157. if (isecdhe) {
  4158. if (!TEST_true(SSL_set1_groups(clientssl, ecdhe_kexch_groups,
  4159. numec)))
  4160. return 0;
  4161. } else {
  4162. if (!TEST_true(SSL_set1_groups(clientssl, ffdhe_kexch_groups,
  4163. numff)))
  4164. return 0;
  4165. }
  4166. } else {
  4167. if (!TEST_true(SSL_set1_groups(clientssl, kexch_groups, 1)))
  4168. return 0;
  4169. if (isecdhe) {
  4170. if (!TEST_true(SSL_set1_groups(serverssl, ecdhe_kexch_groups,
  4171. numec)))
  4172. return 0;
  4173. } else {
  4174. if (!TEST_true(SSL_set1_groups(serverssl, ffdhe_kexch_groups,
  4175. numff)))
  4176. return 0;
  4177. }
  4178. }
  4179. return 1;
  4180. }
  4181. /*-
  4182. * Test the SSL_get_negotiated_group() API across a battery of scenarios.
  4183. * Run through both the ECDHE and FFDHE group lists used in the previous
  4184. * test, for both TLS 1.2 and TLS 1.3, negotiating each group in turn,
  4185. * confirming the expected result; then perform a resumption handshake
  4186. * while offering the same group list, and another resumption handshake
  4187. * offering a different group list. The returned value should be the
  4188. * negotiated group for the initial handshake; for TLS 1.3 resumption
  4189. * handshakes the returned value will be negotiated on the resumption
  4190. * handshake itself, but for TLS 1.2 resumption handshakes the value will
  4191. * be cached in the session from the original handshake, regardless of what
  4192. * was offered in the resumption ClientHello.
  4193. *
  4194. * Using E for the number of EC groups and F for the number of FF groups:
  4195. * E tests of ECDHE with TLS 1.3, server only has one group
  4196. * F tests of FFDHE with TLS 1.3, server only has one group
  4197. * E tests of ECDHE with TLS 1.2, server only has one group
  4198. * F tests of FFDHE with TLS 1.2, server only has one group
  4199. * E tests of ECDHE with TLS 1.3, client sends only one group
  4200. * F tests of FFDHE with TLS 1.3, client sends only one group
  4201. * E tests of ECDHE with TLS 1.2, client sends only one group
  4202. * F tests of FFDHE with TLS 1.2, client sends only one group
  4203. */
  4204. static int test_negotiated_group(int idx)
  4205. {
  4206. int clientmulti, istls13, isecdhe, numec, numff, numgroups;
  4207. int expectednid;
  4208. SSL_CTX *sctx = NULL, *cctx = NULL;
  4209. SSL *serverssl = NULL, *clientssl = NULL;
  4210. SSL_SESSION *origsess = NULL;
  4211. int testresult = 0;
  4212. int kexch_alg;
  4213. int max_version = TLS1_3_VERSION;
  4214. numec = OSSL_NELEM(ecdhe_kexch_groups);
  4215. numff = OSSL_NELEM(ffdhe_kexch_groups);
  4216. numgroups = numec + numff;
  4217. clientmulti = (idx < 2 * numgroups);
  4218. idx = idx % (2 * numgroups);
  4219. istls13 = (idx < numgroups);
  4220. idx = idx % numgroups;
  4221. isecdhe = (idx < numec);
  4222. if (!isecdhe)
  4223. idx -= numec;
  4224. /* Now 'idx' is an index into ecdhe_kexch_groups or ffdhe_kexch_groups */
  4225. if (isecdhe)
  4226. kexch_alg = ecdhe_kexch_groups[idx];
  4227. else
  4228. kexch_alg = ffdhe_kexch_groups[idx];
  4229. /* We expect nothing for the unimplemented TLS 1.2 FFDHE named groups */
  4230. if (!istls13 && !isecdhe)
  4231. expectednid = NID_undef;
  4232. else
  4233. expectednid = kexch_alg;
  4234. if (!istls13)
  4235. max_version = TLS1_2_VERSION;
  4236. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4237. TLS_client_method(), TLS1_VERSION,
  4238. max_version, &sctx, &cctx, cert,
  4239. privkey)))
  4240. goto end;
  4241. /*
  4242. * Force (EC)DHE ciphers for TLS 1.2.
  4243. * Be sure to enable auto tmp DH so that FFDHE can succeed.
  4244. */
  4245. if (!TEST_true(SSL_CTX_set_cipher_list(sctx,
  4246. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4247. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256))
  4248. || !TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  4249. goto end;
  4250. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4251. TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256 ":"
  4252. TLS1_TXT_DHE_RSA_WITH_AES_128_GCM_SHA256)))
  4253. goto end;
  4254. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4255. NULL, NULL)))
  4256. goto end;
  4257. if (!TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti, isecdhe,
  4258. idx)))
  4259. goto end;
  4260. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  4261. goto end;
  4262. /* Initial handshake; always the configured one */
  4263. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4264. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4265. goto end;
  4266. if (!TEST_ptr((origsess = SSL_get1_session(clientssl))))
  4267. goto end;
  4268. SSL_shutdown(clientssl);
  4269. SSL_shutdown(serverssl);
  4270. SSL_free(serverssl);
  4271. SSL_free(clientssl);
  4272. serverssl = clientssl = NULL;
  4273. /* First resumption attempt; use the same config as initial handshake */
  4274. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4275. NULL, NULL))
  4276. || !TEST_true(SSL_set_session(clientssl, origsess))
  4277. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4278. isecdhe, idx)))
  4279. goto end;
  4280. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4281. || !TEST_true(SSL_session_reused(clientssl)))
  4282. goto end;
  4283. /* Still had better agree, since nothing changed... */
  4284. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4285. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4286. goto end;
  4287. SSL_shutdown(clientssl);
  4288. SSL_shutdown(serverssl);
  4289. SSL_free(serverssl);
  4290. SSL_free(clientssl);
  4291. serverssl = clientssl = NULL;
  4292. /*-
  4293. * Second resumption attempt
  4294. * The party that picks one group changes it, which we effectuate by
  4295. * changing 'idx' and updating what we expect.
  4296. */
  4297. if (idx == 0)
  4298. idx = 1;
  4299. else
  4300. idx--;
  4301. if (istls13) {
  4302. if (isecdhe)
  4303. expectednid = ecdhe_kexch_groups[idx];
  4304. else
  4305. expectednid = ffdhe_kexch_groups[idx];
  4306. /* Verify that we are changing what we expect. */
  4307. if (!TEST_int_ne(expectednid, kexch_alg))
  4308. goto end;
  4309. } else {
  4310. /* TLS 1.2 only supports named groups for ECDHE. */
  4311. if (isecdhe)
  4312. expectednid = kexch_alg;
  4313. else
  4314. expectednid = 0;
  4315. }
  4316. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4317. NULL, NULL))
  4318. || !TEST_true(SSL_set_session(clientssl, origsess))
  4319. || !TEST_true(set_ssl_groups(serverssl, clientssl, clientmulti,
  4320. isecdhe, idx)))
  4321. goto end;
  4322. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4323. || !TEST_true(SSL_session_reused(clientssl)))
  4324. goto end;
  4325. /* Check that we get what we expected */
  4326. if (!TEST_uint_eq(SSL_get_negotiated_group(clientssl), expectednid)
  4327. || !TEST_uint_eq(SSL_get_negotiated_group(serverssl), expectednid))
  4328. goto end;
  4329. testresult = 1;
  4330. end:
  4331. SSL_free(serverssl);
  4332. SSL_free(clientssl);
  4333. SSL_CTX_free(sctx);
  4334. SSL_CTX_free(cctx);
  4335. SSL_SESSION_free(origsess);
  4336. return testresult;
  4337. }
  4338. # endif /* !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH) */
  4339. /*
  4340. * Test TLSv1.3 Cipher Suite
  4341. * Test 0 = Set TLS1.3 cipher on context
  4342. * Test 1 = Set TLS1.3 cipher on SSL
  4343. * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
  4344. * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
  4345. */
  4346. static int test_tls13_ciphersuite(int idx)
  4347. {
  4348. SSL_CTX *sctx = NULL, *cctx = NULL;
  4349. SSL *serverssl = NULL, *clientssl = NULL;
  4350. static const struct {
  4351. const char *ciphername;
  4352. int fipscapable;
  4353. int low_security;
  4354. } t13_ciphers[] = {
  4355. { TLS1_3_RFC_AES_128_GCM_SHA256, 1, 0 },
  4356. { TLS1_3_RFC_AES_256_GCM_SHA384, 1, 0 },
  4357. { TLS1_3_RFC_AES_128_CCM_SHA256, 1, 0 },
  4358. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4359. { TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4360. { TLS1_3_RFC_AES_256_GCM_SHA384
  4361. ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256, 0, 0 },
  4362. # endif
  4363. /* CCM8 ciphers are considered low security due to their short tag */
  4364. { TLS1_3_RFC_AES_128_CCM_8_SHA256
  4365. ":" TLS1_3_RFC_AES_128_CCM_SHA256, 1, 1 }
  4366. };
  4367. const char *t13_cipher = NULL;
  4368. const char *t12_cipher = NULL;
  4369. const char *negotiated_scipher;
  4370. const char *negotiated_ccipher;
  4371. int set_at_ctx = 0;
  4372. int set_at_ssl = 0;
  4373. int testresult = 0;
  4374. int max_ver;
  4375. size_t i;
  4376. switch (idx) {
  4377. case 0:
  4378. set_at_ctx = 1;
  4379. break;
  4380. case 1:
  4381. set_at_ssl = 1;
  4382. break;
  4383. case 2:
  4384. set_at_ctx = 1;
  4385. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4386. break;
  4387. case 3:
  4388. set_at_ssl = 1;
  4389. t12_cipher = TLS1_TXT_RSA_WITH_AES_128_SHA256;
  4390. break;
  4391. }
  4392. for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
  4393. # ifdef OPENSSL_NO_TLS1_2
  4394. if (max_ver == TLS1_2_VERSION)
  4395. continue;
  4396. # endif
  4397. for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
  4398. if (is_fips && !t13_ciphers[i].fipscapable)
  4399. continue;
  4400. t13_cipher = t13_ciphers[i].ciphername;
  4401. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4402. TLS_client_method(),
  4403. TLS1_VERSION, max_ver,
  4404. &sctx, &cctx, cert, privkey)))
  4405. goto end;
  4406. if (t13_ciphers[i].low_security) {
  4407. SSL_CTX_set_security_level(sctx, 0);
  4408. SSL_CTX_set_security_level(cctx, 0);
  4409. }
  4410. if (set_at_ctx) {
  4411. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
  4412. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
  4413. goto end;
  4414. if (t12_cipher != NULL) {
  4415. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
  4416. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  4417. t12_cipher)))
  4418. goto end;
  4419. }
  4420. }
  4421. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4422. &clientssl, NULL, NULL)))
  4423. goto end;
  4424. if (set_at_ssl) {
  4425. if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
  4426. || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
  4427. goto end;
  4428. if (t12_cipher != NULL) {
  4429. if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
  4430. || !TEST_true(SSL_set_cipher_list(clientssl,
  4431. t12_cipher)))
  4432. goto end;
  4433. }
  4434. }
  4435. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4436. SSL_ERROR_NONE)))
  4437. goto end;
  4438. negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4439. serverssl));
  4440. negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  4441. clientssl));
  4442. if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
  4443. goto end;
  4444. /*
  4445. * TEST_strn_eq is used below because t13_cipher can contain
  4446. * multiple ciphersuites
  4447. */
  4448. if (max_ver == TLS1_3_VERSION
  4449. && !TEST_strn_eq(t13_cipher, negotiated_scipher,
  4450. strlen(negotiated_scipher)))
  4451. goto end;
  4452. # ifndef OPENSSL_NO_TLS1_2
  4453. /* Below validation is not done when t12_cipher is NULL */
  4454. if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
  4455. && !TEST_str_eq(t12_cipher, negotiated_scipher))
  4456. goto end;
  4457. # endif
  4458. SSL_free(serverssl);
  4459. serverssl = NULL;
  4460. SSL_free(clientssl);
  4461. clientssl = NULL;
  4462. SSL_CTX_free(sctx);
  4463. sctx = NULL;
  4464. SSL_CTX_free(cctx);
  4465. cctx = NULL;
  4466. }
  4467. }
  4468. testresult = 1;
  4469. end:
  4470. SSL_free(serverssl);
  4471. SSL_free(clientssl);
  4472. SSL_CTX_free(sctx);
  4473. SSL_CTX_free(cctx);
  4474. return testresult;
  4475. }
  4476. /*
  4477. * Test TLSv1.3 PSKs
  4478. * Test 0 = Test new style callbacks
  4479. * Test 1 = Test both new and old style callbacks
  4480. * Test 2 = Test old style callbacks
  4481. * Test 3 = Test old style callbacks with no certificate
  4482. */
  4483. static int test_tls13_psk(int idx)
  4484. {
  4485. SSL_CTX *sctx = NULL, *cctx = NULL;
  4486. SSL *serverssl = NULL, *clientssl = NULL;
  4487. const SSL_CIPHER *cipher = NULL;
  4488. const unsigned char key[] = {
  4489. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  4490. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  4491. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  4492. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  4493. };
  4494. int testresult = 0;
  4495. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4496. TLS_client_method(), TLS1_VERSION, 0,
  4497. &sctx, &cctx, idx == 3 ? NULL : cert,
  4498. idx == 3 ? NULL : privkey)))
  4499. goto end;
  4500. if (idx != 3) {
  4501. /*
  4502. * We use a ciphersuite with SHA256 to ease testing old style PSK
  4503. * callbacks which will always default to SHA256. This should not be
  4504. * necessary if we have no cert/priv key. In that case the server should
  4505. * prefer SHA256 automatically.
  4506. */
  4507. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4508. "TLS_AES_128_GCM_SHA256")))
  4509. goto end;
  4510. } else {
  4511. /*
  4512. * As noted above the server should prefer SHA256 automatically. However
  4513. * we are careful not to offer TLS_CHACHA20_POLY1305_SHA256 so this same
  4514. * code works even if we are testing with only the FIPS provider loaded.
  4515. */
  4516. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4517. "TLS_AES_256_GCM_SHA384:"
  4518. "TLS_AES_128_GCM_SHA256")))
  4519. goto end;
  4520. }
  4521. /*
  4522. * Test 0: New style callbacks only
  4523. * Test 1: New and old style callbacks (only the new ones should be used)
  4524. * Test 2: Old style callbacks only
  4525. */
  4526. if (idx == 0 || idx == 1) {
  4527. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  4528. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  4529. }
  4530. #ifndef OPENSSL_NO_PSK
  4531. if (idx >= 1) {
  4532. SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
  4533. SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
  4534. }
  4535. #endif
  4536. srvid = pskid;
  4537. use_session_cb_cnt = 0;
  4538. find_session_cb_cnt = 0;
  4539. psk_client_cb_cnt = 0;
  4540. psk_server_cb_cnt = 0;
  4541. if (idx != 3) {
  4542. /*
  4543. * Check we can create a connection if callback decides not to send a
  4544. * PSK
  4545. */
  4546. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4547. NULL, NULL))
  4548. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4549. SSL_ERROR_NONE))
  4550. || !TEST_false(SSL_session_reused(clientssl))
  4551. || !TEST_false(SSL_session_reused(serverssl)))
  4552. goto end;
  4553. if (idx == 0 || idx == 1) {
  4554. if (!TEST_true(use_session_cb_cnt == 1)
  4555. || !TEST_true(find_session_cb_cnt == 0)
  4556. /*
  4557. * If no old style callback then below should be 0
  4558. * otherwise 1
  4559. */
  4560. || !TEST_true(psk_client_cb_cnt == idx)
  4561. || !TEST_true(psk_server_cb_cnt == 0))
  4562. goto end;
  4563. } else {
  4564. if (!TEST_true(use_session_cb_cnt == 0)
  4565. || !TEST_true(find_session_cb_cnt == 0)
  4566. || !TEST_true(psk_client_cb_cnt == 1)
  4567. || !TEST_true(psk_server_cb_cnt == 0))
  4568. goto end;
  4569. }
  4570. shutdown_ssl_connection(serverssl, clientssl);
  4571. serverssl = clientssl = NULL;
  4572. use_session_cb_cnt = psk_client_cb_cnt = 0;
  4573. }
  4574. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4575. NULL, NULL)))
  4576. goto end;
  4577. /* Create the PSK */
  4578. cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
  4579. clientpsk = SSL_SESSION_new();
  4580. if (!TEST_ptr(clientpsk)
  4581. || !TEST_ptr(cipher)
  4582. || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
  4583. sizeof(key)))
  4584. || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
  4585. || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
  4586. TLS1_3_VERSION))
  4587. || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
  4588. goto end;
  4589. serverpsk = clientpsk;
  4590. /* Check we can create a connection and the PSK is used */
  4591. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4592. || !TEST_true(SSL_session_reused(clientssl))
  4593. || !TEST_true(SSL_session_reused(serverssl)))
  4594. goto end;
  4595. if (idx == 0 || idx == 1) {
  4596. if (!TEST_true(use_session_cb_cnt == 1)
  4597. || !TEST_true(find_session_cb_cnt == 1)
  4598. || !TEST_true(psk_client_cb_cnt == 0)
  4599. || !TEST_true(psk_server_cb_cnt == 0))
  4600. goto end;
  4601. } else {
  4602. if (!TEST_true(use_session_cb_cnt == 0)
  4603. || !TEST_true(find_session_cb_cnt == 0)
  4604. || !TEST_true(psk_client_cb_cnt == 1)
  4605. || !TEST_true(psk_server_cb_cnt == 1))
  4606. goto end;
  4607. }
  4608. shutdown_ssl_connection(serverssl, clientssl);
  4609. serverssl = clientssl = NULL;
  4610. use_session_cb_cnt = find_session_cb_cnt = 0;
  4611. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4612. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4613. NULL, NULL)))
  4614. goto end;
  4615. /* Force an HRR */
  4616. #if defined(OPENSSL_NO_EC)
  4617. if (!TEST_true(SSL_set1_groups_list(serverssl, "ffdhe3072")))
  4618. goto end;
  4619. #else
  4620. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  4621. goto end;
  4622. #endif
  4623. /*
  4624. * Check we can create a connection, the PSK is used and the callbacks are
  4625. * called twice.
  4626. */
  4627. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  4628. || !TEST_true(SSL_session_reused(clientssl))
  4629. || !TEST_true(SSL_session_reused(serverssl)))
  4630. goto end;
  4631. if (idx == 0 || idx == 1) {
  4632. if (!TEST_true(use_session_cb_cnt == 2)
  4633. || !TEST_true(find_session_cb_cnt == 2)
  4634. || !TEST_true(psk_client_cb_cnt == 0)
  4635. || !TEST_true(psk_server_cb_cnt == 0))
  4636. goto end;
  4637. } else {
  4638. if (!TEST_true(use_session_cb_cnt == 0)
  4639. || !TEST_true(find_session_cb_cnt == 0)
  4640. || !TEST_true(psk_client_cb_cnt == 2)
  4641. || !TEST_true(psk_server_cb_cnt == 2))
  4642. goto end;
  4643. }
  4644. shutdown_ssl_connection(serverssl, clientssl);
  4645. serverssl = clientssl = NULL;
  4646. use_session_cb_cnt = find_session_cb_cnt = 0;
  4647. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  4648. if (idx != 3) {
  4649. /*
  4650. * Check that if the server rejects the PSK we can still connect, but with
  4651. * a full handshake
  4652. */
  4653. srvid = "Dummy Identity";
  4654. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4655. NULL, NULL))
  4656. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4657. SSL_ERROR_NONE))
  4658. || !TEST_false(SSL_session_reused(clientssl))
  4659. || !TEST_false(SSL_session_reused(serverssl)))
  4660. goto end;
  4661. if (idx == 0 || idx == 1) {
  4662. if (!TEST_true(use_session_cb_cnt == 1)
  4663. || !TEST_true(find_session_cb_cnt == 1)
  4664. || !TEST_true(psk_client_cb_cnt == 0)
  4665. /*
  4666. * If no old style callback then below should be 0
  4667. * otherwise 1
  4668. */
  4669. || !TEST_true(psk_server_cb_cnt == idx))
  4670. goto end;
  4671. } else {
  4672. if (!TEST_true(use_session_cb_cnt == 0)
  4673. || !TEST_true(find_session_cb_cnt == 0)
  4674. || !TEST_true(psk_client_cb_cnt == 1)
  4675. || !TEST_true(psk_server_cb_cnt == 1))
  4676. goto end;
  4677. }
  4678. shutdown_ssl_connection(serverssl, clientssl);
  4679. serverssl = clientssl = NULL;
  4680. }
  4681. testresult = 1;
  4682. end:
  4683. SSL_SESSION_free(clientpsk);
  4684. SSL_SESSION_free(serverpsk);
  4685. clientpsk = serverpsk = NULL;
  4686. SSL_free(serverssl);
  4687. SSL_free(clientssl);
  4688. SSL_CTX_free(sctx);
  4689. SSL_CTX_free(cctx);
  4690. return testresult;
  4691. }
  4692. static unsigned char cookie_magic_value[] = "cookie magic";
  4693. static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  4694. unsigned int *cookie_len)
  4695. {
  4696. /*
  4697. * Not suitable as a real cookie generation function but good enough for
  4698. * testing!
  4699. */
  4700. memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
  4701. *cookie_len = sizeof(cookie_magic_value) - 1;
  4702. return 1;
  4703. }
  4704. static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  4705. unsigned int cookie_len)
  4706. {
  4707. if (cookie_len == sizeof(cookie_magic_value) - 1
  4708. && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
  4709. return 1;
  4710. return 0;
  4711. }
  4712. static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  4713. size_t *cookie_len)
  4714. {
  4715. unsigned int temp;
  4716. int res = generate_cookie_callback(ssl, cookie, &temp);
  4717. *cookie_len = temp;
  4718. return res;
  4719. }
  4720. static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  4721. size_t cookie_len)
  4722. {
  4723. return verify_cookie_callback(ssl, cookie, cookie_len);
  4724. }
  4725. static int test_stateless(void)
  4726. {
  4727. SSL_CTX *sctx = NULL, *cctx = NULL;
  4728. SSL *serverssl = NULL, *clientssl = NULL;
  4729. int testresult = 0;
  4730. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4731. TLS_client_method(), TLS1_VERSION, 0,
  4732. &sctx, &cctx, cert, privkey)))
  4733. goto end;
  4734. /* The arrival of CCS messages can confuse the test */
  4735. SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  4736. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4737. NULL, NULL))
  4738. /* Send the first ClientHello */
  4739. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4740. SSL_ERROR_WANT_READ))
  4741. /*
  4742. * This should fail with a -1 return because we have no callbacks
  4743. * set up
  4744. */
  4745. || !TEST_int_eq(SSL_stateless(serverssl), -1))
  4746. goto end;
  4747. /* Fatal error so abandon the connection from this client */
  4748. SSL_free(clientssl);
  4749. clientssl = NULL;
  4750. /* Set up the cookie generation and verification callbacks */
  4751. SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
  4752. SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
  4753. /*
  4754. * Create a new connection from the client (we can reuse the server SSL
  4755. * object).
  4756. */
  4757. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4758. NULL, NULL))
  4759. /* Send the first ClientHello */
  4760. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4761. SSL_ERROR_WANT_READ))
  4762. /* This should fail because there is no cookie */
  4763. || !TEST_int_eq(SSL_stateless(serverssl), 0))
  4764. goto end;
  4765. /* Abandon the connection from this client */
  4766. SSL_free(clientssl);
  4767. clientssl = NULL;
  4768. /*
  4769. * Now create a connection from a new client but with the same server SSL
  4770. * object
  4771. */
  4772. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4773. NULL, NULL))
  4774. /* Send the first ClientHello */
  4775. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4776. SSL_ERROR_WANT_READ))
  4777. /* This should fail because there is no cookie */
  4778. || !TEST_int_eq(SSL_stateless(serverssl), 0)
  4779. /* Send the second ClientHello */
  4780. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  4781. SSL_ERROR_WANT_READ))
  4782. /* This should succeed because a cookie is now present */
  4783. || !TEST_int_eq(SSL_stateless(serverssl), 1)
  4784. /* Complete the connection */
  4785. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4786. SSL_ERROR_NONE)))
  4787. goto end;
  4788. shutdown_ssl_connection(serverssl, clientssl);
  4789. serverssl = clientssl = NULL;
  4790. testresult = 1;
  4791. end:
  4792. SSL_free(serverssl);
  4793. SSL_free(clientssl);
  4794. SSL_CTX_free(sctx);
  4795. SSL_CTX_free(cctx);
  4796. return testresult;
  4797. }
  4798. #endif /* OSSL_NO_USABLE_TLS1_3 */
  4799. static int clntaddoldcb = 0;
  4800. static int clntparseoldcb = 0;
  4801. static int srvaddoldcb = 0;
  4802. static int srvparseoldcb = 0;
  4803. static int clntaddnewcb = 0;
  4804. static int clntparsenewcb = 0;
  4805. static int srvaddnewcb = 0;
  4806. static int srvparsenewcb = 0;
  4807. static int snicb = 0;
  4808. #define TEST_EXT_TYPE1 0xff00
  4809. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  4810. size_t *outlen, int *al, void *add_arg)
  4811. {
  4812. int *server = (int *)add_arg;
  4813. unsigned char *data;
  4814. if (SSL_is_server(s))
  4815. srvaddoldcb++;
  4816. else
  4817. clntaddoldcb++;
  4818. if (*server != SSL_is_server(s)
  4819. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  4820. return -1;
  4821. *data = 1;
  4822. *out = data;
  4823. *outlen = sizeof(char);
  4824. return 1;
  4825. }
  4826. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  4827. void *add_arg)
  4828. {
  4829. OPENSSL_free((unsigned char *)out);
  4830. }
  4831. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  4832. size_t inlen, int *al, void *parse_arg)
  4833. {
  4834. int *server = (int *)parse_arg;
  4835. if (SSL_is_server(s))
  4836. srvparseoldcb++;
  4837. else
  4838. clntparseoldcb++;
  4839. if (*server != SSL_is_server(s)
  4840. || inlen != sizeof(char)
  4841. || *in != 1)
  4842. return -1;
  4843. return 1;
  4844. }
  4845. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4846. const unsigned char **out, size_t *outlen, X509 *x,
  4847. size_t chainidx, int *al, void *add_arg)
  4848. {
  4849. int *server = (int *)add_arg;
  4850. unsigned char *data;
  4851. if (SSL_is_server(s))
  4852. srvaddnewcb++;
  4853. else
  4854. clntaddnewcb++;
  4855. if (*server != SSL_is_server(s)
  4856. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  4857. return -1;
  4858. *data = 1;
  4859. *out = data;
  4860. *outlen = sizeof(*data);
  4861. return 1;
  4862. }
  4863. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4864. const unsigned char *out, void *add_arg)
  4865. {
  4866. OPENSSL_free((unsigned char *)out);
  4867. }
  4868. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  4869. const unsigned char *in, size_t inlen, X509 *x,
  4870. size_t chainidx, int *al, void *parse_arg)
  4871. {
  4872. int *server = (int *)parse_arg;
  4873. if (SSL_is_server(s))
  4874. srvparsenewcb++;
  4875. else
  4876. clntparsenewcb++;
  4877. if (*server != SSL_is_server(s)
  4878. || inlen != sizeof(char) || *in != 1)
  4879. return -1;
  4880. return 1;
  4881. }
  4882. static int sni_cb(SSL *s, int *al, void *arg)
  4883. {
  4884. SSL_CTX *ctx = (SSL_CTX *)arg;
  4885. if (SSL_set_SSL_CTX(s, ctx) == NULL) {
  4886. *al = SSL_AD_INTERNAL_ERROR;
  4887. return SSL_TLSEXT_ERR_ALERT_FATAL;
  4888. }
  4889. snicb++;
  4890. return SSL_TLSEXT_ERR_OK;
  4891. }
  4892. static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  4893. {
  4894. return 1;
  4895. }
  4896. /*
  4897. * Custom call back tests.
  4898. * Test 0: Old style callbacks in TLSv1.2
  4899. * Test 1: New style callbacks in TLSv1.2
  4900. * Test 2: New style callbacks in TLSv1.2 with SNI
  4901. * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
  4902. * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  4903. * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
  4904. */
  4905. static int test_custom_exts(int tst)
  4906. {
  4907. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  4908. SSL *clientssl = NULL, *serverssl = NULL;
  4909. int testresult = 0;
  4910. static int server = 1;
  4911. static int client = 0;
  4912. SSL_SESSION *sess = NULL;
  4913. unsigned int context;
  4914. #if defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  4915. /* Skip tests for TLSv1.2 and below in this case */
  4916. if (tst < 3)
  4917. return 1;
  4918. #endif
  4919. /* Reset callback counters */
  4920. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  4921. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  4922. snicb = 0;
  4923. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  4924. TLS_client_method(), TLS1_VERSION, 0,
  4925. &sctx, &cctx, cert, privkey)))
  4926. goto end;
  4927. if (tst == 2
  4928. && !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(), NULL,
  4929. TLS1_VERSION, 0,
  4930. &sctx2, NULL, cert, privkey)))
  4931. goto end;
  4932. if (tst < 3) {
  4933. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  4934. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  4935. if (sctx2 != NULL)
  4936. SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
  4937. }
  4938. if (tst == 5) {
  4939. context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  4940. | SSL_EXT_TLS1_3_CERTIFICATE;
  4941. SSL_CTX_set_verify(sctx,
  4942. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  4943. verify_cb);
  4944. if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
  4945. SSL_FILETYPE_PEM), 1)
  4946. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
  4947. SSL_FILETYPE_PEM), 1)
  4948. || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
  4949. goto end;
  4950. } else if (tst == 4) {
  4951. context = SSL_EXT_CLIENT_HELLO
  4952. | SSL_EXT_TLS1_2_SERVER_HELLO
  4953. | SSL_EXT_TLS1_3_SERVER_HELLO
  4954. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  4955. | SSL_EXT_TLS1_3_CERTIFICATE
  4956. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  4957. } else {
  4958. context = SSL_EXT_CLIENT_HELLO
  4959. | SSL_EXT_TLS1_2_SERVER_HELLO
  4960. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  4961. }
  4962. /* Create a client side custom extension */
  4963. if (tst == 0) {
  4964. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  4965. old_add_cb, old_free_cb,
  4966. &client, old_parse_cb,
  4967. &client)))
  4968. goto end;
  4969. } else {
  4970. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  4971. new_add_cb, new_free_cb,
  4972. &client, new_parse_cb, &client)))
  4973. goto end;
  4974. }
  4975. /* Should not be able to add duplicates */
  4976. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  4977. old_add_cb, old_free_cb,
  4978. &client, old_parse_cb,
  4979. &client))
  4980. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  4981. context, new_add_cb,
  4982. new_free_cb, &client,
  4983. new_parse_cb, &client)))
  4984. goto end;
  4985. /* Create a server side custom extension */
  4986. if (tst == 0) {
  4987. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  4988. old_add_cb, old_free_cb,
  4989. &server, old_parse_cb,
  4990. &server)))
  4991. goto end;
  4992. } else {
  4993. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  4994. new_add_cb, new_free_cb,
  4995. &server, new_parse_cb, &server)))
  4996. goto end;
  4997. if (sctx2 != NULL
  4998. && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
  4999. context, new_add_cb,
  5000. new_free_cb, &server,
  5001. new_parse_cb, &server)))
  5002. goto end;
  5003. }
  5004. /* Should not be able to add duplicates */
  5005. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  5006. old_add_cb, old_free_cb,
  5007. &server, old_parse_cb,
  5008. &server))
  5009. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  5010. context, new_add_cb,
  5011. new_free_cb, &server,
  5012. new_parse_cb, &server)))
  5013. goto end;
  5014. if (tst == 2) {
  5015. /* Set up SNI */
  5016. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  5017. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  5018. goto end;
  5019. }
  5020. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5021. &clientssl, NULL, NULL))
  5022. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5023. SSL_ERROR_NONE)))
  5024. goto end;
  5025. if (tst == 0) {
  5026. if (clntaddoldcb != 1
  5027. || clntparseoldcb != 1
  5028. || srvaddoldcb != 1
  5029. || srvparseoldcb != 1)
  5030. goto end;
  5031. } else if (tst == 1 || tst == 2 || tst == 3) {
  5032. if (clntaddnewcb != 1
  5033. || clntparsenewcb != 1
  5034. || srvaddnewcb != 1
  5035. || srvparsenewcb != 1
  5036. || (tst != 2 && snicb != 0)
  5037. || (tst == 2 && snicb != 1))
  5038. goto end;
  5039. } else if (tst == 5) {
  5040. if (clntaddnewcb != 1
  5041. || clntparsenewcb != 1
  5042. || srvaddnewcb != 1
  5043. || srvparsenewcb != 1)
  5044. goto end;
  5045. } else {
  5046. /* In this case there 2 NewSessionTicket messages created */
  5047. if (clntaddnewcb != 1
  5048. || clntparsenewcb != 5
  5049. || srvaddnewcb != 5
  5050. || srvparsenewcb != 1)
  5051. goto end;
  5052. }
  5053. sess = SSL_get1_session(clientssl);
  5054. SSL_shutdown(clientssl);
  5055. SSL_shutdown(serverssl);
  5056. SSL_free(serverssl);
  5057. SSL_free(clientssl);
  5058. serverssl = clientssl = NULL;
  5059. if (tst == 3 || tst == 5) {
  5060. /* We don't bother with the resumption aspects for these tests */
  5061. testresult = 1;
  5062. goto end;
  5063. }
  5064. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5065. NULL, NULL))
  5066. || !TEST_true(SSL_set_session(clientssl, sess))
  5067. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5068. SSL_ERROR_NONE)))
  5069. goto end;
  5070. /*
  5071. * For a resumed session we expect to add the ClientHello extension. For the
  5072. * old style callbacks we ignore it on the server side because they set
  5073. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  5074. * them.
  5075. */
  5076. if (tst == 0) {
  5077. if (clntaddoldcb != 2
  5078. || clntparseoldcb != 1
  5079. || srvaddoldcb != 1
  5080. || srvparseoldcb != 1)
  5081. goto end;
  5082. } else if (tst == 1 || tst == 2 || tst == 3) {
  5083. if (clntaddnewcb != 2
  5084. || clntparsenewcb != 2
  5085. || srvaddnewcb != 2
  5086. || srvparsenewcb != 2)
  5087. goto end;
  5088. } else {
  5089. /*
  5090. * No Certificate message extensions in the resumption handshake,
  5091. * 2 NewSessionTickets in the initial handshake, 1 in the resumption
  5092. */
  5093. if (clntaddnewcb != 2
  5094. || clntparsenewcb != 8
  5095. || srvaddnewcb != 8
  5096. || srvparsenewcb != 2)
  5097. goto end;
  5098. }
  5099. testresult = 1;
  5100. end:
  5101. SSL_SESSION_free(sess);
  5102. SSL_free(serverssl);
  5103. SSL_free(clientssl);
  5104. SSL_CTX_free(sctx2);
  5105. SSL_CTX_free(sctx);
  5106. SSL_CTX_free(cctx);
  5107. return testresult;
  5108. }
  5109. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  5110. #define SYNTHV1CONTEXT (SSL_EXT_TLS1_2_AND_BELOW_ONLY \
  5111. | SSL_EXT_CLIENT_HELLO \
  5112. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5113. | SSL_EXT_IGNORE_ON_RESUMPTION)
  5114. #define TLS13CONTEXT (SSL_EXT_TLS1_3_CERTIFICATE \
  5115. | SSL_EXT_TLS1_2_SERVER_HELLO \
  5116. | SSL_EXT_CLIENT_HELLO)
  5117. #define SERVERINFO_CUSTOM \
  5118. 0x00, (char)TLSEXT_TYPE_signed_certificate_timestamp, \
  5119. 0x00, 0x03, \
  5120. 0x04, 0x05, 0x06 \
  5121. static const unsigned char serverinfo_custom_tls13[] = {
  5122. 0x00, 0x00, (TLS13CONTEXT >> 8) & 0xff, TLS13CONTEXT & 0xff,
  5123. SERVERINFO_CUSTOM
  5124. };
  5125. static const unsigned char serverinfo_custom_v2[] = {
  5126. 0x00, 0x00, (SYNTHV1CONTEXT >> 8) & 0xff, SYNTHV1CONTEXT & 0xff,
  5127. SERVERINFO_CUSTOM
  5128. };
  5129. static const unsigned char serverinfo_custom_v1[] = {
  5130. SERVERINFO_CUSTOM
  5131. };
  5132. static const size_t serverinfo_custom_tls13_len = sizeof(serverinfo_custom_tls13);
  5133. static const size_t serverinfo_custom_v2_len = sizeof(serverinfo_custom_v2);
  5134. static const size_t serverinfo_custom_v1_len = sizeof(serverinfo_custom_v1);
  5135. static int serverinfo_custom_parse_cb(SSL *s, unsigned int ext_type,
  5136. unsigned int context,
  5137. const unsigned char *in,
  5138. size_t inlen, X509 *x,
  5139. size_t chainidx, int *al,
  5140. void *parse_arg)
  5141. {
  5142. const size_t len = serverinfo_custom_v1_len;
  5143. const unsigned char *si = &serverinfo_custom_v1[len - 3];
  5144. int *p_cb_result = (int*)parse_arg;
  5145. *p_cb_result = TEST_mem_eq(in, inlen, si, 3);
  5146. return 1;
  5147. }
  5148. static int test_serverinfo_custom(const int idx)
  5149. {
  5150. SSL_CTX *sctx = NULL, *cctx = NULL;
  5151. SSL *clientssl = NULL, *serverssl = NULL;
  5152. int testresult = 0;
  5153. int cb_result = 0;
  5154. /*
  5155. * Following variables are set in the switch statement
  5156. * according to the test iteration.
  5157. * Default values do not make much sense: test would fail with them.
  5158. */
  5159. int serverinfo_version = 0;
  5160. int protocol_version = 0;
  5161. unsigned int extension_context = 0;
  5162. const unsigned char *si = NULL;
  5163. size_t si_len = 0;
  5164. const int call_use_serverinfo_ex = idx > 0;
  5165. switch (idx) {
  5166. case 0: /* FALLTHROUGH */
  5167. case 1:
  5168. serverinfo_version = SSL_SERVERINFOV1;
  5169. protocol_version = TLS1_2_VERSION;
  5170. extension_context = SYNTHV1CONTEXT;
  5171. si = serverinfo_custom_v1;
  5172. si_len = serverinfo_custom_v1_len;
  5173. break;
  5174. case 2:
  5175. serverinfo_version = SSL_SERVERINFOV2;
  5176. protocol_version = TLS1_2_VERSION;
  5177. extension_context = SYNTHV1CONTEXT;
  5178. si = serverinfo_custom_v2;
  5179. si_len = serverinfo_custom_v2_len;
  5180. break;
  5181. case 3:
  5182. serverinfo_version = SSL_SERVERINFOV2;
  5183. protocol_version = TLS1_3_VERSION;
  5184. extension_context = TLS13CONTEXT;
  5185. si = serverinfo_custom_tls13;
  5186. si_len = serverinfo_custom_tls13_len;
  5187. break;
  5188. }
  5189. if (!TEST_true(create_ssl_ctx_pair(libctx,
  5190. TLS_method(),
  5191. TLS_method(),
  5192. protocol_version,
  5193. protocol_version,
  5194. &sctx, &cctx, cert, privkey)))
  5195. goto end;
  5196. if (call_use_serverinfo_ex) {
  5197. if (!TEST_true(SSL_CTX_use_serverinfo_ex(sctx, serverinfo_version,
  5198. si, si_len)))
  5199. goto end;
  5200. } else {
  5201. if (!TEST_true(SSL_CTX_use_serverinfo(sctx, si, si_len)))
  5202. goto end;
  5203. }
  5204. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TLSEXT_TYPE_signed_certificate_timestamp,
  5205. extension_context,
  5206. NULL, NULL, NULL,
  5207. serverinfo_custom_parse_cb,
  5208. &cb_result))
  5209. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5210. NULL, NULL))
  5211. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5212. SSL_ERROR_NONE))
  5213. || !TEST_int_eq(SSL_do_handshake(clientssl), 1))
  5214. goto end;
  5215. if (!TEST_true(cb_result))
  5216. goto end;
  5217. testresult = 1;
  5218. end:
  5219. SSL_free(serverssl);
  5220. SSL_free(clientssl);
  5221. SSL_CTX_free(sctx);
  5222. SSL_CTX_free(cctx);
  5223. return testresult;
  5224. }
  5225. #endif
  5226. /*
  5227. * Test that SSL_export_keying_material() produces expected results. There are
  5228. * no test vectors so all we do is test that both sides of the communication
  5229. * produce the same results for different protocol versions.
  5230. */
  5231. #define SMALL_LABEL_LEN 10
  5232. #define LONG_LABEL_LEN 249
  5233. static int test_export_key_mat(int tst)
  5234. {
  5235. int testresult = 0;
  5236. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5237. SSL *clientssl = NULL, *serverssl = NULL;
  5238. const char label[LONG_LABEL_LEN + 1] = "test label";
  5239. const unsigned char context[] = "context";
  5240. const unsigned char *emptycontext = NULL;
  5241. unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
  5242. unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
  5243. size_t labellen;
  5244. const int protocols[] = {
  5245. TLS1_VERSION,
  5246. TLS1_1_VERSION,
  5247. TLS1_2_VERSION,
  5248. TLS1_3_VERSION,
  5249. TLS1_3_VERSION,
  5250. TLS1_3_VERSION
  5251. };
  5252. #ifdef OPENSSL_NO_TLS1
  5253. if (tst == 0)
  5254. return 1;
  5255. #endif
  5256. #ifdef OPENSSL_NO_TLS1_1
  5257. if (tst == 1)
  5258. return 1;
  5259. #endif
  5260. if (is_fips && (tst == 0 || tst == 1))
  5261. return 1;
  5262. #ifdef OPENSSL_NO_TLS1_2
  5263. if (tst == 2)
  5264. return 1;
  5265. #endif
  5266. #ifdef OSSL_NO_USABLE_TLS1_3
  5267. if (tst >= 3)
  5268. return 1;
  5269. #endif
  5270. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5271. TLS_client_method(), TLS1_VERSION, 0,
  5272. &sctx, &cctx, cert, privkey)))
  5273. goto end;
  5274. OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
  5275. SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
  5276. SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
  5277. if ((protocols[tst] < TLS1_2_VERSION) &&
  5278. (!SSL_CTX_set_cipher_list(cctx, "DEFAULT:@SECLEVEL=0")
  5279. || !SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0")))
  5280. goto end;
  5281. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5282. NULL)))
  5283. goto end;
  5284. /*
  5285. * Premature call of SSL_export_keying_material should just fail.
  5286. */
  5287. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5288. sizeof(ckeymat1), label,
  5289. SMALL_LABEL_LEN + 1, context,
  5290. sizeof(context) - 1, 1), 0))
  5291. goto end;
  5292. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5293. SSL_ERROR_NONE)))
  5294. goto end;
  5295. if (tst == 5) {
  5296. /*
  5297. * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
  5298. * go over that.
  5299. */
  5300. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  5301. sizeof(ckeymat1), label,
  5302. LONG_LABEL_LEN + 1, context,
  5303. sizeof(context) - 1, 1), 0))
  5304. goto end;
  5305. testresult = 1;
  5306. goto end;
  5307. } else if (tst == 4) {
  5308. labellen = LONG_LABEL_LEN;
  5309. } else {
  5310. labellen = SMALL_LABEL_LEN;
  5311. }
  5312. if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
  5313. sizeof(ckeymat1), label,
  5314. labellen, context,
  5315. sizeof(context) - 1, 1), 1)
  5316. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
  5317. sizeof(ckeymat2), label,
  5318. labellen,
  5319. emptycontext,
  5320. 0, 1), 1)
  5321. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
  5322. sizeof(ckeymat3), label,
  5323. labellen,
  5324. NULL, 0, 0), 1)
  5325. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
  5326. sizeof(skeymat1), label,
  5327. labellen,
  5328. context,
  5329. sizeof(context) -1, 1),
  5330. 1)
  5331. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
  5332. sizeof(skeymat2), label,
  5333. labellen,
  5334. emptycontext,
  5335. 0, 1), 1)
  5336. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
  5337. sizeof(skeymat3), label,
  5338. labellen,
  5339. NULL, 0, 0), 1)
  5340. /*
  5341. * Check that both sides created the same key material with the
  5342. * same context.
  5343. */
  5344. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5345. sizeof(skeymat1))
  5346. /*
  5347. * Check that both sides created the same key material with an
  5348. * empty context.
  5349. */
  5350. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5351. sizeof(skeymat2))
  5352. /*
  5353. * Check that both sides created the same key material without a
  5354. * context.
  5355. */
  5356. || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
  5357. sizeof(skeymat3))
  5358. /* Different contexts should produce different results */
  5359. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5360. sizeof(ckeymat2)))
  5361. goto end;
  5362. /*
  5363. * Check that an empty context and no context produce different results in
  5364. * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
  5365. */
  5366. if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5367. sizeof(ckeymat3)))
  5368. || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
  5369. sizeof(ckeymat3))))
  5370. goto end;
  5371. testresult = 1;
  5372. end:
  5373. SSL_free(serverssl);
  5374. SSL_free(clientssl);
  5375. SSL_CTX_free(sctx2);
  5376. SSL_CTX_free(sctx);
  5377. SSL_CTX_free(cctx);
  5378. return testresult;
  5379. }
  5380. #ifndef OSSL_NO_USABLE_TLS1_3
  5381. /*
  5382. * Test that SSL_export_keying_material_early() produces expected
  5383. * results. There are no test vectors so all we do is test that both
  5384. * sides of the communication produce the same results for different
  5385. * protocol versions.
  5386. */
  5387. static int test_export_key_mat_early(int idx)
  5388. {
  5389. static const char label[] = "test label";
  5390. static const unsigned char context[] = "context";
  5391. int testresult = 0;
  5392. SSL_CTX *cctx = NULL, *sctx = NULL;
  5393. SSL *clientssl = NULL, *serverssl = NULL;
  5394. SSL_SESSION *sess = NULL;
  5395. const unsigned char *emptycontext = NULL;
  5396. unsigned char ckeymat1[80], ckeymat2[80];
  5397. unsigned char skeymat1[80], skeymat2[80];
  5398. unsigned char buf[1];
  5399. size_t readbytes, written;
  5400. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
  5401. &sess, idx)))
  5402. goto end;
  5403. /* Here writing 0 length early data is enough. */
  5404. if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  5405. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  5406. &readbytes),
  5407. SSL_READ_EARLY_DATA_ERROR)
  5408. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  5409. SSL_EARLY_DATA_ACCEPTED))
  5410. goto end;
  5411. if (!TEST_int_eq(SSL_export_keying_material_early(
  5412. clientssl, ckeymat1, sizeof(ckeymat1), label,
  5413. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5414. || !TEST_int_eq(SSL_export_keying_material_early(
  5415. clientssl, ckeymat2, sizeof(ckeymat2), label,
  5416. sizeof(label) - 1, emptycontext, 0), 1)
  5417. || !TEST_int_eq(SSL_export_keying_material_early(
  5418. serverssl, skeymat1, sizeof(skeymat1), label,
  5419. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  5420. || !TEST_int_eq(SSL_export_keying_material_early(
  5421. serverssl, skeymat2, sizeof(skeymat2), label,
  5422. sizeof(label) - 1, emptycontext, 0), 1)
  5423. /*
  5424. * Check that both sides created the same key material with the
  5425. * same context.
  5426. */
  5427. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  5428. sizeof(skeymat1))
  5429. /*
  5430. * Check that both sides created the same key material with an
  5431. * empty context.
  5432. */
  5433. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  5434. sizeof(skeymat2))
  5435. /* Different contexts should produce different results */
  5436. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  5437. sizeof(ckeymat2)))
  5438. goto end;
  5439. testresult = 1;
  5440. end:
  5441. SSL_SESSION_free(sess);
  5442. SSL_SESSION_free(clientpsk);
  5443. SSL_SESSION_free(serverpsk);
  5444. clientpsk = serverpsk = NULL;
  5445. SSL_free(serverssl);
  5446. SSL_free(clientssl);
  5447. SSL_CTX_free(sctx);
  5448. SSL_CTX_free(cctx);
  5449. return testresult;
  5450. }
  5451. #define NUM_KEY_UPDATE_MESSAGES 40
  5452. /*
  5453. * Test KeyUpdate.
  5454. */
  5455. static int test_key_update(void)
  5456. {
  5457. SSL_CTX *cctx = NULL, *sctx = NULL;
  5458. SSL *clientssl = NULL, *serverssl = NULL;
  5459. int testresult = 0, i, j;
  5460. char buf[20];
  5461. static char *mess = "A test message";
  5462. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5463. TLS_client_method(),
  5464. TLS1_3_VERSION,
  5465. 0,
  5466. &sctx, &cctx, cert, privkey))
  5467. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5468. NULL, NULL))
  5469. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5470. SSL_ERROR_NONE)))
  5471. goto end;
  5472. for (j = 0; j < 2; j++) {
  5473. /* Send lots of KeyUpdate messages */
  5474. for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
  5475. if (!TEST_true(SSL_key_update(clientssl,
  5476. (j == 0)
  5477. ? SSL_KEY_UPDATE_NOT_REQUESTED
  5478. : SSL_KEY_UPDATE_REQUESTED))
  5479. || !TEST_true(SSL_do_handshake(clientssl)))
  5480. goto end;
  5481. }
  5482. /* Check that sending and receiving app data is ok */
  5483. if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
  5484. || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
  5485. strlen(mess)))
  5486. goto end;
  5487. if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
  5488. || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
  5489. strlen(mess)))
  5490. goto end;
  5491. }
  5492. testresult = 1;
  5493. end:
  5494. SSL_free(serverssl);
  5495. SSL_free(clientssl);
  5496. SSL_CTX_free(sctx);
  5497. SSL_CTX_free(cctx);
  5498. return testresult;
  5499. }
  5500. /*
  5501. * Test we can handle a KeyUpdate (update requested) message while
  5502. * write data is pending in peer.
  5503. * Test 0: Client sends KeyUpdate while Server is writing
  5504. * Test 1: Server sends KeyUpdate while Client is writing
  5505. */
  5506. static int test_key_update_peer_in_write(int tst)
  5507. {
  5508. SSL_CTX *cctx = NULL, *sctx = NULL;
  5509. SSL *clientssl = NULL, *serverssl = NULL;
  5510. int testresult = 0;
  5511. char buf[20];
  5512. static char *mess = "A test message";
  5513. BIO *bretry = BIO_new(bio_s_always_retry());
  5514. BIO *tmp = NULL;
  5515. SSL *peerupdate = NULL, *peerwrite = NULL;
  5516. if (!TEST_ptr(bretry)
  5517. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5518. TLS_client_method(),
  5519. TLS1_3_VERSION,
  5520. 0,
  5521. &sctx, &cctx, cert, privkey))
  5522. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5523. NULL, NULL))
  5524. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5525. SSL_ERROR_NONE)))
  5526. goto end;
  5527. peerupdate = tst == 0 ? clientssl : serverssl;
  5528. peerwrite = tst == 0 ? serverssl : clientssl;
  5529. if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
  5530. || !TEST_int_eq(SSL_do_handshake(peerupdate), 1))
  5531. goto end;
  5532. /* Swap the writing endpoint's write BIO to force a retry */
  5533. tmp = SSL_get_wbio(peerwrite);
  5534. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  5535. tmp = NULL;
  5536. goto end;
  5537. }
  5538. SSL_set0_wbio(peerwrite, bretry);
  5539. bretry = NULL;
  5540. /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
  5541. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
  5542. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
  5543. goto end;
  5544. /* Reinstate the original writing endpoint's write BIO */
  5545. SSL_set0_wbio(peerwrite, tmp);
  5546. tmp = NULL;
  5547. /* Now read some data - we will read the key update */
  5548. if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
  5549. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
  5550. goto end;
  5551. /*
  5552. * Complete the write we started previously and read it from the other
  5553. * endpoint
  5554. */
  5555. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5556. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5557. goto end;
  5558. /* Write more data to ensure we send the KeyUpdate message back */
  5559. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  5560. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  5561. goto end;
  5562. testresult = 1;
  5563. end:
  5564. SSL_free(serverssl);
  5565. SSL_free(clientssl);
  5566. SSL_CTX_free(sctx);
  5567. SSL_CTX_free(cctx);
  5568. BIO_free(bretry);
  5569. BIO_free(tmp);
  5570. return testresult;
  5571. }
  5572. /*
  5573. * Test we can handle a KeyUpdate (update requested) message while
  5574. * peer read data is pending after peer accepted keyupdate(the msg header
  5575. * had been read 5 bytes).
  5576. * Test 0: Client sends KeyUpdate while Server is reading
  5577. * Test 1: Server sends KeyUpdate while Client is reading
  5578. */
  5579. static int test_key_update_peer_in_read(int tst)
  5580. {
  5581. SSL_CTX *cctx = NULL, *sctx = NULL;
  5582. SSL *clientssl = NULL, *serverssl = NULL;
  5583. int testresult = 0;
  5584. char prbuf[515], lwbuf[515] = {0};
  5585. static char *mess = "A test message";
  5586. BIO *lbio = NULL, *pbio = NULL;
  5587. SSL *local = NULL, *peer = NULL;
  5588. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5589. TLS_client_method(),
  5590. TLS1_3_VERSION,
  5591. 0,
  5592. &sctx, &cctx, cert, privkey))
  5593. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5594. NULL, NULL))
  5595. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5596. SSL_ERROR_NONE)))
  5597. goto end;
  5598. local = tst == 0 ? clientssl : serverssl;
  5599. peer = tst == 0 ? serverssl : clientssl;
  5600. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  5601. goto end;
  5602. SSL_set_bio(local, lbio, lbio);
  5603. SSL_set_bio(peer, pbio, pbio);
  5604. /*
  5605. * we first write keyupdate msg then appdata in local
  5606. * write data in local will fail with SSL_ERROR_WANT_WRITE,because
  5607. * lwbuf app data msg size + key updata msg size > 512(the size of
  5608. * the bio pair buffer)
  5609. */
  5610. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  5611. || !TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), -1)
  5612. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  5613. goto end;
  5614. /*
  5615. * first read keyupdate msg in peer in peer
  5616. * then read appdata that we know will fail with SSL_ERROR_WANT_READ
  5617. */
  5618. if (!TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), -1)
  5619. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_READ))
  5620. goto end;
  5621. /* Now write some data in peer - we will write the key update */
  5622. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess)))
  5623. goto end;
  5624. /*
  5625. * write data in local previously that we will complete
  5626. * read data in peer previously that we will complete
  5627. */
  5628. if (!TEST_int_eq(SSL_write(local, lwbuf, sizeof(lwbuf)), sizeof(lwbuf))
  5629. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), sizeof(prbuf)))
  5630. goto end;
  5631. /* check that sending and receiving appdata ok */
  5632. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  5633. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  5634. goto end;
  5635. testresult = 1;
  5636. end:
  5637. SSL_free(serverssl);
  5638. SSL_free(clientssl);
  5639. SSL_CTX_free(sctx);
  5640. SSL_CTX_free(cctx);
  5641. return testresult;
  5642. }
  5643. /*
  5644. * Test we can't send a KeyUpdate (update requested) message while
  5645. * local write data is pending.
  5646. * Test 0: Client sends KeyUpdate while Client is writing
  5647. * Test 1: Server sends KeyUpdate while Server is writing
  5648. */
  5649. static int test_key_update_local_in_write(int tst)
  5650. {
  5651. SSL_CTX *cctx = NULL, *sctx = NULL;
  5652. SSL *clientssl = NULL, *serverssl = NULL;
  5653. int testresult = 0;
  5654. char buf[20];
  5655. static char *mess = "A test message";
  5656. BIO *bretry = BIO_new(bio_s_always_retry());
  5657. BIO *tmp = NULL;
  5658. SSL *local = NULL, *peer = NULL;
  5659. if (!TEST_ptr(bretry)
  5660. || !TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5661. TLS_client_method(),
  5662. TLS1_3_VERSION,
  5663. 0,
  5664. &sctx, &cctx, cert, privkey))
  5665. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5666. NULL, NULL))
  5667. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5668. SSL_ERROR_NONE)))
  5669. goto end;
  5670. local = tst == 0 ? clientssl : serverssl;
  5671. peer = tst == 0 ? serverssl : clientssl;
  5672. /* Swap the writing endpoint's write BIO to force a retry */
  5673. tmp = SSL_get_wbio(local);
  5674. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  5675. tmp = NULL;
  5676. goto end;
  5677. }
  5678. SSL_set0_wbio(local, bretry);
  5679. bretry = NULL;
  5680. /* write data in local will fail with SSL_ERROR_WANT_WRITE */
  5681. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), -1)
  5682. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_WRITE))
  5683. goto end;
  5684. /* Reinstate the original writing endpoint's write BIO */
  5685. SSL_set0_wbio(local, tmp);
  5686. tmp = NULL;
  5687. /* SSL_key_update will fail, because writing in local*/
  5688. if (!TEST_false(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  5689. || !TEST_int_eq(ERR_GET_REASON(ERR_peek_error()), SSL_R_BAD_WRITE_RETRY))
  5690. goto end;
  5691. ERR_clear_error();
  5692. /* write data in local previously that we will complete */
  5693. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess)))
  5694. goto end;
  5695. /* SSL_key_update will succeed because there is no pending write data */
  5696. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  5697. || !TEST_int_eq(SSL_do_handshake(local), 1))
  5698. goto end;
  5699. /*
  5700. * we write some appdata in local
  5701. * read data in peer - we will read the keyupdate msg
  5702. */
  5703. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  5704. || !TEST_int_eq(SSL_read(peer, buf, sizeof(buf)), strlen(mess)))
  5705. goto end;
  5706. /* Write more peer more data to ensure we send the keyupdate message back */
  5707. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  5708. || !TEST_int_eq(SSL_read(local, buf, sizeof(buf)), strlen(mess)))
  5709. goto end;
  5710. testresult = 1;
  5711. end:
  5712. SSL_free(serverssl);
  5713. SSL_free(clientssl);
  5714. SSL_CTX_free(sctx);
  5715. SSL_CTX_free(cctx);
  5716. BIO_free(bretry);
  5717. BIO_free(tmp);
  5718. return testresult;
  5719. }
  5720. /*
  5721. * Test we can handle a KeyUpdate (update requested) message while
  5722. * local read data is pending(the msg header had been read 5 bytes).
  5723. * Test 0: Client sends KeyUpdate while Client is reading
  5724. * Test 1: Server sends KeyUpdate while Server is reading
  5725. */
  5726. static int test_key_update_local_in_read(int tst)
  5727. {
  5728. SSL_CTX *cctx = NULL, *sctx = NULL;
  5729. SSL *clientssl = NULL, *serverssl = NULL;
  5730. int testresult = 0;
  5731. char lrbuf[515], pwbuf[515] = {0}, prbuf[20];
  5732. static char *mess = "A test message";
  5733. BIO *lbio = NULL, *pbio = NULL;
  5734. SSL *local = NULL, *peer = NULL;
  5735. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5736. TLS_client_method(),
  5737. TLS1_3_VERSION,
  5738. 0,
  5739. &sctx, &cctx, cert, privkey))
  5740. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5741. NULL, NULL))
  5742. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5743. SSL_ERROR_NONE)))
  5744. goto end;
  5745. local = tst == 0 ? clientssl : serverssl;
  5746. peer = tst == 0 ? serverssl : clientssl;
  5747. if (!TEST_int_eq(BIO_new_bio_pair(&lbio, 512, &pbio, 512), 1))
  5748. goto end;
  5749. SSL_set_bio(local, lbio, lbio);
  5750. SSL_set_bio(peer, pbio, pbio);
  5751. /* write app data in peer will fail with SSL_ERROR_WANT_WRITE */
  5752. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), -1)
  5753. || !TEST_int_eq(SSL_get_error(peer, -1), SSL_ERROR_WANT_WRITE))
  5754. goto end;
  5755. /* read appdata in local will fail with SSL_ERROR_WANT_READ */
  5756. if (!TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), -1)
  5757. || !TEST_int_eq(SSL_get_error(local, -1), SSL_ERROR_WANT_READ))
  5758. goto end;
  5759. /* SSL_do_handshake will send keyupdate msg */
  5760. if (!TEST_true(SSL_key_update(local, SSL_KEY_UPDATE_REQUESTED))
  5761. || !TEST_int_eq(SSL_do_handshake(local), 1))
  5762. goto end;
  5763. /*
  5764. * write data in peer previously that we will complete
  5765. * read data in local previously that we will complete
  5766. */
  5767. if (!TEST_int_eq(SSL_write(peer, pwbuf, sizeof(pwbuf)), sizeof(pwbuf))
  5768. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), sizeof(lrbuf)))
  5769. goto end;
  5770. /*
  5771. * write data in local
  5772. * read data in peer - we will read the key update
  5773. */
  5774. if (!TEST_int_eq(SSL_write(local, mess, strlen(mess)), strlen(mess))
  5775. || !TEST_int_eq(SSL_read(peer, prbuf, sizeof(prbuf)), strlen(mess)))
  5776. goto end;
  5777. /* Write more peer data to ensure we send the keyupdate message back */
  5778. if (!TEST_int_eq(SSL_write(peer, mess, strlen(mess)), strlen(mess))
  5779. || !TEST_int_eq(SSL_read(local, lrbuf, sizeof(lrbuf)), strlen(mess)))
  5780. goto end;
  5781. testresult = 1;
  5782. end:
  5783. SSL_free(serverssl);
  5784. SSL_free(clientssl);
  5785. SSL_CTX_free(sctx);
  5786. SSL_CTX_free(cctx);
  5787. return testresult;
  5788. }
  5789. #endif /* OSSL_NO_USABLE_TLS1_3 */
  5790. static int test_ssl_clear(int idx)
  5791. {
  5792. SSL_CTX *cctx = NULL, *sctx = NULL;
  5793. SSL *clientssl = NULL, *serverssl = NULL;
  5794. int testresult = 0;
  5795. #ifdef OPENSSL_NO_TLS1_2
  5796. if (idx == 1)
  5797. return 1;
  5798. #endif
  5799. /* Create an initial connection */
  5800. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5801. TLS_client_method(), TLS1_VERSION, 0,
  5802. &sctx, &cctx, cert, privkey))
  5803. || (idx == 1
  5804. && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
  5805. TLS1_2_VERSION)))
  5806. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5807. &clientssl, NULL, NULL))
  5808. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5809. SSL_ERROR_NONE)))
  5810. goto end;
  5811. SSL_shutdown(clientssl);
  5812. SSL_shutdown(serverssl);
  5813. SSL_free(serverssl);
  5814. serverssl = NULL;
  5815. /* Clear clientssl - we're going to reuse the object */
  5816. if (!TEST_true(SSL_clear(clientssl)))
  5817. goto end;
  5818. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5819. NULL, NULL))
  5820. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5821. SSL_ERROR_NONE))
  5822. || !TEST_true(SSL_session_reused(clientssl)))
  5823. goto end;
  5824. SSL_shutdown(clientssl);
  5825. SSL_shutdown(serverssl);
  5826. testresult = 1;
  5827. end:
  5828. SSL_free(serverssl);
  5829. SSL_free(clientssl);
  5830. SSL_CTX_free(sctx);
  5831. SSL_CTX_free(cctx);
  5832. return testresult;
  5833. }
  5834. /* Parse CH and retrieve any MFL extension value if present */
  5835. static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
  5836. {
  5837. long len;
  5838. unsigned char *data;
  5839. PACKET pkt, pkt2, pkt3;
  5840. unsigned int MFL_code = 0, type = 0;
  5841. if (!TEST_uint_gt(len = BIO_get_mem_data(bio, (char **) &data), 0))
  5842. goto end;
  5843. memset(&pkt, 0, sizeof(pkt));
  5844. memset(&pkt2, 0, sizeof(pkt2));
  5845. memset(&pkt3, 0, sizeof(pkt3));
  5846. if (!TEST_long_gt(len, 0)
  5847. || !TEST_true(PACKET_buf_init(&pkt, data, len))
  5848. /* Skip the record header */
  5849. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  5850. /* Skip the handshake message header */
  5851. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  5852. /* Skip client version and random */
  5853. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  5854. + SSL3_RANDOM_SIZE))
  5855. /* Skip session id */
  5856. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  5857. /* Skip ciphers */
  5858. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  5859. /* Skip compression */
  5860. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  5861. /* Extensions len */
  5862. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  5863. goto end;
  5864. /* Loop through all extensions */
  5865. while (PACKET_remaining(&pkt2)) {
  5866. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  5867. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  5868. goto end;
  5869. if (type == TLSEXT_TYPE_max_fragment_length) {
  5870. if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
  5871. || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
  5872. goto end;
  5873. *mfl_codemfl_code = MFL_code;
  5874. return 1;
  5875. }
  5876. }
  5877. end:
  5878. return 0;
  5879. }
  5880. /* Maximum-Fragment-Length TLS extension mode to test */
  5881. static const unsigned char max_fragment_len_test[] = {
  5882. TLSEXT_max_fragment_length_512,
  5883. TLSEXT_max_fragment_length_1024,
  5884. TLSEXT_max_fragment_length_2048,
  5885. TLSEXT_max_fragment_length_4096
  5886. };
  5887. static int test_max_fragment_len_ext(int idx_tst)
  5888. {
  5889. SSL_CTX *ctx = NULL;
  5890. SSL *con = NULL;
  5891. int testresult = 0, MFL_mode = 0;
  5892. BIO *rbio, *wbio;
  5893. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL, TLS_client_method(),
  5894. TLS1_VERSION, 0, NULL, &ctx, NULL,
  5895. NULL)))
  5896. return 0;
  5897. if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
  5898. ctx, max_fragment_len_test[idx_tst])))
  5899. goto end;
  5900. con = SSL_new(ctx);
  5901. if (!TEST_ptr(con))
  5902. goto end;
  5903. rbio = BIO_new(BIO_s_mem());
  5904. wbio = BIO_new(BIO_s_mem());
  5905. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  5906. BIO_free(rbio);
  5907. BIO_free(wbio);
  5908. goto end;
  5909. }
  5910. SSL_set_bio(con, rbio, wbio);
  5911. if (!TEST_int_le(SSL_connect(con), 0)) {
  5912. /* This shouldn't succeed because we don't have a server! */
  5913. goto end;
  5914. }
  5915. if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
  5916. /* no MFL in client hello */
  5917. goto end;
  5918. if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
  5919. goto end;
  5920. testresult = 1;
  5921. end:
  5922. SSL_free(con);
  5923. SSL_CTX_free(ctx);
  5924. return testresult;
  5925. }
  5926. #ifndef OSSL_NO_USABLE_TLS1_3
  5927. static int test_pha_key_update(void)
  5928. {
  5929. SSL_CTX *cctx = NULL, *sctx = NULL;
  5930. SSL *clientssl = NULL, *serverssl = NULL;
  5931. int testresult = 0;
  5932. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  5933. TLS_client_method(), TLS1_VERSION, 0,
  5934. &sctx, &cctx, cert, privkey)))
  5935. return 0;
  5936. if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
  5937. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
  5938. || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
  5939. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
  5940. goto end;
  5941. SSL_CTX_set_post_handshake_auth(cctx, 1);
  5942. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5943. NULL, NULL)))
  5944. goto end;
  5945. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5946. SSL_ERROR_NONE)))
  5947. goto end;
  5948. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  5949. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  5950. goto end;
  5951. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  5952. goto end;
  5953. /* Start handshake on the server */
  5954. if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
  5955. goto end;
  5956. /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
  5957. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5958. SSL_ERROR_NONE)))
  5959. goto end;
  5960. SSL_shutdown(clientssl);
  5961. SSL_shutdown(serverssl);
  5962. testresult = 1;
  5963. end:
  5964. SSL_free(serverssl);
  5965. SSL_free(clientssl);
  5966. SSL_CTX_free(sctx);
  5967. SSL_CTX_free(cctx);
  5968. return testresult;
  5969. }
  5970. #endif
  5971. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  5972. static SRP_VBASE *vbase = NULL;
  5973. static int ssl_srp_cb(SSL *s, int *ad, void *arg)
  5974. {
  5975. int ret = SSL3_AL_FATAL;
  5976. char *username;
  5977. SRP_user_pwd *user = NULL;
  5978. username = SSL_get_srp_username(s);
  5979. if (username == NULL) {
  5980. *ad = SSL_AD_INTERNAL_ERROR;
  5981. goto err;
  5982. }
  5983. user = SRP_VBASE_get1_by_user(vbase, username);
  5984. if (user == NULL) {
  5985. *ad = SSL_AD_INTERNAL_ERROR;
  5986. goto err;
  5987. }
  5988. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  5989. user->info) <= 0) {
  5990. *ad = SSL_AD_INTERNAL_ERROR;
  5991. goto err;
  5992. }
  5993. ret = 0;
  5994. err:
  5995. SRP_user_pwd_free(user);
  5996. return ret;
  5997. }
  5998. static int create_new_vfile(char *userid, char *password, const char *filename)
  5999. {
  6000. char *gNid = NULL;
  6001. OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
  6002. TXT_DB *db = NULL;
  6003. int ret = 0;
  6004. BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
  6005. size_t i;
  6006. if (!TEST_ptr(dummy) || !TEST_ptr(row))
  6007. goto end;
  6008. gNid = SRP_create_verifier_ex(userid, password, &row[DB_srpsalt],
  6009. &row[DB_srpverifier], NULL, NULL, libctx, NULL);
  6010. if (!TEST_ptr(gNid))
  6011. goto end;
  6012. /*
  6013. * The only way to create an empty TXT_DB is to provide a BIO with no data
  6014. * in it!
  6015. */
  6016. db = TXT_DB_read(dummy, DB_NUMBER);
  6017. if (!TEST_ptr(db))
  6018. goto end;
  6019. out = BIO_new_file(filename, "w");
  6020. if (!TEST_ptr(out))
  6021. goto end;
  6022. row[DB_srpid] = OPENSSL_strdup(userid);
  6023. row[DB_srptype] = OPENSSL_strdup("V");
  6024. row[DB_srpgN] = OPENSSL_strdup(gNid);
  6025. if (!TEST_ptr(row[DB_srpid])
  6026. || !TEST_ptr(row[DB_srptype])
  6027. || !TEST_ptr(row[DB_srpgN])
  6028. || !TEST_true(TXT_DB_insert(db, row)))
  6029. goto end;
  6030. row = NULL;
  6031. if (TXT_DB_write(out, db) <= 0)
  6032. goto end;
  6033. ret = 1;
  6034. end:
  6035. if (row != NULL) {
  6036. for (i = 0; i < DB_NUMBER; i++)
  6037. OPENSSL_free(row[i]);
  6038. }
  6039. OPENSSL_free(row);
  6040. BIO_free(dummy);
  6041. BIO_free(out);
  6042. TXT_DB_free(db);
  6043. return ret;
  6044. }
  6045. static int create_new_vbase(char *userid, char *password)
  6046. {
  6047. BIGNUM *verifier = NULL, *salt = NULL;
  6048. const SRP_gN *lgN = NULL;
  6049. SRP_user_pwd *user_pwd = NULL;
  6050. int ret = 0;
  6051. lgN = SRP_get_default_gN(NULL);
  6052. if (!TEST_ptr(lgN))
  6053. goto end;
  6054. if (!TEST_true(SRP_create_verifier_BN_ex(userid, password, &salt, &verifier,
  6055. lgN->N, lgN->g, libctx, NULL)))
  6056. goto end;
  6057. user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
  6058. if (!TEST_ptr(user_pwd))
  6059. goto end;
  6060. user_pwd->N = lgN->N;
  6061. user_pwd->g = lgN->g;
  6062. user_pwd->id = OPENSSL_strdup(userid);
  6063. if (!TEST_ptr(user_pwd->id))
  6064. goto end;
  6065. user_pwd->v = verifier;
  6066. user_pwd->s = salt;
  6067. verifier = salt = NULL;
  6068. if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
  6069. goto end;
  6070. user_pwd = NULL;
  6071. ret = 1;
  6072. end:
  6073. SRP_user_pwd_free(user_pwd);
  6074. BN_free(salt);
  6075. BN_free(verifier);
  6076. return ret;
  6077. }
  6078. /*
  6079. * SRP tests
  6080. *
  6081. * Test 0: Simple successful SRP connection, new vbase
  6082. * Test 1: Connection failure due to bad password, new vbase
  6083. * Test 2: Simple successful SRP connection, vbase loaded from existing file
  6084. * Test 3: Connection failure due to bad password, vbase loaded from existing
  6085. * file
  6086. * Test 4: Simple successful SRP connection, vbase loaded from new file
  6087. * Test 5: Connection failure due to bad password, vbase loaded from new file
  6088. */
  6089. static int test_srp(int tst)
  6090. {
  6091. char *userid = "test", *password = "password", *tstsrpfile;
  6092. SSL_CTX *cctx = NULL, *sctx = NULL;
  6093. SSL *clientssl = NULL, *serverssl = NULL;
  6094. int ret, testresult = 0;
  6095. vbase = SRP_VBASE_new(NULL);
  6096. if (!TEST_ptr(vbase))
  6097. goto end;
  6098. if (tst == 0 || tst == 1) {
  6099. if (!TEST_true(create_new_vbase(userid, password)))
  6100. goto end;
  6101. } else {
  6102. if (tst == 4 || tst == 5) {
  6103. if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
  6104. goto end;
  6105. tstsrpfile = tmpfilename;
  6106. } else {
  6107. tstsrpfile = srpvfile;
  6108. }
  6109. if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
  6110. goto end;
  6111. }
  6112. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6113. TLS_client_method(), TLS1_VERSION, 0,
  6114. &sctx, &cctx, cert, privkey)))
  6115. goto end;
  6116. if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
  6117. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
  6118. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
  6119. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
  6120. || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
  6121. goto end;
  6122. if (tst % 2 == 1) {
  6123. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
  6124. goto end;
  6125. } else {
  6126. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
  6127. goto end;
  6128. }
  6129. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6130. NULL, NULL)))
  6131. goto end;
  6132. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  6133. if (ret) {
  6134. if (!TEST_true(tst % 2 == 0))
  6135. goto end;
  6136. } else {
  6137. if (!TEST_true(tst % 2 == 1))
  6138. goto end;
  6139. }
  6140. testresult = 1;
  6141. end:
  6142. SRP_VBASE_free(vbase);
  6143. vbase = NULL;
  6144. SSL_free(serverssl);
  6145. SSL_free(clientssl);
  6146. SSL_CTX_free(sctx);
  6147. SSL_CTX_free(cctx);
  6148. return testresult;
  6149. }
  6150. #endif
  6151. static int info_cb_failed = 0;
  6152. static int info_cb_offset = 0;
  6153. static int info_cb_this_state = -1;
  6154. static struct info_cb_states_st {
  6155. int where;
  6156. const char *statestr;
  6157. } info_cb_states[][60] = {
  6158. {
  6159. /* TLSv1.2 server followed by resumption */
  6160. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6161. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6162. {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
  6163. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
  6164. {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
  6165. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6166. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6167. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6168. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"},
  6169. {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6170. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
  6171. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6172. {SSL_CB_EXIT, NULL}, {0, NULL},
  6173. }, {
  6174. /* TLSv1.2 client followed by resumption */
  6175. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6176. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6177. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
  6178. {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
  6179. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
  6180. {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6181. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6182. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6183. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6184. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  6185. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6186. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6187. }, {
  6188. /* TLSv1.3 server followed by resumption */
  6189. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6190. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6191. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
  6192. {SSL_CB_LOOP, "TWSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  6193. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  6194. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6195. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  6196. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6197. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6198. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6199. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  6200. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6201. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6202. }, {
  6203. /* TLSv1.3 client followed by resumption */
  6204. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6205. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  6206. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
  6207. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  6208. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6209. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6210. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"},
  6211. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  6212. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  6213. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  6214. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6215. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  6216. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6217. {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "TRST"},
  6218. {SSL_CB_EXIT, NULL}, {0, NULL},
  6219. }, {
  6220. /* TLSv1.3 server, early_data */
  6221. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6222. {SSL_CB_LOOP, "PINIT"}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  6223. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  6224. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6225. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6226. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
  6227. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  6228. {SSL_CB_EXIT, NULL}, {0, NULL},
  6229. }, {
  6230. /* TLSv1.3 client, early_data */
  6231. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT"},
  6232. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
  6233. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  6234. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  6235. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  6236. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
  6237. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  6238. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK"}, {SSL_CB_LOOP, "SSLOK"},
  6239. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  6240. }, {
  6241. {0, NULL},
  6242. }
  6243. };
  6244. static void sslapi_info_callback(const SSL *s, int where, int ret)
  6245. {
  6246. struct info_cb_states_st *state = info_cb_states[info_cb_offset];
  6247. /* We do not ever expect a connection to fail in this test */
  6248. if (!TEST_false(ret == 0)) {
  6249. info_cb_failed = 1;
  6250. return;
  6251. }
  6252. /*
  6253. * Do some sanity checks. We never expect these things to happen in this
  6254. * test
  6255. */
  6256. if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
  6257. || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
  6258. || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
  6259. info_cb_failed = 1;
  6260. return;
  6261. }
  6262. /* Now check we're in the right state */
  6263. if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
  6264. info_cb_failed = 1;
  6265. return;
  6266. }
  6267. if ((where & SSL_CB_LOOP) != 0
  6268. && !TEST_int_eq(strcmp(SSL_state_string(s),
  6269. state[info_cb_this_state].statestr), 0)) {
  6270. info_cb_failed = 1;
  6271. return;
  6272. }
  6273. /*
  6274. * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
  6275. */
  6276. if ((where & SSL_CB_HANDSHAKE_DONE)
  6277. && SSL_in_init((SSL *)s) != 0) {
  6278. info_cb_failed = 1;
  6279. return;
  6280. }
  6281. }
  6282. /*
  6283. * Test the info callback gets called when we expect it to.
  6284. *
  6285. * Test 0: TLSv1.2, server
  6286. * Test 1: TLSv1.2, client
  6287. * Test 2: TLSv1.3, server
  6288. * Test 3: TLSv1.3, client
  6289. * Test 4: TLSv1.3, server, early_data
  6290. * Test 5: TLSv1.3, client, early_data
  6291. */
  6292. static int test_info_callback(int tst)
  6293. {
  6294. SSL_CTX *cctx = NULL, *sctx = NULL;
  6295. SSL *clientssl = NULL, *serverssl = NULL;
  6296. SSL_SESSION *clntsess = NULL;
  6297. int testresult = 0;
  6298. int tlsvers;
  6299. if (tst < 2) {
  6300. /* We need either ECDHE or DHE for the TLSv1.2 test to work */
  6301. #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
  6302. || !defined(OPENSSL_NO_DH))
  6303. tlsvers = TLS1_2_VERSION;
  6304. #else
  6305. return 1;
  6306. #endif
  6307. } else {
  6308. #ifndef OSSL_NO_USABLE_TLS1_3
  6309. tlsvers = TLS1_3_VERSION;
  6310. #else
  6311. return 1;
  6312. #endif
  6313. }
  6314. /* Reset globals */
  6315. info_cb_failed = 0;
  6316. info_cb_this_state = -1;
  6317. info_cb_offset = tst;
  6318. #ifndef OSSL_NO_USABLE_TLS1_3
  6319. if (tst >= 4) {
  6320. SSL_SESSION *sess = NULL;
  6321. size_t written, readbytes;
  6322. unsigned char buf[80];
  6323. /* early_data tests */
  6324. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  6325. &serverssl, &sess, 0)))
  6326. goto end;
  6327. /* We don't actually need this reference */
  6328. SSL_SESSION_free(sess);
  6329. SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
  6330. sslapi_info_callback);
  6331. /* Write and read some early data and then complete the connection */
  6332. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  6333. &written))
  6334. || !TEST_size_t_eq(written, strlen(MSG1))
  6335. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  6336. sizeof(buf), &readbytes),
  6337. SSL_READ_EARLY_DATA_SUCCESS)
  6338. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  6339. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  6340. SSL_EARLY_DATA_ACCEPTED)
  6341. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6342. SSL_ERROR_NONE))
  6343. || !TEST_false(info_cb_failed))
  6344. goto end;
  6345. testresult = 1;
  6346. goto end;
  6347. }
  6348. #endif
  6349. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6350. TLS_client_method(),
  6351. tlsvers, tlsvers, &sctx, &cctx, cert,
  6352. privkey)))
  6353. goto end;
  6354. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, 1)))
  6355. goto end;
  6356. /*
  6357. * For even numbered tests we check the server callbacks. For odd numbers we
  6358. * check the client.
  6359. */
  6360. SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
  6361. sslapi_info_callback);
  6362. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  6363. &clientssl, NULL, NULL))
  6364. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6365. SSL_ERROR_NONE))
  6366. || !TEST_false(info_cb_failed))
  6367. goto end;
  6368. clntsess = SSL_get1_session(clientssl);
  6369. SSL_shutdown(clientssl);
  6370. SSL_shutdown(serverssl);
  6371. SSL_free(serverssl);
  6372. SSL_free(clientssl);
  6373. serverssl = clientssl = NULL;
  6374. /* Now do a resumption */
  6375. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6376. NULL))
  6377. || !TEST_true(SSL_set_session(clientssl, clntsess))
  6378. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6379. SSL_ERROR_NONE))
  6380. || !TEST_true(SSL_session_reused(clientssl))
  6381. || !TEST_false(info_cb_failed))
  6382. goto end;
  6383. testresult = 1;
  6384. end:
  6385. SSL_free(serverssl);
  6386. SSL_free(clientssl);
  6387. SSL_SESSION_free(clntsess);
  6388. SSL_CTX_free(sctx);
  6389. SSL_CTX_free(cctx);
  6390. return testresult;
  6391. }
  6392. static int test_ssl_pending(int tst)
  6393. {
  6394. SSL_CTX *cctx = NULL, *sctx = NULL;
  6395. SSL *clientssl = NULL, *serverssl = NULL;
  6396. int testresult = 0;
  6397. char msg[] = "A test message";
  6398. char buf[5];
  6399. size_t written, readbytes;
  6400. if (tst == 0) {
  6401. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6402. TLS_client_method(),
  6403. TLS1_VERSION, 0,
  6404. &sctx, &cctx, cert, privkey)))
  6405. goto end;
  6406. } else {
  6407. #ifndef OPENSSL_NO_DTLS
  6408. if (!TEST_true(create_ssl_ctx_pair(libctx, DTLS_server_method(),
  6409. DTLS_client_method(),
  6410. DTLS1_VERSION, 0,
  6411. &sctx, &cctx, cert, privkey)))
  6412. goto end;
  6413. # ifdef OPENSSL_NO_DTLS1_2
  6414. /* Not supported in the FIPS provider */
  6415. if (is_fips) {
  6416. testresult = 1;
  6417. goto end;
  6418. };
  6419. /*
  6420. * Default sigalgs are SHA1 based in <DTLS1.2 which is in security
  6421. * level 0
  6422. */
  6423. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, "DEFAULT:@SECLEVEL=0"))
  6424. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  6425. "DEFAULT:@SECLEVEL=0")))
  6426. goto end;
  6427. # endif
  6428. #else
  6429. return 1;
  6430. #endif
  6431. }
  6432. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6433. NULL, NULL))
  6434. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6435. SSL_ERROR_NONE)))
  6436. goto end;
  6437. if (!TEST_int_eq(SSL_pending(clientssl), 0)
  6438. || !TEST_false(SSL_has_pending(clientssl))
  6439. || !TEST_int_eq(SSL_pending(serverssl), 0)
  6440. || !TEST_false(SSL_has_pending(serverssl))
  6441. || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  6442. || !TEST_size_t_eq(written, sizeof(msg))
  6443. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  6444. || !TEST_size_t_eq(readbytes, sizeof(buf))
  6445. || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
  6446. || !TEST_true(SSL_has_pending(clientssl)))
  6447. goto end;
  6448. testresult = 1;
  6449. end:
  6450. SSL_free(serverssl);
  6451. SSL_free(clientssl);
  6452. SSL_CTX_free(sctx);
  6453. SSL_CTX_free(cctx);
  6454. return testresult;
  6455. }
  6456. static struct {
  6457. unsigned int maxprot;
  6458. const char *clntciphers;
  6459. const char *clnttls13ciphers;
  6460. const char *srvrciphers;
  6461. const char *srvrtls13ciphers;
  6462. const char *shared;
  6463. const char *fipsshared;
  6464. } shared_ciphers_data[] = {
  6465. /*
  6466. * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
  6467. * TLSv1.3 is enabled but TLSv1.2 is disabled.
  6468. */
  6469. #if defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  6470. {
  6471. TLS1_2_VERSION,
  6472. "AES128-SHA:AES256-SHA",
  6473. NULL,
  6474. "AES256-SHA:DHE-RSA-AES128-SHA",
  6475. NULL,
  6476. "AES256-SHA",
  6477. "AES256-SHA"
  6478. },
  6479. # if !defined(OPENSSL_NO_CHACHA) \
  6480. && !defined(OPENSSL_NO_POLY1305) \
  6481. && !defined(OPENSSL_NO_EC)
  6482. {
  6483. TLS1_2_VERSION,
  6484. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6485. NULL,
  6486. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6487. NULL,
  6488. "AES128-SHA:ECDHE-RSA-CHACHA20-POLY1305",
  6489. "AES128-SHA"
  6490. },
  6491. # endif
  6492. {
  6493. TLS1_2_VERSION,
  6494. "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
  6495. NULL,
  6496. "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
  6497. NULL,
  6498. "AES128-SHA:AES256-SHA",
  6499. "AES128-SHA:AES256-SHA"
  6500. },
  6501. {
  6502. TLS1_2_VERSION,
  6503. "AES128-SHA:AES256-SHA",
  6504. NULL,
  6505. "AES128-SHA:DHE-RSA-AES128-SHA",
  6506. NULL,
  6507. "AES128-SHA",
  6508. "AES128-SHA"
  6509. },
  6510. #endif
  6511. /*
  6512. * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
  6513. * enabled.
  6514. */
  6515. #if !defined(OSSL_NO_USABLE_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
  6516. && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  6517. {
  6518. TLS1_3_VERSION,
  6519. "AES128-SHA:AES256-SHA",
  6520. NULL,
  6521. "AES256-SHA:AES128-SHA256",
  6522. NULL,
  6523. "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
  6524. "TLS_AES_128_GCM_SHA256:AES256-SHA",
  6525. "TLS_AES_256_GCM_SHA384:TLS_AES_128_GCM_SHA256:AES256-SHA"
  6526. },
  6527. #endif
  6528. #ifndef OSSL_NO_USABLE_TLS1_3
  6529. {
  6530. TLS1_3_VERSION,
  6531. "AES128-SHA",
  6532. "TLS_AES_256_GCM_SHA384",
  6533. "AES256-SHA",
  6534. "TLS_AES_256_GCM_SHA384",
  6535. "TLS_AES_256_GCM_SHA384",
  6536. "TLS_AES_256_GCM_SHA384"
  6537. },
  6538. #endif
  6539. };
  6540. static int int_test_ssl_get_shared_ciphers(int tst, int clnt)
  6541. {
  6542. SSL_CTX *cctx = NULL, *sctx = NULL;
  6543. SSL *clientssl = NULL, *serverssl = NULL;
  6544. int testresult = 0;
  6545. char buf[1024];
  6546. OSSL_LIB_CTX *tmplibctx = OSSL_LIB_CTX_new();
  6547. if (!TEST_ptr(tmplibctx))
  6548. goto end;
  6549. /*
  6550. * Regardless of whether we're testing with the FIPS provider loaded into
  6551. * libctx, we want one peer to always use the full set of ciphersuites
  6552. * available. Therefore we use a separate libctx with the default provider
  6553. * loaded into it. We run the same tests twice - once with the client side
  6554. * having the full set of ciphersuites and once with the server side.
  6555. */
  6556. if (clnt) {
  6557. cctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_client_method());
  6558. if (!TEST_ptr(cctx))
  6559. goto end;
  6560. } else {
  6561. sctx = SSL_CTX_new_ex(tmplibctx, NULL, TLS_server_method());
  6562. if (!TEST_ptr(sctx))
  6563. goto end;
  6564. }
  6565. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6566. TLS_client_method(),
  6567. TLS1_VERSION,
  6568. shared_ciphers_data[tst].maxprot,
  6569. &sctx, &cctx, cert, privkey)))
  6570. goto end;
  6571. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  6572. shared_ciphers_data[tst].clntciphers))
  6573. || (shared_ciphers_data[tst].clnttls13ciphers != NULL
  6574. && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  6575. shared_ciphers_data[tst].clnttls13ciphers)))
  6576. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  6577. shared_ciphers_data[tst].srvrciphers))
  6578. || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
  6579. && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  6580. shared_ciphers_data[tst].srvrtls13ciphers))))
  6581. goto end;
  6582. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6583. NULL, NULL))
  6584. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6585. SSL_ERROR_NONE)))
  6586. goto end;
  6587. if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
  6588. || !TEST_int_eq(strcmp(buf,
  6589. is_fips
  6590. ? shared_ciphers_data[tst].fipsshared
  6591. : shared_ciphers_data[tst].shared),
  6592. 0)) {
  6593. TEST_info("Shared ciphers are: %s\n", buf);
  6594. goto end;
  6595. }
  6596. testresult = 1;
  6597. end:
  6598. SSL_free(serverssl);
  6599. SSL_free(clientssl);
  6600. SSL_CTX_free(sctx);
  6601. SSL_CTX_free(cctx);
  6602. OSSL_LIB_CTX_free(tmplibctx);
  6603. return testresult;
  6604. }
  6605. static int test_ssl_get_shared_ciphers(int tst)
  6606. {
  6607. return int_test_ssl_get_shared_ciphers(tst, 0)
  6608. && int_test_ssl_get_shared_ciphers(tst, 1);
  6609. }
  6610. static const char *appdata = "Hello World";
  6611. static int gen_tick_called, dec_tick_called, tick_key_cb_called;
  6612. static int tick_key_renew = 0;
  6613. static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  6614. static int gen_tick_cb(SSL *s, void *arg)
  6615. {
  6616. gen_tick_called = 1;
  6617. return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
  6618. strlen(appdata));
  6619. }
  6620. static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
  6621. const unsigned char *keyname,
  6622. size_t keyname_length,
  6623. SSL_TICKET_STATUS status,
  6624. void *arg)
  6625. {
  6626. void *tickdata;
  6627. size_t tickdlen;
  6628. dec_tick_called = 1;
  6629. if (status == SSL_TICKET_EMPTY)
  6630. return SSL_TICKET_RETURN_IGNORE_RENEW;
  6631. if (!TEST_true(status == SSL_TICKET_SUCCESS
  6632. || status == SSL_TICKET_SUCCESS_RENEW))
  6633. return SSL_TICKET_RETURN_ABORT;
  6634. if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
  6635. &tickdlen))
  6636. || !TEST_size_t_eq(tickdlen, strlen(appdata))
  6637. || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
  6638. return SSL_TICKET_RETURN_ABORT;
  6639. if (tick_key_cb_called) {
  6640. /* Don't change what the ticket key callback wanted to do */
  6641. switch (status) {
  6642. case SSL_TICKET_NO_DECRYPT:
  6643. return SSL_TICKET_RETURN_IGNORE_RENEW;
  6644. case SSL_TICKET_SUCCESS:
  6645. return SSL_TICKET_RETURN_USE;
  6646. case SSL_TICKET_SUCCESS_RENEW:
  6647. return SSL_TICKET_RETURN_USE_RENEW;
  6648. default:
  6649. return SSL_TICKET_RETURN_ABORT;
  6650. }
  6651. }
  6652. return tick_dec_ret;
  6653. }
  6654. #ifndef OPENSSL_NO_DEPRECATED_3_0
  6655. static int tick_key_cb(SSL *s, unsigned char key_name[16],
  6656. unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
  6657. HMAC_CTX *hctx, int enc)
  6658. {
  6659. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  6660. const unsigned char tick_hmac_key[16] = "0123456789abcdef";
  6661. EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  6662. EVP_MD *sha256 = EVP_MD_fetch(libctx, "SHA-256", NULL);
  6663. int ret;
  6664. tick_key_cb_called = 1;
  6665. memset(iv, 0, AES_BLOCK_SIZE);
  6666. memset(key_name, 0, 16);
  6667. if (aes128cbc == NULL
  6668. || sha256 == NULL
  6669. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  6670. || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key), sha256,
  6671. NULL))
  6672. ret = -1;
  6673. else
  6674. ret = tick_key_renew ? 2 : 1;
  6675. EVP_CIPHER_free(aes128cbc);
  6676. EVP_MD_free(sha256);
  6677. return ret;
  6678. }
  6679. #endif
  6680. static int tick_key_evp_cb(SSL *s, unsigned char key_name[16],
  6681. unsigned char iv[EVP_MAX_IV_LENGTH],
  6682. EVP_CIPHER_CTX *ctx, EVP_MAC_CTX *hctx, int enc)
  6683. {
  6684. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  6685. unsigned char tick_hmac_key[16] = "0123456789abcdef";
  6686. OSSL_PARAM params[2];
  6687. EVP_CIPHER *aes128cbc = EVP_CIPHER_fetch(libctx, "AES-128-CBC", NULL);
  6688. int ret;
  6689. tick_key_cb_called = 1;
  6690. memset(iv, 0, AES_BLOCK_SIZE);
  6691. memset(key_name, 0, 16);
  6692. params[0] = OSSL_PARAM_construct_utf8_string(OSSL_MAC_PARAM_DIGEST,
  6693. "SHA256", 0);
  6694. params[1] = OSSL_PARAM_construct_end();
  6695. if (aes128cbc == NULL
  6696. || !EVP_CipherInit_ex(ctx, aes128cbc, NULL, tick_aes_key, iv, enc)
  6697. || !EVP_MAC_init(hctx, tick_hmac_key, sizeof(tick_hmac_key),
  6698. params))
  6699. ret = -1;
  6700. else
  6701. ret = tick_key_renew ? 2 : 1;
  6702. EVP_CIPHER_free(aes128cbc);
  6703. return ret;
  6704. }
  6705. /*
  6706. * Test the various ticket callbacks
  6707. * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
  6708. * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
  6709. * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
  6710. * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
  6711. * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
  6712. * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
  6713. * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
  6714. * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
  6715. * Test 8: TLSv1.2, old ticket key callback, ticket, no renewal
  6716. * Test 9: TLSv1.3, old ticket key callback, ticket, no renewal
  6717. * Test 10: TLSv1.2, old ticket key callback, ticket, renewal
  6718. * Test 11: TLSv1.3, old ticket key callback, ticket, renewal
  6719. * Test 12: TLSv1.2, ticket key callback, ticket, no renewal
  6720. * Test 13: TLSv1.3, ticket key callback, ticket, no renewal
  6721. * Test 14: TLSv1.2, ticket key callback, ticket, renewal
  6722. * Test 15: TLSv1.3, ticket key callback, ticket, renewal
  6723. */
  6724. static int test_ticket_callbacks(int tst)
  6725. {
  6726. SSL_CTX *cctx = NULL, *sctx = NULL;
  6727. SSL *clientssl = NULL, *serverssl = NULL;
  6728. SSL_SESSION *clntsess = NULL;
  6729. int testresult = 0;
  6730. #ifdef OPENSSL_NO_TLS1_2
  6731. if (tst % 2 == 0)
  6732. return 1;
  6733. #endif
  6734. #ifdef OSSL_NO_USABLE_TLS1_3
  6735. if (tst % 2 == 1)
  6736. return 1;
  6737. #endif
  6738. #ifdef OPENSSL_NO_DEPRECATED_3_0
  6739. if (tst >= 8 && tst <= 11)
  6740. return 1;
  6741. #endif
  6742. gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
  6743. /* Which tests the ticket key callback should request renewal for */
  6744. if (tst == 10 || tst == 11 || tst == 14 || tst == 15)
  6745. tick_key_renew = 1;
  6746. else
  6747. tick_key_renew = 0;
  6748. /* Which tests the decrypt ticket callback should request renewal for */
  6749. switch (tst) {
  6750. case 0:
  6751. case 1:
  6752. tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
  6753. break;
  6754. case 2:
  6755. case 3:
  6756. tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
  6757. break;
  6758. case 4:
  6759. case 5:
  6760. tick_dec_ret = SSL_TICKET_RETURN_USE;
  6761. break;
  6762. case 6:
  6763. case 7:
  6764. tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
  6765. break;
  6766. default:
  6767. tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  6768. }
  6769. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6770. TLS_client_method(),
  6771. TLS1_VERSION,
  6772. ((tst % 2) == 0) ? TLS1_2_VERSION
  6773. : TLS1_3_VERSION,
  6774. &sctx, &cctx, cert, privkey)))
  6775. goto end;
  6776. /*
  6777. * We only want sessions to resume from tickets - not the session cache. So
  6778. * switch the cache off.
  6779. */
  6780. if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
  6781. goto end;
  6782. if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
  6783. NULL)))
  6784. goto end;
  6785. if (tst >= 12) {
  6786. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_evp_cb(sctx, tick_key_evp_cb)))
  6787. goto end;
  6788. #ifndef OPENSSL_NO_DEPRECATED_3_0
  6789. } else if (tst >= 8) {
  6790. if (!TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
  6791. goto end;
  6792. #endif
  6793. }
  6794. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6795. NULL, NULL))
  6796. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6797. SSL_ERROR_NONE)))
  6798. goto end;
  6799. /*
  6800. * The decrypt ticket key callback in TLSv1.2 should be called even though
  6801. * we have no ticket yet, because it gets called with a status of
  6802. * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
  6803. * actually send any ticket data). This does not happen in TLSv1.3 because
  6804. * it is not valid to send empty ticket data in TLSv1.3.
  6805. */
  6806. if (!TEST_int_eq(gen_tick_called, 1)
  6807. || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
  6808. goto end;
  6809. gen_tick_called = dec_tick_called = 0;
  6810. clntsess = SSL_get1_session(clientssl);
  6811. SSL_shutdown(clientssl);
  6812. SSL_shutdown(serverssl);
  6813. SSL_free(serverssl);
  6814. SSL_free(clientssl);
  6815. serverssl = clientssl = NULL;
  6816. /* Now do a resumption */
  6817. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  6818. NULL))
  6819. || !TEST_true(SSL_set_session(clientssl, clntsess))
  6820. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  6821. SSL_ERROR_NONE)))
  6822. goto end;
  6823. if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
  6824. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
  6825. if (!TEST_false(SSL_session_reused(clientssl)))
  6826. goto end;
  6827. } else {
  6828. if (!TEST_true(SSL_session_reused(clientssl)))
  6829. goto end;
  6830. }
  6831. if (!TEST_int_eq(gen_tick_called,
  6832. (tick_key_renew
  6833. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  6834. || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
  6835. ? 1 : 0)
  6836. || !TEST_int_eq(dec_tick_called, 1))
  6837. goto end;
  6838. testresult = 1;
  6839. end:
  6840. SSL_SESSION_free(clntsess);
  6841. SSL_free(serverssl);
  6842. SSL_free(clientssl);
  6843. SSL_CTX_free(sctx);
  6844. SSL_CTX_free(cctx);
  6845. return testresult;
  6846. }
  6847. /*
  6848. * Test incorrect shutdown.
  6849. * Test 0: client does not shutdown properly,
  6850. * server does not set SSL_OP_IGNORE_UNEXPECTED_EOF,
  6851. * server should get SSL_ERROR_SSL
  6852. * Test 1: client does not shutdown properly,
  6853. * server sets SSL_OP_IGNORE_UNEXPECTED_EOF,
  6854. * server should get SSL_ERROR_ZERO_RETURN
  6855. */
  6856. static int test_incorrect_shutdown(int tst)
  6857. {
  6858. SSL_CTX *cctx = NULL, *sctx = NULL;
  6859. SSL *clientssl = NULL, *serverssl = NULL;
  6860. int testresult = 0;
  6861. char buf[80];
  6862. BIO *c2s;
  6863. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6864. TLS_client_method(), 0, 0,
  6865. &sctx, &cctx, cert, privkey)))
  6866. goto end;
  6867. if (tst == 1)
  6868. SSL_CTX_set_options(sctx, SSL_OP_IGNORE_UNEXPECTED_EOF);
  6869. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6870. NULL, NULL)))
  6871. goto end;
  6872. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6873. SSL_ERROR_NONE)))
  6874. goto end;
  6875. c2s = SSL_get_rbio(serverssl);
  6876. BIO_set_mem_eof_return(c2s, 0);
  6877. if (!TEST_false(SSL_read(serverssl, buf, sizeof(buf))))
  6878. goto end;
  6879. if (tst == 0 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL) )
  6880. goto end;
  6881. if (tst == 1 && !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_ZERO_RETURN) )
  6882. goto end;
  6883. testresult = 1;
  6884. end:
  6885. SSL_free(serverssl);
  6886. SSL_free(clientssl);
  6887. SSL_CTX_free(sctx);
  6888. SSL_CTX_free(cctx);
  6889. return testresult;
  6890. }
  6891. /*
  6892. * Test bi-directional shutdown.
  6893. * Test 0: TLSv1.2
  6894. * Test 1: TLSv1.2, server continues to read/write after client shutdown
  6895. * Test 2: TLSv1.3, no pending NewSessionTicket messages
  6896. * Test 3: TLSv1.3, pending NewSessionTicket messages
  6897. * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
  6898. * sends key update, client reads it
  6899. * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
  6900. * sends CertificateRequest, client reads and ignores it
  6901. * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
  6902. * doesn't read it
  6903. */
  6904. static int test_shutdown(int tst)
  6905. {
  6906. SSL_CTX *cctx = NULL, *sctx = NULL;
  6907. SSL *clientssl = NULL, *serverssl = NULL;
  6908. int testresult = 0;
  6909. char msg[] = "A test message";
  6910. char buf[80];
  6911. size_t written, readbytes;
  6912. SSL_SESSION *sess;
  6913. #ifdef OPENSSL_NO_TLS1_2
  6914. if (tst <= 1)
  6915. return 1;
  6916. #endif
  6917. #ifdef OSSL_NO_USABLE_TLS1_3
  6918. if (tst >= 2)
  6919. return 1;
  6920. #endif
  6921. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  6922. TLS_client_method(),
  6923. TLS1_VERSION,
  6924. (tst <= 1) ? TLS1_2_VERSION
  6925. : TLS1_3_VERSION,
  6926. &sctx, &cctx, cert, privkey)))
  6927. goto end;
  6928. if (tst == 5)
  6929. SSL_CTX_set_post_handshake_auth(cctx, 1);
  6930. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  6931. NULL, NULL)))
  6932. goto end;
  6933. if (tst == 3) {
  6934. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  6935. SSL_ERROR_NONE, 1, 0))
  6936. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6937. || !TEST_false(SSL_SESSION_is_resumable(sess)))
  6938. goto end;
  6939. } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  6940. SSL_ERROR_NONE))
  6941. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  6942. || !TEST_true(SSL_SESSION_is_resumable(sess))) {
  6943. goto end;
  6944. }
  6945. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  6946. goto end;
  6947. if (tst >= 4) {
  6948. /*
  6949. * Reading on the server after the client has sent close_notify should
  6950. * fail and provide SSL_ERROR_ZERO_RETURN
  6951. */
  6952. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  6953. || !TEST_int_eq(SSL_get_error(serverssl, 0),
  6954. SSL_ERROR_ZERO_RETURN)
  6955. || !TEST_int_eq(SSL_get_shutdown(serverssl),
  6956. SSL_RECEIVED_SHUTDOWN)
  6957. /*
  6958. * Even though we're shutdown on receive we should still be
  6959. * able to write.
  6960. */
  6961. || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  6962. goto end;
  6963. if (tst == 4
  6964. && !TEST_true(SSL_key_update(serverssl,
  6965. SSL_KEY_UPDATE_REQUESTED)))
  6966. goto end;
  6967. if (tst == 5) {
  6968. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  6969. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  6970. goto end;
  6971. }
  6972. if ((tst == 4 || tst == 5)
  6973. && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  6974. goto end;
  6975. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  6976. goto end;
  6977. if (tst == 4 || tst == 5) {
  6978. /* Should still be able to read data from server */
  6979. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  6980. &readbytes))
  6981. || !TEST_size_t_eq(readbytes, sizeof(msg))
  6982. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
  6983. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  6984. &readbytes))
  6985. || !TEST_size_t_eq(readbytes, sizeof(msg))
  6986. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
  6987. goto end;
  6988. }
  6989. }
  6990. /* Writing on the client after sending close_notify shouldn't be possible */
  6991. if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
  6992. goto end;
  6993. if (tst < 4) {
  6994. /*
  6995. * For these tests the client has sent close_notify but it has not yet
  6996. * been received by the server. The server has not sent close_notify
  6997. * yet.
  6998. */
  6999. if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
  7000. /*
  7001. * Writing on the server after sending close_notify shouldn't
  7002. * be possible.
  7003. */
  7004. || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  7005. || !TEST_int_eq(SSL_shutdown(clientssl), 1)
  7006. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7007. || !TEST_true(SSL_SESSION_is_resumable(sess))
  7008. || !TEST_int_eq(SSL_shutdown(serverssl), 1))
  7009. goto end;
  7010. } else if (tst == 4 || tst == 5) {
  7011. /*
  7012. * In this test the client has sent close_notify and it has been
  7013. * received by the server which has responded with a close_notify. The
  7014. * client needs to read the close_notify sent by the server.
  7015. */
  7016. if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
  7017. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  7018. || !TEST_true(SSL_SESSION_is_resumable(sess)))
  7019. goto end;
  7020. } else {
  7021. /*
  7022. * tst == 6
  7023. *
  7024. * The client has sent close_notify and is expecting a close_notify
  7025. * back, but instead there is application data first. The shutdown
  7026. * should fail with a fatal error.
  7027. */
  7028. if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
  7029. || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
  7030. goto end;
  7031. }
  7032. testresult = 1;
  7033. end:
  7034. SSL_free(serverssl);
  7035. SSL_free(clientssl);
  7036. SSL_CTX_free(sctx);
  7037. SSL_CTX_free(cctx);
  7038. return testresult;
  7039. }
  7040. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7041. static int cert_cb_cnt;
  7042. static int cert_cb(SSL *s, void *arg)
  7043. {
  7044. SSL_CTX *ctx = (SSL_CTX *)arg;
  7045. BIO *in = NULL;
  7046. EVP_PKEY *pkey = NULL;
  7047. X509 *x509 = NULL, *rootx = NULL;
  7048. STACK_OF(X509) *chain = NULL;
  7049. char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
  7050. int ret = 0;
  7051. if (cert_cb_cnt == 0) {
  7052. /* Suspend the handshake */
  7053. cert_cb_cnt++;
  7054. return -1;
  7055. } else if (cert_cb_cnt == 1) {
  7056. /*
  7057. * Update the SSL_CTX, set the certificate and private key and then
  7058. * continue the handshake normally.
  7059. */
  7060. if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
  7061. return 0;
  7062. if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
  7063. || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
  7064. SSL_FILETYPE_PEM))
  7065. || !TEST_true(SSL_check_private_key(s)))
  7066. return 0;
  7067. cert_cb_cnt++;
  7068. return 1;
  7069. } else if (cert_cb_cnt == 3) {
  7070. int rv;
  7071. rootfile = test_mk_file_path(certsdir, "rootcert.pem");
  7072. ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  7073. ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  7074. if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
  7075. goto out;
  7076. chain = sk_X509_new_null();
  7077. if (!TEST_ptr(chain))
  7078. goto out;
  7079. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7080. || !TEST_int_gt(BIO_read_filename(in, rootfile), 0)
  7081. || !TEST_ptr(rootx = X509_new_ex(libctx, NULL))
  7082. || !TEST_ptr(PEM_read_bio_X509(in, &rootx, NULL, NULL))
  7083. || !TEST_true(sk_X509_push(chain, rootx)))
  7084. goto out;
  7085. rootx = NULL;
  7086. BIO_free(in);
  7087. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7088. || !TEST_int_gt(BIO_read_filename(in, ecdsacert), 0)
  7089. || !TEST_ptr(x509 = X509_new_ex(libctx, NULL))
  7090. || !TEST_ptr(PEM_read_bio_X509(in, &x509, NULL, NULL)))
  7091. goto out;
  7092. BIO_free(in);
  7093. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  7094. || !TEST_int_gt(BIO_read_filename(in, ecdsakey), 0)
  7095. || !TEST_ptr(pkey = PEM_read_bio_PrivateKey_ex(in, NULL,
  7096. NULL, NULL,
  7097. libctx, NULL)))
  7098. goto out;
  7099. rv = SSL_check_chain(s, x509, pkey, chain);
  7100. /*
  7101. * If the cert doesn't show as valid here (e.g., because we don't
  7102. * have any shared sigalgs), then we will not set it, and there will
  7103. * be no certificate at all on the SSL or SSL_CTX. This, in turn,
  7104. * will cause tls_choose_sigalgs() to fail the connection.
  7105. */
  7106. if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
  7107. == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
  7108. if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
  7109. goto out;
  7110. }
  7111. ret = 1;
  7112. }
  7113. /* Abort the handshake */
  7114. out:
  7115. OPENSSL_free(ecdsacert);
  7116. OPENSSL_free(ecdsakey);
  7117. OPENSSL_free(rootfile);
  7118. BIO_free(in);
  7119. EVP_PKEY_free(pkey);
  7120. X509_free(x509);
  7121. X509_free(rootx);
  7122. OSSL_STACK_OF_X509_free(chain);
  7123. return ret;
  7124. }
  7125. /*
  7126. * Test the certificate callback.
  7127. * Test 0: Callback fails
  7128. * Test 1: Success - no SSL_set_SSL_CTX() in the callback
  7129. * Test 2: Success - SSL_set_SSL_CTX() in the callback
  7130. * Test 3: Success - Call SSL_check_chain from the callback
  7131. * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
  7132. * chain
  7133. * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
  7134. */
  7135. static int test_cert_cb_int(int prot, int tst)
  7136. {
  7137. SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
  7138. SSL *clientssl = NULL, *serverssl = NULL;
  7139. int testresult = 0, ret;
  7140. #ifdef OPENSSL_NO_EC
  7141. /* We use an EC cert in these tests, so we skip in a no-ec build */
  7142. if (tst >= 3)
  7143. return 1;
  7144. #endif
  7145. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7146. TLS_client_method(),
  7147. TLS1_VERSION,
  7148. prot,
  7149. &sctx, &cctx, NULL, NULL)))
  7150. goto end;
  7151. if (tst == 0)
  7152. cert_cb_cnt = -1;
  7153. else if (tst >= 3)
  7154. cert_cb_cnt = 3;
  7155. else
  7156. cert_cb_cnt = 0;
  7157. if (tst == 2) {
  7158. snictx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  7159. if (!TEST_ptr(snictx))
  7160. goto end;
  7161. }
  7162. SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
  7163. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7164. NULL, NULL)))
  7165. goto end;
  7166. if (tst == 4) {
  7167. /*
  7168. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7169. * the chain doesn't meet (the root uses an RSA cert)
  7170. */
  7171. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7172. "ecdsa_secp256r1_sha256")))
  7173. goto end;
  7174. } else if (tst == 5) {
  7175. /*
  7176. * We cause SSL_check_chain() to fail by specifying sig_algs that
  7177. * the ee cert doesn't meet (the ee uses an ECDSA cert)
  7178. */
  7179. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  7180. "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
  7181. goto end;
  7182. }
  7183. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  7184. if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
  7185. || (tst > 0
  7186. && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
  7187. goto end;
  7188. }
  7189. testresult = 1;
  7190. end:
  7191. SSL_free(serverssl);
  7192. SSL_free(clientssl);
  7193. SSL_CTX_free(sctx);
  7194. SSL_CTX_free(cctx);
  7195. SSL_CTX_free(snictx);
  7196. return testresult;
  7197. }
  7198. #endif
  7199. static int test_cert_cb(int tst)
  7200. {
  7201. int testresult = 1;
  7202. #ifndef OPENSSL_NO_TLS1_2
  7203. testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
  7204. #endif
  7205. #ifndef OSSL_NO_USABLE_TLS1_3
  7206. testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
  7207. #endif
  7208. return testresult;
  7209. }
  7210. static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
  7211. {
  7212. X509 *xcert;
  7213. EVP_PKEY *privpkey;
  7214. BIO *in = NULL;
  7215. BIO *priv_in = NULL;
  7216. /* Check that SSL_get0_peer_certificate() returns something sensible */
  7217. if (!TEST_ptr(SSL_get0_peer_certificate(ssl)))
  7218. return 0;
  7219. in = BIO_new_file(cert, "r");
  7220. if (!TEST_ptr(in))
  7221. return 0;
  7222. if (!TEST_ptr(xcert = X509_new_ex(libctx, NULL))
  7223. || !TEST_ptr(PEM_read_bio_X509(in, &xcert, NULL, NULL))
  7224. || !TEST_ptr(priv_in = BIO_new_file(privkey, "r"))
  7225. || !TEST_ptr(privpkey = PEM_read_bio_PrivateKey_ex(priv_in, NULL,
  7226. NULL, NULL,
  7227. libctx, NULL)))
  7228. goto err;
  7229. *x509 = xcert;
  7230. *pkey = privpkey;
  7231. BIO_free(in);
  7232. BIO_free(priv_in);
  7233. return 1;
  7234. err:
  7235. X509_free(xcert);
  7236. BIO_free(in);
  7237. BIO_free(priv_in);
  7238. return 0;
  7239. }
  7240. static int test_client_cert_cb(int tst)
  7241. {
  7242. SSL_CTX *cctx = NULL, *sctx = NULL;
  7243. SSL *clientssl = NULL, *serverssl = NULL;
  7244. int testresult = 0;
  7245. #ifdef OPENSSL_NO_TLS1_2
  7246. if (tst == 0)
  7247. return 1;
  7248. #endif
  7249. #ifdef OSSL_NO_USABLE_TLS1_3
  7250. if (tst == 1)
  7251. return 1;
  7252. #endif
  7253. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7254. TLS_client_method(),
  7255. TLS1_VERSION,
  7256. tst == 0 ? TLS1_2_VERSION
  7257. : TLS1_3_VERSION,
  7258. &sctx, &cctx, cert, privkey)))
  7259. goto end;
  7260. /*
  7261. * Test that setting a client_cert_cb results in a client certificate being
  7262. * sent.
  7263. */
  7264. SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
  7265. SSL_CTX_set_verify(sctx,
  7266. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  7267. verify_cb);
  7268. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7269. NULL, NULL))
  7270. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7271. SSL_ERROR_NONE)))
  7272. goto end;
  7273. testresult = 1;
  7274. end:
  7275. SSL_free(serverssl);
  7276. SSL_free(clientssl);
  7277. SSL_CTX_free(sctx);
  7278. SSL_CTX_free(cctx);
  7279. return testresult;
  7280. }
  7281. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  7282. /*
  7283. * Test setting certificate authorities on both client and server.
  7284. *
  7285. * Test 0: SSL_CTX_set0_CA_list() only
  7286. * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
  7287. * Test 2: Only SSL_CTX_set_client_CA_list()
  7288. */
  7289. static int test_ca_names_int(int prot, int tst)
  7290. {
  7291. SSL_CTX *cctx = NULL, *sctx = NULL;
  7292. SSL *clientssl = NULL, *serverssl = NULL;
  7293. int testresult = 0;
  7294. size_t i;
  7295. X509_NAME *name[] = { NULL, NULL, NULL, NULL };
  7296. char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
  7297. STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
  7298. const STACK_OF(X509_NAME) *sktmp = NULL;
  7299. for (i = 0; i < OSSL_NELEM(name); i++) {
  7300. name[i] = X509_NAME_new();
  7301. if (!TEST_ptr(name[i])
  7302. || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
  7303. MBSTRING_ASC,
  7304. (unsigned char *)
  7305. strnames[i],
  7306. -1, -1, 0)))
  7307. goto end;
  7308. }
  7309. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7310. TLS_client_method(),
  7311. TLS1_VERSION,
  7312. prot,
  7313. &sctx, &cctx, cert, privkey)))
  7314. goto end;
  7315. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  7316. if (tst == 0 || tst == 1) {
  7317. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7318. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
  7319. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
  7320. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7321. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
  7322. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
  7323. goto end;
  7324. SSL_CTX_set0_CA_list(sctx, sk1);
  7325. SSL_CTX_set0_CA_list(cctx, sk2);
  7326. sk1 = sk2 = NULL;
  7327. }
  7328. if (tst == 1 || tst == 2) {
  7329. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  7330. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
  7331. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
  7332. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  7333. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
  7334. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
  7335. goto end;
  7336. SSL_CTX_set_client_CA_list(sctx, sk1);
  7337. SSL_CTX_set_client_CA_list(cctx, sk2);
  7338. sk1 = sk2 = NULL;
  7339. }
  7340. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7341. NULL, NULL))
  7342. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  7343. SSL_ERROR_NONE)))
  7344. goto end;
  7345. /*
  7346. * We only expect certificate authorities to have been sent to the server
  7347. * if we are using TLSv1.3 and SSL_set0_CA_list() was used
  7348. */
  7349. sktmp = SSL_get0_peer_CA_list(serverssl);
  7350. if (prot == TLS1_3_VERSION
  7351. && (tst == 0 || tst == 1)) {
  7352. if (!TEST_ptr(sktmp)
  7353. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7354. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7355. name[0]), 0)
  7356. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7357. name[1]), 0))
  7358. goto end;
  7359. } else if (!TEST_ptr_null(sktmp)) {
  7360. goto end;
  7361. }
  7362. /*
  7363. * In all tests we expect certificate authorities to have been sent to the
  7364. * client. However, SSL_set_client_CA_list() should override
  7365. * SSL_set0_CA_list()
  7366. */
  7367. sktmp = SSL_get0_peer_CA_list(clientssl);
  7368. if (!TEST_ptr(sktmp)
  7369. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  7370. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  7371. name[tst == 0 ? 0 : 2]), 0)
  7372. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  7373. name[tst == 0 ? 1 : 3]), 0))
  7374. goto end;
  7375. testresult = 1;
  7376. end:
  7377. SSL_free(serverssl);
  7378. SSL_free(clientssl);
  7379. SSL_CTX_free(sctx);
  7380. SSL_CTX_free(cctx);
  7381. for (i = 0; i < OSSL_NELEM(name); i++)
  7382. X509_NAME_free(name[i]);
  7383. sk_X509_NAME_pop_free(sk1, X509_NAME_free);
  7384. sk_X509_NAME_pop_free(sk2, X509_NAME_free);
  7385. return testresult;
  7386. }
  7387. #endif
  7388. static int test_ca_names(int tst)
  7389. {
  7390. int testresult = 1;
  7391. #ifndef OPENSSL_NO_TLS1_2
  7392. testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
  7393. #endif
  7394. #ifndef OSSL_NO_USABLE_TLS1_3
  7395. testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
  7396. #endif
  7397. return testresult;
  7398. }
  7399. #ifndef OPENSSL_NO_TLS1_2
  7400. static const char *multiblock_cipherlist_data[]=
  7401. {
  7402. "AES128-SHA",
  7403. "AES128-SHA256",
  7404. "AES256-SHA",
  7405. "AES256-SHA256",
  7406. };
  7407. /* Reduce the fragment size - so the multiblock test buffer can be small */
  7408. # define MULTIBLOCK_FRAGSIZE 512
  7409. static int test_multiblock_write(int test_index)
  7410. {
  7411. static const char *fetchable_ciphers[]=
  7412. {
  7413. "AES-128-CBC-HMAC-SHA1",
  7414. "AES-128-CBC-HMAC-SHA256",
  7415. "AES-256-CBC-HMAC-SHA1",
  7416. "AES-256-CBC-HMAC-SHA256"
  7417. };
  7418. const char *cipherlist = multiblock_cipherlist_data[test_index];
  7419. const SSL_METHOD *smeth = TLS_server_method();
  7420. const SSL_METHOD *cmeth = TLS_client_method();
  7421. int min_version = TLS1_VERSION;
  7422. int max_version = TLS1_2_VERSION; /* Don't select TLS1_3 */
  7423. SSL_CTX *cctx = NULL, *sctx = NULL;
  7424. SSL *clientssl = NULL, *serverssl = NULL;
  7425. int testresult = 0;
  7426. /*
  7427. * Choose a buffer large enough to perform a multi-block operation
  7428. * i.e: write_len >= 4 * frag_size
  7429. * 9 * is chosen so that multiple multiblocks are used + some leftover.
  7430. */
  7431. unsigned char msg[MULTIBLOCK_FRAGSIZE * 9];
  7432. unsigned char buf[sizeof(msg)], *p = buf;
  7433. size_t readbytes, written, len;
  7434. EVP_CIPHER *ciph = NULL;
  7435. /*
  7436. * Check if the cipher exists before attempting to use it since it only has
  7437. * a hardware specific implementation.
  7438. */
  7439. ciph = EVP_CIPHER_fetch(libctx, fetchable_ciphers[test_index], "");
  7440. if (ciph == NULL) {
  7441. TEST_skip("Multiblock cipher is not available for %s", cipherlist);
  7442. return 1;
  7443. }
  7444. EVP_CIPHER_free(ciph);
  7445. /* Set up a buffer with some data that will be sent to the client */
  7446. RAND_bytes(msg, sizeof(msg));
  7447. if (!TEST_true(create_ssl_ctx_pair(libctx, smeth, cmeth, min_version,
  7448. max_version, &sctx, &cctx, cert,
  7449. privkey)))
  7450. goto end;
  7451. if (!TEST_true(SSL_CTX_set_max_send_fragment(sctx, MULTIBLOCK_FRAGSIZE)))
  7452. goto end;
  7453. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7454. NULL, NULL)))
  7455. goto end;
  7456. /* settings to force it to use AES-CBC-HMAC_SHA */
  7457. SSL_set_options(serverssl, SSL_OP_NO_ENCRYPT_THEN_MAC);
  7458. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, cipherlist)))
  7459. goto end;
  7460. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7461. goto end;
  7462. if (!TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  7463. || !TEST_size_t_eq(written, sizeof(msg)))
  7464. goto end;
  7465. len = written;
  7466. while (len > 0) {
  7467. if (!TEST_true(SSL_read_ex(clientssl, p, MULTIBLOCK_FRAGSIZE, &readbytes)))
  7468. goto end;
  7469. p += readbytes;
  7470. len -= readbytes;
  7471. }
  7472. if (!TEST_mem_eq(msg, sizeof(msg), buf, sizeof(buf)))
  7473. goto end;
  7474. testresult = 1;
  7475. end:
  7476. SSL_free(serverssl);
  7477. SSL_free(clientssl);
  7478. SSL_CTX_free(sctx);
  7479. SSL_CTX_free(cctx);
  7480. return testresult;
  7481. }
  7482. #endif /* OPENSSL_NO_TLS1_2 */
  7483. static int test_session_timeout(int test)
  7484. {
  7485. /*
  7486. * Test session ordering and timeout
  7487. * Can't explicitly test performance of the new code,
  7488. * but can test to see if the ordering of the sessions
  7489. * are correct, and they they are removed as expected
  7490. */
  7491. SSL_SESSION *early = NULL;
  7492. SSL_SESSION *middle = NULL;
  7493. SSL_SESSION *late = NULL;
  7494. SSL_CTX *ctx;
  7495. int testresult = 0;
  7496. long now = (long)time(NULL);
  7497. #define TIMEOUT 10
  7498. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_method()))
  7499. || !TEST_ptr(early = SSL_SESSION_new())
  7500. || !TEST_ptr(middle = SSL_SESSION_new())
  7501. || !TEST_ptr(late = SSL_SESSION_new()))
  7502. goto end;
  7503. /* assign unique session ids */
  7504. early->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  7505. memset(early->session_id, 1, SSL3_SSL_SESSION_ID_LENGTH);
  7506. middle->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  7507. memset(middle->session_id, 2, SSL3_SSL_SESSION_ID_LENGTH);
  7508. late->session_id_length = SSL3_SSL_SESSION_ID_LENGTH;
  7509. memset(late->session_id, 3, SSL3_SSL_SESSION_ID_LENGTH);
  7510. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  7511. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  7512. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  7513. goto end;
  7514. /* Make sure they are all added */
  7515. if (!TEST_ptr(early->prev)
  7516. || !TEST_ptr(middle->prev)
  7517. || !TEST_ptr(late->prev))
  7518. goto end;
  7519. if (!TEST_int_ne(SSL_SESSION_set_time(early, now - 10), 0)
  7520. || !TEST_int_ne(SSL_SESSION_set_time(middle, now), 0)
  7521. || !TEST_int_ne(SSL_SESSION_set_time(late, now + 10), 0))
  7522. goto end;
  7523. if (!TEST_int_ne(SSL_SESSION_set_timeout(early, TIMEOUT), 0)
  7524. || !TEST_int_ne(SSL_SESSION_set_timeout(middle, TIMEOUT), 0)
  7525. || !TEST_int_ne(SSL_SESSION_set_timeout(late, TIMEOUT), 0))
  7526. goto end;
  7527. /* Make sure they are all still there */
  7528. if (!TEST_ptr(early->prev)
  7529. || !TEST_ptr(middle->prev)
  7530. || !TEST_ptr(late->prev))
  7531. goto end;
  7532. /* Make sure they are in the expected order */
  7533. if (!TEST_ptr_eq(late->next, middle)
  7534. || !TEST_ptr_eq(middle->next, early)
  7535. || !TEST_ptr_eq(early->prev, middle)
  7536. || !TEST_ptr_eq(middle->prev, late))
  7537. goto end;
  7538. /* This should remove "early" */
  7539. SSL_CTX_flush_sessions(ctx, now + TIMEOUT - 1);
  7540. if (!TEST_ptr_null(early->prev)
  7541. || !TEST_ptr(middle->prev)
  7542. || !TEST_ptr(late->prev))
  7543. goto end;
  7544. /* This should remove "middle" */
  7545. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 1);
  7546. if (!TEST_ptr_null(early->prev)
  7547. || !TEST_ptr_null(middle->prev)
  7548. || !TEST_ptr(late->prev))
  7549. goto end;
  7550. /* This should remove "late" */
  7551. SSL_CTX_flush_sessions(ctx, now + TIMEOUT + 11);
  7552. if (!TEST_ptr_null(early->prev)
  7553. || !TEST_ptr_null(middle->prev)
  7554. || !TEST_ptr_null(late->prev))
  7555. goto end;
  7556. /* Add them back in again */
  7557. if (!TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  7558. || !TEST_int_eq(SSL_CTX_add_session(ctx, middle), 1)
  7559. || !TEST_int_eq(SSL_CTX_add_session(ctx, late), 1))
  7560. goto end;
  7561. /* Make sure they are all added */
  7562. if (!TEST_ptr(early->prev)
  7563. || !TEST_ptr(middle->prev)
  7564. || !TEST_ptr(late->prev))
  7565. goto end;
  7566. /* This should remove all of them */
  7567. SSL_CTX_flush_sessions(ctx, 0);
  7568. if (!TEST_ptr_null(early->prev)
  7569. || !TEST_ptr_null(middle->prev)
  7570. || !TEST_ptr_null(late->prev))
  7571. goto end;
  7572. (void)SSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_UPDATE_TIME
  7573. | SSL_CTX_get_session_cache_mode(ctx));
  7574. /* make sure |now| is NOT equal to the current time */
  7575. now -= 10;
  7576. if (!TEST_int_ne(SSL_SESSION_set_time(early, now), 0)
  7577. || !TEST_int_eq(SSL_CTX_add_session(ctx, early), 1)
  7578. || !TEST_long_ne(SSL_SESSION_get_time(early), now))
  7579. goto end;
  7580. testresult = 1;
  7581. end:
  7582. SSL_CTX_free(ctx);
  7583. SSL_SESSION_free(early);
  7584. SSL_SESSION_free(middle);
  7585. SSL_SESSION_free(late);
  7586. return testresult;
  7587. }
  7588. /*
  7589. * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
  7590. * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
  7591. * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
  7592. * Test 3: Client does not set servername on initial handshake (TLSv1.2)
  7593. * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
  7594. * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
  7595. * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
  7596. * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
  7597. * Test 8: Client does not set servername on initial handshake(TLSv1.3)
  7598. * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
  7599. */
  7600. static int test_servername(int tst)
  7601. {
  7602. SSL_CTX *cctx = NULL, *sctx = NULL;
  7603. SSL *clientssl = NULL, *serverssl = NULL;
  7604. int testresult = 0;
  7605. SSL_SESSION *sess = NULL;
  7606. const char *sexpectedhost = NULL, *cexpectedhost = NULL;
  7607. #ifdef OPENSSL_NO_TLS1_2
  7608. if (tst <= 4)
  7609. return 1;
  7610. #endif
  7611. #ifdef OSSL_NO_USABLE_TLS1_3
  7612. if (tst >= 5)
  7613. return 1;
  7614. #endif
  7615. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7616. TLS_client_method(),
  7617. TLS1_VERSION,
  7618. (tst <= 4) ? TLS1_2_VERSION
  7619. : TLS1_3_VERSION,
  7620. &sctx, &cctx, cert, privkey))
  7621. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7622. NULL, NULL)))
  7623. goto end;
  7624. if (tst != 1 && tst != 6) {
  7625. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  7626. hostname_cb)))
  7627. goto end;
  7628. }
  7629. if (tst != 3 && tst != 8) {
  7630. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  7631. goto end;
  7632. sexpectedhost = cexpectedhost = "goodhost";
  7633. }
  7634. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7635. goto end;
  7636. if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
  7637. cexpectedhost)
  7638. || !TEST_str_eq(SSL_get_servername(serverssl,
  7639. TLSEXT_NAMETYPE_host_name),
  7640. sexpectedhost))
  7641. goto end;
  7642. /* Now repeat with a resumption handshake */
  7643. if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
  7644. || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
  7645. || !TEST_true(SSL_SESSION_is_resumable(sess))
  7646. || !TEST_int_eq(SSL_shutdown(serverssl), 0))
  7647. goto end;
  7648. SSL_free(clientssl);
  7649. SSL_free(serverssl);
  7650. clientssl = serverssl = NULL;
  7651. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  7652. NULL)))
  7653. goto end;
  7654. if (!TEST_true(SSL_set_session(clientssl, sess)))
  7655. goto end;
  7656. sexpectedhost = cexpectedhost = "goodhost";
  7657. if (tst == 2 || tst == 7) {
  7658. /* Set an inconsistent hostname */
  7659. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
  7660. goto end;
  7661. /*
  7662. * In TLSv1.2 we expect the hostname from the original handshake, in
  7663. * TLSv1.3 we expect the hostname from this handshake
  7664. */
  7665. if (tst == 7)
  7666. sexpectedhost = cexpectedhost = "altgoodhost";
  7667. if (!TEST_str_eq(SSL_get_servername(clientssl,
  7668. TLSEXT_NAMETYPE_host_name),
  7669. "altgoodhost"))
  7670. goto end;
  7671. } else if (tst == 4 || tst == 9) {
  7672. /*
  7673. * A TLSv1.3 session does not associate a session with a servername,
  7674. * but a TLSv1.2 session does.
  7675. */
  7676. if (tst == 9)
  7677. sexpectedhost = cexpectedhost = NULL;
  7678. if (!TEST_str_eq(SSL_get_servername(clientssl,
  7679. TLSEXT_NAMETYPE_host_name),
  7680. cexpectedhost))
  7681. goto end;
  7682. } else {
  7683. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  7684. goto end;
  7685. /*
  7686. * In a TLSv1.2 resumption where the hostname was not acknowledged
  7687. * we expect the hostname on the server to be empty. On the client we
  7688. * return what was requested in this case.
  7689. *
  7690. * Similarly if the client didn't set a hostname on an original TLSv1.2
  7691. * session but is now, the server hostname will be empty, but the client
  7692. * is as we set it.
  7693. */
  7694. if (tst == 1 || tst == 3)
  7695. sexpectedhost = NULL;
  7696. if (!TEST_str_eq(SSL_get_servername(clientssl,
  7697. TLSEXT_NAMETYPE_host_name),
  7698. "goodhost"))
  7699. goto end;
  7700. }
  7701. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7702. goto end;
  7703. if (!TEST_true(SSL_session_reused(clientssl))
  7704. || !TEST_true(SSL_session_reused(serverssl))
  7705. || !TEST_str_eq(SSL_get_servername(clientssl,
  7706. TLSEXT_NAMETYPE_host_name),
  7707. cexpectedhost)
  7708. || !TEST_str_eq(SSL_get_servername(serverssl,
  7709. TLSEXT_NAMETYPE_host_name),
  7710. sexpectedhost))
  7711. goto end;
  7712. testresult = 1;
  7713. end:
  7714. SSL_SESSION_free(sess);
  7715. SSL_free(serverssl);
  7716. SSL_free(clientssl);
  7717. SSL_CTX_free(sctx);
  7718. SSL_CTX_free(cctx);
  7719. return testresult;
  7720. }
  7721. #if !defined(OPENSSL_NO_EC) \
  7722. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  7723. /*
  7724. * Test that if signature algorithms are not available, then we do not offer or
  7725. * accept them.
  7726. * Test 0: Two RSA sig algs available: both RSA sig algs shared
  7727. * Test 1: The client only has SHA2-256: only SHA2-256 algorithms shared
  7728. * Test 2: The server only has SHA2-256: only SHA2-256 algorithms shared
  7729. * Test 3: An RSA and an ECDSA sig alg available: both sig algs shared
  7730. * Test 4: The client only has an ECDSA sig alg: only ECDSA algorithms shared
  7731. * Test 5: The server only has an ECDSA sig alg: only ECDSA algorithms shared
  7732. */
  7733. static int test_sigalgs_available(int idx)
  7734. {
  7735. SSL_CTX *cctx = NULL, *sctx = NULL;
  7736. SSL *clientssl = NULL, *serverssl = NULL;
  7737. int testresult = 0;
  7738. OSSL_LIB_CTX *tmpctx = OSSL_LIB_CTX_new();
  7739. OSSL_LIB_CTX *clientctx = libctx, *serverctx = libctx;
  7740. OSSL_PROVIDER *filterprov = NULL;
  7741. int sig, hash;
  7742. if (!TEST_ptr(tmpctx))
  7743. goto end;
  7744. if (idx != 0 && idx != 3) {
  7745. if (!TEST_true(OSSL_PROVIDER_add_builtin(tmpctx, "filter",
  7746. filter_provider_init)))
  7747. goto end;
  7748. filterprov = OSSL_PROVIDER_load(tmpctx, "filter");
  7749. if (!TEST_ptr(filterprov))
  7750. goto end;
  7751. if (idx < 3) {
  7752. /*
  7753. * Only enable SHA2-256 so rsa_pss_rsae_sha384 should not be offered
  7754. * or accepted for the peer that uses this libctx. Note that libssl
  7755. * *requires* SHA2-256 to be available so we cannot disable that. We
  7756. * also need SHA1 for our certificate.
  7757. */
  7758. if (!TEST_true(filter_provider_set_filter(OSSL_OP_DIGEST,
  7759. "SHA2-256:SHA1")))
  7760. goto end;
  7761. } else {
  7762. if (!TEST_true(filter_provider_set_filter(OSSL_OP_SIGNATURE,
  7763. "ECDSA"))
  7764. || !TEST_true(filter_provider_set_filter(OSSL_OP_KEYMGMT,
  7765. "EC:X25519:X448")))
  7766. goto end;
  7767. }
  7768. if (idx == 1 || idx == 4)
  7769. clientctx = tmpctx;
  7770. else
  7771. serverctx = tmpctx;
  7772. }
  7773. cctx = SSL_CTX_new_ex(clientctx, NULL, TLS_client_method());
  7774. sctx = SSL_CTX_new_ex(serverctx, NULL, TLS_server_method());
  7775. if (!TEST_ptr(cctx) || !TEST_ptr(sctx))
  7776. goto end;
  7777. if (idx != 5) {
  7778. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7779. TLS_client_method(),
  7780. TLS1_VERSION,
  7781. 0,
  7782. &sctx, &cctx, cert, privkey)))
  7783. goto end;
  7784. } else {
  7785. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7786. TLS_client_method(),
  7787. TLS1_VERSION,
  7788. 0,
  7789. &sctx, &cctx, cert2, privkey2)))
  7790. goto end;
  7791. }
  7792. /* Ensure we only use TLSv1.2 ciphersuites based on SHA256 */
  7793. if (idx < 4) {
  7794. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7795. "ECDHE-RSA-AES128-GCM-SHA256")))
  7796. goto end;
  7797. } else {
  7798. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  7799. "ECDHE-ECDSA-AES128-GCM-SHA256")))
  7800. goto end;
  7801. }
  7802. if (idx < 3) {
  7803. if (!SSL_CTX_set1_sigalgs_list(cctx,
  7804. "rsa_pss_rsae_sha384"
  7805. ":rsa_pss_rsae_sha256")
  7806. || !SSL_CTX_set1_sigalgs_list(sctx,
  7807. "rsa_pss_rsae_sha384"
  7808. ":rsa_pss_rsae_sha256"))
  7809. goto end;
  7810. } else {
  7811. if (!SSL_CTX_set1_sigalgs_list(cctx, "rsa_pss_rsae_sha256:ECDSA+SHA256")
  7812. || !SSL_CTX_set1_sigalgs_list(sctx,
  7813. "rsa_pss_rsae_sha256:ECDSA+SHA256"))
  7814. goto end;
  7815. }
  7816. if (idx != 5
  7817. && (!TEST_int_eq(SSL_CTX_use_certificate_file(sctx, cert2,
  7818. SSL_FILETYPE_PEM), 1)
  7819. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(sctx,
  7820. privkey2,
  7821. SSL_FILETYPE_PEM), 1)
  7822. || !TEST_int_eq(SSL_CTX_check_private_key(sctx), 1)))
  7823. goto end;
  7824. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7825. NULL, NULL)))
  7826. goto end;
  7827. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7828. goto end;
  7829. /* For tests 0 and 3 we expect 2 shared sigalgs, otherwise exactly 1 */
  7830. if (!TEST_int_eq(SSL_get_shared_sigalgs(serverssl, 0, &sig, &hash, NULL,
  7831. NULL, NULL),
  7832. (idx == 0 || idx == 3) ? 2 : 1))
  7833. goto end;
  7834. if (!TEST_int_eq(hash, idx == 0 ? NID_sha384 : NID_sha256))
  7835. goto end;
  7836. if (!TEST_int_eq(sig, (idx == 4 || idx == 5) ? EVP_PKEY_EC
  7837. : NID_rsassaPss))
  7838. goto end;
  7839. testresult = filter_provider_check_clean_finish();
  7840. end:
  7841. SSL_free(serverssl);
  7842. SSL_free(clientssl);
  7843. SSL_CTX_free(sctx);
  7844. SSL_CTX_free(cctx);
  7845. OSSL_PROVIDER_unload(filterprov);
  7846. OSSL_LIB_CTX_free(tmpctx);
  7847. return testresult;
  7848. }
  7849. #endif /*
  7850. * !defined(OPENSSL_NO_EC) \
  7851. * && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  7852. */
  7853. #ifndef OPENSSL_NO_TLS1_3
  7854. /* This test can run in TLSv1.3 even if ec and dh are disabled */
  7855. static int test_pluggable_group(int idx)
  7856. {
  7857. SSL_CTX *cctx = NULL, *sctx = NULL;
  7858. SSL *clientssl = NULL, *serverssl = NULL;
  7859. int testresult = 0;
  7860. OSSL_PROVIDER *tlsprov = OSSL_PROVIDER_load(libctx, "tls-provider");
  7861. /* Check that we are not impacted by a provider without any groups */
  7862. OSSL_PROVIDER *legacyprov = OSSL_PROVIDER_load(libctx, "legacy");
  7863. const char *group_name = idx == 0 ? "xorgroup" : "xorkemgroup";
  7864. if (!TEST_ptr(tlsprov))
  7865. goto end;
  7866. if (legacyprov == NULL) {
  7867. /*
  7868. * In this case we assume we've been built with "no-legacy" and skip
  7869. * this test (there is no OPENSSL_NO_LEGACY)
  7870. */
  7871. testresult = 1;
  7872. goto end;
  7873. }
  7874. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7875. TLS_client_method(),
  7876. TLS1_3_VERSION,
  7877. TLS1_3_VERSION,
  7878. &sctx, &cctx, cert, privkey))
  7879. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7880. NULL, NULL)))
  7881. goto end;
  7882. if (!TEST_true(SSL_set1_groups_list(serverssl, group_name))
  7883. || !TEST_true(SSL_set1_groups_list(clientssl, group_name)))
  7884. goto end;
  7885. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  7886. goto end;
  7887. if (!TEST_str_eq(group_name,
  7888. SSL_group_to_name(serverssl, SSL_get_shared_group(serverssl, 0))))
  7889. goto end;
  7890. testresult = 1;
  7891. end:
  7892. SSL_free(serverssl);
  7893. SSL_free(clientssl);
  7894. SSL_CTX_free(sctx);
  7895. SSL_CTX_free(cctx);
  7896. OSSL_PROVIDER_unload(tlsprov);
  7897. OSSL_PROVIDER_unload(legacyprov);
  7898. return testresult;
  7899. }
  7900. #endif
  7901. #ifndef OPENSSL_NO_TLS1_2
  7902. static int test_ssl_dup(void)
  7903. {
  7904. SSL_CTX *cctx = NULL, *sctx = NULL;
  7905. SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
  7906. int testresult = 0;
  7907. BIO *rbio = NULL, *wbio = NULL;
  7908. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  7909. TLS_client_method(),
  7910. 0,
  7911. 0,
  7912. &sctx, &cctx, cert, privkey)))
  7913. goto end;
  7914. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  7915. NULL, NULL)))
  7916. goto end;
  7917. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  7918. || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
  7919. goto end;
  7920. client2ssl = SSL_dup(clientssl);
  7921. rbio = SSL_get_rbio(clientssl);
  7922. if (!TEST_ptr(rbio)
  7923. || !TEST_true(BIO_up_ref(rbio)))
  7924. goto end;
  7925. SSL_set0_rbio(client2ssl, rbio);
  7926. rbio = NULL;
  7927. wbio = SSL_get_wbio(clientssl);
  7928. if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
  7929. goto end;
  7930. SSL_set0_wbio(client2ssl, wbio);
  7931. rbio = NULL;
  7932. if (!TEST_ptr(client2ssl)
  7933. /* Handshake not started so pointers should be different */
  7934. || !TEST_ptr_ne(clientssl, client2ssl))
  7935. goto end;
  7936. if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
  7937. || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
  7938. goto end;
  7939. if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
  7940. goto end;
  7941. SSL_free(clientssl);
  7942. clientssl = SSL_dup(client2ssl);
  7943. if (!TEST_ptr(clientssl)
  7944. /* Handshake has finished so pointers should be the same */
  7945. || !TEST_ptr_eq(clientssl, client2ssl))
  7946. goto end;
  7947. testresult = 1;
  7948. end:
  7949. SSL_free(serverssl);
  7950. SSL_free(clientssl);
  7951. SSL_free(client2ssl);
  7952. SSL_CTX_free(sctx);
  7953. SSL_CTX_free(cctx);
  7954. return testresult;
  7955. }
  7956. # ifndef OPENSSL_NO_DH
  7957. static EVP_PKEY *tmp_dh_params = NULL;
  7958. /* Helper function for the test_set_tmp_dh() tests */
  7959. static EVP_PKEY *get_tmp_dh_params(void)
  7960. {
  7961. if (tmp_dh_params == NULL) {
  7962. BIGNUM *p = NULL;
  7963. OSSL_PARAM_BLD *tmpl = NULL;
  7964. EVP_PKEY_CTX *pctx = NULL;
  7965. OSSL_PARAM *params = NULL;
  7966. EVP_PKEY *dhpkey = NULL;
  7967. p = BN_get_rfc3526_prime_2048(NULL);
  7968. if (!TEST_ptr(p))
  7969. goto end;
  7970. pctx = EVP_PKEY_CTX_new_from_name(libctx, "DH", NULL);
  7971. if (!TEST_ptr(pctx)
  7972. || !TEST_int_eq(EVP_PKEY_fromdata_init(pctx), 1))
  7973. goto end;
  7974. tmpl = OSSL_PARAM_BLD_new();
  7975. if (!TEST_ptr(tmpl)
  7976. || !TEST_true(OSSL_PARAM_BLD_push_BN(tmpl,
  7977. OSSL_PKEY_PARAM_FFC_P,
  7978. p))
  7979. || !TEST_true(OSSL_PARAM_BLD_push_uint(tmpl,
  7980. OSSL_PKEY_PARAM_FFC_G,
  7981. 2)))
  7982. goto end;
  7983. params = OSSL_PARAM_BLD_to_param(tmpl);
  7984. if (!TEST_ptr(params)
  7985. || !TEST_int_eq(EVP_PKEY_fromdata(pctx, &dhpkey,
  7986. EVP_PKEY_KEY_PARAMETERS,
  7987. params), 1))
  7988. goto end;
  7989. tmp_dh_params = dhpkey;
  7990. end:
  7991. BN_free(p);
  7992. EVP_PKEY_CTX_free(pctx);
  7993. OSSL_PARAM_BLD_free(tmpl);
  7994. OSSL_PARAM_free(params);
  7995. }
  7996. if (tmp_dh_params != NULL && !EVP_PKEY_up_ref(tmp_dh_params))
  7997. return NULL;
  7998. return tmp_dh_params;
  7999. }
  8000. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8001. /* Callback used by test_set_tmp_dh() */
  8002. static DH *tmp_dh_callback(SSL *s, int is_export, int keylen)
  8003. {
  8004. EVP_PKEY *dhpkey = get_tmp_dh_params();
  8005. DH *ret = NULL;
  8006. if (!TEST_ptr(dhpkey))
  8007. return NULL;
  8008. /*
  8009. * libssl does not free the returned DH, so we free it now knowing that even
  8010. * after we free dhpkey, there will still be a reference to the owning
  8011. * EVP_PKEY in tmp_dh_params, and so the DH object will live for the length
  8012. * of time we need it for.
  8013. */
  8014. ret = EVP_PKEY_get1_DH(dhpkey);
  8015. DH_free(ret);
  8016. EVP_PKEY_free(dhpkey);
  8017. return ret;
  8018. }
  8019. # endif
  8020. /*
  8021. * Test the various methods for setting temporary DH parameters
  8022. *
  8023. * Test 0: Default (no auto) setting
  8024. * Test 1: Explicit SSL_CTX auto off
  8025. * Test 2: Explicit SSL auto off
  8026. * Test 3: Explicit SSL_CTX auto on
  8027. * Test 4: Explicit SSL auto on
  8028. * Test 5: Explicit SSL_CTX auto off, custom DH params via EVP_PKEY
  8029. * Test 6: Explicit SSL auto off, custom DH params via EVP_PKEY
  8030. *
  8031. * The following are testing deprecated APIs, so we only run them if available
  8032. * Test 7: Explicit SSL_CTX auto off, custom DH params via DH
  8033. * Test 8: Explicit SSL auto off, custom DH params via DH
  8034. * Test 9: Explicit SSL_CTX auto off, custom DH params via callback
  8035. * Test 10: Explicit SSL auto off, custom DH params via callback
  8036. */
  8037. static int test_set_tmp_dh(int idx)
  8038. {
  8039. SSL_CTX *cctx = NULL, *sctx = NULL;
  8040. SSL *clientssl = NULL, *serverssl = NULL;
  8041. int testresult = 0;
  8042. int dhauto = (idx == 3 || idx == 4) ? 1 : 0;
  8043. int expected = (idx <= 2) ? 0 : 1;
  8044. EVP_PKEY *dhpkey = NULL;
  8045. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8046. DH *dh = NULL;
  8047. # else
  8048. if (idx >= 7)
  8049. return 1;
  8050. # endif
  8051. if (idx >= 5 && idx <= 8) {
  8052. dhpkey = get_tmp_dh_params();
  8053. if (!TEST_ptr(dhpkey))
  8054. goto end;
  8055. }
  8056. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8057. if (idx == 7 || idx == 8) {
  8058. dh = EVP_PKEY_get1_DH(dhpkey);
  8059. if (!TEST_ptr(dh))
  8060. goto end;
  8061. }
  8062. # endif
  8063. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8064. TLS_client_method(),
  8065. 0,
  8066. 0,
  8067. &sctx, &cctx, cert, privkey)))
  8068. goto end;
  8069. if ((idx & 1) == 1) {
  8070. if (!TEST_true(SSL_CTX_set_dh_auto(sctx, dhauto)))
  8071. goto end;
  8072. }
  8073. if (idx == 5) {
  8074. if (!TEST_true(SSL_CTX_set0_tmp_dh_pkey(sctx, dhpkey)))
  8075. goto end;
  8076. dhpkey = NULL;
  8077. }
  8078. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8079. else if (idx == 7) {
  8080. if (!TEST_true(SSL_CTX_set_tmp_dh(sctx, dh)))
  8081. goto end;
  8082. } else if (idx == 9) {
  8083. SSL_CTX_set_tmp_dh_callback(sctx, tmp_dh_callback);
  8084. }
  8085. # endif
  8086. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8087. NULL, NULL)))
  8088. goto end;
  8089. if ((idx & 1) == 0 && idx != 0) {
  8090. if (!TEST_true(SSL_set_dh_auto(serverssl, dhauto)))
  8091. goto end;
  8092. }
  8093. if (idx == 6) {
  8094. if (!TEST_true(SSL_set0_tmp_dh_pkey(serverssl, dhpkey)))
  8095. goto end;
  8096. dhpkey = NULL;
  8097. }
  8098. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8099. else if (idx == 8) {
  8100. if (!TEST_true(SSL_set_tmp_dh(serverssl, dh)))
  8101. goto end;
  8102. } else if (idx == 10) {
  8103. SSL_set_tmp_dh_callback(serverssl, tmp_dh_callback);
  8104. }
  8105. # endif
  8106. if (!TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  8107. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  8108. || !TEST_true(SSL_set_cipher_list(serverssl, "DHE-RSA-AES128-SHA")))
  8109. goto end;
  8110. /*
  8111. * If autoon then we should succeed. Otherwise we expect failure because
  8112. * there are no parameters
  8113. */
  8114. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  8115. SSL_ERROR_NONE), expected))
  8116. goto end;
  8117. testresult = 1;
  8118. end:
  8119. # ifndef OPENSSL_NO_DEPRECATED_3_0
  8120. DH_free(dh);
  8121. # endif
  8122. SSL_free(serverssl);
  8123. SSL_free(clientssl);
  8124. SSL_CTX_free(sctx);
  8125. SSL_CTX_free(cctx);
  8126. EVP_PKEY_free(dhpkey);
  8127. return testresult;
  8128. }
  8129. /*
  8130. * Test the auto DH keys are appropriately sized
  8131. */
  8132. static int test_dh_auto(int idx)
  8133. {
  8134. SSL_CTX *cctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method());
  8135. SSL_CTX *sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8136. SSL *clientssl = NULL, *serverssl = NULL;
  8137. int testresult = 0;
  8138. EVP_PKEY *tmpkey = NULL;
  8139. char *thiscert = NULL, *thiskey = NULL;
  8140. size_t expdhsize = 0;
  8141. const char *ciphersuite = "DHE-RSA-AES128-SHA";
  8142. if (!TEST_ptr(sctx) || !TEST_ptr(cctx))
  8143. goto end;
  8144. switch (idx) {
  8145. case 0:
  8146. /* The FIPS provider doesn't support this DH size - so we ignore it */
  8147. if (is_fips) {
  8148. testresult = 1;
  8149. goto end;
  8150. }
  8151. thiscert = cert1024;
  8152. thiskey = privkey1024;
  8153. expdhsize = 1024;
  8154. SSL_CTX_set_security_level(sctx, 1);
  8155. SSL_CTX_set_security_level(cctx, 1);
  8156. break;
  8157. case 1:
  8158. /* 2048 bit prime */
  8159. thiscert = cert;
  8160. thiskey = privkey;
  8161. expdhsize = 2048;
  8162. break;
  8163. case 2:
  8164. thiscert = cert3072;
  8165. thiskey = privkey3072;
  8166. expdhsize = 3072;
  8167. break;
  8168. case 3:
  8169. thiscert = cert4096;
  8170. thiskey = privkey4096;
  8171. expdhsize = 4096;
  8172. break;
  8173. case 4:
  8174. thiscert = cert8192;
  8175. thiskey = privkey8192;
  8176. expdhsize = 8192;
  8177. break;
  8178. /* No certificate cases */
  8179. case 5:
  8180. /* The FIPS provider doesn't support this DH size - so we ignore it */
  8181. if (is_fips) {
  8182. testresult = 1;
  8183. goto end;
  8184. }
  8185. ciphersuite = "ADH-AES128-SHA256:@SECLEVEL=0";
  8186. expdhsize = 1024;
  8187. break;
  8188. case 6:
  8189. ciphersuite = "ADH-AES256-SHA256:@SECLEVEL=0";
  8190. expdhsize = 3072;
  8191. break;
  8192. default:
  8193. TEST_error("Invalid text index");
  8194. goto end;
  8195. }
  8196. if (!TEST_true(create_ssl_ctx_pair(libctx, NULL,
  8197. NULL,
  8198. 0,
  8199. 0,
  8200. &sctx, &cctx, thiscert, thiskey)))
  8201. goto end;
  8202. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  8203. NULL, NULL)))
  8204. goto end;
  8205. if (!TEST_true(SSL_set_dh_auto(serverssl, 1))
  8206. || !TEST_true(SSL_set_min_proto_version(serverssl, TLS1_2_VERSION))
  8207. || !TEST_true(SSL_set_max_proto_version(serverssl, TLS1_2_VERSION))
  8208. || !TEST_true(SSL_set_cipher_list(serverssl, ciphersuite))
  8209. || !TEST_true(SSL_set_cipher_list(clientssl, ciphersuite)))
  8210. goto end;
  8211. /*
  8212. * Send the server's first flight. At this point the server has created the
  8213. * temporary DH key but hasn't finished using it yet. Once used it is
  8214. * removed, so we cannot test it.
  8215. */
  8216. if (!TEST_int_le(SSL_connect(clientssl), 0)
  8217. || !TEST_int_le(SSL_accept(serverssl), 0))
  8218. goto end;
  8219. if (!TEST_int_gt(SSL_get_tmp_key(serverssl, &tmpkey), 0))
  8220. goto end;
  8221. if (!TEST_size_t_eq(EVP_PKEY_get_bits(tmpkey), expdhsize))
  8222. goto end;
  8223. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8224. goto end;
  8225. testresult = 1;
  8226. end:
  8227. SSL_free(serverssl);
  8228. SSL_free(clientssl);
  8229. SSL_CTX_free(sctx);
  8230. SSL_CTX_free(cctx);
  8231. EVP_PKEY_free(tmpkey);
  8232. return testresult;
  8233. }
  8234. # endif /* OPENSSL_NO_DH */
  8235. #endif /* OPENSSL_NO_TLS1_2 */
  8236. #ifndef OSSL_NO_USABLE_TLS1_3
  8237. /*
  8238. * Test that setting an SNI callback works with TLSv1.3. Specifically we check
  8239. * that it works even without a certificate configured for the original
  8240. * SSL_CTX
  8241. */
  8242. static int test_sni_tls13(void)
  8243. {
  8244. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  8245. SSL *clientssl = NULL, *serverssl = NULL;
  8246. int testresult = 0;
  8247. /* Reset callback counter */
  8248. snicb = 0;
  8249. /* Create an initial SSL_CTX with no certificate configured */
  8250. sctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8251. if (!TEST_ptr(sctx))
  8252. goto end;
  8253. /* Require TLSv1.3 as a minimum */
  8254. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8255. TLS_client_method(), TLS1_3_VERSION, 0,
  8256. &sctx2, &cctx, cert, privkey)))
  8257. goto end;
  8258. /* Set up SNI */
  8259. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  8260. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  8261. goto end;
  8262. /*
  8263. * Connection should still succeed because the final SSL_CTX has the right
  8264. * certificates configured.
  8265. */
  8266. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  8267. &clientssl, NULL, NULL))
  8268. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  8269. SSL_ERROR_NONE)))
  8270. goto end;
  8271. /* We should have had the SNI callback called exactly once */
  8272. if (!TEST_int_eq(snicb, 1))
  8273. goto end;
  8274. testresult = 1;
  8275. end:
  8276. SSL_free(serverssl);
  8277. SSL_free(clientssl);
  8278. SSL_CTX_free(sctx2);
  8279. SSL_CTX_free(sctx);
  8280. SSL_CTX_free(cctx);
  8281. return testresult;
  8282. }
  8283. /*
  8284. * Test that the lifetime hint of a TLSv1.3 ticket is no more than 1 week
  8285. * 0 = TLSv1.2
  8286. * 1 = TLSv1.3
  8287. */
  8288. static int test_ticket_lifetime(int idx)
  8289. {
  8290. SSL_CTX *cctx = NULL, *sctx = NULL;
  8291. SSL *clientssl = NULL, *serverssl = NULL;
  8292. int testresult = 0;
  8293. int version = TLS1_3_VERSION;
  8294. #define ONE_WEEK_SEC (7 * 24 * 60 * 60)
  8295. #define TWO_WEEK_SEC (2 * ONE_WEEK_SEC)
  8296. if (idx == 0) {
  8297. #ifdef OPENSSL_NO_TLS1_2
  8298. return TEST_skip("TLS 1.2 is disabled.");
  8299. #else
  8300. version = TLS1_2_VERSION;
  8301. #endif
  8302. }
  8303. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8304. TLS_client_method(), version, version,
  8305. &sctx, &cctx, cert, privkey)))
  8306. goto end;
  8307. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  8308. &clientssl, NULL, NULL)))
  8309. goto end;
  8310. /*
  8311. * Set the timeout to be more than 1 week
  8312. * make sure the returned value is the default
  8313. */
  8314. if (!TEST_long_eq(SSL_CTX_set_timeout(sctx, TWO_WEEK_SEC),
  8315. SSL_get_default_timeout(serverssl)))
  8316. goto end;
  8317. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8318. goto end;
  8319. if (idx == 0) {
  8320. /* TLSv1.2 uses the set value */
  8321. if (!TEST_ulong_eq(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), TWO_WEEK_SEC))
  8322. goto end;
  8323. } else {
  8324. /* TLSv1.3 uses the limited value */
  8325. if (!TEST_ulong_le(SSL_SESSION_get_ticket_lifetime_hint(SSL_get_session(clientssl)), ONE_WEEK_SEC))
  8326. goto end;
  8327. }
  8328. testresult = 1;
  8329. end:
  8330. SSL_free(serverssl);
  8331. SSL_free(clientssl);
  8332. SSL_CTX_free(sctx);
  8333. SSL_CTX_free(cctx);
  8334. return testresult;
  8335. }
  8336. #endif
  8337. /*
  8338. * Test that setting an ALPN does not violate RFC
  8339. */
  8340. static int test_set_alpn(void)
  8341. {
  8342. SSL_CTX *ctx = NULL;
  8343. SSL *ssl = NULL;
  8344. int testresult = 0;
  8345. unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
  8346. unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
  8347. unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
  8348. unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
  8349. unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
  8350. unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
  8351. /* Create an initial SSL_CTX with no certificate configured */
  8352. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8353. if (!TEST_ptr(ctx))
  8354. goto end;
  8355. /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
  8356. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
  8357. goto end;
  8358. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
  8359. goto end;
  8360. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
  8361. goto end;
  8362. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
  8363. goto end;
  8364. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
  8365. goto end;
  8366. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
  8367. goto end;
  8368. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
  8369. goto end;
  8370. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
  8371. goto end;
  8372. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
  8373. goto end;
  8374. ssl = SSL_new(ctx);
  8375. if (!TEST_ptr(ssl))
  8376. goto end;
  8377. if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
  8378. goto end;
  8379. if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
  8380. goto end;
  8381. if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
  8382. goto end;
  8383. if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
  8384. goto end;
  8385. if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
  8386. goto end;
  8387. if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
  8388. goto end;
  8389. if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
  8390. goto end;
  8391. if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
  8392. goto end;
  8393. if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
  8394. goto end;
  8395. testresult = 1;
  8396. end:
  8397. SSL_free(ssl);
  8398. SSL_CTX_free(ctx);
  8399. return testresult;
  8400. }
  8401. /*
  8402. * Test SSL_CTX_set1_verify/chain_cert_store and SSL_CTX_get_verify/chain_cert_store.
  8403. */
  8404. static int test_set_verify_cert_store_ssl_ctx(void)
  8405. {
  8406. SSL_CTX *ctx = NULL;
  8407. int testresult = 0;
  8408. X509_STORE *store = NULL, *new_store = NULL,
  8409. *cstore = NULL, *new_cstore = NULL;
  8410. /* Create an initial SSL_CTX. */
  8411. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8412. if (!TEST_ptr(ctx))
  8413. goto end;
  8414. /* Retrieve verify store pointer. */
  8415. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  8416. goto end;
  8417. /* Retrieve chain store pointer. */
  8418. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  8419. goto end;
  8420. /* We haven't set any yet, so this should be NULL. */
  8421. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  8422. goto end;
  8423. /* Create stores. We use separate stores so pointers are different. */
  8424. new_store = X509_STORE_new();
  8425. if (!TEST_ptr(new_store))
  8426. goto end;
  8427. new_cstore = X509_STORE_new();
  8428. if (!TEST_ptr(new_cstore))
  8429. goto end;
  8430. /* Set stores. */
  8431. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, new_store)))
  8432. goto end;
  8433. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, new_cstore)))
  8434. goto end;
  8435. /* Should be able to retrieve the same pointer. */
  8436. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  8437. goto end;
  8438. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  8439. goto end;
  8440. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  8441. goto end;
  8442. /* Should be able to unset again. */
  8443. if (!TEST_true(SSL_CTX_set1_verify_cert_store(ctx, NULL)))
  8444. goto end;
  8445. if (!TEST_true(SSL_CTX_set1_chain_cert_store(ctx, NULL)))
  8446. goto end;
  8447. /* Should now be NULL. */
  8448. if (!TEST_true(SSL_CTX_get0_verify_cert_store(ctx, &store)))
  8449. goto end;
  8450. if (!TEST_true(SSL_CTX_get0_chain_cert_store(ctx, &cstore)))
  8451. goto end;
  8452. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  8453. goto end;
  8454. testresult = 1;
  8455. end:
  8456. X509_STORE_free(new_store);
  8457. X509_STORE_free(new_cstore);
  8458. SSL_CTX_free(ctx);
  8459. return testresult;
  8460. }
  8461. /*
  8462. * Test SSL_set1_verify/chain_cert_store and SSL_get_verify/chain_cert_store.
  8463. */
  8464. static int test_set_verify_cert_store_ssl(void)
  8465. {
  8466. SSL_CTX *ctx = NULL;
  8467. SSL *ssl = NULL;
  8468. int testresult = 0;
  8469. X509_STORE *store = NULL, *new_store = NULL,
  8470. *cstore = NULL, *new_cstore = NULL;
  8471. /* Create an initial SSL_CTX. */
  8472. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8473. if (!TEST_ptr(ctx))
  8474. goto end;
  8475. /* Create an SSL object. */
  8476. ssl = SSL_new(ctx);
  8477. if (!TEST_ptr(ssl))
  8478. goto end;
  8479. /* Retrieve verify store pointer. */
  8480. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  8481. goto end;
  8482. /* Retrieve chain store pointer. */
  8483. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  8484. goto end;
  8485. /* We haven't set any yet, so this should be NULL. */
  8486. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  8487. goto end;
  8488. /* Create stores. We use separate stores so pointers are different. */
  8489. new_store = X509_STORE_new();
  8490. if (!TEST_ptr(new_store))
  8491. goto end;
  8492. new_cstore = X509_STORE_new();
  8493. if (!TEST_ptr(new_cstore))
  8494. goto end;
  8495. /* Set stores. */
  8496. if (!TEST_true(SSL_set1_verify_cert_store(ssl, new_store)))
  8497. goto end;
  8498. if (!TEST_true(SSL_set1_chain_cert_store(ssl, new_cstore)))
  8499. goto end;
  8500. /* Should be able to retrieve the same pointer. */
  8501. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  8502. goto end;
  8503. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  8504. goto end;
  8505. if (!TEST_ptr_eq(store, new_store) || !TEST_ptr_eq(cstore, new_cstore))
  8506. goto end;
  8507. /* Should be able to unset again. */
  8508. if (!TEST_true(SSL_set1_verify_cert_store(ssl, NULL)))
  8509. goto end;
  8510. if (!TEST_true(SSL_set1_chain_cert_store(ssl, NULL)))
  8511. goto end;
  8512. /* Should now be NULL. */
  8513. if (!TEST_true(SSL_get0_verify_cert_store(ssl, &store)))
  8514. goto end;
  8515. if (!TEST_true(SSL_get0_chain_cert_store(ssl, &cstore)))
  8516. goto end;
  8517. if (!TEST_ptr_null(store) || !TEST_ptr_null(cstore))
  8518. goto end;
  8519. testresult = 1;
  8520. end:
  8521. X509_STORE_free(new_store);
  8522. X509_STORE_free(new_cstore);
  8523. SSL_free(ssl);
  8524. SSL_CTX_free(ctx);
  8525. return testresult;
  8526. }
  8527. static int test_inherit_verify_param(void)
  8528. {
  8529. int testresult = 0;
  8530. SSL_CTX *ctx = NULL;
  8531. X509_VERIFY_PARAM *cp = NULL;
  8532. SSL *ssl = NULL;
  8533. X509_VERIFY_PARAM *sp = NULL;
  8534. int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  8535. ctx = SSL_CTX_new_ex(libctx, NULL, TLS_server_method());
  8536. if (!TEST_ptr(ctx))
  8537. goto end;
  8538. cp = SSL_CTX_get0_param(ctx);
  8539. if (!TEST_ptr(cp))
  8540. goto end;
  8541. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
  8542. goto end;
  8543. X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
  8544. ssl = SSL_new(ctx);
  8545. if (!TEST_ptr(ssl))
  8546. goto end;
  8547. sp = SSL_get0_param(ssl);
  8548. if (!TEST_ptr(sp))
  8549. goto end;
  8550. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
  8551. goto end;
  8552. testresult = 1;
  8553. end:
  8554. SSL_free(ssl);
  8555. SSL_CTX_free(ctx);
  8556. return testresult;
  8557. }
  8558. static int test_load_dhfile(void)
  8559. {
  8560. #ifndef OPENSSL_NO_DH
  8561. int testresult = 0;
  8562. SSL_CTX *ctx = NULL;
  8563. SSL_CONF_CTX *cctx = NULL;
  8564. if (dhfile == NULL)
  8565. return 1;
  8566. if (!TEST_ptr(ctx = SSL_CTX_new_ex(libctx, NULL, TLS_client_method()))
  8567. || !TEST_ptr(cctx = SSL_CONF_CTX_new()))
  8568. goto end;
  8569. SSL_CONF_CTX_set_ssl_ctx(cctx, ctx);
  8570. SSL_CONF_CTX_set_flags(cctx,
  8571. SSL_CONF_FLAG_CERTIFICATE
  8572. | SSL_CONF_FLAG_SERVER
  8573. | SSL_CONF_FLAG_FILE);
  8574. if (!TEST_int_eq(SSL_CONF_cmd(cctx, "DHParameters", dhfile), 2))
  8575. goto end;
  8576. testresult = 1;
  8577. end:
  8578. SSL_CONF_CTX_free(cctx);
  8579. SSL_CTX_free(ctx);
  8580. return testresult;
  8581. #else
  8582. return TEST_skip("DH not supported by this build");
  8583. #endif
  8584. }
  8585. #ifndef OSSL_NO_USABLE_TLS1_3
  8586. /* Test that read_ahead works across a key change */
  8587. static int test_read_ahead_key_change(void)
  8588. {
  8589. SSL_CTX *cctx = NULL, *sctx = NULL;
  8590. SSL *clientssl = NULL, *serverssl = NULL;
  8591. int testresult = 0;
  8592. char *msg = "Hello World";
  8593. size_t written, readbytes;
  8594. char buf[80];
  8595. int i;
  8596. if (!TEST_true(create_ssl_ctx_pair(libctx, TLS_server_method(),
  8597. TLS_client_method(), TLS1_3_VERSION, 0,
  8598. &sctx, &cctx, cert, privkey)))
  8599. goto end;
  8600. SSL_CTX_set_read_ahead(sctx, 1);
  8601. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  8602. &clientssl, NULL, NULL)))
  8603. goto end;
  8604. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  8605. goto end;
  8606. /* Write some data, send a key update, write more data */
  8607. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  8608. || !TEST_size_t_eq(written, strlen(msg)))
  8609. goto end;
  8610. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  8611. goto end;
  8612. if (!TEST_true(SSL_write_ex(clientssl, msg, strlen(msg), &written))
  8613. || !TEST_size_t_eq(written, strlen(msg)))
  8614. goto end;
  8615. /*
  8616. * Since read_ahead is on the first read below should read the record with
  8617. * the first app data, the second record with the key update message, and
  8618. * the third record with the app data all in one go. We should be able to
  8619. * still process the read_ahead data correctly even though it crosses
  8620. * epochs
  8621. */
  8622. for (i = 0; i < 2; i++) {
  8623. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf) - 1,
  8624. &readbytes)))
  8625. goto end;
  8626. buf[readbytes] = '\0';
  8627. if (!TEST_str_eq(buf, msg))
  8628. goto end;
  8629. }
  8630. testresult = 1;
  8631. end:
  8632. SSL_free(serverssl);
  8633. SSL_free(clientssl);
  8634. SSL_CTX_free(sctx);
  8635. SSL_CTX_free(cctx);
  8636. return testresult;
  8637. }
  8638. #endif /* OSSL_NO_USABLE_TLS1_3 */
  8639. OPT_TEST_DECLARE_USAGE("certfile privkeyfile srpvfile tmpfile provider config dhfile\n")
  8640. int setup_tests(void)
  8641. {
  8642. char *modulename;
  8643. char *configfile;
  8644. libctx = OSSL_LIB_CTX_new();
  8645. if (!TEST_ptr(libctx))
  8646. return 0;
  8647. defctxnull = OSSL_PROVIDER_load(NULL, "null");
  8648. /*
  8649. * Verify that the default and fips providers in the default libctx are not
  8650. * available
  8651. */
  8652. if (!TEST_false(OSSL_PROVIDER_available(NULL, "default"))
  8653. || !TEST_false(OSSL_PROVIDER_available(NULL, "fips")))
  8654. return 0;
  8655. if (!test_skip_common_options()) {
  8656. TEST_error("Error parsing test options\n");
  8657. return 0;
  8658. }
  8659. if (!TEST_ptr(certsdir = test_get_argument(0))
  8660. || !TEST_ptr(srpvfile = test_get_argument(1))
  8661. || !TEST_ptr(tmpfilename = test_get_argument(2))
  8662. || !TEST_ptr(modulename = test_get_argument(3))
  8663. || !TEST_ptr(configfile = test_get_argument(4))
  8664. || !TEST_ptr(dhfile = test_get_argument(5)))
  8665. return 0;
  8666. if (!TEST_true(OSSL_LIB_CTX_load_config(libctx, configfile)))
  8667. return 0;
  8668. /* Check we have the expected provider available */
  8669. if (!TEST_true(OSSL_PROVIDER_available(libctx, modulename)))
  8670. return 0;
  8671. /* Check the default provider is not available */
  8672. if (strcmp(modulename, "default") != 0
  8673. && !TEST_false(OSSL_PROVIDER_available(libctx, "default")))
  8674. return 0;
  8675. if (strcmp(modulename, "fips") == 0)
  8676. is_fips = 1;
  8677. /*
  8678. * We add, but don't load the test "tls-provider". We'll load it when we
  8679. * need it.
  8680. */
  8681. if (!TEST_true(OSSL_PROVIDER_add_builtin(libctx, "tls-provider",
  8682. tls_provider_init)))
  8683. return 0;
  8684. if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
  8685. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  8686. TEST_error("not supported in this build");
  8687. return 0;
  8688. #else
  8689. int i, mcount, rcount, fcount;
  8690. for (i = 0; i < 4; i++)
  8691. test_export_key_mat(i);
  8692. CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
  8693. test_printf_stdout("malloc %d realloc %d free %d\n",
  8694. mcount, rcount, fcount);
  8695. return 1;
  8696. #endif
  8697. }
  8698. cert = test_mk_file_path(certsdir, "servercert.pem");
  8699. if (cert == NULL)
  8700. goto err;
  8701. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  8702. if (privkey == NULL)
  8703. goto err;
  8704. cert2 = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  8705. if (cert2 == NULL)
  8706. goto err;
  8707. privkey2 = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  8708. if (privkey2 == NULL)
  8709. goto err;
  8710. cert1024 = test_mk_file_path(certsdir, "ee-cert-1024.pem");
  8711. if (cert1024 == NULL)
  8712. goto err;
  8713. privkey1024 = test_mk_file_path(certsdir, "ee-key-1024.pem");
  8714. if (privkey1024 == NULL)
  8715. goto err;
  8716. cert3072 = test_mk_file_path(certsdir, "ee-cert-3072.pem");
  8717. if (cert3072 == NULL)
  8718. goto err;
  8719. privkey3072 = test_mk_file_path(certsdir, "ee-key-3072.pem");
  8720. if (privkey3072 == NULL)
  8721. goto err;
  8722. cert4096 = test_mk_file_path(certsdir, "ee-cert-4096.pem");
  8723. if (cert4096 == NULL)
  8724. goto err;
  8725. privkey4096 = test_mk_file_path(certsdir, "ee-key-4096.pem");
  8726. if (privkey4096 == NULL)
  8727. goto err;
  8728. cert8192 = test_mk_file_path(certsdir, "ee-cert-8192.pem");
  8729. if (cert8192 == NULL)
  8730. goto err;
  8731. privkey8192 = test_mk_file_path(certsdir, "ee-key-8192.pem");
  8732. if (privkey8192 == NULL)
  8733. goto err;
  8734. #if !defined(OPENSSL_NO_KTLS) && !defined(OPENSSL_NO_SOCK)
  8735. # if !defined(OPENSSL_NO_TLS1_2) || !defined(OSSL_NO_USABLE_TLS1_3)
  8736. ADD_ALL_TESTS(test_ktls, NUM_KTLS_TEST_CIPHERS * 4);
  8737. ADD_ALL_TESTS(test_ktls_sendfile, NUM_KTLS_TEST_CIPHERS);
  8738. # endif
  8739. #endif
  8740. ADD_TEST(test_large_message_tls);
  8741. ADD_TEST(test_large_message_tls_read_ahead);
  8742. #ifndef OPENSSL_NO_DTLS
  8743. ADD_TEST(test_large_message_dtls);
  8744. #endif
  8745. ADD_TEST(test_cleanse_plaintext);
  8746. #ifndef OPENSSL_NO_OCSP
  8747. ADD_TEST(test_tlsext_status_type);
  8748. #endif
  8749. ADD_TEST(test_session_with_only_int_cache);
  8750. ADD_TEST(test_session_with_only_ext_cache);
  8751. ADD_TEST(test_session_with_both_cache);
  8752. ADD_TEST(test_session_wo_ca_names);
  8753. #ifndef OSSL_NO_USABLE_TLS1_3
  8754. ADD_ALL_TESTS(test_stateful_tickets, 3);
  8755. ADD_ALL_TESTS(test_stateless_tickets, 3);
  8756. ADD_TEST(test_psk_tickets);
  8757. ADD_ALL_TESTS(test_extra_tickets, 6);
  8758. #endif
  8759. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  8760. ADD_TEST(test_ssl_bio_pop_next_bio);
  8761. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  8762. ADD_TEST(test_ssl_bio_change_rbio);
  8763. ADD_TEST(test_ssl_bio_change_wbio);
  8764. #if !defined(OPENSSL_NO_TLS1_2) || defined(OSSL_NO_USABLE_TLS1_3)
  8765. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  8766. ADD_TEST(test_keylog);
  8767. #endif
  8768. #ifndef OSSL_NO_USABLE_TLS1_3
  8769. ADD_TEST(test_keylog_no_master_key);
  8770. #endif
  8771. ADD_TEST(test_client_cert_verify_cb);
  8772. ADD_TEST(test_ssl_build_cert_chain);
  8773. ADD_TEST(test_ssl_ctx_build_cert_chain);
  8774. #ifndef OPENSSL_NO_TLS1_2
  8775. ADD_TEST(test_client_hello_cb);
  8776. ADD_TEST(test_no_ems);
  8777. ADD_TEST(test_ccs_change_cipher);
  8778. #endif
  8779. #ifndef OSSL_NO_USABLE_TLS1_3
  8780. ADD_ALL_TESTS(test_early_data_read_write, 3);
  8781. /*
  8782. * We don't do replay tests for external PSK. Replay protection isn't used
  8783. * in that scenario.
  8784. */
  8785. ADD_ALL_TESTS(test_early_data_replay, 2);
  8786. ADD_ALL_TESTS(test_early_data_skip, 3);
  8787. ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
  8788. ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
  8789. ADD_ALL_TESTS(test_early_data_skip_abort, 3);
  8790. ADD_ALL_TESTS(test_early_data_not_sent, 3);
  8791. ADD_ALL_TESTS(test_early_data_psk, 8);
  8792. ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
  8793. ADD_ALL_TESTS(test_early_data_not_expected, 3);
  8794. # ifndef OPENSSL_NO_TLS1_2
  8795. ADD_ALL_TESTS(test_early_data_tls1_2, 3);
  8796. # endif
  8797. #endif
  8798. #ifndef OSSL_NO_USABLE_TLS1_3
  8799. ADD_ALL_TESTS(test_set_ciphersuite, 10);
  8800. ADD_TEST(test_ciphersuite_change);
  8801. ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
  8802. # ifdef OPENSSL_NO_PSK
  8803. ADD_ALL_TESTS(test_tls13_psk, 1);
  8804. # else
  8805. ADD_ALL_TESTS(test_tls13_psk, 4);
  8806. # endif /* OPENSSL_NO_PSK */
  8807. # ifndef OPENSSL_NO_TLS1_2
  8808. /* Test with both TLSv1.3 and 1.2 versions */
  8809. ADD_ALL_TESTS(test_key_exchange, 14);
  8810. # if !defined(OPENSSL_NO_EC) && !defined(OPENSSL_NO_DH)
  8811. ADD_ALL_TESTS(test_negotiated_group,
  8812. 4 * (OSSL_NELEM(ecdhe_kexch_groups)
  8813. + OSSL_NELEM(ffdhe_kexch_groups)));
  8814. # endif
  8815. # else
  8816. /* Test with only TLSv1.3 versions */
  8817. ADD_ALL_TESTS(test_key_exchange, 12);
  8818. # endif
  8819. ADD_ALL_TESTS(test_custom_exts, 6);
  8820. ADD_TEST(test_stateless);
  8821. ADD_TEST(test_pha_key_update);
  8822. #else
  8823. ADD_ALL_TESTS(test_custom_exts, 3);
  8824. #endif
  8825. ADD_ALL_TESTS(test_export_key_mat, 6);
  8826. #ifndef OSSL_NO_USABLE_TLS1_3
  8827. ADD_ALL_TESTS(test_export_key_mat_early, 3);
  8828. ADD_TEST(test_key_update);
  8829. ADD_ALL_TESTS(test_key_update_peer_in_write, 2);
  8830. ADD_ALL_TESTS(test_key_update_peer_in_read, 2);
  8831. ADD_ALL_TESTS(test_key_update_local_in_write, 2);
  8832. ADD_ALL_TESTS(test_key_update_local_in_read, 2);
  8833. #endif
  8834. ADD_ALL_TESTS(test_ssl_clear, 2);
  8835. ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
  8836. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  8837. ADD_ALL_TESTS(test_srp, 6);
  8838. #endif
  8839. ADD_ALL_TESTS(test_info_callback, 6);
  8840. ADD_ALL_TESTS(test_ssl_pending, 2);
  8841. ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
  8842. ADD_ALL_TESTS(test_ticket_callbacks, 16);
  8843. ADD_ALL_TESTS(test_shutdown, 7);
  8844. ADD_ALL_TESTS(test_incorrect_shutdown, 2);
  8845. ADD_ALL_TESTS(test_cert_cb, 6);
  8846. ADD_ALL_TESTS(test_client_cert_cb, 2);
  8847. ADD_ALL_TESTS(test_ca_names, 3);
  8848. #ifndef OPENSSL_NO_TLS1_2
  8849. ADD_ALL_TESTS(test_multiblock_write, OSSL_NELEM(multiblock_cipherlist_data));
  8850. #endif
  8851. ADD_ALL_TESTS(test_servername, 10);
  8852. #if !defined(OPENSSL_NO_EC) \
  8853. && (!defined(OSSL_NO_USABLE_TLS1_3) || !defined(OPENSSL_NO_TLS1_2))
  8854. ADD_ALL_TESTS(test_sigalgs_available, 6);
  8855. #endif
  8856. #ifndef OPENSSL_NO_TLS1_3
  8857. ADD_ALL_TESTS(test_pluggable_group, 2);
  8858. #endif
  8859. #ifndef OPENSSL_NO_TLS1_2
  8860. ADD_TEST(test_ssl_dup);
  8861. # ifndef OPENSSL_NO_DH
  8862. ADD_ALL_TESTS(test_set_tmp_dh, 11);
  8863. ADD_ALL_TESTS(test_dh_auto, 7);
  8864. # endif
  8865. #endif
  8866. #ifndef OSSL_NO_USABLE_TLS1_3
  8867. ADD_TEST(test_sni_tls13);
  8868. ADD_ALL_TESTS(test_ticket_lifetime, 2);
  8869. #endif
  8870. ADD_TEST(test_inherit_verify_param);
  8871. ADD_TEST(test_set_alpn);
  8872. ADD_TEST(test_set_verify_cert_store_ssl_ctx);
  8873. ADD_TEST(test_set_verify_cert_store_ssl);
  8874. ADD_ALL_TESTS(test_session_timeout, 1);
  8875. ADD_TEST(test_load_dhfile);
  8876. #ifndef OSSL_NO_USABLE_TLS1_3
  8877. ADD_TEST(test_read_ahead_key_change);
  8878. #endif
  8879. #if !defined(OPENSSL_NO_TLS1_2) && !defined(OSSL_NO_USABLE_TLS1_3)
  8880. ADD_ALL_TESTS(test_serverinfo_custom, 4);
  8881. #endif
  8882. return 1;
  8883. err:
  8884. OPENSSL_free(cert);
  8885. OPENSSL_free(privkey);
  8886. OPENSSL_free(cert2);
  8887. OPENSSL_free(privkey2);
  8888. return 0;
  8889. }
  8890. void cleanup_tests(void)
  8891. {
  8892. # if !defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_DH)
  8893. EVP_PKEY_free(tmp_dh_params);
  8894. #endif
  8895. OPENSSL_free(cert);
  8896. OPENSSL_free(privkey);
  8897. OPENSSL_free(cert2);
  8898. OPENSSL_free(privkey2);
  8899. OPENSSL_free(cert1024);
  8900. OPENSSL_free(privkey1024);
  8901. OPENSSL_free(cert3072);
  8902. OPENSSL_free(privkey3072);
  8903. OPENSSL_free(cert4096);
  8904. OPENSSL_free(privkey4096);
  8905. OPENSSL_free(cert8192);
  8906. OPENSSL_free(privkey8192);
  8907. bio_s_mempacket_test_free();
  8908. bio_s_always_retry_free();
  8909. OSSL_PROVIDER_unload(defctxnull);
  8910. OSSL_LIB_CTX_free(libctx);
  8911. }