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