]> git.ipfire.org Git - thirdparty/openssl.git/blame - ssl/bio_ssl.c
Ensure that BIO_read_ex() and BIO_write_ex() only return 0 or 1
[thirdparty/openssl.git] / ssl / bio_ssl.c
CommitLineData
846e33c7
RS
1/*
2 * Copyright 1995-2016 The OpenSSL Project Authors. All Rights Reserved.
d02b48c6 3 *
846e33c7
RS
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
d02b48c6
RE
8 */
9
10#include <stdio.h>
58964a49 11#include <stdlib.h>
d02b48c6
RE
12#include <string.h>
13#include <errno.h>
ec577822 14#include <openssl/crypto.h>
a146ae55 15#include "internal/bio.h"
ec577822 16#include <openssl/err.h>
b6ba4014 17#include "ssl_locl.h"
d02b48c6 18
3befffa3 19static int ssl_write(BIO *h, const char *buf, size_t num, size_t *written);
d07aee2c 20static int ssl_read(BIO *b, char *out, size_t outl, size_t *read);
0e1c0612
UM
21static int ssl_puts(BIO *h, const char *str);
22static long ssl_ctrl(BIO *h, int cmd, long arg1, void *arg2);
d02b48c6
RE
23static int ssl_new(BIO *h);
24static int ssl_free(BIO *data);
13083215 25static long ssl_callback_ctrl(BIO *h, int cmd, bio_info_cb *fp);
0f113f3e
MC
26typedef struct bio_ssl_st {
27 SSL *ssl; /* The ssl handle :-) */
28 /* re-negotiate every time the total number of bytes is this size */
29 int num_renegotiates;
30 unsigned long renegotiate_count;
31 unsigned long byte_count;
32 unsigned long renegotiate_timeout;
33 unsigned long last_time;
34} BIO_SSL;
35
04f6b0fd 36static const BIO_METHOD methods_sslp = {
0f113f3e
MC
37 BIO_TYPE_SSL, "ssl",
38 ssl_write,
3befffa3 39 NULL,
0f113f3e 40 ssl_read,
d07aee2c 41 NULL,
0f113f3e
MC
42 ssl_puts,
43 NULL, /* ssl_gets, */
44 ssl_ctrl,
45 ssl_new,
46 ssl_free,
47 ssl_callback_ctrl,
48};
a9188d4e 49
04f6b0fd 50const BIO_METHOD *BIO_f_ssl(void)
0f113f3e
MC
51{
52 return (&methods_sslp);
53}
d02b48c6 54
6b691a5c 55static int ssl_new(BIO *bi)
0f113f3e 56{
b51bce94 57 BIO_SSL *bs = OPENSSL_zalloc(sizeof(*bs));
0f113f3e 58
0f113f3e
MC
59 if (bs == NULL) {
60 BIOerr(BIO_F_SSL_NEW, ERR_R_MALLOC_FAILURE);
61 return (0);
62 }
a146ae55
MC
63 BIO_set_init(bi, 0);
64 BIO_set_data(bi, bs);
65 /* Clear all flags */
66 BIO_clear_flags(bi, ~0);
67
68 return 1;
0f113f3e 69}
d02b48c6 70
6b691a5c 71static int ssl_free(BIO *a)
0f113f3e
MC
72{
73 BIO_SSL *bs;
74
75 if (a == NULL)
76 return (0);
a146ae55 77 bs = BIO_get_data(a);
0f113f3e
MC
78 if (bs->ssl != NULL)
79 SSL_shutdown(bs->ssl);
a146ae55
MC
80 if (BIO_get_shutdown(a)) {
81 if (BIO_get_init(a))
0f113f3e 82 SSL_free(bs->ssl);
a146ae55
MC
83 /* Clear all flags */
84 BIO_clear_flags(a, ~0);
85 BIO_set_init(a, 0);
0f113f3e 86 }
a146ae55
MC
87 OPENSSL_free(bs);
88 return 1;
0f113f3e
MC
89}
90
ac0edec1 91static int ssl_read(BIO *b, char *out, size_t outl, size_t *readbytes)
0f113f3e
MC
92{
93 int ret = 1;
94 BIO_SSL *sb;
95 SSL *ssl;
96 int retry_reason = 0;
97 int r = 0;
d02b48c6 98
0f113f3e
MC
99 if (out == NULL)
100 return (0);
a146ae55 101 sb = BIO_get_data(b);
0f113f3e 102 ssl = sb->ssl;
d02b48c6 103
0f113f3e 104 BIO_clear_retry_flags(b);
d02b48c6 105
d07aee2c
MC
106 if (outl > INT_MAX)
107 return -1;
108
0f113f3e 109 ret = SSL_read(ssl, out, outl);
3befffa3 110 if (ret > 0)
ac0edec1 111 *readbytes = ret;
0f113f3e
MC
112
113 switch (SSL_get_error(ssl, ret)) {
114 case SSL_ERROR_NONE:
115 if (ret <= 0)
116 break;
117 if (sb->renegotiate_count > 0) {
ac0edec1 118 sb->byte_count += *readbytes;
0f113f3e
MC
119 if (sb->byte_count > sb->renegotiate_count) {
120 sb->byte_count = 0;
121 sb->num_renegotiates++;
122 SSL_renegotiate(ssl);
123 r = 1;
124 }
125 }
126 if ((sb->renegotiate_timeout > 0) && (!r)) {
127 unsigned long tm;
128
129 tm = (unsigned long)time(NULL);
130 if (tm > sb->last_time + sb->renegotiate_timeout) {
131 sb->last_time = tm;
132 sb->num_renegotiates++;
133 SSL_renegotiate(ssl);
134 }
135 }
136
137 break;
138 case SSL_ERROR_WANT_READ:
139 BIO_set_retry_read(b);
140 break;
141 case SSL_ERROR_WANT_WRITE:
142 BIO_set_retry_write(b);
143 break;
144 case SSL_ERROR_WANT_X509_LOOKUP:
145 BIO_set_retry_special(b);
146 retry_reason = BIO_RR_SSL_X509_LOOKUP;
147 break;
148 case SSL_ERROR_WANT_ACCEPT:
149 BIO_set_retry_special(b);
150 retry_reason = BIO_RR_ACCEPT;
151 break;
152 case SSL_ERROR_WANT_CONNECT:
153 BIO_set_retry_special(b);
154 retry_reason = BIO_RR_CONNECT;
155 break;
156 case SSL_ERROR_SYSCALL:
157 case SSL_ERROR_SSL:
158 case SSL_ERROR_ZERO_RETURN:
159 default:
160 break;
161 }
162
a146ae55 163 BIO_set_retry_reason(b, retry_reason);
d07aee2c 164
3befffa3 165 return ret;
0f113f3e 166}
d02b48c6 167
3befffa3 168static int ssl_write(BIO *b, const char *out, size_t outl, size_t *written)
0f113f3e
MC
169{
170 int ret, r = 0;
171 int retry_reason = 0;
172 SSL *ssl;
173 BIO_SSL *bs;
174
175 if (out == NULL)
176 return (0);
a146ae55 177 bs = BIO_get_data(b);
0f113f3e
MC
178 ssl = bs->ssl;
179
180 BIO_clear_retry_flags(b);
181
3befffa3
MC
182 if (outl > INT_MAX)
183 return 0;
184
0f113f3e
MC
185 ret = SSL_write(ssl, out, outl);
186
187 switch (SSL_get_error(ssl, ret)) {
188 case SSL_ERROR_NONE:
189 if (ret <= 0)
190 break;
191 if (bs->renegotiate_count > 0) {
192 bs->byte_count += ret;
193 if (bs->byte_count > bs->renegotiate_count) {
194 bs->byte_count = 0;
195 bs->num_renegotiates++;
196 SSL_renegotiate(ssl);
197 r = 1;
198 }
199 }
200 if ((bs->renegotiate_timeout > 0) && (!r)) {
201 unsigned long tm;
202
203 tm = (unsigned long)time(NULL);
204 if (tm > bs->last_time + bs->renegotiate_timeout) {
205 bs->last_time = tm;
206 bs->num_renegotiates++;
207 SSL_renegotiate(ssl);
208 }
209 }
210 break;
211 case SSL_ERROR_WANT_WRITE:
212 BIO_set_retry_write(b);
213 break;
214 case SSL_ERROR_WANT_READ:
215 BIO_set_retry_read(b);
216 break;
217 case SSL_ERROR_WANT_X509_LOOKUP:
218 BIO_set_retry_special(b);
219 retry_reason = BIO_RR_SSL_X509_LOOKUP;
220 break;
221 case SSL_ERROR_WANT_CONNECT:
222 BIO_set_retry_special(b);
223 retry_reason = BIO_RR_CONNECT;
224 case SSL_ERROR_SYSCALL:
225 case SSL_ERROR_SSL:
226 default:
227 break;
228 }
229
a146ae55 230 BIO_set_retry_reason(b, retry_reason);
3befffa3
MC
231
232 if (ret > 0) {
233 *written = ret;
234 ret = 1;
235 }
236
a146ae55 237 return ret;
0f113f3e 238}
d02b48c6 239
0e1c0612 240static long ssl_ctrl(BIO *b, int cmd, long num, void *ptr)
0f113f3e
MC
241{
242 SSL **sslp, *ssl;
a146ae55 243 BIO_SSL *bs, *dbs;
0f113f3e
MC
244 BIO *dbio, *bio;
245 long ret = 1;
a146ae55 246 BIO *next;
0f113f3e 247
a146ae55
MC
248 bs = BIO_get_data(b);
249 next = BIO_next(b);
0f113f3e
MC
250 ssl = bs->ssl;
251 if ((ssl == NULL) && (cmd != BIO_C_SET_SSL))
252 return (0);
253 switch (cmd) {
254 case BIO_CTRL_RESET:
255 SSL_shutdown(ssl);
256
257 if (ssl->handshake_func == ssl->method->ssl_connect)
258 SSL_set_connect_state(ssl);
259 else if (ssl->handshake_func == ssl->method->ssl_accept)
260 SSL_set_accept_state(ssl);
261
61986d32 262 if (!SSL_clear(ssl)) {
69f68237
MC
263 ret = 0;
264 break;
265 }
0f113f3e 266
a146ae55
MC
267 if (next != NULL)
268 ret = BIO_ctrl(next, cmd, num, ptr);
0f113f3e
MC
269 else if (ssl->rbio != NULL)
270 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
271 else
272 ret = 1;
273 break;
274 case BIO_CTRL_INFO:
275 ret = 0;
276 break;
277 case BIO_C_SSL_MODE:
278 if (num) /* client mode */
279 SSL_set_connect_state(ssl);
280 else
281 SSL_set_accept_state(ssl);
282 break;
283 case BIO_C_SET_SSL_RENEGOTIATE_TIMEOUT:
284 ret = bs->renegotiate_timeout;
285 if (num < 60)
286 num = 5;
287 bs->renegotiate_timeout = (unsigned long)num;
288 bs->last_time = (unsigned long)time(NULL);
289 break;
290 case BIO_C_SET_SSL_RENEGOTIATE_BYTES:
291 ret = bs->renegotiate_count;
292 if ((long)num >= 512)
293 bs->renegotiate_count = (unsigned long)num;
294 break;
295 case BIO_C_GET_SSL_NUM_RENEGOTIATES:
296 ret = bs->num_renegotiates;
297 break;
298 case BIO_C_SET_SSL:
299 if (ssl != NULL) {
300 ssl_free(b);
301 if (!ssl_new(b))
302 return 0;
303 }
a146ae55 304 BIO_set_shutdown(b, num);
0f113f3e 305 ssl = (SSL *)ptr;
a146ae55 306 bs->ssl = ssl;
0f113f3e
MC
307 bio = SSL_get_rbio(ssl);
308 if (bio != NULL) {
a146ae55
MC
309 if (next != NULL)
310 BIO_push(bio, next);
311 BIO_set_next(b, bio);
fb46be03 312 BIO_up_ref(bio);
0f113f3e 313 }
a146ae55 314 BIO_set_init(b, 1);
0f113f3e
MC
315 break;
316 case BIO_C_GET_SSL:
317 if (ptr != NULL) {
318 sslp = (SSL **)ptr;
319 *sslp = ssl;
320 } else
321 ret = 0;
322 break;
323 case BIO_CTRL_GET_CLOSE:
a146ae55 324 ret = BIO_get_shutdown(b);
0f113f3e
MC
325 break;
326 case BIO_CTRL_SET_CLOSE:
a146ae55 327 BIO_set_shutdown(b, (int)num);
0f113f3e
MC
328 break;
329 case BIO_CTRL_WPENDING:
330 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
331 break;
332 case BIO_CTRL_PENDING:
333 ret = SSL_pending(ssl);
334 if (ret == 0)
335 ret = BIO_pending(ssl->rbio);
336 break;
337 case BIO_CTRL_FLUSH:
338 BIO_clear_retry_flags(b);
339 ret = BIO_ctrl(ssl->wbio, cmd, num, ptr);
340 BIO_copy_next_retry(b);
341 break;
342 case BIO_CTRL_PUSH:
a146ae55 343 if ((next != NULL) && (next != ssl->rbio)) {
eddef305
MC
344 /*
345 * We are going to pass ownership of next to the SSL object...but
346 * we don't own a reference to pass yet - so up ref
347 */
348 BIO_up_ref(next);
a146ae55 349 SSL_set_bio(ssl, next, next);
0f113f3e
MC
350 }
351 break;
352 case BIO_CTRL_POP:
353 /* Only detach if we are the BIO explicitly being popped */
354 if (b == ptr) {
b46fe860
MC
355 /* This will clear the reference we obtained during push */
356 SSL_set_bio(ssl, NULL, NULL);
0f113f3e
MC
357 }
358 break;
359 case BIO_C_DO_STATE_MACHINE:
360 BIO_clear_retry_flags(b);
361
a146ae55 362 BIO_set_retry_reason(b, 0);
0f113f3e
MC
363 ret = (int)SSL_do_handshake(ssl);
364
365 switch (SSL_get_error(ssl, (int)ret)) {
366 case SSL_ERROR_WANT_READ:
367 BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
368 break;
369 case SSL_ERROR_WANT_WRITE:
370 BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
371 break;
372 case SSL_ERROR_WANT_CONNECT:
373 BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
a146ae55 374 BIO_set_retry_reason(b, BIO_get_retry_reason(next));
0f113f3e 375 break;
f1c412c9
DSH
376 case SSL_ERROR_WANT_X509_LOOKUP:
377 BIO_set_retry_special(b);
a146ae55 378 BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
f1c412c9 379 break;
0f113f3e
MC
380 default:
381 break;
382 }
383 break;
384 case BIO_CTRL_DUP:
385 dbio = (BIO *)ptr;
a146ae55
MC
386 dbs = BIO_get_data(dbio);
387 SSL_free(dbs->ssl);
388 dbs->ssl = SSL_dup(ssl);
dbd5c34f
MC
389 dbs->num_renegotiates = bs->num_renegotiates;
390 dbs->renegotiate_count = bs->renegotiate_count;
391 dbs->byte_count = bs->byte_count;
392 dbs->renegotiate_timeout = bs->renegotiate_timeout;
393 dbs->last_time = bs->last_time;
a146ae55 394 ret = (dbs->ssl != NULL);
0f113f3e
MC
395 break;
396 case BIO_C_GET_FD:
397 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
398 break;
399 case BIO_CTRL_SET_CALLBACK:
400 {
401#if 0 /* FIXME: Should this be used? -- Richard
402 * Levitte */
403 SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
404 ret = -1;
d3442bc7 405#else
0f113f3e 406 ret = 0;
d3442bc7 407#endif
0f113f3e
MC
408 }
409 break;
410 case BIO_CTRL_GET_CALLBACK:
411 {
412 void (**fptr) (const SSL *xssl, int type, int val);
413
414 fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
415 *fptr = SSL_get_info_callback(ssl);
416 }
417 break;
418 default:
419 ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
420 break;
421 }
422 return (ret);
423}
d02b48c6 424
13083215 425static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
0f113f3e
MC
426{
427 SSL *ssl;
428 BIO_SSL *bs;
429 long ret = 1;
430
a146ae55 431 bs = BIO_get_data(b);
0f113f3e
MC
432 ssl = bs->ssl;
433 switch (cmd) {
434 case BIO_CTRL_SET_CALLBACK:
435 {
436 /*
437 * FIXME: setting this via a completely different prototype seems
438 * like a crap idea
439 */
440 SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
441 }
442 break;
443 default:
444 ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
445 break;
446 }
447 return (ret);
448}
d3442bc7 449
0e1c0612 450static int ssl_puts(BIO *bp, const char *str)
0f113f3e
MC
451{
452 int n, ret;
d02b48c6 453
0f113f3e
MC
454 n = strlen(str);
455 ret = BIO_write(bp, str, n);
456 return (ret);
457}
d02b48c6 458
6b691a5c 459BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
0f113f3e 460{
85d686e7 461#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
462 BIO *ret = NULL, *buf = NULL, *ssl = NULL;
463
464 if ((buf = BIO_new(BIO_f_buffer())) == NULL)
465 return (NULL);
466 if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
467 goto err;
468 if ((ret = BIO_push(buf, ssl)) == NULL)
469 goto err;
470 return (ret);
471 err:
ca3a82c3
RS
472 BIO_free(buf);
473 BIO_free(ssl);
85d686e7 474#endif
0f113f3e
MC
475 return (NULL);
476}
58964a49 477
6b691a5c 478BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
0f113f3e 479{
4a1fbd13 480#ifndef OPENSSL_NO_SOCK
0f113f3e
MC
481 BIO *ret = NULL, *con = NULL, *ssl = NULL;
482
483 if ((con = BIO_new(BIO_s_connect())) == NULL)
484 return (NULL);
485 if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
486 goto err;
487 if ((ret = BIO_push(ssl, con)) == NULL)
488 goto err;
489 return (ret);
490 err:
ca3a82c3 491 BIO_free(con);
4a1fbd13 492#endif
0f113f3e
MC
493 return (NULL);
494}
58964a49 495
6b691a5c 496BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
0f113f3e
MC
497{
498 BIO *ret;
499 SSL *ssl;
500
501 if ((ret = BIO_new(BIO_f_ssl())) == NULL)
502 return (NULL);
503 if ((ssl = SSL_new(ctx)) == NULL) {
504 BIO_free(ret);
505 return (NULL);
506 }
507 if (client)
508 SSL_set_connect_state(ssl);
509 else
510 SSL_set_accept_state(ssl);
511
512 BIO_set_ssl(ret, ssl, BIO_CLOSE);
513 return (ret);
514}
d02b48c6 515
6b691a5c 516int BIO_ssl_copy_session_id(BIO *t, BIO *f)
0f113f3e 517{
a146ae55 518 BIO_SSL *tdata, *fdata;
0f113f3e
MC
519 t = BIO_find_type(t, BIO_TYPE_SSL);
520 f = BIO_find_type(f, BIO_TYPE_SSL);
521 if ((t == NULL) || (f == NULL))
a146ae55
MC
522 return 0;
523 tdata = BIO_get_data(t);
524 fdata = BIO_get_data(f);
525 if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
0f113f3e 526 return (0);
a146ae55 527 if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
17dd65e6 528 return 0;
0f113f3e
MC
529 return (1);
530}
d02b48c6 531
6b691a5c 532void BIO_ssl_shutdown(BIO *b)
0f113f3e
MC
533{
534 SSL *s;
535
a146ae55
MC
536 b = BIO_find_type(b, BIO_TYPE_SSL);
537 if (b == NULL)
538 return;
539
540 s = BIO_get_data(b);
541 SSL_shutdown(s);
0f113f3e 542}