sslapitest.c 167 KB

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