1 /*- 2 * Copyright (c) 1998-2011 Dag-Erling Smørgrav 3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer 11 * in this position and unchanged. 12 * 2. Redistributions in binary form must reproduce the above copyright 13 * notice, this list of conditions and the following disclaimer in the 14 * documentation and/or other materials provided with the distribution. 15 * 3. The name of the author may not be used to endorse or promote products 16 * derived from this software without specific prior written permission 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30 #include <sys/cdefs.h> 31 __FBSDID("$FreeBSD$"); 32 33 #include <sys/param.h> 34 #include <sys/socket.h> 35 #include <sys/time.h> 36 #include <sys/uio.h> 37 38 #include <netinet/in.h> 39 40 #include <ctype.h> 41 #include <errno.h> 42 #include <fcntl.h> 43 #include <netdb.h> 44 #include <pwd.h> 45 #include <stdarg.h> 46 #include <stdlib.h> 47 #include <stdio.h> 48 #include <string.h> 49 #include <unistd.h> 50 51 #ifdef WITH_SSL 52 #include <openssl/x509v3.h> 53 #endif 54 55 #include "fetch.h" 56 #include "common.h" 57 58 59 /*** Local data **************************************************************/ 60 61 /* 62 * Error messages for resolver errors 63 */ 64 static struct fetcherr netdb_errlist[] = { 65 #ifdef EAI_NODATA 66 { EAI_NODATA, FETCH_RESOLV, "Host not found" }, 67 #endif 68 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" }, 69 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" }, 70 { EAI_NONAME, FETCH_RESOLV, "No address record" }, 71 { -1, FETCH_UNKNOWN, "Unknown resolver error" } 72 }; 73 74 /* End-of-Line */ 75 static const char ENDL[2] = "\r\n"; 76 77 78 /*** Error-reporting functions ***********************************************/ 79 80 /* 81 * Map error code to string 82 */ 83 static struct fetcherr * 84 fetch_finderr(struct fetcherr *p, int e) 85 { 86 while (p->num != -1 && p->num != e) 87 p++; 88 return (p); 89 } 90 91 /* 92 * Set error code 93 */ 94 void 95 fetch_seterr(struct fetcherr *p, int e) 96 { 97 p = fetch_finderr(p, e); 98 fetchLastErrCode = p->cat; 99 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string); 100 } 101 102 /* 103 * Set error code according to errno 104 */ 105 void 106 fetch_syserr(void) 107 { 108 switch (errno) { 109 case 0: 110 fetchLastErrCode = FETCH_OK; 111 break; 112 case EPERM: 113 case EACCES: 114 case EROFS: 115 case EAUTH: 116 case ENEEDAUTH: 117 fetchLastErrCode = FETCH_AUTH; 118 break; 119 case ENOENT: 120 case EISDIR: /* XXX */ 121 fetchLastErrCode = FETCH_UNAVAIL; 122 break; 123 case ENOMEM: 124 fetchLastErrCode = FETCH_MEMORY; 125 break; 126 case EBUSY: 127 case EAGAIN: 128 fetchLastErrCode = FETCH_TEMP; 129 break; 130 case EEXIST: 131 fetchLastErrCode = FETCH_EXISTS; 132 break; 133 case ENOSPC: 134 fetchLastErrCode = FETCH_FULL; 135 break; 136 case EADDRINUSE: 137 case EADDRNOTAVAIL: 138 case ENETDOWN: 139 case ENETUNREACH: 140 case ENETRESET: 141 case EHOSTUNREACH: 142 fetchLastErrCode = FETCH_NETWORK; 143 break; 144 case ECONNABORTED: 145 case ECONNRESET: 146 fetchLastErrCode = FETCH_ABORT; 147 break; 148 case ETIMEDOUT: 149 fetchLastErrCode = FETCH_TIMEOUT; 150 break; 151 case ECONNREFUSED: 152 case EHOSTDOWN: 153 fetchLastErrCode = FETCH_DOWN; 154 break; 155 default: 156 fetchLastErrCode = FETCH_UNKNOWN; 157 } 158 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno)); 159 } 160 161 162 /* 163 * Emit status message 164 */ 165 void 166 fetch_info(const char *fmt, ...) 167 { 168 va_list ap; 169 170 va_start(ap, fmt); 171 vfprintf(stderr, fmt, ap); 172 va_end(ap); 173 fputc('\n', stderr); 174 } 175 176 177 /*** Network-related utility functions ***************************************/ 178 179 /* 180 * Return the default port for a scheme 181 */ 182 int 183 fetch_default_port(const char *scheme) 184 { 185 struct servent *se; 186 187 if ((se = getservbyname(scheme, "tcp")) != NULL) 188 return (ntohs(se->s_port)); 189 if (strcasecmp(scheme, SCHEME_FTP) == 0) 190 return (FTP_DEFAULT_PORT); 191 if (strcasecmp(scheme, SCHEME_HTTP) == 0) 192 return (HTTP_DEFAULT_PORT); 193 return (0); 194 } 195 196 /* 197 * Return the default proxy port for a scheme 198 */ 199 int 200 fetch_default_proxy_port(const char *scheme) 201 { 202 if (strcasecmp(scheme, SCHEME_FTP) == 0) 203 return (FTP_DEFAULT_PROXY_PORT); 204 if (strcasecmp(scheme, SCHEME_HTTP) == 0) 205 return (HTTP_DEFAULT_PROXY_PORT); 206 return (0); 207 } 208 209 210 /* 211 * Create a connection for an existing descriptor. 212 */ 213 conn_t * 214 fetch_reopen(int sd) 215 { 216 conn_t *conn; 217 int opt = 1; 218 219 /* allocate and fill connection structure */ 220 if ((conn = calloc(1, sizeof(*conn))) == NULL) 221 return (NULL); 222 fcntl(sd, F_SETFD, FD_CLOEXEC); 223 setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt); 224 conn->sd = sd; 225 ++conn->ref; 226 return (conn); 227 } 228 229 230 /* 231 * Bump a connection's reference count. 232 */ 233 conn_t * 234 fetch_ref(conn_t *conn) 235 { 236 237 ++conn->ref; 238 return (conn); 239 } 240 241 242 /* 243 * Bind a socket to a specific local address 244 */ 245 int 246 fetch_bind(int sd, int af, const char *addr) 247 { 248 struct addrinfo hints, *res, *res0; 249 int err; 250 251 memset(&hints, 0, sizeof(hints)); 252 hints.ai_family = af; 253 hints.ai_socktype = SOCK_STREAM; 254 hints.ai_protocol = 0; 255 if ((err = getaddrinfo(addr, NULL, &hints, &res0)) != 0) 256 return (-1); 257 for (res = res0; res; res = res->ai_next) 258 if (bind(sd, res->ai_addr, res->ai_addrlen) == 0) 259 return (0); 260 return (-1); 261 } 262 263 264 /* 265 * Establish a TCP connection to the specified port on the specified host. 266 */ 267 conn_t * 268 fetch_connect(const char *host, int port, int af, int verbose) 269 { 270 conn_t *conn; 271 char pbuf[10]; 272 const char *bindaddr; 273 struct addrinfo hints, *res, *res0; 274 int sd, err; 275 276 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port)); 277 278 if (verbose) 279 fetch_info("looking up %s", host); 280 281 /* look up host name and set up socket address structure */ 282 snprintf(pbuf, sizeof(pbuf), "%d", port); 283 memset(&hints, 0, sizeof(hints)); 284 hints.ai_family = af; 285 hints.ai_socktype = SOCK_STREAM; 286 hints.ai_protocol = 0; 287 if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) { 288 netdb_seterr(err); 289 return (NULL); 290 } 291 bindaddr = getenv("FETCH_BIND_ADDRESS"); 292 293 if (verbose) 294 fetch_info("connecting to %s:%d", host, port); 295 296 /* try to connect */ 297 for (sd = -1, res = res0; res; sd = -1, res = res->ai_next) { 298 if ((sd = socket(res->ai_family, res->ai_socktype, 299 res->ai_protocol)) == -1) 300 continue; 301 if (bindaddr != NULL && *bindaddr != '\0' && 302 fetch_bind(sd, res->ai_family, bindaddr) != 0) { 303 fetch_info("failed to bind to '%s'", bindaddr); 304 close(sd); 305 continue; 306 } 307 if (connect(sd, res->ai_addr, res->ai_addrlen) == 0 && 308 fcntl(sd, F_SETFL, O_NONBLOCK) == 0) 309 break; 310 close(sd); 311 } 312 freeaddrinfo(res0); 313 if (sd == -1) { 314 fetch_syserr(); 315 return (NULL); 316 } 317 318 if ((conn = fetch_reopen(sd)) == NULL) { 319 fetch_syserr(); 320 close(sd); 321 } 322 return (conn); 323 } 324 325 #ifdef WITH_SSL 326 /* 327 * Convert characters A-Z to lowercase (intentionally avoid any locale 328 * specific conversions). 329 */ 330 static char 331 fetch_ssl_tolower(char in) 332 { 333 if (in >= 'A' && in <= 'Z') 334 return (in + 32); 335 else 336 return (in); 337 } 338 339 /* 340 * isalpha implementation that intentionally avoids any locale specific 341 * conversions. 342 */ 343 static int 344 fetch_ssl_isalpha(char in) 345 { 346 return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z')); 347 } 348 349 /* 350 * Check if passed hostnames a and b are equal. 351 */ 352 static int 353 fetch_ssl_hname_equal(const char *a, size_t alen, const char *b, 354 size_t blen) 355 { 356 size_t i; 357 358 if (alen != blen) 359 return (0); 360 for (i = 0; i < alen; ++i) { 361 if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i])) 362 return (0); 363 } 364 return (1); 365 } 366 367 /* 368 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9 369 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha- 370 * numeric characters. Double hyphens (like they're found in IDN a-labels 371 * 'xn--') are not allowed. Empty labels are invalid. 372 */ 373 static int 374 fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok) 375 { 376 size_t i; 377 378 if (!len || l[0] == '-' || l[len-1] == '-') 379 return (0); 380 for (i = 0; i < len; ++i) { 381 if (!isdigit(l[i]) && 382 !fetch_ssl_isalpha(l[i]) && 383 !(l[i] == '*' && wcok) && 384 !(l[i] == '-' && l[i - 1] != '-')) 385 return (0); 386 } 387 return (1); 388 } 389 390 /* 391 * Check if host name consists only of numbers. This might indicate an IP 392 * address, which is not a good idea for CN wildcard comparison. 393 */ 394 static int 395 fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len) 396 { 397 size_t i; 398 399 for (i = 0; i < len; ++i) { 400 if (!((hostname[i] >= '0' && hostname[i] <= '9') || 401 hostname[i] == '.')) 402 return (0); 403 } 404 return (1); 405 } 406 407 /* 408 * Check if the host name h passed matches the pattern passed in m which 409 * is usually part of subjectAltName or CN of a certificate presented to 410 * the client. This includes wildcard matching. The algorithm is based on 411 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280. 412 */ 413 static int 414 fetch_ssl_hname_match(const char *h, size_t hlen, const char *m, 415 size_t mlen) 416 { 417 int delta, hdotidx, mdot1idx, wcidx; 418 const char *hdot, *mdot1, *mdot2; 419 const char *wc; /* wildcard */ 420 421 if (!(h && *h && m && *m)) 422 return (0); 423 if ((wc = strnstr(m, "*", mlen)) == NULL) 424 return (fetch_ssl_hname_equal(h, hlen, m, mlen)); 425 wcidx = wc - m; 426 /* hostname should not be just dots and numbers */ 427 if (fetch_ssl_hname_is_only_numbers(h, hlen)) 428 return (0); 429 /* only one wildcard allowed in pattern */ 430 if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL) 431 return (0); 432 /* 433 * there must be at least two more domain labels and 434 * wildcard has to be in the leftmost label (RFC6125) 435 */ 436 mdot1 = strnstr(m, ".", mlen); 437 if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4) 438 return (0); 439 mdot1idx = mdot1 - m; 440 mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1); 441 if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2) 442 return (0); 443 /* hostname must contain a dot and not be the 1st char */ 444 hdot = strnstr(h, ".", hlen); 445 if (hdot == NULL || hdot == h) 446 return (0); 447 hdotidx = hdot - h; 448 /* 449 * host part of hostname must be at least as long as 450 * pattern it's supposed to match 451 */ 452 if (hdotidx < mdot1idx) 453 return (0); 454 /* 455 * don't allow wildcards in non-traditional domain names 456 * (IDN, A-label, U-label...) 457 */ 458 if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) || 459 !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1)) 460 return (0); 461 /* match domain part (part after first dot) */ 462 if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1, 463 mlen - mdot1idx)) 464 return (0); 465 /* match part left of wildcard */ 466 if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx)) 467 return (0); 468 /* match part right of wildcard */ 469 delta = mdot1idx - wcidx - 1; 470 if (!fetch_ssl_hname_equal(hdot - delta, delta, 471 mdot1 - delta, delta)) 472 return (0); 473 /* all tests succeded, it's a match */ 474 return (1); 475 } 476 477 /* 478 * Get numeric host address info - returns NULL if host was not an IP 479 * address. The caller is responsible for deallocation using 480 * freeaddrinfo(3). 481 */ 482 static struct addrinfo * 483 fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len) 484 { 485 struct addrinfo hints, *res; 486 char *host; 487 488 host = (char *)malloc(len + 1); 489 memcpy(host, hostname, len); 490 host[len] = '\0'; 491 memset(&hints, 0, sizeof(hints)); 492 hints.ai_family = PF_UNSPEC; 493 hints.ai_socktype = SOCK_STREAM; 494 hints.ai_protocol = 0; 495 hints.ai_flags = AI_NUMERICHOST; 496 /* port is not relevant for this purpose */ 497 getaddrinfo(host, "443", &hints, &res); 498 free(host); 499 return res; 500 } 501 502 /* 503 * Compare ip address in addrinfo with address passes. 504 */ 505 static int 506 fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost, 507 size_t rhostlen) 508 { 509 const void *left; 510 511 if (lhost->ai_family == AF_INET && rhostlen == 4) { 512 left = (void *)&((struct sockaddr_in*)(void *) 513 lhost->ai_addr)->sin_addr.s_addr; 514 #ifdef INET6 515 } else if (lhost->ai_family == AF_INET6 && rhostlen == 16) { 516 left = (void *)&((struct sockaddr_in6 *)(void *) 517 lhost->ai_addr)->sin6_addr; 518 #endif 519 } else 520 return (0); 521 return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0); 522 } 523 524 /* 525 * Compare ip address in addrinfo with host passed. If host is not an IP 526 * address, comparison will fail. 527 */ 528 static int 529 fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r, 530 size_t rlen) 531 { 532 struct addrinfo *raddr; 533 int ret; 534 char *rip; 535 536 ret = 0; 537 if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL) 538 return 0; /* not a numeric host */ 539 540 if (laddr->ai_family == raddr->ai_family) { 541 if (laddr->ai_family == AF_INET) { 542 rip = (char *)&((struct sockaddr_in *)(void *) 543 raddr->ai_addr)->sin_addr.s_addr; 544 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4); 545 #ifdef INET6 546 } else if (laddr->ai_family == AF_INET6) { 547 rip = (char *)&((struct sockaddr_in6 *)(void *) 548 raddr->ai_addr)->sin6_addr; 549 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16); 550 #endif 551 } 552 553 } 554 freeaddrinfo(raddr); 555 return (ret); 556 } 557 558 /* 559 * Verify server certificate by subjectAltName. 560 */ 561 static int 562 fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames, 563 const char *host, struct addrinfo *ip) 564 { 565 const GENERAL_NAME *name; 566 size_t nslen; 567 int i; 568 const char *ns; 569 570 for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) { 571 #if OPENSSL_VERSION_NUMBER < 0x10000000L 572 /* 573 * This is a workaround, since the following line causes 574 * alignment issues in clang: 575 * name = sk_GENERAL_NAME_value(altnames, i); 576 * OpenSSL explicitly warns not to use those macros 577 * directly, but there isn't much choice (and there 578 * shouldn't be any ill side effects) 579 */ 580 name = (GENERAL_NAME *)SKM_sk_value(void, altnames, i); 581 #else 582 name = sk_GENERAL_NAME_value(altnames, i); 583 #endif 584 ns = (const char *)ASN1_STRING_data(name->d.ia5); 585 nslen = (size_t)ASN1_STRING_length(name->d.ia5); 586 587 if (name->type == GEN_DNS && ip == NULL && 588 fetch_ssl_hname_match(host, strlen(host), ns, nslen)) 589 return (1); 590 else if (name->type == GEN_IPADD && ip != NULL && 591 fetch_ssl_ipaddr_match_bin(ip, ns, nslen)) 592 return (1); 593 } 594 return (0); 595 } 596 597 /* 598 * Verify server certificate by CN. 599 */ 600 static int 601 fetch_ssl_verify_cn(X509_NAME *subject, const char *host, 602 struct addrinfo *ip) 603 { 604 ASN1_STRING *namedata; 605 X509_NAME_ENTRY *nameentry; 606 int cnlen, lastpos, loc, ret; 607 unsigned char *cn; 608 609 ret = 0; 610 lastpos = -1; 611 loc = -1; 612 cn = NULL; 613 /* get most specific CN (last entry in list) and compare */ 614 while ((lastpos = X509_NAME_get_index_by_NID(subject, 615 NID_commonName, lastpos)) != -1) 616 loc = lastpos; 617 618 if (loc > -1) { 619 nameentry = X509_NAME_get_entry(subject, loc); 620 namedata = X509_NAME_ENTRY_get_data(nameentry); 621 cnlen = ASN1_STRING_to_UTF8(&cn, namedata); 622 if (ip == NULL && 623 fetch_ssl_hname_match(host, strlen(host), cn, cnlen)) 624 ret = 1; 625 else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen)) 626 ret = 1; 627 OPENSSL_free(cn); 628 } 629 return (ret); 630 } 631 632 /* 633 * Verify that server certificate subjectAltName/CN matches 634 * hostname. First check, if there are alternative subject names. If yes, 635 * those have to match. Only if those don't exist it falls back to 636 * checking the subject's CN. 637 */ 638 static int 639 fetch_ssl_verify_hname(X509 *cert, const char *host) 640 { 641 struct addrinfo *ip; 642 STACK_OF(GENERAL_NAME) *altnames; 643 X509_NAME *subject; 644 int ret; 645 646 ret = 0; 647 ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host)); 648 altnames = X509_get_ext_d2i(cert, NID_subject_alt_name, 649 NULL, NULL); 650 651 if (altnames != NULL) { 652 ret = fetch_ssl_verify_altname(altnames, host, ip); 653 } else { 654 subject = X509_get_subject_name(cert); 655 if (subject != NULL) 656 ret = fetch_ssl_verify_cn(subject, host, ip); 657 } 658 659 if (ip != NULL) 660 freeaddrinfo(ip); 661 if (altnames != NULL) 662 GENERAL_NAMES_free(altnames); 663 return (ret); 664 } 665 666 /* 667 * Configure transport security layer based on environment. 668 */ 669 static void 670 fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose) 671 { 672 long ssl_ctx_options; 673 674 ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_TICKET; 675 if (getenv("SSL_ALLOW_SSL2") == NULL) 676 ssl_ctx_options |= SSL_OP_NO_SSLv2; 677 if (getenv("SSL_NO_SSL3") != NULL) 678 ssl_ctx_options |= SSL_OP_NO_SSLv3; 679 if (getenv("SSL_NO_TLS1") != NULL) 680 ssl_ctx_options |= SSL_OP_NO_TLSv1; 681 if (verbose) 682 fetch_info("SSL options: %x", ssl_ctx_options); 683 SSL_CTX_set_options(ctx, ssl_ctx_options); 684 } 685 686 687 /* 688 * Configure peer verification based on environment. 689 */ 690 static int 691 fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose) 692 { 693 X509_LOOKUP *crl_lookup; 694 X509_STORE *crl_store; 695 const char *ca_cert_file, *ca_cert_path, *crl_file; 696 697 if (getenv("SSL_NO_VERIFY_PEER") == NULL) { 698 ca_cert_file = getenv("SSL_CA_CERT_FILE") != NULL ? 699 getenv("SSL_CA_CERT_FILE") : "/etc/ssl/cert.pem"; 700 ca_cert_path = getenv("SSL_CA_CERT_PATH"); 701 if (verbose) { 702 fetch_info("Peer verification enabled"); 703 if (ca_cert_file != NULL) 704 fetch_info("Using CA cert file: %s", 705 ca_cert_file); 706 if (ca_cert_path != NULL) 707 fetch_info("Using CA cert path: %s", 708 ca_cert_path); 709 } 710 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER, 711 fetch_ssl_cb_verify_crt); 712 SSL_CTX_load_verify_locations(ctx, ca_cert_file, 713 ca_cert_path); 714 if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) { 715 if (verbose) 716 fetch_info("Using CRL file: %s", crl_file); 717 crl_store = SSL_CTX_get_cert_store(ctx); 718 crl_lookup = X509_STORE_add_lookup(crl_store, 719 X509_LOOKUP_file()); 720 if (crl_lookup == NULL || 721 !X509_load_crl_file(crl_lookup, crl_file, 722 X509_FILETYPE_PEM)) { 723 fprintf(stderr, 724 "Could not load CRL file %s\n", 725 crl_file); 726 return (0); 727 } 728 X509_STORE_set_flags(crl_store, 729 X509_V_FLAG_CRL_CHECK | 730 X509_V_FLAG_CRL_CHECK_ALL); 731 } 732 } 733 return (1); 734 } 735 736 /* 737 * Configure client certificate based on environment. 738 */ 739 static int 740 fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose) 741 { 742 const char *client_cert_file, *client_key_file; 743 744 if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) { 745 client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ? 746 getenv("SSL_CLIENT_KEY_FILE") : client_cert_file; 747 if (verbose) { 748 fetch_info("Using client cert file: %s", 749 client_cert_file); 750 fetch_info("Using client key file: %s", 751 client_key_file); 752 } 753 if (SSL_CTX_use_certificate_chain_file(ctx, 754 client_cert_file) != 1) { 755 fprintf(stderr, 756 "Could not load client certificate %s\n", 757 client_cert_file); 758 return (0); 759 } 760 if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file, 761 SSL_FILETYPE_PEM) != 1) { 762 fprintf(stderr, 763 "Could not load client key %s\n", 764 client_key_file); 765 return (0); 766 } 767 } 768 return (1); 769 } 770 771 /* 772 * Callback for SSL certificate verification, this is called on server 773 * cert verification. It takes no decision, but informs the user in case 774 * verification failed. 775 */ 776 int 777 fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx) 778 { 779 X509 *crt; 780 X509_NAME *name; 781 char *str; 782 783 str = NULL; 784 if (!verified) { 785 if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL && 786 (name = X509_get_subject_name(crt)) != NULL) 787 str = X509_NAME_oneline(name, 0, 0); 788 fprintf(stderr, "Certificate verification failed for %s\n", 789 str != NULL ? str : "no relevant certificate"); 790 OPENSSL_free(str); 791 } 792 return (verified); 793 } 794 795 #endif 796 797 /* 798 * Enable SSL on a connection. 799 */ 800 int 801 fetch_ssl(conn_t *conn, const struct url *URL, int verbose) 802 { 803 #ifdef WITH_SSL 804 int ret, ssl_err; 805 X509_NAME *name; 806 char *str; 807 808 /* Init the SSL library and context */ 809 if (!SSL_library_init()){ 810 fprintf(stderr, "SSL library init failed\n"); 811 return (-1); 812 } 813 814 SSL_load_error_strings(); 815 816 conn->ssl_meth = SSLv23_client_method(); 817 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth); 818 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY); 819 820 fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose); 821 if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose)) 822 return (-1); 823 if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose)) 824 return (-1); 825 826 conn->ssl = SSL_new(conn->ssl_ctx); 827 if (conn->ssl == NULL) { 828 fprintf(stderr, "SSL context creation failed\n"); 829 return (-1); 830 } 831 SSL_set_fd(conn->ssl, conn->sd); 832 833 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT) 834 if (!SSL_set_tlsext_host_name(conn->ssl, 835 __DECONST(struct url *, URL)->host)) { 836 fprintf(stderr, 837 "TLS server name indication extension failed for host %s\n", 838 URL->host); 839 return (-1); 840 } 841 #endif 842 while ((ret = SSL_connect(conn->ssl)) == -1) { 843 ssl_err = SSL_get_error(conn->ssl, ret); 844 if (ssl_err != SSL_ERROR_WANT_READ && 845 ssl_err != SSL_ERROR_WANT_WRITE) { 846 ERR_print_errors_fp(stderr); 847 return (-1); 848 } 849 } 850 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl); 851 852 if (conn->ssl_cert == NULL) { 853 fprintf(stderr, "No server SSL certificate\n"); 854 return (-1); 855 } 856 857 if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) { 858 if (verbose) 859 fetch_info("Verify hostname"); 860 if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) { 861 fprintf(stderr, 862 "SSL certificate subject doesn't match host %s\n", 863 URL->host); 864 return (-1); 865 } 866 } 867 868 if (verbose) { 869 fetch_info("SSL connection established using %s", 870 SSL_get_cipher(conn->ssl)); 871 name = X509_get_subject_name(conn->ssl_cert); 872 str = X509_NAME_oneline(name, 0, 0); 873 fetch_info("Certificate subject: %s", str); 874 OPENSSL_free(str); 875 name = X509_get_issuer_name(conn->ssl_cert); 876 str = X509_NAME_oneline(name, 0, 0); 877 fetch_info("Certificate issuer: %s", str); 878 OPENSSL_free(str); 879 } 880 881 return (0); 882 #else 883 (void)conn; 884 (void)verbose; 885 fprintf(stderr, "SSL support disabled\n"); 886 return (-1); 887 #endif 888 } 889 890 #define FETCH_READ_WAIT -2 891 #define FETCH_READ_ERROR -1 892 #define FETCH_READ_DONE 0 893 894 #ifdef WITH_SSL 895 static ssize_t 896 fetch_ssl_read(SSL *ssl, char *buf, size_t len) 897 { 898 ssize_t rlen; 899 int ssl_err; 900 901 rlen = SSL_read(ssl, buf, len); 902 if (rlen < 0) { 903 ssl_err = SSL_get_error(ssl, rlen); 904 if (ssl_err == SSL_ERROR_WANT_READ || 905 ssl_err == SSL_ERROR_WANT_WRITE) { 906 return (FETCH_READ_WAIT); 907 } else { 908 ERR_print_errors_fp(stderr); 909 return (FETCH_READ_ERROR); 910 } 911 } 912 return (rlen); 913 } 914 #endif 915 916 /* 917 * Cache some data that was read from a socket but cannot be immediately 918 * returned because of an interrupted system call. 919 */ 920 static int 921 fetch_cache_data(conn_t *conn, char *src, size_t nbytes) 922 { 923 char *tmp; 924 925 if (conn->cache.size < nbytes) { 926 tmp = realloc(conn->cache.buf, nbytes); 927 if (tmp == NULL) { 928 fetch_syserr(); 929 return (-1); 930 } 931 conn->cache.buf = tmp; 932 conn->cache.size = nbytes; 933 } 934 935 memcpy(conn->cache.buf, src, nbytes); 936 conn->cache.len = nbytes; 937 conn->cache.pos = 0; 938 939 return (0); 940 } 941 942 943 static ssize_t 944 fetch_socket_read(int sd, char *buf, size_t len) 945 { 946 ssize_t rlen; 947 948 rlen = read(sd, buf, len); 949 if (rlen < 0) { 950 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls)) 951 return (FETCH_READ_WAIT); 952 else 953 return (FETCH_READ_ERROR); 954 } 955 return (rlen); 956 } 957 958 /* 959 * Read a character from a connection w/ timeout 960 */ 961 ssize_t 962 fetch_read(conn_t *conn, char *buf, size_t len) 963 { 964 struct timeval now, timeout, delta; 965 fd_set readfds; 966 ssize_t rlen, total; 967 char *start; 968 969 if (fetchTimeout > 0) { 970 gettimeofday(&timeout, NULL); 971 timeout.tv_sec += fetchTimeout; 972 } 973 974 total = 0; 975 start = buf; 976 977 if (conn->cache.len > 0) { 978 /* 979 * The last invocation of fetch_read was interrupted by a 980 * signal after some data had been read from the socket. Copy 981 * the cached data into the supplied buffer before trying to 982 * read from the socket again. 983 */ 984 total = (conn->cache.len < len) ? conn->cache.len : len; 985 memcpy(buf, conn->cache.buf, total); 986 987 conn->cache.len -= total; 988 conn->cache.pos += total; 989 len -= total; 990 buf += total; 991 } 992 993 while (len > 0) { 994 /* 995 * The socket is non-blocking. Instead of the canonical 996 * select() -> read(), we do the following: 997 * 998 * 1) call read() or SSL_read(). 999 * 2) if an error occurred, return -1. 1000 * 3) if we received data but we still expect more, 1001 * update our counters and loop. 1002 * 4) if read() or SSL_read() signaled EOF, return. 1003 * 5) if we did not receive any data but we're not at EOF, 1004 * call select(). 1005 * 1006 * In the SSL case, this is necessary because if we 1007 * receive a close notification, we have to call 1008 * SSL_read() one additional time after we've read 1009 * everything we received. 1010 * 1011 * In the non-SSL case, it may improve performance (very 1012 * slightly) when reading small amounts of data. 1013 */ 1014 #ifdef WITH_SSL 1015 if (conn->ssl != NULL) 1016 rlen = fetch_ssl_read(conn->ssl, buf, len); 1017 else 1018 #endif 1019 rlen = fetch_socket_read(conn->sd, buf, len); 1020 if (rlen == 0) { 1021 break; 1022 } else if (rlen > 0) { 1023 len -= rlen; 1024 buf += rlen; 1025 total += rlen; 1026 continue; 1027 } else if (rlen == FETCH_READ_ERROR) { 1028 if (errno == EINTR) 1029 fetch_cache_data(conn, start, total); 1030 return (-1); 1031 } 1032 // assert(rlen == FETCH_READ_WAIT); 1033 FD_ZERO(&readfds); 1034 while (!FD_ISSET(conn->sd, &readfds)) { 1035 FD_SET(conn->sd, &readfds); 1036 if (fetchTimeout > 0) { 1037 gettimeofday(&now, NULL); 1038 if (!timercmp(&timeout, &now, >)) { 1039 errno = ETIMEDOUT; 1040 fetch_syserr(); 1041 return (-1); 1042 } 1043 timersub(&timeout, &now, &delta); 1044 } 1045 errno = 0; 1046 if (select(conn->sd + 1, &readfds, NULL, NULL, 1047 fetchTimeout > 0 ? &delta : NULL) < 0) { 1048 if (errno == EINTR) { 1049 if (fetchRestartCalls) 1050 continue; 1051 /* Save anything that was read. */ 1052 fetch_cache_data(conn, start, total); 1053 } 1054 fetch_syserr(); 1055 return (-1); 1056 } 1057 } 1058 } 1059 return (total); 1060 } 1061 1062 1063 /* 1064 * Read a line of text from a connection w/ timeout 1065 */ 1066 #define MIN_BUF_SIZE 1024 1067 1068 int 1069 fetch_getln(conn_t *conn) 1070 { 1071 char *tmp; 1072 size_t tmpsize; 1073 ssize_t len; 1074 char c; 1075 1076 if (conn->buf == NULL) { 1077 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) { 1078 errno = ENOMEM; 1079 return (-1); 1080 } 1081 conn->bufsize = MIN_BUF_SIZE; 1082 } 1083 1084 conn->buf[0] = '\0'; 1085 conn->buflen = 0; 1086 1087 do { 1088 len = fetch_read(conn, &c, 1); 1089 if (len == -1) 1090 return (-1); 1091 if (len == 0) 1092 break; 1093 conn->buf[conn->buflen++] = c; 1094 if (conn->buflen == conn->bufsize) { 1095 tmp = conn->buf; 1096 tmpsize = conn->bufsize * 2 + 1; 1097 if ((tmp = realloc(tmp, tmpsize)) == NULL) { 1098 errno = ENOMEM; 1099 return (-1); 1100 } 1101 conn->buf = tmp; 1102 conn->bufsize = tmpsize; 1103 } 1104 } while (c != '\n'); 1105 1106 conn->buf[conn->buflen] = '\0'; 1107 DEBUG(fprintf(stderr, "<<< %s", conn->buf)); 1108 return (0); 1109 } 1110 1111 1112 /* 1113 * Write to a connection w/ timeout 1114 */ 1115 ssize_t 1116 fetch_write(conn_t *conn, const char *buf, size_t len) 1117 { 1118 struct iovec iov; 1119 1120 iov.iov_base = __DECONST(char *, buf); 1121 iov.iov_len = len; 1122 return fetch_writev(conn, &iov, 1); 1123 } 1124 1125 /* 1126 * Write a vector to a connection w/ timeout 1127 * Note: can modify the iovec. 1128 */ 1129 ssize_t 1130 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt) 1131 { 1132 struct timeval now, timeout, delta; 1133 fd_set writefds; 1134 ssize_t wlen, total; 1135 int r; 1136 1137 if (fetchTimeout) { 1138 FD_ZERO(&writefds); 1139 gettimeofday(&timeout, NULL); 1140 timeout.tv_sec += fetchTimeout; 1141 } 1142 1143 total = 0; 1144 while (iovcnt > 0) { 1145 while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) { 1146 FD_SET(conn->sd, &writefds); 1147 gettimeofday(&now, NULL); 1148 delta.tv_sec = timeout.tv_sec - now.tv_sec; 1149 delta.tv_usec = timeout.tv_usec - now.tv_usec; 1150 if (delta.tv_usec < 0) { 1151 delta.tv_usec += 1000000; 1152 delta.tv_sec--; 1153 } 1154 if (delta.tv_sec < 0) { 1155 errno = ETIMEDOUT; 1156 fetch_syserr(); 1157 return (-1); 1158 } 1159 errno = 0; 1160 r = select(conn->sd + 1, NULL, &writefds, NULL, &delta); 1161 if (r == -1) { 1162 if (errno == EINTR && fetchRestartCalls) 1163 continue; 1164 return (-1); 1165 } 1166 } 1167 errno = 0; 1168 #ifdef WITH_SSL 1169 if (conn->ssl != NULL) 1170 wlen = SSL_write(conn->ssl, 1171 iov->iov_base, iov->iov_len); 1172 else 1173 #endif 1174 wlen = writev(conn->sd, iov, iovcnt); 1175 if (wlen == 0) { 1176 /* we consider a short write a failure */ 1177 /* XXX perhaps we shouldn't in the SSL case */ 1178 errno = EPIPE; 1179 fetch_syserr(); 1180 return (-1); 1181 } 1182 if (wlen < 0) { 1183 if (errno == EINTR && fetchRestartCalls) 1184 continue; 1185 return (-1); 1186 } 1187 total += wlen; 1188 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) { 1189 wlen -= iov->iov_len; 1190 iov++; 1191 iovcnt--; 1192 } 1193 if (iovcnt > 0) { 1194 iov->iov_len -= wlen; 1195 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen; 1196 } 1197 } 1198 return (total); 1199 } 1200 1201 1202 /* 1203 * Write a line of text to a connection w/ timeout 1204 */ 1205 int 1206 fetch_putln(conn_t *conn, const char *str, size_t len) 1207 { 1208 struct iovec iov[2]; 1209 int ret; 1210 1211 DEBUG(fprintf(stderr, ">>> %s\n", str)); 1212 iov[0].iov_base = __DECONST(char *, str); 1213 iov[0].iov_len = len; 1214 iov[1].iov_base = __DECONST(char *, ENDL); 1215 iov[1].iov_len = sizeof(ENDL); 1216 if (len == 0) 1217 ret = fetch_writev(conn, &iov[1], 1); 1218 else 1219 ret = fetch_writev(conn, iov, 2); 1220 if (ret == -1) 1221 return (-1); 1222 return (0); 1223 } 1224 1225 1226 /* 1227 * Close connection 1228 */ 1229 int 1230 fetch_close(conn_t *conn) 1231 { 1232 int ret; 1233 1234 if (--conn->ref > 0) 1235 return (0); 1236 #ifdef WITH_SSL 1237 if (conn->ssl) { 1238 SSL_shutdown(conn->ssl); 1239 SSL_set_connect_state(conn->ssl); 1240 SSL_free(conn->ssl); 1241 conn->ssl = NULL; 1242 } 1243 if (conn->ssl_ctx) { 1244 SSL_CTX_free(conn->ssl_ctx); 1245 conn->ssl_ctx = NULL; 1246 } 1247 if (conn->ssl_cert) { 1248 X509_free(conn->ssl_cert); 1249 conn->ssl_cert = NULL; 1250 } 1251 #endif 1252 ret = close(conn->sd); 1253 free(conn->cache.buf); 1254 free(conn->buf); 1255 free(conn); 1256 return (ret); 1257 } 1258 1259 1260 /*** Directory-related utility functions *************************************/ 1261 1262 int 1263 fetch_add_entry(struct url_ent **p, int *size, int *len, 1264 const char *name, struct url_stat *us) 1265 { 1266 struct url_ent *tmp; 1267 1268 if (*p == NULL) { 1269 *size = 0; 1270 *len = 0; 1271 } 1272 1273 if (*len >= *size - 1) { 1274 tmp = realloc(*p, (*size * 2 + 1) * sizeof(**p)); 1275 if (tmp == NULL) { 1276 errno = ENOMEM; 1277 fetch_syserr(); 1278 return (-1); 1279 } 1280 *size = (*size * 2 + 1); 1281 *p = tmp; 1282 } 1283 1284 tmp = *p + *len; 1285 snprintf(tmp->name, PATH_MAX, "%s", name); 1286 memcpy(&tmp->stat, us, sizeof(*us)); 1287 1288 (*len)++; 1289 (++tmp)->name[0] = 0; 1290 1291 return (0); 1292 } 1293 1294 1295 /*** Authentication-related utility functions ********************************/ 1296 1297 static const char * 1298 fetch_read_word(FILE *f) 1299 { 1300 static char word[1024]; 1301 1302 if (fscanf(f, " %1023s ", word) != 1) 1303 return (NULL); 1304 return (word); 1305 } 1306 1307 /* 1308 * Get authentication data for a URL from .netrc 1309 */ 1310 int 1311 fetch_netrc_auth(struct url *url) 1312 { 1313 char fn[PATH_MAX]; 1314 const char *word; 1315 char *p; 1316 FILE *f; 1317 1318 if ((p = getenv("NETRC")) != NULL) { 1319 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) { 1320 fetch_info("$NETRC specifies a file name " 1321 "longer than PATH_MAX"); 1322 return (-1); 1323 } 1324 } else { 1325 if ((p = getenv("HOME")) != NULL) { 1326 struct passwd *pwd; 1327 1328 if ((pwd = getpwuid(getuid())) == NULL || 1329 (p = pwd->pw_dir) == NULL) 1330 return (-1); 1331 } 1332 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn)) 1333 return (-1); 1334 } 1335 1336 if ((f = fopen(fn, "r")) == NULL) 1337 return (-1); 1338 while ((word = fetch_read_word(f)) != NULL) { 1339 if (strcmp(word, "default") == 0) { 1340 DEBUG(fetch_info("Using default .netrc settings")); 1341 break; 1342 } 1343 if (strcmp(word, "machine") == 0 && 1344 (word = fetch_read_word(f)) != NULL && 1345 strcasecmp(word, url->host) == 0) { 1346 DEBUG(fetch_info("Using .netrc settings for %s", word)); 1347 break; 1348 } 1349 } 1350 if (word == NULL) 1351 goto ferr; 1352 while ((word = fetch_read_word(f)) != NULL) { 1353 if (strcmp(word, "login") == 0) { 1354 if ((word = fetch_read_word(f)) == NULL) 1355 goto ferr; 1356 if (snprintf(url->user, sizeof(url->user), 1357 "%s", word) > (int)sizeof(url->user)) { 1358 fetch_info("login name in .netrc is too long"); 1359 url->user[0] = '\0'; 1360 } 1361 } else if (strcmp(word, "password") == 0) { 1362 if ((word = fetch_read_word(f)) == NULL) 1363 goto ferr; 1364 if (snprintf(url->pwd, sizeof(url->pwd), 1365 "%s", word) > (int)sizeof(url->pwd)) { 1366 fetch_info("password in .netrc is too long"); 1367 url->pwd[0] = '\0'; 1368 } 1369 } else if (strcmp(word, "account") == 0) { 1370 if ((word = fetch_read_word(f)) == NULL) 1371 goto ferr; 1372 /* XXX not supported! */ 1373 } else { 1374 break; 1375 } 1376 } 1377 fclose(f); 1378 return (0); 1379 ferr: 1380 fclose(f); 1381 return (-1); 1382 } 1383 1384 /* 1385 * The no_proxy environment variable specifies a set of domains for 1386 * which the proxy should not be consulted; the contents is a comma-, 1387 * or space-separated list of domain names. A single asterisk will 1388 * override all proxy variables and no transactions will be proxied 1389 * (for compatability with lynx and curl, see the discussion at 1390 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>). 1391 */ 1392 int 1393 fetch_no_proxy_match(const char *host) 1394 { 1395 const char *no_proxy, *p, *q; 1396 size_t h_len, d_len; 1397 1398 if ((no_proxy = getenv("NO_PROXY")) == NULL && 1399 (no_proxy = getenv("no_proxy")) == NULL) 1400 return (0); 1401 1402 /* asterisk matches any hostname */ 1403 if (strcmp(no_proxy, "*") == 0) 1404 return (1); 1405 1406 h_len = strlen(host); 1407 p = no_proxy; 1408 do { 1409 /* position p at the beginning of a domain suffix */ 1410 while (*p == ',' || isspace((unsigned char)*p)) 1411 p++; 1412 1413 /* position q at the first separator character */ 1414 for (q = p; *q; ++q) 1415 if (*q == ',' || isspace((unsigned char)*q)) 1416 break; 1417 1418 d_len = q - p; 1419 if (d_len > 0 && h_len >= d_len && 1420 strncasecmp(host + h_len - d_len, 1421 p, d_len) == 0) { 1422 /* domain name matches */ 1423 return (1); 1424 } 1425 1426 p = q + 1; 1427 } while (*q); 1428 1429 return (0); 1430 } 1431