Coverage Report

Created: 2026-04-12 06:56

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/curl/lib/doh.c
Line
Count
Source
1
/***************************************************************************
2
 *                                  _   _ ____  _
3
 *  Project                     ___| | | |  _ \| |
4
 *                             / __| | | | |_) | |
5
 *                            | (__| |_| |  _ <| |___
6
 *                             \___|\___/|_| \_\_____|
7
 *
8
 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
9
 *
10
 * This software is licensed as described in the file COPYING, which
11
 * you should have received as part of this distribution. The terms
12
 * are also available at https://curl.se/docs/copyright.html.
13
 *
14
 * You may opt to use, copy, modify, merge, publish, distribute and/or sell
15
 * copies of the Software, and permit persons to whom the Software is
16
 * furnished to do so, under the terms of the COPYING file.
17
 *
18
 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
19
 * KIND, either express or implied.
20
 *
21
 * SPDX-License-Identifier: curl
22
 *
23
 ***************************************************************************/
24
#include "curl_setup.h"
25
26
#ifndef CURL_DISABLE_DOH
27
28
#include "urldata.h"
29
#include "curl_addrinfo.h"
30
#include "doh.h"
31
#include "curl_trc.h"
32
#include "httpsrr.h"
33
#include "multiif.h"
34
#include "url.h"
35
#include "connect.h"
36
#include "curlx/strdup.h"
37
#include "curlx/dynbuf.h"
38
#include "escape.h"  /* for Curl_hexencode() */
39
#include "urlapi-int.h"
40
41
0
#define DNS_CLASS_IN 0x01
42
43
static void doh_close(struct Curl_easy *data,
44
                      struct Curl_resolv_async *async);
45
46
#ifdef CURLVERBOSE
47
static const char * const errors[] = {
48
  "",
49
  "Bad label",
50
  "Out of range",
51
  "Label loop",
52
  "Too small",
53
  "Out of memory",
54
  "RDATA length",
55
  "Malformat",
56
  "Bad RCODE",
57
  "Unexpected TYPE",
58
  "Unexpected CLASS",
59
  "No content",
60
  "Bad ID",
61
  "Name too long"
62
};
63
64
static const char *doh_strerror(DOHcode code)
65
0
{
66
0
  if((code >= DOH_OK) && (code <= DOH_DNS_NAME_TOO_LONG))
67
0
    return errors[code];
68
0
  return "bad error code";
69
0
}
70
71
#endif /* CURLVERBOSE */
72
73
/* @unittest 1655
74
 */
75
UNITTEST DOHcode doh_req_encode(const char *host,
76
                                DNStype dnstype,
77
                                unsigned char *dnsp,  /* buffer */
78
                                size_t len,  /* buffer size */
79
                                size_t *olen);  /* output length */
80
UNITTEST DOHcode doh_req_encode(const char *host,
81
                                DNStype dnstype,
82
                                unsigned char *dnsp, /* buffer */
83
                                size_t len,   /* buffer size */
84
                                size_t *olen) /* output length */
85
0
{
86
0
  const size_t hostlen = strlen(host);
87
0
  unsigned char *orig = dnsp;
88
0
  const char *hostp = host;
89
90
  /* The expected output length is 16 bytes more than the length of
91
   * the QNAME-encoding of the hostname.
92
   *
93
   * A valid DNS name may not contain a zero-length label, except at
94
   * the end. For this reason, a name beginning with a dot, or
95
   * containing a sequence of two or more consecutive dots, is invalid
96
   * and cannot be encoded as a QNAME.
97
   *
98
   * If the hostname ends with a trailing dot, the corresponding
99
   * QNAME-encoding is one byte longer than the hostname. If (as is
100
   * also valid) the hostname is shortened by the omission of the
101
   * trailing dot, then its QNAME-encoding will be two bytes longer
102
   * than the hostname.
103
   *
104
   * Each [ label, dot ] pair is encoded as [ length, label ],
105
   * preserving overall length. A final [ label ] without a dot is
106
   * also encoded as [ length, label ], increasing overall length
107
   * by one. The encoding is completed by appending a zero byte,
108
   * representing the zero-length root label, again increasing
109
   * the overall length by one.
110
   */
111
112
0
  size_t expected_len;
113
0
  DEBUGASSERT(hostlen);
114
0
  expected_len = 12 + 1 + hostlen + 4;
115
0
  if(host[hostlen - 1] != '.')
116
0
    expected_len++;
117
118
0
  if(expected_len > DOH_MAX_DNSREQ_SIZE)
119
0
    return DOH_DNS_NAME_TOO_LONG;
120
121
0
  if(len < expected_len)
122
0
    return DOH_TOO_SMALL_BUFFER;
123
124
0
  *dnsp++ = 0; /* 16-bit id */
125
0
  *dnsp++ = 0;
126
0
  *dnsp++ = 0x01; /* |QR|   Opcode  |AA|TC|RD| Set the RD bit */
127
0
  *dnsp++ = '\0'; /* |RA|   Z    |   RCODE   |                */
128
0
  *dnsp++ = '\0';
129
0
  *dnsp++ = 1;    /* QDCOUNT (number of entries in the question section) */
130
0
  *dnsp++ = '\0';
131
0
  *dnsp++ = '\0'; /* ANCOUNT */
132
0
  *dnsp++ = '\0';
133
0
  *dnsp++ = '\0'; /* NSCOUNT */
134
0
  *dnsp++ = '\0';
135
0
  *dnsp++ = '\0'; /* ARCOUNT */
136
137
  /* encode each label and store it in the QNAME */
138
0
  while(*hostp) {
139
0
    size_t labellen;
140
0
    const char *dot = strchr(hostp, '.');
141
0
    if(dot)
142
0
      labellen = dot - hostp;
143
0
    else
144
0
      labellen = strlen(hostp);
145
0
    if((labellen > 63) || (!labellen)) {
146
      /* label is too long or too short, error out */
147
0
      *olen = 0;
148
0
      return DOH_DNS_BAD_LABEL;
149
0
    }
150
    /* label is non-empty, process it */
151
0
    *dnsp++ = (unsigned char)labellen;
152
0
    memcpy(dnsp, hostp, labellen);
153
0
    dnsp += labellen;
154
0
    hostp += labellen;
155
    /* advance past dot, but only if there is one */
156
0
    if(dot)
157
0
      hostp++;
158
0
  } /* next label */
159
160
0
  *dnsp++ = 0; /* append zero-length label for root */
161
162
  /* There are assigned TYPE codes beyond 255: use range [1..65535] */
163
0
  *dnsp++ = (unsigned char)(255 & (dnstype >> 8)); /* upper 8-bit TYPE */
164
0
  *dnsp++ = (unsigned char)(255 & dnstype);        /* lower 8-bit TYPE */
165
166
0
  *dnsp++ = '\0'; /* upper 8-bit CLASS */
167
0
  *dnsp++ = DNS_CLASS_IN; /* IN - "the Internet" */
168
169
0
  *olen = dnsp - orig;
170
171
  /* verify that our estimation of length is valid, since
172
   * this has led to buffer overflows in this function */
173
0
  DEBUGASSERT(*olen == expected_len);
174
0
  return DOH_OK;
175
0
}
176
177
static size_t doh_probe_write_cb(char *contents, size_t size, size_t nmemb,
178
                                 void *userp)
