sslapitest.c 236 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689569056915692569356945695569656975698569957005701570257035704570557065707570857095710571157125713571457155716571757185719572057215722572357245725572657275728572957305731573257335734573557365737573857395740574157425743574457455746574757485749575057515752575357545755575657575758575957605761576257635764576557665767576857695770577157725773577457755776577757785779578057815782578357845785578657875788578957905791579257935794579557965797579857995800580158025803580458055806580758085809581058115812581358145815581658175818581958205821582258235824582558265827582858295830583158325833583458355836583758385839584058415842584358445845584658475848584958505851585258535854585558565857585858595860586158625863586458655866586758685869587058715872587358745875587658775878587958805881588258835884588558865887588858895890589158925893589458955896589758985899590059015902590359045905590659075908590959105911591259135914591559165917591859195920592159225923592459255926592759285929593059315932593359345935593659375938593959405941594259435944594559465947594859495950595159525953595459555956595759585959596059615962596359645965596659675968596959705971597259735974597559765977597859795980598159825983598459855986598759885989599059915992599359945995599659975998599960006001600260036004600560066007600860096010601160126013601460156016601760186019602060216022602360246025602660276028602960306031603260336034603560366037603860396040604160426043604460456046604760486049605060516052605360546055605660576058605960606061606260636064606560666067606860696070607160726073607460756076607760786079608060816082608360846085608660876088608960906091609260936094609560966097609860996100610161026103610461056106610761086109611061116112611361146115611661176118611961206121612261236124612561266127612861296130613161326133613461356136613761386139614061416142614361446145614661476148614961506151615261536154615561566157615861596160616161626163616461656166616761686169617061716172617361746175617661776178617961806181618261836184618561866187618861896190619161926193619461956196619761986199620062016202620362046205620662076208620962106211621262136214621562166217621862196220622162226223622462256226622762286229623062316232623362346235623662376238623962406241624262436244624562466247624862496250625162526253625462556256625762586259626062616262626362646265626662676268626962706271627262736274627562766277627862796280628162826283628462856286628762886289629062916292629362946295629662976298629963006301630263036304630563066307630863096310631163126313631463156316631763186319632063216322632363246325632663276328632963306331633263336334633563366337633863396340634163426343634463456346634763486349635063516352635363546355635663576358635963606361636263636364636563666367636863696370637163726373637463756376637763786379638063816382638363846385638663876388638963906391639263936394639563966397639863996400640164026403640464056406640764086409641064116412641364146415641664176418641964206421642264236424642564266427642864296430643164326433643464356436643764386439644064416442644364446445644664476448644964506451645264536454645564566457645864596460646164626463646464656466646764686469647064716472647364746475647664776478647964806481648264836484648564866487648864896490649164926493649464956496649764986499650065016502650365046505650665076508650965106511651265136514651565166517651865196520652165226523652465256526652765286529653065316532653365346535653665376538653965406541654265436544654565466547654865496550655165526553655465556556655765586559656065616562656365646565656665676568656965706571657265736574657565766577657865796580658165826583658465856586658765886589659065916592659365946595659665976598659966006601660266036604660566066607660866096610661166126613661466156616661766186619662066216622662366246625662666276628662966306631663266336634663566366637663866396640664166426643664466456646664766486649665066516652665366546655665666576658665966606661666266636664666566666667666866696670667166726673667466756676667766786679668066816682668366846685668666876688668966906691669266936694669566966697669866996700670167026703670467056706670767086709671067116712671367146715671667176718671967206721672267236724672567266727672867296730673167326733673467356736673767386739674067416742674367446745674667476748674967506751675267536754675567566757675867596760676167626763676467656766676767686769677067716772677367746775677667776778677967806781678267836784678567866787678867896790679167926793679467956796679767986799680068016802680368046805680668076808680968106811681268136814681568166817681868196820682168226823682468256826682768286829683068316832683368346835683668376838683968406841684268436844684568466847684868496850685168526853685468556856685768586859686068616862686368646865686668676868686968706871687268736874687568766877687868796880688168826883688468856886688768886889689068916892689368946895689668976898689969006901690269036904690569066907690869096910691169126913691469156916691769186919692069216922692369246925692669276928692969306931693269336934693569366937693869396940694169426943694469456946694769486949695069516952695369546955695669576958695969606961696269636964696569666967696869696970697169726973697469756976697769786979698069816982698369846985698669876988
  1. /*
  2. * Copyright 2016-2021 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/opensslconf.h>
  11. #include <openssl/bio.h>
  12. #include <openssl/crypto.h>
  13. #include <openssl/ssl.h>
  14. #include <openssl/ocsp.h>
  15. #include <openssl/srp.h>
  16. #include <openssl/txt_db.h>
  17. #include <openssl/aes.h>
  18. #include <openssl/x509v3.h>
  19. #include "ssltestlib.h"
  20. #include "testutil.h"
  21. #include "testutil/output.h"
  22. #include "internal/nelem.h"
  23. #include "../ssl/ssl_local.h"
  24. #ifndef OPENSSL_NO_TLS1_3
  25. static SSL_SESSION *clientpsk = NULL;
  26. static SSL_SESSION *serverpsk = NULL;
  27. static const char *pskid = "Identity";
  28. static const char *srvid;
  29. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  30. size_t *idlen, SSL_SESSION **sess);
  31. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  32. size_t identity_len, SSL_SESSION **sess);
  33. static int use_session_cb_cnt = 0;
  34. static int find_session_cb_cnt = 0;
  35. static SSL_SESSION *create_a_psk(SSL *ssl);
  36. #endif
  37. static char *certsdir = NULL;
  38. static char *cert = NULL;
  39. static char *privkey = NULL;
  40. static char *srpvfile = NULL;
  41. static char *tmpfilename = NULL;
  42. #define LOG_BUFFER_SIZE 2048
  43. static char server_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  44. static size_t server_log_buffer_index = 0;
  45. static char client_log_buffer[LOG_BUFFER_SIZE + 1] = {0};
  46. static size_t client_log_buffer_index = 0;
  47. static int error_writing_log = 0;
  48. #ifndef OPENSSL_NO_OCSP
  49. static const unsigned char orespder[] = "Dummy OCSP Response";
  50. static int ocsp_server_called = 0;
  51. static int ocsp_client_called = 0;
  52. static int cdummyarg = 1;
  53. static X509 *ocspcert = NULL;
  54. #endif
  55. #define NUM_EXTRA_CERTS 40
  56. #define CLIENT_VERSION_LEN 2
  57. /*
  58. * This structure is used to validate that the correct number of log messages
  59. * of various types are emitted when emitting secret logs.
  60. */
  61. struct sslapitest_log_counts {
  62. unsigned int rsa_key_exchange_count;
  63. unsigned int master_secret_count;
  64. unsigned int client_early_secret_count;
  65. unsigned int client_handshake_secret_count;
  66. unsigned int server_handshake_secret_count;
  67. unsigned int client_application_secret_count;
  68. unsigned int server_application_secret_count;
  69. unsigned int early_exporter_secret_count;
  70. unsigned int exporter_secret_count;
  71. };
  72. static unsigned char serverinfov1[] = {
  73. 0xff, 0xff, /* Dummy extension type */
  74. 0x00, 0x01, /* Extension length is 1 byte */
  75. 0xff /* Dummy extension data */
  76. };
  77. static unsigned char serverinfov2[] = {
  78. 0x00, 0x00, 0x00,
  79. (unsigned char)(SSL_EXT_CLIENT_HELLO & 0xff), /* Dummy context - 4 bytes */
  80. 0xff, 0xff, /* Dummy extension type */
  81. 0x00, 0x01, /* Extension length is 1 byte */
  82. 0xff /* Dummy extension data */
  83. };
  84. static int hostname_cb(SSL *s, int *al, void *arg)
  85. {
  86. const char *hostname = SSL_get_servername(s, TLSEXT_NAMETYPE_host_name);
  87. if (hostname != NULL && (strcmp(hostname, "goodhost") == 0
  88. || strcmp(hostname, "altgoodhost") == 0))
  89. return SSL_TLSEXT_ERR_OK;
  90. return SSL_TLSEXT_ERR_NOACK;
  91. }
  92. static void client_keylog_callback(const SSL *ssl, const char *line)
  93. {
  94. int line_length = strlen(line);
  95. /* If the log doesn't fit, error out. */
  96. if (client_log_buffer_index + line_length > sizeof(client_log_buffer) - 1) {
  97. TEST_info("Client log too full");
  98. error_writing_log = 1;
  99. return;
  100. }
  101. strcat(client_log_buffer, line);
  102. client_log_buffer_index += line_length;
  103. client_log_buffer[client_log_buffer_index++] = '\n';
  104. }
  105. static void server_keylog_callback(const SSL *ssl, const char *line)
  106. {
  107. int line_length = strlen(line);
  108. /* If the log doesn't fit, error out. */
  109. if (server_log_buffer_index + line_length > sizeof(server_log_buffer) - 1) {
  110. TEST_info("Server log too full");
  111. error_writing_log = 1;
  112. return;
  113. }
  114. strcat(server_log_buffer, line);
  115. server_log_buffer_index += line_length;
  116. server_log_buffer[server_log_buffer_index++] = '\n';
  117. }
  118. static int compare_hex_encoded_buffer(const char *hex_encoded,
  119. size_t hex_length,
  120. const uint8_t *raw,
  121. size_t raw_length)
  122. {
  123. size_t i, j;
  124. char hexed[3];
  125. if (!TEST_size_t_eq(raw_length * 2, hex_length))
  126. return 1;
  127. for (i = j = 0; i < raw_length && j + 1 < hex_length; i++, j += 2) {
  128. sprintf(hexed, "%02x", raw[i]);
  129. if (!TEST_int_eq(hexed[0], hex_encoded[j])
  130. || !TEST_int_eq(hexed[1], hex_encoded[j + 1]))
  131. return 1;
  132. }
  133. return 0;
  134. }
  135. static int test_keylog_output(char *buffer, const SSL *ssl,
  136. const SSL_SESSION *session,
  137. struct sslapitest_log_counts *expected)
  138. {
  139. char *token = NULL;
  140. unsigned char actual_client_random[SSL3_RANDOM_SIZE] = {0};
  141. size_t client_random_size = SSL3_RANDOM_SIZE;
  142. unsigned char actual_master_key[SSL_MAX_MASTER_KEY_LENGTH] = {0};
  143. size_t master_key_size = SSL_MAX_MASTER_KEY_LENGTH;
  144. unsigned int rsa_key_exchange_count = 0;
  145. unsigned int master_secret_count = 0;
  146. unsigned int client_early_secret_count = 0;
  147. unsigned int client_handshake_secret_count = 0;
  148. unsigned int server_handshake_secret_count = 0;
  149. unsigned int client_application_secret_count = 0;
  150. unsigned int server_application_secret_count = 0;
  151. unsigned int early_exporter_secret_count = 0;
  152. unsigned int exporter_secret_count = 0;
  153. for (token = strtok(buffer, " \n"); token != NULL;
  154. token = strtok(NULL, " \n")) {
  155. if (strcmp(token, "RSA") == 0) {
  156. /*
  157. * Premaster secret. Tokens should be: 16 ASCII bytes of
  158. * hex-encoded encrypted secret, then the hex-encoded pre-master
  159. * secret.
  160. */
  161. if (!TEST_ptr(token = strtok(NULL, " \n")))
  162. return 0;
  163. if (!TEST_size_t_eq(strlen(token), 16))
  164. return 0;
  165. if (!TEST_ptr(token = strtok(NULL, " \n")))
  166. return 0;
  167. /*
  168. * We can't sensibly check the log because the premaster secret is
  169. * transient, and OpenSSL doesn't keep hold of it once the master
  170. * secret is generated.
  171. */
  172. rsa_key_exchange_count++;
  173. } else if (strcmp(token, "CLIENT_RANDOM") == 0) {
  174. /*
  175. * Master secret. Tokens should be: 64 ASCII bytes of hex-encoded
  176. * client random, then the hex-encoded master secret.
  177. */
  178. client_random_size = SSL_get_client_random(ssl,
  179. actual_client_random,
  180. SSL3_RANDOM_SIZE);
  181. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  182. return 0;
  183. if (!TEST_ptr(token = strtok(NULL, " \n")))
  184. return 0;
  185. if (!TEST_size_t_eq(strlen(token), 64))
  186. return 0;
  187. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  188. actual_client_random,
  189. client_random_size)))
  190. return 0;
  191. if (!TEST_ptr(token = strtok(NULL, " \n")))
  192. return 0;
  193. master_key_size = SSL_SESSION_get_master_key(session,
  194. actual_master_key,
  195. master_key_size);
  196. if (!TEST_size_t_ne(master_key_size, 0))
  197. return 0;
  198. if (!TEST_false(compare_hex_encoded_buffer(token, strlen(token),
  199. actual_master_key,
  200. master_key_size)))
  201. return 0;
  202. master_secret_count++;
  203. } else if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0
  204. || strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0
  205. || strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0
  206. || strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0
  207. || strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0
  208. || strcmp(token, "EARLY_EXPORTER_SECRET") == 0
  209. || strcmp(token, "EXPORTER_SECRET") == 0) {
  210. /*
  211. * TLSv1.3 secret. Tokens should be: 64 ASCII bytes of hex-encoded
  212. * client random, and then the hex-encoded secret. In this case,
  213. * we treat all of these secrets identically and then just
  214. * distinguish between them when counting what we saw.
  215. */
  216. if (strcmp(token, "CLIENT_EARLY_TRAFFIC_SECRET") == 0)
  217. client_early_secret_count++;
  218. else if (strcmp(token, "CLIENT_HANDSHAKE_TRAFFIC_SECRET") == 0)
  219. client_handshake_secret_count++;
  220. else if (strcmp(token, "SERVER_HANDSHAKE_TRAFFIC_SECRET") == 0)
  221. server_handshake_secret_count++;
  222. else if (strcmp(token, "CLIENT_TRAFFIC_SECRET_0") == 0)
  223. client_application_secret_count++;
  224. else if (strcmp(token, "SERVER_TRAFFIC_SECRET_0") == 0)
  225. server_application_secret_count++;
  226. else if (strcmp(token, "EARLY_EXPORTER_SECRET") == 0)
  227. early_exporter_secret_count++;
  228. else if (strcmp(token, "EXPORTER_SECRET") == 0)
  229. exporter_secret_count++;
  230. client_random_size = SSL_get_client_random(ssl,
  231. actual_client_random,
  232. SSL3_RANDOM_SIZE);
  233. if (!TEST_size_t_eq(client_random_size, SSL3_RANDOM_SIZE))
  234. return 0;
  235. if (!TEST_ptr(token = strtok(NULL, " \n")))
  236. return 0;
  237. if (!TEST_size_t_eq(strlen(token), 64))
  238. return 0;
  239. if (!TEST_false(compare_hex_encoded_buffer(token, 64,
  240. actual_client_random,
  241. client_random_size)))
  242. return 0;
  243. if (!TEST_ptr(token = strtok(NULL, " \n")))
  244. return 0;
  245. /*
  246. * TODO(TLS1.3): test that application traffic secrets are what
  247. * we expect */
  248. } else {
  249. TEST_info("Unexpected token %s\n", token);
  250. return 0;
  251. }
  252. }
  253. /* Got what we expected? */
  254. if (!TEST_size_t_eq(rsa_key_exchange_count,
  255. expected->rsa_key_exchange_count)
  256. || !TEST_size_t_eq(master_secret_count,
  257. expected->master_secret_count)
  258. || !TEST_size_t_eq(client_early_secret_count,
  259. expected->client_early_secret_count)
  260. || !TEST_size_t_eq(client_handshake_secret_count,
  261. expected->client_handshake_secret_count)
  262. || !TEST_size_t_eq(server_handshake_secret_count,
  263. expected->server_handshake_secret_count)
  264. || !TEST_size_t_eq(client_application_secret_count,
  265. expected->client_application_secret_count)
  266. || !TEST_size_t_eq(server_application_secret_count,
  267. expected->server_application_secret_count)
  268. || !TEST_size_t_eq(early_exporter_secret_count,
  269. expected->early_exporter_secret_count)
  270. || !TEST_size_t_eq(exporter_secret_count,
  271. expected->exporter_secret_count))
  272. return 0;
  273. return 1;
  274. }
  275. #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
  276. static int test_keylog(void)
  277. {
  278. SSL_CTX *cctx = NULL, *sctx = NULL;
  279. SSL *clientssl = NULL, *serverssl = NULL;
  280. int testresult = 0;
  281. struct sslapitest_log_counts expected = {0};
  282. /* Clean up logging space */
  283. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  284. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  285. client_log_buffer_index = 0;
  286. server_log_buffer_index = 0;
  287. error_writing_log = 0;
  288. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  289. TLS_client_method(),
  290. TLS1_VERSION, TLS_MAX_VERSION,
  291. &sctx, &cctx, cert, privkey)))
  292. return 0;
  293. /* We cannot log the master secret for TLSv1.3, so we should forbid it. */
  294. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  295. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  296. /* We also want to ensure that we use RSA-based key exchange. */
  297. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "RSA")))
  298. goto end;
  299. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  300. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  301. goto end;
  302. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  303. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  304. == client_keylog_callback))
  305. goto end;
  306. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  307. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  308. == server_keylog_callback))
  309. goto end;
  310. /* Now do a handshake and check that the logs have been written to. */
  311. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  312. &clientssl, NULL, NULL))
  313. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  314. SSL_ERROR_NONE))
  315. || !TEST_false(error_writing_log)
  316. || !TEST_int_gt(client_log_buffer_index, 0)
  317. || !TEST_int_gt(server_log_buffer_index, 0))
  318. goto end;
  319. /*
  320. * Now we want to test that our output data was vaguely sensible. We
  321. * do that by using strtok and confirming that we have more or less the
  322. * data we expect. For both client and server, we expect to see one master
  323. * secret. The client should also see a RSA key exchange.
  324. */
  325. expected.rsa_key_exchange_count = 1;
  326. expected.master_secret_count = 1;
  327. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  328. SSL_get_session(clientssl), &expected)))
  329. goto end;
  330. expected.rsa_key_exchange_count = 0;
  331. if (!TEST_true(test_keylog_output(server_log_buffer, serverssl,
  332. SSL_get_session(serverssl), &expected)))
  333. goto end;
  334. testresult = 1;
  335. end:
  336. SSL_free(serverssl);
  337. SSL_free(clientssl);
  338. SSL_CTX_free(sctx);
  339. SSL_CTX_free(cctx);
  340. return testresult;
  341. }
  342. #endif
  343. #ifndef OPENSSL_NO_TLS1_3
  344. static int test_keylog_no_master_key(void)
  345. {
  346. SSL_CTX *cctx = NULL, *sctx = NULL;
  347. SSL *clientssl = NULL, *serverssl = NULL;
  348. SSL_SESSION *sess = NULL;
  349. int testresult = 0;
  350. struct sslapitest_log_counts expected = {0};
  351. unsigned char buf[1];
  352. size_t readbytes, written;
  353. /* Clean up logging space */
  354. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  355. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  356. client_log_buffer_index = 0;
  357. server_log_buffer_index = 0;
  358. error_writing_log = 0;
  359. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  360. TLS1_VERSION, TLS_MAX_VERSION,
  361. &sctx, &cctx, cert, privkey))
  362. || !TEST_true(SSL_CTX_set_max_early_data(sctx,
  363. SSL3_RT_MAX_PLAIN_LENGTH)))
  364. return 0;
  365. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx) == NULL)
  366. || !TEST_true(SSL_CTX_get_keylog_callback(sctx) == NULL))
  367. goto end;
  368. SSL_CTX_set_keylog_callback(cctx, client_keylog_callback);
  369. if (!TEST_true(SSL_CTX_get_keylog_callback(cctx)
  370. == client_keylog_callback))
  371. goto end;
  372. SSL_CTX_set_keylog_callback(sctx, server_keylog_callback);
  373. if (!TEST_true(SSL_CTX_get_keylog_callback(sctx)
  374. == server_keylog_callback))
  375. goto end;
  376. /* Now do a handshake and check that the logs have been written to. */
  377. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  378. &clientssl, NULL, NULL))
  379. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  380. SSL_ERROR_NONE))
  381. || !TEST_false(error_writing_log))
  382. goto end;
  383. /*
  384. * Now we want to test that our output data was vaguely sensible. For this
  385. * test, we expect no CLIENT_RANDOM entry because it doesn't make sense for
  386. * TLSv1.3, but we do expect both client and server to emit keys.
  387. */
  388. expected.client_handshake_secret_count = 1;
  389. expected.server_handshake_secret_count = 1;
  390. expected.client_application_secret_count = 1;
  391. expected.server_application_secret_count = 1;
  392. expected.exporter_secret_count = 1;
  393. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  394. SSL_get_session(clientssl), &expected))
  395. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  396. SSL_get_session(serverssl),
  397. &expected)))
  398. goto end;
  399. /* Terminate old session and resume with early data. */
  400. sess = SSL_get1_session(clientssl);
  401. SSL_shutdown(clientssl);
  402. SSL_shutdown(serverssl);
  403. SSL_free(serverssl);
  404. SSL_free(clientssl);
  405. serverssl = clientssl = NULL;
  406. /* Reset key log */
  407. memset(client_log_buffer, 0, sizeof(client_log_buffer));
  408. memset(server_log_buffer, 0, sizeof(server_log_buffer));
  409. client_log_buffer_index = 0;
  410. server_log_buffer_index = 0;
  411. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  412. &clientssl, NULL, NULL))
  413. || !TEST_true(SSL_set_session(clientssl, sess))
  414. /* Here writing 0 length early data is enough. */
  415. || !TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  416. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  417. &readbytes),
  418. SSL_READ_EARLY_DATA_ERROR)
  419. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  420. SSL_EARLY_DATA_ACCEPTED)
  421. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  422. SSL_ERROR_NONE))
  423. || !TEST_true(SSL_session_reused(clientssl)))
  424. goto end;
  425. /* In addition to the previous entries, expect early secrets. */
  426. expected.client_early_secret_count = 1;
  427. expected.early_exporter_secret_count = 1;
  428. if (!TEST_true(test_keylog_output(client_log_buffer, clientssl,
  429. SSL_get_session(clientssl), &expected))
  430. || !TEST_true(test_keylog_output(server_log_buffer, serverssl,
  431. SSL_get_session(serverssl),
  432. &expected)))
  433. goto end;
  434. testresult = 1;
  435. end:
  436. SSL_SESSION_free(sess);
  437. SSL_free(serverssl);
  438. SSL_free(clientssl);
  439. SSL_CTX_free(sctx);
  440. SSL_CTX_free(cctx);
  441. return testresult;
  442. }
  443. #endif
  444. #ifndef OPENSSL_NO_TLS1_2
  445. static int full_client_hello_callback(SSL *s, int *al, void *arg)
  446. {
  447. int *ctr = arg;
  448. const unsigned char *p;
  449. int *exts;
  450. /* We only configure two ciphers, but the SCSV is added automatically. */
  451. #ifdef OPENSSL_NO_EC
  452. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0x00, 0xff};
  453. #else
  454. const unsigned char expected_ciphers[] = {0x00, 0x9d, 0xc0,
  455. 0x2c, 0x00, 0xff};
  456. #endif
  457. const int expected_extensions[] = {
  458. #ifndef OPENSSL_NO_EC
  459. 11, 10,
  460. #endif
  461. 35, 22, 23, 13};
  462. size_t len;
  463. /* Make sure we can defer processing and get called back. */
  464. if ((*ctr)++ == 0)
  465. return SSL_CLIENT_HELLO_RETRY;
  466. len = SSL_client_hello_get0_ciphers(s, &p);
  467. if (!TEST_mem_eq(p, len, expected_ciphers, sizeof(expected_ciphers))
  468. || !TEST_size_t_eq(
  469. SSL_client_hello_get0_compression_methods(s, &p), 1)
  470. || !TEST_int_eq(*p, 0))
  471. return SSL_CLIENT_HELLO_ERROR;
  472. if (!SSL_client_hello_get1_extensions_present(s, &exts, &len))
  473. return SSL_CLIENT_HELLO_ERROR;
  474. if (len != OSSL_NELEM(expected_extensions) ||
  475. memcmp(exts, expected_extensions, len * sizeof(*exts)) != 0) {
  476. printf("ClientHello callback expected extensions mismatch\n");
  477. OPENSSL_free(exts);
  478. return SSL_CLIENT_HELLO_ERROR;
  479. }
  480. OPENSSL_free(exts);
  481. return SSL_CLIENT_HELLO_SUCCESS;
  482. }
  483. static int test_client_hello_cb(void)
  484. {
  485. SSL_CTX *cctx = NULL, *sctx = NULL;
  486. SSL *clientssl = NULL, *serverssl = NULL;
  487. int testctr = 0, testresult = 0;
  488. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  489. TLS1_VERSION, TLS_MAX_VERSION,
  490. &sctx, &cctx, cert, privkey)))
  491. goto end;
  492. SSL_CTX_set_client_hello_cb(sctx, full_client_hello_callback, &testctr);
  493. /* The gimpy cipher list we configure can't do TLS 1.3. */
  494. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  495. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  496. "AES256-GCM-SHA384:ECDHE-ECDSA-AES256-GCM-SHA384"))
  497. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  498. &clientssl, NULL, NULL))
  499. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  500. SSL_ERROR_WANT_CLIENT_HELLO_CB))
  501. /*
  502. * Passing a -1 literal is a hack since
  503. * the real value was lost.
  504. * */
  505. || !TEST_int_eq(SSL_get_error(serverssl, -1),
  506. SSL_ERROR_WANT_CLIENT_HELLO_CB)
  507. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  508. SSL_ERROR_NONE)))
  509. goto end;
  510. testresult = 1;
  511. end:
  512. SSL_free(serverssl);
  513. SSL_free(clientssl);
  514. SSL_CTX_free(sctx);
  515. SSL_CTX_free(cctx);
  516. return testresult;
  517. }
  518. /*
  519. * Very focused test to exercise a single case in the server-side state
  520. * machine, when the ChangeCipherState message needs to actually change
  521. * from one cipher to a different cipher (i.e., not changing from null
  522. * encryption to real encryption).
  523. */
  524. static int test_ccs_change_cipher(void)
  525. {
  526. SSL_CTX *cctx = NULL, *sctx = NULL;
  527. SSL *clientssl = NULL, *serverssl = NULL;
  528. SSL_SESSION *sess = NULL, *sesspre, *sesspost;
  529. int testresult = 0;
  530. int i;
  531. unsigned char buf;
  532. size_t readbytes;
  533. /*
  534. * Create a conection so we can resume and potentially (but not) use
  535. * a different cipher in the second connection.
  536. */
  537. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  538. TLS_client_method(),
  539. TLS1_VERSION, TLS1_2_VERSION,
  540. &sctx, &cctx, cert, privkey))
  541. || !TEST_true(SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET))
  542. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  543. NULL, NULL))
  544. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  545. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  546. SSL_ERROR_NONE))
  547. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  548. || !TEST_ptr(sess = SSL_get1_session(clientssl)))
  549. goto end;
  550. shutdown_ssl_connection(serverssl, clientssl);
  551. serverssl = clientssl = NULL;
  552. /* Resume, preferring a different cipher. Our server will force the
  553. * same cipher to be used as the initial handshake. */
  554. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  555. NULL, NULL))
  556. || !TEST_true(SSL_set_session(clientssl, sess))
  557. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384:AES128-GCM-SHA256"))
  558. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  559. SSL_ERROR_NONE))
  560. || !TEST_true(SSL_session_reused(clientssl))
  561. || !TEST_true(SSL_session_reused(serverssl))
  562. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  563. || !TEST_ptr_eq(sesspre, sesspost)
  564. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_128_GCM_SHA256,
  565. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  566. goto end;
  567. shutdown_ssl_connection(serverssl, clientssl);
  568. serverssl = clientssl = NULL;
  569. /*
  570. * Now create a fresh connection and try to renegotiate a different
  571. * cipher on it.
  572. */
  573. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  574. NULL, NULL))
  575. || !TEST_true(SSL_set_cipher_list(clientssl, "AES128-GCM-SHA256"))
  576. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  577. SSL_ERROR_NONE))
  578. || !TEST_ptr(sesspre = SSL_get0_session(serverssl))
  579. || !TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384"))
  580. || !TEST_true(SSL_renegotiate(clientssl))
  581. || !TEST_true(SSL_renegotiate_pending(clientssl)))
  582. goto end;
  583. /* Actually drive the renegotiation. */
  584. for (i = 0; i < 3; i++) {
  585. if (SSL_read_ex(clientssl, &buf, sizeof(buf), &readbytes) > 0) {
  586. if (!TEST_ulong_eq(readbytes, 0))
  587. goto end;
  588. } else if (!TEST_int_eq(SSL_get_error(clientssl, 0),
  589. SSL_ERROR_WANT_READ)) {
  590. goto end;
  591. }
  592. if (SSL_read_ex(serverssl, &buf, sizeof(buf), &readbytes) > 0) {
  593. if (!TEST_ulong_eq(readbytes, 0))
  594. goto end;
  595. } else if (!TEST_int_eq(SSL_get_error(serverssl, 0),
  596. SSL_ERROR_WANT_READ)) {
  597. goto end;
  598. }
  599. }
  600. /* sesspre and sesspost should be different since the cipher changed. */
  601. if (!TEST_false(SSL_renegotiate_pending(clientssl))
  602. || !TEST_false(SSL_session_reused(clientssl))
  603. || !TEST_false(SSL_session_reused(serverssl))
  604. || !TEST_ptr(sesspost = SSL_get0_session(serverssl))
  605. || !TEST_ptr_ne(sesspre, sesspost)
  606. || !TEST_int_eq(TLS1_CK_RSA_WITH_AES_256_GCM_SHA384,
  607. SSL_CIPHER_get_id(SSL_get_current_cipher(clientssl))))
  608. goto end;
  609. shutdown_ssl_connection(serverssl, clientssl);
  610. serverssl = clientssl = NULL;
  611. testresult = 1;
  612. end:
  613. SSL_free(serverssl);
  614. SSL_free(clientssl);
  615. SSL_CTX_free(sctx);
  616. SSL_CTX_free(cctx);
  617. SSL_SESSION_free(sess);
  618. return testresult;
  619. }
  620. #endif
  621. static int execute_test_large_message(const SSL_METHOD *smeth,
  622. const SSL_METHOD *cmeth,
  623. int min_version, int max_version,
  624. int read_ahead)
  625. {
  626. SSL_CTX *cctx = NULL, *sctx = NULL;
  627. SSL *clientssl = NULL, *serverssl = NULL;
  628. int testresult = 0;
  629. int i;
  630. BIO *certbio = NULL;
  631. X509 *chaincert = NULL;
  632. int certlen;
  633. if (!TEST_ptr(certbio = BIO_new_file(cert, "r")))
  634. goto end;
  635. chaincert = PEM_read_bio_X509(certbio, NULL, NULL, NULL);
  636. BIO_free(certbio);
  637. certbio = NULL;
  638. if (!TEST_ptr(chaincert))
  639. goto end;
  640. if (!TEST_true(create_ssl_ctx_pair(smeth, cmeth, min_version, max_version,
  641. &sctx, &cctx, cert, privkey)))
  642. goto end;
  643. if (read_ahead) {
  644. /*
  645. * Test that read_ahead works correctly when dealing with large
  646. * records
  647. */
  648. SSL_CTX_set_read_ahead(cctx, 1);
  649. }
  650. /*
  651. * We assume the supplied certificate is big enough so that if we add
  652. * NUM_EXTRA_CERTS it will make the overall message large enough. The
  653. * default buffer size is requested to be 16k, but due to the way BUF_MEM
  654. * works, it ends up allocating a little over 21k (16 * 4/3). So, in this
  655. * test we need to have a message larger than that.
  656. */
  657. certlen = i2d_X509(chaincert, NULL);
  658. OPENSSL_assert(certlen * NUM_EXTRA_CERTS >
  659. (SSL3_RT_MAX_PLAIN_LENGTH * 4) / 3);
  660. for (i = 0; i < NUM_EXTRA_CERTS; i++) {
  661. if (!X509_up_ref(chaincert))
  662. goto end;
  663. if (!SSL_CTX_add_extra_chain_cert(sctx, chaincert)) {
  664. X509_free(chaincert);
  665. goto end;
  666. }
  667. }
  668. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  669. NULL, NULL))
  670. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  671. SSL_ERROR_NONE)))
  672. goto end;
  673. /*
  674. * Calling SSL_clear() first is not required but this tests that SSL_clear()
  675. * doesn't leak (when using enable-crypto-mdebug).
  676. */
  677. if (!TEST_true(SSL_clear(serverssl)))
  678. goto end;
  679. testresult = 1;
  680. end:
  681. X509_free(chaincert);
  682. SSL_free(serverssl);
  683. SSL_free(clientssl);
  684. SSL_CTX_free(sctx);
  685. SSL_CTX_free(cctx);
  686. return testresult;
  687. }
  688. static int test_large_message_tls(void)
  689. {
  690. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  691. TLS1_VERSION, TLS_MAX_VERSION,
  692. 0);
  693. }
  694. static int test_large_message_tls_read_ahead(void)
  695. {
  696. return execute_test_large_message(TLS_server_method(), TLS_client_method(),
  697. TLS1_VERSION, TLS_MAX_VERSION,
  698. 1);
  699. }
  700. #ifndef OPENSSL_NO_DTLS
  701. static int test_large_message_dtls(void)
  702. {
  703. /*
  704. * read_ahead is not relevant to DTLS because DTLS always acts as if
  705. * read_ahead is set.
  706. */
  707. return execute_test_large_message(DTLS_server_method(),
  708. DTLS_client_method(),
  709. DTLS1_VERSION, DTLS_MAX_VERSION,
  710. 0);
  711. }
  712. #endif
  713. #ifndef OPENSSL_NO_OCSP
  714. static int ocsp_server_cb(SSL *s, void *arg)
  715. {
  716. int *argi = (int *)arg;
  717. unsigned char *copy = NULL;
  718. STACK_OF(OCSP_RESPID) *ids = NULL;
  719. OCSP_RESPID *id = NULL;
  720. if (*argi == 2) {
  721. /* In this test we are expecting exactly 1 OCSP_RESPID */
  722. SSL_get_tlsext_status_ids(s, &ids);
  723. if (ids == NULL || sk_OCSP_RESPID_num(ids) != 1)
  724. return SSL_TLSEXT_ERR_ALERT_FATAL;
  725. id = sk_OCSP_RESPID_value(ids, 0);
  726. if (id == NULL || !OCSP_RESPID_match(id, ocspcert))
  727. return SSL_TLSEXT_ERR_ALERT_FATAL;
  728. } else if (*argi != 1) {
  729. return SSL_TLSEXT_ERR_ALERT_FATAL;
  730. }
  731. if (!TEST_ptr(copy = OPENSSL_memdup(orespder, sizeof(orespder))))
  732. return SSL_TLSEXT_ERR_ALERT_FATAL;
  733. SSL_set_tlsext_status_ocsp_resp(s, copy, sizeof(orespder));
  734. ocsp_server_called = 1;
  735. return SSL_TLSEXT_ERR_OK;
  736. }
  737. static int ocsp_client_cb(SSL *s, void *arg)
  738. {
  739. int *argi = (int *)arg;
  740. const unsigned char *respderin;
  741. size_t len;
  742. if (*argi != 1 && *argi != 2)
  743. return 0;
  744. len = SSL_get_tlsext_status_ocsp_resp(s, &respderin);
  745. if (!TEST_mem_eq(orespder, len, respderin, len))
  746. return 0;
  747. ocsp_client_called = 1;
  748. return 1;
  749. }
  750. static int test_tlsext_status_type(void)
  751. {
  752. SSL_CTX *cctx = NULL, *sctx = NULL;
  753. SSL *clientssl = NULL, *serverssl = NULL;
  754. int testresult = 0;
  755. STACK_OF(OCSP_RESPID) *ids = NULL;
  756. OCSP_RESPID *id = NULL;
  757. BIO *certbio = NULL;
  758. if (!create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  759. TLS1_VERSION, TLS_MAX_VERSION,
  760. &sctx, &cctx, cert, privkey))
  761. return 0;
  762. if (SSL_CTX_get_tlsext_status_type(cctx) != -1)
  763. goto end;
  764. /* First just do various checks getting and setting tlsext_status_type */
  765. clientssl = SSL_new(cctx);
  766. if (!TEST_int_eq(SSL_get_tlsext_status_type(clientssl), -1)
  767. || !TEST_true(SSL_set_tlsext_status_type(clientssl,
  768. TLSEXT_STATUSTYPE_ocsp))
  769. || !TEST_int_eq(SSL_get_tlsext_status_type(clientssl),
  770. TLSEXT_STATUSTYPE_ocsp))
  771. goto end;
  772. SSL_free(clientssl);
  773. clientssl = NULL;
  774. if (!SSL_CTX_set_tlsext_status_type(cctx, TLSEXT_STATUSTYPE_ocsp)
  775. || SSL_CTX_get_tlsext_status_type(cctx) != TLSEXT_STATUSTYPE_ocsp)
  776. goto end;
  777. clientssl = SSL_new(cctx);
  778. if (SSL_get_tlsext_status_type(clientssl) != TLSEXT_STATUSTYPE_ocsp)
  779. goto end;
  780. SSL_free(clientssl);
  781. clientssl = NULL;
  782. /*
  783. * Now actually do a handshake and check OCSP information is exchanged and
  784. * the callbacks get called
  785. */
  786. SSL_CTX_set_tlsext_status_cb(cctx, ocsp_client_cb);
  787. SSL_CTX_set_tlsext_status_arg(cctx, &cdummyarg);
  788. SSL_CTX_set_tlsext_status_cb(sctx, ocsp_server_cb);
  789. SSL_CTX_set_tlsext_status_arg(sctx, &cdummyarg);
  790. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  791. &clientssl, NULL, NULL))
  792. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  793. SSL_ERROR_NONE))
  794. || !TEST_true(ocsp_client_called)
  795. || !TEST_true(ocsp_server_called))
  796. goto end;
  797. SSL_free(serverssl);
  798. SSL_free(clientssl);
  799. serverssl = NULL;
  800. clientssl = NULL;
  801. /* Try again but this time force the server side callback to fail */
  802. ocsp_client_called = 0;
  803. ocsp_server_called = 0;
  804. cdummyarg = 0;
  805. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  806. &clientssl, NULL, NULL))
  807. /* This should fail because the callback will fail */
  808. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  809. SSL_ERROR_NONE))
  810. || !TEST_false(ocsp_client_called)
  811. || !TEST_false(ocsp_server_called))
  812. goto end;
  813. SSL_free(serverssl);
  814. SSL_free(clientssl);
  815. serverssl = NULL;
  816. clientssl = NULL;
  817. /*
  818. * This time we'll get the client to send an OCSP_RESPID that it will
  819. * accept.
  820. */
  821. ocsp_client_called = 0;
  822. ocsp_server_called = 0;
  823. cdummyarg = 2;
  824. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  825. &clientssl, NULL, NULL)))
  826. goto end;
  827. /*
  828. * We'll just use any old cert for this test - it doesn't have to be an OCSP
  829. * specific one. We'll use the server cert.
  830. */
  831. if (!TEST_ptr(certbio = BIO_new_file(cert, "r"))
  832. || !TEST_ptr(id = OCSP_RESPID_new())
  833. || !TEST_ptr(ids = sk_OCSP_RESPID_new_null())
  834. || !TEST_ptr(ocspcert = PEM_read_bio_X509(certbio,
  835. NULL, NULL, NULL))
  836. || !TEST_true(OCSP_RESPID_set_by_key(id, ocspcert))
  837. || !TEST_true(sk_OCSP_RESPID_push(ids, id)))
  838. goto end;
  839. id = NULL;
  840. SSL_set_tlsext_status_ids(clientssl, ids);
  841. /* Control has been transferred */
  842. ids = NULL;
  843. BIO_free(certbio);
  844. certbio = NULL;
  845. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  846. SSL_ERROR_NONE))
  847. || !TEST_true(ocsp_client_called)
  848. || !TEST_true(ocsp_server_called))
  849. goto end;
  850. testresult = 1;
  851. end:
  852. SSL_free(serverssl);
  853. SSL_free(clientssl);
  854. SSL_CTX_free(sctx);
  855. SSL_CTX_free(cctx);
  856. sk_OCSP_RESPID_pop_free(ids, OCSP_RESPID_free);
  857. OCSP_RESPID_free(id);
  858. BIO_free(certbio);
  859. X509_free(ocspcert);
  860. ocspcert = NULL;
  861. return testresult;
  862. }
  863. #endif
  864. #if !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  865. static int new_called, remove_called, get_called;
  866. static int new_session_cb(SSL *ssl, SSL_SESSION *sess)
  867. {
  868. new_called++;
  869. /*
  870. * sess has been up-refed for us, but we don't actually need it so free it
  871. * immediately.
  872. */
  873. SSL_SESSION_free(sess);
  874. return 1;
  875. }
  876. static void remove_session_cb(SSL_CTX *ctx, SSL_SESSION *sess)
  877. {
  878. remove_called++;
  879. }
  880. static SSL_SESSION *get_sess_val = NULL;
  881. static SSL_SESSION *get_session_cb(SSL *ssl, const unsigned char *id, int len,
  882. int *copy)
  883. {
  884. get_called++;
  885. *copy = 1;
  886. return get_sess_val;
  887. }
  888. static int execute_test_session(int maxprot, int use_int_cache,
  889. int use_ext_cache)
  890. {
  891. SSL_CTX *sctx = NULL, *cctx = NULL;
  892. SSL *serverssl1 = NULL, *clientssl1 = NULL;
  893. SSL *serverssl2 = NULL, *clientssl2 = NULL;
  894. # ifndef OPENSSL_NO_TLS1_1
  895. SSL *serverssl3 = NULL, *clientssl3 = NULL;
  896. # endif
  897. SSL_SESSION *sess1 = NULL, *sess2 = NULL;
  898. int testresult = 0, numnewsesstick = 1;
  899. new_called = remove_called = 0;
  900. /* TLSv1.3 sends 2 NewSessionTickets */
  901. if (maxprot == TLS1_3_VERSION)
  902. numnewsesstick = 2;
  903. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  904. TLS1_VERSION, TLS_MAX_VERSION,
  905. &sctx, &cctx, cert, privkey)))
  906. return 0;
  907. /*
  908. * Only allow the max protocol version so we can force a connection failure
  909. * later
  910. */
  911. SSL_CTX_set_min_proto_version(cctx, maxprot);
  912. SSL_CTX_set_max_proto_version(cctx, maxprot);
  913. /* Set up session cache */
  914. if (use_ext_cache) {
  915. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  916. SSL_CTX_sess_set_remove_cb(cctx, remove_session_cb);
  917. }
  918. if (use_int_cache) {
  919. /* Also covers instance where both are set */
  920. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT);
  921. } else {
  922. SSL_CTX_set_session_cache_mode(cctx,
  923. SSL_SESS_CACHE_CLIENT
  924. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  925. }
  926. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  927. NULL, NULL))
  928. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  929. SSL_ERROR_NONE))
  930. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1)))
  931. goto end;
  932. /* Should fail because it should already be in the cache */
  933. if (use_int_cache && !TEST_false(SSL_CTX_add_session(cctx, sess1)))
  934. goto end;
  935. if (use_ext_cache
  936. && (!TEST_int_eq(new_called, numnewsesstick)
  937. || !TEST_int_eq(remove_called, 0)))
  938. goto end;
  939. new_called = remove_called = 0;
  940. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  941. &clientssl2, NULL, NULL))
  942. || !TEST_true(SSL_set_session(clientssl2, sess1))
  943. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  944. SSL_ERROR_NONE))
  945. || !TEST_true(SSL_session_reused(clientssl2)))
  946. goto end;
  947. if (maxprot == TLS1_3_VERSION) {
  948. /*
  949. * In TLSv1.3 we should have created a new session even though we have
  950. * resumed. Since we attempted a resume we should also have removed the
  951. * old ticket from the cache so that we try to only use tickets once.
  952. */
  953. if (use_ext_cache
  954. && (!TEST_int_eq(new_called, 1)
  955. || !TEST_int_eq(remove_called, 1)))
  956. goto end;
  957. } else {
  958. /*
  959. * In TLSv1.2 we expect to have resumed so no sessions added or
  960. * removed.
  961. */
  962. if (use_ext_cache
  963. && (!TEST_int_eq(new_called, 0)
  964. || !TEST_int_eq(remove_called, 0)))
  965. goto end;
  966. }
  967. SSL_SESSION_free(sess1);
  968. if (!TEST_ptr(sess1 = SSL_get1_session(clientssl2)))
  969. goto end;
  970. shutdown_ssl_connection(serverssl2, clientssl2);
  971. serverssl2 = clientssl2 = NULL;
  972. new_called = remove_called = 0;
  973. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  974. &clientssl2, NULL, NULL))
  975. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  976. SSL_ERROR_NONE)))
  977. goto end;
  978. if (!TEST_ptr(sess2 = SSL_get1_session(clientssl2)))
  979. goto end;
  980. if (use_ext_cache
  981. && (!TEST_int_eq(new_called, numnewsesstick)
  982. || !TEST_int_eq(remove_called, 0)))
  983. goto end;
  984. new_called = remove_called = 0;
  985. /*
  986. * This should clear sess2 from the cache because it is a "bad" session.
  987. * See SSL_set_session() documentation.
  988. */
  989. if (!TEST_true(SSL_set_session(clientssl2, sess1)))
  990. goto end;
  991. if (use_ext_cache
  992. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  993. goto end;
  994. if (!TEST_ptr_eq(SSL_get_session(clientssl2), sess1))
  995. goto end;
  996. if (use_int_cache) {
  997. /* Should succeeded because it should not already be in the cache */
  998. if (!TEST_true(SSL_CTX_add_session(cctx, sess2))
  999. || !TEST_true(SSL_CTX_remove_session(cctx, sess2)))
  1000. goto end;
  1001. }
  1002. new_called = remove_called = 0;
  1003. /* This shouldn't be in the cache so should fail */
  1004. if (!TEST_false(SSL_CTX_remove_session(cctx, sess2)))
  1005. goto end;
  1006. if (use_ext_cache
  1007. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1008. goto end;
  1009. # if !defined(OPENSSL_NO_TLS1_1)
  1010. new_called = remove_called = 0;
  1011. /* Force a connection failure */
  1012. SSL_CTX_set_max_proto_version(sctx, TLS1_1_VERSION);
  1013. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl3,
  1014. &clientssl3, NULL, NULL))
  1015. || !TEST_true(SSL_set_session(clientssl3, sess1))
  1016. /* This should fail because of the mismatched protocol versions */
  1017. || !TEST_false(create_ssl_connection(serverssl3, clientssl3,
  1018. SSL_ERROR_NONE)))
  1019. goto end;
  1020. /* We should have automatically removed the session from the cache */
  1021. if (use_ext_cache
  1022. && (!TEST_int_eq(new_called, 0) || !TEST_int_eq(remove_called, 1)))
  1023. goto end;
  1024. /* Should succeed because it should not already be in the cache */
  1025. if (use_int_cache && !TEST_true(SSL_CTX_add_session(cctx, sess2)))
  1026. goto end;
  1027. # endif
  1028. /* Now do some tests for server side caching */
  1029. if (use_ext_cache) {
  1030. SSL_CTX_sess_set_new_cb(cctx, NULL);
  1031. SSL_CTX_sess_set_remove_cb(cctx, NULL);
  1032. SSL_CTX_sess_set_new_cb(sctx, new_session_cb);
  1033. SSL_CTX_sess_set_remove_cb(sctx, remove_session_cb);
  1034. SSL_CTX_sess_set_get_cb(sctx, get_session_cb);
  1035. get_sess_val = NULL;
  1036. }
  1037. SSL_CTX_set_session_cache_mode(cctx, 0);
  1038. /* Internal caching is the default on the server side */
  1039. if (!use_int_cache)
  1040. SSL_CTX_set_session_cache_mode(sctx,
  1041. SSL_SESS_CACHE_SERVER
  1042. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1043. SSL_free(serverssl1);
  1044. SSL_free(clientssl1);
  1045. serverssl1 = clientssl1 = NULL;
  1046. SSL_free(serverssl2);
  1047. SSL_free(clientssl2);
  1048. serverssl2 = clientssl2 = NULL;
  1049. SSL_SESSION_free(sess1);
  1050. sess1 = NULL;
  1051. SSL_SESSION_free(sess2);
  1052. sess2 = NULL;
  1053. SSL_CTX_set_max_proto_version(sctx, maxprot);
  1054. if (maxprot == TLS1_2_VERSION)
  1055. SSL_CTX_set_options(sctx, SSL_OP_NO_TICKET);
  1056. new_called = remove_called = get_called = 0;
  1057. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl1, &clientssl1,
  1058. NULL, NULL))
  1059. || !TEST_true(create_ssl_connection(serverssl1, clientssl1,
  1060. SSL_ERROR_NONE))
  1061. || !TEST_ptr(sess1 = SSL_get1_session(clientssl1))
  1062. || !TEST_ptr(sess2 = SSL_get1_session(serverssl1)))
  1063. goto end;
  1064. if (use_int_cache) {
  1065. if (maxprot == TLS1_3_VERSION && !use_ext_cache) {
  1066. /*
  1067. * In TLSv1.3 it should not have been added to the internal cache,
  1068. * except in the case where we also have an external cache (in that
  1069. * case it gets added to the cache in order to generate remove
  1070. * events after timeout).
  1071. */
  1072. if (!TEST_false(SSL_CTX_remove_session(sctx, sess2)))
  1073. goto end;
  1074. } else {
  1075. /* Should fail because it should already be in the cache */
  1076. if (!TEST_false(SSL_CTX_add_session(sctx, sess2)))
  1077. goto end;
  1078. }
  1079. }
  1080. if (use_ext_cache) {
  1081. SSL_SESSION *tmp = sess2;
  1082. if (!TEST_int_eq(new_called, numnewsesstick)
  1083. || !TEST_int_eq(remove_called, 0)
  1084. || !TEST_int_eq(get_called, 0))
  1085. goto end;
  1086. /*
  1087. * Delete the session from the internal cache to force a lookup from
  1088. * the external cache. We take a copy first because
  1089. * SSL_CTX_remove_session() also marks the session as non-resumable.
  1090. */
  1091. if (use_int_cache && maxprot != TLS1_3_VERSION) {
  1092. if (!TEST_ptr(tmp = SSL_SESSION_dup(sess2))
  1093. || !TEST_true(SSL_CTX_remove_session(sctx, sess2)))
  1094. goto end;
  1095. SSL_SESSION_free(sess2);
  1096. }
  1097. sess2 = tmp;
  1098. }
  1099. new_called = remove_called = get_called = 0;
  1100. get_sess_val = sess2;
  1101. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl2,
  1102. &clientssl2, NULL, NULL))
  1103. || !TEST_true(SSL_set_session(clientssl2, sess1))
  1104. || !TEST_true(create_ssl_connection(serverssl2, clientssl2,
  1105. SSL_ERROR_NONE))
  1106. || !TEST_true(SSL_session_reused(clientssl2)))
  1107. goto end;
  1108. if (use_ext_cache) {
  1109. if (!TEST_int_eq(remove_called, 0))
  1110. goto end;
  1111. if (maxprot == TLS1_3_VERSION) {
  1112. if (!TEST_int_eq(new_called, 1)
  1113. || !TEST_int_eq(get_called, 0))
  1114. goto end;
  1115. } else {
  1116. if (!TEST_int_eq(new_called, 0)
  1117. || !TEST_int_eq(get_called, 1))
  1118. goto end;
  1119. }
  1120. }
  1121. testresult = 1;
  1122. end:
  1123. SSL_free(serverssl1);
  1124. SSL_free(clientssl1);
  1125. SSL_free(serverssl2);
  1126. SSL_free(clientssl2);
  1127. # ifndef OPENSSL_NO_TLS1_1
  1128. SSL_free(serverssl3);
  1129. SSL_free(clientssl3);
  1130. # endif
  1131. SSL_SESSION_free(sess1);
  1132. SSL_SESSION_free(sess2);
  1133. SSL_CTX_free(sctx);
  1134. SSL_CTX_free(cctx);
  1135. return testresult;
  1136. }
  1137. #endif /* !defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2) */
  1138. static int test_session_with_only_int_cache(void)
  1139. {
  1140. #ifndef OPENSSL_NO_TLS1_3
  1141. if (!execute_test_session(TLS1_3_VERSION, 1, 0))
  1142. return 0;
  1143. #endif
  1144. #ifndef OPENSSL_NO_TLS1_2
  1145. return execute_test_session(TLS1_2_VERSION, 1, 0);
  1146. #else
  1147. return 1;
  1148. #endif
  1149. }
  1150. static int test_session_with_only_ext_cache(void)
  1151. {
  1152. #ifndef OPENSSL_NO_TLS1_3
  1153. if (!execute_test_session(TLS1_3_VERSION, 0, 1))
  1154. return 0;
  1155. #endif
  1156. #ifndef OPENSSL_NO_TLS1_2
  1157. return execute_test_session(TLS1_2_VERSION, 0, 1);
  1158. #else
  1159. return 1;
  1160. #endif
  1161. }
  1162. static int test_session_with_both_cache(void)
  1163. {
  1164. #ifndef OPENSSL_NO_TLS1_3
  1165. if (!execute_test_session(TLS1_3_VERSION, 1, 1))
  1166. return 0;
  1167. #endif
  1168. #ifndef OPENSSL_NO_TLS1_2
  1169. return execute_test_session(TLS1_2_VERSION, 1, 1);
  1170. #else
  1171. return 1;
  1172. #endif
  1173. }
  1174. #ifndef OPENSSL_NO_TLS1_3
  1175. static SSL_SESSION *sesscache[6];
  1176. static int do_cache;
  1177. static int new_cachesession_cb(SSL *ssl, SSL_SESSION *sess)
  1178. {
  1179. if (do_cache) {
  1180. sesscache[new_called] = sess;
  1181. } else {
  1182. /* We don't need the reference to the session, so free it */
  1183. SSL_SESSION_free(sess);
  1184. }
  1185. new_called++;
  1186. return 1;
  1187. }
  1188. static int post_handshake_verify(SSL *sssl, SSL *cssl)
  1189. {
  1190. SSL_set_verify(sssl, SSL_VERIFY_PEER, NULL);
  1191. if (!TEST_true(SSL_verify_client_post_handshake(sssl)))
  1192. return 0;
  1193. /* Start handshake on the server and client */
  1194. if (!TEST_int_eq(SSL_do_handshake(sssl), 1)
  1195. || !TEST_int_le(SSL_read(cssl, NULL, 0), 0)
  1196. || !TEST_int_le(SSL_read(sssl, NULL, 0), 0)
  1197. || !TEST_true(create_ssl_connection(sssl, cssl,
  1198. SSL_ERROR_NONE)))
  1199. return 0;
  1200. return 1;
  1201. }
  1202. static int setup_ticket_test(int stateful, int idx, SSL_CTX **sctx,
  1203. SSL_CTX **cctx)
  1204. {
  1205. int sess_id_ctx = 1;
  1206. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  1207. TLS1_VERSION, TLS_MAX_VERSION, sctx,
  1208. cctx, cert, privkey))
  1209. || !TEST_true(SSL_CTX_set_num_tickets(*sctx, idx))
  1210. || !TEST_true(SSL_CTX_set_session_id_context(*sctx,
  1211. (void *)&sess_id_ctx,
  1212. sizeof(sess_id_ctx))))
  1213. return 0;
  1214. if (stateful)
  1215. SSL_CTX_set_options(*sctx, SSL_OP_NO_TICKET);
  1216. SSL_CTX_set_session_cache_mode(*cctx, SSL_SESS_CACHE_CLIENT
  1217. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1218. SSL_CTX_sess_set_new_cb(*cctx, new_cachesession_cb);
  1219. return 1;
  1220. }
  1221. static int check_resumption(int idx, SSL_CTX *sctx, SSL_CTX *cctx, int succ)
  1222. {
  1223. SSL *serverssl = NULL, *clientssl = NULL;
  1224. int i;
  1225. /* Test that we can resume with all the tickets we got given */
  1226. for (i = 0; i < idx * 2; i++) {
  1227. new_called = 0;
  1228. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1229. &clientssl, NULL, NULL))
  1230. || !TEST_true(SSL_set_session(clientssl, sesscache[i])))
  1231. goto end;
  1232. SSL_set_post_handshake_auth(clientssl, 1);
  1233. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1234. SSL_ERROR_NONE)))
  1235. goto end;
  1236. /*
  1237. * Following a successful resumption we only get 1 ticket. After a
  1238. * failed one we should get idx tickets.
  1239. */
  1240. if (succ) {
  1241. if (!TEST_true(SSL_session_reused(clientssl))
  1242. || !TEST_int_eq(new_called, 1))
  1243. goto end;
  1244. } else {
  1245. if (!TEST_false(SSL_session_reused(clientssl))
  1246. || !TEST_int_eq(new_called, idx))
  1247. goto end;
  1248. }
  1249. new_called = 0;
  1250. /* After a post-handshake authentication we should get 1 new ticket */
  1251. if (succ
  1252. && (!post_handshake_verify(serverssl, clientssl)
  1253. || !TEST_int_eq(new_called, 1)))
  1254. goto end;
  1255. SSL_shutdown(clientssl);
  1256. SSL_shutdown(serverssl);
  1257. SSL_free(serverssl);
  1258. SSL_free(clientssl);
  1259. serverssl = clientssl = NULL;
  1260. SSL_SESSION_free(sesscache[i]);
  1261. sesscache[i] = NULL;
  1262. }
  1263. return 1;
  1264. end:
  1265. SSL_free(clientssl);
  1266. SSL_free(serverssl);
  1267. return 0;
  1268. }
  1269. static int test_tickets(int stateful, int idx)
  1270. {
  1271. SSL_CTX *sctx = NULL, *cctx = NULL;
  1272. SSL *serverssl = NULL, *clientssl = NULL;
  1273. int testresult = 0;
  1274. size_t j;
  1275. /* idx is the test number, but also the number of tickets we want */
  1276. new_called = 0;
  1277. do_cache = 1;
  1278. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1279. goto end;
  1280. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1281. &clientssl, NULL, NULL)))
  1282. goto end;
  1283. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1284. SSL_ERROR_NONE))
  1285. /* Check we got the number of tickets we were expecting */
  1286. || !TEST_int_eq(idx, new_called))
  1287. goto end;
  1288. SSL_shutdown(clientssl);
  1289. SSL_shutdown(serverssl);
  1290. SSL_free(serverssl);
  1291. SSL_free(clientssl);
  1292. SSL_CTX_free(sctx);
  1293. SSL_CTX_free(cctx);
  1294. clientssl = serverssl = NULL;
  1295. sctx = cctx = NULL;
  1296. /*
  1297. * Now we try to resume with the tickets we previously created. The
  1298. * resumption attempt is expected to fail (because we're now using a new
  1299. * SSL_CTX). We should see idx number of tickets issued again.
  1300. */
  1301. /* Stop caching sessions - just count them */
  1302. do_cache = 0;
  1303. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1304. goto end;
  1305. if (!check_resumption(idx, sctx, cctx, 0))
  1306. goto end;
  1307. /* Start again with caching sessions */
  1308. new_called = 0;
  1309. do_cache = 1;
  1310. SSL_CTX_free(sctx);
  1311. SSL_CTX_free(cctx);
  1312. sctx = cctx = NULL;
  1313. if (!setup_ticket_test(stateful, idx, &sctx, &cctx))
  1314. goto end;
  1315. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1316. &clientssl, NULL, NULL)))
  1317. goto end;
  1318. SSL_set_post_handshake_auth(clientssl, 1);
  1319. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1320. SSL_ERROR_NONE))
  1321. /* Check we got the number of tickets we were expecting */
  1322. || !TEST_int_eq(idx, new_called))
  1323. goto end;
  1324. /* After a post-handshake authentication we should get new tickets issued */
  1325. if (!post_handshake_verify(serverssl, clientssl)
  1326. || !TEST_int_eq(idx * 2, new_called))
  1327. goto end;
  1328. SSL_shutdown(clientssl);
  1329. SSL_shutdown(serverssl);
  1330. SSL_free(serverssl);
  1331. SSL_free(clientssl);
  1332. serverssl = clientssl = NULL;
  1333. /* Stop caching sessions - just count them */
  1334. do_cache = 0;
  1335. /*
  1336. * Check we can resume with all the tickets we created. This time around the
  1337. * resumptions should all be successful.
  1338. */
  1339. if (!check_resumption(idx, sctx, cctx, 1))
  1340. goto end;
  1341. testresult = 1;
  1342. end:
  1343. SSL_free(serverssl);
  1344. SSL_free(clientssl);
  1345. for (j = 0; j < OSSL_NELEM(sesscache); j++) {
  1346. SSL_SESSION_free(sesscache[j]);
  1347. sesscache[j] = NULL;
  1348. }
  1349. SSL_CTX_free(sctx);
  1350. SSL_CTX_free(cctx);
  1351. return testresult;
  1352. }
  1353. static int test_stateless_tickets(int idx)
  1354. {
  1355. return test_tickets(0, idx);
  1356. }
  1357. static int test_stateful_tickets(int idx)
  1358. {
  1359. return test_tickets(1, idx);
  1360. }
  1361. static int test_psk_tickets(void)
  1362. {
  1363. SSL_CTX *sctx = NULL, *cctx = NULL;
  1364. SSL *serverssl = NULL, *clientssl = NULL;
  1365. int testresult = 0;
  1366. int sess_id_ctx = 1;
  1367. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  1368. TLS1_VERSION, TLS_MAX_VERSION, &sctx,
  1369. &cctx, NULL, NULL))
  1370. || !TEST_true(SSL_CTX_set_session_id_context(sctx,
  1371. (void *)&sess_id_ctx,
  1372. sizeof(sess_id_ctx))))
  1373. goto end;
  1374. SSL_CTX_set_session_cache_mode(cctx, SSL_SESS_CACHE_CLIENT
  1375. | SSL_SESS_CACHE_NO_INTERNAL_STORE);
  1376. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  1377. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  1378. SSL_CTX_sess_set_new_cb(cctx, new_session_cb);
  1379. use_session_cb_cnt = 0;
  1380. find_session_cb_cnt = 0;
  1381. srvid = pskid;
  1382. new_called = 0;
  1383. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1384. NULL, NULL)))
  1385. goto end;
  1386. clientpsk = serverpsk = create_a_psk(clientssl);
  1387. if (!TEST_ptr(clientpsk))
  1388. goto end;
  1389. SSL_SESSION_up_ref(clientpsk);
  1390. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  1391. SSL_ERROR_NONE))
  1392. || !TEST_int_eq(1, find_session_cb_cnt)
  1393. || !TEST_int_eq(1, use_session_cb_cnt)
  1394. /* We should always get 1 ticket when using external PSK */
  1395. || !TEST_int_eq(1, new_called))
  1396. goto end;
  1397. testresult = 1;
  1398. end:
  1399. SSL_free(serverssl);
  1400. SSL_free(clientssl);
  1401. SSL_CTX_free(sctx);
  1402. SSL_CTX_free(cctx);
  1403. SSL_SESSION_free(clientpsk);
  1404. SSL_SESSION_free(serverpsk);
  1405. clientpsk = serverpsk = NULL;
  1406. return testresult;
  1407. }
  1408. #endif
  1409. #define USE_NULL 0
  1410. #define USE_BIO_1 1
  1411. #define USE_BIO_2 2
  1412. #define USE_DEFAULT 3
  1413. #define CONNTYPE_CONNECTION_SUCCESS 0
  1414. #define CONNTYPE_CONNECTION_FAIL 1
  1415. #define CONNTYPE_NO_CONNECTION 2
  1416. #define TOTAL_NO_CONN_SSL_SET_BIO_TESTS (3 * 3 * 3 * 3)
  1417. #define TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS (2 * 2)
  1418. #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2)
  1419. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS (2 * 2)
  1420. #else
  1421. # define TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS 0
  1422. #endif
  1423. #define TOTAL_SSL_SET_BIO_TESTS TOTAL_NO_CONN_SSL_SET_BIO_TESTS \
  1424. + TOTAL_CONN_SUCCESS_SSL_SET_BIO_TESTS \
  1425. + TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS
  1426. static void setupbio(BIO **res, BIO *bio1, BIO *bio2, int type)
  1427. {
  1428. switch (type) {
  1429. case USE_NULL:
  1430. *res = NULL;
  1431. break;
  1432. case USE_BIO_1:
  1433. *res = bio1;
  1434. break;
  1435. case USE_BIO_2:
  1436. *res = bio2;
  1437. break;
  1438. }
  1439. }
  1440. /*
  1441. * Tests calls to SSL_set_bio() under various conditions.
  1442. *
  1443. * For the first 3 * 3 * 3 * 3 = 81 tests we do 2 calls to SSL_set_bio() with
  1444. * various combinations of valid BIOs or NULL being set for the rbio/wbio. We
  1445. * then do more tests where we create a successful connection first using our
  1446. * standard connection setup functions, and then call SSL_set_bio() with
  1447. * various combinations of valid BIOs or NULL. We then repeat these tests
  1448. * following a failed connection. In this last case we are looking to check that
  1449. * SSL_set_bio() functions correctly in the case where s->bbio is not NULL.
  1450. */
  1451. static int test_ssl_set_bio(int idx)
  1452. {
  1453. SSL_CTX *sctx = NULL, *cctx = NULL;
  1454. BIO *bio1 = NULL;
  1455. BIO *bio2 = NULL;
  1456. BIO *irbio = NULL, *iwbio = NULL, *nrbio = NULL, *nwbio = NULL;
  1457. SSL *serverssl = NULL, *clientssl = NULL;
  1458. int initrbio, initwbio, newrbio, newwbio, conntype;
  1459. int testresult = 0;
  1460. if (idx < TOTAL_NO_CONN_SSL_SET_BIO_TESTS) {
  1461. initrbio = idx % 3;
  1462. idx /= 3;
  1463. initwbio = idx % 3;
  1464. idx /= 3;
  1465. newrbio = idx % 3;
  1466. idx /= 3;
  1467. newwbio = idx % 3;
  1468. conntype = CONNTYPE_NO_CONNECTION;
  1469. } else {
  1470. idx -= TOTAL_NO_CONN_SSL_SET_BIO_TESTS;
  1471. initrbio = initwbio = USE_DEFAULT;
  1472. newrbio = idx % 2;
  1473. idx /= 2;
  1474. newwbio = idx % 2;
  1475. idx /= 2;
  1476. conntype = idx % 2;
  1477. }
  1478. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  1479. TLS1_VERSION, TLS_MAX_VERSION,
  1480. &sctx, &cctx, cert, privkey)))
  1481. goto end;
  1482. if (conntype == CONNTYPE_CONNECTION_FAIL) {
  1483. /*
  1484. * We won't ever get here if either TLSv1.3 or TLSv1.2 is disabled
  1485. * because we reduced the number of tests in the definition of
  1486. * TOTAL_CONN_FAIL_SSL_SET_BIO_TESTS to avoid this scenario. By setting
  1487. * mismatched protocol versions we will force a connection failure.
  1488. */
  1489. SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION);
  1490. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  1491. }
  1492. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  1493. NULL, NULL)))
  1494. goto end;
  1495. if (initrbio == USE_BIO_1
  1496. || initwbio == USE_BIO_1
  1497. || newrbio == USE_BIO_1
  1498. || newwbio == USE_BIO_1) {
  1499. if (!TEST_ptr(bio1 = BIO_new(BIO_s_mem())))
  1500. goto end;
  1501. }
  1502. if (initrbio == USE_BIO_2
  1503. || initwbio == USE_BIO_2
  1504. || newrbio == USE_BIO_2
  1505. || newwbio == USE_BIO_2) {
  1506. if (!TEST_ptr(bio2 = BIO_new(BIO_s_mem())))
  1507. goto end;
  1508. }
  1509. if (initrbio != USE_DEFAULT) {
  1510. setupbio(&irbio, bio1, bio2, initrbio);
  1511. setupbio(&iwbio, bio1, bio2, initwbio);
  1512. SSL_set_bio(clientssl, irbio, iwbio);
  1513. /*
  1514. * We want to maintain our own refs to these BIO, so do an up ref for
  1515. * each BIO that will have ownership transferred in the SSL_set_bio()
  1516. * call
  1517. */
  1518. if (irbio != NULL)
  1519. BIO_up_ref(irbio);
  1520. if (iwbio != NULL && iwbio != irbio)
  1521. BIO_up_ref(iwbio);
  1522. }
  1523. if (conntype != CONNTYPE_NO_CONNECTION
  1524. && !TEST_true(create_ssl_connection(serverssl, clientssl,
  1525. SSL_ERROR_NONE)
  1526. == (conntype == CONNTYPE_CONNECTION_SUCCESS)))
  1527. goto end;
  1528. setupbio(&nrbio, bio1, bio2, newrbio);
  1529. setupbio(&nwbio, bio1, bio2, newwbio);
  1530. /*
  1531. * We will (maybe) transfer ownership again so do more up refs.
  1532. * SSL_set_bio() has some really complicated ownership rules where BIOs have
  1533. * already been set!
  1534. */
  1535. if (nrbio != NULL
  1536. && nrbio != irbio
  1537. && (nwbio != iwbio || nrbio != nwbio))
  1538. BIO_up_ref(nrbio);
  1539. if (nwbio != NULL
  1540. && nwbio != nrbio
  1541. && (nwbio != iwbio || (nwbio == iwbio && irbio == iwbio)))
  1542. BIO_up_ref(nwbio);
  1543. SSL_set_bio(clientssl, nrbio, nwbio);
  1544. testresult = 1;
  1545. end:
  1546. BIO_free(bio1);
  1547. BIO_free(bio2);
  1548. /*
  1549. * This test is checking that the ref counting for SSL_set_bio is correct.
  1550. * If we get here and we did too many frees then we will fail in the above
  1551. * functions. If we haven't done enough then this will only be detected in
  1552. * a crypto-mdebug build
  1553. */
  1554. SSL_free(serverssl);
  1555. SSL_free(clientssl);
  1556. SSL_CTX_free(sctx);
  1557. SSL_CTX_free(cctx);
  1558. return testresult;
  1559. }
  1560. typedef enum { NO_BIO_CHANGE, CHANGE_RBIO, CHANGE_WBIO } bio_change_t;
  1561. static int execute_test_ssl_bio(int pop_ssl, bio_change_t change_bio)
  1562. {
  1563. BIO *sslbio = NULL, *membio1 = NULL, *membio2 = NULL;
  1564. SSL_CTX *ctx;
  1565. SSL *ssl = NULL;
  1566. int testresult = 0;
  1567. if (!TEST_ptr(ctx = SSL_CTX_new(TLS_method()))
  1568. || !TEST_ptr(ssl = SSL_new(ctx))
  1569. || !TEST_ptr(sslbio = BIO_new(BIO_f_ssl()))
  1570. || !TEST_ptr(membio1 = BIO_new(BIO_s_mem())))
  1571. goto end;
  1572. BIO_set_ssl(sslbio, ssl, BIO_CLOSE);
  1573. /*
  1574. * If anything goes wrong here then we could leak memory, so this will
  1575. * be caught in a crypto-mdebug build
  1576. */
  1577. BIO_push(sslbio, membio1);
  1578. /* Verify changing the rbio/wbio directly does not cause leaks */
  1579. if (change_bio != NO_BIO_CHANGE) {
  1580. if (!TEST_ptr(membio2 = BIO_new(BIO_s_mem()))) {
  1581. ssl = NULL;
  1582. goto end;
  1583. }
  1584. if (change_bio == CHANGE_RBIO)
  1585. SSL_set0_rbio(ssl, membio2);
  1586. else
  1587. SSL_set0_wbio(ssl, membio2);
  1588. }
  1589. ssl = NULL;
  1590. if (pop_ssl)
  1591. BIO_pop(sslbio);
  1592. else
  1593. BIO_pop(membio1);
  1594. testresult = 1;
  1595. end:
  1596. BIO_free(membio1);
  1597. BIO_free(sslbio);
  1598. SSL_free(ssl);
  1599. SSL_CTX_free(ctx);
  1600. return testresult;
  1601. }
  1602. static int test_ssl_bio_pop_next_bio(void)
  1603. {
  1604. return execute_test_ssl_bio(0, NO_BIO_CHANGE);
  1605. }
  1606. static int test_ssl_bio_pop_ssl_bio(void)
  1607. {
  1608. return execute_test_ssl_bio(1, NO_BIO_CHANGE);
  1609. }
  1610. static int test_ssl_bio_change_rbio(void)
  1611. {
  1612. return execute_test_ssl_bio(0, CHANGE_RBIO);
  1613. }
  1614. static int test_ssl_bio_change_wbio(void)
  1615. {
  1616. return execute_test_ssl_bio(0, CHANGE_WBIO);
  1617. }
  1618. #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
  1619. typedef struct {
  1620. /* The list of sig algs */
  1621. const int *list;
  1622. /* The length of the list */
  1623. size_t listlen;
  1624. /* A sigalgs list in string format */
  1625. const char *liststr;
  1626. /* Whether setting the list should succeed */
  1627. int valid;
  1628. /* Whether creating a connection with the list should succeed */
  1629. int connsuccess;
  1630. } sigalgs_list;
  1631. static const int validlist1[] = {NID_sha256, EVP_PKEY_RSA};
  1632. # ifndef OPENSSL_NO_EC
  1633. static const int validlist2[] = {NID_sha256, EVP_PKEY_RSA, NID_sha512, EVP_PKEY_EC};
  1634. static const int validlist3[] = {NID_sha512, EVP_PKEY_EC};
  1635. # endif
  1636. static const int invalidlist1[] = {NID_undef, EVP_PKEY_RSA};
  1637. static const int invalidlist2[] = {NID_sha256, NID_undef};
  1638. static const int invalidlist3[] = {NID_sha256, EVP_PKEY_RSA, NID_sha256};
  1639. static const int invalidlist4[] = {NID_sha256};
  1640. static const sigalgs_list testsigalgs[] = {
  1641. {validlist1, OSSL_NELEM(validlist1), NULL, 1, 1},
  1642. # ifndef OPENSSL_NO_EC
  1643. {validlist2, OSSL_NELEM(validlist2), NULL, 1, 1},
  1644. {validlist3, OSSL_NELEM(validlist3), NULL, 1, 0},
  1645. # endif
  1646. {NULL, 0, "RSA+SHA256", 1, 1},
  1647. # ifndef OPENSSL_NO_EC
  1648. {NULL, 0, "RSA+SHA256:ECDSA+SHA512", 1, 1},
  1649. {NULL, 0, "ECDSA+SHA512", 1, 0},
  1650. # endif
  1651. {invalidlist1, OSSL_NELEM(invalidlist1), NULL, 0, 0},
  1652. {invalidlist2, OSSL_NELEM(invalidlist2), NULL, 0, 0},
  1653. {invalidlist3, OSSL_NELEM(invalidlist3), NULL, 0, 0},
  1654. {invalidlist4, OSSL_NELEM(invalidlist4), NULL, 0, 0},
  1655. {NULL, 0, "RSA", 0, 0},
  1656. {NULL, 0, "SHA256", 0, 0},
  1657. {NULL, 0, "RSA+SHA256:SHA256", 0, 0},
  1658. {NULL, 0, "Invalid", 0, 0}
  1659. };
  1660. static int test_set_sigalgs(int idx)
  1661. {
  1662. SSL_CTX *cctx = NULL, *sctx = NULL;
  1663. SSL *clientssl = NULL, *serverssl = NULL;
  1664. int testresult = 0;
  1665. const sigalgs_list *curr;
  1666. int testctx;
  1667. /* Should never happen */
  1668. if (!TEST_size_t_le((size_t)idx, OSSL_NELEM(testsigalgs) * 2))
  1669. return 0;
  1670. testctx = ((size_t)idx < OSSL_NELEM(testsigalgs));
  1671. curr = testctx ? &testsigalgs[idx]
  1672. : &testsigalgs[idx - OSSL_NELEM(testsigalgs)];
  1673. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  1674. TLS1_VERSION, TLS_MAX_VERSION,
  1675. &sctx, &cctx, cert, privkey)))
  1676. return 0;
  1677. /*
  1678. * TODO(TLS1.3): These APIs cannot set TLSv1.3 sig algs so we just test it
  1679. * for TLSv1.2 for now until we add a new API.
  1680. */
  1681. SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION);
  1682. if (testctx) {
  1683. int ret;
  1684. if (curr->list != NULL)
  1685. ret = SSL_CTX_set1_sigalgs(cctx, curr->list, curr->listlen);
  1686. else
  1687. ret = SSL_CTX_set1_sigalgs_list(cctx, curr->liststr);
  1688. if (!ret) {
  1689. if (curr->valid)
  1690. TEST_info("Failure setting sigalgs in SSL_CTX (%d)\n", idx);
  1691. else
  1692. testresult = 1;
  1693. goto end;
  1694. }
  1695. if (!curr->valid) {
  1696. TEST_info("Not-failed setting sigalgs in SSL_CTX (%d)\n", idx);
  1697. goto end;
  1698. }
  1699. }
  1700. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  1701. &clientssl, NULL, NULL)))
  1702. goto end;
  1703. if (!testctx) {
  1704. int ret;
  1705. if (curr->list != NULL)
  1706. ret = SSL_set1_sigalgs(clientssl, curr->list, curr->listlen);
  1707. else
  1708. ret = SSL_set1_sigalgs_list(clientssl, curr->liststr);
  1709. if (!ret) {
  1710. if (curr->valid)
  1711. TEST_info("Failure setting sigalgs in SSL (%d)\n", idx);
  1712. else
  1713. testresult = 1;
  1714. goto end;
  1715. }
  1716. if (!curr->valid)
  1717. goto end;
  1718. }
  1719. if (!TEST_int_eq(create_ssl_connection(serverssl, clientssl,
  1720. SSL_ERROR_NONE),
  1721. curr->connsuccess))
  1722. goto end;
  1723. testresult = 1;
  1724. end:
  1725. SSL_free(serverssl);
  1726. SSL_free(clientssl);
  1727. SSL_CTX_free(sctx);
  1728. SSL_CTX_free(cctx);
  1729. return testresult;
  1730. }
  1731. #endif
  1732. #ifndef OPENSSL_NO_TLS1_3
  1733. static int psk_client_cb_cnt = 0;
  1734. static int psk_server_cb_cnt = 0;
  1735. static int use_session_cb(SSL *ssl, const EVP_MD *md, const unsigned char **id,
  1736. size_t *idlen, SSL_SESSION **sess)
  1737. {
  1738. switch (++use_session_cb_cnt) {
  1739. case 1:
  1740. /* The first call should always have a NULL md */
  1741. if (md != NULL)
  1742. return 0;
  1743. break;
  1744. case 2:
  1745. /* The second call should always have an md */
  1746. if (md == NULL)
  1747. return 0;
  1748. break;
  1749. default:
  1750. /* We should only be called a maximum of twice */
  1751. return 0;
  1752. }
  1753. if (clientpsk != NULL)
  1754. SSL_SESSION_up_ref(clientpsk);
  1755. *sess = clientpsk;
  1756. *id = (const unsigned char *)pskid;
  1757. *idlen = strlen(pskid);
  1758. return 1;
  1759. }
  1760. #ifndef OPENSSL_NO_PSK
  1761. static unsigned int psk_client_cb(SSL *ssl, const char *hint, char *id,
  1762. unsigned int max_id_len,
  1763. unsigned char *psk,
  1764. unsigned int max_psk_len)
  1765. {
  1766. unsigned int psklen = 0;
  1767. psk_client_cb_cnt++;
  1768. if (strlen(pskid) + 1 > max_id_len)
  1769. return 0;
  1770. /* We should only ever be called a maximum of twice per connection */
  1771. if (psk_client_cb_cnt > 2)
  1772. return 0;
  1773. if (clientpsk == NULL)
  1774. return 0;
  1775. /* We'll reuse the PSK we set up for TLSv1.3 */
  1776. if (SSL_SESSION_get_master_key(clientpsk, NULL, 0) > max_psk_len)
  1777. return 0;
  1778. psklen = SSL_SESSION_get_master_key(clientpsk, psk, max_psk_len);
  1779. strncpy(id, pskid, max_id_len);
  1780. return psklen;
  1781. }
  1782. #endif /* OPENSSL_NO_PSK */
  1783. static int find_session_cb(SSL *ssl, const unsigned char *identity,
  1784. size_t identity_len, SSL_SESSION **sess)
  1785. {
  1786. find_session_cb_cnt++;
  1787. /* We should only ever be called a maximum of twice per connection */
  1788. if (find_session_cb_cnt > 2)
  1789. return 0;
  1790. if (serverpsk == NULL)
  1791. return 0;
  1792. /* Identity should match that set by the client */
  1793. if (strlen(srvid) != identity_len
  1794. || strncmp(srvid, (const char *)identity, identity_len) != 0) {
  1795. /* No PSK found, continue but without a PSK */
  1796. *sess = NULL;
  1797. return 1;
  1798. }
  1799. SSL_SESSION_up_ref(serverpsk);
  1800. *sess = serverpsk;
  1801. return 1;
  1802. }
  1803. #ifndef OPENSSL_NO_PSK
  1804. static unsigned int psk_server_cb(SSL *ssl, const char *identity,
  1805. unsigned char *psk, unsigned int max_psk_len)
  1806. {
  1807. unsigned int psklen = 0;
  1808. psk_server_cb_cnt++;
  1809. /* We should only ever be called a maximum of twice per connection */
  1810. if (find_session_cb_cnt > 2)
  1811. return 0;
  1812. if (serverpsk == NULL)
  1813. return 0;
  1814. /* Identity should match that set by the client */
  1815. if (strcmp(srvid, identity) != 0) {
  1816. return 0;
  1817. }
  1818. /* We'll reuse the PSK we set up for TLSv1.3 */
  1819. if (SSL_SESSION_get_master_key(serverpsk, NULL, 0) > max_psk_len)
  1820. return 0;
  1821. psklen = SSL_SESSION_get_master_key(serverpsk, psk, max_psk_len);
  1822. return psklen;
  1823. }
  1824. #endif /* OPENSSL_NO_PSK */
  1825. #define MSG1 "Hello"
  1826. #define MSG2 "World."
  1827. #define MSG3 "This"
  1828. #define MSG4 "is"
  1829. #define MSG5 "a"
  1830. #define MSG6 "test"
  1831. #define MSG7 "message."
  1832. #define TLS13_AES_128_GCM_SHA256_BYTES ((const unsigned char *)"\x13\x01")
  1833. #define TLS13_AES_256_GCM_SHA384_BYTES ((const unsigned char *)"\x13\x02")
  1834. #define TLS13_CHACHA20_POLY1305_SHA256_BYTES ((const unsigned char *)"\x13\x03")
  1835. #define TLS13_AES_128_CCM_SHA256_BYTES ((const unsigned char *)"\x13\x04")
  1836. #define TLS13_AES_128_CCM_8_SHA256_BYTES ((const unsigned char *)"\x13\05")
  1837. static SSL_SESSION *create_a_psk(SSL *ssl)
  1838. {
  1839. const SSL_CIPHER *cipher = NULL;
  1840. const unsigned char key[] = {
  1841. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a,
  1842. 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
  1843. 0x16, 0x17, 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20,
  1844. 0x21, 0x22, 0x23, 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b,
  1845. 0x2c, 0x2d, 0x2e, 0x2f
  1846. };
  1847. SSL_SESSION *sess = NULL;
  1848. cipher = SSL_CIPHER_find(ssl, TLS13_AES_256_GCM_SHA384_BYTES);
  1849. sess = SSL_SESSION_new();
  1850. if (!TEST_ptr(sess)
  1851. || !TEST_ptr(cipher)
  1852. || !TEST_true(SSL_SESSION_set1_master_key(sess, key,
  1853. sizeof(key)))
  1854. || !TEST_true(SSL_SESSION_set_cipher(sess, cipher))
  1855. || !TEST_true(
  1856. SSL_SESSION_set_protocol_version(sess,
  1857. TLS1_3_VERSION))) {
  1858. SSL_SESSION_free(sess);
  1859. return NULL;
  1860. }
  1861. return sess;
  1862. }
  1863. /*
  1864. * Helper method to setup objects for early data test. Caller frees objects on
  1865. * error.
  1866. */
  1867. static int setupearly_data_test(SSL_CTX **cctx, SSL_CTX **sctx, SSL **clientssl,
  1868. SSL **serverssl, SSL_SESSION **sess, int idx)
  1869. {
  1870. if (*sctx == NULL
  1871. && !TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  1872. TLS_client_method(),
  1873. TLS1_VERSION, TLS_MAX_VERSION,
  1874. sctx, cctx, cert, privkey)))
  1875. return 0;
  1876. if (!TEST_true(SSL_CTX_set_max_early_data(*sctx, SSL3_RT_MAX_PLAIN_LENGTH)))
  1877. return 0;
  1878. if (idx == 1) {
  1879. /* When idx == 1 we repeat the tests with read_ahead set */
  1880. SSL_CTX_set_read_ahead(*cctx, 1);
  1881. SSL_CTX_set_read_ahead(*sctx, 1);
  1882. } else if (idx == 2) {
  1883. /* When idx == 2 we are doing early_data with a PSK. Set up callbacks */
  1884. SSL_CTX_set_psk_use_session_callback(*cctx, use_session_cb);
  1885. SSL_CTX_set_psk_find_session_callback(*sctx, find_session_cb);
  1886. use_session_cb_cnt = 0;
  1887. find_session_cb_cnt = 0;
  1888. srvid = pskid;
  1889. }
  1890. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl, clientssl,
  1891. NULL, NULL)))
  1892. return 0;
  1893. /*
  1894. * For one of the run throughs (doesn't matter which one), we'll try sending
  1895. * some SNI data in the initial ClientHello. This will be ignored (because
  1896. * there is no SNI cb set up by the server), so it should not impact
  1897. * early_data.
  1898. */
  1899. if (idx == 1
  1900. && !TEST_true(SSL_set_tlsext_host_name(*clientssl, "localhost")))
  1901. return 0;
  1902. if (idx == 2) {
  1903. clientpsk = create_a_psk(*clientssl);
  1904. if (!TEST_ptr(clientpsk)
  1905. /*
  1906. * We just choose an arbitrary value for max_early_data which
  1907. * should be big enough for testing purposes.
  1908. */
  1909. || !TEST_true(SSL_SESSION_set_max_early_data(clientpsk,
  1910. 0x100))
  1911. || !TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  1912. SSL_SESSION_free(clientpsk);
  1913. clientpsk = NULL;
  1914. return 0;
  1915. }
  1916. serverpsk = clientpsk;
  1917. if (sess != NULL) {
  1918. if (!TEST_true(SSL_SESSION_up_ref(clientpsk))) {
  1919. SSL_SESSION_free(clientpsk);
  1920. SSL_SESSION_free(serverpsk);
  1921. clientpsk = serverpsk = NULL;
  1922. return 0;
  1923. }
  1924. *sess = clientpsk;
  1925. }
  1926. return 1;
  1927. }
  1928. if (sess == NULL)
  1929. return 1;
  1930. if (!TEST_true(create_ssl_connection(*serverssl, *clientssl,
  1931. SSL_ERROR_NONE)))
  1932. return 0;
  1933. *sess = SSL_get1_session(*clientssl);
  1934. SSL_shutdown(*clientssl);
  1935. SSL_shutdown(*serverssl);
  1936. SSL_free(*serverssl);
  1937. SSL_free(*clientssl);
  1938. *serverssl = *clientssl = NULL;
  1939. if (!TEST_true(create_ssl_objects(*sctx, *cctx, serverssl,
  1940. clientssl, NULL, NULL))
  1941. || !TEST_true(SSL_set_session(*clientssl, *sess)))
  1942. return 0;
  1943. return 1;
  1944. }
  1945. static int test_early_data_read_write(int idx)
  1946. {
  1947. SSL_CTX *cctx = NULL, *sctx = NULL;
  1948. SSL *clientssl = NULL, *serverssl = NULL;
  1949. int testresult = 0;
  1950. SSL_SESSION *sess = NULL;
  1951. unsigned char buf[20], data[1024];
  1952. size_t readbytes, written, eoedlen, rawread, rawwritten;
  1953. BIO *rbio;
  1954. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  1955. &serverssl, &sess, idx)))
  1956. goto end;
  1957. /* Write and read some early data */
  1958. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  1959. &written))
  1960. || !TEST_size_t_eq(written, strlen(MSG1))
  1961. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  1962. sizeof(buf), &readbytes),
  1963. SSL_READ_EARLY_DATA_SUCCESS)
  1964. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  1965. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  1966. SSL_EARLY_DATA_ACCEPTED))
  1967. goto end;
  1968. /*
  1969. * Server should be able to write data, and client should be able to
  1970. * read it.
  1971. */
  1972. if (!TEST_true(SSL_write_early_data(serverssl, MSG2, strlen(MSG2),
  1973. &written))
  1974. || !TEST_size_t_eq(written, strlen(MSG2))
  1975. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1976. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  1977. goto end;
  1978. /* Even after reading normal data, client should be able write early data */
  1979. if (!TEST_true(SSL_write_early_data(clientssl, MSG3, strlen(MSG3),
  1980. &written))
  1981. || !TEST_size_t_eq(written, strlen(MSG3)))
  1982. goto end;
  1983. /* Server should still be able read early data after writing data */
  1984. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  1985. &readbytes),
  1986. SSL_READ_EARLY_DATA_SUCCESS)
  1987. || !TEST_mem_eq(buf, readbytes, MSG3, strlen(MSG3)))
  1988. goto end;
  1989. /* Write more data from server and read it from client */
  1990. if (!TEST_true(SSL_write_early_data(serverssl, MSG4, strlen(MSG4),
  1991. &written))
  1992. || !TEST_size_t_eq(written, strlen(MSG4))
  1993. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  1994. || !TEST_mem_eq(buf, readbytes, MSG4, strlen(MSG4)))
  1995. goto end;
  1996. /*
  1997. * If client writes normal data it should mean writing early data is no
  1998. * longer possible.
  1999. */
  2000. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  2001. || !TEST_size_t_eq(written, strlen(MSG5))
  2002. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2003. SSL_EARLY_DATA_ACCEPTED))
  2004. goto end;
  2005. /*
  2006. * At this point the client has written EndOfEarlyData, ClientFinished and
  2007. * normal (fully protected) data. We are going to cause a delay between the
  2008. * arrival of EndOfEarlyData and ClientFinished. We read out all the data
  2009. * in the read BIO, and then just put back the EndOfEarlyData message.
  2010. */
  2011. rbio = SSL_get_rbio(serverssl);
  2012. if (!TEST_true(BIO_read_ex(rbio, data, sizeof(data), &rawread))
  2013. || !TEST_size_t_lt(rawread, sizeof(data))
  2014. || !TEST_size_t_gt(rawread, SSL3_RT_HEADER_LENGTH))
  2015. goto end;
  2016. /* Record length is in the 4th and 5th bytes of the record header */
  2017. eoedlen = SSL3_RT_HEADER_LENGTH + (data[3] << 8 | data[4]);
  2018. if (!TEST_true(BIO_write_ex(rbio, data, eoedlen, &rawwritten))
  2019. || !TEST_size_t_eq(rawwritten, eoedlen))
  2020. goto end;
  2021. /* Server should be told that there is no more early data */
  2022. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2023. &readbytes),
  2024. SSL_READ_EARLY_DATA_FINISH)
  2025. || !TEST_size_t_eq(readbytes, 0))
  2026. goto end;
  2027. /*
  2028. * Server has not finished init yet, so should still be able to write early
  2029. * data.
  2030. */
  2031. if (!TEST_true(SSL_write_early_data(serverssl, MSG6, strlen(MSG6),
  2032. &written))
  2033. || !TEST_size_t_eq(written, strlen(MSG6)))
  2034. goto end;
  2035. /* Push the ClientFinished and the normal data back into the server rbio */
  2036. if (!TEST_true(BIO_write_ex(rbio, data + eoedlen, rawread - eoedlen,
  2037. &rawwritten))
  2038. || !TEST_size_t_eq(rawwritten, rawread - eoedlen))
  2039. goto end;
  2040. /* Server should be able to read normal data */
  2041. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2042. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  2043. goto end;
  2044. /* Client and server should not be able to write/read early data now */
  2045. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  2046. &written)))
  2047. goto end;
  2048. ERR_clear_error();
  2049. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2050. &readbytes),
  2051. SSL_READ_EARLY_DATA_ERROR))
  2052. goto end;
  2053. ERR_clear_error();
  2054. /* Client should be able to read the data sent by the server */
  2055. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2056. || !TEST_mem_eq(buf, readbytes, MSG6, strlen(MSG6)))
  2057. goto end;
  2058. /*
  2059. * Make sure we process the two NewSessionTickets. These arrive
  2060. * post-handshake. We attempt reads which we do not expect to return any
  2061. * data.
  2062. */
  2063. if (!TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2064. || !TEST_false(SSL_read_ex(clientssl, buf, sizeof(buf),
  2065. &readbytes)))
  2066. goto end;
  2067. /* Server should be able to write normal data */
  2068. if (!TEST_true(SSL_write_ex(serverssl, MSG7, strlen(MSG7), &written))
  2069. || !TEST_size_t_eq(written, strlen(MSG7))
  2070. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2071. || !TEST_mem_eq(buf, readbytes, MSG7, strlen(MSG7)))
  2072. goto end;
  2073. SSL_SESSION_free(sess);
  2074. sess = SSL_get1_session(clientssl);
  2075. use_session_cb_cnt = 0;
  2076. find_session_cb_cnt = 0;
  2077. SSL_shutdown(clientssl);
  2078. SSL_shutdown(serverssl);
  2079. SSL_free(serverssl);
  2080. SSL_free(clientssl);
  2081. serverssl = clientssl = NULL;
  2082. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2083. &clientssl, NULL, NULL))
  2084. || !TEST_true(SSL_set_session(clientssl, sess)))
  2085. goto end;
  2086. /* Write and read some early data */
  2087. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2088. &written))
  2089. || !TEST_size_t_eq(written, strlen(MSG1))
  2090. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2091. &readbytes),
  2092. SSL_READ_EARLY_DATA_SUCCESS)
  2093. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  2094. goto end;
  2095. if (!TEST_int_gt(SSL_connect(clientssl), 0)
  2096. || !TEST_int_gt(SSL_accept(serverssl), 0))
  2097. goto end;
  2098. /* Client and server should not be able to write/read early data now */
  2099. if (!TEST_false(SSL_write_early_data(clientssl, MSG6, strlen(MSG6),
  2100. &written)))
  2101. goto end;
  2102. ERR_clear_error();
  2103. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2104. &readbytes),
  2105. SSL_READ_EARLY_DATA_ERROR))
  2106. goto end;
  2107. ERR_clear_error();
  2108. /* Client and server should be able to write/read normal data */
  2109. if (!TEST_true(SSL_write_ex(clientssl, MSG5, strlen(MSG5), &written))
  2110. || !TEST_size_t_eq(written, strlen(MSG5))
  2111. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2112. || !TEST_size_t_eq(readbytes, strlen(MSG5)))
  2113. goto end;
  2114. testresult = 1;
  2115. end:
  2116. SSL_SESSION_free(sess);
  2117. SSL_SESSION_free(clientpsk);
  2118. SSL_SESSION_free(serverpsk);
  2119. clientpsk = serverpsk = NULL;
  2120. SSL_free(serverssl);
  2121. SSL_free(clientssl);
  2122. SSL_CTX_free(sctx);
  2123. SSL_CTX_free(cctx);
  2124. return testresult;
  2125. }
  2126. static int allow_ed_cb_called = 0;
  2127. static int allow_early_data_cb(SSL *s, void *arg)
  2128. {
  2129. int *usecb = (int *)arg;
  2130. allow_ed_cb_called++;
  2131. if (*usecb == 1)
  2132. return 0;
  2133. return 1;
  2134. }
  2135. /*
  2136. * idx == 0: Standard early_data setup
  2137. * idx == 1: early_data setup using read_ahead
  2138. * usecb == 0: Don't use a custom early data callback
  2139. * usecb == 1: Use a custom early data callback and reject the early data
  2140. * usecb == 2: Use a custom early data callback and accept the early data
  2141. * confopt == 0: Configure anti-replay directly
  2142. * confopt == 1: Configure anti-replay using SSL_CONF
  2143. */
  2144. static int test_early_data_replay_int(int idx, int usecb, int confopt)
  2145. {
  2146. SSL_CTX *cctx = NULL, *sctx = NULL;
  2147. SSL *clientssl = NULL, *serverssl = NULL;
  2148. int testresult = 0;
  2149. SSL_SESSION *sess = NULL;
  2150. size_t readbytes, written;
  2151. unsigned char buf[20];
  2152. allow_ed_cb_called = 0;
  2153. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  2154. TLS1_VERSION, TLS_MAX_VERSION, &sctx,
  2155. &cctx, cert, privkey)))
  2156. return 0;
  2157. if (usecb > 0) {
  2158. if (confopt == 0) {
  2159. SSL_CTX_set_options(sctx, SSL_OP_NO_ANTI_REPLAY);
  2160. } else {
  2161. SSL_CONF_CTX *confctx = SSL_CONF_CTX_new();
  2162. if (!TEST_ptr(confctx))
  2163. goto end;
  2164. SSL_CONF_CTX_set_flags(confctx, SSL_CONF_FLAG_FILE
  2165. | SSL_CONF_FLAG_SERVER);
  2166. SSL_CONF_CTX_set_ssl_ctx(confctx, sctx);
  2167. if (!TEST_int_eq(SSL_CONF_cmd(confctx, "Options", "-AntiReplay"),
  2168. 2)) {
  2169. SSL_CONF_CTX_free(confctx);
  2170. goto end;
  2171. }
  2172. SSL_CONF_CTX_free(confctx);
  2173. }
  2174. SSL_CTX_set_allow_early_data_cb(sctx, allow_early_data_cb, &usecb);
  2175. }
  2176. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2177. &serverssl, &sess, idx)))
  2178. goto end;
  2179. /*
  2180. * The server is configured to accept early data. Create a connection to
  2181. * "use up" the ticket
  2182. */
  2183. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  2184. || !TEST_true(SSL_session_reused(clientssl)))
  2185. goto end;
  2186. SSL_shutdown(clientssl);
  2187. SSL_shutdown(serverssl);
  2188. SSL_free(serverssl);
  2189. SSL_free(clientssl);
  2190. serverssl = clientssl = NULL;
  2191. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2192. &clientssl, NULL, NULL))
  2193. || !TEST_true(SSL_set_session(clientssl, sess)))
  2194. goto end;
  2195. /* Write and read some early data */
  2196. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2197. &written))
  2198. || !TEST_size_t_eq(written, strlen(MSG1)))
  2199. goto end;
  2200. if (usecb <= 1) {
  2201. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2202. &readbytes),
  2203. SSL_READ_EARLY_DATA_FINISH)
  2204. /*
  2205. * The ticket was reused, so the we should have rejected the
  2206. * early data
  2207. */
  2208. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2209. SSL_EARLY_DATA_REJECTED))
  2210. goto end;
  2211. } else {
  2212. /* In this case the callback decides to accept the early data */
  2213. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2214. &readbytes),
  2215. SSL_READ_EARLY_DATA_SUCCESS)
  2216. || !TEST_mem_eq(MSG1, strlen(MSG1), buf, readbytes)
  2217. /*
  2218. * Server will have sent its flight so client can now send
  2219. * end of early data and complete its half of the handshake
  2220. */
  2221. || !TEST_int_gt(SSL_connect(clientssl), 0)
  2222. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2223. &readbytes),
  2224. SSL_READ_EARLY_DATA_FINISH)
  2225. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2226. SSL_EARLY_DATA_ACCEPTED))
  2227. goto end;
  2228. }
  2229. /* Complete the connection */
  2230. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  2231. || !TEST_int_eq(SSL_session_reused(clientssl), (usecb > 0) ? 1 : 0)
  2232. || !TEST_int_eq(allow_ed_cb_called, usecb > 0 ? 1 : 0))
  2233. goto end;
  2234. testresult = 1;
  2235. end:
  2236. SSL_SESSION_free(sess);
  2237. SSL_SESSION_free(clientpsk);
  2238. SSL_SESSION_free(serverpsk);
  2239. clientpsk = serverpsk = NULL;
  2240. SSL_free(serverssl);
  2241. SSL_free(clientssl);
  2242. SSL_CTX_free(sctx);
  2243. SSL_CTX_free(cctx);
  2244. return testresult;
  2245. }
  2246. static int test_early_data_replay(int idx)
  2247. {
  2248. int ret = 1, usecb, confopt;
  2249. for (usecb = 0; usecb < 3; usecb++) {
  2250. for (confopt = 0; confopt < 2; confopt++)
  2251. ret &= test_early_data_replay_int(idx, usecb, confopt);
  2252. }
  2253. return ret;
  2254. }
  2255. /*
  2256. * Helper function to test that a server attempting to read early data can
  2257. * handle a connection from a client where the early data should be skipped.
  2258. * testtype: 0 == No HRR
  2259. * testtype: 1 == HRR
  2260. * testtype: 2 == HRR, invalid early_data sent after HRR
  2261. * testtype: 3 == recv_max_early_data set to 0
  2262. */
  2263. static int early_data_skip_helper(int testtype, int idx)
  2264. {
  2265. SSL_CTX *cctx = NULL, *sctx = NULL;
  2266. SSL *clientssl = NULL, *serverssl = NULL;
  2267. int testresult = 0;
  2268. SSL_SESSION *sess = NULL;
  2269. unsigned char buf[20];
  2270. size_t readbytes, written;
  2271. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2272. &serverssl, &sess, idx)))
  2273. goto end;
  2274. if (testtype == 1 || testtype == 2) {
  2275. /* Force an HRR to occur */
  2276. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  2277. goto end;
  2278. } else if (idx == 2) {
  2279. /*
  2280. * We force early_data rejection by ensuring the PSK identity is
  2281. * unrecognised
  2282. */
  2283. srvid = "Dummy Identity";
  2284. } else {
  2285. /*
  2286. * Deliberately corrupt the creation time. We take 20 seconds off the
  2287. * time. It could be any value as long as it is not within tolerance.
  2288. * This should mean the ticket is rejected.
  2289. */
  2290. if (!TEST_true(SSL_SESSION_set_time(sess, (long)(time(NULL) - 20))))
  2291. goto end;
  2292. }
  2293. if (testtype == 3
  2294. && !TEST_true(SSL_set_recv_max_early_data(serverssl, 0)))
  2295. goto end;
  2296. /* Write some early data */
  2297. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2298. &written))
  2299. || !TEST_size_t_eq(written, strlen(MSG1)))
  2300. goto end;
  2301. /* Server should reject the early data */
  2302. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2303. &readbytes),
  2304. SSL_READ_EARLY_DATA_FINISH)
  2305. || !TEST_size_t_eq(readbytes, 0)
  2306. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2307. SSL_EARLY_DATA_REJECTED))
  2308. goto end;
  2309. switch (testtype) {
  2310. case 0:
  2311. /* Nothing to do */
  2312. break;
  2313. case 1:
  2314. /*
  2315. * Finish off the handshake. We perform the same writes and reads as
  2316. * further down but we expect them to fail due to the incomplete
  2317. * handshake.
  2318. */
  2319. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  2320. || !TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf),
  2321. &readbytes)))
  2322. goto end;
  2323. break;
  2324. case 2:
  2325. {
  2326. BIO *wbio = SSL_get_wbio(clientssl);
  2327. /* A record that will appear as bad early_data */
  2328. const unsigned char bad_early_data[] = {
  2329. 0x17, 0x03, 0x03, 0x00, 0x01, 0x00
  2330. };
  2331. /*
  2332. * We force the client to attempt a write. This will fail because
  2333. * we're still in the handshake. It will cause the second
  2334. * ClientHello to be sent.
  2335. */
  2336. if (!TEST_false(SSL_write_ex(clientssl, MSG2, strlen(MSG2),
  2337. &written)))
  2338. goto end;
  2339. /*
  2340. * Inject some early_data after the second ClientHello. This should
  2341. * cause the server to fail
  2342. */
  2343. if (!TEST_true(BIO_write_ex(wbio, bad_early_data,
  2344. sizeof(bad_early_data), &written)))
  2345. goto end;
  2346. }
  2347. /* fallthrough */
  2348. case 3:
  2349. /*
  2350. * This client has sent more early_data than we are willing to skip
  2351. * (case 3) or sent invalid early_data (case 2) so the connection should
  2352. * abort.
  2353. */
  2354. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2355. || !TEST_int_eq(SSL_get_error(serverssl, 0), SSL_ERROR_SSL))
  2356. goto end;
  2357. /* Connection has failed - nothing more to do */
  2358. testresult = 1;
  2359. goto end;
  2360. default:
  2361. TEST_error("Invalid test type");
  2362. goto end;
  2363. }
  2364. /*
  2365. * Should be able to send normal data despite rejection of early data. The
  2366. * early_data should be skipped.
  2367. */
  2368. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  2369. || !TEST_size_t_eq(written, strlen(MSG2))
  2370. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2371. SSL_EARLY_DATA_REJECTED)
  2372. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2373. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2374. goto end;
  2375. testresult = 1;
  2376. end:
  2377. SSL_SESSION_free(clientpsk);
  2378. SSL_SESSION_free(serverpsk);
  2379. clientpsk = serverpsk = NULL;
  2380. SSL_SESSION_free(sess);
  2381. SSL_free(serverssl);
  2382. SSL_free(clientssl);
  2383. SSL_CTX_free(sctx);
  2384. SSL_CTX_free(cctx);
  2385. return testresult;
  2386. }
  2387. /*
  2388. * Test that a server attempting to read early data can handle a connection
  2389. * from a client where the early data is not acceptable.
  2390. */
  2391. static int test_early_data_skip(int idx)
  2392. {
  2393. return early_data_skip_helper(0, idx);
  2394. }
  2395. /*
  2396. * Test that a server attempting to read early data can handle a connection
  2397. * from a client where an HRR occurs.
  2398. */
  2399. static int test_early_data_skip_hrr(int idx)
  2400. {
  2401. return early_data_skip_helper(1, idx);
  2402. }
  2403. /*
  2404. * Test that a server attempting to read early data can handle a connection
  2405. * from a client where an HRR occurs and correctly fails if early_data is sent
  2406. * after the HRR
  2407. */
  2408. static int test_early_data_skip_hrr_fail(int idx)
  2409. {
  2410. return early_data_skip_helper(2, idx);
  2411. }
  2412. /*
  2413. * Test that a server attempting to read early data will abort if it tries to
  2414. * skip over too much.
  2415. */
  2416. static int test_early_data_skip_abort(int idx)
  2417. {
  2418. return early_data_skip_helper(3, idx);
  2419. }
  2420. /*
  2421. * Test that a server attempting to read early data can handle a connection
  2422. * from a client that doesn't send any.
  2423. */
  2424. static int test_early_data_not_sent(int idx)
  2425. {
  2426. SSL_CTX *cctx = NULL, *sctx = NULL;
  2427. SSL *clientssl = NULL, *serverssl = NULL;
  2428. int testresult = 0;
  2429. SSL_SESSION *sess = NULL;
  2430. unsigned char buf[20];
  2431. size_t readbytes, written;
  2432. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2433. &serverssl, &sess, idx)))
  2434. goto end;
  2435. /* Write some data - should block due to handshake with server */
  2436. SSL_set_connect_state(clientssl);
  2437. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  2438. goto end;
  2439. /* Server should detect that early data has not been sent */
  2440. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2441. &readbytes),
  2442. SSL_READ_EARLY_DATA_FINISH)
  2443. || !TEST_size_t_eq(readbytes, 0)
  2444. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2445. SSL_EARLY_DATA_NOT_SENT)
  2446. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2447. SSL_EARLY_DATA_NOT_SENT))
  2448. goto end;
  2449. /* Continue writing the message we started earlier */
  2450. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  2451. || !TEST_size_t_eq(written, strlen(MSG1))
  2452. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2453. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  2454. || !SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written)
  2455. || !TEST_size_t_eq(written, strlen(MSG2)))
  2456. goto end;
  2457. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  2458. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2459. goto end;
  2460. testresult = 1;
  2461. end:
  2462. SSL_SESSION_free(sess);
  2463. SSL_SESSION_free(clientpsk);
  2464. SSL_SESSION_free(serverpsk);
  2465. clientpsk = serverpsk = NULL;
  2466. SSL_free(serverssl);
  2467. SSL_free(clientssl);
  2468. SSL_CTX_free(sctx);
  2469. SSL_CTX_free(cctx);
  2470. return testresult;
  2471. }
  2472. static const char *servalpn;
  2473. static int alpn_select_cb(SSL *ssl, const unsigned char **out,
  2474. unsigned char *outlen, const unsigned char *in,
  2475. unsigned int inlen, void *arg)
  2476. {
  2477. unsigned int protlen = 0;
  2478. const unsigned char *prot;
  2479. for (prot = in; prot < in + inlen; prot += protlen) {
  2480. protlen = *prot++;
  2481. if (in + inlen < prot + protlen)
  2482. return SSL_TLSEXT_ERR_NOACK;
  2483. if (protlen == strlen(servalpn)
  2484. && memcmp(prot, servalpn, protlen) == 0) {
  2485. *out = prot;
  2486. *outlen = protlen;
  2487. return SSL_TLSEXT_ERR_OK;
  2488. }
  2489. }
  2490. return SSL_TLSEXT_ERR_NOACK;
  2491. }
  2492. /* Test that a PSK can be used to send early_data */
  2493. static int test_early_data_psk(int idx)
  2494. {
  2495. SSL_CTX *cctx = NULL, *sctx = NULL;
  2496. SSL *clientssl = NULL, *serverssl = NULL;
  2497. int testresult = 0;
  2498. SSL_SESSION *sess = NULL;
  2499. unsigned char alpnlist[] = {
  2500. 0x08, 'g', 'o', 'o', 'd', 'a', 'l', 'p', 'n', 0x07, 'b', 'a', 'd', 'a',
  2501. 'l', 'p', 'n'
  2502. };
  2503. #define GOODALPNLEN 9
  2504. #define BADALPNLEN 8
  2505. #define GOODALPN (alpnlist)
  2506. #define BADALPN (alpnlist + GOODALPNLEN)
  2507. int err = 0;
  2508. unsigned char buf[20];
  2509. size_t readbytes, written;
  2510. int readearlyres = SSL_READ_EARLY_DATA_SUCCESS, connectres = 1;
  2511. int edstatus = SSL_EARLY_DATA_ACCEPTED;
  2512. /* We always set this up with a final parameter of "2" for PSK */
  2513. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2514. &serverssl, &sess, 2)))
  2515. goto end;
  2516. servalpn = "goodalpn";
  2517. /*
  2518. * Note: There is no test for inconsistent SNI with late client detection.
  2519. * This is because servers do not acknowledge SNI even if they are using
  2520. * it in a resumption handshake - so it is not actually possible for a
  2521. * client to detect a problem.
  2522. */
  2523. switch (idx) {
  2524. case 0:
  2525. /* Set inconsistent SNI (early client detection) */
  2526. err = SSL_R_INCONSISTENT_EARLY_DATA_SNI;
  2527. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  2528. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "badhost")))
  2529. goto end;
  2530. break;
  2531. case 1:
  2532. /* Set inconsistent ALPN (early client detection) */
  2533. err = SSL_R_INCONSISTENT_EARLY_DATA_ALPN;
  2534. /* SSL_set_alpn_protos returns 0 for success and 1 for failure */
  2535. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN,
  2536. GOODALPNLEN))
  2537. || !TEST_false(SSL_set_alpn_protos(clientssl, BADALPN,
  2538. BADALPNLEN)))
  2539. goto end;
  2540. break;
  2541. case 2:
  2542. /*
  2543. * Set invalid protocol version. Technically this affects PSKs without
  2544. * early_data too, but we test it here because it is similar to the
  2545. * SNI/ALPN consistency tests.
  2546. */
  2547. err = SSL_R_BAD_PSK;
  2548. if (!TEST_true(SSL_SESSION_set_protocol_version(sess, TLS1_2_VERSION)))
  2549. goto end;
  2550. break;
  2551. case 3:
  2552. /*
  2553. * Set inconsistent SNI (server side). In this case the connection
  2554. * will succeed and accept early_data. In TLSv1.3 on the server side SNI
  2555. * is associated with each handshake - not the session. Therefore it
  2556. * should not matter that we used a different server name last time.
  2557. */
  2558. SSL_SESSION_free(serverpsk);
  2559. serverpsk = SSL_SESSION_dup(clientpsk);
  2560. if (!TEST_ptr(serverpsk)
  2561. || !TEST_true(SSL_SESSION_set1_hostname(serverpsk, "badhost")))
  2562. goto end;
  2563. /* Fall through */
  2564. case 4:
  2565. /* Set consistent SNI */
  2566. if (!TEST_true(SSL_SESSION_set1_hostname(sess, "goodhost"))
  2567. || !TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost"))
  2568. || !TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  2569. hostname_cb)))
  2570. goto end;
  2571. break;
  2572. case 5:
  2573. /*
  2574. * Set inconsistent ALPN (server detected). In this case the connection
  2575. * will succeed but reject early_data.
  2576. */
  2577. servalpn = "badalpn";
  2578. edstatus = SSL_EARLY_DATA_REJECTED;
  2579. readearlyres = SSL_READ_EARLY_DATA_FINISH;
  2580. /* Fall through */
  2581. case 6:
  2582. /*
  2583. * Set consistent ALPN.
  2584. * SSL_set_alpn_protos returns 0 for success and 1 for failure. It
  2585. * accepts a list of protos (each one length prefixed).
  2586. * SSL_set1_alpn_selected accepts a single protocol (not length
  2587. * prefixed)
  2588. */
  2589. if (!TEST_true(SSL_SESSION_set1_alpn_selected(sess, GOODALPN + 1,
  2590. GOODALPNLEN - 1))
  2591. || !TEST_false(SSL_set_alpn_protos(clientssl, GOODALPN,
  2592. GOODALPNLEN)))
  2593. goto end;
  2594. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  2595. break;
  2596. case 7:
  2597. /* Set inconsistent ALPN (late client detection) */
  2598. SSL_SESSION_free(serverpsk);
  2599. serverpsk = SSL_SESSION_dup(clientpsk);
  2600. if (!TEST_ptr(serverpsk)
  2601. || !TEST_true(SSL_SESSION_set1_alpn_selected(clientpsk,
  2602. BADALPN + 1,
  2603. BADALPNLEN - 1))
  2604. || !TEST_true(SSL_SESSION_set1_alpn_selected(serverpsk,
  2605. GOODALPN + 1,
  2606. GOODALPNLEN - 1))
  2607. || !TEST_false(SSL_set_alpn_protos(clientssl, alpnlist,
  2608. sizeof(alpnlist))))
  2609. goto end;
  2610. SSL_CTX_set_alpn_select_cb(sctx, alpn_select_cb, NULL);
  2611. edstatus = SSL_EARLY_DATA_ACCEPTED;
  2612. readearlyres = SSL_READ_EARLY_DATA_SUCCESS;
  2613. /* SSL_connect() call should fail */
  2614. connectres = -1;
  2615. break;
  2616. default:
  2617. TEST_error("Bad test index");
  2618. goto end;
  2619. }
  2620. SSL_set_connect_state(clientssl);
  2621. if (err != 0) {
  2622. if (!TEST_false(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2623. &written))
  2624. || !TEST_int_eq(SSL_get_error(clientssl, 0), SSL_ERROR_SSL)
  2625. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()), err))
  2626. goto end;
  2627. } else {
  2628. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2629. &written)))
  2630. goto end;
  2631. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2632. &readbytes), readearlyres)
  2633. || (readearlyres == SSL_READ_EARLY_DATA_SUCCESS
  2634. && !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1)))
  2635. || !TEST_int_eq(SSL_get_early_data_status(serverssl), edstatus)
  2636. || !TEST_int_eq(SSL_connect(clientssl), connectres))
  2637. goto end;
  2638. }
  2639. testresult = 1;
  2640. end:
  2641. SSL_SESSION_free(sess);
  2642. SSL_SESSION_free(clientpsk);
  2643. SSL_SESSION_free(serverpsk);
  2644. clientpsk = serverpsk = NULL;
  2645. SSL_free(serverssl);
  2646. SSL_free(clientssl);
  2647. SSL_CTX_free(sctx);
  2648. SSL_CTX_free(cctx);
  2649. return testresult;
  2650. }
  2651. /*
  2652. * Test TLSv1.3 PSK can be used to send early_data with all 5 ciphersuites
  2653. * idx == 0: Test with TLS1_3_RFC_AES_128_GCM_SHA256
  2654. * idx == 1: Test with TLS1_3_RFC_AES_256_GCM_SHA384
  2655. * idx == 2: Test with TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  2656. * idx == 3: Test with TLS1_3_RFC_AES_128_CCM_SHA256
  2657. * idx == 4: Test with TLS1_3_RFC_AES_128_CCM_8_SHA256
  2658. */
  2659. static int test_early_data_psk_with_all_ciphers(int idx)
  2660. {
  2661. SSL_CTX *cctx = NULL, *sctx = NULL;
  2662. SSL *clientssl = NULL, *serverssl = NULL;
  2663. int testresult = 0;
  2664. SSL_SESSION *sess = NULL;
  2665. unsigned char buf[20];
  2666. size_t readbytes, written;
  2667. const SSL_CIPHER *cipher;
  2668. const char *cipher_str[] = {
  2669. TLS1_3_RFC_AES_128_GCM_SHA256,
  2670. TLS1_3_RFC_AES_256_GCM_SHA384,
  2671. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2672. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  2673. # else
  2674. NULL,
  2675. # endif
  2676. TLS1_3_RFC_AES_128_CCM_SHA256,
  2677. TLS1_3_RFC_AES_128_CCM_8_SHA256
  2678. };
  2679. const unsigned char *cipher_bytes[] = {
  2680. TLS13_AES_128_GCM_SHA256_BYTES,
  2681. TLS13_AES_256_GCM_SHA384_BYTES,
  2682. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2683. TLS13_CHACHA20_POLY1305_SHA256_BYTES,
  2684. # else
  2685. NULL,
  2686. # endif
  2687. TLS13_AES_128_CCM_SHA256_BYTES,
  2688. TLS13_AES_128_CCM_8_SHA256_BYTES
  2689. };
  2690. if (cipher_str[idx] == NULL)
  2691. return 1;
  2692. /* We always set this up with a final parameter of "2" for PSK */
  2693. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2694. &serverssl, &sess, 2)))
  2695. goto end;
  2696. if (!TEST_true(SSL_set_ciphersuites(clientssl, cipher_str[idx]))
  2697. || !TEST_true(SSL_set_ciphersuites(serverssl, cipher_str[idx])))
  2698. goto end;
  2699. /*
  2700. * 'setupearly_data_test' creates only one instance of SSL_SESSION
  2701. * and assigns to both client and server with incremented reference
  2702. * and the same instance is updated in 'sess'.
  2703. * So updating ciphersuite in 'sess' which will get reflected in
  2704. * PSK handshake using psk use sess and find sess cb.
  2705. */
  2706. cipher = SSL_CIPHER_find(clientssl, cipher_bytes[idx]);
  2707. if (!TEST_ptr(cipher) || !TEST_true(SSL_SESSION_set_cipher(sess, cipher)))
  2708. goto end;
  2709. SSL_set_connect_state(clientssl);
  2710. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2711. &written)))
  2712. goto end;
  2713. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2714. &readbytes),
  2715. SSL_READ_EARLY_DATA_SUCCESS)
  2716. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  2717. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2718. SSL_EARLY_DATA_ACCEPTED)
  2719. || !TEST_int_eq(SSL_connect(clientssl), 1)
  2720. || !TEST_int_eq(SSL_accept(serverssl), 1))
  2721. goto end;
  2722. /* Send some normal data from client to server */
  2723. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  2724. || !TEST_size_t_eq(written, strlen(MSG2)))
  2725. goto end;
  2726. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2727. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2728. goto end;
  2729. testresult = 1;
  2730. end:
  2731. SSL_SESSION_free(sess);
  2732. SSL_SESSION_free(clientpsk);
  2733. SSL_SESSION_free(serverpsk);
  2734. clientpsk = serverpsk = NULL;
  2735. if (clientssl != NULL)
  2736. SSL_shutdown(clientssl);
  2737. if (serverssl != NULL)
  2738. SSL_shutdown(serverssl);
  2739. SSL_free(serverssl);
  2740. SSL_free(clientssl);
  2741. SSL_CTX_free(sctx);
  2742. SSL_CTX_free(cctx);
  2743. return testresult;
  2744. }
  2745. /*
  2746. * Test that a server that doesn't try to read early data can handle a
  2747. * client sending some.
  2748. */
  2749. static int test_early_data_not_expected(int idx)
  2750. {
  2751. SSL_CTX *cctx = NULL, *sctx = NULL;
  2752. SSL *clientssl = NULL, *serverssl = NULL;
  2753. int testresult = 0;
  2754. SSL_SESSION *sess = NULL;
  2755. unsigned char buf[20];
  2756. size_t readbytes, written;
  2757. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2758. &serverssl, &sess, idx)))
  2759. goto end;
  2760. /* Write some early data */
  2761. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  2762. &written)))
  2763. goto end;
  2764. /*
  2765. * Server should skip over early data and then block waiting for client to
  2766. * continue handshake
  2767. */
  2768. if (!TEST_int_le(SSL_accept(serverssl), 0)
  2769. || !TEST_int_gt(SSL_connect(clientssl), 0)
  2770. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2771. SSL_EARLY_DATA_REJECTED)
  2772. || !TEST_int_gt(SSL_accept(serverssl), 0)
  2773. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2774. SSL_EARLY_DATA_REJECTED))
  2775. goto end;
  2776. /* Send some normal data from client to server */
  2777. if (!TEST_true(SSL_write_ex(clientssl, MSG2, strlen(MSG2), &written))
  2778. || !TEST_size_t_eq(written, strlen(MSG2)))
  2779. goto end;
  2780. if (!TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2781. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2782. goto end;
  2783. testresult = 1;
  2784. end:
  2785. SSL_SESSION_free(sess);
  2786. SSL_SESSION_free(clientpsk);
  2787. SSL_SESSION_free(serverpsk);
  2788. clientpsk = serverpsk = NULL;
  2789. SSL_free(serverssl);
  2790. SSL_free(clientssl);
  2791. SSL_CTX_free(sctx);
  2792. SSL_CTX_free(cctx);
  2793. return testresult;
  2794. }
  2795. # ifndef OPENSSL_NO_TLS1_2
  2796. /*
  2797. * Test that a server attempting to read early data can handle a connection
  2798. * from a TLSv1.2 client.
  2799. */
  2800. static int test_early_data_tls1_2(int idx)
  2801. {
  2802. SSL_CTX *cctx = NULL, *sctx = NULL;
  2803. SSL *clientssl = NULL, *serverssl = NULL;
  2804. int testresult = 0;
  2805. unsigned char buf[20];
  2806. size_t readbytes, written;
  2807. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  2808. &serverssl, NULL, idx)))
  2809. goto end;
  2810. /* Write some data - should block due to handshake with server */
  2811. SSL_set_max_proto_version(clientssl, TLS1_2_VERSION);
  2812. SSL_set_connect_state(clientssl);
  2813. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written)))
  2814. goto end;
  2815. /*
  2816. * Server should do TLSv1.2 handshake. First it will block waiting for more
  2817. * messages from client after ServerDone. Then SSL_read_early_data should
  2818. * finish and detect that early data has not been sent
  2819. */
  2820. if (!TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2821. &readbytes),
  2822. SSL_READ_EARLY_DATA_ERROR))
  2823. goto end;
  2824. /*
  2825. * Continue writing the message we started earlier. Will still block waiting
  2826. * for the CCS/Finished from server
  2827. */
  2828. if (!TEST_false(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  2829. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  2830. &readbytes),
  2831. SSL_READ_EARLY_DATA_FINISH)
  2832. || !TEST_size_t_eq(readbytes, 0)
  2833. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  2834. SSL_EARLY_DATA_NOT_SENT))
  2835. goto end;
  2836. /* Continue writing the message we started earlier */
  2837. if (!TEST_true(SSL_write_ex(clientssl, MSG1, strlen(MSG1), &written))
  2838. || !TEST_size_t_eq(written, strlen(MSG1))
  2839. || !TEST_int_eq(SSL_get_early_data_status(clientssl),
  2840. SSL_EARLY_DATA_NOT_SENT)
  2841. || !TEST_true(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  2842. || !TEST_mem_eq(buf, readbytes, MSG1, strlen(MSG1))
  2843. || !TEST_true(SSL_write_ex(serverssl, MSG2, strlen(MSG2), &written))
  2844. || !TEST_size_t_eq(written, strlen(MSG2))
  2845. || !SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes)
  2846. || !TEST_mem_eq(buf, readbytes, MSG2, strlen(MSG2)))
  2847. goto end;
  2848. testresult = 1;
  2849. end:
  2850. SSL_SESSION_free(clientpsk);
  2851. SSL_SESSION_free(serverpsk);
  2852. clientpsk = serverpsk = NULL;
  2853. SSL_free(serverssl);
  2854. SSL_free(clientssl);
  2855. SSL_CTX_free(sctx);
  2856. SSL_CTX_free(cctx);
  2857. return testresult;
  2858. }
  2859. # endif /* OPENSSL_NO_TLS1_2 */
  2860. /*
  2861. * Test configuring the TLSv1.3 ciphersuites
  2862. *
  2863. * Test 0: Set a default ciphersuite in the SSL_CTX (no explicit cipher_list)
  2864. * Test 1: Set a non-default ciphersuite in the SSL_CTX (no explicit cipher_list)
  2865. * Test 2: Set a default ciphersuite in the SSL (no explicit cipher_list)
  2866. * Test 3: Set a non-default ciphersuite in the SSL (no explicit cipher_list)
  2867. * Test 4: Set a default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  2868. * Test 5: Set a non-default ciphersuite in the SSL_CTX (SSL_CTX cipher_list)
  2869. * Test 6: Set a default ciphersuite in the SSL (SSL_CTX cipher_list)
  2870. * Test 7: Set a non-default ciphersuite in the SSL (SSL_CTX cipher_list)
  2871. * Test 8: Set a default ciphersuite in the SSL (SSL cipher_list)
  2872. * Test 9: Set a non-default ciphersuite in the SSL (SSL cipher_list)
  2873. */
  2874. static int test_set_ciphersuite(int idx)
  2875. {
  2876. SSL_CTX *cctx = NULL, *sctx = NULL;
  2877. SSL *clientssl = NULL, *serverssl = NULL;
  2878. int testresult = 0;
  2879. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  2880. TLS1_VERSION, TLS_MAX_VERSION,
  2881. &sctx, &cctx, cert, privkey))
  2882. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  2883. "TLS_AES_128_GCM_SHA256:TLS_AES_128_CCM_SHA256")))
  2884. goto end;
  2885. if (idx >=4 && idx <= 7) {
  2886. /* SSL_CTX explicit cipher list */
  2887. if (!TEST_true(SSL_CTX_set_cipher_list(cctx, "AES256-GCM-SHA384")))
  2888. goto end;
  2889. }
  2890. if (idx == 0 || idx == 4) {
  2891. /* Default ciphersuite */
  2892. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  2893. "TLS_AES_128_GCM_SHA256")))
  2894. goto end;
  2895. } else if (idx == 1 || idx == 5) {
  2896. /* Non default ciphersuite */
  2897. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  2898. "TLS_AES_128_CCM_SHA256")))
  2899. goto end;
  2900. }
  2901. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2902. &clientssl, NULL, NULL)))
  2903. goto end;
  2904. if (idx == 8 || idx == 9) {
  2905. /* SSL explicit cipher list */
  2906. if (!TEST_true(SSL_set_cipher_list(clientssl, "AES256-GCM-SHA384")))
  2907. goto end;
  2908. }
  2909. if (idx == 2 || idx == 6 || idx == 8) {
  2910. /* Default ciphersuite */
  2911. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  2912. "TLS_AES_128_GCM_SHA256")))
  2913. goto end;
  2914. } else if (idx == 3 || idx == 7 || idx == 9) {
  2915. /* Non default ciphersuite */
  2916. if (!TEST_true(SSL_set_ciphersuites(clientssl,
  2917. "TLS_AES_128_CCM_SHA256")))
  2918. goto end;
  2919. }
  2920. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  2921. goto end;
  2922. testresult = 1;
  2923. end:
  2924. SSL_free(serverssl);
  2925. SSL_free(clientssl);
  2926. SSL_CTX_free(sctx);
  2927. SSL_CTX_free(cctx);
  2928. return testresult;
  2929. }
  2930. static int test_ciphersuite_change(void)
  2931. {
  2932. SSL_CTX *cctx = NULL, *sctx = NULL;
  2933. SSL *clientssl = NULL, *serverssl = NULL;
  2934. SSL_SESSION *clntsess = NULL;
  2935. int testresult = 0;
  2936. const SSL_CIPHER *aes_128_gcm_sha256 = NULL;
  2937. /* Create a session based on SHA-256 */
  2938. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  2939. TLS1_VERSION, TLS_MAX_VERSION,
  2940. &sctx, &cctx, cert, privkey))
  2941. || !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  2942. "TLS_AES_128_GCM_SHA256"))
  2943. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2944. &clientssl, NULL, NULL))
  2945. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  2946. SSL_ERROR_NONE)))
  2947. goto end;
  2948. clntsess = SSL_get1_session(clientssl);
  2949. /* Save for later */
  2950. aes_128_gcm_sha256 = SSL_SESSION_get0_cipher(clntsess);
  2951. SSL_shutdown(clientssl);
  2952. SSL_shutdown(serverssl);
  2953. SSL_free(serverssl);
  2954. SSL_free(clientssl);
  2955. serverssl = clientssl = NULL;
  2956. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  2957. /* Check we can resume a session with a different SHA-256 ciphersuite */
  2958. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  2959. "TLS_CHACHA20_POLY1305_SHA256"))
  2960. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2961. NULL, NULL))
  2962. || !TEST_true(SSL_set_session(clientssl, clntsess))
  2963. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  2964. SSL_ERROR_NONE))
  2965. || !TEST_true(SSL_session_reused(clientssl)))
  2966. goto end;
  2967. SSL_SESSION_free(clntsess);
  2968. clntsess = SSL_get1_session(clientssl);
  2969. SSL_shutdown(clientssl);
  2970. SSL_shutdown(serverssl);
  2971. SSL_free(serverssl);
  2972. SSL_free(clientssl);
  2973. serverssl = clientssl = NULL;
  2974. # endif
  2975. /*
  2976. * Check attempting to resume a SHA-256 session with no SHA-256 ciphersuites
  2977. * succeeds but does not resume.
  2978. */
  2979. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  2980. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  2981. NULL, NULL))
  2982. || !TEST_true(SSL_set_session(clientssl, clntsess))
  2983. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  2984. SSL_ERROR_SSL))
  2985. || !TEST_false(SSL_session_reused(clientssl)))
  2986. goto end;
  2987. SSL_SESSION_free(clntsess);
  2988. clntsess = NULL;
  2989. SSL_shutdown(clientssl);
  2990. SSL_shutdown(serverssl);
  2991. SSL_free(serverssl);
  2992. SSL_free(clientssl);
  2993. serverssl = clientssl = NULL;
  2994. /* Create a session based on SHA384 */
  2995. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx, "TLS_AES_256_GCM_SHA384"))
  2996. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  2997. &clientssl, NULL, NULL))
  2998. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  2999. SSL_ERROR_NONE)))
  3000. goto end;
  3001. clntsess = SSL_get1_session(clientssl);
  3002. SSL_shutdown(clientssl);
  3003. SSL_shutdown(serverssl);
  3004. SSL_free(serverssl);
  3005. SSL_free(clientssl);
  3006. serverssl = clientssl = NULL;
  3007. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3008. "TLS_AES_128_GCM_SHA256:TLS_AES_256_GCM_SHA384"))
  3009. || !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  3010. "TLS_AES_256_GCM_SHA384"))
  3011. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3012. NULL, NULL))
  3013. || !TEST_true(SSL_set_session(clientssl, clntsess))
  3014. /*
  3015. * We use SSL_ERROR_WANT_READ below so that we can pause the
  3016. * connection after the initial ClientHello has been sent to
  3017. * enable us to make some session changes.
  3018. */
  3019. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3020. SSL_ERROR_WANT_READ)))
  3021. goto end;
  3022. /* Trick the client into thinking this session is for a different digest */
  3023. clntsess->cipher = aes_128_gcm_sha256;
  3024. clntsess->cipher_id = clntsess->cipher->id;
  3025. /*
  3026. * Continue the previously started connection. Server has selected a SHA-384
  3027. * ciphersuite, but client thinks the session is for SHA-256, so it should
  3028. * bail out.
  3029. */
  3030. if (!TEST_false(create_ssl_connection(serverssl, clientssl,
  3031. SSL_ERROR_SSL))
  3032. || !TEST_int_eq(ERR_GET_REASON(ERR_get_error()),
  3033. SSL_R_CIPHERSUITE_DIGEST_HAS_CHANGED))
  3034. goto end;
  3035. testresult = 1;
  3036. end:
  3037. SSL_SESSION_free(clntsess);
  3038. SSL_free(serverssl);
  3039. SSL_free(clientssl);
  3040. SSL_CTX_free(sctx);
  3041. SSL_CTX_free(cctx);
  3042. return testresult;
  3043. }
  3044. /*
  3045. * Test TLSv1.3 Cipher Suite
  3046. * Test 0 = Set TLS1.3 cipher on context
  3047. * Test 1 = Set TLS1.3 cipher on SSL
  3048. * Test 2 = Set TLS1.3 and TLS1.2 cipher on context
  3049. * Test 3 = Set TLS1.3 and TLS1.2 cipher on SSL
  3050. */
  3051. static int test_tls13_ciphersuite(int idx)
  3052. {
  3053. SSL_CTX *sctx = NULL, *cctx = NULL;
  3054. SSL *serverssl = NULL, *clientssl = NULL;
  3055. static const char *t13_ciphers[] = {
  3056. TLS1_3_RFC_AES_128_GCM_SHA256,
  3057. TLS1_3_RFC_AES_256_GCM_SHA384,
  3058. TLS1_3_RFC_AES_128_CCM_SHA256,
  3059. # if !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  3060. TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3061. TLS1_3_RFC_AES_256_GCM_SHA384 ":" TLS1_3_RFC_CHACHA20_POLY1305_SHA256,
  3062. # endif
  3063. TLS1_3_RFC_AES_128_CCM_8_SHA256 ":" TLS1_3_RFC_AES_128_CCM_SHA256
  3064. };
  3065. const char *t13_cipher = NULL;
  3066. const char *t12_cipher = NULL;
  3067. const char *negotiated_scipher;
  3068. const char *negotiated_ccipher;
  3069. int set_at_ctx = 0;
  3070. int set_at_ssl = 0;
  3071. int testresult = 0;
  3072. int max_ver;
  3073. size_t i;
  3074. switch (idx) {
  3075. case 0:
  3076. set_at_ctx = 1;
  3077. break;
  3078. case 1:
  3079. set_at_ssl = 1;
  3080. break;
  3081. case 2:
  3082. set_at_ctx = 1;
  3083. t12_cipher = TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
  3084. break;
  3085. case 3:
  3086. set_at_ssl = 1;
  3087. t12_cipher = TLS1_TXT_ECDHE_RSA_WITH_AES_128_GCM_SHA256;
  3088. break;
  3089. }
  3090. for (max_ver = TLS1_2_VERSION; max_ver <= TLS1_3_VERSION; max_ver++) {
  3091. # ifdef OPENSSL_NO_TLS1_2
  3092. if (max_ver == TLS1_2_VERSION)
  3093. continue;
  3094. # endif
  3095. for (i = 0; i < OSSL_NELEM(t13_ciphers); i++) {
  3096. t13_cipher = t13_ciphers[i];
  3097. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  3098. TLS_client_method(),
  3099. TLS1_VERSION, max_ver,
  3100. &sctx, &cctx, cert, privkey)))
  3101. goto end;
  3102. if (set_at_ctx) {
  3103. if (!TEST_true(SSL_CTX_set_ciphersuites(sctx, t13_cipher))
  3104. || !TEST_true(SSL_CTX_set_ciphersuites(cctx, t13_cipher)))
  3105. goto end;
  3106. if (t12_cipher != NULL) {
  3107. if (!TEST_true(SSL_CTX_set_cipher_list(sctx, t12_cipher))
  3108. || !TEST_true(SSL_CTX_set_cipher_list(cctx,
  3109. t12_cipher)))
  3110. goto end;
  3111. }
  3112. }
  3113. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3114. &clientssl, NULL, NULL)))
  3115. goto end;
  3116. if (set_at_ssl) {
  3117. if (!TEST_true(SSL_set_ciphersuites(serverssl, t13_cipher))
  3118. || !TEST_true(SSL_set_ciphersuites(clientssl, t13_cipher)))
  3119. goto end;
  3120. if (t12_cipher != NULL) {
  3121. if (!TEST_true(SSL_set_cipher_list(serverssl, t12_cipher))
  3122. || !TEST_true(SSL_set_cipher_list(clientssl,
  3123. t12_cipher)))
  3124. goto end;
  3125. }
  3126. }
  3127. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  3128. SSL_ERROR_NONE)))
  3129. goto end;
  3130. negotiated_scipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  3131. serverssl));
  3132. negotiated_ccipher = SSL_CIPHER_get_name(SSL_get_current_cipher(
  3133. clientssl));
  3134. if (!TEST_str_eq(negotiated_scipher, negotiated_ccipher))
  3135. goto end;
  3136. /*
  3137. * TEST_strn_eq is used below because t13_cipher can contain
  3138. * multiple ciphersuites
  3139. */
  3140. if (max_ver == TLS1_3_VERSION
  3141. && !TEST_strn_eq(t13_cipher, negotiated_scipher,
  3142. strlen(negotiated_scipher)))
  3143. goto end;
  3144. # ifndef OPENSSL_NO_TLS1_2
  3145. /* Below validation is not done when t12_cipher is NULL */
  3146. if (max_ver == TLS1_2_VERSION && t12_cipher != NULL
  3147. && !TEST_str_eq(t12_cipher, negotiated_scipher))
  3148. goto end;
  3149. # endif
  3150. SSL_free(serverssl);
  3151. serverssl = NULL;
  3152. SSL_free(clientssl);
  3153. clientssl = NULL;
  3154. SSL_CTX_free(sctx);
  3155. sctx = NULL;
  3156. SSL_CTX_free(cctx);
  3157. cctx = NULL;
  3158. }
  3159. }
  3160. testresult = 1;
  3161. end:
  3162. SSL_free(serverssl);
  3163. SSL_free(clientssl);
  3164. SSL_CTX_free(sctx);
  3165. SSL_CTX_free(cctx);
  3166. return testresult;
  3167. }
  3168. /*
  3169. * Test TLSv1.3 PSKs
  3170. * Test 0 = Test new style callbacks
  3171. * Test 1 = Test both new and old style callbacks
  3172. * Test 2 = Test old style callbacks
  3173. * Test 3 = Test old style callbacks with no certificate
  3174. */
  3175. static int test_tls13_psk(int idx)
  3176. {
  3177. SSL_CTX *sctx = NULL, *cctx = NULL;
  3178. SSL *serverssl = NULL, *clientssl = NULL;
  3179. const SSL_CIPHER *cipher = NULL;
  3180. const unsigned char key[] = {
  3181. 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
  3182. 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
  3183. 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
  3184. 0x24, 0x25, 0x26, 0x27, 0x28, 0x29, 0x2a, 0x2b, 0x2c, 0x2d, 0x2e, 0x2f
  3185. };
  3186. int testresult = 0;
  3187. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  3188. TLS1_VERSION, TLS_MAX_VERSION,
  3189. &sctx, &cctx, idx == 3 ? NULL : cert,
  3190. idx == 3 ? NULL : privkey)))
  3191. goto end;
  3192. if (idx != 3) {
  3193. /*
  3194. * We use a ciphersuite with SHA256 to ease testing old style PSK
  3195. * callbacks which will always default to SHA256. This should not be
  3196. * necessary if we have no cert/priv key. In that case the server should
  3197. * prefer SHA256 automatically.
  3198. */
  3199. if (!TEST_true(SSL_CTX_set_ciphersuites(cctx,
  3200. "TLS_AES_128_GCM_SHA256")))
  3201. goto end;
  3202. }
  3203. /*
  3204. * Test 0: New style callbacks only
  3205. * Test 1: New and old style callbacks (only the new ones should be used)
  3206. * Test 2: Old style callbacks only
  3207. */
  3208. if (idx == 0 || idx == 1) {
  3209. SSL_CTX_set_psk_use_session_callback(cctx, use_session_cb);
  3210. SSL_CTX_set_psk_find_session_callback(sctx, find_session_cb);
  3211. }
  3212. #ifndef OPENSSL_NO_PSK
  3213. if (idx >= 1) {
  3214. SSL_CTX_set_psk_client_callback(cctx, psk_client_cb);
  3215. SSL_CTX_set_psk_server_callback(sctx, psk_server_cb);
  3216. }
  3217. #endif
  3218. srvid = pskid;
  3219. use_session_cb_cnt = 0;
  3220. find_session_cb_cnt = 0;
  3221. psk_client_cb_cnt = 0;
  3222. psk_server_cb_cnt = 0;
  3223. if (idx != 3) {
  3224. /*
  3225. * Check we can create a connection if callback decides not to send a
  3226. * PSK
  3227. */
  3228. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3229. NULL, NULL))
  3230. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3231. SSL_ERROR_NONE))
  3232. || !TEST_false(SSL_session_reused(clientssl))
  3233. || !TEST_false(SSL_session_reused(serverssl)))
  3234. goto end;
  3235. if (idx == 0 || idx == 1) {
  3236. if (!TEST_true(use_session_cb_cnt == 1)
  3237. || !TEST_true(find_session_cb_cnt == 0)
  3238. /*
  3239. * If no old style callback then below should be 0
  3240. * otherwise 1
  3241. */
  3242. || !TEST_true(psk_client_cb_cnt == idx)
  3243. || !TEST_true(psk_server_cb_cnt == 0))
  3244. goto end;
  3245. } else {
  3246. if (!TEST_true(use_session_cb_cnt == 0)
  3247. || !TEST_true(find_session_cb_cnt == 0)
  3248. || !TEST_true(psk_client_cb_cnt == 1)
  3249. || !TEST_true(psk_server_cb_cnt == 0))
  3250. goto end;
  3251. }
  3252. shutdown_ssl_connection(serverssl, clientssl);
  3253. serverssl = clientssl = NULL;
  3254. use_session_cb_cnt = psk_client_cb_cnt = 0;
  3255. }
  3256. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3257. NULL, NULL)))
  3258. goto end;
  3259. /* Create the PSK */
  3260. cipher = SSL_CIPHER_find(clientssl, TLS13_AES_128_GCM_SHA256_BYTES);
  3261. clientpsk = SSL_SESSION_new();
  3262. if (!TEST_ptr(clientpsk)
  3263. || !TEST_ptr(cipher)
  3264. || !TEST_true(SSL_SESSION_set1_master_key(clientpsk, key,
  3265. sizeof(key)))
  3266. || !TEST_true(SSL_SESSION_set_cipher(clientpsk, cipher))
  3267. || !TEST_true(SSL_SESSION_set_protocol_version(clientpsk,
  3268. TLS1_3_VERSION))
  3269. || !TEST_true(SSL_SESSION_up_ref(clientpsk)))
  3270. goto end;
  3271. serverpsk = clientpsk;
  3272. /* Check we can create a connection and the PSK is used */
  3273. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3274. || !TEST_true(SSL_session_reused(clientssl))
  3275. || !TEST_true(SSL_session_reused(serverssl)))
  3276. goto end;
  3277. if (idx == 0 || idx == 1) {
  3278. if (!TEST_true(use_session_cb_cnt == 1)
  3279. || !TEST_true(find_session_cb_cnt == 1)
  3280. || !TEST_true(psk_client_cb_cnt == 0)
  3281. || !TEST_true(psk_server_cb_cnt == 0))
  3282. goto end;
  3283. } else {
  3284. if (!TEST_true(use_session_cb_cnt == 0)
  3285. || !TEST_true(find_session_cb_cnt == 0)
  3286. || !TEST_true(psk_client_cb_cnt == 1)
  3287. || !TEST_true(psk_server_cb_cnt == 1))
  3288. goto end;
  3289. }
  3290. shutdown_ssl_connection(serverssl, clientssl);
  3291. serverssl = clientssl = NULL;
  3292. use_session_cb_cnt = find_session_cb_cnt = 0;
  3293. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  3294. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3295. NULL, NULL)))
  3296. goto end;
  3297. /* Force an HRR */
  3298. if (!TEST_true(SSL_set1_groups_list(serverssl, "P-256")))
  3299. goto end;
  3300. /*
  3301. * Check we can create a connection, the PSK is used and the callbacks are
  3302. * called twice.
  3303. */
  3304. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE))
  3305. || !TEST_true(SSL_session_reused(clientssl))
  3306. || !TEST_true(SSL_session_reused(serverssl)))
  3307. goto end;
  3308. if (idx == 0 || idx == 1) {
  3309. if (!TEST_true(use_session_cb_cnt == 2)
  3310. || !TEST_true(find_session_cb_cnt == 2)
  3311. || !TEST_true(psk_client_cb_cnt == 0)
  3312. || !TEST_true(psk_server_cb_cnt == 0))
  3313. goto end;
  3314. } else {
  3315. if (!TEST_true(use_session_cb_cnt == 0)
  3316. || !TEST_true(find_session_cb_cnt == 0)
  3317. || !TEST_true(psk_client_cb_cnt == 2)
  3318. || !TEST_true(psk_server_cb_cnt == 2))
  3319. goto end;
  3320. }
  3321. shutdown_ssl_connection(serverssl, clientssl);
  3322. serverssl = clientssl = NULL;
  3323. use_session_cb_cnt = find_session_cb_cnt = 0;
  3324. psk_client_cb_cnt = psk_server_cb_cnt = 0;
  3325. if (idx != 3) {
  3326. /*
  3327. * Check that if the server rejects the PSK we can still connect, but with
  3328. * a full handshake
  3329. */
  3330. srvid = "Dummy Identity";
  3331. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3332. NULL, NULL))
  3333. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3334. SSL_ERROR_NONE))
  3335. || !TEST_false(SSL_session_reused(clientssl))
  3336. || !TEST_false(SSL_session_reused(serverssl)))
  3337. goto end;
  3338. if (idx == 0 || idx == 1) {
  3339. if (!TEST_true(use_session_cb_cnt == 1)
  3340. || !TEST_true(find_session_cb_cnt == 1)
  3341. || !TEST_true(psk_client_cb_cnt == 0)
  3342. /*
  3343. * If no old style callback then below should be 0
  3344. * otherwise 1
  3345. */
  3346. || !TEST_true(psk_server_cb_cnt == idx))
  3347. goto end;
  3348. } else {
  3349. if (!TEST_true(use_session_cb_cnt == 0)
  3350. || !TEST_true(find_session_cb_cnt == 0)
  3351. || !TEST_true(psk_client_cb_cnt == 1)
  3352. || !TEST_true(psk_server_cb_cnt == 1))
  3353. goto end;
  3354. }
  3355. shutdown_ssl_connection(serverssl, clientssl);
  3356. serverssl = clientssl = NULL;
  3357. }
  3358. testresult = 1;
  3359. end:
  3360. SSL_SESSION_free(clientpsk);
  3361. SSL_SESSION_free(serverpsk);
  3362. clientpsk = serverpsk = NULL;
  3363. SSL_free(serverssl);
  3364. SSL_free(clientssl);
  3365. SSL_CTX_free(sctx);
  3366. SSL_CTX_free(cctx);
  3367. return testresult;
  3368. }
  3369. static unsigned char cookie_magic_value[] = "cookie magic";
  3370. static int generate_cookie_callback(SSL *ssl, unsigned char *cookie,
  3371. unsigned int *cookie_len)
  3372. {
  3373. /*
  3374. * Not suitable as a real cookie generation function but good enough for
  3375. * testing!
  3376. */
  3377. memcpy(cookie, cookie_magic_value, sizeof(cookie_magic_value) - 1);
  3378. *cookie_len = sizeof(cookie_magic_value) - 1;
  3379. return 1;
  3380. }
  3381. static int verify_cookie_callback(SSL *ssl, const unsigned char *cookie,
  3382. unsigned int cookie_len)
  3383. {
  3384. if (cookie_len == sizeof(cookie_magic_value) - 1
  3385. && memcmp(cookie, cookie_magic_value, cookie_len) == 0)
  3386. return 1;
  3387. return 0;
  3388. }
  3389. static int generate_stateless_cookie_callback(SSL *ssl, unsigned char *cookie,
  3390. size_t *cookie_len)
  3391. {
  3392. unsigned int temp;
  3393. int res = generate_cookie_callback(ssl, cookie, &temp);
  3394. *cookie_len = temp;
  3395. return res;
  3396. }
  3397. static int verify_stateless_cookie_callback(SSL *ssl, const unsigned char *cookie,
  3398. size_t cookie_len)
  3399. {
  3400. return verify_cookie_callback(ssl, cookie, cookie_len);
  3401. }
  3402. static int test_stateless(void)
  3403. {
  3404. SSL_CTX *sctx = NULL, *cctx = NULL;
  3405. SSL *serverssl = NULL, *clientssl = NULL;
  3406. int testresult = 0;
  3407. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  3408. TLS1_VERSION, TLS_MAX_VERSION,
  3409. &sctx, &cctx, cert, privkey)))
  3410. goto end;
  3411. /* The arrival of CCS messages can confuse the test */
  3412. SSL_CTX_clear_options(cctx, SSL_OP_ENABLE_MIDDLEBOX_COMPAT);
  3413. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3414. NULL, NULL))
  3415. /* Send the first ClientHello */
  3416. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3417. SSL_ERROR_WANT_READ))
  3418. /*
  3419. * This should fail with a -1 return because we have no callbacks
  3420. * set up
  3421. */
  3422. || !TEST_int_eq(SSL_stateless(serverssl), -1))
  3423. goto end;
  3424. /* Fatal error so abandon the connection from this client */
  3425. SSL_free(clientssl);
  3426. clientssl = NULL;
  3427. /* Set up the cookie generation and verification callbacks */
  3428. SSL_CTX_set_stateless_cookie_generate_cb(sctx, generate_stateless_cookie_callback);
  3429. SSL_CTX_set_stateless_cookie_verify_cb(sctx, verify_stateless_cookie_callback);
  3430. /*
  3431. * Create a new connection from the client (we can reuse the server SSL
  3432. * object).
  3433. */
  3434. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3435. NULL, NULL))
  3436. /* Send the first ClientHello */
  3437. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3438. SSL_ERROR_WANT_READ))
  3439. /* This should fail because there is no cookie */
  3440. || !TEST_int_eq(SSL_stateless(serverssl), 0))
  3441. goto end;
  3442. /* Abandon the connection from this client */
  3443. SSL_free(clientssl);
  3444. clientssl = NULL;
  3445. /*
  3446. * Now create a connection from a new client but with the same server SSL
  3447. * object
  3448. */
  3449. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3450. NULL, NULL))
  3451. /* Send the first ClientHello */
  3452. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3453. SSL_ERROR_WANT_READ))
  3454. /* This should fail because there is no cookie */
  3455. || !TEST_int_eq(SSL_stateless(serverssl), 0)
  3456. /* Send the second ClientHello */
  3457. || !TEST_false(create_ssl_connection(serverssl, clientssl,
  3458. SSL_ERROR_WANT_READ))
  3459. /* This should succeed because a cookie is now present */
  3460. || !TEST_int_eq(SSL_stateless(serverssl), 1)
  3461. /* Complete the connection */
  3462. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3463. SSL_ERROR_NONE)))
  3464. goto end;
  3465. shutdown_ssl_connection(serverssl, clientssl);
  3466. serverssl = clientssl = NULL;
  3467. testresult = 1;
  3468. end:
  3469. SSL_free(serverssl);
  3470. SSL_free(clientssl);
  3471. SSL_CTX_free(sctx);
  3472. SSL_CTX_free(cctx);
  3473. return testresult;
  3474. }
  3475. #endif /* OPENSSL_NO_TLS1_3 */
  3476. static int clntaddoldcb = 0;
  3477. static int clntparseoldcb = 0;
  3478. static int srvaddoldcb = 0;
  3479. static int srvparseoldcb = 0;
  3480. static int clntaddnewcb = 0;
  3481. static int clntparsenewcb = 0;
  3482. static int srvaddnewcb = 0;
  3483. static int srvparsenewcb = 0;
  3484. static int snicb = 0;
  3485. #define TEST_EXT_TYPE1 0xff00
  3486. static int old_add_cb(SSL *s, unsigned int ext_type, const unsigned char **out,
  3487. size_t *outlen, int *al, void *add_arg)
  3488. {
  3489. int *server = (int *)add_arg;
  3490. unsigned char *data;
  3491. if (SSL_is_server(s))
  3492. srvaddoldcb++;
  3493. else
  3494. clntaddoldcb++;
  3495. if (*server != SSL_is_server(s)
  3496. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  3497. return -1;
  3498. *data = 1;
  3499. *out = data;
  3500. *outlen = sizeof(char);
  3501. return 1;
  3502. }
  3503. static void old_free_cb(SSL *s, unsigned int ext_type, const unsigned char *out,
  3504. void *add_arg)
  3505. {
  3506. OPENSSL_free((unsigned char *)out);
  3507. }
  3508. static int old_parse_cb(SSL *s, unsigned int ext_type, const unsigned char *in,
  3509. size_t inlen, int *al, void *parse_arg)
  3510. {
  3511. int *server = (int *)parse_arg;
  3512. if (SSL_is_server(s))
  3513. srvparseoldcb++;
  3514. else
  3515. clntparseoldcb++;
  3516. if (*server != SSL_is_server(s)
  3517. || inlen != sizeof(char)
  3518. || *in != 1)
  3519. return -1;
  3520. return 1;
  3521. }
  3522. static int new_add_cb(SSL *s, unsigned int ext_type, unsigned int context,
  3523. const unsigned char **out, size_t *outlen, X509 *x,
  3524. size_t chainidx, int *al, void *add_arg)
  3525. {
  3526. int *server = (int *)add_arg;
  3527. unsigned char *data;
  3528. if (SSL_is_server(s))
  3529. srvaddnewcb++;
  3530. else
  3531. clntaddnewcb++;
  3532. if (*server != SSL_is_server(s)
  3533. || (data = OPENSSL_malloc(sizeof(*data))) == NULL)
  3534. return -1;
  3535. *data = 1;
  3536. *out = data;
  3537. *outlen = sizeof(*data);
  3538. return 1;
  3539. }
  3540. static void new_free_cb(SSL *s, unsigned int ext_type, unsigned int context,
  3541. const unsigned char *out, void *add_arg)
  3542. {
  3543. OPENSSL_free((unsigned char *)out);
  3544. }
  3545. static int new_parse_cb(SSL *s, unsigned int ext_type, unsigned int context,
  3546. const unsigned char *in, size_t inlen, X509 *x,
  3547. size_t chainidx, int *al, void *parse_arg)
  3548. {
  3549. int *server = (int *)parse_arg;
  3550. if (SSL_is_server(s))
  3551. srvparsenewcb++;
  3552. else
  3553. clntparsenewcb++;
  3554. if (*server != SSL_is_server(s)
  3555. || inlen != sizeof(char) || *in != 1)
  3556. return -1;
  3557. return 1;
  3558. }
  3559. static int sni_cb(SSL *s, int *al, void *arg)
  3560. {
  3561. SSL_CTX *ctx = (SSL_CTX *)arg;
  3562. if (SSL_set_SSL_CTX(s, ctx) == NULL) {
  3563. *al = SSL_AD_INTERNAL_ERROR;
  3564. return SSL_TLSEXT_ERR_ALERT_FATAL;
  3565. }
  3566. snicb++;
  3567. return SSL_TLSEXT_ERR_OK;
  3568. }
  3569. static int verify_cb(int preverify_ok, X509_STORE_CTX *x509_ctx)
  3570. {
  3571. return 1;
  3572. }
  3573. /*
  3574. * Custom call back tests.
  3575. * Test 0: Old style callbacks in TLSv1.2
  3576. * Test 1: New style callbacks in TLSv1.2
  3577. * Test 2: New style callbacks in TLSv1.2 with SNI
  3578. * Test 3: New style callbacks in TLSv1.3. Extensions in CH and EE
  3579. * Test 4: New style callbacks in TLSv1.3. Extensions in CH, SH, EE, Cert + NST
  3580. * Test 5: New style callbacks in TLSv1.3. Extensions in CR + Client Cert
  3581. */
  3582. static int test_custom_exts(int tst)
  3583. {
  3584. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  3585. SSL *clientssl = NULL, *serverssl = NULL;
  3586. int testresult = 0;
  3587. static int server = 1;
  3588. static int client = 0;
  3589. SSL_SESSION *sess = NULL;
  3590. unsigned int context;
  3591. #if defined(OPENSSL_NO_TLS1_2) && !defined(OPENSSL_NO_TLS1_3)
  3592. /* Skip tests for TLSv1.2 and below in this case */
  3593. if (tst < 3)
  3594. return 1;
  3595. #endif
  3596. /* Reset callback counters */
  3597. clntaddoldcb = clntparseoldcb = srvaddoldcb = srvparseoldcb = 0;
  3598. clntaddnewcb = clntparsenewcb = srvaddnewcb = srvparsenewcb = 0;
  3599. snicb = 0;
  3600. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  3601. TLS1_VERSION, TLS_MAX_VERSION,
  3602. &sctx, &cctx, cert, privkey)))
  3603. goto end;
  3604. if (tst == 2
  3605. && !TEST_true(create_ssl_ctx_pair(TLS_server_method(), NULL,
  3606. TLS1_VERSION, TLS_MAX_VERSION,
  3607. &sctx2, NULL, cert, privkey)))
  3608. goto end;
  3609. if (tst < 3) {
  3610. SSL_CTX_set_options(cctx, SSL_OP_NO_TLSv1_3);
  3611. SSL_CTX_set_options(sctx, SSL_OP_NO_TLSv1_3);
  3612. if (sctx2 != NULL)
  3613. SSL_CTX_set_options(sctx2, SSL_OP_NO_TLSv1_3);
  3614. }
  3615. if (tst == 5) {
  3616. context = SSL_EXT_TLS1_3_CERTIFICATE_REQUEST
  3617. | SSL_EXT_TLS1_3_CERTIFICATE;
  3618. SSL_CTX_set_verify(sctx,
  3619. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  3620. verify_cb);
  3621. if (!TEST_int_eq(SSL_CTX_use_certificate_file(cctx, cert,
  3622. SSL_FILETYPE_PEM), 1)
  3623. || !TEST_int_eq(SSL_CTX_use_PrivateKey_file(cctx, privkey,
  3624. SSL_FILETYPE_PEM), 1)
  3625. || !TEST_int_eq(SSL_CTX_check_private_key(cctx), 1))
  3626. goto end;
  3627. } else if (tst == 4) {
  3628. context = SSL_EXT_CLIENT_HELLO
  3629. | SSL_EXT_TLS1_2_SERVER_HELLO
  3630. | SSL_EXT_TLS1_3_SERVER_HELLO
  3631. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS
  3632. | SSL_EXT_TLS1_3_CERTIFICATE
  3633. | SSL_EXT_TLS1_3_NEW_SESSION_TICKET;
  3634. } else {
  3635. context = SSL_EXT_CLIENT_HELLO
  3636. | SSL_EXT_TLS1_2_SERVER_HELLO
  3637. | SSL_EXT_TLS1_3_ENCRYPTED_EXTENSIONS;
  3638. }
  3639. /* Create a client side custom extension */
  3640. if (tst == 0) {
  3641. if (!TEST_true(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  3642. old_add_cb, old_free_cb,
  3643. &client, old_parse_cb,
  3644. &client)))
  3645. goto end;
  3646. } else {
  3647. if (!TEST_true(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1, context,
  3648. new_add_cb, new_free_cb,
  3649. &client, new_parse_cb, &client)))
  3650. goto end;
  3651. }
  3652. /* Should not be able to add duplicates */
  3653. if (!TEST_false(SSL_CTX_add_client_custom_ext(cctx, TEST_EXT_TYPE1,
  3654. old_add_cb, old_free_cb,
  3655. &client, old_parse_cb,
  3656. &client))
  3657. || !TEST_false(SSL_CTX_add_custom_ext(cctx, TEST_EXT_TYPE1,
  3658. context, new_add_cb,
  3659. new_free_cb, &client,
  3660. new_parse_cb, &client)))
  3661. goto end;
  3662. /* Create a server side custom extension */
  3663. if (tst == 0) {
  3664. if (!TEST_true(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  3665. old_add_cb, old_free_cb,
  3666. &server, old_parse_cb,
  3667. &server)))
  3668. goto end;
  3669. } else {
  3670. if (!TEST_true(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1, context,
  3671. new_add_cb, new_free_cb,
  3672. &server, new_parse_cb, &server)))
  3673. goto end;
  3674. if (sctx2 != NULL
  3675. && !TEST_true(SSL_CTX_add_custom_ext(sctx2, TEST_EXT_TYPE1,
  3676. context, new_add_cb,
  3677. new_free_cb, &server,
  3678. new_parse_cb, &server)))
  3679. goto end;
  3680. }
  3681. /* Should not be able to add duplicates */
  3682. if (!TEST_false(SSL_CTX_add_server_custom_ext(sctx, TEST_EXT_TYPE1,
  3683. old_add_cb, old_free_cb,
  3684. &server, old_parse_cb,
  3685. &server))
  3686. || !TEST_false(SSL_CTX_add_custom_ext(sctx, TEST_EXT_TYPE1,
  3687. context, new_add_cb,
  3688. new_free_cb, &server,
  3689. new_parse_cb, &server)))
  3690. goto end;
  3691. if (tst == 2) {
  3692. /* Set up SNI */
  3693. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  3694. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  3695. goto end;
  3696. }
  3697. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  3698. &clientssl, NULL, NULL))
  3699. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3700. SSL_ERROR_NONE)))
  3701. goto end;
  3702. if (tst == 0) {
  3703. if (clntaddoldcb != 1
  3704. || clntparseoldcb != 1
  3705. || srvaddoldcb != 1
  3706. || srvparseoldcb != 1)
  3707. goto end;
  3708. } else if (tst == 1 || tst == 2 || tst == 3) {
  3709. if (clntaddnewcb != 1
  3710. || clntparsenewcb != 1
  3711. || srvaddnewcb != 1
  3712. || srvparsenewcb != 1
  3713. || (tst != 2 && snicb != 0)
  3714. || (tst == 2 && snicb != 1))
  3715. goto end;
  3716. } else if (tst == 5) {
  3717. if (clntaddnewcb != 1
  3718. || clntparsenewcb != 1
  3719. || srvaddnewcb != 1
  3720. || srvparsenewcb != 1)
  3721. goto end;
  3722. } else {
  3723. /* In this case there 2 NewSessionTicket messages created */
  3724. if (clntaddnewcb != 1
  3725. || clntparsenewcb != 5
  3726. || srvaddnewcb != 5
  3727. || srvparsenewcb != 1)
  3728. goto end;
  3729. }
  3730. sess = SSL_get1_session(clientssl);
  3731. SSL_shutdown(clientssl);
  3732. SSL_shutdown(serverssl);
  3733. SSL_free(serverssl);
  3734. SSL_free(clientssl);
  3735. serverssl = clientssl = NULL;
  3736. if (tst == 3 || tst == 5) {
  3737. /* We don't bother with the resumption aspects for these tests */
  3738. testresult = 1;
  3739. goto end;
  3740. }
  3741. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  3742. NULL, NULL))
  3743. || !TEST_true(SSL_set_session(clientssl, sess))
  3744. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  3745. SSL_ERROR_NONE)))
  3746. goto end;
  3747. /*
  3748. * For a resumed session we expect to add the ClientHello extension. For the
  3749. * old style callbacks we ignore it on the server side because they set
  3750. * SSL_EXT_IGNORE_ON_RESUMPTION. The new style callbacks do not ignore
  3751. * them.
  3752. */
  3753. if (tst == 0) {
  3754. if (clntaddoldcb != 2
  3755. || clntparseoldcb != 1
  3756. || srvaddoldcb != 1
  3757. || srvparseoldcb != 1)
  3758. goto end;
  3759. } else if (tst == 1 || tst == 2 || tst == 3) {
  3760. if (clntaddnewcb != 2
  3761. || clntparsenewcb != 2
  3762. || srvaddnewcb != 2
  3763. || srvparsenewcb != 2)
  3764. goto end;
  3765. } else {
  3766. /*
  3767. * No Certificate message extensions in the resumption handshake,
  3768. * 2 NewSessionTickets in the initial handshake, 1 in the resumption
  3769. */
  3770. if (clntaddnewcb != 2
  3771. || clntparsenewcb != 8
  3772. || srvaddnewcb != 8
  3773. || srvparsenewcb != 2)
  3774. goto end;
  3775. }
  3776. testresult = 1;
  3777. end:
  3778. SSL_SESSION_free(sess);
  3779. SSL_free(serverssl);
  3780. SSL_free(clientssl);
  3781. SSL_CTX_free(sctx2);
  3782. SSL_CTX_free(sctx);
  3783. SSL_CTX_free(cctx);
  3784. return testresult;
  3785. }
  3786. /*
  3787. * Test loading of serverinfo data in various formats. test_sslmessages actually
  3788. * tests to make sure the extensions appear in the handshake
  3789. */
  3790. static int test_serverinfo(int tst)
  3791. {
  3792. unsigned int version;
  3793. unsigned char *sibuf;
  3794. size_t sibuflen;
  3795. int ret, expected, testresult = 0;
  3796. SSL_CTX *ctx;
  3797. ctx = SSL_CTX_new(TLS_method());
  3798. if (!TEST_ptr(ctx))
  3799. goto end;
  3800. if ((tst & 0x01) == 0x01)
  3801. version = SSL_SERVERINFOV2;
  3802. else
  3803. version = SSL_SERVERINFOV1;
  3804. if ((tst & 0x02) == 0x02) {
  3805. sibuf = serverinfov2;
  3806. sibuflen = sizeof(serverinfov2);
  3807. expected = (version == SSL_SERVERINFOV2);
  3808. } else {
  3809. sibuf = serverinfov1;
  3810. sibuflen = sizeof(serverinfov1);
  3811. expected = (version == SSL_SERVERINFOV1);
  3812. }
  3813. if ((tst & 0x04) == 0x04) {
  3814. ret = SSL_CTX_use_serverinfo_ex(ctx, version, sibuf, sibuflen);
  3815. } else {
  3816. ret = SSL_CTX_use_serverinfo(ctx, sibuf, sibuflen);
  3817. /*
  3818. * The version variable is irrelevant in this case - it's what is in the
  3819. * buffer that matters
  3820. */
  3821. if ((tst & 0x02) == 0x02)
  3822. expected = 0;
  3823. else
  3824. expected = 1;
  3825. }
  3826. if (!TEST_true(ret == expected))
  3827. goto end;
  3828. testresult = 1;
  3829. end:
  3830. SSL_CTX_free(ctx);
  3831. return testresult;
  3832. }
  3833. /*
  3834. * Test that SSL_export_keying_material() produces expected results. There are
  3835. * no test vectors so all we do is test that both sides of the communication
  3836. * produce the same results for different protocol versions.
  3837. */
  3838. #define SMALL_LABEL_LEN 10
  3839. #define LONG_LABEL_LEN 249
  3840. static int test_export_key_mat(int tst)
  3841. {
  3842. int testresult = 0;
  3843. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  3844. SSL *clientssl = NULL, *serverssl = NULL;
  3845. const char label[LONG_LABEL_LEN + 1] = "test label";
  3846. const unsigned char context[] = "context";
  3847. const unsigned char *emptycontext = NULL;
  3848. unsigned char ckeymat1[80], ckeymat2[80], ckeymat3[80];
  3849. unsigned char skeymat1[80], skeymat2[80], skeymat3[80];
  3850. size_t labellen;
  3851. const int protocols[] = {
  3852. TLS1_VERSION,
  3853. TLS1_1_VERSION,
  3854. TLS1_2_VERSION,
  3855. TLS1_3_VERSION,
  3856. TLS1_3_VERSION,
  3857. TLS1_3_VERSION
  3858. };
  3859. #ifdef OPENSSL_NO_TLS1
  3860. if (tst == 0)
  3861. return 1;
  3862. #endif
  3863. #ifdef OPENSSL_NO_TLS1_1
  3864. if (tst == 1)
  3865. return 1;
  3866. #endif
  3867. #ifdef OPENSSL_NO_TLS1_2
  3868. if (tst == 2)
  3869. return 1;
  3870. #endif
  3871. #ifdef OPENSSL_NO_TLS1_3
  3872. if (tst >= 3)
  3873. return 1;
  3874. #endif
  3875. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  3876. TLS1_VERSION, TLS_MAX_VERSION,
  3877. &sctx, &cctx, cert, privkey)))
  3878. goto end;
  3879. OPENSSL_assert(tst >= 0 && (size_t)tst < OSSL_NELEM(protocols));
  3880. SSL_CTX_set_max_proto_version(cctx, protocols[tst]);
  3881. SSL_CTX_set_min_proto_version(cctx, protocols[tst]);
  3882. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  3883. NULL)))
  3884. goto end;
  3885. /*
  3886. * Premature call of SSL_export_keying_material should just fail.
  3887. */
  3888. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  3889. sizeof(ckeymat1), label,
  3890. SMALL_LABEL_LEN + 1, context,
  3891. sizeof(context) - 1, 1), 0))
  3892. goto end;
  3893. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  3894. SSL_ERROR_NONE)))
  3895. goto end;
  3896. if (tst == 5) {
  3897. /*
  3898. * TLSv1.3 imposes a maximum label len of 249 bytes. Check we fail if we
  3899. * go over that.
  3900. */
  3901. if (!TEST_int_le(SSL_export_keying_material(clientssl, ckeymat1,
  3902. sizeof(ckeymat1), label,
  3903. LONG_LABEL_LEN + 1, context,
  3904. sizeof(context) - 1, 1), 0))
  3905. goto end;
  3906. testresult = 1;
  3907. goto end;
  3908. } else if (tst == 4) {
  3909. labellen = LONG_LABEL_LEN;
  3910. } else {
  3911. labellen = SMALL_LABEL_LEN;
  3912. }
  3913. if (!TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat1,
  3914. sizeof(ckeymat1), label,
  3915. labellen, context,
  3916. sizeof(context) - 1, 1), 1)
  3917. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat2,
  3918. sizeof(ckeymat2), label,
  3919. labellen,
  3920. emptycontext,
  3921. 0, 1), 1)
  3922. || !TEST_int_eq(SSL_export_keying_material(clientssl, ckeymat3,
  3923. sizeof(ckeymat3), label,
  3924. labellen,
  3925. NULL, 0, 0), 1)
  3926. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat1,
  3927. sizeof(skeymat1), label,
  3928. labellen,
  3929. context,
  3930. sizeof(context) -1, 1),
  3931. 1)
  3932. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat2,
  3933. sizeof(skeymat2), label,
  3934. labellen,
  3935. emptycontext,
  3936. 0, 1), 1)
  3937. || !TEST_int_eq(SSL_export_keying_material(serverssl, skeymat3,
  3938. sizeof(skeymat3), label,
  3939. labellen,
  3940. NULL, 0, 0), 1)
  3941. /*
  3942. * Check that both sides created the same key material with the
  3943. * same context.
  3944. */
  3945. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  3946. sizeof(skeymat1))
  3947. /*
  3948. * Check that both sides created the same key material with an
  3949. * empty context.
  3950. */
  3951. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  3952. sizeof(skeymat2))
  3953. /*
  3954. * Check that both sides created the same key material without a
  3955. * context.
  3956. */
  3957. || !TEST_mem_eq(ckeymat3, sizeof(ckeymat3), skeymat3,
  3958. sizeof(skeymat3))
  3959. /* Different contexts should produce different results */
  3960. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  3961. sizeof(ckeymat2)))
  3962. goto end;
  3963. /*
  3964. * Check that an empty context and no context produce different results in
  3965. * protocols less than TLSv1.3. In TLSv1.3 they should be the same.
  3966. */
  3967. if ((tst < 3 && !TEST_mem_ne(ckeymat2, sizeof(ckeymat2), ckeymat3,
  3968. sizeof(ckeymat3)))
  3969. || (tst >= 3 && !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), ckeymat3,
  3970. sizeof(ckeymat3))))
  3971. goto end;
  3972. testresult = 1;
  3973. end:
  3974. SSL_free(serverssl);
  3975. SSL_free(clientssl);
  3976. SSL_CTX_free(sctx2);
  3977. SSL_CTX_free(sctx);
  3978. SSL_CTX_free(cctx);
  3979. return testresult;
  3980. }
  3981. #ifndef OPENSSL_NO_TLS1_3
  3982. /*
  3983. * Test that SSL_export_keying_material_early() produces expected
  3984. * results. There are no test vectors so all we do is test that both
  3985. * sides of the communication produce the same results for different
  3986. * protocol versions.
  3987. */
  3988. static int test_export_key_mat_early(int idx)
  3989. {
  3990. static const char label[] = "test label";
  3991. static const unsigned char context[] = "context";
  3992. int testresult = 0;
  3993. SSL_CTX *cctx = NULL, *sctx = NULL;
  3994. SSL *clientssl = NULL, *serverssl = NULL;
  3995. SSL_SESSION *sess = NULL;
  3996. const unsigned char *emptycontext = NULL;
  3997. unsigned char ckeymat1[80], ckeymat2[80];
  3998. unsigned char skeymat1[80], skeymat2[80];
  3999. unsigned char buf[1];
  4000. size_t readbytes, written;
  4001. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl, &serverssl,
  4002. &sess, idx)))
  4003. goto end;
  4004. /* Here writing 0 length early data is enough. */
  4005. if (!TEST_true(SSL_write_early_data(clientssl, NULL, 0, &written))
  4006. || !TEST_int_eq(SSL_read_early_data(serverssl, buf, sizeof(buf),
  4007. &readbytes),
  4008. SSL_READ_EARLY_DATA_ERROR)
  4009. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  4010. SSL_EARLY_DATA_ACCEPTED))
  4011. goto end;
  4012. if (!TEST_int_eq(SSL_export_keying_material_early(
  4013. clientssl, ckeymat1, sizeof(ckeymat1), label,
  4014. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  4015. || !TEST_int_eq(SSL_export_keying_material_early(
  4016. clientssl, ckeymat2, sizeof(ckeymat2), label,
  4017. sizeof(label) - 1, emptycontext, 0), 1)
  4018. || !TEST_int_eq(SSL_export_keying_material_early(
  4019. serverssl, skeymat1, sizeof(skeymat1), label,
  4020. sizeof(label) - 1, context, sizeof(context) - 1), 1)
  4021. || !TEST_int_eq(SSL_export_keying_material_early(
  4022. serverssl, skeymat2, sizeof(skeymat2), label,
  4023. sizeof(label) - 1, emptycontext, 0), 1)
  4024. /*
  4025. * Check that both sides created the same key material with the
  4026. * same context.
  4027. */
  4028. || !TEST_mem_eq(ckeymat1, sizeof(ckeymat1), skeymat1,
  4029. sizeof(skeymat1))
  4030. /*
  4031. * Check that both sides created the same key material with an
  4032. * empty context.
  4033. */
  4034. || !TEST_mem_eq(ckeymat2, sizeof(ckeymat2), skeymat2,
  4035. sizeof(skeymat2))
  4036. /* Different contexts should produce different results */
  4037. || !TEST_mem_ne(ckeymat1, sizeof(ckeymat1), ckeymat2,
  4038. sizeof(ckeymat2)))
  4039. goto end;
  4040. testresult = 1;
  4041. end:
  4042. SSL_SESSION_free(sess);
  4043. SSL_SESSION_free(clientpsk);
  4044. SSL_SESSION_free(serverpsk);
  4045. clientpsk = serverpsk = NULL;
  4046. SSL_free(serverssl);
  4047. SSL_free(clientssl);
  4048. SSL_CTX_free(sctx);
  4049. SSL_CTX_free(cctx);
  4050. return testresult;
  4051. }
  4052. #define NUM_KEY_UPDATE_MESSAGES 40
  4053. /*
  4054. * Test KeyUpdate.
  4055. */
  4056. static int test_key_update(void)
  4057. {
  4058. SSL_CTX *cctx = NULL, *sctx = NULL;
  4059. SSL *clientssl = NULL, *serverssl = NULL;
  4060. int testresult = 0, i, j;
  4061. char buf[20];
  4062. static char *mess = "A test message";
  4063. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  4064. TLS_client_method(),
  4065. TLS1_3_VERSION,
  4066. 0,
  4067. &sctx, &cctx, cert, privkey))
  4068. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4069. NULL, NULL))
  4070. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4071. SSL_ERROR_NONE)))
  4072. goto end;
  4073. for (j = 0; j < 2; j++) {
  4074. /* Send lots of KeyUpdate messages */
  4075. for (i = 0; i < NUM_KEY_UPDATE_MESSAGES; i++) {
  4076. if (!TEST_true(SSL_key_update(clientssl,
  4077. (j == 0)
  4078. ? SSL_KEY_UPDATE_NOT_REQUESTED
  4079. : SSL_KEY_UPDATE_REQUESTED))
  4080. || !TEST_true(SSL_do_handshake(clientssl)))
  4081. goto end;
  4082. }
  4083. /* Check that sending and receiving app data is ok */
  4084. if (!TEST_int_eq(SSL_write(clientssl, mess, strlen(mess)), strlen(mess))
  4085. || !TEST_int_eq(SSL_read(serverssl, buf, sizeof(buf)),
  4086. strlen(mess)))
  4087. goto end;
  4088. if (!TEST_int_eq(SSL_write(serverssl, mess, strlen(mess)), strlen(mess))
  4089. || !TEST_int_eq(SSL_read(clientssl, buf, sizeof(buf)),
  4090. strlen(mess)))
  4091. goto end;
  4092. }
  4093. testresult = 1;
  4094. end:
  4095. SSL_free(serverssl);
  4096. SSL_free(clientssl);
  4097. SSL_CTX_free(sctx);
  4098. SSL_CTX_free(cctx);
  4099. return testresult;
  4100. }
  4101. /*
  4102. * Test we can handle a KeyUpdate (update requested) message while write data
  4103. * is pending.
  4104. * Test 0: Client sends KeyUpdate while Server is writing
  4105. * Test 1: Server sends KeyUpdate while Client is writing
  4106. */
  4107. static int test_key_update_in_write(int tst)
  4108. {
  4109. SSL_CTX *cctx = NULL, *sctx = NULL;
  4110. SSL *clientssl = NULL, *serverssl = NULL;
  4111. int testresult = 0;
  4112. char buf[20];
  4113. static char *mess = "A test message";
  4114. BIO *bretry = BIO_new(bio_s_always_retry());
  4115. BIO *tmp = NULL;
  4116. SSL *peerupdate = NULL, *peerwrite = NULL;
  4117. if (!TEST_ptr(bretry)
  4118. || !TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  4119. TLS_client_method(),
  4120. TLS1_3_VERSION,
  4121. 0,
  4122. &sctx, &cctx, cert, privkey))
  4123. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4124. NULL, NULL))
  4125. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4126. SSL_ERROR_NONE)))
  4127. goto end;
  4128. peerupdate = tst == 0 ? clientssl : serverssl;
  4129. peerwrite = tst == 0 ? serverssl : clientssl;
  4130. if (!TEST_true(SSL_key_update(peerupdate, SSL_KEY_UPDATE_REQUESTED))
  4131. || !TEST_true(SSL_do_handshake(peerupdate)))
  4132. goto end;
  4133. /* Swap the writing endpoint's write BIO to force a retry */
  4134. tmp = SSL_get_wbio(peerwrite);
  4135. if (!TEST_ptr(tmp) || !TEST_true(BIO_up_ref(tmp))) {
  4136. tmp = NULL;
  4137. goto end;
  4138. }
  4139. SSL_set0_wbio(peerwrite, bretry);
  4140. bretry = NULL;
  4141. /* Write data that we know will fail with SSL_ERROR_WANT_WRITE */
  4142. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), -1)
  4143. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_WRITE))
  4144. goto end;
  4145. /* Reinstate the original writing endpoint's write BIO */
  4146. SSL_set0_wbio(peerwrite, tmp);
  4147. tmp = NULL;
  4148. /* Now read some data - we will read the key update */
  4149. if (!TEST_int_eq(SSL_read(peerwrite, buf, sizeof(buf)), -1)
  4150. || !TEST_int_eq(SSL_get_error(peerwrite, 0), SSL_ERROR_WANT_READ))
  4151. goto end;
  4152. /*
  4153. * Complete the write we started previously and read it from the other
  4154. * endpoint
  4155. */
  4156. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  4157. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  4158. goto end;
  4159. /* Write more data to ensure we send the KeyUpdate message back */
  4160. if (!TEST_int_eq(SSL_write(peerwrite, mess, strlen(mess)), strlen(mess))
  4161. || !TEST_int_eq(SSL_read(peerupdate, buf, sizeof(buf)), strlen(mess)))
  4162. goto end;
  4163. testresult = 1;
  4164. end:
  4165. SSL_free(serverssl);
  4166. SSL_free(clientssl);
  4167. SSL_CTX_free(sctx);
  4168. SSL_CTX_free(cctx);
  4169. BIO_free(bretry);
  4170. BIO_free(tmp);
  4171. return testresult;
  4172. }
  4173. #endif /* OPENSSL_NO_TLS1_3 */
  4174. static int test_ssl_clear(int idx)
  4175. {
  4176. SSL_CTX *cctx = NULL, *sctx = NULL;
  4177. SSL *clientssl = NULL, *serverssl = NULL;
  4178. int testresult = 0;
  4179. #ifdef OPENSSL_NO_TLS1_2
  4180. if (idx == 1)
  4181. return 1;
  4182. #endif
  4183. /* Create an initial connection */
  4184. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  4185. TLS1_VERSION, TLS_MAX_VERSION,
  4186. &sctx, &cctx, cert, privkey))
  4187. || (idx == 1
  4188. && !TEST_true(SSL_CTX_set_max_proto_version(cctx,
  4189. TLS1_2_VERSION)))
  4190. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4191. &clientssl, NULL, NULL))
  4192. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4193. SSL_ERROR_NONE)))
  4194. goto end;
  4195. SSL_shutdown(clientssl);
  4196. SSL_shutdown(serverssl);
  4197. SSL_free(serverssl);
  4198. serverssl = NULL;
  4199. /* Clear clientssl - we're going to reuse the object */
  4200. if (!TEST_true(SSL_clear(clientssl)))
  4201. goto end;
  4202. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4203. NULL, NULL))
  4204. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4205. SSL_ERROR_NONE))
  4206. || !TEST_true(SSL_session_reused(clientssl)))
  4207. goto end;
  4208. SSL_shutdown(clientssl);
  4209. SSL_shutdown(serverssl);
  4210. testresult = 1;
  4211. end:
  4212. SSL_free(serverssl);
  4213. SSL_free(clientssl);
  4214. SSL_CTX_free(sctx);
  4215. SSL_CTX_free(cctx);
  4216. return testresult;
  4217. }
  4218. /* Parse CH and retrieve any MFL extension value if present */
  4219. static int get_MFL_from_client_hello(BIO *bio, int *mfl_codemfl_code)
  4220. {
  4221. long len;
  4222. unsigned char *data;
  4223. PACKET pkt = {0}, pkt2 = {0}, pkt3 = {0};
  4224. unsigned int MFL_code = 0, type = 0;
  4225. if (!TEST_uint_gt( len = BIO_get_mem_data( bio, (char **) &data ), 0 ) )
  4226. goto end;
  4227. if (!TEST_true( PACKET_buf_init( &pkt, data, len ) )
  4228. /* Skip the record header */
  4229. || !PACKET_forward(&pkt, SSL3_RT_HEADER_LENGTH)
  4230. /* Skip the handshake message header */
  4231. || !TEST_true(PACKET_forward(&pkt, SSL3_HM_HEADER_LENGTH))
  4232. /* Skip client version and random */
  4233. || !TEST_true(PACKET_forward(&pkt, CLIENT_VERSION_LEN
  4234. + SSL3_RANDOM_SIZE))
  4235. /* Skip session id */
  4236. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  4237. /* Skip ciphers */
  4238. || !TEST_true(PACKET_get_length_prefixed_2(&pkt, &pkt2))
  4239. /* Skip compression */
  4240. || !TEST_true(PACKET_get_length_prefixed_1(&pkt, &pkt2))
  4241. /* Extensions len */
  4242. || !TEST_true(PACKET_as_length_prefixed_2(&pkt, &pkt2)))
  4243. goto end;
  4244. /* Loop through all extensions */
  4245. while (PACKET_remaining(&pkt2)) {
  4246. if (!TEST_true(PACKET_get_net_2(&pkt2, &type))
  4247. || !TEST_true(PACKET_get_length_prefixed_2(&pkt2, &pkt3)))
  4248. goto end;
  4249. if (type == TLSEXT_TYPE_max_fragment_length) {
  4250. if (!TEST_uint_ne(PACKET_remaining(&pkt3), 0)
  4251. || !TEST_true(PACKET_get_1(&pkt3, &MFL_code)))
  4252. goto end;
  4253. *mfl_codemfl_code = MFL_code;
  4254. return 1;
  4255. }
  4256. }
  4257. end:
  4258. return 0;
  4259. }
  4260. /* Maximum-Fragment-Length TLS extension mode to test */
  4261. static const unsigned char max_fragment_len_test[] = {
  4262. TLSEXT_max_fragment_length_512,
  4263. TLSEXT_max_fragment_length_1024,
  4264. TLSEXT_max_fragment_length_2048,
  4265. TLSEXT_max_fragment_length_4096
  4266. };
  4267. static int test_max_fragment_len_ext(int idx_tst)
  4268. {
  4269. SSL_CTX *ctx;
  4270. SSL *con = NULL;
  4271. int testresult = 0, MFL_mode = 0;
  4272. BIO *rbio, *wbio;
  4273. ctx = SSL_CTX_new(TLS_method());
  4274. if (!TEST_ptr(ctx))
  4275. goto end;
  4276. if (!TEST_true(SSL_CTX_set_tlsext_max_fragment_length(
  4277. ctx, max_fragment_len_test[idx_tst])))
  4278. goto end;
  4279. con = SSL_new(ctx);
  4280. if (!TEST_ptr(con))
  4281. goto end;
  4282. rbio = BIO_new(BIO_s_mem());
  4283. wbio = BIO_new(BIO_s_mem());
  4284. if (!TEST_ptr(rbio)|| !TEST_ptr(wbio)) {
  4285. BIO_free(rbio);
  4286. BIO_free(wbio);
  4287. goto end;
  4288. }
  4289. SSL_set_bio(con, rbio, wbio);
  4290. SSL_set_connect_state(con);
  4291. if (!TEST_int_le(SSL_connect(con), 0)) {
  4292. /* This shouldn't succeed because we don't have a server! */
  4293. goto end;
  4294. }
  4295. if (!TEST_true(get_MFL_from_client_hello(wbio, &MFL_mode)))
  4296. /* no MFL in client hello */
  4297. goto end;
  4298. if (!TEST_true(max_fragment_len_test[idx_tst] == MFL_mode))
  4299. goto end;
  4300. testresult = 1;
  4301. end:
  4302. SSL_free(con);
  4303. SSL_CTX_free(ctx);
  4304. return testresult;
  4305. }
  4306. #ifndef OPENSSL_NO_TLS1_3
  4307. static int test_pha_key_update(void)
  4308. {
  4309. SSL_CTX *cctx = NULL, *sctx = NULL;
  4310. SSL *clientssl = NULL, *serverssl = NULL;
  4311. int testresult = 0;
  4312. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  4313. TLS1_VERSION, TLS_MAX_VERSION,
  4314. &sctx, &cctx, cert, privkey)))
  4315. return 0;
  4316. if (!TEST_true(SSL_CTX_set_min_proto_version(sctx, TLS1_3_VERSION))
  4317. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_3_VERSION))
  4318. || !TEST_true(SSL_CTX_set_min_proto_version(cctx, TLS1_3_VERSION))
  4319. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_3_VERSION)))
  4320. goto end;
  4321. SSL_CTX_set_post_handshake_auth(cctx, 1);
  4322. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4323. NULL, NULL)))
  4324. goto end;
  4325. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4326. SSL_ERROR_NONE)))
  4327. goto end;
  4328. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  4329. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  4330. goto end;
  4331. if (!TEST_true(SSL_key_update(clientssl, SSL_KEY_UPDATE_NOT_REQUESTED)))
  4332. goto end;
  4333. /* Start handshake on the server */
  4334. if (!TEST_int_eq(SSL_do_handshake(serverssl), 1))
  4335. goto end;
  4336. /* Starts with SSL_connect(), but it's really just SSL_do_handshake() */
  4337. if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  4338. SSL_ERROR_NONE)))
  4339. goto end;
  4340. SSL_shutdown(clientssl);
  4341. SSL_shutdown(serverssl);
  4342. testresult = 1;
  4343. end:
  4344. SSL_free(serverssl);
  4345. SSL_free(clientssl);
  4346. SSL_CTX_free(sctx);
  4347. SSL_CTX_free(cctx);
  4348. return testresult;
  4349. }
  4350. #endif
  4351. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  4352. static SRP_VBASE *vbase = NULL;
  4353. static int ssl_srp_cb(SSL *s, int *ad, void *arg)
  4354. {
  4355. int ret = SSL3_AL_FATAL;
  4356. char *username;
  4357. SRP_user_pwd *user = NULL;
  4358. username = SSL_get_srp_username(s);
  4359. if (username == NULL) {
  4360. *ad = SSL_AD_INTERNAL_ERROR;
  4361. goto err;
  4362. }
  4363. user = SRP_VBASE_get1_by_user(vbase, username);
  4364. if (user == NULL) {
  4365. *ad = SSL_AD_INTERNAL_ERROR;
  4366. goto err;
  4367. }
  4368. if (SSL_set_srp_server_param(s, user->N, user->g, user->s, user->v,
  4369. user->info) <= 0) {
  4370. *ad = SSL_AD_INTERNAL_ERROR;
  4371. goto err;
  4372. }
  4373. ret = 0;
  4374. err:
  4375. SRP_user_pwd_free(user);
  4376. return ret;
  4377. }
  4378. static int create_new_vfile(char *userid, char *password, const char *filename)
  4379. {
  4380. char *gNid = NULL;
  4381. OPENSSL_STRING *row = OPENSSL_zalloc(sizeof(row) * (DB_NUMBER + 1));
  4382. TXT_DB *db = NULL;
  4383. int ret = 0;
  4384. BIO *out = NULL, *dummy = BIO_new_mem_buf("", 0);
  4385. size_t i;
  4386. if (!TEST_ptr(dummy) || !TEST_ptr(row))
  4387. goto end;
  4388. gNid = SRP_create_verifier(userid, password, &row[DB_srpsalt],
  4389. &row[DB_srpverifier], NULL, NULL);
  4390. if (!TEST_ptr(gNid))
  4391. goto end;
  4392. /*
  4393. * The only way to create an empty TXT_DB is to provide a BIO with no data
  4394. * in it!
  4395. */
  4396. db = TXT_DB_read(dummy, DB_NUMBER);
  4397. if (!TEST_ptr(db))
  4398. goto end;
  4399. out = BIO_new_file(filename, "w");
  4400. if (!TEST_ptr(out))
  4401. goto end;
  4402. row[DB_srpid] = OPENSSL_strdup(userid);
  4403. row[DB_srptype] = OPENSSL_strdup("V");
  4404. row[DB_srpgN] = OPENSSL_strdup(gNid);
  4405. if (!TEST_ptr(row[DB_srpid])
  4406. || !TEST_ptr(row[DB_srptype])
  4407. || !TEST_ptr(row[DB_srpgN])
  4408. || !TEST_true(TXT_DB_insert(db, row)))
  4409. goto end;
  4410. row = NULL;
  4411. if (!TXT_DB_write(out, db))
  4412. goto end;
  4413. ret = 1;
  4414. end:
  4415. if (row != NULL) {
  4416. for (i = 0; i < DB_NUMBER; i++)
  4417. OPENSSL_free(row[i]);
  4418. }
  4419. OPENSSL_free(row);
  4420. BIO_free(dummy);
  4421. BIO_free(out);
  4422. TXT_DB_free(db);
  4423. return ret;
  4424. }
  4425. static int create_new_vbase(char *userid, char *password)
  4426. {
  4427. BIGNUM *verifier = NULL, *salt = NULL;
  4428. const SRP_gN *lgN = NULL;
  4429. SRP_user_pwd *user_pwd = NULL;
  4430. int ret = 0;
  4431. lgN = SRP_get_default_gN(NULL);
  4432. if (!TEST_ptr(lgN))
  4433. goto end;
  4434. if (!TEST_true(SRP_create_verifier_BN(userid, password, &salt, &verifier,
  4435. lgN->N, lgN->g)))
  4436. goto end;
  4437. user_pwd = OPENSSL_zalloc(sizeof(*user_pwd));
  4438. if (!TEST_ptr(user_pwd))
  4439. goto end;
  4440. user_pwd->N = lgN->N;
  4441. user_pwd->g = lgN->g;
  4442. user_pwd->id = OPENSSL_strdup(userid);
  4443. if (!TEST_ptr(user_pwd->id))
  4444. goto end;
  4445. user_pwd->v = verifier;
  4446. user_pwd->s = salt;
  4447. verifier = salt = NULL;
  4448. if (sk_SRP_user_pwd_insert(vbase->users_pwd, user_pwd, 0) == 0)
  4449. goto end;
  4450. user_pwd = NULL;
  4451. ret = 1;
  4452. end:
  4453. SRP_user_pwd_free(user_pwd);
  4454. BN_free(salt);
  4455. BN_free(verifier);
  4456. return ret;
  4457. }
  4458. /*
  4459. * SRP tests
  4460. *
  4461. * Test 0: Simple successful SRP connection, new vbase
  4462. * Test 1: Connection failure due to bad password, new vbase
  4463. * Test 2: Simple successful SRP connection, vbase loaded from existing file
  4464. * Test 3: Connection failure due to bad password, vbase loaded from existing
  4465. * file
  4466. * Test 4: Simple successful SRP connection, vbase loaded from new file
  4467. * Test 5: Connection failure due to bad password, vbase loaded from new file
  4468. */
  4469. static int test_srp(int tst)
  4470. {
  4471. char *userid = "test", *password = "password", *tstsrpfile;
  4472. SSL_CTX *cctx = NULL, *sctx = NULL;
  4473. SSL *clientssl = NULL, *serverssl = NULL;
  4474. int ret, testresult = 0;
  4475. vbase = SRP_VBASE_new(NULL);
  4476. if (!TEST_ptr(vbase))
  4477. goto end;
  4478. if (tst == 0 || tst == 1) {
  4479. if (!TEST_true(create_new_vbase(userid, password)))
  4480. goto end;
  4481. } else {
  4482. if (tst == 4 || tst == 5) {
  4483. if (!TEST_true(create_new_vfile(userid, password, tmpfilename)))
  4484. goto end;
  4485. tstsrpfile = tmpfilename;
  4486. } else {
  4487. tstsrpfile = srpvfile;
  4488. }
  4489. if (!TEST_int_eq(SRP_VBASE_init(vbase, tstsrpfile), SRP_NO_ERROR))
  4490. goto end;
  4491. }
  4492. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  4493. TLS1_VERSION, TLS_MAX_VERSION,
  4494. &sctx, &cctx, cert, privkey)))
  4495. goto end;
  4496. if (!TEST_int_gt(SSL_CTX_set_srp_username_callback(sctx, ssl_srp_cb), 0)
  4497. || !TEST_true(SSL_CTX_set_cipher_list(cctx, "SRP-AES-128-CBC-SHA"))
  4498. || !TEST_true(SSL_CTX_set_max_proto_version(sctx, TLS1_2_VERSION))
  4499. || !TEST_true(SSL_CTX_set_max_proto_version(cctx, TLS1_2_VERSION))
  4500. || !TEST_int_gt(SSL_CTX_set_srp_username(cctx, userid), 0))
  4501. goto end;
  4502. if (tst % 2 == 1) {
  4503. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, "badpass"), 0))
  4504. goto end;
  4505. } else {
  4506. if (!TEST_int_gt(SSL_CTX_set_srp_password(cctx, password), 0))
  4507. goto end;
  4508. }
  4509. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4510. NULL, NULL)))
  4511. goto end;
  4512. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  4513. if (ret) {
  4514. if (!TEST_true(tst % 2 == 0))
  4515. goto end;
  4516. } else {
  4517. if (!TEST_true(tst % 2 == 1))
  4518. goto end;
  4519. }
  4520. testresult = 1;
  4521. end:
  4522. SRP_VBASE_free(vbase);
  4523. vbase = NULL;
  4524. SSL_free(serverssl);
  4525. SSL_free(clientssl);
  4526. SSL_CTX_free(sctx);
  4527. SSL_CTX_free(cctx);
  4528. return testresult;
  4529. }
  4530. #endif
  4531. static int info_cb_failed = 0;
  4532. static int info_cb_offset = 0;
  4533. static int info_cb_this_state = -1;
  4534. static struct info_cb_states_st {
  4535. int where;
  4536. const char *statestr;
  4537. } info_cb_states[][60] = {
  4538. {
  4539. /* TLSv1.2 server followed by resumption */
  4540. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4541. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  4542. {SSL_CB_LOOP, "TWSC"}, {SSL_CB_LOOP, "TWSKE"}, {SSL_CB_LOOP, "TWSD"},
  4543. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWSD"}, {SSL_CB_LOOP, "TRCKE"},
  4544. {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWST"},
  4545. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  4546. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  4547. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  4548. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"},
  4549. {SSL_CB_LOOP, "TWSH"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  4550. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TRCCS"},
  4551. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  4552. {SSL_CB_EXIT, NULL}, {0, NULL},
  4553. }, {
  4554. /* TLSv1.2 client followed by resumption */
  4555. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4556. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  4557. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRSC"}, {SSL_CB_LOOP, "TRSKE"},
  4558. {SSL_CB_LOOP, "TRSD"}, {SSL_CB_LOOP, "TWCKE"}, {SSL_CB_LOOP, "TWCCS"},
  4559. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWFIN"},
  4560. {SSL_CB_LOOP, "TRST"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  4561. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  4562. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4563. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  4564. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TRCCS"}, {SSL_CB_LOOP, "TRFIN"},
  4565. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  4566. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL}, {0, NULL},
  4567. }, {
  4568. /* TLSv1.3 server followed by resumption */
  4569. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4570. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  4571. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWSC"},
  4572. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_LOOP, "TED"},
  4573. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRFIN"},
  4574. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  4575. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_ALERT, NULL},
  4576. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4577. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  4578. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  4579. {SSL_CB_LOOP, "TED"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TED"},
  4580. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  4581. {SSL_CB_LOOP, "TWST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  4582. }, {
  4583. /* TLSv1.3 client followed by resumption */
  4584. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4585. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "TWCH"},
  4586. {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"}, {SSL_CB_LOOP, "TRSC"},
  4587. {SSL_CB_LOOP, "TRSCV"}, {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"},
  4588. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  4589. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
  4590. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "},
  4591. {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL},
  4592. {SSL_CB_ALERT, NULL}, {SSL_CB_HANDSHAKE_START, NULL},
  4593. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TWCH"}, {SSL_CB_EXIT, NULL},
  4594. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  4595. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWFIN"},
  4596. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  4597. {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "TRST"},
  4598. {SSL_CB_EXIT, NULL}, {0, NULL},
  4599. }, {
  4600. /* TLSv1.3 server, early_data */
  4601. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4602. {SSL_CB_LOOP, "PINIT "}, {SSL_CB_LOOP, "TRCH"}, {SSL_CB_LOOP, "TWSH"},
  4603. {SSL_CB_LOOP, "TWCCS"}, {SSL_CB_LOOP, "TWEE"}, {SSL_CB_LOOP, "TWFIN"},
  4604. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  4605. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  4606. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TWEOED"}, {SSL_CB_LOOP, "TRFIN"},
  4607. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_LOOP, "TWST"},
  4608. {SSL_CB_EXIT, NULL}, {0, NULL},
  4609. }, {
  4610. /* TLSv1.3 client, early_data */
  4611. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "PINIT "},
  4612. {SSL_CB_LOOP, "TWCH"}, {SSL_CB_LOOP, "TWCCS"},
  4613. {SSL_CB_HANDSHAKE_DONE, NULL}, {SSL_CB_EXIT, NULL},
  4614. {SSL_CB_HANDSHAKE_START, NULL}, {SSL_CB_LOOP, "TED"},
  4615. {SSL_CB_LOOP, "TED"}, {SSL_CB_LOOP, "TRSH"}, {SSL_CB_LOOP, "TREE"},
  4616. {SSL_CB_LOOP, "TRFIN"}, {SSL_CB_LOOP, "TPEDE"}, {SSL_CB_LOOP, "TWEOED"},
  4617. {SSL_CB_LOOP, "TWFIN"}, {SSL_CB_HANDSHAKE_DONE, NULL},
  4618. {SSL_CB_EXIT, NULL}, {SSL_CB_LOOP, "SSLOK "}, {SSL_CB_LOOP, "SSLOK "},
  4619. {SSL_CB_LOOP, "TRST"}, {SSL_CB_EXIT, NULL}, {0, NULL},
  4620. }, {
  4621. {0, NULL},
  4622. }
  4623. };
  4624. static void sslapi_info_callback(const SSL *s, int where, int ret)
  4625. {
  4626. struct info_cb_states_st *state = info_cb_states[info_cb_offset];
  4627. /* We do not ever expect a connection to fail in this test */
  4628. if (!TEST_false(ret == 0)) {
  4629. info_cb_failed = 1;
  4630. return;
  4631. }
  4632. /*
  4633. * Do some sanity checks. We never expect these things to happen in this
  4634. * test
  4635. */
  4636. if (!TEST_false((SSL_is_server(s) && (where & SSL_ST_CONNECT) != 0))
  4637. || !TEST_false(!SSL_is_server(s) && (where & SSL_ST_ACCEPT) != 0)
  4638. || !TEST_int_ne(state[++info_cb_this_state].where, 0)) {
  4639. info_cb_failed = 1;
  4640. return;
  4641. }
  4642. /* Now check we're in the right state */
  4643. if (!TEST_true((where & state[info_cb_this_state].where) != 0)) {
  4644. info_cb_failed = 1;
  4645. return;
  4646. }
  4647. if ((where & SSL_CB_LOOP) != 0
  4648. && !TEST_int_eq(strcmp(SSL_state_string(s),
  4649. state[info_cb_this_state].statestr), 0)) {
  4650. info_cb_failed = 1;
  4651. return;
  4652. }
  4653. /*
  4654. * Check that, if we've got SSL_CB_HANDSHAKE_DONE we are not in init
  4655. */
  4656. if ((where & SSL_CB_HANDSHAKE_DONE)
  4657. && SSL_in_init((SSL *)s) != 0) {
  4658. info_cb_failed = 1;
  4659. return;
  4660. }
  4661. }
  4662. /*
  4663. * Test the info callback gets called when we expect it to.
  4664. *
  4665. * Test 0: TLSv1.2, server
  4666. * Test 1: TLSv1.2, client
  4667. * Test 2: TLSv1.3, server
  4668. * Test 3: TLSv1.3, client
  4669. * Test 4: TLSv1.3, server, early_data
  4670. * Test 5: TLSv1.3, client, early_data
  4671. */
  4672. static int test_info_callback(int tst)
  4673. {
  4674. SSL_CTX *cctx = NULL, *sctx = NULL;
  4675. SSL *clientssl = NULL, *serverssl = NULL;
  4676. SSL_SESSION *clntsess = NULL;
  4677. int testresult = 0;
  4678. int tlsvers;
  4679. if (tst < 2) {
  4680. /* We need either ECDHE or DHE for the TLSv1.2 test to work */
  4681. #if !defined(OPENSSL_NO_TLS1_2) && (!defined(OPENSSL_NO_EC) \
  4682. || !defined(OPENSSL_NO_DH))
  4683. tlsvers = TLS1_2_VERSION;
  4684. #else
  4685. return 1;
  4686. #endif
  4687. } else {
  4688. #ifndef OPENSSL_NO_TLS1_3
  4689. tlsvers = TLS1_3_VERSION;
  4690. #else
  4691. return 1;
  4692. #endif
  4693. }
  4694. /* Reset globals */
  4695. info_cb_failed = 0;
  4696. info_cb_this_state = -1;
  4697. info_cb_offset = tst;
  4698. #ifndef OPENSSL_NO_TLS1_3
  4699. if (tst >= 4) {
  4700. SSL_SESSION *sess = NULL;
  4701. size_t written, readbytes;
  4702. unsigned char buf[80];
  4703. /* early_data tests */
  4704. if (!TEST_true(setupearly_data_test(&cctx, &sctx, &clientssl,
  4705. &serverssl, &sess, 0)))
  4706. goto end;
  4707. /* We don't actually need this reference */
  4708. SSL_SESSION_free(sess);
  4709. SSL_set_info_callback((tst % 2) == 0 ? serverssl : clientssl,
  4710. sslapi_info_callback);
  4711. /* Write and read some early data and then complete the connection */
  4712. if (!TEST_true(SSL_write_early_data(clientssl, MSG1, strlen(MSG1),
  4713. &written))
  4714. || !TEST_size_t_eq(written, strlen(MSG1))
  4715. || !TEST_int_eq(SSL_read_early_data(serverssl, buf,
  4716. sizeof(buf), &readbytes),
  4717. SSL_READ_EARLY_DATA_SUCCESS)
  4718. || !TEST_mem_eq(MSG1, readbytes, buf, strlen(MSG1))
  4719. || !TEST_int_eq(SSL_get_early_data_status(serverssl),
  4720. SSL_EARLY_DATA_ACCEPTED)
  4721. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4722. SSL_ERROR_NONE))
  4723. || !TEST_false(info_cb_failed))
  4724. goto end;
  4725. testresult = 1;
  4726. goto end;
  4727. }
  4728. #endif
  4729. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  4730. TLS_client_method(),
  4731. tlsvers, tlsvers, &sctx, &cctx, cert,
  4732. privkey)))
  4733. goto end;
  4734. /*
  4735. * For even numbered tests we check the server callbacks. For odd numbers we
  4736. * check the client.
  4737. */
  4738. SSL_CTX_set_info_callback((tst % 2) == 0 ? sctx : cctx,
  4739. sslapi_info_callback);
  4740. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  4741. &clientssl, NULL, NULL))
  4742. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4743. SSL_ERROR_NONE))
  4744. || !TEST_false(info_cb_failed))
  4745. goto end;
  4746. clntsess = SSL_get1_session(clientssl);
  4747. SSL_shutdown(clientssl);
  4748. SSL_shutdown(serverssl);
  4749. SSL_free(serverssl);
  4750. SSL_free(clientssl);
  4751. serverssl = clientssl = NULL;
  4752. /* Now do a resumption */
  4753. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  4754. NULL))
  4755. || !TEST_true(SSL_set_session(clientssl, clntsess))
  4756. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4757. SSL_ERROR_NONE))
  4758. || !TEST_true(SSL_session_reused(clientssl))
  4759. || !TEST_false(info_cb_failed))
  4760. goto end;
  4761. testresult = 1;
  4762. end:
  4763. SSL_free(serverssl);
  4764. SSL_free(clientssl);
  4765. SSL_SESSION_free(clntsess);
  4766. SSL_CTX_free(sctx);
  4767. SSL_CTX_free(cctx);
  4768. return testresult;
  4769. }
  4770. static int test_ssl_pending(int tst)
  4771. {
  4772. SSL_CTX *cctx = NULL, *sctx = NULL;
  4773. SSL *clientssl = NULL, *serverssl = NULL;
  4774. int testresult = 0;
  4775. char msg[] = "A test message";
  4776. char buf[5];
  4777. size_t written, readbytes;
  4778. if (tst == 0) {
  4779. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  4780. TLS_client_method(),
  4781. TLS1_VERSION, TLS_MAX_VERSION,
  4782. &sctx, &cctx, cert, privkey)))
  4783. goto end;
  4784. } else {
  4785. #ifndef OPENSSL_NO_DTLS
  4786. if (!TEST_true(create_ssl_ctx_pair(DTLS_server_method(),
  4787. DTLS_client_method(),
  4788. DTLS1_VERSION, DTLS_MAX_VERSION,
  4789. &sctx, &cctx, cert, privkey)))
  4790. goto end;
  4791. #else
  4792. return 1;
  4793. #endif
  4794. }
  4795. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4796. NULL, NULL))
  4797. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4798. SSL_ERROR_NONE)))
  4799. goto end;
  4800. if (!TEST_int_eq(SSL_pending(clientssl), 0)
  4801. || !TEST_false(SSL_has_pending(clientssl))
  4802. || !TEST_int_eq(SSL_pending(serverssl), 0)
  4803. || !TEST_false(SSL_has_pending(serverssl))
  4804. || !TEST_true(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  4805. || !TEST_size_t_eq(written, sizeof(msg))
  4806. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf), &readbytes))
  4807. || !TEST_size_t_eq(readbytes, sizeof(buf))
  4808. || !TEST_int_eq(SSL_pending(clientssl), (int)(written - readbytes))
  4809. || !TEST_true(SSL_has_pending(clientssl)))
  4810. goto end;
  4811. testresult = 1;
  4812. end:
  4813. SSL_free(serverssl);
  4814. SSL_free(clientssl);
  4815. SSL_CTX_free(sctx);
  4816. SSL_CTX_free(cctx);
  4817. return testresult;
  4818. }
  4819. static struct {
  4820. unsigned int maxprot;
  4821. const char *clntciphers;
  4822. const char *clnttls13ciphers;
  4823. const char *srvrciphers;
  4824. const char *srvrtls13ciphers;
  4825. const char *shared;
  4826. } shared_ciphers_data[] = {
  4827. /*
  4828. * We can't establish a connection (even in TLSv1.1) with these ciphersuites if
  4829. * TLSv1.3 is enabled but TLSv1.2 is disabled.
  4830. */
  4831. #if defined(OPENSSL_NO_TLS1_3) || !defined(OPENSSL_NO_TLS1_2)
  4832. {
  4833. TLS1_2_VERSION,
  4834. "AES128-SHA:AES256-SHA",
  4835. NULL,
  4836. "AES256-SHA:DHE-RSA-AES128-SHA",
  4837. NULL,
  4838. "AES256-SHA"
  4839. },
  4840. {
  4841. TLS1_2_VERSION,
  4842. "AES128-SHA:DHE-RSA-AES128-SHA:AES256-SHA",
  4843. NULL,
  4844. "AES128-SHA:DHE-RSA-AES256-SHA:AES256-SHA",
  4845. NULL,
  4846. "AES128-SHA:AES256-SHA"
  4847. },
  4848. {
  4849. TLS1_2_VERSION,
  4850. "AES128-SHA:AES256-SHA",
  4851. NULL,
  4852. "AES128-SHA:DHE-RSA-AES128-SHA",
  4853. NULL,
  4854. "AES128-SHA"
  4855. },
  4856. #endif
  4857. /*
  4858. * This test combines TLSv1.3 and TLSv1.2 ciphersuites so they must both be
  4859. * enabled.
  4860. */
  4861. #if !defined(OPENSSL_NO_TLS1_3) && !defined(OPENSSL_NO_TLS1_2) \
  4862. && !defined(OPENSSL_NO_CHACHA) && !defined(OPENSSL_NO_POLY1305)
  4863. {
  4864. TLS1_3_VERSION,
  4865. "AES128-SHA:AES256-SHA",
  4866. NULL,
  4867. "AES256-SHA:AES128-SHA256",
  4868. NULL,
  4869. "TLS_AES_256_GCM_SHA384:TLS_CHACHA20_POLY1305_SHA256:"
  4870. "TLS_AES_128_GCM_SHA256:AES256-SHA"
  4871. },
  4872. #endif
  4873. #ifndef OPENSSL_NO_TLS1_3
  4874. {
  4875. TLS1_3_VERSION,
  4876. "AES128-SHA",
  4877. "TLS_AES_256_GCM_SHA384",
  4878. "AES256-SHA",
  4879. "TLS_AES_256_GCM_SHA384",
  4880. "TLS_AES_256_GCM_SHA384"
  4881. },
  4882. #endif
  4883. };
  4884. static int test_ssl_get_shared_ciphers(int tst)
  4885. {
  4886. SSL_CTX *cctx = NULL, *sctx = NULL;
  4887. SSL *clientssl = NULL, *serverssl = NULL;
  4888. int testresult = 0;
  4889. char buf[1024];
  4890. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  4891. TLS_client_method(),
  4892. TLS1_VERSION,
  4893. shared_ciphers_data[tst].maxprot,
  4894. &sctx, &cctx, cert, privkey)))
  4895. goto end;
  4896. if (!TEST_true(SSL_CTX_set_cipher_list(cctx,
  4897. shared_ciphers_data[tst].clntciphers))
  4898. || (shared_ciphers_data[tst].clnttls13ciphers != NULL
  4899. && !TEST_true(SSL_CTX_set_ciphersuites(cctx,
  4900. shared_ciphers_data[tst].clnttls13ciphers)))
  4901. || !TEST_true(SSL_CTX_set_cipher_list(sctx,
  4902. shared_ciphers_data[tst].srvrciphers))
  4903. || (shared_ciphers_data[tst].srvrtls13ciphers != NULL
  4904. && !TEST_true(SSL_CTX_set_ciphersuites(sctx,
  4905. shared_ciphers_data[tst].srvrtls13ciphers))))
  4906. goto end;
  4907. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  4908. NULL, NULL))
  4909. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  4910. SSL_ERROR_NONE)))
  4911. goto end;
  4912. if (!TEST_ptr(SSL_get_shared_ciphers(serverssl, buf, sizeof(buf)))
  4913. || !TEST_int_eq(strcmp(buf, shared_ciphers_data[tst].shared), 0)) {
  4914. TEST_info("Shared ciphers are: %s\n", buf);
  4915. goto end;
  4916. }
  4917. testresult = 1;
  4918. end:
  4919. SSL_free(serverssl);
  4920. SSL_free(clientssl);
  4921. SSL_CTX_free(sctx);
  4922. SSL_CTX_free(cctx);
  4923. return testresult;
  4924. }
  4925. static const char *appdata = "Hello World";
  4926. static int gen_tick_called, dec_tick_called, tick_key_cb_called;
  4927. static int tick_key_renew = 0;
  4928. static SSL_TICKET_RETURN tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  4929. static int gen_tick_cb(SSL *s, void *arg)
  4930. {
  4931. gen_tick_called = 1;
  4932. return SSL_SESSION_set1_ticket_appdata(SSL_get_session(s), appdata,
  4933. strlen(appdata));
  4934. }
  4935. static SSL_TICKET_RETURN dec_tick_cb(SSL *s, SSL_SESSION *ss,
  4936. const unsigned char *keyname,
  4937. size_t keyname_length,
  4938. SSL_TICKET_STATUS status,
  4939. void *arg)
  4940. {
  4941. void *tickdata;
  4942. size_t tickdlen;
  4943. dec_tick_called = 1;
  4944. if (status == SSL_TICKET_EMPTY)
  4945. return SSL_TICKET_RETURN_IGNORE_RENEW;
  4946. if (!TEST_true(status == SSL_TICKET_SUCCESS
  4947. || status == SSL_TICKET_SUCCESS_RENEW))
  4948. return SSL_TICKET_RETURN_ABORT;
  4949. if (!TEST_true(SSL_SESSION_get0_ticket_appdata(ss, &tickdata,
  4950. &tickdlen))
  4951. || !TEST_size_t_eq(tickdlen, strlen(appdata))
  4952. || !TEST_int_eq(memcmp(tickdata, appdata, tickdlen), 0))
  4953. return SSL_TICKET_RETURN_ABORT;
  4954. if (tick_key_cb_called) {
  4955. /* Don't change what the ticket key callback wanted to do */
  4956. switch (status) {
  4957. case SSL_TICKET_NO_DECRYPT:
  4958. return SSL_TICKET_RETURN_IGNORE_RENEW;
  4959. case SSL_TICKET_SUCCESS:
  4960. return SSL_TICKET_RETURN_USE;
  4961. case SSL_TICKET_SUCCESS_RENEW:
  4962. return SSL_TICKET_RETURN_USE_RENEW;
  4963. default:
  4964. return SSL_TICKET_RETURN_ABORT;
  4965. }
  4966. }
  4967. return tick_dec_ret;
  4968. }
  4969. static int tick_key_cb(SSL *s, unsigned char key_name[16],
  4970. unsigned char iv[EVP_MAX_IV_LENGTH], EVP_CIPHER_CTX *ctx,
  4971. HMAC_CTX *hctx, int enc)
  4972. {
  4973. const unsigned char tick_aes_key[16] = "0123456789abcdef";
  4974. const unsigned char tick_hmac_key[16] = "0123456789abcdef";
  4975. tick_key_cb_called = 1;
  4976. memset(iv, 0, AES_BLOCK_SIZE);
  4977. memset(key_name, 0, 16);
  4978. if (!EVP_CipherInit_ex(ctx, EVP_aes_128_cbc(), NULL, tick_aes_key, iv, enc)
  4979. || !HMAC_Init_ex(hctx, tick_hmac_key, sizeof(tick_hmac_key),
  4980. EVP_sha256(), NULL))
  4981. return -1;
  4982. return tick_key_renew ? 2 : 1;
  4983. }
  4984. /*
  4985. * Test the various ticket callbacks
  4986. * Test 0: TLSv1.2, no ticket key callback, no ticket, no renewal
  4987. * Test 1: TLSv1.3, no ticket key callback, no ticket, no renewal
  4988. * Test 2: TLSv1.2, no ticket key callback, no ticket, renewal
  4989. * Test 3: TLSv1.3, no ticket key callback, no ticket, renewal
  4990. * Test 4: TLSv1.2, no ticket key callback, ticket, no renewal
  4991. * Test 5: TLSv1.3, no ticket key callback, ticket, no renewal
  4992. * Test 6: TLSv1.2, no ticket key callback, ticket, renewal
  4993. * Test 7: TLSv1.3, no ticket key callback, ticket, renewal
  4994. * Test 8: TLSv1.2, ticket key callback, ticket, no renewal
  4995. * Test 9: TLSv1.3, ticket key callback, ticket, no renewal
  4996. * Test 10: TLSv1.2, ticket key callback, ticket, renewal
  4997. * Test 11: TLSv1.3, ticket key callback, ticket, renewal
  4998. */
  4999. static int test_ticket_callbacks(int tst)
  5000. {
  5001. SSL_CTX *cctx = NULL, *sctx = NULL;
  5002. SSL *clientssl = NULL, *serverssl = NULL;
  5003. SSL_SESSION *clntsess = NULL;
  5004. int testresult = 0;
  5005. #ifdef OPENSSL_NO_TLS1_2
  5006. if (tst % 2 == 0)
  5007. return 1;
  5008. #endif
  5009. #ifdef OPENSSL_NO_TLS1_3
  5010. if (tst % 2 == 1)
  5011. return 1;
  5012. #endif
  5013. gen_tick_called = dec_tick_called = tick_key_cb_called = 0;
  5014. /* Which tests the ticket key callback should request renewal for */
  5015. if (tst == 10 || tst == 11)
  5016. tick_key_renew = 1;
  5017. else
  5018. tick_key_renew = 0;
  5019. /* Which tests the decrypt ticket callback should request renewal for */
  5020. switch (tst) {
  5021. case 0:
  5022. case 1:
  5023. tick_dec_ret = SSL_TICKET_RETURN_IGNORE;
  5024. break;
  5025. case 2:
  5026. case 3:
  5027. tick_dec_ret = SSL_TICKET_RETURN_IGNORE_RENEW;
  5028. break;
  5029. case 4:
  5030. case 5:
  5031. tick_dec_ret = SSL_TICKET_RETURN_USE;
  5032. break;
  5033. case 6:
  5034. case 7:
  5035. tick_dec_ret = SSL_TICKET_RETURN_USE_RENEW;
  5036. break;
  5037. default:
  5038. tick_dec_ret = SSL_TICKET_RETURN_ABORT;
  5039. }
  5040. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5041. TLS_client_method(),
  5042. TLS1_VERSION,
  5043. ((tst % 2) == 0) ? TLS1_2_VERSION
  5044. : TLS1_3_VERSION,
  5045. &sctx, &cctx, cert, privkey)))
  5046. goto end;
  5047. /*
  5048. * We only want sessions to resume from tickets - not the session cache. So
  5049. * switch the cache off.
  5050. */
  5051. if (!TEST_true(SSL_CTX_set_session_cache_mode(sctx, SSL_SESS_CACHE_OFF)))
  5052. goto end;
  5053. if (!TEST_true(SSL_CTX_set_session_ticket_cb(sctx, gen_tick_cb, dec_tick_cb,
  5054. NULL)))
  5055. goto end;
  5056. if (tst >= 8
  5057. && !TEST_true(SSL_CTX_set_tlsext_ticket_key_cb(sctx, tick_key_cb)))
  5058. goto end;
  5059. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5060. NULL, NULL))
  5061. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5062. SSL_ERROR_NONE)))
  5063. goto end;
  5064. /*
  5065. * The decrypt ticket key callback in TLSv1.2 should be called even though
  5066. * we have no ticket yet, because it gets called with a status of
  5067. * SSL_TICKET_EMPTY (the client indicates support for tickets but does not
  5068. * actually send any ticket data). This does not happen in TLSv1.3 because
  5069. * it is not valid to send empty ticket data in TLSv1.3.
  5070. */
  5071. if (!TEST_int_eq(gen_tick_called, 1)
  5072. || !TEST_int_eq(dec_tick_called, ((tst % 2) == 0) ? 1 : 0))
  5073. goto end;
  5074. gen_tick_called = dec_tick_called = 0;
  5075. clntsess = SSL_get1_session(clientssl);
  5076. SSL_shutdown(clientssl);
  5077. SSL_shutdown(serverssl);
  5078. SSL_free(serverssl);
  5079. SSL_free(clientssl);
  5080. serverssl = clientssl = NULL;
  5081. /* Now do a resumption */
  5082. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5083. NULL))
  5084. || !TEST_true(SSL_set_session(clientssl, clntsess))
  5085. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5086. SSL_ERROR_NONE)))
  5087. goto end;
  5088. if (tick_dec_ret == SSL_TICKET_RETURN_IGNORE
  5089. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW) {
  5090. if (!TEST_false(SSL_session_reused(clientssl)))
  5091. goto end;
  5092. } else {
  5093. if (!TEST_true(SSL_session_reused(clientssl)))
  5094. goto end;
  5095. }
  5096. if (!TEST_int_eq(gen_tick_called,
  5097. (tick_key_renew
  5098. || tick_dec_ret == SSL_TICKET_RETURN_IGNORE_RENEW
  5099. || tick_dec_ret == SSL_TICKET_RETURN_USE_RENEW)
  5100. ? 1 : 0)
  5101. || !TEST_int_eq(dec_tick_called, 1))
  5102. goto end;
  5103. testresult = 1;
  5104. end:
  5105. SSL_SESSION_free(clntsess);
  5106. SSL_free(serverssl);
  5107. SSL_free(clientssl);
  5108. SSL_CTX_free(sctx);
  5109. SSL_CTX_free(cctx);
  5110. return testresult;
  5111. }
  5112. /*
  5113. * Test bi-directional shutdown.
  5114. * Test 0: TLSv1.2
  5115. * Test 1: TLSv1.2, server continues to read/write after client shutdown
  5116. * Test 2: TLSv1.3, no pending NewSessionTicket messages
  5117. * Test 3: TLSv1.3, pending NewSessionTicket messages
  5118. * Test 4: TLSv1.3, server continues to read/write after client shutdown, server
  5119. * sends key update, client reads it
  5120. * Test 5: TLSv1.3, server continues to read/write after client shutdown, server
  5121. * sends CertificateRequest, client reads and ignores it
  5122. * Test 6: TLSv1.3, server continues to read/write after client shutdown, client
  5123. * doesn't read it
  5124. */
  5125. static int test_shutdown(int tst)
  5126. {
  5127. SSL_CTX *cctx = NULL, *sctx = NULL;
  5128. SSL *clientssl = NULL, *serverssl = NULL;
  5129. int testresult = 0;
  5130. char msg[] = "A test message";
  5131. char buf[80];
  5132. size_t written, readbytes;
  5133. SSL_SESSION *sess;
  5134. #ifdef OPENSSL_NO_TLS1_2
  5135. if (tst <= 1)
  5136. return 1;
  5137. #endif
  5138. #ifdef OPENSSL_NO_TLS1_3
  5139. if (tst >= 2)
  5140. return 1;
  5141. #endif
  5142. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5143. TLS_client_method(),
  5144. TLS1_VERSION,
  5145. (tst <= 1) ? TLS1_2_VERSION
  5146. : TLS1_3_VERSION,
  5147. &sctx, &cctx, cert, privkey)))
  5148. goto end;
  5149. if (tst == 5)
  5150. SSL_CTX_set_post_handshake_auth(cctx, 1);
  5151. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5152. NULL, NULL)))
  5153. goto end;
  5154. if (tst == 3) {
  5155. if (!TEST_true(create_bare_ssl_connection(serverssl, clientssl,
  5156. SSL_ERROR_NONE, 1))
  5157. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  5158. || !TEST_false(SSL_SESSION_is_resumable(sess)))
  5159. goto end;
  5160. } else if (!TEST_true(create_ssl_connection(serverssl, clientssl,
  5161. SSL_ERROR_NONE))
  5162. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  5163. || !TEST_true(SSL_SESSION_is_resumable(sess))) {
  5164. goto end;
  5165. }
  5166. if (!TEST_int_eq(SSL_shutdown(clientssl), 0))
  5167. goto end;
  5168. if (tst >= 4) {
  5169. /*
  5170. * Reading on the server after the client has sent close_notify should
  5171. * fail and provide SSL_ERROR_ZERO_RETURN
  5172. */
  5173. if (!TEST_false(SSL_read_ex(serverssl, buf, sizeof(buf), &readbytes))
  5174. || !TEST_int_eq(SSL_get_error(serverssl, 0),
  5175. SSL_ERROR_ZERO_RETURN)
  5176. || !TEST_int_eq(SSL_get_shutdown(serverssl),
  5177. SSL_RECEIVED_SHUTDOWN)
  5178. /*
  5179. * Even though we're shutdown on receive we should still be
  5180. * able to write.
  5181. */
  5182. || !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  5183. goto end;
  5184. if (tst == 4
  5185. && !TEST_true(SSL_key_update(serverssl,
  5186. SSL_KEY_UPDATE_REQUESTED)))
  5187. goto end;
  5188. if (tst == 5) {
  5189. SSL_set_verify(serverssl, SSL_VERIFY_PEER, NULL);
  5190. if (!TEST_true(SSL_verify_client_post_handshake(serverssl)))
  5191. goto end;
  5192. }
  5193. if ((tst == 4 || tst == 5)
  5194. && !TEST_true(SSL_write(serverssl, msg, sizeof(msg))))
  5195. goto end;
  5196. if (!TEST_int_eq(SSL_shutdown(serverssl), 1))
  5197. goto end;
  5198. if (tst == 4 || tst == 5) {
  5199. /* Should still be able to read data from server */
  5200. if (!TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  5201. &readbytes))
  5202. || !TEST_size_t_eq(readbytes, sizeof(msg))
  5203. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0)
  5204. || !TEST_true(SSL_read_ex(clientssl, buf, sizeof(buf),
  5205. &readbytes))
  5206. || !TEST_size_t_eq(readbytes, sizeof(msg))
  5207. || !TEST_int_eq(memcmp(msg, buf, readbytes), 0))
  5208. goto end;
  5209. }
  5210. }
  5211. /* Writing on the client after sending close_notify shouldn't be possible */
  5212. if (!TEST_false(SSL_write_ex(clientssl, msg, sizeof(msg), &written)))
  5213. goto end;
  5214. if (tst < 4) {
  5215. /*
  5216. * For these tests the client has sent close_notify but it has not yet
  5217. * been received by the server. The server has not sent close_notify
  5218. * yet.
  5219. */
  5220. if (!TEST_int_eq(SSL_shutdown(serverssl), 0)
  5221. /*
  5222. * Writing on the server after sending close_notify shouldn't
  5223. * be possible.
  5224. */
  5225. || !TEST_false(SSL_write_ex(serverssl, msg, sizeof(msg), &written))
  5226. || !TEST_int_eq(SSL_shutdown(clientssl), 1)
  5227. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  5228. || !TEST_true(SSL_SESSION_is_resumable(sess))
  5229. || !TEST_int_eq(SSL_shutdown(serverssl), 1))
  5230. goto end;
  5231. } else if (tst == 4 || tst == 5) {
  5232. /*
  5233. * In this test the client has sent close_notify and it has been
  5234. * received by the server which has responded with a close_notify. The
  5235. * client needs to read the close_notify sent by the server.
  5236. */
  5237. if (!TEST_int_eq(SSL_shutdown(clientssl), 1)
  5238. || !TEST_ptr_ne(sess = SSL_get_session(clientssl), NULL)
  5239. || !TEST_true(SSL_SESSION_is_resumable(sess)))
  5240. goto end;
  5241. } else {
  5242. /*
  5243. * tst == 6
  5244. *
  5245. * The client has sent close_notify and is expecting a close_notify
  5246. * back, but instead there is application data first. The shutdown
  5247. * should fail with a fatal error.
  5248. */
  5249. if (!TEST_int_eq(SSL_shutdown(clientssl), -1)
  5250. || !TEST_int_eq(SSL_get_error(clientssl, -1), SSL_ERROR_SSL))
  5251. goto end;
  5252. }
  5253. testresult = 1;
  5254. end:
  5255. SSL_free(serverssl);
  5256. SSL_free(clientssl);
  5257. SSL_CTX_free(sctx);
  5258. SSL_CTX_free(cctx);
  5259. return testresult;
  5260. }
  5261. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3)
  5262. static int cert_cb_cnt;
  5263. static int cert_cb(SSL *s, void *arg)
  5264. {
  5265. SSL_CTX *ctx = (SSL_CTX *)arg;
  5266. BIO *in = NULL;
  5267. EVP_PKEY *pkey = NULL;
  5268. X509 *x509 = NULL, *rootx = NULL;
  5269. STACK_OF(X509) *chain = NULL;
  5270. char *rootfile = NULL, *ecdsacert = NULL, *ecdsakey = NULL;
  5271. int ret = 0;
  5272. if (cert_cb_cnt == 0) {
  5273. /* Suspend the handshake */
  5274. cert_cb_cnt++;
  5275. return -1;
  5276. } else if (cert_cb_cnt == 1) {
  5277. /*
  5278. * Update the SSL_CTX, set the certificate and private key and then
  5279. * continue the handshake normally.
  5280. */
  5281. if (ctx != NULL && !TEST_ptr(SSL_set_SSL_CTX(s, ctx)))
  5282. return 0;
  5283. if (!TEST_true(SSL_use_certificate_file(s, cert, SSL_FILETYPE_PEM))
  5284. || !TEST_true(SSL_use_PrivateKey_file(s, privkey,
  5285. SSL_FILETYPE_PEM))
  5286. || !TEST_true(SSL_check_private_key(s)))
  5287. return 0;
  5288. cert_cb_cnt++;
  5289. return 1;
  5290. } else if (cert_cb_cnt == 3) {
  5291. int rv;
  5292. rootfile = test_mk_file_path(certsdir, "rootcert.pem");
  5293. ecdsacert = test_mk_file_path(certsdir, "server-ecdsa-cert.pem");
  5294. ecdsakey = test_mk_file_path(certsdir, "server-ecdsa-key.pem");
  5295. if (!TEST_ptr(rootfile) || !TEST_ptr(ecdsacert) || !TEST_ptr(ecdsakey))
  5296. goto out;
  5297. chain = sk_X509_new_null();
  5298. if (!TEST_ptr(chain))
  5299. goto out;
  5300. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  5301. || !TEST_int_ge(BIO_read_filename(in, rootfile), 0)
  5302. || !TEST_ptr(rootx = PEM_read_bio_X509(in, NULL, NULL, NULL))
  5303. || !TEST_true(sk_X509_push(chain, rootx)))
  5304. goto out;
  5305. rootx = NULL;
  5306. BIO_free(in);
  5307. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  5308. || !TEST_int_ge(BIO_read_filename(in, ecdsacert), 0)
  5309. || !TEST_ptr(x509 = PEM_read_bio_X509(in, NULL, NULL, NULL)))
  5310. goto out;
  5311. BIO_free(in);
  5312. if (!TEST_ptr(in = BIO_new(BIO_s_file()))
  5313. || !TEST_int_ge(BIO_read_filename(in, ecdsakey), 0)
  5314. || !TEST_ptr(pkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL)))
  5315. goto out;
  5316. rv = SSL_check_chain(s, x509, pkey, chain);
  5317. /*
  5318. * If the cert doesn't show as valid here (e.g., because we don't
  5319. * have any shared sigalgs), then we will not set it, and there will
  5320. * be no certificate at all on the SSL or SSL_CTX. This, in turn,
  5321. * will cause tls_choose_sigalgs() to fail the connection.
  5322. */
  5323. if ((rv & (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE))
  5324. == (CERT_PKEY_VALID | CERT_PKEY_CA_SIGNATURE)) {
  5325. if (!SSL_use_cert_and_key(s, x509, pkey, NULL, 1))
  5326. goto out;
  5327. }
  5328. ret = 1;
  5329. }
  5330. /* Abort the handshake */
  5331. out:
  5332. OPENSSL_free(ecdsacert);
  5333. OPENSSL_free(ecdsakey);
  5334. OPENSSL_free(rootfile);
  5335. BIO_free(in);
  5336. EVP_PKEY_free(pkey);
  5337. X509_free(x509);
  5338. X509_free(rootx);
  5339. sk_X509_pop_free(chain, X509_free);
  5340. return ret;
  5341. }
  5342. /*
  5343. * Test the certificate callback.
  5344. * Test 0: Callback fails
  5345. * Test 1: Success - no SSL_set_SSL_CTX() in the callback
  5346. * Test 2: Success - SSL_set_SSL_CTX() in the callback
  5347. * Test 3: Success - Call SSL_check_chain from the callback
  5348. * Test 4: Failure - SSL_check_chain fails from callback due to bad cert in the
  5349. * chain
  5350. * Test 5: Failure - SSL_check_chain fails from callback due to bad ee cert
  5351. */
  5352. static int test_cert_cb_int(int prot, int tst)
  5353. {
  5354. SSL_CTX *cctx = NULL, *sctx = NULL, *snictx = NULL;
  5355. SSL *clientssl = NULL, *serverssl = NULL;
  5356. int testresult = 0, ret;
  5357. #ifdef OPENSSL_NO_EC
  5358. /* We use an EC cert in these tests, so we skip in a no-ec build */
  5359. if (tst >= 3)
  5360. return 1;
  5361. #endif
  5362. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5363. TLS_client_method(),
  5364. TLS1_VERSION,
  5365. prot,
  5366. &sctx, &cctx, NULL, NULL)))
  5367. goto end;
  5368. if (tst == 0)
  5369. cert_cb_cnt = -1;
  5370. else if (tst >= 3)
  5371. cert_cb_cnt = 3;
  5372. else
  5373. cert_cb_cnt = 0;
  5374. if (tst == 2)
  5375. snictx = SSL_CTX_new(TLS_server_method());
  5376. SSL_CTX_set_cert_cb(sctx, cert_cb, snictx);
  5377. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5378. NULL, NULL)))
  5379. goto end;
  5380. if (tst == 4) {
  5381. /*
  5382. * We cause SSL_check_chain() to fail by specifying sig_algs that
  5383. * the chain doesn't meet (the root uses an RSA cert)
  5384. */
  5385. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  5386. "ecdsa_secp256r1_sha256")))
  5387. goto end;
  5388. } else if (tst == 5) {
  5389. /*
  5390. * We cause SSL_check_chain() to fail by specifying sig_algs that
  5391. * the ee cert doesn't meet (the ee uses an ECDSA cert)
  5392. */
  5393. if (!TEST_true(SSL_set1_sigalgs_list(clientssl,
  5394. "rsa_pss_rsae_sha256:rsa_pkcs1_sha256")))
  5395. goto end;
  5396. }
  5397. ret = create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE);
  5398. if (!TEST_true(tst == 0 || tst == 4 || tst == 5 ? !ret : ret)
  5399. || (tst > 0
  5400. && !TEST_int_eq((cert_cb_cnt - 2) * (cert_cb_cnt - 3), 0))) {
  5401. goto end;
  5402. }
  5403. testresult = 1;
  5404. end:
  5405. SSL_free(serverssl);
  5406. SSL_free(clientssl);
  5407. SSL_CTX_free(sctx);
  5408. SSL_CTX_free(cctx);
  5409. SSL_CTX_free(snictx);
  5410. return testresult;
  5411. }
  5412. #endif
  5413. static int test_cert_cb(int tst)
  5414. {
  5415. int testresult = 1;
  5416. #ifndef OPENSSL_NO_TLS1_2
  5417. testresult &= test_cert_cb_int(TLS1_2_VERSION, tst);
  5418. #endif
  5419. #ifndef OPENSSL_NO_TLS1_3
  5420. testresult &= test_cert_cb_int(TLS1_3_VERSION, tst);
  5421. #endif
  5422. return testresult;
  5423. }
  5424. static int client_cert_cb(SSL *ssl, X509 **x509, EVP_PKEY **pkey)
  5425. {
  5426. X509 *xcert, *peer;
  5427. EVP_PKEY *privpkey;
  5428. BIO *in = NULL;
  5429. /* Check that SSL_get_peer_certificate() returns something sensible */
  5430. peer = SSL_get_peer_certificate(ssl);
  5431. if (!TEST_ptr(peer))
  5432. return 0;
  5433. X509_free(peer);
  5434. in = BIO_new_file(cert, "r");
  5435. if (!TEST_ptr(in))
  5436. return 0;
  5437. xcert = PEM_read_bio_X509(in, NULL, NULL, NULL);
  5438. BIO_free(in);
  5439. if (!TEST_ptr(xcert))
  5440. return 0;
  5441. in = BIO_new_file(privkey, "r");
  5442. if (!TEST_ptr(in)) {
  5443. X509_free(xcert);
  5444. return 0;
  5445. }
  5446. privpkey = PEM_read_bio_PrivateKey(in, NULL, NULL, NULL);
  5447. BIO_free(in);
  5448. if (!TEST_ptr(privpkey)) {
  5449. X509_free(xcert);
  5450. return 0;
  5451. }
  5452. *x509 = xcert;
  5453. *pkey = privpkey;
  5454. return 1;
  5455. }
  5456. static int test_client_cert_cb(int tst)
  5457. {
  5458. SSL_CTX *cctx = NULL, *sctx = NULL;
  5459. SSL *clientssl = NULL, *serverssl = NULL;
  5460. int testresult = 0;
  5461. #ifdef OPENSSL_NO_TLS1_2
  5462. if (tst == 0)
  5463. return 1;
  5464. #endif
  5465. #ifdef OPENSSL_NO_TLS1_3
  5466. if (tst == 1)
  5467. return 1;
  5468. #endif
  5469. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5470. TLS_client_method(),
  5471. TLS1_VERSION,
  5472. tst == 0 ? TLS1_2_VERSION
  5473. : TLS1_3_VERSION,
  5474. &sctx, &cctx, cert, privkey)))
  5475. goto end;
  5476. /*
  5477. * Test that setting a client_cert_cb results in a client certificate being
  5478. * sent.
  5479. */
  5480. SSL_CTX_set_client_cert_cb(cctx, client_cert_cb);
  5481. SSL_CTX_set_verify(sctx,
  5482. SSL_VERIFY_PEER | SSL_VERIFY_FAIL_IF_NO_PEER_CERT,
  5483. verify_cb);
  5484. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5485. NULL, NULL))
  5486. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5487. SSL_ERROR_NONE)))
  5488. goto end;
  5489. testresult = 1;
  5490. end:
  5491. SSL_free(serverssl);
  5492. SSL_free(clientssl);
  5493. SSL_CTX_free(sctx);
  5494. SSL_CTX_free(cctx);
  5495. return testresult;
  5496. }
  5497. #if !defined(OPENSSL_NO_TLS1_2) || !defined(OPENSSL_NO_TLS1_3)
  5498. /*
  5499. * Test setting certificate authorities on both client and server.
  5500. *
  5501. * Test 0: SSL_CTX_set0_CA_list() only
  5502. * Test 1: Both SSL_CTX_set0_CA_list() and SSL_CTX_set_client_CA_list()
  5503. * Test 2: Only SSL_CTX_set_client_CA_list()
  5504. */
  5505. static int test_ca_names_int(int prot, int tst)
  5506. {
  5507. SSL_CTX *cctx = NULL, *sctx = NULL;
  5508. SSL *clientssl = NULL, *serverssl = NULL;
  5509. int testresult = 0;
  5510. size_t i;
  5511. X509_NAME *name[] = { NULL, NULL, NULL, NULL };
  5512. char *strnames[] = { "Jack", "Jill", "John", "Joanne" };
  5513. STACK_OF(X509_NAME) *sk1 = NULL, *sk2 = NULL;
  5514. const STACK_OF(X509_NAME) *sktmp = NULL;
  5515. for (i = 0; i < OSSL_NELEM(name); i++) {
  5516. name[i] = X509_NAME_new();
  5517. if (!TEST_ptr(name[i])
  5518. || !TEST_true(X509_NAME_add_entry_by_txt(name[i], "CN",
  5519. MBSTRING_ASC,
  5520. (unsigned char *)
  5521. strnames[i],
  5522. -1, -1, 0)))
  5523. goto end;
  5524. }
  5525. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5526. TLS_client_method(),
  5527. TLS1_VERSION,
  5528. prot,
  5529. &sctx, &cctx, cert, privkey)))
  5530. goto end;
  5531. SSL_CTX_set_verify(sctx, SSL_VERIFY_PEER, NULL);
  5532. if (tst == 0 || tst == 1) {
  5533. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  5534. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[0])))
  5535. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[1])))
  5536. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  5537. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[0])))
  5538. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[1]))))
  5539. goto end;
  5540. SSL_CTX_set0_CA_list(sctx, sk1);
  5541. SSL_CTX_set0_CA_list(cctx, sk2);
  5542. sk1 = sk2 = NULL;
  5543. }
  5544. if (tst == 1 || tst == 2) {
  5545. if (!TEST_ptr(sk1 = sk_X509_NAME_new_null())
  5546. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[2])))
  5547. || !TEST_true(sk_X509_NAME_push(sk1, X509_NAME_dup(name[3])))
  5548. || !TEST_ptr(sk2 = sk_X509_NAME_new_null())
  5549. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[2])))
  5550. || !TEST_true(sk_X509_NAME_push(sk2, X509_NAME_dup(name[3]))))
  5551. goto end;
  5552. SSL_CTX_set_client_CA_list(sctx, sk1);
  5553. SSL_CTX_set_client_CA_list(cctx, sk2);
  5554. sk1 = sk2 = NULL;
  5555. }
  5556. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5557. NULL, NULL))
  5558. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5559. SSL_ERROR_NONE)))
  5560. goto end;
  5561. /*
  5562. * We only expect certificate authorities to have been sent to the server
  5563. * if we are using TLSv1.3 and SSL_set0_CA_list() was used
  5564. */
  5565. sktmp = SSL_get0_peer_CA_list(serverssl);
  5566. if (prot == TLS1_3_VERSION
  5567. && (tst == 0 || tst == 1)) {
  5568. if (!TEST_ptr(sktmp)
  5569. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  5570. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  5571. name[0]), 0)
  5572. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  5573. name[1]), 0))
  5574. goto end;
  5575. } else if (!TEST_ptr_null(sktmp)) {
  5576. goto end;
  5577. }
  5578. /*
  5579. * In all tests we expect certificate authorities to have been sent to the
  5580. * client. However, SSL_set_client_CA_list() should override
  5581. * SSL_set0_CA_list()
  5582. */
  5583. sktmp = SSL_get0_peer_CA_list(clientssl);
  5584. if (!TEST_ptr(sktmp)
  5585. || !TEST_int_eq(sk_X509_NAME_num(sktmp), 2)
  5586. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 0),
  5587. name[tst == 0 ? 0 : 2]), 0)
  5588. || !TEST_int_eq(X509_NAME_cmp(sk_X509_NAME_value(sktmp, 1),
  5589. name[tst == 0 ? 1 : 3]), 0))
  5590. goto end;
  5591. testresult = 1;
  5592. end:
  5593. SSL_free(serverssl);
  5594. SSL_free(clientssl);
  5595. SSL_CTX_free(sctx);
  5596. SSL_CTX_free(cctx);
  5597. for (i = 0; i < OSSL_NELEM(name); i++)
  5598. X509_NAME_free(name[i]);
  5599. sk_X509_NAME_pop_free(sk1, X509_NAME_free);
  5600. sk_X509_NAME_pop_free(sk2, X509_NAME_free);
  5601. return testresult;
  5602. }
  5603. #endif
  5604. static int test_ca_names(int tst)
  5605. {
  5606. int testresult = 1;
  5607. #ifndef OPENSSL_NO_TLS1_2
  5608. testresult &= test_ca_names_int(TLS1_2_VERSION, tst);
  5609. #endif
  5610. #ifndef OPENSSL_NO_TLS1_3
  5611. testresult &= test_ca_names_int(TLS1_3_VERSION, tst);
  5612. #endif
  5613. return testresult;
  5614. }
  5615. /*
  5616. * Test 0: Client sets servername and server acknowledges it (TLSv1.2)
  5617. * Test 1: Client sets servername and server does not acknowledge it (TLSv1.2)
  5618. * Test 2: Client sets inconsistent servername on resumption (TLSv1.2)
  5619. * Test 3: Client does not set servername on initial handshake (TLSv1.2)
  5620. * Test 4: Client does not set servername on resumption handshake (TLSv1.2)
  5621. * Test 5: Client sets servername and server acknowledges it (TLSv1.3)
  5622. * Test 6: Client sets servername and server does not acknowledge it (TLSv1.3)
  5623. * Test 7: Client sets inconsistent servername on resumption (TLSv1.3)
  5624. * Test 8: Client does not set servername on initial handshake(TLSv1.3)
  5625. * Test 9: Client does not set servername on resumption handshake (TLSv1.3)
  5626. */
  5627. static int test_servername(int tst)
  5628. {
  5629. SSL_CTX *cctx = NULL, *sctx = NULL;
  5630. SSL *clientssl = NULL, *serverssl = NULL;
  5631. int testresult = 0;
  5632. SSL_SESSION *sess = NULL;
  5633. const char *sexpectedhost = NULL, *cexpectedhost = NULL;
  5634. #ifdef OPENSSL_NO_TLS1_2
  5635. if (tst <= 4)
  5636. return 1;
  5637. #endif
  5638. #ifdef OPENSSL_NO_TLS1_3
  5639. if (tst >= 5)
  5640. return 1;
  5641. #endif
  5642. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5643. TLS_client_method(),
  5644. TLS1_VERSION,
  5645. (tst <= 4) ? TLS1_2_VERSION
  5646. : TLS1_3_VERSION,
  5647. &sctx, &cctx, cert, privkey))
  5648. || !TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5649. NULL, NULL)))
  5650. goto end;
  5651. if (tst != 1 && tst != 6) {
  5652. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx,
  5653. hostname_cb)))
  5654. goto end;
  5655. }
  5656. if (tst != 3 && tst != 8) {
  5657. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  5658. goto end;
  5659. sexpectedhost = cexpectedhost = "goodhost";
  5660. }
  5661. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  5662. goto end;
  5663. if (!TEST_str_eq(SSL_get_servername(clientssl, TLSEXT_NAMETYPE_host_name),
  5664. cexpectedhost)
  5665. || !TEST_str_eq(SSL_get_servername(serverssl,
  5666. TLSEXT_NAMETYPE_host_name),
  5667. sexpectedhost))
  5668. goto end;
  5669. /* Now repeat with a resumption handshake */
  5670. if (!TEST_int_eq(SSL_shutdown(clientssl), 0)
  5671. || !TEST_ptr_ne(sess = SSL_get1_session(clientssl), NULL)
  5672. || !TEST_true(SSL_SESSION_is_resumable(sess))
  5673. || !TEST_int_eq(SSL_shutdown(serverssl), 0))
  5674. goto end;
  5675. SSL_free(clientssl);
  5676. SSL_free(serverssl);
  5677. clientssl = serverssl = NULL;
  5678. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl, NULL,
  5679. NULL)))
  5680. goto end;
  5681. if (!TEST_true(SSL_set_session(clientssl, sess)))
  5682. goto end;
  5683. sexpectedhost = cexpectedhost = "goodhost";
  5684. if (tst == 2 || tst == 7) {
  5685. /* Set an inconsistent hostname */
  5686. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "altgoodhost")))
  5687. goto end;
  5688. /*
  5689. * In TLSv1.2 we expect the hostname from the original handshake, in
  5690. * TLSv1.3 we expect the hostname from this handshake
  5691. */
  5692. if (tst == 7)
  5693. sexpectedhost = cexpectedhost = "altgoodhost";
  5694. if (!TEST_str_eq(SSL_get_servername(clientssl,
  5695. TLSEXT_NAMETYPE_host_name),
  5696. "altgoodhost"))
  5697. goto end;
  5698. } else if (tst == 4 || tst == 9) {
  5699. /*
  5700. * A TLSv1.3 session does not associate a session with a servername,
  5701. * but a TLSv1.2 session does.
  5702. */
  5703. if (tst == 9)
  5704. sexpectedhost = cexpectedhost = NULL;
  5705. if (!TEST_str_eq(SSL_get_servername(clientssl,
  5706. TLSEXT_NAMETYPE_host_name),
  5707. cexpectedhost))
  5708. goto end;
  5709. } else {
  5710. if (!TEST_true(SSL_set_tlsext_host_name(clientssl, "goodhost")))
  5711. goto end;
  5712. /*
  5713. * In a TLSv1.2 resumption where the hostname was not acknowledged
  5714. * we expect the hostname on the server to be empty. On the client we
  5715. * return what was requested in this case.
  5716. *
  5717. * Similarly if the client didn't set a hostname on an original TLSv1.2
  5718. * session but is now, the server hostname will be empty, but the client
  5719. * is as we set it.
  5720. */
  5721. if (tst == 1 || tst == 3)
  5722. sexpectedhost = NULL;
  5723. if (!TEST_str_eq(SSL_get_servername(clientssl,
  5724. TLSEXT_NAMETYPE_host_name),
  5725. "goodhost"))
  5726. goto end;
  5727. }
  5728. if (!TEST_true(create_ssl_connection(serverssl, clientssl, SSL_ERROR_NONE)))
  5729. goto end;
  5730. if (!TEST_true(SSL_session_reused(clientssl))
  5731. || !TEST_true(SSL_session_reused(serverssl))
  5732. || !TEST_str_eq(SSL_get_servername(clientssl,
  5733. TLSEXT_NAMETYPE_host_name),
  5734. cexpectedhost)
  5735. || !TEST_str_eq(SSL_get_servername(serverssl,
  5736. TLSEXT_NAMETYPE_host_name),
  5737. sexpectedhost))
  5738. goto end;
  5739. testresult = 1;
  5740. end:
  5741. SSL_SESSION_free(sess);
  5742. SSL_free(serverssl);
  5743. SSL_free(clientssl);
  5744. SSL_CTX_free(sctx);
  5745. SSL_CTX_free(cctx);
  5746. return testresult;
  5747. }
  5748. #ifndef OPENSSL_NO_TLS1_2
  5749. static int test_ssl_dup(void)
  5750. {
  5751. SSL_CTX *cctx = NULL, *sctx = NULL;
  5752. SSL *clientssl = NULL, *serverssl = NULL, *client2ssl = NULL;
  5753. int testresult = 0;
  5754. BIO *rbio = NULL, *wbio = NULL;
  5755. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(),
  5756. TLS_client_method(),
  5757. 0,
  5758. 0,
  5759. &sctx, &cctx, cert, privkey)))
  5760. goto end;
  5761. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl, &clientssl,
  5762. NULL, NULL)))
  5763. goto end;
  5764. if (!TEST_true(SSL_set_min_proto_version(clientssl, TLS1_2_VERSION))
  5765. || !TEST_true(SSL_set_max_proto_version(clientssl, TLS1_2_VERSION)))
  5766. goto end;
  5767. client2ssl = SSL_dup(clientssl);
  5768. rbio = SSL_get_rbio(clientssl);
  5769. if (!TEST_ptr(rbio)
  5770. || !TEST_true(BIO_up_ref(rbio)))
  5771. goto end;
  5772. SSL_set0_rbio(client2ssl, rbio);
  5773. rbio = NULL;
  5774. wbio = SSL_get_wbio(clientssl);
  5775. if (!TEST_ptr(wbio) || !TEST_true(BIO_up_ref(wbio)))
  5776. goto end;
  5777. SSL_set0_wbio(client2ssl, wbio);
  5778. rbio = NULL;
  5779. if (!TEST_ptr(client2ssl)
  5780. /* Handshake not started so pointers should be different */
  5781. || !TEST_ptr_ne(clientssl, client2ssl))
  5782. goto end;
  5783. if (!TEST_int_eq(SSL_get_min_proto_version(client2ssl), TLS1_2_VERSION)
  5784. || !TEST_int_eq(SSL_get_max_proto_version(client2ssl), TLS1_2_VERSION))
  5785. goto end;
  5786. if (!TEST_true(create_ssl_connection(serverssl, client2ssl, SSL_ERROR_NONE)))
  5787. goto end;
  5788. SSL_free(clientssl);
  5789. clientssl = SSL_dup(client2ssl);
  5790. if (!TEST_ptr(clientssl)
  5791. /* Handshake has finished so pointers should be the same */
  5792. || !TEST_ptr_eq(clientssl, client2ssl))
  5793. goto end;
  5794. testresult = 1;
  5795. end:
  5796. SSL_free(serverssl);
  5797. SSL_free(clientssl);
  5798. SSL_free(client2ssl);
  5799. SSL_CTX_free(sctx);
  5800. SSL_CTX_free(cctx);
  5801. return testresult;
  5802. }
  5803. #endif
  5804. #ifndef OPENSSL_NO_TLS1_3
  5805. /*
  5806. * Test that setting an SNI callback works with TLSv1.3. Specifically we check
  5807. * that it works even without a certificate configured for the original
  5808. * SSL_CTX
  5809. */
  5810. static int test_sni_tls13(void)
  5811. {
  5812. SSL_CTX *cctx = NULL, *sctx = NULL, *sctx2 = NULL;
  5813. SSL *clientssl = NULL, *serverssl = NULL;
  5814. int testresult = 0;
  5815. /* Reset callback counter */
  5816. snicb = 0;
  5817. /* Create an initial SSL_CTX with no certificate configured */
  5818. sctx = SSL_CTX_new(TLS_server_method());
  5819. if (!TEST_ptr(sctx))
  5820. goto end;
  5821. /* Require TLSv1.3 as a minimum */
  5822. if (!TEST_true(create_ssl_ctx_pair(TLS_server_method(), TLS_client_method(),
  5823. TLS1_3_VERSION, 0, &sctx2, &cctx, cert,
  5824. privkey)))
  5825. goto end;
  5826. /* Set up SNI */
  5827. if (!TEST_true(SSL_CTX_set_tlsext_servername_callback(sctx, sni_cb))
  5828. || !TEST_true(SSL_CTX_set_tlsext_servername_arg(sctx, sctx2)))
  5829. goto end;
  5830. /*
  5831. * Connection should still succeed because the final SSL_CTX has the right
  5832. * certificates configured.
  5833. */
  5834. if (!TEST_true(create_ssl_objects(sctx, cctx, &serverssl,
  5835. &clientssl, NULL, NULL))
  5836. || !TEST_true(create_ssl_connection(serverssl, clientssl,
  5837. SSL_ERROR_NONE)))
  5838. goto end;
  5839. /* We should have had the SNI callback called exactly once */
  5840. if (!TEST_int_eq(snicb, 1))
  5841. goto end;
  5842. testresult = 1;
  5843. end:
  5844. SSL_free(serverssl);
  5845. SSL_free(clientssl);
  5846. SSL_CTX_free(sctx2);
  5847. SSL_CTX_free(sctx);
  5848. SSL_CTX_free(cctx);
  5849. return testresult;
  5850. }
  5851. #endif
  5852. /*
  5853. * Test that setting an ALPN does not violate RFC
  5854. */
  5855. static int test_set_alpn(void)
  5856. {
  5857. SSL_CTX *ctx = NULL;
  5858. SSL *ssl = NULL;
  5859. int testresult = 0;
  5860. unsigned char bad0[] = { 0x00, 'b', 'a', 'd' };
  5861. unsigned char good[] = { 0x04, 'g', 'o', 'o', 'd' };
  5862. unsigned char bad1[] = { 0x01, 'b', 'a', 'd' };
  5863. unsigned char bad2[] = { 0x03, 'b', 'a', 'd', 0x00};
  5864. unsigned char bad3[] = { 0x03, 'b', 'a', 'd', 0x01, 'b', 'a', 'd'};
  5865. unsigned char bad4[] = { 0x03, 'b', 'a', 'd', 0x06, 'b', 'a', 'd'};
  5866. /* Create an initial SSL_CTX with no certificate configured */
  5867. ctx = SSL_CTX_new(TLS_server_method());
  5868. if (!TEST_ptr(ctx))
  5869. goto end;
  5870. /* the set_alpn functions return 0 (false) on success, non-zero (true) on failure */
  5871. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, NULL, 2)))
  5872. goto end;
  5873. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, 0)))
  5874. goto end;
  5875. if (!TEST_false(SSL_CTX_set_alpn_protos(ctx, good, sizeof(good))))
  5876. goto end;
  5877. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, good, 1)))
  5878. goto end;
  5879. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad0, sizeof(bad0))))
  5880. goto end;
  5881. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad1, sizeof(bad1))))
  5882. goto end;
  5883. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad2, sizeof(bad2))))
  5884. goto end;
  5885. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad3, sizeof(bad3))))
  5886. goto end;
  5887. if (!TEST_true(SSL_CTX_set_alpn_protos(ctx, bad4, sizeof(bad4))))
  5888. goto end;
  5889. ssl = SSL_new(ctx);
  5890. if (!TEST_ptr(ssl))
  5891. goto end;
  5892. if (!TEST_false(SSL_set_alpn_protos(ssl, NULL, 2)))
  5893. goto end;
  5894. if (!TEST_false(SSL_set_alpn_protos(ssl, good, 0)))
  5895. goto end;
  5896. if (!TEST_false(SSL_set_alpn_protos(ssl, good, sizeof(good))))
  5897. goto end;
  5898. if (!TEST_true(SSL_set_alpn_protos(ssl, good, 1)))
  5899. goto end;
  5900. if (!TEST_true(SSL_set_alpn_protos(ssl, bad0, sizeof(bad0))))
  5901. goto end;
  5902. if (!TEST_true(SSL_set_alpn_protos(ssl, bad1, sizeof(bad1))))
  5903. goto end;
  5904. if (!TEST_true(SSL_set_alpn_protos(ssl, bad2, sizeof(bad2))))
  5905. goto end;
  5906. if (!TEST_true(SSL_set_alpn_protos(ssl, bad3, sizeof(bad3))))
  5907. goto end;
  5908. if (!TEST_true(SSL_set_alpn_protos(ssl, bad4, sizeof(bad4))))
  5909. goto end;
  5910. testresult = 1;
  5911. end:
  5912. SSL_free(ssl);
  5913. SSL_CTX_free(ctx);
  5914. return testresult;
  5915. }
  5916. static int test_inherit_verify_param(void)
  5917. {
  5918. int testresult = 0;
  5919. SSL_CTX *ctx = NULL;
  5920. X509_VERIFY_PARAM *cp = NULL;
  5921. SSL *ssl = NULL;
  5922. X509_VERIFY_PARAM *sp = NULL;
  5923. int hostflags = X509_CHECK_FLAG_NEVER_CHECK_SUBJECT;
  5924. ctx = SSL_CTX_new(TLS_server_method());
  5925. if (!TEST_ptr(ctx))
  5926. goto end;
  5927. cp = SSL_CTX_get0_param(ctx);
  5928. if (!TEST_ptr(cp))
  5929. goto end;
  5930. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(cp), 0))
  5931. goto end;
  5932. X509_VERIFY_PARAM_set_hostflags(cp, hostflags);
  5933. ssl = SSL_new(ctx);
  5934. if (!TEST_ptr(ssl))
  5935. goto end;
  5936. sp = SSL_get0_param(ssl);
  5937. if (!TEST_ptr(sp))
  5938. goto end;
  5939. if (!TEST_int_eq(X509_VERIFY_PARAM_get_hostflags(sp), hostflags))
  5940. goto end;
  5941. testresult = 1;
  5942. end:
  5943. SSL_free(ssl);
  5944. SSL_CTX_free(ctx);
  5945. return testresult;
  5946. }
  5947. int setup_tests(void)
  5948. {
  5949. if (!TEST_ptr(certsdir = test_get_argument(0))
  5950. || !TEST_ptr(srpvfile = test_get_argument(1))
  5951. || !TEST_ptr(tmpfilename = test_get_argument(2)))
  5952. return 0;
  5953. if (getenv("OPENSSL_TEST_GETCOUNTS") != NULL) {
  5954. #ifdef OPENSSL_NO_CRYPTO_MDEBUG
  5955. TEST_error("not supported in this build");
  5956. return 0;
  5957. #else
  5958. int i, mcount, rcount, fcount;
  5959. for (i = 0; i < 4; i++)
  5960. test_export_key_mat(i);
  5961. CRYPTO_get_alloc_counts(&mcount, &rcount, &fcount);
  5962. test_printf_stdout("malloc %d realloc %d free %d\n",
  5963. mcount, rcount, fcount);
  5964. return 1;
  5965. #endif
  5966. }
  5967. cert = test_mk_file_path(certsdir, "servercert.pem");
  5968. if (cert == NULL)
  5969. return 0;
  5970. privkey = test_mk_file_path(certsdir, "serverkey.pem");
  5971. if (privkey == NULL) {
  5972. OPENSSL_free(cert);
  5973. return 0;
  5974. }
  5975. ADD_TEST(test_large_message_tls);
  5976. ADD_TEST(test_large_message_tls_read_ahead);
  5977. #ifndef OPENSSL_NO_DTLS
  5978. ADD_TEST(test_large_message_dtls);
  5979. #endif
  5980. #ifndef OPENSSL_NO_OCSP
  5981. ADD_TEST(test_tlsext_status_type);
  5982. #endif
  5983. ADD_TEST(test_session_with_only_int_cache);
  5984. ADD_TEST(test_session_with_only_ext_cache);
  5985. ADD_TEST(test_session_with_both_cache);
  5986. #ifndef OPENSSL_NO_TLS1_3
  5987. ADD_ALL_TESTS(test_stateful_tickets, 3);
  5988. ADD_ALL_TESTS(test_stateless_tickets, 3);
  5989. ADD_TEST(test_psk_tickets);
  5990. #endif
  5991. ADD_ALL_TESTS(test_ssl_set_bio, TOTAL_SSL_SET_BIO_TESTS);
  5992. ADD_TEST(test_ssl_bio_pop_next_bio);
  5993. ADD_TEST(test_ssl_bio_pop_ssl_bio);
  5994. ADD_TEST(test_ssl_bio_change_rbio);
  5995. ADD_TEST(test_ssl_bio_change_wbio);
  5996. #if !defined(OPENSSL_NO_TLS1_2) || defined(OPENSSL_NO_TLS1_3)
  5997. ADD_ALL_TESTS(test_set_sigalgs, OSSL_NELEM(testsigalgs) * 2);
  5998. ADD_TEST(test_keylog);
  5999. #endif
  6000. #ifndef OPENSSL_NO_TLS1_3
  6001. ADD_TEST(test_keylog_no_master_key);
  6002. #endif
  6003. #ifndef OPENSSL_NO_TLS1_2
  6004. ADD_TEST(test_client_hello_cb);
  6005. ADD_TEST(test_ccs_change_cipher);
  6006. #endif
  6007. #ifndef OPENSSL_NO_TLS1_3
  6008. ADD_ALL_TESTS(test_early_data_read_write, 3);
  6009. /*
  6010. * We don't do replay tests for external PSK. Replay protection isn't used
  6011. * in that scenario.
  6012. */
  6013. ADD_ALL_TESTS(test_early_data_replay, 2);
  6014. ADD_ALL_TESTS(test_early_data_skip, 3);
  6015. ADD_ALL_TESTS(test_early_data_skip_hrr, 3);
  6016. ADD_ALL_TESTS(test_early_data_skip_hrr_fail, 3);
  6017. ADD_ALL_TESTS(test_early_data_skip_abort, 3);
  6018. ADD_ALL_TESTS(test_early_data_not_sent, 3);
  6019. ADD_ALL_TESTS(test_early_data_psk, 8);
  6020. ADD_ALL_TESTS(test_early_data_psk_with_all_ciphers, 5);
  6021. ADD_ALL_TESTS(test_early_data_not_expected, 3);
  6022. # ifndef OPENSSL_NO_TLS1_2
  6023. ADD_ALL_TESTS(test_early_data_tls1_2, 3);
  6024. # endif
  6025. #endif
  6026. #ifndef OPENSSL_NO_TLS1_3
  6027. ADD_ALL_TESTS(test_set_ciphersuite, 10);
  6028. ADD_TEST(test_ciphersuite_change);
  6029. ADD_ALL_TESTS(test_tls13_ciphersuite, 4);
  6030. #ifdef OPENSSL_NO_PSK
  6031. ADD_ALL_TESTS(test_tls13_psk, 1);
  6032. #else
  6033. ADD_ALL_TESTS(test_tls13_psk, 4);
  6034. #endif /* OPENSSL_NO_PSK */
  6035. ADD_ALL_TESTS(test_custom_exts, 6);
  6036. ADD_TEST(test_stateless);
  6037. ADD_TEST(test_pha_key_update);
  6038. #else
  6039. ADD_ALL_TESTS(test_custom_exts, 3);
  6040. #endif
  6041. ADD_ALL_TESTS(test_serverinfo, 8);
  6042. ADD_ALL_TESTS(test_export_key_mat, 6);
  6043. #ifndef OPENSSL_NO_TLS1_3
  6044. ADD_ALL_TESTS(test_export_key_mat_early, 3);
  6045. ADD_TEST(test_key_update);
  6046. ADD_ALL_TESTS(test_key_update_in_write, 2);
  6047. #endif
  6048. ADD_ALL_TESTS(test_ssl_clear, 2);
  6049. ADD_ALL_TESTS(test_max_fragment_len_ext, OSSL_NELEM(max_fragment_len_test));
  6050. #if !defined(OPENSSL_NO_SRP) && !defined(OPENSSL_NO_TLS1_2)
  6051. ADD_ALL_TESTS(test_srp, 6);
  6052. #endif
  6053. ADD_ALL_TESTS(test_info_callback, 6);
  6054. ADD_ALL_TESTS(test_ssl_pending, 2);
  6055. ADD_ALL_TESTS(test_ssl_get_shared_ciphers, OSSL_NELEM(shared_ciphers_data));
  6056. ADD_ALL_TESTS(test_ticket_callbacks, 12);
  6057. ADD_ALL_TESTS(test_shutdown, 7);
  6058. ADD_ALL_TESTS(test_cert_cb, 6);
  6059. ADD_ALL_TESTS(test_client_cert_cb, 2);
  6060. ADD_ALL_TESTS(test_ca_names, 3);
  6061. ADD_ALL_TESTS(test_servername, 10);
  6062. #ifndef OPENSSL_NO_TLS1_2
  6063. ADD_TEST(test_ssl_dup);
  6064. #endif
  6065. #ifndef OPENSSL_NO_TLS1_3
  6066. ADD_TEST(test_sni_tls13);
  6067. #endif
  6068. ADD_TEST(test_set_alpn);
  6069. ADD_TEST(test_inherit_verify_param);
  6070. return 1;
  6071. }
  6072. void cleanup_tests(void)
  6073. {
  6074. OPENSSL_free(cert);
  6075. OPENSSL_free(privkey);
  6076. bio_s_mempacket_test_free();
  6077. bio_s_always_retry_free();
  6078. }