wolfssl/examples/echoclient/echoclient.c

318 lines
7.8 KiB
C
Raw Normal View History

2011-07-26 13:27:22 -07:00
/* echoclient.c
*
2016-03-17 16:02:13 -06:00
* Copyright (C) 2006-2016 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
* the Free Software Foundation; either version 2 of the License, or
* (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
2015-10-08 15:39:14 +09:00
#include <cyassl/ctaocrypt/settings.h>
2015-02-25 13:34:29 -08:00
/* let's use cyassl layer AND cyassl openssl layer */
#include <cyassl/ssl.h>
#include <cyassl/openssl/ssl.h>
2013-05-16 09:47:27 -07:00
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>
#if !defined(WOLFSSL_MDK_ARM)
2015-10-08 15:39:14 +09:00
#include "cmsis_os.h"
#include "rl_net.h"
2014-04-11 16:20:12 +09:00
#else
#include "rtl.h"
#include "wolfssl_MDK_ARM.h"
2014-04-11 16:20:12 +09:00
#endif
2015-10-08 15:39:14 +09:00
#if defined(WOLFSSL_MDK_SHELL)
char * wolfssl_fgets ( char * str, int num, FILE * f ) ;
#define fgets wolfssl_fgets
#endif
2013-05-16 09:47:27 -07:00
#endif
2015-10-08 15:39:14 +09:00
2011-08-25 14:28:57 -07:00
#include <cyassl/test.h>
2011-02-05 11:14:47 -08:00
#include "examples/echoclient/echoclient.h"
2011-02-05 11:14:47 -08:00
#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;
int inCreated = 0;
int outCreated = 0;
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;
int doDTLS = 0;
2013-03-11 13:19:43 -07:00
int doPSK = 0;
2011-02-05 11:14:47 -08:00
int sendSz;
int argc = 0;
char** argv = 0;
word16 port = yasslPort;
2011-02-05 11:14:47 -08:00
((func_args*)args)->return_code = -1; /* error state */
2013-05-16 09:47:27 -07:00
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;
2013-05-16 09:47:27 -07:00
#endif
2011-02-05 11:14:47 -08:00
if (argc >= 2) {
fin = fopen(argv[1], "r");
inCreated = 1;
}
if (argc >= 3) {
fout = fopen(argv[2], "w");
outCreated = 1;
}
if (!fin) err_sys("can't open input file");
if (!fout) err_sys("can't open output file");
#ifdef CYASSL_DTLS
doDTLS = 1;
#endif
2012-10-30 12:51:14 -07:00
#ifdef CYASSL_LEANPSK
2013-03-11 13:19:43 -07:00
doPSK = 1;
#endif
#if defined(NO_RSA) && !defined(HAVE_ECC)
doPSK = 1;
2012-10-30 12:51:14 -07:00
#endif
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;
#endif
2011-02-05 11:14:47 -08:00
#if defined(CYASSL_DTLS)
method = DTLSv1_2_client_method();
2011-02-05 11:14:47 -08:00
#elif !defined(NO_TLS)
method = CyaSSLv23_client_method();
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
2011-02-05 11:14:47 -08:00
if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_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, eccCert, 0) != SSL_SUCCESS)
2015-01-20 12:36:20 -07:00
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)
load_buffer(ctx, caCert, WOLFSSL_CA);
2011-02-05 11:14:47 -08:00
#endif
2015-04-01 12:14:48 -07:00
#if defined(CYASSL_SNIFFER)
/* 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
if (doPSK) {
#ifndef NO_PSK
const char *defaultCipherList;
2012-10-30 12:51:14 -07:00
CyaSSL_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";
#elif defined(HAVE_AESGCM) && !defined(NO_DH)
defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
2013-03-11 13:19:43 -07:00
#else
defaultCipherList = "PSK-AES128-CBC-SHA256";
#endif
if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=SSL_SUCCESS)
err_sys("client can't set cipher list 2");
2012-10-30 12:51:14 -07:00
#endif
}
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
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)
2013-05-16 09:47:27 -07:00
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
#endif
#ifdef WOLFSSL_ASYNC_CRYPT
ret = wolfAsync_DevOpen(&devId);
if (ret != 0) {
err_sys("Async device open failed");
}
wolfSSL_CTX_UseAsync(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, yasslIP, port, doDTLS, 0, ssl);
2013-05-16 09:47:27 -07:00
2011-02-05 11:14:47 -08:00
SSL_set_fd(ssl, sockfd);
#if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
/* let echoserver bind first, TODO: add Windows signal like pthreads does */
Sleep(100);
#endif
do {
#ifdef WOLFSSL_ASYNC_CRYPT
if (err == WC_PENDING_E) {
ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
if (ret < 0) { break; } else if (ret == 0) { continue; }
}
#endif
err = 0; /* Reset error */
ret = SSL_connect(ssl);
if (ret != SSL_SUCCESS) {
err = SSL_get_error(ssl, 0);
}
} while (ret != SSL_SUCCESS && err == WC_PENDING_E);
if (ret != SSL_SUCCESS) {
char buffer[CYASSL_MAX_ERROR_SZ];
printf("err = %d, %s\n", err, ERR_error_string(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
2012-09-20 15:39:15 -07:00
if (SSL_write(ssl, msg, sendSz) != sendSz)
2011-02-05 11:14:47 -08:00
err_sys("SSL_write failed");
2012-09-20 15:39:15 -07:00
if (strncmp(msg, "quit", 4) == 0) {
2011-02-05 11:14:47 -08:00
fputs("sending server shutdown command: quit!\n", fout);
break;
}
2012-09-20 15:39:15 -07:00
if (strncmp(msg, "break", 5) == 0) {
2011-02-05 11:14:47 -08:00
fputs("sending server session close: break!\n", fout);
break;
}
2015-08-12 16:45:40 +09:00
#ifndef WOLFSSL_MDK_SHELL
2011-02-05 11:14:47 -08:00
while (sendSz) {
int got;
if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
reply[got] = 0;
2011-02-05 11:14:47 -08:00
fputs(reply, fout);
2013-05-16 09:47:27 -07:00
fflush(fout) ;
2011-02-05 11:14:47 -08:00
sendSz -= got;
}
else
break;
}
2013-05-16 09:47:27 -07:00
#else
{
int got;
if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
reply[got] = 0;
fputs(reply, fout);
fflush(fout) ;
sendSz -= got;
}
}
#endif
2011-02-05 11:14:47 -08:00
}
2013-05-16 09:47:27 -07:00
2011-02-05 11:14:47 -08:00
#ifdef CYASSL_DTLS
2012-09-20 15:39:15 -07:00
strncpy(msg, "break", 6);
sendSz = (int)strlen(msg);
2011-02-05 11:14:47 -08:00
/* try to tell server done */
2012-09-20 15:39:15 -07:00
SSL_write(ssl, msg, sendSz);
2011-02-05 11:14:47 -08:00
#else
SSL_shutdown(ssl);
#endif
SSL_free(ssl);
SSL_CTX_free(ctx);
#ifdef WOLFSSL_ASYNC_CRYPT
wolfAsync_DevClose(&devId);
#endif
2011-02-05 11:14:47 -08:00
fflush(fout);
if (inCreated) fclose(fin);
if (outCreated) fclose(fout);
CloseSocket(sockfd);
((func_args*)args)->return_code = 0;
}
/* 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;
CyaSSL_Init();
2015-08-12 16:45:40 +09:00
#if defined(DEBUG_CYASSL) && !defined(WOLFSSL_MDK_SHELL)
CyaSSL_Debugging_ON();
#endif
2014-09-08 19:40:03 -07:00
#ifndef CYASSL_TIRTOS
ChangeToWolfRoot();
2014-05-08 15:52:20 -07:00
#endif
2011-02-05 11:14:47 -08:00
echoclient_test(&args);
2013-05-16 09:47:27 -07:00
CyaSSL_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;
}
2013-05-16 09:47:27 -07:00
2011-02-05 11:14:47 -08:00
#endif /* NO_MAIN_DRIVER */