]> git.sur5r.net Git - freertos/blob - FreeRTOS-Plus/Source/CyaSSL/examples/echoclient/echoclient.c
Final commit before tagging - cosmetic changes only.
[freertos] / FreeRTOS-Plus / Source / CyaSSL / examples / echoclient / echoclient.c
1 /* echoclient.c
2  *
3  * Copyright (C) 2006-2014 wolfSSL Inc.
4  *
5  * This file is part of CyaSSL.
6  *
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.
11  *
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.
16  *
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
20  */
21
22 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25
26 #include <cyassl/ctaocrypt/settings.h>
27
28 #include <cyassl/openssl/ssl.h>
29
30 #if defined(CYASSL_MDK_ARM)
31         #include <stdio.h>
32         #include <string.h>
33
34         #if defined(CYASSL_MDK5)
35             #include "cmsis_os.h"
36             #include "rl_fs.h" 
37             #include "rl_net.h" 
38         #else
39             #include "rtl.h"
40         #endif
41
42         #include "cyassl_MDK_ARM.h"
43 #endif
44
45 #include <cyassl/test.h>
46
47 #include "examples/echoclient/echoclient.h"
48
49 void echoclient_test(void* args)
50 {
51     SOCKET_T sockfd = 0;
52
53     FILE* fin   = stdin  ;
54     FILE* fout = stdout;
55
56     int inCreated  = 0;
57     int outCreated = 0;
58
59     char msg[1024];
60     char reply[1024+1];
61
62     SSL_METHOD* method = 0;
63     SSL_CTX*    ctx    = 0;
64     SSL*        ssl    = 0;
65
66     int doDTLS = 0;
67     int doPSK = 0;
68     int sendSz;
69     int argc    = 0;
70     char** argv = 0;
71     word16 port = yasslPort;
72
73     ((func_args*)args)->return_code = -1; /* error state */
74     
75 #ifndef CYASSL_MDK_SHELL
76     argc = ((func_args*)args)->argc;
77     argv = ((func_args*)args)->argv;
78 #endif
79
80     if (argc >= 2) {
81         fin  = fopen(argv[1], "r"); 
82         inCreated = 1;
83     }
84     if (argc >= 3) {
85         fout = fopen(argv[2], "w");
86         outCreated = 1;
87     }
88
89     if (!fin)  err_sys("can't open input file");
90     if (!fout) err_sys("can't open output file");
91
92 #ifdef CYASSL_DTLS
93     doDTLS  = 1;
94 #endif
95
96 #ifdef CYASSL_LEANPSK 
97     doPSK = 1;
98 #endif
99
100 #if defined(NO_RSA) && !defined(HAVE_ECC)
101     doPSK = 1;
102 #endif
103
104 #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(CYASSL_MDK_SHELL)
105     port = ((func_args*)args)->signal->port;
106 #endif
107
108 #if defined(CYASSL_DTLS)
109     method  = DTLSv1_client_method();
110 #elif  !defined(NO_TLS)
111     method = CyaSSLv23_client_method();
112 #else
113     method = SSLv3_client_method();
114 #endif
115     ctx    = SSL_CTX_new(method);
116
117 #ifndef NO_FILESYSTEM
118     #ifndef NO_RSA
119     if (SSL_CTX_load_verify_locations(ctx, caCert, 0) != SSL_SUCCESS)
120         err_sys("can't load ca file, Please run from CyaSSL home dir");
121     #endif
122     #ifdef HAVE_ECC
123         if (SSL_CTX_load_verify_locations(ctx, eccCert, 0) != SSL_SUCCESS)
124             err_sys("can't load ca file, Please run from CyaSSL home dir");
125     #endif
126 #elif !defined(NO_CERTS)
127     if (!doPSK)
128         load_buffer(ctx, caCert, CYASSL_CA);
129 #endif
130
131 #if defined(CYASSL_SNIFFER) && !defined(HAVE_NTRU) && !defined(HAVE_ECC)
132     /* don't use EDH, can't sniff tmp keys */
133     SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
134 #endif
135     if (doPSK) {
136 #ifndef NO_PSK
137         const char *defaultCipherList;
138
139         CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
140         #ifdef HAVE_NULL_CIPHER
141             defaultCipherList = "PSK-NULL-SHA256";
142         #else
143             defaultCipherList = "PSK-AES128-CBC-SHA256";
144         #endif
145         if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=SSL_SUCCESS)
146             err_sys("client can't set cipher list 2");
147 #endif
148     }
149
150 #if defined(OPENSSL_EXTRA) || defined(HAVE_WEBSERVER)
151     SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
152 #endif
153
154     #if defined(CYASSL_MDK_ARM)
155     CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0);
156     #endif
157
158     ssl = SSL_new(ctx);
159         
160
161     if (doDTLS) {
162         SOCKADDR_IN_T addr;
163         build_addr(&addr, yasslIP, port, 1);
164         CyaSSL_dtls_set_peer(ssl, &addr, sizeof(addr));
165         tcp_socket(&sockfd, 1);
166     }
167     else {
168         tcp_connect(&sockfd, yasslIP, port, 0);
169     }
170         
171     SSL_set_fd(ssl, sockfd);
172 #if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
173     /* let echoserver bind first, TODO: add Windows signal like pthreads does */
174     Sleep(100);
175 #endif
176
177     if (SSL_connect(ssl) != SSL_SUCCESS) err_sys("SSL_connect failed");
178
179     while (fgets(msg, sizeof(msg), fin) != 0) {
180      
181         sendSz = (int)strlen(msg);
182
183         if (SSL_write(ssl, msg, sendSz) != sendSz)
184             err_sys("SSL_write failed");
185
186         if (strncmp(msg, "quit", 4) == 0) {
187             fputs("sending server shutdown command: quit!\n", fout);
188             break;
189         }
190
191         if (strncmp(msg, "break", 5) == 0) {
192             fputs("sending server session close: break!\n", fout);
193             break;
194         }
195
196         #ifndef CYASSL_MDK_SHELL
197         while (sendSz) {
198             int got;
199             if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
200                 reply[got] = 0;
201                 fputs(reply, fout);
202                 fflush(fout) ;
203                 sendSz -= got;
204             }
205             else
206                 break;
207         }
208         #else
209         {
210             int got;
211             if ( (got = SSL_read(ssl, reply, sizeof(reply)-1)) > 0) {
212                 reply[got] = 0;
213                 fputs(reply, fout);
214                 fflush(fout) ;
215                 sendSz -= got;
216             }
217         }
218         #endif
219     }
220
221
222 #ifdef CYASSL_DTLS
223     strncpy(msg, "break", 6);
224     sendSz = (int)strlen(msg);
225     /* try to tell server done */
226     SSL_write(ssl, msg, sendSz);
227 #else
228     SSL_shutdown(ssl);
229 #endif
230
231     SSL_free(ssl);
232     SSL_CTX_free(ctx);
233
234     fflush(fout);
235     if (inCreated)  fclose(fin);
236     if (outCreated) fclose(fout);
237
238     CloseSocket(sockfd);
239     ((func_args*)args)->return_code = 0; 
240 }
241
242
243 /* so overall tests can pull in test function */
244 #ifndef NO_MAIN_DRIVER
245
246     int main(int argc, char** argv)
247     {
248         func_args args;
249
250 #ifdef HAVE_CAVIUM
251         int ret = OpenNitroxDevice(CAVIUM_DIRECT, CAVIUM_DEV_ID);
252         if (ret != 0)
253             err_sys("Cavium OpenNitroxDevice failed");
254 #endif /* HAVE_CAVIUM */
255
256         StartTCP();
257
258         args.argc = argc;
259         args.argv = argv;
260
261         CyaSSL_Init();
262 #if defined(DEBUG_CYASSL) && !defined(CYASSL_MDK_SHELL)
263         CyaSSL_Debugging_ON();
264 #endif
265
266         if (CurrentDir("echoclient"))
267             ChangeDirBack(2);
268         else if (CurrentDir("Debug") || CurrentDir("Release"))
269             ChangeDirBack(3);
270         echoclient_test(&args);
271
272         CyaSSL_Cleanup();
273
274 #ifdef HAVE_CAVIUM
275         CspShutdown(CAVIUM_DEV_ID);
276 #endif
277         return args.return_code;
278     }
279         
280 #endif /* NO_MAIN_DRIVER */
281
282