Coverage Report

Created: 2023-12-08 06:48

/src/curl/lib/doh.c
Line
Count
Source (jump to first uncovered line)
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
25
#include "curl_setup.h"
26
27
#ifndef CURL_DISABLE_DOH
28
29
#include "urldata.h"
30
#include "curl_addrinfo.h"
31
#include "doh.h"
32
33
#include "sendf.h"
34
#include "multiif.h"
35
#include "url.h"
36
#include "share.h"
37
#include "curl_base64.h"
38
#include "connect.h"
39
#include "strdup.h"
40
#include "dynbuf.h"
41
/* The last 3 #include files should be in this order */
42
#include "curl_printf.h"
43
#include "curl_memory.h"
44
#include "memdebug.h"
45
46
0
#define DNS_CLASS_IN 0x01
47
48
#ifndef CURL_DISABLE_VERBOSE_STRINGS
49
static const char * const errors[]={
50
  "",
51
  "Bad label",
52
  "Out of range",
53
  "Label loop",
54
  "Too small",
55
  "Out of memory",
56
  "RDATA length",
57
  "Malformat",
58
  "Bad RCODE",
59
  "Unexpected TYPE",
60
  "Unexpected CLASS",
61
  "No content",
62
  "Bad ID",
63
  "Name too long"
64
};
65
66
static const char *doh_strerror(DOHcode code)
67
0
{
68
0
  if((code >= DOH_OK) && (code <= DOH_DNS_NAME_TOO_LONG))
69
0
    return errors[code];
70
0
  return "bad error code";
71
0
}
72
#endif
73
74
/* @unittest 1655
75
 */
76
UNITTEST DOHcode doh_encode(const char *host,
77
                            DNStype dnstype,
78
                            unsigned char *dnsp, /* buffer */
79
                            size_t len,  /* buffer size */
80
                            size_t *olen) /* output length */
81
0
{
82
0
  const size_t hostlen = strlen(host);
83
0
  unsigned char *orig = dnsp;
84
0
  const char *hostp = host;
85
86
  /* The expected output length is 16 bytes more than the length of
87
   * the QNAME-encoding of the host name.
88
   *
89
   * A valid DNS name may not contain a zero-length label, except at
90
   * the end.  For this reason, a name beginning with a dot, or
91
   * containing a sequence of two or more consecutive dots, is invalid
92
   * and cannot be encoded as a QNAME.
93
   *
94
   * If the host name ends with a trailing dot, the corresponding
95
   * QNAME-encoding is one byte longer than the host name. If (as is
96
   * also valid) the hostname is shortened by the omission of the
97
   * trailing dot, then its QNAME-encoding will be two bytes longer
98
   * than the host name.
99
   *
100
   * Each [ label, dot ] pair is encoded as [ length, label ],
101
   * preserving overall length.  A final [ label ] without a dot is
102
   * also encoded as [ length, label ], increasing overall length
103
   * by one. The encoding is completed by appending a zero byte,
104
   * representing the zero-length root label, again increasing
105
   * the overall length by one.
106
   */
107
108
0
  size_t expected_len;
109
0
  DEBUGASSERT(hostlen);
110
0
  expected_len = 12 + 1 + hostlen + 4;
111
0
  if(host[hostlen-1]!='.')
112
0
    expected_len++;
113
114
0
  if(expected_len > (256 + 16)) /* RFCs 1034, 1035 */
115
0
    return DOH_DNS_NAME_TOO_LONG;
116
117
0
  if(len < expected_len)
118
0
    return DOH_TOO_SMALL_BUFFER;
119
120
0
  *dnsp++ = 0; /* 16 bit id */
121
0
  *dnsp++ = 0;
122
0
  *dnsp++ = 0x01; /* |QR|   Opcode  |AA|TC|RD| Set the RD bit */
123
0
  *dnsp++ = '\0'; /* |RA|   Z    |   RCODE   |                */
124
0
  *dnsp++ = '\0';
125
0
  *dnsp++ = 1;    /* QDCOUNT (number of entries in the question section) */
126
0
  *dnsp++ = '\0';
127
0
  *dnsp++ = '\0'; /* ANCOUNT */
128
0
  *dnsp++ = '\0';
129
0
  *dnsp++ = '\0'; /* NSCOUNT */
130
0
  *dnsp++ = '\0';
131
0
  *dnsp++ = '\0'; /* ARCOUNT */
132
133
  /* encode each label and store it in the QNAME */
134
0
  while(*hostp) {
135
0
    size_t labellen;
136
0
    char *dot = strchr(hostp, '.');
137
0
    if(dot)
138
0
      labellen = dot - hostp;
139
0
    else
140
0
      labellen = strlen(hostp);
141
0
    if((labellen > 63) || (!labellen)) {
142
      /* label is too long or too short, error out */
143
0
      *olen = 0;
144
0
      return DOH_DNS_BAD_LABEL;
145
0
    }
146
    /* label is non-empty, process it */
147
0
    *dnsp++ = (unsigned char)labellen;
148
0
    memcpy(dnsp, hostp, labellen);
149
0
    dnsp += labellen;
150
0
    hostp += labellen;
151
    /* advance past dot, but only if there is one */
152
0
    if(dot)
153
0
      hostp++;
154
0
  } /* next label */
155
156
0
  *dnsp++ = 0; /* append zero-length label for root */
157
158
  /* There are assigned TYPE codes beyond 255: use range [1..65535]  */
159
0
  *dnsp++ = (unsigned char)(255 & (dnstype>>8)); /* upper 8 bit TYPE */
160
0
  *dnsp++ = (unsigned char)(255 & dnstype);      /* lower 8 bit TYPE */
161
162
0
  *dnsp++ = '\0'; /* upper 8 bit CLASS */
163
0
  *dnsp++ = DNS_CLASS_IN; /* IN - "the Internet" */
164
165
0
  *olen = dnsp - orig;
166
167
  /* verify that our estimation of length is valid, since
168
   * this has led to buffer overflows in this function */
169
0
  DEBUGASSERT(*olen == expected_len);
170
0
  return DOH_OK;
171
0
}
172
173
static size_t
174
doh_write_cb(const void *contents, size_t size, size_t nmemb, void *userp)
175
0
{
176
0
  size_t realsize = size * nmemb;
177
0
  struct dynbuf *mem = (struct dynbuf *)userp;
178
179
0
  if(Curl_dyn_addn(mem, contents, realsize))
180
0
    return 0;
181
182
0
  return realsize;
183
0
}
184
185
/* called from multi.c when this DoH transfer is complete */
186
static int doh_done(struct Curl_easy *doh, CURLcode result)
187
0
{
188
0
  struct Curl_easy *data = doh->set.dohfor;
189
0
  struct dohdata *dohp = data->req.doh;
190
  /* so one of the DoH request done for the 'data' transfer is now complete! */
191
0
  dohp->pending--;
192
0
  infof(data, "a DoH request is completed, %u to go", dohp->pending);
193
0
  if(result)
194
0
    infof(data, "DoH request %s", curl_easy_strerror(result));
195
196
0
  if(!dohp->pending) {
197
    /* DoH completed */
198
0
    curl_slist_free_all(dohp->headers);
199
0
    dohp->headers = NULL;
200
0
    Curl_expire(data, 0, EXPIRE_RUN_NOW);
201
0
  }
202
0
  return 0;
203
0
}
204
205
0
#define ERROR_CHECK_SETOPT(x,y) \
206
0
do {                                          \
207
0
  result = curl_easy_setopt(doh, x, y);       \
208
0
  if(result &&                                \
209
0
     result != CURLE_NOT_BUILT_IN &&          \
210
0
     result != CURLE_UNKNOWN_OPTION)          \
211
0
    goto error;                               \
212
0
} while(0)
213
214
static CURLcode dohprobe(struct Curl_easy *data,
215
                         struct dnsprobe *p, DNStype dnstype,
216
                         const char *host,
217
                         const char *url, CURLM *multi,
218
                         struct curl_slist *headers)
