2
0

unit1650.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 2018 - 2022, 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. int rc;
  53. };
  54. static const struct dohrequest req[] = {
  55. {"test.host.name", DNS_TYPE_A, DNS_Q1, sizeof(DNS_Q1)-1, 0 },
  56. {"test.host.name", DNS_TYPE_AAAA, DNS_Q2, sizeof(DNS_Q2)-1, 0 },
  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. int 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. int rc = doh_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. return 1;
  144. }
  145. else 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. return 2;
  150. }
  151. else 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. return 3;
  156. }
  157. }
  158. for(i = 0; i < sizeof(resp) / sizeof(resp[0]); i++) {
  159. struct dohentry d;
  160. int rc;
  161. char *ptr;
  162. size_t len;
  163. int u;
  164. de_init(&d);
  165. rc = doh_decode((const unsigned char *)resp[i].packet, resp[i].size,
  166. resp[i].type, &d);
  167. if(rc != resp[i].rc) {
  168. fprintf(stderr, "resp %zu: Expected return code %d got %d\n", i,
  169. resp[i].rc, rc);
  170. return 4;
  171. }
  172. len = sizeof(buffer);
  173. ptr = (char *)buffer;
  174. for(u = 0; u < d.numaddr; u++) {
  175. size_t o;
  176. struct dohaddr *a;
  177. a = &d.addr[u];
  178. if(resp[i].type == DNS_TYPE_A) {
  179. p = &a->ip.v4[0];
  180. msnprintf(ptr, len, "%u.%u.%u.%u ", p[0], p[1], p[2], p[3]);
  181. o = strlen(ptr);
  182. len -= o;
  183. ptr += o;
  184. }
  185. else {
  186. int j;
  187. for(j = 0; j < 16; j += 2) {
  188. size_t l;
  189. msnprintf(ptr, len, "%s%02x%02x", j?":":"", a->ip.v6[j],
  190. a->ip.v6[j + 1]);
  191. l = strlen(ptr);
  192. len -= l;
  193. ptr += l;
  194. }
  195. msnprintf(ptr, len, " ");
  196. len--;
  197. ptr++;
  198. }
  199. }
  200. for(u = 0; u < d.numcname; u++) {
  201. size_t o;
  202. msnprintf(ptr, len, "%s ", Curl_dyn_ptr(&d.cname[u]));
  203. o = strlen(ptr);
  204. len -= o;
  205. ptr += o;
  206. }
  207. de_cleanup(&d);
  208. if(resp[i].out && strcmp((char *)buffer, resp[i].out)) {
  209. fprintf(stderr, "resp %zu: Expected %s got %s\n", i,
  210. resp[i].out, buffer);
  211. return 1;
  212. }
  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. int rc;
  219. memset(&d, 0, sizeof(d));
  220. rc = doh_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. return 5;
  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. int rc;
  231. memset(&d, 0, sizeof(d));
  232. rc = doh_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. return 7;
  238. }
  239. }
  240. {
  241. int rc;
  242. struct dohentry d;
  243. struct dohaddr *a;
  244. memset(&d, 0, sizeof(d));
  245. rc = doh_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. return 7;
  255. }
  256. fail_if(d.numcname, "bad cname counter");
  257. }
  258. }
  259. }
  260. UNITTEST_STOP
  261. #else /* CURL_DISABLE_DOH */
  262. UNITTEST_START
  263. {
  264. return 1; /* nothing to do, just fail */
  265. }
  266. UNITTEST_STOP
  267. #endif