Files
wolfssl/examples/echoclient/echoclient.c
T

342 lines
9.3 KiB
C
Raw Normal View History

2011-07-26 13:27:22 -07:00
/* echoclient.c
*
2026-02-18 09:52:21 -07:00
* Copyright (C) 2006-2026 wolfSSL Inc.
2011-07-26 13:27:22 -07:00
*
2016-03-17 16:02:13 -06:00
* This file is part of wolfSSL.
2011-07-26 13:27:22 -07:00
*
2015-01-06 12:14:15 -07:00
* wolfSSL is free software; you can redistribute it and/or modify
2011-07-26 13:27:22 -07:00
* it under the terms of the GNU General Public License as published by
2025-07-10 16:01:52 -06:00
* the Free Software Foundation; either version 3 of the License, or
2011-07-26 13:27:22 -07:00
* (at your option) any later version.
*
2015-01-06 12:14:15 -07:00
* wolfSSL is distributed in the hope that it will be useful,
2011-07-26 13:27:22 -07:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
2016-03-17 16:02:13 -06:00
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
2011-07-26 13:27:22 -07:00
*/
2011-02-05 11:14:47 -08:00
2016-03-17 16:02:13 -06:00
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifndef WOLFSSL_USER_SETTINGS
#include <wolfssl/options.h>
#endif
2024-12-06 16:35:21 -06:00
#include <wolfssl/wolfcrypt/settings.h>
/* Force enable the compatibility macros for this example */
#undef TEST_OPENSSL_COEXIST
#undef OPENSSL_COEXIST
#ifndef OPENSSL_EXTRA_X509_SMALL
#define OPENSSL_EXTRA_X509_SMALL
#endif
2023-08-01 10:17:38 -04:00
#include <wolfssl/ssl.h>
2015-10-08 15:39:14 +09:00
#if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
2014-04-11 16:20:12 +09:00
#include <stdio.h>
#include <string.h>
2018-01-01 11:57:31 +09:00
#include "cmsis_os.h"
#include "rl_fs.h"
#include "rl_net.h"
#include "wolfssl_MDK_ARM.h"
2013-05-16 09:47:27 -07:00
#endif
2023-08-01 10:17:38 -04:00
#include <wolfssl/test.h>
2011-02-05 11:14:47 -08:00
2023-08-01 10:17:38 -04:00
#include <wolfssl/openssl/ssl.h>
#include <examples/echoclient/echoclient.h>
2011-02-05 11:14:47 -08:00
#if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
2020-10-09 09:45:00 -07:00
#ifdef NO_FILESYSTEM
#ifdef NO_RSA
#error currently the example only tries to load in a RSA buffer
#endif
#undef USE_CERT_BUFFERS_2048
#define USE_CERT_BUFFERS_2048
#include <wolfssl/certs_test.h>
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
static int devId = INVALID_DEVID;
#endif
2011-02-05 11:14:47 -08:00
void echoclient_test(void* args)
{
SOCKET_T sockfd = 0;
2013-05-16 09:47:27 -07:00
FILE* fin = stdin ;
2011-02-05 11:14:47 -08:00
FILE* fout = stdout;
2020-04-08 09:46:22 +10:00
#ifndef WOLFSSL_MDK_SHELL
2011-02-05 11:14:47 -08:00
int inCreated = 0;
int outCreated = 0;
2020-04-08 09:46:22 +10:00
#endif
2011-02-05 11:14:47 -08:00
2012-09-20 15:39:15 -07:00
char msg[1024];
char reply[1024+1];
2011-02-05 11:14:47 -08:00
SSL_METHOD* method = 0;
SSL_CTX* ctx = 0;
SSL* ssl = 0;
int ret = 0, err = 0;
2013-03-11 13:19:43 -07:00
int doPSK = 0;
2011-02-05 11:14:47 -08:00
int sendSz;
2020-01-15 22:15:38 +10:00
#ifndef WOLFSSL_MDK_SHELL
2011-02-05 11:14:47 -08:00
int argc = 0;
char** argv = 0;
2020-01-15 22:15:38 +10:00
#endif
2021-05-19 11:08:40 +10:00
word16 port;
2023-07-31 15:36:38 -04:00
char buffer[WOLFSSL_MAX_ERROR_SZ];
2011-02-05 11:14:47 -08:00
((func_args*)args)->return_code = -1; /* error state */
2015-08-12 16:45:40 +09:00
#ifndef WOLFSSL_MDK_SHELL
2011-02-05 11:14:47 -08:00
argc = ((func_args*)args)->argc;
argv = ((func_args*)args)->argv;
if (argc >= 2) {
fin = fopen(argv[1], "r");
2011-02-05 11:14:47 -08:00
inCreated = 1;
}
if (argc >= 3) {
fout = fopen(argv[2], "w");
outCreated = 1;
}
2020-01-15 22:15:38 +10:00
#endif
2011-02-05 11:14:47 -08:00
if (!fin) err_sys("can't open input file");
if (!fout) err_sys("can't open output file");
2023-07-31 15:36:38 -04:00
#ifdef WOLFSSL_LEANPSK
2013-03-11 13:19:43 -07:00
doPSK = 1;
#endif
2020-02-19 18:07:45 +10:00
#if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
!defined(HAVE_ED448)
2013-03-11 13:19:43 -07:00
doPSK = 1;
2012-10-30 12:51:14 -07:00
#endif
2020-04-08 09:46:22 +10:00
(void)doPSK;
2012-10-30 12:51:14 -07:00
2015-08-12 16:45:40 +09:00
#if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_SHELL)
port = ((func_args*)args)->signal->port;
2021-05-19 11:08:40 +10:00
#else
2023-08-01 10:17:38 -04:00
port = wolfSSLPort;
#endif
#if !defined(NO_TLS)
2020-06-04 16:42:40 -07:00
#if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SNIFFER)
2023-08-01 13:40:52 -04:00
method = wolfTLSv1_2_client_method();
2020-06-04 16:42:40 -07:00
#else
2023-08-01 10:17:38 -04:00
method = wolfSSLv23_client_method();
2020-06-04 16:42:40 -07:00
#endif
2015-08-12 16:39:13 -07:00
#elif defined(WOLFSSL_ALLOW_SSLV3)
2011-02-05 11:14:47 -08:00
method = SSLv3_client_method();
2015-08-12 16:39:13 -07:00
#else
#error "no valid client method type"
2011-02-05 11:14:47 -08:00
#endif
ctx = SSL_CTX_new(method);
#ifndef NO_FILESYSTEM
2013-03-07 18:10:18 -08:00
#ifndef NO_RSA
if (SSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
2015-01-20 12:36:20 -07:00
err_sys("can't load ca file, Please run from wolfSSL home dir");
2013-03-07 18:10:18 -08:00
#endif
2011-02-05 11:14:47 -08:00
#ifdef HAVE_ECC
if (SSL_CTX_load_verify_locations(ctx, caEccCertFile, 0) != WOLFSSL_SUCCESS)
2015-01-20 12:36:20 -07:00
err_sys("can't load ca file, Please run from wolfSSL home dir");
#elif defined(HAVE_ED25519)
if (SSL_CTX_load_verify_locations(ctx, caEdCertFile, 0) != WOLFSSL_SUCCESS)
err_sys("can't load ca file, Please run from wolfSSL home dir");
2020-02-19 18:07:45 +10:00
#elif defined(HAVE_ED448)
if (SSL_CTX_load_verify_locations(ctx, caEd448CertFile, 0) != WOLFSSL_SUCCESS)
err_sys("can't load ca file, Please run from wolfSSL home dir");
2011-02-05 11:14:47 -08:00
#endif
#elif !defined(NO_CERTS)
if (!doPSK)
2020-10-09 09:45:00 -07:00
if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,
sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
err_sys("can't load ca buffer");
2011-02-05 11:14:47 -08:00
#endif
2023-07-31 15:36:38 -04:00
#if defined(WOLFSSL_SNIFFER)
/* Only set if not running testsuite */
if (XSTRSTR(argv[0], "testsuite") == NULL) {
/* don't use EDH, can't sniff tmp keys */
SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
}
#endif
2013-03-11 13:19:43 -07:00
#ifndef NO_PSK
2020-04-08 09:46:22 +10:00
if (doPSK) {
2013-03-11 13:19:43 -07:00
const char *defaultCipherList;
2023-08-01 10:17:38 -04:00
wolfSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
2013-03-11 13:19:43 -07:00
#ifdef HAVE_NULL_CIPHER
defaultCipherList = "PSK-NULL-SHA256";
2015-08-14 12:49:30 -07:00
#elif defined(HAVE_AESGCM) && !defined(NO_DH)
#ifdef WOLFSSL_TLS13
2020-10-28 11:47:31 +10:00
defaultCipherList = "TLS13-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":DHE-PSK-AES128-GCM-SHA256"
#endif
;
#else
2015-08-14 12:49:30 -07:00
defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
#endif
2020-10-28 11:47:31 +10:00
#elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
defaultCipherList = "TLS13-AES128-GCM-SHA256"
#ifndef WOLFSSL_NO_TLS12
":DHE-PSK-AES128-GCM-SHA256"
#endif
;
2013-03-11 13:19:43 -07:00
#else
defaultCipherList = "PSK-AES128-CBC-SHA256";
#endif
2023-08-01 10:17:38 -04:00
if (wolfSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=WOLFSSL_SUCCESS)
2013-03-11 13:19:43 -07:00
err_sys("client can't set cipher list 2");
wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
2012-10-30 12:51:14 -07:00
}
2020-04-08 09:46:22 +10:00
#endif
#ifdef WOLFSSL_ENCRYPTED_KEYS
2011-02-05 11:14:47 -08:00
SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
#endif
2013-05-16 09:47:27 -07:00
#if defined(WOLFSSL_MDK_ARM)
2023-08-01 10:17:38 -04:00
wolfSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
ret = wolfAsync_DevOpen(&devId);
if (ret < 0) {
2022-05-12 13:07:32 -05:00
fprintf(stderr, "Async device open failed\nRunning without async\n");
}
wolfSSL_CTX_SetDevId(ctx, devId);
#endif /* WOLFSSL_ASYNC_CRYPT */
2013-05-16 09:47:27 -07:00
2011-02-05 11:14:47 -08:00
ssl = SSL_new(ctx);
tcp_connect(&sockfd, wolfSSLIP, port, 0, 0, ssl);
2011-02-05 11:14:47 -08:00
SSL_set_fd(ssl, sockfd);
2013-05-16 09:47:27 -07:00
2025-08-10 19:56:00 +10:00
WOLFSSL_ASYNC_WHILE_PENDING(ret = SSL_connect(ssl), ret != WOLFSSL_SUCCESS);
if (ret != WOLFSSL_SUCCESS) {
2022-05-12 13:07:32 -05:00
fprintf(stderr, "SSL_connect error %d, %s\n", err,
2024-05-08 10:14:53 -04:00
ERR_error_string((unsigned long)err, buffer));
err_sys("SSL_connect failed");
}
2011-02-05 11:14:47 -08:00
2013-05-16 09:47:27 -07:00
while (fgets(msg, sizeof(msg), fin) != 0) {
sendSz = (int)XSTRLEN(msg);
2011-02-05 11:14:47 -08:00
2025-08-10 19:56:00 +10:00
WOLFSSL_ASYNC_WHILE_PENDING(ret = SSL_write(ssl, msg, sendSz), ret <= 0);
if (ret != sendSz) {
2022-05-12 13:07:32 -05:00
fprintf(stderr, "SSL_write msg error %d, %s\n", err,
2024-05-08 10:14:53 -04:00
ERR_error_string((unsigned long)err, buffer));
2011-02-05 11:14:47 -08:00
err_sys("SSL_write failed");
}
2011-02-05 11:14:47 -08:00
2012-09-20 15:39:15 -07:00
if (strncmp(msg, "quit", 4) == 0) {
LIBCALL_CHECK_RET(fputs("sending server shutdown command: quit!\n",
fout));
2011-02-05 11:14:47 -08:00
break;
}
2012-09-20 15:39:15 -07:00
if (strncmp(msg, "break", 5) == 0) {
LIBCALL_CHECK_RET(fputs("sending server session close: break!\n",
fout));
2011-02-05 11:14:47 -08:00
break;
}
#ifndef WOLFSSL_MDK_SHELL
while (sendSz)
#endif
2013-05-16 09:47:27 -07:00
{
2025-08-10 19:56:00 +10:00
WOLFSSL_ASYNC_WHILE_PENDING(
ret = SSL_read(ssl, reply, sizeof(reply)-1), ret <= 0);
if (ret > 0) {
reply[ret] = 0;
LIBCALL_CHECK_RET(fputs(reply, fout));
LIBCALL_CHECK_RET(fflush(fout));
sendSz -= ret;
}
else {
2022-05-12 13:07:32 -05:00
fprintf(stderr, "SSL_read msg error %d, %s\n", err,
2024-05-08 10:14:53 -04:00
ERR_error_string((unsigned long)err, buffer));
err_sys("SSL_read failed");
2013-05-16 09:47:27 -07:00
}
}
2011-02-05 11:14:47 -08:00
}
SSL_shutdown(ssl);
SSL_free(ssl);
SSL_CTX_free(ctx);
#ifdef WOLFSSL_ASYNC_CRYPT
wolfAsync_DevClose(&devId);
#endif
LIBCALL_CHECK_RET(fflush(fout));
2020-04-08 09:46:22 +10:00
#ifndef WOLFSSL_MDK_SHELL
2011-02-05 11:14:47 -08:00
if (inCreated) fclose(fin);
if (outCreated) fclose(fout);
2020-04-08 09:46:22 +10:00
#endif
2011-02-05 11:14:47 -08:00
CloseSocket(sockfd);
((func_args*)args)->return_code = 0;
2011-02-05 11:14:47 -08:00
}
#endif /* !NO_WOLFSSL_CLIENT && !NO_TLS */
2011-02-05 11:14:47 -08:00
/* so overall tests can pull in test function */
#ifndef NO_MAIN_DRIVER
int main(int argc, char** argv)
{
func_args args;
#ifdef HAVE_WNR
if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
err_sys("Whitewood netRandom global config failed");
#endif
2011-02-05 11:14:47 -08:00
StartTCP();
args.argc = argc;
args.argv = argv;
args.return_code = 0;
2011-02-05 11:14:47 -08:00
2023-08-01 10:17:38 -04:00
wolfSSL_Init();
2023-07-31 15:36:38 -04:00
#if defined(DEBUG_WOLFSSL) && !defined(WOLFSSL_MDK_SHELL)
2023-08-01 10:17:38 -04:00
wolfSSL_Debugging_ON();
#endif
2023-07-31 15:36:38 -04:00
#ifndef WOLFSSL_TIRTOS
ChangeToWolfRoot();
2014-05-08 15:52:20 -07:00
#endif
#if !defined(NO_WOLFSSL_CLIENT) && !defined(NO_TLS)
2011-02-05 11:14:47 -08:00
echoclient_test(&args);
#endif
2013-05-16 09:47:27 -07:00
2023-08-01 10:17:38 -04:00
wolfSSL_Cleanup();
2011-02-05 11:14:47 -08:00
#ifdef HAVE_WNR
if (wc_FreeNetRandom() < 0)
err_sys("Failed to free netRandom context");
#endif /* HAVE_WNR */
2011-02-05 11:14:47 -08:00
return args.return_code;
}
2011-02-05 11:14:47 -08:00
#endif /* NO_MAIN_DRIVER */