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;
63 int argc = ((func_args*)args)->argc;
64 char** argv = ((func_args*)args)->argv;
69 fout = fopen(argv[1], "w");
72 if (!fout) err_sys("can't open output file");
75 ((func_args*)args)->return_code = -1; /* error state */
81 tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
83 #if defined(CYASSL_DTLS)
84 method = CyaDTLSv1_server_method();
85 #elif !defined(NO_TLS)
86 method = CyaSSLv23_server_method();
88 method = CyaSSLv3_server_method();
90 ctx = CyaSSL_CTX_new(method);
91 /* CyaSSL_CTX_set_session_cache_mode(ctx, SSL_SESS_CACHE_OFF); */
94 CyaSSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
100 if (CyaSSL_CTX_use_certificate_file(ctx, ntruCert, SSL_FILETYPE_PEM)
102 err_sys("can't load ntru cert file, "
103 "Please run from CyaSSL home dir");
105 if (CyaSSL_CTX_use_NTRUPrivateKey_file(ctx, ntruKey)
107 err_sys("can't load ntru key file, "
108 "Please run from CyaSSL home dir");
111 if (CyaSSL_CTX_use_certificate_file(ctx, eccCert, SSL_FILETYPE_PEM)
113 err_sys("can't load server cert file, "
114 "Please run from CyaSSL home dir");
116 if (CyaSSL_CTX_use_PrivateKey_file(ctx, eccKey, SSL_FILETYPE_PEM)
118 err_sys("can't load server key file, "
119 "Please run from CyaSSL home dir");
122 if (CyaSSL_CTX_use_certificate_file(ctx, svrCert, SSL_FILETYPE_PEM)
124 err_sys("can't load server cert file, "
125 "Please run from CyaSSL home dir");
127 if (CyaSSL_CTX_use_PrivateKey_file(ctx, svrKey, SSL_FILETYPE_PEM)
129 err_sys("can't load server key file, "
130 "Please run from CyaSSL home dir");
133 load_buffer(ctx, svrCert, CYASSL_CERT);
134 load_buffer(ctx, svrKey, CYASSL_KEY);
137 #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
138 /* don't use EDH, can't sniff tmp keys */
139 CyaSSL_CTX_set_cipher_list(ctx, "AES256-SHA");
153 SOCKADDR_IN_T client;
154 socklen_t client_len = sizeof(client);
155 clientfd = accept(sockfd, (struct sockaddr*)&client,
156 (ACCEPT_THIRD_T)&client_len);
158 clientfd = udp_read_connect(sockfd);
160 if (clientfd == -1) err_sys("tcp accept failed");
162 ssl = CyaSSL_new(ctx);
163 if (ssl == NULL) err_sys("SSL_new failed");
164 CyaSSL_set_fd(ssl, clientfd);
165 #if !defined(NO_FILESYSTEM) && defined(OPENSSL_EXTRA)
166 CyaSSL_SetTmpDH_file(ssl, dhParam, SSL_FILETYPE_PEM);
168 SetDH(ssl); /* will repick suites with DHE, higher than PSK */
170 if (CyaSSL_accept(ssl) != SSL_SUCCESS) {
171 printf("SSL_accept failed\n");
173 CloseSocket(clientfd);
176 #if defined(PEER_INFO)
180 while ( (echoSz = CyaSSL_read(ssl, command, sizeof(command))) > 0) {
182 if (firstRead == 1) {
183 firstRead = 0; /* browser may send 1 byte 'G' to start */
184 if (echoSz == 1 && command[0] == 'G') {
189 else if (gotFirstG == 1 && strncmp(command, "ET /", 4) == 0) {
190 strncpy(command, "GET", 4);
191 /* fall through to normal GET */
194 if ( strncmp(command, "quit", 4) == 0) {
195 printf("client sent quit command: shutting down!\n");
199 if ( strncmp(command, "break", 5) == 0) {
200 printf("client sent break command: closing session!\n");
204 if ( strncmp(command, "printstats", 10) == 0) {
209 if ( strncmp(command, "GET", 3) == 0) {
210 char type[] = "HTTP/1.0 200 ok\r\nContent-type:"
211 " text/html\r\n\r\n";
212 char header[] = "<html><body BGCOLOR=\"#ffffff\">\n<pre>\n";
213 char body[] = "greetings from CyaSSL\n";
214 char footer[] = "</body></html>\r\n\r\n";
216 strncpy(command, type, sizeof(type));
217 echoSz = sizeof(type) - 1;
219 strncpy(&command[echoSz], header, sizeof(header));
220 echoSz += sizeof(header) - 1;
221 strncpy(&command[echoSz], body, sizeof(body));
222 echoSz += sizeof(body) - 1;
223 strncpy(&command[echoSz], footer, sizeof(footer));
224 echoSz += sizeof(footer);
226 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
227 err_sys("SSL_write failed");
233 fputs(command, fout);
236 if (CyaSSL_write(ssl, command, echoSz) != echoSz)
237 err_sys("SSL_write failed");
240 CyaSSL_shutdown(ssl);
243 CloseSocket(clientfd);
245 tcp_listen(&sockfd, yasslPort, useAnyAddr, doDTLS);
251 CyaSSL_CTX_free(ctx);
258 ((func_args*)args)->return_code = 0;
263 /* so overall tests can pull in test function */
264 #ifndef NO_MAIN_DRIVER
266 int main(int argc, char** argv)
277 CyaSSL_Debugging_ON();
279 if (CurrentDir("echoserver") || CurrentDir("build"))
281 echoserver_test(&args);
284 return args.return_code;
288 char* myoptarg = NULL;
290 #endif /* NO_MAIN_DRIVER */