219
0
{
220
0
  struct Curl_easy *doh = NULL;
221
0
  char *nurl = NULL;
222
0
  CURLcode result = CURLE_OK;
223
0
  timediff_t timeout_ms;
224
0
  DOHcode d = doh_encode(host, dnstype, p->dohbuffer, sizeof(p->dohbuffer),
225
0
                         &p->dohlen);
226
0
  if(d) {
227
0
    failf(data, "Failed to encode DoH packet [%d]", d);
228
0
    return CURLE_OUT_OF_MEMORY;
229
0
  }
230
231
0
  p->dnstype = dnstype;
232
0
  Curl_dyn_init(&p->serverdoh, DYN_DOH_RESPONSE);
233
234
0
  timeout_ms = Curl_timeleft(data, NULL, TRUE);
235
0
  if(timeout_ms <= 0) {
236
0
    result = CURLE_OPERATION_TIMEDOUT;
237
0
    goto error;
238
0
  }
239
  /* Curl_open() is the internal version of curl_easy_init() */
240
0
  result = Curl_open(&doh);
241
0
  if(!result) {
242
    /* pass in the struct pointer via a local variable to please coverity and
243
       the gcc typecheck helpers */
244
0
    struct dynbuf *resp = &p->serverdoh;
245
0
    doh->state.internal = true;
246
0
    ERROR_CHECK_SETOPT(CURLOPT_URL, url);
247
0
    ERROR_CHECK_SETOPT(CURLOPT_DEFAULT_PROTOCOL, "https");
248
0
    ERROR_CHECK_SETOPT(CURLOPT_WRITEFUNCTION, doh_write_cb);
249
0
    ERROR_CHECK_SETOPT(CURLOPT_WRITEDATA, resp);
250
0
    ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDS, p->dohbuffer);
251
0
    ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDSIZE, (long)p->dohlen);
252
0
    ERROR_CHECK_SETOPT(CURLOPT_HTTPHEADER, headers);
253
0
#ifdef USE_HTTP2
254
0
    ERROR_CHECK_SETOPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
255
0
    ERROR_CHECK_SETOPT(CURLOPT_PIPEWAIT, 1L);