179
0
{
180
0
  size_t realsize = size * nmemb;
181
0
  struct Curl_easy *data = userp;
182
0
  struct doh_request *doh_req = Curl_meta_get(data, CURL_EZM_DOH_PROBE);
183
0
  if(!doh_req)
184
0
    return CURL_WRITEFUNC_ERROR;
185
186
0
  if(curlx_dyn_addn(&doh_req->resp_body, contents, realsize))
187
0
    return 0;
188
189
0
  return realsize;
190
0
}
191
192
#if defined(USE_HTTPSRR) && defined(DEBUGBUILD) && defined(CURLVERBOSE)
193
194
/* doh_print_buf truncates if the hex string will be more than this */
195
#define LOCAL_PB_HEXMAX 400
196
197
static void doh_print_buf(struct Curl_easy *data,
198
                          const char *prefix,
199
                          unsigned char *buf, size_t len)
200
{
201
  unsigned char hexstr[LOCAL_PB_HEXMAX];
202
  size_t hlen = LOCAL_PB_HEXMAX;
203
  bool truncated = FALSE;
204
205
  if(len > (LOCAL_PB_HEXMAX / 2))
206
    truncated = TRUE;
207
  Curl_hexencode(buf, len, hexstr, hlen);
208
  if(!truncated)
209
    infof(data, "%s: len=%d, val=%s", prefix, (int)len, hexstr);
210
  else
211
    infof(data, "%s: len=%d (truncated)val=%s", prefix, (int)len, hexstr);
212
}
213
#endif
214
215
/* called from multi when a sub transfer, e.g. doh probe, is done.
216
 * This looks up the probe response at its meta CURL_EZM_DOH_PROBE
217
 * and copies the response body over to the struct at the master's
218
 * meta at CURL_EZM_DOH_MASTER. */
219
static void doh_probe_done(struct Curl_easy *data,
220
                           struct Curl_easy *doh, CURLcode result)
221
0
{
222
0
  struct Curl_resolv_async *async = NULL;
223
0
  struct doh_probes *dohp = NULL;
224
0
  struct doh_request *doh_req = NULL;
225
0
  int i;
226
227
0
  doh_req = Curl_meta_get(doh, CURL_EZM_DOH_PROBE);
228
0
  if(!doh_req) {
229
0
    DEBUGASSERT(0);
230
0
    return;
231
0
  }
232
233
0
  async = Curl_async_get(data, doh_req->resolv_id);
234
0
  if(!async) {
235
0
    CURL_TRC_DNS(data, "[%u] ignoring outdated DoH response",
236
0
                 doh_req->resolv_id);
237
0
    return;
238
0
  }
239
0
  dohp = async->doh;
240
241
0
  for(i = 0; i < DOH_SLOT_COUNT; ++i) {
242
0
    if(dohp->probe_resp[i].probe_mid == doh->mid)
243
0
      break;
244
0
  }
245
  /* We really should have found the slot where to store the response */
246
0
  if(i >= DOH_SLOT_COUNT) {
247
0
    DEBUGASSERT(0);
248
0
    failf(data, "DoH: unknown sub request done");
249
0
    return;
250
0
  }
251
252
0
  dohp->pending--;
253
0
  infof(doh, "a DoH request is completed, %u to go", dohp->pending);
254
0
  dohp->probe_resp[i].result = result;
255
  /* We expect either the meta data still to exist or the sub request
256
   * to have already failed. */
257
0
  if(!result) {
258
0
    dohp->probe_resp[i].dnstype = doh_req->dnstype;
259
0
    result = curlx_dyn_addn(&dohp->probe_resp[i].body,
260
0
                            curlx_dyn_ptr(&doh_req->resp_body),
261
0
                            curlx_dyn_len(&doh_req->resp_body));
262
0
    curlx_dyn_free(&doh_req->resp_body);
263
0
  }
264
0
  Curl_meta_remove(doh, CURL_EZM_DOH_PROBE);
265
266
0
  if(result)
267
0
    infof(doh, "DoH request %s", curl_easy_strerror(result));
268
269
0
  if(!dohp->pending) {
270
    /* DoH completed, run the transfer picking up the results */
271
0
    Curl_multi_mark_dirty(data);
272
0
  }
273
0
}
274
275
static void doh_probe_dtor(void *key, size_t klen, void *e)
276
0
{
277
0
  (void)key;
278
0
  (void)klen;
279
0
  if(e) {
280
0
    struct doh_request *doh_req = e;
281
0
    curl_slist_free_all(doh_req->req_hds);
282
0
    curlx_dyn_free(&doh_req->resp_body);
283
0
    curlx_free(e);
284
0
  }
285
0
}
286
287
#define ERROR_CHECK_SETOPT(x, y)                        \
288
0
  do {                                                  \
289
0
    result = curl_easy_setopt((CURL *)doh, x, y);       \
290
0
    if(result &&                                        \
291
0
       result != CURLE_NOT_BUILT_IN &&                  \
292
0
       result != CURLE_UNKNOWN_OPTION)                  \
293
0
      goto error;                                       \
294
0
  } while(0)
295
296
static CURLcode doh_probe_run(struct Curl_easy *data,
297
                              DNStype dnstype,
298
                              const char *host,
299
                              const char *url, CURLM *multi,
300
                              uint32_t resolv_id,
301
                              uint32_t *pmid)
302
0
{
303
0
  struct Curl_easy *doh = NULL;
304
0
  CURLcode result = CURLE_OK;
305
0
  timediff_t timeout_ms;
306
0
  struct doh_request *doh_req;
307
0
  DOHcode d;
308
309
0
  *pmid = UINT32_MAX;
310
311
0
  doh_req = curlx_calloc(1, sizeof(*doh_req));
312
0
  if(!doh_req)
313
0
    return CURLE_OUT_OF_MEMORY;
314
0
  doh_req->resolv_id = resolv_id;
315
0
  doh_req->dnstype = dnstype;
316
0
  curlx_dyn_init(&doh_req->resp_body, DYN_DOH_RESPONSE);
317
318
0
  d = doh_req_encode(host, dnstype, doh_req->req_body,
319
0
                     sizeof(doh_req->req_body),
320
0
                     &doh_req->req_body_len);
321
0
  if(d) {
322
0
    failf(data, "Failed to encode DoH packet [%d]", d);
323
0
    result = CURLE_OUT_OF_MEMORY;
324
0
    goto error;
325
0
  }
326
327
0
  timeout_ms = Curl_timeleft_ms(data);
328
0
  if(timeout_ms < 0) {
329
0
    result = CURLE_OPERATION_TIMEDOUT;
330
0
    goto error;
331
0
  }
332
333
0
  doh_req->req_hds =
334
0
    curl_slist_append(NULL, "Content-Type: application/dns-message");
335
0
  if(!doh_req->req_hds) {
336
0
    result = CURLE_OUT_OF_MEMORY;
337
0
    goto error;
338
0
  }
339
340
  /* Curl_open() is the internal version of curl_easy_init() */
341
0
  result = Curl_open(&doh);
342
0
  if(result)
343
0
    goto error;
344
345
  /* pass in the struct pointer via a local variable to please coverity and
346
     the gcc typecheck helpers */
347
0
  VERBOSE(doh->state.feat = &Curl_trc_feat_dns);
348
0
  ERROR_CHECK_SETOPT(CURLOPT_URL, url);
349
0
  ERROR_CHECK_SETOPT(CURLOPT_DEFAULT_PROTOCOL, "https");
350
0
  ERROR_CHECK_SETOPT(CURLOPT_WRITEFUNCTION, doh_probe_write_cb);
351
0
  ERROR_CHECK_SETOPT(CURLOPT_WRITEDATA, doh);
352
0
  ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDS, doh_req->req_body);
