summaryrefslogtreecommitdiff
path: root/samples/net/http_server/src/ssl_utils.c
blob: ac75aadd0e1e951c2a37a802c0ee70d3f826a2e2 (plain)
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/*
 * Copyright (c) 2017 Intel Corporation
 *
 * SPDX-License-Identifier: Apache-2.0
 */

#include <zephyr.h>
#include <net/net_core.h>
#include <net/net_context.h>
#include <net/nbuf.h>
#include <net/net_if.h>
#include <string.h>
#include <errno.h>
#include <misc/printk.h>

#if !defined(CONFIG_MBEDTLS_CFG_FILE)
#include "mbedtls/config.h"
#else
#include CONFIG_MBEDTLS_CFG_FILE
#endif

#include "mbedtls/ssl.h"

#include "config.h"
#include "ssl_utils.h"

#define RX_FIFO_DEPTH 4

K_MEM_POOL_DEFINE(rx_pkts, 4, 64, RX_FIFO_DEPTH, 4);

static void ssl_received(struct net_context *context,
			 struct net_buf *buf, int status, void *user_data)
{
	struct ssl_context *ctx = user_data;
	struct rx_fifo_block *rx_data = NULL;
	struct k_mem_block block;

	ARG_UNUSED(context);
	ARG_UNUSED(status);

	if (!net_nbuf_appdatalen(buf)) {
		net_nbuf_unref(buf);
		return;
	}

	k_mem_pool_alloc(&rx_pkts, &block,
			 sizeof(struct rx_fifo_block), K_FOREVER);
	rx_data = block.data;
	rx_data->buf = buf;

	/* For freeing memory later */
	memcpy(&rx_data->block, &block, sizeof(struct k_mem_block));
	k_fifo_put(&ctx->rx_fifo, (void *)rx_data);
}

static inline void ssl_sent(struct net_context *context,
			    int status, void *token, void *user_data)
{
	struct ssl_context *ctx = user_data;

	k_sem_give(&ctx->tx_sem);
}

int ssl_tx(void *context, const unsigned char *buf, size_t size)
{
	struct ssl_context *ctx = context;
	struct net_context *net_ctx;
	struct net_buf *send_buf;

	int rc, len;

	net_ctx = ctx->net_ctx;

	send_buf = net_nbuf_get_tx(net_ctx, K_NO_WAIT);
	if (!send_buf) {
		return MBEDTLS_ERR_SSL_ALLOC_FAILED;
	}

	rc = net_nbuf_append(send_buf, size, (uint8_t *) buf, K_FOREVER);
	if (!rc) {
		net_nbuf_unref(send_buf);
		return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
	}

	len = net_buf_frags_len(send_buf);

	rc = net_context_send(send_buf, ssl_sent, K_NO_WAIT, NULL, ctx);

	if (rc < 0) {
		net_nbuf_unref(send_buf);
		return MBEDTLS_ERR_SSL_INTERNAL_ERROR;
	}

	k_sem_take(&ctx->tx_sem, K_FOREVER);
	return len;
}

int ssl_rx(void *context, unsigned char *buf, size_t size)
{
	struct ssl_context *ctx = context;
	uint16_t read_bytes;
	struct rx_fifo_block *rx_data;
	uint8_t *ptr;
	int pos;
	int len;
	int rc = 0;

	if (ctx->frag == NULL) {
		rx_data = k_fifo_get(&ctx->rx_fifo, K_FOREVER);
		ctx->rx_nbuf = rx_data->buf;
		k_mem_pool_free(&rx_data->block);

		read_bytes = net_nbuf_appdatalen(ctx->rx_nbuf);

		ctx->remaining = read_bytes;
		ctx->frag = ctx->rx_nbuf->frags;
		ptr = net_nbuf_appdata(ctx->rx_nbuf);

		len = ptr - ctx->frag->data;
		net_buf_pull(ctx->frag, len);
	} else {
		read_bytes = ctx->remaining;
		ptr = ctx->frag->data;
	}

	len = ctx->frag->len;
	pos = 0;
	if (read_bytes > size) {
		while (ctx->frag) {
			read_bytes = len < (size - pos) ? len : (size - pos);
			memcpy(buf + pos, ptr, read_bytes);
			pos += read_bytes;
			if (pos < size) {
				ctx->frag = ctx->frag->frags;
				ptr = ctx->frag->data;
				len = ctx->frag->len;
			} else {
				if (read_bytes == len) {
					ctx->frag = ctx->frag->frags;
				} else {
					net_buf_pull(ctx->frag, read_bytes);
				}

				ctx->remaining -= size;
				return size;
			}
		}
	} else {
		while (ctx->frag) {
			memcpy(buf + pos, ptr, len);
			pos += len;
			ctx->frag = ctx->frag->frags;
			if (!ctx->frag) {
				break;
			}

			ptr = ctx->frag->data;
			len = ctx->frag->len;
		}

		net_nbuf_unref(ctx->rx_nbuf);
		ctx->rx_nbuf = NULL;
		ctx->frag = NULL;
		ctx->remaining = 0;

		if (read_bytes != pos) {
			return -EIO;
		}

		rc = read_bytes;
	}

	return rc;
}

