main.c 10 KB

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