256
0
#endif
257
#ifndef CURLDEBUG
258
    /* enforce HTTPS if not debug */
259
    ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
260
#else
261
    /* in debug mode, also allow http */
262
0
    ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
263
0
#endif
264
0
    ERROR_CHECK_SETOPT(CURLOPT_TIMEOUT_MS, (long)timeout_ms);
265
0
    ERROR_CHECK_SETOPT(CURLOPT_SHARE, data->share);
266
0
    if(data->set.err && data->set.err != stderr)
267
0
      ERROR_CHECK_SETOPT(CURLOPT_STDERR, data->set.err);
268
0
    if(data->set.verbose)
269
0
      ERROR_CHECK_SETOPT(CURLOPT_VERBOSE, 1L);
270
0
    if(data->set.no_signal)
271
0
      ERROR_CHECK_SETOPT(CURLOPT_NOSIGNAL, 1L);
272
273
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYHOST,
274
0
      data->set.doh_verifyhost ? 2L : 0L);
275
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYPEER,
276
0
      data->set.doh_verifypeer ? 1L : 0L);
277
0
    ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYSTATUS,
278
0
      data->set.doh_verifystatus ? 1L : 0L);
279
280
    /* Inherit *some* SSL options from the user's transfer. This is a
281
       best-guess as to which options are needed for compatibility. #3661
282
283
       Note DoH does not inherit the user's proxy server so proxy SSL settings
284
       have no effect and are not inherited. If that changes then two new
285
       options should be added to check doh proxy insecure separately,
286
       CURLOPT_DOH_PROXY_SSL_VERIFYHOST and CURLOPT_DOH_PROXY_SSL_VERIFYPEER.
287
       */
288
0
    if(data->set.ssl.falsestart)
289
0
      ERROR_CHECK_SETOPT(CURLOPT_SSL_FALSESTART, 1L);
290
0
    if(data->set.str[STRING_SSL_CAFILE]) {
291
0
      ERROR_CHECK_SETOPT(CURLOPT_CAINFO,
292
0
                         data->set.str[STRING_SSL_CAFILE]);
293
0
    }
294
0
    if(data->set.blobs[BLOB_CAINFO]) {
295
0
      ERROR_CHECK_SETOPT(CURLOPT_CAINFO_BLOB,
296
0
                         data->set.blobs[BLOB_CAINFO]);
297
0
    }
298
0
    if(data->set.str[STRING_SSL_CAPATH]) {
299
0
      ERROR_CHECK_SETOPT(CURLOPT_CAPATH,
300
0
                         data->set.str[STRING_SSL_CAPATH]);
301
0
    }
302
0
    if(data->set.str[STRING_SSL_CRLFILE]) {
303
0
      ERROR_CHECK_SETOPT(CURLOPT_CRLFILE,
304
0
                         data->set.str[STRING_SSL_CRLFILE]);
305
0
    }
306
0
    if(data->set.ssl.certinfo)
307
0
      ERROR_CHECK_SETOPT(CURLOPT_CERTINFO, 1L);
308
0
    if(data->set.ssl.fsslctx)
309
0
      ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_FUNCTION, data->set.ssl.fsslctx);
310
0
    if(data->set.ssl.fsslctxp)
311
0
      ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_DATA, data->set.ssl.fsslctxp);
312
0
    if(data->set.fdebug)
313
0
      ERROR_CHECK_SETOPT(CURLOPT_DEBUGFUNCTION, data->set.fdebug);
314
0
    if(data->set.debugdata)
315
0
      ERROR_CHECK_SETOPT(CURLOPT_DEBUGDATA, data->set.debugdata);
316
0
    if(data->set.str[STRING_SSL_EC_CURVES]) {
317
0
      ERROR_CHECK_SETOPT(CURLOPT_SSL_EC_CURVES,
318
0
                         data->set.str[STRING_SSL_EC_CURVES]);
319
0
    }
320
321
0
    {
322
0
      long mask =
323
0
        (data->set.ssl.enable_beast ?
324
0
         CURLSSLOPT_ALLOW_BEAST : 0) |
325
0
        (data->set.ssl.no_revoke ?
326
0
         CURLSSLOPT_NO_REVOKE : 0) |
327
0
        (data->set.ssl.no_partialchain ?
328
0
         CURLSSLOPT_NO_PARTIALCHAIN : 0) |
329
0
        (data->set.ssl.revoke_best_effort ?
330
0
         CURLSSLOPT_REVOKE_BEST_EFFORT : 0) |
331
0
        (data->set.ssl.native_ca_store ?
332
0
         CURLSSLOPT_NATIVE_CA : 0) |
333
0
        (data->set.ssl.auto_client_cert ?
334
0
         CURLSSLOPT_AUTO_CLIENT_CERT : 0);
335
336
0
      (void)curl_easy_setopt(doh, CURLOPT_SSL_OPTIONS, mask);
337
0
    }
338
339
0
    doh->set.fmultidone = doh_done;
340
0
    doh->set.dohfor = data; /* identify for which transfer this is done */
341
0
    p->easy = doh;
