ssl_client2.c 108 KB

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