File: main.c

package info (click to toggle)
wolfssl 4.6.0%2Bp1-0%2Bdeb11u2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 52,520 kB
  • sloc: ansic: 601,530; asm: 126,822; sh: 6,275; xml: 3,342; cs: 2,752; python: 1,555; makefile: 915; javascript: 748; perl: 289; cpp: 97; objc: 80; tcl: 73
file content (246 lines) | stat: -rw-r--r-- 6,158 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/* main.c
 *
 * Copyright (C) 2006-2020 wolfSSL Inc.
 *
 * This file is part of wolfSSL.
 *
 * wolfSSL is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * wolfSSL is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include "wolfssl/wolfcrypt/settings.h"

#include "rl_net.h"                      /* Network definitions                */
#include <time.h>

#if defined(WOLFSSL_CMSIS_RTOS)
#include "cmsis_os.h"
#elif defined(WOLFSSL_CMSIS_RTOSv2)
#include "cmsis_os2.h"
#endif

/* Dummy definition for test RTC */
#define RTC_YEAR 2019
#if defined(STM32F7xx)
#include "stm32f7xx_hal.h"
#elif defined(STM32F4xx)
#include "stm32f4xx_hal.h"
#elif defined(STM32F2xx)
#include "stm32f2xx_hal.h"
#endif

//-------- <<< Use Configuration Wizard in Context Menu >>> -----------------

//   <h>Server parameter
//   ====================

//   <s.6>Port
//   <i> Default: "11111"
#define SERVER_PORT "11111"
//   </h>

//   <h>Protocol
//   ====================

//   <o>SSL/TLS Version<0=> SSL3 <1=> TLS1.0 <2=> TLS1.1 <3=> TLS1.2 <4=> TLS1.3
#define TLS_VER 3

//   <s.2>Other option
#define OTHER_OPTIONS ""
//   </h>

//   <h>RTC: for validate certificate date
//    <o>Year <1970-2099>
#define RTC_YEAR 2019
//    <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
#define RTC_MONTH 1
//    <o>Day <1-31>
#define RTC_DAY 1
//    </h>

//------------- <<< end of configuration section >>> -----------------------

#warning "write MPU specific Set ups\n"
static void SystemClock_Config (void) {

}

static void MPU_Config (void) {

}

static void CPU_CACHE_Enable (void) {

}

/*-----------------------------------------------------------------------------
 *        Initialize a Flash Memory Card
 *----------------------------------------------------------------------------*/
#if !defined(NO_FILESYSTEM)
#include "rl_fs.h"                      /* FileSystem definitions             */

static void init_filesystem (void) {
  int32_t retv;

  retv = finit ("M0:");
  if (retv == fsOK) {
    retv = fmount ("M0:");
    if (retv == fsOK) {
      printf ("Drive M0 ready!\n");
    }
    else {
      printf ("Drive M0 mount failed(%d)!\n", retv);
    }
  }
  else {
    printf ("Drive M0 initialization failed!\n");
  }
}
#endif

#if defined(WOLFSSL_CMSIS_RTOS) || defined(WOLFSSL_CMSIS_RTOSv2)

#if defined(WOLFSSL_CMSIS_RTOS)
extern uint32_t os_time;
#endif

uint32_t HAL_GetTick(void)
{
    #if defined(WOLFSSL_CMSIS_RTOS)
        return os_time;
    #elif defined(WOLFSSL_CMSIS_RTOSv2)
        return osKernelGetTickCount();
    #endif
}

double current_time(int reset)
{
    #if defined(WOLFSSL_CMSIS_RTOS)
        return (double)os_time / 1000.0;
    #elif defined(WOLFSSL_CMSIS_RTOSv2)
        return (double)osKernelGetTickCount() / 1000.0;
    #endif
}

#else

#include <stdint.h>
#define DWT                 ((DWT_Type       *)     (0xE0001000UL)     )
typedef struct
{
  uint32_t CTRL;       /*!< Offset: 0x000 (R/W)  Control Register           */
  uint32_t CYCCNT;     /*!< Offset: 0x004 (R/W)  Cycle Count Register       */
} DWT_Type;

extern uint32_t SystemCoreClock;

double current_time(int reset)
{
    if (reset)
        DWT->CYCCNT = 0;
    return ((double)DWT->CYCCNT / SystemCoreClock);
}
#endif

static time_t epochTime;
time_t time(time_t *t)
{
    return epochTime;
}

void setTime(time_t t)
{
    epochTime = t;
}


extern void server_test(void const *arg);

#if defined(WOLFSSL_CMSIS_RTOSv2)
void app_main(void *arg)
#else
void app_main(void const*arg)
#endif
{
    if (netInitialize() == netOK)
        server_test(arg);
    else
        printf("ERROR: netInitialize\n");
}

#if defined(WOLFSSL_CMSIS_RTOS)
osThreadDef(app_main, osPriorityLow, 1, 32 * 1024);
#endif

/*----------------------------------------------------------------------------
  Main Thread 'main': Run Network
 *---------------------------------------------------------------------------*/
#include <stdio.h>
typedef struct func_args {
    int    argc;
    char** argv;
} func_args;

int myoptind = 0;
char *myoptarg = NULL;

int main(void)
{
    static char *argv[] =
        {   "server",  "-p", SERVER_PORT,
                       "-v",  " ",  OTHER_OPTIONS } ;
    static   func_args args  =
        {  sizeof(argv)/sizeof(*argv[0]), argv } ;

    char *verStr[] = { "SSL3", "TLS1.0", "TLS1.1", "TLS1.2", "TLS1.3"};
    #define VERSIZE 2
    char ver[VERSIZE];
                    
    MPU_Config();                             /* Configure the MPU              */
    CPU_CACHE_Enable();                       /* Enable the CPU Cache           */
    HAL_Init();                               /* Initialize the HAL Library     */
    SystemClock_Config();                     /* Configure the System Clock     */

    #if !defined(NO_FILESYSTEM)
    init_filesystem ();
    #endif

#if defined(WOLFSSL_CMSIS_RTOSv2)
    osKernelInitialize();
#endif

#if defined(DEBUG_WOLFSSL)
    printf("Turning ON Debug message\n");
    wolfSSL_Debugging_ON();
#endif

    snprintf(ver, VERSIZE, "%d", TLS_VER);
    argv[4] = ver;

    printf("SSL/TLS Server\n ") ;
    printf("    Server Port: %s\n    Version: %s\n", argv[2], verStr[TLS_VER]) ;
    printf("    Other options: %s\n", OTHER_OPTIONS);   
    setTime((RTC_YEAR-1970)*365*24*60*60 + RTC_MONTH*30*24*60*60 + RTC_DAY*24*60*60);

#if defined(WOLFSSL_CMSIS_RTOS)
    osThreadCreate(osThread(app_main), (void *)&args);
#elif defined(WOLFSSL_CMSIS_RTOSv2)
    osThreadNew(app_main, (void *)&args, NULL);
#endif
    osKernelStart();
}