1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553
|
/***************************************************************************
* _ _ ____ _
* Project ___| | | | _ \| |
* / __| | | | |_) | |
* | (__| |_| | _ <| |___
* \___|\___/|_| \_\_____|
*
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
*
* This software is licensed as described in the file COPYING, which
* you should have received as part of this distribution. The terms
* are also available at https://curl.se/docs/copyright.html.
*
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
* copies of the Software, and permit persons to whom the Software is
* furnished to do so, under the terms of the COPYING file.
*
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
* KIND, either express or implied.
*
* SPDX-License-Identifier: curl
*
***************************************************************************/
#include "unitcheck.h"
/* DoH + HTTPSRR are required */
#if !defined(CURL_DISABLE_DOH) && defined(USE_HTTPSRR)
#include "doh.h"
#include "httpsrr.h"
static CURLcode t1658_setup(void)
{
/* whatever you want done first */
curl_global_init(CURL_GLOBAL_ALL);
return CURLE_OK;
}
extern CURLcode doh_resp_decode_httpsrr(struct Curl_easy *data,
const unsigned char *cp, size_t len,
struct Curl_https_rrinfo **hrr);
extern void doh_print_httpsrr(struct Curl_easy *data,
struct Curl_https_rrinfo *hrr);
/*
* The idea here is that we pass one DNS packet at the time to the decoder. we
* then generate a string output with the results and compare if it matches
* the expected. One by one.
*/
static char rrbuffer[256];
static void rrresults(struct Curl_https_rrinfo *rr, CURLcode res)
{
char *p = rrbuffer;
const char *pend = rrbuffer + sizeof(rrbuffer);
curl_msnprintf(rrbuffer, sizeof(rrbuffer), "r:%d|", (int)res);
p += strlen(rrbuffer);
if(rr) {
unsigned int i;
curl_msnprintf(p, pend - p, "p:%d|", rr->priority);
p += strlen(p);
curl_msnprintf(p, pend - p, "%s|", rr->target ? rr->target : "-");
p += strlen(p);
for(i = 0; i < MAX_HTTPSRR_ALPNS && rr->alpns[i] != ALPN_none; i++) {
curl_msnprintf(p, pend - p, "alpn:%x|", rr->alpns[i]);
p += strlen(p);
}
if(rr->no_def_alpn) {
curl_msnprintf(p, pend - p, "no-def-alpn|");
p += strlen(p);
}
if(rr->port >= 0) {
curl_msnprintf(p, pend - p, "port:%d|", rr->port);
p += strlen(p);
}
if(rr->ipv4hints) {
for(i = 0; i < rr->ipv4hints_len; i += 4) {
curl_msnprintf(p, pend - p, "ipv4:%d.%d.%d.%d|",
rr->ipv4hints[i],
rr->ipv4hints[i + 1],
rr->ipv4hints[i + 2],
rr->ipv4hints[i + 3]);
p += strlen(p);
}
}
if(rr->echconfiglist) {
curl_msnprintf(p, pend - p, "ech:");
p += strlen(p);
for(i = 0; i < rr->echconfiglist_len; i++) {
curl_msnprintf(p, pend - p, "%02x", rr->echconfiglist[i]);
p += strlen(p);
}
curl_msnprintf(p, pend - p, "|");
p += strlen(p);
}
if(rr->ipv6hints) {
for(i = 0; i < rr->ipv6hints_len; i += 16) {
int x;
curl_msnprintf(p, pend - p, "ipv6:");
p += strlen(p);
for(x = 0; x < 16; x += 2) {
curl_msnprintf(p, pend - p, "%s%02x%02x",
x ? ":" : "",
rr->ipv6hints[i + x],
rr->ipv6hints[i + x + 1]);
p += strlen(p);
}
curl_msnprintf(p, pend - p, "|");
p += strlen(p);
}
}
}
}
static CURLcode test_unit1658(const char *arg)
{
UNITTEST_BEGIN(t1658_setup())
/* The "SvcParamKeys" specified within the HTTPS RR packet *must* be
provided in numerical order. */
struct test {
const char *name;
const unsigned char *dns;
size_t len; /* size of the dns packet */
const char *expect;
};
static const struct test t[] = {
{
"single h2 alpn",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* length byte */
"h2",
15,
"r:0|p:0|name.|alpn:10|"
},
{
"single h2 alpn missing last byte",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* length byte */
"h", /* missing byte */
14,
"r:8|"
},
{
"two alpns",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x06" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x02" /* APLN length byte */
"h3",
23,
"r:0|p:0|name.some.|alpn:10|alpn:20|"
},
{
"wrong syntax alpns",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x06" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x03" /* APLN length byte (WRONG) */
"h3",
23,
"r:61|"
},
{
"five alpns (ignore dupes)", /* we only support four */
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x0f" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x02" /* ALPN length byte */
"h2"
"\x02" /* ALPN length byte */
"h2"
"\x02" /* ALPN length byte */
"h2"
"\x02" /* APLN length byte */
"h3",
32,
"r:0|p:0|name.some.|alpn:10|alpn:20|"
},
{
"rname only",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00", /* RNAME */
13,
"r:0|p:0|name.some.|"
},
{
"rname with low ascii byte",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04som\x03\x00", /* RNAME */
13,
"r:8|"
},
{
"rname with null byte",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04sa\x00e\x04some\x00", /* RNAME */
13,
"r:27|"
},
{
"rname only (missing byte)",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x05some\x00", /* RNAME */
/* it lacks a byte */
13,
"r:27|"
},
{
"unrecognized alpn",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x06" /* data size */
"\x02" /* ALPN length byte */
"h8" /* unrecognized */
"\x02" /* APLN length byte */
"h1",
23,
"r:0|p:0|name.some.|alpn:8|"
},
{
"alnt + no-default-alpn",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x02" /* RR (2 == NO DEFAULT ALPN) */
"\x00\x00", /* must be zero */
24,
"r:0|p:0|name.some.|alpn:10|no-def-alpn|"
},
{
"alnt + no-default-alpn with size",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x02" /* RR (2 == NO DEFAULT ALPN) */
"\x00\x01" /* must be zero */
"\xff",
25,
"r:43|"
},
{
"alnt + no-default-alpn with size too short package",
(const unsigned char *)"\x00\x00" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x02" /* RR (2 == NO DEFAULT ALPN) */
"\x00\x01", /* must be zero */
/* missing last byte in the packet */
24,
"r:8|"
},
{
"rname + blank alpn field",
(const unsigned char *)"\x11\x11" /* 16-bit prio */
"\x04name\x04some\x00" /* RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x00", /* data size, strictly speaking this is illegal:
"one or more alpn-ids" */
17,
"r:0|p:4369|name.some.|"
},
{
"no rname + blank alpn",
(const unsigned char *)"\x00\x11" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x00", /* data size */
7,
"r:0|p:17|.|"
},
{
"unsupported field",
(const unsigned char *)"\xff\xff" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x07" /* RR (7 == not a supported data) */
"\x00\x02" /* data size */
"FF", /* unknown to curl */
9,
"r:0|p:65535|.|"
},
{
"unsupported field (wrong size)",
(const unsigned char *)"\xff\xff" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x07" /* RR (7 == not a supported data) */
"\x00\x02" /* data size */
"F", /* unknown to curl */
8,
"r:8|"
},
{
"port number",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x03" /* RR (3 == PORT) */
"\x00\x02" /* data size */
"\x12\x34", /* port number */
16,
"r:0|p:16|.|alpn:10|port:4660|"
},
{
"port number with wrong size (3 bytes)",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x03" /* RR (3 == PORT) */
"\x00\x03" /* data size */
"\x12\x34\x00", /* 24-bit port number */
17,
"r:43|"
},
{
"port number with wrong size (1 byte)",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x03" /* RR (3 == PORT) */
"\x00\x01" /* data size */
"\x12", /* 8-bit port number */
15,
"r:43|"
},
{
"alpn + two ipv4 addresses",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x04" /* RR (4 == Ipv4hints) */
"\x00\x08" /* data size */
"\xc0\xa8\x00\x01" /* 32 bits */
"\xc0\xa8\x00\x02", /* 32 bits */
22,
"r:0|p:16|.|alpn:10|ipv4:192.168.0.1|ipv4:192.168.0.2|"
},
{
"alpn + two ipv4 addresses in wrong order",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x04" /* RR (4 == Ipv4hints) */
"\x00\x08" /* data size */
"\xc0\xa8\x00\x01" /* 32 bits */
"\xc0\xa8\x00\x02" /* 32 bits */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2",
22,
"r:8|"
},
{
"alpn + ipv4 address with wrong size",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x04" /* RR (4 == Ipv4hints) */
"\x00\x05" /* data size */
"\xc0\xa8\x00\x01\xff", /* 32 + 8 bits */
19,
"r:43|"
},
{
"alpn + one ipv6 address",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x06" /* RR (6 == Ipv6hints) */
"\x00\x10" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x23",
30,
"r:0|p:16|.|alpn:10|ipv6:fe80:dabb:c1ff:fea3:8a22:1234:5678:9123|"
},
{
"alpn + one ipv6 address with wrong size",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x06" /* RR (6 == Ipv6hints) */
"\x00\x11" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x23\x45",
31,
"r:43|"
},
{
"alpn + two ipv6 addresses",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x06" /* RR (6 == Ipv6hints) */
"\x00\x20" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x23"
"\xee\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x25",
46,
"r:0|p:16|.|alpn:10|ipv6:fe80:dabb:c1ff:fea3:8a22:1234:5678:9123|"
"ipv6:ee80:dabb:c1ff:fea3:8a22:1234:5678:9125|"
},
{
"alpn + ech",
(const unsigned char *)"\x00\x10" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x03" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x00\x05" /* RR (5 == ECH) */
"\x00\x10" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x23",
30,
"r:0|p:16|.|alpn:10|ech:fe80dabbc1fffea38a22123456789123|"
},
{
"fully packed",
(const unsigned char *)"\xa0\x0b" /* 16-bit prio */
"\x00" /* no RNAME */
"\x00\x00" /* RR (0 == MANDATORY) */
"\x00\x00" /* data size */
"\x00\x01" /* RR (1 == ALPN) */
"\x00\x06" /* data size */
"\x02" /* ALPN length byte */
"h2"
"\x02" /* ALPN length byte */
"h1"
"\x00\x02" /* RR (2 == NO DEFAULT ALPN) */
"\x00\x00" /* must be zero */
"\x00\x03" /* RR (3 == PORT) */
"\x00\x02" /* data size */
"\xbc\x71" /* port number */
"\x00\x04" /* RR (4 == Ipv4hints) */
"\x00\x08" /* data size */
"\xc0\xa8\x00\x01" /* 32 bits */
"\xc0\xa8\x00\x02" /* 32 bits */
"\x00\x05" /* RR (5 == ECH) */
"\x00\x10" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\x7e\xb3\x8a\x22\x12\x34\x56\x78\x91\x23"
"\x00\x06" /* RR (6 == Ipv6hints) */
"\x00\x20" /* data size */
"\xfe\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x23"
"\xee\x80\xda\xbb\xc1\xff\xfe\xa3\x8a\x22\x12\x34\x56\x78\x91\x25"
"\x01\x07" /* RR (263 == not supported) */
"\x00\x04" /* data size */
"FFAA", /* unknown to the world */
103,
"r:0|p:40971|.|alpn:10|alpn:8|no-def-alpn|port:48241|"
"ipv4:192.168.0.1|ipv4:192.168.0.2|"
"ech:fe80dabbc1ff7eb38a22123456789123|"
"ipv6:fe80:dabb:c1ff:fea3:8a22:1234:5678:9123|"
"ipv6:ee80:dabb:c1ff:fea3:8a22:1234:5678:9125|"
}
};
CURLcode result = CURLE_OUT_OF_MEMORY;
CURL *easy;
easy = curl_easy_init();
/* so that we get the log output: */
curl_easy_setopt(easy, CURLOPT_VERBOSE, 1L);
if(easy) {
unsigned int i;
for(i = 0; i < CURL_ARRAYSIZE(t); i++) {
struct Curl_https_rrinfo *hrr;
curl_mprintf("test %u: %s\n", i, t[i].name);
result = doh_resp_decode_httpsrr(easy, t[i].dns, t[i].len, &hrr);
/* create an output */
rrresults(hrr, result);
/* is the output the expected? */
if(strcmp(rrbuffer, t[i].expect)) {
curl_mfprintf(stderr, "Test %s (%u) failed\n"
"Expected: %s\n"
"Received: %s\n", t[i].name, i, t[i].expect, rrbuffer);
unitfail++;
}
/* free the generated struct */
if(hrr) {
Curl_httpsrr_cleanup(hrr);
curl_free(hrr);
}
}
curl_easy_cleanup(easy);
}
UNITTEST_END(curl_global_cleanup())
}
#else /* CURL_DISABLE_DOH || !USE_HTTPSRR */
static CURLcode test_unit1658(const char *arg)
{
UNITTEST_BEGIN_SIMPLE
UNITTEST_END_SIMPLE
}
#endif
|