client.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862
  1. /* client.c
  2. *
  3. * Copyright (C) 2006-2014 wolfSSL Inc.
  4. *
  5. * This file is part of CyaSSL.
  6. *
  7. * CyaSSL is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * CyaSSL is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #if defined(CYASSL_MDK_ARM)
  25. #include <stdio.h>
  26. #include <string.h>
  27. #if defined(CYASSL_MDK5)
  28. #include "cmsis_os.h"
  29. #include "rl_fs.h"
  30. #include "rl_net.h"
  31. #else
  32. #include "rtl.h"
  33. #endif
  34. #include "cyassl_MDK_ARM.h"
  35. #endif
  36. #include <cyassl/ctaocrypt/settings.h>
  37. #if !defined(CYASSL_TRACK_MEMORY) && !defined(NO_MAIN_DRIVER)
  38. /* in case memory tracker wants stats */
  39. #define CYASSL_TRACK_MEMORY
  40. #endif
  41. #include <cyassl/ssl.h>
  42. #include <cyassl/test.h>
  43. #include "examples/client/client.h"
  44. #ifdef CYASSL_CALLBACKS
  45. int handShakeCB(HandShakeInfo*);
  46. int timeoutCB(TimeoutInfo*);
  47. Timeval timeout;
  48. #endif
  49. static void NonBlockingSSL_Connect(CYASSL* ssl)
  50. {
  51. #ifndef CYASSL_CALLBACKS
  52. int ret = CyaSSL_connect(ssl);
  53. #else
  54. int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
  55. #endif
  56. int error = CyaSSL_get_error(ssl, 0);
  57. SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl);
  58. int select_ret;
  59. while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
  60. error == SSL_ERROR_WANT_WRITE)) {
  61. int currTimeout = 1;
  62. if (error == SSL_ERROR_WANT_READ)
  63. printf("... client would read block\n");
  64. else
  65. printf("... client would write block\n");
  66. #ifdef CYASSL_DTLS
  67. currTimeout = CyaSSL_dtls_get_current_timeout(ssl);
  68. #endif
  69. select_ret = tcp_select(sockfd, currTimeout);
  70. if ((select_ret == TEST_RECV_READY) ||
  71. (select_ret == TEST_ERROR_READY)) {
  72. #ifndef CYASSL_CALLBACKS
  73. ret = CyaSSL_connect(ssl);
  74. #else
  75. ret = CyaSSL_connect_ex(ssl,handShakeCB,timeoutCB,timeout);
  76. #endif
  77. error = CyaSSL_get_error(ssl, 0);
  78. }
  79. else if (select_ret == TEST_TIMEOUT && !CyaSSL_dtls(ssl)) {
  80. error = SSL_ERROR_WANT_READ;
  81. }
  82. #ifdef CYASSL_DTLS
  83. else if (select_ret == TEST_TIMEOUT && CyaSSL_dtls(ssl) &&
  84. CyaSSL_dtls_got_timeout(ssl) >= 0) {
  85. error = SSL_ERROR_WANT_READ;
  86. }
  87. #endif
  88. else {
  89. error = SSL_FATAL_ERROR;
  90. }
  91. }
  92. if (ret != SSL_SUCCESS)
  93. err_sys("SSL_connect failed");
  94. }
  95. static void Usage(void)
  96. {
  97. printf("client " LIBCYASSL_VERSION_STRING
  98. " NOTE: All files relative to CyaSSL home dir\n");
  99. printf("-? Help, print this usage\n");
  100. printf("-h <host> Host to connect to, default %s\n", yasslIP);
  101. printf("-p <num> Port to connect on, not 0, default %d\n", yasslPort);
  102. printf("-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default %d\n",
  103. CLIENT_DEFAULT_VERSION);
  104. printf("-l <str> Cipher list\n");
  105. printf("-c <file> Certificate file, default %s\n", cliCert);
  106. printf("-k <file> Key file, default %s\n", cliKey);
  107. printf("-A <file> Certificate Authority file, default %s\n", caCert);
  108. printf("-b <num> Benchmark <num> connections and print stats\n");
  109. printf("-s Use pre Shared keys\n");
  110. printf("-t Track CyaSSL memory use\n");
  111. printf("-d Disable peer checks\n");
  112. printf("-D Override Date Errors example\n");
  113. printf("-g Send server HTTP GET\n");
  114. printf("-u Use UDP DTLS,"
  115. " add -v 2 for DTLSv1 (default), -v 3 for DTLSv1.2\n");
  116. printf("-m Match domain name in cert\n");
  117. printf("-N Use Non-blocking sockets\n");
  118. printf("-r Resume session\n");
  119. printf("-f Fewer packets/group messages\n");
  120. printf("-x Disable client cert/key loading\n");
  121. #ifdef SHOW_SIZES
  122. printf("-z Print structure sizes\n");
  123. #endif
  124. #ifdef HAVE_SNI
  125. printf("-S <str> Use Host Name Indication\n");
  126. #endif
  127. #ifdef HAVE_MAX_FRAGMENT
  128. printf("-L <num> Use Maximum Fragment Length [1-5]\n");
  129. #endif
  130. #ifdef HAVE_TRUNCATED_HMAC
  131. printf("-T Use Truncated HMAC\n");
  132. #endif
  133. #ifdef HAVE_OCSP
  134. printf("-o Perform OCSP lookup on peer certificate\n");
  135. printf("-O <url> Perform OCSP lookup using <url> as responder\n");
  136. #endif
  137. #ifdef ATOMIC_USER
  138. printf("-U Atomic User Record Layer Callbacks\n");
  139. #endif
  140. #ifdef HAVE_PK_CALLBACKS
  141. printf("-P Public Key Callbacks\n");
  142. #endif
  143. }
  144. THREAD_RETURN CYASSL_THREAD client_test(void* args)
  145. {
  146. SOCKET_T sockfd = 0;
  147. CYASSL_METHOD* method = 0;
  148. CYASSL_CTX* ctx = 0;
  149. CYASSL* ssl = 0;
  150. CYASSL* sslResume = 0;
  151. CYASSL_SESSION* session = 0;
  152. char resumeMsg[] = "resuming cyassl!";
  153. int resumeSz = sizeof(resumeMsg);
  154. char msg[32] = "hello cyassl!"; /* GET may make bigger */
  155. char reply[80];
  156. int input;
  157. int msgSz = (int)strlen(msg);
  158. word16 port = yasslPort;
  159. char* host = (char*)yasslIP;
  160. const char* domain = "www.yassl.com";
  161. int ch;
  162. int version = CLIENT_INVALID_VERSION;
  163. int usePsk = 0;
  164. int sendGET = 0;
  165. int benchmark = 0;
  166. int doDTLS = 0;
  167. int matchName = 0;
  168. int doPeerCheck = 1;
  169. int nonBlocking = 0;
  170. int resumeSession = 0;
  171. int trackMemory = 0;
  172. int useClientCert = 1;
  173. int fewerPackets = 0;
  174. int atomicUser = 0;
  175. int pkCallbacks = 0;
  176. int overrideDateErrors = 0;
  177. char* cipherList = NULL;
  178. const char* verifyCert = caCert;
  179. const char* ourCert = cliCert;
  180. const char* ourKey = cliKey;
  181. #ifdef HAVE_SNI
  182. char* sniHostName = NULL;
  183. #endif
  184. #ifdef HAVE_MAX_FRAGMENT
  185. byte maxFragment = 0;
  186. #endif
  187. #ifdef HAVE_TRUNCATED_HMAC
  188. byte truncatedHMAC = 0;
  189. #endif
  190. #ifdef HAVE_OCSP
  191. int useOcsp = 0;
  192. char* ocspUrl = NULL;
  193. #endif
  194. int argc = ((func_args*)args)->argc;
  195. char** argv = ((func_args*)args)->argv;
  196. ((func_args*)args)->return_code = -1; /* error state */
  197. #ifdef NO_RSA
  198. verifyCert = (char*)eccCert;
  199. ourCert = (char*)cliEccCert;
  200. ourKey = (char*)cliEccKey;
  201. #endif
  202. (void)resumeSz;
  203. (void)session;
  204. (void)sslResume;
  205. (void)trackMemory;
  206. (void)atomicUser;
  207. (void)pkCallbacks;
  208. StackTrap();
  209. while ((ch = mygetopt(argc, argv,
  210. "?gdDusmNrtfxUPh:p:v:l:A:c:k:b:zS:L:ToO:")) != -1) {
  211. switch (ch) {
  212. case '?' :
  213. Usage();
  214. exit(EXIT_SUCCESS);
  215. case 'g' :
  216. sendGET = 1;
  217. break;
  218. case 'd' :
  219. doPeerCheck = 0;
  220. break;
  221. case 'D' :
  222. overrideDateErrors = 1;
  223. break;
  224. case 'u' :
  225. doDTLS = 1;
  226. break;
  227. case 's' :
  228. usePsk = 1;
  229. break;
  230. case 't' :
  231. #ifdef USE_CYASSL_MEMORY
  232. trackMemory = 1;
  233. #endif
  234. break;
  235. case 'm' :
  236. matchName = 1;
  237. break;
  238. case 'x' :
  239. useClientCert = 0;
  240. break;
  241. case 'f' :
  242. fewerPackets = 1;
  243. break;
  244. case 'U' :
  245. #ifdef ATOMIC_USER
  246. atomicUser = 1;
  247. #endif
  248. break;
  249. case 'P' :
  250. #ifdef HAVE_PK_CALLBACKS
  251. pkCallbacks = 1;
  252. #endif
  253. break;
  254. case 'h' :
  255. host = myoptarg;
  256. domain = myoptarg;
  257. break;
  258. case 'p' :
  259. port = (word16)atoi(myoptarg);
  260. #if !defined(NO_MAIN_DRIVER) || defined(USE_WINDOWS_API)
  261. if (port == 0)
  262. err_sys("port number cannot be 0");
  263. #endif
  264. break;
  265. case 'v' :
  266. version = atoi(myoptarg);
  267. if (version < 0 || version > 3) {
  268. Usage();
  269. exit(MY_EX_USAGE);
  270. }
  271. break;
  272. case 'l' :
  273. cipherList = myoptarg;
  274. break;
  275. case 'A' :
  276. verifyCert = myoptarg;
  277. break;
  278. case 'c' :
  279. ourCert = myoptarg;
  280. break;
  281. case 'k' :
  282. ourKey = myoptarg;
  283. break;
  284. case 'b' :
  285. benchmark = atoi(myoptarg);
  286. if (benchmark < 0 || benchmark > 1000000) {
  287. Usage();
  288. exit(MY_EX_USAGE);
  289. }
  290. break;
  291. case 'N' :
  292. nonBlocking = 1;
  293. break;
  294. case 'r' :
  295. resumeSession = 1;
  296. break;
  297. case 'z' :
  298. #ifndef CYASSL_LEANPSK
  299. CyaSSL_GetObjectSize();
  300. #endif
  301. break;
  302. case 'S' :
  303. #ifdef HAVE_SNI
  304. sniHostName = myoptarg;
  305. #endif
  306. break;
  307. case 'L' :
  308. #ifdef HAVE_MAX_FRAGMENT
  309. maxFragment = atoi(myoptarg);
  310. if (maxFragment < CYASSL_MFL_2_9 ||
  311. maxFragment > CYASSL_MFL_2_13) {
  312. Usage();
  313. exit(MY_EX_USAGE);
  314. }
  315. #endif
  316. break;
  317. case 'T' :
  318. #ifdef HAVE_TRUNCATED_HMAC
  319. truncatedHMAC = 1;
  320. #endif
  321. break;
  322. case 'o' :
  323. #ifdef HAVE_OCSP
  324. useOcsp = 1;
  325. #endif
  326. break;
  327. case 'O' :
  328. #ifdef HAVE_OCSP
  329. useOcsp = 1;
  330. ocspUrl = myoptarg;
  331. #endif
  332. break;
  333. default:
  334. Usage();
  335. exit(MY_EX_USAGE);
  336. }
  337. }
  338. myoptind = 0; /* reset for test cases */
  339. /* sort out DTLS versus TLS versions */
  340. if (version == CLIENT_INVALID_VERSION) {
  341. if (doDTLS)
  342. version = CLIENT_DTLS_DEFAULT_VERSION;
  343. else
  344. version = CLIENT_DEFAULT_VERSION;
  345. }
  346. else {
  347. if (doDTLS) {
  348. if (version == 3)
  349. version = -2;
  350. else
  351. version = -1;
  352. }
  353. }
  354. #ifdef USE_CYASSL_MEMORY
  355. if (trackMemory)
  356. InitMemoryTracker();
  357. #endif
  358. switch (version) {
  359. #ifndef NO_OLD_TLS
  360. case 0:
  361. method = wolfSSLv3_client_method();
  362. break;
  363. #ifndef NO_TLS
  364. case 1:
  365. method = CyaTLSv1_client_method();
  366. break;
  367. case 2:
  368. method = CyaTLSv1_1_client_method();
  369. break;
  370. #endif /* NO_TLS */
  371. #endif /* NO_OLD_TLS */
  372. #ifndef NO_TLS
  373. case 3:
  374. method = CyaTLSv1_2_client_method();
  375. break;
  376. #endif
  377. #ifdef CYASSL_DTLS
  378. case -1:
  379. method = CyaDTLSv1_client_method();
  380. break;
  381. case -2:
  382. method = CyaDTLSv1_2_client_method();
  383. break;
  384. #endif
  385. default:
  386. err_sys("Bad SSL version");
  387. break;
  388. }
  389. if (method == NULL)
  390. err_sys("unable to get method");
  391. ctx = CyaSSL_CTX_new(method);
  392. if (ctx == NULL)
  393. err_sys("unable to get ctx");
  394. if (cipherList)
  395. if (CyaSSL_CTX_set_cipher_list(ctx, cipherList) != SSL_SUCCESS)
  396. err_sys("client can't set cipher list 1");
  397. #ifdef CYASSL_LEANPSK
  398. usePsk = 1;
  399. #endif
  400. #if defined(NO_RSA) && !defined(HAVE_ECC)
  401. usePsk = 1;
  402. #endif
  403. if (fewerPackets)
  404. CyaSSL_CTX_set_group_messages(ctx);
  405. if (usePsk) {
  406. #ifndef NO_PSK
  407. CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
  408. if (cipherList == NULL) {
  409. const char *defaultCipherList;
  410. #ifdef HAVE_NULL_CIPHER
  411. defaultCipherList = "PSK-NULL-SHA256";
  412. #else
  413. defaultCipherList = "PSK-AES128-CBC-SHA256";
  414. #endif
  415. if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=SSL_SUCCESS)
  416. err_sys("client can't set cipher list 2");
  417. }
  418. #endif
  419. useClientCert = 0;
  420. }
  421. #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
  422. CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  423. #endif
  424. #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
  425. if (cipherList == NULL) {
  426. /* don't use EDH, can't sniff tmp keys */
  427. if (CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA256") != SSL_SUCCESS) {
  428. err_sys("client can't set cipher list 3");
  429. }
  430. }
  431. #endif
  432. #ifdef HAVE_OCSP
  433. if (useOcsp) {
  434. if (ocspUrl != NULL) {
  435. CyaSSL_CTX_SetOCSP_OverrideURL(ctx, ocspUrl);
  436. CyaSSL_CTX_EnableOCSP(ctx, CYASSL_OCSP_NO_NONCE
  437. | CYASSL_OCSP_URL_OVERRIDE);
  438. }
  439. else
  440. CyaSSL_CTX_EnableOCSP(ctx, CYASSL_OCSP_NO_NONCE);
  441. }
  442. #endif
  443. #ifdef USER_CA_CB
  444. CyaSSL_CTX_SetCACb(ctx, CaCb);
  445. #endif
  446. #ifdef VERIFY_CALLBACK
  447. CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
  448. #endif
  449. #if !defined(NO_FILESYSTEM) && !defined(NO_CERTS)
  450. if (useClientCert){
  451. if (CyaSSL_CTX_use_certificate_chain_file(ctx, ourCert) != SSL_SUCCESS)
  452. err_sys("can't load client cert file, check file and run from"
  453. " CyaSSL home dir");
  454. if (CyaSSL_CTX_use_PrivateKey_file(ctx, ourKey, SSL_FILETYPE_PEM)
  455. != SSL_SUCCESS)
  456. err_sys("can't load client private key file, check file and run "
  457. "from CyaSSL home dir");
  458. }
  459. if (!usePsk) {
  460. if (CyaSSL_CTX_load_verify_locations(ctx, verifyCert, 0) != SSL_SUCCESS)
  461. err_sys("can't load ca file, Please run from CyaSSL home dir");
  462. }
  463. #endif
  464. #if !defined(NO_CERTS)
  465. if (!usePsk && doPeerCheck == 0)
  466. CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  467. if (!usePsk && overrideDateErrors == 1)
  468. CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myDateCb);
  469. #endif
  470. #ifdef HAVE_CAVIUM
  471. CyaSSL_CTX_UseCavium(ctx, CAVIUM_DEV_ID);
  472. #endif
  473. #ifdef HAVE_SNI
  474. if (sniHostName)
  475. if (CyaSSL_CTX_UseSNI(ctx, 0, sniHostName, XSTRLEN(sniHostName))
  476. != SSL_SUCCESS)
  477. err_sys("UseSNI failed");
  478. #endif
  479. #ifdef HAVE_MAX_FRAGMENT
  480. if (maxFragment)
  481. if (CyaSSL_CTX_UseMaxFragment(ctx, maxFragment) != SSL_SUCCESS)
  482. err_sys("UseMaxFragment failed");
  483. #endif
  484. #ifdef HAVE_TRUNCATED_HMAC
  485. if (truncatedHMAC)
  486. if (CyaSSL_CTX_UseTruncatedHMAC(ctx) != SSL_SUCCESS)
  487. err_sys("UseTruncatedHMAC failed");
  488. #endif
  489. if (benchmark) {
  490. /* time passed in number of connects give average */
  491. int times = benchmark;
  492. int i = 0;
  493. double start = current_time(), avg;
  494. for (i = 0; i < times; i++) {
  495. tcp_connect(&sockfd, host, port, doDTLS);
  496. ssl = CyaSSL_new(ctx);
  497. CyaSSL_set_fd(ssl, sockfd);
  498. if (CyaSSL_connect(ssl) != SSL_SUCCESS)
  499. err_sys("SSL_connect failed");
  500. CyaSSL_shutdown(ssl);
  501. CyaSSL_free(ssl);
  502. CloseSocket(sockfd);
  503. }
  504. avg = current_time() - start;
  505. avg /= times;
  506. avg *= 1000; /* milliseconds */
  507. printf("CyaSSL_connect avg took: %8.3f milliseconds\n", avg);
  508. CyaSSL_CTX_free(ctx);
  509. ((func_args*)args)->return_code = 0;
  510. exit(EXIT_SUCCESS);
  511. }
  512. #if defined(CYASSL_MDK_ARM)
  513. CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
  514. #endif
  515. ssl = CyaSSL_new(ctx);
  516. if (ssl == NULL)
  517. err_sys("unable to get SSL object");
  518. if (doDTLS) {
  519. SOCKADDR_IN_T addr;
  520. build_addr(&addr, host, port, 1);
  521. CyaSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
  522. tcp_socket(&sockfd, 1);
  523. }
  524. else {
  525. tcp_connect(&sockfd, host, port, 0);
  526. }
  527. CyaSSL_set_fd(ssl, sockfd);
  528. #ifdef HAVE_CRL
  529. if (CyaSSL_EnableCRL(ssl, CYASSL_CRL_CHECKALL) != SSL_SUCCESS)
  530. err_sys("can't enable crl check");
  531. if (CyaSSL_LoadCRL(ssl, crlPemDir, SSL_FILETYPE_PEM, 0) != SSL_SUCCESS)
  532. err_sys("can't load crl, check crlfile and date validity");
  533. if (CyaSSL_SetCRL_Cb(ssl, CRL_CallBack) != SSL_SUCCESS)
  534. err_sys("can't set crl callback");
  535. #endif
  536. #ifdef ATOMIC_USER
  537. if (atomicUser)
  538. SetupAtomicUser(ctx, ssl);
  539. #endif
  540. #ifdef HAVE_PK_CALLBACKS
  541. if (pkCallbacks)
  542. SetupPkCallbacks(ctx, ssl);
  543. #endif
  544. if (matchName && doPeerCheck)
  545. CyaSSL_check_domain_name(ssl, domain);
  546. #ifndef CYASSL_CALLBACKS
  547. if (nonBlocking) {
  548. CyaSSL_set_using_nonblock(ssl, 1);
  549. tcp_set_nonblocking(&sockfd);
  550. NonBlockingSSL_Connect(ssl);
  551. }
  552. else if (CyaSSL_connect(ssl) != SSL_SUCCESS) {
  553. /* see note at top of README */
  554. int err = CyaSSL_get_error(ssl, 0);
  555. char buffer[CYASSL_MAX_ERROR_SZ];
  556. printf("err = %d, %s\n", err,
  557. CyaSSL_ERR_error_string(err, buffer));
  558. err_sys("SSL_connect failed");
  559. /* if you're getting an error here */
  560. }
  561. #else
  562. timeout.tv_sec = 2;
  563. timeout.tv_usec = 0;
  564. NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  565. #endif
  566. showPeer(ssl);
  567. if (sendGET) {
  568. printf("SSL connect ok, sending GET...\n");
  569. msgSz = 28;
  570. strncpy(msg, "GET /index.html HTTP/1.0\r\n\r\n", msgSz);
  571. msg[msgSz] = '\0';
  572. }
  573. if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
  574. err_sys("SSL_write failed");
  575. input = CyaSSL_read(ssl, reply, sizeof(reply)-1);
  576. if (input > 0) {
  577. reply[input] = 0;
  578. printf("Server response: %s\n", reply);
  579. if (sendGET) { /* get html */
  580. while (1) {
  581. input = CyaSSL_read(ssl, reply, sizeof(reply)-1);
  582. if (input > 0) {
  583. reply[input] = 0;
  584. printf("%s\n", reply);
  585. }
  586. else
  587. break;
  588. }
  589. }
  590. }
  591. else if (input < 0) {
  592. int readErr = CyaSSL_get_error(ssl, 0);
  593. if (readErr != SSL_ERROR_WANT_READ)
  594. err_sys("CyaSSL_read failed");
  595. }
  596. #ifndef NO_SESSION_CACHE
  597. if (resumeSession) {
  598. if (doDTLS) {
  599. strncpy(msg, "break", 6);
  600. msgSz = (int)strlen(msg);
  601. /* try to send session close */
  602. CyaSSL_write(ssl, msg, msgSz);
  603. }
  604. session = CyaSSL_get_session(ssl);
  605. sslResume = CyaSSL_new(ctx);
  606. }
  607. #endif
  608. if (doDTLS == 0) /* don't send alert after "break" command */
  609. CyaSSL_shutdown(ssl); /* echoserver will interpret as new conn */
  610. #ifdef ATOMIC_USER
  611. if (atomicUser)
  612. FreeAtomicUser(ssl);
  613. #endif
  614. CyaSSL_free(ssl);
  615. CloseSocket(sockfd);
  616. #ifndef NO_SESSION_CACHE
  617. if (resumeSession) {
  618. if (doDTLS) {
  619. SOCKADDR_IN_T addr;
  620. #ifdef USE_WINDOWS_API
  621. Sleep(500);
  622. #else
  623. sleep(1);
  624. #endif
  625. build_addr(&addr, host, port, 1);
  626. CyaSSL_dtls_set_peer(sslResume, &addr, sizeof(addr));
  627. tcp_socket(&sockfd, 1);
  628. }
  629. else {
  630. tcp_connect(&sockfd, host, port, 0);
  631. }
  632. CyaSSL_set_fd(sslResume, sockfd);
  633. CyaSSL_set_session(sslResume, session);
  634. showPeer(sslResume);
  635. #ifndef CYASSL_CALLBACKS
  636. if (nonBlocking) {
  637. CyaSSL_set_using_nonblock(sslResume, 1);
  638. tcp_set_nonblocking(&sockfd);
  639. NonBlockingSSL_Connect(sslResume);
  640. }
  641. else if (CyaSSL_connect(sslResume) != SSL_SUCCESS)
  642. err_sys("SSL resume failed");
  643. #else
  644. timeout.tv_sec = 2;
  645. timeout.tv_usec = 0;
  646. NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
  647. #endif
  648. if (CyaSSL_session_reused(sslResume))
  649. printf("reused session id\n");
  650. else
  651. printf("didn't reuse session id!!!\n");
  652. if (CyaSSL_write(sslResume, resumeMsg, resumeSz) != resumeSz)
  653. err_sys("SSL_write failed");
  654. if (nonBlocking) {
  655. /* give server a chance to bounce a message back to client */
  656. #ifdef USE_WINDOWS_API
  657. Sleep(500);
  658. #else
  659. sleep(1);
  660. #endif
  661. }
  662. input = CyaSSL_read(sslResume, reply, sizeof(reply)-1);
  663. if (input > 0) {
  664. reply[input] = 0;
  665. printf("Server resume response: %s\n", reply);
  666. }
  667. /* try to send session break */
  668. CyaSSL_write(sslResume, msg, msgSz);
  669. CyaSSL_shutdown(sslResume);
  670. CyaSSL_free(sslResume);
  671. CloseSocket(sockfd);
  672. }
  673. #endif /* NO_SESSION_CACHE */
  674. CyaSSL_CTX_free(ctx);
  675. ((func_args*)args)->return_code = 0;
  676. #ifdef USE_CYASSL_MEMORY
  677. if (trackMemory)
  678. ShowMemoryTracker();
  679. #endif /* USE_CYASSL_MEMORY */
  680. return 0;
  681. }
  682. /* so overall tests can pull in test function */
  683. #ifndef NO_MAIN_DRIVER
  684. int main(int argc, char** argv)
  685. {
  686. func_args args;
  687. #ifdef HAVE_CAVIUM
  688. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  689. if (ret != 0)
  690. err_sys("Cavium OpenNitroxDevice failed");
  691. #endif /* HAVE_CAVIUM */
  692. StartTCP();
  693. args.argc = argc;
  694. args.argv = argv;
  695. CyaSSL_Init();
  696. #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL) && !defined(STACK_TRAP)
  697. CyaSSL_Debugging_ON();
  698. #endif
  699. if (CurrentDir("client"))
  700. ChangeDirBack(2);
  701. else if (CurrentDir("Debug") || CurrentDir("Release"))
  702. ChangeDirBack(3);
  703. #ifdef HAVE_STACK_SIZE
  704. StackSizeCheck(&args, client_test);
  705. #else
  706. client_test(&args);
  707. #endif
  708. CyaSSL_Cleanup();
  709. #ifdef HAVE_CAVIUM
  710. CspShutdown(CAVIUM_DEV_ID);
  711. #endif
  712. return args.return_code;
  713. }
  714. int myoptind = 0;
  715. char* myoptarg = NULL;
  716. #endif /* NO_MAIN_DRIVER */
  717. #ifdef CYASSL_CALLBACKS
  718. int handShakeCB(HandShakeInfo* info)
  719. {
  720. (void)info;
  721. return 0;
  722. }
  723. int timeoutCB(TimeoutInfo* info)
  724. {
  725. (void)info;
  726. return 0;
  727. }
  728. #endif