main.c 9.6 KB

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