BIO_s_connect(): Enable BIO_gets()
[openssl.git] / crypto / bio / bss_conn.c
1 /*
2  * Copyright 1995-2021 The OpenSSL Project Authors. All Rights Reserved.
3  *
4  * Licensed under the Apache License 2.0 (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 <errno.h>
12
13 #include "bio_local.h"
14 #include "internal/ktls.h"
15
16 #ifndef OPENSSL_NO_SOCK
17
18 typedef struct bio_connect_st {
19     int state;
20     int connect_family;
21     char *param_hostname;
22     char *param_service;
23     int connect_mode;
24 # ifndef OPENSSL_NO_KTLS
25     unsigned char record_type;
26 # endif
27
28     BIO_ADDRINFO *addr_first;
29     const BIO_ADDRINFO *addr_iter;
30     /*
31      * int socket; this will be kept in bio->num so that it is compatible
32      * with the bss_sock bio
33      */
34     /*
35      * called when the connection is initially made callback(BIO,state,ret);
36      * The callback should return 'ret'.  state is for compatibility with the
37      * ssl info_callback
38      */
39     BIO_info_cb *info_callback;
40 } BIO_CONNECT;
41
42 static int conn_write(BIO *h, const char *buf, int num);
43 static int conn_read(BIO *h, char *buf, int size);
44 static int conn_puts(BIO *h, const char *str);
45 static int conn_gets(BIO *h, char *buf, int size);
46 static long conn_ctrl(BIO *h, int cmd, long arg1, void *arg2);
47 static int conn_new(BIO *h);
48 static int conn_free(BIO *data);
49 static long conn_callback_ctrl(BIO *h, int cmd, BIO_info_cb *);
50
51 static int conn_state(BIO *b, BIO_CONNECT *c);
52 static void conn_close_socket(BIO *data);
53 BIO_CONNECT *BIO_CONNECT_new(void);
54 void BIO_CONNECT_free(BIO_CONNECT *a);
55
56 #define BIO_CONN_S_BEFORE                1
57 #define BIO_CONN_S_GET_ADDR              2
58 #define BIO_CONN_S_CREATE_SOCKET         3
59 #define BIO_CONN_S_CONNECT               4
60 #define BIO_CONN_S_OK                    5
61 #define BIO_CONN_S_BLOCKED_CONNECT       6
62 #define BIO_CONN_S_CONNECT_ERROR         7
63
64 static const BIO_METHOD methods_connectp = {
65     BIO_TYPE_CONNECT,
66     "socket connect",
67     bwrite_conv,
68     conn_write,
69     bread_conv,
70     conn_read,
71     conn_puts,
72     conn_gets,
73     conn_ctrl,
74     conn_new,
75     conn_free,
76     conn_callback_ctrl,
77 };
78
79 static int conn_state(BIO *b, BIO_CONNECT *c)
80 {
81     int ret = -1, i;
82     BIO_info_cb *cb = NULL;
83
84     if (c->info_callback != NULL)
85         cb = c->info_callback;
86
87     for (;;) {
88         switch (c->state) {
89         case BIO_CONN_S_BEFORE:
90             if (c->param_hostname == NULL && c->param_service == NULL) {
91                 ERR_raise_data(ERR_LIB_BIO,
92                                BIO_R_NO_HOSTNAME_OR_SERVICE_SPECIFIED,
93                                "hostname=%s service=%s",
94                                c->param_hostname, c->param_service);
95                 goto exit_loop;
96             }
97             c->state = BIO_CONN_S_GET_ADDR;
98             break;
99
100         case BIO_CONN_S_GET_ADDR:
101             {
102                 int family = AF_UNSPEC;
103                 switch (c->connect_family) {
104                 case BIO_FAMILY_IPV6:
105                     if (1) { /* This is a trick we use to avoid bit rot.
106                               * at least the "else" part will always be
107                               * compiled.
108                               */
109 #ifdef AF_INET6
110                         family = AF_INET6;
111                     } else {
112 #endif
113                         ERR_raise(ERR_LIB_BIO, BIO_R_UNAVAILABLE_IP_FAMILY);
114                         goto exit_loop;
115                     }
116                     break;
117                 case BIO_FAMILY_IPV4:
118                     family = AF_INET;
119                     break;
120                 case BIO_FAMILY_IPANY:
121                     family = AF_UNSPEC;
122                     break;
123                 default:
124                     ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_IP_FAMILY);
125                     goto exit_loop;
126                 }
127                 if (BIO_lookup(c->param_hostname, c->param_service,
128                                BIO_LOOKUP_CLIENT,
129                                family, SOCK_STREAM, &c->addr_first) == 0)
130                     goto exit_loop;
131             }
132             if (c->addr_first == NULL) {
133                 ERR_raise(ERR_LIB_BIO, BIO_R_LOOKUP_RETURNED_NOTHING);
134                 goto exit_loop;
135             }
136             c->addr_iter = c->addr_first;
137             c->state = BIO_CONN_S_CREATE_SOCKET;
138             break;
139
140         case BIO_CONN_S_CREATE_SOCKET:
141             ret = BIO_socket(BIO_ADDRINFO_family(c->addr_iter),
142                              BIO_ADDRINFO_socktype(c->addr_iter),
143                              BIO_ADDRINFO_protocol(c->addr_iter), 0);
144             if (ret == (int)INVALID_SOCKET) {
145                 ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
146                                "calling socket(%s, %s)",
147                                c->param_hostname, c->param_service);
148                 ERR_raise(ERR_LIB_BIO, BIO_R_UNABLE_TO_CREATE_SOCKET);
149                 goto exit_loop;
150             }
151             b->num = ret;
152             c->state = BIO_CONN_S_CONNECT;
153             break;
154
155         case BIO_CONN_S_CONNECT:
156             BIO_clear_retry_flags(b);
157             ERR_set_mark();
158             ret = BIO_connect(b->num, BIO_ADDRINFO_address(c->addr_iter),
159                               BIO_SOCK_KEEPALIVE | c->connect_mode);
160             b->retry_reason = 0;
161             if (ret == 0) {
162                 if (BIO_sock_should_retry(ret)) {
163                     BIO_set_retry_special(b);
164                     c->state = BIO_CONN_S_BLOCKED_CONNECT;
165                     b->retry_reason = BIO_RR_CONNECT;
166                     ERR_pop_to_mark();
167                 } else if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter))
168                            != NULL) {
169                     /*
170                      * if there are more addresses to try, do that first
171                      */
172                     BIO_closesocket(b->num);
173                     c->state = BIO_CONN_S_CREATE_SOCKET;
174                     ERR_pop_to_mark();
175                     break;
176                 } else {
177                     ERR_clear_last_mark();
178                     ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(),
179                                    "calling connect(%s, %s)",
180                                     c->param_hostname, c->param_service);
181                     c->state = BIO_CONN_S_CONNECT_ERROR;
182                     break;
183                 }
184                 goto exit_loop;
185             } else {
186                 ERR_clear_last_mark();
187                 c->state = BIO_CONN_S_OK;
188             }
189             break;
190
191         case BIO_CONN_S_BLOCKED_CONNECT:
192             i = BIO_sock_error(b->num);
193             if (i != 0) {
194                 BIO_clear_retry_flags(b);
195                 if ((c->addr_iter = BIO_ADDRINFO_next(c->addr_iter)) != NULL) {
196                     /*
197                      * if there are more addresses to try, do that first
198                      */
199                     BIO_closesocket(b->num);
200                     c->state = BIO_CONN_S_CREATE_SOCKET;
201                     break;
202                 }
203                 ERR_raise_data(ERR_LIB_SYS, i,
204                                "calling connect(%s, %s)",
205                                 c->param_hostname, c->param_service);
206                 ERR_raise(ERR_LIB_BIO, BIO_R_NBIO_CONNECT_ERROR);
207                 ret = 0;
208                 goto exit_loop;
209             } else
210                 c->state = BIO_CONN_S_OK;
211             break;
212
213         case BIO_CONN_S_CONNECT_ERROR:
214             ERR_raise(ERR_LIB_BIO, BIO_R_CONNECT_ERROR);
215             ret = 0;
216             goto exit_loop;
217
218         case BIO_CONN_S_OK:
219             ret = 1;
220             goto exit_loop;
221         default:
222             /* abort(); */
223             goto exit_loop;
224         }
225
226         if (cb != NULL) {
227             if ((ret = cb((BIO *)b, c->state, ret)) == 0)
228                 goto end;
229         }
230     }
231
232     /* Loop does not exit */
233  exit_loop:
234     if (cb != NULL)
235         ret = cb((BIO *)b, c->state, ret);
236  end:
237     return ret;
238 }
239
240 BIO_CONNECT *BIO_CONNECT_new(void)
241 {
242     BIO_CONNECT *ret;
243
244     if ((ret = OPENSSL_zalloc(sizeof(*ret))) == NULL) {
245         ERR_raise(ERR_LIB_BIO, ERR_R_MALLOC_FAILURE);
246         return NULL;
247     }
248     ret->state = BIO_CONN_S_BEFORE;
249     ret->connect_family = BIO_FAMILY_IPANY;
250     return ret;
251 }
252
253 void BIO_CONNECT_free(BIO_CONNECT *a)
254 {
255     if (a == NULL)
256         return;
257     OPENSSL_free(a->param_hostname);
258     OPENSSL_free(a->param_service);
259     BIO_ADDRINFO_free(a->addr_first);
260     OPENSSL_free(a);
261 }
262
263 const BIO_METHOD *BIO_s_connect(void)
264 {
265     return &methods_connectp;
266 }
267
268 static int conn_new(BIO *bi)
269 {
270     bi->init = 0;
271     bi->num = (int)INVALID_SOCKET;
272     bi->flags = 0;
273     if ((bi->ptr = (char *)BIO_CONNECT_new()) == NULL)
274         return 0;
275     else
276         return 1;
277 }
278
279 static void conn_close_socket(BIO *bio)
280 {
281     BIO_CONNECT *c;
282
283     c = (BIO_CONNECT *)bio->ptr;
284     if (bio->num != (int)INVALID_SOCKET) {
285         /* Only do a shutdown if things were established */
286         if (c->state == BIO_CONN_S_OK)
287             shutdown(bio->num, 2);
288         BIO_closesocket(bio->num);
289         bio->num = (int)INVALID_SOCKET;
290     }
291 }
292
293 static int conn_free(BIO *a)
294 {
295     BIO_CONNECT *data;
296
297     if (a == NULL)
298         return 0;
299     data = (BIO_CONNECT *)a->ptr;
300
301     if (a->shutdown) {
302         conn_close_socket(a);
303         BIO_CONNECT_free(data);
304         a->ptr = NULL;
305         a->flags = 0;
306         a->init = 0;
307     }
308     return 1;
309 }
310
311 static int conn_read(BIO *b, char *out, int outl)
312 {
313     int ret = 0;
314     BIO_CONNECT *data;
315
316     data = (BIO_CONNECT *)b->ptr;
317     if (data->state != BIO_CONN_S_OK) {
318         ret = conn_state(b, data);
319         if (ret <= 0)
320             return ret;
321     }
322
323     if (out != NULL) {
324         clear_socket_error();
325 # ifndef OPENSSL_NO_KTLS
326         if (BIO_get_ktls_recv(b))
327             ret = ktls_read_record(b->num, out, outl);
328         else
329 # endif
330             ret = readsocket(b->num, out, outl);
331         BIO_clear_retry_flags(b);
332         if (ret <= 0) {
333             if (BIO_sock_should_retry(ret))
334                 BIO_set_retry_read(b);
335             else if (ret == 0)
336                 b->flags |= BIO_FLAGS_IN_EOF;
337         }
338     }
339     return ret;
340 }
341
342 static int conn_write(BIO *b, const char *in, int inl)
343 {
344     int ret;
345     BIO_CONNECT *data;
346
347     data = (BIO_CONNECT *)b->ptr;
348     if (data->state != BIO_CONN_S_OK) {
349         ret = conn_state(b, data);
350         if (ret <= 0)
351             return ret;
352     }
353
354     clear_socket_error();
355 # ifndef OPENSSL_NO_KTLS
356     if (BIO_should_ktls_ctrl_msg_flag(b)) {
357         ret = ktls_send_ctrl_message(b->num, data->record_type, in, inl);
358         if (ret >= 0) {
359             ret = inl;
360             BIO_clear_ktls_ctrl_msg_flag(b);
361         }
362     } else
363 # endif
364         ret = writesocket(b->num, in, inl);
365     BIO_clear_retry_flags(b);
366     if (ret <= 0) {
367         if (BIO_sock_should_retry(ret))
368             BIO_set_retry_write(b);
369     }
370     return ret;
371 }
372
373 static long conn_ctrl(BIO *b, int cmd, long num, void *ptr)
374 {
375     BIO *dbio;
376     int *ip;
377     const char **pptr = NULL;
378     long ret = 1;
379     BIO_CONNECT *data;
380 # ifndef OPENSSL_NO_KTLS
381     ktls_crypto_info_t *crypto_info;
382 # endif
383
384     data = (BIO_CONNECT *)b->ptr;
385
386     switch (cmd) {
387     case BIO_CTRL_RESET:
388         ret = 0;
389         data->state = BIO_CONN_S_BEFORE;
390         conn_close_socket(b);
391         BIO_ADDRINFO_free(data->addr_first);
392         data->addr_first = NULL;
393         b->flags = 0;
394         break;
395     case BIO_C_DO_STATE_MACHINE:
396         /* use this one to start the connection */
397         if (data->state != BIO_CONN_S_OK)
398             ret = (long)conn_state(b, data);
399         else
400             ret = 1;
401         break;
402     case BIO_C_GET_CONNECT:
403         if (ptr != NULL) {
404             pptr = (const char **)ptr;
405             if (num == 0) {
406                 *pptr = data->param_hostname;
407             } else if (num == 1) {
408                 *pptr = data->param_service;
409             } else if (num == 2) {
410                 *pptr = (const char *)BIO_ADDRINFO_address(data->addr_iter);
411             } else if (num == 3) {
412                 switch (BIO_ADDRINFO_family(data->addr_iter)) {
413 # ifdef AF_INET6
414                 case AF_INET6:
415                     ret = BIO_FAMILY_IPV6;
416                     break;
417 # endif
418                 case AF_INET:
419                     ret = BIO_FAMILY_IPV4;
420                     break;
421                 case 0:
422                     ret = data->connect_family;
423                     break;
424                 default:
425                     ret = -1;
426                     break;
427                 }
428             } else {
429                 ret = 0;
430             }
431         } else {
432             ret = 0;
433         }
434         break;
435     case BIO_C_SET_CONNECT:
436         if (ptr != NULL) {
437             b->init = 1;
438             if (num == 0) { /* BIO_set_conn_hostname */
439                 char *hold_service = data->param_service;
440                 /* We affect the hostname regardless.  However, the input
441                  * string might contain a host:service spec, so we must
442                  * parse it, which might or might not affect the service
443                  */
444
445                 OPENSSL_free(data->param_hostname);
446                 data->param_hostname = NULL;
447                 ret = BIO_parse_hostserv(ptr,
448                                          &data->param_hostname,
449                                          &data->param_service,
450                                          BIO_PARSE_PRIO_HOST);
451                 if (hold_service != data->param_service)
452                     OPENSSL_free(hold_service);
453             } else if (num == 1) { /* BIO_set_conn_port */
454                 OPENSSL_free(data->param_service);
455                 if ((data->param_service = OPENSSL_strdup(ptr)) == NULL)
456                     ret = 0;
457             } else if (num == 2) { /* BIO_set_conn_address */
458                 const BIO_ADDR *addr = (const BIO_ADDR *)ptr;
459                 char *host = BIO_ADDR_hostname_string(addr, 1);
460                 char *service = BIO_ADDR_service_string(addr, 1);
461
462                 ret = host != NULL && service != NULL;
463                 if (ret) {
464                     OPENSSL_free(data->param_hostname);
465                     data->param_hostname = host;
466                     OPENSSL_free(data->param_service);
467                     data->param_service = service;
468                     BIO_ADDRINFO_free(data->addr_first);
469                     data->addr_first = NULL;
470                     data->addr_iter = NULL;
471                 } else {
472                     OPENSSL_free(host);
473                     OPENSSL_free(service);
474                 }
475             } else if (num == 3) { /* BIO_set_conn_ip_family */
476                 data->connect_family = *(int *)ptr;
477             } else {
478                 ret = 0;
479             }
480         }
481         break;
482     case BIO_C_SET_NBIO:
483         if (num != 0)
484             data->connect_mode |= BIO_SOCK_NONBLOCK;
485         else
486             data->connect_mode &= ~BIO_SOCK_NONBLOCK;
487         break;
488     case BIO_C_SET_CONNECT_MODE:
489         data->connect_mode = (int)num;
490         break;
491     case BIO_C_GET_FD:
492         if (b->init) {
493             ip = (int *)ptr;
494             if (ip != NULL)
495                 *ip = b->num;
496             ret = b->num;
497         } else
498             ret = -1;
499         break;
500     case BIO_CTRL_GET_CLOSE:
501         ret = b->shutdown;
502         break;
503     case BIO_CTRL_SET_CLOSE:
504         b->shutdown = (int)num;
505         break;
506     case BIO_CTRL_PENDING:
507     case BIO_CTRL_WPENDING:
508         ret = 0;
509         break;
510     case BIO_CTRL_FLUSH:
511         break;
512     case BIO_CTRL_DUP:
513         {
514             dbio = (BIO *)ptr;
515             if (data->param_hostname)
516                 BIO_set_conn_hostname(dbio, data->param_hostname);
517             if (data->param_service)
518                 BIO_set_conn_port(dbio, data->param_service);
519             BIO_set_conn_ip_family(dbio, data->connect_family);
520             BIO_set_conn_mode(dbio, data->connect_mode);
521             /*
522              * FIXME: the cast of the function seems unlikely to be a good
523              * idea
524              */
525             (void)BIO_set_info_callback(dbio, data->info_callback);
526         }
527         break;
528     case BIO_CTRL_SET_CALLBACK:
529         ret = 0; /* use callback ctrl */
530         break;
531     case BIO_CTRL_GET_CALLBACK:
532         {
533             BIO_info_cb **fptr;
534
535             fptr = (BIO_info_cb **)ptr;
536             *fptr = data->info_callback;
537         }
538         break;
539     case BIO_CTRL_EOF:
540         ret = (b->flags & BIO_FLAGS_IN_EOF) != 0;
541         break;
542 # ifndef OPENSSL_NO_KTLS
543     case BIO_CTRL_SET_KTLS:
544         crypto_info = (ktls_crypto_info_t *)ptr;
545         ret = ktls_start(b->num, crypto_info, num);
546         if (ret)
547             BIO_set_ktls_flag(b, num);
548         break;
549     case BIO_CTRL_GET_KTLS_SEND:
550         return BIO_should_ktls_flag(b, 1) != 0;
551     case BIO_CTRL_GET_KTLS_RECV:
552         return BIO_should_ktls_flag(b, 0) != 0;
553     case BIO_CTRL_SET_KTLS_TX_SEND_CTRL_MSG:
554         BIO_set_ktls_ctrl_msg_flag(b);
555         data->record_type = num;
556         ret = 0;
557         break;
558     case BIO_CTRL_CLEAR_KTLS_TX_CTRL_MSG:
559         BIO_clear_ktls_ctrl_msg_flag(b);
560         ret = 0;
561         break;
562 # endif
563     default:
564         ret = 0;
565         break;
566     }
567     return ret;
568 }
569
570 static long conn_callback_ctrl(BIO *b, int cmd, BIO_info_cb *fp)
571 {
572     long ret = 1;
573     BIO_CONNECT *data;
574
575     data = (BIO_CONNECT *)b->ptr;
576
577     switch (cmd) {
578     case BIO_CTRL_SET_CALLBACK:
579         {
580             data->info_callback = fp;
581         }
582         break;
583     default:
584         ret = 0;
585         break;
586     }
587     return ret;
588 }
589
590 static int conn_puts(BIO *bp, const char *str)
591 {
592     int n, ret;
593
594     n = strlen(str);
595     ret = conn_write(bp, str, n);
596     return ret;
597 }
598
599 int conn_gets(BIO *bio, char *buf, int size)
600 {
601     BIO_CONNECT *data;
602     char *ptr = buf;
603     int ret = 0;
604
605     if (buf == NULL) {
606         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
607         return -1;
608     }
609     if (size <= 0) {
610         ERR_raise(ERR_LIB_BIO, BIO_R_INVALID_ARGUMENT);
611         return -1;
612     }
613     *buf = '\0';
614
615     if (bio == NULL || bio->ptr == NULL) {
616         ERR_raise(ERR_LIB_BIO, ERR_R_PASSED_NULL_PARAMETER);
617         return -1;
618     }
619     data = (BIO_CONNECT *)bio->ptr;
620     if (data->state != BIO_CONN_S_OK) {
621         ret = conn_state(bio, data);
622         if (ret <= 0)
623             return ret;
624     }
625
626     clear_socket_error();
627     while (size-- > 1) {
628 # ifndef OPENSSL_NO_KTLS
629         if (BIO_get_ktls_recv(bio))
630             ret = ktls_read_record(bio->num, ptr, 1);
631         else
632 # endif
633             ret = readsocket(bio->num, ptr, 1);
634         BIO_clear_retry_flags(bio);
635         if (ret <= 0) {
636             if (BIO_sock_should_retry(ret))
637                 BIO_set_retry_read(bio);
638             else if (ret == 0)
639                 bio->flags |= BIO_FLAGS_IN_EOF;
640             break;
641         }
642         if (*ptr++ == '\n')
643             break;
644     }
645     *ptr = '\0';
646     return ret > 0 || (bio->flags & BIO_FLAGS_IN_EOF) != 0 ? ptr - buf : ret;
647 }
648
649 BIO *BIO_new_connect(const char *str)
650 {
651     BIO *ret;
652
653     ret = BIO_new(BIO_s_connect());
654     if (ret == NULL)
655         return NULL;
656     if (BIO_set_conn_hostname(ret, str))
657         return ret;
658     BIO_free(ret);
659     return NULL;
660 }
661
662 #endif