353
0
  ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDSIZE, (long)doh_req->req_body_len);
354
0
  ERROR_CHECK_SETOPT(CURLOPT_HTTPHEADER, doh_req->req_hds);
355
0
#ifdef USE_HTTP2
356
0
  ERROR_CHECK_SETOPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
357
0
  ERROR_CHECK_SETOPT(CURLOPT_PIPEWAIT, 1L);
358
0
#endif
359
#ifndef DEBUGBUILD
360
  /* enforce HTTPS if not debug */
361
  ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
362
#else
363
  /* in debug mode, also allow http */
364
0
  ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTP | CURLPROTO_HTTPS);
365
0
#endif
366
0
  ERROR_CHECK_SETOPT(CURLOPT_TIMEOUT_MS, (long)timeout_ms);
367
0
  ERROR_CHECK_SETOPT(CURLOPT_SHARE, (CURLSH *)data->share);
368
0
  if(data->set.err && data->set.err != stderr)
369
0
    ERROR_CHECK_SETOPT(CURLOPT_STDERR, data->set.err);
370
0
  if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_dns))
371
0
    ERROR_CHECK_SETOPT(CURLOPT_VERBOSE, 1L);
372
0
  if(data->set.no_signal)
373
0
    ERROR_CHECK_SETOPT(CURLOPT_NOSIGNAL, 1L);
374
375
0
  ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYHOST,
376
0
                     data->set.doh_verifyhost ? 2L : 0L);
377
0
  ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYPEER,
378
0
                     data->set.doh_verifypeer ? 1L : 0L);
379
0
  ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYSTATUS,
380
0
                     data->set.doh_verifystatus ? 1L : 0L);
381
382
  /* Inherit *some* SSL options from the user's transfer. This is a
383
     best-guess as to which options are needed for compatibility. #3661
384
385
     Note DoH does not inherit the user's proxy server so proxy SSL settings
386
     have no effect and are not inherited. If that changes then two new
387
     options should be added to check doh proxy insecure separately,
388
     CURLOPT_DOH_PROXY_SSL_VERIFYHOST and CURLOPT_DOH_PROXY_SSL_VERIFYPEER.
389
     */
390
0
  doh->set.ssl.custom_cafile = data->set.ssl.custom_cafile;
391
0
  doh->set.ssl.custom_capath = data->set.ssl.custom_capath;
392
0
  doh->set.ssl.custom_cablob = data->set.ssl.custom_cablob;
393
0
  if(data->set.str[STRING_SSL_CAFILE]) {
394
0
    ERROR_CHECK_SETOPT(CURLOPT_CAINFO, data->set.str[STRING_SSL_CAFILE]);
395
0
  }
396
0
  if(data->set.blobs[BLOB_CAINFO]) {
397
0
    ERROR_CHECK_SETOPT(CURLOPT_CAINFO_BLOB, data->set.blobs[BLOB_CAINFO]);
398
0
  }
399
0
  if(data->set.str[STRING_SSL_CAPATH]) {
400
0
    ERROR_CHECK_SETOPT(CURLOPT_CAPATH, data->set.str[STRING_SSL_CAPATH]);
401
0
  }
402
0
  if(data->set.str[STRING_SSL_CRLFILE]) {
403
0
    ERROR_CHECK_SETOPT(CURLOPT_CRLFILE, data->set.str[STRING_SSL_CRLFILE]);
404
0
  }
405
0
  if(data->set.ssl.certinfo)
406
0
    ERROR_CHECK_SETOPT(CURLOPT_CERTINFO, 1L);
407
0
  if(data->set.ssl.fsslctx)
408
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_FUNCTION, data->set.ssl.fsslctx);
409
0
  if(data->set.ssl.fsslctxp)
410
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_DATA, data->set.ssl.fsslctxp);
411
0
  if(data->set.fdebug)
412
0
    ERROR_CHECK_SETOPT(CURLOPT_DEBUGFUNCTION, data->set.fdebug);
413
0
  if(data->set.debugdata)
414
0
    ERROR_CHECK_SETOPT(CURLOPT_DEBUGDATA, data->set.debugdata);
415
0
  if(data->set.str[STRING_SSL_EC_CURVES]) {
416
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_EC_CURVES,
417
0
                       data->set.str[STRING_SSL_EC_CURVES]);
418
0
  }
419
420
0
  (void)curl_easy_setopt(doh, CURLOPT_SSL_OPTIONS,
421
0
                         (long)data->set.ssl.primary.ssl_options);
422
423
0
  doh->state.internal = TRUE;
424
0
  doh->master_mid = data->mid; /* master transfer of this one */
425
426
0
  result = Curl_meta_set(doh, CURL_EZM_DOH_PROBE, doh_req, doh_probe_dtor);
427
0
  doh_req = NULL; /* call took ownership */
428
0
  if(result)
429
0
    goto error;
430
431
  /* DoH handles must not inherit private_data. The handles may be passed to
432
     the user via callbacks and the user will be able to identify them as
433
     internal handles because private data is not set. The user can then set
434
     private_data via CURLOPT_PRIVATE if they so choose. */
435
0
  DEBUGASSERT(!doh->set.private_data);
436
437
0
  if(curl_multi_add_handle(multi, doh))
438
0
    goto error;
439
440
0
  *pmid = doh->mid;
441
0
  return CURLE_OK;
442
443
0
error:
444
0
  Curl_close(&doh);
445
0
  if(doh_req)
446
0
    doh_probe_dtor(NULL, 0, doh_req);
447
0
  return result;
448
0
}
449
450
/*
451
 * Curl_doh() starts a name resolve using DoH. It resolves a name and returns
452
 * a 'Curl_addrinfo *' with the address information.
453
 */
454
455
CURLcode Curl_doh(struct Curl_easy *data,
456
                  struct Curl_resolv_async *async)
