3 * Copyright (C) 2006-2012 Sawtooth Consulting Ltd.
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
26 #include <cyassl/ssl.h>
27 #include <cyassl/test.h>
29 #ifndef NO_MAIN_DRIVER
35 CYASSL_API void PrintSessionStats(void);
39 static void SignalReady(void* args)
41 #if defined(_POSIX_THREADS) && defined(NO_MAIN_DRIVER)
42 /* signal ready to tcp_accept */
43 func_args* server_args = (func_args*)args;
44 tcp_ready* ready = server_args->signal;
45 pthread_mutex_lock(&ready->mutex);
47 pthread_cond_signal(&ready->cond);
48 pthread_mutex_unlock(&ready->mutex);
53 THREAD_RETURN CYASSL_THREAD echoserver_test(void* args)
56 CYASSL_METHOD* method = 0;
61 int argc = ((func_args*)args)->argc;
62 char** argv = ((func_args*)args)->argv;
67 fout = fopen(argv[1], "w");
70 if (!fout) err_sys("can't open output file");
73 ((func_args*)args)->return_code = -1; /* error state */
77 #if defined(CYASSL_DTLS)
78 method = CyaDTLSv1_server_method();
79 #elif !defined(NO_TLS)
80 method = CyaSSLv23_server_method();
82 method = CyaSSLv3_server_method();
84 ctx = CyaSSL_CTX_new(method);
85 /* CyaSSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); */
88 CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
94 if (CyaSSL_CTX_use_certificate_file(ctx, ntruCert, SSL_FILETYPE_PEM)
96 err_sys("can't load ntru cert file, "
97 "Please run from CyaSSL home dir");
99 if (CyaSSL_CTX_use_NTRUPrivateKey_file(ctx, ntruKey)
101 err_sys("can't load ntru key file, "
102 "Please run from CyaSSL home dir");
105 if (CyaSSL_CTX_use_certificate_file(ctx, eccCert, SSL_FILETYPE_PEM)
107 err_sys("can't load server cert file, "
108 "Please run from CyaSSL home dir");
110 if (CyaSSL_CTX_use_PrivateKey_file(ctx, eccKey, SSL_FILETYPE_PEM)
112 err_sys("can't load server key file, "
113 "Please run from CyaSSL home dir");
116 if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
118 err_sys("can't load server cert file, "
119 "Please run from CyaSSL home dir");
121 if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
123 err_sys("can't load server key file, "
124 "Please run from CyaSSL home dir");
127 load_buffer(ctx, svrCert, CYASSL_CERT);
128 load_buffer(ctx, svrKey, CYASSL_KEY);
142 SOCKADDR_IN_T client;
143 socklen_t client_len = sizeof(client);
144 clientfd = accept(sockfd, (struct sockaddr*)&client,
145 (ACCEPT_THIRD_T)&client_len);
147 clientfd = udp_read_connect(sockfd);
149 if (clientfd == -1) err_sys("tcp accept failed");
151 ssl = CyaSSL_new(ctx);
152 if (ssl == NULL) err_sys("SSL_new failed");
153 CyaSSL_set_fd(ssl, clientfd);
154 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
155 CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
157 SetDH(ssl); /* will repick suites with DHE, higher than PSK */
159 if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
160 printf("SSL_accept failed\n");
162 CloseSocket(clientfd);
165 #if defined(PEER_INFO)
169 while ( (echoSz = CyaSSL_read(ssl, command, sizeof(command))) > 0) {
171 if (firstRead == 1) {
172 firstRead = 0; /* browser may send 1 byte 'G' to start */
173 if (echoSz == 1 && command[0] == 'G') {
178 else if (gotFirstG == 1 && strncmp(command, "ET /", 4) == 0) {
179 strncpy(command, "GET", 4);
180 /* fall through to normal GET */
183 if ( strncmp(command, "quit", 4) == 0) {
184 printf("client sent quit command: shutting down!\n");
188 if ( strncmp(command, "break", 5) == 0) {
189 printf("client sent break command: closing session!\n");
193 if ( strncmp(command, "printstats", 10) == 0) {
198 if ( strncmp(command, "GET", 3) == 0) {
199 char type[] = "HTTP/1.0 200 ok\r\nContent-type:"
200 " text/html\r\n\r\n";
201 char header[] = "<html><body BGCOLOR=\"#ffffff\">\n<pre>\n";
202 char body[] = "greetings from CyaSSL\n";
203 char footer[] = "</body></html>\r\n\r\n";
205 strncpy(command, type, sizeof(type));
206 echoSz = sizeof(type) - 1;
208 strncpy(&command[echoSz], header, sizeof(header));
209 echoSz += sizeof(header) - 1;
210 strncpy(&command[echoSz], body, sizeof(body));
211 echoSz += sizeof(body) - 1;
212 strncpy(&command[echoSz], footer, sizeof(footer));
213 echoSz += sizeof(footer);
215 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
216 err_sys("SSL_write failed");
222 fputs(command, fout);
225 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
226 err_sys("SSL_write failed");
229 CyaSSL_shutdown(ssl);
232 CloseSocket(clientfd);
240 CyaSSL_CTX_free(ctx);
247 ((func_args*)args)->return_code = 0;
252 /* so overall tests can pull in test function */
253 #ifndef NO_MAIN_DRIVER
255 int main(int argc, char** argv)
266 CyaSSL_Debugging_ON();
268 if (CurrentDir("echoserver") || CurrentDir("build"))
270 echoserver_test(&args);
273 return args.return_code;
276 #endif /* NO_MAIN_DRIVER */