summaryrefslogtreecommitdiffstats
path: root/src/lib-ssl-iostream/ostream-openssl.c
blob: 8434a9b7ccada3ec8f02ca2426cde6f2106c9bc9 (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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* Copyright (c) 2009-2018 Dovecot authors, see the included COPYING file */

#include "lib.h"
#include "buffer.h"
#include "istream.h"
#include "ostream-private.h"
#include "iostream-openssl.h"

struct ssl_ostream {
	struct ostream_private ostream;
	struct ssl_iostream *ssl_io;
	buffer_t *buffer;

	bool shutdown:1;
};

static void
o_stream_ssl_close(struct iostream_private *stream, bool close_parent)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;

	if (close_parent)
		o_stream_close(sstream->ssl_io->plain_output);
}

static void o_stream_ssl_destroy(struct iostream_private *stream)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;
	struct istream *ssl_input = sstream->ssl_io->ssl_input;

	openssl_iostream_shutdown(sstream->ssl_io);
	sstream->ssl_io->ssl_output = NULL;
	i_stream_unref(&ssl_input);
	ssl_iostream_unref(&sstream->ssl_io);
	buffer_free(&sstream->buffer);
}

static size_t get_buffer_avail_size(const struct ssl_ostream *sstream)
{
	if (sstream->ostream.max_buffer_size == 0) {
		if (sstream->buffer == NULL)
			return 0;
		/* we're requested to use whatever space is available in
		   the buffer */
		return buffer_get_writable_size(sstream->buffer) - sstream->buffer->used;
	} else {
		size_t buffer_used = (sstream->buffer == NULL ? 0 :
				      sstream->buffer->used);
		return sstream->ostream.max_buffer_size > buffer_used ?
			sstream->ostream.max_buffer_size - buffer_used : 0;
	}
}

static size_t
o_stream_ssl_buffer(struct ssl_ostream *sstream, const struct const_iovec *iov,
		    unsigned int iov_count, size_t bytes_sent)
{
	size_t avail, skip_left, size;
	unsigned int i;

	if (sstream->buffer == NULL)
		sstream->buffer = buffer_create_dynamic(default_pool,
			I_MIN(IO_BLOCK_SIZE, sstream->ostream.max_buffer_size));

	skip_left = bytes_sent;
	for (i = 0; i < iov_count; i++) {
		if (skip_left < iov[i].iov_len)
			break;
		skip_left -= iov[i].iov_len;
	}

	avail = get_buffer_avail_size(sstream);
	if (i < iov_count && skip_left > 0) {
		size = I_MIN(iov[i].iov_len - skip_left, avail);
		buffer_append(sstream->buffer,
			      CONST_PTR_OFFSET(iov[i].iov_base, skip_left),
			      size);
		bytes_sent += size;
		avail -= size;
		if (size != iov[i].iov_len)
			i = iov_count;
	}
	if (avail > 0)
		o_stream_set_flush_pending(sstream->ssl_io->plain_output, TRUE);

	for (; i < iov_count; i++) {
		size = I_MIN(iov[i].iov_len, avail);
		buffer_append(sstream->buffer, iov[i].iov_base, size);
		bytes_sent += size;
		avail -= size;

		if (size != iov[i].iov_len)
			break;
	}

	sstream->ostream.ostream.offset += bytes_sent;
	return bytes_sent;
}

