2011-07-27 00:27:22 +04:00
|
|
|
/* echoclient.c
|
|
|
|
*
|
2014-04-12 01:58:58 +04:00
|
|
|
* Copyright (C) 2006-2014 wolfSSL Inc.
|
2011-07-27 00:27:22 +04:00
|
|
|
*
|
|
|
|
* This file is part of CyaSSL.
|
|
|
|
*
|
|
|
|
* CyaSSL is free software; you can redistribute it and/or modify
|
|
|
|
* 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.
|
|
|
|
*
|
|
|
|
* CyaSSL is distributed in the hope that it will be useful,
|
|
|
|
* 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
|
2014-04-12 01:58:58 +04:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
|
2011-07-27 00:27:22 +04:00
|
|
|
*/
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2011-08-25 03:37:16 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2011-08-05 02:42:55 +04:00
|
|
|
|
2013-04-10 23:17:23 +04:00
|
|
|
#include <cyassl/ctaocrypt/settings.h>
|
|
|
|
|
2011-08-25 02:54:58 +04:00
|
|
|
#include <cyassl/openssl/ssl.h>
|
2013-05-16 20:47:27 +04:00
|
|
|
|
|
|
|
#if defined(CYASSL_MDK_ARM)
|
2014-04-11 11:20:12 +04:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#if defined(CYASSL_MDK5)
|
|
|
|
#include "cmsis_os.h"
|
|
|
|
#include "rl_fs.h"
|
|
|
|
#include "rl_net.h"
|
|
|
|
#else
|
|
|
|
#include "rtl.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "cyassl_MDK_ARM.h"
|
2013-05-16 20:47:27 +04:00
|
|
|
#endif
|
|
|
|
|
2011-08-26 01:28:57 +04:00
|
|
|
#include <cyassl/test.h>
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-09-20 10:38:41 +04:00
|
|
|
#include "examples/echoclient/echoclient.h"
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
void echoclient_test(void* args)
|
|
|
|
{
|
|
|
|
SOCKET_T sockfd = 0;
|
|
|
|
|
2013-05-16 20:47:27 +04:00
|
|
|
FILE* fin = stdin ;
|
2011-02-05 22:14:47 +03:00
|
|
|
FILE* fout = stdout;
|
|
|
|
|
|
|
|
int inCreated = 0;
|
|
|
|
int outCreated = 0;
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
char msg[1024];
|
2013-02-08 23:21:48 +04:00
|
|
|
char reply[1024+1];
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
SSL_METHOD* method = 0;
|
|
|
|
SSL_CTX* ctx = 0;
|
|
|
|
SSL* ssl = 0;
|
|
|
|
|
2012-08-02 22:54:49 +04:00
|
|
|
int doDTLS = 0;
|
2013-03-12 00:19:43 +04:00
|
|
|
int doPSK = 0;
|
2011-02-05 22:14:47 +03:00
|
|
|
int sendSz;
|
|
|
|
int argc = 0;
|
|
|
|
char** argv = 0;
|
2014-03-04 04:46:48 +04:00
|
|
|
word16 port = yasslPort;
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
((func_args*)args)->return_code = -1; /* error state */
|
2013-05-16 20:47:27 +04:00
|
|
|
|
|
|
|
#ifndef CYASSL_MDK_SHELL
|
2011-02-05 22:14:47 +03:00
|
|
|
argc = ((func_args*)args)->argc;
|
|
|
|
argv = ((func_args*)args)->argv;
|
2013-05-16 20:47:27 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03: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");
|
|
|
|
|
2012-08-02 22:54:49 +04:00
|
|
|
#ifdef CYASSL_DTLS
|
|
|
|
doDTLS = 1;
|
|
|
|
#endif
|
|
|
|
|
2012-10-30 23:51:14 +04:00
|
|
|
#ifdef CYASSL_LEANPSK
|
2013-03-12 00:19:43 +04:00
|
|
|
doPSK = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NO_RSA) && !defined(HAVE_ECC)
|
|
|
|
doPSK = 1;
|
2012-10-30 23:51:14 +04:00
|
|
|
#endif
|
|
|
|
|
2013-05-21 04:56:27 +04:00
|
|
|
#if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_SHELL)
|
2013-03-27 09:00:39 +04:00
|
|
|
port = ((func_args*)args)->signal->port;
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#if defined(CYASSL_DTLS)
|
|
|
|
method = DTLSv1_client_method();
|
|
|
|
#elif !defined(NO_TLS)
|
2012-07-09 20:50:26 +04:00
|
|
|
method = CyaSSLv23_client_method();
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
|
|
|
method = SSLv3_client_method();
|
|
|
|
#endif
|
|
|
|
ctx = SSL_CTX_new(method);
|
|
|
|
|
|
|
|
#ifndef NO_FILESYSTEM
|
2013-03-08 06:10:18 +04:00
|
|
|
#ifndef NO_RSA
|
2011-02-05 22:14:47 +03:00
|
|
|
if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
|
2011-08-25 02:54:58 +04:00
|
|
|
err_sys("can't load ca file, Please run from CyaSSL home dir");
|
2013-03-08 06:10:18 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef HAVE_ECC
|
|
|
|
if (SSL_CTX_load_verify_locations(ctx, eccCert, 0) != SSL_SUCCESS)
|
2011-08-25 02:54:58 +04:00
|
|
|
err_sys("can't load ca file, Please run from CyaSSL home dir");
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
2012-11-01 23:36:47 +04:00
|
|
|
#elif !defined(NO_CERTS)
|
2013-03-13 23:14:05 +04:00
|
|
|
if (!doPSK)
|
2012-10-30 23:51:14 +04:00
|
|
|
load_buffer(ctx, caCert, CYASSL_CA);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
|
|
|
|
2011-06-06 22:19:52 +04:00
|
|
|
#if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
|
2011-04-29 21:41:21 +04:00
|
|
|
/* don't use EDH, can't sniff tmp keys */
|
|
|
|
SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
|
|
|
|
#endif
|
2013-03-12 00:19:43 +04:00
|
|
|
if (doPSK) {
|
|
|
|
#ifndef NO_PSK
|
|
|
|
const char *defaultCipherList;
|
|
|
|
|
2012-10-30 23:51:14 +04:00
|
|
|
CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
|
2013-03-12 00:19:43 +04:00
|
|
|
#ifdef HAVE_NULL_CIPHER
|
|
|
|
defaultCipherList = "PSK-NULL-SHA256";
|
|
|
|
#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 23:51:14 +04:00
|
|
|
#endif
|
|
|
|
}
|
2011-04-29 21:41:21 +04:00
|
|
|
|
2014-03-04 00:18:26 +04:00
|
|
|
#if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
|
2011-02-05 22:14:47 +03:00
|
|
|
SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
|
|
|
|
#endif
|
2013-05-16 20:47:27 +04:00
|
|
|
|
|
|
|
#if defined(CYASSL_MDK_ARM)
|
|
|
|
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
ssl = SSL_new(ctx);
|
|
|
|
|
2012-10-02 20:15:50 +04:00
|
|
|
if (doDTLS) {
|
|
|
|
SOCKADDR_IN_T addr;
|
2013-04-11 23:30:09 +04:00
|
|
|
build_addr(&addr, yasslIP, port, 1);
|
2012-10-02 20:15:50 +04:00
|
|
|
CyaSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
|
|
|
|
tcp_socket(&sockfd, 1);
|
|
|
|
}
|
|
|
|
else {
|
2013-03-27 09:00:39 +04:00
|
|
|
tcp_connect(&sockfd, yasslIP, port, 0);
|
2012-10-02 20:15:50 +04:00
|
|
|
}
|
2013-05-16 20:47:27 +04:00
|
|
|
|
2011-02-05 22:14:47 +03: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
|
|
|
|
|
2013-05-16 20:47:27 +04:00
|
|
|
if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2013-05-16 20:47:27 +04:00
|
|
|
while (fgets(msg, sizeof(msg), fin) != 0) {
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
sendSz = (int)strlen(msg);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
if (SSL_write(ssl, msg, sendSz) != sendSz)
|
2011-02-05 22:14:47 +03:00
|
|
|
err_sys("SSL_write failed");
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
if (strncmp(msg, "quit", 4) == 0) {
|
2011-02-05 22:14:47 +03:00
|
|
|
fputs("sending server shutdown command: quit!\n", fout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2012-09-21 02:39:15 +04:00
|
|
|
if (strncmp(msg, "break", 5) == 0) {
|
2011-02-05 22:14:47 +03:00
|
|
|
fputs("sending server session close: break!\n", fout);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2013-05-16 20:47:27 +04:00
|
|
|
#ifndef CYASSL_MDK_SHELL
|
2011-02-05 22:14:47 +03:00
|
|
|
while (sendSz) {
|
|
|
|
int got;
|
2013-02-08 23:21:48 +04:00
|
|
|
if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
|
2012-07-01 02:51:18 +04:00
|
|
|
reply[got] = 0;
|
2011-02-05 22:14:47 +03:00
|
|
|
fputs(reply, fout);
|
2013-05-16 20:47:27 +04:00
|
|
|
fflush(fout) ;
|
2011-02-05 22:14:47 +03:00
|
|
|
sendSz -= got;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
2013-05-16 20:47:27 +04: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 22:14:47 +03:00
|
|
|
}
|
|
|
|
|
2013-05-16 20:47:27 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef CYASSL_DTLS
|
2012-09-21 02:39:15 +04:00
|
|
|
strncpy(msg, "break", 6);
|
|
|
|
sendSz = (int)strlen(msg);
|
2011-02-05 22:14:47 +03:00
|
|
|
/* try to tell server done */
|
2012-09-21 02:39:15 +04:00
|
|
|
SSL_write(ssl, msg, sendSz);
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
|
|
|
SSL_shutdown(ssl);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
SSL_free(ssl);
|
|
|
|
SSL_CTX_free(ctx);
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
2013-02-02 00:21:38 +04:00
|
|
|
#ifdef HAVE_CAVIUM
|
|
|
|
int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
|
|
|
|
if (ret != 0)
|
|
|
|
err_sys("Cavium OpenNitroxDevice failed");
|
|
|
|
#endif /* HAVE_CAVIUM */
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
StartTCP();
|
|
|
|
|
|
|
|
args.argc = argc;
|
|
|
|
args.argv = argv;
|
|
|
|
|
2011-06-09 00:19:39 +04:00
|
|
|
CyaSSL_Init();
|
2013-05-16 20:47:27 +04:00
|
|
|
#if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL)
|
2012-07-01 02:51:18 +04:00
|
|
|
CyaSSL_Debugging_ON();
|
|
|
|
#endif
|
2014-09-09 06:40:03 +04:00
|
|
|
#ifndef CYASSL_TIRTOS
|
2013-12-20 03:23:57 +04:00
|
|
|
if (CurrentDir("echoclient"))
|
2011-09-24 03:13:02 +04:00
|
|
|
ChangeDirBack(2);
|
2013-12-20 03:23:57 +04:00
|
|
|
else if (CurrentDir("Debug") || CurrentDir("Release"))
|
|
|
|
ChangeDirBack(3);
|
2014-05-09 02:52:20 +04:00
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
echoclient_test(&args);
|
2013-05-16 20:47:27 +04:00
|
|
|
|
2011-06-09 00:19:39 +04:00
|
|
|
CyaSSL_Cleanup();
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2013-02-02 00:21:38 +04:00
|
|
|
#ifdef HAVE_CAVIUM
|
|
|
|
CspShutdown(CAVIUM_DEV_ID);
|
|
|
|
#endif
|
2011-02-05 22:14:47 +03:00
|
|
|
return args.return_code;
|
|
|
|
}
|
2013-05-16 20:47:27 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif /* NO_MAIN_DRIVER */
|
|
|
|
|
|
|
|
|