2011-07-27 00:27:22 +04:00
|
|
|
/* client.c
|
|
|
|
*
|
2012-02-13 23:54:10 +04:00
|
|
|
* Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
|
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
|
|
|
|
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
|
|
|
|
*/
|
|
|
|
|
2011-08-25 03:37:16 +04:00
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include <config.h>
|
|
|
|
#endif
|
2011-08-05 02:42:55 +04:00
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
#include <cyassl/ssl.h>
|
2011-08-26 01:28:57 +04:00
|
|
|
#include <cyassl/test.h>
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
#define TEST_RESUME
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CYASSL_CALLBACKS
|
|
|
|
int handShakeCB(HandShakeInfo*);
|
|
|
|
int timeoutCB(TimeoutInfo*);
|
|
|
|
Timeval timeout;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(NON_BLOCKING) || defined(CYASSL_CALLBACKS)
|
2011-11-03 21:56:15 +04:00
|
|
|
void NonBlockingSSL_Connect(CyaSSL* ssl)
|
2011-02-05 22:14:47 +03:00
|
|
|
{
|
|
|
|
#ifndef CYASSL_CALLBACKS
|
2011-11-03 21:56:15 +04:00
|
|
|
int ret = CyaSSL_connect(ssl);
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
|
|
|
int ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
|
|
|
|
#endif
|
2011-11-03 21:56:15 +04:00
|
|
|
int error = CyaSSL_get_error(ssl, 0);
|
2011-02-05 22:14:47 +03:00
|
|
|
while (ret != SSL_SUCCESS && (error == SSL_ERROR_WANT_READ ||
|
|
|
|
error == SSL_ERROR_WANT_WRITE)) {
|
|
|
|
if (error == SSL_ERROR_WANT_READ)
|
|
|
|
printf("... client would read block\n");
|
|
|
|
else
|
|
|
|
printf("... client would write block\n");
|
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
Sleep(100);
|
|
|
|
#else
|
|
|
|
sleep(1);
|
|
|
|
#endif
|
|
|
|
#ifndef CYASSL_CALLBACKS
|
2011-11-03 21:56:15 +04:00
|
|
|
ret = CyaSSL_connect(ssl);
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
|
|
|
ret = CyaSSL_connect_ex(ssl, handShakeCB, timeoutCB, timeout);
|
|
|
|
#endif
|
2011-11-03 21:56:15 +04:00
|
|
|
error = CyaSSL_get_error(ssl, 0);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
|
|
|
if (ret != SSL_SUCCESS)
|
|
|
|
err_sys("SSL_connect failed");
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
void client_test(void* args)
|
|
|
|
{
|
|
|
|
SOCKET_T sockfd = 0;
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CYASSL_METHOD* method = 0;
|
|
|
|
CYASSL_CTX* ctx = 0;
|
|
|
|
CYASSL* ssl = 0;
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifdef TEST_RESUME
|
2011-11-03 21:56:15 +04:00
|
|
|
CYASSL* sslResume = 0;
|
|
|
|
CYASSL_SESSION* session = 0;
|
2011-02-05 22:14:47 +03:00
|
|
|
char resumeMsg[] = "resuming cyassl!";
|
|
|
|
int resumeSz = sizeof(resumeMsg);
|
|
|
|
#endif
|
|
|
|
|
2011-03-22 18:18:27 +03:00
|
|
|
char msg[64] = "hello cyassl!";
|
2011-02-05 22:14:47 +03:00
|
|
|
char reply[1024];
|
|
|
|
int input;
|
2011-03-22 18:18:27 +03:00
|
|
|
int msgSz = strlen(msg);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
int argc = ((func_args*)args)->argc;
|
|
|
|
char** argv = ((func_args*)args)->argv;
|
|
|
|
|
|
|
|
((func_args*)args)->return_code = -1; /* error state */
|
|
|
|
|
|
|
|
#if defined(CYASSL_DTLS)
|
2011-11-03 21:56:15 +04:00
|
|
|
method = CyaDTLSv1_client_method();
|
2011-02-05 22:14:47 +03:00
|
|
|
#elif !defined(NO_TLS)
|
2011-11-03 21:56:15 +04:00
|
|
|
method = CyaSSLv23_client_method();
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
2011-11-03 21:56:15 +04:00
|
|
|
method = CyaSSLv3_client_method();
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
2011-11-03 21:56:15 +04:00
|
|
|
ctx = CyaSSL_CTX_new(method);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifndef NO_PSK
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef OPENSSL_EXTRA
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
|
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 */
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA");
|
2011-04-29 21:41:21 +04:00
|
|
|
#endif
|
|
|
|
|
2012-01-27 00:52:54 +04:00
|
|
|
#ifdef USER_CA_CB
|
|
|
|
CyaSSL_CTX_SetCACb(ctx, CaCb);
|
|
|
|
#endif
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifndef NO_FILESYSTEM
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_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");
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef HAVE_ECC
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_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
|
|
|
|
#else
|
|
|
|
load_buffer(ctx, caCert, CYASSL_CA);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef VERIFY_CALLBACK
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, myVerify);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
|
|
|
|
2012-05-17 04:04:56 +04:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
if (argc == 3) {
|
|
|
|
/* ./client server securePort */
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); /* TODO: add ca cert */
|
2011-02-05 22:14:47 +03:00
|
|
|
/* this is just to allow easy testing of other servers */
|
|
|
|
tcp_connect(&sockfd, argv[1], (short)atoi(argv[2]));
|
|
|
|
}
|
|
|
|
else if (argc == 1) {
|
|
|
|
/* ./client // plain mode */
|
|
|
|
/* for client cert authentication if server requests */
|
|
|
|
#ifndef NO_FILESYSTEM
|
2012-05-02 21:30:15 +04:00
|
|
|
#ifdef HAVE_ECC
|
|
|
|
if (CyaSSL_CTX_use_certificate_file(ctx, cliEccCert, SSL_FILETYPE_PEM)
|
|
|
|
!= SSL_SUCCESS)
|
|
|
|
err_sys("can't load ecc client cert file, "
|
|
|
|
"Please run from CyaSSL home dir");
|
|
|
|
|
|
|
|
if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliEccKey, SSL_FILETYPE_PEM)
|
|
|
|
!= SSL_SUCCESS)
|
|
|
|
err_sys("can't load ecc client key file, "
|
|
|
|
"Please run from CyaSSL home dir");
|
|
|
|
#else
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_CTX_use_certificate_file(ctx, cliCert, SSL_FILETYPE_PEM)
|
2011-02-05 22:14:47 +03:00
|
|
|
!= SSL_SUCCESS)
|
2011-08-25 02:54:58 +04:00
|
|
|
err_sys("can't load client cert file, "
|
|
|
|
"Please run from CyaSSL home dir");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_CTX_use_PrivateKey_file(ctx, cliKey, SSL_FILETYPE_PEM)
|
2011-02-05 22:14:47 +03:00
|
|
|
!= SSL_SUCCESS)
|
2011-08-25 02:54:58 +04:00
|
|
|
err_sys("can't load client key file, "
|
|
|
|
"Please run from CyaSSL home dir");
|
2012-05-02 21:30:15 +04:00
|
|
|
#endif /* HAVE_ECC */
|
2011-02-05 22:14:47 +03:00
|
|
|
#else
|
|
|
|
load_buffer(ctx, cliCert, CYASSL_CERT);
|
|
|
|
load_buffer(ctx, cliKey, CYASSL_KEY);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
tcp_connect(&sockfd, yasslIP, yasslPort);
|
|
|
|
}
|
|
|
|
else if (argc == 2) {
|
|
|
|
/* time passed in number of connects give average */
|
|
|
|
int times = atoi(argv[1]);
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
double start = current_time(), avg;
|
|
|
|
|
|
|
|
for (i = 0; i < times; i++) {
|
|
|
|
tcp_connect(&sockfd, yasslIP, yasslPort);
|
2011-11-03 21:56:15 +04:00
|
|
|
ssl = CyaSSL_new(ctx);
|
|
|
|
CyaSSL_set_fd(ssl, sockfd);
|
|
|
|
if (CyaSSL_connect(ssl) != SSL_SUCCESS)
|
2011-02-05 22:14:47 +03:00
|
|
|
err_sys("SSL_connect failed");
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_shutdown(ssl);
|
|
|
|
CyaSSL_free(ssl);
|
2011-02-05 22:14:47 +03:00
|
|
|
CloseSocket(sockfd);
|
|
|
|
}
|
|
|
|
avg = current_time() - start;
|
|
|
|
avg /= times;
|
|
|
|
avg *= 1000; /* milliseconds */
|
|
|
|
printf("SSL_connect avg took:%6.3f milliseconds\n", avg);
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_free(ctx);
|
2011-02-05 22:14:47 +03:00
|
|
|
((func_args*)args)->return_code = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
err_sys("usage: ./client server securePort");
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
ssl = CyaSSL_new(ctx);
|
|
|
|
CyaSSL_set_fd(ssl, sockfd);
|
2012-05-17 04:04:56 +04:00
|
|
|
#ifdef HAVE_CRL
|
2012-05-24 23:45:10 +04:00
|
|
|
CyaSSL_EnableCRL(ssl, CYASSL_CRL_CHECKALL);
|
2012-05-18 21:52:32 +04:00
|
|
|
CyaSSL_LoadCRL(ssl, crlPemDir, SSL_FILETYPE_PEM, 0);
|
2012-05-17 04:35:51 +04:00
|
|
|
CyaSSL_SetCRL_Cb(ssl, CRL_CallBack);
|
2012-05-17 04:04:56 +04:00
|
|
|
#endif
|
2011-04-11 23:55:17 +04:00
|
|
|
if (argc != 3)
|
|
|
|
CyaSSL_check_domain_name(ssl, "www.yassl.com");
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef NON_BLOCKING
|
|
|
|
tcp_set_nonblocking(&sockfd);
|
|
|
|
NonBlockingSSL_Connect(ssl);
|
|
|
|
#else
|
|
|
|
#ifndef CYASSL_CALLBACKS
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_connect(ssl) != SSL_SUCCESS) {/* see note at top of README */
|
|
|
|
int err = CyaSSL_get_error(ssl, 0);
|
2011-02-05 22:14:47 +03:00
|
|
|
char buffer[80];
|
2011-11-03 21:56:15 +04:00
|
|
|
printf("err = %d, %s\n", err, CyaSSL_ERR_error_string(err, buffer));
|
2011-02-05 22:14:47 +03:00
|
|
|
err_sys("SSL_connect failed");/* if you're getting an error here */
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
timeout.tv_sec = 2;
|
|
|
|
timeout.tv_usec = 0;
|
|
|
|
NonBlockingSSL_Connect(ssl); /* will keep retrying on timeout */
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
showPeer(ssl);
|
|
|
|
|
|
|
|
if (argc == 3) {
|
|
|
|
printf("SSL connect ok, sending GET...\n");
|
2011-03-22 18:18:27 +03:00
|
|
|
msgSz = 28;
|
|
|
|
strncpy(msg, "GET /index.html HTTP/1.0\r\n\r\n", msgSz);
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_write(ssl, msg, msgSz) != msgSz)
|
2011-02-05 22:14:47 +03:00
|
|
|
err_sys("SSL_write failed");
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
input = CyaSSL_read(ssl, reply, sizeof(reply));
|
2011-02-05 22:14:47 +03:00
|
|
|
if (input > 0) {
|
|
|
|
reply[input] = 0;
|
|
|
|
printf("Server response: %s\n", reply);
|
2011-03-22 18:18:27 +03:00
|
|
|
|
|
|
|
if (argc == 3) { /* get html */
|
|
|
|
while (1) {
|
2011-11-03 21:56:15 +04:00
|
|
|
input = CyaSSL_read(ssl, reply, sizeof(reply));
|
2011-03-22 18:18:27 +03:00
|
|
|
if (input > 0) {
|
|
|
|
reply[input] = 0;
|
|
|
|
printf("%s\n", reply);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2011-02-05 22:14:47 +03:00
|
|
|
}
|
2011-03-22 18:18:27 +03:00
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef TEST_RESUME
|
|
|
|
#ifdef CYASSL_DTLS
|
|
|
|
strncpy(msg, "break", 6);
|
|
|
|
msgSz = (int)strlen(msg);
|
|
|
|
/* try to send session close */
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_write(ssl, msg, msgSz);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
2011-11-03 21:56:15 +04:00
|
|
|
session = CyaSSL_get_session(ssl);
|
|
|
|
sslResume = CyaSSL_new(ctx);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_shutdown(ssl);
|
|
|
|
CyaSSL_free(ssl);
|
2011-02-05 22:14:47 +03:00
|
|
|
CloseSocket(sockfd);
|
|
|
|
|
|
|
|
#ifdef TEST_RESUME
|
|
|
|
#ifdef CYASSL_DTLS
|
|
|
|
#ifdef USE_WINDOWS_API
|
|
|
|
Sleep(500);
|
|
|
|
#else
|
|
|
|
sleep(1);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
if (argc == 3)
|
|
|
|
tcp_connect(&sockfd, argv[1], (short)atoi(argv[2]));
|
|
|
|
else
|
|
|
|
tcp_connect(&sockfd, yasslIP, yasslPort);
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_set_fd(sslResume, sockfd);
|
|
|
|
CyaSSL_set_session(sslResume, session);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
showPeer(sslResume);
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_connect(sslResume) != SSL_SUCCESS) err_sys("SSL resume failed");
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
#ifdef OPENSSL_EXTRA
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_session_reused(sslResume))
|
2011-02-05 22:14:47 +03:00
|
|
|
printf("reused session id\n");
|
|
|
|
else
|
|
|
|
printf("didn't reuse session id!!!\n");
|
|
|
|
#endif
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
if (CyaSSL_write(sslResume, resumeMsg, resumeSz) != resumeSz)
|
2011-02-05 22:14:47 +03:00
|
|
|
err_sys("SSL_write failed");
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
input = CyaSSL_read(sslResume, reply, sizeof(reply));
|
2011-02-05 22:14:47 +03:00
|
|
|
if (input > 0) {
|
|
|
|
reply[input] = 0;
|
|
|
|
printf("Server resume response: %s\n", reply);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* try to send session break */
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_write(sslResume, msg, msgSz);
|
2011-02-05 22:14:47 +03:00
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_shutdown(sslResume);
|
|
|
|
CyaSSL_free(sslResume);
|
2011-02-05 22:14:47 +03:00
|
|
|
#endif /* TEST_RESUME */
|
|
|
|
|
2011-11-03 21:56:15 +04:00
|
|
|
CyaSSL_CTX_free(ctx);
|
2011-02-05 22:14:47 +03:00
|
|
|
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;
|
|
|
|
|
|
|
|
StartTCP();
|
|
|
|
|
|
|
|
args.argc = argc;
|
|
|
|
args.argv = argv;
|
|
|
|
|
2011-06-09 00:19:39 +04:00
|
|
|
CyaSSL_Init();
|
2011-02-05 22:14:47 +03:00
|
|
|
#ifdef DEBUG_CYASSL
|
|
|
|
CyaSSL_Debugging_ON();
|
|
|
|
#endif
|
2011-09-26 03:35:54 +04:00
|
|
|
if (CurrentDir("client") || CurrentDir("build"))
|
2011-09-24 03:13:02 +04:00
|
|
|
ChangeDirBack(2);
|
|
|
|
|
2011-02-05 22:14:47 +03:00
|
|
|
client_test(&args);
|
2011-06-09 00:19:39 +04:00
|
|
|
CyaSSL_Cleanup();
|
2011-02-05 22:14:47 +03:00
|
|
|
|
|
|
|
return args.return_code;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* NO_MAIN_DRIVER */
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifdef CYASSL_CALLBACKS
|
|
|
|
|
|
|
|
int handShakeCB(HandShakeInfo* info)
|
|
|
|
{
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
int timeoutCB(TimeoutInfo* info)
|
|
|
|
{
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|