summaryrefslogtreecommitdiff
path: root/ustream-ssl.c
blob: cd69f9e97449435ee407f81b36ae49930cbef5fc (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
/*
 * ustream-ssl - library for SSL over ustream
 *
 * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org>
 *
 * Permission to use, copy, modify, and/or distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <libubox/ustream.h>

#include "ustream-ssl.h"
#include "ustream-internal.h"

static void ustream_ssl_error_cb(struct uloop_timeout *t)
{
	struct ustream_ssl *us = container_of(t, struct ustream_ssl, error_timer);
	static char buffer[128];
	int error = us->error;

	if (us->notify_error)
		us->notify_error(us, error, __ustream_ssl_strerror(us->error, buffer, sizeof(buffer)));
}

static void ustream_ssl_check_conn(struct ustream_ssl *us)
{
	if (us->connected || us->error)
		return;

	if (__ustream_ssl_connect(us) == U_SSL_OK) {

		/* __ustream_ssl_connect() will also return U_SSL_OK when certificate
		 * verification failed!
		 *
		 * Applications may register a custom .notify_verify_error callback in the
		 * struct ustream_ssl which is called upon verification failures, but there
		 * is no straight forward way for the callback to terminate the connection
		 * initiation right away, e.g. through a true or false return value.
		 *
		 * Instead, existing implementations appear to set .eof field of the underlying
		 * ustream in the hope that this inhibits further operations on the stream.
		 *
		 * Declare this informal behaviour "official" and check for the state of the
		 * .eof member after __ustream_ssl_connect() returned, and do not write the
		 * pending data if it is set to true.
		 */

		if (us->stream.eof)
			return;

		us->connected = true;
		if (us->notify_connected)
			us->notify_connected(us);
		ustream_write_pending(&us->stream);
	}
}

static bool __ustream_ssl_poll(struct ustream *s)
{
	struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);
	char *buf;
	int len, ret;
	bool more = false;

	ustream_ssl_check_conn(us);
	if (!us->connected || us->error)
		return false;

	do {
		buf = ustream_reserve(&us->stream, 1, &len);
		if (!len)
			break;

		ret = __ustream_ssl_read(us, buf, len);
		switch (ret) {
		case U_SSL_PENDING:
			return more;
		case U_SSL_ERROR:
			return false;
		case 0:
			us->stream.eof = true;
			ustream_state_change(&us->stream);
			return false;
		default:
			ustream_fill_read(&us->stream, ret);
			more = true;
			continue;
		}
	} while (1);

	return more;
}

static void ustream_ssl_notify_read(struct ustream *s, int bytes)
{
	__ustream_ssl_poll(s);
}

static void ustream_ssl_notify_write(struct ustream *s, int bytes)
{
	struct ustream_ssl *us = container_of(s->next, struct ustream_ssl, stream);

	ustream_ssl_check_conn(us);
	ustream_write_pending(s->next);
}

static void ustream_ssl_notify_state(struct ustream *s)
{
	s->next->write_error = true;
	ustream_state_change(s->next);
}

static int ustream_ssl_write(struct ustream *s, const char *buf, int len, bool more)
{
	struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);

	if (!us->connected || us->error)
		return 0;

	if (us->conn->w.data_bytes)
		return 0;

	return __ustream_ssl_write(us, buf, len);
}

static void ustream_ssl_set_read_blocked(struct ustream *s)
{
	struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);

	ustream_set_read_blocked(us->conn, !!s->read_blocked);
}

static void ustream_ssl_free(struct ustream *s)
{
	struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);

	if (us->conn) {
		us->conn->next = NULL;
		us->conn->notify_read = NULL;
		us->conn->notify_write = NULL;
		us->conn->notify_state = NULL;
	}

	uloop_timeout_cancel(&us->error_timer);
	__ustream_ssl_session_free(us->ssl);
	free(us->peer_cn);

	us->ctx = NULL;
	us->ssl = NULL;
	us->conn = NULL;
	us->peer_cn = NULL;
	us->connected = false;
	us->error = false;
	us->valid_cert = false;
	us->valid_cn = false;
}

static bool ustream_ssl_poll(struct ustream *s)
{
	struct ustream_ssl *us = container_of(s, struct ustream_ssl, stream);
	bool fd_poll;

	fd_poll = ustream_poll(us->conn);
	return __ustream_ssl_poll(us->conn) || fd_poll;
}

static void ustream_ssl_stream_init(struct ustream_ssl *us)
{
	struct ustream *conn = us->conn;
	struct ustream *s = &us->stream;

	conn->notify_read = ustream_ssl_notify_read;
	conn->notify_write = ustream_ssl_notify_write;
	conn->notify_state = ustream_ssl_notify_state;

	s->free = ustream_ssl_free;
	s->write = ustream_ssl_write;
	s->poll = ustream_ssl_poll;
	s->set_read_blocked = ustream_ssl_set_read_blocked;
	ustream_init_defaults(s);
}

static int _ustream_ssl_init(struct ustream_ssl *us, struct ustream *conn, struct ustream_ssl_ctx *ctx, bool server)
{
	us->error_timer.cb = ustream_ssl_error_cb;
	us->server = server;
	us->conn = conn;
	us->ctx = ctx;

	us->ssl = __ustream_ssl_session_new(us->ctx);
	if (!us->ssl)
		return -ENOMEM;

	conn->next = &us->stream;
	ustream_set_io(ctx, us->ssl, conn);
	ustream_ssl_stream_init(us);

	if (us->server_name)
		__ustream_ssl_set_server_name(us);

	ustream_ssl_check_conn(us);

	return 0;
}

static int _ustream_ssl_set_peer_cn(struct ustream_ssl *us, const char *name)
{
	us->peer_cn = strdup(name);
	__ustream_ssl_update_peer_cn(us);

	return 0;
}

const struct ustream_ssl_ops ustream_ssl_ops = {
	.context_new = __ustream_ssl_context_new,
	.context_set_crt_file = __ustream_ssl_set_crt_file,
	.context_set_key_file = __ustream_ssl_set_key_file,
	.context_add_ca_crt_file = __ustream_ssl_add_ca_crt_file,
	.context_set_ciphers = __ustream_ssl_set_ciphers,
	.context_set_require_validation = __ustream_ssl_set_require_validation,
	.context_free = __ustream_ssl_context_free,
	.init = _ustream_ssl_init,
	.set_peer_cn = _ustream_ssl_set_peer_cn,
};