457
0
{
458
0
  CURLcode result = CURLE_OK;
459
0
  struct doh_probes *dohp = NULL;
460
0
  struct connectdata *conn = data->conn;
461
0
  size_t i;
462
463
0
  DEBUGASSERT(conn);
464
0
  DEBUGASSERT(!async->doh);
465
0
  DEBUGASSERT(async->hostname[0]);
466
0
  if(async->doh)
467
0
    Curl_doh_cleanup(data, async);
468
469
  /* start clean, consider allocating this struct on demand */
470
0
  async->doh = dohp = curlx_calloc(1, sizeof(struct doh_probes));
471
0
  if(!dohp)
472
0
    return CURLE_OUT_OF_MEMORY;
473
474
0
  for(i = 0; i < DOH_SLOT_COUNT; ++i) {
475
0
    dohp->probe_resp[i].probe_mid = UINT32_MAX;
476
0
    curlx_dyn_init(&dohp->probe_resp[i].body, DYN_DOH_RESPONSE);
477
0
  }
478
479
0
  conn->bits.doh = TRUE;
480
0
  dohp->host = async->hostname;
481
0
  dohp->port = async->port;
482
  /* We are making sub easy handles and want to be called back when
483
   * one is done. */
484
0
  data->sub_xfer_done = doh_probe_done;
485
486
  /* create IPv4 DoH request */
487
0
  if(async->dns_queries & CURL_DNSQ_A) {
488
0
    result = doh_probe_run(data, CURL_DNS_TYPE_A,
489
0
                           async->hostname, data->set.str[STRING_DOH],
490
0
                           data->multi, async->id,
491
0
                           &dohp->probe_resp[DOH_SLOT_IPV4].probe_mid);
492
0
    if(result)
493
0
      goto error;
494
0
    dohp->pending++;
495
0
  }
496
497
0
#ifdef USE_IPV6
498
0
  if(async->dns_queries & CURL_DNSQ_AAAA) {
499
    /* create IPv6 DoH request */
500
0
    result = doh_probe_run(data, CURL_DNS_TYPE_AAAA,
501
0
                           async->hostname, data->set.str[STRING_DOH],
502
0
                           data->multi, async->id,
503
0
                           &dohp->probe_resp[DOH_SLOT_IPV6].probe_mid);
504
0
    if(result)
505
0
      goto error;
506
0
    dohp->pending++;
507
0
  }
508
0
#endif
509
510
#ifdef USE_HTTPSRR
511
  if(async->dns_queries & CURL_DNSQ_HTTPS) {
512
    char *qname = NULL;
513
    if(async->port != PORT_HTTPS) {
514
      qname = curl_maprintf("_%d._https.%s", async->port, async->hostname);
515
      if(!qname)
516
        goto error;
517
    }
518
    result = doh_probe_run(data, CURL_DNS_TYPE_HTTPS,
519
                           qname ? qname : async->hostname,
520
                           data->set.str[STRING_DOH], data->multi,
521
                           async->id,
522
                           &dohp->probe_resp[DOH_SLOT_HTTPS_RR].probe_mid);
523
    curlx_free(qname);
524
    if(result)
525
      goto error;
526
    dohp->pending++;
527
  }
528
#endif
529
0
  return CURLE_OK;
530
531
0
error:
532
0
  Curl_doh_cleanup(data, async);
533
0
  return result;
534
0
}
535
536
static DOHcode doh_skipqname(const unsigned char *doh, size_t dohlen,
537
                             unsigned int *indexp)
538
0
{
539
0
  unsigned char length;
540
0
  do {
541
0
    if(dohlen < (*indexp + 1))
542
0
      return DOH_DNS_OUT_OF_RANGE;
543
0
    length = doh[*indexp];
544
0
    if((length & 0xc0) == 0xc0) {
545
      /* name pointer, advance over it and be done */
546
0
      if(dohlen < (*indexp + 2))
547
0
        return DOH_DNS_OUT_OF_RANGE;
548
0
      *indexp += 2;
549
0
      break;
550
0
    }
551
0
    if(length & 0xc0)
552
0
      return DOH_DNS_BAD_LABEL;
553
0
    if(dohlen < (*indexp + 1 + length))
554
0
      return DOH_DNS_OUT_OF_RANGE;
555
0
    *indexp += (unsigned int)(1 + length);
556
0
  } while(length);
557
0
  return DOH_OK;
558
0
}
559
560
static unsigned short doh_get16bit(const unsigned char *doh,
561
                                   unsigned int index)
562
0
{
563
0
  return (unsigned short)((doh[index] << 8) | doh[index + 1]);
564
0
}
565
566
static unsigned int doh_get32bit(const unsigned char *doh, unsigned int index)
567
0
{
568
  /* make clang and gcc optimize this to bswap by incrementing
569
     the pointer first. */
570
0
  doh += index;
571
572
  /* avoid undefined behavior by casting to unsigned before shifting
573
     24 bits, possibly into the sign bit. codegen is same, but
574
     ub sanitizer will not be upset */
575
0
  return ((unsigned)doh[0] << 24) | ((unsigned)doh[1] << 16) |
576
0
         ((unsigned)doh[2] << 8) | doh[3];
577
0
}
578
579
static void doh_store_a(const unsigned char *doh, int index,
580
                        struct dohentry *d)
581
0
{
582
  /* silently ignore addresses over the limit */
583
0
  if(d->numaddr < DOH_MAX_ADDR) {
584
0
    struct dohaddr *a = &d->addr[d->numaddr];
585
0
    a->type = CURL_DNS_TYPE_A;
586
0
    memcpy(&a->ip.v4, &doh[index], 4);
587
0
    d->numaddr++;
588
0
  }
589
0
}
590
591
static void doh_store_aaaa(const unsigned char *doh, int index,
592
                           struct dohentry *d)
593
0
{
594
  /* silently ignore addresses over the limit */
595
0
  if(d->numaddr < DOH_MAX_ADDR) {
596
0
    struct dohaddr *a = &d->addr[d->numaddr];
597
0
    a->type = CURL_DNS_TYPE_AAAA;
598
0
    memcpy(&a->ip.v6, &doh[index], 16);
599
0
    d->numaddr++;
600
0
  }
601
0
}
602
603
#ifdef USE_HTTPSRR
604
static DOHcode doh_store_https(const unsigned char *doh, int index,
605
                               struct dohentry *d, uint16_t len)
606
{
607
  /* silently ignore RRs over the limit */
608
  if(d->numhttps_rrs < DOH_MAX_HTTPS) {
609
    struct dohhttps_rr *h = &d->https_rrs[d->numhttps_rrs];
610
    h->val = curlx_memdup(&doh[index], len);
611
    if(!h->val)
612
      return DOH_OUT_OF_MEM;
613
    h->len = len;
614
    d->numhttps_rrs++;
615
  }
616
  return DOH_OK;
617
}
618
#endif
619
620
static DOHcode doh_store_cname(const unsigned char *doh, size_t dohlen,
621
                               unsigned int index, struct dohentry *d)
622
0
{
623
0
  struct dynbuf *c;
624
0
  unsigned int loop = 128; /* a valid DNS name can never loop this much */
625
0
  unsigned char length;
626
627
0
  if(d->numcname == DOH_MAX_CNAME)
628
0
    return DOH_OK; /* skip! */
629
630
0
  c = &d->cname[d->numcname++];
631
0
  do {
632
0
    if(index >= dohlen)
633
0
      return DOH_DNS_OUT_OF_RANGE;
634
0
    length = doh[index];
635
0
    if((length & 0xc0) == 0xc0) {
636
0
      int newpos;
637
      /* name pointer, get the new offset (14 bits) */
638
0
      if((index + 1) >= dohlen)
639
0
        return DOH_DNS_OUT_OF_RANGE;
640
641
      /* move to the new index */
642
0
      newpos = (length & 0x3f) << 8 | doh[index + 1];
643
0
      index = (unsigned int)newpos;
644
0
      continue;
645
0
    }
646
0
    else if(length & 0xc0)
647
0
      return DOH_DNS_BAD_LABEL; /* bad input */
648
0
    else
649
0
      index++;
650
651
0
    if(length) {
652
0
      if(curlx_dyn_len(c)) {
653
0
        if(curlx_dyn_addn(c, STRCONST(".")))
654
0
          return DOH_OUT_OF_MEM;
655
0
      }
656
0
      if((index + length) > dohlen)
657
0
        return DOH_DNS_BAD_LABEL;
658
659
0
      if(curlx_dyn_addn(c, &doh[index], length))
660
0
        return DOH_OUT_OF_MEM;
661
0
      index += length;
662
0
    }
663
0
  } while(length && --loop);
664
665
0
  if(!loop)
666
0
    return DOH_DNS_LABEL_LOOP;
667
0
  return DOH_OK;
668
0
}
669
670
static DOHcode doh_rdata(const unsigned char *doh,
671
                         size_t dohlen,
672
                         unsigned short rdlength,
673
                         unsigned short type,
674
                         int index,
675
                         struct dohentry *d)
