ssl_server2.c 142 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093
  1. /*
  2. * SSL client with options
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. */
  19. #define MBEDTLS_ALLOW_PRIVATE_ACCESS
  20. #include "ssl_test_lib.h"
  21. #if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
  22. int main( void )
  23. {
  24. mbedtls_printf( MBEDTLS_SSL_TEST_IMPOSSIBLE );
  25. mbedtls_exit( 0 );
  26. }
  27. #elif !defined(MBEDTLS_SSL_SRV_C)
  28. int main( void )
  29. {
  30. mbedtls_printf( "MBEDTLS_SSL_SRV_C not defined.\n" );
  31. mbedtls_exit( 0 );
  32. }
  33. #else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */
  34. #include <stdint.h>
  35. #if !defined(_MSC_VER)
  36. #include <inttypes.h>
  37. #endif
  38. #if !defined(_WIN32)
  39. #include <signal.h>
  40. #endif
  41. #if defined(MBEDTLS_SSL_CACHE_C)
  42. #include "mbedtls/ssl_cache.h"
  43. #endif
  44. #if defined(MBEDTLS_SSL_TICKET_C)
  45. #include "mbedtls/ssl_ticket.h"
  46. #endif
  47. #if defined(MBEDTLS_SSL_COOKIE_C)
  48. #include "mbedtls/ssl_cookie.h"
  49. #endif
  50. #if defined(MBEDTLS_SSL_SERVER_NAME_INDICATION) && defined(MBEDTLS_FS_IO)
  51. #define SNI_OPTION
  52. #endif
  53. #if defined(_WIN32)
  54. #include <windows.h>
  55. #endif
  56. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  57. #include "test/psa_crypto_helpers.h"
  58. #endif
  59. /* Size of memory to be allocated for the heap, when using the library's memory
  60. * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
  61. #define MEMORY_HEAP_SIZE 120000
  62. #define DFL_SERVER_ADDR NULL
  63. #define DFL_SERVER_PORT "4433"
  64. #define DFL_RESPONSE_SIZE -1
  65. #define DFL_DEBUG_LEVEL 0
  66. #define DFL_NBIO 0
  67. #define DFL_EVENT 0
  68. #define DFL_READ_TIMEOUT 0
  69. #define DFL_CA_FILE ""
  70. #define DFL_CA_PATH ""
  71. #define DFL_CRT_FILE ""
  72. #define DFL_KEY_FILE ""
  73. #define DFL_KEY_OPAQUE 0
  74. #define DFL_KEY_PWD ""
  75. #define DFL_CRT_FILE2 ""
  76. #define DFL_KEY_FILE2 ""
  77. #define DFL_KEY_PWD2 ""
  78. #define DFL_ASYNC_OPERATIONS "-"
  79. #define DFL_ASYNC_PRIVATE_DELAY1 ( -1 )
  80. #define DFL_ASYNC_PRIVATE_DELAY2 ( -1 )
  81. #define DFL_ASYNC_PRIVATE_ERROR ( 0 )
  82. #define DFL_PSK ""
  83. #define DFL_PSK_OPAQUE 0
  84. #define DFL_PSK_LIST_OPAQUE 0
  85. #define DFL_PSK_IDENTITY "Client_identity"
  86. #define DFL_ECJPAKE_PW NULL
  87. #define DFL_PSK_LIST NULL
  88. #define DFL_FORCE_CIPHER 0
  89. #define DFL_TLS13_KEX_MODES MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL
  90. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  91. #define DFL_ALLOW_LEGACY -2
  92. #define DFL_RENEGOTIATE 0
  93. #define DFL_RENEGO_DELAY -2
  94. #define DFL_RENEGO_PERIOD ( (uint64_t)-1 )
  95. #define DFL_EXCHANGES 1
  96. #define DFL_MIN_VERSION -1
  97. #define DFL_MAX_VERSION -1
  98. #define DFL_SHA1 -1
  99. #define DFL_CID_ENABLED 0
  100. #define DFL_CID_VALUE ""
  101. #define DFL_CID_ENABLED_RENEGO -1
  102. #define DFL_CID_VALUE_RENEGO NULL
  103. #define DFL_AUTH_MODE -1
  104. #define DFL_CERT_REQ_CA_LIST MBEDTLS_SSL_CERT_REQ_CA_LIST_ENABLED
  105. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  106. #define DFL_TRUNC_HMAC -1
  107. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  108. #define DFL_TICKET_TIMEOUT 86400
  109. #define DFL_CACHE_MAX -1
  110. #define DFL_CACHE_TIMEOUT -1
  111. #define DFL_SNI NULL
  112. #define DFL_ALPN_STRING NULL
  113. #define DFL_CURVES NULL
  114. #define DFL_SIG_ALGS NULL
  115. #define DFL_DHM_FILE NULL
  116. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  117. #define DFL_COOKIES 1
  118. #define DFL_ANTI_REPLAY -1
  119. #define DFL_HS_TO_MIN 0
  120. #define DFL_HS_TO_MAX 0
  121. #define DFL_DTLS_MTU -1
  122. #define DFL_BADMAC_LIMIT -1
  123. #define DFL_DGRAM_PACKING 1
  124. #define DFL_EXTENDED_MS -1
  125. #define DFL_ETM -1
  126. #define DFL_SERIALIZE 0
  127. #define DFL_CONTEXT_FILE ""
  128. #define DFL_EXTENDED_MS_ENFORCE -1
  129. #define DFL_CA_CALLBACK 0
  130. #define DFL_EAP_TLS 0
  131. #define DFL_REPRODUCIBLE 0
  132. #define DFL_NSS_KEYLOG 0
  133. #define DFL_NSS_KEYLOG_FILE NULL
  134. #define DFL_QUERY_CONFIG_MODE 0
  135. #define DFL_USE_SRTP 0
  136. #define DFL_SRTP_FORCE_PROFILE 0
  137. #define DFL_SRTP_SUPPORT_MKI 0
  138. #define LONG_RESPONSE "<p>01-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  139. "02-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  140. "03-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  141. "04-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  142. "05-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  143. "06-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah\r\n" \
  144. "07-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah-blah</p>\r\n"
  145. /* Uncomment LONG_RESPONSE at the end of HTTP_RESPONSE to test sending longer
  146. * packets (for fragmentation purposes) */
  147. #define HTTP_RESPONSE \
  148. "HTTP/1.0 200 OK\r\nContent-Type: text/html\r\n\r\n" \
  149. "<h2>mbed TLS Test Server</h2>\r\n" \
  150. "<p>Successful connection using: %s</p>\r\n" // LONG_RESPONSE
  151. /*
  152. * Size of the basic I/O buffer. Able to hold our default response.
  153. *
  154. * You will need to adapt the mbedtls_ssl_get_bytes_avail() test in ssl-opt.sh
  155. * if you change this value to something outside the range <= 100 or > 500
  156. */
  157. #define DFL_IO_BUF_LEN 200
  158. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  159. #if defined(MBEDTLS_FS_IO)
  160. #define USAGE_IO \
  161. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  162. " default: \"\" (pre-loaded)\n" \
  163. " use \"none\" to skip loading any top-level CAs.\n" \
  164. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  165. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  166. " use \"none\" to skip loading any top-level CAs.\n" \
  167. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  168. " default: see note after key_file2\n" \
  169. " key_file=%%s default: see note after key_file2\n" \
  170. " key_pwd=%%s Password for key specified by key_file argument\n"\
  171. " default: none\n" \
  172. " crt_file2=%%s Your second cert and chain (in bottom to top order, top may be omitted)\n" \
  173. " default: see note after key_file2\n" \
  174. " key_file2=%%s default: see note below\n" \
  175. " note: if neither crt_file/key_file nor crt_file2/key_file2 are used,\n" \
  176. " preloaded certificate(s) and key(s) are used if available\n" \
  177. " key_pwd2=%%s Password for key specified by key_file2 argument\n"\
  178. " default: none\n" \
  179. " dhm_file=%%s File containing Diffie-Hellman parameters\n" \
  180. " default: preloaded parameters\n"
  181. #else
  182. #define USAGE_IO \
  183. "\n" \
  184. " No file operations available (MBEDTLS_FS_IO not defined)\n" \
  185. "\n"
  186. #endif /* MBEDTLS_FS_IO */
  187. #else
  188. #define USAGE_IO ""
  189. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  190. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
  191. #define USAGE_KEY_OPAQUE \
  192. " key_opaque=%%d Handle your private keys as if they were opaque\n" \
  193. " default: 0 (disabled)\n"
  194. #else
  195. #define USAGE_KEY_OPAQUE ""
  196. #endif
  197. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  198. #define USAGE_SSL_ASYNC \
  199. " async_operations=%%c... d=decrypt, s=sign (default: -=off)\n" \
  200. " async_private_delay1=%%d Asynchronous delay for key_file or preloaded key\n" \
  201. " async_private_delay2=%%d Asynchronous delay for key_file2 and sni\n" \
  202. " default: -1 (not asynchronous)\n" \
  203. " async_private_error=%%d Async callback error injection (default=0=none,\n" \
  204. " 1=start, 2=cancel, 3=resume, negative=first time only)"
  205. #else
  206. #define USAGE_SSL_ASYNC ""
  207. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  208. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  209. #define USAGE_CID \
  210. " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
  211. " default: 0 (disabled)\n" \
  212. " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
  213. " default: same as 'cid' parameter\n" \
  214. " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
  215. " default: \"\"\n" \
  216. " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
  217. " default: same as 'cid_val' parameter\n"
  218. #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  219. #define USAGE_CID ""
  220. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  221. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  222. #define USAGE_PSK_RAW \
  223. " psk=%%s default: \"\" (disabled)\n" \
  224. " The PSK values are in hex, without 0x.\n" \
  225. " psk_list=%%s default: \"\"\n" \
  226. " A list of (PSK identity, PSK value) pairs.\n" \
  227. " The PSK values are in hex, without 0x.\n" \
  228. " id1,psk1[,id2,psk2[,...]]\n" \
  229. " psk_identity=%%s default: \"Client_identity\"\n"
  230. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  231. #define USAGE_PSK_SLOT \
  232. " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
  233. " Enable this to store the PSK configured through command line\n" \
  234. " parameter `psk` in a PSA-based key slot.\n" \
  235. " Note: Currently only supported in conjunction with\n" \
  236. " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
  237. " to force a particular PSK-only ciphersuite.\n" \
  238. " Note: This is to test integration of PSA-based opaque PSKs with\n" \
  239. " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
  240. " with prepopulated key slots instead of importing raw key material.\n" \
  241. " psk_list_opaque=%%d default: 0 (don't use opaque dynamic PSKs)\n" \
  242. " Enable this to store the list of dynamically chosen PSKs configured\n" \
  243. " through the command line parameter `psk_list` in PSA-based key slots.\n" \
  244. " Note: Currently only supported in conjunction with\n" \
  245. " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
  246. " to force a particular PSK-only ciphersuite.\n" \
  247. " Note: This is to test integration of PSA-based opaque PSKs with\n" \
  248. " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
  249. " with prepopulated key slots instead of importing raw key material.\n"
  250. #else
  251. #define USAGE_PSK_SLOT ""
  252. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  253. #define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
  254. #else
  255. #define USAGE_PSK ""
  256. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  257. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  258. #define USAGE_CA_CALLBACK \
  259. " ca_callback=%%d default: 0 (disabled)\n" \
  260. " Enable this to use the trusted certificate callback function\n"
  261. #else
  262. #define USAGE_CA_CALLBACK ""
  263. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  264. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  265. #define USAGE_TICKETS \
  266. " tickets=%%d default: 1 (enabled)\n" \
  267. " ticket_timeout=%%d default: 86400 (one day)\n"
  268. #else
  269. #define USAGE_TICKETS ""
  270. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  271. #define USAGE_EAP_TLS \
  272. " eap_tls=%%d default: 0 (disabled)\n"
  273. #define USAGE_NSS_KEYLOG \
  274. " nss_keylog=%%d default: 0 (disabled)\n" \
  275. " This cannot be used with eap_tls=1\n"
  276. #define USAGE_NSS_KEYLOG_FILE \
  277. " nss_keylog_file=%%s\n"
  278. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  279. #define USAGE_SRTP \
  280. " use_srtp=%%d default: 0 (disabled)\n" \
  281. " srtp_force_profile=%%d default: 0 (all enabled)\n" \
  282. " available profiles:\n" \
  283. " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
  284. " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
  285. " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
  286. " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
  287. " support_mki=%%d default: 0 (not supported)\n"
  288. #else /* MBEDTLS_SSL_DTLS_SRTP */
  289. #define USAGE_SRTP ""
  290. #endif
  291. #if defined(MBEDTLS_SSL_CACHE_C)
  292. #define USAGE_CACHE \
  293. " cache_max=%%d default: cache default (50)\n" \
  294. " cache_timeout=%%d default: cache default (1d)\n"
  295. #else
  296. #define USAGE_CACHE ""
  297. #endif /* MBEDTLS_SSL_CACHE_C */
  298. #if defined(SNI_OPTION)
  299. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  300. #define SNI_CRL ",crl"
  301. #else
  302. #define SNI_CRL ""
  303. #endif
  304. #define USAGE_SNI \
  305. " sni=%%s name1,cert1,key1,ca1"SNI_CRL",auth1[,...]\n" \
  306. " default: disabled\n"
  307. #else
  308. #define USAGE_SNI ""
  309. #endif /* SNI_OPTION */
  310. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  311. #define USAGE_MAX_FRAG_LEN \
  312. " max_frag_len=%%d default: 16384 (tls default)\n" \
  313. " options: 512, 1024, 2048, 4096\n"
  314. #else
  315. #define USAGE_MAX_FRAG_LEN ""
  316. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  317. #if defined(MBEDTLS_SSL_ALPN)
  318. #define USAGE_ALPN \
  319. " alpn=%%s default: \"\" (disabled)\n" \
  320. " example: spdy/1,http/1.1\n"
  321. #else
  322. #define USAGE_ALPN ""
  323. #endif /* MBEDTLS_SSL_ALPN */
  324. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  325. #define USAGE_COOKIES \
  326. " cookies=0/1/-1 default: 1 (enabled)\n" \
  327. " 0: disabled, -1: library default (broken)\n"
  328. #else
  329. #define USAGE_COOKIES ""
  330. #endif
  331. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  332. #define USAGE_ANTI_REPLAY \
  333. " anti_replay=0/1 default: (library default: enabled)\n"
  334. #else
  335. #define USAGE_ANTI_REPLAY ""
  336. #endif
  337. #define USAGE_BADMAC_LIMIT \
  338. " badmac_limit=%%d default: (library default: disabled)\n"
  339. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  340. #define USAGE_DTLS \
  341. " dtls=%%d default: 0 (TLS)\n" \
  342. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  343. " range of DTLS handshake timeouts in millisecs\n" \
  344. " mtu=%%d default: (library default: unlimited)\n" \
  345. " dgram_packing=%%d default: 1 (allowed)\n" \
  346. " allow or forbid packing of multiple\n" \
  347. " records within a single datgram.\n"
  348. #else
  349. #define USAGE_DTLS ""
  350. #endif
  351. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  352. #define USAGE_EMS \
  353. " extended_ms=0/1 default: (library default: on)\n"
  354. #else
  355. #define USAGE_EMS ""
  356. #endif
  357. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  358. #define USAGE_ETM \
  359. " etm=0/1 default: (library default: on)\n"
  360. #else
  361. #define USAGE_ETM ""
  362. #endif
  363. #define USAGE_REPRODUCIBLE \
  364. " reproducible=0/1 default: 0 (disabled)\n"
  365. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  366. #define USAGE_RENEGO \
  367. " renegotiation=%%d default: 0 (disabled)\n" \
  368. " renegotiate=%%d default: 0 (disabled)\n" \
  369. " renego_delay=%%d default: -2 (library default)\n" \
  370. " renego_period=%%d default: (2^64 - 1 for TLS, 2^48 - 1 for DTLS)\n"
  371. #else
  372. #define USAGE_RENEGO ""
  373. #endif
  374. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  375. #define USAGE_ECJPAKE \
  376. " ecjpake_pw=%%s default: none (disabled)\n"
  377. #else
  378. #define USAGE_ECJPAKE ""
  379. #endif
  380. #if defined(MBEDTLS_ECP_C)
  381. #define USAGE_CURVES \
  382. " curves=a,b,c,d default: \"default\" (library default)\n" \
  383. " example: \"secp521r1,brainpoolP512r1\"\n" \
  384. " - use \"none\" for empty list\n" \
  385. " - see mbedtls_ecp_curve_list()\n" \
  386. " for acceptable curve names\n"
  387. #else
  388. #define USAGE_CURVES ""
  389. #endif
  390. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  391. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  392. #define USAGE_SIG_ALGS \
  393. " sig_algs=a,b,c,d default: \"default\" (library default)\n" \
  394. " example: \"ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384\"\n"
  395. #else
  396. #define USAGE_SIG_ALGS ""
  397. #endif
  398. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  399. #define USAGE_SERIALIZATION \
  400. " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
  401. " options: 1 (serialize)\n" \
  402. " 2 (serialize with re-initialization)\n" \
  403. " context_file=%%s The file path to write a serialized connection\n"\
  404. " in the form of base64 code (serialize option\n" \
  405. " must be set)\n" \
  406. " default: \"\" (do nothing)\n" \
  407. " option: a file path\n"
  408. #else
  409. #define USAGE_SERIALIZATION ""
  410. #endif
  411. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  412. #define USAGE_TLS13_KEY_EXCHANGE_MODES \
  413. " tls13_kex_modes=%%s default: all\n" \
  414. " options: psk, psk_ephemeral, ephemeral, ephemeral_all, psk_all, all\n"
  415. #else
  416. #define USAGE_TLS13_KEY_EXCHANGE_MODES ""
  417. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  418. /* USAGE is arbitrarily split to stay under the portable string literal
  419. * length limit: 4095 bytes in C99. */
  420. #define USAGE1 \
  421. "\n usage: ssl_server2 param=<>...\n" \
  422. "\n acceptable parameters:\n" \
  423. " server_addr=%%s default: (all interfaces)\n" \
  424. " server_port=%%d default: 4433\n" \
  425. " debug_level=%%d default: 0 (disabled)\n" \
  426. " buffer_size=%%d default: 200 \n" \
  427. " (minimum: 1)\n" \
  428. " response_size=%%d default: about 152 (basic response)\n" \
  429. " (minimum: 0, max: 16384)\n" \
  430. " increases buffer_size if bigger\n"\
  431. " nbio=%%d default: 0 (blocking I/O)\n" \
  432. " options: 1 (non-blocking), 2 (added delays)\n" \
  433. " event=%%d default: 0 (loop)\n" \
  434. " options: 1 (level-triggered, implies nbio=1),\n" \
  435. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  436. "\n" \
  437. USAGE_DTLS \
  438. USAGE_SRTP \
  439. USAGE_COOKIES \
  440. USAGE_ANTI_REPLAY \
  441. USAGE_BADMAC_LIMIT \
  442. "\n"
  443. #define USAGE2 \
  444. " auth_mode=%%s default: (library default: none)\n" \
  445. " options: none, optional, required\n" \
  446. " cert_req_ca_list=%%d default: 1 (send ca list)\n" \
  447. " options: 1 (send ca list), 0 (don't send)\n" \
  448. USAGE_IO \
  449. USAGE_KEY_OPAQUE \
  450. "\n" \
  451. USAGE_PSK \
  452. USAGE_CA_CALLBACK \
  453. USAGE_ECJPAKE \
  454. "\n"
  455. #define USAGE3 \
  456. " allow_legacy=%%d default: (library default: no)\n" \
  457. USAGE_RENEGO \
  458. " exchanges=%%d default: 1\n" \
  459. "\n" \
  460. USAGE_TICKETS \
  461. USAGE_EAP_TLS \
  462. USAGE_REPRODUCIBLE \
  463. USAGE_NSS_KEYLOG \
  464. USAGE_NSS_KEYLOG_FILE \
  465. USAGE_CACHE \
  466. USAGE_MAX_FRAG_LEN \
  467. USAGE_ALPN \
  468. USAGE_EMS \
  469. USAGE_ETM \
  470. USAGE_CURVES \
  471. USAGE_SIG_ALGS \
  472. "\n"
  473. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  474. #define TLS1_3_VERSION_OPTIONS ", tls1_3"
  475. #else /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  476. #define TLS1_3_VERSION_OPTIONS ""
  477. #endif /* !MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  478. #define USAGE4 \
  479. USAGE_SSL_ASYNC \
  480. USAGE_SNI \
  481. " allow_sha1=%%d default: 0\n" \
  482. " min_version=%%s default: (library default: tls1_2)\n" \
  483. " max_version=%%s default: (library default: tls1_2)\n" \
  484. " force_version=%%s default: \"\" (none)\n" \
  485. " options: tls1_2, dtls1_2" TLS1_3_VERSION_OPTIONS \
  486. "\n\n" \
  487. " force_ciphersuite=<name> default: all enabled\n" \
  488. USAGE_TLS13_KEY_EXCHANGE_MODES \
  489. " query_config=<name> return 0 if the specified\n" \
  490. " configuration macro is defined and 1\n" \
  491. " otherwise. The expansion of the macro\n" \
  492. " is printed if it is defined\n" \
  493. USAGE_SERIALIZATION \
  494. " acceptable ciphersuite names:\n"
  495. #define ALPN_LIST_SIZE 10
  496. #define CURVE_LIST_SIZE 20
  497. #define SIG_ALG_LIST_SIZE 5
  498. #define PUT_UINT64_BE(out_be,in_le,i) \
  499. { \
  500. (out_be)[(i) + 0] = (unsigned char)( ( (in_le) >> 56 ) & 0xFF ); \
  501. (out_be)[(i) + 1] = (unsigned char)( ( (in_le) >> 48 ) & 0xFF ); \
  502. (out_be)[(i) + 2] = (unsigned char)( ( (in_le) >> 40 ) & 0xFF ); \
  503. (out_be)[(i) + 3] = (unsigned char)( ( (in_le) >> 32 ) & 0xFF ); \
  504. (out_be)[(i) + 4] = (unsigned char)( ( (in_le) >> 24 ) & 0xFF ); \
  505. (out_be)[(i) + 5] = (unsigned char)( ( (in_le) >> 16 ) & 0xFF ); \
  506. (out_be)[(i) + 6] = (unsigned char)( ( (in_le) >> 8 ) & 0xFF ); \
  507. (out_be)[(i) + 7] = (unsigned char)( ( (in_le) >> 0 ) & 0xFF ); \
  508. }
  509. /* This is global so it can be easily accessed by callback functions */
  510. rng_context_t rng;
  511. /*
  512. * global options
  513. */
  514. struct options
  515. {
  516. const char *server_addr; /* address on which the ssl service runs */
  517. const char *server_port; /* port on which the ssl service runs */
  518. int debug_level; /* level of debugging */
  519. int nbio; /* should I/O be blocking? */
  520. int event; /* loop or event-driven IO? level or edge triggered? */
  521. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  522. int response_size; /* pad response with header to requested size */
  523. uint16_t buffer_size; /* IO buffer size */
  524. const char *ca_file; /* the file with the CA certificate(s) */
  525. const char *ca_path; /* the path with the CA certificate(s) reside */
  526. const char *crt_file; /* the file with the server certificate */
  527. const char *key_file; /* the file with the server key */
  528. int key_opaque; /* handle private key as if it were opaque */
  529. const char *key_pwd; /* the password for the server key */
  530. const char *crt_file2; /* the file with the 2nd server certificate */
  531. const char *key_file2; /* the file with the 2nd server key */
  532. const char *key_pwd2; /* the password for the 2nd server key */
  533. const char *async_operations; /* supported SSL asynchronous operations */
  534. int async_private_delay1; /* number of times f_async_resume needs to be called for key 1, or -1 for no async */
  535. int async_private_delay2; /* number of times f_async_resume needs to be called for key 2, or -1 for no async */
  536. int async_private_error; /* inject error in async private callback */
  537. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  538. int psk_opaque;
  539. int psk_list_opaque;
  540. #endif
  541. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  542. int ca_callback; /* Use callback for trusted certificate list */
  543. #endif
  544. const char *psk; /* the pre-shared key */
  545. const char *psk_identity; /* the pre-shared key identity */
  546. char *psk_list; /* list of PSK id/key pairs for callback */
  547. const char *ecjpake_pw; /* the EC J-PAKE password */
  548. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  549. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  550. int tls13_kex_modes; /* supported TLS 1.3 key exchange modes */
  551. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  552. int renegotiation; /* enable / disable renegotiation */
  553. int allow_legacy; /* allow legacy renegotiation */
  554. int renegotiate; /* attempt renegotiation? */
  555. int renego_delay; /* delay before enforcing renegotiation */
  556. uint64_t renego_period; /* period for automatic renegotiation */
  557. int exchanges; /* number of data exchanges */
  558. int min_version; /* minimum protocol version accepted */
  559. int max_version; /* maximum protocol version accepted */
  560. int allow_sha1; /* flag for SHA-1 support */
  561. int auth_mode; /* verify mode for connection */
  562. int cert_req_ca_list; /* should we send the CA list? */
  563. unsigned char mfl_code; /* code for maximum fragment length */
  564. int trunc_hmac; /* accept truncated hmac? */
  565. int tickets; /* enable / disable session tickets */
  566. int ticket_timeout; /* session ticket lifetime */
  567. int cache_max; /* max number of session cache entries */
  568. int cache_timeout; /* expiration delay of session cache entries */
  569. char *sni; /* string describing sni information */
  570. const char *curves; /* list of supported elliptic curves */
  571. const char *sig_algs; /* supported TLS 1.3 signature algorithms */
  572. const char *alpn_string; /* ALPN supported protocols */
  573. const char *dhm_file; /* the file with the DH parameters */
  574. int extended_ms; /* allow negotiation of extended MS? */
  575. int etm; /* allow negotiation of encrypt-then-MAC? */
  576. int transport; /* TLS or DTLS? */
  577. int cookies; /* Use cookies for DTLS? -1 to break them */
  578. int anti_replay; /* Use anti-replay for DTLS? -1 for default */
  579. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  580. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  581. int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
  582. int dgram_packing; /* allow/forbid datagram packing */
  583. int badmac_limit; /* Limit of records with bad MAC */
  584. int eap_tls; /* derive EAP-TLS keying material? */
  585. int nss_keylog; /* export NSS key log material */
  586. const char *nss_keylog_file; /* NSS key log file */
  587. int cid_enabled; /* whether to use the CID extension or not */
  588. int cid_enabled_renego; /* whether to use the CID extension or not
  589. * during renegotiation */
  590. const char *cid_val; /* the CID to use for incoming messages */
  591. int serialize; /* serialize/deserialize connection */
  592. const char *context_file; /* the file to write a serialized connection
  593. * in the form of base64 code (serialize
  594. * option must be set) */
  595. const char *cid_val_renego; /* the CID to use for incoming messages
  596. * after renegotiation */
  597. int reproducible; /* make communication reproducible */
  598. int query_config_mode; /* whether to read config */
  599. int use_srtp; /* Support SRTP */
  600. int force_srtp_profile; /* SRTP protection profile to use or all */
  601. int support_mki; /* The dtls mki mki support */
  602. } opt;
  603. #include "ssl_test_common_source.c"
  604. /*
  605. * Return authmode from string, or -1 on error
  606. */
  607. static int get_auth_mode( const char *s )
  608. {
  609. if( strcmp( s, "none" ) == 0 )
  610. return( MBEDTLS_SSL_VERIFY_NONE );
  611. if( strcmp( s, "optional" ) == 0 )
  612. return( MBEDTLS_SSL_VERIFY_OPTIONAL );
  613. if( strcmp( s, "required" ) == 0 )
  614. return( MBEDTLS_SSL_VERIFY_REQUIRED );
  615. return( -1 );
  616. }
  617. /*
  618. * Used by sni_parse and psk_parse to handle coma-separated lists
  619. */
  620. #define GET_ITEM( dst ) \
  621. do \
  622. { \
  623. (dst) = p; \
  624. while( *p != ',' ) \
  625. if( ++p > end ) \
  626. goto error; \
  627. *p++ = '\0'; \
  628. } while( 0 )
  629. #if defined(SNI_OPTION)
  630. typedef struct _sni_entry sni_entry;
  631. struct _sni_entry {
  632. const char *name;
  633. mbedtls_x509_crt *cert;
  634. mbedtls_pk_context *key;
  635. mbedtls_x509_crt* ca;
  636. mbedtls_x509_crl* crl;
  637. int authmode;
  638. sni_entry *next;
  639. };
  640. void sni_free( sni_entry *head )
  641. {
  642. sni_entry *cur = head, *next;
  643. while( cur != NULL )
  644. {
  645. mbedtls_x509_crt_free( cur->cert );
  646. mbedtls_free( cur->cert );
  647. mbedtls_pk_free( cur->key );
  648. mbedtls_free( cur->key );
  649. mbedtls_x509_crt_free( cur->ca );
  650. mbedtls_free( cur->ca );
  651. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  652. mbedtls_x509_crl_free( cur->crl );
  653. mbedtls_free( cur->crl );
  654. #endif
  655. next = cur->next;
  656. mbedtls_free( cur );
  657. cur = next;
  658. }
  659. }
  660. /*
  661. * Parse a string of sextuples name1,crt1,key1,ca1,crl1,auth1[,...]
  662. * into a usable sni_entry list. For ca1, crl1, auth1, the special value
  663. * '-' means unset. If ca1 is unset, then crl1 is ignored too.
  664. *
  665. * Modifies the input string! This is not production quality!
  666. */
  667. sni_entry *sni_parse( char *sni_string )
  668. {
  669. sni_entry *cur = NULL, *new = NULL;
  670. char *p = sni_string;
  671. char *end = p;
  672. char *crt_file, *key_file, *ca_file, *auth_str;
  673. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  674. char *crl_file;
  675. #endif
  676. while( *end != '\0' )
  677. ++end;
  678. *end = ',';
  679. while( p <= end )
  680. {
  681. if( ( new = mbedtls_calloc( 1, sizeof( sni_entry ) ) ) == NULL )
  682. {
  683. sni_free( cur );
  684. return( NULL );
  685. }
  686. GET_ITEM( new->name );
  687. GET_ITEM( crt_file );
  688. GET_ITEM( key_file );
  689. GET_ITEM( ca_file );
  690. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  691. GET_ITEM( crl_file );
  692. #endif
  693. GET_ITEM( auth_str );
  694. if( ( new->cert = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL ||
  695. ( new->key = mbedtls_calloc( 1, sizeof( mbedtls_pk_context ) ) ) == NULL )
  696. goto error;
  697. mbedtls_x509_crt_init( new->cert );
  698. mbedtls_pk_init( new->key );
  699. if( mbedtls_x509_crt_parse_file( new->cert, crt_file ) != 0 ||
  700. mbedtls_pk_parse_keyfile( new->key, key_file, "", rng_get, &rng ) != 0 )
  701. goto error;
  702. if( strcmp( ca_file, "-" ) != 0 )
  703. {
  704. if( ( new->ca = mbedtls_calloc( 1, sizeof( mbedtls_x509_crt ) ) ) == NULL )
  705. goto error;
  706. mbedtls_x509_crt_init( new->ca );
  707. if( mbedtls_x509_crt_parse_file( new->ca, ca_file ) != 0 )
  708. goto error;
  709. }
  710. #if defined(MBEDTLS_X509_CRL_PARSE_C)
  711. if( strcmp( crl_file, "-" ) != 0 )
  712. {
  713. if( ( new->crl = mbedtls_calloc( 1, sizeof( mbedtls_x509_crl ) ) ) == NULL )
  714. goto error;
  715. mbedtls_x509_crl_init( new->crl );
  716. if( mbedtls_x509_crl_parse_file( new->crl, crl_file ) != 0 )
  717. goto error;
  718. }
  719. #endif
  720. if( strcmp( auth_str, "-" ) != 0 )
  721. {
  722. if( ( new->authmode = get_auth_mode( auth_str ) ) < 0 )
  723. goto error;
  724. }
  725. else
  726. new->authmode = DFL_AUTH_MODE;
  727. new->next = cur;
  728. cur = new;
  729. }
  730. return( cur );
  731. error:
  732. sni_free( new );
  733. sni_free( cur );
  734. return( NULL );
  735. }
  736. /*
  737. * SNI callback.
  738. */
  739. int sni_callback( void *p_info, mbedtls_ssl_context *ssl,
  740. const unsigned char *name, size_t name_len )
  741. {
  742. const sni_entry *cur = (const sni_entry *) p_info;
  743. while( cur != NULL )
  744. {
  745. if( name_len == strlen( cur->name ) &&
  746. memcmp( name, cur->name, name_len ) == 0 )
  747. {
  748. if( cur->ca != NULL )
  749. mbedtls_ssl_set_hs_ca_chain( ssl, cur->ca, cur->crl );
  750. if( cur->authmode != DFL_AUTH_MODE )
  751. mbedtls_ssl_set_hs_authmode( ssl, cur->authmode );
  752. return( mbedtls_ssl_set_hs_own_cert( ssl, cur->cert, cur->key ) );
  753. }
  754. cur = cur->next;
  755. }
  756. return( -1 );
  757. }
  758. #endif /* SNI_OPTION */
  759. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  760. typedef struct _psk_entry psk_entry;
  761. struct _psk_entry
  762. {
  763. const char *name;
  764. size_t key_len;
  765. unsigned char key[MBEDTLS_PSK_MAX_LEN];
  766. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  767. psa_key_id_t slot;
  768. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  769. psk_entry *next;
  770. };
  771. /*
  772. * Free a list of psk_entry's
  773. */
  774. int psk_free( psk_entry *head )
  775. {
  776. psk_entry *next;
  777. while( head != NULL )
  778. {
  779. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  780. psa_status_t status;
  781. psa_key_id_t const slot = head->slot;
  782. if( slot != 0 )
  783. {
  784. status = psa_destroy_key( slot );
  785. if( status != PSA_SUCCESS )
  786. return( status );
  787. }
  788. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  789. next = head->next;
  790. mbedtls_free( head );
  791. head = next;
  792. }
  793. return( 0 );
  794. }
  795. /*
  796. * Parse a string of pairs name1,key1[,name2,key2[,...]]
  797. * into a usable psk_entry list.
  798. *
  799. * Modifies the input string! This is not production quality!
  800. */
  801. psk_entry *psk_parse( char *psk_string )
  802. {
  803. psk_entry *cur = NULL, *new = NULL;
  804. char *p = psk_string;
  805. char *end = p;
  806. char *key_hex;
  807. while( *end != '\0' )
  808. ++end;
  809. *end = ',';
  810. while( p <= end )
  811. {
  812. if( ( new = mbedtls_calloc( 1, sizeof( psk_entry ) ) ) == NULL )
  813. goto error;
  814. memset( new, 0, sizeof( psk_entry ) );
  815. GET_ITEM( new->name );
  816. GET_ITEM( key_hex );
  817. if( mbedtls_test_unhexify( new->key, MBEDTLS_PSK_MAX_LEN,
  818. key_hex, &new->key_len ) != 0 )
  819. goto error;
  820. new->next = cur;
  821. cur = new;
  822. }
  823. return( cur );
  824. error:
  825. psk_free( new );
  826. psk_free( cur );
  827. return( 0 );
  828. }
  829. /*
  830. * PSK callback
  831. */
  832. int psk_callback( void *p_info, mbedtls_ssl_context *ssl,
  833. const unsigned char *name, size_t name_len )
  834. {
  835. psk_entry *cur = (psk_entry *) p_info;
  836. while( cur != NULL )
  837. {
  838. if( name_len == strlen( cur->name ) &&
  839. memcmp( name, cur->name, name_len ) == 0 )
  840. {
  841. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  842. if( cur->slot != 0 )
  843. return( mbedtls_ssl_set_hs_psk_opaque( ssl, cur->slot ) );
  844. else
  845. #endif
  846. return( mbedtls_ssl_set_hs_psk( ssl, cur->key, cur->key_len ) );
  847. }
  848. cur = cur->next;
  849. }
  850. return( -1 );
  851. }
  852. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  853. static mbedtls_net_context listen_fd, client_fd;
  854. /* Interruption handler to ensure clean exit (for valgrind testing) */
  855. #if !defined(_WIN32)
  856. static int received_sigterm = 0;
  857. void term_handler( int sig )
  858. {
  859. ((void) sig);
  860. received_sigterm = 1;
  861. mbedtls_net_free( &listen_fd ); /* causes mbedtls_net_accept() to abort */
  862. mbedtls_net_free( &client_fd ); /* causes net_read() to abort */
  863. }
  864. #endif
  865. /** Return true if \p ret is a status code indicating that there is an
  866. * operation in progress on an SSL connection, and false if it indicates
  867. * success or a fatal error.
  868. *
  869. * The possible operations in progress are:
  870. *
  871. * - A read, when the SSL input buffer does not contain a full message.
  872. * - A write, when the SSL output buffer contains some data that has not
  873. * been sent over the network yet.
  874. * - An asynchronous callback that has not completed yet. */
  875. static int mbedtls_status_is_ssl_in_progress( int ret )
  876. {
  877. return( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  878. ret == MBEDTLS_ERR_SSL_WANT_WRITE ||
  879. ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  880. }
  881. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  882. typedef struct
  883. {
  884. mbedtls_x509_crt *cert; /*!< Certificate corresponding to the key */
  885. mbedtls_pk_context *pk; /*!< Private key */
  886. unsigned delay; /*!< Number of resume steps to go through */
  887. unsigned pk_owned : 1; /*!< Whether to free the pk object on exit */
  888. } ssl_async_key_slot_t;
  889. typedef enum {
  890. SSL_ASYNC_INJECT_ERROR_NONE = 0, /*!< Let the callbacks succeed */
  891. SSL_ASYNC_INJECT_ERROR_START, /*!< Inject error during start */
  892. SSL_ASYNC_INJECT_ERROR_CANCEL, /*!< Close the connection after async start */
  893. SSL_ASYNC_INJECT_ERROR_RESUME, /*!< Inject error during resume */
  894. #define SSL_ASYNC_INJECT_ERROR_MAX SSL_ASYNC_INJECT_ERROR_RESUME
  895. } ssl_async_inject_error_t;
  896. typedef struct
  897. {
  898. ssl_async_key_slot_t slots[4]; /* key, key2, sni1, sni2 */
  899. size_t slots_used;
  900. ssl_async_inject_error_t inject_error;
  901. int (*f_rng)(void *, unsigned char *, size_t);
  902. void *p_rng;
  903. } ssl_async_key_context_t;
  904. int ssl_async_set_key( ssl_async_key_context_t *ctx,
  905. mbedtls_x509_crt *cert,
  906. mbedtls_pk_context *pk,
  907. int pk_take_ownership,
  908. unsigned delay )
  909. {
  910. if( ctx->slots_used >= sizeof( ctx->slots ) / sizeof( *ctx->slots ) )
  911. return( -1 );
  912. ctx->slots[ctx->slots_used].cert = cert;
  913. ctx->slots[ctx->slots_used].pk = pk;
  914. ctx->slots[ctx->slots_used].delay = delay;
  915. ctx->slots[ctx->slots_used].pk_owned = pk_take_ownership;
  916. ++ctx->slots_used;
  917. return( 0 );
  918. }
  919. #define SSL_ASYNC_INPUT_MAX_SIZE 512
  920. typedef enum
  921. {
  922. ASYNC_OP_SIGN,
  923. ASYNC_OP_DECRYPT,
  924. } ssl_async_operation_type_t;
  925. /* Note that the enum above and the array below need to be kept in sync!
  926. * `ssl_async_operation_names[op]` is the name of op for each value `op`
  927. * of type `ssl_async_operation_type_t`. */
  928. static const char *const ssl_async_operation_names[] =
  929. {
  930. "sign",
  931. "decrypt",
  932. };
  933. typedef struct
  934. {
  935. unsigned slot;
  936. ssl_async_operation_type_t operation_type;
  937. mbedtls_md_type_t md_alg;
  938. unsigned char input[SSL_ASYNC_INPUT_MAX_SIZE];
  939. size_t input_len;
  940. unsigned remaining_delay;
  941. } ssl_async_operation_context_t;
  942. static int ssl_async_start( mbedtls_ssl_context *ssl,
  943. mbedtls_x509_crt *cert,
  944. ssl_async_operation_type_t op_type,
  945. mbedtls_md_type_t md_alg,
  946. const unsigned char *input,
  947. size_t input_len )
  948. {
  949. ssl_async_key_context_t *config_data =
  950. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  951. unsigned slot;
  952. ssl_async_operation_context_t *ctx = NULL;
  953. const char *op_name = ssl_async_operation_names[op_type];
  954. {
  955. char dn[100];
  956. if( mbedtls_x509_dn_gets( dn, sizeof( dn ), &cert->subject ) > 0 )
  957. mbedtls_printf( "Async %s callback: looking for DN=%s\n",
  958. op_name, dn );
  959. }
  960. /* Look for a private key that matches the public key in cert.
  961. * Since this test code has the private key inside Mbed TLS,
  962. * we call mbedtls_pk_check_pair to match a private key with the
  963. * public key. */
  964. for( slot = 0; slot < config_data->slots_used; slot++ )
  965. {
  966. if( mbedtls_pk_check_pair( &cert->pk,
  967. config_data->slots[slot].pk,
  968. rng_get, &rng ) == 0 )
  969. break;
  970. }
  971. if( slot == config_data->slots_used )
  972. {
  973. mbedtls_printf( "Async %s callback: no key matches this certificate.\n",
  974. op_name );
  975. return( MBEDTLS_ERR_SSL_HW_ACCEL_FALLTHROUGH );
  976. }
  977. mbedtls_printf( "Async %s callback: using key slot %u, delay=%u.\n",
  978. op_name, slot, config_data->slots[slot].delay );
  979. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_START )
  980. {
  981. mbedtls_printf( "Async %s callback: injected error\n", op_name );
  982. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  983. }
  984. if( input_len > SSL_ASYNC_INPUT_MAX_SIZE )
  985. return( MBEDTLS_ERR_SSL_BAD_INPUT_DATA );
  986. ctx = mbedtls_calloc( 1, sizeof( *ctx ) );
  987. if( ctx == NULL )
  988. return( MBEDTLS_ERR_SSL_ALLOC_FAILED );
  989. ctx->slot = slot;
  990. ctx->operation_type = op_type;
  991. ctx->md_alg = md_alg;
  992. memcpy( ctx->input, input, input_len );
  993. ctx->input_len = input_len;
  994. ctx->remaining_delay = config_data->slots[slot].delay;
  995. mbedtls_ssl_set_async_operation_data( ssl, ctx );
  996. if( ctx->remaining_delay == 0 )
  997. return( 0 );
  998. else
  999. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  1000. }
  1001. static int ssl_async_sign( mbedtls_ssl_context *ssl,
  1002. mbedtls_x509_crt *cert,
  1003. mbedtls_md_type_t md_alg,
  1004. const unsigned char *hash,
  1005. size_t hash_len )
  1006. {
  1007. return( ssl_async_start( ssl, cert,
  1008. ASYNC_OP_SIGN, md_alg,
  1009. hash, hash_len ) );
  1010. }
  1011. static int ssl_async_decrypt( mbedtls_ssl_context *ssl,
  1012. mbedtls_x509_crt *cert,
  1013. const unsigned char *input,
  1014. size_t input_len )
  1015. {
  1016. return( ssl_async_start( ssl, cert,
  1017. ASYNC_OP_DECRYPT, MBEDTLS_MD_NONE,
  1018. input, input_len ) );
  1019. }
  1020. static int ssl_async_resume( mbedtls_ssl_context *ssl,
  1021. unsigned char *output,
  1022. size_t *output_len,
  1023. size_t output_size )
  1024. {
  1025. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  1026. ssl_async_key_context_t *config_data =
  1027. mbedtls_ssl_conf_get_async_config_data( ssl->conf );
  1028. ssl_async_key_slot_t *key_slot = &config_data->slots[ctx->slot];
  1029. int ret;
  1030. const char *op_name;
  1031. if( ctx->remaining_delay > 0 )
  1032. {
  1033. --ctx->remaining_delay;
  1034. mbedtls_printf( "Async resume (slot %u): call %u more times.\n",
  1035. ctx->slot, ctx->remaining_delay );
  1036. return( MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS );
  1037. }
  1038. switch( ctx->operation_type )
  1039. {
  1040. case ASYNC_OP_DECRYPT:
  1041. ret = mbedtls_pk_decrypt( key_slot->pk,
  1042. ctx->input, ctx->input_len,
  1043. output, output_len, output_size,
  1044. config_data->f_rng, config_data->p_rng );
  1045. break;
  1046. case ASYNC_OP_SIGN:
  1047. ret = mbedtls_pk_sign( key_slot->pk,
  1048. ctx->md_alg,
  1049. ctx->input, ctx->input_len,
  1050. output, output_size, output_len,
  1051. config_data->f_rng, config_data->p_rng );
  1052. break;
  1053. default:
  1054. mbedtls_printf( "Async resume (slot %u): unknown operation type %ld. This shouldn't happen.\n",
  1055. ctx->slot, (long) ctx->operation_type );
  1056. mbedtls_free( ctx );
  1057. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1058. break;
  1059. }
  1060. op_name = ssl_async_operation_names[ctx->operation_type];
  1061. if( config_data->inject_error == SSL_ASYNC_INJECT_ERROR_RESUME )
  1062. {
  1063. mbedtls_printf( "Async resume callback: %s done but injected error\n",
  1064. op_name );
  1065. mbedtls_free( ctx );
  1066. return( MBEDTLS_ERR_PK_FEATURE_UNAVAILABLE );
  1067. }
  1068. mbedtls_printf( "Async resume (slot %u): %s done, status=%d.\n",
  1069. ctx->slot, op_name, ret );
  1070. mbedtls_free( ctx );
  1071. return( ret );
  1072. }
  1073. static void ssl_async_cancel( mbedtls_ssl_context *ssl )
  1074. {
  1075. ssl_async_operation_context_t *ctx = mbedtls_ssl_get_async_operation_data( ssl );
  1076. mbedtls_printf( "Async cancel callback.\n" );
  1077. mbedtls_free( ctx );
  1078. }
  1079. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1080. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1081. static psa_status_t psa_setup_psk_key_slot( psa_key_id_t *slot,
  1082. psa_algorithm_t alg,
  1083. unsigned char *psk,
  1084. size_t psk_len )
  1085. {
  1086. psa_status_t status;
  1087. psa_key_attributes_t key_attributes;
  1088. key_attributes = psa_key_attributes_init();
  1089. psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
  1090. psa_set_key_algorithm( &key_attributes, alg );
  1091. psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
  1092. status = psa_import_key( &key_attributes, psk, psk_len, slot );
  1093. if( status != PSA_SUCCESS )
  1094. {
  1095. fprintf( stderr, "IMPORT\n" );
  1096. return( status );
  1097. }
  1098. return( PSA_SUCCESS );
  1099. }
  1100. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1101. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1102. int report_cid_usage( mbedtls_ssl_context *ssl,
  1103. const char *additional_description )
  1104. {
  1105. int ret;
  1106. unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
  1107. size_t peer_cid_len;
  1108. int cid_negotiated;
  1109. if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1110. return( 0 );
  1111. /* Check if the use of a CID has been negotiated */
  1112. ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
  1113. peer_cid, &peer_cid_len );
  1114. if( ret != 0 )
  1115. {
  1116. mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
  1117. (unsigned int) -ret );
  1118. return( ret );
  1119. }
  1120. if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
  1121. {
  1122. if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
  1123. {
  1124. mbedtls_printf( "(%s) Use of Connection ID was not offered by client.\n",
  1125. additional_description );
  1126. }
  1127. }
  1128. else
  1129. {
  1130. size_t idx=0;
  1131. mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
  1132. additional_description );
  1133. mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
  1134. additional_description,
  1135. (unsigned) peer_cid_len );
  1136. while( idx < peer_cid_len )
  1137. {
  1138. mbedtls_printf( "%02x ", peer_cid[ idx ] );
  1139. idx++;
  1140. }
  1141. mbedtls_printf( "\n" );
  1142. }
  1143. return( 0 );
  1144. }
  1145. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1146. int main( int argc, char *argv[] )
  1147. {
  1148. int ret = 0, len, written, frags, exchanges_left;
  1149. int query_config_ret = 0;
  1150. io_ctx_t io_ctx;
  1151. unsigned char* buf = 0;
  1152. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1153. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1154. psa_algorithm_t alg = 0;
  1155. psa_key_id_t psk_slot = 0;
  1156. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1157. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  1158. size_t psk_len = 0;
  1159. psk_entry *psk_info = NULL;
  1160. #endif
  1161. const char *pers = "ssl_server2";
  1162. unsigned char client_ip[16] = { 0 };
  1163. size_t cliip_len;
  1164. #if defined(MBEDTLS_SSL_COOKIE_C)
  1165. mbedtls_ssl_cookie_ctx cookie_ctx;
  1166. #endif
  1167. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1168. mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
  1169. #endif
  1170. mbedtls_ssl_context ssl;
  1171. mbedtls_ssl_config conf;
  1172. #if defined(MBEDTLS_TIMING_C)
  1173. mbedtls_timing_delay_context timer;
  1174. #endif
  1175. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1176. unsigned char renego_period[8] = { 0 };
  1177. #endif
  1178. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1179. uint32_t flags;
  1180. mbedtls_x509_crt cacert;
  1181. mbedtls_x509_crt srvcert;
  1182. mbedtls_pk_context pkey;
  1183. mbedtls_x509_crt srvcert2;
  1184. mbedtls_pk_context pkey2;
  1185. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1186. psa_key_id_t key_slot = 0; /* invalid key slot */
  1187. psa_key_id_t key_slot2 = 0; /* invalid key slot */
  1188. #endif
  1189. int key_cert_init = 0, key_cert_init2 = 0;
  1190. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1191. ssl_async_key_context_t ssl_async_keys;
  1192. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1193. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1194. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1195. mbedtls_dhm_context dhm;
  1196. #endif
  1197. #if defined(MBEDTLS_SSL_CACHE_C)
  1198. mbedtls_ssl_cache_context cache;
  1199. #endif
  1200. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1201. mbedtls_ssl_ticket_context ticket_ctx;
  1202. #endif
  1203. #if defined(SNI_OPTION)
  1204. sni_entry *sni_info = NULL;
  1205. #endif
  1206. #if defined(MBEDTLS_ECP_C)
  1207. uint16_t group_list[CURVE_LIST_SIZE];
  1208. const mbedtls_ecp_curve_info * curve_cur;
  1209. #endif
  1210. #if defined(MBEDTLS_SSL_ALPN)
  1211. const char *alpn_list[ALPN_LIST_SIZE];
  1212. #endif
  1213. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1214. unsigned char alloc_buf[MEMORY_HEAP_SIZE];
  1215. #endif
  1216. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1217. unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
  1218. unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
  1219. size_t cid_len = 0;
  1220. size_t cid_renego_len = 0;
  1221. #endif
  1222. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  1223. unsigned char *context_buf = NULL;
  1224. size_t context_buf_len = 0;
  1225. #endif
  1226. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  1227. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1228. uint16_t sig_alg_list[SIG_ALG_LIST_SIZE];
  1229. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL &&
  1230. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1231. int i;
  1232. char *p, *q;
  1233. const int *list;
  1234. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1235. psa_status_t status;
  1236. #endif
  1237. unsigned char eap_tls_keymaterial[16];
  1238. unsigned char eap_tls_iv[8];
  1239. const char* eap_tls_label = "client EAP encryption";
  1240. eap_tls_keys eap_tls_keying;
  1241. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  1242. /*! master keys and master salt for SRTP generated during handshake */
  1243. unsigned char dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
  1244. const char* dtls_srtp_label = "EXTRACTOR-dtls_srtp";
  1245. dtls_srtp_keys dtls_srtp_keying;
  1246. const mbedtls_ssl_srtp_profile default_profiles[] = {
  1247. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
  1248. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
  1249. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80,
  1250. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
  1251. MBEDTLS_TLS_SRTP_UNSET
  1252. };
  1253. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1254. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  1255. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  1256. #if defined(MBEDTLS_MEMORY_DEBUG)
  1257. size_t current_heap_memory, peak_heap_memory, heap_blocks;
  1258. #endif /* MBEDTLS_MEMORY_DEBUG */
  1259. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  1260. #if defined(MBEDTLS_TEST_HOOKS)
  1261. test_hooks_init( );
  1262. #endif /* MBEDTLS_TEST_HOOKS */
  1263. /*
  1264. * Make sure memory references are valid in case we exit early.
  1265. */
  1266. mbedtls_net_init( &client_fd );
  1267. mbedtls_net_init( &listen_fd );
  1268. mbedtls_ssl_init( &ssl );
  1269. mbedtls_ssl_config_init( &conf );
  1270. rng_init( &rng );
  1271. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1272. mbedtls_x509_crt_init( &cacert );
  1273. mbedtls_x509_crt_init( &srvcert );
  1274. mbedtls_pk_init( &pkey );
  1275. mbedtls_x509_crt_init( &srvcert2 );
  1276. mbedtls_pk_init( &pkey2 );
  1277. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1278. memset( &ssl_async_keys, 0, sizeof( ssl_async_keys ) );
  1279. #endif
  1280. #endif
  1281. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  1282. mbedtls_dhm_init( &dhm );
  1283. #endif
  1284. #if defined(MBEDTLS_SSL_CACHE_C)
  1285. mbedtls_ssl_cache_init( &cache );
  1286. #endif
  1287. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1288. mbedtls_ssl_ticket_init( &ticket_ctx );
  1289. #endif
  1290. #if defined(MBEDTLS_SSL_ALPN)
  1291. memset( (void *) alpn_list, 0, sizeof( alpn_list ) );
  1292. #endif
  1293. #if defined(MBEDTLS_SSL_COOKIE_C)
  1294. mbedtls_ssl_cookie_init( &cookie_ctx );
  1295. #endif
  1296. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1297. status = psa_crypto_init();
  1298. if( status != PSA_SUCCESS )
  1299. {
  1300. mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
  1301. (int) status );
  1302. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  1303. goto exit;
  1304. }
  1305. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1306. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  1307. mbedtls_test_enable_insecure_external_rng( );
  1308. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  1309. #if !defined(_WIN32)
  1310. /* Abort cleanly on SIGTERM and SIGINT */
  1311. signal( SIGTERM, term_handler );
  1312. signal( SIGINT, term_handler );
  1313. #endif
  1314. if( argc == 0 )
  1315. {
  1316. usage:
  1317. if( ret == 0 )
  1318. ret = 1;
  1319. mbedtls_printf( USAGE1 );
  1320. mbedtls_printf( USAGE2 );
  1321. mbedtls_printf( USAGE3 );
  1322. mbedtls_printf( USAGE4 );
  1323. list = mbedtls_ssl_list_ciphersuites();
  1324. while( *list )
  1325. {
  1326. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1327. list++;
  1328. if( !*list )
  1329. break;
  1330. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  1331. list++;
  1332. }
  1333. mbedtls_printf("\n");
  1334. goto exit;
  1335. }
  1336. opt.buffer_size = DFL_IO_BUF_LEN;
  1337. opt.server_addr = DFL_SERVER_ADDR;
  1338. opt.server_port = DFL_SERVER_PORT;
  1339. opt.debug_level = DFL_DEBUG_LEVEL;
  1340. opt.event = DFL_EVENT;
  1341. opt.response_size = DFL_RESPONSE_SIZE;
  1342. opt.nbio = DFL_NBIO;
  1343. opt.cid_enabled = DFL_CID_ENABLED;
  1344. opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
  1345. opt.cid_val = DFL_CID_VALUE;
  1346. opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
  1347. opt.read_timeout = DFL_READ_TIMEOUT;
  1348. opt.ca_file = DFL_CA_FILE;
  1349. opt.ca_path = DFL_CA_PATH;
  1350. opt.crt_file = DFL_CRT_FILE;
  1351. opt.key_file = DFL_KEY_FILE;
  1352. opt.key_opaque = DFL_KEY_OPAQUE;
  1353. opt.key_pwd = DFL_KEY_PWD;
  1354. opt.crt_file2 = DFL_CRT_FILE2;
  1355. opt.key_file2 = DFL_KEY_FILE2;
  1356. opt.key_pwd2 = DFL_KEY_PWD2;
  1357. opt.async_operations = DFL_ASYNC_OPERATIONS;
  1358. opt.async_private_delay1 = DFL_ASYNC_PRIVATE_DELAY1;
  1359. opt.async_private_delay2 = DFL_ASYNC_PRIVATE_DELAY2;
  1360. opt.async_private_error = DFL_ASYNC_PRIVATE_ERROR;
  1361. opt.psk = DFL_PSK;
  1362. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1363. opt.psk_opaque = DFL_PSK_OPAQUE;
  1364. opt.psk_list_opaque = DFL_PSK_LIST_OPAQUE;
  1365. #endif
  1366. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  1367. opt.ca_callback = DFL_CA_CALLBACK;
  1368. #endif
  1369. opt.psk_identity = DFL_PSK_IDENTITY;
  1370. opt.psk_list = DFL_PSK_LIST;
  1371. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  1372. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  1373. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1374. opt.tls13_kex_modes = DFL_TLS13_KEX_MODES;
  1375. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1376. opt.renegotiation = DFL_RENEGOTIATION;
  1377. opt.allow_legacy = DFL_ALLOW_LEGACY;
  1378. opt.renegotiate = DFL_RENEGOTIATE;
  1379. opt.renego_delay = DFL_RENEGO_DELAY;
  1380. opt.renego_period = DFL_RENEGO_PERIOD;
  1381. opt.exchanges = DFL_EXCHANGES;
  1382. opt.min_version = DFL_MIN_VERSION;
  1383. opt.max_version = DFL_MAX_VERSION;
  1384. opt.allow_sha1 = DFL_SHA1;
  1385. opt.auth_mode = DFL_AUTH_MODE;
  1386. opt.cert_req_ca_list = DFL_CERT_REQ_CA_LIST;
  1387. opt.mfl_code = DFL_MFL_CODE;
  1388. opt.trunc_hmac = DFL_TRUNC_HMAC;
  1389. opt.tickets = DFL_TICKETS;
  1390. opt.ticket_timeout = DFL_TICKET_TIMEOUT;
  1391. opt.cache_max = DFL_CACHE_MAX;
  1392. opt.cache_timeout = DFL_CACHE_TIMEOUT;
  1393. opt.sni = DFL_SNI;
  1394. opt.alpn_string = DFL_ALPN_STRING;
  1395. opt.curves = DFL_CURVES;
  1396. opt.sig_algs = DFL_SIG_ALGS;
  1397. opt.dhm_file = DFL_DHM_FILE;
  1398. opt.transport = DFL_TRANSPORT;
  1399. opt.cookies = DFL_COOKIES;
  1400. opt.anti_replay = DFL_ANTI_REPLAY;
  1401. opt.hs_to_min = DFL_HS_TO_MIN;
  1402. opt.hs_to_max = DFL_HS_TO_MAX;
  1403. opt.dtls_mtu = DFL_DTLS_MTU;
  1404. opt.dgram_packing = DFL_DGRAM_PACKING;
  1405. opt.badmac_limit = DFL_BADMAC_LIMIT;
  1406. opt.extended_ms = DFL_EXTENDED_MS;
  1407. opt.etm = DFL_ETM;
  1408. opt.serialize = DFL_SERIALIZE;
  1409. opt.context_file = DFL_CONTEXT_FILE;
  1410. opt.eap_tls = DFL_EAP_TLS;
  1411. opt.reproducible = DFL_REPRODUCIBLE;
  1412. opt.nss_keylog = DFL_NSS_KEYLOG;
  1413. opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
  1414. opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
  1415. opt.use_srtp = DFL_USE_SRTP;
  1416. opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
  1417. opt.support_mki = DFL_SRTP_SUPPORT_MKI;
  1418. for( i = 1; i < argc; i++ )
  1419. {
  1420. p = argv[i];
  1421. if( ( q = strchr( p, '=' ) ) == NULL )
  1422. goto usage;
  1423. *q++ = '\0';
  1424. if( strcmp( p, "server_port" ) == 0 )
  1425. opt.server_port = q;
  1426. else if( strcmp( p, "server_addr" ) == 0 )
  1427. opt.server_addr = q;
  1428. else if( strcmp( p, "dtls" ) == 0 )
  1429. {
  1430. int t = atoi( q );
  1431. if( t == 0 )
  1432. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  1433. else if( t == 1 )
  1434. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1435. else
  1436. goto usage;
  1437. }
  1438. else if( strcmp( p, "debug_level" ) == 0 )
  1439. {
  1440. opt.debug_level = atoi( q );
  1441. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  1442. goto usage;
  1443. }
  1444. else if( strcmp( p, "nbio" ) == 0 )
  1445. {
  1446. opt.nbio = atoi( q );
  1447. if( opt.nbio < 0 || opt.nbio > 2 )
  1448. goto usage;
  1449. }
  1450. else if( strcmp( p, "event" ) == 0 )
  1451. {
  1452. opt.event = atoi( q );
  1453. if( opt.event < 0 || opt.event > 2 )
  1454. goto usage;
  1455. }
  1456. else if( strcmp( p, "read_timeout" ) == 0 )
  1457. opt.read_timeout = atoi( q );
  1458. else if( strcmp( p, "buffer_size" ) == 0 )
  1459. {
  1460. opt.buffer_size = atoi( q );
  1461. if( opt.buffer_size < 1 )
  1462. goto usage;
  1463. }
  1464. else if( strcmp( p, "response_size" ) == 0 )
  1465. {
  1466. opt.response_size = atoi( q );
  1467. if( opt.response_size < 0 || opt.response_size > MBEDTLS_SSL_OUT_CONTENT_LEN )
  1468. goto usage;
  1469. if( opt.buffer_size < opt.response_size )
  1470. opt.buffer_size = opt.response_size;
  1471. }
  1472. else if( strcmp( p, "ca_file" ) == 0 )
  1473. opt.ca_file = q;
  1474. else if( strcmp( p, "ca_path" ) == 0 )
  1475. opt.ca_path = q;
  1476. else if( strcmp( p, "crt_file" ) == 0 )
  1477. opt.crt_file = q;
  1478. else if( strcmp( p, "key_file" ) == 0 )
  1479. opt.key_file = q;
  1480. else if( strcmp( p, "key_pwd" ) == 0 )
  1481. opt.key_pwd = q;
  1482. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
  1483. else if( strcmp( p, "key_opaque" ) == 0 )
  1484. opt.key_opaque = atoi( q );
  1485. #endif
  1486. else if( strcmp( p, "crt_file2" ) == 0 )
  1487. opt.crt_file2 = q;
  1488. else if( strcmp( p, "key_file2" ) == 0 )
  1489. opt.key_file2 = q;
  1490. else if( strcmp( p, "key_pwd2" ) == 0 )
  1491. opt.key_pwd2 = q;
  1492. else if( strcmp( p, "dhm_file" ) == 0 )
  1493. opt.dhm_file = q;
  1494. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  1495. else if( strcmp( p, "async_operations" ) == 0 )
  1496. opt.async_operations = q;
  1497. else if( strcmp( p, "async_private_delay1" ) == 0 )
  1498. opt.async_private_delay1 = atoi( q );
  1499. else if( strcmp( p, "async_private_delay2" ) == 0 )
  1500. opt.async_private_delay2 = atoi( q );
  1501. else if( strcmp( p, "async_private_error" ) == 0 )
  1502. {
  1503. int n = atoi( q );
  1504. if( n < -SSL_ASYNC_INJECT_ERROR_MAX ||
  1505. n > SSL_ASYNC_INJECT_ERROR_MAX )
  1506. {
  1507. ret = 2;
  1508. goto usage;
  1509. }
  1510. opt.async_private_error = n;
  1511. }
  1512. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  1513. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1514. else if( strcmp( p, "cid" ) == 0 )
  1515. {
  1516. opt.cid_enabled = atoi( q );
  1517. if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
  1518. goto usage;
  1519. }
  1520. else if( strcmp( p, "cid_renego" ) == 0 )
  1521. {
  1522. opt.cid_enabled_renego = atoi( q );
  1523. if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
  1524. goto usage;
  1525. }
  1526. else if( strcmp( p, "cid_val" ) == 0 )
  1527. {
  1528. opt.cid_val = q;
  1529. }
  1530. else if( strcmp( p, "cid_val_renego" ) == 0 )
  1531. {
  1532. opt.cid_val_renego = q;
  1533. }
  1534. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1535. else if( strcmp( p, "psk" ) == 0 )
  1536. opt.psk = q;
  1537. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1538. else if( strcmp( p, "psk_opaque" ) == 0 )
  1539. opt.psk_opaque = atoi( q );
  1540. else if( strcmp( p, "psk_list_opaque" ) == 0 )
  1541. opt.psk_list_opaque = atoi( q );
  1542. #endif
  1543. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  1544. else if( strcmp( p, "ca_callback" ) == 0)
  1545. opt.ca_callback = atoi( q );
  1546. #endif
  1547. else if( strcmp( p, "psk_identity" ) == 0 )
  1548. opt.psk_identity = q;
  1549. else if( strcmp( p, "psk_list" ) == 0 )
  1550. opt.psk_list = q;
  1551. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  1552. opt.ecjpake_pw = q;
  1553. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  1554. {
  1555. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  1556. if( opt.force_ciphersuite[0] == 0 )
  1557. {
  1558. ret = 2;
  1559. goto usage;
  1560. }
  1561. opt.force_ciphersuite[1] = 0;
  1562. }
  1563. else if( strcmp( p, "curves" ) == 0 )
  1564. opt.curves = q;
  1565. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  1566. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1567. else if( strcmp( p, "sig_algs" ) == 0 )
  1568. opt.sig_algs = q;
  1569. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL && && \
  1570. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1571. else if( strcmp( p, "renegotiation" ) == 0 )
  1572. {
  1573. opt.renegotiation = (atoi( q )) ?
  1574. MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  1575. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  1576. }
  1577. else if( strcmp( p, "allow_legacy" ) == 0 )
  1578. {
  1579. switch( atoi( q ) )
  1580. {
  1581. case -1:
  1582. opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
  1583. break;
  1584. case 0:
  1585. opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  1586. break;
  1587. case 1:
  1588. opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
  1589. break;
  1590. default: goto usage;
  1591. }
  1592. }
  1593. else if( strcmp( p, "renegotiate" ) == 0 )
  1594. {
  1595. opt.renegotiate = atoi( q );
  1596. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  1597. goto usage;
  1598. }
  1599. else if( strcmp( p, "renego_delay" ) == 0 )
  1600. {
  1601. opt.renego_delay = atoi( q );
  1602. }
  1603. else if( strcmp( p, "renego_period" ) == 0 )
  1604. {
  1605. #if defined(_MSC_VER)
  1606. opt.renego_period = _strtoui64( q, NULL, 10 );
  1607. #else
  1608. if( sscanf( q, "%" SCNu64, &opt.renego_period ) != 1 )
  1609. goto usage;
  1610. #endif /* _MSC_VER */
  1611. if( opt.renego_period < 2 )
  1612. goto usage;
  1613. }
  1614. else if( strcmp( p, "exchanges" ) == 0 )
  1615. {
  1616. opt.exchanges = atoi( q );
  1617. if( opt.exchanges < 0 )
  1618. goto usage;
  1619. }
  1620. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1621. else if( strcmp( p, "tls13_kex_modes" ) == 0 )
  1622. {
  1623. if( strcmp( q, "psk" ) == 0 )
  1624. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK;
  1625. else if( strcmp(q, "psk_ephemeral" ) == 0 )
  1626. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1627. else if( strcmp(q, "ephemeral" ) == 0 )
  1628. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL;
  1629. else if( strcmp(q, "ephemeral_all" ) == 0 )
  1630. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL_ALL;
  1631. else if( strcmp( q, "psk_all" ) == 0 )
  1632. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_ALL;
  1633. else if( strcmp( q, "all" ) == 0 )
  1634. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL;
  1635. else goto usage;
  1636. }
  1637. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1638. else if( strcmp( p, "min_version" ) == 0 )
  1639. {
  1640. if( strcmp( q, "tls1_2" ) == 0 ||
  1641. strcmp( q, "dtls1_2" ) == 0 )
  1642. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1643. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1644. else if( strcmp( q, "tls1_3" ) == 0 )
  1645. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1646. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1647. else
  1648. goto usage;
  1649. }
  1650. else if( strcmp( p, "max_version" ) == 0 )
  1651. {
  1652. if( strcmp( q, "tls1_2" ) == 0 ||
  1653. strcmp( q, "dtls1_2" ) == 0 )
  1654. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1655. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1656. else if( strcmp( q, "tls1_3" ) == 0 )
  1657. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1658. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1659. else
  1660. goto usage;
  1661. }
  1662. else if( strcmp( p, "allow_sha1" ) == 0 )
  1663. {
  1664. switch( atoi( q ) )
  1665. {
  1666. case 0: opt.allow_sha1 = 0; break;
  1667. case 1: opt.allow_sha1 = 1; break;
  1668. default: goto usage;
  1669. }
  1670. }
  1671. else if( strcmp( p, "force_version" ) == 0 )
  1672. {
  1673. if( strcmp( q, "tls1_2" ) == 0 )
  1674. {
  1675. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1676. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1677. }
  1678. else if( strcmp( q, "dtls1_2" ) == 0 )
  1679. {
  1680. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1681. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1682. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1683. }
  1684. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1685. else if( strcmp( q, "tls1_3" ) == 0 )
  1686. {
  1687. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1688. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1689. }
  1690. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1691. else
  1692. goto usage;
  1693. }
  1694. else if( strcmp( p, "auth_mode" ) == 0 )
  1695. {
  1696. if( ( opt.auth_mode = get_auth_mode( q ) ) < 0 )
  1697. goto usage;
  1698. }
  1699. else if( strcmp( p, "cert_req_ca_list" ) == 0 )
  1700. {
  1701. opt.cert_req_ca_list = atoi( q );
  1702. if( opt.cert_req_ca_list < 0 || opt.cert_req_ca_list > 1 )
  1703. goto usage;
  1704. }
  1705. else if( strcmp( p, "max_frag_len" ) == 0 )
  1706. {
  1707. if( strcmp( q, "512" ) == 0 )
  1708. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  1709. else if( strcmp( q, "1024" ) == 0 )
  1710. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  1711. else if( strcmp( q, "2048" ) == 0 )
  1712. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  1713. else if( strcmp( q, "4096" ) == 0 )
  1714. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  1715. else
  1716. goto usage;
  1717. }
  1718. else if( strcmp( p, "alpn" ) == 0 )
  1719. {
  1720. opt.alpn_string = q;
  1721. }
  1722. else if( strcmp( p, "trunc_hmac" ) == 0 )
  1723. {
  1724. switch( atoi( q ) )
  1725. {
  1726. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  1727. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  1728. default: goto usage;
  1729. }
  1730. }
  1731. else if( strcmp( p, "extended_ms" ) == 0 )
  1732. {
  1733. switch( atoi( q ) )
  1734. {
  1735. case 0:
  1736. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
  1737. break;
  1738. case 1:
  1739. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1740. break;
  1741. default: goto usage;
  1742. }
  1743. }
  1744. else if( strcmp( p, "etm" ) == 0 )
  1745. {
  1746. switch( atoi( q ) )
  1747. {
  1748. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  1749. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  1750. default: goto usage;
  1751. }
  1752. }
  1753. else if( strcmp( p, "tickets" ) == 0 )
  1754. {
  1755. opt.tickets = atoi( q );
  1756. if( opt.tickets < 0 || opt.tickets > 1 )
  1757. goto usage;
  1758. }
  1759. else if( strcmp( p, "ticket_timeout" ) == 0 )
  1760. {
  1761. opt.ticket_timeout = atoi( q );
  1762. if( opt.ticket_timeout < 0 )
  1763. goto usage;
  1764. }
  1765. else if( strcmp( p, "cache_max" ) == 0 )
  1766. {
  1767. opt.cache_max = atoi( q );
  1768. if( opt.cache_max < 0 )
  1769. goto usage;
  1770. }
  1771. else if( strcmp( p, "cache_timeout" ) == 0 )
  1772. {
  1773. opt.cache_timeout = atoi( q );
  1774. if( opt.cache_timeout < 0 )
  1775. goto usage;
  1776. }
  1777. else if( strcmp( p, "cookies" ) == 0 )
  1778. {
  1779. opt.cookies = atoi( q );
  1780. if( opt.cookies < -1 || opt.cookies > 1)
  1781. goto usage;
  1782. }
  1783. else if( strcmp( p, "anti_replay" ) == 0 )
  1784. {
  1785. opt.anti_replay = atoi( q );
  1786. if( opt.anti_replay < 0 || opt.anti_replay > 1)
  1787. goto usage;
  1788. }
  1789. else if( strcmp( p, "badmac_limit" ) == 0 )
  1790. {
  1791. opt.badmac_limit = atoi( q );
  1792. if( opt.badmac_limit < 0 )
  1793. goto usage;
  1794. }
  1795. else if( strcmp( p, "hs_timeout" ) == 0 )
  1796. {
  1797. if( ( p = strchr( q, '-' ) ) == NULL )
  1798. goto usage;
  1799. *p++ = '\0';
  1800. opt.hs_to_min = atoi( q );
  1801. opt.hs_to_max = atoi( p );
  1802. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  1803. goto usage;
  1804. }
  1805. else if( strcmp( p, "mtu" ) == 0 )
  1806. {
  1807. opt.dtls_mtu = atoi( q );
  1808. if( opt.dtls_mtu < 0 )
  1809. goto usage;
  1810. }
  1811. else if( strcmp( p, "dgram_packing" ) == 0 )
  1812. {
  1813. opt.dgram_packing = atoi( q );
  1814. if( opt.dgram_packing != 0 &&
  1815. opt.dgram_packing != 1 )
  1816. {
  1817. goto usage;
  1818. }
  1819. }
  1820. else if( strcmp( p, "sni" ) == 0 )
  1821. {
  1822. opt.sni = q;
  1823. }
  1824. else if( strcmp( p, "query_config" ) == 0 )
  1825. {
  1826. opt.query_config_mode = 1;
  1827. query_config_ret = query_config( q );
  1828. goto exit;
  1829. }
  1830. else if( strcmp( p, "serialize") == 0 )
  1831. {
  1832. opt.serialize = atoi( q );
  1833. if( opt.serialize < 0 || opt.serialize > 2)
  1834. goto usage;
  1835. }
  1836. else if( strcmp( p, "context_file") == 0 )
  1837. {
  1838. opt.context_file = q;
  1839. }
  1840. else if( strcmp( p, "eap_tls" ) == 0 )
  1841. {
  1842. opt.eap_tls = atoi( q );
  1843. if( opt.eap_tls < 0 || opt.eap_tls > 1 )
  1844. goto usage;
  1845. }
  1846. else if( strcmp( p, "reproducible" ) == 0 )
  1847. {
  1848. opt.reproducible = 1;
  1849. }
  1850. else if( strcmp( p, "nss_keylog" ) == 0 )
  1851. {
  1852. opt.nss_keylog = atoi( q );
  1853. if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
  1854. goto usage;
  1855. }
  1856. else if( strcmp( p, "nss_keylog_file" ) == 0 )
  1857. {
  1858. opt.nss_keylog_file = q;
  1859. }
  1860. else if( strcmp( p, "use_srtp" ) == 0 )
  1861. {
  1862. opt.use_srtp = atoi ( q );
  1863. }
  1864. else if( strcmp( p, "srtp_force_profile" ) == 0 )
  1865. {
  1866. opt.force_srtp_profile = atoi( q );
  1867. }
  1868. else if( strcmp( p, "support_mki" ) == 0 )
  1869. {
  1870. opt.support_mki = atoi( q );
  1871. }
  1872. else
  1873. goto usage;
  1874. }
  1875. if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
  1876. {
  1877. mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
  1878. goto usage;
  1879. }
  1880. /* Event-driven IO is incompatible with the above custom
  1881. * receive and send functions, as the polling builds on
  1882. * refers to the underlying net_context. */
  1883. if( opt.event == 1 && opt.nbio != 1 )
  1884. {
  1885. mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
  1886. opt.nbio = 1;
  1887. }
  1888. #if defined(MBEDTLS_DEBUG_C)
  1889. mbedtls_debug_set_threshold( opt.debug_level );
  1890. #endif
  1891. buf = mbedtls_calloc( 1, opt.buffer_size + 1 );
  1892. if( buf == NULL )
  1893. {
  1894. mbedtls_printf( "Could not allocate %u bytes\n", opt.buffer_size );
  1895. ret = 3;
  1896. goto exit;
  1897. }
  1898. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1899. if( opt.psk_opaque != 0 )
  1900. {
  1901. if( strlen( opt.psk ) == 0 )
  1902. {
  1903. mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
  1904. ret = 2;
  1905. goto usage;
  1906. }
  1907. if( opt.force_ciphersuite[0] <= 0 )
  1908. {
  1909. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1910. ret = 2;
  1911. goto usage;
  1912. }
  1913. }
  1914. if( opt.psk_list_opaque != 0 )
  1915. {
  1916. if( opt.psk_list == NULL )
  1917. {
  1918. mbedtls_printf( "psk_slot set but no psk to be imported specified.\n" );
  1919. ret = 2;
  1920. goto usage;
  1921. }
  1922. if( opt.force_ciphersuite[0] <= 0 )
  1923. {
  1924. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1925. ret = 2;
  1926. goto usage;
  1927. }
  1928. }
  1929. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1930. if( opt.force_ciphersuite[0] > 0 )
  1931. {
  1932. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1933. ciphersuite_info =
  1934. mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  1935. if( opt.max_version != -1 &&
  1936. ciphersuite_info->min_minor_ver > opt.max_version )
  1937. {
  1938. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1939. ret = 2;
  1940. goto usage;
  1941. }
  1942. if( opt.min_version != -1 &&
  1943. ciphersuite_info->max_minor_ver < opt.min_version )
  1944. {
  1945. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1946. ret = 2;
  1947. goto usage;
  1948. }
  1949. /* If we select a version that's not supported by
  1950. * this suite, then there will be no common ciphersuite... */
  1951. if( opt.max_version == -1 ||
  1952. opt.max_version > ciphersuite_info->max_minor_ver )
  1953. {
  1954. opt.max_version = ciphersuite_info->max_minor_ver;
  1955. }
  1956. if( opt.min_version < ciphersuite_info->min_minor_ver )
  1957. {
  1958. opt.min_version = ciphersuite_info->min_minor_ver;
  1959. }
  1960. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1961. if( opt.psk_opaque != 0 || opt.psk_list_opaque != 0 )
  1962. {
  1963. /* Ensure that the chosen ciphersuite is PSK-only; we must know
  1964. * the ciphersuite in advance to set the correct policy for the
  1965. * PSK key slot. This limitation might go away in the future. */
  1966. if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
  1967. opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
  1968. {
  1969. mbedtls_printf( "opaque PSKs are only supported in conjunction with forcing TLS 1.2 and a PSK-only ciphersuite through the 'force_ciphersuite' option.\n" );
  1970. ret = 2;
  1971. goto usage;
  1972. }
  1973. /* Determine KDF algorithm the opaque PSK will be used in. */
  1974. #if defined(MBEDTLS_SHA384_C)
  1975. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  1976. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
  1977. else
  1978. #endif /* MBEDTLS_SHA384_C */
  1979. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
  1980. }
  1981. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1982. }
  1983. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1984. if( mbedtls_test_unhexify( cid, sizeof( cid ),
  1985. opt.cid_val, &cid_len ) != 0 )
  1986. {
  1987. mbedtls_printf( "CID not valid hex\n" );
  1988. goto exit;
  1989. }
  1990. /* Keep CID settings for renegotiation unless
  1991. * specified otherwise. */
  1992. if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
  1993. opt.cid_enabled_renego = opt.cid_enabled;
  1994. if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
  1995. opt.cid_val_renego = opt.cid_val;
  1996. if( mbedtls_test_unhexify( cid_renego, sizeof( cid_renego ),
  1997. opt.cid_val_renego, &cid_renego_len ) != 0 )
  1998. {
  1999. mbedtls_printf( "CID not valid hex\n" );
  2000. goto exit;
  2001. }
  2002. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2003. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2004. /*
  2005. * Unhexify the pre-shared key and parse the list if any given
  2006. */
  2007. if( mbedtls_test_unhexify( psk, sizeof( psk ),
  2008. opt.psk, &psk_len ) != 0 )
  2009. {
  2010. mbedtls_printf( "pre-shared key not valid hex\n" );
  2011. goto exit;
  2012. }
  2013. if( opt.psk_list != NULL )
  2014. {
  2015. if( ( psk_info = psk_parse( opt.psk_list ) ) == NULL )
  2016. {
  2017. mbedtls_printf( "psk_list invalid" );
  2018. goto exit;
  2019. }
  2020. }
  2021. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  2022. #if defined(MBEDTLS_ECP_C)
  2023. if( opt.curves != NULL )
  2024. {
  2025. p = (char *) opt.curves;
  2026. i = 0;
  2027. if( strcmp( p, "none" ) == 0 )
  2028. {
  2029. group_list[0] = 0;
  2030. }
  2031. else if( strcmp( p, "default" ) != 0 )
  2032. {
  2033. /* Leave room for a final NULL in curve list */
  2034. while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
  2035. {
  2036. q = p;
  2037. /* Terminate the current string */
  2038. while( *p != ',' && *p != '\0' )
  2039. p++;
  2040. if( *p == ',' )
  2041. *p++ = '\0';
  2042. if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
  2043. {
  2044. group_list[i++] = curve_cur->tls_id;
  2045. }
  2046. else
  2047. {
  2048. mbedtls_printf( "unknown curve %s\n", q );
  2049. mbedtls_printf( "supported curves: " );
  2050. for( curve_cur = mbedtls_ecp_curve_list();
  2051. curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
  2052. curve_cur++ )
  2053. {
  2054. mbedtls_printf( "%s ", curve_cur->name );
  2055. }
  2056. mbedtls_printf( "\n" );
  2057. goto exit;
  2058. }
  2059. }
  2060. mbedtls_printf("Number of curves: %d\n", i );
  2061. if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
  2062. {
  2063. mbedtls_printf( "curves list too long, maximum %d",
  2064. CURVE_LIST_SIZE - 1 );
  2065. goto exit;
  2066. }
  2067. group_list[i] = 0;
  2068. }
  2069. }
  2070. #endif /* MBEDTLS_ECP_C */
  2071. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  2072. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  2073. if( opt.sig_algs != NULL )
  2074. {
  2075. p = (char *) opt.sig_algs;
  2076. i = 0;
  2077. /* Leave room for a final MBEDTLS_TLS13_SIG_NONE in signature algorithm list (sig_alg_list). */
  2078. while( i < SIG_ALG_LIST_SIZE - 1 && *p != '\0' )
  2079. {
  2080. q = p;
  2081. /* Terminate the current string */
  2082. while( *p != ',' && *p != '\0' )
  2083. p++;
  2084. if( *p == ',' )
  2085. *p++ = '\0';
  2086. if( strcmp( q, "ecdsa_secp256r1_sha256" ) == 0 )
  2087. {
  2088. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256;
  2089. }
  2090. else if( strcmp( q, "ecdsa_secp384r1_sha384" ) == 0 )
  2091. {
  2092. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384;
  2093. }
  2094. else if( strcmp( q, "ecdsa_secp521r1_sha512" ) == 0 )
  2095. {
  2096. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512;
  2097. }
  2098. else
  2099. {
  2100. mbedtls_printf( "unknown signature algorithm %s\n", q );
  2101. mbedtls_printf( "supported signature algorithms: " );
  2102. mbedtls_printf( "ecdsa_secp256r1_sha256 " );
  2103. mbedtls_printf( "ecdsa_secp384r1_sha384 " );
  2104. mbedtls_printf( "ecdsa_secp521r1_sha512 " );
  2105. mbedtls_printf( "\n" );
  2106. goto exit;
  2107. }
  2108. }
  2109. if( i == ( SIG_ALG_LIST_SIZE - 1 ) && *p != '\0' )
  2110. {
  2111. mbedtls_printf( "signature algorithm list too long, maximum %d",
  2112. SIG_ALG_LIST_SIZE - 1 );
  2113. goto exit;
  2114. }
  2115. sig_alg_list[i] = MBEDTLS_TLS13_SIG_NONE;
  2116. }
  2117. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL &&
  2118. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  2119. #if defined(MBEDTLS_SSL_ALPN)
  2120. if( opt.alpn_string != NULL )
  2121. {
  2122. p = (char *) opt.alpn_string;
  2123. i = 0;
  2124. /* Leave room for a final NULL in alpn_list */
  2125. while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
  2126. {
  2127. alpn_list[i++] = p;
  2128. /* Terminate the current string and move on to next one */
  2129. while( *p != ',' && *p != '\0' )
  2130. p++;
  2131. if( *p == ',' )
  2132. *p++ = '\0';
  2133. }
  2134. }
  2135. #endif /* MBEDTLS_SSL_ALPN */
  2136. /*
  2137. * 0. Initialize the RNG and the session data
  2138. */
  2139. mbedtls_printf( "\n . Seeding the random number generator..." );
  2140. fflush( stdout );
  2141. ret = rng_seed( &rng, opt.reproducible, pers );
  2142. if( ret != 0 )
  2143. goto exit;
  2144. mbedtls_printf( " ok\n" );
  2145. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2146. /*
  2147. * 1.1. Load the trusted CA
  2148. */
  2149. mbedtls_printf( " . Loading the CA root certificate ..." );
  2150. fflush( stdout );
  2151. if( strcmp( opt.ca_path, "none" ) == 0 ||
  2152. strcmp( opt.ca_file, "none" ) == 0 )
  2153. {
  2154. ret = 0;
  2155. }
  2156. else
  2157. #if defined(MBEDTLS_FS_IO)
  2158. if( strlen( opt.ca_path ) )
  2159. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  2160. else if( strlen( opt.ca_file ) )
  2161. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  2162. else
  2163. #endif
  2164. {
  2165. #if defined(MBEDTLS_PEM_PARSE_C)
  2166. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  2167. {
  2168. ret = mbedtls_x509_crt_parse( &cacert,
  2169. (const unsigned char *) mbedtls_test_cas[i],
  2170. mbedtls_test_cas_len[i] );
  2171. if( ret != 0 )
  2172. break;
  2173. }
  2174. if( ret == 0 )
  2175. #endif /* MBEDTLS_PEM_PARSE_C */
  2176. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  2177. {
  2178. ret = mbedtls_x509_crt_parse_der( &cacert,
  2179. (const unsigned char *) mbedtls_test_cas_der[i],
  2180. mbedtls_test_cas_der_len[i] );
  2181. if( ret != 0 )
  2182. break;
  2183. }
  2184. }
  2185. if( ret < 0 )
  2186. {
  2187. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n", (unsigned int) -ret );
  2188. goto exit;
  2189. }
  2190. mbedtls_printf( " ok (%d skipped)\n", ret );
  2191. /*
  2192. * 1.2. Load own certificate and private key
  2193. */
  2194. mbedtls_printf( " . Loading the server cert. and key..." );
  2195. fflush( stdout );
  2196. #if defined(MBEDTLS_FS_IO)
  2197. if( strlen( opt.crt_file ) && strcmp( opt.crt_file, "none" ) != 0 )
  2198. {
  2199. key_cert_init++;
  2200. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert, opt.crt_file ) ) != 0 )
  2201. {
  2202. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file returned -0x%x\n\n",
  2203. (unsigned int) -ret );
  2204. goto exit;
  2205. }
  2206. }
  2207. if( strlen( opt.key_file ) && strcmp( opt.key_file, "none" ) != 0 )
  2208. {
  2209. key_cert_init++;
  2210. if( ( ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file,
  2211. opt.key_pwd, rng_get, &rng ) ) != 0 )
  2212. {
  2213. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile returned -0x%x\n\n", (unsigned int) -ret );
  2214. goto exit;
  2215. }
  2216. }
  2217. if( key_cert_init == 1 )
  2218. {
  2219. mbedtls_printf( " failed\n ! crt_file without key_file or vice-versa\n\n" );
  2220. goto exit;
  2221. }
  2222. if( strlen( opt.crt_file2 ) && strcmp( opt.crt_file2, "none" ) != 0 )
  2223. {
  2224. key_cert_init2++;
  2225. if( ( ret = mbedtls_x509_crt_parse_file( &srvcert2, opt.crt_file2 ) ) != 0 )
  2226. {
  2227. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse_file(2) returned -0x%x\n\n",
  2228. (unsigned int) -ret );
  2229. goto exit;
  2230. }
  2231. }
  2232. if( strlen( opt.key_file2 ) && strcmp( opt.key_file2, "none" ) != 0 )
  2233. {
  2234. key_cert_init2++;
  2235. if( ( ret = mbedtls_pk_parse_keyfile( &pkey2, opt.key_file2,
  2236. opt.key_pwd2, rng_get, &rng ) ) != 0 )
  2237. {
  2238. mbedtls_printf( " failed\n ! mbedtls_pk_parse_keyfile(2) returned -0x%x\n\n",
  2239. (unsigned int) -ret );
  2240. goto exit;
  2241. }
  2242. }
  2243. if( key_cert_init2 == 1 )
  2244. {
  2245. mbedtls_printf( " failed\n ! crt_file2 without key_file2 or vice-versa\n\n" );
  2246. goto exit;
  2247. }
  2248. #endif
  2249. if( key_cert_init == 0 &&
  2250. strcmp( opt.crt_file, "none" ) != 0 &&
  2251. strcmp( opt.key_file, "none" ) != 0 &&
  2252. key_cert_init2 == 0 &&
  2253. strcmp( opt.crt_file2, "none" ) != 0 &&
  2254. strcmp( opt.key_file2, "none" ) != 0 )
  2255. {
  2256. #if defined(MBEDTLS_RSA_C)
  2257. if( ( ret = mbedtls_x509_crt_parse( &srvcert,
  2258. (const unsigned char *) mbedtls_test_srv_crt_rsa,
  2259. mbedtls_test_srv_crt_rsa_len ) ) != 0 )
  2260. {
  2261. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  2262. (unsigned int) -ret );
  2263. goto exit;
  2264. }
  2265. if( ( ret = mbedtls_pk_parse_key( &pkey,
  2266. (const unsigned char *) mbedtls_test_srv_key_rsa,
  2267. mbedtls_test_srv_key_rsa_len, NULL, 0,
  2268. rng_get, &rng ) ) != 0 )
  2269. {
  2270. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
  2271. (unsigned int) -ret );
  2272. goto exit;
  2273. }
  2274. key_cert_init = 2;
  2275. #endif /* MBEDTLS_RSA_C */
  2276. #if defined(MBEDTLS_ECDSA_C)
  2277. if( ( ret = mbedtls_x509_crt_parse( &srvcert2,
  2278. (const unsigned char *) mbedtls_test_srv_crt_ec,
  2279. mbedtls_test_srv_crt_ec_len ) ) != 0 )
  2280. {
  2281. mbedtls_printf( " failed\n ! x509_crt_parse2 returned -0x%x\n\n",
  2282. (unsigned int) -ret );
  2283. goto exit;
  2284. }
  2285. if( ( ret = mbedtls_pk_parse_key( &pkey2,
  2286. (const unsigned char *) mbedtls_test_srv_key_ec,
  2287. mbedtls_test_srv_key_ec_len, NULL, 0,
  2288. rng_get, &rng ) ) != 0 )
  2289. {
  2290. mbedtls_printf( " failed\n ! pk_parse_key2 returned -0x%x\n\n",
  2291. (unsigned int) -ret );
  2292. goto exit;
  2293. }
  2294. key_cert_init2 = 2;
  2295. #endif /* MBEDTLS_ECDSA_C */
  2296. }
  2297. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2298. if( opt.key_opaque != 0 )
  2299. {
  2300. if ( mbedtls_pk_get_type( &pkey ) == MBEDTLS_PK_ECKEY )
  2301. {
  2302. if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
  2303. PSA_ALG_ANY_HASH ) ) != 0 )
  2304. {
  2305. mbedtls_printf( " failed\n ! "
  2306. "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", (unsigned int) -ret );
  2307. goto exit;
  2308. }
  2309. }
  2310. if ( mbedtls_pk_get_type( &pkey2 ) == MBEDTLS_PK_ECKEY )
  2311. {
  2312. if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey2, &key_slot2,
  2313. PSA_ALG_ANY_HASH ) ) != 0 )
  2314. {
  2315. mbedtls_printf( " failed\n ! "
  2316. "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", (unsigned int) -ret );
  2317. goto exit;
  2318. }
  2319. }
  2320. }
  2321. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2322. mbedtls_printf( " ok (key types: %s - %s)\n", mbedtls_pk_get_name( &pkey ), mbedtls_pk_get_name( &pkey2 ) );
  2323. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2324. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  2325. if( opt.dhm_file != NULL )
  2326. {
  2327. mbedtls_printf( " . Loading DHM parameters..." );
  2328. fflush( stdout );
  2329. if( ( ret = mbedtls_dhm_parse_dhmfile( &dhm, opt.dhm_file ) ) != 0 )
  2330. {
  2331. mbedtls_printf( " failed\n ! mbedtls_dhm_parse_dhmfile returned -0x%04X\n\n",
  2332. (unsigned int) -ret );
  2333. goto exit;
  2334. }
  2335. mbedtls_printf( " ok\n" );
  2336. }
  2337. #endif
  2338. #if defined(SNI_OPTION)
  2339. if( opt.sni != NULL )
  2340. {
  2341. mbedtls_printf( " . Setting up SNI information..." );
  2342. fflush( stdout );
  2343. if( ( sni_info = sni_parse( opt.sni ) ) == NULL )
  2344. {
  2345. mbedtls_printf( " failed\n" );
  2346. goto exit;
  2347. }
  2348. mbedtls_printf( " ok\n" );
  2349. }
  2350. #endif /* SNI_OPTION */
  2351. /*
  2352. * 2. Setup stuff
  2353. */
  2354. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  2355. fflush( stdout );
  2356. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  2357. MBEDTLS_SSL_IS_SERVER,
  2358. opt.transport,
  2359. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  2360. {
  2361. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n", (unsigned int) -ret );
  2362. goto exit;
  2363. }
  2364. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2365. /* The default algorithms profile disables SHA-1, but our tests still
  2366. rely on it heavily. Hence we allow it here. A real-world server
  2367. should use the default profile unless there is a good reason not to. */
  2368. if( opt.allow_sha1 > 0 )
  2369. {
  2370. crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
  2371. mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
  2372. mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
  2373. }
  2374. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2375. if( opt.auth_mode != DFL_AUTH_MODE )
  2376. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  2377. if( opt.cert_req_ca_list != DFL_CERT_REQ_CA_LIST )
  2378. mbedtls_ssl_conf_cert_req_ca_list( &conf, opt.cert_req_ca_list );
  2379. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2380. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  2381. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min, opt.hs_to_max );
  2382. if( opt.dgram_packing != DFL_DGRAM_PACKING )
  2383. mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
  2384. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2385. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2386. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  2387. {
  2388. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n", ret );
  2389. goto exit;
  2390. }
  2391. #endif
  2392. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2393. if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
  2394. {
  2395. if( opt.cid_enabled == 1 &&
  2396. opt.cid_enabled_renego == 1 &&
  2397. cid_len != cid_renego_len )
  2398. {
  2399. mbedtls_printf( "CID length must not change during renegotiation\n" );
  2400. goto usage;
  2401. }
  2402. if( opt.cid_enabled == 1 )
  2403. ret = mbedtls_ssl_conf_cid( &conf, cid_len,
  2404. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  2405. else
  2406. ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
  2407. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  2408. if( ret != 0 )
  2409. {
  2410. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
  2411. (unsigned int) -ret );
  2412. goto exit;
  2413. }
  2414. }
  2415. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2416. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  2417. const mbedtls_ssl_srtp_profile forced_profile[] = { opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET };
  2418. if( opt.use_srtp == 1 )
  2419. {
  2420. if( opt.force_srtp_profile != 0 )
  2421. {
  2422. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, forced_profile );
  2423. }
  2424. else
  2425. {
  2426. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles( &conf, default_profiles );
  2427. }
  2428. if( ret != 0 )
  2429. {
  2430. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n", ret );
  2431. goto exit;
  2432. }
  2433. mbedtls_ssl_conf_srtp_mki_value_supported( &conf,
  2434. opt.support_mki ?
  2435. MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED :
  2436. MBEDTLS_SSL_DTLS_SRTP_MKI_UNSUPPORTED );
  2437. }
  2438. else if( opt.force_srtp_profile != 0 )
  2439. {
  2440. mbedtls_printf( " failed\n ! must enable use_srtp to force srtp profile\n\n" );
  2441. goto exit;
  2442. }
  2443. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2444. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  2445. if( opt.extended_ms != DFL_EXTENDED_MS )
  2446. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  2447. #endif
  2448. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  2449. if( opt.etm != DFL_ETM )
  2450. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  2451. #endif
  2452. #if defined(MBEDTLS_SSL_ALPN)
  2453. if( opt.alpn_string != NULL )
  2454. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  2455. {
  2456. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n", ret );
  2457. goto exit;
  2458. }
  2459. #endif
  2460. if (opt.reproducible)
  2461. {
  2462. #if defined(MBEDTLS_HAVE_TIME)
  2463. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  2464. mbedtls_platform_set_time( dummy_constant_time );
  2465. #else
  2466. fprintf( stderr, "Warning: reproducible option used without constant time\n" );
  2467. #endif
  2468. #endif /* MBEDTLS_HAVE_TIME */
  2469. }
  2470. mbedtls_ssl_conf_rng( &conf, rng_get, &rng );
  2471. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  2472. #if defined(MBEDTLS_SSL_CACHE_C)
  2473. if( opt.cache_max != -1 )
  2474. mbedtls_ssl_cache_set_max_entries( &cache, opt.cache_max );
  2475. if( opt.cache_timeout != -1 )
  2476. mbedtls_ssl_cache_set_timeout( &cache, opt.cache_timeout );
  2477. mbedtls_ssl_conf_session_cache( &conf, &cache,
  2478. mbedtls_ssl_cache_get,
  2479. mbedtls_ssl_cache_set );
  2480. #endif
  2481. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  2482. if( opt.tickets == MBEDTLS_SSL_SESSION_TICKETS_ENABLED )
  2483. {
  2484. if( ( ret = mbedtls_ssl_ticket_setup( &ticket_ctx,
  2485. rng_get, &rng,
  2486. MBEDTLS_CIPHER_AES_256_GCM,
  2487. opt.ticket_timeout ) ) != 0 )
  2488. {
  2489. mbedtls_printf( " failed\n ! mbedtls_ssl_ticket_setup returned %d\n\n", ret );
  2490. goto exit;
  2491. }
  2492. mbedtls_ssl_conf_session_tickets_cb( &conf,
  2493. mbedtls_ssl_ticket_write,
  2494. mbedtls_ssl_ticket_parse,
  2495. &ticket_ctx );
  2496. }
  2497. #endif
  2498. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2499. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2500. {
  2501. #if defined(MBEDTLS_SSL_COOKIE_C)
  2502. if( opt.cookies > 0 )
  2503. {
  2504. if( ( ret = mbedtls_ssl_cookie_setup( &cookie_ctx,
  2505. rng_get, &rng ) ) != 0 )
  2506. {
  2507. mbedtls_printf( " failed\n ! mbedtls_ssl_cookie_setup returned %d\n\n", ret );
  2508. goto exit;
  2509. }
  2510. mbedtls_ssl_conf_dtls_cookies( &conf, mbedtls_ssl_cookie_write, mbedtls_ssl_cookie_check,
  2511. &cookie_ctx );
  2512. }
  2513. else
  2514. #endif /* MBEDTLS_SSL_COOKIE_C */
  2515. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2516. if( opt.cookies == 0 )
  2517. {
  2518. mbedtls_ssl_conf_dtls_cookies( &conf, NULL, NULL, NULL );
  2519. }
  2520. else
  2521. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2522. {
  2523. ; /* Nothing to do */
  2524. }
  2525. #if defined(MBEDTLS_SSL_DTLS_ANTI_REPLAY)
  2526. if( opt.anti_replay != DFL_ANTI_REPLAY )
  2527. mbedtls_ssl_conf_dtls_anti_replay( &conf, opt.anti_replay );
  2528. #endif
  2529. if( opt.badmac_limit != DFL_BADMAC_LIMIT )
  2530. mbedtls_ssl_conf_dtls_badmac_limit( &conf, opt.badmac_limit );
  2531. }
  2532. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  2533. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  2534. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  2535. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  2536. mbedtls_ssl_conf_tls13_key_exchange_modes( &conf, opt.tls13_kex_modes );
  2537. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  2538. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  2539. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  2540. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2541. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  2542. if( opt.renego_delay != DFL_RENEGO_DELAY )
  2543. mbedtls_ssl_conf_renegotiation_enforced( &conf, opt.renego_delay );
  2544. if( opt.renego_period != DFL_RENEGO_PERIOD )
  2545. {
  2546. PUT_UINT64_BE( renego_period, opt.renego_period, 0 );
  2547. mbedtls_ssl_conf_renegotiation_period( &conf, renego_period );
  2548. }
  2549. #endif
  2550. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2551. if( strcmp( opt.ca_path, "none" ) != 0 &&
  2552. strcmp( opt.ca_file, "none" ) != 0 )
  2553. {
  2554. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  2555. if( opt.ca_callback != 0 )
  2556. mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert);
  2557. else
  2558. #endif
  2559. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  2560. }
  2561. if( key_cert_init )
  2562. {
  2563. mbedtls_pk_context *pk = &pkey;
  2564. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2565. if( opt.async_private_delay1 >= 0 )
  2566. {
  2567. ret = ssl_async_set_key( &ssl_async_keys, &srvcert, pk, 0,
  2568. opt.async_private_delay1 );
  2569. if( ret < 0 )
  2570. {
  2571. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2572. ret );
  2573. goto exit;
  2574. }
  2575. pk = NULL;
  2576. }
  2577. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2578. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert, pk ) ) != 0 )
  2579. {
  2580. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2581. goto exit;
  2582. }
  2583. }
  2584. if( key_cert_init2 )
  2585. {
  2586. mbedtls_pk_context *pk = &pkey2;
  2587. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2588. if( opt.async_private_delay2 >= 0 )
  2589. {
  2590. ret = ssl_async_set_key( &ssl_async_keys, &srvcert2, pk, 0,
  2591. opt.async_private_delay2 );
  2592. if( ret < 0 )
  2593. {
  2594. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2595. ret );
  2596. goto exit;
  2597. }
  2598. pk = NULL;
  2599. }
  2600. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2601. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &srvcert2, pk ) ) != 0 )
  2602. {
  2603. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n", ret );
  2604. goto exit;
  2605. }
  2606. }
  2607. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2608. if( opt.async_operations[0] != '-' )
  2609. {
  2610. mbedtls_ssl_async_sign_t *sign = NULL;
  2611. mbedtls_ssl_async_decrypt_t *decrypt = NULL;
  2612. const char *r;
  2613. for( r = opt.async_operations; *r; r++ )
  2614. {
  2615. switch( *r )
  2616. {
  2617. case 'd':
  2618. decrypt = ssl_async_decrypt;
  2619. break;
  2620. case 's':
  2621. sign = ssl_async_sign;
  2622. break;
  2623. }
  2624. }
  2625. ssl_async_keys.inject_error = ( opt.async_private_error < 0 ?
  2626. - opt.async_private_error :
  2627. opt.async_private_error );
  2628. ssl_async_keys.f_rng = rng_get;
  2629. ssl_async_keys.p_rng = &rng;
  2630. mbedtls_ssl_conf_async_private_cb( &conf,
  2631. sign,
  2632. decrypt,
  2633. ssl_async_resume,
  2634. ssl_async_cancel,
  2635. &ssl_async_keys );
  2636. }
  2637. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2638. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2639. #if defined(SNI_OPTION)
  2640. if( opt.sni != NULL )
  2641. {
  2642. mbedtls_ssl_conf_sni( &conf, sni_callback, sni_info );
  2643. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2644. if( opt.async_private_delay2 >= 0 )
  2645. {
  2646. sni_entry *cur;
  2647. for( cur = sni_info; cur != NULL; cur = cur->next )
  2648. {
  2649. ret = ssl_async_set_key( &ssl_async_keys,
  2650. cur->cert, cur->key, 1,
  2651. opt.async_private_delay2 );
  2652. if( ret < 0 )
  2653. {
  2654. mbedtls_printf( " Test error: ssl_async_set_key failed (%d)\n",
  2655. ret );
  2656. goto exit;
  2657. }
  2658. cur->key = NULL;
  2659. }
  2660. }
  2661. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2662. }
  2663. #endif
  2664. #if defined(MBEDTLS_ECP_C)
  2665. if( opt.curves != NULL &&
  2666. strcmp( opt.curves, "default" ) != 0 )
  2667. {
  2668. mbedtls_ssl_conf_groups( &conf, group_list );
  2669. }
  2670. #endif
  2671. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  2672. if( opt.sig_algs != NULL )
  2673. mbedtls_ssl_conf_sig_algs( &conf, sig_alg_list );
  2674. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  2675. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  2676. if( strlen( opt.psk ) != 0 && strlen( opt.psk_identity ) != 0 )
  2677. {
  2678. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2679. if( opt.psk_opaque != 0 )
  2680. {
  2681. /* The algorithm has already been determined earlier. */
  2682. status = psa_setup_psk_key_slot( &psk_slot, alg, psk, psk_len );
  2683. if( status != PSA_SUCCESS )
  2684. {
  2685. fprintf( stderr, "SETUP FAIL\n" );
  2686. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2687. goto exit;
  2688. }
  2689. if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, psk_slot,
  2690. (const unsigned char *) opt.psk_identity,
  2691. strlen( opt.psk_identity ) ) ) != 0 )
  2692. {
  2693. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
  2694. ret );
  2695. goto exit;
  2696. }
  2697. }
  2698. else
  2699. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2700. if( psk_len > 0 )
  2701. {
  2702. ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  2703. (const unsigned char *) opt.psk_identity,
  2704. strlen( opt.psk_identity ) );
  2705. if( ret != 0 )
  2706. {
  2707. mbedtls_printf( " failed\n mbedtls_ssl_conf_psk returned -0x%04X\n\n", (unsigned int) -ret );
  2708. goto exit;
  2709. }
  2710. }
  2711. }
  2712. if( opt.psk_list != NULL )
  2713. {
  2714. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2715. if( opt.psk_list_opaque != 0 )
  2716. {
  2717. psk_entry *cur_psk;
  2718. for( cur_psk = psk_info; cur_psk != NULL; cur_psk = cur_psk->next )
  2719. {
  2720. status = psa_setup_psk_key_slot( &cur_psk->slot, alg,
  2721. cur_psk->key,
  2722. cur_psk->key_len );
  2723. if( status != PSA_SUCCESS )
  2724. {
  2725. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2726. goto exit;
  2727. }
  2728. }
  2729. }
  2730. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2731. mbedtls_ssl_conf_psk_cb( &conf, psk_callback, psk_info );
  2732. }
  2733. #endif
  2734. #if defined(MBEDTLS_DHM_C)
  2735. /*
  2736. * Use different group than default DHM group
  2737. */
  2738. #if defined(MBEDTLS_FS_IO)
  2739. if( opt.dhm_file != NULL )
  2740. ret = mbedtls_ssl_conf_dh_param_ctx( &conf, &dhm );
  2741. #endif
  2742. if( ret != 0 )
  2743. {
  2744. mbedtls_printf( " failed\n mbedtls_ssl_conf_dh_param returned -0x%04X\n\n", (unsigned int) -ret );
  2745. goto exit;
  2746. }
  2747. #endif
  2748. if( opt.min_version != DFL_MIN_VERSION )
  2749. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.min_version );
  2750. if( opt.max_version != DFL_MIN_VERSION )
  2751. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3, opt.max_version );
  2752. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  2753. {
  2754. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n", (unsigned int) -ret );
  2755. goto exit;
  2756. }
  2757. if( opt.eap_tls != 0 )
  2758. {
  2759. mbedtls_ssl_set_export_keys_cb( &ssl, eap_tls_key_derivation,
  2760. &eap_tls_keying );
  2761. }
  2762. else if( opt.nss_keylog != 0 )
  2763. {
  2764. mbedtls_ssl_set_export_keys_cb( &ssl,
  2765. nss_keylog_export,
  2766. NULL );
  2767. }
  2768. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  2769. else if( opt.use_srtp != 0 )
  2770. {
  2771. mbedtls_ssl_set_export_keys_cb( &ssl, dtls_srtp_key_derivation,
  2772. &dtls_srtp_keying );
  2773. }
  2774. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2775. io_ctx.ssl = &ssl;
  2776. io_ctx.net = &client_fd;
  2777. mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
  2778. opt.nbio == 0 ? recv_timeout_cb : NULL );
  2779. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2780. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2781. {
  2782. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
  2783. cid, cid_len ) ) != 0 )
  2784. {
  2785. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  2786. ret );
  2787. goto exit;
  2788. }
  2789. }
  2790. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2791. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  2792. if( opt.dtls_mtu != DFL_DTLS_MTU )
  2793. mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
  2794. #endif
  2795. #if defined(MBEDTLS_TIMING_C)
  2796. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  2797. mbedtls_timing_get_delay );
  2798. #endif
  2799. mbedtls_printf( " ok\n" );
  2800. /*
  2801. * 3. Setup the listening TCP socket
  2802. */
  2803. mbedtls_printf( " . Bind on %s://%s:%s/ ...",
  2804. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  2805. opt.server_addr ? opt.server_addr : "*",
  2806. opt.server_port );
  2807. fflush( stdout );
  2808. if( ( ret = mbedtls_net_bind( &listen_fd, opt.server_addr, opt.server_port,
  2809. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  2810. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  2811. {
  2812. mbedtls_printf( " failed\n ! mbedtls_net_bind returned -0x%x\n\n", (unsigned int) -ret );
  2813. goto exit;
  2814. }
  2815. mbedtls_printf( " ok\n" );
  2816. reset:
  2817. #if !defined(_WIN32)
  2818. if( received_sigterm )
  2819. {
  2820. mbedtls_printf( " interrupted by SIGTERM (not in net_accept())\n" );
  2821. if( ret == MBEDTLS_ERR_NET_INVALID_CONTEXT )
  2822. ret = 0;
  2823. goto exit;
  2824. }
  2825. #endif
  2826. if( ret == MBEDTLS_ERR_SSL_CLIENT_RECONNECT )
  2827. {
  2828. mbedtls_printf( " ! Client initiated reconnection from same port\n" );
  2829. goto handshake;
  2830. }
  2831. #ifdef MBEDTLS_ERROR_C
  2832. if( ret != 0 )
  2833. {
  2834. char error_buf[100];
  2835. mbedtls_strerror( ret, error_buf, 100 );
  2836. mbedtls_printf("Last error was: %d - %s\n\n", ret, error_buf );
  2837. }
  2838. #endif
  2839. mbedtls_net_free( &client_fd );
  2840. mbedtls_ssl_session_reset( &ssl );
  2841. /*
  2842. * 3. Wait until a client connects
  2843. */
  2844. mbedtls_printf( " . Waiting for a remote connection ..." );
  2845. fflush( stdout );
  2846. if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
  2847. client_ip, sizeof( client_ip ), &cliip_len ) ) != 0 )
  2848. {
  2849. #if !defined(_WIN32)
  2850. if( received_sigterm )
  2851. {
  2852. mbedtls_printf( " interrupted by SIGTERM (in net_accept())\n" );
  2853. if( ret == MBEDTLS_ERR_NET_ACCEPT_FAILED )
  2854. ret = 0;
  2855. goto exit;
  2856. }
  2857. #endif
  2858. mbedtls_printf( " failed\n ! mbedtls_net_accept returned -0x%x\n\n", (unsigned int) -ret );
  2859. goto exit;
  2860. }
  2861. if( opt.nbio > 0 )
  2862. ret = mbedtls_net_set_nonblock( &client_fd );
  2863. else
  2864. ret = mbedtls_net_set_block( &client_fd );
  2865. if( ret != 0 )
  2866. {
  2867. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n", (unsigned int) -ret );
  2868. goto exit;
  2869. }
  2870. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  2871. #if defined(MBEDTLS_SSL_DTLS_HELLO_VERIFY)
  2872. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2873. {
  2874. if( ( ret = mbedtls_ssl_set_client_transport_id( &ssl,
  2875. client_ip, cliip_len ) ) != 0 )
  2876. {
  2877. mbedtls_printf( " failed\n ! mbedtls_ssl_set_client_transport_id() returned -0x%x\n\n",
  2878. (unsigned int) -ret );
  2879. goto exit;
  2880. }
  2881. }
  2882. #endif /* MBEDTLS_SSL_DTLS_HELLO_VERIFY */
  2883. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  2884. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  2885. {
  2886. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  2887. (const unsigned char *) opt.ecjpake_pw,
  2888. strlen( opt.ecjpake_pw ) ) ) != 0 )
  2889. {
  2890. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n", ret );
  2891. goto exit;
  2892. }
  2893. }
  2894. #endif
  2895. mbedtls_printf( " ok\n" );
  2896. /*
  2897. * 4. Handshake
  2898. */
  2899. handshake:
  2900. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  2901. fflush( stdout );
  2902. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  2903. {
  2904. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2905. if( ret == MBEDTLS_ERR_SSL_ASYNC_IN_PROGRESS &&
  2906. ssl_async_keys.inject_error == SSL_ASYNC_INJECT_ERROR_CANCEL )
  2907. {
  2908. mbedtls_printf( " cancelling on injected error\n" );
  2909. break;
  2910. }
  2911. #endif /* MBEDTLS_SSL_ASYNC_PRIVATE */
  2912. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  2913. break;
  2914. /* For event-driven IO, wait for socket to become available */
  2915. if( opt.event == 1 /* level triggered IO */ )
  2916. {
  2917. #if defined(MBEDTLS_TIMING_C)
  2918. ret = idle( &client_fd, &timer, ret );
  2919. #else
  2920. ret = idle( &client_fd, ret );
  2921. #endif
  2922. if( ret != 0 )
  2923. goto reset;
  2924. }
  2925. }
  2926. if( ret == MBEDTLS_ERR_SSL_HELLO_VERIFY_REQUIRED )
  2927. {
  2928. mbedtls_printf( " hello verification requested\n" );
  2929. ret = 0;
  2930. goto reset;
  2931. }
  2932. else if( ret != 0 )
  2933. {
  2934. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n", (unsigned int) -ret );
  2935. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2936. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  2937. {
  2938. char vrfy_buf[512];
  2939. flags = mbedtls_ssl_get_verify_result( &ssl );
  2940. x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2941. mbedtls_printf( "%s\n", vrfy_buf );
  2942. }
  2943. #endif
  2944. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  2945. if( opt.async_private_error < 0 )
  2946. /* Injected error only the first time round, to test reset */
  2947. ssl_async_keys.inject_error = SSL_ASYNC_INJECT_ERROR_NONE;
  2948. #endif
  2949. goto reset;
  2950. }
  2951. else /* ret == 0 */
  2952. {
  2953. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  2954. mbedtls_ssl_get_version( &ssl ), mbedtls_ssl_get_ciphersuite( &ssl ) );
  2955. }
  2956. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  2957. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  2958. else
  2959. mbedtls_printf( " [ Record expansion is unknown ]\n" );
  2960. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  2961. mbedtls_printf( " [ Maximum incoming record payload length is %u ]\n",
  2962. (unsigned int) mbedtls_ssl_get_max_in_record_payload( &ssl ) );
  2963. mbedtls_printf( " [ Maximum outgoing record payload length is %u ]\n",
  2964. (unsigned int) mbedtls_ssl_get_max_out_record_payload( &ssl ) );
  2965. #endif
  2966. #if defined(MBEDTLS_SSL_ALPN)
  2967. if( opt.alpn_string != NULL )
  2968. {
  2969. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  2970. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  2971. alp ? alp : "(none)" );
  2972. }
  2973. #endif
  2974. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2975. /*
  2976. * 5. Verify the client certificate
  2977. */
  2978. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  2979. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  2980. {
  2981. char vrfy_buf[512];
  2982. mbedtls_printf( " failed\n" );
  2983. x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ), " ! ", flags );
  2984. mbedtls_printf( "%s\n", vrfy_buf );
  2985. }
  2986. else
  2987. mbedtls_printf( " ok\n" );
  2988. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  2989. if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
  2990. {
  2991. char crt_buf[512];
  2992. mbedtls_printf( " . Peer certificate information ...\n" );
  2993. mbedtls_x509_crt_info( crt_buf, sizeof( crt_buf ), " ",
  2994. mbedtls_ssl_get_peer_cert( &ssl ) );
  2995. mbedtls_printf( "%s\n", crt_buf );
  2996. }
  2997. #endif /* MBEDTLS_X509_REMOVE_INFO */
  2998. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2999. if( opt.eap_tls != 0 )
  3000. {
  3001. size_t j = 0;
  3002. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
  3003. eap_tls_keying.master_secret,
  3004. sizeof( eap_tls_keying.master_secret ),
  3005. eap_tls_label,
  3006. eap_tls_keying.randbytes,
  3007. sizeof( eap_tls_keying.randbytes ),
  3008. eap_tls_keymaterial,
  3009. sizeof( eap_tls_keymaterial ) ) )
  3010. != 0 )
  3011. {
  3012. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  3013. (unsigned int) -ret );
  3014. goto reset;
  3015. }
  3016. mbedtls_printf( " EAP-TLS key material is:" );
  3017. for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
  3018. {
  3019. if( j % 8 == 0 )
  3020. mbedtls_printf("\n ");
  3021. mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
  3022. }
  3023. mbedtls_printf("\n");
  3024. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
  3025. eap_tls_label,
  3026. eap_tls_keying.randbytes,
  3027. sizeof( eap_tls_keying.randbytes ),
  3028. eap_tls_iv,
  3029. sizeof( eap_tls_iv ) ) ) != 0 )
  3030. {
  3031. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  3032. (unsigned int) -ret );
  3033. goto reset;
  3034. }
  3035. mbedtls_printf( " EAP-TLS IV is:" );
  3036. for( j = 0; j < sizeof( eap_tls_iv ); j++ )
  3037. {
  3038. if( j % 8 == 0 )
  3039. mbedtls_printf("\n ");
  3040. mbedtls_printf("%02x ", eap_tls_iv[j] );
  3041. }
  3042. mbedtls_printf("\n");
  3043. }
  3044. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  3045. else if( opt.use_srtp != 0 )
  3046. {
  3047. size_t j = 0;
  3048. mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
  3049. mbedtls_ssl_get_dtls_srtp_negotiation_result( &ssl, &dtls_srtp_negotiation_result );
  3050. if( dtls_srtp_negotiation_result.chosen_dtls_srtp_profile
  3051. == MBEDTLS_TLS_SRTP_UNSET )
  3052. {
  3053. mbedtls_printf( " Unable to negotiate "
  3054. "the use of DTLS-SRTP\n" );
  3055. }
  3056. else
  3057. {
  3058. if( ( ret = mbedtls_ssl_tls_prf( dtls_srtp_keying.tls_prf_type,
  3059. dtls_srtp_keying.master_secret,
  3060. sizeof( dtls_srtp_keying.master_secret ),
  3061. dtls_srtp_label,
  3062. dtls_srtp_keying.randbytes,
  3063. sizeof( dtls_srtp_keying.randbytes ),
  3064. dtls_srtp_key_material,
  3065. sizeof( dtls_srtp_key_material ) ) )
  3066. != 0 )
  3067. {
  3068. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  3069. (unsigned int) -ret );
  3070. goto exit;
  3071. }
  3072. mbedtls_printf( " DTLS-SRTP key material is:" );
  3073. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  3074. {
  3075. if( j % 8 == 0 )
  3076. mbedtls_printf( "\n " );
  3077. mbedtls_printf( "%02x ", dtls_srtp_key_material[j] );
  3078. }
  3079. mbedtls_printf( "\n" );
  3080. /* produce a less readable output used to perform automatic checks
  3081. * - compare client and server output
  3082. * - interop test with openssl which client produces this kind of output
  3083. */
  3084. mbedtls_printf( " Keying material: " );
  3085. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  3086. {
  3087. mbedtls_printf( "%02X", dtls_srtp_key_material[j] );
  3088. }
  3089. mbedtls_printf( "\n" );
  3090. if ( dtls_srtp_negotiation_result.mki_len > 0 )
  3091. {
  3092. mbedtls_printf( " DTLS-SRTP mki value: " );
  3093. for( j = 0; j < dtls_srtp_negotiation_result.mki_len; j++ )
  3094. {
  3095. mbedtls_printf( "%02X", dtls_srtp_negotiation_result.mki_value[j] );
  3096. }
  3097. }
  3098. else
  3099. {
  3100. mbedtls_printf( " DTLS-SRTP no mki value negotiated" );
  3101. }
  3102. mbedtls_printf( "\n" );
  3103. }
  3104. }
  3105. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  3106. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  3107. ret = report_cid_usage( &ssl, "initial handshake" );
  3108. if( ret != 0 )
  3109. goto exit;
  3110. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  3111. {
  3112. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
  3113. cid_renego, cid_renego_len ) ) != 0 )
  3114. {
  3115. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  3116. ret );
  3117. goto exit;
  3118. }
  3119. }
  3120. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  3121. #if defined(MBEDTLS_MEMORY_DEBUG)
  3122. mbedtls_memory_buffer_alloc_cur_get( &current_heap_memory, &heap_blocks );
  3123. mbedtls_memory_buffer_alloc_max_get( &peak_heap_memory, &heap_blocks );
  3124. mbedtls_printf( "Heap memory usage after handshake: %lu bytes. Peak memory usage was %lu\n",
  3125. (unsigned long) current_heap_memory, (unsigned long) peak_heap_memory );
  3126. #endif /* MBEDTLS_MEMORY_DEBUG */
  3127. if( opt.exchanges == 0 )
  3128. goto close_notify;
  3129. exchanges_left = opt.exchanges;
  3130. data_exchange:
  3131. /*
  3132. * 6. Read the HTTP Request
  3133. */
  3134. mbedtls_printf( " < Read from client:" );
  3135. fflush( stdout );
  3136. /*
  3137. * TLS and DTLS need different reading styles (stream vs datagram)
  3138. */
  3139. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  3140. {
  3141. do
  3142. {
  3143. int terminated = 0;
  3144. len = opt.buffer_size - 1;
  3145. memset( buf, 0, opt.buffer_size );
  3146. ret = mbedtls_ssl_read( &ssl, buf, len );
  3147. if( mbedtls_status_is_ssl_in_progress( ret ) )
  3148. {
  3149. if( opt.event == 1 /* level triggered IO */ )
  3150. {
  3151. #if defined(MBEDTLS_TIMING_C)
  3152. idle( &client_fd, &timer, ret );
  3153. #else
  3154. idle( &client_fd, ret );
  3155. #endif
  3156. }
  3157. continue;
  3158. }
  3159. if( ret <= 0 )
  3160. {
  3161. switch( ret )
  3162. {
  3163. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  3164. mbedtls_printf( " connection was closed gracefully\n" );
  3165. goto close_notify;
  3166. case 0:
  3167. case MBEDTLS_ERR_NET_CONN_RESET:
  3168. mbedtls_printf( " connection was reset by peer\n" );
  3169. ret = MBEDTLS_ERR_NET_CONN_RESET;
  3170. goto reset;
  3171. default:
  3172. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
  3173. goto reset;
  3174. }
  3175. }
  3176. if( mbedtls_ssl_get_bytes_avail( &ssl ) == 0 )
  3177. {
  3178. len = ret;
  3179. buf[len] = '\0';
  3180. mbedtls_printf( " %d bytes read\n\n%s\n", len, (char *) buf );
  3181. /* End of message should be detected according to the syntax of the
  3182. * application protocol (eg HTTP), just use a dummy test here. */
  3183. if( buf[len - 1] == '\n' )
  3184. terminated = 1;
  3185. }
  3186. else
  3187. {
  3188. int extra_len, ori_len;
  3189. unsigned char *larger_buf;
  3190. ori_len = ret;
  3191. extra_len = (int) mbedtls_ssl_get_bytes_avail( &ssl );
  3192. larger_buf = mbedtls_calloc( 1, ori_len + extra_len + 1 );
  3193. if( larger_buf == NULL )
  3194. {
  3195. mbedtls_printf( " ! memory allocation failed\n" );
  3196. ret = 1;
  3197. goto reset;
  3198. }
  3199. memset( larger_buf, 0, ori_len + extra_len );
  3200. memcpy( larger_buf, buf, ori_len );
  3201. /* This read should never fail and get the whole cached data */
  3202. ret = mbedtls_ssl_read( &ssl, larger_buf + ori_len, extra_len );
  3203. if( ret != extra_len ||
  3204. mbedtls_ssl_get_bytes_avail( &ssl ) != 0 )
  3205. {
  3206. mbedtls_printf( " ! mbedtls_ssl_read failed on cached data\n" );
  3207. ret = 1;
  3208. goto reset;
  3209. }
  3210. larger_buf[ori_len + extra_len] = '\0';
  3211. mbedtls_printf( " %d bytes read (%d + %d)\n\n%s\n",
  3212. ori_len + extra_len, ori_len, extra_len,
  3213. (char *) larger_buf );
  3214. /* End of message should be detected according to the syntax of the
  3215. * application protocol (eg HTTP), just use a dummy test here. */
  3216. if( larger_buf[ori_len + extra_len - 1] == '\n' )
  3217. terminated = 1;
  3218. mbedtls_free( larger_buf );
  3219. }
  3220. if( terminated )
  3221. {
  3222. ret = 0;
  3223. break;
  3224. }
  3225. }
  3226. while( 1 );
  3227. }
  3228. else /* Not stream, so datagram */
  3229. {
  3230. len = opt.buffer_size - 1;
  3231. memset( buf, 0, opt.buffer_size );
  3232. do
  3233. {
  3234. /* Without the call to `mbedtls_ssl_check_pending`, it might
  3235. * happen that the client sends application data in the same
  3236. * datagram as the Finished message concluding the handshake.
  3237. * In this case, the application data would be ready to be
  3238. * processed while the underlying transport wouldn't signal
  3239. * any further incoming data.
  3240. *
  3241. * See the test 'Event-driven I/O: session-id resume, UDP packing'
  3242. * in tests/ssl-opt.sh.
  3243. */
  3244. /* For event-driven IO, wait for socket to become available */
  3245. if( mbedtls_ssl_check_pending( &ssl ) == 0 &&
  3246. opt.event == 1 /* level triggered IO */ )
  3247. {
  3248. #if defined(MBEDTLS_TIMING_C)
  3249. idle( &client_fd, &timer, MBEDTLS_ERR_SSL_WANT_READ );
  3250. #else
  3251. idle( &client_fd, MBEDTLS_ERR_SSL_WANT_READ );
  3252. #endif
  3253. }
  3254. ret = mbedtls_ssl_read( &ssl, buf, len );
  3255. /* Note that even if `mbedtls_ssl_check_pending` returns true,
  3256. * it can happen that the subsequent call to `mbedtls_ssl_read`
  3257. * returns `MBEDTLS_ERR_SSL_WANT_READ`, because the pending messages
  3258. * might be discarded (e.g. because they are retransmissions). */
  3259. }
  3260. while( mbedtls_status_is_ssl_in_progress( ret ) );
  3261. if( ret <= 0 )
  3262. {
  3263. switch( ret )
  3264. {
  3265. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  3266. mbedtls_printf( " connection was closed gracefully\n" );
  3267. ret = 0;
  3268. goto close_notify;
  3269. default:
  3270. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
  3271. goto reset;
  3272. }
  3273. }
  3274. len = ret;
  3275. buf[len] = '\0';
  3276. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  3277. ret = 0;
  3278. }
  3279. /*
  3280. * 7a. Request renegotiation while client is waiting for input from us.
  3281. * (only on the first exchange, to be able to test retransmission)
  3282. */
  3283. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  3284. if( opt.renegotiate && exchanges_left == opt.exchanges )
  3285. {
  3286. mbedtls_printf( " . Requestion renegotiation..." );
  3287. fflush( stdout );
  3288. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  3289. {
  3290. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3291. {
  3292. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n", ret );
  3293. goto reset;
  3294. }
  3295. /* For event-driven IO, wait for socket to become available */
  3296. if( opt.event == 1 /* level triggered IO */ )
  3297. {
  3298. #if defined(MBEDTLS_TIMING_C)
  3299. idle( &client_fd, &timer, ret );
  3300. #else
  3301. idle( &client_fd, ret );
  3302. #endif
  3303. }
  3304. }
  3305. mbedtls_printf( " ok\n" );
  3306. }
  3307. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  3308. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  3309. ret = report_cid_usage( &ssl, "after renegotiation" );
  3310. if( ret != 0 )
  3311. goto exit;
  3312. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  3313. /*
  3314. * 7. Write the 200 Response
  3315. */
  3316. mbedtls_printf( " > Write to client:" );
  3317. fflush( stdout );
  3318. len = sprintf( (char *) buf, HTTP_RESPONSE,
  3319. mbedtls_ssl_get_ciphersuite( &ssl ) );
  3320. /* Add padding to the response to reach opt.response_size in length */
  3321. if( opt.response_size != DFL_RESPONSE_SIZE &&
  3322. len < opt.response_size )
  3323. {
  3324. memset( buf + len, 'B', opt.response_size - len );
  3325. len += opt.response_size - len;
  3326. }
  3327. /* Truncate if response size is smaller than the "natural" size */
  3328. if( opt.response_size != DFL_RESPONSE_SIZE &&
  3329. len > opt.response_size )
  3330. {
  3331. len = opt.response_size;
  3332. /* Still end with \r\n unless that's really not possible */
  3333. if( len >= 2 ) buf[len - 2] = '\r';
  3334. if( len >= 1 ) buf[len - 1] = '\n';
  3335. }
  3336. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  3337. {
  3338. for( written = 0, frags = 0; written < len; written += ret, frags++ )
  3339. {
  3340. while( ( ret = mbedtls_ssl_write( &ssl, buf + written, len - written ) )
  3341. <= 0 )
  3342. {
  3343. if( ret == MBEDTLS_ERR_NET_CONN_RESET )
  3344. {
  3345. mbedtls_printf( " failed\n ! peer closed the connection\n\n" );
  3346. goto reset;
  3347. }
  3348. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3349. {
  3350. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  3351. goto reset;
  3352. }
  3353. /* For event-driven IO, wait for socket to become available */
  3354. if( opt.event == 1 /* level triggered IO */ )
  3355. {
  3356. #if defined(MBEDTLS_TIMING_C)
  3357. idle( &client_fd, &timer, ret );
  3358. #else
  3359. idle( &client_fd, ret );
  3360. #endif
  3361. }
  3362. }
  3363. }
  3364. }
  3365. else /* Not stream, so datagram */
  3366. {
  3367. while( 1 )
  3368. {
  3369. ret = mbedtls_ssl_write( &ssl, buf, len );
  3370. if( ! mbedtls_status_is_ssl_in_progress( ret ) )
  3371. break;
  3372. /* For event-driven IO, wait for socket to become available */
  3373. if( opt.event == 1 /* level triggered IO */ )
  3374. {
  3375. #if defined(MBEDTLS_TIMING_C)
  3376. idle( &client_fd, &timer, ret );
  3377. #else
  3378. idle( &client_fd, ret );
  3379. #endif
  3380. }
  3381. }
  3382. if( ret < 0 )
  3383. {
  3384. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n", ret );
  3385. goto reset;
  3386. }
  3387. frags = 1;
  3388. written = ret;
  3389. }
  3390. buf[written] = '\0';
  3391. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n", written, frags, (char *) buf );
  3392. ret = 0;
  3393. /*
  3394. * 7b. Simulate serialize/deserialize and go back to data exchange
  3395. */
  3396. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  3397. if( opt.serialize != 0 )
  3398. {
  3399. size_t buf_len;
  3400. mbedtls_printf( " . Serializing live connection..." );
  3401. ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
  3402. if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
  3403. {
  3404. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  3405. "-0x%x\n\n", (unsigned int) -ret );
  3406. goto exit;
  3407. }
  3408. if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
  3409. {
  3410. mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
  3411. "serialized context" );
  3412. goto exit;
  3413. }
  3414. context_buf_len = buf_len;
  3415. if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
  3416. buf_len, &buf_len ) ) != 0 )
  3417. {
  3418. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  3419. "-0x%x\n\n", (unsigned int) -ret );
  3420. goto exit;
  3421. }
  3422. mbedtls_printf( " ok\n" );
  3423. /* Save serialized context to the 'opt.context_file' as a base64 code */
  3424. if( 0 < strlen( opt.context_file ) )
  3425. {
  3426. FILE *b64_file;
  3427. uint8_t *b64_buf;
  3428. size_t b64_len;
  3429. mbedtls_printf( " . Save serialized context to a file... " );
  3430. mbedtls_base64_encode( NULL, 0, &b64_len, context_buf, buf_len );
  3431. if( ( b64_buf = mbedtls_calloc( 1, b64_len ) ) == NULL )
  3432. {
  3433. mbedtls_printf( "failed\n ! Couldn't allocate buffer for "
  3434. "the base64 code\n" );
  3435. goto exit;
  3436. }
  3437. if( ( ret = mbedtls_base64_encode( b64_buf, b64_len, &b64_len,
  3438. context_buf, buf_len ) ) != 0 )
  3439. {
  3440. mbedtls_printf( "failed\n ! mbedtls_base64_encode returned "
  3441. "-0x%x\n", (unsigned int) -ret );
  3442. mbedtls_free( b64_buf );
  3443. goto exit;
  3444. }
  3445. if( ( b64_file = fopen( opt.context_file, "w" ) ) == NULL )
  3446. {
  3447. mbedtls_printf( "failed\n ! Cannot open '%s' for writing.\n",
  3448. opt.context_file );
  3449. mbedtls_free( b64_buf );
  3450. goto exit;
  3451. }
  3452. if( b64_len != fwrite( b64_buf, 1, b64_len, b64_file ) )
  3453. {
  3454. mbedtls_printf( "failed\n ! fwrite(%ld bytes) failed\n",
  3455. (long) b64_len );
  3456. mbedtls_free( b64_buf );
  3457. fclose( b64_file );
  3458. goto exit;
  3459. }
  3460. mbedtls_free( b64_buf );
  3461. fclose( b64_file );
  3462. mbedtls_printf( "ok\n" );
  3463. }
  3464. /*
  3465. * This simulates a workflow where you have a long-lived server
  3466. * instance, potentially with a pool of ssl_context objects, and you
  3467. * just want to re-use one while the connection is inactive: in that
  3468. * case you can just reset() it, and then it's ready to receive
  3469. * serialized data from another connection (or the same here).
  3470. */
  3471. if( opt.serialize == 1 )
  3472. {
  3473. /* nothing to do here, done by context_save() already */
  3474. mbedtls_printf( " . Context has been reset... ok\n" );
  3475. }
  3476. /*
  3477. * This simulates a workflow where you have one server instance per
  3478. * connection, and want to release it entire when the connection is
  3479. * inactive, and spawn it again when needed again - this would happen
  3480. * between ssl_free() and ssl_init() below, together with any other
  3481. * teardown/startup code needed - for example, preparing the
  3482. * ssl_config again (see section 3 "setup stuff" in this file).
  3483. */
  3484. if( opt.serialize == 2 )
  3485. {
  3486. mbedtls_printf( " . Freeing and reinitializing context..." );
  3487. mbedtls_ssl_free( &ssl );
  3488. mbedtls_ssl_init( &ssl );
  3489. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  3490. {
  3491. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
  3492. "-0x%x\n\n", (unsigned int) -ret );
  3493. goto exit;
  3494. }
  3495. /*
  3496. * This illustrates the minimum amount of things you need to set
  3497. * up, however you could set up much more if desired, for example
  3498. * if you want to share your set up code between the case of
  3499. * establishing a new connection and this case.
  3500. */
  3501. if( opt.nbio == 2 )
  3502. mbedtls_ssl_set_bio( &ssl, &client_fd, delayed_send,
  3503. delayed_recv, NULL );
  3504. else
  3505. mbedtls_ssl_set_bio( &ssl, &client_fd, mbedtls_net_send,
  3506. mbedtls_net_recv,
  3507. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  3508. #if defined(MBEDTLS_TIMING_C)
  3509. mbedtls_ssl_set_timer_cb( &ssl, &timer,
  3510. mbedtls_timing_set_delay,
  3511. mbedtls_timing_get_delay );
  3512. #endif /* MBEDTLS_TIMING_C */
  3513. mbedtls_printf( " ok\n" );
  3514. }
  3515. mbedtls_printf( " . Deserializing connection..." );
  3516. if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
  3517. buf_len ) ) != 0 )
  3518. {
  3519. mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
  3520. "-0x%x\n\n", (unsigned int) -ret );
  3521. goto exit;
  3522. }
  3523. mbedtls_free( context_buf );
  3524. context_buf = NULL;
  3525. context_buf_len = 0;
  3526. mbedtls_printf( " ok\n" );
  3527. }
  3528. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  3529. /*
  3530. * 7c. Continue doing data exchanges?
  3531. */
  3532. if( --exchanges_left > 0 )
  3533. goto data_exchange;
  3534. /*
  3535. * 8. Done, cleanly close the connection
  3536. */
  3537. close_notify:
  3538. mbedtls_printf( " . Closing the connection..." );
  3539. /* No error checking, the connection might be closed already */
  3540. do ret = mbedtls_ssl_close_notify( &ssl );
  3541. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  3542. ret = 0;
  3543. mbedtls_printf( " done\n" );
  3544. goto reset;
  3545. /*
  3546. * Cleanup and exit
  3547. */
  3548. exit:
  3549. #ifdef MBEDTLS_ERROR_C
  3550. if( ret != 0 )
  3551. {
  3552. char error_buf[100];
  3553. mbedtls_strerror( ret, error_buf, 100 );
  3554. mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int) -ret, error_buf );
  3555. }
  3556. #endif
  3557. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3558. {
  3559. mbedtls_printf( " . Cleaning up..." );
  3560. fflush( stdout );
  3561. }
  3562. mbedtls_net_free( &client_fd );
  3563. mbedtls_net_free( &listen_fd );
  3564. mbedtls_ssl_free( &ssl );
  3565. mbedtls_ssl_config_free( &conf );
  3566. #if defined(MBEDTLS_SSL_CACHE_C)
  3567. mbedtls_ssl_cache_free( &cache );
  3568. #endif
  3569. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  3570. mbedtls_ssl_ticket_free( &ticket_ctx );
  3571. #endif
  3572. #if defined(MBEDTLS_SSL_COOKIE_C)
  3573. mbedtls_ssl_cookie_free( &cookie_ctx );
  3574. #endif
  3575. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  3576. if( context_buf != NULL )
  3577. mbedtls_platform_zeroize( context_buf, context_buf_len );
  3578. mbedtls_free( context_buf );
  3579. #endif
  3580. #if defined(SNI_OPTION)
  3581. sni_free( sni_info );
  3582. #endif
  3583. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  3584. ret = psk_free( psk_info );
  3585. if( ( ret != 0 ) && ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
  3586. mbedtls_printf( "Failed to list of opaque PSKs - error was %d\n", ret );
  3587. #endif
  3588. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  3589. mbedtls_x509_crt_free( &cacert );
  3590. mbedtls_x509_crt_free( &srvcert );
  3591. mbedtls_pk_free( &pkey );
  3592. mbedtls_x509_crt_free( &srvcert2 );
  3593. mbedtls_pk_free( &pkey2 );
  3594. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3595. psa_destroy_key( key_slot );
  3596. psa_destroy_key( key_slot2 );
  3597. #endif
  3598. #endif
  3599. #if defined(MBEDTLS_DHM_C) && defined(MBEDTLS_FS_IO)
  3600. mbedtls_dhm_free( &dhm );
  3601. #endif
  3602. #if defined(MBEDTLS_SSL_ASYNC_PRIVATE)
  3603. for( i = 0; (size_t) i < ssl_async_keys.slots_used; i++ )
  3604. {
  3605. if( ssl_async_keys.slots[i].pk_owned )
  3606. {
  3607. mbedtls_pk_free( ssl_async_keys.slots[i].pk );
  3608. mbedtls_free( ssl_async_keys.slots[i].pk );
  3609. ssl_async_keys.slots[i].pk = NULL;
  3610. }
  3611. }
  3612. #endif
  3613. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
  3614. defined(MBEDTLS_USE_PSA_CRYPTO)
  3615. if( opt.psk_opaque != 0 )
  3616. {
  3617. /* This is ok even if the slot hasn't been
  3618. * initialized (we might have jumed here
  3619. * immediately because of bad cmd line params,
  3620. * for example). */
  3621. status = psa_destroy_key( psk_slot );
  3622. if( ( status != PSA_SUCCESS ) &&
  3623. ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
  3624. {
  3625. mbedtls_printf( "Failed to destroy key slot %u - error was %d",
  3626. (unsigned) psk_slot, (int) status );
  3627. }
  3628. }
  3629. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
  3630. MBEDTLS_USE_PSA_CRYPTO */
  3631. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  3632. const char* message = mbedtls_test_helper_is_psa_leaking();
  3633. if( message )
  3634. {
  3635. if( ret == 0 )
  3636. ret = 1;
  3637. mbedtls_printf( "PSA memory leak detected: %s\n", message);
  3638. }
  3639. #endif
  3640. /* For builds with MBEDTLS_TEST_USE_PSA_CRYPTO_RNG psa crypto
  3641. * resources are freed by rng_free(). */
  3642. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  3643. !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  3644. mbedtls_psa_crypto_free( );
  3645. #endif
  3646. rng_free( &rng );
  3647. mbedtls_free( buf );
  3648. #if defined(MBEDTLS_TEST_HOOKS)
  3649. /* Let test hooks detect errors such as resource leaks.
  3650. * Don't do it in query_config mode, because some test code prints
  3651. * information to stdout and this gets mixed with the regular output. */
  3652. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3653. {
  3654. if( test_hooks_failure_detected( ) )
  3655. {
  3656. if( ret == 0 )
  3657. ret = 1;
  3658. mbedtls_printf( "Test hooks detected errors.\n" );
  3659. }
  3660. }
  3661. test_hooks_free( );
  3662. #endif /* MBEDTLS_TEST_HOOKS */
  3663. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  3664. #if defined(MBEDTLS_MEMORY_DEBUG)
  3665. mbedtls_memory_buffer_alloc_status();
  3666. #endif
  3667. mbedtls_memory_buffer_alloc_free();
  3668. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  3669. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3670. {
  3671. mbedtls_printf( " done.\n" );
  3672. #if defined(_WIN32)
  3673. mbedtls_printf( " + Press Enter to exit this program.\n" );
  3674. fflush( stdout ); getchar();
  3675. #endif
  3676. }
  3677. // Shell can not handle large exit numbers -> 1 for errors
  3678. if( ret < 0 )
  3679. ret = 1;
  3680. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  3681. mbedtls_exit( ret );
  3682. else
  3683. mbedtls_exit( query_config_ret );
  3684. }
  3685. #endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_SRV_C */