342
343
    /* DoH handles must not inherit private_data. The handles may be passed to
344
       the user via callbacks and the user will be able to identify them as
345
       internal handles because private data is not set. The user can then set
346
       private_data via CURLOPT_PRIVATE if they so choose. */
347
0
    DEBUGASSERT(!doh->set.private_data);
348
349
0
    if(curl_multi_add_handle(multi, doh))
350
0
      goto error;
351
0
  }
352
0
  else
353
0
    goto error;
354
0
  free(nurl);
355
0
  return CURLE_OK;
356
357
0
error:
358
0
  free(nurl);
359
0
  Curl_close(&doh);
360
0
  return result;
361
0
}
362
363
/*
364
 * Curl_doh() resolves a name using DoH. It resolves a name and returns a
365
 * 'Curl_addrinfo *' with the address information.
366
 */
367
368
struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
369
                               const char *hostname,
370
                               int port,
371
                               int *waitp)
372
0
{
373
0
  CURLcode result = CURLE_OK;
374
0
  int slot;
375
0
  struct dohdata *dohp;
376
0
  struct connectdata *conn = data->conn;
377
0
  *waitp = FALSE;
378
0
  (void)hostname;
379
0
  (void)port;
380
381
0
  DEBUGASSERT(!data->req.doh);
382
0
  DEBUGASSERT(conn);
383
384
  /* start clean, consider allocating this struct on demand */
385
0
  dohp = data->req.doh = calloc(1, sizeof(struct dohdata));
386
0
  if(!dohp)
387
0
    return NULL;
388
389
0
  conn->bits.doh = TRUE;
390
0
  dohp->host = hostname;
391
0
  dohp->port = port;
392
0
  dohp->headers =
393
0
    curl_slist_append(NULL,
394
0
                      "Content-Type: application/dns-message");
395
0
  if(!dohp->headers)
396
0
    goto error;
397
398
  /* create IPv4 DoH request */
399
0
  result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V4],
400
0
                    DNS_TYPE_A, hostname, data->set.str[STRING_DOH],
401
0
                    data->multi, dohp->headers);
402
0
  if(result)
403
0
    goto error;
404
0
  dohp->pending++;
405
406
0
#ifdef ENABLE_IPV6
407
0
  if((conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
408
    /* create IPv6 DoH request */
409
0
    result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V6],
410
0
                      DNS_TYPE_AAAA, hostname, data->set.str[STRING_DOH],
411
0
                      data->multi, dohp->headers);
412
0
    if(result)
413
0
      goto error;
414
0
    dohp->pending++;
415
0
  }
416
0
#endif
417
0
  *waitp = TRUE; /* this never returns synchronously */
418
0
  return NULL;
419
420
0
error:
421
0
  curl_slist_free_all(dohp->headers);
422
0
  data->req.doh->headers = NULL;
423
0
  for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
424
0
    (void)curl_multi_remove_handle(data->multi, dohp->probe[slot].easy);
425
0
    Curl_close(&dohp->probe[slot].easy);
426
0
  }
427
0
  Curl_safefree(data->req.doh);
428
0
  return NULL;
429
0
}
430
431
static DOHcode skipqname(const unsigned char *doh, size_t dohlen,
432
                         unsigned int *indexp)
433
0
{
434
0
  unsigned char length;
435
0
  do {
436
0
    if(dohlen < (*indexp + 1))
437
0
      return DOH_DNS_OUT_OF_RANGE;
438
0
    length = doh[*indexp];
439
0
    if((length & 0xc0) == 0xc0) {
440
      /* name pointer, advance over it and be done */
441
0
      if(dohlen < (*indexp + 2))
442
0
        return DOH_DNS_OUT_OF_RANGE;
443
0
      *indexp += 2;
444
0
      break;
445
0
    }
446
0
    if(length & 0xc0)
447
0
      return DOH_DNS_BAD_LABEL;
448
0
    if(dohlen < (*indexp + 1 + length))
449
0
      return DOH_DNS_OUT_OF_RANGE;
450
0
    *indexp += 1 + length;
451
0
  } while(length);
452
0
  return DOH_OK;
453
0
}
454
455
static unsigned short get16bit(const unsigned char *doh, int index)
456
0
{
457
0
  return (unsigned short)((doh[index] << 8) | doh[index + 1]);
458
0
}
459
460
static unsigned int get32bit(const unsigned char *doh, int index)
461
0
{
462
   /* make clang and gcc optimize this to bswap by incrementing
463
      the pointer first. */
464
0
   doh += index;
465
466
   /* avoid undefined behavior by casting to unsigned before shifting
467
      24 bits, possibly into the sign bit. codegen is same, but
468
      ub sanitizer won't be upset */
469
0
  return ( (unsigned)doh[0] << 24) | (doh[1] << 16) |(doh[2] << 8) | doh[3];
470
0
}
471
472
static DOHcode store_a(const unsigned char *doh, int index, struct dohentry *d)
473
0
{
474
  /* silently ignore addresses over the limit */
475
0
  if(d->numaddr < DOH_MAX_ADDR) {
476
0
    struct dohaddr *a = &d->addr[d->numaddr];
477
0
    a->type = DNS_TYPE_A;
478
0
    memcpy(&a->ip.v4, &doh[index], 4);
479
0
    d->numaddr++;
480
0
  }
481
0
  return DOH_OK;
482
0
}
483
484
static DOHcode store_aaaa(const unsigned char *doh,
485
                          int index,
486
                          struct dohentry *d)