676
0
{
677
  /* RDATA
678
     - A (TYPE 1): 4 bytes
679
     - AAAA (TYPE 28): 16 bytes
680
     - NS (TYPE 2): N bytes
681
     - HTTPS (TYPE 65): N bytes */
682
0
  DOHcode rc;
683
684
0
  switch(type) {
685
0
  case CURL_DNS_TYPE_A:
686
0
    if(rdlength != 4)
687
0
      return DOH_DNS_RDATA_LEN;
688
0
    doh_store_a(doh, index, d);
689
0
    break;
690
0
  case CURL_DNS_TYPE_AAAA:
691
0
    if(rdlength != 16)
692
0
      return DOH_DNS_RDATA_LEN;
693
0
    doh_store_aaaa(doh, index, d);
694
0
    break;
695
#ifdef USE_HTTPSRR
696
  case CURL_DNS_TYPE_HTTPS:
697
    rc = doh_store_https(doh, index, d, rdlength);
698
    if(rc)
699
      return rc;
700
    break;
701
#endif
702
0
  case CURL_DNS_TYPE_CNAME:
703
0
    rc = doh_store_cname(doh, dohlen, (unsigned int)index, d);
704
0
    if(rc)
705
0
      return rc;
706
0
    break;
707
0
  case CURL_DNS_TYPE_DNAME:
708
    /* explicit for clarity; skip; rely on synthesized CNAME */
709
0
    break;
710
0
  default:
711
    /* unsupported type, skip it */
712
0
    break;
713
0
  }
714
0
  return DOH_OK;
715
0
}
716
717
UNITTEST void de_init(struct dohentry *d);
718
UNITTEST void de_init(struct dohentry *de)
719
0
{
720
0
  int i;
721
0
  memset(de, 0, sizeof(*de));
722
0
  de->ttl = INT_MAX;
723
0
  for(i = 0; i < DOH_MAX_CNAME; i++)
724
0
    curlx_dyn_init(&de->cname[i], DYN_DOH_CNAME);
725
0
}
726
727
UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
728
                                 size_t dohlen,
729
                                 DNStype dnstype,
730
                                 struct dohentry *d);
731
UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
732
                                 size_t dohlen,
733
                                 DNStype dnstype,
734
                                 struct dohentry *d)
735
0
{
736
0
  unsigned char rcode;
737
0
  unsigned short qdcount;
738
0
  unsigned short ancount;
739
0
  unsigned short type = 0;
740
0
  unsigned short rdlength;
741
0
  unsigned short nscount;
742
0
  unsigned short arcount;
743
0
  unsigned int index = 12;
744
0
  DOHcode rc;
745
746
0
  if(dohlen < 12)
747
0
    return DOH_TOO_SMALL_BUFFER; /* too small */
748
0
  if(!doh || doh[0] || doh[1])
749
0
    return DOH_DNS_BAD_ID; /* bad ID */
750
0
  rcode = doh[3] & 0x0f;
751
0
  if(rcode)
752
0
    return DOH_DNS_BAD_RCODE; /* bad rcode */
753
754
0
  qdcount = doh_get16bit(doh, 4);
755
0
  while(qdcount) {
756
0
    rc = doh_skipqname(doh, dohlen, &index);
757
0
    if(rc)
758
0
      return rc; /* bad qname */
759
0
    if(dohlen < (index + 4))
760
0
      return DOH_DNS_OUT_OF_RANGE;
761
0
    index += 4; /* skip question's type and class */
762
0
    qdcount--;
763
0
  }
764
765
0
  ancount = doh_get16bit(doh, 6);
766
0
  while(ancount) {
767
0
    unsigned short dnsclass;
768
0
    unsigned int ttl;
769
770
0
    rc = doh_skipqname(doh, dohlen, &index);
771
0
    if(rc)
772
0
      return rc; /* bad qname */
773
774
0
    if(dohlen < (index + 2))
775
0
      return DOH_DNS_OUT_OF_RANGE;
776
777
0
    type = doh_get16bit(doh, index);
778
0
    if((type != CURL_DNS_TYPE_CNAME) &&  /* may be synthesized from DNAME */
779
0
       (type != CURL_DNS_TYPE_DNAME) &&  /* if present, accept and ignore */
780
0
       (type != dnstype))
781
      /* Not the same type as was asked for nor CNAME nor DNAME */
782
0
      return DOH_DNS_UNEXPECTED_TYPE;
783
0
    index += 2;
784
785
0
    if(dohlen < (index + 2))
786
0
      return DOH_DNS_OUT_OF_RANGE;
787
0
    dnsclass = doh_get16bit(doh, index);
788
0
    if(DNS_CLASS_IN != dnsclass)
789
0
      return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
790
0
    index += 2;
791
792
0
    if(dohlen < (index + 4))
793
0
      return DOH_DNS_OUT_OF_RANGE;
794
795
0
    ttl = doh_get32bit(doh, index);
796
0
    if(ttl < d->ttl)
797
0
      d->ttl = ttl;
798
0
    index += 4;
799
800
0
    if(dohlen < (index + 2))
801
0
      return DOH_DNS_OUT_OF_RANGE;
802
803
0
    rdlength = doh_get16bit(doh, index);
804
0
    index += 2;
805
0
    if(dohlen < (index + rdlength))
806
0
      return DOH_DNS_OUT_OF_RANGE;
807
808
0
    rc = doh_rdata(doh, dohlen, rdlength, type, (int)index, d);
809
0
    if(rc)
810
0
      return rc; /* bad doh_rdata */
811
0
    index += rdlength;
812
0
    ancount--;
813
0
  }
814
815
0
  nscount = doh_get16bit(doh, 8);
816
0
  while(nscount) {
817
0
    rc = doh_skipqname(doh, dohlen, &index);
818
0
    if(rc)
819
0
      return rc; /* bad qname */
820
821
0
    if(dohlen < (index + 8))
822
0
      return DOH_DNS_OUT_OF_RANGE;
823
824
0
    index += 2 + 2 + 4; /* type, dnsclass and ttl */
825
826
0
    if(dohlen < (index + 2))
827
0
      return DOH_DNS_OUT_OF_RANGE;
828
829
0
    rdlength = doh_get16bit(doh, index);
830
0
    index += 2;
831
0
    if(dohlen < (index + rdlength))
832
0
      return DOH_DNS_OUT_OF_RANGE;
833
0
    index += rdlength;
834
0
    nscount--;
835
0
  }
836
837
0
  arcount = doh_get16bit(doh, 10);
838
0
  while(arcount) {
839
0
    rc = doh_skipqname(doh, dohlen, &index);
840
0
    if(rc)
841
0
      return rc; /* bad qname */
842
843
0
    if(dohlen < (index + 8))
844
0
      return DOH_DNS_OUT_OF_RANGE;
845
846
0
    index += 2 + 2 + 4; /* type, dnsclass and ttl */
847
848
0
    if(dohlen < (index + 2))
849
0
      return DOH_DNS_OUT_OF_RANGE;
850
851
0
    rdlength = doh_get16bit(doh, index);
852
0
    index += 2;
853
0
    if(dohlen < (index + rdlength))
854
0
      return DOH_DNS_OUT_OF_RANGE;
855
0
    index += rdlength;
856
0
    arcount--;
857
0
  }
858
859
0
  if(index != dohlen)
860
0
    return DOH_DNS_MALFORMAT; /* something is wrong */
861
862
#ifdef USE_HTTTPS
863
  if((type != CURL_DNS_TYPE_NS) && !d->numcname && !d->numaddr &&
864
     !d->numhttps_rrs)
865
#else
866
0
  if((type != CURL_DNS_TYPE_NS) && !d->numcname && !d->numaddr)
867
0
#endif
868
    /* nothing stored! */
869
0
    return DOH_NO_CONTENT;
870
871
0
  return DOH_OK; /* ok */
872
0
}
873
874
#ifdef CURLVERBOSE
875
static void doh_show(struct Curl_easy *data,
876
                     const struct dohentry *d)
