other-licenses/android/ns_name.c

Tue, 06 Jan 2015 21:39:09 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Tue, 06 Jan 2015 21:39:09 +0100
branch
TOR_BUG_9701
changeset 8
97036ab72558
permissions
-rw-r--r--

Conditionally force memory storage according to privacy.thirdparty.isolate;
This solves Tor bug #9701, complying with disk avoidance documented in
https://www.torproject.org/projects/torbrowser/design/#disk-avoidance.

michael@0 1 /* $NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $ */
michael@0 2
michael@0 3 /*
michael@0 4 * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
michael@0 5 * Copyright (c) 1996,1999 by Internet Software Consortium.
michael@0 6 *
michael@0 7 * Permission to use, copy, modify, and distribute this software for any
michael@0 8 * purpose with or without fee is hereby granted, provided that the above
michael@0 9 * copyright notice and this permission notice appear in all copies.
michael@0 10 *
michael@0 11 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
michael@0 12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
michael@0 13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
michael@0 14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
michael@0 15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
michael@0 16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
michael@0 17 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
michael@0 18 */
michael@0 19
michael@0 20 /*
michael@0 21 * This version of this file is derived from Android 2.3 "Gingerbread",
michael@0 22 * which contains uncredited changes by Android/Google developers. It has
michael@0 23 * been modified in 2011 for use in the Android build of Mozilla Firefox by
michael@0 24 * Mozilla contributors (including Michael Edwards <m.k.edwards@gmail.com>,
michael@0 25 * and Steve Workman <sjhworkman@gmail.com>).
michael@0 26 * These changes are offered under the same license as the original NetBSD
michael@0 27 * file, whose copyright and license are unchanged above.
michael@0 28 */
michael@0 29
michael@0 30 #define ANDROID_CHANGES 1
michael@0 31 #define MOZILLA_NECKO_EXCLUDE_CODE 1
michael@0 32
michael@0 33 #include <sys/cdefs.h>
michael@0 34 #ifndef lint
michael@0 35 #ifdef notdef
michael@0 36 static const char rcsid[] = "Id: ns_name.c,v 1.3.2.4.4.2 2004/05/04 03:27:47 marka Exp";
michael@0 37 #else
michael@0 38 __RCSID("$NetBSD: ns_name.c,v 1.3 2004/11/07 02:19:49 christos Exp $");
michael@0 39 #endif
michael@0 40 #endif
michael@0 41
michael@0 42 #include <sys/types.h>
michael@0 43
michael@0 44 #include <netinet/in.h>
michael@0 45 #include "arpa_nameser.h"
michael@0 46
michael@0 47 #include <errno.h>
michael@0 48 #ifdef ANDROID_CHANGES
michael@0 49 #include "resolv_private.h"
michael@0 50 #else
michael@0 51 #include <resolv.h>
michael@0 52 #endif
michael@0 53 #include <string.h>
michael@0 54 #include <ctype.h>
michael@0 55 #include <stdlib.h>
michael@0 56 #include <limits.h>
michael@0 57
michael@0 58 #ifdef SPRINTF_CHAR
michael@0 59 # define SPRINTF(x) strlen(sprintf/**/x)
michael@0 60 #else
michael@0 61 # define SPRINTF(x) ((size_t)sprintf x)
michael@0 62 #endif
michael@0 63
michael@0 64 #define NS_TYPE_ELT 0x40 /* EDNS0 extended label type */
michael@0 65 #define DNS_LABELTYPE_BITSTRING 0x41
michael@0 66
michael@0 67 /* Data. */
michael@0 68
michael@0 69 static const char digits[] = "0123456789";
michael@0 70
michael@0 71 static const char digitvalue[256] = {
michael@0 72 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*16*/
michael@0 73 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*32*/
michael@0 74 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*48*/
michael@0 75 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /*64*/
michael@0 76 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*80*/
michael@0 77 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*96*/
michael@0 78 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*112*/
michael@0 79 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*128*/
michael@0 80 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 81 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 82 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 83 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 84 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
michael@0 87 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, /*256*/
michael@0 88 };
michael@0 89
michael@0 90 /* Forward. */
michael@0 91
michael@0 92 static int special(int);
michael@0 93 static int printable(int);
michael@0 94 static int dn_find(const u_char *, const u_char *,
michael@0 95 const u_char * const *,
michael@0 96 const u_char * const *);
michael@0 97 static int encode_bitsring(const char **, const char *,
michael@0 98 unsigned char **, unsigned char **,
michael@0 99 unsigned const char *);
michael@0 100 static int labellen(const u_char *);
michael@0 101 static int decode_bitstring(const unsigned char **,
michael@0 102 char *, const char *);
michael@0 103
michael@0 104 /* Public. */
michael@0 105
michael@0 106 /*
michael@0 107 * ns_name_ntop(src, dst, dstsiz)
michael@0 108 * Convert an encoded domain name to printable ascii as per RFC1035.
michael@0 109 * return:
michael@0 110 * Number of bytes written to buffer, or -1 (with errno set)
michael@0 111 * notes:
michael@0 112 * The root is returned as "."
michael@0 113 * All other domains are returned in non absolute form
michael@0 114 */
michael@0 115 int
michael@0 116 ns_name_ntop(const u_char *src, char *dst, size_t dstsiz)
michael@0 117 {
michael@0 118 const u_char *cp;
michael@0 119 char *dn, *eom;
michael@0 120 u_char c;
michael@0 121 u_int n;
michael@0 122 int l;
michael@0 123
michael@0 124 cp = src;
michael@0 125 dn = dst;
michael@0 126 eom = dst + dstsiz;
michael@0 127
michael@0 128 while ((n = *cp++) != 0) {
michael@0 129 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
michael@0 130 /* Some kind of compression pointer. */
michael@0 131 errno = EMSGSIZE;
michael@0 132 return (-1);
michael@0 133 }
michael@0 134 if (dn != dst) {
michael@0 135 if (dn >= eom) {
michael@0 136 errno = EMSGSIZE;
michael@0 137 return (-1);
michael@0 138 }
michael@0 139 *dn++ = '.';
michael@0 140 }
michael@0 141 if ((l = labellen(cp - 1)) < 0) {
michael@0 142 errno = EMSGSIZE; /* XXX */
michael@0 143 return(-1);
michael@0 144 }
michael@0 145 if (dn + l >= eom) {
michael@0 146 errno = EMSGSIZE;
michael@0 147 return (-1);
michael@0 148 }
michael@0 149 if ((n & NS_CMPRSFLGS) == NS_TYPE_ELT) {
michael@0 150 int m;
michael@0 151
michael@0 152 if (n != DNS_LABELTYPE_BITSTRING) {
michael@0 153 /* XXX: labellen should reject this case */
michael@0 154 errno = EINVAL;
michael@0 155 return(-1);
michael@0 156 }
michael@0 157 if ((m = decode_bitstring(&cp, dn, eom)) < 0)
michael@0 158 {
michael@0 159 errno = EMSGSIZE;
michael@0 160 return(-1);
michael@0 161 }
michael@0 162 dn += m;
michael@0 163 continue;
michael@0 164 }
michael@0 165 for (; l > 0; l--) {
michael@0 166 c = *cp++;
michael@0 167 if (special(c)) {
michael@0 168 if (dn + 1 >= eom) {
michael@0 169 errno = EMSGSIZE;
michael@0 170 return (-1);
michael@0 171 }
michael@0 172 *dn++ = '\\';
michael@0 173 *dn++ = (char)c;
michael@0 174 } else if (!printable(c)) {
michael@0 175 if (dn + 3 >= eom) {
michael@0 176 errno = EMSGSIZE;
michael@0 177 return (-1);
michael@0 178 }
michael@0 179 *dn++ = '\\';
michael@0 180 *dn++ = digits[c / 100];
michael@0 181 *dn++ = digits[(c % 100) / 10];
michael@0 182 *dn++ = digits[c % 10];
michael@0 183 } else {
michael@0 184 if (dn >= eom) {
michael@0 185 errno = EMSGSIZE;
michael@0 186 return (-1);
michael@0 187 }
michael@0 188 *dn++ = (char)c;
michael@0 189 }
michael@0 190 }
michael@0 191 }
michael@0 192 if (dn == dst) {
michael@0 193 if (dn >= eom) {
michael@0 194 errno = EMSGSIZE;
michael@0 195 return (-1);
michael@0 196 }
michael@0 197 *dn++ = '.';
michael@0 198 }
michael@0 199 if (dn >= eom) {
michael@0 200 errno = EMSGSIZE;
michael@0 201 return (-1);
michael@0 202 }
michael@0 203 *dn++ = '\0';
michael@0 204 return (dn - dst);
michael@0 205 }
michael@0 206
michael@0 207 /*
michael@0 208 * ns_name_pton(src, dst, dstsiz)
michael@0 209 * Convert a ascii string into an encoded domain name as per RFC1035.
michael@0 210 * return:
michael@0 211 * -1 if it fails
michael@0 212 * 1 if string was fully qualified
michael@0 213 * 0 is string was not fully qualified
michael@0 214 * notes:
michael@0 215 * Enforces label and domain length limits.
michael@0 216 */
michael@0 217
michael@0 218 int
michael@0 219 ns_name_pton(const char *src, u_char *dst, size_t dstsiz)
michael@0 220 {
michael@0 221 u_char *label, *bp, *eom;
michael@0 222 int c, n, escaped, e = 0;
michael@0 223 char *cp;
michael@0 224
michael@0 225 escaped = 0;
michael@0 226 bp = dst;
michael@0 227 eom = dst + dstsiz;
michael@0 228 label = bp++;
michael@0 229
michael@0 230 while ((c = *src++) != 0) {
michael@0 231 if (escaped) {
michael@0 232 if (c == '[') { /* start a bit string label */
michael@0 233 if ((cp = strchr(src, ']')) == NULL) {
michael@0 234 errno = EINVAL; /* ??? */
michael@0 235 return(-1);
michael@0 236 }
michael@0 237 if ((e = encode_bitsring(&src, cp + 2,
michael@0 238 &label, &bp, eom))
michael@0 239 != 0) {
michael@0 240 errno = e;
michael@0 241 return(-1);
michael@0 242 }
michael@0 243 escaped = 0;
michael@0 244 label = bp++;
michael@0 245 if ((c = *src++) == 0)
michael@0 246 goto done;
michael@0 247 else if (c != '.') {
michael@0 248 errno = EINVAL;
michael@0 249 return(-1);
michael@0 250 }
michael@0 251 continue;
michael@0 252 }
michael@0 253 else if ((cp = strchr(digits, c)) != NULL) {
michael@0 254 n = (cp - digits) * 100;
michael@0 255 if ((c = *src++) == 0 ||
michael@0 256 (cp = strchr(digits, c)) == NULL) {
michael@0 257 errno = EMSGSIZE;
michael@0 258 return (-1);
michael@0 259 }
michael@0 260 n += (cp - digits) * 10;
michael@0 261 if ((c = *src++) == 0 ||
michael@0 262 (cp = strchr(digits, c)) == NULL) {
michael@0 263 errno = EMSGSIZE;
michael@0 264 return (-1);
michael@0 265 }
michael@0 266 n += (cp - digits);
michael@0 267 if (n > 255) {
michael@0 268 errno = EMSGSIZE;
michael@0 269 return (-1);
michael@0 270 }
michael@0 271 c = n;
michael@0 272 }
michael@0 273 escaped = 0;
michael@0 274 } else if (c == '\\') {
michael@0 275 escaped = 1;
michael@0 276 continue;
michael@0 277 } else if (c == '.') {
michael@0 278 c = (bp - label - 1);
michael@0 279 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
michael@0 280 errno = EMSGSIZE;
michael@0 281 return (-1);
michael@0 282 }
michael@0 283 if (label >= eom) {
michael@0 284 errno = EMSGSIZE;
michael@0 285 return (-1);
michael@0 286 }
michael@0 287 *label = c;
michael@0 288 /* Fully qualified ? */
michael@0 289 if (*src == '\0') {
michael@0 290 if (c != 0) {
michael@0 291 if (bp >= eom) {
michael@0 292 errno = EMSGSIZE;
michael@0 293 return (-1);
michael@0 294 }
michael@0 295 *bp++ = '\0';
michael@0 296 }
michael@0 297 if ((bp - dst) > MAXCDNAME) {
michael@0 298 errno = EMSGSIZE;
michael@0 299 return (-1);
michael@0 300 }
michael@0 301 return (1);
michael@0 302 }
michael@0 303 if (c == 0 || *src == '.') {
michael@0 304 errno = EMSGSIZE;
michael@0 305 return (-1);
michael@0 306 }
michael@0 307 label = bp++;
michael@0 308 continue;
michael@0 309 }
michael@0 310 if (bp >= eom) {
michael@0 311 errno = EMSGSIZE;
michael@0 312 return (-1);
michael@0 313 }
michael@0 314 *bp++ = (u_char)c;
michael@0 315 }
michael@0 316 c = (bp - label - 1);
michael@0 317 if ((c & NS_CMPRSFLGS) != 0) { /* Label too big. */
michael@0 318 errno = EMSGSIZE;
michael@0 319 return (-1);
michael@0 320 }
michael@0 321 done:
michael@0 322 if (label >= eom) {
michael@0 323 errno = EMSGSIZE;
michael@0 324 return (-1);
michael@0 325 }
michael@0 326 *label = c;
michael@0 327 if (c != 0) {
michael@0 328 if (bp >= eom) {
michael@0 329 errno = EMSGSIZE;
michael@0 330 return (-1);
michael@0 331 }
michael@0 332 *bp++ = 0;
michael@0 333 }
michael@0 334 if ((bp - dst) > MAXCDNAME) { /* src too big */
michael@0 335 errno = EMSGSIZE;
michael@0 336 return (-1);
michael@0 337 }
michael@0 338 return (0);
michael@0 339 }
michael@0 340
michael@0 341 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 342 /*
michael@0 343 * ns_name_ntol(src, dst, dstsiz)
michael@0 344 * Convert a network strings labels into all lowercase.
michael@0 345 * return:
michael@0 346 * Number of bytes written to buffer, or -1 (with errno set)
michael@0 347 * notes:
michael@0 348 * Enforces label and domain length limits.
michael@0 349 */
michael@0 350
michael@0 351 int
michael@0 352 ns_name_ntol(const u_char *src, u_char *dst, size_t dstsiz)
michael@0 353 {
michael@0 354 const u_char *cp;
michael@0 355 u_char *dn, *eom;
michael@0 356 u_char c;
michael@0 357 u_int n;
michael@0 358 int l;
michael@0 359
michael@0 360 cp = src;
michael@0 361 dn = dst;
michael@0 362 eom = dst + dstsiz;
michael@0 363
michael@0 364 if (dn >= eom) {
michael@0 365 errno = EMSGSIZE;
michael@0 366 return (-1);
michael@0 367 }
michael@0 368 while ((n = *cp++) != 0) {
michael@0 369 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
michael@0 370 /* Some kind of compression pointer. */
michael@0 371 errno = EMSGSIZE;
michael@0 372 return (-1);
michael@0 373 }
michael@0 374 *dn++ = n;
michael@0 375 if ((l = labellen(cp - 1)) < 0) {
michael@0 376 errno = EMSGSIZE;
michael@0 377 return (-1);
michael@0 378 }
michael@0 379 if (dn + l >= eom) {
michael@0 380 errno = EMSGSIZE;
michael@0 381 return (-1);
michael@0 382 }
michael@0 383 for (; l > 0; l--) {
michael@0 384 c = *cp++;
michael@0 385 if (isupper(c))
michael@0 386 *dn++ = tolower(c);
michael@0 387 else
michael@0 388 *dn++ = c;
michael@0 389 }
michael@0 390 }
michael@0 391 *dn++ = '\0';
michael@0 392 return (dn - dst);
michael@0 393 }
michael@0 394 #endif
michael@0 395
michael@0 396 /*
michael@0 397 * ns_name_unpack(msg, eom, src, dst, dstsiz)
michael@0 398 * Unpack a domain name from a message, source may be compressed.
michael@0 399 * return:
michael@0 400 * -1 if it fails, or consumed octets if it succeeds.
michael@0 401 */
michael@0 402 int
michael@0 403 ns_name_unpack(const u_char *msg, const u_char *eom, const u_char *src,
michael@0 404 u_char *dst, size_t dstsiz)
michael@0 405 {
michael@0 406 const u_char *srcp, *dstlim;
michael@0 407 u_char *dstp;
michael@0 408 int n, len, checked, l;
michael@0 409
michael@0 410 len = -1;
michael@0 411 checked = 0;
michael@0 412 dstp = dst;
michael@0 413 srcp = src;
michael@0 414 dstlim = dst + dstsiz;
michael@0 415 if (srcp < msg || srcp >= eom) {
michael@0 416 errno = EMSGSIZE;
michael@0 417 return (-1);
michael@0 418 }
michael@0 419 /* Fetch next label in domain name. */
michael@0 420 while ((n = *srcp++) != 0) {
michael@0 421 /* Check for indirection. */
michael@0 422 switch (n & NS_CMPRSFLGS) {
michael@0 423 case 0:
michael@0 424 case NS_TYPE_ELT:
michael@0 425 /* Limit checks. */
michael@0 426 if ((l = labellen(srcp - 1)) < 0) {
michael@0 427 errno = EMSGSIZE;
michael@0 428 return(-1);
michael@0 429 }
michael@0 430 if (dstp + l + 1 >= dstlim || srcp + l >= eom) {
michael@0 431 errno = EMSGSIZE;
michael@0 432 return (-1);
michael@0 433 }
michael@0 434 checked += l + 1;
michael@0 435 *dstp++ = n;
michael@0 436 memcpy(dstp, srcp, (size_t)l);
michael@0 437 dstp += l;
michael@0 438 srcp += l;
michael@0 439 break;
michael@0 440
michael@0 441 case NS_CMPRSFLGS:
michael@0 442 if (srcp >= eom) {
michael@0 443 errno = EMSGSIZE;
michael@0 444 return (-1);
michael@0 445 }
michael@0 446 if (len < 0)
michael@0 447 len = srcp - src + 1;
michael@0 448 srcp = msg + (((n & 0x3f) << 8) | (*srcp & 0xff));
michael@0 449 if (srcp < msg || srcp >= eom) { /* Out of range. */
michael@0 450 errno = EMSGSIZE;
michael@0 451 return (-1);
michael@0 452 }
michael@0 453 checked += 2;
michael@0 454 /*
michael@0 455 * Check for loops in the compressed name;
michael@0 456 * if we've looked at the whole message,
michael@0 457 * there must be a loop.
michael@0 458 */
michael@0 459 if (checked >= eom - msg) {
michael@0 460 errno = EMSGSIZE;
michael@0 461 return (-1);
michael@0 462 }
michael@0 463 break;
michael@0 464
michael@0 465 default:
michael@0 466 errno = EMSGSIZE;
michael@0 467 return (-1); /* flag error */
michael@0 468 }
michael@0 469 }
michael@0 470 *dstp = '\0';
michael@0 471 if (len < 0)
michael@0 472 len = srcp - src;
michael@0 473 return (len);
michael@0 474 }
michael@0 475
michael@0 476 /*
michael@0 477 * ns_name_pack(src, dst, dstsiz, dnptrs, lastdnptr)
michael@0 478 * Pack domain name 'domain' into 'comp_dn'.
michael@0 479 * return:
michael@0 480 * Size of the compressed name, or -1.
michael@0 481 * notes:
michael@0 482 * 'dnptrs' is an array of pointers to previous compressed names.
michael@0 483 * dnptrs[0] is a pointer to the beginning of the message. The array
michael@0 484 * ends with NULL.
michael@0 485 * 'lastdnptr' is a pointer to the end of the array pointed to
michael@0 486 * by 'dnptrs'.
michael@0 487 * Side effects:
michael@0 488 * The list of pointers in dnptrs is updated for labels inserted into
michael@0 489 * the message as we compress the name. If 'dnptr' is NULL, we don't
michael@0 490 * try to compress names. If 'lastdnptr' is NULL, we don't update the
michael@0 491 * list.
michael@0 492 */
michael@0 493 int
michael@0 494 ns_name_pack(const u_char *src, u_char *dst, int dstsiz,
michael@0 495 const u_char **dnptrs, const u_char **lastdnptr)
michael@0 496 {
michael@0 497 u_char *dstp;
michael@0 498 const u_char **cpp, **lpp, *eob, *msg;
michael@0 499 const u_char *srcp;
michael@0 500 int n, l, first = 1;
michael@0 501
michael@0 502 srcp = src;
michael@0 503 dstp = dst;
michael@0 504 eob = dstp + dstsiz;
michael@0 505 lpp = cpp = NULL;
michael@0 506 if (dnptrs != NULL) {
michael@0 507 if ((msg = *dnptrs++) != NULL) {
michael@0 508 for (cpp = dnptrs; *cpp != NULL; cpp++)
michael@0 509 ;
michael@0 510 lpp = cpp; /* end of list to search */
michael@0 511 }
michael@0 512 } else
michael@0 513 msg = NULL;
michael@0 514
michael@0 515 /* make sure the domain we are about to add is legal */
michael@0 516 l = 0;
michael@0 517 do {
michael@0 518 int l0;
michael@0 519
michael@0 520 n = *srcp;
michael@0 521 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
michael@0 522 errno = EMSGSIZE;
michael@0 523 return (-1);
michael@0 524 }
michael@0 525 if ((l0 = labellen(srcp)) < 0) {
michael@0 526 errno = EINVAL;
michael@0 527 return(-1);
michael@0 528 }
michael@0 529 l += l0 + 1;
michael@0 530 if (l > MAXCDNAME) {
michael@0 531 errno = EMSGSIZE;
michael@0 532 return (-1);
michael@0 533 }
michael@0 534 srcp += l0 + 1;
michael@0 535 } while (n != 0);
michael@0 536
michael@0 537 /* from here on we need to reset compression pointer array on error */
michael@0 538 srcp = src;
michael@0 539 do {
michael@0 540 /* Look to see if we can use pointers. */
michael@0 541 n = *srcp;
michael@0 542 if (n != 0 && msg != NULL) {
michael@0 543 l = dn_find(srcp, msg, (const u_char * const *)dnptrs,
michael@0 544 (const u_char * const *)lpp);
michael@0 545 if (l >= 0) {
michael@0 546 if (dstp + 1 >= eob) {
michael@0 547 goto cleanup;
michael@0 548 }
michael@0 549 *dstp++ = ((u_int32_t)l >> 8) | NS_CMPRSFLGS;
michael@0 550 *dstp++ = l % 256;
michael@0 551 return (dstp - dst);
michael@0 552 }
michael@0 553 /* Not found, save it. */
michael@0 554 if (lastdnptr != NULL && cpp < lastdnptr - 1 &&
michael@0 555 (dstp - msg) < 0x4000 && first) {
michael@0 556 *cpp++ = dstp;
michael@0 557 *cpp = NULL;
michael@0 558 first = 0;
michael@0 559 }
michael@0 560 }
michael@0 561 /* copy label to buffer */
michael@0 562 if ((n & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
michael@0 563 /* Should not happen. */
michael@0 564 goto cleanup;
michael@0 565 }
michael@0 566 n = labellen(srcp);
michael@0 567 if (dstp + 1 + n >= eob) {
michael@0 568 goto cleanup;
michael@0 569 }
michael@0 570 memcpy(dstp, srcp, (size_t)(n + 1));
michael@0 571 srcp += n + 1;
michael@0 572 dstp += n + 1;
michael@0 573 } while (n != 0);
michael@0 574
michael@0 575 if (dstp > eob) {
michael@0 576 cleanup:
michael@0 577 if (msg != NULL)
michael@0 578 *lpp = NULL;
michael@0 579 errno = EMSGSIZE;
michael@0 580 return (-1);
michael@0 581 }
michael@0 582 return (dstp - dst);
michael@0 583 }
michael@0 584
michael@0 585 /*
michael@0 586 * ns_name_uncompress(msg, eom, src, dst, dstsiz)
michael@0 587 * Expand compressed domain name to presentation format.
michael@0 588 * return:
michael@0 589 * Number of bytes read out of `src', or -1 (with errno set).
michael@0 590 * note:
michael@0 591 * Root domain returns as "." not "".
michael@0 592 */
michael@0 593 int
michael@0 594 ns_name_uncompress(const u_char *msg, const u_char *eom, const u_char *src,
michael@0 595 char *dst, size_t dstsiz)
michael@0 596 {
michael@0 597 u_char tmp[NS_MAXCDNAME];
michael@0 598 int n;
michael@0 599
michael@0 600 if ((n = ns_name_unpack(msg, eom, src, tmp, sizeof tmp)) == -1)
michael@0 601 return (-1);
michael@0 602 if (ns_name_ntop(tmp, dst, dstsiz) == -1)
michael@0 603 return (-1);
michael@0 604 return (n);
michael@0 605 }
michael@0 606
michael@0 607 /*
michael@0 608 * ns_name_compress(src, dst, dstsiz, dnptrs, lastdnptr)
michael@0 609 * Compress a domain name into wire format, using compression pointers.
michael@0 610 * return:
michael@0 611 * Number of bytes consumed in `dst' or -1 (with errno set).
michael@0 612 * notes:
michael@0 613 * 'dnptrs' is an array of pointers to previous compressed names.
michael@0 614 * dnptrs[0] is a pointer to the beginning of the message.
michael@0 615 * The list ends with NULL. 'lastdnptr' is a pointer to the end of the
michael@0 616 * array pointed to by 'dnptrs'. Side effect is to update the list of
michael@0 617 * pointers for labels inserted into the message as we compress the name.
michael@0 618 * If 'dnptr' is NULL, we don't try to compress names. If 'lastdnptr'
michael@0 619 * is NULL, we don't update the list.
michael@0 620 */
michael@0 621 int
michael@0 622 ns_name_compress(const char *src, u_char *dst, size_t dstsiz,
michael@0 623 const u_char **dnptrs, const u_char **lastdnptr)
michael@0 624 {
michael@0 625 u_char tmp[NS_MAXCDNAME];
michael@0 626
michael@0 627 if (ns_name_pton(src, tmp, sizeof tmp) == -1)
michael@0 628 return (-1);
michael@0 629 return (ns_name_pack(tmp, dst, (int)dstsiz, dnptrs, lastdnptr));
michael@0 630 }
michael@0 631
michael@0 632 #ifndef MOZILLA_NECKO_EXCLUDE_CODE
michael@0 633 /*
michael@0 634 * Reset dnptrs so that there are no active references to pointers at or
michael@0 635 * after src.
michael@0 636 */
michael@0 637 void
michael@0 638 ns_name_rollback(const u_char *src, const u_char **dnptrs,
michael@0 639 const u_char **lastdnptr)
michael@0 640 {
michael@0 641 while (dnptrs < lastdnptr && *dnptrs != NULL) {
michael@0 642 if (*dnptrs >= src) {
michael@0 643 *dnptrs = NULL;
michael@0 644 break;
michael@0 645 }
michael@0 646 dnptrs++;
michael@0 647 }
michael@0 648 }
michael@0 649 #endif
michael@0 650
michael@0 651 /*
michael@0 652 * ns_name_skip(ptrptr, eom)
michael@0 653 * Advance *ptrptr to skip over the compressed name it points at.
michael@0 654 * return:
michael@0 655 * 0 on success, -1 (with errno set) on failure.
michael@0 656 */
michael@0 657 int
michael@0 658 ns_name_skip(const u_char **ptrptr, const u_char *eom)
michael@0 659 {
michael@0 660 const u_char *cp;
michael@0 661 u_int n;
michael@0 662 int l;
michael@0 663
michael@0 664 cp = *ptrptr;
michael@0 665 while (cp < eom && (n = *cp++) != 0) {
michael@0 666 /* Check for indirection. */
michael@0 667 switch (n & NS_CMPRSFLGS) {
michael@0 668 case 0: /* normal case, n == len */
michael@0 669 cp += n;
michael@0 670 continue;
michael@0 671 case NS_TYPE_ELT: /* EDNS0 extended label */
michael@0 672 if ((l = labellen(cp - 1)) < 0) {
michael@0 673 errno = EMSGSIZE; /* XXX */
michael@0 674 return(-1);
michael@0 675 }
michael@0 676 cp += l;
michael@0 677 continue;
michael@0 678 case NS_CMPRSFLGS: /* indirection */
michael@0 679 cp++;
michael@0 680 break;
michael@0 681 default: /* illegal type */
michael@0 682 errno = EMSGSIZE;
michael@0 683 return (-1);
michael@0 684 }
michael@0 685 break;
michael@0 686 }
michael@0 687 if (cp > eom) {
michael@0 688 errno = EMSGSIZE;
michael@0 689 return (-1);
michael@0 690 }
michael@0 691 *ptrptr = cp;
michael@0 692 return (0);
michael@0 693 }
michael@0 694
michael@0 695 /* Private. */
michael@0 696
michael@0 697 /*
michael@0 698 * special(ch)
michael@0 699 * Thinking in noninternationalized USASCII (per the DNS spec),
michael@0 700 * is this characted special ("in need of quoting") ?
michael@0 701 * return:
michael@0 702 * boolean.
michael@0 703 */
michael@0 704 static int
michael@0 705 special(int ch) {
michael@0 706 switch (ch) {
michael@0 707 case 0x22: /* '"' */
michael@0 708 case 0x2E: /* '.' */
michael@0 709 case 0x3B: /* ';' */
michael@0 710 case 0x5C: /* '\\' */
michael@0 711 case 0x28: /* '(' */
michael@0 712 case 0x29: /* ')' */
michael@0 713 /* Special modifiers in zone files. */
michael@0 714 case 0x40: /* '@' */
michael@0 715 case 0x24: /* '$' */
michael@0 716 return (1);
michael@0 717 default:
michael@0 718 return (0);
michael@0 719 }
michael@0 720 }
michael@0 721
michael@0 722 /*
michael@0 723 * printable(ch)
michael@0 724 * Thinking in noninternationalized USASCII (per the DNS spec),
michael@0 725 * is this character visible and not a space when printed ?
michael@0 726 * return:
michael@0 727 * boolean.
michael@0 728 */
michael@0 729 static int
michael@0 730 printable(int ch) {
michael@0 731 return (ch > 0x20 && ch < 0x7f);
michael@0 732 }
michael@0 733
michael@0 734 /*
michael@0 735 * Thinking in noninternationalized USASCII (per the DNS spec),
michael@0 736 * convert this character to lower case if it's upper case.
michael@0 737 */
michael@0 738 static int
michael@0 739 mklower(int ch) {
michael@0 740 if (ch >= 0x41 && ch <= 0x5A)
michael@0 741 return (ch + 0x20);
michael@0 742 return (ch);
michael@0 743 }
michael@0 744
michael@0 745 /*
michael@0 746 * dn_find(domain, msg, dnptrs, lastdnptr)
michael@0 747 * Search for the counted-label name in an array of compressed names.
michael@0 748 * return:
michael@0 749 * offset from msg if found, or -1.
michael@0 750 * notes:
michael@0 751 * dnptrs is the pointer to the first name on the list,
michael@0 752 * not the pointer to the start of the message.
michael@0 753 */
michael@0 754 static int
michael@0 755 dn_find(const u_char *domain, const u_char *msg,
michael@0 756 const u_char * const *dnptrs,
michael@0 757 const u_char * const *lastdnptr)
michael@0 758 {
michael@0 759 const u_char *dn, *cp, *sp;
michael@0 760 const u_char * const *cpp;
michael@0 761 u_int n;
michael@0 762
michael@0 763 for (cpp = dnptrs; cpp < lastdnptr; cpp++) {
michael@0 764 sp = *cpp;
michael@0 765 /*
michael@0 766 * terminate search on:
michael@0 767 * root label
michael@0 768 * compression pointer
michael@0 769 * unusable offset
michael@0 770 */
michael@0 771 while (*sp != 0 && (*sp & NS_CMPRSFLGS) == 0 &&
michael@0 772 (sp - msg) < 0x4000) {
michael@0 773 dn = domain;
michael@0 774 cp = sp;
michael@0 775 while ((n = *cp++) != 0) {
michael@0 776 /*
michael@0 777 * check for indirection
michael@0 778 */
michael@0 779 switch (n & NS_CMPRSFLGS) {
michael@0 780 case 0: /* normal case, n == len */
michael@0 781 n = labellen(cp - 1); /* XXX */
michael@0 782
michael@0 783 if (n != *dn++)
michael@0 784 goto next;
michael@0 785
michael@0 786 for (; n > 0; n--)
michael@0 787 if (mklower(*dn++) !=
michael@0 788 mklower(*cp++))
michael@0 789 goto next;
michael@0 790 /* Is next root for both ? */
michael@0 791 if (*dn == '\0' && *cp == '\0')
michael@0 792 return (sp - msg);
michael@0 793 if (*dn)
michael@0 794 continue;
michael@0 795 goto next;
michael@0 796 case NS_CMPRSFLGS: /* indirection */
michael@0 797 cp = msg + (((n & 0x3f) << 8) | *cp);
michael@0 798 break;
michael@0 799
michael@0 800 default: /* illegal type */
michael@0 801 errno = EMSGSIZE;
michael@0 802 return (-1);
michael@0 803 }
michael@0 804 }
michael@0 805 next: ;
michael@0 806 sp += *sp + 1;
michael@0 807 }
michael@0 808 }
michael@0 809 errno = ENOENT;
michael@0 810 return (-1);
michael@0 811 }
michael@0 812
michael@0 813 static int
michael@0 814 decode_bitstring(const unsigned char **cpp, char *dn, const char *eom)
michael@0 815 {
michael@0 816 const unsigned char *cp = *cpp;
michael@0 817 char *beg = dn, tc;
michael@0 818 int b, blen, plen, i;
michael@0 819
michael@0 820 if ((blen = (*cp & 0xff)) == 0)
michael@0 821 blen = 256;
michael@0 822 plen = (blen + 3) / 4;
michael@0 823 plen += sizeof("\\[x/]") + (blen > 99 ? 3 : (blen > 9) ? 2 : 1);
michael@0 824 if (dn + plen >= eom)
michael@0 825 return(-1);
michael@0 826
michael@0 827 cp++;
michael@0 828 i = SPRINTF((dn, "\\[x"));
michael@0 829 if (i < 0)
michael@0 830 return (-1);
michael@0 831 dn += i;
michael@0 832 for (b = blen; b > 7; b -= 8, cp++) {
michael@0 833 i = SPRINTF((dn, "%02x", *cp & 0xff));
michael@0 834 if (i < 0)
michael@0 835 return (-1);
michael@0 836 dn += i;
michael@0 837 }
michael@0 838 if (b > 4) {
michael@0 839 tc = *cp++;
michael@0 840 i = SPRINTF((dn, "%02x", tc & (0xff << (8 - b))));
michael@0 841 if (i < 0)
michael@0 842 return (-1);
michael@0 843 dn += i;
michael@0 844 } else if (b > 0) {
michael@0 845 tc = *cp++;
michael@0 846 i = SPRINTF((dn, "%1x",
michael@0 847 (((u_int32_t)tc >> 4) & 0x0f) & (0x0f << (4 - b))));
michael@0 848 if (i < 0)
michael@0 849 return (-1);
michael@0 850 dn += i;
michael@0 851 }
michael@0 852 i = SPRINTF((dn, "/%d]", blen));
michael@0 853 if (i < 0)
michael@0 854 return (-1);
michael@0 855 dn += i;
michael@0 856
michael@0 857 *cpp = cp;
michael@0 858 return(dn - beg);
michael@0 859 }
michael@0 860
michael@0 861 static int
michael@0 862 encode_bitsring(const char **bp, const char *end, unsigned char **labelp,
michael@0 863 unsigned char ** dst, unsigned const char *eom)
michael@0 864 {
michael@0 865 int afterslash = 0;
michael@0 866 const char *cp = *bp;
michael@0 867 unsigned char *tp;
michael@0 868 char c;
michael@0 869 const char *beg_blen;
michael@0 870 char *end_blen = NULL;
michael@0 871 int value = 0, count = 0, tbcount = 0, blen = 0;
michael@0 872
michael@0 873 beg_blen = end_blen = NULL;
michael@0 874
michael@0 875 /* a bitstring must contain at least 2 characters */
michael@0 876 if (end - cp < 2)
michael@0 877 return(EINVAL);
michael@0 878
michael@0 879 /* XXX: currently, only hex strings are supported */
michael@0 880 if (*cp++ != 'x')
michael@0 881 return(EINVAL);
michael@0 882 if (!isxdigit((*cp) & 0xff)) /* reject '\[x/BLEN]' */
michael@0 883 return(EINVAL);
michael@0 884
michael@0 885 for (tp = *dst + 1; cp < end && tp < eom; cp++) {
michael@0 886 switch((c = *cp)) {
michael@0 887 case ']': /* end of the bitstring */
michael@0 888 if (afterslash) {
michael@0 889 if (beg_blen == NULL)
michael@0 890 return(EINVAL);
michael@0 891 blen = (int)strtol(beg_blen, &end_blen, 10);
michael@0 892 if (*end_blen != ']')
michael@0 893 return(EINVAL);
michael@0 894 }
michael@0 895 if (count)
michael@0 896 *tp++ = ((value << 4) & 0xff);
michael@0 897 cp++; /* skip ']' */
michael@0 898 goto done;
michael@0 899 case '/':
michael@0 900 afterslash = 1;
michael@0 901 break;
michael@0 902 default:
michael@0 903 if (afterslash) {
michael@0 904 if (!isdigit(c&0xff))
michael@0 905 return(EINVAL);
michael@0 906 if (beg_blen == NULL) {
michael@0 907
michael@0 908 if (c == '0') {
michael@0 909 /* blen never begings with 0 */
michael@0 910 return(EINVAL);
michael@0 911 }
michael@0 912 beg_blen = cp;
michael@0 913 }
michael@0 914 } else {
michael@0 915 if (!isxdigit(c&0xff))
michael@0 916 return(EINVAL);
michael@0 917 value <<= 4;
michael@0 918 value += digitvalue[(int)c];
michael@0 919 count += 4;
michael@0 920 tbcount += 4;
michael@0 921 if (tbcount > 256)
michael@0 922 return(EINVAL);
michael@0 923 if (count == 8) {
michael@0 924 *tp++ = value;
michael@0 925 count = 0;
michael@0 926 }
michael@0 927 }
michael@0 928 break;
michael@0 929 }
michael@0 930 }
michael@0 931 done:
michael@0 932 if (cp >= end || tp >= eom)
michael@0 933 return(EMSGSIZE);
michael@0 934
michael@0 935 /*
michael@0 936 * bit length validation:
michael@0 937 * If a <length> is present, the number of digits in the <bit-data>
michael@0 938 * MUST be just sufficient to contain the number of bits specified
michael@0 939 * by the <length>. If there are insignificant bits in a final
michael@0 940 * hexadecimal or octal digit, they MUST be zero.
michael@0 941 * RFC 2673, Section 3.2.
michael@0 942 */
michael@0 943 if (blen > 0) {
michael@0 944 int traillen;
michael@0 945
michael@0 946 if (((blen + 3) & ~3) != tbcount)
michael@0 947 return(EINVAL);
michael@0 948 traillen = tbcount - blen; /* between 0 and 3 */
michael@0 949 if (((value << (8 - traillen)) & 0xff) != 0)
michael@0 950 return(EINVAL);
michael@0 951 }
michael@0 952 else
michael@0 953 blen = tbcount;
michael@0 954 if (blen == 256)
michael@0 955 blen = 0;
michael@0 956
michael@0 957 /* encode the type and the significant bit fields */
michael@0 958 **labelp = DNS_LABELTYPE_BITSTRING;
michael@0 959 **dst = blen;
michael@0 960
michael@0 961 *bp = cp;
michael@0 962 *dst = tp;
michael@0 963
michael@0 964 return(0);
michael@0 965 }
michael@0 966
michael@0 967 static int
michael@0 968 labellen(const u_char *lp)
michael@0 969 {
michael@0 970 int bitlen;
michael@0 971 u_char l = *lp;
michael@0 972
michael@0 973 if ((l & NS_CMPRSFLGS) == NS_CMPRSFLGS) {
michael@0 974 /* should be avoided by the caller */
michael@0 975 return(-1);
michael@0 976 }
michael@0 977
michael@0 978 if ((l & NS_CMPRSFLGS) == NS_TYPE_ELT) {
michael@0 979 if (l == DNS_LABELTYPE_BITSTRING) {
michael@0 980 if ((bitlen = *(lp + 1)) == 0)
michael@0 981 bitlen = 256;
michael@0 982 return((bitlen + 7 ) / 8 + 1);
michael@0 983 }
michael@0 984 return(-1); /* unknwon ELT */
michael@0 985 }
michael@0 986 return(l);
michael@0 987 }

mercurial