1 /* main.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 #ifdef HAVE_CONFIG_H
23     #include <config.h>
24 #endif
25 
26 #include "wolfssl/wolfcrypt/settings.h"
27 
28 #include "rl_net.h"                      /* Network definitions                */
29 #include <time.h>
30 
31 #if defined(WOLFSSL_CMSIS_RTOS)
32     #include "cmsis_os.h"
33 #elif defined(WOLFSSL_CMSIS_RTOSv2)
34     #include "cmsis_os2.h"
35 #endif
36 
37 #if defined(STM32F7xx)
38 #include "stm32f7xx_hal.h"
39 #elif defined(STM32F4xx)
40 #include "stm32f4xx_hal.h"
41 #elif defined(STM32F2xx)
42 #include "stm32f2xx_hal.h"
43 #endif
44 
45 //-------- <<< Use Configuration Wizard in Context Menu >>> -----------------
46 
47 //   <h>Remote Address
48 //   ====================
49 //
50 //     <s.15>IP Address
51 //     <i>Static IPv4 Address
52 //     <i>Default: "192.168.1.1"
53 #define REMOTE_IP "192.168.10.4"
54 
55 //   <s.6>Port
56 //   <i> Default: "11111"
57 #define REMOTE_PORT "11111"
58 //   </h>
59 
60 //   <h>Protocol
61 //   ====================
62 
63 //   <o>SSL/TLS Version<0=> SSL3 <1=> TLS1.0 <2=> TLS1.1 <3=> TLS1.2 <4=> TLS1.3
64 #define TLS_VER 3
65 
66 //   <s.2>Other option
67 #define OTHER_OPTIONS ""
68 //   </h>
69 
70 //   <h>RTC: for validate certificate date
71 //    <o>Year <1970-2099>
72 #define RTC_YEAR 2019
73 //    <o>Month <1=>Jan<2=>Feb<3=>Mar<4=>Apr<5=>May<6=>Jun<7=>Jul<8=>Aug<9=>Sep<10=>Oct<11=>Nov<12=>Dec
74 #define RTC_MONTH 1
75 //    <o>Day <1-31>
76 #define RTC_DAY 1
77 //    </h>
78 
79 //------------- <<< end of configuration section >>> -----------------------
80 
81 #warning "write MPU specific Set ups\n"
SystemClock_Config(void)82 static void SystemClock_Config(void)
83 {
84 }
85 
MPU_Config(void)86 static void MPU_Config(void)
87 {
88 }
89 
CPU_CACHE_Enable(void)90 static void CPU_CACHE_Enable(void)
91 {
92 }
93 
94 #if defined(WOLFSSL_CMSIS_RTOS)
95 extern uint32_t os_time;
96 #endif
97 
HAL_GetTick(void)98 uint32_t HAL_GetTick(void) {
99     #if defined(WOLFSSL_CMSIS_RTOS)
100         return os_time;
101     #elif defined(WOLFSSL_CMSIS_RTOSv2)
102         return osKernelGetTickCount();
103     #endif
104 }
105 
106 static  time_t epochTime;
time(time_t * t)107 time_t time(time_t *t){
108      return epochTime ;
109 }
110 
setTime(time_t t)111 void setTime(time_t t){
112     epochTime = t;
113 }
114 
current_time(int reset)115 double current_time(int reset)
116 {
117     if (reset)
118         return 0;
119     #if defined(WOLFSSL_CMSIS_RTOS)
120         return (double)os_time / 1000.0;
121     #elif defined(WOLFSSL_CMSIS_RTOSv2)
122         return (double)osKernelGetTickCount() / 1000.0;
123     #endif
124 }
125 
126 /*-----------------------------------------------------------------------------
127  *        Initialize a Flash Memory Card
128  *----------------------------------------------------------------------------*/
129 #if !defined(NO_FILESYSTEM)
130 #include "rl_fs.h"                      /* FileSystem definitions             */
131 
init_filesystem(void)132 static void init_filesystem (void) {
133   int32_t retv;
134 
135   retv = finit ("M0:");
136   if (retv == fsOK) {
137     retv = fmount ("M0:");
138     if (retv == fsOK) {
139       printf ("Drive M0 ready!\n");
140     }
141     else {
142       printf ("Drive M0 mount failed(%d)!\n", retv);
143     }
144   }
145   else {
146     printf ("Drive M0 initialization failed!\n");
147   }
148 }
149 #endif
150 
151 extern void client_test(void const*arg);
152 
153 #if defined(WOLFSSL_CMSIS_RTOSv2)
app_main(void * arg)154 void app_main(void *arg)
155 #else
156 void app_main(void const*arg)
157 #endif
158 {
159     if(netInitialize () == netOK)
160 	      client_test(arg);
161 		else printf("ERROR: netInitialize\n");
162 }
163 
164 #if defined(WOLFSSL_CMSIS_RTOS)
165 osThreadDef(app_main, osPriorityLow, 1, 32*1024);
166 #endif
167 
168 /*----------------------------------------------------------------------------
169   Main Thread 'main': Run Network
170  *---------------------------------------------------------------------------*/
171 #include <stdio.h>
172 typedef struct func_args {
173     int    argc;
174     char** argv;
175 } func_args;
176 
177 
178 int myoptind = 0;
179 char* myoptarg = NULL;
180 
main(void)181 int main (void) {
182     static char *argv[] =
183         {   "client",   "-h", REMOTE_IP, "-p", REMOTE_PORT,
184                                    "-v",  " ",  OTHER_OPTIONS } ;
185     static   func_args args  =
186         {  sizeof(argv)/sizeof(*argv[0]), argv } ;
187 
188     char *verStr[] = { "SSL3", "TLS1.0", "TLS1.1", "TLS1.2", "TLS1.3"};
189     #define VERSIZE 2
190     static char ver[VERSIZE];
191 
192     MPU_Config();                             /* Configure the MPU              */
193     CPU_CACHE_Enable();                       /* Enable the CPU Cache           */
194     HAL_Init();                               /* Initialize the HAL Library     */
195     SystemClock_Config();                     /* Configure the System Clock     */
196     #if defined(WOLFSSL_CMSIS_RTOSv2)
197     osKernelInitialize();
198     #endif
199 
200     #if !defined(NO_FILESYSTEM)
201     init_filesystem ();
202     #endif
203 
204     #if defined(DEBUG_WOLFSSL)
205         printf("Turning ON Debug message\n") ;
206         wolfSSL_Debugging_ON() ;
207     #endif
208 
209     snprintf(ver, VERSIZE, "%d", TLS_VER);
210     argv[6] = ver;
211 
212     printf("SSL/TLS Client(%d)\n ", (int)(sizeof(argv)/sizeof(argv[0]))) ;
213     printf("    Remote IP: %s, Port: %s\n    Version: %s\n", argv[2], argv[4],  verStr[TLS_VER]) ;
214     printf("    Other options: %s\n", OTHER_OPTIONS);
215     setTime((time_t)((RTC_YEAR-1970)*365*24*60*60) + RTC_MONTH*30*24*60*60 + RTC_DAY*24*60*60);
216 
217     #if defined(WOLFSSL_CMSIS_RTOS)
218         osThreadCreate (osThread(app_main), (void *)&args);
219     #elif defined(WOLFSSL_CMSIS_RTOSv2)
220         osThreadNew(app_main, (void *)&args, NULL);
221     #endif
222     osKernelStart();
223 
224 }
225 
226