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