static int o_stream_ssl_flush_buffer(struct ssl_ostream *sstream)
{
	struct ssl_iostream *ssl_io = sstream->ssl_io;
	size_t pos = 0;
	int ret = 1;

	i_assert(!sstream->shutdown);

	while (pos < sstream->buffer->used) {
		/* we're writing plaintext data to OpenSSL, which it encrypts
		   and writes to bio_int's buffer. ssl_iostream_bio_sync()
		   reads it from there and adds to plain_output stream. */
		ret = SSL_write(ssl_io->ssl,
				CONST_PTR_OFFSET(sstream->buffer->data, pos),
				sstream->buffer->used - pos);
		if (ret <= 0) {
			ret = openssl_iostream_handle_error(
				ssl_io, ret, OPENSSL_IOSTREAM_SYNC_TYPE_WRITE,
				"SSL_write");
			if (ret < 0) {
				io_stream_set_error(
					&sstream->ostream.iostream,
					"%s", ssl_io->last_error);
				sstream->ostream.ostream.stream_errno = errno;
				break;
			}
			if (ret == 0)
				break;
		} else {
			pos += ret;
			ret = openssl_iostream_bio_sync(
				ssl_io, OPENSSL_IOSTREAM_SYNC_TYPE_WRITE);
			if (ret < 0) {
				i_assert(ssl_io->plain_stream_errstr != NULL &&
					 ssl_io->plain_stream_errno != 0);
				io_stream_set_error(
					&sstream->ostream.iostream,
					"%s", ssl_io->plain_stream_errstr);
				sstream->ostream.ostream.stream_errno =
					ssl_io->plain_stream_errno;
				break;
			}
		}
	}
	buffer_delete(sstream->buffer, 0, pos);
	return ret <= 0 ? ret : 1;
}

static int o_stream_ssl_flush(struct ostream_private *stream)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;
	struct ssl_iostream *ssl_io = sstream->ssl_io;
	struct ostream *plain_output = ssl_io->plain_output;
	int ret = 1;

	if (!ssl_io->handshaked) {
		if ((ret = ssl_iostream_handshake(ssl_io)) < 0) {
			/* handshake failed */
			i_assert(errno != 0);
			io_stream_set_error(&stream->iostream,
					    "%s", ssl_io->last_error);
			stream->ostream.stream_errno = errno;
			return ret;
		}
	}
	if (ret > 0 &&
	    openssl_iostream_bio_sync(
		ssl_io, OPENSSL_IOSTREAM_SYNC_TYPE_HANDSHAKE) < 0) {
		i_assert(ssl_io->plain_stream_errno != 0 &&
			 ssl_io->plain_stream_errstr != NULL);
		io_stream_set_error(&stream->iostream,
				    "%s", ssl_io->plain_stream_errstr);
		stream->ostream.stream_errno = ssl_io->plain_stream_errno;
		return -1;
	}

	if (ret > 0 && sstream->buffer != NULL && sstream->buffer->used > 0) {
		/* we can try to send some of our buffered data */
		ret = o_stream_ssl_flush_buffer(sstream);
	}

	/* Stream is finished; shutdown the SSL write direction once our buffer
	   is empty. */
	if (stream->finished && !sstream->shutdown && ret >= 0 &&
	    (sstream->buffer == NULL || sstream->buffer->used == 0)) {
		sstream->shutdown = TRUE;
		if (SSL_shutdown(ssl_io->ssl) < 0) {
			io_stream_set_error(
				&sstream->ostream.iostream, "%s",
				t_strdup_printf("SSL_shutdown() failed: %s",
						openssl_iostream_error()));
			sstream->ostream.ostream.stream_errno = EIO;
			ret = -1;
		}
	}

	if (ret == 0 && ssl_io->want_read) {
		/* we need to read more data until we can continue. */
		o_stream_set_flush_pending(plain_output, FALSE);
		ssl_io->ostream_flush_waiting_input = TRUE;
		ret = 1;
	}

	if (ret <= 0)
		return ret;

	/* return 1 only when the output buffer is empty, which is what the
	   caller expects. */
	return o_stream_get_buffer_used_size(plain_output) == 0 ? 1 : 0;
}

static ssize_t
o_stream_ssl_sendv(struct ostream_private *stream,
		   const struct const_iovec *iov, unsigned int iov_count)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;
	size_t bytes_sent = 0;

	i_assert(!sstream->shutdown);

	bytes_sent = o_stream_ssl_buffer(sstream, iov, iov_count, bytes_sent);
	if (sstream->ssl_io->handshaked &&
	    sstream->buffer->used == bytes_sent) {
		/* buffer was empty before calling this. try to write it
		   immediately. */
		if (o_stream_ssl_flush_buffer(sstream) < 0)
			return -1;
	}
	return bytes_sent;
}

static void o_stream_ssl_switch_ioloop_to(struct ostream_private *stream,
					  struct ioloop *ioloop)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)stream;

	o_stream_switch_ioloop_to(sstream->ssl_io->plain_output, ioloop);
}

