sslapitest.c 148 KB

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