sslapitest.c 155 KB

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