/src/openssl/crypto/bio/bio_addr.c
Line | Count | Source (jump to first uncovered line) |
1 | | /* |
2 | | * Copyright 2016-2024 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 | | #ifndef _GNU_SOURCE |
11 | | # define _GNU_SOURCE |
12 | | #endif |
13 | | |
14 | | /* |
15 | | * VC configurations may define UNICODE, to indicate to the C RTL that |
16 | | * WCHAR functions are preferred. |
17 | | * This affects functions like gai_strerror(), which is implemented as |
18 | | * an alias macro for gai_strerrorA() (which returns a const char *) or |
19 | | * gai_strerrorW() (which returns a const WCHAR *). This source file |
20 | | * assumes POSIX declarations, so prefer the non-UNICODE definitions. |
21 | | */ |
22 | | #undef UNICODE |
23 | | |
24 | | #include <assert.h> |
25 | | #include <string.h> |
26 | | |
27 | | #include "bio_local.h" |
28 | | #include <openssl/crypto.h> |
29 | | |
30 | | #ifndef OPENSSL_NO_SOCK |
31 | | #include <openssl/err.h> |
32 | | #include <openssl/buffer.h> |
33 | | #include "internal/thread_once.h" |
34 | | |
35 | | CRYPTO_RWLOCK *bio_lookup_lock; |
36 | | static CRYPTO_ONCE bio_lookup_init = CRYPTO_ONCE_STATIC_INIT; |
37 | | |
38 | | /* |
39 | | * Throughout this file and bio_local.h, the existence of the macro |
40 | | * AI_PASSIVE is used to detect the availability of struct addrinfo, |
41 | | * getnameinfo() and getaddrinfo(). If that macro doesn't exist, |
42 | | * we use our own implementation instead, using gethostbyname, |
43 | | * getservbyname and a few other. |
44 | | */ |
45 | | |
46 | | /********************************************************************** |
47 | | * |
48 | | * Address structure |
49 | | * |
50 | | */ |
51 | | |
52 | | BIO_ADDR *BIO_ADDR_new(void) |
53 | 0 | { |
54 | 0 | BIO_ADDR *ret = OPENSSL_zalloc(sizeof(*ret)); |
55 | |
|
56 | 0 | if (ret == NULL) |
57 | 0 | return NULL; |
58 | | |
59 | 0 | ret->sa.sa_family = AF_UNSPEC; |
60 | 0 | return ret; |
61 | 0 | } |
62 | | |
63 | | void BIO_ADDR_free(BIO_ADDR *ap) |
64 | 0 | { |
65 | 0 | OPENSSL_free(ap); |
66 | 0 | } |
67 | | |
68 | | int BIO_ADDR_copy(BIO_ADDR *dst, const BIO_ADDR *src) |
69 | 0 | { |
70 | 0 | if (dst == NULL || src == NULL) |
71 | 0 | return 0; |
72 | | |
73 | 0 | if (src->sa.sa_family == AF_UNSPEC) { |
74 | 0 | BIO_ADDR_clear(dst); |
75 | 0 | return 1; |
76 | 0 | } |
77 | | |
78 | 0 | return BIO_ADDR_make(dst, &src->sa); |
79 | 0 | } |
80 | | |
81 | | BIO_ADDR *BIO_ADDR_dup(const BIO_ADDR *ap) |
82 | 0 | { |
83 | 0 | BIO_ADDR *ret = NULL; |
84 | |
|
85 | 0 | if (ap != NULL) { |
86 | 0 | ret = BIO_ADDR_new(); |
87 | 0 | if (ret != NULL && !BIO_ADDR_copy(ret, ap)) { |
88 | 0 | BIO_ADDR_free(ret); |
89 | 0 | ret = NULL; |
90 | 0 | } |
91 | 0 | } |
92 | 0 | return ret; |
93 | 0 | } |
94 | | |
95 | | void BIO_ADDR_clear(BIO_ADDR *ap) |
96 | 0 | { |
97 | 0 | memset(ap, 0, sizeof(*ap)); |
98 | 0 | ap->sa.sa_family = AF_UNSPEC; |
99 | 0 | } |
100 | | |
101 | | /* |
102 | | * BIO_ADDR_make - non-public routine to fill a BIO_ADDR with the contents |
103 | | * of a struct sockaddr. |
104 | | */ |
105 | | int BIO_ADDR_make(BIO_ADDR *ap, const struct sockaddr *sa) |
106 | 0 | { |
107 | 0 | if (sa->sa_family == AF_INET) { |
108 | 0 | memcpy(&(ap->s_in), sa, sizeof(struct sockaddr_in)); |
109 | 0 | return 1; |
110 | 0 | } |
111 | 0 | #if OPENSSL_USE_IPV6 |
112 | 0 | if (sa->sa_family == AF_INET6) { |
113 | 0 | memcpy(&(ap->s_in6), sa, sizeof(struct sockaddr_in6)); |
114 | 0 | return 1; |
115 | 0 | } |
116 | 0 | #endif |
117 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
118 | 0 | if (sa->sa_family == AF_UNIX) { |
119 | 0 | memcpy(&(ap->s_un), sa, sizeof(struct sockaddr_un)); |
120 | 0 | return 1; |
121 | 0 | } |
122 | 0 | #endif |
123 | | |
124 | 0 | return 0; |
125 | 0 | } |
126 | | |
127 | | int BIO_ADDR_rawmake(BIO_ADDR *ap, int family, |
128 | | const void *where, size_t wherelen, |
129 | | unsigned short port) |
130 | 0 | { |
131 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
132 | 0 | if (family == AF_UNIX) { |
133 | 0 | if (wherelen + 1 > sizeof(ap->s_un.sun_path)) |
134 | 0 | return 0; |
135 | 0 | memset(&ap->s_un, 0, sizeof(ap->s_un)); |
136 | 0 | ap->s_un.sun_family = family; |
137 | 0 | strncpy(ap->s_un.sun_path, where, sizeof(ap->s_un.sun_path) - 1); |
138 | 0 | return 1; |
139 | 0 | } |
140 | 0 | #endif |
141 | 0 | if (family == AF_INET) { |
142 | 0 | if (wherelen != sizeof(struct in_addr)) |
143 | 0 | return 0; |
144 | 0 | memset(&ap->s_in, 0, sizeof(ap->s_in)); |
145 | 0 | ap->s_in.sin_family = family; |
146 | 0 | ap->s_in.sin_port = port; |
147 | 0 | ap->s_in.sin_addr = *(struct in_addr *)where; |
148 | 0 | return 1; |
149 | 0 | } |
150 | 0 | #if OPENSSL_USE_IPV6 |
151 | 0 | if (family == AF_INET6) { |
152 | 0 | if (wherelen != sizeof(struct in6_addr)) |
153 | 0 | return 0; |
154 | 0 | memset(&ap->s_in6, 0, sizeof(ap->s_in6)); |
155 | 0 | ap->s_in6.sin6_family = family; |
156 | 0 | ap->s_in6.sin6_port = port; |
157 | 0 | ap->s_in6.sin6_addr = *(struct in6_addr *)where; |
158 | 0 | return 1; |
159 | 0 | } |
160 | 0 | #endif |
161 | | |
162 | 0 | return 0; |
163 | 0 | } |
164 | | |
165 | | int BIO_ADDR_family(const BIO_ADDR *ap) |
166 | 0 | { |
167 | 0 | return ap->sa.sa_family; |
168 | 0 | } |
169 | | |
170 | | int BIO_ADDR_rawaddress(const BIO_ADDR *ap, void *p, size_t *l) |
171 | 0 | { |
172 | 0 | size_t len = 0; |
173 | 0 | const void *addrptr = NULL; |
174 | |
|
175 | 0 | if (ap->sa.sa_family == AF_INET) { |
176 | 0 | len = sizeof(ap->s_in.sin_addr); |
177 | 0 | addrptr = &ap->s_in.sin_addr; |
178 | 0 | } |
179 | 0 | #if OPENSSL_USE_IPV6 |
180 | 0 | else if (ap->sa.sa_family == AF_INET6) { |
181 | 0 | len = sizeof(ap->s_in6.sin6_addr); |
182 | 0 | addrptr = &ap->s_in6.sin6_addr; |
183 | 0 | } |
184 | 0 | #endif |
185 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
186 | 0 | else if (ap->sa.sa_family == AF_UNIX) { |
187 | 0 | len = strlen(ap->s_un.sun_path); |
188 | 0 | addrptr = &ap->s_un.sun_path; |
189 | 0 | } |
190 | 0 | #endif |
191 | |
|
192 | 0 | if (addrptr == NULL) |
193 | 0 | return 0; |
194 | | |
195 | 0 | if (p != NULL) { |
196 | 0 | memcpy(p, addrptr, len); |
197 | 0 | } |
198 | 0 | if (l != NULL) |
199 | 0 | *l = len; |
200 | |
|
201 | 0 | return 1; |
202 | 0 | } |
203 | | |
204 | | unsigned short BIO_ADDR_rawport(const BIO_ADDR *ap) |
205 | 0 | { |
206 | 0 | if (ap->sa.sa_family == AF_INET) |
207 | 0 | return ap->s_in.sin_port; |
208 | 0 | #if OPENSSL_USE_IPV6 |
209 | 0 | if (ap->sa.sa_family == AF_INET6) |
210 | 0 | return ap->s_in6.sin6_port; |
211 | 0 | #endif |
212 | 0 | return 0; |
213 | 0 | } |
214 | | |
215 | | /*- |
216 | | * addr_strings - helper function to get host and service names |
217 | | * @ap: the BIO_ADDR that has the input info |
218 | | * @numeric: 0 if actual names should be returned, 1 if the numeric |
219 | | * representation should be returned. |
220 | | * @hostname: a pointer to a pointer to a memory area to store the |
221 | | * hostname or numeric representation. Unused if NULL. |
222 | | * @service: a pointer to a pointer to a memory area to store the |
223 | | * service name or numeric representation. Unused if NULL. |
224 | | * |
225 | | * The return value is 0 on failure, with the error code in the error |
226 | | * stack, and 1 on success. |
227 | | */ |
228 | | static int addr_strings(const BIO_ADDR *ap, int numeric, |
229 | | char **hostname, char **service) |
230 | 0 | { |
231 | 0 | if (BIO_sock_init() != 1) |
232 | 0 | return 0; |
233 | | |
234 | 0 | if (1) { |
235 | 0 | #ifdef AI_PASSIVE |
236 | 0 | int ret = 0; |
237 | 0 | char host[NI_MAXHOST] = "", serv[NI_MAXSERV] = ""; |
238 | 0 | int flags = 0; |
239 | |
|
240 | 0 | if (numeric) |
241 | 0 | flags |= NI_NUMERICHOST | NI_NUMERICSERV; |
242 | |
|
243 | 0 | if ((ret = getnameinfo(BIO_ADDR_sockaddr(ap), |
244 | 0 | BIO_ADDR_sockaddr_size(ap), |
245 | 0 | host, sizeof(host), serv, sizeof(serv), |
246 | 0 | flags)) != 0) { |
247 | 0 | # ifdef EAI_SYSTEM |
248 | 0 | if (ret == EAI_SYSTEM) { |
249 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
250 | 0 | "calling getnameinfo()"); |
251 | 0 | } else |
252 | 0 | # endif |
253 | 0 | { |
254 | 0 | ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, gai_strerror(ret)); |
255 | 0 | } |
256 | 0 | return 0; |
257 | 0 | } |
258 | | |
259 | | /* VMS getnameinfo() has a bug, it doesn't fill in serv, which |
260 | | * leaves it with whatever garbage that happens to be there. |
261 | | * However, we initialise serv with the empty string (serv[0] |
262 | | * is therefore NUL), so it gets real easy to detect when things |
263 | | * didn't go the way one might expect. |
264 | | */ |
265 | 0 | if (serv[0] == '\0') { |
266 | 0 | BIO_snprintf(serv, sizeof(serv), "%d", |
267 | 0 | ntohs(BIO_ADDR_rawport(ap))); |
268 | 0 | } |
269 | |
|
270 | 0 | if (hostname != NULL) |
271 | 0 | *hostname = OPENSSL_strdup(host); |
272 | 0 | if (service != NULL) |
273 | 0 | *service = OPENSSL_strdup(serv); |
274 | 0 | } else { |
275 | 0 | #endif |
276 | 0 | if (hostname != NULL) |
277 | 0 | *hostname = OPENSSL_strdup(inet_ntoa(ap->s_in.sin_addr)); |
278 | 0 | if (service != NULL) { |
279 | 0 | char serv[6]; /* port is 16 bits => max 5 decimal digits */ |
280 | 0 | BIO_snprintf(serv, sizeof(serv), "%d", ntohs(ap->s_in.sin_port)); |
281 | 0 | *service = OPENSSL_strdup(serv); |
282 | 0 | } |
283 | 0 | } |
284 | | |
285 | 0 | if ((hostname != NULL && *hostname == NULL) |
286 | 0 | || (service != NULL && *service == NULL)) { |
287 | 0 | if (hostname != NULL) { |
288 | 0 | OPENSSL_free(*hostname); |
289 | 0 | *hostname = NULL; |
290 | 0 | } |
291 | 0 | if (service != NULL) { |
292 | 0 | OPENSSL_free(*service); |
293 | 0 | *service = NULL; |
294 | 0 | } |
295 | 0 | return 0; |
296 | 0 | } |
297 | | |
298 | 0 | return 1; |
299 | 0 | } |
300 | | |
301 | | char *BIO_ADDR_hostname_string(const BIO_ADDR *ap, int numeric) |
302 | 0 | { |
303 | 0 | char *hostname = NULL; |
304 | |
|
305 | 0 | if (addr_strings(ap, numeric, &hostname, NULL)) |
306 | 0 | return hostname; |
307 | | |
308 | 0 | return NULL; |
309 | 0 | } |
310 | | |
311 | | char *BIO_ADDR_service_string(const BIO_ADDR *ap, int numeric) |
312 | 0 | { |
313 | 0 | char *service = NULL; |
314 | |
|
315 | 0 | if (addr_strings(ap, numeric, NULL, &service)) |
316 | 0 | return service; |
317 | | |
318 | 0 | return NULL; |
319 | 0 | } |
320 | | |
321 | | char *BIO_ADDR_path_string(const BIO_ADDR *ap) |
322 | 0 | { |
323 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
324 | 0 | if (ap->sa.sa_family == AF_UNIX) |
325 | 0 | return OPENSSL_strdup(ap->s_un.sun_path); |
326 | 0 | #endif |
327 | 0 | return NULL; |
328 | 0 | } |
329 | | |
330 | | /* |
331 | | * BIO_ADDR_sockaddr - non-public routine to return the struct sockaddr |
332 | | * for a given BIO_ADDR. In reality, this is simply a type safe cast. |
333 | | * The returned struct sockaddr is const, so it can't be tampered with. |
334 | | */ |
335 | | const struct sockaddr *BIO_ADDR_sockaddr(const BIO_ADDR *ap) |
336 | 0 | { |
337 | 0 | return &(ap->sa); |
338 | 0 | } |
339 | | |
340 | | /* |
341 | | * BIO_ADDR_sockaddr_noconst - non-public function that does the same |
342 | | * as BIO_ADDR_sockaddr, but returns a non-const. USE WITH CARE, as |
343 | | * it allows you to tamper with the data (and thereby the contents |
344 | | * of the input BIO_ADDR). |
345 | | */ |
346 | | struct sockaddr *BIO_ADDR_sockaddr_noconst(BIO_ADDR *ap) |
347 | 0 | { |
348 | 0 | return &(ap->sa); |
349 | 0 | } |
350 | | |
351 | | /* |
352 | | * BIO_ADDR_sockaddr_size - non-public function that returns the size |
353 | | * of the struct sockaddr the BIO_ADDR is using. If the protocol family |
354 | | * isn't set or is something other than AF_INET, AF_INET6 or AF_UNIX, |
355 | | * the size of the BIO_ADDR type is returned. |
356 | | */ |
357 | | socklen_t BIO_ADDR_sockaddr_size(const BIO_ADDR *ap) |
358 | 0 | { |
359 | 0 | if (ap->sa.sa_family == AF_INET) |
360 | 0 | return sizeof(ap->s_in); |
361 | 0 | #if OPENSSL_USE_IPV6 |
362 | 0 | if (ap->sa.sa_family == AF_INET6) |
363 | 0 | return sizeof(ap->s_in6); |
364 | 0 | #endif |
365 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
366 | 0 | if (ap->sa.sa_family == AF_UNIX) |
367 | 0 | return sizeof(ap->s_un); |
368 | 0 | #endif |
369 | 0 | return sizeof(*ap); |
370 | 0 | } |
371 | | |
372 | | /********************************************************************** |
373 | | * |
374 | | * Address info database |
375 | | * |
376 | | */ |
377 | | |
378 | | const BIO_ADDRINFO *BIO_ADDRINFO_next(const BIO_ADDRINFO *bai) |
379 | 0 | { |
380 | 0 | if (bai != NULL) |
381 | 0 | return bai->bai_next; |
382 | 0 | return NULL; |
383 | 0 | } |
384 | | |
385 | | int BIO_ADDRINFO_family(const BIO_ADDRINFO *bai) |
386 | 0 | { |
387 | 0 | if (bai != NULL) |
388 | 0 | return bai->bai_family; |
389 | 0 | return 0; |
390 | 0 | } |
391 | | |
392 | | int BIO_ADDRINFO_socktype(const BIO_ADDRINFO *bai) |
393 | 0 | { |
394 | 0 | if (bai != NULL) |
395 | 0 | return bai->bai_socktype; |
396 | 0 | return 0; |
397 | 0 | } |
398 | | |
399 | | int BIO_ADDRINFO_protocol(const BIO_ADDRINFO *bai) |
400 | 0 | { |
401 | 0 | if (bai != NULL) { |
402 | 0 | if (bai->bai_protocol != 0) |
403 | 0 | return bai->bai_protocol; |
404 | | |
405 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
406 | 0 | if (bai->bai_family == AF_UNIX) |
407 | 0 | return 0; |
408 | 0 | #endif |
409 | | |
410 | 0 | switch (bai->bai_socktype) { |
411 | 0 | case SOCK_STREAM: |
412 | 0 | return IPPROTO_TCP; |
413 | 0 | case SOCK_DGRAM: |
414 | 0 | return IPPROTO_UDP; |
415 | 0 | default: |
416 | 0 | break; |
417 | 0 | } |
418 | 0 | } |
419 | 0 | return 0; |
420 | 0 | } |
421 | | |
422 | | /* |
423 | | * BIO_ADDRINFO_sockaddr_size - non-public function that returns the size |
424 | | * of the struct sockaddr inside the BIO_ADDRINFO. |
425 | | */ |
426 | | socklen_t BIO_ADDRINFO_sockaddr_size(const BIO_ADDRINFO *bai) |
427 | 0 | { |
428 | 0 | if (bai != NULL) |
429 | 0 | return bai->bai_addrlen; |
430 | 0 | return 0; |
431 | 0 | } |
432 | | |
433 | | /* |
434 | | * BIO_ADDRINFO_sockaddr - non-public function that returns bai_addr |
435 | | * as the struct sockaddr it is. |
436 | | */ |
437 | | const struct sockaddr *BIO_ADDRINFO_sockaddr(const BIO_ADDRINFO *bai) |
438 | 0 | { |
439 | 0 | if (bai != NULL) |
440 | 0 | return bai->bai_addr; |
441 | 0 | return NULL; |
442 | 0 | } |
443 | | |
444 | | const BIO_ADDR *BIO_ADDRINFO_address(const BIO_ADDRINFO *bai) |
445 | 0 | { |
446 | 0 | if (bai != NULL) |
447 | 0 | return (BIO_ADDR *)bai->bai_addr; |
448 | 0 | return NULL; |
449 | 0 | } |
450 | | |
451 | | void BIO_ADDRINFO_free(BIO_ADDRINFO *bai) |
452 | 0 | { |
453 | 0 | if (bai == NULL) |
454 | 0 | return; |
455 | | |
456 | 0 | #ifdef AI_PASSIVE |
457 | 0 | # ifndef OPENSSL_NO_UNIX_SOCK |
458 | 0 | # define _cond bai->bai_family != AF_UNIX |
459 | | # else |
460 | | # define _cond 1 |
461 | | # endif |
462 | 0 | if (_cond) { |
463 | 0 | freeaddrinfo(bai); |
464 | 0 | return; |
465 | 0 | } |
466 | 0 | #endif |
467 | | |
468 | | /* Free manually when we know that addrinfo_wrap() was used. |
469 | | * See further comment above addrinfo_wrap() |
470 | | */ |
471 | 0 | while (bai != NULL) { |
472 | 0 | BIO_ADDRINFO *next = bai->bai_next; |
473 | 0 | OPENSSL_free(bai->bai_addr); |
474 | 0 | OPENSSL_free(bai); |
475 | 0 | bai = next; |
476 | 0 | } |
477 | 0 | } |
478 | | |
479 | | /********************************************************************** |
480 | | * |
481 | | * Service functions |
482 | | * |
483 | | */ |
484 | | |
485 | | /*- |
486 | | * The specs in hostserv can take these forms: |
487 | | * |
488 | | * host:service => *host = "host", *service = "service" |
489 | | * host:* => *host = "host", *service = NULL |
490 | | * host: => *host = "host", *service = NULL |
491 | | * :service => *host = NULL, *service = "service" |
492 | | * *:service => *host = NULL, *service = "service" |
493 | | * |
494 | | * in case no : is present in the string, the result depends on |
495 | | * hostserv_prio, as follows: |
496 | | * |
497 | | * when hostserv_prio == BIO_PARSE_PRIO_HOST |
498 | | * host => *host = "host", *service untouched |
499 | | * |
500 | | * when hostserv_prio == BIO_PARSE_PRIO_SERV |
501 | | * service => *host untouched, *service = "service" |
502 | | * |
503 | | */ |
504 | | int BIO_parse_hostserv(const char *hostserv, char **host, char **service, |
505 | | enum BIO_hostserv_priorities hostserv_prio) |
506 | 0 | { |
507 | 0 | const char *h = NULL; size_t hl = 0; |
508 | 0 | const char *p = NULL; size_t pl = 0; |
509 | |
|
510 | 0 | if (*hostserv == '[') { |
511 | 0 | if ((p = strchr(hostserv, ']')) == NULL) |
512 | 0 | goto spec_err; |
513 | 0 | h = hostserv + 1; |
514 | 0 | hl = p - h; |
515 | 0 | p++; |
516 | 0 | if (*p == '\0') |
517 | 0 | p = NULL; |
518 | 0 | else if (*p != ':') |
519 | 0 | goto spec_err; |
520 | 0 | else { |
521 | 0 | p++; |
522 | 0 | pl = strlen(p); |
523 | 0 | } |
524 | 0 | } else { |
525 | 0 | const char *p2 = strrchr(hostserv, ':'); |
526 | 0 | p = strchr(hostserv, ':'); |
527 | | |
528 | | /*- |
529 | | * Check for more than one colon. There are three possible |
530 | | * interpretations: |
531 | | * 1. IPv6 address with port number, last colon being separator. |
532 | | * 2. IPv6 address only. |
533 | | * 3. IPv6 address only if hostserv_prio == BIO_PARSE_PRIO_HOST, |
534 | | * IPv6 address and port number if hostserv_prio == BIO_PARSE_PRIO_SERV |
535 | | * Because of this ambiguity, we currently choose to make it an |
536 | | * error. |
537 | | */ |
538 | 0 | if (p != p2) |
539 | 0 | goto amb_err; |
540 | | |
541 | 0 | if (p != NULL) { |
542 | 0 | h = hostserv; |
543 | 0 | hl = p - h; |
544 | 0 | p++; |
545 | 0 | pl = strlen(p); |
546 | 0 | } else if (hostserv_prio == BIO_PARSE_PRIO_HOST) { |
547 | 0 | h = hostserv; |
548 | 0 | hl = strlen(h); |
549 | 0 | } else { |
550 | 0 | p = hostserv; |
551 | 0 | pl = strlen(p); |
552 | 0 | } |
553 | 0 | } |
554 | | |
555 | 0 | if (p != NULL && strchr(p, ':')) |
556 | 0 | goto spec_err; |
557 | | |
558 | 0 | if (h != NULL && host != NULL) { |
559 | 0 | if (hl == 0 |
560 | 0 | || (hl == 1 && h[0] == '*')) { |
561 | 0 | *host = NULL; |
562 | 0 | } else { |
563 | 0 | *host = OPENSSL_strndup(h, hl); |
564 | 0 | if (*host == NULL) |
565 | 0 | return 0; |
566 | 0 | } |
567 | 0 | } |
568 | 0 | if (p != NULL && service != NULL) { |
569 | 0 | if (pl == 0 |
570 | 0 | || (pl == 1 && p[0] == '*')) { |
571 | 0 | *service = NULL; |
572 | 0 | } else { |
573 | 0 | *service = OPENSSL_strndup(p, pl); |
574 | 0 | if (*service == NULL) |
575 | 0 | return 0; |
576 | 0 | } |
577 | 0 | } |
578 | | |
579 | 0 | return 1; |
580 | 0 | amb_err: |
581 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_AMBIGUOUS_HOST_OR_SERVICE); |
582 | 0 | return 0; |
583 | 0 | spec_err: |
584 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
585 | 0 | return 0; |
586 | 0 | } |
587 | | |
588 | | /* addrinfo_wrap is used to build our own addrinfo "chain". |
589 | | * (it has only one entry, so calling it a chain may be a stretch) |
590 | | * It should ONLY be called when getaddrinfo() and friends |
591 | | * aren't available, OR when dealing with a non IP protocol |
592 | | * family, such as AF_UNIX |
593 | | * |
594 | | * the return value is 1 on success, or 0 on failure, which |
595 | | * only happens if a memory allocation error occurred. |
596 | | */ |
597 | | static int addrinfo_wrap(int family, int socktype, |
598 | | const void *where, size_t wherelen, |
599 | | unsigned short port, |
600 | | BIO_ADDRINFO **bai) |
601 | 0 | { |
602 | 0 | if ((*bai = OPENSSL_zalloc(sizeof(**bai))) == NULL) |
603 | 0 | return 0; |
604 | | |
605 | 0 | (*bai)->bai_family = family; |
606 | 0 | (*bai)->bai_socktype = socktype; |
607 | 0 | if (socktype == SOCK_STREAM) |
608 | 0 | (*bai)->bai_protocol = IPPROTO_TCP; |
609 | 0 | if (socktype == SOCK_DGRAM) |
610 | 0 | (*bai)->bai_protocol = IPPROTO_UDP; |
611 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
612 | 0 | if (family == AF_UNIX) |
613 | 0 | (*bai)->bai_protocol = 0; |
614 | 0 | #endif |
615 | 0 | { |
616 | | /* Magic: We know that BIO_ADDR_sockaddr_noconst is really |
617 | | just an advanced cast of BIO_ADDR* to struct sockaddr * |
618 | | by the power of union, so while it may seem that we're |
619 | | creating a memory leak here, we are not. It will be |
620 | | all right. */ |
621 | 0 | BIO_ADDR *addr = BIO_ADDR_new(); |
622 | 0 | if (addr != NULL) { |
623 | 0 | BIO_ADDR_rawmake(addr, family, where, wherelen, port); |
624 | 0 | (*bai)->bai_addr = BIO_ADDR_sockaddr_noconst(addr); |
625 | 0 | } |
626 | 0 | } |
627 | 0 | (*bai)->bai_next = NULL; |
628 | 0 | if ((*bai)->bai_addr == NULL) { |
629 | 0 | BIO_ADDRINFO_free(*bai); |
630 | 0 | *bai = NULL; |
631 | 0 | return 0; |
632 | 0 | } |
633 | 0 | return 1; |
634 | 0 | } |
635 | | |
636 | | DEFINE_RUN_ONCE_STATIC(do_bio_lookup_init) |
637 | 0 | { |
638 | 0 | bio_lookup_lock = CRYPTO_THREAD_lock_new(); |
639 | 0 | return bio_lookup_lock != NULL; |
640 | 0 | } |
641 | | |
642 | | int BIO_lookup(const char *host, const char *service, |
643 | | enum BIO_lookup_type lookup_type, |
644 | | int family, int socktype, BIO_ADDRINFO **res) |
645 | 0 | { |
646 | 0 | return BIO_lookup_ex(host, service, lookup_type, family, socktype, 0, res); |
647 | 0 | } |
648 | | |
649 | | /*- |
650 | | * BIO_lookup_ex - look up the host and service you want to connect to. |
651 | | * @host: the host (or node, in case family == AF_UNIX) you want to connect to. |
652 | | * @service: the service you want to connect to. |
653 | | * @lookup_type: declare intent with the result, client or server. |
654 | | * @family: the address family you want to use. Use AF_UNSPEC for any, or |
655 | | * AF_INET, AF_INET6 or AF_UNIX. |
656 | | * @socktype: The socket type you want to use. Can be SOCK_STREAM, SOCK_DGRAM |
657 | | * or 0 for all. |
658 | | * @protocol: The protocol to use, e.g. IPPROTO_TCP or IPPROTO_UDP or 0 for all. |
659 | | * Note that some platforms may not return IPPROTO_SCTP without |
660 | | * explicitly requesting it (i.e. IPPROTO_SCTP may not be returned |
661 | | * with 0 for the protocol) |
662 | | * @res: Storage place for the resulting list of returned addresses |
663 | | * |
664 | | * This will do a lookup of the host and service that you want to connect to. |
665 | | * It returns a linked list of different addresses you can try to connect to. |
666 | | * |
667 | | * When no longer needed you should call BIO_ADDRINFO_free() to free the result. |
668 | | * |
669 | | * The return value is 1 on success or 0 in case of error. |
670 | | */ |
671 | | int BIO_lookup_ex(const char *host, const char *service, int lookup_type, |
672 | | int family, int socktype, int protocol, BIO_ADDRINFO **res) |
673 | 0 | { |
674 | 0 | int ret = 0; /* Assume failure */ |
675 | |
|
676 | 0 | switch (family) { |
677 | 0 | case AF_INET: |
678 | 0 | #if OPENSSL_USE_IPV6 |
679 | 0 | case AF_INET6: |
680 | 0 | #endif |
681 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
682 | 0 | case AF_UNIX: |
683 | 0 | #endif |
684 | 0 | #ifdef AF_UNSPEC |
685 | 0 | case AF_UNSPEC: |
686 | 0 | #endif |
687 | 0 | break; |
688 | 0 | default: |
689 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_UNSUPPORTED_PROTOCOL_FAMILY); |
690 | 0 | return 0; |
691 | 0 | } |
692 | | |
693 | 0 | #ifndef OPENSSL_NO_UNIX_SOCK |
694 | 0 | if (family == AF_UNIX) { |
695 | 0 | if (addrinfo_wrap(family, socktype, host, strlen(host), 0, res)) |
696 | 0 | return 1; |
697 | 0 | else |
698 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); |
699 | 0 | return 0; |
700 | 0 | } |
701 | 0 | #endif |
702 | | |
703 | 0 | if (BIO_sock_init() != 1) |
704 | 0 | return 0; |
705 | | |
706 | 0 | if (1) { |
707 | 0 | #ifdef AI_PASSIVE |
708 | 0 | int gai_ret = 0, old_ret = 0; |
709 | 0 | struct addrinfo hints; |
710 | |
|
711 | 0 | memset(&hints, 0, sizeof(hints)); |
712 | |
|
713 | 0 | hints.ai_family = family; |
714 | 0 | hints.ai_socktype = socktype; |
715 | 0 | hints.ai_protocol = protocol; |
716 | 0 | # ifdef AI_ADDRCONFIG |
717 | 0 | # ifdef AF_UNSPEC |
718 | 0 | if (host != NULL && family == AF_UNSPEC) |
719 | 0 | # endif |
720 | 0 | hints.ai_flags |= AI_ADDRCONFIG; |
721 | 0 | # endif |
722 | |
|
723 | 0 | if (lookup_type == BIO_LOOKUP_SERVER) |
724 | 0 | hints.ai_flags |= AI_PASSIVE; |
725 | | |
726 | | /* Note that |res| SHOULD be a 'struct addrinfo **' thanks to |
727 | | * macro magic in bio_local.h |
728 | | */ |
729 | 0 | # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) |
730 | 0 | retry: |
731 | 0 | # endif |
732 | 0 | switch ((gai_ret = getaddrinfo(host, service, &hints, res))) { |
733 | 0 | # ifdef EAI_SYSTEM |
734 | 0 | case EAI_SYSTEM: |
735 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
736 | 0 | "calling getaddrinfo()"); |
737 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_SYS_LIB); |
738 | 0 | break; |
739 | 0 | # endif |
740 | 0 | # ifdef EAI_MEMORY |
741 | 0 | case EAI_MEMORY: |
742 | 0 | ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, |
743 | 0 | gai_strerror(old_ret ? old_ret : gai_ret)); |
744 | 0 | break; |
745 | 0 | # endif |
746 | 0 | case 0: |
747 | 0 | ret = 1; /* Success */ |
748 | 0 | break; |
749 | 0 | default: |
750 | 0 | # if defined(AI_ADDRCONFIG) && defined(AI_NUMERICHOST) |
751 | 0 | if (hints.ai_flags & AI_ADDRCONFIG) { |
752 | 0 | hints.ai_flags &= ~AI_ADDRCONFIG; |
753 | 0 | hints.ai_flags |= AI_NUMERICHOST; |
754 | 0 | old_ret = gai_ret; |
755 | 0 | goto retry; |
756 | 0 | } |
757 | 0 | # endif |
758 | 0 | ERR_raise_data(ERR_LIB_BIO, ERR_R_SYS_LIB, |
759 | 0 | gai_strerror(old_ret ? old_ret : gai_ret)); |
760 | 0 | break; |
761 | 0 | } |
762 | 0 | } else { |
763 | 0 | #endif |
764 | 0 | const struct hostent *he; |
765 | | /* |
766 | | * Because struct hostent is defined for 32-bit pointers only with |
767 | | * VMS C, we need to make sure that '&he_fallback_address' and |
768 | | * '&he_fallback_addresses' are 32-bit pointers |
769 | | */ |
770 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
771 | | # pragma pointer_size save |
772 | | # pragma pointer_size 32 |
773 | | #endif |
774 | | /* Windows doesn't seem to have in_addr_t */ |
775 | | #if defined(OPENSSL_SYS_WINDOWS) || defined(OPENSSL_SYS_MSDOS) |
776 | | static uint32_t he_fallback_address; |
777 | | static const char *he_fallback_addresses[] = { |
778 | | (char *)&he_fallback_address, NULL |
779 | | }; |
780 | | #else |
781 | 0 | static in_addr_t he_fallback_address; |
782 | 0 | static const char *he_fallback_addresses[] = { |
783 | 0 | (char *)&he_fallback_address, NULL |
784 | 0 | }; |
785 | 0 | #endif |
786 | 0 | static const struct hostent he_fallback = { |
787 | 0 | NULL, NULL, AF_INET, sizeof(he_fallback_address), |
788 | 0 | (char **)&he_fallback_addresses |
789 | 0 | }; |
790 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
791 | | # pragma pointer_size restore |
792 | | #endif |
793 | |
|
794 | 0 | struct servent *se; |
795 | | /* Apparently, on WIN64, s_proto and s_port have traded places... */ |
796 | | #ifdef _WIN64 |
797 | | struct servent se_fallback = { NULL, NULL, NULL, 0 }; |
798 | | #else |
799 | 0 | struct servent se_fallback = { NULL, NULL, 0, NULL }; |
800 | 0 | #endif |
801 | |
|
802 | 0 | if (!RUN_ONCE(&bio_lookup_init, do_bio_lookup_init)) { |
803 | | /* Should this be raised inside do_bio_lookup_init()? */ |
804 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_CRYPTO_LIB); |
805 | 0 | return 0; |
806 | 0 | } |
807 | | |
808 | 0 | if (!CRYPTO_THREAD_write_lock(bio_lookup_lock)) |
809 | 0 | return 0; |
810 | | |
811 | 0 | he_fallback_address = INADDR_ANY; |
812 | 0 | if (host == NULL) { |
813 | 0 | he = &he_fallback; |
814 | 0 | switch (lookup_type) { |
815 | 0 | case BIO_LOOKUP_CLIENT: |
816 | 0 | he_fallback_address = INADDR_LOOPBACK; |
817 | 0 | break; |
818 | 0 | case BIO_LOOKUP_SERVER: |
819 | 0 | he_fallback_address = INADDR_ANY; |
820 | 0 | break; |
821 | 0 | default: |
822 | | /* We forgot to handle a lookup type! */ |
823 | 0 | assert("We forgot to handle a lookup type!" == NULL); |
824 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_INTERNAL_ERROR); |
825 | 0 | ret = 0; |
826 | 0 | goto err; |
827 | 0 | } |
828 | 0 | } else { |
829 | 0 | he = gethostbyname(host); |
830 | |
|
831 | 0 | if (he == NULL) { |
832 | 0 | #ifndef OPENSSL_SYS_WINDOWS |
833 | | /* |
834 | | * This might be misleading, because h_errno is used as if |
835 | | * it was errno. To minimize mixup add 1000. Underlying |
836 | | * reason for this is that hstrerror is declared obsolete, |
837 | | * not to mention that a) h_errno is not always guaranteed |
838 | | * to be meaningless; b) hstrerror can reside in yet another |
839 | | * library, linking for sake of hstrerror is an overkill; |
840 | | * c) this path is not executed on contemporary systems |
841 | | * anyway [above getaddrinfo/gai_strerror is]. We just let |
842 | | * system administrator figure this out... |
843 | | */ |
844 | | # if defined(OPENSSL_SYS_VXWORKS) |
845 | | /* h_errno doesn't exist on VxWorks */ |
846 | | ERR_raise_data(ERR_LIB_SYS, 1000, |
847 | | "calling gethostbyname()"); |
848 | | # else |
849 | 0 | ERR_raise_data(ERR_LIB_SYS, 1000 + h_errno, |
850 | 0 | "calling gethostbyname()"); |
851 | 0 | # endif |
852 | | #else |
853 | | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
854 | | "calling gethostbyname()"); |
855 | | #endif |
856 | 0 | ret = 0; |
857 | 0 | goto err; |
858 | 0 | } |
859 | 0 | } |
860 | | |
861 | 0 | if (service == NULL) { |
862 | 0 | se_fallback.s_port = 0; |
863 | 0 | se_fallback.s_proto = NULL; |
864 | 0 | se = &se_fallback; |
865 | 0 | } else { |
866 | 0 | char *endp = NULL; |
867 | 0 | long portnum = strtol(service, &endp, 10); |
868 | | |
869 | | /* |
870 | | * Because struct servent is defined for 32-bit pointers only with |
871 | | * VMS C, we need to make sure that 'proto' is a 32-bit pointer. |
872 | | */ |
873 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
874 | | # pragma pointer_size save |
875 | | # pragma pointer_size 32 |
876 | | #endif |
877 | 0 | char *proto = NULL; |
878 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
879 | | # pragma pointer_size restore |
880 | | #endif |
881 | |
|
882 | 0 | switch (socktype) { |
883 | 0 | case SOCK_STREAM: |
884 | 0 | proto = "tcp"; |
885 | 0 | break; |
886 | 0 | case SOCK_DGRAM: |
887 | 0 | proto = "udp"; |
888 | 0 | break; |
889 | 0 | } |
890 | | |
891 | 0 | if (endp != service && *endp == '\0' |
892 | 0 | && portnum > 0 && portnum < 65536) { |
893 | 0 | se_fallback.s_port = htons((unsigned short)portnum); |
894 | 0 | se_fallback.s_proto = proto; |
895 | 0 | se = &se_fallback; |
896 | 0 | } else if (endp == service) { |
897 | 0 | se = getservbyname(service, proto); |
898 | |
|
899 | 0 | if (se == NULL) { |
900 | 0 | ERR_raise_data(ERR_LIB_SYS, get_last_socket_error(), |
901 | 0 | "calling getservbyname()"); |
902 | 0 | goto err; |
903 | 0 | } |
904 | 0 | } else { |
905 | 0 | ERR_raise(ERR_LIB_BIO, BIO_R_MALFORMED_HOST_OR_SERVICE); |
906 | 0 | goto err; |
907 | 0 | } |
908 | 0 | } |
909 | | |
910 | 0 | *res = NULL; |
911 | |
|
912 | 0 | { |
913 | | /* |
914 | | * Because hostent::h_addr_list is an array of 32-bit pointers with VMS C, |
915 | | * we must make sure our iterator designates the same element type, hence |
916 | | * the pointer size dance. |
917 | | */ |
918 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
919 | | # pragma pointer_size save |
920 | | # pragma pointer_size 32 |
921 | | #endif |
922 | 0 | char **addrlistp; |
923 | | #if defined(OPENSSL_SYS_VMS) && defined(__DECC) |
924 | | # pragma pointer_size restore |
925 | | #endif |
926 | 0 | size_t addresses; |
927 | 0 | BIO_ADDRINFO *tmp_bai = NULL; |
928 | | |
929 | | /* The easiest way to create a linked list from an |
930 | | array is to start from the back */ |
931 | 0 | for (addrlistp = he->h_addr_list; *addrlistp != NULL; |
932 | 0 | addrlistp++) |
933 | 0 | ; |
934 | |
|
935 | 0 | for (addresses = addrlistp - he->h_addr_list; |
936 | 0 | addrlistp--, addresses-- > 0; ) { |
937 | 0 | if (!addrinfo_wrap(he->h_addrtype, socktype, |
938 | 0 | *addrlistp, he->h_length, |
939 | 0 | se->s_port, &tmp_bai)) |
940 | 0 | goto addrinfo_wrap_err; |
941 | 0 | tmp_bai->bai_next = *res; |
942 | 0 | *res = tmp_bai; |
943 | 0 | continue; |
944 | 0 | addrinfo_wrap_err: |
945 | 0 | BIO_ADDRINFO_free(*res); |
946 | 0 | *res = NULL; |
947 | 0 | ERR_raise(ERR_LIB_BIO, ERR_R_BIO_LIB); |
948 | 0 | ret = 0; |
949 | 0 | goto err; |
950 | 0 | } |
951 | | |
952 | 0 | ret = 1; |
953 | 0 | } |
954 | 0 | err: |
955 | 0 | CRYPTO_THREAD_unlock(bio_lookup_lock); |
956 | 0 | } |
957 | | |
958 | 0 | return ret; |
959 | 0 | } |
960 | | |
961 | | #endif /* OPENSSL_NO_SOCK */ |