axssl.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883
  1. /*
  2. * Copyright (c) 2007, Cameron Rich
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions are met:
  8. *
  9. * * Redistributions of source code must retain the above copyright notice,
  10. * this list of conditions and the following disclaimer.
  11. * * Redistributions in binary form must reproduce the above copyright notice,
  12. * this list of conditions and the following disclaimer in the documentation
  13. * and/or other materials provided with the distribution.
  14. * * Neither the name of the axTLS project nor the names of its contributors
  15. * may be used to endorse or promote products derived from this software
  16. * without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  19. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  20. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  21. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  22. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
  23. * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  24. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  25. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  26. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  27. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. /**
  31. * Demonstrate the use of the axTLS library in C with a set of
  32. * command-line parameters similar to openssl. In fact, openssl clients
  33. * should be able to communicate with axTLS servers and visa-versa.
  34. *
  35. * This code has various bits enabled depending on the configuration. To enable
  36. * the most interesting version, compile with the 'full mode' enabled.
  37. *
  38. * To see what options you have, run the following:
  39. * > axssl s_server -?
  40. * > axssl s_client -?
  41. *
  42. * The axtls shared library must be in the same directory or be found
  43. * by the OS.
  44. */
  45. #include <string.h>
  46. #include <stdio.h>
  47. #include <stdlib.h>
  48. #include "ssl.h"
  49. /* define standard input */
  50. #ifndef STDIN_FILENO
  51. #define STDIN_FILENO 0
  52. #endif
  53. static void do_server(int argc, char *argv[]);
  54. static void print_options(char *option);
  55. static void print_server_options(char *option);
  56. static void do_client(int argc, char *argv[]);
  57. static void print_client_options(char *option);
  58. static void display_cipher(SSL *ssl);
  59. static void display_session_id(SSL *ssl);
  60. /**
  61. * Main entry point. Doesn't do much except works out whether we are a client
  62. * or a server.
  63. */
  64. int main(int argc, char *argv[])
  65. {
  66. #ifdef WIN32
  67. WSADATA wsaData;
  68. WORD wVersionRequested = MAKEWORD(2, 2);
  69. WSAStartup(wVersionRequested, &wsaData);
  70. #elif !defined(CONFIG_PLATFORM_SOLARIS)
  71. signal(SIGPIPE, SIG_IGN); /* ignore pipe errors */
  72. #endif
  73. if (argc == 2 && strcmp(argv[1], "version") == 0)
  74. {
  75. printf("axssl %s %s\n", ssl_version(), __DATE__);
  76. exit(0);
  77. }
  78. if (argc < 2 || (
  79. strcmp(argv[1], "s_server") && strcmp(argv[1], "s_client")))
  80. print_options(argc > 1 ? argv[1] : "");
  81. strcmp(argv[1], "s_server") ?
  82. do_client(argc, argv) : do_server(argc, argv);
  83. return 0;
  84. }
  85. /**
  86. * Implement the SSL server logic.
  87. */
  88. static void do_server(int argc, char *argv[])
  89. {
  90. int i = 2;
  91. uint16_t port = 4433;
  92. uint32_t options = SSL_DISPLAY_CERTS;
  93. int client_fd;
  94. SSL_CTX *ssl_ctx;
  95. int server_fd, res = 0;
  96. socklen_t client_len;
  97. #ifndef CONFIG_SSL_SKELETON_MODE
  98. char *private_key_file = NULL;
  99. const char *password = NULL;
  100. char **cert;
  101. int cert_index = 0;
  102. int cert_size = ssl_get_config(SSL_MAX_CERT_CFG_OFFSET);
  103. #endif
  104. #ifdef WIN32
  105. char yes = 1;
  106. #else
  107. int yes = 1;
  108. #endif
  109. struct sockaddr_in serv_addr;
  110. struct sockaddr_in client_addr;
  111. int quiet = 0;
  112. #ifdef CONFIG_SSL_CERT_VERIFICATION
  113. int ca_cert_index = 0;
  114. int ca_cert_size = ssl_get_config(SSL_MAX_CA_CERT_CFG_OFFSET);
  115. char **ca_cert = (char **)calloc(1, sizeof(char *)*ca_cert_size);
  116. #endif
  117. fd_set read_set;
  118. #ifndef CONFIG_SSL_SKELETON_MODE
  119. cert = (char **)calloc(1, sizeof(char *)*cert_size);
  120. #endif
  121. while (i < argc)
  122. {
  123. if (strcmp(argv[i], "-accept") == 0)
  124. {
  125. if (i >= argc-1)
  126. {
  127. print_server_options(argv[i]);
  128. }
  129. port = atoi(argv[++i]);
  130. }
  131. #ifndef CONFIG_SSL_SKELETON_MODE
  132. else if (strcmp(argv[i], "-cert") == 0)
  133. {
  134. if (i >= argc-1 || cert_index >= cert_size)
  135. {
  136. print_server_options(argv[i]);
  137. }
  138. cert[cert_index++] = argv[++i];
  139. }
  140. else if (strcmp(argv[i], "-key") == 0)
  141. {
  142. if (i >= argc-1)
  143. {
  144. print_server_options(argv[i]);
  145. }
  146. private_key_file = argv[++i];
  147. options |= SSL_NO_DEFAULT_KEY;
  148. }
  149. else if (strcmp(argv[i], "-pass") == 0)
  150. {
  151. if (i >= argc-1)
  152. {
  153. print_server_options(argv[i]);
  154. }
  155. password = argv[++i];
  156. }
  157. #endif
  158. else if (strcmp(argv[i], "-quiet") == 0)
  159. {
  160. quiet = 1;
  161. options &= ~SSL_DISPLAY_CERTS;
  162. }
  163. #ifdef CONFIG_SSL_CERT_VERIFICATION
  164. else if (strcmp(argv[i], "-verify") == 0)
  165. {
  166. options |= SSL_CLIENT_AUTHENTICATION;
  167. }
  168. else if (strcmp(argv[i], "-CAfile") == 0)
  169. {
  170. if (i >= argc-1 || ca_cert_index >= ca_cert_size)
  171. {
  172. print_server_options(argv[i]);
  173. }
  174. ca_cert[ca_cert_index++] = argv[++i];
  175. }
  176. #endif
  177. #ifdef CONFIG_SSL_FULL_MODE
  178. else if (strcmp(argv[i], "-debug") == 0)
  179. {
  180. options |= SSL_DISPLAY_BYTES;
  181. }
  182. else if (strcmp(argv[i], "-state") == 0)
  183. {
  184. options |= SSL_DISPLAY_STATES;
  185. }
  186. else if (strcmp(argv[i], "-show-rsa") == 0)
  187. {
  188. options |= SSL_DISPLAY_RSA;
  189. }
  190. #endif
  191. else /* don't know what this is */
  192. {
  193. print_server_options(argv[i]);
  194. }
  195. i++;
  196. }
  197. if ((ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_SVR_SESS)) == NULL)
  198. {
  199. fprintf(stderr, "Error: Server context is invalid\n");
  200. exit(1);
  201. }
  202. #ifndef CONFIG_SSL_SKELETON_MODE
  203. if (private_key_file)
  204. {
  205. int obj_type = SSL_OBJ_RSA_KEY;
  206. /* auto-detect the key type from the file extension */
  207. if (strstr(private_key_file, ".p8"))
  208. obj_type = SSL_OBJ_PKCS8;
  209. else if (strstr(private_key_file, ".p12"))
  210. obj_type = SSL_OBJ_PKCS12;
  211. if (ssl_obj_load(ssl_ctx, obj_type, private_key_file, password))
  212. {
  213. fprintf(stderr, "Error: Private key '%s' is undefined.\n",
  214. private_key_file);
  215. exit(1);
  216. }
  217. }
  218. for (i = 0; i < cert_index; i++)
  219. {
  220. if (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, cert[i], NULL))
  221. {
  222. printf("Certificate '%s' is undefined.\n", cert[i]);
  223. exit(1);
  224. }
  225. }
  226. #endif
  227. #ifdef CONFIG_SSL_CERT_VERIFICATION
  228. for (i = 0; i < ca_cert_index; i++)
  229. {
  230. if (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CACERT, ca_cert[i], NULL))
  231. {
  232. printf("Certificate '%s' is undefined.\n", ca_cert[i]);
  233. exit(1);
  234. }
  235. }
  236. free(ca_cert);
  237. #endif
  238. #ifndef CONFIG_SSL_SKELETON_MODE
  239. free(cert);
  240. #endif
  241. /* Create socket for incoming connections */
  242. if ((server_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
  243. {
  244. perror("socket");
  245. return;
  246. }
  247. setsockopt(server_fd, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes));
  248. /* Construct local address structure */
  249. memset(&serv_addr, 0, sizeof(serv_addr)); /* Zero out structure */
  250. serv_addr.sin_family = AF_INET; /* Internet address family */
  251. serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); /* Any incoming interface */
  252. serv_addr.sin_port = htons(port); /* Local port */
  253. /* Bind to the local address */
  254. if (bind(server_fd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0)
  255. {
  256. perror("bind");
  257. exit(1);
  258. }
  259. if (listen(server_fd, 5) < 0)
  260. {
  261. perror("listen");
  262. exit(1);
  263. }
  264. client_len = sizeof(client_addr);
  265. /*************************************************************************
  266. * This is where the interesting stuff happens. Up until now we've
  267. * just been setting up sockets etc. Now we do the SSL handshake.
  268. *************************************************************************/
  269. for (;;)
  270. {
  271. SSL *ssl;
  272. int reconnected = 0;
  273. if (!quiet)
  274. {
  275. printf("ACCEPT\n");
  276. TTY_FLUSH();
  277. }
  278. if ((client_fd = accept(server_fd,
  279. (struct sockaddr *)&client_addr, &client_len)) < 0)
  280. {
  281. res = 1;
  282. break;
  283. }
  284. ssl = ssl_server_new(ssl_ctx, client_fd);
  285. /* now read (and display) whatever the client sends us */
  286. for (;;)
  287. {
  288. /* allow parallel reading of client and standard input */
  289. FD_ZERO(&read_set);
  290. FD_SET(client_fd, &read_set);
  291. #ifndef WIN32
  292. /* win32 doesn't like mixing up stdin and sockets */
  293. if (isatty(STDIN_FILENO))/* but only if we are in an active shell */
  294. {
  295. FD_SET(STDIN_FILENO, &read_set);
  296. }
  297. if ((res = select(client_fd+1, &read_set, NULL, NULL, NULL)) > 0)
  298. {
  299. uint8_t buf[1024];
  300. /* read standard input? */
  301. if (FD_ISSET(STDIN_FILENO, &read_set))
  302. {
  303. if (fgets((char *)buf, sizeof(buf), stdin) == NULL)
  304. {
  305. res = SSL_ERROR_CONN_LOST;
  306. }
  307. else
  308. {
  309. /* small hack to check renegotiation */
  310. if (buf[0] == 'r' && (buf[1] == '\n' || buf[1] == '\r'))
  311. {
  312. res = ssl_renegotiate(ssl);
  313. }
  314. else /* write our ramblings to the client */
  315. {
  316. res = ssl_write(ssl, buf, strlen((char *)buf)+1);
  317. }
  318. }
  319. }
  320. else /* a socket read */
  321. #endif
  322. {
  323. /* keep reading until we get something interesting */
  324. uint8_t *read_buf;
  325. if ((res = ssl_read(ssl, &read_buf)) == SSL_OK)
  326. {
  327. /* are we in the middle of doing a handshake? */
  328. if (ssl_handshake_status(ssl) != SSL_OK)
  329. {
  330. reconnected = 0;
  331. }
  332. else if (!reconnected)
  333. {
  334. /* we are connected/reconnected */
  335. if (!quiet)
  336. {
  337. display_session_id(ssl);
  338. display_cipher(ssl);
  339. }
  340. reconnected = 1;
  341. }
  342. }
  343. if (res > SSL_OK) /* display our interesting output */
  344. {
  345. printf("%s", read_buf);
  346. TTY_FLUSH();
  347. }
  348. else if (res < SSL_OK && !quiet)
  349. {
  350. ssl_display_error(res);
  351. }
  352. }
  353. #ifndef WIN32
  354. }
  355. #endif
  356. if (res < SSL_OK)
  357. {
  358. if (!quiet)
  359. {
  360. printf("CONNECTION CLOSED\n");
  361. TTY_FLUSH();
  362. }
  363. break;
  364. }
  365. }
  366. /* client was disconnected or the handshake failed. */
  367. ssl_free(ssl);
  368. SOCKET_CLOSE(client_fd);
  369. }
  370. ssl_ctx_free(ssl_ctx);
  371. }
  372. /**
  373. * Implement the SSL client logic.
  374. */
  375. static void do_client(int argc, char *argv[])
  376. {
  377. #ifdef CONFIG_SSL_ENABLE_CLIENT
  378. int res, i = 2;
  379. uint16_t port = 4433;
  380. uint32_t options = SSL_SERVER_VERIFY_LATER|SSL_DISPLAY_CERTS;
  381. int client_fd;
  382. char *private_key_file = NULL;
  383. struct sockaddr_in client_addr;
  384. struct hostent *hostent;
  385. int reconnect = 0;
  386. uint32_t sin_addr;
  387. SSL_CTX *ssl_ctx;
  388. SSL *ssl = NULL;
  389. int quiet = 0;
  390. int cert_index = 0, ca_cert_index = 0;
  391. int cert_size, ca_cert_size;
  392. char **ca_cert, **cert;
  393. uint8_t session_id[SSL_SESSION_ID_SIZE];
  394. fd_set read_set;
  395. const char *password = NULL;
  396. FD_ZERO(&read_set);
  397. sin_addr = inet_addr("127.0.0.1");
  398. cert_size = ssl_get_config(SSL_MAX_CERT_CFG_OFFSET);
  399. ca_cert_size = ssl_get_config(SSL_MAX_CA_CERT_CFG_OFFSET);
  400. ca_cert = (char **)calloc(1, sizeof(char *)*ca_cert_size);
  401. cert = (char **)calloc(1, sizeof(char *)*cert_size);
  402. while (i < argc)
  403. {
  404. if (strcmp(argv[i], "-connect") == 0)
  405. {
  406. char *host, *ptr;
  407. if (i >= argc-1)
  408. {
  409. print_client_options(argv[i]);
  410. }
  411. host = argv[++i];
  412. if ((ptr = strchr(host, ':')) == NULL)
  413. {
  414. print_client_options(argv[i]);
  415. }
  416. *ptr++ = 0;
  417. port = atoi(ptr);
  418. hostent = gethostbyname(host);
  419. if (hostent == NULL)
  420. {
  421. print_client_options(argv[i]);
  422. }
  423. sin_addr = *((uint32_t **)hostent->h_addr_list)[0];
  424. }
  425. else if (strcmp(argv[i], "-cert") == 0)
  426. {
  427. if (i >= argc-1 || cert_index >= cert_size)
  428. {
  429. print_client_options(argv[i]);
  430. }
  431. cert[cert_index++] = argv[++i];
  432. }
  433. else if (strcmp(argv[i], "-key") == 0)
  434. {
  435. if (i >= argc-1)
  436. {
  437. print_client_options(argv[i]);
  438. }
  439. private_key_file = argv[++i];
  440. options |= SSL_NO_DEFAULT_KEY;
  441. }
  442. else if (strcmp(argv[i], "-CAfile") == 0)
  443. {
  444. if (i >= argc-1 || ca_cert_index >= ca_cert_size)
  445. {
  446. print_client_options(argv[i]);
  447. }
  448. ca_cert[ca_cert_index++] = argv[++i];
  449. }
  450. else if (strcmp(argv[i], "-verify") == 0)
  451. {
  452. options &= ~SSL_SERVER_VERIFY_LATER;
  453. }
  454. else if (strcmp(argv[i], "-reconnect") == 0)
  455. {
  456. reconnect = 4;
  457. }
  458. else if (strcmp(argv[i], "-quiet") == 0)
  459. {
  460. quiet = 1;
  461. options &= ~SSL_DISPLAY_CERTS;
  462. }
  463. else if (strcmp(argv[i], "-pass") == 0)
  464. {
  465. if (i >= argc-1)
  466. {
  467. print_client_options(argv[i]);
  468. }
  469. password = argv[++i];
  470. }
  471. #ifdef CONFIG_SSL_FULL_MODE
  472. else if (strcmp(argv[i], "-debug") == 0)
  473. {
  474. options |= SSL_DISPLAY_BYTES;
  475. }
  476. else if (strcmp(argv[i], "-state") == 0)
  477. {
  478. options |= SSL_DISPLAY_STATES;
  479. }
  480. else if (strcmp(argv[i], "-show-rsa") == 0)
  481. {
  482. options |= SSL_DISPLAY_RSA;
  483. }
  484. #endif
  485. else /* don't know what this is */
  486. {
  487. print_client_options(argv[i]);
  488. }
  489. i++;
  490. }
  491. if ((ssl_ctx = ssl_ctx_new(options, SSL_DEFAULT_CLNT_SESS)) == NULL)
  492. {
  493. fprintf(stderr, "Error: Client context is invalid\n");
  494. exit(1);
  495. }
  496. if (private_key_file)
  497. {
  498. int obj_type = SSL_OBJ_RSA_KEY;
  499. /* auto-detect the key type from the file extension */
  500. if (strstr(private_key_file, ".p8"))
  501. obj_type = SSL_OBJ_PKCS8;
  502. else if (strstr(private_key_file, ".p12"))
  503. obj_type = SSL_OBJ_PKCS12;
  504. if (ssl_obj_load(ssl_ctx, obj_type, private_key_file, password))
  505. {
  506. fprintf(stderr, "Error: Private key '%s' is undefined.\n",
  507. private_key_file);
  508. exit(1);
  509. }
  510. }
  511. for (i = 0; i < cert_index; i++)
  512. {
  513. if (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CERT, cert[i], NULL))
  514. {
  515. printf("Certificate '%s' is undefined.\n", cert[i]);
  516. exit(1);
  517. }
  518. }
  519. for (i = 0; i < ca_cert_index; i++)
  520. {
  521. if (ssl_obj_load(ssl_ctx, SSL_OBJ_X509_CACERT, ca_cert[i], NULL))
  522. {
  523. printf("Certificate '%s' is undefined.\n", ca_cert[i]);
  524. exit(1);
  525. }
  526. }
  527. free(cert);
  528. free(ca_cert);
  529. /*************************************************************************
  530. * This is where the interesting stuff happens. Up until now we've
  531. * just been setting up sockets etc. Now we do the SSL handshake.
  532. *************************************************************************/
  533. client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  534. memset(&client_addr, 0, sizeof(client_addr));
  535. client_addr.sin_family = AF_INET;
  536. client_addr.sin_port = htons(port);
  537. client_addr.sin_addr.s_addr = sin_addr;
  538. if (connect(client_fd, (struct sockaddr *)&client_addr,
  539. sizeof(client_addr)) < 0)
  540. {
  541. perror("connect");
  542. exit(1);
  543. }
  544. if (!quiet)
  545. {
  546. printf("CONNECTED\n");
  547. TTY_FLUSH();
  548. }
  549. /* Try session resumption? */
  550. if (reconnect)
  551. {
  552. while (reconnect--)
  553. {
  554. ssl = ssl_client_new(ssl_ctx, client_fd, session_id,
  555. sizeof(session_id));
  556. if ((res = ssl_handshake_status(ssl)) != SSL_OK)
  557. {
  558. if (!quiet)
  559. {
  560. ssl_display_error(res);
  561. }
  562. ssl_free(ssl);
  563. exit(1);
  564. }
  565. display_session_id(ssl);
  566. memcpy(session_id, ssl_get_session_id(ssl), SSL_SESSION_ID_SIZE);
  567. if (reconnect)
  568. {
  569. ssl_free(ssl);
  570. SOCKET_CLOSE(client_fd);
  571. client_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  572. connect(client_fd, (struct sockaddr *)&client_addr,
  573. sizeof(client_addr));
  574. }
  575. }
  576. }
  577. else
  578. {
  579. ssl = ssl_client_new(ssl_ctx, client_fd, NULL, 0);
  580. }
  581. /* check the return status */
  582. if ((res = ssl_handshake_status(ssl)) != SSL_OK)
  583. {
  584. if (!quiet)
  585. {
  586. ssl_display_error(res);
  587. }
  588. exit(1);
  589. }
  590. if (!quiet)
  591. {
  592. const char *common_name = ssl_get_cert_dn(ssl,
  593. SSL_X509_CERT_COMMON_NAME);
  594. if (common_name)
  595. {
  596. printf("Common Name:\t\t\t%s\n", common_name);
  597. }
  598. display_session_id(ssl);
  599. display_cipher(ssl);
  600. }
  601. for (;;)
  602. {
  603. uint8_t buf[1024];
  604. res = SSL_OK;
  605. /* allow parallel reading of server and standard input */
  606. FD_SET(client_fd, &read_set);
  607. #ifndef WIN32
  608. /* win32 doesn't like mixing up stdin and sockets */
  609. FD_SET(STDIN_FILENO, &read_set);
  610. if ((res = select(client_fd+1, &read_set, NULL, NULL, NULL)) > 0)
  611. {
  612. /* read standard input? */
  613. if (FD_ISSET(STDIN_FILENO, &read_set))
  614. #endif
  615. {
  616. if (fgets((char *)buf, sizeof(buf), stdin) == NULL)
  617. {
  618. /* bomb out of here */
  619. ssl_free(ssl);
  620. break;
  621. }
  622. else
  623. {
  624. /* small hack to check renegotiation */
  625. if (buf[0] == 'R' && (buf[1] == '\n' || buf[1] == '\r'))
  626. {
  627. res = ssl_renegotiate(ssl);
  628. }
  629. else
  630. {
  631. res = ssl_write(ssl, buf, strlen((char *)buf)+1);
  632. }
  633. }
  634. }
  635. #ifndef WIN32
  636. else /* a socket read */
  637. {
  638. uint8_t *read_buf;
  639. res = ssl_read(ssl, &read_buf);
  640. if (res > 0) /* display our interesting output */
  641. {
  642. printf("%s", read_buf);
  643. TTY_FLUSH();
  644. }
  645. }
  646. }
  647. #endif
  648. if (res < 0)
  649. {
  650. if (!quiet)
  651. {
  652. ssl_display_error(res);
  653. }
  654. break; /* get outta here */
  655. }
  656. }
  657. ssl_ctx_free(ssl_ctx);
  658. SOCKET_CLOSE(client_fd);
  659. #else
  660. print_client_options(argv[1]);
  661. #endif
  662. }
  663. /**
  664. * We've had some sort of command-line error. Print out the basic options.
  665. */
  666. static void print_options(char *option)
  667. {
  668. printf("axssl: Error: '%s' is an invalid command.\n", option);
  669. printf("usage: axssl [s_server|s_client|version] [args ...]\n");
  670. exit(1);
  671. }
  672. /**
  673. * We've had some sort of command-line error. Print out the server options.
  674. */
  675. static void print_server_options(char *option)
  676. {
  677. #ifndef CONFIG_SSL_SKELETON_MODE
  678. int cert_size = ssl_get_config(SSL_MAX_CERT_CFG_OFFSET);
  679. #endif
  680. #ifdef CONFIG_SSL_CERT_VERIFICATION
  681. int ca_cert_size = ssl_get_config(SSL_MAX_CA_CERT_CFG_OFFSET);
  682. #endif
  683. printf("unknown option %s\n", option);
  684. printf("usage: s_server [args ...]\n");
  685. printf(" -accept arg\t- port to accept on (default is 4433)\n");
  686. #ifndef CONFIG_SSL_SKELETON_MODE
  687. printf(" -cert arg\t- certificate file to add (in addition to default)"
  688. " to chain -\n"
  689. "\t\t Can repeat up to %d times\n", cert_size);
  690. printf(" -key arg\t- Private key file to use\n");
  691. printf(" -pass\t\t- private key file pass phrase source\n");
  692. #endif
  693. printf(" -quiet\t\t- No server output\n");
  694. #ifdef CONFIG_SSL_CERT_VERIFICATION
  695. printf(" -verify\t- turn on peer certificate verification\n");
  696. printf(" -CAfile arg\t- Certificate authority\n");
  697. printf("\t\t Can repeat up to %d times\n", ca_cert_size);
  698. #endif
  699. #ifdef CONFIG_SSL_FULL_MODE
  700. printf(" -debug\t\t- Print more output\n");
  701. printf(" -state\t\t- Show state messages\n");
  702. printf(" -show-rsa\t- Show RSA state\n");
  703. #endif
  704. exit(1);
  705. }
  706. /**
  707. * We've had some sort of command-line error. Print out the client options.
  708. */
  709. static void print_client_options(char *option)
  710. {
  711. #ifdef CONFIG_SSL_ENABLE_CLIENT
  712. int cert_size = ssl_get_config(SSL_MAX_CERT_CFG_OFFSET);
  713. int ca_cert_size = ssl_get_config(SSL_MAX_CA_CERT_CFG_OFFSET);
  714. #endif
  715. printf("unknown option %s\n", option);
  716. #ifdef CONFIG_SSL_ENABLE_CLIENT
  717. printf("usage: s_client [args ...]\n");
  718. printf(" -connect host:port - who to connect to (default "
  719. "is localhost:4433)\n");
  720. printf(" -verify\t- turn on peer certificate verification\n");
  721. printf(" -cert arg\t- certificate file to use\n");
  722. printf("\t\t Can repeat up to %d times\n", cert_size);
  723. printf(" -key arg\t- Private key file to use\n");
  724. printf(" -CAfile arg\t- Certificate authority\n");
  725. printf("\t\t Can repeat up to %d times\n", ca_cert_size);
  726. printf(" -quiet\t\t- No client output\n");
  727. printf(" -reconnect\t- Drop and re-make the connection "
  728. "with the same Session-ID\n");
  729. printf(" -pass\t\t- private key file pass phrase source\n");
  730. #ifdef CONFIG_SSL_FULL_MODE
  731. printf(" -debug\t\t- Print more output\n");
  732. printf(" -state\t\t- Show state messages\n");
  733. printf(" -show-rsa\t- Show RSA state\n");
  734. #endif
  735. #else
  736. printf("Change configuration to allow this feature\n");
  737. #endif
  738. exit(1);
  739. }
  740. /**
  741. * Display what cipher we are using
  742. */
  743. static void display_cipher(SSL *ssl)
  744. {
  745. printf("CIPHER is ");
  746. switch (ssl_get_cipher_id(ssl))
  747. {
  748. case SSL_AES128_SHA:
  749. printf("AES128-SHA");
  750. break;
  751. case SSL_AES256_SHA:
  752. printf("AES256-SHA");
  753. break;
  754. case SSL_RC4_128_SHA:
  755. printf("RC4-SHA");
  756. break;
  757. case SSL_RC4_128_MD5:
  758. printf("RC4-MD5");
  759. break;
  760. default:
  761. printf("Unknown - %d", ssl_get_cipher_id(ssl));
  762. break;
  763. }
  764. printf("\n");
  765. TTY_FLUSH();
  766. }
  767. /**
  768. * Display what session id we have.
  769. */
  770. static void display_session_id(SSL *ssl)
  771. {
  772. int i;
  773. const uint8_t *session_id = ssl_get_session_id(ssl);
  774. int sess_id_size = ssl_get_session_id_size(ssl);
  775. if (sess_id_size > 0)
  776. {
  777. printf("-----BEGIN SSL SESSION PARAMETERS-----\n");
  778. for (i = 0; i < sess_id_size; i++)
  779. {
  780. printf("%02x", session_id[i]);
  781. }
  782. printf("\n-----END SSL SESSION PARAMETERS-----\n");
  783. TTY_FLUSH();
  784. }
  785. }