2
0

doh.c 40 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411
  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. #ifndef CURL_DISABLE_DOH
  26. #include "urldata.h"
  27. #include "curl_addrinfo.h"
  28. #include "doh.h"
  29. #include "sendf.h"
  30. #include "multiif.h"
  31. #include "url.h"
  32. #include "share.h"
  33. #include "curl_base64.h"
  34. #include "connect.h"
  35. #include "strdup.h"
  36. #include "dynbuf.h"
  37. /* The last 3 #include files should be in this order */
  38. #include "curl_printf.h"
  39. #include "curl_memory.h"
  40. #include "memdebug.h"
  41. #include "escape.h"
  42. #define DNS_CLASS_IN 0x01
  43. /* local_print_buf truncates if the hex string will be more than this */
  44. #define LOCAL_PB_HEXMAX 400
  45. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  46. static const char * const errors[]={
  47. "",
  48. "Bad label",
  49. "Out of range",
  50. "Label loop",
  51. "Too small",
  52. "Out of memory",
  53. "RDATA length",
  54. "Malformat",
  55. "Bad RCODE",
  56. "Unexpected TYPE",
  57. "Unexpected CLASS",
  58. "No content",
  59. "Bad ID",
  60. "Name too long"
  61. };
  62. static const char *doh_strerror(DOHcode code)
  63. {
  64. if((code >= DOH_OK) && (code <= DOH_DNS_NAME_TOO_LONG))
  65. return errors[code];
  66. return "bad error code";
  67. }
  68. struct curl_trc_feat Curl_doh_trc = {
  69. "DoH",
  70. CURL_LOG_LVL_NONE,
  71. };
  72. #endif /* !CURL_DISABLE_VERBOSE_STRINGS */
  73. /* @unittest 1655
  74. */
  75. UNITTEST DOHcode doh_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. {
  81. const size_t hostlen = strlen(host);
  82. unsigned char *orig = dnsp;
  83. const char *hostp = host;
  84. /* The expected output length is 16 bytes more than the length of
  85. * the QNAME-encoding of the host name.
  86. *
  87. * A valid DNS name may not contain a zero-length label, except at
  88. * the end. For this reason, a name beginning with a dot, or
  89. * containing a sequence of two or more consecutive dots, is invalid
  90. * and cannot be encoded as a QNAME.
  91. *
  92. * If the host name ends with a trailing dot, the corresponding
  93. * QNAME-encoding is one byte longer than the host name. If (as is
  94. * also valid) the hostname is shortened by the omission of the
  95. * trailing dot, then its QNAME-encoding will be two bytes longer
  96. * than the host name.
  97. *
  98. * Each [ label, dot ] pair is encoded as [ length, label ],
  99. * preserving overall length. A final [ label ] without a dot is
  100. * also encoded as [ length, label ], increasing overall length
  101. * by one. The encoding is completed by appending a zero byte,
  102. * representing the zero-length root label, again increasing
  103. * the overall length by one.
  104. */
  105. size_t expected_len;
  106. DEBUGASSERT(hostlen);
  107. expected_len = 12 + 1 + hostlen + 4;
  108. if(host[hostlen-1]!='.')
  109. expected_len++;
  110. if(expected_len > (256 + 16)) /* RFCs 1034, 1035 */
  111. return DOH_DNS_NAME_TOO_LONG;
  112. if(len < expected_len)
  113. return DOH_TOO_SMALL_BUFFER;
  114. *dnsp++ = 0; /* 16 bit id */
  115. *dnsp++ = 0;
  116. *dnsp++ = 0x01; /* |QR| Opcode |AA|TC|RD| Set the RD bit */
  117. *dnsp++ = '\0'; /* |RA| Z | RCODE | */
  118. *dnsp++ = '\0';
  119. *dnsp++ = 1; /* QDCOUNT (number of entries in the question section) */
  120. *dnsp++ = '\0';
  121. *dnsp++ = '\0'; /* ANCOUNT */
  122. *dnsp++ = '\0';
  123. *dnsp++ = '\0'; /* NSCOUNT */
  124. *dnsp++ = '\0';
  125. *dnsp++ = '\0'; /* ARCOUNT */
  126. /* encode each label and store it in the QNAME */
  127. while(*hostp) {
  128. size_t labellen;
  129. char *dot = strchr(hostp, '.');
  130. if(dot)
  131. labellen = dot - hostp;
  132. else
  133. labellen = strlen(hostp);
  134. if((labellen > 63) || (!labellen)) {
  135. /* label is too long or too short, error out */
  136. *olen = 0;
  137. return DOH_DNS_BAD_LABEL;
  138. }
  139. /* label is non-empty, process it */
  140. *dnsp++ = (unsigned char)labellen;
  141. memcpy(dnsp, hostp, labellen);
  142. dnsp += labellen;
  143. hostp += labellen;
  144. /* advance past dot, but only if there is one */
  145. if(dot)
  146. hostp++;
  147. } /* next label */
  148. *dnsp++ = 0; /* append zero-length label for root */
  149. /* There are assigned TYPE codes beyond 255: use range [1..65535] */
  150. *dnsp++ = (unsigned char)(255 & (dnstype>>8)); /* upper 8 bit TYPE */
  151. *dnsp++ = (unsigned char)(255 & dnstype); /* lower 8 bit TYPE */
  152. *dnsp++ = '\0'; /* upper 8 bit CLASS */
  153. *dnsp++ = DNS_CLASS_IN; /* IN - "the Internet" */
  154. *olen = dnsp - orig;
  155. /* verify that our estimation of length is valid, since
  156. * this has led to buffer overflows in this function */
  157. DEBUGASSERT(*olen == expected_len);
  158. return DOH_OK;
  159. }
  160. static size_t
  161. doh_write_cb(const void *contents, size_t size, size_t nmemb, void *userp)
  162. {
  163. size_t realsize = size * nmemb;
  164. struct dynbuf *mem = (struct dynbuf *)userp;
  165. if(Curl_dyn_addn(mem, contents, realsize))
  166. return 0;
  167. return realsize;
  168. }
  169. #if defined(USE_HTTPSRR) && defined(DEBUGBUILD)
  170. static void local_print_buf(struct Curl_easy *data,
  171. const char *prefix,
  172. unsigned char *buf, size_t len)
  173. {
  174. unsigned char hexstr[LOCAL_PB_HEXMAX];
  175. size_t hlen = LOCAL_PB_HEXMAX;
  176. bool truncated = false;
  177. if(len > (LOCAL_PB_HEXMAX / 2))
  178. truncated = true;
  179. Curl_hexencode(buf, len, hexstr, hlen);
  180. if(!truncated)
  181. infof(data, "%s: len=%d, val=%s", prefix, (int)len, hexstr);
  182. else
  183. infof(data, "%s: len=%d (truncated)val=%s", prefix, (int)len, hexstr);
  184. return;
  185. }
  186. #endif
  187. /* called from multi.c when this DoH transfer is complete */
  188. static int doh_done(struct Curl_easy *doh, CURLcode result)
  189. {
  190. struct Curl_easy *data = doh->set.dohfor;
  191. struct dohdata *dohp = data->req.doh;
  192. /* so one of the DoH request done for the 'data' transfer is now complete! */
  193. dohp->pending--;
  194. infof(doh, "a DoH request is completed, %u to go", dohp->pending);
  195. if(result)
  196. infof(doh, "DoH request %s", curl_easy_strerror(result));
  197. if(!dohp->pending) {
  198. /* DoH completed */
  199. curl_slist_free_all(dohp->headers);
  200. dohp->headers = NULL;
  201. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  202. }
  203. return 0;
  204. }
  205. #define ERROR_CHECK_SETOPT(x,y) \
  206. do { \
  207. result = curl_easy_setopt(doh, x, y); \
  208. if(result && \
  209. result != CURLE_NOT_BUILT_IN && \
  210. result != CURLE_UNKNOWN_OPTION) \
  211. goto error; \
  212. } while(0)
  213. static CURLcode dohprobe(struct Curl_easy *data,
  214. struct dnsprobe *p, DNStype dnstype,
  215. const char *host,
  216. const char *url, CURLM *multi,
  217. struct curl_slist *headers)
  218. {
  219. struct Curl_easy *doh = NULL;
  220. CURLcode result = CURLE_OK;
  221. timediff_t timeout_ms;
  222. DOHcode d = doh_encode(host, dnstype, p->dohbuffer, sizeof(p->dohbuffer),
  223. &p->dohlen);
  224. if(d) {
  225. failf(data, "Failed to encode DoH packet [%d]", d);
  226. return CURLE_OUT_OF_MEMORY;
  227. }
  228. p->dnstype = dnstype;
  229. Curl_dyn_init(&p->serverdoh, DYN_DOH_RESPONSE);
  230. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  231. if(timeout_ms <= 0) {
  232. result = CURLE_OPERATION_TIMEDOUT;
  233. goto error;
  234. }
  235. /* Curl_open() is the internal version of curl_easy_init() */
  236. result = Curl_open(&doh);
  237. if(!result) {
  238. /* pass in the struct pointer via a local variable to please coverity and
  239. the gcc typecheck helpers */
  240. struct dynbuf *resp = &p->serverdoh;
  241. doh->state.internal = true;
  242. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  243. doh->state.feat = &Curl_doh_trc;
  244. #endif
  245. ERROR_CHECK_SETOPT(CURLOPT_URL, url);
  246. ERROR_CHECK_SETOPT(CURLOPT_DEFAULT_PROTOCOL, "https");
  247. ERROR_CHECK_SETOPT(CURLOPT_WRITEFUNCTION, doh_write_cb);
  248. ERROR_CHECK_SETOPT(CURLOPT_WRITEDATA, resp);
  249. ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDS, p->dohbuffer);
  250. ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDSIZE, (long)p->dohlen);
  251. ERROR_CHECK_SETOPT(CURLOPT_HTTPHEADER, headers);
  252. #ifdef USE_HTTP2
  253. ERROR_CHECK_SETOPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  254. ERROR_CHECK_SETOPT(CURLOPT_PIPEWAIT, 1L);
  255. #endif
  256. #ifndef DEBUGBUILD
  257. /* enforce HTTPS if not debug */
  258. ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
  259. #else
  260. /* in debug mode, also allow http */
  261. ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTP|CURLPROTO_HTTPS);
  262. #endif
  263. ERROR_CHECK_SETOPT(CURLOPT_TIMEOUT_MS, (long)timeout_ms);
  264. ERROR_CHECK_SETOPT(CURLOPT_SHARE, data->share);
  265. if(data->set.err && data->set.err != stderr)
  266. ERROR_CHECK_SETOPT(CURLOPT_STDERR, data->set.err);
  267. if(Curl_trc_ft_is_verbose(data, &Curl_doh_trc))
  268. ERROR_CHECK_SETOPT(CURLOPT_VERBOSE, 1L);
  269. if(data->set.no_signal)
  270. ERROR_CHECK_SETOPT(CURLOPT_NOSIGNAL, 1L);
  271. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYHOST,
  272. data->set.doh_verifyhost ? 2L : 0L);
  273. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYPEER,
  274. data->set.doh_verifypeer ? 1L : 0L);
  275. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYSTATUS,
  276. data->set.doh_verifystatus ? 1L : 0L);
  277. /* Inherit *some* SSL options from the user's transfer. This is a
  278. best-guess as to which options are needed for compatibility. #3661
  279. Note DoH does not inherit the user's proxy server so proxy SSL settings
  280. have no effect and are not inherited. If that changes then two new
  281. options should be added to check doh proxy insecure separately,
  282. CURLOPT_DOH_PROXY_SSL_VERIFYHOST and CURLOPT_DOH_PROXY_SSL_VERIFYPEER.
  283. */
  284. if(data->set.ssl.falsestart)
  285. ERROR_CHECK_SETOPT(CURLOPT_SSL_FALSESTART, 1L);
  286. if(data->set.str[STRING_SSL_CAFILE]) {
  287. ERROR_CHECK_SETOPT(CURLOPT_CAINFO,
  288. data->set.str[STRING_SSL_CAFILE]);
  289. }
  290. if(data->set.blobs[BLOB_CAINFO]) {
  291. ERROR_CHECK_SETOPT(CURLOPT_CAINFO_BLOB,
  292. data->set.blobs[BLOB_CAINFO]);
  293. }
  294. if(data->set.str[STRING_SSL_CAPATH]) {
  295. ERROR_CHECK_SETOPT(CURLOPT_CAPATH,
  296. data->set.str[STRING_SSL_CAPATH]);
  297. }
  298. if(data->set.str[STRING_SSL_CRLFILE]) {
  299. ERROR_CHECK_SETOPT(CURLOPT_CRLFILE,
  300. data->set.str[STRING_SSL_CRLFILE]);
  301. }
  302. if(data->set.ssl.certinfo)
  303. ERROR_CHECK_SETOPT(CURLOPT_CERTINFO, 1L);
  304. if(data->set.ssl.fsslctx)
  305. ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_FUNCTION, data->set.ssl.fsslctx);
  306. if(data->set.ssl.fsslctxp)
  307. ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_DATA, data->set.ssl.fsslctxp);
  308. if(data->set.fdebug)
  309. ERROR_CHECK_SETOPT(CURLOPT_DEBUGFUNCTION, data->set.fdebug);
  310. if(data->set.debugdata)
  311. ERROR_CHECK_SETOPT(CURLOPT_DEBUGDATA, data->set.debugdata);
  312. if(data->set.str[STRING_SSL_EC_CURVES]) {
  313. ERROR_CHECK_SETOPT(CURLOPT_SSL_EC_CURVES,
  314. data->set.str[STRING_SSL_EC_CURVES]);
  315. }
  316. {
  317. long mask =
  318. (data->set.ssl.enable_beast ?
  319. CURLSSLOPT_ALLOW_BEAST : 0) |
  320. (data->set.ssl.no_revoke ?
  321. CURLSSLOPT_NO_REVOKE : 0) |
  322. (data->set.ssl.no_partialchain ?
  323. CURLSSLOPT_NO_PARTIALCHAIN : 0) |
  324. (data->set.ssl.revoke_best_effort ?
  325. CURLSSLOPT_REVOKE_BEST_EFFORT : 0) |
  326. (data->set.ssl.native_ca_store ?
  327. CURLSSLOPT_NATIVE_CA : 0) |
  328. (data->set.ssl.auto_client_cert ?
  329. CURLSSLOPT_AUTO_CLIENT_CERT : 0);
  330. (void)curl_easy_setopt(doh, CURLOPT_SSL_OPTIONS, mask);
  331. }
  332. doh->set.fmultidone = doh_done;
  333. doh->set.dohfor = data; /* identify for which transfer this is done */
  334. p->easy = doh;
  335. /* DoH handles must not inherit private_data. The handles may be passed to
  336. the user via callbacks and the user will be able to identify them as
  337. internal handles because private data is not set. The user can then set
  338. private_data via CURLOPT_PRIVATE if they so choose. */
  339. DEBUGASSERT(!doh->set.private_data);
  340. if(curl_multi_add_handle(multi, doh))
  341. goto error;
  342. }
  343. else
  344. goto error;
  345. return CURLE_OK;
  346. error:
  347. Curl_close(&doh);
  348. return result;
  349. }
  350. /*
  351. * Curl_doh() resolves a name using DoH. It resolves a name and returns a
  352. * 'Curl_addrinfo *' with the address information.
  353. */
  354. struct Curl_addrinfo *Curl_doh(struct Curl_easy *data,
  355. const char *hostname,
  356. int port,
  357. int *waitp)
  358. {
  359. CURLcode result = CURLE_OK;
  360. int slot;
  361. struct dohdata *dohp;
  362. struct connectdata *conn = data->conn;
  363. #ifdef USE_HTTPSRR
  364. /* for now, this is only used when ECH is enabled */
  365. # ifdef USE_ECH
  366. char *qname = NULL;
  367. # endif
  368. #endif
  369. *waitp = FALSE;
  370. (void)hostname;
  371. (void)port;
  372. DEBUGASSERT(!data->req.doh);
  373. DEBUGASSERT(conn);
  374. /* start clean, consider allocating this struct on demand */
  375. dohp = data->req.doh = calloc(1, sizeof(struct dohdata));
  376. if(!dohp)
  377. return NULL;
  378. conn->bits.doh = TRUE;
  379. dohp->host = hostname;
  380. dohp->port = port;
  381. dohp->headers =
  382. curl_slist_append(NULL,
  383. "Content-Type: application/dns-message");
  384. if(!dohp->headers)
  385. goto error;
  386. /* create IPv4 DoH request */
  387. result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V4],
  388. DNS_TYPE_A, hostname, data->set.str[STRING_DOH],
  389. data->multi, dohp->headers);
  390. if(result)
  391. goto error;
  392. dohp->pending++;
  393. #ifdef USE_IPV6
  394. if((conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  395. /* create IPv6 DoH request */
  396. result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_IPADDR_V6],
  397. DNS_TYPE_AAAA, hostname, data->set.str[STRING_DOH],
  398. data->multi, dohp->headers);
  399. if(result)
  400. goto error;
  401. dohp->pending++;
  402. }
  403. #endif
  404. #ifdef USE_HTTPSRR
  405. /*
  406. * TODO: Figure out the conditions under which we want to make
  407. * a request for an HTTPS RR when we are not doing ECH. For now,
  408. * making this request breaks a bunch of DoH tests, e.g. test2100,
  409. * where the additional request doesn't match the pre-cooked data
  410. * files, so there's a bit of work attached to making the request
  411. * in a non-ECH use-case. For the present, we'll only make the
  412. * request when ECH is enabled in the build and is being used for
  413. * the curl operation.
  414. */
  415. # ifdef USE_ECH
  416. if(data->set.tls_ech & CURLECH_ENABLE
  417. || data->set.tls_ech & CURLECH_HARD) {
  418. if(port == 443)
  419. qname = strdup(hostname);
  420. else
  421. qname = aprintf("_%d._https.%s", port, hostname);
  422. if(!qname)
  423. goto error;
  424. result = dohprobe(data, &dohp->probe[DOH_PROBE_SLOT_HTTPS],
  425. DNS_TYPE_HTTPS, qname, data->set.str[STRING_DOH],
  426. data->multi, dohp->headers);
  427. free(qname);
  428. if(result)
  429. goto error;
  430. dohp->pending++;
  431. }
  432. # endif
  433. #endif
  434. *waitp = TRUE; /* this never returns synchronously */
  435. return NULL;
  436. error:
  437. curl_slist_free_all(dohp->headers);
  438. data->req.doh->headers = NULL;
  439. for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
  440. (void)curl_multi_remove_handle(data->multi, dohp->probe[slot].easy);
  441. Curl_close(&dohp->probe[slot].easy);
  442. }
  443. Curl_safefree(data->req.doh);
  444. return NULL;
  445. }
  446. static DOHcode skipqname(const unsigned char *doh, size_t dohlen,
  447. unsigned int *indexp)
  448. {
  449. unsigned char length;
  450. do {
  451. if(dohlen < (*indexp + 1))
  452. return DOH_DNS_OUT_OF_RANGE;
  453. length = doh[*indexp];
  454. if((length & 0xc0) == 0xc0) {
  455. /* name pointer, advance over it and be done */
  456. if(dohlen < (*indexp + 2))
  457. return DOH_DNS_OUT_OF_RANGE;
  458. *indexp += 2;
  459. break;
  460. }
  461. if(length & 0xc0)
  462. return DOH_DNS_BAD_LABEL;
  463. if(dohlen < (*indexp + 1 + length))
  464. return DOH_DNS_OUT_OF_RANGE;
  465. *indexp += (unsigned int)(1 + length);
  466. } while(length);
  467. return DOH_OK;
  468. }
  469. static unsigned short get16bit(const unsigned char *doh, unsigned int index)
  470. {
  471. return (unsigned short)((doh[index] << 8) | doh[index + 1]);
  472. }
  473. static unsigned int get32bit(const unsigned char *doh, unsigned int index)
  474. {
  475. /* make clang and gcc optimize this to bswap by incrementing
  476. the pointer first. */
  477. doh += index;
  478. /* avoid undefined behavior by casting to unsigned before shifting
  479. 24 bits, possibly into the sign bit. codegen is same, but
  480. ub sanitizer won't be upset */
  481. return ((unsigned)doh[0] << 24) | ((unsigned)doh[1] << 16) |
  482. ((unsigned)doh[2] << 8) | doh[3];
  483. }
  484. static DOHcode store_a(const unsigned char *doh, int index, struct dohentry *d)
  485. {
  486. /* silently ignore addresses over the limit */
  487. if(d->numaddr < DOH_MAX_ADDR) {
  488. struct dohaddr *a = &d->addr[d->numaddr];
  489. a->type = DNS_TYPE_A;
  490. memcpy(&a->ip.v4, &doh[index], 4);
  491. d->numaddr++;
  492. }
  493. return DOH_OK;
  494. }
  495. static DOHcode store_aaaa(const unsigned char *doh,
  496. int index,
  497. struct dohentry *d)
  498. {
  499. /* silently ignore addresses over the limit */
  500. if(d->numaddr < DOH_MAX_ADDR) {
  501. struct dohaddr *a = &d->addr[d->numaddr];
  502. a->type = DNS_TYPE_AAAA;
  503. memcpy(&a->ip.v6, &doh[index], 16);
  504. d->numaddr++;
  505. }
  506. return DOH_OK;
  507. }
  508. #ifdef USE_HTTPSRR
  509. static DOHcode store_https(const unsigned char *doh,
  510. int index,
  511. struct dohentry *d,
  512. uint16_t len)
  513. {
  514. /* silently ignore RRs over the limit */
  515. if(d->numhttps_rrs < DOH_MAX_HTTPS) {
  516. struct dohhttps_rr *h = &d->https_rrs[d->numhttps_rrs];
  517. h->val = Curl_memdup(&doh[index], len);
  518. if(!h->val)
  519. return DOH_OUT_OF_MEM;
  520. h->len = len;
  521. d->numhttps_rrs++;
  522. }
  523. return DOH_OK;
  524. }
  525. #endif
  526. static DOHcode store_cname(const unsigned char *doh,
  527. size_t dohlen,
  528. unsigned int index,
  529. struct dohentry *d)
  530. {
  531. struct dynbuf *c;
  532. unsigned int loop = 128; /* a valid DNS name can never loop this much */
  533. unsigned char length;
  534. if(d->numcname == DOH_MAX_CNAME)
  535. return DOH_OK; /* skip! */
  536. c = &d->cname[d->numcname++];
  537. do {
  538. if(index >= dohlen)
  539. return DOH_DNS_OUT_OF_RANGE;
  540. length = doh[index];
  541. if((length & 0xc0) == 0xc0) {
  542. int newpos;
  543. /* name pointer, get the new offset (14 bits) */
  544. if((index + 1) >= dohlen)
  545. return DOH_DNS_OUT_OF_RANGE;
  546. /* move to the new index */
  547. newpos = (length & 0x3f) << 8 | doh[index + 1];
  548. index = (unsigned int)newpos;
  549. continue;
  550. }
  551. else if(length & 0xc0)
  552. return DOH_DNS_BAD_LABEL; /* bad input */
  553. else
  554. index++;
  555. if(length) {
  556. if(Curl_dyn_len(c)) {
  557. if(Curl_dyn_addn(c, STRCONST(".")))
  558. return DOH_OUT_OF_MEM;
  559. }
  560. if((index + length) > dohlen)
  561. return DOH_DNS_BAD_LABEL;
  562. if(Curl_dyn_addn(c, &doh[index], length))
  563. return DOH_OUT_OF_MEM;
  564. index += length;
  565. }
  566. } while(length && --loop);
  567. if(!loop)
  568. return DOH_DNS_LABEL_LOOP;
  569. return DOH_OK;
  570. }
  571. static DOHcode rdata(const unsigned char *doh,
  572. size_t dohlen,
  573. unsigned short rdlength,
  574. unsigned short type,
  575. int index,
  576. struct dohentry *d)
  577. {
  578. /* RDATA
  579. - A (TYPE 1): 4 bytes
  580. - AAAA (TYPE 28): 16 bytes
  581. - NS (TYPE 2): N bytes
  582. - HTTPS (TYPE 65): N bytes */
  583. DOHcode rc;
  584. switch(type) {
  585. case DNS_TYPE_A:
  586. if(rdlength != 4)
  587. return DOH_DNS_RDATA_LEN;
  588. rc = store_a(doh, index, d);
  589. if(rc)
  590. return rc;
  591. break;
  592. case DNS_TYPE_AAAA:
  593. if(rdlength != 16)
  594. return DOH_DNS_RDATA_LEN;
  595. rc = store_aaaa(doh, index, d);
  596. if(rc)
  597. return rc;
  598. break;
  599. #ifdef USE_HTTPSRR
  600. case DNS_TYPE_HTTPS:
  601. rc = store_https(doh, index, d, rdlength);
  602. if(rc)
  603. return rc;
  604. break;
  605. #endif
  606. case DNS_TYPE_CNAME:
  607. rc = store_cname(doh, dohlen, (unsigned int)index, d);
  608. if(rc)
  609. return rc;
  610. break;
  611. case DNS_TYPE_DNAME:
  612. /* explicit for clarity; just skip; rely on synthesized CNAME */
  613. break;
  614. default:
  615. /* unsupported type, just skip it */
  616. break;
  617. }
  618. return DOH_OK;
  619. }
  620. UNITTEST void de_init(struct dohentry *de)
  621. {
  622. int i;
  623. memset(de, 0, sizeof(*de));
  624. de->ttl = INT_MAX;
  625. for(i = 0; i < DOH_MAX_CNAME; i++)
  626. Curl_dyn_init(&de->cname[i], DYN_DOH_CNAME);
  627. }
  628. UNITTEST DOHcode doh_decode(const unsigned char *doh,
  629. size_t dohlen,
  630. DNStype dnstype,
  631. struct dohentry *d)
  632. {
  633. unsigned char rcode;
  634. unsigned short qdcount;
  635. unsigned short ancount;
  636. unsigned short type = 0;
  637. unsigned short rdlength;
  638. unsigned short nscount;
  639. unsigned short arcount;
  640. unsigned int index = 12;
  641. DOHcode rc;
  642. if(dohlen < 12)
  643. return DOH_TOO_SMALL_BUFFER; /* too small */
  644. if(!doh || doh[0] || doh[1])
  645. return DOH_DNS_BAD_ID; /* bad ID */
  646. rcode = doh[3] & 0x0f;
  647. if(rcode)
  648. return DOH_DNS_BAD_RCODE; /* bad rcode */
  649. qdcount = get16bit(doh, 4);
  650. while(qdcount) {
  651. rc = skipqname(doh, dohlen, &index);
  652. if(rc)
  653. return rc; /* bad qname */
  654. if(dohlen < (index + 4))
  655. return DOH_DNS_OUT_OF_RANGE;
  656. index += 4; /* skip question's type and class */
  657. qdcount--;
  658. }
  659. ancount = get16bit(doh, 6);
  660. while(ancount) {
  661. unsigned short class;
  662. unsigned int ttl;
  663. rc = skipqname(doh, dohlen, &index);
  664. if(rc)
  665. return rc; /* bad qname */
  666. if(dohlen < (index + 2))
  667. return DOH_DNS_OUT_OF_RANGE;
  668. type = get16bit(doh, index);
  669. if((type != DNS_TYPE_CNAME) /* may be synthesized from DNAME */
  670. && (type != DNS_TYPE_DNAME) /* if present, accept and ignore */
  671. && (type != dnstype))
  672. /* Not the same type as was asked for nor CNAME nor DNAME */
  673. return DOH_DNS_UNEXPECTED_TYPE;
  674. index += 2;
  675. if(dohlen < (index + 2))
  676. return DOH_DNS_OUT_OF_RANGE;
  677. class = get16bit(doh, index);
  678. if(DNS_CLASS_IN != class)
  679. return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
  680. index += 2;
  681. if(dohlen < (index + 4))
  682. return DOH_DNS_OUT_OF_RANGE;
  683. ttl = get32bit(doh, index);
  684. if(ttl < d->ttl)
  685. d->ttl = ttl;
  686. index += 4;
  687. if(dohlen < (index + 2))
  688. return DOH_DNS_OUT_OF_RANGE;
  689. rdlength = get16bit(doh, index);
  690. index += 2;
  691. if(dohlen < (index + rdlength))
  692. return DOH_DNS_OUT_OF_RANGE;
  693. rc = rdata(doh, dohlen, rdlength, type, (int)index, d);
  694. if(rc)
  695. return rc; /* bad rdata */
  696. index += rdlength;
  697. ancount--;
  698. }
  699. nscount = get16bit(doh, 8);
  700. while(nscount) {
  701. rc = skipqname(doh, dohlen, &index);
  702. if(rc)
  703. return rc; /* bad qname */
  704. if(dohlen < (index + 8))
  705. return DOH_DNS_OUT_OF_RANGE;
  706. index += 2 + 2 + 4; /* type, class and ttl */
  707. if(dohlen < (index + 2))
  708. return DOH_DNS_OUT_OF_RANGE;
  709. rdlength = get16bit(doh, index);
  710. index += 2;
  711. if(dohlen < (index + rdlength))
  712. return DOH_DNS_OUT_OF_RANGE;
  713. index += rdlength;
  714. nscount--;
  715. }
  716. arcount = get16bit(doh, 10);
  717. while(arcount) {
  718. rc = skipqname(doh, dohlen, &index);
  719. if(rc)
  720. return rc; /* bad qname */
  721. if(dohlen < (index + 8))
  722. return DOH_DNS_OUT_OF_RANGE;
  723. index += 2 + 2 + 4; /* type, class and ttl */
  724. if(dohlen < (index + 2))
  725. return DOH_DNS_OUT_OF_RANGE;
  726. rdlength = get16bit(doh, index);
  727. index += 2;
  728. if(dohlen < (index + rdlength))
  729. return DOH_DNS_OUT_OF_RANGE;
  730. index += rdlength;
  731. arcount--;
  732. }
  733. if(index != dohlen)
  734. return DOH_DNS_MALFORMAT; /* something is wrong */
  735. #ifdef USE_HTTTPS
  736. if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr && !d->numhttps_rrs)
  737. #else
  738. if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr)
  739. #endif
  740. /* nothing stored! */
  741. return DOH_NO_CONTENT;
  742. return DOH_OK; /* ok */
  743. }
  744. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  745. static void showdoh(struct Curl_easy *data,
  746. const struct dohentry *d)
  747. {
  748. int i;
  749. infof(data, "[DoH] TTL: %u seconds", d->ttl);
  750. for(i = 0; i < d->numaddr; i++) {
  751. const struct dohaddr *a = &d->addr[i];
  752. if(a->type == DNS_TYPE_A) {
  753. infof(data, "[DoH] A: %u.%u.%u.%u",
  754. a->ip.v4[0], a->ip.v4[1],
  755. a->ip.v4[2], a->ip.v4[3]);
  756. }
  757. else if(a->type == DNS_TYPE_AAAA) {
  758. int j;
  759. char buffer[128];
  760. char *ptr;
  761. size_t len;
  762. len = msnprintf(buffer, 128, "[DoH] AAAA: ");
  763. ptr = &buffer[len];
  764. len = sizeof(buffer) - len;
  765. for(j = 0; j < 16; j += 2) {
  766. size_t l;
  767. msnprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6[j],
  768. d->addr[i].ip.v6[j + 1]);
  769. l = strlen(ptr);
  770. len -= l;
  771. ptr += l;
  772. }
  773. infof(data, "%s", buffer);
  774. }
  775. }
  776. #ifdef USE_HTTPSRR
  777. for(i = 0; i < d->numhttps_rrs; i++) {
  778. # ifdef DEBUGBUILD
  779. local_print_buf(data, "DoH HTTPS",
  780. d->https_rrs[i].val, d->https_rrs[i].len);
  781. # else
  782. infof(data, "DoH HTTPS RR: length %d", d->https_rrs[i].len);
  783. # endif
  784. }
  785. #endif
  786. for(i = 0; i < d->numcname; i++) {
  787. infof(data, "CNAME: %s", Curl_dyn_ptr(&d->cname[i]));
  788. }
  789. }
  790. #else
  791. #define showdoh(x,y)
  792. #endif
  793. /*
  794. * doh2ai()
  795. *
  796. * This function returns a pointer to the first element of a newly allocated
  797. * Curl_addrinfo struct linked list filled with the data from a set of DoH
  798. * lookups. Curl_addrinfo is meant to work like the addrinfo struct does for
  799. * a IPv6 stack, but usable also for IPv4, all hosts and environments.
  800. *
  801. * The memory allocated by this function *MUST* be free'd later on calling
  802. * Curl_freeaddrinfo(). For each successful call to this function there
  803. * must be an associated call later to Curl_freeaddrinfo().
  804. */
  805. static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
  806. int port, struct Curl_addrinfo **aip)
  807. {
  808. struct Curl_addrinfo *ai;
  809. struct Curl_addrinfo *prevai = NULL;
  810. struct Curl_addrinfo *firstai = NULL;
  811. struct sockaddr_in *addr;
  812. #ifdef USE_IPV6
  813. struct sockaddr_in6 *addr6;
  814. #endif
  815. CURLcode result = CURLE_OK;
  816. int i;
  817. size_t hostlen = strlen(hostname) + 1; /* include null-terminator */
  818. DEBUGASSERT(de);
  819. if(!de->numaddr)
  820. return CURLE_COULDNT_RESOLVE_HOST;
  821. for(i = 0; i < de->numaddr; i++) {
  822. size_t ss_size;
  823. CURL_SA_FAMILY_T addrtype;
  824. if(de->addr[i].type == DNS_TYPE_AAAA) {
  825. #ifndef USE_IPV6
  826. /* we can't handle IPv6 addresses */
  827. continue;
  828. #else
  829. ss_size = sizeof(struct sockaddr_in6);
  830. addrtype = AF_INET6;
  831. #endif
  832. }
  833. else {
  834. ss_size = sizeof(struct sockaddr_in);
  835. addrtype = AF_INET;
  836. }
  837. ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
  838. if(!ai) {
  839. result = CURLE_OUT_OF_MEMORY;
  840. break;
  841. }
  842. ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
  843. ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
  844. memcpy(ai->ai_canonname, hostname, hostlen);
  845. if(!firstai)
  846. /* store the pointer we want to return from this function */
  847. firstai = ai;
  848. if(prevai)
  849. /* make the previous entry point to this */
  850. prevai->ai_next = ai;
  851. ai->ai_family = addrtype;
  852. /* we return all names as STREAM, so when using this address for TFTP
  853. the type must be ignored and conn->socktype be used instead! */
  854. ai->ai_socktype = SOCK_STREAM;
  855. ai->ai_addrlen = (curl_socklen_t)ss_size;
  856. /* leave the rest of the struct filled with zero */
  857. switch(ai->ai_family) {
  858. case AF_INET:
  859. addr = (void *)ai->ai_addr; /* storage area for this info */
  860. DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
  861. memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
  862. #ifdef __MINGW32__
  863. addr->sin_family = (short)addrtype;
  864. #else
  865. addr->sin_family = addrtype;
  866. #endif
  867. addr->sin_port = htons((unsigned short)port);
  868. break;
  869. #ifdef USE_IPV6
  870. case AF_INET6:
  871. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  872. DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
  873. memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
  874. #ifdef __MINGW32__
  875. addr6->sin6_family = (short)addrtype;
  876. #else
  877. addr6->sin6_family = addrtype;
  878. #endif
  879. addr6->sin6_port = htons((unsigned short)port);
  880. break;
  881. #endif
  882. }
  883. prevai = ai;
  884. }
  885. if(result) {
  886. Curl_freeaddrinfo(firstai);
  887. firstai = NULL;
  888. }
  889. *aip = firstai;
  890. return result;
  891. }
  892. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  893. static const char *type2name(DNStype dnstype)
  894. {
  895. switch(dnstype) {
  896. case DNS_TYPE_A:
  897. return "A";
  898. case DNS_TYPE_AAAA:
  899. return "AAAA";
  900. #ifdef USE_HTTPSRR
  901. case DNS_TYPE_HTTPS:
  902. return "HTTPS";
  903. #endif
  904. default:
  905. return "unknown";
  906. }
  907. }
  908. #endif
  909. UNITTEST void de_cleanup(struct dohentry *d)
  910. {
  911. int i = 0;
  912. for(i = 0; i < d->numcname; i++) {
  913. Curl_dyn_free(&d->cname[i]);
  914. }
  915. #ifdef USE_HTTPSRR
  916. for(i = 0; i < d->numhttps_rrs; i++)
  917. free(d->https_rrs[i].val);
  918. #endif
  919. }
  920. #ifdef USE_HTTPSRR
  921. /*
  922. * @brief decode the DNS name in a binary RRData
  923. * @param buf points to the buffer (in/out)
  924. * @param remaining points to the remaining buffer length (in/out)
  925. * @param dnsname returns the string form name on success
  926. * @return is 1 for success, error otherwise
  927. *
  928. * The encoding here is defined in
  929. * https://tools.ietf.org/html/rfc1035#section-3.1
  930. *
  931. * The input buffer pointer will be modified so it points to
  932. * just after the end of the DNS name encoding on output. (And
  933. * that's why it's an "unsigned char **" :-)
  934. */
  935. static CURLcode local_decode_rdata_name(unsigned char **buf, size_t *remaining,
  936. char **dnsname)
  937. {
  938. unsigned char *cp = NULL;
  939. int rem = 0;
  940. unsigned char clen = 0; /* chunk len */
  941. struct dynbuf thename;
  942. DEBUGASSERT(buf && remaining && dnsname);
  943. if(!buf || !remaining || !dnsname)
  944. return CURLE_OUT_OF_MEMORY;
  945. rem = (int)*remaining;
  946. if(rem <= 0) {
  947. Curl_dyn_free(&thename);
  948. return CURLE_OUT_OF_MEMORY;
  949. }
  950. Curl_dyn_init(&thename, CURL_MAXLEN_host_name);
  951. cp = *buf;
  952. clen = *cp++;
  953. if(clen == 0) {
  954. /* special case - return "." as name */
  955. if(Curl_dyn_addn(&thename, ".", 1))
  956. return CURLE_OUT_OF_MEMORY;
  957. }
  958. while(clen) {
  959. if(clen >= rem) {
  960. Curl_dyn_free(&thename);
  961. return CURLE_OUT_OF_MEMORY;
  962. }
  963. if(Curl_dyn_addn(&thename, cp, clen) ||
  964. Curl_dyn_addn(&thename, ".", 1))
  965. return CURLE_TOO_LARGE;
  966. cp += clen;
  967. rem -= (clen + 1);
  968. if(rem <= 0) {
  969. Curl_dyn_free(&thename);
  970. return CURLE_OUT_OF_MEMORY;
  971. }
  972. clen = *cp++;
  973. }
  974. *buf = cp;
  975. *remaining = rem - 1;
  976. *dnsname = Curl_dyn_ptr(&thename);
  977. return CURLE_OK;
  978. }
  979. static CURLcode local_decode_rdata_alpn(unsigned char *rrval, size_t len,
  980. char **alpns)
  981. {
  982. /*
  983. * spec here is as per draft-ietf-dnsop-svcb-https, section-7.1.1
  984. * encoding is catenated list of strings each preceded by a one
  985. * octet length
  986. * output is comma-sep list of the strings
  987. * implementations may or may not handle quoting of comma within
  988. * string values, so we might see a comma within the wire format
  989. * version of a string, in which case we'll precede that by a
  990. * backslash - same goes for a backslash character, and of course
  991. * we need to use two backslashes in strings when we mean one;-)
  992. */
  993. int remaining = (int) len;
  994. char *oval;
  995. size_t i;
  996. unsigned char *cp = rrval;
  997. struct dynbuf dval;
  998. if(!alpns)
  999. return CURLE_OUT_OF_MEMORY;
  1000. Curl_dyn_init(&dval, DYN_DOH_RESPONSE);
  1001. remaining = (int)len;
  1002. cp = rrval;
  1003. while(remaining > 0) {
  1004. size_t tlen = (size_t) *cp++;
  1005. /* if not 1st time, add comma */
  1006. if(remaining != (int)len && Curl_dyn_addn(&dval, ",", 1))
  1007. goto err;
  1008. remaining--;
  1009. if(tlen > (size_t)remaining)
  1010. goto err;
  1011. /* add escape char if needed, clunky but easier to read */
  1012. for(i = 0; i != tlen; i++) {
  1013. if('\\' == *cp || ',' == *cp) {
  1014. if(Curl_dyn_addn(&dval, "\\", 1))
  1015. goto err;
  1016. }
  1017. if(Curl_dyn_addn(&dval, cp++, 1))
  1018. goto err;
  1019. }
  1020. remaining -= (int)tlen;
  1021. }
  1022. /* this string is always null terminated */
  1023. oval = Curl_dyn_ptr(&dval);
  1024. if(!oval)
  1025. goto err;
  1026. *alpns = oval;
  1027. return CURLE_OK;
  1028. err:
  1029. Curl_dyn_free(&dval);
  1030. return CURLE_BAD_CONTENT_ENCODING;
  1031. }
  1032. #ifdef DEBUGBUILD
  1033. static CURLcode test_alpn_escapes(void)
  1034. {
  1035. /* we'll use an example from draft-ietf-dnsop-svcb, figure 10 */
  1036. static unsigned char example[] = {
  1037. 0x08, /* length 8 */
  1038. 0x66, 0x5c, 0x6f, 0x6f, 0x2c, 0x62, 0x61, 0x72, /* value "f\\oo,bar" */
  1039. 0x02, /* length 2 */
  1040. 0x68, 0x32 /* value "h2" */
  1041. };
  1042. size_t example_len = sizeof(example);
  1043. char *aval = NULL;
  1044. static const char *expected = "f\\\\oo\\,bar,h2";
  1045. if(local_decode_rdata_alpn(example, example_len, &aval) != CURLE_OK)
  1046. return CURLE_BAD_CONTENT_ENCODING;
  1047. if(strlen(aval) != strlen(expected))
  1048. return CURLE_BAD_CONTENT_ENCODING;
  1049. if(memcmp(aval, expected, strlen(aval)))
  1050. return CURLE_BAD_CONTENT_ENCODING;
  1051. return CURLE_OK;
  1052. }
  1053. #endif
  1054. static CURLcode Curl_doh_decode_httpsrr(unsigned char *rrval, size_t len,
  1055. struct Curl_https_rrinfo **hrr)
  1056. {
  1057. size_t remaining = len;
  1058. unsigned char *cp = rrval;
  1059. uint16_t pcode = 0, plen = 0;
  1060. struct Curl_https_rrinfo *lhrr = NULL;
  1061. char *dnsname = NULL;
  1062. #ifdef DEBUGBUILD
  1063. /* a few tests of escaping, shouldn't be here but ok for now */
  1064. if(test_alpn_escapes() != CURLE_OK)
  1065. return CURLE_OUT_OF_MEMORY;
  1066. #endif
  1067. lhrr = calloc(1, sizeof(struct Curl_https_rrinfo));
  1068. if(!lhrr)
  1069. return CURLE_OUT_OF_MEMORY;
  1070. lhrr->val = Curl_memdup(rrval, len);
  1071. if(!lhrr->val)
  1072. goto err;
  1073. lhrr->len = len;
  1074. if(remaining <= 2)
  1075. goto err;
  1076. lhrr->priority = (uint16_t)((cp[0] << 8) + cp[1]);
  1077. cp += 2;
  1078. remaining -= (uint16_t)2;
  1079. if(local_decode_rdata_name(&cp, &remaining, &dnsname) != CURLE_OK)
  1080. goto err;
  1081. lhrr->target = dnsname;
  1082. while(remaining >= 4) {
  1083. pcode = (uint16_t)((*cp << 8) + (*(cp + 1)));
  1084. cp += 2;
  1085. plen = (uint16_t)((*cp << 8) + (*(cp + 1)));
  1086. cp += 2;
  1087. remaining -= 4;
  1088. if(pcode == HTTPS_RR_CODE_ALPN) {
  1089. if(local_decode_rdata_alpn(cp, plen, &lhrr->alpns) != CURLE_OK)
  1090. goto err;
  1091. }
  1092. if(pcode == HTTPS_RR_CODE_NO_DEF_ALPN)
  1093. lhrr->no_def_alpn = TRUE;
  1094. else if(pcode == HTTPS_RR_CODE_IPV4) {
  1095. lhrr->ipv4hints = Curl_memdup(cp, plen);
  1096. if(!lhrr->ipv4hints)
  1097. goto err;
  1098. lhrr->ipv4hints_len = (size_t)plen;
  1099. }
  1100. else if(pcode == HTTPS_RR_CODE_ECH) {
  1101. lhrr->echconfiglist = Curl_memdup(cp, plen);
  1102. if(!lhrr->echconfiglist)
  1103. goto err;
  1104. lhrr->echconfiglist_len = (size_t)plen;
  1105. }
  1106. else if(pcode == HTTPS_RR_CODE_IPV6) {
  1107. lhrr->ipv6hints = Curl_memdup(cp, plen);
  1108. if(!lhrr->ipv6hints)
  1109. goto err;
  1110. lhrr->ipv6hints_len = (size_t)plen;
  1111. }
  1112. if(plen > 0 && plen <= remaining) {
  1113. cp += plen;
  1114. remaining -= plen;
  1115. }
  1116. }
  1117. DEBUGASSERT(!remaining);
  1118. *hrr = lhrr;
  1119. return CURLE_OK;
  1120. err:
  1121. if(lhrr) {
  1122. free(lhrr->target);
  1123. free(lhrr->echconfiglist);
  1124. free(lhrr->val);
  1125. free(lhrr);
  1126. }
  1127. return CURLE_OUT_OF_MEMORY;
  1128. }
  1129. # ifdef DEBUGBUILD
  1130. static void local_print_httpsrr(struct Curl_easy *data,
  1131. struct Curl_https_rrinfo *hrr)
  1132. {
  1133. DEBUGASSERT(hrr);
  1134. infof(data, "HTTPS RR: priority %d, target: %s",
  1135. hrr->priority, hrr->target);
  1136. if(hrr->alpns)
  1137. infof(data, "HTTPS RR: alpns %s", hrr->alpns);
  1138. else
  1139. infof(data, "HTTPS RR: no alpns");
  1140. if(hrr->no_def_alpn)
  1141. infof(data, "HTTPS RR: no_def_alpn set");
  1142. else
  1143. infof(data, "HTTPS RR: no_def_alpn not set");
  1144. if(hrr->ipv4hints) {
  1145. local_print_buf(data, "HTTPS RR: ipv4hints",
  1146. hrr->ipv4hints, hrr->ipv4hints_len);
  1147. }
  1148. else
  1149. infof(data, "HTTPS RR: no ipv4hints");
  1150. if(hrr->echconfiglist) {
  1151. local_print_buf(data, "HTTPS RR: ECHConfigList",
  1152. hrr->echconfiglist, hrr->echconfiglist_len);
  1153. }
  1154. else
  1155. infof(data, "HTTPS RR: no ECHConfigList");
  1156. if(hrr->ipv6hints) {
  1157. local_print_buf(data, "HTTPS RR: ipv6hint",
  1158. hrr->ipv6hints, hrr->ipv6hints_len);
  1159. }
  1160. else
  1161. infof(data, "HTTPS RR: no ipv6hints");
  1162. return;
  1163. }
  1164. # endif
  1165. #endif
  1166. CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
  1167. struct Curl_dns_entry **dnsp)
  1168. {
  1169. CURLcode result;
  1170. struct dohdata *dohp = data->req.doh;
  1171. *dnsp = NULL; /* defaults to no response */
  1172. if(!dohp)
  1173. return CURLE_OUT_OF_MEMORY;
  1174. if(!dohp->probe[DOH_PROBE_SLOT_IPADDR_V4].easy &&
  1175. !dohp->probe[DOH_PROBE_SLOT_IPADDR_V6].easy) {
  1176. failf(data, "Could not DoH-resolve: %s", data->state.async.hostname);
  1177. return CONN_IS_PROXIED(data->conn)?CURLE_COULDNT_RESOLVE_PROXY:
  1178. CURLE_COULDNT_RESOLVE_HOST;
  1179. }
  1180. else if(!dohp->pending) {
  1181. #ifndef USE_HTTPSRR
  1182. DOHcode rc[DOH_PROBE_SLOTS] = {
  1183. DOH_OK, DOH_OK
  1184. };
  1185. #else
  1186. DOHcode rc[DOH_PROBE_SLOTS] = {
  1187. DOH_OK, DOH_OK, DOH_OK
  1188. };
  1189. #endif
  1190. struct dohentry de;
  1191. int slot;
  1192. /* remove DoH handles from multi handle and close them */
  1193. for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
  1194. curl_multi_remove_handle(data->multi, dohp->probe[slot].easy);
  1195. Curl_close(&dohp->probe[slot].easy);
  1196. }
  1197. /* parse the responses, create the struct and return it! */
  1198. de_init(&de);
  1199. for(slot = 0; slot < DOH_PROBE_SLOTS; slot++) {
  1200. struct dnsprobe *p = &dohp->probe[slot];
  1201. if(!p->dnstype)
  1202. continue;
  1203. rc[slot] = doh_decode(Curl_dyn_uptr(&p->serverdoh),
  1204. Curl_dyn_len(&p->serverdoh),
  1205. p->dnstype,
  1206. &de);
  1207. Curl_dyn_free(&p->serverdoh);
  1208. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1209. if(rc[slot]) {
  1210. infof(data, "DoH: %s type %s for %s", doh_strerror(rc[slot]),
  1211. type2name(p->dnstype), dohp->host);
  1212. }
  1213. #endif
  1214. } /* next slot */
  1215. result = CURLE_COULDNT_RESOLVE_HOST; /* until we know better */
  1216. if(!rc[DOH_PROBE_SLOT_IPADDR_V4] || !rc[DOH_PROBE_SLOT_IPADDR_V6]) {
  1217. /* we have an address, of one kind or other */
  1218. struct Curl_dns_entry *dns;
  1219. struct Curl_addrinfo *ai;
  1220. if(Curl_trc_ft_is_verbose(data, &Curl_doh_trc)) {
  1221. infof(data, "[DoH] Host name: %s", dohp->host);
  1222. showdoh(data, &de);
  1223. }
  1224. result = doh2ai(&de, dohp->host, dohp->port, &ai);
  1225. if(result) {
  1226. de_cleanup(&de);
  1227. return result;
  1228. }
  1229. if(data->share)
  1230. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1231. /* we got a response, store it in the cache */
  1232. dns = Curl_cache_addr(data, ai, dohp->host, 0, dohp->port);
  1233. if(data->share)
  1234. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1235. if(!dns) {
  1236. /* returned failure, bail out nicely */
  1237. Curl_freeaddrinfo(ai);
  1238. }
  1239. else {
  1240. data->state.async.dns = dns;
  1241. *dnsp = dns;
  1242. result = CURLE_OK; /* address resolution OK */
  1243. }
  1244. } /* address processing done */
  1245. /* Now process any build-specific attributes retrieved from DNS */
  1246. #ifdef USE_HTTPSRR
  1247. if(de.numhttps_rrs > 0 && result == CURLE_OK && *dnsp) {
  1248. struct Curl_https_rrinfo *hrr = NULL;
  1249. result = Curl_doh_decode_httpsrr(de.https_rrs->val, de.https_rrs->len,
  1250. &hrr);
  1251. if(result) {
  1252. infof(data, "Failed to decode HTTPS RR");
  1253. return result;
  1254. }
  1255. infof(data, "Some HTTPS RR to process");
  1256. # ifdef DEBUGBUILD
  1257. local_print_httpsrr(data, hrr);
  1258. # endif
  1259. (*dnsp)->hinfo = hrr;
  1260. }
  1261. #endif
  1262. /* All done */
  1263. de_cleanup(&de);
  1264. Curl_safefree(data->req.doh);
  1265. return result;
  1266. } /* !dohp->pending */
  1267. /* else wait for pending DoH transactions to complete */
  1268. return CURLE_OK;
  1269. }
  1270. #endif /* CURL_DISABLE_DOH */