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