doh.c 26 KB

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