ssl_client2.c 120 KB

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