Coverage Report

Created: 2024-02-25 06:14

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