doh.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2018 - 2019, 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.haxx.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. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #include "urldata.h"
  24. #include "curl_addrinfo.h"
  25. #include "doh.h"
  26. #include "sendf.h"
  27. #include "multiif.h"
  28. #include "url.h"
  29. #include "share.h"
  30. #include "curl_base64.h"
  31. #include "connect.h"
  32. #include "strdup.h"
  33. /* The last 3 #include files should be in this order */
  34. #include "curl_printf.h"
  35. #include "curl_memory.h"
  36. #include "memdebug.h"
  37. #define DNS_CLASS_IN 0x01
  38. #define DOH_MAX_RESPONSE_SIZE 3000 /* bytes */
  39. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  40. static const char * const errors[]={
  41. "",
  42. "Bad label",
  43. "Out of range",
  44. "Label loop",
  45. "Too small",
  46. "Out of memory",
  47. "RDATA length",
  48. "Malformat",
  49. "Bad RCODE",
  50. "Unexpected TYPE",
  51. "Unexpected CLASS",
  52. "No content",
  53. "Bad ID"
  54. };
  55. static const char *doh_strerror(DOHcode code)
  56. {
  57. if((code >= DOH_OK) && (code <= DOH_DNS_BAD_ID))
  58. return errors[code];
  59. return "bad error code";
  60. }
  61. #endif
  62. #ifdef DEBUGBUILD
  63. #define UNITTEST
  64. #else
  65. #define UNITTEST static
  66. #endif
  67. UNITTEST DOHcode doh_encode(const char *host,
  68. DNStype dnstype,
  69. unsigned char *dnsp, /* buffer */
  70. size_t len, /* buffer size */
  71. size_t *olen) /* output length */
  72. {
  73. size_t hostlen = strlen(host);
  74. unsigned char *orig = dnsp;
  75. const char *hostp = host;
  76. if(len < (12 + hostlen + 4))
  77. return DOH_TOO_SMALL_BUFFER;
  78. *dnsp++ = 0; /* 16 bit id */
  79. *dnsp++ = 0;
  80. *dnsp++ = 0x01; /* |QR| Opcode |AA|TC|RD| Set the RD bit */
  81. *dnsp++ = '\0'; /* |RA| Z | RCODE | */
  82. *dnsp++ = '\0';
  83. *dnsp++ = 1; /* QDCOUNT (number of entries in the question section) */
  84. *dnsp++ = '\0';
  85. *dnsp++ = '\0'; /* ANCOUNT */
  86. *dnsp++ = '\0';
  87. *dnsp++ = '\0'; /* NSCOUNT */
  88. *dnsp++ = '\0';
  89. *dnsp++ = '\0'; /* ARCOUNT */
  90. /* store a QNAME */
  91. do {
  92. char *dot = strchr(hostp, '.');
  93. size_t labellen;
  94. bool found = false;
  95. if(dot) {
  96. found = true;
  97. labellen = dot - hostp;
  98. }
  99. else
  100. labellen = strlen(hostp);
  101. if(labellen > 63) {
  102. /* too long label, error out */
  103. *olen = 0;
  104. return DOH_DNS_BAD_LABEL;
  105. }
  106. *dnsp++ = (unsigned char)labellen;
  107. memcpy(dnsp, hostp, labellen);
  108. dnsp += labellen;
  109. hostp += labellen + 1;
  110. if(!found) {
  111. *dnsp++ = 0; /* terminating zero */
  112. break;
  113. }
  114. } while(1);
  115. *dnsp++ = '\0'; /* upper 8 bit TYPE */
  116. *dnsp++ = (unsigned char)dnstype;
  117. *dnsp++ = '\0'; /* upper 8 bit CLASS */
  118. *dnsp++ = DNS_CLASS_IN; /* IN - "the Internet" */
  119. *olen = dnsp - orig;
  120. return DOH_OK;
  121. }
  122. static size_t
  123. doh_write_cb(void *contents, size_t size, size_t nmemb, void *userp)
  124. {
  125. size_t realsize = size * nmemb;
  126. struct dohresponse *mem = (struct dohresponse *)userp;
  127. if((mem->size + realsize) > DOH_MAX_RESPONSE_SIZE)
  128. /* suspiciously much for us */
  129. return 0;
  130. mem->memory = Curl_saferealloc(mem->memory, mem->size + realsize);
  131. if(!mem->memory)
  132. /* out of memory! */
  133. return 0;
  134. memcpy(&(mem->memory[mem->size]), contents, realsize);
  135. mem->size += realsize;
  136. return realsize;
  137. }
  138. /* called from multi.c when this DOH transfer is complete */
  139. static int Curl_doh_done(struct Curl_easy *doh, CURLcode result)
  140. {
  141. struct Curl_easy *data = doh->set.dohfor;
  142. /* so one of the DOH request done for the 'data' transfer is now complete! */
  143. data->req.doh.pending--;
  144. infof(data, "a DOH request is completed, %u to go\n", data->req.doh.pending);
  145. if(result)
  146. infof(data, "DOH request %s\n", curl_easy_strerror(result));
  147. if(!data->req.doh.pending) {
  148. /* DOH completed */
  149. curl_slist_free_all(data->req.doh.headers);
  150. data->req.doh.headers = NULL;
  151. Curl_expire(data, 0, EXPIRE_RUN_NOW);
  152. }
  153. return 0;
  154. }
  155. #define ERROR_CHECK_SETOPT(x,y) \
  156. do { \
  157. result = curl_easy_setopt(doh, x, y); \
  158. if(result) \
  159. goto error; \
  160. } WHILE_FALSE
  161. static CURLcode dohprobe(struct Curl_easy *data,
  162. struct dnsprobe *p, DNStype dnstype,
  163. const char *host,
  164. const char *url, CURLM *multi,
  165. struct curl_slist *headers)
  166. {
  167. struct Curl_easy *doh = NULL;
  168. char *nurl = NULL;
  169. CURLcode result = CURLE_OK;
  170. timediff_t timeout_ms;
  171. DOHcode d = doh_encode(host, dnstype, p->dohbuffer, sizeof(p->dohbuffer),
  172. &p->dohlen);
  173. if(d) {
  174. failf(data, "Failed to encode DOH packet [%d]\n", d);
  175. return CURLE_OUT_OF_MEMORY;
  176. }
  177. p->dnstype = dnstype;
  178. p->serverdoh.memory = NULL;
  179. /* the memory will be grown as needed by realloc in the doh_write_cb
  180. function */
  181. p->serverdoh.size = 0;
  182. /* Note: this is code for sending the DoH request with GET but there's still
  183. no logic that actually enables this. We should either add that ability or
  184. yank out the GET code. Discuss! */
  185. if(data->set.doh_get) {
  186. char *b64;
  187. size_t b64len;
  188. result = Curl_base64url_encode(data, (char *)p->dohbuffer, p->dohlen,
  189. &b64, &b64len);
  190. if(result)
  191. goto error;
  192. nurl = aprintf("%s?dns=%s", url, b64);
  193. free(b64);
  194. if(!nurl) {
  195. result = CURLE_OUT_OF_MEMORY;
  196. goto error;
  197. }
  198. url = nurl;
  199. }
  200. timeout_ms = Curl_timeleft(data, NULL, TRUE);
  201. /* Curl_open() is the internal version of curl_easy_init() */
  202. result = Curl_open(&doh);
  203. if(!result) {
  204. /* pass in the struct pointer via a local variable to please coverity and
  205. the gcc typecheck helpers */
  206. struct dohresponse *resp = &p->serverdoh;
  207. ERROR_CHECK_SETOPT(CURLOPT_URL, url);
  208. ERROR_CHECK_SETOPT(CURLOPT_WRITEFUNCTION, doh_write_cb);
  209. ERROR_CHECK_SETOPT(CURLOPT_WRITEDATA, resp);
  210. if(!data->set.doh_get) {
  211. ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDS, p->dohbuffer);
  212. ERROR_CHECK_SETOPT(CURLOPT_POSTFIELDSIZE, (long)p->dohlen);
  213. }
  214. ERROR_CHECK_SETOPT(CURLOPT_HTTPHEADER, headers);
  215. #ifdef USE_NGHTTP2
  216. ERROR_CHECK_SETOPT(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2TLS);
  217. #endif
  218. #ifndef CURLDEBUG
  219. /* enforce HTTPS if not debug */
  220. ERROR_CHECK_SETOPT(CURLOPT_PROTOCOLS, CURLPROTO_HTTPS);
  221. #endif
  222. ERROR_CHECK_SETOPT(CURLOPT_TIMEOUT_MS, (long)timeout_ms);
  223. if(data->set.verbose)
  224. ERROR_CHECK_SETOPT(CURLOPT_VERBOSE, 1L);
  225. if(data->set.no_signal)
  226. ERROR_CHECK_SETOPT(CURLOPT_NOSIGNAL, 1L);
  227. /* Inherit *some* SSL options from the user's transfer. This is a
  228. best-guess as to which options are needed for compatibility. #3661 */
  229. if(data->set.ssl.falsestart)
  230. ERROR_CHECK_SETOPT(CURLOPT_SSL_FALSESTART, 1L);
  231. if(data->set.ssl.primary.verifyhost)
  232. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYHOST, 2L);
  233. if(data->set.proxy_ssl.primary.verifyhost)
  234. ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_VERIFYHOST, 2L);
  235. if(data->set.ssl.primary.verifypeer)
  236. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYPEER, 1L);
  237. if(data->set.proxy_ssl.primary.verifypeer)
  238. ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_VERIFYPEER, 1L);
  239. if(data->set.ssl.primary.verifystatus)
  240. ERROR_CHECK_SETOPT(CURLOPT_SSL_VERIFYSTATUS, 1L);
  241. if(data->set.str[STRING_SSL_CAFILE_ORIG]) {
  242. ERROR_CHECK_SETOPT(CURLOPT_CAINFO,
  243. data->set.str[STRING_SSL_CAFILE_ORIG]);
  244. }
  245. if(data->set.str[STRING_SSL_CAFILE_PROXY]) {
  246. ERROR_CHECK_SETOPT(CURLOPT_PROXY_CAINFO,
  247. data->set.str[STRING_SSL_CAFILE_PROXY]);
  248. }
  249. if(data->set.str[STRING_SSL_CAPATH_ORIG]) {
  250. ERROR_CHECK_SETOPT(CURLOPT_CAPATH,
  251. data->set.str[STRING_SSL_CAPATH_ORIG]);
  252. }
  253. if(data->set.str[STRING_SSL_CAPATH_PROXY]) {
  254. ERROR_CHECK_SETOPT(CURLOPT_PROXY_CAPATH,
  255. data->set.str[STRING_SSL_CAPATH_PROXY]);
  256. }
  257. if(data->set.str[STRING_SSL_CRLFILE_ORIG]) {
  258. ERROR_CHECK_SETOPT(CURLOPT_CRLFILE,
  259. data->set.str[STRING_SSL_CRLFILE_ORIG]);
  260. }
  261. if(data->set.str[STRING_SSL_CRLFILE_PROXY]) {
  262. ERROR_CHECK_SETOPT(CURLOPT_PROXY_CRLFILE,
  263. data->set.str[STRING_SSL_CRLFILE_PROXY]);
  264. }
  265. if(data->set.ssl.certinfo)
  266. ERROR_CHECK_SETOPT(CURLOPT_CERTINFO, 1L);
  267. if(data->set.str[STRING_SSL_RANDOM_FILE]) {
  268. ERROR_CHECK_SETOPT(CURLOPT_RANDOM_FILE,
  269. data->set.str[STRING_SSL_RANDOM_FILE]);
  270. }
  271. if(data->set.str[STRING_SSL_EGDSOCKET]) {
  272. ERROR_CHECK_SETOPT(CURLOPT_EGDSOCKET,
  273. data->set.str[STRING_SSL_EGDSOCKET]);
  274. }
  275. if(data->set.ssl.no_revoke)
  276. ERROR_CHECK_SETOPT(CURLOPT_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
  277. if(data->set.proxy_ssl.no_revoke)
  278. ERROR_CHECK_SETOPT(CURLOPT_PROXY_SSL_OPTIONS, CURLSSLOPT_NO_REVOKE);
  279. if(data->set.ssl.fsslctx)
  280. ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_FUNCTION, data->set.ssl.fsslctx);
  281. if(data->set.ssl.fsslctxp)
  282. ERROR_CHECK_SETOPT(CURLOPT_SSL_CTX_DATA, data->set.ssl.fsslctxp);
  283. doh->set.fmultidone = Curl_doh_done;
  284. doh->set.dohfor = data; /* identify for which transfer this is done */
  285. p->easy = doh;
  286. /* add this transfer to the multi handle */
  287. if(curl_multi_add_handle(multi, doh))
  288. goto error;
  289. }
  290. else
  291. goto error;
  292. free(nurl);
  293. return CURLE_OK;
  294. error:
  295. free(nurl);
  296. Curl_close(doh);
  297. return result;
  298. }
  299. /*
  300. * Curl_doh() resolves a name using DOH. It resolves a name and returns a
  301. * 'Curl_addrinfo *' with the address information.
  302. */
  303. Curl_addrinfo *Curl_doh(struct connectdata *conn,
  304. const char *hostname,
  305. int port,
  306. int *waitp)
  307. {
  308. struct Curl_easy *data = conn->data;
  309. CURLcode result = CURLE_OK;
  310. *waitp = TRUE; /* this never returns synchronously */
  311. (void)conn;
  312. (void)hostname;
  313. (void)port;
  314. /* start clean, consider allocating this struct on demand */
  315. memset(&data->req.doh, 0, sizeof(struct dohdata));
  316. data->req.doh.host = hostname;
  317. data->req.doh.port = port;
  318. data->req.doh.headers =
  319. curl_slist_append(NULL,
  320. "Content-Type: application/dns-message");
  321. if(!data->req.doh.headers)
  322. goto error;
  323. if(conn->ip_version != CURL_IPRESOLVE_V6) {
  324. /* create IPv4 DOH request */
  325. result = dohprobe(data, &data->req.doh.probe[0], DNS_TYPE_A,
  326. hostname, data->set.str[STRING_DOH],
  327. data->multi, data->req.doh.headers);
  328. if(result)
  329. goto error;
  330. data->req.doh.pending++;
  331. }
  332. if(conn->ip_version != CURL_IPRESOLVE_V4) {
  333. /* create IPv6 DOH request */
  334. result = dohprobe(data, &data->req.doh.probe[1], DNS_TYPE_AAAA,
  335. hostname, data->set.str[STRING_DOH],
  336. data->multi, data->req.doh.headers);
  337. if(result)
  338. goto error;
  339. data->req.doh.pending++;
  340. }
  341. return NULL;
  342. error:
  343. curl_slist_free_all(data->req.doh.headers);
  344. data->req.doh.headers = NULL;
  345. curl_easy_cleanup(data->req.doh.probe[0].easy);
  346. data->req.doh.probe[0].easy = NULL;
  347. curl_easy_cleanup(data->req.doh.probe[1].easy);
  348. data->req.doh.probe[1].easy = NULL;
  349. return NULL;
  350. }
  351. static DOHcode skipqname(unsigned char *doh, size_t dohlen,
  352. unsigned int *indexp)
  353. {
  354. unsigned char length;
  355. do {
  356. if(dohlen < (*indexp + 1))
  357. return DOH_DNS_OUT_OF_RANGE;
  358. length = doh[*indexp];
  359. if((length & 0xc0) == 0xc0) {
  360. /* name pointer, advance over it and be done */
  361. if(dohlen < (*indexp + 2))
  362. return DOH_DNS_OUT_OF_RANGE;
  363. *indexp += 2;
  364. break;
  365. }
  366. if(length & 0xc0)
  367. return DOH_DNS_BAD_LABEL;
  368. if(dohlen < (*indexp + 1 + length))
  369. return DOH_DNS_OUT_OF_RANGE;
  370. *indexp += 1 + length;
  371. } while(length);
  372. return DOH_OK;
  373. }
  374. static unsigned short get16bit(unsigned char *doh, int index)
  375. {
  376. return (unsigned short)((doh[index] << 8) | doh[index + 1]);
  377. }
  378. static unsigned int get32bit(unsigned char *doh, int index)
  379. {
  380. return (doh[index] << 24) | (doh[index + 1] << 16) |
  381. (doh[index + 2] << 8) | doh[index + 3];
  382. }
  383. static DOHcode store_a(unsigned char *doh, int index, struct dohentry *d)
  384. {
  385. /* silently ignore addresses over the limit */
  386. if(d->numaddr < DOH_MAX_ADDR) {
  387. struct dohaddr *a = &d->addr[d->numaddr];
  388. a->type = DNS_TYPE_A;
  389. memcpy(&a->ip.v4, &doh[index], 4);
  390. d->numaddr++;
  391. }
  392. return DOH_OK;
  393. }
  394. static DOHcode store_aaaa(unsigned char *doh, int index, struct dohentry *d)
  395. {
  396. /* silently ignore addresses over the limit */
  397. if(d->numaddr < DOH_MAX_ADDR) {
  398. struct dohaddr *a = &d->addr[d->numaddr];
  399. a->type = DNS_TYPE_AAAA;
  400. memcpy(&a->ip.v6, &doh[index], 16);
  401. d->numaddr++;
  402. }
  403. return DOH_OK;
  404. }
  405. static DOHcode cnameappend(struct cnamestore *c,
  406. unsigned char *src,
  407. size_t len)
  408. {
  409. if(!c->alloc) {
  410. c->allocsize = len + 1;
  411. c->alloc = malloc(c->allocsize);
  412. if(!c->alloc)
  413. return DOH_OUT_OF_MEM;
  414. }
  415. else if(c->allocsize < (c->allocsize + len + 1)) {
  416. char *ptr;
  417. c->allocsize += len + 1;
  418. ptr = realloc(c->alloc, c->allocsize);
  419. if(!ptr) {
  420. free(c->alloc);
  421. return DOH_OUT_OF_MEM;
  422. }
  423. c->alloc = ptr;
  424. }
  425. memcpy(&c->alloc[c->len], src, len);
  426. c->len += len;
  427. c->alloc[c->len] = 0; /* keep it zero terminated */
  428. return DOH_OK;
  429. }
  430. static DOHcode store_cname(unsigned char *doh,
  431. size_t dohlen,
  432. unsigned int index,
  433. struct dohentry *d)
  434. {
  435. struct cnamestore *c;
  436. unsigned int loop = 128; /* a valid DNS name can never loop this much */
  437. unsigned char length;
  438. if(d->numcname == DOH_MAX_CNAME)
  439. return DOH_OK; /* skip! */
  440. c = &d->cname[d->numcname++];
  441. do {
  442. if(index >= dohlen)
  443. return DOH_DNS_OUT_OF_RANGE;
  444. length = doh[index];
  445. if((length & 0xc0) == 0xc0) {
  446. int newpos;
  447. /* name pointer, get the new offset (14 bits) */
  448. if((index + 1) >= dohlen)
  449. return DOH_DNS_OUT_OF_RANGE;
  450. /* move to the the new index */
  451. newpos = (length & 0x3f) << 8 | doh[index + 1];
  452. index = newpos;
  453. continue;
  454. }
  455. else if(length & 0xc0)
  456. return DOH_DNS_BAD_LABEL; /* bad input */
  457. else
  458. index++;
  459. if(length) {
  460. DOHcode rc;
  461. if(c->len) {
  462. rc = cnameappend(c, (unsigned char *)".", 1);
  463. if(rc)
  464. return rc;
  465. }
  466. if((index + length) > dohlen)
  467. return DOH_DNS_BAD_LABEL;
  468. rc = cnameappend(c, &doh[index], length);
  469. if(rc)
  470. return rc;
  471. index += length;
  472. }
  473. } while(length && --loop);
  474. if(!loop)
  475. return DOH_DNS_LABEL_LOOP;
  476. return DOH_OK;
  477. }
  478. static DOHcode rdata(unsigned char *doh,
  479. size_t dohlen,
  480. unsigned short rdlength,
  481. unsigned short type,
  482. int index,
  483. struct dohentry *d)
  484. {
  485. /* RDATA
  486. - A (TYPE 1): 4 bytes
  487. - AAAA (TYPE 28): 16 bytes
  488. - NS (TYPE 2): N bytes */
  489. DOHcode rc;
  490. switch(type) {
  491. case DNS_TYPE_A:
  492. if(rdlength != 4)
  493. return DOH_DNS_RDATA_LEN;
  494. rc = store_a(doh, index, d);
  495. if(rc)
  496. return rc;
  497. break;
  498. case DNS_TYPE_AAAA:
  499. if(rdlength != 16)
  500. return DOH_DNS_RDATA_LEN;
  501. rc = store_aaaa(doh, index, d);
  502. if(rc)
  503. return rc;
  504. break;
  505. case DNS_TYPE_CNAME:
  506. rc = store_cname(doh, dohlen, index, d);
  507. if(rc)
  508. return rc;
  509. break;
  510. default:
  511. /* unsupported type, just skip it */
  512. break;
  513. }
  514. return DOH_OK;
  515. }
  516. static void init_dohentry(struct dohentry *de)
  517. {
  518. memset(de, 0, sizeof(*de));
  519. de->ttl = INT_MAX;
  520. }
  521. UNITTEST DOHcode doh_decode(unsigned char *doh,
  522. size_t dohlen,
  523. DNStype dnstype,
  524. struct dohentry *d)
  525. {
  526. unsigned char rcode;
  527. unsigned short qdcount;
  528. unsigned short ancount;
  529. unsigned short type = 0;
  530. unsigned short class;
  531. unsigned short rdlength;
  532. unsigned short nscount;
  533. unsigned short arcount;
  534. unsigned int index = 12;
  535. DOHcode rc;
  536. if(dohlen < 12)
  537. return DOH_TOO_SMALL_BUFFER; /* too small */
  538. if(!doh || doh[0] || doh[1])
  539. return DOH_DNS_BAD_ID; /* bad ID */
  540. rcode = doh[3] & 0x0f;
  541. if(rcode)
  542. return DOH_DNS_BAD_RCODE; /* bad rcode */
  543. qdcount = get16bit(doh, 4);
  544. while(qdcount) {
  545. rc = skipqname(doh, dohlen, &index);
  546. if(rc)
  547. return rc; /* bad qname */
  548. if(dohlen < (index + 4))
  549. return DOH_DNS_OUT_OF_RANGE;
  550. index += 4; /* skip question's type and class */
  551. qdcount--;
  552. }
  553. ancount = get16bit(doh, 6);
  554. while(ancount) {
  555. unsigned int ttl;
  556. rc = skipqname(doh, dohlen, &index);
  557. if(rc)
  558. return rc; /* bad qname */
  559. if(dohlen < (index + 2))
  560. return DOH_DNS_OUT_OF_RANGE;
  561. type = get16bit(doh, index);
  562. if((type != DNS_TYPE_CNAME) && (type != dnstype))
  563. /* Not the same type as was asked for nor CNAME */
  564. return DOH_DNS_UNEXPECTED_TYPE;
  565. index += 2;
  566. if(dohlen < (index + 2))
  567. return DOH_DNS_OUT_OF_RANGE;
  568. class = get16bit(doh, index);
  569. if(DNS_CLASS_IN != class)
  570. return DOH_DNS_UNEXPECTED_CLASS; /* unsupported */
  571. index += 2;
  572. if(dohlen < (index + 4))
  573. return DOH_DNS_OUT_OF_RANGE;
  574. ttl = get32bit(doh, index);
  575. if(ttl < d->ttl)
  576. d->ttl = ttl;
  577. index += 4;
  578. if(dohlen < (index + 2))
  579. return DOH_DNS_OUT_OF_RANGE;
  580. rdlength = get16bit(doh, index);
  581. index += 2;
  582. if(dohlen < (index + rdlength))
  583. return DOH_DNS_OUT_OF_RANGE;
  584. rc = rdata(doh, dohlen, rdlength, type, index, d);
  585. if(rc)
  586. return rc; /* bad rdata */
  587. index += rdlength;
  588. ancount--;
  589. }
  590. nscount = get16bit(doh, 8);
  591. while(nscount) {
  592. rc = skipqname(doh, dohlen, &index);
  593. if(rc)
  594. return rc; /* bad qname */
  595. if(dohlen < (index + 8))
  596. return DOH_DNS_OUT_OF_RANGE;
  597. index += 2 + 2 + 4; /* type, class and ttl */
  598. if(dohlen < (index + 2))
  599. return DOH_DNS_OUT_OF_RANGE;
  600. rdlength = get16bit(doh, index);
  601. index += 2;
  602. if(dohlen < (index + rdlength))
  603. return DOH_DNS_OUT_OF_RANGE;
  604. index += rdlength;
  605. nscount--;
  606. }
  607. arcount = get16bit(doh, 10);
  608. while(arcount) {
  609. rc = skipqname(doh, dohlen, &index);
  610. if(rc)
  611. return rc; /* bad qname */
  612. if(dohlen < (index + 8))
  613. return DOH_DNS_OUT_OF_RANGE;
  614. index += 2 + 2 + 4; /* type, class and ttl */
  615. if(dohlen < (index + 2))
  616. return DOH_DNS_OUT_OF_RANGE;
  617. rdlength = get16bit(doh, index);
  618. index += 2;
  619. if(dohlen < (index + rdlength))
  620. return DOH_DNS_OUT_OF_RANGE;
  621. index += rdlength;
  622. arcount--;
  623. }
  624. if(index != dohlen)
  625. return DOH_DNS_MALFORMAT; /* something is wrong */
  626. if((type != DNS_TYPE_NS) && !d->numcname && !d->numaddr)
  627. /* nothing stored! */
  628. return DOH_NO_CONTENT;
  629. return DOH_OK; /* ok */
  630. }
  631. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  632. static void showdoh(struct Curl_easy *data,
  633. struct dohentry *d)
  634. {
  635. int i;
  636. infof(data, "TTL: %u seconds\n", d->ttl);
  637. for(i = 0; i < d->numaddr; i++) {
  638. struct dohaddr *a = &d->addr[i];
  639. if(a->type == DNS_TYPE_A) {
  640. infof(data, "DOH A: %u.%u.%u.%u\n",
  641. a->ip.v4[0], a->ip.v4[1],
  642. a->ip.v4[2], a->ip.v4[3]);
  643. }
  644. else if(a->type == DNS_TYPE_AAAA) {
  645. int j;
  646. char buffer[128];
  647. char *ptr;
  648. size_t len;
  649. msnprintf(buffer, 128, "DOH AAAA: ");
  650. ptr = &buffer[10];
  651. len = 118;
  652. for(j = 0; j < 16; j += 2) {
  653. size_t l;
  654. msnprintf(ptr, len, "%s%02x%02x", j?":":"", d->addr[i].ip.v6[j],
  655. d->addr[i].ip.v6[j + 1]);
  656. l = strlen(ptr);
  657. len -= l;
  658. ptr += l;
  659. }
  660. infof(data, "%s\n", buffer);
  661. }
  662. }
  663. for(i = 0; i < d->numcname; i++) {
  664. infof(data, "CNAME: %s\n", d->cname[i].alloc);
  665. }
  666. }
  667. #else
  668. #define showdoh(x,y)
  669. #endif
  670. /*
  671. * doh2ai()
  672. *
  673. * This function returns a pointer to the first element of a newly allocated
  674. * Curl_addrinfo struct linked list filled with the data from a set of DOH
  675. * lookups. Curl_addrinfo is meant to work like the addrinfo struct does for
  676. * a IPv6 stack, but usable also for IPv4, all hosts and environments.
  677. *
  678. * The memory allocated by this function *MUST* be free'd later on calling
  679. * Curl_freeaddrinfo(). For each successful call to this function there
  680. * must be an associated call later to Curl_freeaddrinfo().
  681. */
  682. static Curl_addrinfo *
  683. doh2ai(const struct dohentry *de, const char *hostname, int port)
  684. {
  685. Curl_addrinfo *ai;
  686. Curl_addrinfo *prevai = NULL;
  687. Curl_addrinfo *firstai = NULL;
  688. struct sockaddr_in *addr;
  689. #ifdef ENABLE_IPV6
  690. struct sockaddr_in6 *addr6;
  691. #endif
  692. CURLcode result = CURLE_OK;
  693. int i;
  694. if(!de)
  695. /* no input == no output! */
  696. return NULL;
  697. for(i = 0; i < de->numaddr; i++) {
  698. size_t ss_size;
  699. CURL_SA_FAMILY_T addrtype;
  700. if(de->addr[i].type == DNS_TYPE_AAAA) {
  701. #ifndef ENABLE_IPV6
  702. /* we can't handle IPv6 addresses */
  703. continue;
  704. #else
  705. ss_size = sizeof(struct sockaddr_in6);
  706. addrtype = AF_INET6;
  707. #endif
  708. }
  709. else {
  710. ss_size = sizeof(struct sockaddr_in);
  711. addrtype = AF_INET;
  712. }
  713. ai = calloc(1, sizeof(Curl_addrinfo));
  714. if(!ai) {
  715. result = CURLE_OUT_OF_MEMORY;
  716. break;
  717. }
  718. ai->ai_canonname = strdup(hostname);
  719. if(!ai->ai_canonname) {
  720. result = CURLE_OUT_OF_MEMORY;
  721. free(ai);
  722. break;
  723. }
  724. ai->ai_addr = calloc(1, ss_size);
  725. if(!ai->ai_addr) {
  726. result = CURLE_OUT_OF_MEMORY;
  727. free(ai->ai_canonname);
  728. free(ai);
  729. break;
  730. }
  731. if(!firstai)
  732. /* store the pointer we want to return from this function */
  733. firstai = ai;
  734. if(prevai)
  735. /* make the previous entry point to this */
  736. prevai->ai_next = ai;
  737. ai->ai_family = addrtype;
  738. /* we return all names as STREAM, so when using this address for TFTP
  739. the type must be ignored and conn->socktype be used instead! */
  740. ai->ai_socktype = SOCK_STREAM;
  741. ai->ai_addrlen = (curl_socklen_t)ss_size;
  742. /* leave the rest of the struct filled with zero */
  743. switch(ai->ai_family) {
  744. case AF_INET:
  745. addr = (void *)ai->ai_addr; /* storage area for this info */
  746. DEBUGASSERT(sizeof(struct in_addr) == sizeof(de->addr[i].ip.v4));
  747. memcpy(&addr->sin_addr, &de->addr[i].ip.v4, sizeof(struct in_addr));
  748. addr->sin_family = (CURL_SA_FAMILY_T)addrtype;
  749. addr->sin_port = htons((unsigned short)port);
  750. break;
  751. #ifdef ENABLE_IPV6
  752. case AF_INET6:
  753. addr6 = (void *)ai->ai_addr; /* storage area for this info */
  754. DEBUGASSERT(sizeof(struct in6_addr) == sizeof(de->addr[i].ip.v6));
  755. memcpy(&addr6->sin6_addr, &de->addr[i].ip.v6, sizeof(struct in6_addr));
  756. addr6->sin6_family = (CURL_SA_FAMILY_T)addrtype;
  757. addr6->sin6_port = htons((unsigned short)port);
  758. break;
  759. #endif
  760. }
  761. prevai = ai;
  762. }
  763. if(result) {
  764. Curl_freeaddrinfo(firstai);
  765. firstai = NULL;
  766. }
  767. return firstai;
  768. }
  769. #ifndef CURL_DISABLE_VERBOSE_STRINGS
  770. static const char *type2name(DNStype dnstype)
  771. {
  772. return (dnstype == DNS_TYPE_A)?"A":"AAAA";
  773. }
  774. #endif
  775. UNITTEST void de_cleanup(struct dohentry *d)
  776. {
  777. int i = 0;
  778. for(i = 0; i < d->numcname; i++) {
  779. free(d->cname[i].alloc);
  780. }
  781. }
  782. CURLcode Curl_doh_is_resolved(struct connectdata *conn,
  783. struct Curl_dns_entry **dnsp)
  784. {
  785. struct Curl_easy *data = conn->data;
  786. *dnsp = NULL; /* defaults to no response */
  787. if(!data->req.doh.probe[0].easy && !data->req.doh.probe[1].easy) {
  788. failf(data, "Could not DOH-resolve: %s", conn->async.hostname);
  789. return conn->bits.proxy?CURLE_COULDNT_RESOLVE_PROXY:
  790. CURLE_COULDNT_RESOLVE_HOST;
  791. }
  792. else if(!data->req.doh.pending) {
  793. DOHcode rc;
  794. DOHcode rc2;
  795. struct dohentry de;
  796. struct Curl_dns_entry *dns;
  797. struct Curl_addrinfo *ai;
  798. /* remove DOH handles from multi handle and close them */
  799. curl_multi_remove_handle(data->multi, data->req.doh.probe[0].easy);
  800. Curl_close(data->req.doh.probe[0].easy);
  801. curl_multi_remove_handle(data->multi, data->req.doh.probe[1].easy);
  802. Curl_close(data->req.doh.probe[1].easy);
  803. /* parse the responses, create the struct and return it! */
  804. init_dohentry(&de);
  805. rc = doh_decode(data->req.doh.probe[0].serverdoh.memory,
  806. data->req.doh.probe[0].serverdoh.size,
  807. data->req.doh.probe[0].dnstype,
  808. &de);
  809. free(data->req.doh.probe[0].serverdoh.memory);
  810. if(rc) {
  811. infof(data, "DOH: %s type %s for %s\n", doh_strerror(rc),
  812. type2name(data->req.doh.probe[0].dnstype),
  813. data->req.doh.host);
  814. }
  815. rc2 = doh_decode(data->req.doh.probe[1].serverdoh.memory,
  816. data->req.doh.probe[1].serverdoh.size,
  817. data->req.doh.probe[1].dnstype,
  818. &de);
  819. free(data->req.doh.probe[1].serverdoh.memory);
  820. if(rc2) {
  821. infof(data, "DOH: %s type %s for %s\n", doh_strerror(rc2),
  822. type2name(data->req.doh.probe[1].dnstype),
  823. data->req.doh.host);
  824. }
  825. if(!rc || !rc2) {
  826. infof(data, "DOH Host name: %s\n", data->req.doh.host);
  827. showdoh(data, &de);
  828. ai = doh2ai(&de, data->req.doh.host, data->req.doh.port);
  829. if(!ai) {
  830. de_cleanup(&de);
  831. return CURLE_OUT_OF_MEMORY;
  832. }
  833. if(data->share)
  834. Curl_share_lock(data, CURL_LOCK_DATA_DNS, CURL_LOCK_ACCESS_SINGLE);
  835. /* we got a response, store it in the cache */
  836. dns = Curl_cache_addr(data, ai, data->req.doh.host, data->req.doh.port);
  837. if(data->share)
  838. Curl_share_unlock(data, CURL_LOCK_DATA_DNS);
  839. de_cleanup(&de);
  840. if(!dns)
  841. /* returned failure, bail out nicely */
  842. Curl_freeaddrinfo(ai);
  843. else {
  844. conn->async.dns = dns;
  845. *dnsp = dns;
  846. return CURLE_OK;
  847. }
  848. }
  849. de_cleanup(&de);
  850. return CURLE_COULDNT_RESOLVE_HOST;
  851. }
  852. return CURLE_OK;
  853. }