main.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  1. /*
  2. * Copyright 2022-2024 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <stdio.h>
  10. #include <string.h>
  11. #include <signal.h>
  12. #include <openssl/ssl.h>
  13. #include <openssl/err.h>
  14. #if !defined(OPENSSL_SYS_WINDOWS)
  15. #include <unistd.h>
  16. #include <sys/socket.h>
  17. #include <arpa/inet.h>
  18. #include <netinet/in.h>
  19. #else
  20. #include <winsock.h>
  21. #endif
  22. static const int server_port = 4433;
  23. typedef unsigned char bool;
  24. #define true 1
  25. #define false 0
  26. /*
  27. * This flag won't be useful until both accept/read (TCP & SSL) methods
  28. * can be called with a timeout. TBD.
  29. */
  30. static volatile bool server_running = true;
  31. static int create_socket(bool isServer)
  32. {
  33. int s;
  34. int optval = 1;
  35. struct sockaddr_in addr;
  36. s = socket(AF_INET, SOCK_STREAM, 0);
  37. if (s < 0) {
  38. perror("Unable to create socket");
  39. exit(EXIT_FAILURE);
  40. }
  41. if (isServer) {
  42. addr.sin_family = AF_INET;
  43. addr.sin_port = htons(server_port);
  44. addr.sin_addr.s_addr = INADDR_ANY;
  45. /* Reuse the address; good for quick restarts */
  46. if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &optval, sizeof(optval))
  47. < 0) {
  48. perror("setsockopt(SO_REUSEADDR) failed");
  49. exit(EXIT_FAILURE);
  50. }
  51. if (bind(s, (struct sockaddr*) &addr, sizeof(addr)) < 0) {
  52. perror("Unable to bind");
  53. exit(EXIT_FAILURE);
  54. }
  55. if (listen(s, 1) < 0) {
  56. perror("Unable to listen");
  57. exit(EXIT_FAILURE);
  58. }
  59. }
  60. return s;
  61. }
  62. static SSL_CTX* create_context(bool isServer)
  63. {
  64. const SSL_METHOD *method;
  65. SSL_CTX *ctx;
  66. if (isServer)
  67. method = TLS_server_method();
  68. else
  69. method = TLS_client_method();
  70. ctx = SSL_CTX_new(method);
  71. if (ctx == NULL) {
  72. perror("Unable to create SSL context");
  73. ERR_print_errors_fp(stderr);
  74. exit(EXIT_FAILURE);
  75. }
  76. return ctx;
  77. }
  78. static void configure_server_context(SSL_CTX *ctx)
  79. {
  80. /* Set the key and cert */
  81. if (SSL_CTX_use_certificate_chain_file(ctx, "cert.pem") <= 0) {
  82. ERR_print_errors_fp(stderr);
  83. exit(EXIT_FAILURE);
  84. }
  85. if (SSL_CTX_use_PrivateKey_file(ctx, "key.pem", SSL_FILETYPE_PEM) <= 0) {
  86. ERR_print_errors_fp(stderr);
  87. exit(EXIT_FAILURE);
  88. }
  89. }
  90. static void configure_client_context(SSL_CTX *ctx)
  91. {
  92. /*
  93. * Configure the client to abort the handshake if certificate verification
  94. * fails
  95. */
  96. SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, NULL);
  97. /*
  98. * In a real application you would probably just use the default system certificate trust store and call:
  99. * SSL_CTX_set_default_verify_paths(ctx);
  100. * In this demo though we are using a self-signed certificate, so the client must trust it directly.
  101. */
  102. if (!SSL_CTX_load_verify_locations(ctx, "cert.pem", NULL)) {
  103. ERR_print_errors_fp(stderr);
  104. exit(EXIT_FAILURE);
  105. }
  106. }
  107. static void usage(void)
  108. {
  109. printf("Usage: sslecho s\n");
  110. printf(" --or--\n");
  111. printf(" sslecho c ip\n");
  112. printf(" c=client, s=server, ip=dotted ip of server\n");
  113. exit(EXIT_FAILURE);
  114. }
  115. #define BUFFERSIZE 1024
  116. int main(int argc, char **argv)
  117. {
  118. bool isServer;
  119. int result;
  120. SSL_CTX *ssl_ctx = NULL;
  121. SSL *ssl = NULL;
  122. int server_skt = -1;
  123. int client_skt = -1;
  124. /* used by fgets */
  125. char buffer[BUFFERSIZE];
  126. char *txbuf;
  127. char rxbuf[128];
  128. size_t rxcap = sizeof(rxbuf);
  129. int rxlen;
  130. char *rem_server_ip = NULL;
  131. struct sockaddr_in addr;
  132. #if defined(OPENSSL_SYS_CYGWIN) || defined(OPENSSL_SYS_WINDOWS)
  133. int addr_len = sizeof(addr);
  134. #else
  135. unsigned int addr_len = sizeof(addr);
  136. #endif
  137. #if !defined (OPENSSL_SYS_WINDOWS)
  138. /* ignore SIGPIPE so that server can continue running when client pipe closes abruptly */
  139. signal(SIGPIPE, SIG_IGN);
  140. #endif
  141. /* Splash */
  142. printf("\nsslecho : Simple Echo Client/Server : %s : %s\n\n", __DATE__,
  143. __TIME__);
  144. /* Need to know if client or server */
  145. if (argc < 2) {
  146. usage();
  147. /* NOTREACHED */
  148. }
  149. isServer = (argv[1][0] == 's') ? true : false;
  150. /* If client get remote server address (could be 127.0.0.1) */
  151. if (!isServer) {
  152. if (argc != 3) {
  153. usage();
  154. /* NOTREACHED */
  155. }
  156. rem_server_ip = argv[2];
  157. }
  158. /* Create context used by both client and server */
  159. ssl_ctx = create_context(isServer);
  160. /* If server */
  161. if (isServer) {
  162. printf("We are the server on port: %d\n\n", server_port);
  163. /* Configure server context with appropriate key files */
  164. configure_server_context(ssl_ctx);
  165. /* Create server socket; will bind with server port and listen */
  166. server_skt = create_socket(true);
  167. /*
  168. * Loop to accept clients.
  169. * Need to implement timeouts on TCP & SSL connect/read functions
  170. * before we can catch a CTRL-C and kill the server.
  171. */
  172. while (server_running) {
  173. /* Wait for TCP connection from client */
  174. client_skt = accept(server_skt, (struct sockaddr*) &addr,
  175. &addr_len);
  176. if (client_skt < 0) {
  177. perror("Unable to accept");
  178. exit(EXIT_FAILURE);
  179. }
  180. printf("Client TCP connection accepted\n");
  181. /* Create server SSL structure using newly accepted client socket */
  182. ssl = SSL_new(ssl_ctx);
  183. if (!SSL_set_fd(ssl, client_skt)) {
  184. ERR_print_errors_fp(stderr);
  185. exit(EXIT_FAILURE);
  186. }
  187. /* Wait for SSL connection from the client */
  188. if (SSL_accept(ssl) <= 0) {
  189. ERR_print_errors_fp(stderr);
  190. server_running = false;
  191. } else {
  192. printf("Client SSL connection accepted\n\n");
  193. /* Echo loop */
  194. while (true) {
  195. /* Get message from client; will fail if client closes connection */
  196. if ((rxlen = SSL_read(ssl, rxbuf, rxcap)) <= 0) {
  197. if (rxlen == 0) {
  198. printf("Client closed connection\n");
  199. } else {
  200. printf("SSL_read returned %d\n", rxlen);
  201. }
  202. ERR_print_errors_fp(stderr);
  203. break;
  204. }
  205. /* Insure null terminated input */
  206. rxbuf[rxlen] = 0;
  207. /* Look for kill switch */
  208. if (strcmp(rxbuf, "kill\n") == 0) {
  209. /* Terminate...with extreme prejudice */
  210. printf("Server received 'kill' command\n");
  211. server_running = false;
  212. break;
  213. }
  214. /* Show received message */
  215. printf("Received: %s", rxbuf);
  216. /* Echo it back */
  217. if (SSL_write(ssl, rxbuf, rxlen) <= 0) {
  218. ERR_print_errors_fp(stderr);
  219. }
  220. }
  221. }
  222. if (server_running) {
  223. /* Cleanup for next client */
  224. SSL_shutdown(ssl);
  225. SSL_free(ssl);
  226. close(client_skt);
  227. }
  228. }
  229. printf("Server exiting...\n");
  230. }
  231. /* Else client */
  232. else {
  233. printf("We are the client\n\n");
  234. /* Configure client context so we verify the server correctly */
  235. configure_client_context(ssl_ctx);
  236. /* Create "bare" socket */
  237. client_skt = create_socket(false);
  238. /* Set up connect address */
  239. addr.sin_family = AF_INET;
  240. inet_pton(AF_INET, rem_server_ip, &addr.sin_addr.s_addr);
  241. addr.sin_port = htons(server_port);
  242. /* Do TCP connect with server */
  243. if (connect(client_skt, (struct sockaddr*) &addr, sizeof(addr)) != 0) {
  244. perror("Unable to TCP connect to server");
  245. goto exit;
  246. } else {
  247. printf("TCP connection to server successful\n");
  248. }
  249. /* Create client SSL structure using dedicated client socket */
  250. ssl = SSL_new(ssl_ctx);
  251. if (!SSL_set_fd(ssl, client_skt)) {
  252. ERR_print_errors_fp(stderr);
  253. goto exit;
  254. }
  255. /* Set hostname for SNI */
  256. SSL_set_tlsext_host_name(ssl, rem_server_ip);
  257. /* Configure server hostname check */
  258. if (!SSL_set1_host(ssl, rem_server_ip)) {
  259. ERR_print_errors_fp(stderr);
  260. goto exit;
  261. }
  262. /* Now do SSL connect with server */
  263. if (SSL_connect(ssl) == 1) {
  264. printf("SSL connection to server successful\n\n");
  265. /* Loop to send input from keyboard */
  266. while (true) {
  267. /* Get a line of input */
  268. memset(buffer, 0, BUFFERSIZE);
  269. txbuf = fgets(buffer, BUFFERSIZE, stdin);
  270. /* Exit loop on error */
  271. if (txbuf == NULL) {
  272. break;
  273. }
  274. /* Exit loop if just a carriage return */
  275. if (txbuf[0] == '\n') {
  276. break;
  277. }
  278. /* Send it to the server */
  279. if ((result = SSL_write(ssl, txbuf, strlen(txbuf))) <= 0) {
  280. printf("Server closed connection\n");
  281. ERR_print_errors_fp(stderr);
  282. break;
  283. }
  284. /* Wait for the echo */
  285. rxlen = SSL_read(ssl, rxbuf, rxcap);
  286. if (rxlen <= 0) {
  287. printf("Server closed connection\n");
  288. ERR_print_errors_fp(stderr);
  289. break;
  290. } else {
  291. /* Show it */
  292. rxbuf[rxlen] = 0;
  293. printf("Received: %s", rxbuf);
  294. }
  295. }
  296. printf("Client exiting...\n");
  297. } else {
  298. printf("SSL connection to server failed\n\n");
  299. ERR_print_errors_fp(stderr);
  300. }
  301. }
  302. exit:
  303. /* Close up */
  304. if (ssl != NULL) {
  305. SSL_shutdown(ssl);
  306. SSL_free(ssl);
  307. }
  308. SSL_CTX_free(ssl_ctx);
  309. if (client_skt != -1)
  310. close(client_skt);
  311. if (server_skt != -1)
  312. close(server_skt);
  313. printf("sslecho exiting\n");
  314. return EXIT_SUCCESS;
  315. }