doh.c 38 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <[email protected]>, 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(char *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((CURL *)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, (CURLSH *)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. *waitp = FALSE;
  368. (void)hostname;
  369. (void)port;
  370. DEBUGASSERT(!data->req.doh);
  371. DEBUGASSERT(conn);
  372. /* start clean, consider allocating this struct on demand */
  373. dohp = data->req.doh = calloc(1, sizeof(struct doh_probes));
  374. if(!dohp)
  375. return NULL;
  376. for(i = 0; i < DOH_SLOT_COUNT; ++i) {
  377. dohp->probe[i].easy_mid = -1;
  378. }
  379. conn->bits.doh = TRUE;
  380. dohp->host = hostname;
  381. dohp->port = port;
  382. dohp->req_hds =
  383. curl_slist_append(NULL,
  384. "Content-Type: application/dns-message");
  385. if(!dohp->req_hds)
  386. goto error;
  387. /* create IPv4 DoH request */
  388. result = doh_run_probe(data, &dohp->probe[DOH_SLOT_IPV4],
  389. DNS_TYPE_A, hostname, data->set.str[STRING_DOH],
  390. data->multi, dohp->req_hds);
  391. if(result)
  392. goto error;
  393. dohp->pending++;
  394. #ifdef USE_IPV6
  395. if((conn->ip_version != CURL_IPRESOLVE_V4) && Curl_ipv6works(data)) {
  396. /* create IPv6 DoH request */
  397. result = doh_run_probe(data, &dohp->probe[DOH_SLOT_IPV6],
  398. DNS_TYPE_AAAA, hostname, data->set.str[STRING_DOH],
  399. data->multi, dohp->req_hds);
  400. if(result)
  401. goto error;
  402. dohp->pending++;
  403. }
  404. #endif
  405. #ifdef USE_HTTPSRR
  406. if(conn->handler->protocol & PROTO_FAMILY_HTTP) {
  407. /* Only use HTTPS RR for HTTP(S) transfers */
  408. char *qname = NULL;
  409. if(port != PORT_HTTPS) {
  410. qname = aprintf("_%d._https.%s", port, hostname);
  411. if(!qname)
  412. goto error;
  413. }
  414. result = doh_run_probe(data, &dohp->probe[DOH_SLOT_HTTPS_RR],
  415. DNS_TYPE_HTTPS,
  416. qname ? qname : hostname, data->set.str[STRING_DOH],
  417. data->multi, dohp->req_hds);
  418. free(qname);
  419. if(result)
  420. goto error;
  421. dohp->pending++;
  422. }
  423. #endif
  424. *waitp = TRUE; /* this never returns synchronously */
  425. return NULL;
  426. error:
  427. Curl_doh_cleanup(data);
  428. return NULL;
  429. }
  430. static DOHcode doh_skipqname(const unsigned char *doh, size_t dohlen,
  431. unsigned int *indexp)
  432. {
  433. unsigned char length;
  434. do {
  435. if(dohlen < (*indexp + 1))
  436. return DOH_DNS_OUT_OF_RANGE;
  437. length = doh[*indexp];
  438. if((length & 0xc0) == 0xc0) {
  439. /* name pointer, advance over it and be done */
  440. if(dohlen < (*indexp + 2))
  441. return DOH_DNS_OUT_OF_RANGE;
  442. *indexp += 2;
  443. break;
  444. }
  445. if(length & 0xc0)
  446. return DOH_DNS_BAD_LABEL;
  447. if(dohlen < (*indexp + 1 + length))
  448. return DOH_DNS_OUT_OF_RANGE;
  449. *indexp += (unsigned int)(1 + length);
  450. } while(length);
  451. return DOH_OK;
  452. }
  453. static unsigned short doh_get16bit(const unsigned char *doh,
  454. unsigned int index)
  455. {
  456. return (unsigned short)((doh[index] << 8) | doh[index + 1]);
  457. }
  458. static unsigned int doh_get32bit(const unsigned char *doh, unsigned int index)
  459. {
  460. /* make clang and gcc optimize this to bswap by incrementing
  461. the pointer first. */
  462. doh += index;
  463. /* avoid undefined behavior by casting to unsigned before shifting
  464. 24 bits, possibly into the sign bit. codegen is same, but
  465. ub sanitizer will not be upset */
  466. return ((unsigned)doh[0] << 24) | ((unsigned)doh[1] << 16) |
  467. ((unsigned)doh[2] << 8) | doh[3];
  468. }
  469. static void doh_store_a(const unsigned char *doh, int index,
  470. struct dohentry *d)
  471. {
  472. /* silently ignore addresses over the limit */
  473. if(d->numaddr < DOH_MAX_ADDR) {
  474. struct dohaddr *a = &d->addr[d->numaddr];
  475. a->type = DNS_TYPE_A;
  476. memcpy(&a->ip.v4, &doh[index], 4);
  477. d->numaddr++;
  478. }
  479. }
  480. static void doh_store_aaaa(const unsigned char *doh, int index,
  481. struct dohentry *d)
  482. {
  483. /* silently ignore addresses over the limit */
  484. if(d->numaddr < DOH_MAX_ADDR) {
  485. struct dohaddr *a = &d->addr[d->numaddr];
  486. a->type = DNS_TYPE_AAAA;
  487. memcpy(&a->ip.v6, &doh[index], 16);
  488. d->numaddr++;
  489. }
  490. }
  491. #ifdef USE_HTTPSRR
  492. static DOHcode doh_store_https(const unsigned char *doh, int index,
  493. struct dohentry *d, uint16_t len)
  494. {
  495. /* silently ignore RRs over the limit */
  496. if(d->numhttps_rrs < DOH_MAX_HTTPS) {
  497. struct dohhttps_rr *h = &d->https_rrs[d->numhttps_rrs];
  498. h->val = Curl_memdup(&doh[index], len);
  499. if(!h->val)
  500. return DOH_OUT_OF_MEM;
  501. h->len = len;
  502. d->numhttps_rrs++;
  503. }
  504. return DOH_OK;
  505. }
  506. #endif
  507. static DOHcode doh_store_cname(const unsigned char *doh, size_t dohlen,
  508. unsigned int index, struct dohentry *d)
  509. {
  510. struct dynbuf *c;
  511. unsigned int loop = 128; /* a valid DNS name can never loop this much */
  512. unsigned char length;
  513. if(d->numcname == DOH_MAX_CNAME)
  514. return DOH_OK; /* skip! */
  515. c = &d->cname[d->numcname++];
  516. do {
  517. if(index >= dohlen)
  518. return DOH_DNS_OUT_OF_RANGE;
  519. length = doh[index];
  520. if((length & 0xc0) == 0xc0) {
  521. int newpos;
  522. /* name pointer, get the new offset (14 bits) */
  523. if((index + 1) >= dohlen)
  524. return DOH_DNS_OUT_OF_RANGE;
  525. /* move to the new index */
  526. newpos = (length & 0x3f) << 8 | doh[index + 1];
  527. index = (unsigned int)newpos;
  528. continue;
  529. }
  530. else if(length & 0xc0)
  531. return DOH_DNS_BAD_LABEL; /* bad input */
  532. else
  533. index++;
  534. if(length) {
  535. if(Curl_dyn_len(c)) {
  536. if(Curl_dyn_addn(c, STRCONST(".")))
  537. return DOH_OUT_OF_MEM;
  538. }
  539. if((index + length) > dohlen)
  540. return DOH_DNS_BAD_LABEL;
  541. if(Curl_dyn_addn(c, &doh[index], length))
  542. return DOH_OUT_OF_MEM;
  543. index += length;
  544. }
  545. } while(length && --loop);
  546. if(!loop)
  547. return DOH_DNS_LABEL_LOOP;
  548. return DOH_OK;
  549. }
  550. static DOHcode doh_rdata(const unsigned char *doh,
  551. size_t dohlen,
  552. unsigned short rdlength,
  553. unsigned short type,
  554. int index,
  555. struct dohentry *d)
  556. {
  557. /* RDATA
  558. - A (TYPE 1): 4 bytes
  559. - AAAA (TYPE 28): 16 bytes
  560. - NS (TYPE 2): N bytes
  561. - HTTPS (TYPE 65): N bytes */
  562. DOHcode rc;
  563. switch(type) {
  564. case DNS_TYPE_A:
  565. if(rdlength != 4)
  566. return DOH_DNS_RDATA_LEN;
  567. doh_store_a(doh, index, d);
  568. break;
  569. case DNS_TYPE_AAAA:
  570. if(rdlength != 16)
  571. return DOH_DNS_RDATA_LEN;
  572. doh_store_aaaa(doh, index, d);
  573. break;
  574. #ifdef USE_HTTPSRR
  575. case DNS_TYPE_HTTPS:
  576. rc = doh_store_https(doh, index, d, rdlength);
  577. if(rc)
  578. return rc;
  579. break;
  580. #endif
  581. case DNS_TYPE_CNAME:
  582. rc = doh_store_cname(doh, dohlen, (unsigned int)index, d);
  583. if(rc)
  584. return rc;
  585. break;
  586. case DNS_TYPE_DNAME:
  587. /* explicit for clarity; just skip; rely on synthesized CNAME */
  588. break;
  589. default:
  590. /* unsupported type, just skip it */
  591. break;
  592. }
  593. return DOH_OK;
  594. }
  595. UNITTEST void de_init(struct dohentry *de)
  596. {
  597. int i;
  598. memset(de, 0, sizeof(*de));
  599. de->ttl = INT_MAX;
  600. for(i = 0; i < DOH_MAX_CNAME; i++)
  601. Curl_dyn_init(&de->cname[i], DYN_DOH_CNAME);
  602. }
  603. UNITTEST DOHcode doh_resp_decode(const unsigned char *doh,
  604. size_t dohlen,
  605. DNStype dnstype,
  606. struct dohentry *d)
  607. {
  608. unsigned char rcode;
  609. unsigned short qdcount;
  610. unsigned short ancount;
  611. unsigned short type = 0;
  612. unsigned short rdlength;
  613. unsigned short nscount;
  614. unsigned short arcount;
  615. unsigned int index = 12;
  616. DOHcode rc;
  617. if(dohlen < 12)
  618. return DOH_TOO_SMALL_BUFFER; /* too small */
  619. if(!doh || doh[0] || doh[1])
  620. return DOH_DNS_BAD_ID; /* bad ID */
  621. rcode = doh[3] & 0x0f;
  622. if(rcode)
  623. return DOH_DNS_BAD_RCODE; /* bad rcode */
  624. qdcount = doh_get16bit(doh, 4);
  625. while(qdcount) {
  626. rc = doh_skipqname(doh, dohlen, &index);
  627. if(rc)
  628. return rc; /* bad qname */
  629. if(dohlen < (index + 4))
  630. return DOH_DNS_OUT_OF_RANGE;
  631. index += 4; /* skip question's type and class */
  632. qdcount--;
  633. }
  634. ancount = doh_get16bit(doh, 6);
  635. while(ancount) {
  636. unsigned short class;
  637. unsigned int ttl;
  638. rc = doh_skipqname(doh, dohlen, &index);
  639. if(rc)
  640. return rc; /* bad qname */
  641. if(dohlen < (index + 2))
  642. return DOH_DNS_OUT_OF_RANGE;
  643. type = doh_get16bit(doh, index);
  644. if((type != DNS_TYPE_CNAME) /* may be synthesized from DNAME */
  645. && (type != DNS_TYPE_DNAME) /* if present, accept and ignore */
  646. && (type != dnstype))
  647. /* Not the same type as was asked for nor CNAME nor DNAME */
  648. return DOH_DNS_UNEXPECTED_TYPE;
  649. index += 2;
  650. if(dohlen < (index + 2))
  651. return DOH_DNS_OUT_OF_RANGE;
  652. class = doh_get16bit(doh, index);
  653. if(DNS_CLASS_IN != class)
  654. return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
  655. index += 2;
  656. if(dohlen < (index + 4))
  657. return DOH_DNS_OUT_OF_RANGE;
  658. ttl = doh_get32bit(doh, index);
  659. if(ttl < d->ttl)
  660. d->ttl = ttl;
  661. index += 4;
  662. if(dohlen < (index + 2))
  663. return DOH_DNS_OUT_OF_RANGE;
  664. rdlength = doh_get16bit(doh, index);
  665. index += 2;
  666. if(dohlen < (index + rdlength))
  667. return DOH_DNS_OUT_OF_RANGE;
  668. rc = doh_rdata(doh, dohlen, rdlength, type, (int)index, d);
  669. if(rc)
  670. return rc; /* bad doh_rdata */
  671. index += rdlength;
  672. ancount--;
  673. }
  674. nscount = doh_get16bit(doh, 8);
  675. while(nscount) {
  676. rc = doh_skipqname(doh, dohlen, &index);
  677. if(rc)
  678. return rc; /* bad qname */
  679. if(dohlen < (index + 8))
  680. return DOH_DNS_OUT_OF_RANGE;
  681. index += 2 + 2 + 4; /* type, class and ttl */
  682. if(dohlen < (index + 2))
  683. return DOH_DNS_OUT_OF_RANGE;
  684. rdlength = doh_get16bit(doh, index);
  685. index += 2;
  686. if(dohlen < (index + rdlength))
  687. return DOH_DNS_OUT_OF_RANGE;
  688. index += rdlength;
  689. nscount--;
  690. }
  691. arcount = doh_get16bit(doh, 10);
  692. while(arcount) {
  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. arcount--;
  707. }
  708. if(index != dohlen)
  709. return DOH_DNS_MALFORMAT; /* something is wrong */
  710. #ifdef USE_HTTTPS
  711. if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr && !d->numhttps_rrs)
  712. #else
  713. if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr)
  714. #endif
  715. /* nothing stored! */
  716. return DOH_NO_CONTENT;
  717. return DOH_OK; /* ok */
  718. }
  719. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  720. static void doh_show(struct Curl_easy *data,
  721. const struct dohentry *d)
  722. {
  723. int i;
  724. infof(data, "[DoH] TTL: %u seconds", d->ttl);
  725. for(i = 0; i < d->numaddr; i++) {
  726. const struct dohaddr *a = &d->addr[i];
  727. if(a->type == DNS_TYPE_A) {
  728. infof(data, "[DoH] A: %u.%u.%u.%u",
  729. a->ip.v4[0], a->ip.v4[1],
  730. a->ip.v4[2], a->ip.v4[3]);
  731. }
  732. else if(a->type == DNS_TYPE_AAAA) {
  733. int j;
  734. char buffer[128] = "[DoH] AAAA: ";
  735. size_t len = strlen(buffer);
  736. char *ptr = &buffer[len];
  737. len = sizeof(buffer) - len;
  738. for(j = 0; j < 16; j += 2) {
  739. size_t l;
  740. msnprintf(ptr, len, "%s%02x%02x", j ? ":" : "", d->addr[i].ip.v6[j],
  741. d->addr[i].ip.v6[j + 1]);
  742. l = strlen(ptr);
  743. len -= l;
  744. ptr += l;
  745. }
  746. infof(data, "%s", buffer);
  747. }
  748. }
  749. #ifdef USE_HTTPSRR
  750. for(i = 0; i < d->numhttps_rrs; i++) {
  751. # ifdef DEBUGBUILD
  752. doh_print_buf(data, "DoH HTTPS",
  753. d->https_rrs[i].val, d->https_rrs[i].len);
  754. # else
  755. infof(data, "DoH HTTPS RR: length %d", d->https_rrs[i].len);
  756. # endif
  757. }
  758. #endif
  759. for(i = 0; i < d->numcname; i++) {
  760. infof(data, "CNAME: %s", Curl_dyn_ptr(&d->cname[i]));
  761. }
  762. }
  763. #else
  764. #define doh_show(x,y)
  765. #endif
  766. /*
  767. * doh2ai()
  768. *
  769. * This function returns a pointer to the first element of a newly allocated
  770. * Curl_addrinfo struct linked list filled with the data from a set of DoH
  771. * lookups. Curl_addrinfo is meant to work like the addrinfo struct does for
  772. * a IPv6 stack, but usable also for IPv4, all hosts and environments.
  773. *
  774. * The memory allocated by this function *MUST* be free'd later on calling
  775. * Curl_freeaddrinfo(). For each successful call to this function there
  776. * must be an associated call later to Curl_freeaddrinfo().
  777. */
  778. static CURLcode doh2ai(const struct dohentry *de, const char *hostname,
  779. int port, struct Curl_addrinfo **aip)
  780. {
  781. struct Curl_addrinfo *ai;
  782. struct Curl_addrinfo *prevai = NULL;
  783. struct Curl_addrinfo *firstai = NULL;
  784. struct sockaddr_in *addr;
  785. #ifdef USE_IPV6
  786. struct sockaddr_in6 *addr6;
  787. #endif
  788. CURLcode result = CURLE_OK;
  789. int i;
  790. size_t hostlen = strlen(hostname) + 1; /* include null-terminator */
  791. DEBUGASSERT(de);
  792. if(!de->numaddr)
  793. return CURLE_COULDNT_RESOLVE_HOST;
  794. for(i = 0; i < de->numaddr; i++) {
  795. size_t ss_size;
  796. CURL_SA_FAMILY_T addrtype;
  797. if(de->addr[i].type == DNS_TYPE_AAAA) {
  798. #ifndef USE_IPV6
  799. /* we cannot handle IPv6 addresses */
  800. continue;
  801. #else
  802. ss_size = sizeof(struct sockaddr_in6);
  803. addrtype = AF_INET6;
  804. #endif
  805. }
  806. else {
  807. ss_size = sizeof(struct sockaddr_in);
  808. addrtype = AF_INET;
  809. }
  810. ai = calloc(1, sizeof(struct Curl_addrinfo) + ss_size + hostlen);
  811. if(!ai) {
  812. result = CURLE_OUT_OF_MEMORY;
  813. break;
  814. }
  815. ai->ai_addr = (void *)((char *)ai + sizeof(struct Curl_addrinfo));
  816. ai->ai_canonname = (void *)((char *)ai->ai_addr + ss_size);
  817. memcpy(ai->ai_canonname, hostname, hostlen);
  818. if(!firstai)
  819. /* store the pointer we want to return from this function */
  820. firstai = ai;
  821. if(prevai)
  822. /* make the previous entry point to this */
  823. prevai->ai_next = ai;
  824. ai->ai_family = addrtype;
  825. /* we return all names as STREAM, so when using this address for TFTP
  826. the type must be ignored and conn->socktype be used instead! */
  827. ai->ai_socktype = SOCK_STREAM;
  828. ai->ai_addrlen = (curl_socklen_t)ss_size;
  829. /* leave the rest of the struct filled with zero */
  830. switch(ai->ai_family) {
  831. case AF_INET:
  832. addr = (void *)ai->ai_addr; /* storage area for this info */
  833. DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
  834. memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
  835. addr->sin_family = (CURL_SA_FAMILY_T)addrtype;
  836. addr->sin_port = htons((unsigned short)port);
  837. break;
  838. #ifdef USE_IPV6
  839. case AF_INET6:
  840. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  841. DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
  842. memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
  843. addr6->sin6_family = (CURL_SA_FAMILY_T)addrtype;
  844. addr6->sin6_port = htons((unsigned short)port);
  845. break;
  846. #endif
  847. }
  848. prevai = ai;
  849. }
  850. if(result) {
  851. Curl_freeaddrinfo(firstai);
  852. firstai = NULL;
  853. }
  854. *aip = firstai;
  855. return result;
  856. }
  857. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  858. static const char *doh_type2name(DNStype dnstype)
  859. {
  860. switch(dnstype) {
  861. case DNS_TYPE_A:
  862. return "A";
  863. case DNS_TYPE_AAAA:
  864. return "AAAA";
  865. #ifdef USE_HTTPSRR
  866. case DNS_TYPE_HTTPS:
  867. return "HTTPS";
  868. #endif
  869. default:
  870. return "unknown";
  871. }
  872. }
  873. #endif
  874. UNITTEST void de_cleanup(struct dohentry *d)
  875. {
  876. int i = 0;
  877. for(i = 0; i < d->numcname; i++) {
  878. Curl_dyn_free(&d->cname[i]);
  879. }
  880. #ifdef USE_HTTPSRR
  881. for(i = 0; i < d->numhttps_rrs; i++)
  882. Curl_safefree(d->https_rrs[i].val);
  883. #endif
  884. }
  885. #ifdef USE_HTTPSRR
  886. /*
  887. * @brief decode the DNS name in a binary RRData
  888. * @param buf points to the buffer (in/out)
  889. * @param remaining points to the remaining buffer length (in/out)
  890. * @param dnsname returns the string form name on success
  891. * @return is 1 for success, error otherwise
  892. *
  893. * The encoding here is defined in
  894. * https://tools.ietf.org/html/rfc1035#section-3.1
  895. *
  896. * The input buffer pointer will be modified so it points to
  897. * just after the end of the DNS name encoding on output. (And
  898. * that is why it is an "unsigned char **" :-)
  899. */
  900. static CURLcode doh_decode_rdata_name(unsigned char **buf, size_t *remaining,
  901. char **dnsname)
  902. {
  903. unsigned char *cp = NULL;
  904. int rem = 0;
  905. unsigned char clen = 0; /* chunk len */
  906. struct dynbuf thename;
  907. DEBUGASSERT(buf && remaining && dnsname);
  908. if(!buf || !remaining || !dnsname)
  909. return CURLE_OUT_OF_MEMORY;
  910. rem = (int)*remaining;
  911. if(rem <= 0) {
  912. Curl_dyn_free(&thename);
  913. return CURLE_OUT_OF_MEMORY;
  914. }
  915. Curl_dyn_init(&thename, CURL_MAXLEN_host_name);
  916. cp = *buf;
  917. clen = *cp++;
  918. if(clen == 0) {
  919. /* special case - return "." as name */
  920. if(Curl_dyn_addn(&thename, ".", 1))
  921. return CURLE_OUT_OF_MEMORY;
  922. }
  923. while(clen) {
  924. if(clen >= rem) {
  925. Curl_dyn_free(&thename);
  926. return CURLE_OUT_OF_MEMORY;
  927. }
  928. if(Curl_dyn_addn(&thename, cp, clen) ||
  929. Curl_dyn_addn(&thename, ".", 1))
  930. return CURLE_TOO_LARGE;
  931. cp += clen;
  932. rem -= (clen + 1);
  933. if(rem <= 0) {
  934. Curl_dyn_free(&thename);
  935. return CURLE_OUT_OF_MEMORY;
  936. }
  937. clen = *cp++;
  938. }
  939. *buf = cp;
  940. *remaining = rem - 1;
  941. *dnsname = Curl_dyn_ptr(&thename);
  942. return CURLE_OK;
  943. }
  944. #ifdef DEBUGBUILD
  945. static CURLcode doh_test_alpn_escapes(void)
  946. {
  947. /* we will use an example from draft-ietf-dnsop-svcb, figure 10 */
  948. static unsigned char example[] = {
  949. 0x08, /* length 8 */
  950. 0x66, 0x5c, 0x6f, 0x6f, 0x2c, 0x62, 0x61, 0x72, /* value "f\\oo,bar" */
  951. 0x02, /* length 2 */
  952. 0x68, 0x32 /* value "h2" */
  953. };
  954. size_t example_len = sizeof(example);
  955. unsigned char aval[MAX_HTTPSRR_ALPNS] = { 0 };
  956. static const char expected[2] = { ALPN_h2, ALPN_none };
  957. if(Curl_httpsrr_decode_alpn(example, example_len, aval) != CURLE_OK)
  958. return CURLE_BAD_CONTENT_ENCODING;
  959. if(memcmp(aval, expected, sizeof(expected)))
  960. return CURLE_BAD_CONTENT_ENCODING;
  961. return CURLE_OK;
  962. }
  963. #endif
  964. static CURLcode doh_resp_decode_httpsrr(unsigned char *cp, size_t len,
  965. struct Curl_https_rrinfo **hrr)
  966. {
  967. uint16_t pcode = 0, plen = 0;
  968. struct Curl_https_rrinfo *lhrr = NULL;
  969. char *dnsname = NULL;
  970. #ifdef DEBUGBUILD
  971. /* a few tests of escaping, should not be here but ok for now */
  972. if(doh_test_alpn_escapes() != CURLE_OK)
  973. return CURLE_OUT_OF_MEMORY;
  974. #endif
  975. if(len <= 2)
  976. return CURLE_BAD_FUNCTION_ARGUMENT;
  977. lhrr = calloc(1, sizeof(struct Curl_https_rrinfo));
  978. if(!lhrr)
  979. return CURLE_OUT_OF_MEMORY;
  980. lhrr->priority = doh_get16bit(cp, 0);
  981. cp += 2;
  982. len -= 2;
  983. if(doh_decode_rdata_name(&cp, &len, &dnsname) != CURLE_OK)
  984. goto err;
  985. lhrr->target = dnsname;
  986. lhrr->port = -1; /* until set */
  987. while(len >= 4) {
  988. pcode = doh_get16bit(cp, 0);
  989. plen = doh_get16bit(cp, 2);
  990. cp += 4;
  991. len -= 4;
  992. switch(pcode) {
  993. case HTTPS_RR_CODE_ALPN:
  994. if(Curl_httpsrr_decode_alpn(cp, plen, lhrr->alpns) != CURLE_OK)
  995. goto err;
  996. break;
  997. case HTTPS_RR_CODE_NO_DEF_ALPN:
  998. lhrr->no_def_alpn = TRUE;
  999. break;
  1000. case HTTPS_RR_CODE_IPV4:
  1001. if(!plen)
  1002. goto err;
  1003. lhrr->ipv4hints = Curl_memdup(cp, plen);
  1004. if(!lhrr->ipv4hints)
  1005. goto err;
  1006. lhrr->ipv4hints_len = (size_t)plen;
  1007. break;
  1008. case HTTPS_RR_CODE_ECH:
  1009. if(!plen)
  1010. goto err;
  1011. lhrr->echconfiglist = Curl_memdup(cp, plen);
  1012. if(!lhrr->echconfiglist)
  1013. goto err;
  1014. lhrr->echconfiglist_len = (size_t)plen;
  1015. break;
  1016. case HTTPS_RR_CODE_IPV6:
  1017. if(!plen)
  1018. goto err;
  1019. lhrr->ipv6hints = Curl_memdup(cp, plen);
  1020. if(!lhrr->ipv6hints)
  1021. goto err;
  1022. lhrr->ipv6hints_len = (size_t)plen;
  1023. break;
  1024. case HTTPS_RR_CODE_PORT:
  1025. lhrr->port = doh_get16bit(cp, 0);
  1026. break;
  1027. default:
  1028. break;
  1029. }
  1030. if(plen > 0 && plen <= len) {
  1031. cp += plen;
  1032. len -= plen;
  1033. }
  1034. }
  1035. DEBUGASSERT(!len);
  1036. *hrr = lhrr;
  1037. return CURLE_OK;
  1038. err:
  1039. Curl_safefree(lhrr->target);
  1040. Curl_safefree(lhrr->echconfiglist);
  1041. Curl_safefree(lhrr);
  1042. return CURLE_OUT_OF_MEMORY;
  1043. }
  1044. # ifdef DEBUGBUILD
  1045. static void doh_print_httpsrr(struct Curl_easy *data,
  1046. struct Curl_https_rrinfo *hrr)
  1047. {
  1048. DEBUGASSERT(hrr);
  1049. infof(data, "HTTPS RR: priority %d, target: %s",
  1050. hrr->priority, hrr->target);
  1051. if(hrr->alpns[0] != ALPN_none)
  1052. infof(data, "HTTPS RR: alpns %u %u %u %u",
  1053. hrr->alpns[0], hrr->alpns[1], hrr->alpns[2], hrr->alpns[3]);
  1054. else
  1055. infof(data, "HTTPS RR: no alpns");
  1056. if(hrr->no_def_alpn)
  1057. infof(data, "HTTPS RR: no_def_alpn set");
  1058. else
  1059. infof(data, "HTTPS RR: no_def_alpn not set");
  1060. if(hrr->ipv4hints) {
  1061. doh_print_buf(data, "HTTPS RR: ipv4hints",
  1062. hrr->ipv4hints, hrr->ipv4hints_len);
  1063. }
  1064. else
  1065. infof(data, "HTTPS RR: no ipv4hints");
  1066. if(hrr->echconfiglist) {
  1067. doh_print_buf(data, "HTTPS RR: ECHConfigList",
  1068. hrr->echconfiglist, hrr->echconfiglist_len);
  1069. }
  1070. else
  1071. infof(data, "HTTPS RR: no ECHConfigList");
  1072. if(hrr->ipv6hints) {
  1073. doh_print_buf(data, "HTTPS RR: ipv6hint",
  1074. hrr->ipv6hints, hrr->ipv6hints_len);
  1075. }
  1076. else
  1077. infof(data, "HTTPS RR: no ipv6hints");
  1078. return;
  1079. }
  1080. # endif
  1081. #endif
  1082. CURLcode Curl_doh_is_resolved(struct Curl_easy *data,
  1083. struct Curl_dns_entry **dnsp)
  1084. {
  1085. CURLcode result;
  1086. struct doh_probes *dohp = data->req.doh;
  1087. *dnsp = NULL; /* defaults to no response */
  1088. if(!dohp)
  1089. return CURLE_OUT_OF_MEMORY;
  1090. if(dohp->probe[DOH_SLOT_IPV4].easy_mid < 0 &&
  1091. dohp->probe[DOH_SLOT_IPV6].easy_mid < 0) {
  1092. failf(data, "Could not DoH-resolve: %s", data->state.async.hostname);
  1093. return CONN_IS_PROXIED(data->conn) ? CURLE_COULDNT_RESOLVE_PROXY :
  1094. CURLE_COULDNT_RESOLVE_HOST;
  1095. }
  1096. else if(!dohp->pending) {
  1097. DOHcode rc[DOH_SLOT_COUNT];
  1098. struct dohentry de;
  1099. int slot;
  1100. memset(rc, 0, sizeof(rc));
  1101. /* remove DoH handles from multi handle and close them */
  1102. Curl_doh_close(data);
  1103. /* parse the responses, create the struct and return it! */
  1104. de_init(&de);
  1105. for(slot = 0; slot < DOH_SLOT_COUNT; slot++) {
  1106. struct doh_probe *p = &dohp->probe[slot];
  1107. if(!p->dnstype)
  1108. continue;
  1109. rc[slot] = doh_resp_decode(Curl_dyn_uptr(&p->resp_body),
  1110. Curl_dyn_len(&p->resp_body),
  1111. p->dnstype, &de);
  1112. Curl_dyn_free(&p->resp_body);
  1113. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  1114. if(rc[slot]) {
  1115. infof(data, "DoH: %s type %s for %s", doh_strerror(rc[slot]),
  1116. doh_type2name(p->dnstype), dohp->host);
  1117. }
  1118. #endif
  1119. } /* next slot */
  1120. result = CURLE_COULDNT_RESOLVE_HOST; /* until we know better */
  1121. if(!rc[DOH_SLOT_IPV4] || !rc[DOH_SLOT_IPV6]) {
  1122. /* we have an address, of one kind or other */
  1123. struct Curl_dns_entry *dns;
  1124. struct Curl_addrinfo *ai;
  1125. if(Curl_trc_ft_is_verbose(data, &Curl_doh_trc)) {
  1126. infof(data, "[DoH] hostname: %s", dohp->host);
  1127. doh_show(data, &de);
  1128. }
  1129. result = doh2ai(&de, dohp->host, dohp->port, &ai);
  1130. if(result) {
  1131. de_cleanup(&de);
  1132. return result;
  1133. }
  1134. if(data->share)
  1135. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  1136. /* we got a response, store it in the cache */
  1137. dns = Curl_cache_addr(data, ai, dohp->host, 0, dohp->port, FALSE);
  1138. if(data->share)
  1139. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  1140. if(!dns) {
  1141. /* returned failure, bail out nicely */
  1142. Curl_freeaddrinfo(ai);
  1143. }
  1144. else {
  1145. data->state.async.dns = dns;
  1146. *dnsp = dns;
  1147. result = CURLE_OK; /* address resolution OK */
  1148. }
  1149. } /* address processing done */
  1150. /* Now process any build-specific attributes retrieved from DNS */
  1151. #ifdef USE_HTTPSRR
  1152. if(de.numhttps_rrs > 0 && result == CURLE_OK && *dnsp) {
  1153. struct Curl_https_rrinfo *hrr = NULL;
  1154. result = doh_resp_decode_httpsrr(de.https_rrs->val, de.https_rrs->len,
  1155. &hrr);
  1156. if(result) {
  1157. infof(data, "Failed to decode HTTPS RR");
  1158. return result;
  1159. }
  1160. infof(data, "Some HTTPS RR to process");
  1161. # ifdef DEBUGBUILD
  1162. doh_print_httpsrr(data, hrr);
  1163. # endif
  1164. (*dnsp)->hinfo = hrr;
  1165. }
  1166. #endif
  1167. /* All done */
  1168. de_cleanup(&de);
  1169. Curl_doh_cleanup(data);
  1170. return result;
  1171. } /* !dohp->pending */
  1172. /* else wait for pending DoH transactions to complete */
  1173. return CURLE_OK;
  1174. }
  1175. void Curl_doh_close(struct Curl_easy *data)
  1176. {
  1177. struct doh_probes *doh = data->req.doh;
  1178. if(doh && data->multi) {
  1179. struct Curl_easy *probe_data;
  1180. curl_off_t mid;
  1181. size_t slot;
  1182. for(slot = 0; slot < DOH_SLOT_COUNT; slot++) {
  1183. mid = doh->probe[slot].easy_mid;
  1184. if(mid < 0)
  1185. continue;
  1186. doh->probe[slot].easy_mid = -1;
  1187. /* should have been called before data is removed from multi handle */
  1188. DEBUGASSERT(data->multi);
  1189. probe_data = data->multi ? Curl_multi_get_handle(data->multi, mid) :
  1190. NULL;
  1191. if(!probe_data) {
  1192. DEBUGF(infof(data, "Curl_doh_close: xfer for mid=%"
  1193. FMT_OFF_T " not found!",
  1194. doh->probe[slot].easy_mid));
  1195. continue;
  1196. }
  1197. /* data->multi might already be reset at this time */
  1198. curl_multi_remove_handle(data->multi, probe_data);
  1199. Curl_close(&probe_data);
  1200. }
  1201. }
  1202. }
  1203. void Curl_doh_cleanup(struct Curl_easy *data)
  1204. {
  1205. struct doh_probes *doh = data->req.doh;
  1206. if(doh) {
  1207. Curl_doh_close(data);
  1208. curl_slist_free_all(doh->req_hds);
  1209. data->req.doh->req_hds = NULL;
  1210. Curl_safefree(data->req.doh);
  1211. }
  1212. }
  1213. #endif /* CURL_DISABLE_DOH */