diff options
Diffstat (limited to 'lib/chibios/os/various/wolfssl_bindings/wolfssl_chibios.c')
-rw-r--r-- | lib/chibios/os/various/wolfssl_bindings/wolfssl_chibios.c | 252 |
1 files changed, 252 insertions, 0 deletions
diff --git a/lib/chibios/os/various/wolfssl_bindings/wolfssl_chibios.c b/lib/chibios/os/various/wolfssl_bindings/wolfssl_chibios.c new file mode 100644 index 000000000..947f0200b --- /dev/null +++ b/lib/chibios/os/various/wolfssl_bindings/wolfssl_chibios.c | |||
@@ -0,0 +1,252 @@ | |||
1 | /* | ||
2 | ChibiOS - Copyright (C) 2006..2018 Giovanni Di Sirio | ||
3 | |||
4 | Licensed under the Apache License, Version 2.0 (the "License"); | ||
5 | you may not use this file except in compliance with the License. | ||
6 | You may obtain a copy of the License at | ||
7 | |||
8 | http://www.apache.org/licenses/LICENSE-2.0 | ||
9 | |||
10 | Unless required by applicable law or agreed to in writing, software | ||
11 | distributed under the License is distributed on an "AS IS" BASIS, | ||
12 | WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
13 | See the License for the specific language governing permissions and | ||
14 | limitations under the License. | ||
15 | */ | ||
16 | /* | ||
17 | * **** This file incorporates work covered by the following copyright and **** | ||
18 | * **** permission notice: **** | ||
19 | * | ||
20 | * Copyright (C) 2006-2017 wolfSSL Inc. | ||
21 | * | ||
22 | * This file is part of wolfSSL. | ||
23 | * | ||
24 | * wolfSSL is free software; you can redistribute it and/or modify | ||
25 | * it under the terms of the GNU General Public License as published by | ||
26 | * the Free Software Foundation; either version 2 of the License, or | ||
27 | * (at your option) any later version. | ||
28 | * | ||
29 | * wolfSSL is distributed in the hope that it will be useful, | ||
30 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
31 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
32 | * GNU General Public License for more details. | ||
33 | * | ||
34 | * You should have received a copy of the GNU General Public License | ||
35 | * along with this program; if not, write to the Free Software | ||
36 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA | ||
37 | * | ||
38 | */ | ||
39 | |||
40 | #include "ch.h" | ||
41 | #include "wolfssl_chibios.h" | ||
42 | #include "lwip/opt.h" | ||
43 | #include "lwip/arch.h" | ||
44 | #include "lwip/api.h" | ||
45 | #include "lwip/mem.h" | ||
46 | #include "lwip/sockets.h" | ||
47 | #include "lwip/tcp.h" | ||
48 | #include <string.h> | ||
49 | static int wolfssl_is_initialized = 0; | ||
50 | |||
51 | sslconn *sslconn_accept(sslconn *sk) | ||
52 | { | ||
53 | sslconn *new; | ||
54 | struct netconn *newconn = NULL; | ||
55 | err_t err; | ||
56 | err = netconn_accept(sk->conn, &newconn); | ||
57 | if (err != ERR_OK) { | ||
58 | return NULL; | ||
59 | } | ||
60 | new = chHeapAlloc(NULL, sizeof(sslconn)); | ||
61 | if (!new) | ||
62 | return NULL; | ||
63 | new->conn = newconn; | ||
64 | new->ctx = sk->ctx; | ||
65 | new->ssl = wolfSSL_new(new->ctx); | ||
66 | wolfSSL_SetIOReadCtx(new->ssl, new); | ||
67 | wolfSSL_SetIOWriteCtx(new->ssl, new); | ||
68 | |||
69 | if (wolfSSL_accept(new->ssl) == SSL_SUCCESS) { | ||
70 | wolfSSL_set_using_nonblock(new->ssl, 1); | ||
71 | newconn->pcb.tcp->mss = 1480; | ||
72 | return new; | ||
73 | } else { | ||
74 | wolfSSL_free(new->ssl); | ||
75 | chHeapFree(new); | ||
76 | return NULL; | ||
77 | } | ||
78 | } | ||
79 | |||
80 | sslconn *sslconn_new(enum netconn_type t, WOLFSSL_METHOD* method) | ||
81 | { | ||
82 | sslconn *sk; | ||
83 | if (!wolfssl_is_initialized) { | ||
84 | wolfSSL_Init(); | ||
85 | wolfssl_is_initialized++; | ||
86 | } | ||
87 | |||
88 | sk = chHeapAlloc(NULL, sizeof(sslconn)); | ||
89 | if (!sk) | ||
90 | return NULL; | ||
91 | memset(sk, 0, sizeof(sslconn)); | ||
92 | sk->ctx = wolfSSL_CTX_new(method); | ||
93 | if (!sk->ctx) | ||
94 | goto error; | ||
95 | sk->conn = netconn_new(t); | ||
96 | if (!sk->conn) | ||
97 | goto error; | ||
98 | wolfSSL_SetIORecv(sk->ctx, wolfssl_recv_cb); | ||
99 | wolfSSL_SetIOSend(sk->ctx, wolfssl_send_cb); | ||
100 | return sk; | ||
101 | |||
102 | error: | ||
103 | if (sk->ctx) | ||
104 | wolfSSL_CTX_free(sk->ctx); | ||
105 | chHeapFree(sk); | ||
106 | return NULL; | ||
107 | } | ||
108 | |||
109 | void sslconn_close(sslconn *sk) | ||
110 | { | ||
111 | netconn_delete(sk->conn); | ||
112 | wolfSSL_free(sk->ssl); | ||
113 | chHeapFree(sk); | ||
114 | } | ||
115 | |||
116 | |||
117 | /* IO Callbacks */ | ||
118 | int wolfssl_send_cb(WOLFSSL* ssl, char *buf, int sz, void *ctx) | ||
119 | { | ||
120 | sslconn *sk = (sslconn *)ctx; | ||
121 | int err; | ||
122 | (void)ssl; | ||
123 | err = netconn_write(sk->conn, buf, sz, NETCONN_COPY); | ||
124 | if (err == ERR_OK) | ||
125 | return sz; | ||
126 | else | ||
127 | return -2; | ||
128 | } | ||
129 | |||
130 | |||
131 | #define MAX_SSL_BUF 1460 | ||
132 | static uint8_t ssl_recv_buffer[MAX_SSL_BUF]; | ||
133 | static int ssl_rb_len = 0; | ||
134 | static int ssl_rb_off = 0; | ||
135 | |||
136 | int wolfssl_recv_cb(WOLFSSL *ssl, char *buf, int sz, void *ctx) | ||
137 | { | ||
138 | sslconn *sk = (sslconn *)ctx; | ||
139 | struct netbuf *inbuf = NULL; | ||
140 | uint8_t *net_buf; | ||
141 | uint16_t buflen; | ||
142 | (void)ssl; | ||
143 | err_t err; | ||
144 | |||
145 | if (ssl_rb_len > 0) { | ||
146 | if (sz > ssl_rb_len - ssl_rb_off) | ||
147 | sz = ssl_rb_len - ssl_rb_off; | ||
148 | memcpy(buf, ssl_recv_buffer + ssl_rb_off, sz); | ||
149 | ssl_rb_off += sz; | ||
150 | if (ssl_rb_off >= ssl_rb_len) { | ||
151 | ssl_rb_len = 0; | ||
152 | ssl_rb_off = 0; | ||
153 | } | ||
154 | return sz; | ||
155 | } | ||
156 | |||
157 | |||
158 | err = netconn_recv(sk->conn, &inbuf); | ||
159 | if (err == ERR_OK) { | ||
160 | netbuf_data(inbuf, (void **)&net_buf, &buflen); | ||
161 | ssl_rb_len = buflen; | ||
162 | if (ssl_rb_len > MAX_SSL_BUF) | ||
163 | ssl_rb_len = MAX_SSL_BUF; | ||
164 | memcpy(ssl_recv_buffer, net_buf, ssl_rb_len); | ||
165 | ssl_rb_off = 0; | ||
166 | if (sz > ssl_rb_len) | ||
167 | sz = ssl_rb_len; | ||
168 | memcpy(buf, ssl_recv_buffer, sz); | ||
169 | ssl_rb_off += sz; | ||
170 | if (ssl_rb_off >= ssl_rb_len) { | ||
171 | ssl_rb_len = 0; | ||
172 | ssl_rb_off = 0; | ||
173 | } | ||
174 | netbuf_delete(inbuf); | ||
175 | return sz; | ||
176 | } | ||
177 | else | ||
178 | return 0; | ||
179 | //return WOLFSSL_CBIO_ERR_WANT_READ; | ||
180 | } | ||
181 | |||
182 | #ifndef ST2S | ||
183 | # define ST2S(n) (((n) + CH_CFG_ST_FREQUENCY - 1UL) / CH_CFG_ST_FREQUENCY) | ||
184 | #endif | ||
185 | |||
186 | #ifndef ST2MS | ||
187 | #define ST2MS(n) (((n) * 1000UL + CH_CFG_ST_FREQUENCY - 1UL) / CH_CFG_ST_FREQUENCY) | ||
188 | #endif | ||
189 | |||
190 | |||
191 | word32 LowResTimer(void) | ||
192 | { | ||
193 | systime_t t = chVTGetSystemTimeX(); | ||
194 | return ST2S(t); | ||
195 | } | ||
196 | |||
197 | uint32_t TimeNowInMilliseconds(void) | ||
198 | { | ||
199 | systime_t t = chVTGetSystemTimeX(); | ||
200 | return ST2MS(t); | ||
201 | } | ||
202 | |||
203 | void *chHeapRealloc (void *addr, uint32_t size) | ||
204 | { | ||
205 | union heap_header *hp; | ||
206 | uint32_t prev_size, new_size; | ||
207 | |||
208 | void *ptr; | ||
209 | |||
210 | if(addr == NULL) { | ||
211 | return chHeapAlloc(NULL, size); | ||
212 | } | ||
213 | |||
214 | /* previous allocated segment is preceded by an heap_header */ | ||
215 | hp = addr - sizeof(union heap_header); | ||
216 | prev_size = hp->used.size; /* size is always multiple of 8 */ | ||
217 | |||
218 | /* check new size memory alignment */ | ||
219 | if(size % 8 == 0) { | ||
220 | new_size = size; | ||
221 | } | ||
222 | else { | ||
223 | new_size = ((int) (size / 8)) * 8 + 8; | ||
224 | } | ||
225 | |||
226 | if(prev_size >= new_size) { | ||
227 | return addr; | ||
228 | } | ||
229 | |||
230 | ptr = chHeapAlloc(NULL, size); | ||
231 | if(ptr == NULL) { | ||
232 | return NULL; | ||
233 | } | ||
234 | |||
235 | memcpy(ptr, addr, prev_size); | ||
236 | |||
237 | chHeapFree(addr); | ||
238 | |||
239 | return ptr; | ||
240 | } | ||
241 | |||
242 | void *chibios_alloc(void *heap, int size) | ||
243 | { | ||
244 | return chHeapAlloc(heap, size); | ||
245 | } | ||
246 | |||
247 | void chibios_free(void *ptr) | ||
248 | { | ||
249 | if (ptr) | ||
250 | chHeapFree(ptr); | ||
251 | } | ||
252 | |||