|
| 1 | +/* server-tls-writedup.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2022 wolfSSL Inc. |
| 4 | + * |
| 5 | + * This file is part of wolfSSL. (formerly known as CyaSSL) |
| 6 | + * |
| 7 | + * wolfSSL 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 | + * wolfSSL 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA |
| 20 | + */ |
| 21 | + |
| 22 | +/* the usual suspects */ |
| 23 | +#include <stdlib.h> |
| 24 | +#include <stdio.h> |
| 25 | +#include <string.h> |
| 26 | + |
| 27 | +/* socket includes */ |
| 28 | +#include <sys/socket.h> |
| 29 | +#include <arpa/inet.h> |
| 30 | +#include <netinet/in.h> |
| 31 | +#include <unistd.h> |
| 32 | + |
| 33 | +/* wolfSSL */ |
| 34 | +#include <wolfssl/options.h> |
| 35 | +#include <wolfssl/ssl.h> |
| 36 | + |
| 37 | +#define DEFAULT_PORT 11111 |
| 38 | + |
| 39 | +#define CERT_FILE "../certs/server-cert.pem" |
| 40 | +#define KEY_FILE "../certs/server-key.pem" |
| 41 | + |
| 42 | + |
| 43 | + |
| 44 | +int main() |
| 45 | +{ |
| 46 | + int ret; |
| 47 | +#ifdef HAVE_WRITE_DUP |
| 48 | + int sockfd = SOCKET_INVALID; |
| 49 | + int connd = SOCKET_INVALID; |
| 50 | + struct sockaddr_in servAddr; |
| 51 | + struct sockaddr_in clientAddr; |
| 52 | + socklen_t size = sizeof(clientAddr); |
| 53 | + char buff[256]; |
| 54 | + size_t len; |
| 55 | + int shutdown = 0; |
| 56 | + const char* reply = "I hear ya fa shizzle!\n"; |
| 57 | + |
| 58 | + /* declare wolfSSL objects */ |
| 59 | + WOLFSSL_CTX* ctx = NULL; |
| 60 | + WOLFSSL *ssl = NULL, *write_ssl = NULL; |
| 61 | + |
| 62 | +#ifdef DEBUG_WOLFSSL |
| 63 | + wolfSSL_Debugging_ON(); |
| 64 | +#endif |
| 65 | + |
| 66 | + /* Initialize wolfSSL */ |
| 67 | + wolfSSL_Init(); |
| 68 | + |
| 69 | + |
| 70 | + /* Create a socket that uses an internet IPv4 address, |
| 71 | + * Sets the socket to be stream based (TCP), |
| 72 | + * 0 means choose the default protocol. */ |
| 73 | + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 74 | + fprintf(stderr, "ERROR: failed to create the socket\n"); |
| 75 | + ret = -1; |
| 76 | + goto exit; |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | + /* Create and initialize WOLFSSL_CTX */ |
| 81 | + if ((ctx = wolfSSL_CTX_new(wolfSSLv23_method())) == NULL) { |
| 82 | + fprintf(stderr, "ERROR: failed to create WOLFSSL_CTX\n"); |
| 83 | + ret = -1; |
| 84 | + goto exit; |
| 85 | + } |
| 86 | + |
| 87 | + /* Load server certificates into WOLFSSL_CTX */ |
| 88 | + if ((ret = wolfSSL_CTX_use_certificate_file(ctx, CERT_FILE, SSL_FILETYPE_PEM)) |
| 89 | + != WOLFSSL_SUCCESS) { |
| 90 | + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", |
| 91 | + CERT_FILE); |
| 92 | + goto exit; |
| 93 | + } |
| 94 | + |
| 95 | + /* Load server key into WOLFSSL_CTX */ |
| 96 | + if ((ret = wolfSSL_CTX_use_PrivateKey_file(ctx, KEY_FILE, SSL_FILETYPE_PEM)) |
| 97 | + != WOLFSSL_SUCCESS) { |
| 98 | + fprintf(stderr, "ERROR: failed to load %s, please check the file.\n", |
| 99 | + KEY_FILE); |
| 100 | + goto exit; |
| 101 | + } |
| 102 | + |
| 103 | + /* Set the protocol min / max */ |
| 104 | + wolfSSL_CTX_set_min_proto_version(ctx, TLS1_2_VERSION); |
| 105 | + wolfSSL_CTX_set_max_proto_version(ctx, TLS1_3_VERSION); |
| 106 | + |
| 107 | + /* Initialize the server address struct with zeros */ |
| 108 | + memset(&servAddr, 0, sizeof(servAddr)); |
| 109 | + |
| 110 | + /* Fill in the server address */ |
| 111 | + servAddr.sin_family = AF_INET; /* using IPv4 */ |
| 112 | + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ |
| 113 | + servAddr.sin_addr.s_addr = INADDR_ANY; /* from anywhere */ |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | + /* Bind the server socket to our port */ |
| 118 | + if (bind(sockfd, (struct sockaddr*)&servAddr, sizeof(servAddr)) == -1) { |
| 119 | + fprintf(stderr, "ERROR: failed to bind\n"); |
| 120 | + ret = -1; |
| 121 | + goto exit; |
| 122 | + } |
| 123 | + |
| 124 | + /* Listen for a new connection, allow 5 pending connections */ |
| 125 | + if (listen(sockfd, 5) == -1) { |
| 126 | + fprintf(stderr, "ERROR: failed to listen\n"); |
| 127 | + ret = -1; |
| 128 | + goto exit; |
| 129 | + } |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + /* Continue to accept clients until shutdown is issued */ |
| 134 | + while (!shutdown) { |
| 135 | + printf("Waiting for a connection...\n"); |
| 136 | + |
| 137 | + /* Accept client connections */ |
| 138 | + if ((connd = accept(sockfd, (struct sockaddr*)&clientAddr, &size)) |
| 139 | + == -1) { |
| 140 | + fprintf(stderr, "ERROR: failed to accept the connection\n\n"); |
| 141 | + ret = -1; |
| 142 | + goto exit; |
| 143 | + } |
| 144 | + |
| 145 | + /* Create a WOLFSSL object */ |
| 146 | + if ((ssl = wolfSSL_new(ctx)) == NULL) { |
| 147 | + fprintf(stderr, "ERROR: failed to create WOLFSSL object\n"); |
| 148 | + ret = -1; |
| 149 | + goto exit; |
| 150 | + } |
| 151 | + |
| 152 | + /* Attach wolfSSL to the socket */ |
| 153 | + wolfSSL_set_fd(ssl, connd); |
| 154 | + |
| 155 | + /* Establish TLS connection */ |
| 156 | + ret = wolfSSL_accept(ssl); |
| 157 | + if (ret != WOLFSSL_SUCCESS) { |
| 158 | + fprintf(stderr, "wolfSSL_accept error = %d\n", |
| 159 | + wolfSSL_get_error(ssl, ret)); |
| 160 | + goto exit; |
| 161 | + } |
| 162 | + |
| 163 | + |
| 164 | + printf("Client connected successfully\n"); |
| 165 | + |
| 166 | + |
| 167 | + |
| 168 | + write_ssl = wolfSSL_write_dup(ssl); |
| 169 | + if (write_ssl == NULL) { |
| 170 | + fprintf(stderr, "wolfSSL_write_dup failed\n"); |
| 171 | + ret = -1; |
| 172 | + goto exit; |
| 173 | + } |
| 174 | + |
| 175 | + { |
| 176 | + int w_ver = wolfSSL_version(write_ssl); |
| 177 | + int r_ver = wolfSSL_version(ssl); |
| 178 | + if (w_ver != r_ver) { |
| 179 | + fprintf(stderr, "ERROR: SSL versions are different\n"); |
| 180 | + goto exit; |
| 181 | + } |
| 182 | + } |
| 183 | + |
| 184 | + /* Read the client data into our buff array */ |
| 185 | + memset(buff, 0, sizeof(buff)); |
| 186 | + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { |
| 187 | + fprintf(stderr, "ERROR: failed to read\n"); |
| 188 | + goto exit; |
| 189 | + } |
| 190 | + |
| 191 | + /* Print to stdout any data the client sends */ |
| 192 | + printf("Client: %s\n", buff); |
| 193 | + |
| 194 | + /* Check for server shutdown command */ |
| 195 | + if (strncmp(buff, "shutdown", 8) == 0) { |
| 196 | + printf("Shutdown command issued!\n"); |
| 197 | + shutdown = 1; |
| 198 | + } |
| 199 | + |
| 200 | + /* Write our reply into buff */ |
| 201 | + memset(buff, 0, sizeof(buff)); |
| 202 | + memcpy(buff, reply, strlen(reply)); |
| 203 | + len = strnlen(buff, sizeof(buff)); |
| 204 | + |
| 205 | + /* Reply back to the client */ |
| 206 | + if ((ret = wolfSSL_write(write_ssl, buff, len)) != len) { |
| 207 | + fprintf(stderr, "ERROR: failed to write\n"); |
| 208 | + goto exit; |
| 209 | + } |
| 210 | + |
| 211 | + /* Notify the client that the connection is ending */ |
| 212 | + wolfSSL_shutdown(write_ssl); |
| 213 | + wolfSSL_shutdown(ssl); |
| 214 | + printf("Shutdown complete\n"); |
| 215 | + |
| 216 | + /* Cleanup after this connection */ |
| 217 | + wolfSSL_free(write_ssl);/* Free the dup wolfSSL object */ |
| 218 | + write_ssl = NULL; |
| 219 | + wolfSSL_free(ssl); /* Free the wolfSSL object */ |
| 220 | + ssl = NULL; |
| 221 | + close(connd); /* Close the connection to the client */ |
| 222 | + } |
| 223 | + |
| 224 | + ret = 0; |
| 225 | + |
| 226 | +exit: |
| 227 | + /* Cleanup and return */ |
| 228 | + if (write_ssl) { |
| 229 | + wolfSSL_free(write_ssl);/* Free the dup wolfSSL object */ |
| 230 | + write_ssl = NULL; |
| 231 | + } |
| 232 | + if (ssl) |
| 233 | + wolfSSL_free(ssl); /* Free the wolfSSL object */ |
| 234 | + if (connd != SOCKET_INVALID) |
| 235 | + close(connd); /* Close the connection to the client */ |
| 236 | + if (sockfd != SOCKET_INVALID) |
| 237 | + close(sockfd); /* Close the socket listening for clients */ |
| 238 | + if (ctx) |
| 239 | + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ |
| 240 | + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ |
| 241 | +#else |
| 242 | + printf("wolfSSL not configured with --enable-writedup.\n" |
| 243 | + "Please re-configure and re-install wolfSSL to try out" |
| 244 | + "this example!\n"); |
| 245 | +#endif |
| 246 | + return ret; /* Return reporting a success */ |
| 247 | +} |
0 commit comments