1 | /**
|
---|
2 | * @file: An implementation of TCP MD5 signatures by using various hooks in
|
---|
3 | * lwIP to implement custom tcp options and custom socket options.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (c) 2018 Simon Goldschmidt
|
---|
8 | * All rights reserved.
|
---|
9 | *
|
---|
10 | * Redistribution and use in source and binary forms, with or without modification,
|
---|
11 | * are permitted provided that the following conditions are met:
|
---|
12 | *
|
---|
13 | * 1. Redistributions of source code must retain the above copyright notice,
|
---|
14 | * this list of conditions and the following disclaimer.
|
---|
15 | * 2. Redistributions in binary form must reproduce the above copyright notice,
|
---|
16 | * this list of conditions and the following disclaimer in the documentation
|
---|
17 | * and/or other materials provided with the distribution.
|
---|
18 | * 3. The name of the author may not be used to endorse or promote products
|
---|
19 | * derived from this software without specific prior written permission.
|
---|
20 | *
|
---|
21 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
|
---|
22 | * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
|
---|
23 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
|
---|
24 | * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
|
---|
25 | * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
|
---|
26 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
---|
27 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
---|
28 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
|
---|
29 | * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
|
---|
30 | * OF SUCH DAMAGE.
|
---|
31 | *
|
---|
32 | * Author: Simon Goldschmidt <goldsimon@gmx.de>
|
---|
33 | */
|
---|
34 |
|
---|
35 | #include "tcp_md5.h"
|
---|
36 | #include "lwip/ip_addr.h"
|
---|
37 | #include "lwip/sys.h"
|
---|
38 | #include "lwip/prot/tcp.h"
|
---|
39 | #include "lwip/priv/tcp_priv.h"
|
---|
40 | #include "lwip/sockets.h"
|
---|
41 | #include "lwip/priv/sockets_priv.h"
|
---|
42 | #include "lwip/api.h"
|
---|
43 | #include <string.h>
|
---|
44 |
|
---|
45 | /* pull in md5 of ppp? */
|
---|
46 | #include "netif/ppp/ppp_opts.h"
|
---|
47 | #if !PPP_SUPPORT || (!LWIP_USE_EXTERNAL_POLARSSL && !LWIP_USE_EXTERNAL_MBEDTLS)
|
---|
48 | #undef LWIP_INCLUDED_POLARSSL_MD5
|
---|
49 | #define LWIP_INCLUDED_POLARSSL_MD5 1
|
---|
50 | #include "netif/ppp/polarssl/md5.h"
|
---|
51 | #endif
|
---|
52 |
|
---|
53 | #if !LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
54 | #error tcp_md5 needs LWIP_TCP_PCB_NUM_EXT_ARGS
|
---|
55 | #endif
|
---|
56 |
|
---|
57 | #define LWIP_TCP_OPT_MD5 19 /* number of the md5 option */
|
---|
58 | #define LWIP_TCP_OPT_LEN_MD5 18 /* length of the md5 option */
|
---|
59 | #define LWIP_TCP_OPT_LEN_MD5_OUT 20 /* 18 + alignment */
|
---|
60 |
|
---|
61 | #define LWIP_TCP_MD5_DIGEST_LEN 16
|
---|
62 |
|
---|
63 | /* This keeps the md5 state internally */
|
---|
64 | struct tcp_md5_conn_info {
|
---|
65 | struct tcp_md5_conn_info *next;
|
---|
66 | ip_addr_t remote_addr;
|
---|
67 | u16_t remote_port;
|
---|
68 | u8_t key[TCP_MD5SIG_MAXKEYLEN];
|
---|
69 | u16_t key_len;
|
---|
70 | };
|
---|
71 |
|
---|
72 | /* Callback function prototypes: */
|
---|
73 | static void tcp_md5_extarg_destroy(u8_t id, void *data);
|
---|
74 | static err_t tcp_md5_extarg_passive_open(u8_t id, struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb);
|
---|
75 | /* Define our tcp ext arg callback structure: */
|
---|
76 | const struct tcp_ext_arg_callbacks tcp_md5_ext_arg_callbacks = {
|
---|
77 | tcp_md5_extarg_destroy,
|
---|
78 | tcp_md5_extarg_passive_open
|
---|
79 | };
|
---|
80 |
|
---|
81 | static u8_t tcp_md5_extarg_id = LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID;
|
---|
82 | static u8_t tcp_md5_opts_buf[40];
|
---|
83 |
|
---|
84 | /** Initialize this module (allocates a tcp ext arg id) */
|
---|
85 | void
|
---|
86 | tcp_md5_init(void)
|
---|
87 | {
|
---|
88 | tcp_md5_extarg_id = tcp_ext_arg_alloc_id();
|
---|
89 | }
|
---|
90 |
|
---|
91 | /* Create a conn-info structure that holds the md5 state per connection */
|
---|
92 | static struct tcp_md5_conn_info *
|
---|
93 | tcp_md5_conn_info_alloc(void)
|
---|
94 | {
|
---|
95 | return (struct tcp_md5_conn_info *)mem_malloc(sizeof(struct tcp_md5_conn_info));
|
---|
96 | }
|
---|
97 |
|
---|
98 | /* Frees a conn-info structure that holds the md5 state per connection */
|
---|
99 | static void
|
---|
100 | tcp_md5_conn_info_free(struct tcp_md5_conn_info *info)
|
---|
101 | {
|
---|
102 | mem_free(info);
|
---|
103 | }
|
---|
104 |
|
---|
105 | /* A pcb is about to be destroyed. Free its extdata */
|
---|
106 | static void
|
---|
107 | tcp_md5_extarg_destroy(u8_t id, void *data)
|
---|
108 | {
|
---|
109 | struct tcp_md5_conn_info *iter;
|
---|
110 |
|
---|
111 | LWIP_ASSERT("tcp_md5_extarg_id != LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID",
|
---|
112 | tcp_md5_extarg_id != LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID);
|
---|
113 | LWIP_ASSERT("id == tcp_md5_extarg_id", id == tcp_md5_extarg_id);
|
---|
114 | LWIP_UNUSED_ARG(id);
|
---|
115 |
|
---|
116 | iter = (struct tcp_md5_conn_info *)data;
|
---|
117 | while (iter != NULL) {
|
---|
118 | struct tcp_md5_conn_info *info = iter;
|
---|
119 | iter = iter->next;
|
---|
120 | tcp_md5_conn_info_free(info);
|
---|
121 | }
|
---|
122 | }
|
---|
123 |
|
---|
124 | /* Try to find an md5 connection info for the specified remote connection */
|
---|
125 | static struct tcp_md5_conn_info *
|
---|
126 | tcp_md5_get_info(const struct tcp_pcb *pcb, const ip_addr_t *remote_ip, u16_t remote_port)
|
---|
127 | {
|
---|
128 | if (pcb != NULL) {
|
---|
129 | struct tcp_md5_conn_info *info = (struct tcp_md5_conn_info *)tcp_ext_arg_get(pcb, tcp_md5_extarg_id);
|
---|
130 | while (info != NULL) {
|
---|
131 | if (ip_addr_cmp(&info->remote_addr, remote_ip)) {
|
---|
132 | if (info->remote_port == remote_port) {
|
---|
133 | return info;
|
---|
134 | }
|
---|
135 | }
|
---|
136 | info = info->next;
|
---|
137 | }
|
---|
138 | }
|
---|
139 | return NULL;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /* Passive open: copy md5 connection info from listen pcb to connection pcb
|
---|
143 | * or return error (connection will be closed)
|
---|
144 | */
|
---|
145 | static err_t
|
---|
146 | tcp_md5_extarg_passive_open(u8_t id, struct tcp_pcb_listen *lpcb, struct tcp_pcb *cpcb)
|
---|
147 | {
|
---|
148 | struct tcp_md5_conn_info *iter;
|
---|
149 |
|
---|
150 | LWIP_ASSERT("lpcb != NULL", lpcb != NULL);
|
---|
151 | LWIP_ASSERT("cpcb != NULL", cpcb != NULL);
|
---|
152 | LWIP_ASSERT("tcp_md5_extarg_id != LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID",
|
---|
153 | tcp_md5_extarg_id != LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID);
|
---|
154 | LWIP_ASSERT("id == tcp_md5_extarg_id", id == tcp_md5_extarg_id);
|
---|
155 | LWIP_UNUSED_ARG(id);
|
---|
156 |
|
---|
157 | iter = (struct tcp_md5_conn_info *)tcp_ext_arg_get((struct tcp_pcb *)lpcb, id);
|
---|
158 | while (iter != NULL) {
|
---|
159 | if (iter->remote_port == cpcb->remote_port) {
|
---|
160 | if (ip_addr_cmp(&iter->remote_addr, &cpcb->remote_ip)) {
|
---|
161 | struct tcp_md5_conn_info *info = tcp_md5_conn_info_alloc();
|
---|
162 | if (info != NULL) {
|
---|
163 | memcpy(info, iter, sizeof(struct tcp_md5_conn_info));
|
---|
164 | tcp_ext_arg_set(cpcb, id, info);
|
---|
165 | tcp_ext_arg_set_callbacks(cpcb, id, &tcp_md5_ext_arg_callbacks);
|
---|
166 | return ERR_OK;
|
---|
167 | } else {
|
---|
168 | return ERR_MEM;
|
---|
169 | }
|
---|
170 | }
|
---|
171 | }
|
---|
172 | iter = iter->next;
|
---|
173 | }
|
---|
174 | /* remote connection not found */
|
---|
175 | return ERR_VAL;
|
---|
176 | }
|
---|
177 |
|
---|
178 | /* Parse tcp header options and return 1 if an md5 signature option was found */
|
---|
179 | static int
|
---|
180 | tcp_md5_parseopt(const u8_t *opts, u16_t optlen, u8_t *md5_digest_out)
|
---|
181 | {
|
---|
182 | u8_t data;
|
---|
183 | u16_t optidx;
|
---|
184 |
|
---|
185 | /* Parse the TCP MSS option, if present. */
|
---|
186 | if (optlen != 0) {
|
---|
187 | for (optidx = 0; optidx < optlen; ) {
|
---|
188 | u8_t opt = opts[optidx++];
|
---|
189 | switch (opt) {
|
---|
190 | case LWIP_TCP_OPT_EOL:
|
---|
191 | /* End of options. */
|
---|
192 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: EOL\n"));
|
---|
193 | return 0;
|
---|
194 | case LWIP_TCP_OPT_NOP:
|
---|
195 | /* NOP option. */
|
---|
196 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: NOP\n"));
|
---|
197 | break;
|
---|
198 | case LWIP_TCP_OPT_MD5:
|
---|
199 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: MD5\n"));
|
---|
200 | if (opts[optidx++] != LWIP_TCP_OPT_LEN_MD5 || (optidx - 2 + LWIP_TCP_OPT_LEN_MD5) > optlen) {
|
---|
201 | /* Bad length */
|
---|
202 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
|
---|
203 | return 0;
|
---|
204 | }
|
---|
205 | /* An MD5 option with the right option length. */
|
---|
206 | memcpy(md5_digest_out, &opts[optidx], LWIP_TCP_MD5_DIGEST_LEN);
|
---|
207 | /* no need to process the options further */
|
---|
208 | return 1;
|
---|
209 | break;
|
---|
210 | default:
|
---|
211 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: other\n"));
|
---|
212 | data = opts[optidx++];
|
---|
213 | if (data < 2) {
|
---|
214 | LWIP_DEBUGF(TCP_INPUT_DEBUG, ("tcp_parseopt: bad length\n"));
|
---|
215 | /* If the length field is zero, the options are malformed
|
---|
216 | and we don't process them further. */
|
---|
217 | return 0;
|
---|
218 | }
|
---|
219 | /* All other options have a length field, so that we easily
|
---|
220 | can skip past them. */
|
---|
221 | optidx += data - 2;
|
---|
222 | }
|
---|
223 | }
|
---|
224 | }
|
---|
225 | return 0;
|
---|
226 | }
|
---|
227 |
|
---|
228 | /* Get tcp options into contiguous memory. May be required if input pbufs
|
---|
229 | * are chained.
|
---|
230 | */
|
---|
231 | static const u8_t*
|
---|
232 | tcp_md5_options_singlebuf(struct tcp_hdr *hdr, u16_t optlen, u16_t opt1len, u8_t *opt2)
|
---|
233 | {
|
---|
234 | const u8_t *opts;
|
---|
235 | LWIP_ASSERT("hdr != NULL", hdr != NULL);
|
---|
236 | LWIP_ASSERT("optlen >= opt1len", optlen >= opt1len);
|
---|
237 | opts = (const u8_t *)hdr + TCP_HLEN;
|
---|
238 | if (optlen == opt1len) {
|
---|
239 | /* arleady in one piece */
|
---|
240 | return opts;
|
---|
241 | }
|
---|
242 | if (optlen > sizeof(tcp_md5_opts_buf)) {
|
---|
243 | /* options too long */
|
---|
244 | return NULL;
|
---|
245 | }
|
---|
246 | LWIP_ASSERT("opt2 != NULL", opt2 != NULL);
|
---|
247 | /* copy first part */
|
---|
248 | memcpy(tcp_md5_opts_buf, opts, opt1len);
|
---|
249 | /* copy second part */
|
---|
250 | memcpy(&tcp_md5_opts_buf[opt1len], opt2, optlen - opt1len);
|
---|
251 | return tcp_md5_opts_buf;
|
---|
252 | }
|
---|
253 |
|
---|
254 | /* Create the md5 digest for a given segment */
|
---|
255 | static int
|
---|
256 | tcp_md5_create_digest(const ip_addr_t *ip_src, const ip_addr_t *ip_dst, const struct tcp_hdr *hdr,
|
---|
257 | const u8_t *key, size_t key_len, u8_t *digest_out, struct pbuf *p)
|
---|
258 | {
|
---|
259 | md5_context ctx;
|
---|
260 | u8_t tmp8;
|
---|
261 | u16_t tmp16;
|
---|
262 | const size_t addr_len = IP_ADDR_RAW_SIZE(*ip_src);
|
---|
263 |
|
---|
264 | if (p != NULL) {
|
---|
265 | LWIP_ASSERT("pbuf must not point to tcp header here!", (const void *)hdr != p->payload);
|
---|
266 | }
|
---|
267 |
|
---|
268 | /* Generate the hash, using MD5. */
|
---|
269 | md5_starts(&ctx);
|
---|
270 | /* 1. the TCP pseudo-header (in the order: source IP address,
|
---|
271 | destination IP address, zero-padded protocol number, and
|
---|
272 | segment length) */
|
---|
273 | md5_update(&ctx, (const unsigned char*)ip_src, addr_len);
|
---|
274 | md5_update(&ctx, (const unsigned char*)ip_dst, addr_len);
|
---|
275 | tmp8 = 0; /* zero-padded */
|
---|
276 | md5_update(&ctx, &tmp8, 1);
|
---|
277 | tmp8 = IP_PROTO_TCP;
|
---|
278 | md5_update(&ctx, &tmp8, 1);
|
---|
279 | tmp16 = lwip_htons(TCPH_HDRLEN_BYTES(hdr) + (p ? p->tot_len : 0));
|
---|
280 | md5_update(&ctx, (const unsigned char*)&tmp16, 2);
|
---|
281 | /* 2. the TCP header, excluding options, and assuming a checksum of
|
---|
282 | zero */
|
---|
283 | md5_update(&ctx, (const unsigned char*)hdr, sizeof(struct tcp_hdr));
|
---|
284 | /* 3. the TCP segment data (if any) */
|
---|
285 | if ((p != NULL) && (p->tot_len != 0)) {
|
---|
286 | struct pbuf *q;
|
---|
287 | for (q = p; q != NULL; q = q->next) {
|
---|
288 | md5_update(&ctx, (const unsigned char*)q->payload, q->len);
|
---|
289 | }
|
---|
290 | }
|
---|
291 | /* 4. an independently-specified key or password, known to both TCPs
|
---|
292 | and presumably connection-specific */
|
---|
293 | md5_update(&ctx, key, key_len);
|
---|
294 |
|
---|
295 | md5_finish(&ctx, digest_out);
|
---|
296 | return 1;
|
---|
297 | }
|
---|
298 |
|
---|
299 | /* Duplicate a tcp header and make sure the fields are in network byte order */
|
---|
300 | static void
|
---|
301 | tcp_md5_dup_tcphdr(struct tcp_hdr *tcphdr_copy, const struct tcp_hdr *tcphdr_in, int tcphdr_in_is_host_order)
|
---|
302 | {
|
---|
303 | memcpy(tcphdr_copy, tcphdr_in, sizeof(struct tcp_hdr));
|
---|
304 | tcphdr_copy->chksum = 0; /* checksum is zero for the pseudo header */
|
---|
305 | if (tcphdr_in_is_host_order) {
|
---|
306 | /* lwIP writes the TCP header values back to the buffer, we need to invert that here: */
|
---|
307 | tcphdr_copy->src = lwip_htons(tcphdr_copy->src);
|
---|
308 | tcphdr_copy->dest = lwip_htons(tcphdr_copy->dest);
|
---|
309 | tcphdr_copy->seqno = lwip_htonl(tcphdr_copy->seqno);
|
---|
310 | tcphdr_copy->ackno = lwip_htonl(tcphdr_copy->ackno);
|
---|
311 | tcphdr_copy->wnd = lwip_htons(tcphdr_copy->wnd);
|
---|
312 | tcphdr_copy->urgp = lwip_htons(tcphdr_copy->urgp);
|
---|
313 | }
|
---|
314 | }
|
---|
315 |
|
---|
316 | /* Check if md5 is enabled on a given pcb */
|
---|
317 | static int
|
---|
318 | tcp_md5_is_enabled_on_pcb(const struct tcp_pcb *pcb)
|
---|
319 | {
|
---|
320 | if (tcp_md5_extarg_id != LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID) {
|
---|
321 | struct tcp_md5_conn_info *info = (struct tcp_md5_conn_info *)tcp_ext_arg_get(pcb, tcp_md5_extarg_id);
|
---|
322 | if (info != NULL) {
|
---|
323 | return 1;
|
---|
324 | }
|
---|
325 | }
|
---|
326 | return 0;
|
---|
327 | }
|
---|
328 |
|
---|
329 | /* Check if md5 is enabled on a given listen pcb */
|
---|
330 | static int
|
---|
331 | tcp_md5_is_enabled_on_lpcb(const struct tcp_pcb_listen *lpcb)
|
---|
332 | {
|
---|
333 | /* same as for connection pcbs */
|
---|
334 | return tcp_md5_is_enabled_on_pcb((const struct tcp_pcb *)lpcb);
|
---|
335 | }
|
---|
336 |
|
---|
337 | /* Hook implementation for LWIP_HOOK_TCP_OPT_LENGTH_SEGMENT */
|
---|
338 | u8_t
|
---|
339 | tcp_md5_get_additional_option_length(const struct tcp_pcb *pcb, u8_t internal_option_length)
|
---|
340 | {
|
---|
341 | if ((pcb != NULL) && tcp_md5_is_enabled_on_pcb(pcb)) {
|
---|
342 | u8_t new_option_length = internal_option_length + LWIP_TCP_OPT_LEN_MD5_OUT;
|
---|
343 | LWIP_ASSERT("overflow", new_option_length > internal_option_length);
|
---|
344 | LWIP_ASSERT("options too long", new_option_length <= TCP_MAX_OPTION_BYTES);
|
---|
345 | return new_option_length;
|
---|
346 | }
|
---|
347 | return internal_option_length;
|
---|
348 | }
|
---|
349 |
|
---|
350 | /* Hook implementation for LWIP_HOOK_TCP_INPACKET_PCB when called for listen pcbs */
|
---|
351 | static err_t
|
---|
352 | tcp_md5_check_listen(struct tcp_pcb_listen* lpcb, struct tcp_hdr *hdr, u16_t optlen, u16_t opt1len, u8_t *opt2)
|
---|
353 | {
|
---|
354 | LWIP_ASSERT("lpcb != NULL", lpcb != NULL);
|
---|
355 |
|
---|
356 | if (tcp_md5_is_enabled_on_lpcb(lpcb)) {
|
---|
357 | const u8_t *opts;
|
---|
358 | u8_t digest_received[LWIP_TCP_MD5_DIGEST_LEN];
|
---|
359 | u8_t digest_calculated[LWIP_TCP_MD5_DIGEST_LEN];
|
---|
360 | const struct tcp_md5_conn_info *info = tcp_md5_get_info((struct tcp_pcb *)lpcb, ip_current_src_addr(), hdr->src);
|
---|
361 | if (info != NULL) {
|
---|
362 | opts = tcp_md5_options_singlebuf(hdr, optlen, opt1len, opt2);
|
---|
363 | if (opts != NULL) {
|
---|
364 | if (tcp_md5_parseopt(opts, optlen, digest_received)) {
|
---|
365 | struct tcp_hdr tcphdr_copy;
|
---|
366 | tcp_md5_dup_tcphdr(&tcphdr_copy, hdr, 1);
|
---|
367 | if (tcp_md5_create_digest(ip_current_src_addr(), ip_current_dest_addr(), &tcphdr_copy, info->key, info->key_len, digest_calculated, NULL)) {
|
---|
368 | /* everything set up, compare the digests */
|
---|
369 | if (!memcmp(digest_received, digest_calculated, LWIP_TCP_MD5_DIGEST_LEN)) {
|
---|
370 | /* equal */
|
---|
371 | return ERR_OK;
|
---|
372 | }
|
---|
373 | /* not equal */
|
---|
374 | }
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 | /* md5 enabled on this pcb but no match or other error -> fail */
|
---|
379 | return ERR_VAL;
|
---|
380 | }
|
---|
381 | return ERR_OK;
|
---|
382 | }
|
---|
383 |
|
---|
384 | /* Hook implementation for LWIP_HOOK_TCP_INPACKET_PCB */
|
---|
385 | err_t
|
---|
386 | tcp_md5_check_inpacket(struct tcp_pcb* pcb, struct tcp_hdr *hdr, u16_t optlen, u16_t opt1len, u8_t *opt2, struct pbuf *p)
|
---|
387 | {
|
---|
388 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
389 |
|
---|
390 | if (pcb->state == LISTEN) {
|
---|
391 | return tcp_md5_check_listen((struct tcp_pcb_listen *)pcb, hdr, optlen, opt1len, opt2);
|
---|
392 | }
|
---|
393 |
|
---|
394 | if (tcp_md5_is_enabled_on_pcb(pcb)) {
|
---|
395 | const struct tcp_md5_conn_info *info = tcp_md5_get_info(pcb, ip_current_src_addr(), hdr->src);
|
---|
396 | if (info != NULL) {
|
---|
397 | const u8_t *opts;
|
---|
398 | u8_t digest_received[LWIP_TCP_MD5_DIGEST_LEN];
|
---|
399 | u8_t digest_calculated[LWIP_TCP_MD5_DIGEST_LEN];
|
---|
400 | opts = tcp_md5_options_singlebuf(hdr, optlen, opt1len, opt2);
|
---|
401 | if (opts != NULL) {
|
---|
402 | if (tcp_md5_parseopt(opts, optlen, digest_received)) {
|
---|
403 | struct tcp_hdr hdr_copy;
|
---|
404 | tcp_md5_dup_tcphdr(&hdr_copy, hdr, 1);
|
---|
405 | if (tcp_md5_create_digest(&pcb->remote_ip, &pcb->local_ip, &hdr_copy, info->key, info->key_len, digest_calculated, p)) {
|
---|
406 | /* everything set up, compare the digests */
|
---|
407 | if (!memcmp(digest_received, digest_calculated, LWIP_TCP_MD5_DIGEST_LEN)) {
|
---|
408 | /* equal */
|
---|
409 | return ERR_OK;
|
---|
410 | }
|
---|
411 | /* not equal */
|
---|
412 | }
|
---|
413 | }
|
---|
414 | }
|
---|
415 | }
|
---|
416 | /* md5 enabled on this pcb but no match or other error -> fail */
|
---|
417 | return ERR_VAL;
|
---|
418 | }
|
---|
419 | return ERR_OK;
|
---|
420 | }
|
---|
421 |
|
---|
422 | /* Hook implementation for LWIP_HOOK_TCP_ADD_TX_OPTIONS */
|
---|
423 | u32_t *
|
---|
424 | tcp_md5_add_tx_options(struct pbuf *p, struct tcp_hdr *hdr, const struct tcp_pcb *pcb, u32_t *opts)
|
---|
425 | {
|
---|
426 | LWIP_ASSERT("p != NULL", p != NULL);
|
---|
427 | LWIP_ASSERT("hdr != NULL", hdr != NULL);
|
---|
428 | LWIP_ASSERT("pcb != NULL", pcb != NULL);
|
---|
429 | LWIP_ASSERT("opts != NULL", opts != NULL);
|
---|
430 |
|
---|
431 | if (tcp_md5_is_enabled_on_pcb(pcb)) {
|
---|
432 | u8_t digest_calculated[LWIP_TCP_MD5_DIGEST_LEN];
|
---|
433 | u32_t *opts_ret = opts + 5; /* we use 20 bytes: 2 bytes padding + 18 bytes for this option */
|
---|
434 | u8_t *ptr = (u8_t*)opts;
|
---|
435 |
|
---|
436 | const struct tcp_md5_conn_info *info = tcp_md5_get_info(pcb, &pcb->remote_ip, pcb->remote_port);
|
---|
437 | if (info != NULL) {
|
---|
438 | struct tcp_hdr hdr_copy;
|
---|
439 | size_t hdrsize = TCPH_HDRLEN_BYTES(hdr);
|
---|
440 | tcp_md5_dup_tcphdr(&hdr_copy, hdr, 0);
|
---|
441 | /* p->payload points to the tcp header */
|
---|
442 | LWIP_ASSERT("p->payload == hdr", p->payload == hdr);
|
---|
443 | if (!pbuf_remove_header(p, hdrsize)) {
|
---|
444 | u8_t ret;
|
---|
445 | if (!tcp_md5_create_digest(&pcb->local_ip, &pcb->remote_ip, &hdr_copy, info->key, info->key_len, digest_calculated, p)) {
|
---|
446 | info = NULL;
|
---|
447 | }
|
---|
448 | ret = pbuf_add_header_force(p, hdrsize);
|
---|
449 | LWIP_ASSERT("tcp_md5_add_tx_options: pbuf_add_header_force failed", !ret);
|
---|
450 | LWIP_UNUSED_ARG(ret);
|
---|
451 | } else {
|
---|
452 | LWIP_ASSERT("error", 0);
|
---|
453 | }
|
---|
454 | }
|
---|
455 | if (info == NULL) {
|
---|
456 | /* create an invalid signature by zeroing the digest */
|
---|
457 | memset(&digest_calculated, 0, sizeof(digest_calculated));
|
---|
458 | }
|
---|
459 |
|
---|
460 | *ptr++ = LWIP_TCP_OPT_NOP;
|
---|
461 | *ptr++ = LWIP_TCP_OPT_NOP;
|
---|
462 | *ptr++ = LWIP_TCP_OPT_MD5;
|
---|
463 | *ptr++ = LWIP_TCP_OPT_LEN_MD5;
|
---|
464 | memcpy(ptr, digest_calculated, LWIP_TCP_MD5_DIGEST_LEN);
|
---|
465 | ptr += LWIP_TCP_MD5_DIGEST_LEN;
|
---|
466 | LWIP_ASSERT("ptr == opts_ret", ptr == (u8_t *)opts_ret);
|
---|
467 | return opts_ret;
|
---|
468 | }
|
---|
469 | return opts;
|
---|
470 | }
|
---|
471 |
|
---|
472 | /* Hook implementation for LWIP_HOOK_SOCKETS_SETSOCKOPT */
|
---|
473 | int
|
---|
474 | tcp_md5_setsockopt_hook(struct lwip_sock *sock, int level, int optname, const void *optval, socklen_t optlen, int *err)
|
---|
475 | {
|
---|
476 | LWIP_ASSERT("sock != NULL", sock != NULL);
|
---|
477 | LWIP_ASSERT("err != NULL", err != NULL);
|
---|
478 |
|
---|
479 | if ((level == IPPROTO_TCP) && (optname == TCP_MD5SIG)) {
|
---|
480 | const struct tcp_md5sig *md5 = (const struct tcp_md5sig*)optval;
|
---|
481 | if ((optval == NULL) || (optlen < sizeof(struct tcp_md5sig))) {
|
---|
482 | *err = EINVAL;
|
---|
483 | } else {
|
---|
484 | if (sock->conn && (NETCONNTYPE_GROUP(netconn_type(sock->conn)) == NETCONN_TCP) && (sock->conn->pcb.tcp != NULL)) {
|
---|
485 | if (tcp_md5_extarg_id == LWIP_TCP_PCB_NUM_EXT_ARG_ID_INVALID) {
|
---|
486 | /* not initialized */
|
---|
487 | *err = EINVAL;
|
---|
488 | } else {
|
---|
489 | struct tcp_md5_conn_info *info = tcp_md5_conn_info_alloc();
|
---|
490 | if (info == NULL) {
|
---|
491 | *err = ENOMEM;
|
---|
492 | } else {
|
---|
493 | int addr_valid = 0;
|
---|
494 | /* OK, fill and link this request */
|
---|
495 | memcpy(info->key, md5->tcpm_key, TCP_MD5SIG_MAXKEYLEN);
|
---|
496 | info->key_len = md5->tcpm_keylen;
|
---|
497 | memset(&info->remote_addr, 0, sizeof(info->remote_addr));
|
---|
498 | if (md5->tcpm_addr.ss_family == AF_INET) {
|
---|
499 | #if LWIP_IPV4
|
---|
500 | const struct sockaddr_in *sin = (const struct sockaddr_in *)&md5->tcpm_addr;
|
---|
501 | memcpy(&info->remote_addr, &sin->sin_addr, sizeof(sin->sin_addr));
|
---|
502 | IP_SET_TYPE_VAL(info->remote_addr, IPADDR_TYPE_V4);
|
---|
503 | info->remote_port = lwip_htons(sin->sin_port);
|
---|
504 | addr_valid = 1;
|
---|
505 | #endif /* LWIP_IPV4 */
|
---|
506 | } else if (md5->tcpm_addr.ss_family == AF_INET6) {
|
---|
507 | #if LWIP_IPV6
|
---|
508 | const struct sockaddr_in6 *sin6 = (const struct sockaddr_in6 *)&md5->tcpm_addr;
|
---|
509 | memcpy(&info->remote_addr, &sin6->sin6_addr, sizeof(sin6->sin6_addr));
|
---|
510 | IP_SET_TYPE_VAL(info->remote_addr, IPADDR_TYPE_V6);
|
---|
511 | info->remote_port = lwip_htons(sin6->sin6_port);
|
---|
512 | addr_valid = 1;
|
---|
513 | #endif /* LWIP_IPV6 */
|
---|
514 | }
|
---|
515 | if (addr_valid) {
|
---|
516 | /* store it */
|
---|
517 | tcp_ext_arg_set_callbacks(sock->conn->pcb.tcp, tcp_md5_extarg_id, &tcp_md5_ext_arg_callbacks);
|
---|
518 | info->next = (struct tcp_md5_conn_info *)tcp_ext_arg_get(sock->conn->pcb.tcp, tcp_md5_extarg_id);
|
---|
519 | tcp_ext_arg_set(sock->conn->pcb.tcp, tcp_md5_extarg_id, info);
|
---|
520 | } else {
|
---|
521 | *err = EINVAL;
|
---|
522 | tcp_md5_conn_info_free(info);
|
---|
523 | }
|
---|
524 | }
|
---|
525 | }
|
---|
526 | } else {
|
---|
527 | /* not a tcp netconn */
|
---|
528 | *err = EINVAL;
|
---|
529 | }
|
---|
530 | }
|
---|
531 | return 1;
|
---|
532 | }
|
---|
533 | return 0;
|
---|
534 | }
|
---|