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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
|
/*
ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
/*
* **** This file incorporates work covered by the following copyright and ****
* **** permission notice: ****
*
* Copyright (C) 2006-2017 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
*
*/
#include "ch.h"
#include <time.h>
#include "wolfssl_chibios.h"
#include "lwip/opt.h"
#include "lwip/arch.h"
#include "lwip/api.h"
#include "lwip/mem.h"
#include "lwip/sockets.h"
#include "lwip/tcp.h"
#include <string.h>
#if defined WOLFSSL_USE_NETCONN
static int wolfssl_is_initialized = 0;
static int ssl_rb_len = 0;
static int ssl_rb_off = 0;
#define MAX_SSL_BUF 1460
static uint8_t ssl_recv_buffer[MAX_SSL_BUF];
sslconn *sslconn_accept(sslconn *sk) {
sslconn *new;
struct netconn *newconn = NULL;
err_t err;
err = netconn_accept(sk->conn, &newconn);
if (err != ERR_OK) {
return NULL;
}
new = chHeapAlloc(NULL, sizeof(sslconn));
if (!new)
return NULL;
new->conn = newconn;
new->ctx = sk->ctx;
new->ssl = wolfSSL_new(new->ctx);
wolfSSL_SetIOReadCtx(new->ssl, new);
wolfSSL_SetIOWriteCtx(new->ssl, new);
if (wolfSSL_accept(new->ssl) == SSL_SUCCESS) {
wolfSSL_set_using_nonblock(new->ssl, 1);
newconn->pcb.tcp->mss = 1480;
return new;
} else {
wolfSSL_free(new->ssl);
chHeapFree(new);
return NULL;
}
}
sslconn *sslconn_new(enum netconn_type t, WOLFSSL_METHOD* method) {
sslconn *sk;
if (!wolfssl_is_initialized) {
wolfSSL_Init();
wolfssl_is_initialized++;
}
sk = chHeapAlloc(NULL, sizeof(sslconn));
if (!sk)
return NULL;
memset(sk, 0, sizeof(sslconn));
sk->ctx = wolfSSL_CTX_new(method);
if (!sk->ctx)
goto error;
sk->conn = netconn_new(t);
if (!sk->conn)
goto error;
wolfSSL_SetIORecv(sk->ctx, wolfssl_recv_cb);
wolfSSL_SetIOSend(sk->ctx, wolfssl_send_cb);
return sk;
error:
if (sk->ctx)
wolfSSL_CTX_free(sk->ctx);
chHeapFree(sk);
return NULL;
}
void sslconn_close(sslconn *sk) {
netconn_delete(sk->conn);
wolfSSL_free(sk->ssl);
chHeapFree(sk);
}
#endif /* WOLFSSL_USE_NETCONN */
/* IO Callbacks */
int wolfssl_send_cb(WOLFSSL* ssl, char *buf, int sz, void *ctx) {
#if defined WOLFSSL_USE_NETCONN
sslconn *sk = (sslconn *)ctx;
int err;
(void)ssl;
err = netconn_write(sk->conn, buf, sz, NETCONN_COPY);
if (err == ERR_OK)
return sz;
else
return -2;
#elif defined WOLFSSL_USE_SOCKET
(void)ssl;
/* By default, ctx will be a pointer to the file descriptor to write to.
* This can be changed by calling wolfSSL_SetIOWriteCtx(). */
int sockfd = *(int*)ctx;
return write(sockfd, buf, sz);
#endif
}
int wolfssl_recv_cb(WOLFSSL *ssl, char *buf, int sz, void *ctx) {
#if defined WOLFSSL_USE_NETCONN
sslconn *sk = (sslconn *)ctx;
struct netbuf *inbuf = NULL;
uint8_t *net_buf;
uint16_t buflen;
(void)ssl;
err_t err;
if (ssl_rb_len > 0) {
if (sz > ssl_rb_len - ssl_rb_off)
sz = ssl_rb_len - ssl_rb_off;
memcpy(buf, ssl_recv_buffer + ssl_rb_off, sz);
ssl_rb_off += sz;
if (ssl_rb_off >= ssl_rb_len) {
ssl_rb_len = 0;
ssl_rb_off = 0;
}
return sz;
}
err = netconn_recv(sk->conn, &inbuf);
if (err == ERR_OK) {
netbuf_data(inbuf, (void **)&net_buf, &buflen);
ssl_rb_len = buflen;
if (ssl_rb_len > MAX_SSL_BUF)
ssl_rb_len = MAX_SSL_BUF;
memcpy(ssl_recv_buffer, net_buf, ssl_rb_len);
ssl_rb_off = 0;
if (sz > ssl_rb_len)
sz = ssl_rb_len;
memcpy(buf, ssl_recv_buffer, sz);
ssl_rb_off += sz;
if (ssl_rb_off >= ssl_rb_len) {
ssl_rb_len = 0;
ssl_rb_off = 0;
}
netbuf_delete(inbuf);
return sz;
}
else
return 0;
//return WOLFSSL_CBIO_ERR_WANT_READ;
#elif defined WOLFSSL_USE_SOCKET
(void)ssl;
int sockfd = *(int*)ctx;
return read(sockfd, buf, sz);
#endif
}
#ifndef ST2S
#define ST2S(n) (((n) + CH_CFG_ST_FREQUENCY - 1UL) / CH_CFG_ST_FREQUENCY)
#endif
#ifndef ST2MS
#define ST2MS(n) (((n) * 1000UL + CH_CFG_ST_FREQUENCY - 1UL) / CH_CFG_ST_FREQUENCY)
#endif
word32 LowResTimer(void) {
systime_t t = chVTGetSystemTimeX();
return ST2S(t);
}
word32 epochTime(void) {
RTCDateTime date;
struct tm now_tm;
rtcGetTime(&RTCD0, &date);
rtcConvertDateTimeToStructTm(&date, &now_tm, NULL);
return (word32) mktime(&now_tm);
}
uint32_t TimeNowInMilliseconds(void) {
systime_t t = chVTGetSystemTimeX();
return ST2MS(t);
}
void *chHeapRealloc (void *addr, uint32_t size) {
union heap_header *hp;
uint32_t prev_size, new_size;
void *ptr;
if(addr == NULL) {
return chHeapAlloc(NULL, size);
}
/* previous allocated segment is preceded by an heap_header */
hp = addr - sizeof(union heap_header);
prev_size = hp->used.size; /* size is always multiple of 8 */
/* check new size memory alignment */
if(size % 8 == 0) {
new_size = size;
}
else {
new_size = ((int) (size / 8)) * 8 + 8;
}
if(prev_size >= new_size) {
return addr;
}
ptr = chHeapAlloc(NULL, size);
if(ptr == NULL) {
return NULL;
}
memcpy(ptr, addr, prev_size);
chHeapFree(addr);
return ptr;
}
void *chibios_alloc(void *heap, int size) {
return chHeapAlloc(heap, size);
}
void chibios_free(void *ptr) {
if (ptr)
chHeapFree(ptr);
}
|