doh.c 41 KB

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