3 * Copyright (C) 2006-2014 wolfSSL Inc.
5 * This file is part of CyaSSL.
7 * CyaSSL 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.
12 * CyaSSL 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.
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
26 #include <cyassl/ctaocrypt/settings.h>
28 #if defined(CYASSL_MDK_ARM)
32 #if defined(CYASSL_MDK5)
40 #include "cyassl_MDK_ARM.h"
43 #include <cyassl/ssl.h>
44 #include <cyassl/test.h>
46 #ifndef NO_MAIN_DRIVER
50 #include "examples/echoserver/echoserver.h"
54 CYASSL_API void PrintSessionStats(void);
57 #define SVR_COMMAND_SIZE 256
59 static void SignalReady(void* args, word16 port)
61 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER) && !defined(__MINGW32__)
62 /* signal ready to tcp_accept */
63 func_args* server_args = (func_args*)args;
64 tcp_ready* ready = server_args->signal;
65 pthread_mutex_lock(&ready->mutex);
68 pthread_cond_signal(&ready->cond);
69 pthread_mutex_unlock(&ready->mutex);
76 THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
79 CYASSL_METHOD* method = 0;
87 word16 port = yasslPort;
88 int argc = ((func_args*)args)->argc;
89 char** argv = ((func_args*)args)->argv;
94 fout = fopen(argv[1], "w");
97 if (!fout) err_sys("can't open output file");
103 ((func_args*)args)->return_code = -1; /* error state */
109 #ifdef CYASSL_LEANPSK
113 #if defined(NO_RSA) && !defined(HAVE_ECC)
117 #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && \
118 !defined(CYASSL_SNIFFER) && !defined(CYASSL_MDK_SHELL)
121 #if defined(USE_ANY_ADDR)
124 tcp_listen(&sockfd, &port, useAnyAddr, doDTLS);
126 #if defined(CYASSL_DTLS)
127 method = CyaDTLSv1_server_method();
128 #elif !defined(NO_TLS)
129 method = CyaSSLv23_server_method();
131 method = CyaSSLv3_server_method();
133 ctx = CyaSSL_CTX_new(method);
134 /* CyaSSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); */
136 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
137 CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
140 #ifndef NO_FILESYSTEM
144 if (CyaSSL_CTX_use_certificate_file(ctx, ntruCert, SSL_FILETYPE_PEM)
146 err_sys("can't load ntru cert file, "
147 "Please run from CyaSSL home dir");
149 if (CyaSSL_CTX_use_NTRUPrivateKey_file(ctx, ntruKey)
151 err_sys("can't load ntru key file, "
152 "Please run from CyaSSL home dir");
153 #elif defined(HAVE_ECC)
155 if (CyaSSL_CTX_use_certificate_file(ctx, eccCert, SSL_FILETYPE_PEM)
157 err_sys("can't load server cert file, "
158 "Please run from CyaSSL home dir");
160 if (CyaSSL_CTX_use_PrivateKey_file(ctx, eccKey, SSL_FILETYPE_PEM)
162 err_sys("can't load server key file, "
163 "Please run from CyaSSL home dir");
164 #elif defined(NO_CERTS)
165 /* do nothing, just don't load cert files */
168 if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
170 err_sys("can't load server cert file, "
171 "Please run from CyaSSL home dir");
173 if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
175 err_sys("can't load server key file, "
176 "Please run from CyaSSL home dir");
179 #elif !defined(NO_CERTS)
181 load_buffer(ctx, svrCert, CYASSL_CERT);
182 load_buffer(ctx, svrKey, CYASSL_KEY);
186 #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
187 /* don't use EDH, can't sniff tmp keys */
188 CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA");
193 const char *defaultCipherList;
195 CyaSSL_CTX_set_psk_server_callback(ctx, my_psk_server_cb);
196 CyaSSL_CTX_use_psk_identity_hint(ctx, "cyassl server");
197 #ifdef HAVE_NULL_CIPHER
198 defaultCipherList = "PSK-NULL-SHA256";
200 defaultCipherList = "PSK-AES128-CBC-SHA256";
202 if (CyaSSL_CTX_set_cipher_list(ctx, defaultCipherList) != SSL_SUCCESS)
203 err_sys("server can't set cipher list 2");
207 SignalReady(args, port);
211 char command[SVR_COMMAND_SIZE+1];
218 SOCKADDR_IN_T client;
219 socklen_t client_len = sizeof(client);
220 clientfd = accept(sockfd, (struct sockaddr*)&client,
221 (ACCEPT_THIRD_T)&client_len);
223 clientfd = udp_read_connect(sockfd);
225 if (clientfd == -1) err_sys("tcp accept failed");
227 ssl = CyaSSL_new(ctx);
228 if (ssl == NULL) err_sys("SSL_new failed");
229 CyaSSL_set_fd(ssl, clientfd);
230 #if !defined(NO_FILESYSTEM) && !defined(NO_DH)
231 CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
232 #elif !defined(NO_DH)
233 SetDH(ssl); /* will repick suites with DHE, higher than PSK */
235 if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
236 printf("SSL_accept failed\n");
238 CloseSocket(clientfd);
241 #if defined(PEER_INFO)
245 while ( (echoSz = CyaSSL_read(ssl, command, sizeof(command)-1)) > 0) {
247 if (firstRead == 1) {
248 firstRead = 0; /* browser may send 1 byte 'G' to start */
249 if (echoSz == 1 && command[0] == 'G') {
254 else if (gotFirstG == 1 && strncmp(command, "ET /", 4) == 0) {
255 strncpy(command, "GET", 4);
256 /* fall through to normal GET */
259 if ( strncmp(command, "quit", 4) == 0) {
260 printf("client sent quit command: shutting down!\n");
264 if ( strncmp(command, "break", 5) == 0) {
265 printf("client sent break command: closing session!\n");
269 if ( strncmp(command, "printstats", 10) == 0) {
274 if ( strncmp(command, "GET", 3) == 0) {
275 char type[] = "HTTP/1.0 200 ok\r\nContent-type:"
276 " text/html\r\n\r\n";
277 char header[] = "<html><body BGCOLOR=\"#ffffff\">\n<pre>\n";
278 char body[] = "greetings from CyaSSL\n";
279 char footer[] = "</body></html>\r\n\r\n";
281 strncpy(command, type, sizeof(type));
282 echoSz = sizeof(type) - 1;
284 strncpy(&command[echoSz], header, sizeof(header));
285 echoSz += (int)sizeof(header) - 1;
286 strncpy(&command[echoSz], body, sizeof(body));
287 echoSz += (int)sizeof(body) - 1;
288 strncpy(&command[echoSz], footer, sizeof(footer));
289 echoSz += (int)sizeof(footer);
291 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
292 err_sys("SSL_write failed");
298 fputs(command, fout);
301 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
302 err_sys("SSL_write failed");
305 CyaSSL_shutdown(ssl);
308 CloseSocket(clientfd);
310 tcp_listen(&sockfd, &port, useAnyAddr, doDTLS);
311 SignalReady(args, port);
316 CyaSSL_CTX_free(ctx);
323 ((func_args*)args)->return_code = 0;
328 /* so overall tests can pull in test function */
329 #ifndef NO_MAIN_DRIVER
331 int main(int argc, char** argv)
336 int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
338 err_sys("Cavium OpenNitroxDevice failed");
339 #endif /* HAVE_CAVIUM */
347 #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL)
348 CyaSSL_Debugging_ON();
350 if (CurrentDir("echoserver"))
352 else if (CurrentDir("Debug") || CurrentDir("Release"))
354 echoserver_test(&args);
358 CspShutdown(CAVIUM_DEV_ID);
360 return args.return_code;
364 #endif /* NO_MAIN_DRIVER */