487
0
{
488
  /* silently ignore addresses over the limit */
489
0
  if(d->numaddr < DOH_MAX_ADDR) {
490
0
    struct dohaddr *a = &d->addr[d->numaddr];
491
0
    a->type = DNS_TYPE_AAAA;
492
0
    memcpy(&a->ip.v6, &doh[index], 16);
493
0
    d->numaddr++;
494
0
  }
495
0
  return DOH_OK;
496
0
}
497
498
static DOHcode store_cname(const unsigned char *doh,
499
                           size_t dohlen,
500
                           unsigned int index,
501
                           struct dohentry *d)
502
0
{
503
0
  struct dynbuf *c;
504
0
  unsigned int loop = 128; /* a valid DNS name can never loop this much */
505
0
  unsigned char length;
506
507
0
  if(d->numcname == DOH_MAX_CNAME)
508
0
    return DOH_OK; /* skip! */
509
510
0
  c = &d->cname[d->numcname++];
511
0
  do {
512
0
    if(index >= dohlen)
513
0
      return DOH_DNS_OUT_OF_RANGE;
514
0
    length = doh[index];
515
0
    if((length & 0xc0) == 0xc0) {
516
0
      int newpos;
517
      /* name pointer, get the new offset (14 bits) */
518
0
      if((index + 1) >= dohlen)
519
0
        return DOH_DNS_OUT_OF_RANGE;
520
521
      /* move to the new index */
522
0
      newpos = (length & 0x3f) << 8 | doh[index + 1];
523
0
      index = newpos;
524
0
      continue;
525
0
    }
526
0
    else if(length & 0xc0)
527
0
      return DOH_DNS_BAD_LABEL; /* bad input */
528
0
    else
529
0
      index++;
530
531
0
    if(length) {
532
0
      if(Curl_dyn_len(c)) {
533
0
        if(Curl_dyn_addn(c, STRCONST(".")))
534
0
          return DOH_OUT_OF_MEM;
535
0
      }
536
0
      if((index + length) > dohlen)
537
0
        return DOH_DNS_BAD_LABEL;
538
539
0
      if(Curl_dyn_addn(c, &doh[index], length))
540
0
        return DOH_OUT_OF_MEM;
541
0
      index += length;
542
0
    }
543
0
  } while(length && --loop);
544
545
0
  if(!loop)
546
0
    return DOH_DNS_LABEL_LOOP;
547
0
  return DOH_OK;
548
0
}
549
550
static DOHcode rdata(const unsigned char *doh,
551
                     size_t dohlen,
552
                     unsigned short rdlength,
553
                     unsigned short type,
554
                     int index,
555
                     struct dohentry *d)
556
0
{
557
  /* RDATA
558
     - A (TYPE 1):  4 bytes
559
     - AAAA (TYPE 28): 16 bytes
560
     - NS (TYPE 2): N bytes */
561
0
  DOHcode rc;
562
563
0
  switch(type) {
564
0
  case DNS_TYPE_A:
565
0
    if(rdlength != 4)
566
0
      return DOH_DNS_RDATA_LEN;
567
0
    rc = store_a(doh, index, d);
568
0
    if(rc)
569
0
      return rc;
570
0
    break;
571
0
  case DNS_TYPE_AAAA:
572
0
    if(rdlength != 16)
573
0
      return DOH_DNS_RDATA_LEN;
574
0
    rc = store_aaaa(doh, index, d);
575
0
    if(rc)
576
0
      return rc;
577
0
    break;
578
0
  case DNS_TYPE_CNAME:
579
0
    rc = store_cname(doh, dohlen, index, d);
580
0
    if(rc)
581
0
      return rc;
582
0
    break;
583
0
  case DNS_TYPE_DNAME:
584
    /* explicit for clarity; just skip; rely on synthesized CNAME  */
585
0
    break;
586
0
  default:
587
    /* unsupported type, just skip it */
588
0
    break;
589
0
  }
590
0
  return DOH_OK;
591
0
}
592
593
UNITTEST void de_init(struct dohentry *de)
594
0
{
595
0
  int i;
596
0
  memset(de, 0, sizeof(*de));
597
0
  de->ttl = INT_MAX;
598
0
  for(i = 0; i < DOH_MAX_CNAME; i++)
599
0
    Curl_dyn_init(&de->cname[i], DYN_DOH_CNAME);
600
0
}
601
602
603
UNITTEST DOHcode doh_decode(const unsigned char *doh,
604
                            size_t dohlen,
605
                            DNStype dnstype,
606
                            struct dohentry *d)