static int plain_flush_callback(struct ssl_ostream *sstream)
{
	struct ostream *ostream = &sstream->ostream.ostream;
	int ret, ret2;

	/* try to actually flush the pending data */
	if ((ret = o_stream_flush(sstream->ssl_io->plain_output)) < 0)
		return -1;

	/* we may be able to copy more data, try it */
	o_stream_ref(ostream);
	if (sstream->ostream.callback != NULL)
		ret2 = sstream->ostream.callback(sstream->ostream.context);
	else
		ret2 = o_stream_flush(&sstream->ostream.ostream);
	if (ret2 == 0)
		o_stream_set_flush_pending(sstream->ssl_io->plain_output, TRUE);
	o_stream_unref(&ostream);
	if (ret2 < 0)
		return -1;
	return ret > 0 && ret2 > 0 ? 1 : 0;
}

static size_t
o_stream_ssl_get_buffer_used_size(const struct ostream_private *stream)
{
	const struct ssl_ostream *sstream = (const struct ssl_ostream *)stream;
	BIO *bio = SSL_get_wbio(sstream->ssl_io->ssl);
	size_t wbuf_avail = BIO_ctrl_get_write_guarantee(bio);
	size_t wbuf_total_size = BIO_get_write_buf_size(bio, 0);
	size_t buffer_used = (sstream->buffer == NULL ? 0 :
			      sstream->buffer->used);
	i_assert(wbuf_avail <= wbuf_total_size);
	return buffer_used + (wbuf_total_size - wbuf_avail) +
		o_stream_get_buffer_used_size(sstream->ssl_io->plain_output);
}

static size_t
o_stream_ssl_get_buffer_avail_size(const struct ostream_private *stream)
{
	const struct ssl_ostream *sstream = (const struct ssl_ostream *)stream;

	return get_buffer_avail_size(sstream);
}

static void
o_stream_ssl_flush_pending(struct ostream_private *_stream, bool set)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)_stream;

	o_stream_set_flush_pending(sstream->ssl_io->plain_output, set);
}

static void o_stream_ssl_set_max_buffer_size(struct iostream_private *_stream,
					     size_t max_size)
{
	struct ssl_ostream *sstream = (struct ssl_ostream *)_stream;

	sstream->ostream.max_buffer_size = max_size;
	o_stream_set_max_buffer_size(sstream->ssl_io->plain_output, max_size);
}

struct ostream *openssl_o_stream_create_ssl(struct ssl_iostream *ssl_io)
{
	struct ssl_ostream *sstream;

	ssl_io->refcount++;

	/* When ostream is destroyed, it's flushed. With iostream-ssl the
	   flushing requires both istream and ostream to be available. The
	   istream is referenced here to make sure it's not destroyed before
	   the ostream. */
	i_assert(ssl_io->ssl_input != NULL);
	i_stream_ref(ssl_io->ssl_input);

	sstream = i_new(struct ssl_ostream, 1);
	sstream->ssl_io = ssl_io;
	sstream->ostream.max_buffer_size =
		ssl_io->plain_output->real_stream->max_buffer_size;
	sstream->ostream.iostream.close = o_stream_ssl_close;
	sstream->ostream.iostream.destroy = o_stream_ssl_destroy;
	sstream->ostream.sendv = o_stream_ssl_sendv;
	sstream->ostream.flush = o_stream_ssl_flush;
	sstream->ostream.switch_ioloop_to = o_stream_ssl_switch_ioloop_to;

	sstream->ostream.get_buffer_used_size =
		o_stream_ssl_get_buffer_used_size;
	sstream->ostream.get_buffer_avail_size =
		o_stream_ssl_get_buffer_avail_size;
	sstream->ostream.flush_pending = o_stream_ssl_flush_pending;
	sstream->ostream.iostream.set_max_buffer_size =
		o_stream_ssl_set_max_buffer_size;

	sstream->ostream.callback = ssl_io->plain_output->real_stream->callback;
	sstream->ostream.context = ssl_io->plain_output->real_stream->context;
	o_stream_set_flush_callback(ssl_io->plain_output,
				    plain_flush_callback, sstream);

	return o_stream_create(&sstream->ostream, NULL,
			       o_stream_get_fd(ssl_io->plain_output));
}