2018-09-06 03:16:02 -04:00
|
|
|
/***************************************************************************
|
|
|
|
* _ _ ____ _
|
|
|
|
* Project ___| | | | _ \| |
|
|
|
|
* / __| | | | |_) | |
|
|
|
|
* | (__| |_| | _ <| |___
|
|
|
|
* \___|\___/|_| \_\_____|
|
|
|
|
*
|
2020-12-31 04:11:49 -05:00
|
|
|
* Copyright (C) 2018 - 2021, Daniel Stenberg, <daniel@haxx.se>, et al.
|
2018-09-06 03:16:02 -04:00
|
|
|
*
|
|
|
|
* This software is licensed as described in the file COPYING, which
|
|
|
|
* you should have received as part of this distribution. The terms
|
2020-11-04 08:02:01 -05:00
|
|
|
* are also available at https://curl.se/docs/copyright.html.
|
2018-09-06 03:16:02 -04:00
|
|
|
*
|
|
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
|
|
*
|
|
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
|
|
* KIND, either express or implied.
|
|
|
|
*
|
|
|
|
***************************************************************************/
|
|
|
|
|
|
|
|
#include "curl_setup.h"
|
2018-09-29 08:08:29 -04:00
|
|
|
|
2019-05-05 11:08:21 -04:00
|
|
|
#ifndef CURL_DISABLE_DOH
|
|
|
|
|
2018-09-06 03:16:02 -04:00
|
|
|
#include "urldata.h"
|
|
|
|
#include "curl_addrinfo.h"
|
2018-09-29 08:08:29 -04:00
|
|
|
#include "doh.h"
|
|
|
|
|
2018-09-06 03:16:02 -04:00
|
|
|
#include "sendf.h"
|
|
|
|
#include "multiif.h"
|
|
|
|
#include "url.h"
|
|
|
|
#include "share.h"
|
|
|
|
#include "curl_base64.h"
|
|
|
|
#include "connect.h"
|
2018-12-05 09:21:27 -05:00
|
|
|
#include "strdup.h"
|
2020-05-02 11:04:08 -04:00
|
|
|
#include "dynbuf.h"
|
2018-09-06 03:16:02 -04:00
|
|
|
/* The last 3 #include files should be in this order */
|
|
|
|
#include "curl_printf.h"
|
|
|
|
#include "curl_memory.h"
|
|
|
|
#include "memdebug.h"
|
|
|
|
|
|
|
|
#define DNS_CLASS_IN 0x01
|
|
|
|
|
2018-10-16 17:35:44 -04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2018-09-06 03:16:02 -04:00
|
|
|
static const char * const errors[]={
|
|
|
|
"",
|
|
|
|
"Bad label",
|
|
|
|
"Out of range",
|
|
|
|
"Label loop",
|
|
|
|
"Too small",
|
|
|
|
"Out of memory",
|
|
|
|
"RDATA length",
|
|
|
|
"Malformat",
|
|
|
|
"Bad RCODE",
|
|
|
|
"Unexpected TYPE",
|
|
|
|
"Unexpected CLASS",
|
|
|
|
"No content",
|
2020-08-26 07:33:42 -04:00
|
|
|
"Bad ID",
|
|
|
|
"Name too long"
|
2018-09-06 03:16:02 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static const char *doh_strerror(DOHcode code)
|
|
|
|
{
|
2020-08-26 07:33:42 -04:00
|
|
|
if((code >= DOH_OK) && (code <= DOH_DNS_NAME_TOO_LONG))
|
2018-09-06 03:16:02 -04:00
|
|
|
return errors[code];
|
|
|
|
return "bad error code";
|
|
|
|
}
|
2018-10-16 17:35:44 -04:00
|
|
|
#endif
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2018-06-19 10:08:05 -04:00
|
|
|
#ifdef DEBUGBUILD
|
|
|
|
#define UNITTEST
|
|
|
|
#else
|
|
|
|
#define UNITTEST static
|
|
|
|
#endif
|
|
|
|
|
2019-09-13 21:16:09 -04:00
|
|
|
/* @unittest 1655
|
|
|
|
*/
|
2018-06-19 10:08:05 -04:00
|
|
|
UNITTEST DOHcode doh_encode(const char *host,
|
|
|
|
DNStype dnstype,
|
|
|
|
unsigned char *dnsp, /* buffer */
|
|
|
|
size_t len, /* buffer size */
|
|
|
|
size_t *olen) /* output length */
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
2019-09-13 21:16:09 -04:00
|
|
|
const size_t hostlen = strlen(host);
|
2018-09-06 03:16:02 -04:00
|
|
|
unsigned char *orig = dnsp;
|
|
|
|
const char *hostp = host;
|
|
|
|
|
2019-11-14 14:21:09 -05:00
|
|
|
/* The expected output length is 16 bytes more than the length of
|
|
|
|
* the QNAME-encoding of the host name.
|
|
|
|
*
|
|
|
|
* A valid DNS name may not contain a zero-length label, except at
|
|
|
|
* the end. For this reason, a name beginning with a dot, or
|
|
|
|
* containing a sequence of two or more consecutive dots, is invalid
|
|
|
|
* and cannot be encoded as a QNAME.
|
|
|
|
*
|
|
|
|
* If the host name ends with a trailing dot, the corresponding
|
|
|
|
* QNAME-encoding is one byte longer than the host name. If (as is
|
|
|
|
* also valid) the hostname is shortened by the omission of the
|
|
|
|
* trailing dot, then its QNAME-encoding will be two bytes longer
|
|
|
|
* than the host name.
|
|
|
|
*
|
|
|
|
* Each [ label, dot ] pair is encoded as [ length, label ],
|
|
|
|
* preserving overall length. A final [ label ] without a dot is
|
|
|
|
* also encoded as [ length, label ], increasing overall length
|
|
|
|
* by one. The encoding is completed by appending a zero byte,
|
|
|
|
* representing the zero-length root label, again increasing
|
|
|
|
* the overall length by one.
|
2019-09-13 21:16:09 -04:00
|
|
|
*/
|
2019-11-14 14:21:09 -05:00
|
|
|
|
|
|
|
size_t expected_len;
|
|
|
|
DEBUGASSERT(hostlen);
|
|
|
|
expected_len = 12 + 1 + hostlen + 4;
|
|
|
|
if(host[hostlen-1]!='.')
|
|
|
|
expected_len++;
|
|
|
|
|
|
|
|
if(expected_len > (256 + 16)) /* RFCs 1034, 1035 */
|
|
|
|
return DOH_DNS_NAME_TOO_LONG;
|
2019-09-13 21:16:09 -04:00
|
|
|
|
|
|
|
if(len < expected_len)
|
2018-09-06 03:16:02 -04:00
|
|
|
return DOH_TOO_SMALL_BUFFER;
|
|
|
|
|
|
|
|
*dnsp++ = 0; /* 16 bit id */
|
|
|
|
*dnsp++ = 0;
|
|
|
|
*dnsp++ = 0x01; /* |QR| Opcode |AA|TC|RD| Set the RD bit */
|
|
|
|
*dnsp++ = '\0'; /* |RA| Z | RCODE | */
|
|
|
|
*dnsp++ = '\0';
|
|
|
|
*dnsp++ = 1; /* QDCOUNT (number of entries in the question section) */
|
|
|
|
*dnsp++ = '\0';
|
|
|
|
*dnsp++ = '\0'; /* ANCOUNT */
|
|
|
|
*dnsp++ = '\0';
|
|
|
|
*dnsp++ = '\0'; /* NSCOUNT */
|
|
|
|
*dnsp++ = '\0';
|
|
|
|
*dnsp++ = '\0'; /* ARCOUNT */
|
|
|
|
|
2019-11-14 14:21:09 -05:00
|
|
|
/* encode each label and store it in the QNAME */
|
|
|
|
while(*hostp) {
|
2018-09-06 03:16:02 -04:00
|
|
|
size_t labellen;
|
2019-11-14 14:21:09 -05:00
|
|
|
char *dot = strchr(hostp, '.');
|
|
|
|
if(dot)
|
2018-09-06 03:16:02 -04:00
|
|
|
labellen = dot - hostp;
|
|
|
|
else
|
|
|
|
labellen = strlen(hostp);
|
2019-11-14 14:21:09 -05:00
|
|
|
if((labellen > 63) || (!labellen)) {
|
|
|
|
/* label is too long or too short, error out */
|
2018-06-19 10:08:05 -04:00
|
|
|
*olen = 0;
|
2018-09-06 03:16:02 -04:00
|
|
|
return DOH_DNS_BAD_LABEL;
|
2018-06-19 10:08:05 -04:00
|
|
|
}
|
2019-11-14 14:21:09 -05:00
|
|
|
/* label is non-empty, process it */
|
2018-09-06 03:16:02 -04:00
|
|
|
*dnsp++ = (unsigned char)labellen;
|
|
|
|
memcpy(dnsp, hostp, labellen);
|
|
|
|
dnsp += labellen;
|
2019-11-14 14:21:09 -05:00
|
|
|
hostp += labellen;
|
|
|
|
/* advance past dot, but only if there is one */
|
|
|
|
if(dot)
|
|
|
|
hostp++;
|
|
|
|
} /* next label */
|
|
|
|
|
|
|
|
*dnsp++ = 0; /* append zero-length label for root */
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2019-09-19 09:38:14 -04:00
|
|
|
/* There are assigned TYPE codes beyond 255: use range [1..65535] */
|
|
|
|
*dnsp++ = (unsigned char)(255 & (dnstype>>8)); /* upper 8 bit TYPE */
|
|
|
|
*dnsp++ = (unsigned char)(255 & dnstype); /* lower 8 bit TYPE */
|
|
|
|
|
2018-09-06 03:16:02 -04:00
|
|
|
*dnsp++ = '\0'; /* upper 8 bit CLASS */
|
|
|
|
*dnsp++ = DNS_CLASS_IN; /* IN - "the Internet" */
|
|
|
|
|
|
|
|
*olen = dnsp - orig;
|
2019-09-13 21:16:09 -04:00
|
|
|
|
2019-11-14 14:21:09 -05:00
|
|
|
/* verify that our estimation of length is valid, since
|
|
|
|
* this has led to buffer overflows in this function */
|
2019-09-13 21:16:09 -04:00
|
|
|
DEBUGASSERT(*olen == expected_len);
|
2018-09-06 03:16:02 -04:00
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t
|
2020-04-27 06:07:34 -04:00
|
|
|
doh_write_cb(const void *contents, size_t size, size_t nmemb, void *userp)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
size_t realsize = size * nmemb;
|
2020-05-02 11:04:08 -04:00
|
|
|
struct dynbuf *mem = (struct dynbuf *)userp;
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2020-05-02 11:04:08 -04:00
|
|
|
if(Curl_dyn_addn(mem, contents, realsize))
|
2018-09-06 03:16:02 -04:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
return realsize;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* called from multi.c when this DOH transfer is complete */
|
2021-01-13 05:46:14 -05:00
|
|
|
static int doh_done(struct Curl_easy *doh, CURLcode result)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
struct Curl_easy *data = doh->set.dohfor;
|
2021-01-19 09:57:24 -05:00
|
|
|
struct dohdata *dohp = data->req.doh;
|
2018-09-06 03:16:02 -04:00
|
|
|
/* so one of the DOH request done for the 'data' transfer is now complete! */
|
2021-01-19 09:57:24 -05:00
|
|
|
dohp->pending--;
|
|
|
|
infof(data, "a DOH request is completed, %u to go\n", dohp->pending);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(result)
|
|
|
|
infof(data, "DOH request %s\n", curl_easy_strerror(result));
|
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
if(!dohp->pending) {
|
2018-09-06 03:16:02 -04:00
|
|
|
/* DOH completed */
|
2021-01-19 09:57:24 -05:00
|
|
|
curl_slist_free_all(dohp->headers);
|
|
|
|
dohp->headers = NULL;
|
2018-09-06 03:16:02 -04:00
|
|
|
Curl_expire(data, 0, EXPIRE_RUN_NOW);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2019-03-09 03:16:33 -05:00
|
|
|
#define ERROR_CHECK_SETOPT(x,y) \
|
|
|
|
do { \
|
|
|
|
result = curl_easy_setopt(doh, x, y); \
|
|
|
|
if(result) \
|
|
|
|
goto error; \
|
2019-11-30 03:29:36 -05:00
|
|
|
} while(0)
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
static CURLcode dohprobe(struct Curl_easy *data,
|
|
|
|
struct dnsprobe *p, DNStype dnstype,
|
|
|
|
const char *host,
|
|
|
|
const char *url, CURLM *multi,
|
|
|
|
struct curl_slist *headers)
|
|
|
|
{
|
|
|
|
struct Curl_easy *doh = NULL;
|
|
|
|
char *nurl = NULL;
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
timediff_t timeout_ms;
|
|
|
|
DOHcode d = doh_encode(host, dnstype, p->dohbuffer, sizeof(p->dohbuffer),
|
|
|
|
&p->dohlen);
|
|
|
|
if(d) {
|
2020-12-23 17:41:13 -05:00
|
|
|
failf(data, "Failed to encode DOH packet [%d]", d);
|
2018-09-06 03:16:02 -04:00
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->dnstype = dnstype;
|
2020-05-02 11:04:08 -04:00
|
|
|
Curl_dyn_init(&p->serverdoh, DYN_DOH_RESPONSE);
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
/* Note: this is code for sending the DoH request with GET but there's still
|
|
|
|
no logic that actually enables this. We should either add that ability or
|
|
|
|
yank out the GET code. Discuss! */
|
|
|
|
if(data->set.doh_get) {
|
|
|
|
char *b64;
|
|
|
|
size_t b64len;
|
|
|
|
result = Curl_base64url_encode(data, (char *)p->dohbuffer, p->dohlen,
|
|
|
|
&b64, &b64len);
|
|
|
|
if(result)
|
|
|
|
goto error;
|
|
|
|
nurl = aprintf("%s?dns=%s", url, b64);
|
|
|
|
free(b64);
|
|
|
|
if(!nurl) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
url = nurl;
|
|
|
|
}
|
|
|
|
|
|
|
|
timeout_ms = Curl_timeleft(data, NULL, TRUE);
|
2019-09-23 07:10:12 -04:00
|
|
|
if(timeout_ms <= 0) {
|
|
|
|
result = CURLE_OPERATION_TIMEDOUT;
|
|
|
|
goto error;
|
|
|
|
}
|
2018-09-06 03:16:02 -04:00
|
|
|
/* Curl_open() is the internal version of curl_easy_init() */
|
|
|
|
result = Curl_open(&doh);
|
|
|
|
if(!result) {
|
2018-09-07 03:26:08 -04:00
|
|
|
/* pass in the struct pointer via a local variable to please coverity and
|
|
|
|
the gcc typecheck helpers */
|
2020-05-02 11:04:08 -04:00
|
|
|
struct dynbuf *resp = &p->serverdoh;
|
2018-09-06 03:16:02 -04:00
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_URL, url);
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_WRITEFUNCTION, doh_write_cb);
|
2018-09-07 03:26:08 -04:00
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_WRITEDATA, resp);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(!data->set.doh_get) {
|
2018-09-07 03:26:08 -04:00
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDS, p->dohbuffer);
|
2018-10-02 05:15:29 -04:00
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDSIZE, (long)p->dohlen);
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_HTTPHEADER, headers);
|
2018-12-03 05:51:52 -05:00
|
|
|
#ifdef USE_NGHTTP2
|
2018-09-06 03:16:02 -04:00
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
|
2018-12-03 05:51:52 -05:00
|
|
|
#endif
|
2018-09-06 03:16:02 -04:00
|
|
|
#ifndef CURLDEBUG
|
|
|
|
/* enforce HTTPS if not debug */
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
|
2019-09-23 07:11:49 -04:00
|
|
|
#else
|
|
|
|
/* in debug mode, also allow http */
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
|
2018-09-06 03:16:02 -04:00
|
|
|
#endif
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_TIMEOUT_MS, (long)timeout_ms);
|
2019-03-09 03:16:33 -05:00
|
|
|
if(data->set.verbose)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_VERBOSE, 1L);
|
|
|
|
if(data->set.no_signal)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_NOSIGNAL, 1L);
|
|
|
|
|
|
|
|
/* Inherit *some* SSL options from the user's transfer. This is a
|
|
|
|
best-guess as to which options are needed for compatibility. #3661 */
|
|
|
|
if(data->set.ssl.falsestart)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_FALSESTART, 1L);
|
|
|
|
if(data->set.ssl.primary.verifyhost)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYHOST, 2L);
|
2019-12-11 04:47:06 -05:00
|
|
|
#ifndef CURL_DISABLE_PROXY
|
2019-03-09 03:16:33 -05:00
|
|
|
if(data->set.proxy_ssl.primary.verifyhost)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_VERIFYHOST, 2L);
|
|
|
|
if(data->set.proxy_ssl.primary.verifypeer)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
|
2019-12-11 04:47:06 -05:00
|
|
|
if(data->set.str[STRING_SSL_CAFILE_PROXY]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_CAINFO,
|
|
|
|
data->set.str[STRING_SSL_CAFILE_PROXY]);
|
|
|
|
}
|
|
|
|
if(data->set.str[STRING_SSL_CRLFILE_PROXY]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_CRLFILE,
|
|
|
|
data->set.str[STRING_SSL_CRLFILE_PROXY]);
|
|
|
|
}
|
|
|
|
if(data->set.proxy_ssl.no_revoke)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
|
schannel: add "best effort" revocation check option
- Implement new option CURLSSLOPT_REVOKE_BEST_EFFORT and
--ssl-revoke-best-effort to allow a "best effort" revocation check.
A best effort revocation check ignores errors that the revocation check
was unable to take place. The reasoning is described in detail below and
discussed further in the PR.
---
When running e.g. with Fiddler, the schannel backend fails with an
unhelpful error message:
Unknown error (0x80092012) - The revocation function was unable
to check revocation for the certificate.
Sadly, many enterprise users who are stuck behind MITM proxies suffer
the very same problem.
This has been discussed in plenty of issues:
https://github.com/curl/curl/issues/3727,
https://github.com/curl/curl/issues/264, for example.
In the latter, a Microsoft Edge developer even made the case that the
common behavior is to ignore issues when a certificate has no recorded
distribution point for revocation lists, or when the server is offline.
This is also known as "best effort" strategy and addresses the Fiddler
issue.
Unfortunately, this strategy was not chosen as the default for schannel
(and is therefore a backend-specific behavior: OpenSSL seems to happily
ignore the offline servers and missing distribution points).
To maintain backward-compatibility, we therefore add a new flag
(`CURLSSLOPT_REVOKE_BEST_EFFORT`) and a new option
(`--ssl-revoke-best-effort`) to select the new behavior.
Due to the many related issues Git for Windows and GitHub Desktop, the
plan is to make this behavior the default in these software packages.
The test 2070 was added to verify this behavior, adapted from 310.
Based-on-work-by: georgeok <giorgos.n.oikonomou@gmail.com>
Co-authored-by: Markus Olsson <j.markus.olsson@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Closes https://github.com/curl/curl/pull/4981
2020-02-26 05:24:26 -05:00
|
|
|
else if(data->set.proxy_ssl.revoke_best_effort)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_OPTIONS,
|
|
|
|
CURLSSLOPT_REVOKE_BEST_EFFORT);
|
2019-12-11 04:47:06 -05:00
|
|
|
if(data->set.str[STRING_SSL_CAPATH_PROXY]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_PROXY_CAPATH,
|
|
|
|
data->set.str[STRING_SSL_CAPATH_PROXY]);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if(data->set.ssl.primary.verifypeer)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYPEER, 1L);
|
2019-03-09 03:16:33 -05:00
|
|
|
if(data->set.ssl.primary.verifystatus)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYSTATUS, 1L);
|
|
|
|
if(data->set.str[STRING_SSL_CAFILE_ORIG]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_CAINFO,
|
|
|
|
data->set.str[STRING_SSL_CAFILE_ORIG]);
|
|
|
|
}
|
|
|
|
if(data->set.str[STRING_SSL_CAPATH_ORIG]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_CAPATH,
|
|
|
|
data->set.str[STRING_SSL_CAPATH_ORIG]);
|
|
|
|
}
|
|
|
|
if(data->set.str[STRING_SSL_CRLFILE_ORIG]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_CRLFILE,
|
|
|
|
data->set.str[STRING_SSL_CRLFILE_ORIG]);
|
|
|
|
}
|
|
|
|
if(data->set.ssl.certinfo)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_CERTINFO, 1L);
|
|
|
|
if(data->set.str[STRING_SSL_RANDOM_FILE]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_RANDOM_FILE,
|
|
|
|
data->set.str[STRING_SSL_RANDOM_FILE]);
|
|
|
|
}
|
|
|
|
if(data->set.str[STRING_SSL_EGDSOCKET]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_EGDSOCKET,
|
|
|
|
data->set.str[STRING_SSL_EGDSOCKET]);
|
|
|
|
}
|
|
|
|
if(data->set.ssl.no_revoke)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
|
schannel: add "best effort" revocation check option
- Implement new option CURLSSLOPT_REVOKE_BEST_EFFORT and
--ssl-revoke-best-effort to allow a "best effort" revocation check.
A best effort revocation check ignores errors that the revocation check
was unable to take place. The reasoning is described in detail below and
discussed further in the PR.
---
When running e.g. with Fiddler, the schannel backend fails with an
unhelpful error message:
Unknown error (0x80092012) - The revocation function was unable
to check revocation for the certificate.
Sadly, many enterprise users who are stuck behind MITM proxies suffer
the very same problem.
This has been discussed in plenty of issues:
https://github.com/curl/curl/issues/3727,
https://github.com/curl/curl/issues/264, for example.
In the latter, a Microsoft Edge developer even made the case that the
common behavior is to ignore issues when a certificate has no recorded
distribution point for revocation lists, or when the server is offline.
This is also known as "best effort" strategy and addresses the Fiddler
issue.
Unfortunately, this strategy was not chosen as the default for schannel
(and is therefore a backend-specific behavior: OpenSSL seems to happily
ignore the offline servers and missing distribution points).
To maintain backward-compatibility, we therefore add a new flag
(`CURLSSLOPT_REVOKE_BEST_EFFORT`) and a new option
(`--ssl-revoke-best-effort`) to select the new behavior.
Due to the many related issues Git for Windows and GitHub Desktop, the
plan is to make this behavior the default in these software packages.
The test 2070 was added to verify this behavior, adapted from 310.
Based-on-work-by: georgeok <giorgos.n.oikonomou@gmail.com>
Co-authored-by: Markus Olsson <j.markus.olsson@gmail.com>
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Closes https://github.com/curl/curl/pull/4981
2020-02-26 05:24:26 -05:00
|
|
|
else if(data->set.ssl.revoke_best_effort)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_OPTIONS, CURLSSLOPT_REVOKE_BEST_EFFORT);
|
2019-03-09 03:16:33 -05:00
|
|
|
if(data->set.ssl.fsslctx)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_FUNCTION, data->set.ssl.fsslctx);
|
|
|
|
if(data->set.ssl.fsslctxp)
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_DATA, data->set.ssl.fsslctxp);
|
2020-08-29 08:09:24 -04:00
|
|
|
if(data->set.str[STRING_SSL_EC_CURVES]) {
|
|
|
|
ERROR_CHECK_SETOPT(CURLOPT_SSL_EC_CURVES,
|
|
|
|
data->set.str[STRING_SSL_EC_CURVES]);
|
|
|
|
}
|
2019-03-09 03:16:33 -05:00
|
|
|
|
2021-01-13 05:46:14 -05:00
|
|
|
doh->set.fmultidone = doh_done;
|
2018-09-06 03:16:02 -04:00
|
|
|
doh->set.dohfor = data; /* identify for which transfer this is done */
|
|
|
|
p->easy = doh;
|
|
|
|
|
|
|
|
/* add this transfer to the multi handle */
|
|
|
|
if(curl_multi_add_handle(multi, doh))
|
|
|
|
goto error;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
goto error;
|
|
|
|
free(nurl);
|
|
|
|
return CURLE_OK;
|
|
|
|
|
|
|
|
error:
|
|
|
|
free(nurl);
|
2019-10-28 04:28:05 -04:00
|
|
|
Curl_close(&doh);
|
2018-09-06 03:16:02 -04:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Curl_doh() resolves a name using DOH. It resolves a name and returns a
|
|
|
|
* 'Curl_addrinfo *' with the address information.
|
|
|
|
*/
|
|
|
|
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *Curl_doh(struct connectdata *conn,
|
|
|
|
const char *hostname,
|
|
|
|
int port,
|
|
|
|
int *waitp)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
struct Curl_easy *data = conn->data;
|
|
|
|
CURLcode result = CURLE_OK;
|
2019-11-21 07:15:44 -05:00
|
|
|
int slot;
|
2021-01-19 09:57:24 -05:00
|
|
|
struct dohdata *dohp;
|
2018-09-06 03:16:02 -04:00
|
|
|
*waitp = TRUE; /* this never returns synchronously */
|
|
|
|
(void)conn;
|
|
|
|
(void)hostname;
|
|
|
|
(void)port;
|
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
DEBUGASSERT(!data->req.doh);
|
|
|
|
|
2018-09-06 03:16:02 -04:00
|
|
|
/* start clean, consider allocating this struct on demand */
|
2021-01-19 09:57:24 -05:00
|
|
|
dohp = data->req.doh = calloc(sizeof(struct dohdata), 1);
|
|
|
|
if(!dohp)
|
|
|
|
return NULL;
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2020-05-26 05:07:06 -04:00
|
|
|
conn->bits.doh = TRUE;
|
2021-01-19 09:57:24 -05:00
|
|
|
dohp->host = hostname;
|
|
|
|
dohp->port = port;
|
|
|
|
dohp->headers =
|
2018-09-06 03:16:02 -04:00
|
|
|
curl_slist_append(NULL,
|
|
|
|
"Content-Type: application/dns-message");
|
2021-01-19 09:57:24 -05:00
|
|
|
if(!dohp->headers)
|
2018-09-06 03:16:02 -04:00
|
|
|
goto error;
|
|
|
|
|
|
|
|
if(conn->ip_version != CURL_IPRESOLVE_V6) {
|
|
|
|
/* create IPv4 DOH request */
|
2021-01-19 09:57:24 -05:00
|
|
|
result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V4],
|
2019-11-21 07:15:44 -05:00
|
|
|
DNS_TYPE_A, hostname, data->set.str[STRING_DOH],
|
2021-01-19 09:57:24 -05:00
|
|
|
data->multi, dohp->headers);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(result)
|
|
|
|
goto error;
|
2021-01-19 09:57:24 -05:00
|
|
|
dohp->pending++;
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if(conn->ip_version != CURL_IPRESOLVE_V4) {
|
|
|
|
/* create IPv6 DOH request */
|
2021-01-19 09:57:24 -05:00
|
|
|
result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V6],
|
2019-11-21 07:15:44 -05:00
|
|
|
DNS_TYPE_AAAA, hostname, data->set.str[STRING_DOH],
|
2021-01-19 09:57:24 -05:00
|
|
|
data->multi, dohp->headers);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(result)
|
|
|
|
goto error;
|
2021-01-19 09:57:24 -05:00
|
|
|
dohp->pending++;
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
error:
|
2021-01-19 09:57:24 -05:00
|
|
|
curl_slist_free_all(dohp->headers);
|
|
|
|
data->req.doh->headers = NULL;
|
2019-11-21 07:15:44 -05:00
|
|
|
for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
|
2021-01-19 09:57:24 -05:00
|
|
|
Curl_close(&dohp->probe[slot].easy);
|
2019-11-21 07:15:44 -05:00
|
|
|
}
|
2021-01-19 09:57:24 -05:00
|
|
|
Curl_safefree(data->req.doh);
|
2018-09-06 03:16:02 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static DOHcode skipqname(const unsigned char *doh, size_t dohlen,
|
2018-09-06 03:16:02 -04:00
|
|
|
unsigned int *indexp)
|
|
|
|
{
|
|
|
|
unsigned char length;
|
|
|
|
do {
|
|
|
|
if(dohlen < (*indexp + 1))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
length = doh[*indexp];
|
|
|
|
if((length & 0xc0) == 0xc0) {
|
|
|
|
/* name pointer, advance over it and be done */
|
|
|
|
if(dohlen < (*indexp + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
*indexp += 2;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if(length & 0xc0)
|
|
|
|
return DOH_DNS_BAD_LABEL;
|
|
|
|
if(dohlen < (*indexp + 1 + length))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
*indexp += 1 + length;
|
|
|
|
} while(length);
|
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static unsigned short get16bit(const unsigned char *doh, int index)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
return (unsigned short)((doh[index] << 8) | doh[index + 1]);
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static unsigned int get32bit(const unsigned char *doh, int index)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
2019-09-13 14:06:21 -04:00
|
|
|
/* make clang and gcc optimize this to bswap by incrementing
|
|
|
|
the pointer first. */
|
|
|
|
doh += index;
|
|
|
|
|
2020-12-31 04:11:49 -05:00
|
|
|
/* avoid undefined behavior by casting to unsigned before shifting
|
2019-09-13 14:06:21 -04:00
|
|
|
24 bits, possibly into the sign bit. codegen is same, but
|
|
|
|
ub sanitizer won't be upset */
|
|
|
|
return ( (unsigned)doh[0] << 24) | (doh[1] << 16) |(doh[2] << 8) | doh[3];
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static DOHcode store_a(const unsigned char *doh, int index, struct dohentry *d)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
/* silently ignore addresses over the limit */
|
2018-06-19 10:08:05 -04:00
|
|
|
if(d->numaddr < DOH_MAX_ADDR) {
|
2018-09-06 03:16:02 -04:00
|
|
|
struct dohaddr *a = &d->addr[d->numaddr];
|
|
|
|
a->type = DNS_TYPE_A;
|
2018-10-04 05:57:29 -04:00
|
|
|
memcpy(&a->ip.v4, &doh[index], 4);
|
2018-09-06 03:16:02 -04:00
|
|
|
d->numaddr++;
|
|
|
|
}
|
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static DOHcode store_aaaa(const unsigned char *doh,
|
|
|
|
int index,
|
|
|
|
struct dohentry *d)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
/* silently ignore addresses over the limit */
|
2018-06-19 10:08:05 -04:00
|
|
|
if(d->numaddr < DOH_MAX_ADDR) {
|
2018-09-06 03:16:02 -04:00
|
|
|
struct dohaddr *a = &d->addr[d->numaddr];
|
|
|
|
a->type = DNS_TYPE_AAAA;
|
2018-10-04 05:57:29 -04:00
|
|
|
memcpy(&a->ip.v6, &doh[index], 16);
|
2018-09-06 03:16:02 -04:00
|
|
|
d->numaddr++;
|
|
|
|
}
|
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static DOHcode store_cname(const unsigned char *doh,
|
2018-09-06 03:16:02 -04:00
|
|
|
size_t dohlen,
|
|
|
|
unsigned int index,
|
|
|
|
struct dohentry *d)
|
|
|
|
{
|
2020-05-02 11:04:08 -04:00
|
|
|
struct dynbuf *c;
|
2018-09-06 03:16:02 -04:00
|
|
|
unsigned int loop = 128; /* a valid DNS name can never loop this much */
|
|
|
|
unsigned char length;
|
2018-06-19 10:08:05 -04:00
|
|
|
|
|
|
|
if(d->numcname == DOH_MAX_CNAME)
|
|
|
|
return DOH_OK; /* skip! */
|
|
|
|
|
|
|
|
c = &d->cname[d->numcname++];
|
2018-09-06 03:16:02 -04:00
|
|
|
do {
|
|
|
|
if(index >= dohlen)
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
length = doh[index];
|
|
|
|
if((length & 0xc0) == 0xc0) {
|
|
|
|
int newpos;
|
|
|
|
/* name pointer, get the new offset (14 bits) */
|
|
|
|
if((index + 1) >= dohlen)
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
2020-02-02 03:49:28 -05:00
|
|
|
/* move to the new index */
|
2018-09-06 03:16:02 -04:00
|
|
|
newpos = (length & 0x3f) << 8 | doh[index + 1];
|
|
|
|
index = newpos;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
else if(length & 0xc0)
|
|
|
|
return DOH_DNS_BAD_LABEL; /* bad input */
|
|
|
|
else
|
|
|
|
index++;
|
|
|
|
|
|
|
|
if(length) {
|
2020-05-02 11:04:08 -04:00
|
|
|
if(Curl_dyn_len(c)) {
|
|
|
|
if(Curl_dyn_add(c, "."))
|
|
|
|
return DOH_OUT_OF_MEM;
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
if((index + length) > dohlen)
|
|
|
|
return DOH_DNS_BAD_LABEL;
|
|
|
|
|
2020-05-02 11:04:08 -04:00
|
|
|
if(Curl_dyn_addn(c, &doh[index], length))
|
|
|
|
return DOH_OUT_OF_MEM;
|
2018-09-06 03:16:02 -04:00
|
|
|
index += length;
|
|
|
|
}
|
|
|
|
} while(length && --loop);
|
|
|
|
|
|
|
|
if(!loop)
|
|
|
|
return DOH_DNS_LABEL_LOOP;
|
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
static DOHcode rdata(const unsigned char *doh,
|
2018-09-06 03:16:02 -04:00
|
|
|
size_t dohlen,
|
|
|
|
unsigned short rdlength,
|
|
|
|
unsigned short type,
|
|
|
|
int index,
|
|
|
|
struct dohentry *d)
|
|
|
|
{
|
|
|
|
/* RDATA
|
|
|
|
- A (TYPE 1): 4 bytes
|
|
|
|
- AAAA (TYPE 28): 16 bytes
|
|
|
|
- NS (TYPE 2): N bytes */
|
|
|
|
DOHcode rc;
|
|
|
|
|
|
|
|
switch(type) {
|
|
|
|
case DNS_TYPE_A:
|
|
|
|
if(rdlength != 4)
|
|
|
|
return DOH_DNS_RDATA_LEN;
|
|
|
|
rc = store_a(doh, index, d);
|
|
|
|
if(rc)
|
|
|
|
return rc;
|
|
|
|
break;
|
|
|
|
case DNS_TYPE_AAAA:
|
|
|
|
if(rdlength != 16)
|
|
|
|
return DOH_DNS_RDATA_LEN;
|
|
|
|
rc = store_aaaa(doh, index, d);
|
|
|
|
if(rc)
|
|
|
|
return rc;
|
|
|
|
break;
|
|
|
|
case DNS_TYPE_CNAME:
|
|
|
|
rc = store_cname(doh, dohlen, index, d);
|
|
|
|
if(rc)
|
|
|
|
return rc;
|
|
|
|
break;
|
2019-11-14 14:21:09 -05:00
|
|
|
case DNS_TYPE_DNAME:
|
|
|
|
/* explicit for clarity; just skip; rely on synthesized CNAME */
|
|
|
|
break;
|
2018-09-06 03:16:02 -04:00
|
|
|
default:
|
|
|
|
/* unsupported type, just skip it */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return DOH_OK;
|
|
|
|
}
|
|
|
|
|
2020-05-02 11:04:08 -04:00
|
|
|
UNITTEST void de_init(struct dohentry *de)
|
2018-10-04 06:13:06 -04:00
|
|
|
{
|
2020-05-02 11:04:08 -04:00
|
|
|
int i;
|
2018-10-04 06:13:06 -04:00
|
|
|
memset(de, 0, sizeof(*de));
|
|
|
|
de->ttl = INT_MAX;
|
2020-05-02 11:04:08 -04:00
|
|
|
for(i = 0; i < DOH_MAX_CNAME; i++)
|
|
|
|
Curl_dyn_init(&de->cname[i], DYN_DOH_CNAME);
|
2018-10-04 06:13:06 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-04-27 06:07:34 -04:00
|
|
|
UNITTEST DOHcode doh_decode(const unsigned char *doh,
|
2018-06-19 10:08:05 -04:00
|
|
|
size_t dohlen,
|
|
|
|
DNStype dnstype,
|
|
|
|
struct dohentry *d)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
unsigned char rcode;
|
|
|
|
unsigned short qdcount;
|
|
|
|
unsigned short ancount;
|
|
|
|
unsigned short type = 0;
|
|
|
|
unsigned short rdlength;
|
|
|
|
unsigned short nscount;
|
|
|
|
unsigned short arcount;
|
|
|
|
unsigned int index = 12;
|
|
|
|
DOHcode rc;
|
|
|
|
|
|
|
|
if(dohlen < 12)
|
|
|
|
return DOH_TOO_SMALL_BUFFER; /* too small */
|
2018-12-05 09:21:27 -05:00
|
|
|
if(!doh || doh[0] || doh[1])
|
2018-09-06 03:16:02 -04:00
|
|
|
return DOH_DNS_BAD_ID; /* bad ID */
|
|
|
|
rcode = doh[3] & 0x0f;
|
|
|
|
if(rcode)
|
|
|
|
return DOH_DNS_BAD_RCODE; /* bad rcode */
|
|
|
|
|
|
|
|
qdcount = get16bit(doh, 4);
|
|
|
|
while(qdcount) {
|
|
|
|
rc = skipqname(doh, dohlen, &index);
|
|
|
|
if(rc)
|
|
|
|
return rc; /* bad qname */
|
|
|
|
if(dohlen < (index + 4))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
index += 4; /* skip question's type and class */
|
|
|
|
qdcount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
ancount = get16bit(doh, 6);
|
|
|
|
while(ancount) {
|
2019-05-11 15:42:48 -04:00
|
|
|
unsigned short class;
|
2018-09-06 03:16:02 -04:00
|
|
|
unsigned int ttl;
|
|
|
|
|
|
|
|
rc = skipqname(doh, dohlen, &index);
|
|
|
|
if(rc)
|
|
|
|
return rc; /* bad qname */
|
|
|
|
|
|
|
|
if(dohlen < (index + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
type = get16bit(doh, index);
|
2019-11-14 14:21:09 -05:00
|
|
|
if((type != DNS_TYPE_CNAME) /* may be synthesized from DNAME */
|
|
|
|
&& (type != DNS_TYPE_DNAME) /* if present, accept and ignore */
|
|
|
|
&& (type != dnstype))
|
|
|
|
/* Not the same type as was asked for nor CNAME nor DNAME */
|
2018-09-06 03:16:02 -04:00
|
|
|
return DOH_DNS_UNEXPECTED_TYPE;
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
if(dohlen < (index + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
class = get16bit(doh, index);
|
|
|
|
if(DNS_CLASS_IN != class)
|
|
|
|
return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
|
|
|
|
index += 2;
|
|
|
|
|
|
|
|
if(dohlen < (index + 4))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
ttl = get32bit(doh, index);
|
|
|
|
if(ttl < d->ttl)
|
|
|
|
d->ttl = ttl;
|
|
|
|
index += 4;
|
|
|
|
|
|
|
|
if(dohlen < (index + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
rdlength = get16bit(doh, index);
|
|
|
|
index += 2;
|
|
|
|
if(dohlen < (index + rdlength))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
rc = rdata(doh, dohlen, rdlength, type, index, d);
|
|
|
|
if(rc)
|
|
|
|
return rc; /* bad rdata */
|
|
|
|
index += rdlength;
|
|
|
|
ancount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
nscount = get16bit(doh, 8);
|
|
|
|
while(nscount) {
|
|
|
|
rc = skipqname(doh, dohlen, &index);
|
|
|
|
if(rc)
|
|
|
|
return rc; /* bad qname */
|
|
|
|
|
|
|
|
if(dohlen < (index + 8))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
index += 2 + 2 + 4; /* type, class and ttl */
|
|
|
|
|
|
|
|
if(dohlen < (index + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
rdlength = get16bit(doh, index);
|
|
|
|
index += 2;
|
|
|
|
if(dohlen < (index + rdlength))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
index += rdlength;
|
|
|
|
nscount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
arcount = get16bit(doh, 10);
|
|
|
|
while(arcount) {
|
|
|
|
rc = skipqname(doh, dohlen, &index);
|
|
|
|
if(rc)
|
|
|
|
return rc; /* bad qname */
|
|
|
|
|
|
|
|
if(dohlen < (index + 8))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
index += 2 + 2 + 4; /* type, class and ttl */
|
|
|
|
|
|
|
|
if(dohlen < (index + 2))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
|
|
|
|
rdlength = get16bit(doh, index);
|
|
|
|
index += 2;
|
|
|
|
if(dohlen < (index + rdlength))
|
|
|
|
return DOH_DNS_OUT_OF_RANGE;
|
|
|
|
index += rdlength;
|
|
|
|
arcount--;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(index != dohlen)
|
|
|
|
return DOH_DNS_MALFORMAT; /* something is wrong */
|
|
|
|
|
|
|
|
if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr)
|
|
|
|
/* nothing stored! */
|
|
|
|
return DOH_NO_CONTENT;
|
|
|
|
|
|
|
|
return DOH_OK; /* ok */
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:35:44 -04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2018-09-06 03:16:02 -04:00
|
|
|
static void showdoh(struct Curl_easy *data,
|
2020-04-27 06:07:34 -04:00
|
|
|
const struct dohentry *d)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
infof(data, "TTL: %u seconds\n", d->ttl);
|
|
|
|
for(i = 0; i < d->numaddr; i++) {
|
2020-04-27 06:07:34 -04:00
|
|
|
const struct dohaddr *a = &d->addr[i];
|
2018-09-06 03:16:02 -04:00
|
|
|
if(a->type == DNS_TYPE_A) {
|
2018-10-04 05:57:29 -04:00
|
|
|
infof(data, "DOH A: %u.%u.%u.%u\n",
|
|
|
|
a->ip.v4[0], a->ip.v4[1],
|
|
|
|
a->ip.v4[2], a->ip.v4[3]);
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
else if(a->type == DNS_TYPE_AAAA) {
|
|
|
|
int j;
|
|
|
|
char buffer[128];
|
|
|
|
char *ptr;
|
|
|
|
size_t len;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(buffer, 128, "DOH AAAA: ");
|
2018-09-06 03:16:02 -04:00
|
|
|
ptr = &buffer[10];
|
|
|
|
len = 118;
|
|
|
|
for(j = 0; j < 16; j += 2) {
|
|
|
|
size_t l;
|
2018-11-22 03:01:24 -05:00
|
|
|
msnprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6[j],
|
|
|
|
d->addr[i].ip.v6[j + 1]);
|
2018-09-06 03:16:02 -04:00
|
|
|
l = strlen(ptr);
|
|
|
|
len -= l;
|
|
|
|
ptr += l;
|
|
|
|
}
|
|
|
|
infof(data, "%s\n", buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for(i = 0; i < d->numcname; i++) {
|
2020-05-02 11:04:08 -04:00
|
|
|
infof(data, "CNAME: %s\n", Curl_dyn_ptr(&d->cname[i]));
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
}
|
2018-10-16 17:35:44 -04:00
|
|
|
#else
|
|
|
|
#define showdoh(x,y)
|
|
|
|
#endif
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
/*
|
|
|
|
* doh2ai()
|
|
|
|
*
|
|
|
|
* This function returns a pointer to the first element of a newly allocated
|
|
|
|
* Curl_addrinfo struct linked list filled with the data from a set of DOH
|
|
|
|
* lookups. Curl_addrinfo is meant to work like the addrinfo struct does for
|
|
|
|
* a IPv6 stack, but usable also for IPv4, all hosts and environments.
|
|
|
|
*
|
|
|
|
* The memory allocated by this function *MUST* be free'd later on calling
|
|
|
|
* Curl_freeaddrinfo(). For each successful call to this function there
|
|
|
|
* must be an associated call later to Curl_freeaddrinfo().
|
|
|
|
*/
|
|
|
|
|
2020-05-13 18:05:04 -04:00
|
|
|
static struct Curl_addrinfo *
|
2018-09-06 03:16:02 -04:00
|
|
|
doh2ai(const struct dohentry *de, const char *hostname, int port)
|
|
|
|
{
|
2020-05-13 18:05:04 -04:00
|
|
|
struct Curl_addrinfo *ai;
|
|
|
|
struct Curl_addrinfo *prevai = NULL;
|
|
|
|
struct Curl_addrinfo *firstai = NULL;
|
2018-09-06 03:16:02 -04:00
|
|
|
struct sockaddr_in *addr;
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
struct sockaddr_in6 *addr6;
|
|
|
|
#endif
|
|
|
|
CURLcode result = CURLE_OK;
|
|
|
|
int i;
|
2020-06-06 17:10:18 -04:00
|
|
|
size_t hostlen = strlen(hostname) + 1; /* include zero terminator */
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
if(!de)
|
|
|
|
/* no input == no output! */
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
for(i = 0; i < de->numaddr; i++) {
|
|
|
|
size_t ss_size;
|
|
|
|
CURL_SA_FAMILY_T addrtype;
|
|
|
|
if(de->addr[i].type == DNS_TYPE_AAAA) {
|
|
|
|
#ifndef ENABLE_IPV6
|
|
|
|
/* we can't handle IPv6 addresses */
|
|
|
|
continue;
|
|
|
|
#else
|
|
|
|
ss_size = sizeof(struct sockaddr_in6);
|
|
|
|
addrtype = AF_INET6;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
ss_size = sizeof(struct sockaddr_in);
|
|
|
|
addrtype = AF_INET;
|
|
|
|
}
|
|
|
|
|
2020-06-06 17:10:18 -04:00
|
|
|
ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(!ai) {
|
|
|
|
result = CURLE_OUT_OF_MEMORY;
|
|
|
|
break;
|
|
|
|
}
|
2020-06-06 17:10:18 -04:00
|
|
|
ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
|
|
|
|
ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
|
|
|
|
memcpy(ai->ai_canonname, hostname, hostlen);
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
if(!firstai)
|
|
|
|
/* store the pointer we want to return from this function */
|
|
|
|
firstai = ai;
|
|
|
|
|
|
|
|
if(prevai)
|
|
|
|
/* make the previous entry point to this */
|
|
|
|
prevai->ai_next = ai;
|
|
|
|
|
|
|
|
ai->ai_family = addrtype;
|
|
|
|
|
|
|
|
/* we return all names as STREAM, so when using this address for TFTP
|
|
|
|
the type must be ignored and conn->socktype be used instead! */
|
|
|
|
ai->ai_socktype = SOCK_STREAM;
|
|
|
|
|
|
|
|
ai->ai_addrlen = (curl_socklen_t)ss_size;
|
|
|
|
|
|
|
|
/* leave the rest of the struct filled with zero */
|
|
|
|
|
|
|
|
switch(ai->ai_family) {
|
|
|
|
case AF_INET:
|
|
|
|
addr = (void *)ai->ai_addr; /* storage area for this info */
|
2018-10-04 05:57:29 -04:00
|
|
|
DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
|
2018-09-06 03:16:02 -04:00
|
|
|
memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
|
2020-07-19 09:02:16 -04:00
|
|
|
addr->sin_family = addrtype;
|
2018-09-06 03:16:02 -04:00
|
|
|
addr->sin_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
|
|
|
|
#ifdef ENABLE_IPV6
|
|
|
|
case AF_INET6:
|
|
|
|
addr6 = (void *)ai->ai_addr; /* storage area for this info */
|
2018-10-04 05:57:29 -04:00
|
|
|
DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
|
2018-09-06 03:16:02 -04:00
|
|
|
memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
|
2020-07-19 09:02:16 -04:00
|
|
|
addr6->sin6_family = addrtype;
|
2018-09-06 03:16:02 -04:00
|
|
|
addr6->sin6_port = htons((unsigned short)port);
|
|
|
|
break;
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
prevai = ai;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(result) {
|
|
|
|
Curl_freeaddrinfo(firstai);
|
|
|
|
firstai = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return firstai;
|
|
|
|
}
|
|
|
|
|
2018-10-16 17:35:44 -04:00
|
|
|
#ifndef CURL_DISABLE_VERBOSE_STRINGS
|
2018-09-06 03:16:02 -04:00
|
|
|
static const char *type2name(DNStype dnstype)
|
|
|
|
{
|
|
|
|
return (dnstype == DNS_TYPE_A)?"A":"AAAA";
|
|
|
|
}
|
2018-10-16 17:35:44 -04:00
|
|
|
#endif
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2018-06-19 10:08:05 -04:00
|
|
|
UNITTEST void de_cleanup(struct dohentry *d)
|
2018-09-06 03:16:02 -04:00
|
|
|
{
|
|
|
|
int i = 0;
|
|
|
|
for(i = 0; i < d->numcname; i++) {
|
2020-05-02 11:04:08 -04:00
|
|
|
Curl_dyn_free(&d->cname[i]);
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
CURLcode Curl_doh_is_resolved(struct connectdata *conn,
|
|
|
|
struct Curl_dns_entry **dnsp)
|
|
|
|
{
|
2019-11-21 07:15:44 -05:00
|
|
|
CURLcode result;
|
2018-09-06 03:16:02 -04:00
|
|
|
struct Curl_easy *data = conn->data;
|
2021-01-19 09:57:24 -05:00
|
|
|
struct dohdata *dohp = data->req.doh;
|
2018-09-06 03:16:02 -04:00
|
|
|
*dnsp = NULL; /* defaults to no response */
|
2021-01-21 17:31:19 -05:00
|
|
|
if(!dohp)
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
if(!dohp->probe[DOH_PROBE_SLOT_IPADDR_V4].easy &&
|
|
|
|
!dohp->probe[DOH_PROBE_SLOT_IPADDR_V6].easy) {
|
2018-09-06 03:16:02 -04:00
|
|
|
failf(data, "Could not DOH-resolve: %s", conn->async.hostname);
|
|
|
|
return conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
|
|
|
|
CURLE_COULDNT_RESOLVE_HOST;
|
|
|
|
}
|
2021-01-19 09:57:24 -05:00
|
|
|
else if(!dohp->pending) {
|
2020-05-02 11:04:08 -04:00
|
|
|
DOHcode rc[DOH_PROBE_SLOTS] = {
|
|
|
|
DOH_OK, DOH_OK
|
|
|
|
};
|
2018-09-06 03:16:02 -04:00
|
|
|
struct dohentry de;
|
2019-11-21 07:15:44 -05:00
|
|
|
int slot;
|
2018-09-06 03:16:02 -04:00
|
|
|
/* remove DOH handles from multi handle and close them */
|
2019-11-21 07:15:44 -05:00
|
|
|
for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
|
2021-01-19 09:57:24 -05:00
|
|
|
curl_multi_remove_handle(data->multi, dohp->probe[slot].easy);
|
|
|
|
Curl_close(&dohp->probe[slot].easy);
|
2019-11-21 07:15:44 -05:00
|
|
|
}
|
2018-09-06 03:16:02 -04:00
|
|
|
/* parse the responses, create the struct and return it! */
|
2020-05-02 11:04:08 -04:00
|
|
|
de_init(&de);
|
2019-11-21 07:15:44 -05:00
|
|
|
for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
|
2021-01-19 09:57:24 -05:00
|
|
|
struct dnsprobe *p = &dohp->probe[slot];
|
2020-05-02 11:04:08 -04:00
|
|
|
if(!p->dnstype)
|
|
|
|
continue;
|
|
|
|
rc[slot] = doh_decode(Curl_dyn_uptr(&p->serverdoh),
|
|
|
|
Curl_dyn_len(&p->serverdoh),
|
|
|
|
p->dnstype,
|
2019-11-21 07:15:44 -05:00
|
|
|
&de);
|
2020-05-02 11:04:08 -04:00
|
|
|
Curl_dyn_free(&p->serverdoh);
|
2019-11-21 07:15:44 -05:00
|
|
|
if(rc[slot]) {
|
|
|
|
infof(data, "DOH: %s type %s for %s\n", doh_strerror(rc[slot]),
|
2021-01-19 09:57:24 -05:00
|
|
|
type2name(p->dnstype), dohp->host);
|
2019-11-21 07:15:44 -05:00
|
|
|
}
|
|
|
|
} /* next slot */
|
|
|
|
|
|
|
|
result = CURLE_COULDNT_RESOLVE_HOST; /* until we know better */
|
|
|
|
if(!rc[DOH_PROBE_SLOT_IPADDR_V4] || !rc[DOH_PROBE_SLOT_IPADDR_V6]) {
|
|
|
|
/* we have an address, of one kind or other */
|
2019-05-11 15:42:48 -04:00
|
|
|
struct Curl_dns_entry *dns;
|
|
|
|
struct Curl_addrinfo *ai;
|
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
infof(data, "DOH Host name: %s\n", dohp->host);
|
2018-09-06 03:16:02 -04:00
|
|
|
showdoh(data, &de);
|
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
ai = doh2ai(&de, dohp->host, dohp->port);
|
2018-09-06 03:16:02 -04:00
|
|
|
if(!ai) {
|
|
|
|
de_cleanup(&de);
|
|
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
|
|
|
|
|
|
|
|
/* we got a response, store it in the cache */
|
2021-01-19 09:57:24 -05:00
|
|
|
dns = Curl_cache_addr(data, ai, dohp->host, dohp->port);
|
2018-09-06 03:16:02 -04:00
|
|
|
|
|
|
|
if(data->share)
|
|
|
|
Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
|
|
|
|
|
2019-11-21 07:15:44 -05:00
|
|
|
if(!dns) {
|
2018-09-06 03:16:02 -04:00
|
|
|
/* returned failure, bail out nicely */
|
|
|
|
Curl_freeaddrinfo(ai);
|
2019-11-21 07:15:44 -05:00
|
|
|
}
|
2018-09-06 03:16:02 -04:00
|
|
|
else {
|
|
|
|
conn->async.dns = dns;
|
|
|
|
*dnsp = dns;
|
2019-11-21 07:15:44 -05:00
|
|
|
result = CURLE_OK; /* address resolution OK */
|
2018-09-06 03:16:02 -04:00
|
|
|
}
|
2019-11-21 07:15:44 -05:00
|
|
|
} /* address processing done */
|
|
|
|
|
|
|
|
/* Now process any build-specific attributes retrieved from DNS */
|
|
|
|
|
|
|
|
/* All done */
|
2018-09-06 03:16:02 -04:00
|
|
|
de_cleanup(&de);
|
2021-01-19 09:57:24 -05:00
|
|
|
Curl_safefree(data->req.doh);
|
2019-11-21 07:15:44 -05:00
|
|
|
return result;
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2021-01-19 09:57:24 -05:00
|
|
|
} /* !dohp->pending */
|
2018-09-06 03:16:02 -04:00
|
|
|
|
2019-11-21 07:15:44 -05:00
|
|
|
/* else wait for pending DOH transactions to complete */
|
2018-09-06 03:16:02 -04:00
|
|
|
return CURLE_OK;
|
|
|
|
}
|
2019-05-05 11:08:21 -04:00
|
|
|
|
|
|
|
#endif /* CURL_DISABLE_DOH */
|