|
| 1 | +/* client-tls.c |
| 2 | + * |
| 3 | + * Copyright (C) 2006-2023 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 | +#include <wolfssl/test.h> |
| 37 | + |
| 38 | +#define DEFAULT_PORT 11111 |
| 39 | +#define ERR_MSG_LEN 80 |
| 40 | + |
| 41 | +#define CERT_FILE "../certs/ca-cert.pem" |
| 42 | + |
| 43 | + |
| 44 | + |
| 45 | +static void ShowCiphers(void) |
| 46 | +{ |
| 47 | + static char ciphers[WOLFSSL_CIPHER_LIST_MAX_SIZE]; |
| 48 | + int ret = wolfSSL_get_ciphers(ciphers, (int)sizeof(ciphers)); |
| 49 | + if (ret == WOLFSSL_SUCCESS) { |
| 50 | + printf("%s\n", ciphers); |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +int main(int argc, char** argv) |
| 56 | +{ |
| 57 | + int sockfd; |
| 58 | + struct sockaddr_in servAddr; |
| 59 | + char buff[256]; |
| 60 | + size_t len; |
| 61 | + int ret; |
| 62 | + int err; |
| 63 | + char buffer[ERR_MSG_LEN]; |
| 64 | + char* cipherList = NULL; |
| 65 | + |
| 66 | + /* declare wolfSSL objects */ |
| 67 | + WOLFSSL_CTX* ctx; |
| 68 | + WOLFSSL* ssl = NULL; |
| 69 | + |
| 70 | + |
| 71 | + |
| 72 | + /* Check for proper calling convention */ |
| 73 | + switch (argc) { |
| 74 | + case 2: |
| 75 | + if (XSTRCMP("-e", argv[1]) == 0) { |
| 76 | + ShowCiphers(); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + break; |
| 80 | + |
| 81 | + case 4: |
| 82 | + if (XSTRCMP("-l", argv[2]) == 0) { |
| 83 | + cipherList = argv[3]; |
| 84 | + } |
| 85 | + break; |
| 86 | + |
| 87 | + default: |
| 88 | + printf("usage:\n"); |
| 89 | + printf("\t%s <IPv4 address>\n", argv[0]); |
| 90 | + printf("\t%s -e\n", argv[0]); |
| 91 | + printf("\t%s <IPv4 address> -l <Cipher suite>\n", argv[0]); |
| 92 | + return 0; |
| 93 | + } |
| 94 | + |
| 95 | + /* Create a socket that uses an internet IPv4 address, |
| 96 | + * Sets the socket to be stream based (TCP), |
| 97 | + * 0 means choose the default protocol. */ |
| 98 | + if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -1) { |
| 99 | + ret = -1; |
| 100 | + err = wolfSSL_get_error(ssl, ret); |
| 101 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 102 | + goto end; |
| 103 | + } |
| 104 | + |
| 105 | + /* Initialize the server address struct with zeros */ |
| 106 | + memset(&servAddr, 0, sizeof(servAddr)); |
| 107 | + |
| 108 | + /* Fill in the server address */ |
| 109 | + servAddr.sin_family = AF_INET; /* using IPv4 */ |
| 110 | + servAddr.sin_port = htons(DEFAULT_PORT); /* on DEFAULT_PORT */ |
| 111 | + |
| 112 | + /* Get the server IPv4 address from the command line call */ |
| 113 | + if (inet_pton(AF_INET, argv[1], &servAddr.sin_addr) != 1) { |
| 114 | + ret = -1; |
| 115 | + err = wolfSSL_get_error(ssl, ret); |
| 116 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 117 | + goto end; |
| 118 | + } |
| 119 | + |
| 120 | + /* Connect to the server */ |
| 121 | + if ((ret = connect(sockfd, (struct sockaddr*) &servAddr, sizeof(servAddr))) |
| 122 | + == -1) { |
| 123 | + err = wolfSSL_get_error(ssl, ret); |
| 124 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 125 | + goto end; |
| 126 | + } |
| 127 | + |
| 128 | + /*---------------------------------*/ |
| 129 | + /* Start of wolfSSL initialization and configuration */ |
| 130 | + /*---------------------------------*/ |
| 131 | + /* Initialize wolfSSL */ |
| 132 | + if ((ret = wolfSSL_Init()) != WOLFSSL_SUCCESS) { |
| 133 | + err = wolfSSL_get_error(ssl, ret); |
| 134 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 135 | + goto socket_cleanup; |
| 136 | + } |
| 137 | + |
| 138 | + /* Create and initialize WOLFSSL_CTX */ |
| 139 | + if ((ctx = wolfSSL_CTX_new(wolfTLSv1_2_client_method())) == NULL) { |
| 140 | + ret = -1; |
| 141 | + err = wolfSSL_get_error(ssl, ret); |
| 142 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 143 | + goto socket_cleanup; |
| 144 | + } |
| 145 | + |
| 146 | + /* Set cipher suite */ |
| 147 | + if (cipherList != NULL) { |
| 148 | + if (wolfSSL_CTX_set_cipher_list(ctx, cipherList) != WOLFSSL_SUCCESS) { |
| 149 | + err = wolfSSL_get_error(ssl, ret); |
| 150 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 151 | + goto ctx_cleanup; |
| 152 | + } |
| 153 | + } |
| 154 | + |
| 155 | + /* Load client certificates into WOLFSSL_CTX */ |
| 156 | + if ((ret = wolfSSL_CTX_load_verify_locations(ctx, CERT_FILE, NULL)) |
| 157 | + != SSL_SUCCESS) { |
| 158 | + err = wolfSSL_get_error(ssl, ret); |
| 159 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 160 | + goto ctx_cleanup; |
| 161 | + } |
| 162 | + |
| 163 | + /* Create a WOLFSSL object */ |
| 164 | + if ((ssl = wolfSSL_new(ctx)) == NULL) { |
| 165 | + ret = -1; |
| 166 | + err = wolfSSL_get_error(ssl, ret); |
| 167 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 168 | + goto ctx_cleanup; |
| 169 | + } |
| 170 | + |
| 171 | + /* Attach wolfSSL to the socket */ |
| 172 | + if ((ret = wolfSSL_set_fd(ssl, sockfd)) != WOLFSSL_SUCCESS) { |
| 173 | + err = wolfSSL_get_error(ssl, ret); |
| 174 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 175 | + goto cleanup; |
| 176 | + } |
| 177 | + |
| 178 | + /* Connect to wolfSSL on the server side */ |
| 179 | + if ((ret = wolfSSL_connect(ssl)) != SSL_SUCCESS) { |
| 180 | + err = wolfSSL_get_error(ssl, ret); |
| 181 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 182 | + goto cleanup; |
| 183 | + } |
| 184 | + |
| 185 | + /* Show Description of cipher suite */ |
| 186 | + static int lng_index = 0; |
| 187 | + showPeerEx(ssl, lng_index); |
| 188 | + |
| 189 | + /* Get a message for the server from stdin */ |
| 190 | + printf("Message for server: "); |
| 191 | + memset(buff, 0, sizeof(buff)); |
| 192 | + if (fgets(buff, sizeof(buff), stdin) == NULL) { |
| 193 | + ret = -1; |
| 194 | + err = wolfSSL_get_error(ssl, ret); |
| 195 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 196 | + goto cleanup; |
| 197 | + } |
| 198 | + len = strnlen(buff, sizeof(buff)); |
| 199 | + |
| 200 | + /* Send the message to the server */ |
| 201 | + if ((ret = wolfSSL_write(ssl, buff, len)) != len) { |
| 202 | + err = wolfSSL_get_error(ssl, ret); |
| 203 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 204 | + goto cleanup; |
| 205 | + } |
| 206 | + |
| 207 | + /* Read the server data into our buff array */ |
| 208 | + memset(buff, 0, sizeof(buff)); |
| 209 | + if ((ret = wolfSSL_read(ssl, buff, sizeof(buff)-1)) == -1) { |
| 210 | + err = wolfSSL_get_error(ssl, ret); |
| 211 | + printf("error = %d, %s\n", err, wolfSSL_ERR_error_string(err, buffer)); |
| 212 | + goto cleanup; |
| 213 | + } |
| 214 | + |
| 215 | + /* Print to stdout any data the server sends */ |
| 216 | + printf("Server: %s\n", buff); |
| 217 | + |
| 218 | + /* Bidirectional shutdown */ |
| 219 | + while (wolfSSL_shutdown(ssl) == SSL_SHUTDOWN_NOT_DONE) { |
| 220 | + printf("Shutdown not complete\n"); |
| 221 | + } |
| 222 | + |
| 223 | + printf("Shutdown complete\n"); |
| 224 | + |
| 225 | + ret = 0; |
| 226 | + |
| 227 | + /* Cleanup and return */ |
| 228 | +cleanup: |
| 229 | + wolfSSL_free(ssl); /* Free the wolfSSL object */ |
| 230 | +ctx_cleanup: |
| 231 | + wolfSSL_CTX_free(ctx); /* Free the wolfSSL context object */ |
| 232 | + wolfSSL_Cleanup(); /* Cleanup the wolfSSL environment */ |
| 233 | +socket_cleanup: |
| 234 | + close(sockfd); /* Close the connection to the server */ |
| 235 | +end: |
| 236 | + return ret; /* Return reporting a success */ |
| 237 | +} |
0 commit comments