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>
57 #include <openssl/x509v3.h>
64 /*** Local data **************************************************************/
67 * Error messages for resolver errors
69 static struct fetcherr netdb_errlist[] = {
71 { EAI_NODATA, FETCH_RESOLV, "Host not found" },
73 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" },
74 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" },
75 { EAI_NONAME, FETCH_RESOLV, "No address record" },
76 { -1, FETCH_UNKNOWN, "Unknown resolver error" }
80 * SOCKS5 error enumerations
85 SOCKS5_ERR_READ_METHOD,
88 SOCKS5_ERR_NOTIMPLEMENTED,
89 SOCKS5_ERR_HOSTNAME_SIZE,
92 SOCKS5_ERR_NON_VER5_RESP,
94 SOCKS5_ERR_NOT_ALLOWED,
95 SOCKS5_ERR_NET_UNREACHABLE,
96 SOCKS5_ERR_HOST_UNREACHABLE,
97 SOCKS5_ERR_CONN_REFUSED,
98 SOCKS5_ERR_TTL_EXPIRED,
99 SOCKS5_ERR_COM_UNSUPPORTED,
100 SOCKS5_ERR_ADDR_UNSUPPORTED,
101 SOCKS5_ERR_UNSPECIFIED,
102 /* Configuration errors */
104 SOCKS5_ERR_BAD_PROXY_FORMAT,
109 * Error messages for SOCKS5 errors
111 static struct fetcherr socks5_errlist[] = {
112 /* SOCKS5 protocol errors */
113 { SOCKS5_ERR_SELECTION, FETCH_ABORT, "SOCKS5: Failed to send selection method" },
114 { SOCKS5_ERR_READ_METHOD, FETCH_ABORT, "SOCKS5: Failed to read method" },
115 { SOCKS5_ERR_VER5_ONLY, FETCH_PROTO, "SOCKS5: Only version 5 is implemented" },
116 { SOCKS5_ERR_NOMETHODS, FETCH_PROTO, "SOCKS5: No acceptable methods" },
117 { SOCKS5_ERR_NOTIMPLEMENTED, FETCH_PROTO, "SOCKS5: Method currently not implemented" },
118 { SOCKS5_ERR_HOSTNAME_SIZE, FETCH_PROTO, "SOCKS5: Hostname size is above 256 bytes" },
119 { SOCKS5_ERR_REQUEST, FETCH_PROTO, "SOCKS5: Failed to request" },
120 { SOCKS5_ERR_REPLY, FETCH_PROTO, "SOCKS5: Failed to receive reply" },
121 { SOCKS5_ERR_NON_VER5_RESP, FETCH_PROTO, "SOCKS5: Server responded with a non-version 5 response" },
122 { SOCKS5_ERR_GENERAL, FETCH_ABORT, "SOCKS5: General server failure" },
123 { SOCKS5_ERR_NOT_ALLOWED, FETCH_AUTH, "SOCKS5: Connection not allowed by ruleset" },
124 { SOCKS5_ERR_NET_UNREACHABLE, FETCH_NETWORK, "SOCKS5: Network unreachable" },
125 { SOCKS5_ERR_HOST_UNREACHABLE, FETCH_ABORT, "SOCKS5: Host unreachable" },
126 { SOCKS5_ERR_CONN_REFUSED, FETCH_ABORT, "SOCKS5: Connection refused" },
127 { SOCKS5_ERR_TTL_EXPIRED, FETCH_TIMEOUT, "SOCKS5: TTL expired" },
128 { SOCKS5_ERR_COM_UNSUPPORTED, FETCH_PROTO, "SOCKS5: Command not supported" },
129 { SOCKS5_ERR_ADDR_UNSUPPORTED, FETCH_ABORT, "SOCKS5: Address type not supported" },
130 { SOCKS5_ERR_UNSPECIFIED, FETCH_UNKNOWN, "SOCKS5: Unspecified error" },
131 /* Configuration error */
132 { SOCKS5_ERR_BAD_HOST, FETCH_ABORT, "SOCKS5: Bad proxy host" },
133 { SOCKS5_ERR_BAD_PROXY_FORMAT, FETCH_ABORT, "SOCKS5: Bad proxy format" },
134 { SOCKS5_ERR_BAD_PORT, FETCH_ABORT, "SOCKS5: Bad port" }
138 static const char ENDL[2] = "\r\n";
141 /*** Error-reporting functions ***********************************************/
144 * Map error code to string
146 static struct fetcherr *
147 fetch_finderr(struct fetcherr *p, int e)
149 while (p->num != -1 && p->num != e)
158 fetch_seterr(struct fetcherr *p, int e)
160 p = fetch_finderr(p, e);
161 fetchLastErrCode = p->cat;
162 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string);
166 * Set error code according to errno
173 fetchLastErrCode = FETCH_OK;
180 fetchLastErrCode = FETCH_AUTH;
183 case EISDIR: /* XXX */
184 fetchLastErrCode = FETCH_UNAVAIL;
187 fetchLastErrCode = FETCH_MEMORY;
191 fetchLastErrCode = FETCH_TEMP;
194 fetchLastErrCode = FETCH_EXISTS;
197 fetchLastErrCode = FETCH_FULL;
205 fetchLastErrCode = FETCH_NETWORK;
209 fetchLastErrCode = FETCH_ABORT;
212 fetchLastErrCode = FETCH_TIMEOUT;
216 fetchLastErrCode = FETCH_DOWN;
219 fetchLastErrCode = FETCH_UNKNOWN;
221 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno));
226 * Emit status message
229 fetch_info(const char *fmt, ...)
234 vfprintf(stderr, fmt, ap);
240 /*** Network-related utility functions ***************************************/
243 * Return the default port for a scheme
246 fetch_default_port(const char *scheme)
250 if ((se = getservbyname(scheme, "tcp")) != NULL)
251 return (ntohs(se->s_port));
252 if (strcmp(scheme, SCHEME_FTP) == 0)
253 return (FTP_DEFAULT_PORT);
254 if (strcmp(scheme, SCHEME_HTTP) == 0)
255 return (HTTP_DEFAULT_PORT);
260 * Return the default proxy port for a scheme
263 fetch_default_proxy_port(const char *scheme)
265 if (strcmp(scheme, SCHEME_FTP) == 0)
266 return (FTP_DEFAULT_PROXY_PORT);
267 if (strcmp(scheme, SCHEME_HTTP) == 0)
268 return (HTTP_DEFAULT_PROXY_PORT);
274 * Create a connection for an existing descriptor.
282 /* allocate and fill connection structure */
283 if ((conn = calloc(1, sizeof(*conn))) == NULL)
285 fcntl(sd, F_SETFD, FD_CLOEXEC);
286 setsockopt(sd, SOL_SOCKET, SO_NOSIGPIPE, &opt, sizeof opt);
294 * Bump a connection's reference count.
297 fetch_ref(conn_t *conn)
309 fetch_resolve(const char *addr, int port, int af)
311 char hbuf[256], sbuf[8];
312 struct addrinfo hints, *res;
313 const char *hb, *he, *sep;
314 const char *host, *service;
317 /* first, check for a bracketed IPv6 address */
320 if ((sep = strchr(hb, ']')) == NULL) {
327 sep = strchrnul(hb, ':');
331 /* see if we need to copy the host name */
333 len = snprintf(hbuf, sizeof(hbuf),
334 "%.*s", (int)(he - hb), hb);
337 if (len >= (int)sizeof(hbuf)) {
338 errno = ENAMETOOLONG;
346 /* was it followed by a service name? */
347 if (*sep == '\0' && port != 0) {
348 if (port < 1 || port > 65535) {
352 if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0)
355 } else if (*sep != '\0') {
362 memset(&hints, 0, sizeof(hints));
363 hints.ai_family = af;
364 hints.ai_socktype = SOCK_STREAM;
365 hints.ai_flags = AI_ADDRCONFIG;
366 if ((err = getaddrinfo(host, service, &hints, &res)) != 0) {
378 * Bind a socket to a specific local address
381 fetch_bind(int sd, int af, const char *addr)
383 struct addrinfo *cliai, *ai;
386 if ((cliai = fetch_resolve(addr, 0, af)) == NULL)
388 for (ai = cliai; ai != NULL; ai = ai->ai_next)
389 if ((err = bind(sd, ai->ai_addr, ai->ai_addrlen)) == 0)
394 return (err == 0 ? 0 : -1);
399 * SOCKS5 connection initiation, based on RFC 1928
400 * Default DNS resolution over SOCKS5
403 fetch_socks5_init(conn_t *conn, const char *host, int port, int verbose)
406 * Size is based on largest packet prefix (4 bytes) +
407 * Largest FQDN (256) + one byte size (1) +
410 unsigned char buf[BUFF_SIZE];
415 fetch_info("Initializing SOCKS5 connection: %s:%d", host, port);
417 /* Connection initialization */
419 *ptr++ = SOCKS_VERSION_5;
420 *ptr++ = SOCKS_CONNECTION;
423 if (fetch_write(conn, buf, 3) != 3) {
424 ret = SOCKS5_ERR_SELECTION;
428 /* Verify response from SOCKS5 server */
429 if (fetch_read(conn, buf, 2) != 2) {
430 ret = SOCKS5_ERR_READ_METHOD;
435 if (ptr[0] != SOCKS_VERSION_5) {
436 ret = SOCKS5_ERR_VER5_ONLY;
439 if (ptr[1] == SOCKS_NOMETHODS) {
440 ret = SOCKS5_ERR_NOMETHODS;
443 else if (ptr[1] != SOCKS5_NOTIMPLEMENTED) {
444 ret = SOCKS5_ERR_NOTIMPLEMENTED;
449 *ptr++ = SOCKS_VERSION_5;
450 *ptr++ = SOCKS_CONNECTION;
452 /* Encode all targets as a hostname to avoid DNS leaks */
453 *ptr++ = SOCKS_ATYP_DOMAINNAME;
454 if (strlen(host) > FQDN_SIZE) {
455 ret = SOCKS5_ERR_HOSTNAME_SIZE;
458 *ptr++ = strlen(host);
459 strncpy(ptr, host, strlen(host));
460 ptr = ptr + strlen(host);
463 *ptr++ = port & 0x00ff;
464 *ptr++ = (port & 0xff00) >> 8;
466 if (fetch_write(conn, buf, ptr - buf) != ptr - buf) {
467 ret = SOCKS5_ERR_REQUEST;
471 /* BND.ADDR is variable length, read the largest on non-blocking socket */
472 if (!fetch_read(conn, buf, BUFF_SIZE)) {
473 ret = SOCKS5_ERR_REPLY;
478 if (*ptr++ != SOCKS_VERSION_5) {
479 ret = SOCKS5_ERR_NON_VER5_RESP;
486 case SOCKS_GENERAL_FAILURE:
487 ret = SOCKS5_ERR_GENERAL;
489 case SOCKS_CONNECTION_NOT_ALLOWED:
490 ret = SOCKS5_ERR_NOT_ALLOWED;
492 case SOCKS_NETWORK_UNREACHABLE:
493 ret = SOCKS5_ERR_NET_UNREACHABLE;
495 case SOCKS_HOST_UNREACHABLE:
496 ret = SOCKS5_ERR_HOST_UNREACHABLE;
498 case SOCKS_CONNECTION_REFUSED:
499 ret = SOCKS5_ERR_CONN_REFUSED;
501 case SOCKS_TTL_EXPIRED:
502 ret = SOCKS5_ERR_TTL_EXPIRED;
504 case SOCKS_COMMAND_NOT_SUPPORTED:
505 ret = SOCKS5_ERR_COM_UNSUPPORTED;
507 case SOCKS_ADDRESS_NOT_SUPPORTED:
508 ret = SOCKS5_ERR_ADDR_UNSUPPORTED;
511 ret = SOCKS5_ERR_UNSPECIFIED;
523 * Perform SOCKS5 initialization
526 fetch_socks5_getenv(char **host, int *port)
528 char *socks5env, *endptr, *ext;
529 const char *portDelim;
533 if ((socks5env = getenv("SOCKS5_PROXY")) == NULL || *socks5env == '\0') {
540 * IPv6 addresses begin and end in brackets. Set the port delimiter
541 * accordingly and search for it so we can do appropriate validation.
543 if (socks5env[0] == '[')
546 slen = strlen(socks5env);
547 ext = strstr(socks5env, portDelim);
548 if (socks5env[0] == '[') {
549 if (socks5env[slen - 1] == ']') {
550 *host = strndup(socks5env, slen);
551 } else if (ext != NULL) {
552 *host = strndup(socks5env, ext - socks5env + 1);
554 socks5_seterr(SOCKS5_ERR_BAD_PROXY_FORMAT);
558 *host = strndup(socks5env, ext - socks5env);
562 fprintf(stderr, "Failure to allocate memory, exiting.\n");
566 *port = 1080; /* Default port as defined in RFC1928 */
568 ext += strlen(portDelim);
570 *port = strtoimax(ext, (char **)&endptr, 10);
571 if (*endptr != '\0' || errno != 0 || *port < 0 ||
575 socks5_seterr(SOCKS5_ERR_BAD_PORT);
585 * Establish a TCP connection to the specified port on the specified host.
588 fetch_connect(const char *host, int port, int af, int verbose)
590 struct addrinfo *cais = NULL, *sais = NULL, *cai, *sai;
591 const char *bindaddr;
593 int err = 0, sd = -1;
597 DEBUGF("---> %s:%d\n", host, port);
600 * Check if SOCKS5_PROXY env variable is set. fetch_socks5_getenv
601 * will either set sockshost = NULL or allocate memory in all cases.
604 if (!fetch_socks5_getenv(&sockshost, &socksport))
607 /* Not using SOCKS5 proxy */
608 if (sockshost == NULL) {
609 /* resolve server address */
611 fetch_info("resolving server address: %s:%d", host,
613 if ((sais = fetch_resolve(host, port, af)) == NULL)
616 /* resolve client address */
617 bindaddr = getenv("FETCH_BIND_ADDRESS");
618 if (bindaddr != NULL && *bindaddr != '\0') {
620 fetch_info("resolving client address: %s",
622 if ((cais = fetch_resolve(bindaddr, 0, af)) == NULL)
626 /* resolve socks5 proxy address */
628 fetch_info("resolving SOCKS5 server address: %s:%d",
629 sockshost, socksport);
630 if ((sais = fetch_resolve(sockshost, socksport, af)) == NULL) {
631 socks5_seterr(SOCKS5_ERR_BAD_HOST);
636 /* try each server address in turn */
637 for (err = 0, sai = sais; sai != NULL; sai = sai->ai_next) {
639 if ((sd = socket(sai->ai_family, SOCK_STREAM, 0)) < 0)
641 /* attempt to bind to client address */
642 for (err = 0, cai = cais; cai != NULL; cai = cai->ai_next) {
643 if (cai->ai_family != sai->ai_family)
645 if ((err = bind(sd, cai->ai_addr, cai->ai_addrlen)) == 0)
650 fetch_info("failed to bind to %s", bindaddr);
653 /* attempt to connect to server address */
654 if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0)
656 /* clean up before next attempt */
661 if (verbose && sockshost == NULL) {
662 fetch_info("failed to connect to %s:%d", host, port);
664 } else if (sockshost != NULL) {
667 "failed to connect to SOCKS5 server %s:%d",
668 sockshost, socksport);
669 socks5_seterr(SOCKS5_ERR_CONN_REFUSED);
675 if ((conn = fetch_reopen(sd)) == NULL)
679 if (!fetch_socks5_init(conn, host, port, verbose))
691 /* Fully close if it was opened; otherwise just don't leak the fd. */
705 * Convert characters A-Z to lowercase (intentionally avoid any locale
706 * specific conversions).
709 fetch_ssl_tolower(char in)
711 if (in >= 'A' && in <= 'Z')
718 * isalpha implementation that intentionally avoids any locale specific
722 fetch_ssl_isalpha(char in)
724 return ((in >= 'A' && in <= 'Z') || (in >= 'a' && in <= 'z'));
728 * Check if passed hostnames a and b are equal.
731 fetch_ssl_hname_equal(const char *a, size_t alen, const char *b,
738 for (i = 0; i < alen; ++i) {
739 if (fetch_ssl_tolower(a[i]) != fetch_ssl_tolower(b[i]))
746 * Check if domain label is traditional, meaning that only A-Z, a-z, 0-9
747 * and '-' (hyphen) are allowed. Hyphens have to be surrounded by alpha-
748 * numeric characters. Double hyphens (like they're found in IDN a-labels
749 * 'xn--') are not allowed. Empty labels are invalid.
752 fetch_ssl_is_trad_domain_label(const char *l, size_t len, int wcok)
756 if (!len || l[0] == '-' || l[len-1] == '-')
758 for (i = 0; i < len; ++i) {
759 if (!isdigit(l[i]) &&
760 !fetch_ssl_isalpha(l[i]) &&
761 !(l[i] == '*' && wcok) &&
762 !(l[i] == '-' && l[i - 1] != '-'))
769 * Check if host name consists only of numbers. This might indicate an IP
770 * address, which is not a good idea for CN wildcard comparison.
773 fetch_ssl_hname_is_only_numbers(const char *hostname, size_t len)
777 for (i = 0; i < len; ++i) {
778 if (!((hostname[i] >= '0' && hostname[i] <= '9') ||
786 * Check if the host name h passed matches the pattern passed in m which
787 * is usually part of subjectAltName or CN of a certificate presented to
788 * the client. This includes wildcard matching. The algorithm is based on
789 * RFC6125, sections 6.4.3 and 7.2, which clarifies RFC2818 and RFC3280.
792 fetch_ssl_hname_match(const char *h, size_t hlen, const char *m,
795 int delta, hdotidx, mdot1idx, wcidx;
796 const char *hdot, *mdot1, *mdot2;
797 const char *wc; /* wildcard */
799 if (!(h && *h && m && *m))
801 if ((wc = strnstr(m, "*", mlen)) == NULL)
802 return (fetch_ssl_hname_equal(h, hlen, m, mlen));
804 /* hostname should not be just dots and numbers */
805 if (fetch_ssl_hname_is_only_numbers(h, hlen))
807 /* only one wildcard allowed in pattern */
808 if (strnstr(wc + 1, "*", mlen - wcidx - 1) != NULL)
811 * there must be at least two more domain labels and
812 * wildcard has to be in the leftmost label (RFC6125)
814 mdot1 = strnstr(m, ".", mlen);
815 if (mdot1 == NULL || mdot1 < wc || (mlen - (mdot1 - m)) < 4)
817 mdot1idx = mdot1 - m;
818 mdot2 = strnstr(mdot1 + 1, ".", mlen - mdot1idx - 1);
819 if (mdot2 == NULL || (mlen - (mdot2 - m)) < 2)
821 /* hostname must contain a dot and not be the 1st char */
822 hdot = strnstr(h, ".", hlen);
823 if (hdot == NULL || hdot == h)
827 * host part of hostname must be at least as long as
828 * pattern it's supposed to match
830 if (hdotidx < mdot1idx)
833 * don't allow wildcards in non-traditional domain names
834 * (IDN, A-label, U-label...)
836 if (!fetch_ssl_is_trad_domain_label(h, hdotidx, 0) ||
837 !fetch_ssl_is_trad_domain_label(m, mdot1idx, 1))
839 /* match domain part (part after first dot) */
840 if (!fetch_ssl_hname_equal(hdot, hlen - hdotidx, mdot1,
843 /* match part left of wildcard */
844 if (!fetch_ssl_hname_equal(h, wcidx, m, wcidx))
846 /* match part right of wildcard */
847 delta = mdot1idx - wcidx - 1;
848 if (!fetch_ssl_hname_equal(hdot - delta, delta,
849 mdot1 - delta, delta))
851 /* all tests succeeded, it's a match */
856 * Get numeric host address info - returns NULL if host was not an IP
857 * address. The caller is responsible for deallocation using
860 static struct addrinfo *
861 fetch_ssl_get_numeric_addrinfo(const char *hostname, size_t len)
863 struct addrinfo hints, *res;
866 host = (char *)malloc(len + 1);
867 memcpy(host, hostname, len);
869 memset(&hints, 0, sizeof(hints));
870 hints.ai_family = PF_UNSPEC;
871 hints.ai_socktype = SOCK_STREAM;
872 hints.ai_protocol = 0;
873 hints.ai_flags = AI_NUMERICHOST;
874 /* port is not relevant for this purpose */
875 if (getaddrinfo(host, "443", &hints, &res) != 0)
882 * Compare ip address in addrinfo with address passes.
885 fetch_ssl_ipaddr_match_bin(const struct addrinfo *lhost, const char *rhost,
890 if (lhost->ai_family == AF_INET && rhostlen == 4) {
891 left = (void *)&((struct sockaddr_in*)(void *)
892 lhost->ai_addr)->sin_addr.s_addr;
894 } else if (lhost->ai_family == AF_INET6 && rhostlen == 16) {
895 left = (void *)&((struct sockaddr_in6 *)(void *)
896 lhost->ai_addr)->sin6_addr;
900 return (!memcmp(left, (const void *)rhost, rhostlen) ? 1 : 0);
904 * Compare ip address in addrinfo with host passed. If host is not an IP
905 * address, comparison will fail.
908 fetch_ssl_ipaddr_match(const struct addrinfo *laddr, const char *r,
911 struct addrinfo *raddr;
916 if ((raddr = fetch_ssl_get_numeric_addrinfo(r, rlen)) == NULL)
917 return 0; /* not a numeric host */
919 if (laddr->ai_family == raddr->ai_family) {
920 if (laddr->ai_family == AF_INET) {
921 rip = (char *)&((struct sockaddr_in *)(void *)
922 raddr->ai_addr)->sin_addr.s_addr;
923 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 4);
925 } else if (laddr->ai_family == AF_INET6) {
926 rip = (char *)&((struct sockaddr_in6 *)(void *)
927 raddr->ai_addr)->sin6_addr;
928 ret = fetch_ssl_ipaddr_match_bin(laddr, rip, 16);
938 * Verify server certificate by subjectAltName.
941 fetch_ssl_verify_altname(STACK_OF(GENERAL_NAME) *altnames,
942 const char *host, struct addrinfo *ip)
944 const GENERAL_NAME *name;
949 for (i = 0; i < sk_GENERAL_NAME_num(altnames); ++i) {
950 #if OPENSSL_VERSION_NUMBER < 0x10000000L
952 * This is a workaround, since the following line causes
953 * alignment issues in clang:
954 * name = sk_GENERAL_NAME_value(altnames, i);
955 * OpenSSL explicitly warns not to use those macros
956 * directly, but there isn't much choice (and there
957 * shouldn't be any ill side effects)
959 name = (GENERAL_NAME *)SKM_sk_value(void, altnames, i);
961 name = sk_GENERAL_NAME_value(altnames, i);
963 #if OPENSSL_VERSION_NUMBER < 0x10100000L
964 ns = (const char *)ASN1_STRING_data(name->d.ia5);
966 ns = (const char *)ASN1_STRING_get0_data(name->d.ia5);
968 nslen = (size_t)ASN1_STRING_length(name->d.ia5);
970 if (name->type == GEN_DNS && ip == NULL &&
971 fetch_ssl_hname_match(host, strlen(host), ns, nslen))
973 else if (name->type == GEN_IPADD && ip != NULL &&
974 fetch_ssl_ipaddr_match_bin(ip, ns, nslen))
981 * Verify server certificate by CN.
984 fetch_ssl_verify_cn(X509_NAME *subject, const char *host,
987 ASN1_STRING *namedata;
988 X509_NAME_ENTRY *nameentry;
989 int cnlen, lastpos, loc, ret;
996 /* get most specific CN (last entry in list) and compare */
997 while ((lastpos = X509_NAME_get_index_by_NID(subject,
998 NID_commonName, lastpos)) != -1)
1002 nameentry = X509_NAME_get_entry(subject, loc);
1003 namedata = X509_NAME_ENTRY_get_data(nameentry);
1004 cnlen = ASN1_STRING_to_UTF8(&cn, namedata);
1006 fetch_ssl_hname_match(host, strlen(host), cn, cnlen))
1008 else if (ip != NULL && fetch_ssl_ipaddr_match(ip, cn, cnlen))
1016 * Verify that server certificate subjectAltName/CN matches
1017 * hostname. First check, if there are alternative subject names. If yes,
1018 * those have to match. Only if those don't exist it falls back to
1019 * checking the subject's CN.
1022 fetch_ssl_verify_hname(X509 *cert, const char *host)
1024 struct addrinfo *ip;
1025 STACK_OF(GENERAL_NAME) *altnames;
1030 ip = fetch_ssl_get_numeric_addrinfo(host, strlen(host));
1031 altnames = X509_get_ext_d2i(cert, NID_subject_alt_name,
1034 if (altnames != NULL) {
1035 ret = fetch_ssl_verify_altname(altnames, host, ip);
1037 subject = X509_get_subject_name(cert);
1038 if (subject != NULL)
1039 ret = fetch_ssl_verify_cn(subject, host, ip);
1044 if (altnames != NULL)
1045 GENERAL_NAMES_free(altnames);
1050 * Configure transport security layer based on environment.
1053 fetch_ssl_setup_transport_layer(SSL_CTX *ctx, int verbose)
1055 long ssl_ctx_options;
1057 ssl_ctx_options = SSL_OP_ALL | SSL_OP_NO_SSLv3 | SSL_OP_NO_TICKET;
1058 if (getenv("SSL_NO_TLS1") != NULL)
1059 ssl_ctx_options |= SSL_OP_NO_TLSv1;
1060 if (getenv("SSL_NO_TLS1_1") != NULL)
1061 ssl_ctx_options |= SSL_OP_NO_TLSv1_1;
1062 if (getenv("SSL_NO_TLS1_2") != NULL)
1063 ssl_ctx_options |= SSL_OP_NO_TLSv1_2;
1065 fetch_info("SSL options: %lx", ssl_ctx_options);
1066 SSL_CTX_set_options(ctx, ssl_ctx_options);
1071 * Configure peer verification based on environment.
1073 #define LOCAL_CERT_FILE _PATH_LOCALBASE "/etc/ssl/cert.pem"
1074 #define BASE_CERT_FILE "/etc/ssl/cert.pem"
1076 fetch_ssl_setup_peer_verification(SSL_CTX *ctx, int verbose)
1078 X509_LOOKUP *crl_lookup;
1079 X509_STORE *crl_store;
1080 const char *ca_cert_file, *ca_cert_path, *crl_file;
1082 if (getenv("SSL_NO_VERIFY_PEER") == NULL) {
1083 ca_cert_file = getenv("SSL_CA_CERT_FILE");
1084 if (ca_cert_file == NULL &&
1085 access(LOCAL_CERT_FILE, R_OK) == 0)
1086 ca_cert_file = LOCAL_CERT_FILE;
1087 if (ca_cert_file == NULL &&
1088 access(BASE_CERT_FILE, R_OK) == 0)
1089 ca_cert_file = BASE_CERT_FILE;
1090 ca_cert_path = getenv("SSL_CA_CERT_PATH");
1092 fetch_info("Peer verification enabled");
1093 if (ca_cert_file != NULL)
1094 fetch_info("Using CA cert file: %s",
1096 if (ca_cert_path != NULL)
1097 fetch_info("Using CA cert path: %s",
1099 if (ca_cert_file == NULL && ca_cert_path == NULL)
1100 fetch_info("Using OpenSSL default "
1101 "CA cert file and path");
1103 SSL_CTX_set_verify(ctx, SSL_VERIFY_PEER,
1104 fetch_ssl_cb_verify_crt);
1105 if (ca_cert_file != NULL || ca_cert_path != NULL)
1106 SSL_CTX_load_verify_locations(ctx, ca_cert_file,
1109 SSL_CTX_set_default_verify_paths(ctx);
1110 if ((crl_file = getenv("SSL_CRL_FILE")) != NULL) {
1112 fetch_info("Using CRL file: %s", crl_file);
1113 crl_store = SSL_CTX_get_cert_store(ctx);
1114 crl_lookup = X509_STORE_add_lookup(crl_store,
1115 X509_LOOKUP_file());
1116 if (crl_lookup == NULL ||
1117 !X509_load_crl_file(crl_lookup, crl_file,
1118 X509_FILETYPE_PEM)) {
1120 "Could not load CRL file %s\n",
1124 X509_STORE_set_flags(crl_store,
1125 X509_V_FLAG_CRL_CHECK |
1126 X509_V_FLAG_CRL_CHECK_ALL);
1133 * Configure client certificate based on environment.
1136 fetch_ssl_setup_client_certificate(SSL_CTX *ctx, int verbose)
1138 const char *client_cert_file, *client_key_file;
1140 if ((client_cert_file = getenv("SSL_CLIENT_CERT_FILE")) != NULL) {
1141 client_key_file = getenv("SSL_CLIENT_KEY_FILE") != NULL ?
1142 getenv("SSL_CLIENT_KEY_FILE") : client_cert_file;
1144 fetch_info("Using client cert file: %s",
1146 fetch_info("Using client key file: %s",
1149 if (SSL_CTX_use_certificate_chain_file(ctx,
1150 client_cert_file) != 1) {
1152 "Could not load client certificate %s\n",
1156 if (SSL_CTX_use_PrivateKey_file(ctx, client_key_file,
1157 SSL_FILETYPE_PEM) != 1) {
1159 "Could not load client key %s\n",
1168 * Callback for SSL certificate verification, this is called on server
1169 * cert verification. It takes no decision, but informs the user in case
1170 * verification failed.
1173 fetch_ssl_cb_verify_crt(int verified, X509_STORE_CTX *ctx)
1181 if ((crt = X509_STORE_CTX_get_current_cert(ctx)) != NULL &&
1182 (name = X509_get_subject_name(crt)) != NULL)
1183 str = X509_NAME_oneline(name, 0, 0);
1184 fprintf(stderr, "Certificate verification failed for %s\n",
1185 str != NULL ? str : "no relevant certificate");
1194 * Enable SSL on a connection.
1197 fetch_ssl(conn_t *conn, const struct url *URL, int verbose)
1204 /* Init the SSL library and context */
1205 if (!SSL_library_init()){
1206 fprintf(stderr, "SSL library init failed\n");
1210 SSL_load_error_strings();
1212 conn->ssl_meth = SSLv23_client_method();
1213 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth);
1214 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY);
1216 fetch_ssl_setup_transport_layer(conn->ssl_ctx, verbose);
1217 if (!fetch_ssl_setup_peer_verification(conn->ssl_ctx, verbose))
1219 if (!fetch_ssl_setup_client_certificate(conn->ssl_ctx, verbose))
1222 conn->ssl = SSL_new(conn->ssl_ctx);
1223 if (conn->ssl == NULL) {
1224 fprintf(stderr, "SSL context creation failed\n");
1227 SSL_set_fd(conn->ssl, conn->sd);
1229 #if OPENSSL_VERSION_NUMBER >= 0x0090806fL && !defined(OPENSSL_NO_TLSEXT)
1230 if (!SSL_set_tlsext_host_name(conn->ssl,
1231 __DECONST(struct url *, URL)->host)) {
1233 "TLS server name indication extension failed for host %s\n",
1238 while ((ret = SSL_connect(conn->ssl)) == -1) {
1239 ssl_err = SSL_get_error(conn->ssl, ret);
1240 if (ssl_err != SSL_ERROR_WANT_READ &&
1241 ssl_err != SSL_ERROR_WANT_WRITE) {
1242 ERR_print_errors_fp(stderr);
1246 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl);
1248 if (conn->ssl_cert == NULL) {
1249 fprintf(stderr, "No server SSL certificate\n");
1253 if (getenv("SSL_NO_VERIFY_HOSTNAME") == NULL) {
1255 fetch_info("Verify hostname");
1256 if (!fetch_ssl_verify_hname(conn->ssl_cert, URL->host)) {
1258 "SSL certificate subject doesn't match host %s\n",
1265 fetch_info("%s connection established using %s",
1266 SSL_get_version(conn->ssl), SSL_get_cipher(conn->ssl));
1267 name = X509_get_subject_name(conn->ssl_cert);
1268 str = X509_NAME_oneline(name, 0, 0);
1269 fetch_info("Certificate subject: %s", str);
1271 name = X509_get_issuer_name(conn->ssl_cert);
1272 str = X509_NAME_oneline(name, 0, 0);
1273 fetch_info("Certificate issuer: %s", str);
1282 fprintf(stderr, "SSL support disabled\n");
1287 #define FETCH_READ_WAIT -2
1288 #define FETCH_READ_ERROR -1
1289 #define FETCH_READ_DONE 0
1293 fetch_ssl_read(SSL *ssl, char *buf, size_t len)
1298 rlen = SSL_read(ssl, buf, len);
1300 ssl_err = SSL_get_error(ssl, rlen);
1301 if (ssl_err == SSL_ERROR_WANT_READ ||
1302 ssl_err == SSL_ERROR_WANT_WRITE) {
1303 return (FETCH_READ_WAIT);
1305 ERR_print_errors_fp(stderr);
1306 return (FETCH_READ_ERROR);
1314 fetch_socket_read(int sd, char *buf, size_t len)
1318 rlen = read(sd, buf, len);
1320 if (errno == EAGAIN || (errno == EINTR && fetchRestartCalls))
1321 return (FETCH_READ_WAIT);
1323 return (FETCH_READ_ERROR);
1329 * Read a character from a connection w/ timeout
1332 fetch_read(conn_t *conn, char *buf, size_t len)
1334 struct timeval now, timeout, delta;
1339 if (fetchTimeout > 0) {
1340 gettimeofday(&timeout, NULL);
1341 timeout.tv_sec += fetchTimeout;
1345 memset(&pfd, 0, sizeof pfd);
1347 pfd.events = POLLIN | POLLERR;
1351 * The socket is non-blocking. Instead of the canonical
1352 * poll() -> read(), we do the following:
1354 * 1) call read() or SSL_read().
1355 * 2) if we received some data, return it.
1356 * 3) if an error occurred, return -1.
1357 * 4) if read() or SSL_read() signaled EOF, return.
1358 * 5) if we did not receive any data but we're not at EOF,
1361 * In the SSL case, this is necessary because if we
1362 * receive a close notification, we have to call
1363 * SSL_read() one additional time after we've read
1364 * everything we received.
1366 * In the non-SSL case, it may improve performance (very
1367 * slightly) when reading small amounts of data.
1370 if (conn->ssl != NULL)
1371 rlen = fetch_ssl_read(conn->ssl, buf, len);
1374 rlen = fetch_socket_read(conn->sd, buf, len);
1377 } else if (rlen == FETCH_READ_ERROR) {
1381 // assert(rlen == FETCH_READ_WAIT);
1382 if (fetchTimeout > 0) {
1383 gettimeofday(&now, NULL);
1384 if (!timercmp(&timeout, &now, >)) {
1389 timersub(&timeout, &now, &delta);
1390 deltams = delta.tv_sec * 1000 +
1391 delta.tv_usec / 1000;;
1395 if (poll(&pfd, 1, deltams) < 0) {
1396 if (errno == EINTR && fetchRestartCalls)
1407 * Read a line of text from a connection w/ timeout
1409 #define MIN_BUF_SIZE 1024
1412 fetch_getln(conn_t *conn)
1419 if (conn->buf == NULL) {
1420 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) {
1424 conn->bufsize = MIN_BUF_SIZE;
1427 conn->buf[0] = '\0';
1431 len = fetch_read(conn, &c, 1);
1436 conn->buf[conn->buflen++] = c;
1437 if (conn->buflen == conn->bufsize) {
1439 tmpsize = conn->bufsize * 2 + 1;
1440 if ((tmp = realloc(tmp, tmpsize)) == NULL) {
1445 conn->bufsize = tmpsize;
1447 } while (c != '\n');
1449 conn->buf[conn->buflen] = '\0';
1450 DEBUGF("<<< %s", conn->buf);
1456 * Write to a connection w/ timeout
1459 fetch_write(conn_t *conn, const char *buf, size_t len)
1463 iov.iov_base = __DECONST(char *, buf);
1465 return fetch_writev(conn, &iov, 1);
1469 * Write a vector to a connection w/ timeout
1470 * Note: can modify the iovec.
1473 fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt)
1475 struct timeval now, timeout, delta;
1477 ssize_t wlen, total;
1480 memset(&pfd, 0, sizeof pfd);
1483 pfd.events = POLLOUT | POLLERR;
1484 gettimeofday(&timeout, NULL);
1485 timeout.tv_sec += fetchTimeout;
1489 while (iovcnt > 0) {
1490 while (fetchTimeout && pfd.revents == 0) {
1491 gettimeofday(&now, NULL);
1492 if (!timercmp(&timeout, &now, >)) {
1497 timersub(&timeout, &now, &delta);
1498 deltams = delta.tv_sec * 1000 +
1499 delta.tv_usec / 1000;
1502 if (poll(&pfd, 1, deltams) < 0) {
1503 /* POSIX compliance */
1504 if (errno == EAGAIN)
1506 if (errno == EINTR && fetchRestartCalls)
1513 if (conn->ssl != NULL)
1514 wlen = SSL_write(conn->ssl,
1515 iov->iov_base, iov->iov_len);
1518 wlen = writev(conn->sd, iov, iovcnt);
1520 /* we consider a short write a failure */
1521 /* XXX perhaps we shouldn't in the SSL case */
1527 if (errno == EINTR && fetchRestartCalls)
1532 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) {
1533 wlen -= iov->iov_len;
1538 iov->iov_len -= wlen;
1539 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen;
1547 * Write a line of text to a connection w/ timeout
1550 fetch_putln(conn_t *conn, const char *str, size_t len)
1552 struct iovec iov[2];
1555 DEBUGF(">>> %s\n", str);
1556 iov[0].iov_base = __DECONST(char *, str);
1557 iov[0].iov_len = len;
1558 iov[1].iov_base = __DECONST(char *, ENDL);
1559 iov[1].iov_len = sizeof(ENDL);
1561 ret = fetch_writev(conn, &iov[1], 1);
1563 ret = fetch_writev(conn, iov, 2);
1574 fetch_close(conn_t *conn)
1578 if (--conn->ref > 0)
1582 SSL_shutdown(conn->ssl);
1583 SSL_set_connect_state(conn->ssl);
1584 SSL_free(conn->ssl);
1587 if (conn->ssl_ctx) {
1588 SSL_CTX_free(conn->ssl_ctx);
1589 conn->ssl_ctx = NULL;
1591 if (conn->ssl_cert) {
1592 X509_free(conn->ssl_cert);
1593 conn->ssl_cert = NULL;
1596 ret = close(conn->sd);
1603 /*** Directory-related utility functions *************************************/
1606 fetch_add_entry(struct url_ent **p, int *size, int *len,
1607 const char *name, struct url_stat *us)
1609 struct url_ent *tmp;
1616 if (*len >= *size - 1) {
1617 tmp = reallocarray(*p, *size * 2 + 1, sizeof(**p));
1623 *size = (*size * 2 + 1);
1628 snprintf(tmp->name, PATH_MAX, "%s", name);
1629 memcpy(&tmp->stat, us, sizeof(*us));
1632 (++tmp)->name[0] = 0;
1638 /*** Authentication-related utility functions ********************************/
1641 fetch_read_word(FILE *f)
1643 static char word[1024];
1645 if (fscanf(f, " %1023s ", word) != 1)
1651 fetch_netrc_open(void)
1658 if ((p = getenv("NETRC")) != NULL) {
1659 DEBUGF("NETRC=%s\n", p);
1660 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) {
1661 fetch_info("$NETRC specifies a file name "
1662 "longer than PATH_MAX");
1666 if ((p = getenv("HOME")) == NULL) {
1667 if ((pwd = getpwuid(getuid())) == NULL ||
1668 (p = pwd->pw_dir) == NULL)
1671 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn))
1675 if ((fd = open(fn, O_RDONLY)) < 0) {
1677 DEBUGF("%s: %s\n", fn, strerror(serrno));
1684 * Get authentication data for a URL from .netrc
1687 fetch_netrc_auth(struct url *url)
1693 if (url->netrcfd < 0)
1694 url->netrcfd = fetch_netrc_open();
1695 if (url->netrcfd < 0)
1697 if ((f = fdopen(url->netrcfd, "r")) == NULL) {
1699 DEBUGF("fdopen(netrcfd): %s", strerror(errno));
1700 close(url->netrcfd);
1706 DEBUGF("searching netrc for %s\n", url->host);
1707 while ((word = fetch_read_word(f)) != NULL) {
1708 if (strcmp(word, "default") == 0) {
1709 DEBUGF("using default netrc settings\n");
1712 if (strcmp(word, "machine") == 0 &&
1713 (word = fetch_read_word(f)) != NULL &&
1714 strcasecmp(word, url->host) == 0) {
1715 DEBUGF("using netrc settings for %s\n", word);
1721 while ((word = fetch_read_word(f)) != NULL) {
1722 if (strcmp(word, "login") == 0) {
1723 if ((word = fetch_read_word(f)) == NULL)
1725 if (snprintf(url->user, sizeof(url->user),
1726 "%s", word) > (int)sizeof(url->user)) {
1727 fetch_info("login name in .netrc is too long");
1728 url->user[0] = '\0';
1730 } else if (strcmp(word, "password") == 0) {
1731 if ((word = fetch_read_word(f)) == NULL)
1733 if (snprintf(url->pwd, sizeof(url->pwd),
1734 "%s", word) > (int)sizeof(url->pwd)) {
1735 fetch_info("password in .netrc is too long");
1738 } else if (strcmp(word, "account") == 0) {
1739 if ((word = fetch_read_word(f)) == NULL)
1741 /* XXX not supported! */
1758 * The no_proxy environment variable specifies a set of domains for
1759 * which the proxy should not be consulted; the contents is a comma-,
1760 * or space-separated list of domain names. A single asterisk will
1761 * override all proxy variables and no transactions will be proxied
1762 * (for compatibility with lynx and curl, see the discussion at
1763 * <http://curl.haxx.se/mail/archive_pre_oct_99/0009.html>).
1766 fetch_no_proxy_match(const char *host)
1768 const char *no_proxy, *p, *q;
1769 size_t h_len, d_len;
1771 if ((no_proxy = getenv("NO_PROXY")) == NULL &&
1772 (no_proxy = getenv("no_proxy")) == NULL)
1775 /* asterisk matches any hostname */
1776 if (strcmp(no_proxy, "*") == 0)
1779 h_len = strlen(host);
1782 /* position p at the beginning of a domain suffix */
1783 while (*p == ',' || isspace((unsigned char)*p))
1786 /* position q at the first separator character */
1787 for (q = p; *q; ++q)
1788 if (*q == ',' || isspace((unsigned char)*q))
1792 if (d_len > 0 && h_len >= d_len &&
1793 strncasecmp(host + h_len - d_len,
1795 /* domain name matches */