1 /* 2 * -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- 3 * 4 * The contents of this file are subject to the Netscape Public License 5 * Version 1.0 (the "NPL"); you may not use this file except in 6 * compliance with the NPL. You may obtain a copy of the NPL at 7 * http://www.mozilla.org/NPL/ 8 * 9 * Software distributed under the NPL is distributed on an "AS IS" basis, 10 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL 11 * for the specific language governing rights and limitations under the 12 * NPL. 13 * 14 * The Initial Developer of this code under the NPL is Netscape 15 * Communications Corporation. Portions created by Netscape are 16 * Copyright (C) 1998 Netscape Communications Corporation. All Rights 17 * Reserved. 18 */ 19 20 /* 21 * Copyright (c) 1990 Regents of the University of Michigan. 22 * All rights reserved. 23 * 24 * Redistribution and use in source and binary forms are permitted 25 * provided that this notice is preserved and that due credit is given 26 * to the University of Michigan at Ann Arbor. The name of the University 27 * may not be used to endorse or promote products derived from this 28 * software without specific prior written permission. This software 29 * is provided ``as is'' without express or implied warranty. 30 */ 31 32 /* 33 * Copyright 2007 Sun Microsystems, Inc. All rights reserved. 34 * Use is subject to license terms. 35 */ 36 37 #pragma ident "%Z%%M% %I% %E% SMI" 38 39 #include <sys/types.h> 40 #include <netinet/in.h> 41 #include <inttypes.h> 42 43 #include <ber_der.h> 44 #include "kmfber_int.h" 45 46 /* the following constants are used in kmfber_calc_lenlen */ 47 48 #define LENMASK1 0xFF 49 #define LENMASK2 0xFFFF 50 #define LENMASK3 0xFFFFFF 51 #define LENMASK4 0xFFFFFFFF 52 #define _MASK 0x80 53 54 int 55 kmfber_calc_taglen(ber_tag_t tag) 56 { 57 int i; 58 ber_int_t mask; 59 60 /* find the first non-all-zero byte in the tag */ 61 for (i = sizeof (ber_int_t) - 1; i > 0; i--) { 62 mask = (LENMASK3 << (i * 8)); 63 /* not all zero */ 64 if (tag & mask) 65 break; 66 } 67 68 return (i + 1); 69 } 70 71 static int 72 ber_put_tag(BerElement *ber, ber_tag_t tag, int nosos) 73 { 74 ber_int_t taglen; 75 ber_tag_t ntag; 76 77 taglen = kmfber_calc_taglen(tag); 78 79 ntag = htonl(tag); 80 81 return (kmfber_write(ber, 82 ((char *) &ntag) + sizeof (ber_int_t) - taglen, 83 taglen, nosos)); 84 } 85 86 int 87 kmfber_calc_lenlen(ber_int_t len) 88 { 89 /* 90 * short len if it's less than 128 - one byte giving the len, 91 * with bit 8 0. 92 */ 93 94 if (len <= 0x7F) 95 return (1); 96 97 /* 98 * long len otherwise - one byte with bit 8 set, giving the 99 * length of the length, followed by the length itself. 100 */ 101 102 if (len <= LENMASK1) 103 return (2); 104 if (len <= LENMASK2) 105 return (3); 106 if (len <= LENMASK3) 107 return (4); 108 109 return (5); 110 } 111 112 int 113 kmfber_put_len(BerElement *ber, ber_int_t len, int nosos) 114 { 115 int i; 116 char lenlen; 117 ber_int_t mask, netlen; 118 119 /* 120 * short len if it's less than 128 - one byte giving the len, 121 * with bit 8 0. 122 */ 123 if (len <= 127) { 124 netlen = htonl(len); 125 return (kmfber_write(ber, 126 (char *)&netlen + sizeof (ber_int_t) - 1, 127 1, nosos)); 128 } 129 130 /* 131 * long len otherwise - one byte with bit 8 set, giving the 132 * length of the length, followed by the length itself. 133 */ 134 135 /* find the first non-all-zero byte */ 136 for (i = sizeof (ber_int_t) - 1; i > 0; i--) { 137 mask = (LENMASK1 << (i * 8)); 138 /* not all zero */ 139 if (len & mask) 140 break; 141 } 142 lenlen = ++i; 143 if (lenlen > 4) 144 return (-1); 145 lenlen |= 0x80; 146 147 /* write the length of the length */ 148 if (kmfber_write(ber, &lenlen, 1, nosos) != 1) 149 return (-1); 150 151 /* write the length itself */ 152 netlen = htonl(len); 153 if (kmfber_write(ber, 154 (char *) &netlen + (sizeof (ber_int_t) - i), i, nosos) != i) 155 return (-1); 156 157 return (i + 1); 158 } 159 160 static int 161 ber_put_int_or_enum(BerElement *ber, ber_int_t num, ber_tag_t tag) 162 { 163 int i, sign; 164 ber_int_t len, lenlen, taglen, netnum, mask; 165 166 sign = (num < 0); 167 168 /* 169 * high bit is set - look for first non-all-one byte 170 * high bit is clear - look for first non-all-zero byte 171 */ 172 for (i = sizeof (ber_int_t) - 1; i > 0; i--) { 173 mask = (LENMASK1 << (i * 8)); 174 175 if (sign) { 176 /* not all ones */ 177 if ((num & mask) != mask) 178 break; 179 } else { 180 /* not all zero */ 181 if (num & mask) 182 break; 183 } 184 } 185 186 /* 187 * we now have the "leading byte". if the high bit on this 188 * byte matches the sign bit, we need to "back up" a byte. 189 */ 190 mask = (num & (_MASK << (i * 8))); 191 if ((mask && !sign) || (sign && !mask)) 192 i++; 193 194 len = i + 1; 195 196 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 197 return (-1); 198 199 if ((lenlen = kmfber_put_len(ber, len, 0)) == -1) 200 return (-1); 201 i++; 202 netnum = htonl(num); 203 if (kmfber_write(ber, 204 (char *) &netnum + (sizeof (ber_int_t) - i), i, 0) == i) 205 /* length of tag + length + contents */ 206 return (taglen + lenlen + i); 207 208 return (-1); 209 } 210 211 static int 212 kmfber_put_enum(BerElement *ber, ber_int_t num, ber_tag_t tag) 213 { 214 if (tag == KMFBER_DEFAULT) 215 tag = BER_ENUMERATED; 216 217 return (ber_put_int_or_enum(ber, num, tag)); 218 } 219 220 int 221 ber_put_int(BerElement *ber, ber_int_t num, ber_tag_t tag) 222 { 223 if (tag == KMFBER_DEFAULT) 224 tag = BER_INTEGER; 225 226 return (ber_put_int_or_enum(ber, num, tag)); 227 } 228 229 int 230 ber_put_oid(BerElement *ber, struct berval *oid, ber_tag_t tag) 231 { 232 ber_int_t taglen, lenlen, rc, len; 233 234 if (tag == KMFBER_DEFAULT) 235 tag = 0x06; /* TODO: Add new OID constant to header */ 236 237 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 238 return (-1); 239 240 len = (ber_int_t)oid->bv_len; 241 if ((lenlen = kmfber_put_len(ber, len, 0)) == -1 || 242 kmfber_write(ber, oid->bv_val, oid->bv_len, 0) != 243 (ber_int_t)oid->bv_len) { 244 rc = -1; 245 } else { 246 /* return length of tag + length + contents */ 247 rc = taglen + lenlen + oid->bv_len; 248 } 249 return (rc); 250 } 251 252 int 253 ber_put_big_int(BerElement *ber, ber_tag_t tag, char *data, 254 ber_len_t len) 255 { 256 ber_int_t taglen, lenlen, ilen, rc; 257 char zero = 0x00; 258 259 if (tag == KMFBER_DEFAULT) 260 tag = BER_INTEGER; 261 262 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 263 return (-1); 264 265 /* Add a leading 0 if the high order bit is set */ 266 if (data[0] & 0x80) 267 len++; 268 269 ilen = (ber_int_t)len; 270 if ((lenlen = kmfber_put_len(ber, ilen, 0)) == -1) 271 return (-1); 272 273 /* add leading 0 if hi bit set */ 274 if ((data[0] & 0x80) && kmfber_write(ber, &zero, 1, 0) != 1) 275 return (-1); 276 277 /* Adjust the length of the write if hi-order bit is set */ 278 if (data[0] & 0x80) 279 ilen = len - 1; 280 if (kmfber_write(ber, data, ilen, 0) != (ber_int_t)ilen) { 281 return (-1); 282 } else { 283 /* return length of tag + length + contents */ 284 rc = taglen + lenlen + len; 285 } 286 return (rc); 287 } 288 289 static int 290 kmfber_put_ostring(BerElement *ber, char *str, ber_len_t len, 291 ber_tag_t tag) 292 { 293 ber_int_t taglen, lenlen, ilen, rc; 294 #ifdef STR_TRANSLATION 295 int free_str; 296 #endif /* STR_TRANSLATION */ 297 298 if (tag == KMFBER_DEFAULT) 299 tag = BER_OCTET_STRING; 300 301 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 302 return (-1); 303 304 #ifdef STR_TRANSLATION 305 if (len > 0 && (ber->ber_options & KMFBER_OPT_TRANSLATE_STRINGS) != 0 && 306 ber->ber_encode_translate_proc != NULL) { 307 if ((*(ber->ber_encode_translate_proc))(&str, &len, 0) 308 != 0) { 309 return (-1); 310 } 311 free_str = 1; 312 } else { 313 free_str = 0; 314 } 315 #endif /* STR_TRANSLATION */ 316 317 /* 318 * Note: below is a spot where we limit ber_write 319 * to signed long (instead of unsigned long) 320 */ 321 ilen = (ber_int_t)len; 322 if ((lenlen = kmfber_put_len(ber, ilen, 0)) == -1 || 323 kmfber_write(ber, str, len, 0) != (ber_int_t)len) { 324 rc = -1; 325 } else { 326 /* return length of tag + length + contents */ 327 rc = taglen + lenlen + len; 328 } 329 330 #ifdef STR_TRANSLATION 331 if (free_str) { 332 free(str); 333 } 334 #endif /* STR_TRANSLATION */ 335 336 return (rc); 337 } 338 339 static int 340 kmfber_put_string(BerElement *ber, char *str, ber_tag_t tag) 341 { 342 return (kmfber_put_ostring(ber, str, (ber_len_t)strlen(str), tag)); 343 } 344 345 static int 346 kmfber_put_bitstring(BerElement *ber, char *str, 347 ber_len_t blen /* in bits */, ber_tag_t tag) 348 { 349 ber_int_t taglen, lenlen, len; 350 unsigned char unusedbits; 351 352 if (tag == KMFBER_DEFAULT) 353 tag = BER_BIT_STRING; 354 355 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 356 return (-1); 357 358 len = (blen + 7) / 8; 359 unusedbits = (unsigned char) (len * 8 - blen); 360 if ((lenlen = kmfber_put_len(ber, len + 1, 0)) == -1) 361 return (-1); 362 363 if (kmfber_write(ber, (char *)&unusedbits, 1, 0) != 1) 364 return (-1); 365 366 if (kmfber_write(ber, str, len, 0) != len) 367 return (-1); 368 369 /* return length of tag + length + unused bit count + contents */ 370 return (taglen + 1 + lenlen + len); 371 } 372 373 static int 374 kmfber_put_null(BerElement *ber, ber_tag_t tag) 375 { 376 int taglen; 377 378 if (tag == KMFBER_DEFAULT) 379 tag = BER_NULL; 380 381 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 382 return (-1); 383 384 if (kmfber_put_len(ber, 0, 0) != 1) 385 return (-1); 386 387 return (taglen + 1); 388 } 389 390 static int 391 kmfber_put_boolean(BerElement *ber, int boolval, ber_tag_t tag) 392 { 393 int taglen; 394 unsigned char trueval = 0xff; 395 unsigned char falseval = 0x00; 396 397 if (tag == KMFBER_DEFAULT) 398 tag = BER_BOOLEAN; 399 400 if ((taglen = ber_put_tag(ber, tag, 0)) == -1) 401 return (-1); 402 403 if (kmfber_put_len(ber, 1, 0) != 1) 404 return (-1); 405 406 if (kmfber_write(ber, (char *)(boolval ? &trueval : &falseval), 1, 0) 407 != 1) 408 return (-1); 409 410 return (taglen + 2); 411 } 412 413 #define FOUR_BYTE_LEN 5 414 415 416 /* 417 * The idea here is roughly this: we maintain a stack of these Seqorset 418 * structures. This is pushed when we see the beginning of a new set or 419 * sequence. It is popped when we see the end of a set or sequence. 420 * Since we don't want to malloc and free these structures all the time, 421 * we pre-allocate a small set of them within the ber element structure. 422 * thus we need to spot when we've overflowed this stack and fall back to 423 * malloc'ing instead. 424 */ 425 static int 426 ber_start_seqorset(BerElement *ber, ber_tag_t tag) 427 { 428 Seqorset *new_sos; 429 430 /* can we fit into the local stack ? */ 431 if (ber->ber_sos_stack_posn < SOS_STACK_SIZE) { 432 /* yes */ 433 new_sos = &ber->ber_sos_stack[ber->ber_sos_stack_posn]; 434 } else { 435 /* no */ 436 if ((new_sos = (Seqorset *)malloc(sizeof (Seqorset))) 437 == NULLSEQORSET) { 438 return (-1); 439 } 440 } 441 ber->ber_sos_stack_posn++; 442 443 if (ber->ber_sos == NULLSEQORSET) 444 new_sos->sos_first = ber->ber_ptr; 445 else 446 new_sos->sos_first = ber->ber_sos->sos_ptr; 447 448 /* Set aside room for a 4 byte length field */ 449 new_sos->sos_ptr = new_sos->sos_first + kmfber_calc_taglen(tag) + 450 FOUR_BYTE_LEN; 451 new_sos->sos_tag = tag; 452 453 new_sos->sos_next = ber->ber_sos; 454 new_sos->sos_clen = 0; 455 456 ber->ber_sos = new_sos; 457 if (ber->ber_sos->sos_ptr > ber->ber_end) { 458 (void) realloc(ber, ber->ber_sos->sos_ptr - ber->ber_end); 459 } 460 return (0); 461 } 462 463 static int 464 kmfber_start_seq(BerElement *ber, ber_tag_t tag) 465 { 466 if (tag == KMFBER_DEFAULT) 467 tag = BER_CONSTRUCTED_SEQUENCE; 468 469 return (ber_start_seqorset(ber, tag)); 470 } 471 472 static int 473 kmfber_start_set(BerElement *ber, ber_tag_t tag) 474 { 475 if (tag == KMFBER_DEFAULT) 476 tag = BER_CONSTRUCTED_SET; 477 478 return (ber_start_seqorset(ber, tag)); 479 } 480 481 static int 482 ber_put_seqorset(BerElement *ber) 483 { 484 ber_int_t netlen, len, taglen, lenlen; 485 unsigned char ltag = 0x80 + FOUR_BYTE_LEN - 1; 486 Seqorset *next; 487 Seqorset **sos = &ber->ber_sos; 488 489 /* 490 * If this is the toplevel sequence or set, we need to actually 491 * write the stuff out. Otherwise, it's already been put in 492 * the appropriate buffer and will be written when the toplevel 493 * one is written. In this case all we need to do is update the 494 * length and tag. 495 */ 496 497 len = (*sos)->sos_clen; 498 netlen = (ber_len_t)htonl(len); 499 500 if (ber->ber_options & KMFBER_OPT_USE_DER) { 501 lenlen = kmfber_calc_lenlen(len); 502 } else { 503 lenlen = FOUR_BYTE_LEN; 504 } 505 506 if ((next = (*sos)->sos_next) == NULLSEQORSET) { 507 /* write the tag */ 508 if ((taglen = ber_put_tag(ber, (*sos)->sos_tag, 1)) == -1) 509 return (-1); 510 511 if (ber->ber_options & KMFBER_OPT_USE_DER) { 512 /* Write the length in the minimum # of octets */ 513 if (kmfber_put_len(ber, len, 1) == -1) 514 return (-1); 515 516 if (lenlen != FOUR_BYTE_LEN) { 517 /* 518 * We set aside FOUR_BYTE_LEN bytes for 519 * the length field. Move the data if 520 * we don't actually need that much 521 */ 522 (void) memmove((*sos)->sos_first + taglen + 523 lenlen, (*sos)->sos_first + taglen + 524 FOUR_BYTE_LEN, len); 525 } 526 } else { 527 /* Fill FOUR_BYTE_LEN bytes for length field */ 528 /* one byte of length length */ 529 if (kmfber_write(ber, (char *)<ag, 1, 1) != 1) 530 return (-1); 531 532 /* the length itself */ 533 if (kmfber_write(ber, 534 (char *)&netlen + sizeof (ber_int_t) 535 - (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1, 1) != 536 FOUR_BYTE_LEN - 1) 537 return (-1); 538 } 539 /* The ber_ptr is at the set/seq start - move it to the end */ 540 ber->ber_ptr += len; 541 } else { 542 ber_tag_t ntag; 543 544 /* the tag */ 545 taglen = kmfber_calc_taglen((*sos)->sos_tag); 546 ntag = htonl((*sos)->sos_tag); 547 (void) memmove((*sos)->sos_first, (char *)&ntag + 548 sizeof (ber_int_t) - taglen, taglen); 549 550 if (ber->ber_options & KMFBER_OPT_USE_DER) { 551 ltag = (lenlen == 1) ? (unsigned char)len : 552 (unsigned char) (0x80 + (lenlen - 1)); 553 } 554 555 /* one byte of length length */ 556 (void) memmove((*sos)->sos_first + 1, <ag, 1); 557 558 if (ber->ber_options & KMFBER_OPT_USE_DER) { 559 if (lenlen > 1) { 560 /* Write the length itself */ 561 (void) memmove((*sos)->sos_first + 2, 562 (char *)&netlen + sizeof (ber_uint_t) - 563 (lenlen - 1), 564 lenlen - 1); 565 } 566 if (lenlen != FOUR_BYTE_LEN) { 567 /* 568 * We set aside FOUR_BYTE_LEN bytes for 569 * the length field. Move the data if 570 * we don't actually need that much 571 */ 572 (void) memmove((*sos)->sos_first + taglen + 573 lenlen, (*sos)->sos_first + taglen + 574 FOUR_BYTE_LEN, len); 575 } 576 } else { 577 /* the length itself */ 578 (void) memmove((*sos)->sos_first + taglen + 1, 579 (char *) &netlen + sizeof (ber_int_t) - 580 (FOUR_BYTE_LEN - 1), FOUR_BYTE_LEN - 1); 581 } 582 583 next->sos_clen += (taglen + lenlen + len); 584 next->sos_ptr += (taglen + lenlen + len); 585 } 586 587 /* we're done with this seqorset, so free it up */ 588 /* was this one from the local stack ? */ 589 if (ber->ber_sos_stack_posn > SOS_STACK_SIZE) { 590 free((char *)(*sos)); 591 } 592 ber->ber_sos_stack_posn--; 593 *sos = next; 594 595 return (taglen + lenlen + len); 596 } 597 598 /* VARARGS */ 599 int 600 kmfber_printf(BerElement *ber, const char *fmt, ...) 601 { 602 va_list ap; 603 char *s, **ss; 604 struct berval **bv, *oid; 605 int rc, i, t; 606 ber_int_t len; 607 608 va_start(ap, fmt); 609 610 #ifdef KMFBER_DEBUG 611 if (lber_debug & 64) { 612 char msg[80]; 613 sprintf(msg, "kmfber_printf fmt (%s)\n", fmt); 614 ber_err_print(msg); 615 } 616 #endif 617 618 for (rc = 0; *fmt && rc != -1; fmt++) { 619 switch (*fmt) { 620 case 'b': /* boolean */ 621 i = va_arg(ap, int); 622 rc = kmfber_put_boolean(ber, i, ber->ber_tag); 623 break; 624 625 case 'i': /* int */ 626 i = va_arg(ap, int); 627 rc = ber_put_int(ber, (ber_int_t)i, ber->ber_tag); 628 break; 629 630 case 'D': /* Object ID */ 631 if ((oid = va_arg(ap, struct berval *)) == NULL) 632 break; 633 rc = ber_put_oid(ber, oid, ber->ber_tag); 634 break; 635 case 'I': /* int */ 636 s = va_arg(ap, char *); 637 len = va_arg(ap, ber_int_t); 638 rc = ber_put_big_int(ber, ber->ber_tag, s, len); 639 break; 640 641 case 'e': /* enumeration */ 642 i = va_arg(ap, int); 643 rc = kmfber_put_enum(ber, (ber_int_t)i, ber->ber_tag); 644 break; 645 646 case 'l': 647 t = va_arg(ap, int); 648 rc = kmfber_put_len(ber, t, 0); 649 break; 650 case 'n': /* null */ 651 rc = kmfber_put_null(ber, ber->ber_tag); 652 break; 653 654 case 'o': /* octet string (non-null terminated) */ 655 s = va_arg(ap, char *); 656 len = va_arg(ap, int); 657 rc = kmfber_put_ostring(ber, s, len, ber->ber_tag); 658 break; 659 660 case 's': /* string */ 661 s = va_arg(ap, char *); 662 rc = kmfber_put_string(ber, s, ber->ber_tag); 663 break; 664 665 case 'B': /* bit string */ 666 s = va_arg(ap, char *); 667 len = va_arg(ap, int); /* in bits */ 668 rc = kmfber_put_bitstring(ber, s, len, ber->ber_tag); 669 break; 670 671 case 't': /* tag for the next element */ 672 ber->ber_tag = va_arg(ap, ber_tag_t); 673 ber->ber_usertag = 1; 674 break; 675 676 case 'T': /* Write an explicit tag, but don't change current */ 677 t = va_arg(ap, int); 678 rc = ber_put_tag(ber, t, 0); 679 break; 680 681 case 'v': /* vector of strings */ 682 if ((ss = va_arg(ap, char **)) == NULL) 683 break; 684 for (i = 0; ss[i] != NULL; i++) { 685 if ((rc = kmfber_put_string(ber, ss[i], 686 ber->ber_tag)) == -1) 687 break; 688 } 689 break; 690 691 case 'V': /* sequences of strings + lengths */ 692 if ((bv = va_arg(ap, struct berval **)) == NULL) 693 break; 694 for (i = 0; bv[i] != NULL; i++) { 695 if ((rc = kmfber_put_ostring(ber, bv[i]->bv_val, 696 bv[i]->bv_len, ber->ber_tag)) == -1) 697 break; 698 } 699 break; 700 701 case '{': /* begin sequence */ 702 rc = kmfber_start_seq(ber, ber->ber_tag); 703 break; 704 705 case '}': /* end sequence */ 706 rc = ber_put_seqorset(ber); 707 break; 708 709 case '[': /* begin set */ 710 rc = kmfber_start_set(ber, ber->ber_tag); 711 break; 712 713 case ']': /* end set */ 714 rc = ber_put_seqorset(ber); 715 break; 716 717 default: { 718 #ifdef KMFBER_DEBUG 719 char msg[80]; 720 sprintf(msg, "unknown fmt %c\n", *fmt); 721 ber_err_print(msg); 722 #endif 723 rc = -1; 724 break; 725 } 726 } 727 728 if (ber->ber_usertag == 0) 729 ber->ber_tag = KMFBER_DEFAULT; 730 else 731 ber->ber_usertag = 0; 732 } 733 734 va_end(ap); 735 736 return (rc); 737 } 738