607
0
{
608
0
  unsigned char rcode;
609
0
  unsigned short qdcount;
610
0
  unsigned short ancount;
611
0
  unsigned short type = 0;
612
0
  unsigned short rdlength;
613
0
  unsigned short nscount;
614
0
  unsigned short arcount;
615
0
  unsigned int index = 12;
616
0
  DOHcode rc;
617
618
0
  if(dohlen < 12)
619
0
    return DOH_TOO_SMALL_BUFFER; /* too small */
620
0
  if(!doh || doh[0] || doh[1])
621
0
    return DOH_DNS_BAD_ID; /* bad ID */
622
0
  rcode = doh[3] & 0x0f;
623
0
  if(rcode)
624
0
    return DOH_DNS_BAD_RCODE; /* bad rcode */
625
626
0
  qdcount = get16bit(doh, 4);
627
0
  while(qdcount) {
628
0
    rc = skipqname(doh, dohlen, &index);
629
0
    if(rc)
630
0
      return rc; /* bad qname */
631
0
    if(dohlen < (index + 4))
632
0
      return DOH_DNS_OUT_OF_RANGE;
633
0
    index += 4; /* skip question's type and class */
634
0
    qdcount--;
635
0
  }
636
637
0
  ancount = get16bit(doh, 6);
638
0
  while(ancount) {
639
0
    unsigned short class;
640
0
    unsigned int ttl;
641
642
0
    rc = skipqname(doh, dohlen, &index);
643
0
    if(rc)
644
0
      return rc; /* bad qname */
645
646
0
    if(dohlen < (index + 2))
647
0
      return DOH_DNS_OUT_OF_RANGE;
648
649
0
    type = get16bit(doh, index);
650
0
    if((type != DNS_TYPE_CNAME)    /* may be synthesized from DNAME */
651
0
       && (type != DNS_TYPE_DNAME) /* if present, accept and ignore */
652
0
       && (type != dnstype))
653
      /* Not the same type as was asked for nor CNAME nor DNAME */
654
0
      return DOH_DNS_UNEXPECTED_TYPE;
655
0
    index += 2;
656
657
0
    if(dohlen < (index + 2))
658
0
      return DOH_DNS_OUT_OF_RANGE;
659
0
    class = get16bit(doh, index);
660
0
    if(DNS_CLASS_IN != class)
661
0
      return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
662
0
    index += 2;
663
664
0
    if(dohlen < (index + 4))
665
0
      return DOH_DNS_OUT_OF_RANGE;
666
667
0
    ttl = get32bit(doh, index);
668
0
    if(ttl < d->ttl)
669
0
      d->ttl = ttl;
670
0
    index += 4;
671
672
0
    if(dohlen < (index + 2))
673
0
      return DOH_DNS_OUT_OF_RANGE;
674
675
0
    rdlength = get16bit(doh, index);
676
0
    index += 2;
677
0
    if(dohlen < (index + rdlength))
678
0
      return DOH_DNS_OUT_OF_RANGE;
679
680
0
    rc = rdata(doh, dohlen, rdlength, type, index, d);
681
0
    if(rc)
682
0
      return rc; /* bad rdata */
683
0
    index += rdlength;
684
0
    ancount--;
685
0
  }
686
687
0
  nscount = get16bit(doh, 8);
688
0
  while(nscount) {
689
0
    rc = skipqname(doh, dohlen, &index);
690
0
    if(rc)
691
0
      return rc; /* bad qname */
692
693
0
    if(dohlen < (index + 8))
694
0
      return DOH_DNS_OUT_OF_RANGE;
695
696
0
    index += 2 + 2 + 4; /* type, class and ttl */
697
698
0
    if(dohlen < (index + 2))
699
0
      return DOH_DNS_OUT_OF_RANGE;
700
701
0
    rdlength = get16bit(doh, index);
702
0
    index += 2;
703
0
    if(dohlen < (index + rdlength))
704
0
      return DOH_DNS_OUT_OF_RANGE;
705
0
    index += rdlength;
706
0
    nscount--;
707
0
  }
708
709
0
  arcount = get16bit(doh, 10);
710
0
  while(arcount) {
711
0
    rc = skipqname(doh, dohlen, &index);
712
0
    if(rc)
713
0
      return rc; /* bad qname */
714
715
0
    if(dohlen < (index + 8))
716
0
      return DOH_DNS_OUT_OF_RANGE;
717
718
0
    index += 2 + 2 + 4; /* type, class and ttl */
719
720
0
    if(dohlen < (index + 2))
721
0
      return DOH_DNS_OUT_OF_RANGE;
722
723
0
    rdlength = get16bit(doh, index);
724
0
    index += 2;
725
0
    if(dohlen < (index + rdlength))
726
0
      return DOH_DNS_OUT_OF_RANGE;
727
0
    index += rdlength;
728
0
    arcount--;
729
0
  }
730
731
0
  if(index != dohlen)
732
0
    return DOH_DNS_MALFORMAT; /* something is wrong */
733
734
0
  if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr)
735
    /* nothing stored! */
736
0
    return DOH_NO_CONTENT;
737
738
0
  return DOH_OK; /* ok */
