2
0

unit1650.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curlcheck.h"
  25. #include "doh.h"
  26. #include "dynbuf.h"
  27. static CURLcode unit_setup(void)
  28. {
  29. return CURLE_OK;
  30. }
  31. static void unit_stop(void)
  32. {
  33. }
  34. #ifndef CURL_DISABLE_DOH
  35. #define DNS_PREAMBLE "\x00\x00\x01\x00\x00\x01\x00\x00\x00\x00\x00\x00"
  36. #define LABEL_TEST "\x04\x74\x65\x73\x74"
  37. #define LABEL_HOST "\x04\x68\x6f\x73\x74"
  38. #define LABEL_NAME "\x04\x6e\x61\x6d\x65"
  39. #define DNSA_TYPE "\x01"
  40. #define DNSAAAA_TYPE "\x1c"
  41. #define DNSA_EPILOGUE "\x00\x00" DNSA_TYPE "\x00\x01"
  42. #define DNSAAAA_EPILOGUE "\x00\x00" DNSAAAA_TYPE "\x00\x01"
  43. #define DNS_Q1 DNS_PREAMBLE LABEL_TEST LABEL_HOST LABEL_NAME DNSA_EPILOGUE
  44. #define DNS_Q2 DNS_PREAMBLE LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE
  45. struct dohrequest {
  46. /* input */
  47. const char *name;
  48. DNStype type;
  49. /* output */
  50. const char *packet;
  51. size_t size;
  52. DOHcode rc;
  53. };
  54. static const struct dohrequest req[] = {
  55. {"test.host.name", DNS_TYPE_A, DNS_Q1, sizeof(DNS_Q1)-1, DOH_OK },
  56. {"test.host.name", DNS_TYPE_AAAA, DNS_Q2, sizeof(DNS_Q2)-1, DOH_OK },
  57. {"zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz"
  58. ".host.name",
  59. DNS_TYPE_AAAA, NULL, 0, DOH_DNS_BAD_LABEL }
  60. };
  61. struct dohresp {
  62. /* input */
  63. const char *packet;
  64. size_t size;
  65. DNStype type;
  66. /* output */
  67. DOHcode rc;
  68. const char *out;
  69. };
  70. #define DNS_FOO_EXAMPLE_COM \
  71. "\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f" \
  72. "\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x01\x00" \
  73. "\x01\xc0\x0c\x00\x01\x00\x01\x00\x00\x00\x37\x00\x04\x7f\x00\x00" \
  74. "\x01"
  75. static const char full49[] = DNS_FOO_EXAMPLE_COM;
  76. static const struct dohresp resp[] = {
  77. {"\x00\x00", 2, DNS_TYPE_A, DOH_TOO_SMALL_BUFFER, NULL },
  78. {"\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 12,
  79. DNS_TYPE_A, DOH_DNS_BAD_ID, NULL },
  80. {"\x00\x00\x00\x01\x00\x01\x00\x01\x00\x01\x00\x01", 12,
  81. DNS_TYPE_A, DOH_DNS_BAD_RCODE, NULL },
  82. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f", 16,
  83. DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
  84. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00", 17,
  85. DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
  86. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00"
  87. "\x00\x01\x00\x01", 21,
  88. DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
  89. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x03\x66\x6f\x6f\x00"
  90. "\x00\x01\x00\x01"
  91. "\x04", 18,
  92. DNS_TYPE_A, DOH_DNS_OUT_OF_RANGE, NULL },
  93. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x63\x75\x72"
  94. "\x6c\x04\x63\x75\x72\x6c\x00\x00\x05\x00\x01\xc0\x0c\x00\x05\x00"
  95. "\x01\x00\x00\x00\x37\x00\x11\x08\x61\x6e\x79\x77\x68\x65\x72\x65"
  96. "\x06\x72\x65\x61\x6c\x6c\x79\x00", 56,
  97. DNS_TYPE_A, DOH_OK, "anywhere.really "},
  98. {DNS_FOO_EXAMPLE_COM, 49, DNS_TYPE_A, DOH_OK, "127.0.0.1 "},
  99. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x61\x61\x61"
  100. "\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
  101. "\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
  102. "\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20", 62,
  103. DNS_TYPE_AAAA, DOH_OK,
  104. "2020:2020:0000:0000:0000:0000:0000:2020 " },
  105. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x00\x04\x63\x75\x72"
  106. "\x6c\x04\x63\x75\x72\x6c\x00\x00\x05\x00\x01\xc0\x0c\x00\x05\x00"
  107. "\x01\x00\x00\x00\x37\x00"
  108. "\x07\x03\x61\x6e\x79\xc0\x27\x00", 46,
  109. DNS_TYPE_A, DOH_DNS_LABEL_LOOP, NULL},
  110. /* packet with NSCOUNT == 1 */
  111. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x01\x00\x00\x04\x61\x61\x61"
  112. "\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
  113. "\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
  114. "\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20"
  115. LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE "\x00\x00\x00\x01"
  116. "\00\x04\x01\x01\x01\x01", /* RDDATA */
  117. 62 + 30,
  118. DNS_TYPE_AAAA, DOH_OK,
  119. "2020:2020:0000:0000:0000:0000:0000:2020 " },
  120. /* packet with ARCOUNT == 1 */
  121. {"\x00\x00\x01\x00\x00\x01\x00\x01\x00\x00\x00\x01\x04\x61\x61\x61"
  122. "\x61\x07\x65\x78\x61\x6d\x70\x6c\x65\x03\x63\x6f\x6d\x00\x00\x1c"
  123. "\x00\x01\xc0\x0c\x00\x1c\x00\x01\x00\x00\x00\x37\x00\x10\x20\x20"
  124. "\x20\x20\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x20\x20"
  125. LABEL_TEST LABEL_HOST LABEL_NAME DNSAAAA_EPILOGUE "\x00\x00\x00\x01"
  126. "\00\x04\x01\x01\x01\x01", /* RDDATA */
  127. 62 + 30,
  128. DNS_TYPE_AAAA, DOH_OK,
  129. "2020:2020:0000:0000:0000:0000:0000:2020 " },
  130. };
  131. UNITTEST_START
  132. {
  133. size_t size = 0;
  134. unsigned char buffer[256];
  135. size_t i;
  136. unsigned char *p;
  137. for(i = 0; i < sizeof(req) / sizeof(req[0]); i++) {
  138. DOHcode rc = doh_req_encode(req[i].name, req[i].type,
  139. buffer, sizeof(buffer), &size);
  140. if(rc != req[i].rc) {
  141. fprintf(stderr, "req %zu: Expected return code %d got %d\n", i,
  142. req[i].rc, rc);
  143. abort_if(rc != req[i].rc, "return code");
  144. }
  145. if(size != req[i].size) {
  146. fprintf(stderr, "req %zu: Expected size %zu got %zu\n", i,
  147. req[i].size, size);
  148. fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
  149. abort_if(size != req[i].size, "size");
  150. }
  151. if(req[i].packet && memcmp(req[i].packet, buffer, size)) {
  152. fprintf(stderr, "DNS encode made: %s\n", hexdump(buffer, size));
  153. fprintf(stderr, "... instead of: %s\n",
  154. hexdump((unsigned char *)req[i].packet, size));
  155. abort_if(req[i].packet && memcmp(req[i].packet, buffer, size),
  156. "contents");
  157. }
  158. }
  159. for(i = 0; i < sizeof(resp) / sizeof(resp[0]); i++) {
  160. struct dohentry d;
  161. DOHcode rc;
  162. char *ptr;
  163. size_t len;
  164. int u;
  165. de_init(&d);
  166. rc = doh_resp_decode((const unsigned char *)resp[i].packet, resp[i].size,
  167. resp[i].type, &d);
  168. if(rc != resp[i].rc) {
  169. fprintf(stderr, "resp %zu: Expected return code %d got %d\n", i,
  170. resp[i].rc, rc);
  171. abort_if(rc != resp[i].rc, "return code");
  172. }
  173. len = sizeof(buffer);
  174. ptr = (char *)buffer;
  175. for(u = 0; u < d.numaddr; u++) {
  176. size_t o;
  177. struct dohaddr *a;
  178. a = &d.addr[u];
  179. if(resp[i].type == DNS_TYPE_A) {
  180. p = &a->ip.v4[0];
  181. msnprintf(ptr, len, "%u.%u.%u.%u ", p[0], p[1], p[2], p[3]);
  182. o = strlen(ptr);
  183. len -= o;
  184. ptr += o;
  185. }
  186. else {
  187. int j;
  188. for(j = 0; j < 16; j += 2) {
  189. size_t l;
  190. msnprintf(ptr, len, "%s%02x%02x", j?":":"", a->ip.v6[j],
  191. a->ip.v6[j + 1]);
  192. l = strlen(ptr);
  193. len -= l;
  194. ptr += l;
  195. }
  196. msnprintf(ptr, len, " ");
  197. len--;
  198. ptr++;
  199. }
  200. }
  201. for(u = 0; u < d.numcname; u++) {
  202. size_t o;
  203. msnprintf(ptr, len, "%s ", Curl_dyn_ptr(&d.cname[u]));
  204. o = strlen(ptr);
  205. len -= o;
  206. ptr += o;
  207. }
  208. de_cleanup(&d);
  209. if(resp[i].out && strcmp((char *)buffer, resp[i].out)) {
  210. fprintf(stderr, "resp %zu: Expected %s got %s\n", i,
  211. resp[i].out, buffer);
  212. abort_if(resp[i].out && strcmp((char *)buffer, resp[i].out), "content");
  213. }
  214. }
  215. /* pass all sizes into the decoder until full */
  216. for(i = 0; i < sizeof(full49)-1; i++) {
  217. struct dohentry d;
  218. DOHcode rc;
  219. memset(&d, 0, sizeof(d));
  220. rc = doh_resp_decode((const unsigned char *)full49, i, DNS_TYPE_A, &d);
  221. if(!rc) {
  222. /* none of them should work */
  223. fprintf(stderr, "%zu: %d\n", i, rc);
  224. abort_if(!rc, "error rc");
  225. }
  226. }
  227. /* and try all pieces from the other end of the packet */
  228. for(i = 1; i < sizeof(full49); i++) {
  229. struct dohentry d;
  230. DOHcode rc;
  231. memset(&d, 0, sizeof(d));
  232. rc = doh_resp_decode((const unsigned char *)&full49[i], sizeof(full49)-i-1,
  233. DNS_TYPE_A, &d);
  234. if(!rc) {
  235. /* none of them should work */
  236. fprintf(stderr, "2 %zu: %d\n", i, rc);
  237. abort_if(!rc, "error rc");
  238. }
  239. }
  240. {
  241. DOHcode rc;
  242. struct dohentry d;
  243. struct dohaddr *a;
  244. memset(&d, 0, sizeof(d));
  245. rc = doh_resp_decode((const unsigned char *)full49, sizeof(full49)-1,
  246. DNS_TYPE_A, &d);
  247. fail_if(d.numaddr != 1, "missing address");
  248. a = &d.addr[0];
  249. p = &a->ip.v4[0];
  250. msnprintf((char *)buffer, sizeof(buffer),
  251. "%u.%u.%u.%u", p[0], p[1], p[2], p[3]);
  252. if(rc || strcmp((char *)buffer, "127.0.0.1")) {
  253. fprintf(stderr, "bad address decoded: %s, rc == %d\n", buffer, rc);
  254. abort_if(rc || strcmp((char *)buffer, "127.0.0.1"), "bad address");
  255. }
  256. fail_if(d.numcname, "bad cname counter");
  257. }
  258. }
  259. UNITTEST_STOP
  260. #else /* CURL_DISABLE_DOH */
  261. UNITTEST_START
  262. /* nothing to do, just succeed */
  263. UNITTEST_STOP
  264. #endif