Add #defines for magic numbers in API.
[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             /*
331              * We are going to pass ownership of next to the SSL object...but
332              * we don't own a reference to pass yet - so up ref
333              */
334             BIO_up_ref(next);
335             SSL_set_bio(ssl, next, next);
336         }
337         break;
338     case BIO_CTRL_POP:
339         /* Only detach if we are the BIO explicitly being popped */
340         if (b == ptr) {
341             /* This will clear the reference we obtained during push */
342             SSL_set_bio(ssl, NULL, NULL);
343         }
344         break;
345     case BIO_C_DO_STATE_MACHINE:
346         BIO_clear_retry_flags(b);
347
348         BIO_set_retry_reason(b, 0);
349         ret = (int)SSL_do_handshake(ssl);
350
351         switch (SSL_get_error(ssl, (int)ret)) {
352         case SSL_ERROR_WANT_READ:
353             BIO_set_flags(b, BIO_FLAGS_READ | BIO_FLAGS_SHOULD_RETRY);
354             break;
355         case SSL_ERROR_WANT_WRITE:
356             BIO_set_flags(b, BIO_FLAGS_WRITE | BIO_FLAGS_SHOULD_RETRY);
357             break;
358         case SSL_ERROR_WANT_CONNECT:
359             BIO_set_flags(b, BIO_FLAGS_IO_SPECIAL | BIO_FLAGS_SHOULD_RETRY);
360             BIO_set_retry_reason(b, BIO_get_retry_reason(next));
361             break;
362         case SSL_ERROR_WANT_X509_LOOKUP:
363             BIO_set_retry_special(b);
364             BIO_set_retry_reason(b, BIO_RR_SSL_X509_LOOKUP);
365             break;
366         default:
367             break;
368         }
369         break;
370     case BIO_CTRL_DUP:
371         dbio = (BIO *)ptr;
372         dbs = BIO_get_data(dbio);
373         SSL_free(dbs->ssl);
374         dbs->ssl = SSL_dup(ssl);
375         dbs->num_renegotiates = bs->num_renegotiates;
376         dbs->renegotiate_count = bs->renegotiate_count;
377         dbs->byte_count = bs->byte_count;
378         dbs->renegotiate_timeout = bs->renegotiate_timeout;
379         dbs->last_time = bs->last_time;
380         ret = (dbs->ssl != NULL);
381         break;
382     case BIO_C_GET_FD:
383         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
384         break;
385     case BIO_CTRL_SET_CALLBACK:
386         {
387 #if 0                           /* FIXME: Should this be used? -- Richard
388                                  * Levitte */
389             SSLerr(SSL_F_SSL_CTRL, ERR_R_SHOULD_NOT_HAVE_BEEN_CALLED);
390             ret = -1;
391 #else
392             ret = 0;
393 #endif
394         }
395         break;
396     case BIO_CTRL_GET_CALLBACK:
397         {
398             void (**fptr) (const SSL *xssl, int type, int val);
399
400             fptr = (void (**)(const SSL *xssl, int type, int val))ptr;
401             *fptr = SSL_get_info_callback(ssl);
402         }
403         break;
404     default:
405         ret = BIO_ctrl(ssl->rbio, cmd, num, ptr);
406         break;
407     }
408     return (ret);
409 }
410
411 static long ssl_callback_ctrl(BIO *b, int cmd, bio_info_cb *fp)
412 {
413     SSL *ssl;
414     BIO_SSL *bs;
415     long ret = 1;
416
417     bs = BIO_get_data(b);
418     ssl = bs->ssl;
419     switch (cmd) {
420     case BIO_CTRL_SET_CALLBACK:
421         {
422             /*
423              * FIXME: setting this via a completely different prototype seems
424              * like a crap idea
425              */
426             SSL_set_info_callback(ssl, (void (*)(const SSL *, int, int))fp);
427         }
428         break;
429     default:
430         ret = BIO_callback_ctrl(ssl->rbio, cmd, fp);
431         break;
432     }
433     return (ret);
434 }
435
436 static int ssl_puts(BIO *bp, const char *str)
437 {
438     int n, ret;
439
440     n = strlen(str);
441     ret = BIO_write(bp, str, n);
442     return (ret);
443 }
444
445 BIO *BIO_new_buffer_ssl_connect(SSL_CTX *ctx)
446 {
447 #ifndef OPENSSL_NO_SOCK
448     BIO *ret = NULL, *buf = NULL, *ssl = NULL;
449
450     if ((buf = BIO_new(BIO_f_buffer())) == NULL)
451         return (NULL);
452     if ((ssl = BIO_new_ssl_connect(ctx)) == NULL)
453         goto err;
454     if ((ret = BIO_push(buf, ssl)) == NULL)
455         goto err;
456     return (ret);
457  err:
458     BIO_free(buf);
459     BIO_free(ssl);
460 #endif
461     return (NULL);
462 }
463
464 BIO *BIO_new_ssl_connect(SSL_CTX *ctx)
465 {
466 #ifndef OPENSSL_NO_SOCK
467     BIO *ret = NULL, *con = NULL, *ssl = NULL;
468
469     if ((con = BIO_new(BIO_s_connect())) == NULL)
470         return (NULL);
471     if ((ssl = BIO_new_ssl(ctx, 1)) == NULL)
472         goto err;
473     if ((ret = BIO_push(ssl, con)) == NULL)
474         goto err;
475     return (ret);
476  err:
477     BIO_free(con);
478 #endif
479     return (NULL);
480 }
481
482 BIO *BIO_new_ssl(SSL_CTX *ctx, int client)
483 {
484     BIO *ret;
485     SSL *ssl;
486
487     if ((ret = BIO_new(BIO_f_ssl())) == NULL)
488         return (NULL);
489     if ((ssl = SSL_new(ctx)) == NULL) {
490         BIO_free(ret);
491         return (NULL);
492     }
493     if (client)
494         SSL_set_connect_state(ssl);
495     else
496         SSL_set_accept_state(ssl);
497
498     BIO_set_ssl(ret, ssl, BIO_CLOSE);
499     return (ret);
500 }
501
502 int BIO_ssl_copy_session_id(BIO *t, BIO *f)
503 {
504     BIO_SSL *tdata, *fdata;
505     t = BIO_find_type(t, BIO_TYPE_SSL);
506     f = BIO_find_type(f, BIO_TYPE_SSL);
507     if ((t == NULL) || (f == NULL))
508         return 0;
509     tdata = BIO_get_data(t);
510     fdata = BIO_get_data(f);
511     if ((tdata->ssl == NULL) || (fdata->ssl == NULL))
512         return (0);
513     if (!SSL_copy_session_id(tdata->ssl, (fdata->ssl)))
514         return 0;
515     return (1);
516 }
517
518 void BIO_ssl_shutdown(BIO *b)
519 {
520     SSL *s;
521
522     b = BIO_find_type(b, BIO_TYPE_SSL);
523     if (b == NULL)
524         return;
525
526     s = BIO_get_data(b);
527     SSL_shutdown(s);
528 }