739
0
}
740
741
#ifndef CURL_DISABLE_VERBOSE_STRINGS
742
static void showdoh(struct Curl_easy *data,
743
                    const struct dohentry *d)
744
0
{
745
0
  int i;
746
0
  infof(data, "TTL: %u seconds", d->ttl);
747
0
  for(i = 0; i < d->numaddr; i++) {
748
0
    const struct dohaddr *a = &d->addr[i];
749
0
    if(a->type == DNS_TYPE_A) {
750
0
      infof(data, "DoH A: %u.%u.%u.%u",
751
0
            a->ip.v4[0], a->ip.v4[1],
752
0
            a->ip.v4[2], a->ip.v4[3]);
753
0
    }
754
0
    else if(a->type == DNS_TYPE_AAAA) {
755
0
      int j;
756
0
      char buffer[128];
757
0
      char *ptr;
758
0
      size_t len;
759
0
      msnprintf(buffer, 128, "DoH AAAA: ");
760
0
      ptr = &buffer[10];
761
0
      len = 118;
762
0
      for(j = 0; j < 16; j += 2) {
763
0
        size_t l;
764
0
        msnprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6[j],
765
0
                  d->addr[i].ip.v6[j + 1]);
766
0
        l = strlen(ptr);
767
0
        len -= l;
768
0
        ptr += l;
769
0
      }
770
0
      infof(data, "%s", buffer);
771
0
    }
772
0
  }
773
0
  for(i = 0; i < d->numcname; i++) {
774
0
    infof(data, "CNAME: %s", Curl_dyn_ptr(&d->cname[i]));
775
0
  }
776
0
}
777
#else
778
#define showdoh(x,y)
779
#endif
780
781
/*
782
 * doh2ai()
783
 *
784
 * This function returns a pointer to the first element of a newly allocated
785
 * Curl_addrinfo struct linked list filled with the data from a set of DoH
786
 * lookups.  Curl_addrinfo is meant to work like the addrinfo struct does for
787
 * a IPv6 stack, but usable also for IPv4, all hosts and environments.
788
 *
789
 * The memory allocated by this function *MUST* be free'd later on calling
790
 * Curl_freeaddrinfo().  For each successful call to this function there
791
 * must be an associated call later to Curl_freeaddrinfo().
792
 */
793
794
static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
795
                       int port, struct Curl_addrinfo **aip)
796
0
{
797
0
  struct Curl_addrinfo *ai;
798
0
  struct Curl_addrinfo *prevai = NULL;
799
0
  struct Curl_addrinfo *firstai = NULL;
800
0
  struct sockaddr_in *addr;
801
0
#ifdef ENABLE_IPV6
802
0
  struct sockaddr_in6 *addr6;
803
0
#endif
804
0
  CURLcode result = CURLE_OK;
805
0
  int i;
806
0
  size_t hostlen = strlen(hostname) + 1; /* include null-terminator */
807
808
0
  DEBUGASSERT(de);
809
810
0
  if(!de->numaddr)
811
0
    return CURLE_COULDNT_RESOLVE_HOST;
812
813
0
  for(i = 0; i < de->numaddr; i++) {
814
0
    size_t ss_size;
815
0
    CURL_SA_FAMILY_T addrtype;
816
0
    if(de->addr[i].type == DNS_TYPE_AAAA) {
817
#ifndef ENABLE_IPV6
818
      /* we can't handle IPv6 addresses */
819
      continue;
820
#else
821
0
      ss_size = sizeof(struct sockaddr_in6);
822
0
      addrtype = AF_INET6;
823
0
#endif
824
0
    }
825
0
    else {
826
0
      ss_size = sizeof(struct sockaddr_in);
827
0
      addrtype = AF_INET;
828
0
    }
829
830
0
    ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
831
0
    if(!ai) {
832
0
      result = CURLE_OUT_OF_MEMORY;
833
0
      break;
834
0
    }
835
0
    ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
836
0
    ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
837
0
    memcpy(ai->ai_canonname, hostname, hostlen);
838
839
0
    if(!firstai)
840
      /* store the pointer we want to return from this function */
841
0
      firstai = ai;
842
843
0
    if(prevai)
844
      /* make the previous entry point to this */
845
0
      prevai->ai_next = ai;
846
847
0
    ai->ai_family = addrtype;
848
849
    /* we return all names as STREAM, so when using this address for TFTP
850
       the type must be ignored and conn->socktype be used instead! */
851
0
    ai->ai_socktype = SOCK_STREAM;
852
853
0
    ai->ai_addrlen = (curl_socklen_t)ss_size;
854
855
    /* leave the rest of the struct filled with zero */
856
857
0
    switch(ai->ai_family) {
858
0
    case AF_INET:
859
0
      addr = (void *)ai->ai_addr; /* storage area for this info */
860
0
      DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
861
0
      memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
862
0
      addr->sin_family = addrtype;
863
0
      addr->sin_port = htons((unsigned short)port);
864
0
      break;
865
866
0
#ifdef ENABLE_IPV6
867
0
    case AF_INET6:
868
0
      addr6 = (void *)ai->ai_addr; /* storage area for this info */
869
0
      DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
870
0
      memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
871
0
      addr6->sin6_family = addrtype;
872
0
      addr6->sin6_port = htons((unsigned short)port);
873
0
      break;
874
0
#endif
875
0
    }
876
877
0
    prevai = ai;
878
0
  }
879
880
0
  if(result) {
881
0
    Curl_freeaddrinfo(firstai);
882
0
    firstai = NULL;
883
0
  }
884
0
  *aip = firstai;
885
886
0
  return result;
887
0
}
888
889
#ifndef CURL_DISABLE_VERBOSE_STRINGS
890
static const char *type2name(DNStype dnstype)
891
0
{
892
0
  return (dnstype == DNS_TYPE_A)?"A":"AAAA";
893
0
}
894
#endif
895
896
UNITTEST void de_cleanup(struct dohentry *d)
897
0
{
898
0
  int i = 0;
899
0
  for(i = 0; i < d->numcname; i++) {
900
0
    Curl_dyn_free(&d->cname[i]);
901
0
  }
902
0
}
903
904
CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
905
                              struct Curl_dns_entry **dnsp)
