sslapitest.c 186 KB

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