ssl_client2.c 72 KB

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