877
0
{
878
0
  int i;
879
0
  infof(data, "[DoH] TTL: %u seconds", d->ttl);
880
0
  for(i = 0; i < d->numaddr; i++) {
881
0
    const struct dohaddr *a = &d->addr[i];
882
0
    if(a->type == CURL_DNS_TYPE_A) {
883
0
      infof(data, "[DoH] A: %u.%u.%u.%u",
884
0
            a->ip.v4[0], a->ip.v4[1],
885
0
            a->ip.v4[2], a->ip.v4[3]);
886
0
    }
887
0
    else if(a->type == CURL_DNS_TYPE_AAAA) {
888
0
      int j;
889
0
      char buffer[128] = "[DoH] AAAA: ";
890
0
      size_t len = strlen(buffer);
891
0
      char *ptr = &buffer[len];
892
0
      len = sizeof(buffer) - len;
893
0
      for(j = 0; j < 16; j += 2) {
894
0
        size_t l;
895
0
        curl_msnprintf(ptr, len, "%s%02x%02x", j ? ":" : "",
896
0
                       d->addr[i].ip.v6[j],
897
0
                       d->addr[i].ip.v6[j + 1]);
898
0
        l = strlen(ptr);
899
0
        len -= l;
900
0
        ptr += l;
901
0
      }
902
0
      infof(data, "%s", buffer);
903
0
    }
904
0
  }
905
#ifdef USE_HTTPSRR
906
  for(i = 0; i < d->numhttps_rrs; i++) {
907
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
908
    doh_print_buf(data, "DoH HTTPS", d->https_rrs[i].val, d->https_rrs[i].len);
909
#else
910
    infof(data, "DoH HTTPS RR: length %d", d->https_rrs[i].len);
911
#endif
912
  }
913
#endif /* USE_HTTPSRR */
914
0
  for(i = 0; i < d->numcname; i++) {
915
0
    infof(data, "CNAME: %s", curlx_dyn_ptr(&d->cname[i]));
916
0
  }
917
0
}
918
#else
919
#define doh_show(x, y)
920
#endif
921
922
/*
923
 * doh2ai()
924
 *
925
 * This function returns a pointer to the first element of a newly allocated
926
 * Curl_addrinfo struct linked list filled with the data from a set of DoH
927
 * lookups. Curl_addrinfo is meant to work like the addrinfo struct does for
928
 * an IPv6 stack, but usable also for IPv4, all hosts and environments.
929
 *
930
 * The memory allocated by this function *MUST* be free'd later on calling
931
 * Curl_freeaddrinfo(). For each successful call to this function there
932
 * must be an associated call later to Curl_freeaddrinfo().
933
 */
934
935
static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
936
                       int port, struct Curl_addrinfo **aip)
937
0
{
938
0
  struct Curl_addrinfo *ai;
939
0
  struct Curl_addrinfo *prevai = NULL;
940
0
  struct Curl_addrinfo *firstai = NULL;
941
0
  struct sockaddr_in *addr;
942
0
#ifdef USE_IPV6
943
0
  struct sockaddr_in6 *addr6;
944
0
#endif
945
0
  CURLcode result = CURLE_OK;
946
0
  int i;
947
0
  size_t hostlen = strlen(hostname) + 1; /* include null-terminator */
948
949
0
  DEBUGASSERT(de);
950
951
0
  if(!de->numaddr)
952
0
    return CURLE_COULDNT_RESOLVE_HOST;
953
954
0
  for(i = 0; i < de->numaddr; i++) {
955
0
    size_t ss_size;
956
0
    CURL_SA_FAMILY_T addrtype;
957
0
    if(de->addr[i].type == CURL_DNS_TYPE_AAAA) {
958
#ifndef USE_IPV6
959
      /* we cannot handle IPv6 addresses */
960
      continue;
961
#else
962
0
      ss_size = sizeof(struct sockaddr_in6);
963
0
      addrtype = AF_INET6;
964
0
#endif
965
0
    }
966
0
    else {
967
0
      ss_size = sizeof(struct sockaddr_in);
968
0
      addrtype = AF_INET;
969
0
    }
970
971
0
    ai = curlx_calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
972
0
    if(!ai) {
973
0
      result = CURLE_OUT_OF_MEMORY;
974
0
      break;
975
0
    }
976
0
    ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
977
0
    ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
978
0
    memcpy(ai->ai_canonname, hostname, hostlen);
979
980
0
    if(!firstai)
981
      /* store the pointer we want to return from this function */
982
0
      firstai = ai;
983
984
0
    if(prevai)
985
      /* make the previous entry point to this */
986
0
      prevai->ai_next = ai;
987
988
0
    ai->ai_family = addrtype;
989
990
    /* we return all names as STREAM, so when using this address for TFTP
991
       the type must be ignored and conn->socktype be used instead! */
992
0
    ai->ai_socktype = SOCK_STREAM;
993
994
0
    ai->ai_addrlen = (curl_socklen_t)ss_size;
995
996
    /* leave the rest of the struct filled with zero */
997
998
0
    switch(ai->ai_family) {
999
0
    case AF_INET:
1000
0
      addr = (void *)ai->ai_addr; /* storage area for this info */
1001
0
      DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
1002
0
      memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
1003
0
      addr->sin_family = addrtype;
1004
0
      addr->sin_port = htons((unsigned short)port);
1005
0
      break;
1006
1007
0
#ifdef USE_IPV6
1008
0
    case AF_INET6:
1009
0
      addr6 = (void *)ai->ai_addr; /* storage area for this info */
1010
0
      DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
1011
0
      memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
1012
0
      addr6->sin6_family = addrtype;
1013
0
      addr6->sin6_port = htons((unsigned short)port);
1014
0
      break;
1015
0
#endif
1016
0
    }
1017
1018
0
    prevai = ai;
1019
0
  }
1020
1021
0
  if(result) {
1022
0
    Curl_freeaddrinfo(firstai);
1023
0
    firstai = NULL;
1024
0
  }
1025
0
  *aip = firstai;
1026
1027
0
  return result;
1028
0
}
1029
1030
#ifdef CURLVERBOSE
1031
static const char *doh_type2name(DNStype dnstype)
1032
0
{
1033
0
  switch(dnstype) {
1034
0
  case CURL_DNS_TYPE_A:
1035
0
    return "A";
1036
0
  case CURL_DNS_TYPE_AAAA:
1037
0
    return "AAAA";
1038
#ifdef USE_HTTPSRR
1039
  case CURL_DNS_TYPE_HTTPS:
1040
    return "HTTPS";
1041
#endif
1042
0
  default:
1043
0
    return "unknown";
1044
0
  }
1045
0
}
1046
#endif
1047
1048
UNITTEST void de_cleanup(struct dohentry *d);
1049
UNITTEST void de_cleanup(struct dohentry *d)
1050
0
{
1051
0
  int i = 0;
1052
0
  for(i = 0; i < d->numcname; i++) {
1053
0
    curlx_dyn_free(&d->cname[i]);
1054
0
  }
1055
#ifdef USE_HTTPSRR
1056
  for(i = 0; i < d->numhttps_rrs; i++)
1057
    curlx_safefree(d->https_rrs[i].val);
1058
#endif
1059
0
}
1060
1061
#ifdef USE_HTTPSRR
1062
1063
/*
1064
 * @brief decode the DNS name in a binary RRData
1065
 * @param buf points to the buffer (in/out)
1066
 * @param remaining points to the remaining buffer length (in/out)
1067
 * @param dnsname returns the string form name on success
1068
 * @return is 1 for success, error otherwise
1069
 *
1070
 * The encoding here is defined in
1071
 * https://datatracker.ietf.org/doc/html/rfc1035#section-3.1
1072
 *
1073
 * The input buffer pointer will be modified so it points to after the end of
1074
 * the DNS name encoding on output. (that is why it is an "unsigned char
1075
 * **" :-)
1076
 */
