ssl_client2.c 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150
  1. /*
  2. * SSL client with certificate authentication
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  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. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. #if !defined(MBEDTLS_CONFIG_FILE)
  22. #include "mbedtls/config.h"
  23. #else
  24. #include MBEDTLS_CONFIG_FILE
  25. #endif
  26. #if defined(MBEDTLS_PLATFORM_C)
  27. #include "mbedtls/platform.h"
  28. #else
  29. #include <stdio.h>
  30. #include <stdlib.h>
  31. #define mbedtls_time time
  32. #define mbedtls_time_t time_t
  33. #define mbedtls_printf printf
  34. #define mbedtls_fprintf fprintf
  35. #define mbedtls_snprintf snprintf
  36. #define mbedtls_exit exit
  37. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  38. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  39. #endif
  40. #if !defined(MBEDTLS_ENTROPY_C) || \
  41. !defined(MBEDTLS_SSL_TLS_C) || !defined(MBEDTLS_SSL_CLI_C) || \
  42. !defined(MBEDTLS_NET_C) || !defined(MBEDTLS_CTR_DRBG_C)
  43. int main( void )
  44. {
  45. mbedtls_printf("MBEDTLS_ENTROPY_C and/or "
  46. "MBEDTLS_SSL_TLS_C and/or MBEDTLS_SSL_CLI_C and/or "
  47. "MBEDTLS_NET_C and/or MBEDTLS_CTR_DRBG_C and/or not defined.\n");
  48. return( 0 );
  49. }
  50. #else
  51. #include "mbedtls/net_sockets.h"
  52. #include "mbedtls/ssl.h"
  53. #include "mbedtls/entropy.h"
  54. #include "mbedtls/ctr_drbg.h"
  55. #include "mbedtls/certs.h"
  56. #include "mbedtls/x509.h"
  57. #include "mbedtls/error.h"
  58. #include "mbedtls/debug.h"
  59. #include "mbedtls/timing.h"
  60. #include <stdio.h>
  61. #include <stdlib.h>
  62. #include <string.h>
  63. #define MAX_REQUEST_SIZE 20000
  64. #define MAX_REQUEST_SIZE_STR "20000"
  65. #define DFL_SERVER_NAME "localhost"
  66. #define DFL_SERVER_ADDR NULL
  67. #define DFL_SERVER_PORT "4433"
  68. #define DFL_REQUEST_PAGE "/"
  69. #define DFL_REQUEST_SIZE -1
  70. #define DFL_DEBUG_LEVEL 0
  71. #define DFL_NBIO 0
  72. #define DFL_EVENT 0
  73. #define DFL_READ_TIMEOUT 0
  74. #define DFL_MAX_RESEND 0
  75. #define DFL_CA_FILE ""
  76. #define DFL_CA_PATH ""
  77. #define DFL_CRT_FILE ""
  78. #define DFL_KEY_FILE ""
  79. #define DFL_PSK ""
  80. #define DFL_PSK_IDENTITY "Client_identity"
  81. #define DFL_ECJPAKE_PW NULL
  82. #define DFL_EC_MAX_OPS -1
  83. #define DFL_FORCE_CIPHER 0
  84. #define DFL_RENEGOTIATION MBEDTLS_SSL_RENEGOTIATION_DISABLED
  85. #define DFL_ALLOW_LEGACY -2
  86. #define DFL_RENEGOTIATE 0
  87. #define DFL_EXCHANGES 1
  88. #define DFL_MIN_VERSION -1
  89. #define DFL_MAX_VERSION -1
  90. #define DFL_ARC4 -1
  91. #define DFL_SHA1 -1
  92. #define DFL_AUTH_MODE -1
  93. #define DFL_MFL_CODE MBEDTLS_SSL_MAX_FRAG_LEN_NONE
  94. #define DFL_TRUNC_HMAC -1
  95. #define DFL_RECSPLIT -1
  96. #define DFL_DHMLEN -1
  97. #define DFL_RECONNECT 0
  98. #define DFL_RECO_DELAY 0
  99. #define DFL_RECONNECT_HARD 0
  100. #define DFL_TICKETS MBEDTLS_SSL_SESSION_TICKETS_ENABLED
  101. #define DFL_ALPN_STRING NULL
  102. #define DFL_CURVES NULL
  103. #define DFL_TRANSPORT MBEDTLS_SSL_TRANSPORT_STREAM
  104. #define DFL_HS_TO_MIN 0
  105. #define DFL_HS_TO_MAX 0
  106. #define DFL_DTLS_MTU -1
  107. #define DFL_DGRAM_PACKING 1
  108. #define DFL_FALLBACK -1
  109. #define DFL_EXTENDED_MS -1
  110. #define DFL_ETM -1
  111. #define GET_REQUEST "GET %s HTTP/1.0\r\nExtra-header: "
  112. #define GET_REQUEST_END "\r\n\r\n"
  113. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  114. #if defined(MBEDTLS_FS_IO)
  115. #define USAGE_IO \
  116. " ca_file=%%s The single file containing the top-level CA(s) you fully trust\n" \
  117. " default: \"\" (pre-loaded)\n" \
  118. " use \"none\" to skip loading any top-level CAs.\n" \
  119. " ca_path=%%s The path containing the top-level CA(s) you fully trust\n" \
  120. " default: \"\" (pre-loaded) (overrides ca_file)\n" \
  121. " use \"none\" to skip loading any top-level CAs.\n" \
  122. " crt_file=%%s Your own cert and chain (in bottom to top order, top may be omitted)\n" \
  123. " default: \"\" (pre-loaded)\n" \
  124. " key_file=%%s default: \"\" (pre-loaded)\n"
  125. #else
  126. #define USAGE_IO \
  127. " No file operations available (MBEDTLS_FS_IO not defined)\n"
  128. #endif /* MBEDTLS_FS_IO */
  129. #else
  130. #define USAGE_IO ""
  131. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  132. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  133. #define USAGE_PSK \
  134. " psk=%%s default: \"\" (in hex, without 0x)\n" \
  135. " psk_identity=%%s default: \"Client_identity\"\n"
  136. #else
  137. #define USAGE_PSK ""
  138. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  139. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  140. #define USAGE_TICKETS \
  141. " tickets=%%d default: 1 (enabled)\n"
  142. #else
  143. #define USAGE_TICKETS ""
  144. #endif /* MBEDTLS_SSL_SESSION_TICKETS */
  145. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  146. #define USAGE_TRUNC_HMAC \
  147. " trunc_hmac=%%d default: library default\n"
  148. #else
  149. #define USAGE_TRUNC_HMAC ""
  150. #endif /* MBEDTLS_SSL_TRUNCATED_HMAC */
  151. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  152. #define USAGE_MAX_FRAG_LEN \
  153. " max_frag_len=%%d default: 16384 (tls default)\n" \
  154. " options: 512, 1024, 2048, 4096\n"
  155. #else
  156. #define USAGE_MAX_FRAG_LEN ""
  157. #endif /* MBEDTLS_SSL_MAX_FRAGMENT_LENGTH */
  158. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  159. #define USAGE_RECSPLIT \
  160. " recsplit=0/1 default: (library default: on)\n"
  161. #else
  162. #define USAGE_RECSPLIT
  163. #endif
  164. #if defined(MBEDTLS_DHM_C)
  165. #define USAGE_DHMLEN \
  166. " dhmlen=%%d default: (library default: 1024 bits)\n"
  167. #else
  168. #define USAGE_DHMLEN
  169. #endif
  170. #if defined(MBEDTLS_SSL_ALPN)
  171. #define USAGE_ALPN \
  172. " alpn=%%s default: \"\" (disabled)\n" \
  173. " example: spdy/1,http/1.1\n"
  174. #else
  175. #define USAGE_ALPN ""
  176. #endif /* MBEDTLS_SSL_ALPN */
  177. #if defined(MBEDTLS_ECP_C)
  178. #define USAGE_CURVES \
  179. " curves=a,b,c,d default: \"default\" (library default)\n" \
  180. " example: \"secp521r1,brainpoolP512r1\"\n" \
  181. " - use \"none\" for empty list\n" \
  182. " - see mbedtls_ecp_curve_list()\n" \
  183. " for acceptable curve names\n"
  184. #else
  185. #define USAGE_CURVES ""
  186. #endif
  187. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  188. #define USAGE_DTLS \
  189. " dtls=%%d default: 0 (TLS)\n" \
  190. " hs_timeout=%%d-%%d default: (library default: 1000-60000)\n" \
  191. " range of DTLS handshake timeouts in millisecs\n" \
  192. " mtu=%%d default: (library default: unlimited)\n" \
  193. " dgram_packing=%%d default: 1 (allowed)\n" \
  194. " allow or forbid packing of multiple\n" \
  195. " records within a single datgram.\n"
  196. #else
  197. #define USAGE_DTLS ""
  198. #endif
  199. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  200. #define USAGE_FALLBACK \
  201. " fallback=0/1 default: (library default: off)\n"
  202. #else
  203. #define USAGE_FALLBACK ""
  204. #endif
  205. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  206. #define USAGE_EMS \
  207. " extended_ms=0/1 default: (library default: on)\n"
  208. #else
  209. #define USAGE_EMS ""
  210. #endif
  211. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  212. #define USAGE_ETM \
  213. " etm=0/1 default: (library default: on)\n"
  214. #else
  215. #define USAGE_ETM ""
  216. #endif
  217. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  218. #define USAGE_RENEGO \
  219. " renegotiation=%%d default: 0 (disabled)\n" \
  220. " renegotiate=%%d default: 0 (disabled)\n"
  221. #else
  222. #define USAGE_RENEGO ""
  223. #endif
  224. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  225. #define USAGE_ECJPAKE \
  226. " ecjpake_pw=%%s default: none (disabled)\n"
  227. #else
  228. #define USAGE_ECJPAKE ""
  229. #endif
  230. #if defined(MBEDTLS_ECP_RESTARTABLE)
  231. #define USAGE_ECRESTART \
  232. " ec_max_ops=%%s default: library default (restart disabled)\n"
  233. #else
  234. #define USAGE_ECRESTART ""
  235. #endif
  236. #define USAGE \
  237. "\n usage: ssl_client2 param=<>...\n" \
  238. "\n acceptable parameters:\n" \
  239. " server_name=%%s default: localhost\n" \
  240. " server_addr=%%s default: given by name\n" \
  241. " server_port=%%d default: 4433\n" \
  242. " request_page=%%s default: \".\"\n" \
  243. " request_size=%%d default: about 34 (basic request)\n" \
  244. " (minimum: 0, max: " MAX_REQUEST_SIZE_STR ")\n" \
  245. " If 0, in the first exchange only an empty\n" \
  246. " application data message is sent followed by\n" \
  247. " a second non-empty message before attempting\n" \
  248. " to read a response from the server\n" \
  249. " debug_level=%%d default: 0 (disabled)\n" \
  250. " nbio=%%d default: 0 (blocking I/O)\n" \
  251. " options: 1 (non-blocking), 2 (added delays)\n" \
  252. " event=%%d default: 0 (loop)\n" \
  253. " options: 1 (level-triggered, implies nbio=1),\n" \
  254. " read_timeout=%%d default: 0 ms (no timeout)\n" \
  255. " max_resend=%%d default: 0 (no resend on timeout)\n" \
  256. "\n" \
  257. USAGE_DTLS \
  258. "\n" \
  259. " auth_mode=%%s default: (library default: none)\n" \
  260. " options: none, optional, required\n" \
  261. USAGE_IO \
  262. "\n" \
  263. USAGE_PSK \
  264. USAGE_ECJPAKE \
  265. USAGE_ECRESTART \
  266. "\n" \
  267. " allow_legacy=%%d default: (library default: no)\n" \
  268. USAGE_RENEGO \
  269. " exchanges=%%d default: 1\n" \
  270. " reconnect=%%d default: 0 (disabled)\n" \
  271. " reco_delay=%%d default: 0 seconds\n" \
  272. " reconnect_hard=%%d default: 0 (disabled)\n" \
  273. USAGE_TICKETS \
  274. USAGE_MAX_FRAG_LEN \
  275. USAGE_TRUNC_HMAC \
  276. USAGE_ALPN \
  277. USAGE_FALLBACK \
  278. USAGE_EMS \
  279. USAGE_ETM \
  280. USAGE_CURVES \
  281. USAGE_RECSPLIT \
  282. USAGE_DHMLEN \
  283. "\n" \
  284. " arc4=%%d default: (library default: 0)\n" \
  285. " allow_sha1=%%d default: 0\n" \
  286. " min_version=%%s default: (library default: tls1)\n" \
  287. " max_version=%%s default: (library default: tls1_2)\n" \
  288. " force_version=%%s default: \"\" (none)\n" \
  289. " options: ssl3, tls1, tls1_1, tls1_2, dtls1, dtls1_2\n" \
  290. "\n" \
  291. " force_ciphersuite=<name> default: all enabled\n"\
  292. " query_config=<name> return 0 if the specified\n" \
  293. " configuration macro is defined and 1\n" \
  294. " otherwise. The expansion of the macro\n" \
  295. " is printed if it is defined\n" \
  296. " acceptable ciphersuite names:\n"
  297. #define ALPN_LIST_SIZE 10
  298. #define CURVE_LIST_SIZE 20
  299. /*
  300. * global options
  301. */
  302. struct options
  303. {
  304. const char *server_name; /* hostname of the server (client only) */
  305. const char *server_addr; /* address of the server (client only) */
  306. const char *server_port; /* port on which the ssl service runs */
  307. int debug_level; /* level of debugging */
  308. int nbio; /* should I/O be blocking? */
  309. int event; /* loop or event-driven IO? level or edge triggered? */
  310. uint32_t read_timeout; /* timeout on mbedtls_ssl_read() in milliseconds */
  311. int max_resend; /* DTLS times to resend on read timeout */
  312. const char *request_page; /* page on server to request */
  313. int request_size; /* pad request with header to requested size */
  314. const char *ca_file; /* the file with the CA certificate(s) */
  315. const char *ca_path; /* the path with the CA certificate(s) reside */
  316. const char *crt_file; /* the file with the client certificate */
  317. const char *key_file; /* the file with the client key */
  318. const char *psk; /* the pre-shared key */
  319. const char *psk_identity; /* the pre-shared key identity */
  320. const char *ecjpake_pw; /* the EC J-PAKE password */
  321. int ec_max_ops; /* EC consecutive operations limit */
  322. int force_ciphersuite[2]; /* protocol/ciphersuite to use, or all */
  323. int renegotiation; /* enable / disable renegotiation */
  324. int allow_legacy; /* allow legacy renegotiation */
  325. int renegotiate; /* attempt renegotiation? */
  326. int renego_delay; /* delay before enforcing renegotiation */
  327. int exchanges; /* number of data exchanges */
  328. int min_version; /* minimum protocol version accepted */
  329. int max_version; /* maximum protocol version accepted */
  330. int arc4; /* flag for arc4 suites support */
  331. int allow_sha1; /* flag for SHA-1 support */
  332. int auth_mode; /* verify mode for connection */
  333. unsigned char mfl_code; /* code for maximum fragment length */
  334. int trunc_hmac; /* negotiate truncated hmac or not */
  335. int recsplit; /* enable record splitting? */
  336. int dhmlen; /* minimum DHM params len in bits */
  337. int reconnect; /* attempt to resume session */
  338. int reco_delay; /* delay in seconds before resuming session */
  339. int reconnect_hard; /* unexpectedly reconnect from the same port */
  340. int tickets; /* enable / disable session tickets */
  341. const char *curves; /* list of supported elliptic curves */
  342. const char *alpn_string; /* ALPN supported protocols */
  343. int transport; /* TLS or DTLS? */
  344. uint32_t hs_to_min; /* Initial value of DTLS handshake timer */
  345. uint32_t hs_to_max; /* Max value of DTLS handshake timer */
  346. int dtls_mtu; /* UDP Maximum tranport unit for DTLS */
  347. int fallback; /* is this a fallback connection? */
  348. int dgram_packing; /* allow/forbid datagram packing */
  349. int extended_ms; /* negotiate extended master secret? */
  350. int etm; /* negotiate encrypt then mac? */
  351. } opt;
  352. int query_config( const char *config );
  353. static void my_debug( void *ctx, int level,
  354. const char *file, int line,
  355. const char *str )
  356. {
  357. const char *p, *basename;
  358. /* Extract basename from file */
  359. for( p = basename = file; *p != '\0'; p++ )
  360. if( *p == '/' || *p == '\\' )
  361. basename = p + 1;
  362. mbedtls_fprintf( (FILE *) ctx, "%s:%04d: |%d| %s",
  363. basename, line, level, str );
  364. fflush( (FILE *) ctx );
  365. }
  366. /*
  367. * Test recv/send functions that make sure each try returns
  368. * WANT_READ/WANT_WRITE at least once before sucesseding
  369. */
  370. static int my_recv( void *ctx, unsigned char *buf, size_t len )
  371. {
  372. static int first_try = 1;
  373. int ret;
  374. if( first_try )
  375. {
  376. first_try = 0;
  377. return( MBEDTLS_ERR_SSL_WANT_READ );
  378. }
  379. ret = mbedtls_net_recv( ctx, buf, len );
  380. if( ret != MBEDTLS_ERR_SSL_WANT_READ )
  381. first_try = 1; /* Next call will be a new operation */
  382. return( ret );
  383. }
  384. static int my_send( void *ctx, const unsigned char *buf, size_t len )
  385. {
  386. static int first_try = 1;
  387. int ret;
  388. if( first_try )
  389. {
  390. first_try = 0;
  391. return( MBEDTLS_ERR_SSL_WANT_WRITE );
  392. }
  393. ret = mbedtls_net_send( ctx, buf, len );
  394. if( ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  395. first_try = 1; /* Next call will be a new operation */
  396. return( ret );
  397. }
  398. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  399. /*
  400. * Enabled if debug_level > 1 in code below
  401. */
  402. static int my_verify( void *data, mbedtls_x509_crt *crt,
  403. int depth, uint32_t *flags )
  404. {
  405. char buf[1024];
  406. ((void) data);
  407. mbedtls_printf( "\nVerify requested for (Depth %d):\n", depth );
  408. mbedtls_x509_crt_info( buf, sizeof( buf ) - 1, "", crt );
  409. mbedtls_printf( "%s", buf );
  410. if ( ( *flags ) == 0 )
  411. mbedtls_printf( " This certificate has no flags\n" );
  412. else
  413. {
  414. mbedtls_x509_crt_verify_info( buf, sizeof( buf ), " ! ", *flags );
  415. mbedtls_printf( "%s\n", buf );
  416. }
  417. return( 0 );
  418. }
  419. static int ssl_sig_hashes_for_test[] = {
  420. #if defined(MBEDTLS_SHA512_C)
  421. MBEDTLS_MD_SHA512,
  422. MBEDTLS_MD_SHA384,
  423. #endif
  424. #if defined(MBEDTLS_SHA256_C)
  425. MBEDTLS_MD_SHA256,
  426. MBEDTLS_MD_SHA224,
  427. #endif
  428. #if defined(MBEDTLS_SHA1_C)
  429. /* Allow SHA-1 as we use it extensively in tests. */
  430. MBEDTLS_MD_SHA1,
  431. #endif
  432. MBEDTLS_MD_NONE
  433. };
  434. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  435. /*
  436. * Wait for an event from the underlying transport or the timer
  437. * (Used in event-driven IO mode).
  438. */
  439. #if !defined(MBEDTLS_TIMING_C)
  440. int idle( mbedtls_net_context *fd,
  441. int idle_reason )
  442. #else
  443. int idle( mbedtls_net_context *fd,
  444. mbedtls_timing_delay_context *timer,
  445. int idle_reason )
  446. #endif
  447. {
  448. int ret;
  449. int poll_type = 0;
  450. if( idle_reason == MBEDTLS_ERR_SSL_WANT_WRITE )
  451. poll_type = MBEDTLS_NET_POLL_WRITE;
  452. else if( idle_reason == MBEDTLS_ERR_SSL_WANT_READ )
  453. poll_type = MBEDTLS_NET_POLL_READ;
  454. #if !defined(MBEDTLS_TIMING_C)
  455. else
  456. return( 0 );
  457. #endif
  458. while( 1 )
  459. {
  460. /* Check if timer has expired */
  461. #if defined(MBEDTLS_TIMING_C)
  462. if( timer != NULL &&
  463. mbedtls_timing_get_delay( timer ) == 2 )
  464. {
  465. break;
  466. }
  467. #endif /* MBEDTLS_TIMING_C */
  468. /* Check if underlying transport became available */
  469. if( poll_type != 0 )
  470. {
  471. ret = mbedtls_net_poll( fd, poll_type, 0 );
  472. if( ret < 0 )
  473. return( ret );
  474. if( ret == poll_type )
  475. break;
  476. }
  477. }
  478. return( 0 );
  479. }
  480. int main( int argc, char *argv[] )
  481. {
  482. int ret = 0, len, tail_len, i, written, frags, retry_left;
  483. mbedtls_net_context server_fd;
  484. unsigned char buf[MAX_REQUEST_SIZE + 1];
  485. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  486. unsigned char psk[MBEDTLS_PSK_MAX_LEN];
  487. size_t psk_len = 0;
  488. #endif
  489. #if defined(MBEDTLS_SSL_ALPN)
  490. const char *alpn_list[ALPN_LIST_SIZE];
  491. #endif
  492. #if defined(MBEDTLS_ECP_C)
  493. mbedtls_ecp_group_id curve_list[CURVE_LIST_SIZE];
  494. const mbedtls_ecp_curve_info *curve_cur;
  495. #endif
  496. const char *pers = "ssl_client2";
  497. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  498. mbedtls_x509_crt_profile crt_profile_for_test = mbedtls_x509_crt_profile_default;
  499. #endif
  500. mbedtls_entropy_context entropy;
  501. mbedtls_ctr_drbg_context ctr_drbg;
  502. mbedtls_ssl_context ssl;
  503. mbedtls_ssl_config conf;
  504. mbedtls_ssl_session saved_session;
  505. #if defined(MBEDTLS_TIMING_C)
  506. mbedtls_timing_delay_context timer;
  507. #endif
  508. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  509. uint32_t flags;
  510. mbedtls_x509_crt cacert;
  511. mbedtls_x509_crt clicert;
  512. mbedtls_pk_context pkey;
  513. #endif
  514. char *p, *q;
  515. const int *list;
  516. /*
  517. * Make sure memory references are valid.
  518. */
  519. mbedtls_net_init( &server_fd );
  520. mbedtls_ssl_init( &ssl );
  521. mbedtls_ssl_config_init( &conf );
  522. memset( &saved_session, 0, sizeof( mbedtls_ssl_session ) );
  523. mbedtls_ctr_drbg_init( &ctr_drbg );
  524. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  525. mbedtls_x509_crt_init( &cacert );
  526. mbedtls_x509_crt_init( &clicert );
  527. mbedtls_pk_init( &pkey );
  528. #endif
  529. #if defined(MBEDTLS_SSL_ALPN)
  530. memset( (void * ) alpn_list, 0, sizeof( alpn_list ) );
  531. #endif
  532. if( argc == 0 )
  533. {
  534. usage:
  535. if( ret == 0 )
  536. ret = 1;
  537. mbedtls_printf( USAGE );
  538. list = mbedtls_ssl_list_ciphersuites();
  539. while( *list )
  540. {
  541. mbedtls_printf(" %-42s", mbedtls_ssl_get_ciphersuite_name( *list ) );
  542. list++;
  543. if( !*list )
  544. break;
  545. mbedtls_printf(" %s\n", mbedtls_ssl_get_ciphersuite_name( *list ) );
  546. list++;
  547. }
  548. mbedtls_printf("\n");
  549. goto exit;
  550. }
  551. opt.server_name = DFL_SERVER_NAME;
  552. opt.server_addr = DFL_SERVER_ADDR;
  553. opt.server_port = DFL_SERVER_PORT;
  554. opt.debug_level = DFL_DEBUG_LEVEL;
  555. opt.nbio = DFL_NBIO;
  556. opt.event = DFL_EVENT;
  557. opt.read_timeout = DFL_READ_TIMEOUT;
  558. opt.max_resend = DFL_MAX_RESEND;
  559. opt.request_page = DFL_REQUEST_PAGE;
  560. opt.request_size = DFL_REQUEST_SIZE;
  561. opt.ca_file = DFL_CA_FILE;
  562. opt.ca_path = DFL_CA_PATH;
  563. opt.crt_file = DFL_CRT_FILE;
  564. opt.key_file = DFL_KEY_FILE;
  565. opt.psk = DFL_PSK;
  566. opt.psk_identity = DFL_PSK_IDENTITY;
  567. opt.ecjpake_pw = DFL_ECJPAKE_PW;
  568. opt.ec_max_ops = DFL_EC_MAX_OPS;
  569. opt.force_ciphersuite[0]= DFL_FORCE_CIPHER;
  570. opt.renegotiation = DFL_RENEGOTIATION;
  571. opt.allow_legacy = DFL_ALLOW_LEGACY;
  572. opt.renegotiate = DFL_RENEGOTIATE;
  573. opt.exchanges = DFL_EXCHANGES;
  574. opt.min_version = DFL_MIN_VERSION;
  575. opt.max_version = DFL_MAX_VERSION;
  576. opt.arc4 = DFL_ARC4;
  577. opt.allow_sha1 = DFL_SHA1;
  578. opt.auth_mode = DFL_AUTH_MODE;
  579. opt.mfl_code = DFL_MFL_CODE;
  580. opt.trunc_hmac = DFL_TRUNC_HMAC;
  581. opt.recsplit = DFL_RECSPLIT;
  582. opt.dhmlen = DFL_DHMLEN;
  583. opt.reconnect = DFL_RECONNECT;
  584. opt.reco_delay = DFL_RECO_DELAY;
  585. opt.reconnect_hard = DFL_RECONNECT_HARD;
  586. opt.tickets = DFL_TICKETS;
  587. opt.alpn_string = DFL_ALPN_STRING;
  588. opt.curves = DFL_CURVES;
  589. opt.transport = DFL_TRANSPORT;
  590. opt.hs_to_min = DFL_HS_TO_MIN;
  591. opt.hs_to_max = DFL_HS_TO_MAX;
  592. opt.dtls_mtu = DFL_DTLS_MTU;
  593. opt.fallback = DFL_FALLBACK;
  594. opt.extended_ms = DFL_EXTENDED_MS;
  595. opt.etm = DFL_ETM;
  596. opt.dgram_packing = DFL_DGRAM_PACKING;
  597. for( i = 1; i < argc; i++ )
  598. {
  599. p = argv[i];
  600. if( ( q = strchr( p, '=' ) ) == NULL )
  601. goto usage;
  602. *q++ = '\0';
  603. if( strcmp( p, "server_name" ) == 0 )
  604. opt.server_name = q;
  605. else if( strcmp( p, "server_addr" ) == 0 )
  606. opt.server_addr = q;
  607. else if( strcmp( p, "server_port" ) == 0 )
  608. opt.server_port = q;
  609. else if( strcmp( p, "dtls" ) == 0 )
  610. {
  611. int t = atoi( q );
  612. if( t == 0 )
  613. opt.transport = MBEDTLS_SSL_TRANSPORT_STREAM;
  614. else if( t == 1 )
  615. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  616. else
  617. goto usage;
  618. }
  619. else if( strcmp( p, "debug_level" ) == 0 )
  620. {
  621. opt.debug_level = atoi( q );
  622. if( opt.debug_level < 0 || opt.debug_level > 65535 )
  623. goto usage;
  624. }
  625. else if( strcmp( p, "nbio" ) == 0 )
  626. {
  627. opt.nbio = atoi( q );
  628. if( opt.nbio < 0 || opt.nbio > 2 )
  629. goto usage;
  630. }
  631. else if( strcmp( p, "event" ) == 0 )
  632. {
  633. opt.event = atoi( q );
  634. if( opt.event < 0 || opt.event > 2 )
  635. goto usage;
  636. }
  637. else if( strcmp( p, "read_timeout" ) == 0 )
  638. opt.read_timeout = atoi( q );
  639. else if( strcmp( p, "max_resend" ) == 0 )
  640. {
  641. opt.max_resend = atoi( q );
  642. if( opt.max_resend < 0 )
  643. goto usage;
  644. }
  645. else if( strcmp( p, "request_page" ) == 0 )
  646. opt.request_page = q;
  647. else if( strcmp( p, "request_size" ) == 0 )
  648. {
  649. opt.request_size = atoi( q );
  650. if( opt.request_size < 0 ||
  651. opt.request_size > MAX_REQUEST_SIZE )
  652. goto usage;
  653. }
  654. else if( strcmp( p, "ca_file" ) == 0 )
  655. opt.ca_file = q;
  656. else if( strcmp( p, "ca_path" ) == 0 )
  657. opt.ca_path = q;
  658. else if( strcmp( p, "crt_file" ) == 0 )
  659. opt.crt_file = q;
  660. else if( strcmp( p, "key_file" ) == 0 )
  661. opt.key_file = q;
  662. else if( strcmp( p, "psk" ) == 0 )
  663. opt.psk = q;
  664. else if( strcmp( p, "psk_identity" ) == 0 )
  665. opt.psk_identity = q;
  666. else if( strcmp( p, "ecjpake_pw" ) == 0 )
  667. opt.ecjpake_pw = q;
  668. else if( strcmp( p, "ec_max_ops" ) == 0 )
  669. opt.ec_max_ops = atoi( q );
  670. else if( strcmp( p, "force_ciphersuite" ) == 0 )
  671. {
  672. opt.force_ciphersuite[0] = mbedtls_ssl_get_ciphersuite_id( q );
  673. if( opt.force_ciphersuite[0] == 0 )
  674. {
  675. ret = 2;
  676. goto usage;
  677. }
  678. opt.force_ciphersuite[1] = 0;
  679. }
  680. else if( strcmp( p, "renegotiation" ) == 0 )
  681. {
  682. opt.renegotiation = (atoi( q )) ?
  683. MBEDTLS_SSL_RENEGOTIATION_ENABLED :
  684. MBEDTLS_SSL_RENEGOTIATION_DISABLED;
  685. }
  686. else if( strcmp( p, "allow_legacy" ) == 0 )
  687. {
  688. switch( atoi( q ) )
  689. {
  690. case -1:
  691. opt.allow_legacy = MBEDTLS_SSL_LEGACY_BREAK_HANDSHAKE;
  692. break;
  693. case 0:
  694. opt.allow_legacy = MBEDTLS_SSL_LEGACY_NO_RENEGOTIATION;
  695. break;
  696. case 1:
  697. opt.allow_legacy = MBEDTLS_SSL_LEGACY_ALLOW_RENEGOTIATION;
  698. break;
  699. default: goto usage;
  700. }
  701. }
  702. else if( strcmp( p, "renegotiate" ) == 0 )
  703. {
  704. opt.renegotiate = atoi( q );
  705. if( opt.renegotiate < 0 || opt.renegotiate > 1 )
  706. goto usage;
  707. }
  708. else if( strcmp( p, "exchanges" ) == 0 )
  709. {
  710. opt.exchanges = atoi( q );
  711. if( opt.exchanges < 1 )
  712. goto usage;
  713. }
  714. else if( strcmp( p, "reconnect" ) == 0 )
  715. {
  716. opt.reconnect = atoi( q );
  717. if( opt.reconnect < 0 || opt.reconnect > 2 )
  718. goto usage;
  719. }
  720. else if( strcmp( p, "reco_delay" ) == 0 )
  721. {
  722. opt.reco_delay = atoi( q );
  723. if( opt.reco_delay < 0 )
  724. goto usage;
  725. }
  726. else if( strcmp( p, "reconnect_hard" ) == 0 )
  727. {
  728. opt.reconnect_hard = atoi( q );
  729. if( opt.reconnect_hard < 0 || opt.reconnect_hard > 1 )
  730. goto usage;
  731. }
  732. else if( strcmp( p, "tickets" ) == 0 )
  733. {
  734. opt.tickets = atoi( q );
  735. if( opt.tickets < 0 || opt.tickets > 2 )
  736. goto usage;
  737. }
  738. else if( strcmp( p, "alpn" ) == 0 )
  739. {
  740. opt.alpn_string = q;
  741. }
  742. else if( strcmp( p, "fallback" ) == 0 )
  743. {
  744. switch( atoi( q ) )
  745. {
  746. case 0: opt.fallback = MBEDTLS_SSL_IS_NOT_FALLBACK; break;
  747. case 1: opt.fallback = MBEDTLS_SSL_IS_FALLBACK; break;
  748. default: goto usage;
  749. }
  750. }
  751. else if( strcmp( p, "extended_ms" ) == 0 )
  752. {
  753. switch( atoi( q ) )
  754. {
  755. case 0:
  756. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_DISABLED;
  757. break;
  758. case 1:
  759. opt.extended_ms = MBEDTLS_SSL_EXTENDED_MS_ENABLED;
  760. break;
  761. default: goto usage;
  762. }
  763. }
  764. else if( strcmp( p, "curves" ) == 0 )
  765. opt.curves = q;
  766. else if( strcmp( p, "etm" ) == 0 )
  767. {
  768. switch( atoi( q ) )
  769. {
  770. case 0: opt.etm = MBEDTLS_SSL_ETM_DISABLED; break;
  771. case 1: opt.etm = MBEDTLS_SSL_ETM_ENABLED; break;
  772. default: goto usage;
  773. }
  774. }
  775. else if( strcmp( p, "min_version" ) == 0 )
  776. {
  777. if( strcmp( q, "ssl3" ) == 0 )
  778. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  779. else if( strcmp( q, "tls1" ) == 0 )
  780. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  781. else if( strcmp( q, "tls1_1" ) == 0 ||
  782. strcmp( q, "dtls1" ) == 0 )
  783. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  784. else if( strcmp( q, "tls1_2" ) == 0 ||
  785. strcmp( q, "dtls1_2" ) == 0 )
  786. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  787. else
  788. goto usage;
  789. }
  790. else if( strcmp( p, "max_version" ) == 0 )
  791. {
  792. if( strcmp( q, "ssl3" ) == 0 )
  793. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  794. else if( strcmp( q, "tls1" ) == 0 )
  795. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  796. else if( strcmp( q, "tls1_1" ) == 0 ||
  797. strcmp( q, "dtls1" ) == 0 )
  798. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  799. else if( strcmp( q, "tls1_2" ) == 0 ||
  800. strcmp( q, "dtls1_2" ) == 0 )
  801. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  802. else
  803. goto usage;
  804. }
  805. else if( strcmp( p, "arc4" ) == 0 )
  806. {
  807. switch( atoi( q ) )
  808. {
  809. case 0: opt.arc4 = MBEDTLS_SSL_ARC4_DISABLED; break;
  810. case 1: opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED; break;
  811. default: goto usage;
  812. }
  813. }
  814. else if( strcmp( p, "allow_sha1" ) == 0 )
  815. {
  816. switch( atoi( q ) )
  817. {
  818. case 0: opt.allow_sha1 = 0; break;
  819. case 1: opt.allow_sha1 = 1; break;
  820. default: goto usage;
  821. }
  822. }
  823. else if( strcmp( p, "force_version" ) == 0 )
  824. {
  825. if( strcmp( q, "ssl3" ) == 0 )
  826. {
  827. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_0;
  828. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_0;
  829. }
  830. else if( strcmp( q, "tls1" ) == 0 )
  831. {
  832. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_1;
  833. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_1;
  834. }
  835. else if( strcmp( q, "tls1_1" ) == 0 )
  836. {
  837. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  838. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  839. }
  840. else if( strcmp( q, "tls1_2" ) == 0 )
  841. {
  842. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  843. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  844. }
  845. else if( strcmp( q, "dtls1" ) == 0 )
  846. {
  847. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  848. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_2;
  849. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  850. }
  851. else if( strcmp( q, "dtls1_2" ) == 0 )
  852. {
  853. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_3;
  854. opt.max_version = MBEDTLS_SSL_MINOR_VERSION_3;
  855. opt.transport = MBEDTLS_SSL_TRANSPORT_DATAGRAM;
  856. }
  857. else
  858. goto usage;
  859. }
  860. else if( strcmp( p, "auth_mode" ) == 0 )
  861. {
  862. if( strcmp( q, "none" ) == 0 )
  863. opt.auth_mode = MBEDTLS_SSL_VERIFY_NONE;
  864. else if( strcmp( q, "optional" ) == 0 )
  865. opt.auth_mode = MBEDTLS_SSL_VERIFY_OPTIONAL;
  866. else if( strcmp( q, "required" ) == 0 )
  867. opt.auth_mode = MBEDTLS_SSL_VERIFY_REQUIRED;
  868. else
  869. goto usage;
  870. }
  871. else if( strcmp( p, "max_frag_len" ) == 0 )
  872. {
  873. if( strcmp( q, "512" ) == 0 )
  874. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_512;
  875. else if( strcmp( q, "1024" ) == 0 )
  876. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_1024;
  877. else if( strcmp( q, "2048" ) == 0 )
  878. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_2048;
  879. else if( strcmp( q, "4096" ) == 0 )
  880. opt.mfl_code = MBEDTLS_SSL_MAX_FRAG_LEN_4096;
  881. else
  882. goto usage;
  883. }
  884. else if( strcmp( p, "trunc_hmac" ) == 0 )
  885. {
  886. switch( atoi( q ) )
  887. {
  888. case 0: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_DISABLED; break;
  889. case 1: opt.trunc_hmac = MBEDTLS_SSL_TRUNC_HMAC_ENABLED; break;
  890. default: goto usage;
  891. }
  892. }
  893. else if( strcmp( p, "hs_timeout" ) == 0 )
  894. {
  895. if( ( p = strchr( q, '-' ) ) == NULL )
  896. goto usage;
  897. *p++ = '\0';
  898. opt.hs_to_min = atoi( q );
  899. opt.hs_to_max = atoi( p );
  900. if( opt.hs_to_min == 0 || opt.hs_to_max < opt.hs_to_min )
  901. goto usage;
  902. }
  903. else if( strcmp( p, "mtu" ) == 0 )
  904. {
  905. opt.dtls_mtu = atoi( q );
  906. if( opt.dtls_mtu < 0 )
  907. goto usage;
  908. }
  909. else if( strcmp( p, "dgram_packing" ) == 0 )
  910. {
  911. opt.dgram_packing = atoi( q );
  912. if( opt.dgram_packing != 0 &&
  913. opt.dgram_packing != 1 )
  914. {
  915. goto usage;
  916. }
  917. }
  918. else if( strcmp( p, "recsplit" ) == 0 )
  919. {
  920. opt.recsplit = atoi( q );
  921. if( opt.recsplit < 0 || opt.recsplit > 1 )
  922. goto usage;
  923. }
  924. else if( strcmp( p, "dhmlen" ) == 0 )
  925. {
  926. opt.dhmlen = atoi( q );
  927. if( opt.dhmlen < 0 )
  928. goto usage;
  929. }
  930. else if( strcmp( p, "query_config" ) == 0 )
  931. {
  932. return query_config( q );
  933. }
  934. else
  935. goto usage;
  936. }
  937. /* Event-driven IO is incompatible with the above custom
  938. * receive and send functions, as the polling builds on
  939. * refers to the underlying net_context. */
  940. if( opt.event == 1 && opt.nbio != 1 )
  941. {
  942. mbedtls_printf( "Warning: event-driven IO mandates nbio=1 - overwrite\n" );
  943. opt.nbio = 1;
  944. }
  945. #if defined(MBEDTLS_DEBUG_C)
  946. mbedtls_debug_set_threshold( opt.debug_level );
  947. #endif
  948. if( opt.force_ciphersuite[0] > 0 )
  949. {
  950. const mbedtls_ssl_ciphersuite_t *ciphersuite_info;
  951. ciphersuite_info =
  952. mbedtls_ssl_ciphersuite_from_id( opt.force_ciphersuite[0] );
  953. if( opt.max_version != -1 &&
  954. ciphersuite_info->min_minor_ver > opt.max_version )
  955. {
  956. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  957. ret = 2;
  958. goto usage;
  959. }
  960. if( opt.min_version != -1 &&
  961. ciphersuite_info->max_minor_ver < opt.min_version )
  962. {
  963. mbedtls_printf( "forced ciphersuite not allowed with this protocol version\n" );
  964. ret = 2;
  965. goto usage;
  966. }
  967. /* If the server selects a version that's not supported by
  968. * this suite, then there will be no common ciphersuite... */
  969. if( opt.max_version == -1 ||
  970. opt.max_version > ciphersuite_info->max_minor_ver )
  971. {
  972. opt.max_version = ciphersuite_info->max_minor_ver;
  973. }
  974. if( opt.min_version < ciphersuite_info->min_minor_ver )
  975. {
  976. opt.min_version = ciphersuite_info->min_minor_ver;
  977. /* DTLS starts with TLS 1.1 */
  978. if( opt.transport == MBEDTLS_SSL_TRANSPORT_DATAGRAM &&
  979. opt.min_version < MBEDTLS_SSL_MINOR_VERSION_2 )
  980. opt.min_version = MBEDTLS_SSL_MINOR_VERSION_2;
  981. }
  982. /* Enable RC4 if needed and not explicitly disabled */
  983. if( ciphersuite_info->cipher == MBEDTLS_CIPHER_ARC4_128 )
  984. {
  985. if( opt.arc4 == MBEDTLS_SSL_ARC4_DISABLED )
  986. {
  987. mbedtls_printf( "forced RC4 ciphersuite with RC4 disabled\n" );
  988. ret = 2;
  989. goto usage;
  990. }
  991. opt.arc4 = MBEDTLS_SSL_ARC4_ENABLED;
  992. }
  993. }
  994. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  995. /*
  996. * Unhexify the pre-shared key if any is given
  997. */
  998. if( strlen( opt.psk ) )
  999. {
  1000. unsigned char c;
  1001. size_t j;
  1002. if( strlen( opt.psk ) % 2 != 0 )
  1003. {
  1004. mbedtls_printf( "pre-shared key not valid hex\n" );
  1005. goto exit;
  1006. }
  1007. psk_len = strlen( opt.psk ) / 2;
  1008. for( j = 0; j < strlen( opt.psk ); j += 2 )
  1009. {
  1010. c = opt.psk[j];
  1011. if( c >= '0' && c <= '9' )
  1012. c -= '0';
  1013. else if( c >= 'a' && c <= 'f' )
  1014. c -= 'a' - 10;
  1015. else if( c >= 'A' && c <= 'F' )
  1016. c -= 'A' - 10;
  1017. else
  1018. {
  1019. mbedtls_printf( "pre-shared key not valid hex\n" );
  1020. goto exit;
  1021. }
  1022. psk[ j / 2 ] = c << 4;
  1023. c = opt.psk[j + 1];
  1024. if( c >= '0' && c <= '9' )
  1025. c -= '0';
  1026. else if( c >= 'a' && c <= 'f' )
  1027. c -= 'a' - 10;
  1028. else if( c >= 'A' && c <= 'F' )
  1029. c -= 'A' - 10;
  1030. else
  1031. {
  1032. mbedtls_printf( "pre-shared key not valid hex\n" );
  1033. goto exit;
  1034. }
  1035. psk[ j / 2 ] |= c;
  1036. }
  1037. }
  1038. #endif /* MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED */
  1039. #if defined(MBEDTLS_ECP_C)
  1040. if( opt.curves != NULL )
  1041. {
  1042. p = (char *) opt.curves;
  1043. i = 0;
  1044. if( strcmp( p, "none" ) == 0 )
  1045. {
  1046. curve_list[0] = MBEDTLS_ECP_DP_NONE;
  1047. }
  1048. else if( strcmp( p, "default" ) != 0 )
  1049. {
  1050. /* Leave room for a final NULL in curve list */
  1051. while( i < CURVE_LIST_SIZE - 1 && *p != '\0' )
  1052. {
  1053. q = p;
  1054. /* Terminate the current string */
  1055. while( *p != ',' && *p != '\0' )
  1056. p++;
  1057. if( *p == ',' )
  1058. *p++ = '\0';
  1059. if( ( curve_cur = mbedtls_ecp_curve_info_from_name( q ) ) != NULL )
  1060. {
  1061. curve_list[i++] = curve_cur->grp_id;
  1062. }
  1063. else
  1064. {
  1065. mbedtls_printf( "unknown curve %s\n", q );
  1066. mbedtls_printf( "supported curves: " );
  1067. for( curve_cur = mbedtls_ecp_curve_list();
  1068. curve_cur->grp_id != MBEDTLS_ECP_DP_NONE;
  1069. curve_cur++ )
  1070. {
  1071. mbedtls_printf( "%s ", curve_cur->name );
  1072. }
  1073. mbedtls_printf( "\n" );
  1074. goto exit;
  1075. }
  1076. }
  1077. mbedtls_printf("Number of curves: %d\n", i );
  1078. if( i == CURVE_LIST_SIZE - 1 && *p != '\0' )
  1079. {
  1080. mbedtls_printf( "curves list too long, maximum %d",
  1081. CURVE_LIST_SIZE - 1 );
  1082. goto exit;
  1083. }
  1084. curve_list[i] = MBEDTLS_ECP_DP_NONE;
  1085. }
  1086. }
  1087. #endif /* MBEDTLS_ECP_C */
  1088. #if defined(MBEDTLS_SSL_ALPN)
  1089. if( opt.alpn_string != NULL )
  1090. {
  1091. p = (char *) opt.alpn_string;
  1092. i = 0;
  1093. /* Leave room for a final NULL in alpn_list */
  1094. while( i < ALPN_LIST_SIZE - 1 && *p != '\0' )
  1095. {
  1096. alpn_list[i++] = p;
  1097. /* Terminate the current string and move on to next one */
  1098. while( *p != ',' && *p != '\0' )
  1099. p++;
  1100. if( *p == ',' )
  1101. *p++ = '\0';
  1102. }
  1103. }
  1104. #endif /* MBEDTLS_SSL_ALPN */
  1105. /*
  1106. * 0. Initialize the RNG and the session data
  1107. */
  1108. mbedtls_printf( "\n . Seeding the random number generator..." );
  1109. fflush( stdout );
  1110. mbedtls_entropy_init( &entropy );
  1111. if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func,
  1112. &entropy, (const unsigned char *) pers,
  1113. strlen( pers ) ) ) != 0 )
  1114. {
  1115. mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned -0x%x\n",
  1116. -ret );
  1117. goto exit;
  1118. }
  1119. mbedtls_printf( " ok\n" );
  1120. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1121. /*
  1122. * 1.1. Load the trusted CA
  1123. */
  1124. mbedtls_printf( " . Loading the CA root certificate ..." );
  1125. fflush( stdout );
  1126. if( strcmp( opt.ca_path, "none" ) == 0 ||
  1127. strcmp( opt.ca_file, "none" ) == 0 )
  1128. {
  1129. ret = 0;
  1130. }
  1131. else
  1132. #if defined(MBEDTLS_FS_IO)
  1133. if( strlen( opt.ca_path ) )
  1134. ret = mbedtls_x509_crt_parse_path( &cacert, opt.ca_path );
  1135. else if( strlen( opt.ca_file ) )
  1136. ret = mbedtls_x509_crt_parse_file( &cacert, opt.ca_file );
  1137. else
  1138. #endif
  1139. #if defined(MBEDTLS_CERTS_C)
  1140. {
  1141. #if defined(MBEDTLS_PEM_PARSE_C)
  1142. for( i = 0; mbedtls_test_cas[i] != NULL; i++ )
  1143. {
  1144. ret = mbedtls_x509_crt_parse( &cacert,
  1145. (const unsigned char *) mbedtls_test_cas[i],
  1146. mbedtls_test_cas_len[i] );
  1147. if( ret != 0 )
  1148. break;
  1149. }
  1150. if( ret == 0 )
  1151. #endif /* MBEDTLS_PEM_PARSE_C */
  1152. for( i = 0; mbedtls_test_cas_der[i] != NULL; i++ )
  1153. {
  1154. ret = mbedtls_x509_crt_parse_der( &cacert,
  1155. (const unsigned char *) mbedtls_test_cas_der[i],
  1156. mbedtls_test_cas_der_len[i] );
  1157. if( ret != 0 )
  1158. break;
  1159. }
  1160. }
  1161. #else
  1162. {
  1163. ret = 1;
  1164. mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
  1165. }
  1166. #endif /* MBEDTLS_CERTS_C */
  1167. if( ret < 0 )
  1168. {
  1169. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  1170. -ret );
  1171. goto exit;
  1172. }
  1173. mbedtls_printf( " ok (%d skipped)\n", ret );
  1174. /*
  1175. * 1.2. Load own certificate and private key
  1176. *
  1177. * (can be skipped if client authentication is not required)
  1178. */
  1179. mbedtls_printf( " . Loading the client cert. and key..." );
  1180. fflush( stdout );
  1181. if( strcmp( opt.crt_file, "none" ) == 0 )
  1182. ret = 0;
  1183. else
  1184. #if defined(MBEDTLS_FS_IO)
  1185. if( strlen( opt.crt_file ) )
  1186. ret = mbedtls_x509_crt_parse_file( &clicert, opt.crt_file );
  1187. else
  1188. #endif
  1189. #if defined(MBEDTLS_CERTS_C)
  1190. ret = mbedtls_x509_crt_parse( &clicert,
  1191. (const unsigned char *) mbedtls_test_cli_crt,
  1192. mbedtls_test_cli_crt_len );
  1193. #else
  1194. {
  1195. ret = 1;
  1196. mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
  1197. }
  1198. #endif
  1199. if( ret != 0 )
  1200. {
  1201. mbedtls_printf( " failed\n ! mbedtls_x509_crt_parse returned -0x%x\n\n",
  1202. -ret );
  1203. goto exit;
  1204. }
  1205. if( strcmp( opt.key_file, "none" ) == 0 )
  1206. ret = 0;
  1207. else
  1208. #if defined(MBEDTLS_FS_IO)
  1209. if( strlen( opt.key_file ) )
  1210. ret = mbedtls_pk_parse_keyfile( &pkey, opt.key_file, "" );
  1211. else
  1212. #endif
  1213. #if defined(MBEDTLS_CERTS_C)
  1214. ret = mbedtls_pk_parse_key( &pkey,
  1215. (const unsigned char *) mbedtls_test_cli_key,
  1216. mbedtls_test_cli_key_len, NULL, 0 );
  1217. #else
  1218. {
  1219. ret = 1;
  1220. mbedtls_printf( "MBEDTLS_CERTS_C not defined." );
  1221. }
  1222. #endif
  1223. if( ret != 0 )
  1224. {
  1225. mbedtls_printf( " failed\n ! mbedtls_pk_parse_key returned -0x%x\n\n",
  1226. -ret );
  1227. goto exit;
  1228. }
  1229. mbedtls_printf( " ok\n" );
  1230. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1231. /*
  1232. * 2. Start the connection
  1233. */
  1234. if( opt.server_addr == NULL)
  1235. opt.server_addr = opt.server_name;
  1236. mbedtls_printf( " . Connecting to %s/%s/%s...",
  1237. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ? "tcp" : "udp",
  1238. opt.server_addr, opt.server_port );
  1239. fflush( stdout );
  1240. if( ( ret = mbedtls_net_connect( &server_fd,
  1241. opt.server_addr, opt.server_port,
  1242. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  1243. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  1244. {
  1245. mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
  1246. -ret );
  1247. goto exit;
  1248. }
  1249. if( opt.nbio > 0 )
  1250. ret = mbedtls_net_set_nonblock( &server_fd );
  1251. else
  1252. ret = mbedtls_net_set_block( &server_fd );
  1253. if( ret != 0 )
  1254. {
  1255. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
  1256. -ret );
  1257. goto exit;
  1258. }
  1259. mbedtls_printf( " ok\n" );
  1260. /*
  1261. * 3. Setup stuff
  1262. */
  1263. mbedtls_printf( " . Setting up the SSL/TLS structure..." );
  1264. fflush( stdout );
  1265. if( ( ret = mbedtls_ssl_config_defaults( &conf,
  1266. MBEDTLS_SSL_IS_CLIENT,
  1267. opt.transport,
  1268. MBEDTLS_SSL_PRESET_DEFAULT ) ) != 0 )
  1269. {
  1270. mbedtls_printf( " failed\n ! mbedtls_ssl_config_defaults returned -0x%x\n\n",
  1271. -ret );
  1272. goto exit;
  1273. }
  1274. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1275. /* The default algorithms profile disables SHA-1, but our tests still
  1276. rely on it heavily. */
  1277. if( opt.allow_sha1 > 0 )
  1278. {
  1279. crt_profile_for_test.allowed_mds |= MBEDTLS_X509_ID_FLAG( MBEDTLS_MD_SHA1 );
  1280. mbedtls_ssl_conf_cert_profile( &conf, &crt_profile_for_test );
  1281. mbedtls_ssl_conf_sig_hashes( &conf, ssl_sig_hashes_for_test );
  1282. }
  1283. if( opt.debug_level > 0 )
  1284. mbedtls_ssl_conf_verify( &conf, my_verify, NULL );
  1285. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1286. if( opt.auth_mode != DFL_AUTH_MODE )
  1287. mbedtls_ssl_conf_authmode( &conf, opt.auth_mode );
  1288. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1289. if( opt.hs_to_min != DFL_HS_TO_MIN || opt.hs_to_max != DFL_HS_TO_MAX )
  1290. mbedtls_ssl_conf_handshake_timeout( &conf, opt.hs_to_min,
  1291. opt.hs_to_max );
  1292. if( opt.dgram_packing != DFL_DGRAM_PACKING )
  1293. mbedtls_ssl_set_datagram_packing( &ssl, opt.dgram_packing );
  1294. #endif /* MBEDTLS_SSL_PROTO_DTLS */
  1295. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1296. if( ( ret = mbedtls_ssl_conf_max_frag_len( &conf, opt.mfl_code ) ) != 0 )
  1297. {
  1298. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_max_frag_len returned %d\n\n",
  1299. ret );
  1300. goto exit;
  1301. }
  1302. #endif
  1303. #if defined(MBEDTLS_SSL_TRUNCATED_HMAC)
  1304. if( opt.trunc_hmac != DFL_TRUNC_HMAC )
  1305. mbedtls_ssl_conf_truncated_hmac( &conf, opt.trunc_hmac );
  1306. #endif
  1307. #if defined(MBEDTLS_SSL_EXTENDED_MASTER_SECRET)
  1308. if( opt.extended_ms != DFL_EXTENDED_MS )
  1309. mbedtls_ssl_conf_extended_master_secret( &conf, opt.extended_ms );
  1310. #endif
  1311. #if defined(MBEDTLS_SSL_ENCRYPT_THEN_MAC)
  1312. if( opt.etm != DFL_ETM )
  1313. mbedtls_ssl_conf_encrypt_then_mac( &conf, opt.etm );
  1314. #endif
  1315. #if defined(MBEDTLS_SSL_CBC_RECORD_SPLITTING)
  1316. if( opt.recsplit != DFL_RECSPLIT )
  1317. mbedtls_ssl_conf_cbc_record_splitting( &conf, opt.recsplit
  1318. ? MBEDTLS_SSL_CBC_RECORD_SPLITTING_ENABLED
  1319. : MBEDTLS_SSL_CBC_RECORD_SPLITTING_DISABLED );
  1320. #endif
  1321. #if defined(MBEDTLS_DHM_C)
  1322. if( opt.dhmlen != DFL_DHMLEN )
  1323. mbedtls_ssl_conf_dhm_min_bitlen( &conf, opt.dhmlen );
  1324. #endif
  1325. #if defined(MBEDTLS_SSL_ALPN)
  1326. if( opt.alpn_string != NULL )
  1327. if( ( ret = mbedtls_ssl_conf_alpn_protocols( &conf, alpn_list ) ) != 0 )
  1328. {
  1329. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_alpn_protocols returned %d\n\n",
  1330. ret );
  1331. goto exit;
  1332. }
  1333. #endif
  1334. mbedtls_ssl_conf_rng( &conf, mbedtls_ctr_drbg_random, &ctr_drbg );
  1335. mbedtls_ssl_conf_dbg( &conf, my_debug, stdout );
  1336. mbedtls_ssl_conf_read_timeout( &conf, opt.read_timeout );
  1337. #if defined(MBEDTLS_SSL_SESSION_TICKETS)
  1338. mbedtls_ssl_conf_session_tickets( &conf, opt.tickets );
  1339. #endif
  1340. if( opt.force_ciphersuite[0] != DFL_FORCE_CIPHER )
  1341. mbedtls_ssl_conf_ciphersuites( &conf, opt.force_ciphersuite );
  1342. #if defined(MBEDTLS_ARC4_C)
  1343. if( opt.arc4 != DFL_ARC4 )
  1344. mbedtls_ssl_conf_arc4_support( &conf, opt.arc4 );
  1345. #endif
  1346. if( opt.allow_legacy != DFL_ALLOW_LEGACY )
  1347. mbedtls_ssl_conf_legacy_renegotiation( &conf, opt.allow_legacy );
  1348. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1349. mbedtls_ssl_conf_renegotiation( &conf, opt.renegotiation );
  1350. #endif
  1351. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1352. if( strcmp( opt.ca_path, "none" ) != 0 &&
  1353. strcmp( opt.ca_file, "none" ) != 0 )
  1354. {
  1355. mbedtls_ssl_conf_ca_chain( &conf, &cacert, NULL );
  1356. }
  1357. if( strcmp( opt.crt_file, "none" ) != 0 &&
  1358. strcmp( opt.key_file, "none" ) != 0 )
  1359. {
  1360. if( ( ret = mbedtls_ssl_conf_own_cert( &conf, &clicert, &pkey ) ) != 0 )
  1361. {
  1362. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_own_cert returned %d\n\n",
  1363. ret );
  1364. goto exit;
  1365. }
  1366. }
  1367. #endif
  1368. #if defined(MBEDTLS_ECP_C)
  1369. if( opt.curves != NULL &&
  1370. strcmp( opt.curves, "default" ) != 0 )
  1371. {
  1372. mbedtls_ssl_conf_curves( &conf, curve_list );
  1373. }
  1374. #endif
  1375. #if defined(MBEDTLS_KEY_EXCHANGE__SOME__PSK_ENABLED)
  1376. if( ( ret = mbedtls_ssl_conf_psk( &conf, psk, psk_len,
  1377. (const unsigned char *) opt.psk_identity,
  1378. strlen( opt.psk_identity ) ) ) != 0 )
  1379. {
  1380. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_psk returned %d\n\n",
  1381. ret );
  1382. goto exit;
  1383. }
  1384. #endif
  1385. if( opt.min_version != DFL_MIN_VERSION )
  1386. mbedtls_ssl_conf_min_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1387. opt.min_version );
  1388. if( opt.max_version != DFL_MAX_VERSION )
  1389. mbedtls_ssl_conf_max_version( &conf, MBEDTLS_SSL_MAJOR_VERSION_3,
  1390. opt.max_version );
  1391. #if defined(MBEDTLS_SSL_FALLBACK_SCSV)
  1392. if( opt.fallback != DFL_FALLBACK )
  1393. mbedtls_ssl_conf_fallback( &conf, opt.fallback );
  1394. #endif
  1395. if( ( ret = mbedtls_ssl_setup( &ssl, &conf ) ) != 0 )
  1396. {
  1397. mbedtls_printf( " failed\n ! mbedtls_ssl_setup returned -0x%x\n\n",
  1398. -ret );
  1399. goto exit;
  1400. }
  1401. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1402. if( ( ret = mbedtls_ssl_set_hostname( &ssl, opt.server_name ) ) != 0 )
  1403. {
  1404. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hostname returned %d\n\n",
  1405. ret );
  1406. goto exit;
  1407. }
  1408. #endif
  1409. #if defined(MBEDTLS_KEY_EXCHANGE_ECJPAKE_ENABLED)
  1410. if( opt.ecjpake_pw != DFL_ECJPAKE_PW )
  1411. {
  1412. if( ( ret = mbedtls_ssl_set_hs_ecjpake_password( &ssl,
  1413. (const unsigned char *) opt.ecjpake_pw,
  1414. strlen( opt.ecjpake_pw ) ) ) != 0 )
  1415. {
  1416. mbedtls_printf( " failed\n ! mbedtls_ssl_set_hs_ecjpake_password returned %d\n\n",
  1417. ret );
  1418. goto exit;
  1419. }
  1420. }
  1421. #endif
  1422. if( opt.nbio == 2 )
  1423. mbedtls_ssl_set_bio( &ssl, &server_fd, my_send, my_recv, NULL );
  1424. else
  1425. mbedtls_ssl_set_bio( &ssl, &server_fd,
  1426. mbedtls_net_send, mbedtls_net_recv,
  1427. opt.nbio == 0 ? mbedtls_net_recv_timeout : NULL );
  1428. #if defined(MBEDTLS_SSL_PROTO_DTLS)
  1429. if( opt.dtls_mtu != DFL_DTLS_MTU )
  1430. mbedtls_ssl_set_mtu( &ssl, opt.dtls_mtu );
  1431. #endif
  1432. #if defined(MBEDTLS_TIMING_C)
  1433. mbedtls_ssl_set_timer_cb( &ssl, &timer, mbedtls_timing_set_delay,
  1434. mbedtls_timing_get_delay );
  1435. #endif
  1436. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1437. if( opt.ec_max_ops != DFL_EC_MAX_OPS )
  1438. mbedtls_ecp_set_max_ops( opt.ec_max_ops );
  1439. #endif
  1440. mbedtls_printf( " ok\n" );
  1441. /*
  1442. * 4. Handshake
  1443. */
  1444. mbedtls_printf( " . Performing the SSL/TLS handshake..." );
  1445. fflush( stdout );
  1446. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  1447. {
  1448. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1449. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1450. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1451. {
  1452. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n",
  1453. -ret );
  1454. if( ret == MBEDTLS_ERR_X509_CERT_VERIFY_FAILED )
  1455. mbedtls_printf(
  1456. " Unable to verify the server's certificate. "
  1457. "Either it is invalid,\n"
  1458. " or you didn't set ca_file or ca_path "
  1459. "to an appropriate value.\n"
  1460. " Alternatively, you may want to use "
  1461. "auth_mode=optional for testing purposes.\n" );
  1462. mbedtls_printf( "\n" );
  1463. goto exit;
  1464. }
  1465. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1466. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1467. continue;
  1468. #endif
  1469. /* For event-driven IO, wait for socket to become available */
  1470. if( opt.event == 1 /* level triggered IO */ )
  1471. {
  1472. #if defined(MBEDTLS_TIMING_C)
  1473. ret = idle( &server_fd, &timer, ret );
  1474. #else
  1475. ret = idle( &server_fd, ret );
  1476. #endif
  1477. if( ret != 0 )
  1478. goto exit;
  1479. }
  1480. }
  1481. mbedtls_printf( " ok\n [ Protocol is %s ]\n [ Ciphersuite is %s ]\n",
  1482. mbedtls_ssl_get_version( &ssl ),
  1483. mbedtls_ssl_get_ciphersuite( &ssl ) );
  1484. if( ( ret = mbedtls_ssl_get_record_expansion( &ssl ) ) >= 0 )
  1485. mbedtls_printf( " [ Record expansion is %d ]\n", ret );
  1486. else
  1487. mbedtls_printf( " [ Record expansion is unknown (compression) ]\n" );
  1488. #if defined(MBEDTLS_SSL_MAX_FRAGMENT_LENGTH)
  1489. mbedtls_printf( " [ Maximum fragment length is %u ]\n",
  1490. (unsigned int) mbedtls_ssl_get_max_frag_len( &ssl ) );
  1491. #endif
  1492. #if defined(MBEDTLS_SSL_ALPN)
  1493. if( opt.alpn_string != NULL )
  1494. {
  1495. const char *alp = mbedtls_ssl_get_alpn_protocol( &ssl );
  1496. mbedtls_printf( " [ Application Layer Protocol is %s ]\n",
  1497. alp ? alp : "(none)" );
  1498. }
  1499. #endif
  1500. if( opt.reconnect != 0 )
  1501. {
  1502. mbedtls_printf(" . Saving session for reuse..." );
  1503. fflush( stdout );
  1504. if( ( ret = mbedtls_ssl_get_session( &ssl, &saved_session ) ) != 0 )
  1505. {
  1506. mbedtls_printf( " failed\n ! mbedtls_ssl_get_session returned -0x%x\n\n",
  1507. -ret );
  1508. goto exit;
  1509. }
  1510. mbedtls_printf( " ok\n" );
  1511. }
  1512. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1513. /*
  1514. * 5. Verify the server certificate
  1515. */
  1516. mbedtls_printf( " . Verifying peer X.509 certificate..." );
  1517. if( ( flags = mbedtls_ssl_get_verify_result( &ssl ) ) != 0 )
  1518. {
  1519. char vrfy_buf[512];
  1520. mbedtls_printf( " failed\n" );
  1521. mbedtls_x509_crt_verify_info( vrfy_buf, sizeof( vrfy_buf ),
  1522. " ! ", flags );
  1523. mbedtls_printf( "%s\n", vrfy_buf );
  1524. }
  1525. else
  1526. mbedtls_printf( " ok\n" );
  1527. if( mbedtls_ssl_get_peer_cert( &ssl ) != NULL )
  1528. {
  1529. mbedtls_printf( " . Peer certificate information ...\n" );
  1530. mbedtls_x509_crt_info( (char *) buf, sizeof( buf ) - 1, " ",
  1531. mbedtls_ssl_get_peer_cert( &ssl ) );
  1532. mbedtls_printf( "%s\n", buf );
  1533. }
  1534. #endif /* MBEDTLS_X509_CRT_PARSE_C */
  1535. #if defined(MBEDTLS_SSL_RENEGOTIATION)
  1536. if( opt.renegotiate )
  1537. {
  1538. /*
  1539. * Perform renegotiation (this must be done when the server is waiting
  1540. * for input from our side).
  1541. */
  1542. mbedtls_printf( " . Performing renegotiation..." );
  1543. fflush( stdout );
  1544. while( ( ret = mbedtls_ssl_renegotiate( &ssl ) ) != 0 )
  1545. {
  1546. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1547. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1548. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1549. {
  1550. mbedtls_printf( " failed\n ! mbedtls_ssl_renegotiate returned %d\n\n",
  1551. ret );
  1552. goto exit;
  1553. }
  1554. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1555. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1556. continue;
  1557. #endif
  1558. /* For event-driven IO, wait for socket to become available */
  1559. if( opt.event == 1 /* level triggered IO */ )
  1560. {
  1561. #if defined(MBEDTLS_TIMING_C)
  1562. idle( &server_fd, &timer, ret );
  1563. #else
  1564. idle( &server_fd, ret );
  1565. #endif
  1566. }
  1567. }
  1568. mbedtls_printf( " ok\n" );
  1569. }
  1570. #endif /* MBEDTLS_SSL_RENEGOTIATION */
  1571. /*
  1572. * 6. Write the GET request
  1573. */
  1574. retry_left = opt.max_resend;
  1575. send_request:
  1576. mbedtls_printf( " > Write to server:" );
  1577. fflush( stdout );
  1578. len = mbedtls_snprintf( (char *) buf, sizeof( buf ) - 1, GET_REQUEST,
  1579. opt.request_page );
  1580. tail_len = (int) strlen( GET_REQUEST_END );
  1581. /* Add padding to GET request to reach opt.request_size in length */
  1582. if( opt.request_size != DFL_REQUEST_SIZE &&
  1583. len + tail_len < opt.request_size )
  1584. {
  1585. memset( buf + len, 'A', opt.request_size - len - tail_len );
  1586. len += opt.request_size - len - tail_len;
  1587. }
  1588. strncpy( (char *) buf + len, GET_REQUEST_END, sizeof( buf ) - len - 1 );
  1589. len += tail_len;
  1590. /* Truncate if request size is smaller than the "natural" size */
  1591. if( opt.request_size != DFL_REQUEST_SIZE &&
  1592. len > opt.request_size )
  1593. {
  1594. len = opt.request_size;
  1595. /* Still end with \r\n unless that's really not possible */
  1596. if( len >= 2 ) buf[len - 2] = '\r';
  1597. if( len >= 1 ) buf[len - 1] = '\n';
  1598. }
  1599. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  1600. {
  1601. written = 0;
  1602. frags = 0;
  1603. do
  1604. {
  1605. while( ( ret = mbedtls_ssl_write( &ssl, buf + written,
  1606. len - written ) ) < 0 )
  1607. {
  1608. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1609. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1610. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1611. {
  1612. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned -0x%x\n\n",
  1613. -ret );
  1614. goto exit;
  1615. }
  1616. /* For event-driven IO, wait for socket to become available */
  1617. if( opt.event == 1 /* level triggered IO */ )
  1618. {
  1619. #if defined(MBEDTLS_TIMING_C)
  1620. idle( &server_fd, &timer, ret );
  1621. #else
  1622. idle( &server_fd, ret );
  1623. #endif
  1624. }
  1625. }
  1626. frags++;
  1627. written += ret;
  1628. }
  1629. while( written < len );
  1630. }
  1631. else /* Not stream, so datagram */
  1632. {
  1633. while( 1 )
  1634. {
  1635. ret = mbedtls_ssl_write( &ssl, buf, len );
  1636. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1637. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1638. continue;
  1639. #endif
  1640. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1641. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  1642. break;
  1643. /* For event-driven IO, wait for socket to become available */
  1644. if( opt.event == 1 /* level triggered IO */ )
  1645. {
  1646. #if defined(MBEDTLS_TIMING_C)
  1647. idle( &server_fd, &timer, ret );
  1648. #else
  1649. idle( &server_fd, ret );
  1650. #endif
  1651. }
  1652. }
  1653. if( ret < 0 )
  1654. {
  1655. mbedtls_printf( " failed\n ! mbedtls_ssl_write returned %d\n\n",
  1656. ret );
  1657. goto exit;
  1658. }
  1659. frags = 1;
  1660. written = ret;
  1661. if( written < len )
  1662. {
  1663. mbedtls_printf( " warning\n ! request didn't fit into single datagram and "
  1664. "was truncated to size %u", (unsigned) written );
  1665. }
  1666. }
  1667. buf[written] = '\0';
  1668. mbedtls_printf( " %d bytes written in %d fragments\n\n%s\n",
  1669. written, frags, (char *) buf );
  1670. /* Send a non-empty request if request_size == 0 */
  1671. if ( len == 0 )
  1672. {
  1673. opt.request_size = DFL_REQUEST_SIZE;
  1674. goto send_request;
  1675. }
  1676. /*
  1677. * 7. Read the HTTP response
  1678. */
  1679. mbedtls_printf( " < Read from server:" );
  1680. fflush( stdout );
  1681. /*
  1682. * TLS and DTLS need different reading styles (stream vs datagram)
  1683. */
  1684. if( opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM )
  1685. {
  1686. do
  1687. {
  1688. len = sizeof( buf ) - 1;
  1689. memset( buf, 0, sizeof( buf ) );
  1690. ret = mbedtls_ssl_read( &ssl, buf, len );
  1691. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1692. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1693. continue;
  1694. #endif
  1695. if( ret == MBEDTLS_ERR_SSL_WANT_READ ||
  1696. ret == MBEDTLS_ERR_SSL_WANT_WRITE )
  1697. {
  1698. /* For event-driven IO, wait for socket to become available */
  1699. if( opt.event == 1 /* level triggered IO */ )
  1700. {
  1701. #if defined(MBEDTLS_TIMING_C)
  1702. idle( &server_fd, &timer, ret );
  1703. #else
  1704. idle( &server_fd, ret );
  1705. #endif
  1706. }
  1707. continue;
  1708. }
  1709. if( ret <= 0 )
  1710. {
  1711. switch( ret )
  1712. {
  1713. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  1714. mbedtls_printf( " connection was closed gracefully\n" );
  1715. ret = 0;
  1716. goto close_notify;
  1717. case 0:
  1718. case MBEDTLS_ERR_NET_CONN_RESET:
  1719. mbedtls_printf( " connection was reset by peer\n" );
  1720. ret = 0;
  1721. goto reconnect;
  1722. default:
  1723. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n",
  1724. -ret );
  1725. goto exit;
  1726. }
  1727. }
  1728. len = ret;
  1729. buf[len] = '\0';
  1730. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  1731. /* End of message should be detected according to the syntax of the
  1732. * application protocol (eg HTTP), just use a dummy test here. */
  1733. if( ret > 0 && buf[len-1] == '\n' )
  1734. {
  1735. ret = 0;
  1736. break;
  1737. }
  1738. }
  1739. while( 1 );
  1740. }
  1741. else /* Not stream, so datagram */
  1742. {
  1743. len = sizeof( buf ) - 1;
  1744. memset( buf, 0, sizeof( buf ) );
  1745. while( 1 )
  1746. {
  1747. ret = mbedtls_ssl_read( &ssl, buf, len );
  1748. #if defined(MBEDTLS_ECP_RESTARTABLE)
  1749. if( ret == MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1750. continue;
  1751. #endif
  1752. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1753. ret != MBEDTLS_ERR_SSL_WANT_WRITE )
  1754. break;
  1755. /* For event-driven IO, wait for socket to become available */
  1756. if( opt.event == 1 /* level triggered IO */ )
  1757. {
  1758. #if defined(MBEDTLS_TIMING_C)
  1759. idle( &server_fd, &timer, ret );
  1760. #else
  1761. idle( &server_fd, ret );
  1762. #endif
  1763. }
  1764. }
  1765. if( ret <= 0 )
  1766. {
  1767. switch( ret )
  1768. {
  1769. case MBEDTLS_ERR_SSL_TIMEOUT:
  1770. mbedtls_printf( " timeout\n" );
  1771. if( retry_left-- > 0 )
  1772. goto send_request;
  1773. goto exit;
  1774. case MBEDTLS_ERR_SSL_PEER_CLOSE_NOTIFY:
  1775. mbedtls_printf( " connection was closed gracefully\n" );
  1776. ret = 0;
  1777. goto close_notify;
  1778. default:
  1779. mbedtls_printf( " mbedtls_ssl_read returned -0x%x\n", -ret );
  1780. goto exit;
  1781. }
  1782. }
  1783. len = ret;
  1784. buf[len] = '\0';
  1785. mbedtls_printf( " %d bytes read\n\n%s", len, (char *) buf );
  1786. ret = 0;
  1787. }
  1788. /*
  1789. * 7b. Simulate hard reset and reconnect from same port?
  1790. */
  1791. if( opt.reconnect_hard != 0 )
  1792. {
  1793. opt.reconnect_hard = 0;
  1794. mbedtls_printf( " . Restarting connection from same port..." );
  1795. fflush( stdout );
  1796. if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
  1797. {
  1798. mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
  1799. -ret );
  1800. goto exit;
  1801. }
  1802. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  1803. {
  1804. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1805. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1806. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1807. {
  1808. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  1809. -ret );
  1810. goto exit;
  1811. }
  1812. /* For event-driven IO, wait for socket to become available */
  1813. if( opt.event == 1 /* level triggered IO */ )
  1814. {
  1815. #if defined(MBEDTLS_TIMING_C)
  1816. idle( &server_fd, &timer, ret );
  1817. #else
  1818. idle( &server_fd, ret );
  1819. #endif
  1820. }
  1821. }
  1822. mbedtls_printf( " ok\n" );
  1823. goto send_request;
  1824. }
  1825. /*
  1826. * 7c. Continue doing data exchanges?
  1827. */
  1828. if( --opt.exchanges > 0 )
  1829. goto send_request;
  1830. /*
  1831. * 8. Done, cleanly close the connection
  1832. */
  1833. close_notify:
  1834. mbedtls_printf( " . Closing the connection..." );
  1835. fflush( stdout );
  1836. /* No error checking, the connection might be closed already */
  1837. do ret = mbedtls_ssl_close_notify( &ssl );
  1838. while( ret == MBEDTLS_ERR_SSL_WANT_WRITE );
  1839. ret = 0;
  1840. mbedtls_printf( " done\n" );
  1841. /*
  1842. * 9. Reconnect?
  1843. */
  1844. reconnect:
  1845. if( opt.reconnect != 0 )
  1846. {
  1847. --opt.reconnect;
  1848. mbedtls_net_free( &server_fd );
  1849. #if defined(MBEDTLS_TIMING_C)
  1850. if( opt.reco_delay > 0 )
  1851. mbedtls_net_usleep( 1000000 * opt.reco_delay );
  1852. #endif
  1853. mbedtls_printf( " . Reconnecting with saved session..." );
  1854. if( ( ret = mbedtls_ssl_session_reset( &ssl ) ) != 0 )
  1855. {
  1856. mbedtls_printf( " failed\n ! mbedtls_ssl_session_reset returned -0x%x\n\n",
  1857. -ret );
  1858. goto exit;
  1859. }
  1860. if( ( ret = mbedtls_ssl_set_session( &ssl, &saved_session ) ) != 0 )
  1861. {
  1862. mbedtls_printf( " failed\n ! mbedtls_ssl_conf_session returned %d\n\n",
  1863. ret );
  1864. goto exit;
  1865. }
  1866. if( ( ret = mbedtls_net_connect( &server_fd,
  1867. opt.server_addr, opt.server_port,
  1868. opt.transport == MBEDTLS_SSL_TRANSPORT_STREAM ?
  1869. MBEDTLS_NET_PROTO_TCP : MBEDTLS_NET_PROTO_UDP ) ) != 0 )
  1870. {
  1871. mbedtls_printf( " failed\n ! mbedtls_net_connect returned -0x%x\n\n",
  1872. -ret );
  1873. goto exit;
  1874. }
  1875. if( opt.nbio > 0 )
  1876. ret = mbedtls_net_set_nonblock( &server_fd );
  1877. else
  1878. ret = mbedtls_net_set_block( &server_fd );
  1879. if( ret != 0 )
  1880. {
  1881. mbedtls_printf( " failed\n ! net_set_(non)block() returned -0x%x\n\n",
  1882. -ret );
  1883. goto exit;
  1884. }
  1885. while( ( ret = mbedtls_ssl_handshake( &ssl ) ) != 0 )
  1886. {
  1887. if( ret != MBEDTLS_ERR_SSL_WANT_READ &&
  1888. ret != MBEDTLS_ERR_SSL_WANT_WRITE &&
  1889. ret != MBEDTLS_ERR_SSL_CRYPTO_IN_PROGRESS )
  1890. {
  1891. mbedtls_printf( " failed\n ! mbedtls_ssl_handshake returned -0x%x\n\n",
  1892. -ret );
  1893. goto exit;
  1894. }
  1895. }
  1896. mbedtls_printf( " ok\n" );
  1897. goto send_request;
  1898. }
  1899. /*
  1900. * Cleanup and exit
  1901. */
  1902. exit:
  1903. #ifdef MBEDTLS_ERROR_C
  1904. if( ret != 0 )
  1905. {
  1906. char error_buf[100];
  1907. mbedtls_strerror( ret, error_buf, 100 );
  1908. mbedtls_printf("Last error was: -0x%X - %s\n\n", -ret, error_buf );
  1909. }
  1910. #endif
  1911. mbedtls_net_free( &server_fd );
  1912. #if defined(MBEDTLS_X509_CRT_PARSE_C)
  1913. mbedtls_x509_crt_free( &clicert );
  1914. mbedtls_x509_crt_free( &cacert );
  1915. mbedtls_pk_free( &pkey );
  1916. #endif
  1917. mbedtls_ssl_session_free( &saved_session );
  1918. mbedtls_ssl_free( &ssl );
  1919. mbedtls_ssl_config_free( &conf );
  1920. mbedtls_ctr_drbg_free( &ctr_drbg );
  1921. mbedtls_entropy_free( &entropy );
  1922. #if defined(_WIN32)
  1923. mbedtls_printf( " + Press Enter to exit this program.\n" );
  1924. fflush( stdout ); getchar();
  1925. #endif
  1926. // Shell can not handle large exit numbers -> 1 for errors
  1927. if( ret < 0 )
  1928. ret = 1;
  1929. return( ret );
  1930. }
  1931. #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_SSL_TLS_C &&
  1932. MBEDTLS_SSL_CLI_C && MBEDTLS_NET_C && MBEDTLS_RSA_C &&
  1933. MBEDTLS_CTR_DRBG_C MBEDTLS_TIMING_C */