1 /* echoclient.c
2  *
3  * Copyright (C) 2006-2021 wolfSSL Inc.
4  *
5  * This file is part of wolfSSL.
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-1335, USA
20  */
21 
22 
23 #ifdef HAVE_CONFIG_H
24     #include <config.h>
25 #endif
26 
27 #include <cyassl/ctaocrypt/settings.h>
28 /* let's use cyassl layer AND cyassl openssl layer */
29 #include <cyassl/ssl.h>
30 #include <cyassl/openssl/ssl.h>
31 #ifdef CYASSL_DTLS
32     #include <cyassl/error-ssl.h>
33 #endif
34 
35 #if defined(WOLFSSL_MDK_ARM) || defined(WOLFSSL_KEIL_TCP_NET)
36         #include <stdio.h>
37         #include <string.h>
38         #include "cmsis_os.h"
39         #include "rl_fs.h"
40         #include "rl_net.h"
41         #include "wolfssl_MDK_ARM.h"
42 #endif
43 
44 #include <cyassl/test.h>
45 
46 #include <examples/echoclient/echoclient.h>
47 
48 #ifndef NO_WOLFSSL_CLIENT
49 
50 
51 #ifdef NO_FILESYSTEM
52 #ifdef NO_RSA
53 #error currently the example only tries to load in a RSA buffer
54 #endif
55 #undef USE_CERT_BUFFERS_2048
56 #define USE_CERT_BUFFERS_2048
57 #include <wolfssl/certs_test.h>
58 #endif
59 
60 #ifdef WOLFSSL_ASYNC_CRYPT
61     static int devId = INVALID_DEVID;
62 #endif
63 
64 
echoclient_test(void * args)65 void echoclient_test(void* args)
66 {
67     SOCKET_T sockfd = 0;
68 
69     FILE* fin   = stdin  ;
70     FILE* fout = stdout;
71 
72 #ifndef WOLFSSL_MDK_SHELL
73     int inCreated  = 0;
74     int outCreated = 0;
75 #endif
76 
77     char msg[1024];
78     char reply[1024+1];
79 
80     SSL_METHOD* method = 0;
81     SSL_CTX*    ctx    = 0;
82     SSL*        ssl    = 0;
83 
84     int ret = 0, err = 0;
85     int doDTLS = 0;
86     int doPSK = 0;
87     int sendSz;
88 #ifndef WOLFSSL_MDK_SHELL
89     int argc    = 0;
90     char** argv = 0;
91 #endif
92     word16 port;
93     char buffer[CYASSL_MAX_ERROR_SZ];
94 
95     ((func_args*)args)->return_code = -1; /* error state */
96 
97 #ifndef WOLFSSL_MDK_SHELL
98     argc = ((func_args*)args)->argc;
99     argv = ((func_args*)args)->argv;
100 
101     if (argc >= 2) {
102         fin  = fopen(argv[1], "r");
103         inCreated = 1;
104     }
105     if (argc >= 3) {
106         fout = fopen(argv[2], "w");
107         outCreated = 1;
108     }
109 #endif
110 
111     if (!fin)  err_sys("can't open input file");
112     if (!fout) err_sys("can't open output file");
113 
114 #ifdef CYASSL_DTLS
115     doDTLS  = 1;
116 #endif
117 
118 #ifdef CYASSL_LEANPSK
119     doPSK = 1;
120 #endif
121 #if defined(NO_RSA) && !defined(HAVE_ECC) && !defined(HAVE_ED25519) && \
122                                                             !defined(HAVE_ED448)
123     doPSK = 1;
124 #endif
125     (void)doPSK;
126 
127 #if defined(NO_MAIN_DRIVER) && !defined(USE_WINDOWS_API) && !defined(WOLFSSL_MDK_SHELL)
128     port = ((func_args*)args)->signal->port;
129 #else
130     port = yasslPort;
131 #endif
132 
133 #if defined(CYASSL_DTLS)
134     method  = DTLSv1_2_client_method();
135 #elif !defined(NO_TLS)
136     #if defined(WOLFSSL_TLS13) && defined(WOLFSSL_SNIFFER)
137     method = CyaTLSv1_2_client_method();
138     #else
139     method = CyaSSLv23_client_method();
140     #endif
141 #elif defined(WOLFSSL_ALLOW_SSLV3)
142     method = SSLv3_client_method();
143 #else
144     #error "no valid client method type"
145 #endif
146     ctx    = SSL_CTX_new(method);
147 
148 #ifndef NO_FILESYSTEM
149     #ifndef NO_RSA
150     if (SSL_CTX_load_verify_locations(ctx, caCertFile, 0) != WOLFSSL_SUCCESS)
151         err_sys("can't load ca file, Please run from wolfSSL home dir");
152     #endif
153     #ifdef HAVE_ECC
154         if (SSL_CTX_load_verify_locations(ctx, caEccCertFile, 0) != WOLFSSL_SUCCESS)
155             err_sys("can't load ca file, Please run from wolfSSL home dir");
156     #elif defined(HAVE_ED25519)
157         if (SSL_CTX_load_verify_locations(ctx, caEdCertFile, 0) != WOLFSSL_SUCCESS)
158             err_sys("can't load ca file, Please run from wolfSSL home dir");
159     #elif defined(HAVE_ED448)
160         if (SSL_CTX_load_verify_locations(ctx, caEd448CertFile, 0) != WOLFSSL_SUCCESS)
161             err_sys("can't load ca file, Please run from wolfSSL home dir");
162     #endif
163 #elif !defined(NO_CERTS)
164     if (!doPSK)
165         if (wolfSSL_CTX_load_verify_buffer(ctx, ca_cert_der_2048,
166             sizeof_ca_cert_der_2048, WOLFSSL_FILETYPE_ASN1) != WOLFSSL_SUCCESS)
167             err_sys("can't load ca buffer");
168 #endif
169 
170 #if defined(CYASSL_SNIFFER)
171     /* Only set if not running testsuite */
172     if (XSTRSTR(argv[0], "testsuite") != 0) {
173         /* don't use EDH, can't sniff tmp keys */
174         SSL_CTX_set_cipher_list(ctx, "AES256-SHA");
175     }
176 #endif
177 #ifndef NO_PSK
178     if (doPSK) {
179         const char *defaultCipherList;
180 
181         CyaSSL_CTX_set_psk_client_callback(ctx, my_psk_client_cb);
182         #ifdef HAVE_NULL_CIPHER
183             defaultCipherList = "PSK-NULL-SHA256";
184         #elif defined(HAVE_AESGCM) && !defined(NO_DH)
185             #ifdef WOLFSSL_TLS13
186             defaultCipherList = "TLS13-AES128-GCM-SHA256"
187                 #ifndef WOLFSSL_NO_TLS12
188                                 ":DHE-PSK-AES128-GCM-SHA256"
189                 #endif
190                 ;
191             #else
192             defaultCipherList = "DHE-PSK-AES128-GCM-SHA256";
193             #endif
194         #elif defined(HAVE_AESGCM) && defined(WOLFSSL_TLS13)
195             defaultCipherList = "TLS13-AES128-GCM-SHA256"
196                 #ifndef WOLFSSL_NO_TLS12
197                                 ":DHE-PSK-AES128-GCM-SHA256"
198                 #endif
199                 ;
200         #else
201             defaultCipherList = "PSK-AES128-CBC-SHA256";
202         #endif
203         if (CyaSSL_CTX_set_cipher_list(ctx,defaultCipherList) !=WOLFSSL_SUCCESS)
204             err_sys("client can't set cipher list 2");
205         wolfSSL_CTX_set_psk_callback_ctx(ctx, (void*)defaultCipherList);
206     }
207 #endif
208 
209 #ifdef WOLFSSL_ENCRYPTED_KEYS
210     SSL_CTX_set_default_passwd_cb(ctx, PasswordCallBack);
211 #endif
212 
213 #if defined(WOLFSSL_MDK_ARM)
214     CyaSSL_CTX_set_verify(ctx, WOLFSSL_VERIFY_NONE, 0);
215 #endif
216 
217 #ifdef WOLFSSL_ASYNC_CRYPT
218     ret = wolfAsync_DevOpen(&devId);
219     if (ret < 0) {
220         printf("Async device open failed\nRunning without async\n");
221     }
222     wolfSSL_CTX_SetDevId(ctx, devId);
223 #endif /* WOLFSSL_ASYNC_CRYPT */
224 
225     ssl = SSL_new(ctx);
226     tcp_connect(&sockfd, yasslIP, port, doDTLS, 0, ssl);
227 
228     SSL_set_fd(ssl, sockfd);
229 #if defined(USE_WINDOWS_API) && defined(CYASSL_DTLS) && defined(NO_MAIN_DRIVER)
230     /* let echoserver bind first, TODO: add Windows signal like pthreads does */
231     Sleep(100);
232 #endif
233 
234     do {
235         err = 0; /* Reset error */
236         ret = SSL_connect(ssl);
237         if (ret != WOLFSSL_SUCCESS) {
238             err = SSL_get_error(ssl, 0);
239         #ifdef WOLFSSL_ASYNC_CRYPT
240             if (err == WC_PENDING_E) {
241                 ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
242                 if (ret < 0) break;
243             }
244         #endif
245         }
246     } while (err == WC_PENDING_E);
247     if (ret != WOLFSSL_SUCCESS) {
248         printf("SSL_connect error %d, %s\n", err,
249             ERR_error_string(err, buffer));
250         err_sys("SSL_connect failed");
251     }
252 
253     while (fgets(msg, sizeof(msg), fin) != 0) {
254 
255         sendSz = (int)XSTRLEN(msg);
256 
257         do {
258             err = 0; /* reset error */
259             ret = SSL_write(ssl, msg, sendSz);
260             if (ret <= 0) {
261                 err = SSL_get_error(ssl, 0);
262             #ifdef WOLFSSL_ASYNC_CRYPT
263                 if (err == WC_PENDING_E) {
264                     ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
265                     if (ret < 0) break;
266                 }
267             #endif
268             }
269         } while (err == WC_PENDING_E);
270         if (ret != sendSz) {
271             printf("SSL_write msg error %d, %s\n", err,
272                 ERR_error_string(err, buffer));
273             err_sys("SSL_write failed");
274         }
275 
276         if (strncmp(msg, "quit", 4) == 0) {
277             fputs("sending server shutdown command: quit!\n", fout);
278             break;
279         }
280 
281         if (strncmp(msg, "break", 5) == 0) {
282             fputs("sending server session close: break!\n", fout);
283             break;
284         }
285 
286     #ifndef WOLFSSL_MDK_SHELL
287         while (sendSz)
288     #endif
289         {
290             do {
291                 err = 0; /* reset error */
292                 ret = SSL_read(ssl, reply, sizeof(reply)-1);
293                 if (ret <= 0) {
294                     err = SSL_get_error(ssl, 0);
295                 #ifdef WOLFSSL_ASYNC_CRYPT
296                     if (err == WC_PENDING_E) {
297                         ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
298                         if (ret < 0) break;
299                     }
300                 #endif
301                 }
302             } while (err == WC_PENDING_E);
303             if (ret > 0) {
304                 reply[ret] = 0;
305                 fputs(reply, fout);
306                 fflush(fout) ;
307                 sendSz -= ret;
308             }
309 #ifdef CYASSL_DTLS
310             else if (wolfSSL_dtls(ssl) && err == DECRYPT_ERROR) {
311                 /* This condition is OK. The packet should be dropped
312                  * silently when there is a decrypt or MAC error on
313                  * a DTLS record. */
314                 sendSz = 0;
315             }
316 #endif
317             else {
318                 printf("SSL_read msg error %d, %s\n", err,
319                     ERR_error_string(err, buffer));
320                 err_sys("SSL_read failed");
321             }
322         }
323     }
324 
325 
326 #ifdef CYASSL_DTLS
327     strncpy(msg, "break", 6);
328     sendSz = (int)strlen(msg);
329     /* try to tell server done */
330     do {
331         err = 0; /* reset error */
332         ret = SSL_write(ssl, msg, sendSz);
333         if (ret <= 0) {
334             err = SSL_get_error(ssl, 0);
335         #ifdef WOLFSSL_ASYNC_CRYPT
336             if (err == WC_PENDING_E) {
337                 ret = wolfSSL_AsyncPoll(ssl, WOLF_POLL_FLAG_CHECK_HW);
338                 if (ret < 0) break;
339             }
340         #endif
341         }
342     } while (err == WC_PENDING_E);
343 #else
344     SSL_shutdown(ssl);
345 #endif
346 
347     SSL_free(ssl);
348     SSL_CTX_free(ctx);
349 
350 #ifdef WOLFSSL_ASYNC_CRYPT
351     wolfAsync_DevClose(&devId);
352 #endif
353 
354     fflush(fout);
355 #ifndef WOLFSSL_MDK_SHELL
356     if (inCreated)  fclose(fin);
357     if (outCreated) fclose(fout);
358 #endif
359 
360     CloseSocket(sockfd);
361     ((func_args*)args)->return_code = 0;
362 }
363 
364 #endif /* !NO_WOLFSSL_CLIENT */
365 
366 /* so overall tests can pull in test function */
367 #ifndef NO_MAIN_DRIVER
368 
main(int argc,char ** argv)369     int main(int argc, char** argv)
370     {
371         func_args args;
372 
373 #ifdef HAVE_WNR
374         if (wc_InitNetRandom(wnrConfig, NULL, 5000) != 0)
375             err_sys("Whitewood netRandom global config failed");
376 #endif
377 
378         StartTCP();
379 
380         args.argc = argc;
381         args.argv = argv;
382         args.return_code = 0;
383 
384         CyaSSL_Init();
385 #if defined(DEBUG_CYASSL) && !defined(WOLFSSL_MDK_SHELL)
386         CyaSSL_Debugging_ON();
387 #endif
388 #ifndef CYASSL_TIRTOS
389         ChangeToWolfRoot();
390 #endif
391 #ifndef NO_WOLFSSL_CLIENT
392         echoclient_test(&args);
393 #endif
394 
395         CyaSSL_Cleanup();
396 
397 #ifdef HAVE_WNR
398         if (wc_FreeNetRandom() < 0)
399             err_sys("Failed to free netRandom context");
400 #endif /* HAVE_WNR */
401 
402         return args.return_code;
403     }
404 
405 #endif /* NO_MAIN_DRIVER */
406