2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 1998-2016 Dag-Erling Smørgrav
5 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de>
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer
13 * in this position and unchanged.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include <sys/cdefs.h>
33 __FBSDID("$FreeBSD$");
35 #include <sys/param.h>
36 #include <sys/socket.h>
40 #include <netinet/in.h>
56 #include <openssl/x509v3.h>
63 /*** Local data **************************************************************/
66 * Error messages for resolver errors
68 static struct fetcherr netdb_errlist[] = {
70 { EAI_NODATA, FETCH_RESOLV, "Host not found" },
72 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" },
73 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" },
74 { EAI_NONAME, FETCH_RESOLV, "No address record" },
75 { -1, FETCH_UNKNOWN, "Unknown resolver error" }
79 * SOCKS5 error enumerations
84 SOCKS5_ERR_READ_METHOD,
87 SOCKS5_ERR_NOTIMPLEMENTED,
88 SOCKS5_ERR_HOSTNAME_SIZE,
91 SOCKS5_ERR_NON_VER5_RESP,
93 SOCKS5_ERR_NOT_ALLOWED,
94 SOCKS5_ERR_NET_UNREACHABLE,
95 SOCKS5_ERR_HOST_UNREACHABLE,
96 SOCKS5_ERR_CONN_REFUSED,
97 SOCKS5_ERR_TTL_EXPIRED,
98 SOCKS5_ERR_COM_UNSUPPORTED,
99 SOCKS5_ERR_ADDR_UNSUPPORTED,
100 SOCKS5_ERR_UNSPECIFIED,
101 /* Configuration errors */
103 SOCKS5_ERR_BAD_PROXY_FORMAT,
108 * Error messages for SOCKS5 errors
110 static struct fetcherr socks5_errlist[] = {
111 /* SOCKS5 protocol errors */
112 { SOCKS5_ERR_SELECTION, FETCH_ABORT, "SOCKS5: Failed to send selection method" },
113 { SOCKS5_ERR_READ_METHOD, FETCH_ABORT, "SOCKS5: Failed to read method" },
114 { SOCKS5_ERR_VER5_ONLY, FETCH_PROTO, "SOCKS5: Only version 5 is implemented" },
115 { SOCKS5_ERR_NOMETHODS, FETCH_PROTO, "SOCKS5: No acceptable methods" },
116 { SOCKS5_ERR_NOTIMPLEMENTED, FETCH_PROTO, "SOCKS5: Method currently not implemented" },
117 { SOCKS5_ERR_HOSTNAME_SIZE, FETCH_PROTO, "SOCKS5: Hostname size is above 256 bytes" },
118 { SOCKS5_ERR_REQUEST, FETCH_PROTO, "SOCKS5: Failed to request" },
119 { SOCKS5_ERR_REPLY, FETCH_PROTO, "SOCKS5: Failed to receive reply" },
120 { SOCKS5_ERR_NON_VER5_RESP, FETCH_PROTO, "SOCKS5: Server responded with a non-version 5 response" },
121 { SOCKS5_ERR_GENERAL, FETCH_ABORT, "SOCKS5: General server failure" },
122 { SOCKS5_ERR_NOT_ALLOWED, FETCH_AUTH, "SOCKS5: Connection not allowed by ruleset" },
123 { SOCKS5_ERR_NET_UNREACHABLE, FETCH_NETWORK, "SOCKS5: Network unreachable" },
124 { SOCKS5_ERR_HOST_UNREACHABLE, FETCH_ABORT, "SOCKS5: Host unreachable" },
125 { SOCKS5_ERR_CONN_REFUSED, FETCH_ABORT, "SOCKS5: Connection refused" },
126 { SOCKS5_ERR_TTL_EXPIRED, FETCH_TIMEOUT, "SOCKS5: TTL expired" },
127 { SOCKS5_ERR_COM_UNSUPPORTED, FETCH_PROTO, "SOCKS5: Command not supported" },
128 { SOCKS5_ERR_ADDR_UNSUPPORTED, FETCH_ABORT, "SOCKS5: Address type not supported" },
129 { SOCKS5_ERR_UNSPECIFIED, FETCH_UNKNOWN, "SOCKS5: Unspecified error" },
130 /* Configuration error */
131 { SOCKS5_ERR_BAD_HOST, FETCH_ABORT, "SOCKS5: Bad proxy host" },
132 { SOCKS5_ERR_BAD_PROXY_FORMAT, FETCH_ABORT, "SOCKS5: Bad proxy format" },
133 { SOCKS5_ERR_BAD_PORT, FETCH_ABORT, "SOCKS5: Bad port" }
137 static const char ENDL[2] = "\r\n";
140 /*** Error-reporting functions ***********************************************/
143 * Map error code to string
145 static struct fetcherr *
146 fetch_finderr(struct fetcherr *p, int e)
148 while (p->num != -1 && p->num != e)
157 fetch_seterr(struct fetcherr *p, int e)
159 p = fetch_finderr(p, e);
160 fetchLastErrCode = p->cat;
161 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
165 * Set error code according to errno
172 fetchLastErrCode = FETCH_OK;
179 fetchLastErrCode = FETCH_AUTH;
182 case EISDIR: /* XXX */
183 fetchLastErrCode = FETCH_UNAVAIL;
186 fetchLastErrCode = FETCH_MEMORY;
190 fetchLastErrCode = FETCH_TEMP;
193 fetchLastErrCode = FETCH_EXISTS;
196 fetchLastErrCode = FETCH_FULL;
204 fetchLastErrCode = FETCH_NETWORK;
208 fetchLastErrCode = FETCH_ABORT;
211 fetchLastErrCode = FETCH_TIMEOUT;
215 fetchLastErrCode = FETCH_DOWN;
218 fetchLastErrCode = FETCH_UNKNOWN;
220 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
225 * Emit status message
228 fetch_info(const char *fmt, ...)
233 vfprintf(stderr, fmt, ap);
239 /*** Network-related utility functions ***************************************/
242 * Return the default port for a scheme
245 fetch_default_port(const char *scheme)
249 if ((se = getservbyname(scheme, "tcp")) != NULL)
250 return (ntohs(se->s_port));
251 if (strcmp(scheme, SCHEME_FTP) == 0)
252 return (FTP_DEFAULT_PORT);
253 if (strcmp(scheme, SCHEME_HTTP) == 0)
254 return (HTTP_DEFAULT_PORT);
259 * Return the default proxy port for a scheme
262 fetch_default_proxy_port(const char *scheme)
264 if (strcmp(scheme, SCHEME_FTP) == 0)
265 return (FTP_DEFAULT_PROXY_PORT);
266 if (strcmp(scheme, SCHEME_HTTP) == 0)
267 return (HTTP_DEFAULT_PROXY_PORT);
273 * Create a connection for an existing descriptor.
281 /* allocate and fill connection structure */
282 if ((conn = calloc(1, sizeof(*conn))) == NULL)
284 fcntl(sd, F_SETFD, FD_CLOEXEC);
285 setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
293 * Bump a connection's reference count.
296 fetch_ref(conn_t *conn)
308 fetch_resolve(const char *addr, int port, int af)
310 char hbuf[256], sbuf[8];
311 struct addrinfo hints, *res;
312 const char *hb, *he, *sep;
313 const char *host, *service;
316 /* first, check for a bracketed IPv6 address */
319 if ((sep = strchr(hb, ']')) == NULL) {
326 sep = strchrnul(hb, ':');
330 /* see if we need to copy the host name */
332 len = snprintf(hbuf, sizeof(hbuf),
333 "%.*s", (int)(he - hb), hb);
336 if (len >= (int)sizeof(hbuf)) {
337 errno = ENAMETOOLONG;
345 /* was it followed by a service name? */
346 if (*sep == '\0' && port != 0) {
347 if (port < 1 || port > 65535) {
351 if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0)
354 } else if (*sep != '\0') {
361 memset(&hints, 0, sizeof(hints));
362 hints.ai_family = af;
363 hints.ai_socktype = SOCK_STREAM;
364 hints.ai_flags = AI_ADDRCONFIG;
365 if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
377 * Bind a socket to a specific local address
380 fetch_bind(int sd, int af, const char *addr)
382 struct addrinfo *cliai, *ai;
385 if ((cliai = fetch_resolve(addr, 0, af)) == NULL)
387 for (ai = cliai; ai != NULL; ai = ai->ai_next)
388 if ((err = bind(sd, ai->ai_addr, ai->ai_addrlen)) == 0)
393 return (err == 0 ? 0 : -1);
398 * SOCKS5 connection initiation, based on RFC 1928
399 * Default DNS resolution over SOCKS5
402 fetch_socks5_init(conn_t *conn, const char *host, int port, int verbose)
405 * Size is based on largest packet prefix (4 bytes) +
406 * Largest FQDN (256) + one byte size (1) +
409 unsigned char buf[BUFF_SIZE];
414 fetch_info("Initializing SOCKS5 connection: %s:%d", host, port);
416 /* Connection initialization */
418 *ptr++ = SOCKS_VERSION_5;
419 *ptr++ = SOCKS_CONNECTION;
422 if (fetch_write(conn, buf, 3) != 3) {
423 ret = SOCKS5_ERR_SELECTION;
427 /* Verify response from SOCKS5 server */
428 if (fetch_read(conn, buf, 2) != 2) {
429 ret = SOCKS5_ERR_READ_METHOD;
434 if (ptr[0] != SOCKS_VERSION_5) {
435 ret = SOCKS5_ERR_VER5_ONLY;
438 if (ptr[1] == SOCKS_NOMETHODS) {
439 ret = SOCKS5_ERR_NOMETHODS;
442 else if (ptr[1] != SOCKS5_NOTIMPLEMENTED) {
443 ret = SOCKS5_ERR_NOTIMPLEMENTED;
448 *ptr++ = SOCKS_VERSION_5;
449 *ptr++ = SOCKS_CONNECTION;
451 /* Encode all targets as a hostname to avoid DNS leaks */
452 *ptr++ = SOCKS_ATYP_DOMAINNAME;
453 if (strlen(host) > FQDN_SIZE) {
454 ret = SOCKS5_ERR_HOSTNAME_SIZE;
457 *ptr++ = strlen(host);
458 strncpy(ptr, host, strlen(host));
459 ptr = ptr + strlen(host);
462 *ptr++ = port & 0x00ff;
463 *ptr++ = (port & 0xff00) >> 8;
465 if (fetch_write(conn, buf, ptr - buf) != ptr - buf) {
466 ret = SOCKS5_ERR_REQUEST;
470 /* BND.ADDR is variable length, read the largest on non-blocking socket */
471 if (!fetch_read(conn, buf, BUFF_SIZE)) {
472 ret = SOCKS5_ERR_REPLY;
477 if (*ptr++ != SOCKS_VERSION_5) {
478 ret = SOCKS5_ERR_NON_VER5_RESP;
485 case SOCKS_GENERAL_FAILURE:
486 ret = SOCKS5_ERR_GENERAL;
488 case SOCKS_CONNECTION_NOT_ALLOWED:
489 ret = SOCKS5_ERR_NOT_ALLOWED;
491 case SOCKS_NETWORK_UNREACHABLE:
492 ret = SOCKS5_ERR_NET_UNREACHABLE;
494 case SOCKS_HOST_UNREACHABLE:
495 ret = SOCKS5_ERR_HOST_UNREACHABLE;
497 case SOCKS_CONNECTION_REFUSED:
498 ret = SOCKS5_ERR_CONN_REFUSED;
500 case SOCKS_TTL_EXPIRED:
501 ret = SOCKS5_ERR_TTL_EXPIRED;
503 case SOCKS_COMMAND_NOT_SUPPORTED:
504 ret = SOCKS5_ERR_COM_UNSUPPORTED;
506 case SOCKS_ADDRESS_NOT_SUPPORTED:
507 ret = SOCKS5_ERR_ADDR_UNSUPPORTED;
510 ret = SOCKS5_ERR_UNSPECIFIED;
522 * Perform SOCKS5 initialization
525 fetch_socks5_getenv(char **host, int *port)
527 char *socks5env, *endptr, *ext;
528 const char *portDelim;
532 if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
539 * IPv6 addresses begin and end in brackets. Set the port delimiter
540 * accordingly and search for it so we can do appropriate validation.
542 if (socks5env[0] == '[')
545 slen = strlen(socks5env);
546 ext = strstr(socks5env, portDelim);
547 if (socks5env[0] == '[') {
548 if (socks5env[slen - 1] == ']') {
549 *host = strndup(socks5env, slen);
550 } else if (ext != NULL) {
551 *host = strndup(socks5env, ext - socks5env + 1);
553 socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
557 *host = strndup(socks5env, ext - socks5env);
561 fprintf(stderr, "Failure to allocate memory, exiting.\n");
565 *port = 1080; /* Default port as defined in RFC1928 */
567 ext += strlen(portDelim);
569 *port = strtoimax(ext, (char **)&endptr, 10);
570 if (*endptr != '\0' || errno != 0 || *port < 0 ||
574 socks5_seterr(SOCKS5_ERR_BAD_PORT);
584 * Establish a TCP connection to the specified port on the specified host.
587 fetch_connect(const char *host, int port, int af, int verbose)
589 struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
590 const char *bindaddr;
592 int err = 0, sd = -1;
596 DEBUGF("---> %s:%d\n", host, port);
599 * Check if SOCKS5_PROXY env variable is set. fetch_socks5_getenv
600 * will either set sockshost = NULL or allocate memory in all cases.
603 if (!fetch_socks5_getenv(&sockshost, &socksport))
606 /* Not using SOCKS5 proxy */
607 if (sockshost == NULL) {
608 /* resolve server address */
610 fetch_info("resolving server address: %s:%d", host,
612 if ((sais = fetch_resolve(host, port, af)) == NULL)
615 /* resolve client address */
616 bindaddr = getenv("FETCH_BIND_ADDRESS");
617 if (bindaddr != NULL && *bindaddr != '\0') {
619 fetch_info("resolving client address: %s",
621 if ((cais = fetch_resolve(bindaddr, 0, af)) == NULL)
625 /* resolve socks5 proxy address */
627 fetch_info("resolving SOCKS5 server address: %s:%d",
628 sockshost, socksport);
629 if ((sais = fetch_resolve(sockshost, socksport, af)) == NULL) {
630 socks5_seterr(SOCKS5_ERR_BAD_HOST);
635 /* try each server address in turn */
636 for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
638 if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0)
640 /* attempt to bind to client address */
641 for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
642 if (cai->ai_family != sai->ai_family)
644 if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
649 fetch_info("failed to bind to %s", bindaddr);
652 /* attempt to connect to server address */
653 if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0)
655 /* clean up before next attempt */
660 if (verbose && sockshost == NULL) {
661 fetch_info("failed to connect to %s:%d", host, port);
663 } else if (sockshost != NULL) {
666 "failed to connect to SOCKS5 server %s:%d",
667 sockshost, socksport);
668 socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
674 if ((conn = fetch_reopen(sd)) == NULL)
678 if (!fetch_socks5_init(conn, host, port, verbose))
690 /* Fully close if it was opened; otherwise just don't leak the fd. */
704 * Convert characters A-Z to lowercase (intentionally avoid any locale
705 * specific conversions).
708 fetch_ssl_tolower(char in)
710 if (in >= 'A' && in <= 'Z')
717 * isalpha implementation that intentionally avoids any locale specific
721 fetch_ssl_isalpha(char in)
723 return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
727 * Check if passed hostnames a and b are equal.
730 fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
737 for (i = 0; i < alen; ++i) {
738 if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
745 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
746 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
747 * numeric characters. Double hyphens (like they're found in IDN a-labels
748 * 'xn--') are not allowed. Empty labels are invalid.
751 fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
755 if (!len || l[0] == '-' || l[len-1] == '-')
757 for (i = 0; i < len; ++i) {
758 if (!isdigit(l[i]) &&
759 !fetch_ssl_isalpha(l[i]) &&
760 !(l[i] == '*' && wcok) &&
761 !(l[i] == '-' && l[i - 1] != '-'))
768 * Check if host name consists only of numbers. This might indicate an IP
769 * address, which is not a good idea for CN wildcard comparison.
772 fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
776 for (i = 0; i < len; ++i) {
777 if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
785 * Check if the host name h passed matches the pattern passed in m which
786 * is usually part of subjectAltName or CN of a certificate presented to
787 * the client. This includes wildcard matching. The algorithm is based on
788 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
791 fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
794 int delta, hdotidx, mdot1idx, wcidx;
795 const char *hdot, *mdot1, *mdot2;
796 const char *wc; /* wildcard */
798 if (!(h && *h && m && *m))
800 if ((wc = strnstr(m, "*", mlen)) == NULL)
801 return (fetch_ssl_hname_equal(h, hlen, m, mlen));
803 /* hostname should not be just dots and numbers */
804 if (fetch_ssl_hname_is_only_numbers(h, hlen))
806 /* only one wildcard allowed in pattern */
807 if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
810 * there must be at least two more domain labels and
811 * wildcard has to be in the leftmost label (RFC6125)
813 mdot1 = strnstr(m, ".", mlen);
814 if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
816 mdot1idx = mdot1 - m;
817 mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
818 if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
820 /* hostname must contain a dot and not be the 1st char */
821 hdot = strnstr(h, ".", hlen);
822 if (hdot == NULL || hdot == h)
826 * host part of hostname must be at least as long as
827 * pattern it's supposed to match
829 if (hdotidx < mdot1idx)
832 * don't allow wildcards in non-traditional domain names
833 * (IDN, A-label, U-label...)
835 if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
836 !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
838 /* match domain part (part after first dot) */
839 if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
842 /* match part left of wildcard */
843 if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
845 /* match part right of wildcard */
846 delta = mdot1idx - wcidx - 1;
847 if (!fetch_ssl_hname_equal(hdot - delta, delta,
848 mdot1 - delta, delta))
850 /* all tests succeeded, it's a match */
855 * Get numeric host address info - returns NULL if host was not an IP
856 * address. The caller is responsible for deallocation using
859 static struct addrinfo *
860 fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
862 struct addrinfo hints, *res;
865 host = (char *)malloc(len + 1);
866 memcpy(host, hostname, len);
868 memset(&hints, 0, sizeof(hints));
869 hints.ai_family = PF_UNSPEC;
870 hints.ai_socktype = SOCK_STREAM;
871 hints.ai_protocol = 0;
872 hints.ai_flags = AI_NUMERICHOST;
873 /* port is not relevant for this purpose */
874 if (getaddrinfo(host, "443", &hints, &res) != 0)
881 * Compare ip address in addrinfo with address passes.
884 fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
889 if (lhost->ai_family == AF_INET && rhostlen == 4) {
890 left = (void *)&((struct sockaddr_in*)(void *)
891 lhost->ai_addr)->sin_addr.s_addr;
893 } else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
894 left = (void *)&((struct sockaddr_in6 *)(void *)
895 lhost->ai_addr)->sin6_addr;
899 return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
903 * Compare ip address in addrinfo with host passed. If host is not an IP
904 * address, comparison will fail.
907 fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
910 struct addrinfo *raddr;
915 if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
916 return 0; /* not a numeric host */
918 if (laddr->ai_family == raddr->ai_family) {
919 if (laddr->ai_family == AF_INET) {
920 rip = (char *)&((struct sockaddr_in *)(void *)
921 raddr->ai_addr)->sin_addr.s_addr;
922 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
924 } else if (laddr->ai_family == AF_INET6) {
925 rip = (char *)&((struct sockaddr_in6 *)(void *)
926 raddr->ai_addr)->sin6_addr;
927 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
937 * Verify server certificate by subjectAltName.
940 fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
941 const char *host, struct addrinfo *ip)
943 const GENERAL_NAME *name;
948 for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
949 #if OPENSSL_VERSION_NUMBER < 0x10000000L
951 * This is a workaround, since the following line causes
952 * alignment issues in clang:
953 * name = sk_GENERAL_NAME_value(altnames, i);
954 * OpenSSL explicitly warns not to use those macros
955 * directly, but there isn't much choice (and there
956 * shouldn't be any ill side effects)
958 name = (GENERAL_NAME *)SKM_sk_value(void, altnames, i);
960 name = sk_GENERAL_NAME_value(altnames, i);
962 #if OPENSSL_VERSION_NUMBER < 0x10100000L
963 ns = (const char *)ASN1_STRING_data(name->d.ia5);
965 ns = (const char *)ASN1_STRING_get0_data(name->d.ia5);
967 nslen = (size_t)ASN1_STRING_length(name->d.ia5);
969 if (name->type == GEN_DNS && ip == NULL &&
970 fetch_ssl_hname_match(host, strlen(host), ns, nslen))
972 else if (name->type == GEN_IPADD && ip != NULL &&
973 fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
980 * Verify server certificate by CN.
983 fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
986 ASN1_STRING *namedata;
987 X509_NAME_ENTRY *nameentry;
988 int cnlen, lastpos, loc, ret;
995 /* get most specific CN (last entry in list) and compare */
996 while ((lastpos = X509_NAME_get_index_by_NID(subject,
997 NID_commonName, lastpos)) != -1)
1001 nameentry = X509_NAME_get_entry(subject, loc);
1002 namedata = X509_NAME_ENTRY_get_data(nameentry);
1003 cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
1005 fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
1007 else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
1015 * Verify that server certificate subjectAltName/CN matches
1016 * hostname. First check, if there are alternative subject names. If yes,
1017 * those have to match. Only if those don't exist it falls back to
1018 * checking the subject's CN.
1021 fetch_ssl_verify_hname(X509 *cert, const char *host)
1023 struct addrinfo *ip;
1024 STACK_OF(GENERAL_NAME) *altnames;
1029 ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
1030 altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
1033 if (altnames != NULL) {
1034 ret = fetch_ssl_verify_altname(altnames, host, ip);
1036 subject = X509_get_subject_name(cert);
1037 if (subject != NULL)
1038 ret = fetch_ssl_verify_cn(subject, host, ip);
1043 if (altnames != NULL)
1044 GENERAL_NAMES_free(altnames);
1049 * Configure transport security layer based on environment.
1052 fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
1054 long ssl_ctx_options;
1056 ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_SSLv2 | SSL_OP_NO_TICKET;
1057 if (getenv("SSL_ALLOW_SSL3") == NULL)
1058 ssl_ctx_options |= SSL_OP_NO_SSLv3;
1059 if (getenv("SSL_NO_TLS1") != NULL)
1060 ssl_ctx_options |= SSL_OP_NO_TLSv1;
1061 if (getenv("SSL_NO_TLS1_1") != NULL)
1062 ssl_ctx_options |= SSL_OP_NO_TLSv1_1;
1063 if (getenv("SSL_NO_TLS1_2") != NULL)
1064 ssl_ctx_options |= SSL_OP_NO_TLSv1_2;
1066 fetch_info("SSL options: %lx", ssl_ctx_options);
1067 SSL_CTX_set_options(ctx, ssl_ctx_options);
1072 * Configure peer verification based on environment.
1074 #define LOCAL_CERT_FILE "/usr/local/etc/ssl/cert.pem"
1075 #define BASE_CERT_FILE "/etc/ssl/cert.pem"
1077 fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
1079 X509_LOOKUP *crl_lookup;
1080 X509_STORE *crl_store;
1081 const char *ca_cert_file, *ca_cert_path, *crl_file;
1083 if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
1084 ca_cert_file = getenv("SSL_CA_CERT_FILE");
1085 if (ca_cert_file == NULL &&
1086 access(LOCAL_CERT_FILE, R_OK) == 0)
1087 ca_cert_file = LOCAL_CERT_FILE;
1088 if (ca_cert_file == NULL &&
1089 access(BASE_CERT_FILE, R_OK) == 0)
1090 ca_cert_file = BASE_CERT_FILE;
1091 ca_cert_path = getenv("SSL_CA_CERT_PATH");
1093 fetch_info("Peer verification enabled");
1094 if (ca_cert_file != NULL)
1095 fetch_info("Using CA cert file: %s",
1097 if (ca_cert_path != NULL)
1098 fetch_info("Using CA cert path: %s",
1100 if (ca_cert_file == NULL && ca_cert_path == NULL)
1101 fetch_info("Using OpenSSL default "
1102 "CA cert file and path");
1104 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
1105 fetch_ssl_cb_verify_crt);
1106 if (ca_cert_file != NULL || ca_cert_path != NULL)
1107 SSL_CTX_load_verify_locations(ctx, ca_cert_file,
1110 SSL_CTX_set_default_verify_paths(ctx);
1111 if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
1113 fetch_info("Using CRL file: %s", crl_file);
1114 crl_store = SSL_CTX_get_cert_store(ctx);
1115 crl_lookup = X509_STORE_add_lookup(crl_store,
1116 X509_LOOKUP_file());
1117 if (crl_lookup == NULL ||
1118 !X509_load_crl_file(crl_lookup, crl_file,
1119 X509_FILETYPE_PEM)) {
1121 "Could not load CRL file %s\n",
1125 X509_STORE_set_flags(crl_store,
1126 X509_V_FLAG_CRL_CHECK |
1127 X509_V_FLAG_CRL_CHECK_ALL);
1134 * Configure client certificate based on environment.
1137 fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
1139 const char *client_cert_file, *client_key_file;
1141 if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
1142 client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
1143 getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
1145 fetch_info("Using client cert file: %s",
1147 fetch_info("Using client key file: %s",
1150 if (SSL_CTX_use_certificate_chain_file(ctx,
1151 client_cert_file) != 1) {
1153 "Could not load client certificate %s\n",
1157 if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
1158 SSL_FILETYPE_PEM) != 1) {
1160 "Could not load client key %s\n",
1169 * Callback for SSL certificate verification, this is called on server
1170 * cert verification. It takes no decision, but informs the user in case
1171 * verification failed.
1174 fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
1182 if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
1183 (name = X509_get_subject_name(crt)) != NULL)
1184 str = X509_NAME_oneline(name, 0, 0);
1185 fprintf(stderr, "Certificate verification failed for %s\n",
1186 str != NULL ? str : "no relevant certificate");
1195 * Enable SSL on a connection.
1198 fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
1205 /* Init the SSL library and context */
1206 if (!SSL_library_init()){
1207 fprintf(stderr, "SSL library init failed\n");
1211 SSL_load_error_strings();
1213 conn->ssl_meth = SSLv23_client_method();
1214 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
1215 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
1217 fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
1218 if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
1220 if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
1223 conn->ssl = SSL_new(conn->ssl_ctx);
1224 if (conn->ssl == NULL) {
1225 fprintf(stderr, "SSL context creation failed\n");
1228 SSL_set_fd(conn->ssl, conn->sd);
1230 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
1231 if (!SSL_set_tlsext_host_name(conn->ssl,
1232 __DECONST(struct url *, URL)->host)) {
1234 "TLS server name indication extension failed for host %s\n",
1239 while ((ret = SSL_connect(conn->ssl)) == -1) {
1240 ssl_err = SSL_get_error(conn->ssl, ret);
1241 if (ssl_err != SSL_ERROR_WANT_READ &&
1242 ssl_err != SSL_ERROR_WANT_WRITE) {
1243 ERR_print_errors_fp(stderr);
1247 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
1249 if (conn->ssl_cert == NULL) {
1250 fprintf(stderr, "No server SSL certificate\n");
1254 if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
1256 fetch_info("Verify hostname");
1257 if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
1259 "SSL certificate subject doesn't match host %s\n",
1266 fetch_info("%s connection established using %s",
1267 SSL_get_version(conn->ssl), SSL_get_cipher(conn->ssl));
1268 name = X509_get_subject_name(conn->ssl_cert);
1269 str = X509_NAME_oneline(name, 0, 0);
1270 fetch_info("Certificate subject: %s", str);
1272 name = X509_get_issuer_name(conn->ssl_cert);
1273 str = X509_NAME_oneline(name, 0, 0);
1274 fetch_info("Certificate issuer: %s", str);
1283 fprintf(stderr, "SSL support disabled\n");
1288 #define FETCH_READ_WAIT -2
1289 #define FETCH_READ_ERROR -1
1290 #define FETCH_READ_DONE 0
1294 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
1299 rlen = SSL_read(ssl, buf, len);
1301 ssl_err = SSL_get_error(ssl, rlen);
1302 if (ssl_err == SSL_ERROR_WANT_READ ||
1303 ssl_err == SSL_ERROR_WANT_WRITE) {
1304 return (FETCH_READ_WAIT);
1306 ERR_print_errors_fp(stderr);
1307 return (FETCH_READ_ERROR);
1315 fetch_socket_read(int sd, char *buf, size_t len)
1319 rlen = read(sd, buf, len);
1321 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
1322 return (FETCH_READ_WAIT);
1324 return (FETCH_READ_ERROR);
1330 * Read a character from a connection w/ timeout
1333 fetch_read(conn_t *conn, char *buf, size_t len)
1335 struct timeval now, timeout, delta;
1340 if (fetchTimeout > 0) {
1341 gettimeofday(&timeout, NULL);
1342 timeout.tv_sec += fetchTimeout;
1346 memset(&pfd, 0, sizeof pfd);
1348 pfd.events = POLLIN | POLLERR;
1352 * The socket is non-blocking. Instead of the canonical
1353 * poll() -> read(), we do the following:
1355 * 1) call read() or SSL_read().
1356 * 2) if we received some data, return it.
1357 * 3) if an error occurred, return -1.
1358 * 4) if read() or SSL_read() signaled EOF, return.
1359 * 5) if we did not receive any data but we're not at EOF,
1362 * In the SSL case, this is necessary because if we
1363 * receive a close notification, we have to call
1364 * SSL_read() one additional time after we've read
1365 * everything we received.
1367 * In the non-SSL case, it may improve performance (very
1368 * slightly) when reading small amounts of data.
1371 if (conn->ssl != NULL)
1372 rlen = fetch_ssl_read(conn->ssl, buf, len);
1375 rlen = fetch_socket_read(conn->sd, buf, len);
1378 } else if (rlen == FETCH_READ_ERROR) {
1382 // assert(rlen == FETCH_READ_WAIT);
1383 if (fetchTimeout > 0) {
1384 gettimeofday(&now, NULL);
1385 if (!timercmp(&timeout, &now, >)) {
1390 timersub(&timeout, &now, &delta);
1391 deltams = delta.tv_sec * 1000 +
1392 delta.tv_usec / 1000;;
1396 if (poll(&pfd, 1, deltams) < 0) {
1397 if (errno == EINTR && fetchRestartCalls)
1408 * Read a line of text from a connection w/ timeout
1410 #define MIN_BUF_SIZE 1024
1413 fetch_getln(conn_t *conn)
1420 if (conn->buf == NULL) {
1421 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
1425 conn->bufsize = MIN_BUF_SIZE;
1428 conn->buf[0] = '\0';
1432 len = fetch_read(conn, &c, 1);
1437 conn->buf[conn->buflen++] = c;
1438 if (conn->buflen == conn->bufsize) {
1440 tmpsize = conn->bufsize * 2 + 1;
1441 if ((tmp = realloc(tmp, tmpsize)) == NULL) {
1446 conn->bufsize = tmpsize;
1448 } while (c != '\n');
1450 conn->buf[conn->buflen] = '\0';
1451 DEBUGF("<<< %s", conn->buf);
1457 * Write to a connection w/ timeout
1460 fetch_write(conn_t *conn, const char *buf, size_t len)
1464 iov.iov_base = __DECONST(char *, buf);
1466 return fetch_writev(conn, &iov, 1);
1470 * Write a vector to a connection w/ timeout
1471 * Note: can modify the iovec.
1474 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
1476 struct timeval now, timeout, delta;
1478 ssize_t wlen, total;
1481 memset(&pfd, 0, sizeof pfd);
1484 pfd.events = POLLOUT | POLLERR;
1485 gettimeofday(&timeout, NULL);
1486 timeout.tv_sec += fetchTimeout;
1490 while (iovcnt > 0) {
1491 while (fetchTimeout && pfd.revents == 0) {
1492 gettimeofday(&now, NULL);
1493 if (!timercmp(&timeout, &now, >)) {
1498 timersub(&timeout, &now, &delta);
1499 deltams = delta.tv_sec * 1000 +
1500 delta.tv_usec / 1000;
1503 if (poll(&pfd, 1, deltams) < 0) {
1504 /* POSIX compliance */
1505 if (errno == EAGAIN)
1507 if (errno == EINTR && fetchRestartCalls)
1514 if (conn->ssl != NULL)
1515 wlen = SSL_write(conn->ssl,
1516 iov->iov_base, iov->iov_len);
1519 wlen = writev(conn->sd, iov, iovcnt);
1521 /* we consider a short write a failure */
1522 /* XXX perhaps we shouldn't in the SSL case */
1528 if (errno == EINTR && fetchRestartCalls)
1533 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
1534 wlen -= iov->iov_len;
1539 iov->iov_len -= wlen;
1540 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
1548 * Write a line of text to a connection w/ timeout
1551 fetch_putln(conn_t *conn, const char *str, size_t len)
1553 struct iovec iov[2];
1556 DEBUGF(">>> %s\n", str);
1557 iov[0].iov_base = __DECONST(char *, str);
1558 iov[0].iov_len = len;
1559 iov[1].iov_base = __DECONST(char *, ENDL);
1560 iov[1].iov_len = sizeof(ENDL);
1562 ret = fetch_writev(conn, &iov[1], 1);
1564 ret = fetch_writev(conn, iov, 2);
1575 fetch_close(conn_t *conn)
1579 if (--conn->ref > 0)
1583 SSL_shutdown(conn->ssl);
1584 SSL_set_connect_state(conn->ssl);
1585 SSL_free(conn->ssl);
1588 if (conn->ssl_ctx) {
1589 SSL_CTX_free(conn->ssl_ctx);
1590 conn->ssl_ctx = NULL;
1592 if (conn->ssl_cert) {
1593 X509_free(conn->ssl_cert);
1594 conn->ssl_cert = NULL;
1597 ret = close(conn->sd);
1604 /*** Directory-related utility functions *************************************/
1607 fetch_add_entry(struct url_ent **p, int *size, int *len,
1608 const char *name, struct url_stat *us)
1610 struct url_ent *tmp;
1617 if (*len >= *size - 1) {
1618 tmp = reallocarray(*p, *size * 2 + 1, sizeof(**p));
1624 *size = (*size * 2 + 1);
1629 snprintf(tmp->name, PATH_MAX, "%s", name);
1630 memcpy(&tmp->stat, us, sizeof(*us));
1633 (++tmp)->name[0] = 0;
1639 /*** Authentication-related utility functions ********************************/
1642 fetch_read_word(FILE *f)
1644 static char word[1024];
1646 if (fscanf(f, " %1023s ", word) != 1)
1652 fetch_netrc_open(void)
1659 if ((p = getenv("NETRC")) != NULL) {
1660 DEBUGF("NETRC=%s\n", p);
1661 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
1662 fetch_info("$NETRC specifies a file name "
1663 "longer than PATH_MAX");
1667 if ((p = getenv("HOME")) == NULL) {
1668 if ((pwd = getpwuid(getuid())) == NULL ||
1669 (p = pwd->pw_dir) == NULL)
1672 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
1676 if ((fd = open(fn, O_RDONLY)) < 0) {
1678 DEBUGF("%s: %s\n", fn, strerror(serrno));
1685 * Get authentication data for a URL from .netrc
1688 fetch_netrc_auth(struct url *url)
1694 if (url->netrcfd < 0)
1695 url->netrcfd = fetch_netrc_open();
1696 if (url->netrcfd < 0)
1698 if ((f = fdopen(url->netrcfd, "r")) == NULL) {
1700 DEBUGF("fdopen(netrcfd): %s", strerror(errno));
1701 close(url->netrcfd);
1707 DEBUGF("searching netrc for %s\n", url->host);
1708 while ((word = fetch_read_word(f)) != NULL) {
1709 if (strcmp(word, "default") == 0) {
1710 DEBUGF("using default netrc settings\n");
1713 if (strcmp(word, "machine") == 0 &&
1714 (word = fetch_read_word(f)) != NULL &&
1715 strcasecmp(word, url->host) == 0) {
1716 DEBUGF("using netrc settings for %s\n", word);
1722 while ((word = fetch_read_word(f)) != NULL) {
1723 if (strcmp(word, "login") == 0) {
1724 if ((word = fetch_read_word(f)) == NULL)
1726 if (snprintf(url->user, sizeof(url->user),
1727 "%s", word) > (int)sizeof(url->user)) {
1728 fetch_info("login name in .netrc is too long");
1729 url->user[0] = '\0';
1731 } else if (strcmp(word, "password") == 0) {
1732 if ((word = fetch_read_word(f)) == NULL)
1734 if (snprintf(url->pwd, sizeof(url->pwd),
1735 "%s", word) > (int)sizeof(url->pwd)) {
1736 fetch_info("password in .netrc is too long");
1739 } else if (strcmp(word, "account") == 0) {
1740 if ((word = fetch_read_word(f)) == NULL)
1742 /* XXX not supported! */
1759 * The no_proxy environment variable specifies a set of domains for
1760 * which the proxy should not be consulted; the contents is a comma-,
1761 * or space-separated list of domain names. A single asterisk will
1762 * override all proxy variables and no transactions will be proxied
1763 * (for compatibility with lynx and curl, see the discussion at
1764 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
1767 fetch_no_proxy_match(const char *host)
1769 const char *no_proxy, *p, *q;
1770 size_t h_len, d_len;
1772 if ((no_proxy = getenv("NO_PROXY")) == NULL &&
1773 (no_proxy = getenv("no_proxy")) == NULL)
1776 /* asterisk matches any hostname */
1777 if (strcmp(no_proxy, "*") == 0)
1780 h_len = strlen(host);
1783 /* position p at the beginning of a domain suffix */
1784 while (*p == ',' || isspace((unsigned char)*p))
1787 /* position q at the first separator character */
1788 for (q = p; *q; ++q)
1789 if (*q == ',' || isspace((unsigned char)*q))
1793 if (d_len > 0 && h_len >= d_len &&
1794 strncasecmp(host + h_len - d_len,
1796 /* domain name matches */