1077
static CURLcode doh_decode_rdata_name(const unsigned char **buf,
1078
                                      size_t *remaining, char **dnsname)
1079
{
1080
  const unsigned char *cp = NULL;
1081
  size_t rem = 0;
1082
  unsigned char clen = 0; /* chunk len */
1083
  struct dynbuf thename;
1084
1085
  DEBUGASSERT(buf && remaining && dnsname);
1086
  if(!buf || !remaining || !dnsname || !*remaining)
1087
    return CURLE_OUT_OF_MEMORY;
1088
  curlx_dyn_init(&thename, CURL_MAXLEN_host_name);
1089
  rem = *remaining;
1090
  cp = *buf;
1091
  clen = *cp++;
1092
  if(clen == 0) {
1093
    /* special case - return "." as name */
1094
    if(curlx_dyn_addn(&thename, ".", 1))
1095
      return CURLE_OUT_OF_MEMORY;
1096
  }
1097
  while(clen) {
1098
    if(clen >= rem) {
1099
      curlx_dyn_free(&thename);
1100
      return CURLE_OUT_OF_MEMORY;
1101
    }
1102
    if(curlx_dyn_addn(&thename, cp, clen) ||
1103
       curlx_dyn_addn(&thename, ".", 1))
1104
      return CURLE_TOO_LARGE;
1105
1106
    cp += clen;
1107
    rem -= (clen + 1);
1108
    if(rem <= 0) {
1109
      curlx_dyn_free(&thename);
1110
      return CURLE_OUT_OF_MEMORY;
1111
    }
1112
    clen = *cp++;
1113
  }
1114
  *buf = cp;
1115
  *remaining = rem - 1;
1116
  *dnsname = curlx_dyn_ptr(&thename);
1117
  return CURLE_OK;
1118
}
1119
1120
UNITTEST CURLcode doh_resp_decode_httpsrr(struct Curl_easy *data,
1121
                                          const unsigned char *cp, size_t len,
1122
                                          struct Curl_https_rrinfo **hrr);
1123
1124
/* @unittest 1658 */
1125
UNITTEST CURLcode doh_resp_decode_httpsrr(struct Curl_easy *data,
1126
                                          const unsigned char *cp, size_t len,
1127
                                          struct Curl_https_rrinfo **hrr)
1128
{
1129
  uint16_t pcode = 0, plen = 0;
1130
  uint32_t expected_min_pcode = 0;
1131
  struct Curl_https_rrinfo *lhrr = NULL;
1132
  char *dnsname = NULL;
1133
  CURLcode result = CURLE_OUT_OF_MEMORY;
1134
  size_t olen;
1135
1136
  (void)data;
1137
  *hrr = NULL;
1138
  if(len <= 2)
1139
    return CURLE_BAD_FUNCTION_ARGUMENT;
1140
  lhrr = curlx_calloc(1, sizeof(struct Curl_https_rrinfo));
1141
  if(!lhrr)
1142
    return CURLE_OUT_OF_MEMORY;
1143
  lhrr->priority = doh_get16bit(cp, 0);
1144
  cp += 2;
1145
  len -= 2;
1146
  if(doh_decode_rdata_name(&cp, &len, &dnsname) != CURLE_OK)
1147
    goto err;
1148
  lhrr->target = dnsname;
1149
  if(Curl_junkscan(dnsname, &olen, FALSE)) {
1150
    /* unacceptable hostname content */
1151
    result = CURLE_WEIRD_SERVER_REPLY;
1152
    goto err;
1153
  }
1154
  while(len >= 4) {
1155
    pcode = doh_get16bit(cp, 0);
1156
    plen = doh_get16bit(cp, 2);
1157
    cp += 4;
1158
    len -= 4;
1159
    if(pcode < expected_min_pcode || plen > len) {
1160
      result = CURLE_WEIRD_SERVER_REPLY;
1161
      goto err;
1162
    }
1163
    result = Curl_httpsrr_set(lhrr, pcode, cp, plen);
1164
    if(result)
1165
      goto err;
1166
    Curl_httpsrr_trace(data, lhrr);
1167
    cp += plen;
1168
    len -= plen;
1169
    expected_min_pcode = pcode + 1;
1170
  }
1171
  DEBUGASSERT(!len);
1172
  *hrr = lhrr;
1173
  return CURLE_OK;
1174
err:
1175
  Curl_httpsrr_cleanup(lhrr);
1176
  curlx_safefree(lhrr);
1177
  return result;
1178
}
1179
1180
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
1181
UNITTEST void doh_print_httpsrr(struct Curl_easy *data,
1182
                                struct Curl_https_rrinfo *hrr);
1183
1184
UNITTEST void doh_print_httpsrr(struct Curl_easy *data,
1185
                                struct Curl_https_rrinfo *hrr)
1186
{
1187
  DEBUGASSERT(hrr);
1188
  infof(data, "HTTPS RR: priority %d, target: %s", hrr->priority, hrr->target);
1189
  if(hrr->alpns[0] != ALPN_none)
1190
    infof(data, "HTTPS RR: alpns %u %u %u %u",
1191
          hrr->alpns[0], hrr->alpns[1], hrr->alpns[2], hrr->alpns[3]);
1192
  else
1193
    infof(data, "HTTPS RR: no alpns");
1194
  if(hrr->no_def_alpn)
1195
    infof(data, "HTTPS RR: no_def_alpn set");
1196
  else
1197
    infof(data, "HTTPS RR: no_def_alpn not set");
1198
  if(hrr->ipv4hints) {
1199
    doh_print_buf(data, "HTTPS RR: ipv4hints",
1200
                  hrr->ipv4hints, hrr->ipv4hints_len);
1201
  }
1202
  else
1203
    infof(data, "HTTPS RR: no ipv4hints");
1204
  if(hrr->echconfiglist) {
1205
    doh_print_buf(data, "HTTPS RR: ECHConfigList",
1206
                  hrr->echconfiglist, hrr->echconfiglist_len);
1207
  }
1208
  else
1209
    infof(data, "HTTPS RR: no ECHConfigList");
1210
  if(hrr->ipv6hints) {
1211
    doh_print_buf(data, "HTTPS RR: ipv6hint",
1212
                  hrr->ipv6hints, hrr->ipv6hints_len);
1213
  }
1214
  else
1215
    infof(data, "HTTPS RR: no ipv6hints");
1216
}
1217
# endif
1218
#endif
1219
1220
CURLcode Curl_doh_take_result(struct Curl_easy *data,
1221
                              struct Curl_resolv_async *async,
1222
                              struct Curl_dns_entry **pdns)
