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