906
0
{
907
0
  struct connectdata *conn = data->conn;
908
0
  CURLcode result;
909
0
  struct dohdata *dohp = data->req.doh;
910
0
  *dnsp = NULL; /* defaults to no response */
911
0
  if(!dohp)
912
0
    return CURLE_OUT_OF_MEMORY;
913
914
0
  if(!dohp->probe[DOH_PROBE_SLOT_IPADDR_V4].easy &&
915
0
     !dohp->probe[DOH_PROBE_SLOT_IPADDR_V6].easy) {
916
0
    failf(data, "Could not DoH-resolve: %s", conn->resolve_async.hostname);
917
0
    return CONN_IS_PROXIED(data->conn)?CURLE_COULDNT_RESOLVE_PROXY:
918
0
      CURLE_COULDNT_RESOLVE_HOST;
919
0
  }
920
0
  else if(!dohp->pending) {
921
0
    DOHcode rc[DOH_PROBE_SLOTS] = {
922
0
      DOH_OK, DOH_OK
923
0
    };
924
0
    struct dohentry de;
925
0
    int slot;
926
    /* remove DoH handles from multi handle and close them */
927
0
    for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
928
0
      curl_multi_remove_handle(data->multi, dohp->probe[slot].easy);
929
0
      Curl_close(&dohp->probe[slot].easy);
930
0
    }
931
    /* parse the responses, create the struct and return it! */
932
0
    de_init(&de);
933
0
    for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
934
0
      struct dnsprobe *p = &dohp->probe[slot];
935
0
      if(!p->dnstype)
936
0
        continue;
937
0
      rc[slot] = doh_decode(Curl_dyn_uptr(&p->serverdoh),
938
0
                            Curl_dyn_len(&p->serverdoh),
939
0
                            p->dnstype,
940
0
                            &de);
941
0
      Curl_dyn_free(&p->serverdoh);
942
0
#ifndef CURL_DISABLE_VERBOSE_STRINGS
943
0
      if(rc[slot]) {
944
0
        infof(data, "DoH: %s type %s for %s", doh_strerror(rc[slot]),
945
0
              type2name(p->dnstype), dohp->host);
946
0
      }
947
0
#endif
948
0
    } /* next slot */
949
950
0
    result = CURLE_COULDNT_RESOLVE_HOST; /* until we know better */
951
0
    if(!rc[DOH_PROBE_SLOT_IPADDR_V4] || !rc[DOH_PROBE_SLOT_IPADDR_V6]) {
952
      /* we have an address, of one kind or other */
953
0
      struct Curl_dns_entry *dns;
954
0
      struct Curl_addrinfo *ai;
955
956
0
      infof(data, "DoH Host name: %s", dohp->host);
957
0
      showdoh(data, &de);
958
959
0
      result = doh2ai(&de, dohp->host, dohp->port, &ai);
960
0
      if(result) {
961
0
        de_cleanup(&de);
962
0
        return result;
963
0
      }
964
965
0
      if(data->share)
966
0
        Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
967
968
      /* we got a response, store it in the cache */
969
0
      dns = Curl_cache_addr(data, ai, dohp->host, 0, dohp->port);
970
971
0
      if(data->share)
972
0
        Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
973
974
0
      if(!dns) {
975
        /* returned failure, bail out nicely */
976
0
        Curl_freeaddrinfo(ai);
977
0
      }
978
0
      else {
979
0
        conn->resolve_async.dns = dns;
980
0
        *dnsp = dns;
981
0
        result = CURLE_OK;      /* address resolution OK */
982
0
      }
983
0
    } /* address processing done */
984
985
    /* Now process any build-specific attributes retrieved from DNS */
986
987
    /* All done */
988
0
    de_cleanup(&de);
989
0
    Curl_safefree(data->req.doh);
990
0
    return result;
991
992
0
  } /* !dohp->pending */
993
994
  /* else wait for pending DoH transactions to complete */
995
0
  return CURLE_OK;
996
0
}
997
998
#endif /* CURL_DISABLE_DOH */