static void ssl_accepted(struct net_context *context,
			 struct sockaddr *addr,
			 socklen_t addrlen, int error, void *user_data)
{
	int ret;
	struct ssl_context *ctx = user_data;

	ctx->net_ctx = context;
	ret = net_context_recv(context, ssl_received, 0, user_data);
	if (ret < 0) {
		printk("Cannot receive TCP packet (family %d)",
		       net_context_get_family(context));
	}

}

#if defined(CONFIG_NET_IPV6)
int ssl_init(struct ssl_context *ctx, void *addr)
{
	struct net_context *tcp_ctx = { 0 };
	struct sockaddr_in6 my_addr = { 0 };
	struct in6_addr *server_addr = addr;
	int rc;

	k_sem_init(&ctx->tx_sem, 0, UINT_MAX);
	k_fifo_init(&ctx->rx_fifo);

	my_mcast_addr.sin6_family = AF_INET6;

	net_ipaddr_copy(&my_addr.sin6_addr, server_addr);
	my_addr.sin6_family = AF_INET6;
	my_addr.sin6_port = htons(SERVER_PORT);

	rc = net_context_get(AF_INET6, SOCK_STREAM, IPPROTO_TCP, &tcp_ctx);
	if (rc < 0) {
		printk("Cannot get network context for IPv6 TCP (%d)", rc);
		return -EIO;
	}

	rc = net_context_bind(tcp_ctx, (struct sockaddr *)&my_addr,
			      sizeof(struct sockaddr_in6));
	if (rc < 0) {
		printk("Cannot bind IPv6 TCP port %d (%d)", SERVER_PORT, rc);
		goto error;
	}

	ctx->rx_nbuf = NULL;
	ctx->remaining = 0;
	ctx->net_ctx = tcp_ctx;

	rc = net_context_listen(ctx->net_ctx, 0);
	if (rc < 0) {
		printk("Cannot listen IPv6 TCP (%d)", rc);
		return -EIO;
	}

	rc = net_context_accept(ctx->net_ctx, ssl_accepted, 0, ctx);
	if (rc < 0) {
		printk("Cannot accept IPv4 (%d)", rc);
		return -EIO;
	}

	return 0;

error:
	net_context_put(tcp_ctx);
	return -EINVAL;
}

#else
int ssl_init(struct ssl_context *ctx, void *addr)
{
	struct net_context *tcp_ctx = { 0 };
	struct sockaddr_in my_addr4 = { 0 };
	struct in_addr *server_addr = addr;
	int rc;

	k_sem_init(&ctx->tx_sem, 0, UINT_MAX);
	k_fifo_init(&ctx->rx_fifo);

	net_ipaddr_copy(&my_addr4.sin_addr, server_addr);
	my_addr4.sin_family = AF_INET;
	my_addr4.sin_port = htons(SERVER_PORT);

	rc = net_context_get(AF_INET, SOCK_STREAM, IPPROTO_TCP, &tcp_ctx);
	if (rc < 0) {
		printk("Cannot get network context for IPv4 TCP (%d)", rc);
		return -EIO;
	}

	rc = net_context_bind(tcp_ctx, (struct sockaddr *)&my_addr4,
			      sizeof(struct sockaddr_in));
	if (rc < 0) {
		printk("Cannot bind IPv4 TCP port %d (%d)", SERVER_PORT, rc);
		goto error;
	}

	ctx->rx_nbuf = NULL;
	ctx->remaining = 0;
	ctx->net_ctx = tcp_ctx;

	rc = net_context_listen(ctx->net_ctx, 0);
	if (rc < 0) {
		printk("Cannot listen IPv4 (%d)", rc);
		return -EIO;
	}

	rc = net_context_accept(ctx->net_ctx, ssl_accepted, 0, ctx);
	if (rc < 0) {
		printk("Cannot accept IPv4 (%d)", rc);
		return -EIO;
	}

	return 0;

error:
	net_context_put(tcp_ctx);
	return -EINVAL;
}
#endif