1223
0
{
1224
0
  struct doh_probes *dohp = async->doh;
1225
0
  CURLcode result = CURLE_OK;
1226
0
  struct dohentry de;
1227
1228
0
  *pdns = NULL; /* defaults to no response */
1229
0
  if(!dohp)
1230
0
    return CURLE_OUT_OF_MEMORY;
1231
1232
0
  if(dohp->probe_resp[DOH_SLOT_IPV4].probe_mid == UINT32_MAX &&
1233
0
     dohp->probe_resp[DOH_SLOT_IPV6].probe_mid == UINT32_MAX) {
1234
0
    failf(data, "Could not DoH-resolve: %s", dohp->host);
1235
0
    return CONN_IS_PROXIED(data->conn) ? CURLE_COULDNT_RESOLVE_PROXY :
1236
0
      CURLE_COULDNT_RESOLVE_HOST;
1237
0
  }
1238
0
  else if(!dohp->pending) {
1239
0
    DOHcode rc[DOH_SLOT_COUNT];
1240
0
    int slot;
1241
1242
0
    memset(rc, 0, sizeof(rc));
1243
    /* remove DoH handles from multi handle and close them */
1244
0
    doh_close(data, async);
1245
    /* parse the responses, create the struct and return it! */
1246
0
    de_init(&de);
1247
0
    for(slot = 0; slot < DOH_SLOT_COUNT; slot++) {
1248
0
      struct doh_response *p = &dohp->probe_resp[slot];
1249
0
      if(!p->dnstype)
1250
0
        continue;
1251
0
      rc[slot] = doh_resp_decode(curlx_dyn_uptr(&p->body),
1252
0
                                 curlx_dyn_len(&p->body),
1253
0
                                 p->dnstype, &de);
1254
0
      if(rc[slot]) {
1255
0
        CURL_TRC_DNS(data, "DoH: %s type %s for %s", doh_strerror(rc[slot]),
1256
0
                     doh_type2name(p->dnstype), dohp->host);
1257
0
      }
1258
0
    } /* next slot */
1259
1260
0
    if(!rc[DOH_SLOT_IPV4] || !rc[DOH_SLOT_IPV6]) {
1261
      /* we have an address, of one kind or other */
1262
0
      struct Curl_dns_entry *dns;
1263
0
      struct Curl_addrinfo *ai;
1264
1265
0
      if(Curl_trc_ft_is_verbose(data, &Curl_trc_feat_dns)) {
1266
0
        CURL_TRC_DNS(data, "hostname: %s", dohp->host);
1267
0
        doh_show(data, &de);
1268
0
      }
1269
1270
0
      result = doh2ai(&de, dohp->host, dohp->port, &ai);
1271
0
      if(result)
1272
0
        goto error;
1273
1274
      /* we got a response, create a dns entry. */
1275
0
      dns = Curl_dnscache_mk_entry(data, async->dns_queries,
1276
0
                                   &ai, dohp->host, dohp->port);
1277
0
      if(!dns) {
1278
0
        result = CURLE_OUT_OF_MEMORY;
1279
0
        goto error;
1280
0
      }
1281
1282
      /* Now add and HTTPSRR information if we have */
1283
#ifdef USE_HTTPSRR
1284
      if(de.numhttps_rrs > 0 && result == CURLE_OK) {
1285
        struct Curl_https_rrinfo *hrr = NULL;
1286
        result = doh_resp_decode_httpsrr(data, de.https_rrs->val,
1287
                                         de.https_rrs->len, &hrr);
1288
        if(result) {
1289
          infof(data, "Failed to decode HTTPS RR");
1290
          Curl_dns_entry_unlink(data, &dns);
1291
          goto error;
1292
        }
1293
        infof(data, "Some HTTPS RR to process");
1294
#if defined(DEBUGBUILD) && defined(CURLVERBOSE)
1295
        doh_print_httpsrr(data, hrr);
1296
#endif
1297
        Curl_dns_entry_set_https_rr(dns, hrr);
1298
      }
1299
#endif /* USE_HTTPSRR */
1300
1301
      /* and add the entry to the cache */
1302
0
      result = Curl_dnscache_add(data, dns);
1303
0
      *pdns = dns;
1304
0
    } /* address processing done */
1305
0
    else {
1306
0
      result = CONN_IS_PROXIED(data->conn) ? CURLE_COULDNT_RESOLVE_PROXY :
1307
0
        CURLE_COULDNT_RESOLVE_HOST;
1308
0
    }
1309
1310
0
  } /* !dohp->pending */
1311
0
  else
1312
    /* wait for pending DoH transactions to complete */
1313
0
    return CURLE_AGAIN;
1314
1315
0
error:
1316
0
  de_cleanup(&de);
1317
0
  Curl_doh_cleanup(data, async);
1318
0
  return result;
1319
0
}
1320
1321
static void doh_close(struct Curl_easy *data,
1322
                      struct Curl_resolv_async *async)
1323
0
{
1324
0
  struct doh_probes *doh = async ? async->doh : NULL;
1325
0
  if(doh && data->multi) {
1326
0
    struct Curl_easy *probe_data;
1327
0
    uint32_t mid;
1328
0
    size_t slot;
1329
0
    for(slot = 0; slot < DOH_SLOT_COUNT; slot++) {
1330
0
      mid = doh->probe_resp[slot].probe_mid;
1331
0
      if(mid == UINT32_MAX)
1332
0
        continue;
1333
0
      doh->probe_resp[slot].probe_mid = UINT32_MAX;
1334
      /* should have been called before data is removed from multi handle */
1335
0
      DEBUGASSERT(data->multi);
1336
0
      probe_data = data->multi ? Curl_multi_get_easy(data->multi, mid) : NULL;
1337
0
      if(!probe_data) {
1338
0
        DEBUGF(infof(data, "Curl_doh_close: xfer for mid=%u not found!",
1339
0
                     doh->probe_resp[slot].probe_mid));
1340
0
        continue;
1341
0
      }
1342
      /* data->multi might already be reset at this time */
1343
0
      curl_multi_remove_handle(data->multi, probe_data);
1344
0
      Curl_close(&probe_data);
1345
0
    }
1346
0
    data->sub_xfer_done = NULL;
1347
0
  }
1348
0
}
1349
1350
void Curl_doh_cleanup(struct Curl_easy *data,
1351
                      struct Curl_resolv_async *async)
1352
0
{
1353
0
  struct doh_probes *dohp = async->doh;
1354
0
  if(dohp) {
1355
0
    int i;
1356
0
    doh_close(data, async);
1357
0
    for(i = 0; i < DOH_SLOT_COUNT; ++i) {
1358
0
      curlx_dyn_free(&dohp->probe_resp[i].body);
1359
0
    }
1360
    curlx_safefree(async->doh);
1361
0
  }
1362
0
}
1363
1364
#endif /* CURL_DISABLE_DOH */