PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 1ms

/examples/server/server.c

https://github.com/andersmalm/cyassl
C | 436 lines | 335 code | 78 blank | 23 comment | 78 complexity | 78304e28dfc3bbe2c6101cd2c912a806 MD5 | raw file
Possible License(s): GPL-2.0
  1. /* server.c
  2. *
  3. * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
  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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
  20. */
  21. #ifdef HAVE_CONFIG_H
  22. #include <config.h>
  23. #endif
  24. #include <cyassl/openssl/ssl.h>
  25. #include <cyassl/test.h>
  26. #include "examples/server/server.h"
  27. #ifdef CYASSL_CALLBACKS
  28. int srvHandShakeCB(HandShakeInfo*);
  29. int srvTimeoutCB(TimeoutInfo*);
  30. Timeval srvTo;
  31. #endif
  32. static void NonBlockingSSL_Accept(SSL* ssl)
  33. {
  34. #ifndef CYASSL_CALLBACKS
  35. int ret = SSL_accept(ssl);
  36. #else
  37. int ret = CyaSSL_accept_ex(ssl, srvHandShakeCB, srvTimeoutCB, srvTo);
  38. #endif
  39. int error = SSL_get_error(ssl, 0);
  40. SOCKET_T sockfd = (SOCKET_T)CyaSSL_get_fd(ssl);
  41. int select_ret;
  42. while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
  43. error == SSL_ERROR_WANT_WRITE)) {
  44. if (error == SSL_ERROR_WANT_READ)
  45. printf("... server would read block\n");
  46. else
  47. printf("... server would write block\n");
  48. if (CyaSSL_dtls(ssl))
  49. select_ret = tcp_select(sockfd,
  50. CyaSSL_dtls_get_current_timeout(ssl));
  51. else
  52. select_ret = tcp_select(sockfd, 1);
  53. if ((select_ret == TEST_RECV_READY) ||
  54. (select_ret == TEST_ERROR_READY)) {
  55. #ifndef CYASSL_CALLBACKS
  56. ret = SSL_accept(ssl);
  57. #else
  58. ret = CyaSSL_accept_ex(ssl,
  59. srvHandShakeCB, srvTimeoutCB, srvTo);
  60. #endif
  61. error = SSL_get_error(ssl, 0);
  62. }
  63. else if (select_ret == TEST_TIMEOUT &&
  64. (!CyaSSL_dtls(ssl) ||
  65. (CyaSSL_dtls_got_timeout(ssl) >= 0))) {
  66. error = SSL_ERROR_WANT_READ;
  67. }
  68. else {
  69. error = SSL_FATAL_ERROR;
  70. }
  71. }
  72. if (ret != SSL_SUCCESS)
  73. err_sys("SSL_accept failed");
  74. }
  75. static void Usage(void)
  76. {
  77. printf("server " LIBCYASSL_VERSION_STRING
  78. " NOTE: All files relative to CyaSSL home dir\n");
  79. printf("-? Help, print this usage\n");
  80. printf("-p <num> Port to listen on, default %d\n", yasslPort);
  81. printf("-v <num> SSL version [0-3], SSLv3(0) - TLS1.2(3)), default %d\n",
  82. SERVER_DEFAULT_VERSION);
  83. printf("-l <str> Cipher list\n");
  84. printf("-c <file> Certificate file, default %s\n", svrCert);
  85. printf("-k <file> Key file, default %s\n", svrKey);
  86. printf("-A <file> Certificate Authority file, default %s\n", cliCert);
  87. printf("-d Disable client cert check\n");
  88. printf("-b Bind to any interface instead of localhost only\n");
  89. printf("-s Use pre Shared keys\n");
  90. printf("-u Use UDP DTLS\n");
  91. printf("-N Use Non-blocking sockets\n");
  92. }
  93. THREAD_RETURN CYASSL_THREAD server_test(void* args)
  94. {
  95. SOCKET_T sockfd = 0;
  96. int clientfd = 0;
  97. SSL_METHOD* method = 0;
  98. SSL_CTX* ctx = 0;
  99. SSL* ssl = 0;
  100. char msg[] = "I hear you fa shizzle!";
  101. char input[1024];
  102. int idx;
  103. int ch;
  104. int version = SERVER_DEFAULT_VERSION;
  105. int doCliCertCheck = 1;
  106. int useAnyAddr = 0;
  107. int port = yasslPort;
  108. int usePsk = 0;
  109. int doDTLS = 0;
  110. int useNtruKey = 0;
  111. int nonBlocking = 0;
  112. char* cipherList = NULL;
  113. char* verifyCert = (char*)cliCert;
  114. char* ourCert = (char*)svrCert;
  115. char* ourKey = (char*)svrKey;
  116. int argc = ((func_args*)args)->argc;
  117. char** argv = ((func_args*)args)->argv;
  118. ((func_args*)args)->return_code = -1; /* error state */
  119. while ((ch = mygetopt(argc, argv, "?dbsnNup:v:l:A:c:k:")) != -1) {
  120. switch (ch) {
  121. case '?' :
  122. Usage();
  123. exit(EXIT_SUCCESS);
  124. case 'd' :
  125. doCliCertCheck = 0;
  126. break;
  127. case 'b' :
  128. useAnyAddr = 1;
  129. break;
  130. case 's' :
  131. usePsk = 1;
  132. break;
  133. case 'n' :
  134. useNtruKey = 1;
  135. break;
  136. case 'u' :
  137. doDTLS = 1;
  138. version = -1; /* DTLS flag */
  139. break;
  140. case 'p' :
  141. port = atoi(myoptarg);
  142. break;
  143. case 'v' :
  144. version = atoi(myoptarg);
  145. if (version < 0 || version > 3) {
  146. Usage();
  147. exit(MY_EX_USAGE);
  148. }
  149. if (doDTLS)
  150. version = -1; /* stay with DTLS */
  151. break;
  152. case 'l' :
  153. cipherList = myoptarg;
  154. break;
  155. case 'A' :
  156. verifyCert = myoptarg;
  157. break;
  158. case 'c' :
  159. ourCert = myoptarg;
  160. break;
  161. case 'k' :
  162. ourKey = myoptarg;
  163. break;
  164. case 'N':
  165. nonBlocking = 1;
  166. break;
  167. default:
  168. Usage();
  169. exit(MY_EX_USAGE);
  170. }
  171. }
  172. myoptind = 0; /* reset for test cases */
  173. switch (version) {
  174. #ifndef NO_OLD_TLS
  175. case 0:
  176. method = SSLv3_server_method();
  177. break;
  178. case 1:
  179. method = TLSv1_server_method();
  180. break;
  181. case 2:
  182. method = TLSv1_1_server_method();
  183. break;
  184. #endif
  185. case 3:
  186. method = TLSv1_2_server_method();
  187. break;
  188. #ifdef CYASSL_DTLS
  189. case -1:
  190. method = DTLSv1_server_method();
  191. break;
  192. #endif
  193. default:
  194. err_sys("Bad SSL version");
  195. }
  196. if (method == NULL)
  197. err_sys("unable to get method");
  198. ctx = SSL_CTX_new(method);
  199. if (ctx == NULL)
  200. err_sys("unable to get ctx");
  201. if (cipherList)
  202. if (SSL_CTX_set_cipher_list(ctx, cipherList) != SSL_SUCCESS)
  203. err_sys("server can't set cipher list 1");
  204. #ifdef CYASSL_LEANPSK
  205. usePsk = 1;
  206. #endif
  207. #ifndef NO_FILESYSTEM
  208. if (!usePsk) {
  209. if (SSL_CTX_use_certificate_file(ctx, ourCert, SSL_FILETYPE_PEM)
  210. != SSL_SUCCESS)
  211. err_sys("can't load server cert file, check file and run from"
  212. " CyaSSL home dir");
  213. }
  214. #endif
  215. #ifdef HAVE_NTRU
  216. if (useNtruKey) {
  217. if (CyaSSL_CTX_use_NTRUPrivateKey_file(ctx, ourKey)
  218. != SSL_SUCCESS)
  219. err_sys("can't load ntru key file, "
  220. "Please run from CyaSSL home dir");
  221. }
  222. #endif
  223. #ifndef NO_FILESYSTEM
  224. if (!useNtruKey && !usePsk) {
  225. if (SSL_CTX_use_PrivateKey_file(ctx, ourKey, SSL_FILETYPE_PEM)
  226. != SSL_SUCCESS)
  227. err_sys("can't load server cert file, check file and run from"
  228. " CyaSSL home dir");
  229. }
  230. #endif
  231. if (usePsk) {
  232. #ifndef NO_PSK
  233. SSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
  234. SSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
  235. if (cipherList == NULL) {
  236. const char *defaultCipherList;
  237. #ifdef HAVE_NULL_CIPHER
  238. defaultCipherList = "PSK-NULL-SHA";
  239. #else
  240. defaultCipherList = "PSK-AES256-CBC-SHA";
  241. #endif
  242. if (SSL_CTX_set_cipher_list(ctx, defaultCipherList) != SSL_SUCCESS)
  243. err_sys("server can't set cipher list 2");
  244. }
  245. #endif
  246. }
  247. #ifndef NO_FILESYSTEM
  248. /* if not using PSK, verify peer with certs */
  249. if (doCliCertCheck && usePsk == 0) {
  250. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER |
  251. SSL_VERIFY_FAIL_IF_NO_PEER_CERT,0);
  252. if (SSL_CTX_load_verify_locations(ctx, verifyCert, 0) != SSL_SUCCESS)
  253. err_sys("can't load ca file, Please run from CyaSSL home dir");
  254. }
  255. #endif
  256. #ifdef OPENSSL_EXTRA
  257. SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
  258. #endif
  259. #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
  260. /* don't use EDH, can't sniff tmp keys */
  261. if (cipherList == NULL) {
  262. if (SSL_CTX_set_cipher_list(ctx, "AES256-SHA") != SSL_SUCCESS)
  263. err_sys("server can't set cipher list 3");
  264. }
  265. #endif
  266. ssl = SSL_new(ctx);
  267. if (ssl == NULL)
  268. err_sys("unable to get SSL");
  269. #ifdef HAVE_CRL
  270. CyaSSL_EnableCRL(ssl, 0);
  271. CyaSSL_LoadCRL(ssl, crlPemDir, SSL_FILETYPE_PEM, CYASSL_CRL_MONITOR |
  272. CYASSL_CRL_START_MON);
  273. CyaSSL_SetCRL_Cb(ssl, CRL_CallBack);
  274. #endif
  275. tcp_accept(&sockfd, &clientfd, (func_args*)args, port, useAnyAddr, doDTLS);
  276. if (!doDTLS)
  277. CloseSocket(sockfd);
  278. SSL_set_fd(ssl, clientfd);
  279. if (usePsk == 0) {
  280. #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
  281. CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
  282. #elif !defined(NO_CERTS)
  283. SetDH(ssl); /* repick suites with DHE, higher priority than PSK */
  284. #endif
  285. }
  286. #ifndef CYASSL_CALLBACKS
  287. if (nonBlocking) {
  288. CyaSSL_set_using_nonblock(ssl, 1);
  289. tcp_set_nonblocking(&clientfd);
  290. NonBlockingSSL_Accept(ssl);
  291. } else if (SSL_accept(ssl) != SSL_SUCCESS) {
  292. int err = SSL_get_error(ssl, 0);
  293. char buffer[80];
  294. printf("error = %d, %s\n", err, ERR_error_string(err, buffer));
  295. err_sys("SSL_accept failed");
  296. }
  297. #else
  298. NonBlockingSSL_Accept(ssl);
  299. #endif
  300. showPeer(ssl);
  301. idx = SSL_read(ssl, input, sizeof(input));
  302. if (idx > 0) {
  303. input[idx] = 0;
  304. printf("Client message: %s\n", input);
  305. }
  306. else if (idx < 0) {
  307. int readErr = SSL_get_error(ssl, 0);
  308. if (readErr != SSL_ERROR_WANT_READ)
  309. err_sys("SSL_read failed");
  310. }
  311. if (SSL_write(ssl, msg, sizeof(msg)) != sizeof(msg))
  312. err_sys("SSL_write failed");
  313. SSL_shutdown(ssl);
  314. SSL_free(ssl);
  315. SSL_CTX_free(ctx);
  316. CloseSocket(clientfd);
  317. ((func_args*)args)->return_code = 0;
  318. return 0;
  319. }
  320. /* so overall tests can pull in test function */
  321. #ifndef NO_MAIN_DRIVER
  322. int main(int argc, char** argv)
  323. {
  324. func_args args;
  325. #ifdef HAVE_CAVIUM
  326. int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
  327. if (ret != 0)
  328. err_sys("Cavium OpenNitroxDevice failed");
  329. #endif /* HAVE_CAVIUM */
  330. StartTCP();
  331. args.argc = argc;
  332. args.argv = argv;
  333. CyaSSL_Init();
  334. #ifdef DEBUG_CYASSL
  335. CyaSSL_Debugging_ON();
  336. #endif
  337. if (CurrentDir("server") || CurrentDir("build"))
  338. ChangeDirBack(2);
  339. server_test(&args);
  340. CyaSSL_Cleanup();
  341. #ifdef HAVE_CAVIUM
  342. CspShutdown(CAVIUM_DEV_ID);
  343. #endif
  344. return args.return_code;
  345. }
  346. int myoptind = 0;
  347. char* myoptarg = NULL;
  348. #endif /* NO_MAIN_DRIVER */
  349. #ifdef CYASSL_CALLBACKS
  350. int srvHandShakeCB(HandShakeInfo* info)
  351. {
  352. return 0;
  353. }
  354. int srvTimeoutCB(TimeoutInfo* info)
  355. {
  356. return 0;
  357. }
  358. #endif