ssl_client2.c 110 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128
  1. /*
  2. * SSL client with certificate authentication
  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_USE_PSA_CRYPTO)
  22. #include "test/psa_crypto_helpers.h"
  23. #endif
  24. #if defined(MBEDTLS_SSL_TEST_IMPOSSIBLE)
  25. int main( void )
  26. {
  27. mbedtls_printf( MBEDTLS_SSL_TEST_IMPOSSIBLE );
  28. mbedtls_exit( 0 );
  29. }
  30. #elif !defined(MBEDTLS_SSL_CLI_C)
  31. int main( void )
  32. {
  33. mbedtls_printf( "MBEDTLS_SSL_CLI_C not defined.\n" );
  34. mbedtls_exit( 0 );
  35. }
  36. #else /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */
  37. /* Size of memory to be allocated for the heap, when using the library's memory
  38. * management and MBEDTLS_MEMORY_BUFFER_ALLOC_C is enabled. */
  39. #define MEMORY_HEAP_SIZE 120000
  40. #define MAX_REQUEST_SIZE 20000
  41. #define MAX_REQUEST_SIZE_STR "20000"
  42. #define DFL_SERVER_NAME "localhost"
  43. #define DFL_SERVER_ADDR NULL
  44. #define DFL_SERVER_PORT "4433"
  45. #define DFL_REQUEST_PAGE "/"
  46. #define DFL_REQUEST_SIZE -1
  47. #define DFL_DEBUG_LEVEL 0
  48. #define DFL_CONTEXT_CRT_CB 0
  49. #define DFL_NBIO 0
  50. #define DFL_EVENT 0
  51. #define DFL_READ_TIMEOUT 0
  52. #define DFL_MAX_RESEND 0
  53. #define DFL_CA_FILE ""
  54. #define DFL_CA_PATH ""
  55. #define DFL_CRT_FILE ""
  56. #define DFL_KEY_FILE ""
  57. #define DFL_KEY_OPAQUE 0
  58. #define DFL_KEY_PWD ""
  59. #define DFL_PSK ""
  60. #define DFL_PSK_OPAQUE 0
  61. #define DFL_PSK_IDENTITY "Client_identity"
  62. #define DFL_ECJPAKE_PW NULL
  63. #define DFL_EC_MAX_OPS -1
  64. #define DFL_FORCE_CIPHER 0
  65. #define DFL_TLS13_KEX_MODES MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL
  66. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  67. #define DFL_ALLOW_LEGACY -2
  68. #define DFL_RENEGOTIATE 0
  69. #define DFL_EXCHANGES 1
  70. #define DFL_MIN_VERSION -1
  71. #define DFL_MAX_VERSION -1
  72. #define DFL_SHA1 -1
  73. #define DFL_AUTH_MODE -1
  74. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  75. #define DFL_TRUNC_HMAC -1
  76. #define DFL_RECSPLIT -1
  77. #define DFL_DHMLEN -1
  78. #define DFL_RECONNECT 0
  79. #define DFL_RECO_DELAY 0
  80. #define DFL_RECO_MODE 1
  81. #define DFL_CID_ENABLED 0
  82. #define DFL_CID_VALUE ""
  83. #define DFL_CID_ENABLED_RENEGO -1
  84. #define DFL_CID_VALUE_RENEGO NULL
  85. #define DFL_RECONNECT_HARD 0
  86. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  87. #define DFL_ALPN_STRING NULL
  88. #define DFL_CURVES NULL
  89. #define DFL_SIG_ALGS NULL
  90. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  91. #define DFL_HS_TO_MIN 0
  92. #define DFL_HS_TO_MAX 0
  93. #define DFL_DTLS_MTU -1
  94. #define DFL_DGRAM_PACKING 1
  95. #define DFL_FALLBACK -1
  96. #define DFL_EXTENDED_MS -1
  97. #define DFL_ETM -1
  98. #define DFL_SERIALIZE 0
  99. #define DFL_CONTEXT_FILE ""
  100. #define DFL_EXTENDED_MS_ENFORCE -1
  101. #define DFL_CA_CALLBACK 0
  102. #define DFL_EAP_TLS 0
  103. #define DFL_REPRODUCIBLE 0
  104. #define DFL_NSS_KEYLOG 0
  105. #define DFL_NSS_KEYLOG_FILE NULL
  106. #define DFL_SKIP_CLOSE_NOTIFY 0
  107. #define DFL_QUERY_CONFIG_MODE 0
  108. #define DFL_USE_SRTP 0
  109. #define DFL_SRTP_FORCE_PROFILE 0
  110. #define DFL_SRTP_MKI ""
  111. #define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
  112. #define GET_REQUEST_END "\r\n\r\n"
  113. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  114. #define USAGE_CONTEXT_CRT_CB \
  115. " context_crt_cb=%%d This determines whether the CRT verification callback is bound\n" \
  116. " to the SSL configuration of the SSL context.\n" \
  117. " Possible values:\n"\
  118. " - 0 (default): Use CRT callback bound to configuration\n" \
  119. " - 1: Use CRT callback bound to SSL context\n"
  120. #else
  121. #define USAGE_CONTEXT_CRT_CB ""
  122. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  123. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  124. #if defined(MBEDTLS_FS_IO)
  125. #define USAGE_IO \
  126. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  127. " default: \"\" (pre-loaded)\n" \
  128. " use \"none\" to skip loading any top-level CAs.\n" \
  129. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  130. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  131. " use \"none\" to skip loading any top-level CAs.\n" \
  132. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  133. " default: \"\" (pre-loaded)\n" \
  134. " key_file=%%s default: \"\" (pre-loaded)\n"\
  135. " key_pwd=%%s Password for key specified by key_file argument\n"\
  136. " default: none\n"
  137. #else
  138. #define USAGE_IO \
  139. " No file operations available (MBEDTLS_FS_IO not defined)\n"
  140. #endif /* MBEDTLS_FS_IO */
  141. #else /* MBEDTLS_X509_CRT_PARSE_C */
  142. #define USAGE_IO ""
  143. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  144. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
  145. #define USAGE_KEY_OPAQUE \
  146. " key_opaque=%%d Handle your private key as if it were opaque\n" \
  147. " default: 0 (disabled)\n"
  148. #else
  149. #define USAGE_KEY_OPAQUE ""
  150. #endif
  151. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  152. #define USAGE_CID \
  153. " cid=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension.\n" \
  154. " default: 0 (disabled)\n" \
  155. " cid_renego=%%d Disable (0) or enable (1) the use of the DTLS Connection ID extension during renegotiation.\n" \
  156. " default: same as 'cid' parameter\n" \
  157. " cid_val=%%s The CID to use for incoming messages (in hex, without 0x).\n" \
  158. " default: \"\"\n" \
  159. " cid_val_renego=%%s The CID to use for incoming messages (in hex, without 0x) after renegotiation.\n" \
  160. " default: same as 'cid_val' parameter\n"
  161. #else /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  162. #define USAGE_CID ""
  163. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  164. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  165. #define USAGE_PSK_RAW \
  166. " psk=%%s default: \"\" (disabled)\n" \
  167. " The PSK values are in hex, without 0x.\n" \
  168. " psk_identity=%%s default: \"Client_identity\"\n"
  169. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  170. #define USAGE_PSK_SLOT \
  171. " psk_opaque=%%d default: 0 (don't use opaque static PSK)\n" \
  172. " Enable this to store the PSK configured through command line\n" \
  173. " parameter `psk` in a PSA-based key slot.\n" \
  174. " Note: Currently only supported in conjunction with\n" \
  175. " the use of min_version to force TLS 1.2 and force_ciphersuite \n" \
  176. " to force a particular PSK-only ciphersuite.\n" \
  177. " Note: This is to test integration of PSA-based opaque PSKs with\n" \
  178. " Mbed TLS only. Production systems are likely to configure Mbed TLS\n" \
  179. " with prepopulated key slots instead of importing raw key material.\n"
  180. #else
  181. #define USAGE_PSK_SLOT ""
  182. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  183. #define USAGE_PSK USAGE_PSK_RAW USAGE_PSK_SLOT
  184. #else
  185. #define USAGE_PSK ""
  186. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  187. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  188. #define USAGE_CA_CALLBACK \
  189. " ca_callback=%%d default: 0 (disabled)\n" \
  190. " Enable this to use the trusted certificate callback function\n"
  191. #else
  192. #define USAGE_CA_CALLBACK ""
  193. #endif /* MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK */
  194. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  195. #define USAGE_TICKETS \
  196. " tickets=%%d default: 1 (enabled)\n"
  197. #else
  198. #define USAGE_TICKETS ""
  199. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  200. #define USAGE_EAP_TLS \
  201. " eap_tls=%%d default: 0 (disabled)\n"
  202. #define USAGE_NSS_KEYLOG \
  203. " nss_keylog=%%d default: 0 (disabled)\n" \
  204. " This cannot be used with eap_tls=1\n"
  205. #define USAGE_NSS_KEYLOG_FILE \
  206. " nss_keylog_file=%%s\n"
  207. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  208. #define USAGE_SRTP \
  209. " use_srtp=%%d default: 0 (disabled)\n" \
  210. " This cannot be used with eap_tls=1 or "\
  211. " nss_keylog=1\n" \
  212. " srtp_force_profile=%%d default: 0 (all enabled)\n" \
  213. " available profiles:\n" \
  214. " 1 - SRTP_AES128_CM_HMAC_SHA1_80\n" \
  215. " 2 - SRTP_AES128_CM_HMAC_SHA1_32\n" \
  216. " 3 - SRTP_NULL_HMAC_SHA1_80\n" \
  217. " 4 - SRTP_NULL_HMAC_SHA1_32\n" \
  218. " mki=%%s default: \"\" (in hex, without 0x)\n"
  219. #else /* MBEDTLS_SSL_DTLS_SRTP */
  220. #define USAGE_SRTP ""
  221. #endif
  222. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  223. #define USAGE_MAX_FRAG_LEN \
  224. " max_frag_len=%%d default: 16384 (tls default)\n" \
  225. " options: 512, 1024, 2048, 4096\n"
  226. #else
  227. #define USAGE_MAX_FRAG_LEN ""
  228. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  229. #if defined(MBEDTLS_DHM_C)
  230. #define USAGE_DHMLEN \
  231. " dhmlen=%%d default: (library default: 1024 bits)\n"
  232. #else
  233. #define USAGE_DHMLEN
  234. #endif
  235. #if defined(MBEDTLS_SSL_ALPN)
  236. #define USAGE_ALPN \
  237. " alpn=%%s default: \"\" (disabled)\n" \
  238. " example: spdy/1,http/1.1\n"
  239. #else
  240. #define USAGE_ALPN ""
  241. #endif /* MBEDTLS_SSL_ALPN */
  242. #if defined(MBEDTLS_ECP_C)
  243. #define USAGE_CURVES \
  244. " curves=a,b,c,d default: \"default\" (library default)\n" \
  245. " example: \"secp521r1,brainpoolP512r1\"\n" \
  246. " - use \"none\" for empty list\n" \
  247. " - see mbedtls_ecp_curve_list()\n" \
  248. " for acceptable curve names\n"
  249. #else
  250. #define USAGE_CURVES ""
  251. #endif
  252. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  253. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  254. #define USAGE_SIG_ALGS \
  255. " sig_algs=a,b,c,d default: \"default\" (library default)\n" \
  256. " example: \"ecdsa_secp256r1_sha256,ecdsa_secp384r1_sha384\"\n"
  257. #else
  258. #define USAGE_SIG_ALGS ""
  259. #endif
  260. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  261. #define USAGE_DTLS \
  262. " dtls=%%d default: 0 (TLS)\n" \
  263. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  264. " range of DTLS handshake timeouts in millisecs\n" \
  265. " mtu=%%d default: (library default: unlimited)\n" \
  266. " dgram_packing=%%d default: 1 (allowed)\n" \
  267. " allow or forbid packing of multiple\n" \
  268. " records within a single datgram.\n"
  269. #else
  270. #define USAGE_DTLS ""
  271. #endif
  272. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  273. #define USAGE_EMS \
  274. " extended_ms=0/1 default: (library default: on)\n"
  275. #else
  276. #define USAGE_EMS ""
  277. #endif
  278. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  279. #define USAGE_ETM \
  280. " etm=0/1 default: (library default: on)\n"
  281. #else
  282. #define USAGE_ETM ""
  283. #endif
  284. #define USAGE_REPRODUCIBLE \
  285. " reproducible=0/1 default: 0 (disabled)\n"
  286. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  287. #define USAGE_RENEGO \
  288. " renegotiation=%%d default: 0 (disabled)\n" \
  289. " renegotiate=%%d default: 0 (disabled)\n"
  290. #else
  291. #define USAGE_RENEGO ""
  292. #endif
  293. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  294. #define USAGE_ECJPAKE \
  295. " ecjpake_pw=%%s default: none (disabled)\n"
  296. #else
  297. #define USAGE_ECJPAKE ""
  298. #endif
  299. #if defined(MBEDTLS_ECP_RESTARTABLE)
  300. #define USAGE_ECRESTART \
  301. " ec_max_ops=%%s default: library default (restart disabled)\n"
  302. #else
  303. #define USAGE_ECRESTART ""
  304. #endif
  305. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  306. #define USAGE_SERIALIZATION \
  307. " serialize=%%d default: 0 (do not serialize/deserialize)\n" \
  308. " options: 1 (serialize)\n" \
  309. " 2 (serialize with re-initialization)\n" \
  310. " context_file=%%s The file path to write a serialized connection\n"\
  311. " in the form of base64 code (serialize option\n" \
  312. " must be set)\n" \
  313. " default: \"\" (do nothing)\n" \
  314. " option: a file path\n"
  315. #else
  316. #define USAGE_SERIALIZATION ""
  317. #endif
  318. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  319. #define USAGE_TLS13_KEY_EXCHANGE_MODES \
  320. " tls13_kex_modes=%%s default: all\n" \
  321. " options: psk, psk_ephemeral, ephemeral, ephemeral_all, psk_all, all\n"
  322. #else
  323. #define USAGE_TLS13_KEY_EXCHANGE_MODES ""
  324. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  325. /* USAGE is arbitrarily split to stay under the portable string literal
  326. * length limit: 4095 bytes in C99. */
  327. #define USAGE1 \
  328. "\n usage: ssl_client2 param=<>...\n" \
  329. "\n acceptable parameters:\n" \
  330. " server_name=%%s default: localhost\n" \
  331. " server_addr=%%s default: given by name\n" \
  332. " server_port=%%d default: 4433\n" \
  333. " request_page=%%s default: \".\"\n" \
  334. " request_size=%%d default: about 34 (basic request)\n" \
  335. " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
  336. " If 0, in the first exchange only an empty\n" \
  337. " application data message is sent followed by\n" \
  338. " a second non-empty message before attempting\n" \
  339. " to read a response from the server\n" \
  340. " debug_level=%%d default: 0 (disabled)\n" \
  341. " nbio=%%d default: 0 (blocking I/O)\n" \
  342. " options: 1 (non-blocking), 2 (added delays)\n" \
  343. " event=%%d default: 0 (loop)\n" \
  344. " options: 1 (level-triggered, implies nbio=1),\n" \
  345. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  346. " max_resend=%%d default: 0 (no resend on timeout)\n" \
  347. " skip_close_notify=%%d default: 0 (send close_notify)\n" \
  348. "\n" \
  349. USAGE_DTLS \
  350. USAGE_CID \
  351. USAGE_SRTP \
  352. "\n"
  353. #define USAGE2 \
  354. " auth_mode=%%s default: (library default: none)\n" \
  355. " options: none, optional, required\n" \
  356. USAGE_IO \
  357. USAGE_KEY_OPAQUE \
  358. USAGE_CA_CALLBACK \
  359. "\n" \
  360. USAGE_PSK \
  361. USAGE_ECJPAKE \
  362. USAGE_ECRESTART \
  363. "\n"
  364. #define USAGE3 \
  365. " allow_legacy=%%d default: (library default: no)\n" \
  366. USAGE_RENEGO \
  367. " exchanges=%%d default: 1\n" \
  368. " reconnect=%%d number of reconnections using session resumption\n" \
  369. " default: 0 (disabled)\n" \
  370. " reco_delay=%%d default: 0 seconds\n" \
  371. " reco_mode=%%d 0: copy session, 1: serialize session\n" \
  372. " default: 1\n" \
  373. " reconnect_hard=%%d default: 0 (disabled)\n" \
  374. USAGE_TICKETS \
  375. USAGE_EAP_TLS \
  376. USAGE_MAX_FRAG_LEN \
  377. USAGE_CONTEXT_CRT_CB \
  378. USAGE_ALPN \
  379. USAGE_EMS \
  380. USAGE_ETM \
  381. USAGE_REPRODUCIBLE \
  382. USAGE_CURVES \
  383. USAGE_SIG_ALGS \
  384. USAGE_DHMLEN \
  385. "\n"
  386. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  387. #define TLS1_3_VERSION_OPTIONS ", tls1_3"
  388. #else /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  389. #define TLS1_3_VERSION_OPTIONS ""
  390. #endif /* !MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  391. #define USAGE4 \
  392. " allow_sha1=%%d default: 0\n" \
  393. " min_version=%%s default: (library default: tls1_2)\n" \
  394. " max_version=%%s default: (library default: tls1_2)\n" \
  395. " force_version=%%s default: \"\" (none)\n" \
  396. " options: tls1_2, dtls1_2" TLS1_3_VERSION_OPTIONS \
  397. "\n\n" \
  398. " force_ciphersuite=<name> default: all enabled\n" \
  399. USAGE_TLS13_KEY_EXCHANGE_MODES \
  400. " query_config=<name> return 0 if the specified\n" \
  401. " configuration macro is defined and 1\n" \
  402. " otherwise. The expansion of the macro\n" \
  403. " is printed if it is defined\n" \
  404. USAGE_SERIALIZATION \
  405. " acceptable ciphersuite names:\n"
  406. #define ALPN_LIST_SIZE 10
  407. #define CURVE_LIST_SIZE 20
  408. #define SIG_ALG_LIST_SIZE 5
  409. /*
  410. * global options
  411. */
  412. struct options
  413. {
  414. const char *server_name; /* hostname of the server (client only) */
  415. const char *server_addr; /* address of the server (client only) */
  416. const char *server_port; /* port on which the ssl service runs */
  417. int debug_level; /* level of debugging */
  418. int nbio; /* should I/O be blocking? */
  419. int event; /* loop or event-driven IO? level or edge triggered? */
  420. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  421. int max_resend; /* DTLS times to resend on read timeout */
  422. const char *request_page; /* page on server to request */
  423. int request_size; /* pad request with header to requested size */
  424. const char *ca_file; /* the file with the CA certificate(s) */
  425. const char *ca_path; /* the path with the CA certificate(s) reside */
  426. const char *crt_file; /* the file with the client certificate */
  427. const char *key_file; /* the file with the client key */
  428. int key_opaque; /* handle private key as if it were opaque */
  429. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  430. int psk_opaque;
  431. #endif
  432. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  433. int ca_callback; /* Use callback for trusted certificate list */
  434. #endif
  435. const char *key_pwd; /* the password for the client key */
  436. const char *psk; /* the pre-shared key */
  437. const char *psk_identity; /* the pre-shared key identity */
  438. const char *ecjpake_pw; /* the EC J-PAKE password */
  439. int ec_max_ops; /* EC consecutive operations limit */
  440. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  441. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  442. int tls13_kex_modes; /* supported TLS 1.3 key exchange modes */
  443. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  444. int renegotiation; /* enable / disable renegotiation */
  445. int allow_legacy; /* allow legacy renegotiation */
  446. int renegotiate; /* attempt renegotiation? */
  447. int renego_delay; /* delay before enforcing renegotiation */
  448. int exchanges; /* number of data exchanges */
  449. int min_version; /* minimum protocol version accepted */
  450. int max_version; /* maximum protocol version accepted */
  451. int allow_sha1; /* flag for SHA-1 support */
  452. int auth_mode; /* verify mode for connection */
  453. unsigned char mfl_code; /* code for maximum fragment length */
  454. int trunc_hmac; /* negotiate truncated hmac or not */
  455. int recsplit; /* enable record splitting? */
  456. int dhmlen; /* minimum DHM params len in bits */
  457. int reconnect; /* attempt to resume session */
  458. int reco_delay; /* delay in seconds before resuming session */
  459. int reco_mode; /* how to keep the session around */
  460. int reconnect_hard; /* unexpectedly reconnect from the same port */
  461. int tickets; /* enable / disable session tickets */
  462. const char *curves; /* list of supported elliptic curves */
  463. const char *sig_algs; /* supported TLS 1.3 signature algorithms */
  464. const char *alpn_string; /* ALPN supported protocols */
  465. int transport; /* TLS or DTLS? */
  466. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  467. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  468. int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
  469. int fallback; /* is this a fallback connection? */
  470. int dgram_packing; /* allow/forbid datagram packing */
  471. int extended_ms; /* negotiate extended master secret? */
  472. int etm; /* negotiate encrypt then mac? */
  473. int context_crt_cb; /* use context-specific CRT verify callback */
  474. int eap_tls; /* derive EAP-TLS keying material? */
  475. int nss_keylog; /* export NSS key log material */
  476. const char *nss_keylog_file; /* NSS key log file */
  477. int cid_enabled; /* whether to use the CID extension or not */
  478. int cid_enabled_renego; /* whether to use the CID extension or not
  479. * during renegotiation */
  480. const char *cid_val; /* the CID to use for incoming messages */
  481. int serialize; /* serialize/deserialize connection */
  482. const char *context_file; /* the file to write a serialized connection
  483. * in the form of base64 code (serialize
  484. * option must be set) */
  485. const char *cid_val_renego; /* the CID to use for incoming messages
  486. * after renegotiation */
  487. int reproducible; /* make communication reproducible */
  488. int skip_close_notify; /* skip sending the close_notify alert */
  489. int query_config_mode; /* whether to read config */
  490. int use_srtp; /* Support SRTP */
  491. int force_srtp_profile; /* SRTP protection profile to use or all */
  492. const char *mki; /* The dtls mki value to use */
  493. } opt;
  494. #include "ssl_test_common_source.c"
  495. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  496. static unsigned char peer_crt_info[1024];
  497. /*
  498. * Enabled if debug_level > 1 in code below
  499. */
  500. static int my_verify( void *data, mbedtls_x509_crt *crt,
  501. int depth, uint32_t *flags )
  502. {
  503. char buf[1024];
  504. ((void) data);
  505. mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
  506. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  507. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  508. if( depth == 0 )
  509. memcpy( peer_crt_info, buf, sizeof( buf ) );
  510. if( opt.debug_level == 0 )
  511. return( 0 );
  512. mbedtls_printf( "%s", buf );
  513. #else
  514. ((void) crt);
  515. ((void) depth);
  516. #endif
  517. if ( ( *flags ) == 0 )
  518. mbedtls_printf( " This certificate has no flags\n" );
  519. else
  520. {
  521. x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
  522. mbedtls_printf( "%s\n", buf );
  523. }
  524. return( 0 );
  525. }
  526. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  527. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  528. int report_cid_usage( mbedtls_ssl_context *ssl,
  529. const char *additional_description )
  530. {
  531. int ret;
  532. unsigned char peer_cid[ MBEDTLS_SSL_CID_OUT_LEN_MAX ];
  533. size_t peer_cid_len;
  534. int cid_negotiated;
  535. if( opt.transport != MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  536. return( 0 );
  537. /* Check if the use of a CID has been negotiated,
  538. * but don't ask for the CID value and length.
  539. *
  540. * Note: Here and below, we're demonstrating the various ways
  541. * in which mbedtls_ssl_get_peer_cid() can be called,
  542. * depending on whether or not the length/value of the
  543. * peer's CID is needed.
  544. *
  545. * An actual application, however, should use
  546. * just one call to mbedtls_ssl_get_peer_cid(). */
  547. ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
  548. NULL, NULL );
  549. if( ret != 0 )
  550. {
  551. mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
  552. (unsigned int) -ret );
  553. return( ret );
  554. }
  555. if( cid_negotiated == MBEDTLS_SSL_CID_DISABLED )
  556. {
  557. if( opt.cid_enabled == MBEDTLS_SSL_CID_ENABLED )
  558. {
  559. mbedtls_printf( "(%s) Use of Connection ID was rejected by the server.\n",
  560. additional_description );
  561. }
  562. }
  563. else
  564. {
  565. size_t idx=0;
  566. mbedtls_printf( "(%s) Use of Connection ID has been negotiated.\n",
  567. additional_description );
  568. /* Ask for just the length of the peer's CID. */
  569. ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
  570. NULL, &peer_cid_len );
  571. if( ret != 0 )
  572. {
  573. mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
  574. (unsigned int) -ret );
  575. return( ret );
  576. }
  577. /* Ask for just length + value of the peer's CID. */
  578. ret = mbedtls_ssl_get_peer_cid( ssl, &cid_negotiated,
  579. peer_cid, &peer_cid_len );
  580. if( ret != 0 )
  581. {
  582. mbedtls_printf( " failed\n ! mbedtls_ssl_get_peer_cid returned -0x%x\n\n",
  583. (unsigned int) -ret );
  584. return( ret );
  585. }
  586. mbedtls_printf( "(%s) Peer CID (length %u Bytes): ",
  587. additional_description,
  588. (unsigned) peer_cid_len );
  589. while( idx < peer_cid_len )
  590. {
  591. mbedtls_printf( "%02x ", peer_cid[ idx ] );
  592. idx++;
  593. }
  594. mbedtls_printf( "\n" );
  595. }
  596. return( 0 );
  597. }
  598. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  599. int main( int argc, char *argv[] )
  600. {
  601. int ret = 0, len, tail_len, i, written, frags, retry_left;
  602. int query_config_ret = 0;
  603. mbedtls_net_context server_fd;
  604. io_ctx_t io_ctx;
  605. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  606. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  607. uint16_t sig_alg_list[SIG_ALG_LIST_SIZE];
  608. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL &&
  609. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  610. unsigned char buf[MAX_REQUEST_SIZE + 1];
  611. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  612. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  613. size_t psk_len = 0;
  614. #endif
  615. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  616. unsigned char cid[MBEDTLS_SSL_CID_IN_LEN_MAX];
  617. unsigned char cid_renego[MBEDTLS_SSL_CID_IN_LEN_MAX];
  618. size_t cid_len = 0;
  619. size_t cid_renego_len = 0;
  620. #endif
  621. #if defined(MBEDTLS_SSL_ALPN)
  622. const char *alpn_list[ALPN_LIST_SIZE];
  623. #endif
  624. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  625. unsigned char alloc_buf[MEMORY_HEAP_SIZE];
  626. #endif
  627. #if defined(MBEDTLS_ECP_C)
  628. uint16_t group_list[CURVE_LIST_SIZE];
  629. const mbedtls_ecp_curve_info *curve_cur;
  630. #endif
  631. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  632. unsigned char mki[MBEDTLS_TLS_SRTP_MAX_MKI_LENGTH];
  633. size_t mki_len=0;
  634. #endif
  635. const char *pers = "ssl_client2";
  636. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  637. psa_key_id_t slot = 0;
  638. psa_algorithm_t alg = 0;
  639. psa_key_attributes_t key_attributes;
  640. psa_status_t status;
  641. #endif
  642. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  643. mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
  644. #endif
  645. rng_context_t rng;
  646. mbedtls_ssl_context ssl;
  647. mbedtls_ssl_config conf;
  648. mbedtls_ssl_session saved_session;
  649. unsigned char *session_data = NULL;
  650. size_t session_data_len = 0;
  651. #if defined(MBEDTLS_TIMING_C)
  652. mbedtls_timing_delay_context timer;
  653. #endif
  654. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  655. uint32_t flags;
  656. mbedtls_x509_crt cacert;
  657. mbedtls_x509_crt clicert;
  658. mbedtls_pk_context pkey;
  659. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  660. psa_key_id_t key_slot = 0; /* invalid key slot */
  661. #endif
  662. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  663. char *p, *q;
  664. const int *list;
  665. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  666. unsigned char *context_buf = NULL;
  667. size_t context_buf_len;
  668. #endif
  669. unsigned char eap_tls_keymaterial[16];
  670. unsigned char eap_tls_iv[8];
  671. const char* eap_tls_label = "client EAP encryption";
  672. eap_tls_keys eap_tls_keying;
  673. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  674. /*! master keys and master salt for SRTP generated during handshake */
  675. unsigned char dtls_srtp_key_material[MBEDTLS_TLS_SRTP_MAX_KEY_MATERIAL_LENGTH];
  676. const char* dtls_srtp_label = "EXTRACTOR-dtls_srtp";
  677. dtls_srtp_keys dtls_srtp_keying;
  678. const mbedtls_ssl_srtp_profile default_profiles[] = {
  679. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_80,
  680. MBEDTLS_TLS_SRTP_AES128_CM_HMAC_SHA1_32,
  681. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_80,
  682. MBEDTLS_TLS_SRTP_NULL_HMAC_SHA1_32,
  683. MBEDTLS_TLS_SRTP_UNSET
  684. };
  685. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  686. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  687. mbedtls_memory_buffer_alloc_init( alloc_buf, sizeof(alloc_buf) );
  688. #endif
  689. #if defined(MBEDTLS_TEST_HOOKS)
  690. test_hooks_init( );
  691. #endif /* MBEDTLS_TEST_HOOKS */
  692. /*
  693. * Make sure memory references are valid.
  694. */
  695. mbedtls_net_init( &server_fd );
  696. mbedtls_ssl_init( &ssl );
  697. mbedtls_ssl_config_init( &conf );
  698. memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
  699. rng_init( &rng );
  700. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  701. mbedtls_x509_crt_init( &cacert );
  702. mbedtls_x509_crt_init( &clicert );
  703. mbedtls_pk_init( &pkey );
  704. #endif
  705. #if defined(MBEDTLS_SSL_ALPN)
  706. memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
  707. #endif
  708. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  709. status = psa_crypto_init();
  710. if( status != PSA_SUCCESS )
  711. {
  712. mbedtls_fprintf( stderr, "Failed to initialize PSA Crypto implementation: %d\n",
  713. (int) status );
  714. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  715. goto exit;
  716. }
  717. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  718. #if defined(MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG)
  719. mbedtls_test_enable_insecure_external_rng( );
  720. #endif /* MBEDTLS_PSA_CRYPTO_EXTERNAL_RNG */
  721. if( argc == 0 )
  722. {
  723. usage:
  724. if( ret == 0 )
  725. ret = 1;
  726. mbedtls_printf( USAGE1 );
  727. mbedtls_printf( USAGE2 );
  728. mbedtls_printf( USAGE3 );
  729. mbedtls_printf( USAGE4 );
  730. list = mbedtls_ssl_list_ciphersuites();
  731. while( *list )
  732. {
  733. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  734. list++;
  735. if( !*list )
  736. break;
  737. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  738. list++;
  739. }
  740. mbedtls_printf("\n");
  741. goto exit;
  742. }
  743. opt.server_name = DFL_SERVER_NAME;
  744. opt.server_addr = DFL_SERVER_ADDR;
  745. opt.server_port = DFL_SERVER_PORT;
  746. opt.debug_level = DFL_DEBUG_LEVEL;
  747. opt.cid_enabled = DFL_CID_ENABLED;
  748. opt.cid_val = DFL_CID_VALUE;
  749. opt.cid_enabled_renego = DFL_CID_ENABLED_RENEGO;
  750. opt.cid_val_renego = DFL_CID_VALUE_RENEGO;
  751. opt.nbio = DFL_NBIO;
  752. opt.event = DFL_EVENT;
  753. opt.context_crt_cb = DFL_CONTEXT_CRT_CB;
  754. opt.read_timeout = DFL_READ_TIMEOUT;
  755. opt.max_resend = DFL_MAX_RESEND;
  756. opt.request_page = DFL_REQUEST_PAGE;
  757. opt.request_size = DFL_REQUEST_SIZE;
  758. opt.ca_file = DFL_CA_FILE;
  759. opt.ca_path = DFL_CA_PATH;
  760. opt.crt_file = DFL_CRT_FILE;
  761. opt.key_file = DFL_KEY_FILE;
  762. opt.key_opaque = DFL_KEY_OPAQUE;
  763. opt.key_pwd = DFL_KEY_PWD;
  764. opt.psk = DFL_PSK;
  765. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  766. opt.psk_opaque = DFL_PSK_OPAQUE;
  767. #endif
  768. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  769. opt.ca_callback = DFL_CA_CALLBACK;
  770. #endif
  771. opt.psk_identity = DFL_PSK_IDENTITY;
  772. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  773. opt.ec_max_ops = DFL_EC_MAX_OPS;
  774. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  775. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  776. opt.tls13_kex_modes = DFL_TLS13_KEX_MODES;
  777. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  778. opt.renegotiation = DFL_RENEGOTIATION;
  779. opt.allow_legacy = DFL_ALLOW_LEGACY;
  780. opt.renegotiate = DFL_RENEGOTIATE;
  781. opt.exchanges = DFL_EXCHANGES;
  782. opt.min_version = DFL_MIN_VERSION;
  783. opt.max_version = DFL_MAX_VERSION;
  784. opt.allow_sha1 = DFL_SHA1;
  785. opt.auth_mode = DFL_AUTH_MODE;
  786. opt.mfl_code = DFL_MFL_CODE;
  787. opt.trunc_hmac = DFL_TRUNC_HMAC;
  788. opt.recsplit = DFL_RECSPLIT;
  789. opt.dhmlen = DFL_DHMLEN;
  790. opt.reconnect = DFL_RECONNECT;
  791. opt.reco_delay = DFL_RECO_DELAY;
  792. opt.reco_mode = DFL_RECO_MODE;
  793. opt.reconnect_hard = DFL_RECONNECT_HARD;
  794. opt.tickets = DFL_TICKETS;
  795. opt.alpn_string = DFL_ALPN_STRING;
  796. opt.curves = DFL_CURVES;
  797. opt.sig_algs = DFL_SIG_ALGS;
  798. opt.transport = DFL_TRANSPORT;
  799. opt.hs_to_min = DFL_HS_TO_MIN;
  800. opt.hs_to_max = DFL_HS_TO_MAX;
  801. opt.dtls_mtu = DFL_DTLS_MTU;
  802. opt.fallback = DFL_FALLBACK;
  803. opt.extended_ms = DFL_EXTENDED_MS;
  804. opt.etm = DFL_ETM;
  805. opt.dgram_packing = DFL_DGRAM_PACKING;
  806. opt.serialize = DFL_SERIALIZE;
  807. opt.context_file = DFL_CONTEXT_FILE;
  808. opt.eap_tls = DFL_EAP_TLS;
  809. opt.reproducible = DFL_REPRODUCIBLE;
  810. opt.nss_keylog = DFL_NSS_KEYLOG;
  811. opt.nss_keylog_file = DFL_NSS_KEYLOG_FILE;
  812. opt.skip_close_notify = DFL_SKIP_CLOSE_NOTIFY;
  813. opt.query_config_mode = DFL_QUERY_CONFIG_MODE;
  814. opt.use_srtp = DFL_USE_SRTP;
  815. opt.force_srtp_profile = DFL_SRTP_FORCE_PROFILE;
  816. opt.mki = DFL_SRTP_MKI;
  817. for( i = 1; i < argc; i++ )
  818. {
  819. p = argv[i];
  820. if( ( q = strchr( p, '=' ) ) == NULL )
  821. goto usage;
  822. *q++ = '\0';
  823. if( strcmp( p, "server_name" ) == 0 )
  824. opt.server_name = q;
  825. else if( strcmp( p, "server_addr" ) == 0 )
  826. opt.server_addr = q;
  827. else if( strcmp( p, "server_port" ) == 0 )
  828. opt.server_port = q;
  829. else if( strcmp( p, "dtls" ) == 0 )
  830. {
  831. int t = atoi( q );
  832. if( t == 0 )
  833. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  834. else if( t == 1 )
  835. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  836. else
  837. goto usage;
  838. }
  839. else if( strcmp( p, "debug_level" ) == 0 )
  840. {
  841. opt.debug_level = atoi( q );
  842. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  843. goto usage;
  844. }
  845. else if( strcmp( p, "context_crt_cb" ) == 0 )
  846. {
  847. opt.context_crt_cb = atoi( q );
  848. if( opt.context_crt_cb != 0 && opt.context_crt_cb != 1 )
  849. goto usage;
  850. }
  851. else if( strcmp( p, "nbio" ) == 0 )
  852. {
  853. opt.nbio = atoi( q );
  854. if( opt.nbio < 0 || opt.nbio > 2 )
  855. goto usage;
  856. }
  857. else if( strcmp( p, "event" ) == 0 )
  858. {
  859. opt.event = atoi( q );
  860. if( opt.event < 0 || opt.event > 2 )
  861. goto usage;
  862. }
  863. else if( strcmp( p, "read_timeout" ) == 0 )
  864. opt.read_timeout = atoi( q );
  865. else if( strcmp( p, "max_resend" ) == 0 )
  866. {
  867. opt.max_resend = atoi( q );
  868. if( opt.max_resend < 0 )
  869. goto usage;
  870. }
  871. else if( strcmp( p, "request_page" ) == 0 )
  872. opt.request_page = q;
  873. else if( strcmp( p, "request_size" ) == 0 )
  874. {
  875. opt.request_size = atoi( q );
  876. if( opt.request_size < 0 ||
  877. opt.request_size > MAX_REQUEST_SIZE )
  878. goto usage;
  879. }
  880. else if( strcmp( p, "ca_file" ) == 0 )
  881. opt.ca_file = q;
  882. else if( strcmp( p, "ca_path" ) == 0 )
  883. opt.ca_path = q;
  884. else if( strcmp( p, "crt_file" ) == 0 )
  885. opt.crt_file = q;
  886. else if( strcmp( p, "key_file" ) == 0 )
  887. opt.key_file = q;
  888. else if( strcmp( p, "key_pwd" ) == 0 )
  889. opt.key_pwd = q;
  890. #if defined(MBEDTLS_USE_PSA_CRYPTO) && defined(MBEDTLS_X509_CRT_PARSE_C)
  891. else if( strcmp( p, "key_opaque" ) == 0 )
  892. opt.key_opaque = atoi( q );
  893. #endif
  894. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  895. else if( strcmp( p, "cid" ) == 0 )
  896. {
  897. opt.cid_enabled = atoi( q );
  898. if( opt.cid_enabled != 0 && opt.cid_enabled != 1 )
  899. goto usage;
  900. }
  901. else if( strcmp( p, "cid_renego" ) == 0 )
  902. {
  903. opt.cid_enabled_renego = atoi( q );
  904. if( opt.cid_enabled_renego != 0 && opt.cid_enabled_renego != 1 )
  905. goto usage;
  906. }
  907. else if( strcmp( p, "cid_val" ) == 0 )
  908. {
  909. opt.cid_val = q;
  910. }
  911. else if( strcmp( p, "cid_val_renego" ) == 0 )
  912. {
  913. opt.cid_val_renego = q;
  914. }
  915. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  916. else if( strcmp( p, "psk" ) == 0 )
  917. opt.psk = q;
  918. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  919. else if( strcmp( p, "psk_opaque" ) == 0 )
  920. opt.psk_opaque = atoi( q );
  921. #endif
  922. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  923. else if( strcmp( p, "ca_callback" ) == 0)
  924. opt.ca_callback = atoi( q );
  925. #endif
  926. else if( strcmp( p, "psk_identity" ) == 0 )
  927. opt.psk_identity = q;
  928. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  929. opt.ecjpake_pw = q;
  930. else if( strcmp( p, "ec_max_ops" ) == 0 )
  931. opt.ec_max_ops = atoi( q );
  932. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  933. {
  934. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  935. if( opt.force_ciphersuite[0] == 0 )
  936. {
  937. ret = 2;
  938. goto usage;
  939. }
  940. opt.force_ciphersuite[1] = 0;
  941. }
  942. else if( strcmp( p, "renegotiation" ) == 0 )
  943. {
  944. opt.renegotiation = (atoi( q )) ?
  945. MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  946. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  947. }
  948. else if( strcmp( p, "allow_legacy" ) == 0 )
  949. {
  950. switch( atoi( q ) )
  951. {
  952. case -1:
  953. opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
  954. break;
  955. case 0:
  956. opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  957. break;
  958. case 1:
  959. opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
  960. break;
  961. default: goto usage;
  962. }
  963. }
  964. else if( strcmp( p, "renegotiate" ) == 0 )
  965. {
  966. opt.renegotiate = atoi( q );
  967. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  968. goto usage;
  969. }
  970. else if( strcmp( p, "exchanges" ) == 0 )
  971. {
  972. opt.exchanges = atoi( q );
  973. if( opt.exchanges < 1 )
  974. goto usage;
  975. }
  976. else if( strcmp( p, "reconnect" ) == 0 )
  977. {
  978. opt.reconnect = atoi( q );
  979. if( opt.reconnect < 0 || opt.reconnect > 2 )
  980. goto usage;
  981. }
  982. else if( strcmp( p, "reco_delay" ) == 0 )
  983. {
  984. opt.reco_delay = atoi( q );
  985. if( opt.reco_delay < 0 )
  986. goto usage;
  987. }
  988. else if( strcmp( p, "reco_mode" ) == 0 )
  989. {
  990. opt.reco_mode = atoi( q );
  991. if( opt.reco_mode < 0 )
  992. goto usage;
  993. }
  994. else if( strcmp( p, "reconnect_hard" ) == 0 )
  995. {
  996. opt.reconnect_hard = atoi( q );
  997. if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
  998. goto usage;
  999. }
  1000. else if( strcmp( p, "tickets" ) == 0 )
  1001. {
  1002. opt.tickets = atoi( q );
  1003. if( opt.tickets < 0 || opt.tickets > 2 )
  1004. goto usage;
  1005. }
  1006. else if( strcmp( p, "alpn" ) == 0 )
  1007. {
  1008. opt.alpn_string = q;
  1009. }
  1010. else if( strcmp( p, "extended_ms" ) == 0 )
  1011. {
  1012. switch( atoi( q ) )
  1013. {
  1014. case 0:
  1015. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
  1016. break;
  1017. case 1:
  1018. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  1019. break;
  1020. default: goto usage;
  1021. }
  1022. }
  1023. else if( strcmp( p, "curves" ) == 0 )
  1024. opt.curves = q;
  1025. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  1026. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1027. else if( strcmp( p, "sig_algs" ) == 0 )
  1028. opt.sig_algs = q;
  1029. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL &&
  1030. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1031. else if( strcmp( p, "etm" ) == 0 )
  1032. {
  1033. switch( atoi( q ) )
  1034. {
  1035. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  1036. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  1037. default: goto usage;
  1038. }
  1039. }
  1040. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1041. else if( strcmp( p, "tls13_kex_modes" ) == 0 )
  1042. {
  1043. if( strcmp( q, "psk" ) == 0 )
  1044. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK;
  1045. else if( strcmp(q, "psk_ephemeral" ) == 0 )
  1046. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_EPHEMERAL;
  1047. else if( strcmp(q, "ephemeral" ) == 0 )
  1048. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL;
  1049. else if( strcmp(q, "ephemeral_all" ) == 0 )
  1050. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_EPHEMERAL_ALL;
  1051. else if( strcmp( q, "psk_all" ) == 0 )
  1052. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_PSK_ALL;
  1053. else if( strcmp( q, "all" ) == 0 )
  1054. opt.tls13_kex_modes = MBEDTLS_SSL_TLS13_KEY_EXCHANGE_MODE_ALL;
  1055. else goto usage;
  1056. }
  1057. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1058. else if( strcmp( p, "min_version" ) == 0 )
  1059. {
  1060. if( strcmp( q, "tls1_2" ) == 0 ||
  1061. strcmp( q, "dtls1_2" ) == 0 )
  1062. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1063. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1064. else if( strcmp( q, "tls1_3" ) == 0 )
  1065. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1066. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1067. else
  1068. goto usage;
  1069. }
  1070. else if( strcmp( p, "max_version" ) == 0 )
  1071. {
  1072. if( strcmp( q, "tls1_2" ) == 0 ||
  1073. strcmp( q, "dtls1_2" ) == 0 )
  1074. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1075. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1076. else if( strcmp( q, "tls1_3" ) == 0 )
  1077. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1078. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1079. else
  1080. goto usage;
  1081. }
  1082. else if( strcmp( p, "allow_sha1" ) == 0 )
  1083. {
  1084. switch( atoi( q ) )
  1085. {
  1086. case 0: opt.allow_sha1 = 0; break;
  1087. case 1: opt.allow_sha1 = 1; break;
  1088. default: goto usage;
  1089. }
  1090. }
  1091. else if( strcmp( p, "force_version" ) == 0 )
  1092. {
  1093. if( strcmp( q, "tls1_2" ) == 0 )
  1094. {
  1095. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1096. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1097. }
  1098. else if( strcmp( q, "dtls1_2" ) == 0 )
  1099. {
  1100. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1101. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1102. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  1103. }
  1104. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1105. else if( strcmp( q, "tls1_3" ) == 0 )
  1106. {
  1107. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1108. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_4;
  1109. }
  1110. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1111. else
  1112. goto usage;
  1113. }
  1114. else if( strcmp( p, "auth_mode" ) == 0 )
  1115. {
  1116. if( strcmp( q, "none" ) == 0 )
  1117. opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
  1118. else if( strcmp( q, "optional" ) == 0 )
  1119. opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
  1120. else if( strcmp( q, "required" ) == 0 )
  1121. opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  1122. else
  1123. goto usage;
  1124. }
  1125. else if( strcmp( p, "max_frag_len" ) == 0 )
  1126. {
  1127. if( strcmp( q, "512" ) == 0 )
  1128. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  1129. else if( strcmp( q, "1024" ) == 0 )
  1130. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  1131. else if( strcmp( q, "2048" ) == 0 )
  1132. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  1133. else if( strcmp( q, "4096" ) == 0 )
  1134. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  1135. else
  1136. goto usage;
  1137. }
  1138. else if( strcmp( p, "trunc_hmac" ) == 0 )
  1139. {
  1140. switch( atoi( q ) )
  1141. {
  1142. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  1143. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  1144. default: goto usage;
  1145. }
  1146. }
  1147. else if( strcmp( p, "hs_timeout" ) == 0 )
  1148. {
  1149. if( ( p = strchr( q, '-' ) ) == NULL )
  1150. goto usage;
  1151. *p++ = '\0';
  1152. opt.hs_to_min = atoi( q );
  1153. opt.hs_to_max = atoi( p );
  1154. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  1155. goto usage;
  1156. }
  1157. else if( strcmp( p, "mtu" ) == 0 )
  1158. {
  1159. opt.dtls_mtu = atoi( q );
  1160. if( opt.dtls_mtu < 0 )
  1161. goto usage;
  1162. }
  1163. else if( strcmp( p, "dgram_packing" ) == 0 )
  1164. {
  1165. opt.dgram_packing = atoi( q );
  1166. if( opt.dgram_packing != 0 &&
  1167. opt.dgram_packing != 1 )
  1168. {
  1169. goto usage;
  1170. }
  1171. }
  1172. else if( strcmp( p, "recsplit" ) == 0 )
  1173. {
  1174. opt.recsplit = atoi( q );
  1175. if( opt.recsplit < 0 || opt.recsplit > 1 )
  1176. goto usage;
  1177. }
  1178. else if( strcmp( p, "dhmlen" ) == 0 )
  1179. {
  1180. opt.dhmlen = atoi( q );
  1181. if( opt.dhmlen < 0 )
  1182. goto usage;
  1183. }
  1184. else if( strcmp( p, "query_config" ) == 0 )
  1185. {
  1186. opt.query_config_mode = 1;
  1187. query_config_ret = query_config( q );
  1188. goto exit;
  1189. }
  1190. else if( strcmp( p, "serialize") == 0 )
  1191. {
  1192. opt.serialize = atoi( q );
  1193. if( opt.serialize < 0 || opt.serialize > 2)
  1194. goto usage;
  1195. }
  1196. else if( strcmp( p, "context_file") == 0 )
  1197. {
  1198. opt.context_file = q;
  1199. }
  1200. else if( strcmp( p, "eap_tls" ) == 0 )
  1201. {
  1202. opt.eap_tls = atoi( q );
  1203. if( opt.eap_tls < 0 || opt.eap_tls > 1 )
  1204. goto usage;
  1205. }
  1206. else if( strcmp( p, "reproducible" ) == 0 )
  1207. {
  1208. opt.reproducible = 1;
  1209. }
  1210. else if( strcmp( p, "nss_keylog" ) == 0 )
  1211. {
  1212. opt.nss_keylog = atoi( q );
  1213. if( opt.nss_keylog < 0 || opt.nss_keylog > 1 )
  1214. goto usage;
  1215. }
  1216. else if( strcmp( p, "nss_keylog_file" ) == 0 )
  1217. {
  1218. opt.nss_keylog_file = q;
  1219. }
  1220. else if( strcmp( p, "skip_close_notify" ) == 0 )
  1221. {
  1222. opt.skip_close_notify = atoi( q );
  1223. if( opt.skip_close_notify < 0 || opt.skip_close_notify > 1 )
  1224. goto usage;
  1225. }
  1226. else if( strcmp( p, "use_srtp" ) == 0 )
  1227. {
  1228. opt.use_srtp = atoi ( q );
  1229. }
  1230. else if( strcmp( p, "srtp_force_profile" ) == 0 )
  1231. {
  1232. opt.force_srtp_profile = atoi( q );
  1233. }
  1234. else if( strcmp( p, "mki" ) == 0 )
  1235. {
  1236. opt.mki = q;
  1237. }
  1238. else
  1239. goto usage;
  1240. }
  1241. if( opt.nss_keylog != 0 && opt.eap_tls != 0 )
  1242. {
  1243. mbedtls_printf( "Error: eap_tls and nss_keylog options cannot be used together.\n" );
  1244. goto usage;
  1245. }
  1246. /* Event-driven IO is incompatible with the above custom
  1247. * receive and send functions, as the polling builds on
  1248. * refers to the underlying net_context. */
  1249. if( opt.event == 1 && opt.nbio != 1 )
  1250. {
  1251. mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
  1252. opt.nbio = 1;
  1253. }
  1254. #if defined(MBEDTLS_DEBUG_C)
  1255. mbedtls_debug_set_threshold( opt.debug_level );
  1256. #endif
  1257. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1258. /*
  1259. * Unhexify the pre-shared key if any is given
  1260. */
  1261. if( strlen( opt.psk ) )
  1262. {
  1263. if( mbedtls_test_unhexify( psk, sizeof( psk ),
  1264. opt.psk, &psk_len ) != 0 )
  1265. {
  1266. mbedtls_printf( "pre-shared key not valid\n" );
  1267. goto exit;
  1268. }
  1269. }
  1270. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1271. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1272. if( opt.psk_opaque != 0 )
  1273. {
  1274. if( opt.psk == NULL )
  1275. {
  1276. mbedtls_printf( "psk_opaque set but no psk to be imported specified.\n" );
  1277. ret = 2;
  1278. goto usage;
  1279. }
  1280. if( opt.force_ciphersuite[0] <= 0 )
  1281. {
  1282. 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" );
  1283. ret = 2;
  1284. goto usage;
  1285. }
  1286. }
  1287. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1288. if( opt.force_ciphersuite[0] > 0 )
  1289. {
  1290. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  1291. ciphersuite_info =
  1292. mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  1293. if( opt.max_version != -1 &&
  1294. ciphersuite_info->min_minor_ver > opt.max_version )
  1295. {
  1296. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1297. ret = 2;
  1298. goto usage;
  1299. }
  1300. if( opt.min_version != -1 &&
  1301. ciphersuite_info->max_minor_ver < opt.min_version )
  1302. {
  1303. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  1304. ret = 2;
  1305. goto usage;
  1306. }
  1307. /* If the server selects a version that's not supported by
  1308. * this suite, then there will be no common ciphersuite... */
  1309. if( opt.max_version == -1 ||
  1310. opt.max_version > ciphersuite_info->max_minor_ver )
  1311. {
  1312. opt.max_version = ciphersuite_info->max_minor_ver;
  1313. }
  1314. if( opt.min_version < ciphersuite_info->min_minor_ver )
  1315. {
  1316. opt.min_version = ciphersuite_info->min_minor_ver;
  1317. /* DTLS starts with TLS 1.2 */
  1318. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  1319. opt.min_version < MBEDTLS_SSL_MINOR_VERSION_3 )
  1320. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  1321. }
  1322. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1323. if( opt.psk_opaque != 0 )
  1324. {
  1325. /* Ensure that the chosen ciphersuite is PSK-only; we must know
  1326. * the ciphersuite in advance to set the correct policy for the
  1327. * PSK key slot. This limitation might go away in the future. */
  1328. if( ciphersuite_info->key_exchange != MBEDTLS_KEY_EXCHANGE_PSK ||
  1329. opt.min_version != MBEDTLS_SSL_MINOR_VERSION_3 )
  1330. {
  1331. 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" );
  1332. ret = 2;
  1333. goto usage;
  1334. }
  1335. /* Determine KDF algorithm the opaque PSK will be used in. */
  1336. #if defined(MBEDTLS_SHA384_C)
  1337. if( ciphersuite_info->mac == MBEDTLS_MD_SHA384 )
  1338. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_384);
  1339. else
  1340. #endif /* MBEDTLS_SHA384_C */
  1341. alg = PSA_ALG_TLS12_PSK_TO_MS(PSA_ALG_SHA_256);
  1342. }
  1343. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1344. }
  1345. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1346. if( mbedtls_test_unhexify( cid, sizeof( cid ),
  1347. opt.cid_val, &cid_len ) != 0 )
  1348. {
  1349. mbedtls_printf( "CID not valid\n" );
  1350. goto exit;
  1351. }
  1352. /* Keep CID settings for renegotiation unless
  1353. * specified otherwise. */
  1354. if( opt.cid_enabled_renego == DFL_CID_ENABLED_RENEGO )
  1355. opt.cid_enabled_renego = opt.cid_enabled;
  1356. if( opt.cid_val_renego == DFL_CID_VALUE_RENEGO )
  1357. opt.cid_val_renego = opt.cid_val;
  1358. if( mbedtls_test_unhexify( cid_renego, sizeof( cid_renego ),
  1359. opt.cid_val_renego, &cid_renego_len ) != 0 )
  1360. {
  1361. mbedtls_printf( "CID not valid\n" );
  1362. goto exit;
  1363. }
  1364. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1365. #if defined(MBEDTLS_ECP_C)
  1366. if( opt.curves != NULL )
  1367. {
  1368. p = (char *) opt.curves;
  1369. i = 0;
  1370. if( strcmp( p, "none" ) == 0 )
  1371. {
  1372. group_list[0] = 0;
  1373. }
  1374. else if( strcmp( p, "default" ) != 0 )
  1375. {
  1376. /* Leave room for a final NULL in curve list */
  1377. while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
  1378. {
  1379. q = p;
  1380. /* Terminate the current string */
  1381. while( *p != ',' && *p != '\0' )
  1382. p++;
  1383. if( *p == ',' )
  1384. *p++ = '\0';
  1385. if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
  1386. {
  1387. group_list[i++] = curve_cur->tls_id;
  1388. }
  1389. else
  1390. {
  1391. mbedtls_printf( "unknown curve %s\n", q );
  1392. mbedtls_printf( "supported curves: " );
  1393. for( curve_cur = mbedtls_ecp_curve_list();
  1394. curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
  1395. curve_cur++ )
  1396. {
  1397. mbedtls_printf( "%s ", curve_cur->name );
  1398. }
  1399. mbedtls_printf( "\n" );
  1400. goto exit;
  1401. }
  1402. }
  1403. mbedtls_printf("Number of curves: %d\n", i );
  1404. if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
  1405. {
  1406. mbedtls_printf( "curves list too long, maximum %d",
  1407. CURVE_LIST_SIZE - 1 );
  1408. goto exit;
  1409. }
  1410. group_list[i] = 0;
  1411. }
  1412. }
  1413. #endif /* MBEDTLS_ECP_C */
  1414. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL) && \
  1415. defined(MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED)
  1416. if( opt.sig_algs != NULL )
  1417. {
  1418. p = (char *) opt.sig_algs;
  1419. i = 0;
  1420. /* Leave room for a final MBEDTLS_TLS13_SIG_NONE in signature algorithm list (sig_alg_list). */
  1421. while( i < SIG_ALG_LIST_SIZE - 1 && *p != '\0' )
  1422. {
  1423. q = p;
  1424. /* Terminate the current string */
  1425. while( *p != ',' && *p != '\0' )
  1426. p++;
  1427. if( *p == ',' )
  1428. *p++ = '\0';
  1429. if( strcmp( q, "ecdsa_secp256r1_sha256" ) == 0 )
  1430. {
  1431. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP256R1_SHA256;
  1432. }
  1433. else if( strcmp( q, "ecdsa_secp384r1_sha384" ) == 0 )
  1434. {
  1435. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP384R1_SHA384;
  1436. }
  1437. else if( strcmp( q, "ecdsa_secp521r1_sha512" ) == 0 )
  1438. {
  1439. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_ECDSA_SECP521R1_SHA512;
  1440. }
  1441. else if( strcmp( q, "rsa_pss_rsae_sha256" ) == 0 )
  1442. {
  1443. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_RSA_PSS_RSAE_SHA256;
  1444. }
  1445. else if( strcmp( q, "rsa_pkcs1_sha256" ) == 0 )
  1446. {
  1447. sig_alg_list[i++] = MBEDTLS_TLS13_SIG_RSA_PKCS1_SHA256;
  1448. }
  1449. else
  1450. {
  1451. mbedtls_printf( "unknown signature algorithm %s\n", q );
  1452. mbedtls_printf( "supported signature algorithms: " );
  1453. mbedtls_printf( "ecdsa_secp256r1_sha256 " );
  1454. mbedtls_printf( "ecdsa_secp384r1_sha384 " );
  1455. mbedtls_printf( "ecdsa_secp521r1_sha512 " );
  1456. mbedtls_printf( "rsa_pss_rsae_sha256 " );
  1457. mbedtls_printf( "rsa_pkcs1_sha256 " );
  1458. mbedtls_printf( "\n" );
  1459. goto exit;
  1460. }
  1461. }
  1462. if( i == ( SIG_ALG_LIST_SIZE - 1 ) && *p != '\0' )
  1463. {
  1464. mbedtls_printf( "signature algorithm list too long, maximum %d",
  1465. SIG_ALG_LIST_SIZE - 1 );
  1466. goto exit;
  1467. }
  1468. sig_alg_list[i] = MBEDTLS_TLS13_SIG_NONE;
  1469. }
  1470. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL &&
  1471. MBEDTLS_KEY_EXCHANGE_WITH_CERT_ENABLED */
  1472. #if defined(MBEDTLS_SSL_ALPN)
  1473. if( opt.alpn_string != NULL )
  1474. {
  1475. p = (char *) opt.alpn_string;
  1476. i = 0;
  1477. /* Leave room for a final NULL in alpn_list */
  1478. while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
  1479. {
  1480. alpn_list[i++] = p;
  1481. /* Terminate the current string and move on to next one */
  1482. while( *p != ',' && *p != '\0' )
  1483. p++;
  1484. if( *p == ',' )
  1485. *p++ = '\0';
  1486. }
  1487. }
  1488. #endif /* MBEDTLS_SSL_ALPN */
  1489. /*
  1490. * 0. Initialize the RNG and the session data
  1491. */
  1492. mbedtls_printf( "\n . Seeding the random number generator..." );
  1493. fflush( stdout );
  1494. ret = rng_seed( &rng, opt.reproducible, pers );
  1495. if( ret != 0 )
  1496. goto exit;
  1497. mbedtls_printf( " ok\n" );
  1498. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1499. /*
  1500. * 1.1. Load the trusted CA
  1501. */
  1502. mbedtls_printf( " . Loading the CA root certificate ..." );
  1503. fflush( stdout );
  1504. if( strcmp( opt.ca_path, "none" ) == 0 ||
  1505. strcmp( opt.ca_file, "none" ) == 0 )
  1506. {
  1507. ret = 0;
  1508. }
  1509. else
  1510. #if defined(MBEDTLS_FS_IO)
  1511. if( strlen( opt.ca_path ) )
  1512. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  1513. else if( strlen( opt.ca_file ) )
  1514. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  1515. else
  1516. #endif
  1517. {
  1518. #if defined(MBEDTLS_PEM_PARSE_C)
  1519. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  1520. {
  1521. ret = mbedtls_x509_crt_parse( &cacert,
  1522. (const unsigned char *) mbedtls_test_cas[i],
  1523. mbedtls_test_cas_len[i] );
  1524. if( ret != 0 )
  1525. break;
  1526. }
  1527. if( ret == 0 )
  1528. #endif /* MBEDTLS_PEM_PARSE_C */
  1529. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  1530. {
  1531. ret = mbedtls_x509_crt_parse_der( &cacert,
  1532. (const unsigned char *) mbedtls_test_cas_der[i],
  1533. mbedtls_test_cas_der_len[i] );
  1534. if( ret != 0 )
  1535. break;
  1536. }
  1537. }
  1538. if( ret < 0 )
  1539. {
  1540. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  1541. (unsigned int) -ret );
  1542. goto exit;
  1543. }
  1544. mbedtls_printf( " ok (%d skipped)\n", ret );
  1545. /*
  1546. * 1.2. Load own certificate and private key
  1547. *
  1548. * (can be skipped if client authentication is not required)
  1549. */
  1550. mbedtls_printf( " . Loading the client cert. and key..." );
  1551. fflush( stdout );
  1552. if( strcmp( opt.crt_file, "none" ) == 0 )
  1553. ret = 0;
  1554. else
  1555. #if defined(MBEDTLS_FS_IO)
  1556. if( strlen( opt.crt_file ) )
  1557. ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
  1558. else
  1559. #endif
  1560. ret = mbedtls_x509_crt_parse( &clicert,
  1561. (const unsigned char *) mbedtls_test_cli_crt,
  1562. mbedtls_test_cli_crt_len );
  1563. if( ret != 0 )
  1564. {
  1565. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  1566. (unsigned int) -ret );
  1567. goto exit;
  1568. }
  1569. if( strcmp( opt.key_file, "none" ) == 0 )
  1570. ret = 0;
  1571. else
  1572. #if defined(MBEDTLS_FS_IO)
  1573. if( strlen( opt.key_file ) )
  1574. ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, opt.key_pwd, rng_get, &rng );
  1575. else
  1576. #endif
  1577. ret = mbedtls_pk_parse_key( &pkey,
  1578. (const unsigned char *) mbedtls_test_cli_key,
  1579. mbedtls_test_cli_key_len, NULL, 0, rng_get, &rng );
  1580. if( ret != 0 )
  1581. {
  1582. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
  1583. (unsigned int) -ret );
  1584. goto exit;
  1585. }
  1586. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1587. if( opt.key_opaque != 0 )
  1588. {
  1589. if( ( ret = mbedtls_pk_wrap_as_opaque( &pkey, &key_slot,
  1590. PSA_ALG_ANY_HASH ) ) != 0 )
  1591. {
  1592. mbedtls_printf( " failed\n ! "
  1593. "mbedtls_pk_wrap_as_opaque returned -0x%x\n\n", (unsigned int) -ret );
  1594. goto exit;
  1595. }
  1596. }
  1597. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1598. mbedtls_printf( " ok (key type: %s)\n", mbedtls_pk_get_name( &pkey ) );
  1599. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1600. /*
  1601. * 2. Setup stuff
  1602. */
  1603. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  1604. fflush( stdout );
  1605. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  1606. MBEDTLS_SSL_IS_CLIENT,
  1607. opt.transport,
  1608. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  1609. {
  1610. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
  1611. (unsigned int) -ret );
  1612. goto exit;
  1613. }
  1614. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1615. /* The default algorithms profile disables SHA-1, but our tests still
  1616. rely on it heavily. */
  1617. if( opt.allow_sha1 > 0 )
  1618. {
  1619. crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
  1620. mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
  1621. mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
  1622. }
  1623. if( opt.context_crt_cb == 0 )
  1624. mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
  1625. memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
  1626. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1627. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1628. if( opt.cid_enabled == 1 || opt.cid_enabled_renego == 1 )
  1629. {
  1630. if( opt.cid_enabled == 1 &&
  1631. opt.cid_enabled_renego == 1 &&
  1632. cid_len != cid_renego_len )
  1633. {
  1634. mbedtls_printf( "CID length must not change during renegotiation\n" );
  1635. goto usage;
  1636. }
  1637. if( opt.cid_enabled == 1 )
  1638. ret = mbedtls_ssl_conf_cid( &conf, cid_len,
  1639. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  1640. else
  1641. ret = mbedtls_ssl_conf_cid( &conf, cid_renego_len,
  1642. MBEDTLS_SSL_UNEXPECTED_CID_IGNORE );
  1643. if( ret != 0 )
  1644. {
  1645. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_cid_len returned -%#04x\n\n",
  1646. (unsigned int) -ret );
  1647. goto exit;
  1648. }
  1649. }
  1650. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1651. if( opt.auth_mode != DFL_AUTH_MODE )
  1652. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  1653. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1654. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  1655. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
  1656. opt.hs_to_max );
  1657. if( opt.dgram_packing != DFL_DGRAM_PACKING )
  1658. mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
  1659. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1660. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1661. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  1662. {
  1663. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
  1664. ret );
  1665. goto exit;
  1666. }
  1667. #endif
  1668. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1669. const mbedtls_ssl_srtp_profile forced_profile[] =
  1670. { opt.force_srtp_profile, MBEDTLS_TLS_SRTP_UNSET };
  1671. if( opt.use_srtp == 1 )
  1672. {
  1673. if( opt.force_srtp_profile != 0 )
  1674. {
  1675. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles ( &conf, forced_profile );
  1676. }
  1677. else
  1678. {
  1679. ret = mbedtls_ssl_conf_dtls_srtp_protection_profiles ( &conf, default_profiles );
  1680. }
  1681. if( ret != 0 )
  1682. {
  1683. mbedtls_printf( " failed\n ! "
  1684. "mbedtls_ssl_conf_dtls_srtp_protection_profiles returned %d\n\n",
  1685. ret );
  1686. goto exit;
  1687. }
  1688. }
  1689. else if( opt.force_srtp_profile != 0 )
  1690. {
  1691. mbedtls_printf( " failed\n ! must enable use_srtp to force srtp profile\n\n" );
  1692. goto exit;
  1693. }
  1694. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1695. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1696. if( opt.extended_ms != DFL_EXTENDED_MS )
  1697. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  1698. #endif
  1699. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1700. if( opt.etm != DFL_ETM )
  1701. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  1702. #endif
  1703. #if defined(MBEDTLS_DHM_C)
  1704. if( opt.dhmlen != DFL_DHMLEN )
  1705. mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
  1706. #endif
  1707. #if defined(MBEDTLS_SSL_ALPN)
  1708. if( opt.alpn_string != NULL )
  1709. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  1710. {
  1711. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
  1712. ret );
  1713. goto exit;
  1714. }
  1715. #endif
  1716. if (opt.reproducible)
  1717. {
  1718. #if defined(MBEDTLS_HAVE_TIME)
  1719. #if defined(MBEDTLS_PLATFORM_TIME_ALT)
  1720. mbedtls_platform_set_time( dummy_constant_time );
  1721. #else
  1722. fprintf( stderr, "Warning: reproducible option used without constant time\n" );
  1723. #endif
  1724. #endif /* MBEDTLS_HAVE_TIME */
  1725. }
  1726. mbedtls_ssl_conf_rng( &conf, rng_get, &rng );
  1727. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  1728. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  1729. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1730. mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
  1731. #endif
  1732. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  1733. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  1734. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1735. mbedtls_ssl_conf_tls13_key_exchange_modes( &conf, opt.tls13_kex_modes );
  1736. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1737. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  1738. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  1739. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1740. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  1741. #endif
  1742. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1743. if( strcmp( opt.ca_path, "none" ) != 0 &&
  1744. strcmp( opt.ca_file, "none" ) != 0 )
  1745. {
  1746. #if defined(MBEDTLS_X509_TRUSTED_CERTIFICATE_CALLBACK)
  1747. if( opt.ca_callback != 0 )
  1748. mbedtls_ssl_conf_ca_cb( &conf, ca_callback, &cacert );
  1749. else
  1750. #endif
  1751. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  1752. }
  1753. if( strcmp( opt.crt_file, "none" ) != 0 &&
  1754. strcmp( opt.key_file, "none" ) != 0 )
  1755. {
  1756. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
  1757. {
  1758. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
  1759. ret );
  1760. goto exit;
  1761. }
  1762. }
  1763. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1764. #if defined(MBEDTLS_ECP_C)
  1765. if( opt.curves != NULL &&
  1766. strcmp( opt.curves, "default" ) != 0 )
  1767. {
  1768. mbedtls_ssl_conf_groups( &conf, group_list );
  1769. }
  1770. #endif
  1771. #if defined(MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL)
  1772. if( opt.sig_algs != NULL )
  1773. mbedtls_ssl_conf_sig_algs( &conf, sig_alg_list );
  1774. #endif /* MBEDTLS_SSL_PROTO_TLS1_3_EXPERIMENTAL */
  1775. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED)
  1776. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  1777. if( opt.psk_opaque != 0 )
  1778. {
  1779. key_attributes = psa_key_attributes_init();
  1780. psa_set_key_usage_flags( &key_attributes, PSA_KEY_USAGE_DERIVE );
  1781. psa_set_key_algorithm( &key_attributes, alg );
  1782. psa_set_key_type( &key_attributes, PSA_KEY_TYPE_DERIVE );
  1783. status = psa_import_key( &key_attributes, psk, psk_len, &slot );
  1784. if( status != PSA_SUCCESS )
  1785. {
  1786. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  1787. goto exit;
  1788. }
  1789. if( ( ret = mbedtls_ssl_conf_psk_opaque( &conf, slot,
  1790. (const unsigned char *) opt.psk_identity,
  1791. strlen( opt.psk_identity ) ) ) != 0 )
  1792. {
  1793. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk_opaque returned %d\n\n",
  1794. ret );
  1795. goto exit;
  1796. }
  1797. }
  1798. else
  1799. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  1800. if( psk_len > 0 )
  1801. {
  1802. ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  1803. (const unsigned char *) opt.psk_identity,
  1804. strlen( opt.psk_identity ) );
  1805. if( ret != 0 )
  1806. {
  1807. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n", ret );
  1808. goto exit;
  1809. }
  1810. }
  1811. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED */
  1812. if( opt.min_version != DFL_MIN_VERSION )
  1813. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1814. opt.min_version );
  1815. if( opt.max_version != DFL_MAX_VERSION )
  1816. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1817. opt.max_version );
  1818. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  1819. {
  1820. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
  1821. (unsigned int) -ret );
  1822. goto exit;
  1823. }
  1824. if( opt.eap_tls != 0 )
  1825. {
  1826. mbedtls_ssl_set_export_keys_cb( &ssl, eap_tls_key_derivation,
  1827. &eap_tls_keying );
  1828. }
  1829. else if( opt.nss_keylog != 0 )
  1830. {
  1831. mbedtls_ssl_set_export_keys_cb( &ssl,
  1832. nss_keylog_export,
  1833. NULL );
  1834. }
  1835. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  1836. else if( opt.use_srtp != 0 )
  1837. {
  1838. mbedtls_ssl_set_export_keys_cb( &ssl, dtls_srtp_key_derivation,
  1839. &dtls_srtp_keying );
  1840. }
  1841. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  1842. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1843. if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
  1844. {
  1845. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
  1846. ret );
  1847. goto exit;
  1848. }
  1849. #endif
  1850. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1851. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  1852. {
  1853. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  1854. (const unsigned char *) opt.ecjpake_pw,
  1855. strlen( opt.ecjpake_pw ) ) ) != 0 )
  1856. {
  1857. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
  1858. ret );
  1859. goto exit;
  1860. }
  1861. }
  1862. #endif
  1863. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1864. if( opt.context_crt_cb == 1 )
  1865. mbedtls_ssl_set_verify( &ssl, my_verify, NULL );
  1866. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1867. io_ctx.ssl = &ssl;
  1868. io_ctx.net = &server_fd;
  1869. mbedtls_ssl_set_bio( &ssl, &io_ctx, send_cb, recv_cb,
  1870. opt.nbio == 0 ? recv_timeout_cb : NULL );
  1871. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  1872. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  1873. {
  1874. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled,
  1875. cid, cid_len ) ) != 0 )
  1876. {
  1877. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  1878. ret );
  1879. goto exit;
  1880. }
  1881. }
  1882. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  1883. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1884. if( opt.dtls_mtu != DFL_DTLS_MTU )
  1885. mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
  1886. #endif
  1887. #if defined(MBEDTLS_TIMING_C)
  1888. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  1889. mbedtls_timing_get_delay );
  1890. #endif
  1891. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1892. if( opt.ec_max_ops != DFL_EC_MAX_OPS )
  1893. mbedtls_ecp_set_max_ops( opt.ec_max_ops );
  1894. #endif
  1895. #if defined(MBEDTLS_SSL_DTLS_SRTP)
  1896. if( opt.use_srtp != 0 && strlen( opt.mki ) != 0 )
  1897. {
  1898. if( mbedtls_test_unhexify( mki, sizeof( mki ),
  1899. opt.mki,&mki_len ) != 0 )
  1900. {
  1901. mbedtls_printf( "mki value not valid hex\n" );
  1902. goto exit;
  1903. }
  1904. mbedtls_ssl_conf_srtp_mki_value_supported( &conf, MBEDTLS_SSL_DTLS_SRTP_MKI_SUPPORTED );
  1905. if( ( ret = mbedtls_ssl_dtls_srtp_set_mki_value( &ssl, mki,
  1906. (uint16_t) strlen( opt.mki ) / 2 ) ) != 0 )
  1907. {
  1908. mbedtls_printf( " failed\n ! mbedtls_ssl_dtls_srtp_set_mki_value returned %d\n\n", ret );
  1909. goto exit;
  1910. }
  1911. }
  1912. #endif
  1913. mbedtls_printf( " ok\n" );
  1914. /*
  1915. * 3. Start the connection
  1916. */
  1917. if( opt.server_addr == NULL)
  1918. opt.server_addr = opt.server_name;
  1919. mbedtls_printf( " . Connecting to %s/%s/%s...",
  1920. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  1921. opt.server_addr, opt.server_port );
  1922. fflush( stdout );
  1923. if( ( ret = mbedtls_net_connect( &server_fd,
  1924. opt.server_addr, opt.server_port,
  1925. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  1926. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  1927. {
  1928. mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
  1929. (unsigned int) -ret );
  1930. goto exit;
  1931. }
  1932. if( opt.nbio > 0 )
  1933. ret = mbedtls_net_set_nonblock( &server_fd );
  1934. else
  1935. ret = mbedtls_net_set_block( &server_fd );
  1936. if( ret != 0 )
  1937. {
  1938. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
  1939. (unsigned int) -ret );
  1940. goto exit;
  1941. }
  1942. mbedtls_printf( " ok\n" );
  1943. /*
  1944. * 4. Handshake
  1945. */
  1946. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  1947. fflush( stdout );
  1948. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  1949. {
  1950. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1951. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1952. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1953. {
  1954. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
  1955. (unsigned int) -ret );
  1956. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  1957. mbedtls_printf(
  1958. " Unable to verify the server's certificate. "
  1959. "Either it is invalid,\n"
  1960. " or you didn't set ca_file or ca_path "
  1961. "to an appropriate value.\n"
  1962. " Alternatively, you may want to use "
  1963. "auth_mode=optional for testing purposes.\n" );
  1964. mbedtls_printf( "\n" );
  1965. goto exit;
  1966. }
  1967. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1968. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1969. continue;
  1970. #endif
  1971. /* For event-driven IO, wait for socket to become available */
  1972. if( opt.event == 1 /* level triggered IO */ )
  1973. {
  1974. #if defined(MBEDTLS_TIMING_C)
  1975. ret = idle( &server_fd, &timer, ret );
  1976. #else
  1977. ret = idle( &server_fd, ret );
  1978. #endif
  1979. if( ret != 0 )
  1980. goto exit;
  1981. }
  1982. }
  1983. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  1984. mbedtls_ssl_get_version( &ssl ),
  1985. mbedtls_ssl_get_ciphersuite( &ssl ) );
  1986. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  1987. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  1988. else
  1989. mbedtls_printf( " [ Record expansion is unknown ]\n" );
  1990. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1991. mbedtls_printf( " [ Maximum incoming record payload length is %u ]\n",
  1992. (unsigned int) mbedtls_ssl_get_max_in_record_payload( &ssl ) );
  1993. mbedtls_printf( " [ Maximum outgoing record payload length is %u ]\n",
  1994. (unsigned int) mbedtls_ssl_get_max_out_record_payload( &ssl ) );
  1995. #endif
  1996. #if defined(MBEDTLS_SSL_ALPN)
  1997. if( opt.alpn_string != NULL )
  1998. {
  1999. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  2000. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  2001. alp ? alp : "(none)" );
  2002. }
  2003. #endif
  2004. if( opt.eap_tls != 0 )
  2005. {
  2006. size_t j = 0;
  2007. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type,
  2008. eap_tls_keying.master_secret,
  2009. sizeof( eap_tls_keying.master_secret ),
  2010. eap_tls_label,
  2011. eap_tls_keying.randbytes,
  2012. sizeof( eap_tls_keying.randbytes ),
  2013. eap_tls_keymaterial,
  2014. sizeof( eap_tls_keymaterial ) ) )
  2015. != 0 )
  2016. {
  2017. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  2018. (unsigned int) -ret );
  2019. goto exit;
  2020. }
  2021. mbedtls_printf( " EAP-TLS key material is:" );
  2022. for( j = 0; j < sizeof( eap_tls_keymaterial ); j++ )
  2023. {
  2024. if( j % 8 == 0 )
  2025. mbedtls_printf("\n ");
  2026. mbedtls_printf("%02x ", eap_tls_keymaterial[j] );
  2027. }
  2028. mbedtls_printf("\n");
  2029. if( ( ret = mbedtls_ssl_tls_prf( eap_tls_keying.tls_prf_type, NULL, 0,
  2030. eap_tls_label,
  2031. eap_tls_keying.randbytes,
  2032. sizeof( eap_tls_keying.randbytes ),
  2033. eap_tls_iv,
  2034. sizeof( eap_tls_iv ) ) ) != 0 )
  2035. {
  2036. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  2037. (unsigned int) -ret );
  2038. goto exit;
  2039. }
  2040. mbedtls_printf( " EAP-TLS IV is:" );
  2041. for( j = 0; j < sizeof( eap_tls_iv ); j++ )
  2042. {
  2043. if( j % 8 == 0 )
  2044. mbedtls_printf("\n ");
  2045. mbedtls_printf("%02x ", eap_tls_iv[j] );
  2046. }
  2047. mbedtls_printf("\n");
  2048. }
  2049. #if defined( MBEDTLS_SSL_DTLS_SRTP )
  2050. else if( opt.use_srtp != 0 )
  2051. {
  2052. size_t j = 0;
  2053. mbedtls_dtls_srtp_info dtls_srtp_negotiation_result;
  2054. mbedtls_ssl_get_dtls_srtp_negotiation_result( &ssl, &dtls_srtp_negotiation_result );
  2055. if( dtls_srtp_negotiation_result.chosen_dtls_srtp_profile
  2056. == MBEDTLS_TLS_SRTP_UNSET )
  2057. {
  2058. mbedtls_printf( " Unable to negotiate "
  2059. "the use of DTLS-SRTP\n" );
  2060. }
  2061. else
  2062. {
  2063. if( ( ret = mbedtls_ssl_tls_prf( dtls_srtp_keying.tls_prf_type,
  2064. dtls_srtp_keying.master_secret,
  2065. sizeof( dtls_srtp_keying.master_secret ),
  2066. dtls_srtp_label,
  2067. dtls_srtp_keying.randbytes,
  2068. sizeof( dtls_srtp_keying.randbytes ),
  2069. dtls_srtp_key_material,
  2070. sizeof( dtls_srtp_key_material ) ) )
  2071. != 0 )
  2072. {
  2073. mbedtls_printf( " failed\n ! mbedtls_ssl_tls_prf returned -0x%x\n\n",
  2074. (unsigned int) -ret );
  2075. goto exit;
  2076. }
  2077. mbedtls_printf( " DTLS-SRTP key material is:" );
  2078. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  2079. {
  2080. if( j % 8 == 0 )
  2081. mbedtls_printf( "\n " );
  2082. mbedtls_printf( "%02x ", dtls_srtp_key_material[j] );
  2083. }
  2084. mbedtls_printf( "\n" );
  2085. /* produce a less readable output used to perform automatic checks
  2086. * - compare client and server output
  2087. * - interop test with openssl which client produces this kind of output
  2088. */
  2089. mbedtls_printf( " Keying material: " );
  2090. for( j = 0; j < sizeof( dtls_srtp_key_material ); j++ )
  2091. {
  2092. mbedtls_printf( "%02X", dtls_srtp_key_material[j] );
  2093. }
  2094. mbedtls_printf( "\n" );
  2095. if ( dtls_srtp_negotiation_result.mki_len > 0 )
  2096. {
  2097. mbedtls_printf( " DTLS-SRTP mki value: " );
  2098. for( j = 0; j < dtls_srtp_negotiation_result.mki_len; j++ )
  2099. {
  2100. mbedtls_printf( "%02X", dtls_srtp_negotiation_result.mki_value[j] );
  2101. }
  2102. }
  2103. else
  2104. {
  2105. mbedtls_printf( " DTLS-SRTP no mki value negotiated" );
  2106. }
  2107. mbedtls_printf( "\n" );
  2108. }
  2109. }
  2110. #endif /* MBEDTLS_SSL_DTLS_SRTP */
  2111. if( opt.reconnect != 0 )
  2112. {
  2113. mbedtls_printf(" . Saving session for reuse..." );
  2114. fflush( stdout );
  2115. if( opt.reco_mode == 1 )
  2116. {
  2117. mbedtls_ssl_session exported_session;
  2118. /* free any previously saved data */
  2119. if( session_data != NULL )
  2120. {
  2121. mbedtls_platform_zeroize( session_data, session_data_len );
  2122. mbedtls_free( session_data );
  2123. session_data = NULL;
  2124. }
  2125. mbedtls_ssl_session_init( &exported_session );
  2126. ret = mbedtls_ssl_get_session( &ssl, &exported_session );
  2127. if( ret != 0 )
  2128. {
  2129. mbedtls_printf(
  2130. "failed\n ! mbedtls_ssl_get_session() returned -%#02x\n",
  2131. (unsigned) -ret );
  2132. goto exit;
  2133. }
  2134. /* get size of the buffer needed */
  2135. mbedtls_ssl_session_save( &exported_session, NULL, 0, &session_data_len );
  2136. session_data = mbedtls_calloc( 1, session_data_len );
  2137. if( session_data == NULL )
  2138. {
  2139. mbedtls_printf( " failed\n ! alloc %u bytes for session data\n",
  2140. (unsigned) session_data_len );
  2141. mbedtls_ssl_session_free( &exported_session );
  2142. ret = MBEDTLS_ERR_SSL_ALLOC_FAILED;
  2143. goto exit;
  2144. }
  2145. /* actually save session data */
  2146. if( ( ret = mbedtls_ssl_session_save( &exported_session,
  2147. session_data, session_data_len,
  2148. &session_data_len ) ) != 0 )
  2149. {
  2150. mbedtls_printf( " failed\n ! mbedtls_ssl_session_saved returned -0x%04x\n\n",
  2151. (unsigned int) -ret );
  2152. mbedtls_ssl_session_free( &exported_session );
  2153. goto exit;
  2154. }
  2155. mbedtls_ssl_session_free( &exported_session );
  2156. }
  2157. else
  2158. {
  2159. if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
  2160. {
  2161. mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
  2162. (unsigned int) -ret );
  2163. goto exit;
  2164. }
  2165. }
  2166. mbedtls_printf( " ok\n" );
  2167. if( opt.reco_mode == 1 )
  2168. {
  2169. mbedtls_printf( " [ Saved %u bytes of session data]\n",
  2170. (unsigned) session_data_len );
  2171. }
  2172. }
  2173. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2174. /*
  2175. * 5. Verify the server certificate
  2176. */
  2177. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  2178. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  2179. {
  2180. char vrfy_buf[512];
  2181. mbedtls_printf( " failed\n" );
  2182. x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
  2183. " ! ", flags );
  2184. mbedtls_printf( "%s\n", vrfy_buf );
  2185. }
  2186. else
  2187. mbedtls_printf( " ok\n" );
  2188. #if !defined(MBEDTLS_X509_REMOVE_INFO)
  2189. mbedtls_printf( " . Peer certificate information ...\n" );
  2190. mbedtls_printf( "%s\n", peer_crt_info );
  2191. #endif /* !MBEDTLS_X509_REMOVE_INFO */
  2192. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2193. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2194. ret = report_cid_usage( &ssl, "initial handshake" );
  2195. if( ret != 0 )
  2196. goto exit;
  2197. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM )
  2198. {
  2199. if( ( ret = mbedtls_ssl_set_cid( &ssl, opt.cid_enabled_renego,
  2200. cid_renego,
  2201. cid_renego_len ) ) != 0 )
  2202. {
  2203. mbedtls_printf( " failed\n ! mbedtls_ssl_set_cid returned %d\n\n",
  2204. ret );
  2205. goto exit;
  2206. }
  2207. }
  2208. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2209. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  2210. if( opt.renegotiate )
  2211. {
  2212. /*
  2213. * Perform renegotiation (this must be done when the server is waiting
  2214. * for input from our side).
  2215. */
  2216. mbedtls_printf( " . Performing renegotiation..." );
  2217. fflush( stdout );
  2218. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  2219. {
  2220. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2221. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  2222. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2223. {
  2224. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
  2225. ret );
  2226. goto exit;
  2227. }
  2228. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2229. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2230. continue;
  2231. #endif
  2232. /* For event-driven IO, wait for socket to become available */
  2233. if( opt.event == 1 /* level triggered IO */ )
  2234. {
  2235. #if defined(MBEDTLS_TIMING_C)
  2236. idle( &server_fd, &timer, ret );
  2237. #else
  2238. idle( &server_fd, ret );
  2239. #endif
  2240. }
  2241. }
  2242. mbedtls_printf( " ok\n" );
  2243. }
  2244. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  2245. #if defined(MBEDTLS_SSL_DTLS_CONNECTION_ID)
  2246. ret = report_cid_usage( &ssl, "after renegotiation" );
  2247. if( ret != 0 )
  2248. goto exit;
  2249. #endif /* MBEDTLS_SSL_DTLS_CONNECTION_ID */
  2250. /*
  2251. * 6. Write the GET request
  2252. */
  2253. retry_left = opt.max_resend;
  2254. send_request:
  2255. mbedtls_printf( " > Write to server:" );
  2256. fflush( stdout );
  2257. len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
  2258. opt.request_page );
  2259. tail_len = (int) strlen( GET_REQUEST_END );
  2260. /* Add padding to GET request to reach opt.request_size in length */
  2261. if( opt.request_size != DFL_REQUEST_SIZE &&
  2262. len + tail_len < opt.request_size )
  2263. {
  2264. memset( buf + len, 'A', opt.request_size - len - tail_len );
  2265. len += opt.request_size - len - tail_len;
  2266. }
  2267. strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
  2268. len += tail_len;
  2269. /* Truncate if request size is smaller than the "natural" size */
  2270. if( opt.request_size != DFL_REQUEST_SIZE &&
  2271. len > opt.request_size )
  2272. {
  2273. len = opt.request_size;
  2274. /* Still end with \r\n unless that's really not possible */
  2275. if( len >= 2 ) buf[len - 2] = '\r';
  2276. if( len >= 1 ) buf[len - 1] = '\n';
  2277. }
  2278. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  2279. {
  2280. written = 0;
  2281. frags = 0;
  2282. do
  2283. {
  2284. while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
  2285. len - written ) ) < 0 )
  2286. {
  2287. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2288. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  2289. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2290. {
  2291. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
  2292. (unsigned int) -ret );
  2293. goto exit;
  2294. }
  2295. /* For event-driven IO, wait for socket to become available */
  2296. if( opt.event == 1 /* level triggered IO */ )
  2297. {
  2298. #if defined(MBEDTLS_TIMING_C)
  2299. idle( &server_fd, &timer, ret );
  2300. #else
  2301. idle( &server_fd, ret );
  2302. #endif
  2303. }
  2304. }
  2305. frags++;
  2306. written += ret;
  2307. }
  2308. while( written < len );
  2309. }
  2310. else /* Not stream, so datagram */
  2311. {
  2312. while( 1 )
  2313. {
  2314. ret = mbedtls_ssl_write( &ssl, buf, len );
  2315. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2316. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2317. continue;
  2318. #endif
  2319. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2320. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  2321. break;
  2322. /* For event-driven IO, wait for socket to become available */
  2323. if( opt.event == 1 /* level triggered IO */ )
  2324. {
  2325. #if defined(MBEDTLS_TIMING_C)
  2326. idle( &server_fd, &timer, ret );
  2327. #else
  2328. idle( &server_fd, ret );
  2329. #endif
  2330. }
  2331. }
  2332. if( ret < 0 )
  2333. {
  2334. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
  2335. ret );
  2336. goto exit;
  2337. }
  2338. frags = 1;
  2339. written = ret;
  2340. if( written < len )
  2341. {
  2342. mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
  2343. "was truncated to size %u", (unsigned) written );
  2344. }
  2345. }
  2346. buf[written] = '\0';
  2347. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
  2348. written, frags, (char *) buf );
  2349. /* Send a non-empty request if request_size == 0 */
  2350. if ( len == 0 )
  2351. {
  2352. opt.request_size = DFL_REQUEST_SIZE;
  2353. goto send_request;
  2354. }
  2355. /*
  2356. * 7. Read the HTTP response
  2357. */
  2358. mbedtls_printf( " < Read from server:" );
  2359. fflush( stdout );
  2360. /*
  2361. * TLS and DTLS need different reading styles (stream vs datagram)
  2362. */
  2363. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  2364. {
  2365. do
  2366. {
  2367. len = sizeof( buf ) - 1;
  2368. memset( buf, 0, sizeof( buf ) );
  2369. ret = mbedtls_ssl_read( &ssl, buf, len );
  2370. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2371. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2372. continue;
  2373. #endif
  2374. if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  2375. ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  2376. {
  2377. /* For event-driven IO, wait for socket to become available */
  2378. if( opt.event == 1 /* level triggered IO */ )
  2379. {
  2380. #if defined(MBEDTLS_TIMING_C)
  2381. idle( &server_fd, &timer, ret );
  2382. #else
  2383. idle( &server_fd, ret );
  2384. #endif
  2385. }
  2386. continue;
  2387. }
  2388. if( ret <= 0 )
  2389. {
  2390. switch( ret )
  2391. {
  2392. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  2393. mbedtls_printf( " connection was closed gracefully\n" );
  2394. ret = 0;
  2395. goto close_notify;
  2396. case 0:
  2397. case MBEDTLS_ERR_NET_CONN_RESET:
  2398. mbedtls_printf( " connection was reset by peer\n" );
  2399. ret = 0;
  2400. goto reconnect;
  2401. default:
  2402. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
  2403. (unsigned int) -ret );
  2404. goto exit;
  2405. }
  2406. }
  2407. len = ret;
  2408. buf[len] = '\0';
  2409. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  2410. /* End of message should be detected according to the syntax of the
  2411. * application protocol (eg HTTP), just use a dummy test here. */
  2412. if( ret > 0 && buf[len-1] == '\n' )
  2413. {
  2414. ret = 0;
  2415. break;
  2416. }
  2417. }
  2418. while( 1 );
  2419. }
  2420. else /* Not stream, so datagram */
  2421. {
  2422. len = sizeof( buf ) - 1;
  2423. memset( buf, 0, sizeof( buf ) );
  2424. while( 1 )
  2425. {
  2426. ret = mbedtls_ssl_read( &ssl, buf, len );
  2427. #if defined(MBEDTLS_ECP_RESTARTABLE)
  2428. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2429. continue;
  2430. #endif
  2431. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2432. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  2433. break;
  2434. /* For event-driven IO, wait for socket to become available */
  2435. if( opt.event == 1 /* level triggered IO */ )
  2436. {
  2437. #if defined(MBEDTLS_TIMING_C)
  2438. idle( &server_fd, &timer, ret );
  2439. #else
  2440. idle( &server_fd, ret );
  2441. #endif
  2442. }
  2443. }
  2444. if( ret <= 0 )
  2445. {
  2446. switch( ret )
  2447. {
  2448. case MBEDTLS_ERR_SSL_TIMEOUT:
  2449. mbedtls_printf( " timeout\n" );
  2450. if( retry_left-- > 0 )
  2451. goto send_request;
  2452. goto exit;
  2453. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  2454. mbedtls_printf( " connection was closed gracefully\n" );
  2455. ret = 0;
  2456. goto close_notify;
  2457. default:
  2458. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", (unsigned int) -ret );
  2459. goto exit;
  2460. }
  2461. }
  2462. len = ret;
  2463. buf[len] = '\0';
  2464. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  2465. ret = 0;
  2466. }
  2467. /*
  2468. * 7b. Simulate hard reset and reconnect from same port?
  2469. */
  2470. if( opt.reconnect_hard != 0 )
  2471. {
  2472. opt.reconnect_hard = 0;
  2473. mbedtls_printf( " . Restarting connection from same port..." );
  2474. fflush( stdout );
  2475. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2476. memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
  2477. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2478. if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
  2479. {
  2480. mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
  2481. (unsigned int) -ret );
  2482. goto exit;
  2483. }
  2484. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  2485. {
  2486. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2487. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  2488. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2489. {
  2490. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  2491. (unsigned int) -ret );
  2492. goto exit;
  2493. }
  2494. /* For event-driven IO, wait for socket to become available */
  2495. if( opt.event == 1 /* level triggered IO */ )
  2496. {
  2497. #if defined(MBEDTLS_TIMING_C)
  2498. idle( &server_fd, &timer, ret );
  2499. #else
  2500. idle( &server_fd, ret );
  2501. #endif
  2502. }
  2503. }
  2504. mbedtls_printf( " ok\n" );
  2505. goto send_request;
  2506. }
  2507. /*
  2508. * 7c. Simulate serialize/deserialize and go back to data exchange
  2509. */
  2510. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  2511. if( opt.serialize != 0 )
  2512. {
  2513. size_t buf_len;
  2514. mbedtls_printf( " . Serializing live connection..." );
  2515. ret = mbedtls_ssl_context_save( &ssl, NULL, 0, &buf_len );
  2516. if( ret != MBEDTLS_ERR_SSL_BUFFER_TOO_SMALL )
  2517. {
  2518. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  2519. "-0x%x\n\n", (unsigned int) -ret );
  2520. goto exit;
  2521. }
  2522. if( ( context_buf = mbedtls_calloc( 1, buf_len ) ) == NULL )
  2523. {
  2524. mbedtls_printf( " failed\n ! Couldn't allocate buffer for "
  2525. "serialized context" );
  2526. goto exit;
  2527. }
  2528. context_buf_len = buf_len;
  2529. if( ( ret = mbedtls_ssl_context_save( &ssl, context_buf,
  2530. buf_len, &buf_len ) ) != 0 )
  2531. {
  2532. mbedtls_printf( " failed\n ! mbedtls_ssl_context_save returned "
  2533. "-0x%x\n\n", (unsigned int) -ret );
  2534. goto exit;
  2535. }
  2536. mbedtls_printf( " ok\n" );
  2537. /* Save serialized context to the 'opt.context_file' as a base64 code */
  2538. if( 0 < strlen( opt.context_file ) )
  2539. {
  2540. FILE *b64_file;
  2541. uint8_t *b64_buf;
  2542. size_t b64_len;
  2543. mbedtls_printf( " . Save serialized context to a file... " );
  2544. mbedtls_base64_encode( NULL, 0, &b64_len, context_buf, buf_len );
  2545. if( ( b64_buf = mbedtls_calloc( 1, b64_len ) ) == NULL )
  2546. {
  2547. mbedtls_printf( "failed\n ! Couldn't allocate buffer for "
  2548. "the base64 code\n" );
  2549. goto exit;
  2550. }
  2551. if( ( ret = mbedtls_base64_encode( b64_buf, b64_len, &b64_len,
  2552. context_buf, buf_len ) ) != 0 )
  2553. {
  2554. mbedtls_printf( "failed\n ! mbedtls_base64_encode returned "
  2555. "-0x%x\n", (unsigned int) -ret );
  2556. mbedtls_free( b64_buf );
  2557. goto exit;
  2558. }
  2559. if( ( b64_file = fopen( opt.context_file, "w" ) ) == NULL )
  2560. {
  2561. mbedtls_printf( "failed\n ! Cannot open '%s' for writing.\n",
  2562. opt.context_file );
  2563. mbedtls_free( b64_buf );
  2564. goto exit;
  2565. }
  2566. if( b64_len != fwrite( b64_buf, 1, b64_len, b64_file ) )
  2567. {
  2568. mbedtls_printf( "failed\n ! fwrite(%ld bytes) failed\n",
  2569. (long) b64_len );
  2570. mbedtls_free( b64_buf );
  2571. fclose( b64_file );
  2572. goto exit;
  2573. }
  2574. mbedtls_free( b64_buf );
  2575. fclose( b64_file );
  2576. mbedtls_printf( "ok\n" );
  2577. }
  2578. if( opt.serialize == 1 )
  2579. {
  2580. /* nothing to do here, done by context_save() already */
  2581. mbedtls_printf( " . Context has been reset... ok\n" );
  2582. }
  2583. if( opt.serialize == 2 )
  2584. {
  2585. mbedtls_printf( " . Freeing and reinitializing context..." );
  2586. mbedtls_ssl_free( &ssl );
  2587. mbedtls_ssl_init( &ssl );
  2588. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  2589. {
  2590. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned "
  2591. "-0x%x\n\n", (unsigned int) -ret );
  2592. goto exit;
  2593. }
  2594. if( opt.nbio == 2 )
  2595. mbedtls_ssl_set_bio( &ssl, &server_fd, delayed_send,
  2596. delayed_recv, NULL );
  2597. else
  2598. mbedtls_ssl_set_bio( &ssl, &server_fd, mbedtls_net_send,
  2599. mbedtls_net_recv,
  2600. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  2601. #if defined(MBEDTLS_TIMING_C)
  2602. mbedtls_ssl_set_timer_cb( &ssl, &timer,
  2603. mbedtls_timing_set_delay,
  2604. mbedtls_timing_get_delay );
  2605. #endif /* MBEDTLS_TIMING_C */
  2606. mbedtls_printf( " ok\n" );
  2607. }
  2608. mbedtls_printf( " . Deserializing connection..." );
  2609. if( ( ret = mbedtls_ssl_context_load( &ssl, context_buf,
  2610. buf_len ) ) != 0 )
  2611. {
  2612. mbedtls_printf( "failed\n ! mbedtls_ssl_context_load returned "
  2613. "-0x%x\n\n", (unsigned int) -ret );
  2614. goto exit;
  2615. }
  2616. mbedtls_free( context_buf );
  2617. context_buf = NULL;
  2618. context_buf_len = 0;
  2619. mbedtls_printf( " ok\n" );
  2620. }
  2621. #endif /* MBEDTLS_SSL_CONTEXT_SERIALIZATION */
  2622. /*
  2623. * 7d. Continue doing data exchanges?
  2624. */
  2625. if( --opt.exchanges > 0 )
  2626. goto send_request;
  2627. /*
  2628. * 8. Done, cleanly close the connection
  2629. */
  2630. close_notify:
  2631. mbedtls_printf( " . Closing the connection..." );
  2632. fflush( stdout );
  2633. /*
  2634. * Most of the time sending a close_notify before closing is the right
  2635. * thing to do. However, when the server already knows how many messages
  2636. * are expected and closes the connection by itself, this alert becomes
  2637. * redundant. Sometimes with DTLS this redundancy becomes a problem by
  2638. * leading to a race condition where the server might close the connection
  2639. * before seeing the alert, and since UDP is connection-less when the
  2640. * alert arrives it will be seen as a new connection, which will fail as
  2641. * the alert is clearly not a valid ClientHello. This may cause spurious
  2642. * failures in tests that use DTLS and resumption with ssl_server2 in
  2643. * ssl-opt.sh, avoided by enabling skip_close_notify client-side.
  2644. */
  2645. if( opt.skip_close_notify == 0 )
  2646. {
  2647. /* No error checking, the connection might be closed already */
  2648. do ret = mbedtls_ssl_close_notify( &ssl );
  2649. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  2650. ret = 0;
  2651. }
  2652. mbedtls_printf( " done\n" );
  2653. /*
  2654. * 9. Reconnect?
  2655. */
  2656. reconnect:
  2657. if( opt.reconnect != 0 )
  2658. {
  2659. --opt.reconnect;
  2660. mbedtls_net_free( &server_fd );
  2661. #if defined(MBEDTLS_TIMING_C)
  2662. if( opt.reco_delay > 0 )
  2663. mbedtls_net_usleep( 1000000 * opt.reco_delay );
  2664. #endif
  2665. mbedtls_printf( " . Reconnecting with saved session..." );
  2666. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2667. memset( peer_crt_info, 0, sizeof( peer_crt_info ) );
  2668. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2669. if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
  2670. {
  2671. mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
  2672. (unsigned int) -ret );
  2673. goto exit;
  2674. }
  2675. if( opt.reco_mode == 1 )
  2676. {
  2677. if( ( ret = mbedtls_ssl_session_load( &saved_session,
  2678. session_data,
  2679. session_data_len ) ) != 0 )
  2680. {
  2681. mbedtls_printf( " failed\n ! mbedtls_ssl_session_load returned -0x%x\n\n",
  2682. (unsigned int) -ret );
  2683. goto exit;
  2684. }
  2685. }
  2686. if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
  2687. {
  2688. mbedtls_printf( " failed\n ! mbedtls_ssl_set_session returned -0x%x\n\n",
  2689. (unsigned int) -ret );
  2690. goto exit;
  2691. }
  2692. if( ( ret = mbedtls_net_connect( &server_fd,
  2693. opt.server_addr, opt.server_port,
  2694. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  2695. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  2696. {
  2697. mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
  2698. (unsigned int) -ret );
  2699. goto exit;
  2700. }
  2701. if( opt.nbio > 0 )
  2702. ret = mbedtls_net_set_nonblock( &server_fd );
  2703. else
  2704. ret = mbedtls_net_set_block( &server_fd );
  2705. if( ret != 0 )
  2706. {
  2707. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
  2708. (unsigned int) -ret );
  2709. goto exit;
  2710. }
  2711. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  2712. {
  2713. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  2714. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  2715. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  2716. {
  2717. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  2718. (unsigned int) -ret );
  2719. goto exit;
  2720. }
  2721. }
  2722. mbedtls_printf( " ok\n" );
  2723. goto send_request;
  2724. }
  2725. /*
  2726. * Cleanup and exit
  2727. */
  2728. exit:
  2729. #ifdef MBEDTLS_ERROR_C
  2730. if( ret != 0 )
  2731. {
  2732. char error_buf[100];
  2733. mbedtls_strerror( ret, error_buf, 100 );
  2734. mbedtls_printf("Last error was: -0x%X - %s\n\n", (unsigned int) -ret, error_buf );
  2735. }
  2736. #endif
  2737. mbedtls_net_free( &server_fd );
  2738. mbedtls_ssl_free( &ssl );
  2739. mbedtls_ssl_config_free( &conf );
  2740. mbedtls_ssl_session_free( &saved_session );
  2741. if( session_data != NULL )
  2742. mbedtls_platform_zeroize( session_data, session_data_len );
  2743. mbedtls_free( session_data );
  2744. #if defined(MBEDTLS_SSL_CONTEXT_SERIALIZATION)
  2745. if( context_buf != NULL )
  2746. mbedtls_platform_zeroize( context_buf, context_buf_len );
  2747. mbedtls_free( context_buf );
  2748. #endif
  2749. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  2750. mbedtls_x509_crt_free( &clicert );
  2751. mbedtls_x509_crt_free( &cacert );
  2752. mbedtls_pk_free( &pkey );
  2753. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2754. psa_destroy_key( key_slot );
  2755. #endif
  2756. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  2757. #if defined(MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED) && \
  2758. defined(MBEDTLS_USE_PSA_CRYPTO)
  2759. if( opt.psk_opaque != 0 )
  2760. {
  2761. /* This is ok even if the slot hasn't been
  2762. * initialized (we might have jumed here
  2763. * immediately because of bad cmd line params,
  2764. * for example). */
  2765. status = psa_destroy_key( slot );
  2766. if( ( status != PSA_SUCCESS ) &&
  2767. ( opt.query_config_mode == DFL_QUERY_CONFIG_MODE ) )
  2768. {
  2769. mbedtls_printf( "Failed to destroy key slot %u - error was %d",
  2770. (unsigned) slot, (int) status );
  2771. if( ret == 0 )
  2772. ret = MBEDTLS_ERR_SSL_HW_ACCEL_FAILED;
  2773. }
  2774. }
  2775. #endif /* MBEDTLS_KEY_EXCHANGE_SOME_PSK_ENABLED &&
  2776. MBEDTLS_USE_PSA_CRYPTO */
  2777. #if defined(MBEDTLS_USE_PSA_CRYPTO)
  2778. const char* message = mbedtls_test_helper_is_psa_leaking();
  2779. if( message )
  2780. {
  2781. if( ret == 0 )
  2782. ret = 1;
  2783. mbedtls_printf( "PSA memory leak detected: %s\n", message);
  2784. }
  2785. #endif /* MBEDTLS_USE_PSA_CRYPTO */
  2786. /* For builds with MBEDTLS_TEST_USE_PSA_CRYPTO_RNG psa crypto
  2787. * resources are freed by rng_free(). */
  2788. #if defined(MBEDTLS_USE_PSA_CRYPTO) && \
  2789. !defined(MBEDTLS_TEST_USE_PSA_CRYPTO_RNG)
  2790. mbedtls_psa_crypto_free( );
  2791. #endif
  2792. rng_free( &rng );
  2793. #if defined(MBEDTLS_TEST_HOOKS)
  2794. if( test_hooks_failure_detected( ) )
  2795. {
  2796. if( ret == 0 )
  2797. ret = 1;
  2798. mbedtls_printf( "Test hooks detected errors.\n" );
  2799. }
  2800. test_hooks_free( );
  2801. #endif /* MBEDTLS_TEST_HOOKS */
  2802. #if defined(MBEDTLS_MEMORY_BUFFER_ALLOC_C)
  2803. #if defined(MBEDTLS_MEMORY_DEBUG)
  2804. mbedtls_memory_buffer_alloc_status();
  2805. #endif
  2806. mbedtls_memory_buffer_alloc_free();
  2807. #endif /* MBEDTLS_MEMORY_BUFFER_ALLOC_C */
  2808. #if defined(_WIN32)
  2809. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  2810. {
  2811. mbedtls_printf( " + Press Enter to exit this program.\n" );
  2812. fflush( stdout ); getchar();
  2813. }
  2814. #endif
  2815. // Shell can not handle large exit numbers -> 1 for errors
  2816. if( ret < 0 )
  2817. ret = 1;
  2818. if( opt.query_config_mode == DFL_QUERY_CONFIG_MODE )
  2819. mbedtls_exit( ret );
  2820. else
  2821. mbedtls_exit( query_config_ret );
  2822. }
  2823. #endif /* !MBEDTLS_SSL_TEST_IMPOSSIBLE && MBEDTLS_SSL_CLI_C */