1 /*- 2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are met: 6 * 7 * a) Redistributions of source code must retain the above copyright notice, 8 * this list of conditions and the following disclaimer. 9 * 10 * b) Redistributions in binary form must reproduce the above copyright 11 * notice, this list of conditions and the following disclaimer in 12 * the documentation and/or other materials provided with the distribution. 13 * 14 * c) Neither the name of Cisco Systems, Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived 16 * from this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 28 * THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include <sys/cdefs.h> 32 __FBSDID("$FreeBSD$"); 33 34 #include "opt_sctp.h" 35 #include <sys/param.h> 36 #include <sys/systm.h> 37 #include <sys/malloc.h> 38 #include <sys/mbuf.h> 39 #include <sys/domain.h> 40 #include <sys/protosw.h> 41 #include <sys/socket.h> 42 #include <sys/socketvar.h> 43 #include <sys/proc.h> 44 #include <sys/kernel.h> 45 #include <sys/sysctl.h> 46 47 #include <net/if.h> 48 #include <net/if_types.h> 49 #include <net/route.h> 50 #include <netinet/in.h> 51 #include <netinet/in_systm.h> 52 #include <netinet/ip.h> 53 #include <netinet/in_pcb.h> 54 #include <netinet/in_var.h> 55 #include <netinet/ip_var.h> 56 57 #include <netinet/sctp_os.h> 58 #include <netinet/sctp.h> 59 #include <netinet/sctp_header.h> 60 #include <netinet/sctp_pcb.h> 61 #include <netinet/sctp_var.h> 62 #include <netinet/sctputil.h> 63 #include <netinet/sctp_indata.h> 64 #include <netinet/sctp_output.h> 65 #include <netinet/sctp_auth.h> 66 67 #ifdef SCTP_DEBUG 68 extern uint32_t sctp_debug_on; 69 70 #define SCTP_AUTH_DEBUG (sctp_debug_on & SCTP_DEBUG_AUTH1) 71 #define SCTP_AUTH_DEBUG2 (sctp_debug_on & SCTP_DEBUG_AUTH2) 72 #endif /* SCTP_DEBUG */ 73 74 75 inline void 76 sctp_clear_chunklist(sctp_auth_chklist_t * chklist) 77 { 78 bzero(chklist, sizeof(*chklist)); 79 /* chklist->num_chunks = 0; */ 80 } 81 82 sctp_auth_chklist_t * 83 sctp_alloc_chunklist(void) 84 { 85 sctp_auth_chklist_t *chklist; 86 87 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist), 88 "AUTH chklist"); 89 if (chklist == NULL) { 90 #ifdef SCTP_DEBUG 91 if (sctp_debug_on & SCTP_AUTH_DEBUG) { 92 printf("sctp_alloc_chunklist: failed to get memory!\n"); 93 } 94 #endif /* SCTP_DEBUG */ 95 } else { 96 sctp_clear_chunklist(chklist); 97 } 98 return (chklist); 99 } 100 101 void 102 sctp_free_chunklist(sctp_auth_chklist_t * list) 103 { 104 if (list != NULL) 105 SCTP_FREE(list); 106 } 107 108 sctp_auth_chklist_t * 109 sctp_copy_chunklist(sctp_auth_chklist_t * list) 110 { 111 sctp_auth_chklist_t *new_list; 112 113 if (list == NULL) 114 return (NULL); 115 116 /* get a new list */ 117 new_list = sctp_alloc_chunklist(); 118 if (new_list == NULL) 119 return (NULL); 120 /* copy it */ 121 bcopy(list, new_list, sizeof(*new_list)); 122 123 return (new_list); 124 } 125 126 127 /* 128 * add a chunk to the required chunks list 129 */ 130 int 131 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list) 132 { 133 if (list == NULL) 134 return (-1); 135 136 /* is chunk restricted? */ 137 if ((chunk == SCTP_INITIATION) || 138 (chunk == SCTP_INITIATION_ACK) || 139 (chunk == SCTP_SHUTDOWN_COMPLETE) || 140 (chunk == SCTP_AUTHENTICATION)) { 141 return (-1); 142 } 143 if (list->chunks[chunk] == 0) { 144 list->chunks[chunk] = 1; 145 list->num_chunks++; 146 #ifdef SCTP_DEBUG 147 if (SCTP_AUTH_DEBUG) 148 printf("SCTP: added chunk %u (0x%02x) to Auth list\n", 149 chunk, chunk); 150 #endif 151 } 152 return (0); 153 } 154 155 /* 156 * delete a chunk from the required chunks list 157 */ 158 int 159 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list) 160 { 161 if (list == NULL) 162 return (-1); 163 164 /* is chunk restricted? */ 165 if ((chunk == SCTP_ASCONF) || 166 (chunk == SCTP_ASCONF_ACK)) { 167 return (-1); 168 } 169 if (list->chunks[chunk] == 1) { 170 list->chunks[chunk] = 0; 171 list->num_chunks--; 172 #ifdef SCTP_DEBUG 173 if (SCTP_AUTH_DEBUG) 174 printf("SCTP: deleted chunk %u (0x%02x) from Auth list\n", 175 chunk, chunk); 176 #endif 177 } 178 return (0); 179 } 180 181 inline int 182 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list) 183 { 184 if (list == NULL) 185 return (0); 186 else 187 return (list->num_chunks); 188 } 189 190 /* 191 * set the default list of chunks requiring AUTH 192 */ 193 void 194 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list) 195 { 196 sctp_auth_add_chunk(SCTP_ASCONF, list); 197 sctp_auth_add_chunk(SCTP_ASCONF_ACK, list); 198 } 199 200 /* 201 * return the current number and list of required chunks caller must 202 * guarantee ptr has space for up to 256 bytes 203 */ 204 int 205 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr) 206 { 207 int i, count = 0; 208 209 if (list == NULL) 210 return (0); 211 212 for (i = 0; i < 256; i++) { 213 if (list->chunks[i] != 0) { 214 *ptr++ = i; 215 count++; 216 } 217 } 218 return (count); 219 } 220 221 int 222 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr) 223 { 224 int i, size = 0; 225 226 if (list == NULL) 227 return (0); 228 229 if (list->num_chunks <= 32) { 230 /* just list them, one byte each */ 231 for (i = 0; i < 256; i++) { 232 if (list->chunks[i] != 0) { 233 *ptr++ = i; 234 size++; 235 } 236 } 237 } else { 238 int index, offset; 239 240 /* pack into a 32 byte bitfield */ 241 for (i = 0; i < 256; i++) { 242 if (list->chunks[i] != 0) { 243 index = i / 8; 244 offset = i % 8; 245 ptr[index] |= (1 << offset); 246 } 247 } 248 size = 32; 249 } 250 return (size); 251 } 252 253 int 254 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks, 255 sctp_auth_chklist_t * list) 256 { 257 int i; 258 int size; 259 260 if (list == NULL) 261 return (0); 262 263 if (num_chunks <= 32) { 264 /* just pull them, one byte each */ 265 for (i = 0; i < num_chunks; i++) { 266 sctp_auth_add_chunk(*ptr++, list); 267 } 268 size = num_chunks; 269 } else { 270 int index, offset; 271 272 /* unpack from a 32 byte bitfield */ 273 for (index = 0; index < 32; index++) { 274 for (offset = 0; offset < 8; offset++) { 275 if (ptr[index] & (1 << offset)) { 276 sctp_auth_add_chunk((index * 8) + offset, list); 277 } 278 } 279 } 280 size = 32; 281 } 282 return (size); 283 } 284 285 286 /* 287 * allocate structure space for a key of length keylen 288 */ 289 sctp_key_t * 290 sctp_alloc_key(uint32_t keylen) 291 { 292 sctp_key_t *new_key; 293 294 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen, 295 "AUTH key"); 296 if (new_key == NULL) { 297 /* out of memory */ 298 return (NULL); 299 } 300 new_key->keylen = keylen; 301 return (new_key); 302 } 303 304 void 305 sctp_free_key(sctp_key_t * key) 306 { 307 if (key != NULL) 308 SCTP_FREE(key); 309 } 310 311 void 312 sctp_print_key(sctp_key_t * key, const char *str) 313 { 314 uint32_t i; 315 316 if (key == NULL) { 317 printf("%s: [Null key]\n", str); 318 return; 319 } 320 printf("%s: len %u, ", str, key->keylen); 321 if (key->keylen) { 322 for (i = 0; i < key->keylen; i++) 323 printf("%02x", key->key[i]); 324 printf("\n"); 325 } else { 326 printf("[Null key]\n"); 327 } 328 } 329 330 void 331 sctp_show_key(sctp_key_t * key, const char *str) 332 { 333 uint32_t i; 334 335 if (key == NULL) { 336 printf("%s: [Null key]\n", str); 337 return; 338 } 339 printf("%s: len %u, ", str, key->keylen); 340 if (key->keylen) { 341 for (i = 0; i < key->keylen; i++) 342 printf("%02x", key->key[i]); 343 printf("\n"); 344 } else { 345 printf("[Null key]\n"); 346 } 347 } 348 349 static inline uint32_t 350 sctp_get_keylen(sctp_key_t * key) 351 { 352 if (key != NULL) 353 return (key->keylen); 354 else 355 return (0); 356 } 357 358 /* 359 * generate a new random key of length 'keylen' 360 */ 361 sctp_key_t * 362 sctp_generate_random_key(uint32_t keylen) 363 { 364 sctp_key_t *new_key; 365 366 /* validate keylen */ 367 if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX) 368 keylen = SCTP_AUTH_RANDOM_SIZE_MAX; 369 370 new_key = sctp_alloc_key(keylen); 371 if (new_key == NULL) { 372 /* out of memory */ 373 return (NULL); 374 } 375 sctp_read_random(new_key->key, keylen); 376 new_key->keylen = keylen; 377 return (new_key); 378 } 379 380 sctp_key_t * 381 sctp_set_key(uint8_t * key, uint32_t keylen) 382 { 383 sctp_key_t *new_key; 384 385 new_key = sctp_alloc_key(keylen); 386 if (new_key == NULL) { 387 /* out of memory */ 388 return (NULL); 389 } 390 bcopy(key, new_key->key, keylen); 391 return (new_key); 392 } 393 394 /* 395 * given two keys of variable size, compute which key is "larger/smaller" 396 * returns: 1 if key1 > key2 -1 if key1 < key2 0 if key1 = key2 397 */ 398 static int 399 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2) 400 { 401 uint32_t maxlen; 402 uint32_t i; 403 uint32_t key1len, key2len; 404 uint8_t *key_1, *key_2; 405 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX]; 406 407 /* sanity/length check */ 408 key1len = sctp_get_keylen(key1); 409 key2len = sctp_get_keylen(key2); 410 if ((key1len == 0) && (key2len == 0)) 411 return (0); 412 else if (key1len == 0) 413 return (-1); 414 else if (key2len == 0) 415 return (1); 416 417 if (key1len != key2len) { 418 if (key1len >= key2len) 419 maxlen = key1len; 420 else 421 maxlen = key2len; 422 bzero(temp, maxlen); 423 if (key1len < maxlen) { 424 /* prepend zeroes to key1 */ 425 bcopy(key1->key, temp + (maxlen - key1len), key1len); 426 key_1 = temp; 427 key_2 = key2->key; 428 } else { 429 /* prepend zeroes to key2 */ 430 bcopy(key2->key, temp + (maxlen - key2len), key2len); 431 key_1 = key1->key; 432 key_2 = temp; 433 } 434 } else { 435 maxlen = key1len; 436 key_1 = key1->key; 437 key_2 = key2->key; 438 } 439 440 for (i = 0; i < maxlen; i++) { 441 if (*key_1 > *key_2) 442 return (1); 443 else if (*key_1 < *key_2) 444 return (-1); 445 key_1++; 446 key_2++; 447 } 448 449 /* keys are equal value, so check lengths */ 450 if (key1len == key2len) 451 return (0); 452 else if (key1len < key2len) 453 return (-1); 454 else 455 return (1); 456 } 457 458 /* 459 * generate the concatenated keying material based on the two keys and the 460 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific 461 * order for concatenation 462 */ 463 sctp_key_t * 464 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared) 465 { 466 uint32_t keylen; 467 sctp_key_t *new_key; 468 uint8_t *key_ptr; 469 470 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) + 471 sctp_get_keylen(shared); 472 473 if (keylen > 0) { 474 /* get space for the new key */ 475 new_key = sctp_alloc_key(keylen); 476 if (new_key == NULL) { 477 /* out of memory */ 478 return (NULL); 479 } 480 new_key->keylen = keylen; 481 key_ptr = new_key->key; 482 } else { 483 /* all keys empty/null?! */ 484 return (NULL); 485 } 486 487 /* concatenate the keys */ 488 if (sctp_compare_key(key1, key2) <= 0) { 489 /* key is key1 + shared + key2 */ 490 if (sctp_get_keylen(key1)) { 491 bcopy(key1->key, key_ptr, key1->keylen); 492 key_ptr += key1->keylen; 493 } 494 if (sctp_get_keylen(shared)) { 495 bcopy(shared->key, key_ptr, shared->keylen); 496 key_ptr += shared->keylen; 497 } 498 if (sctp_get_keylen(key2)) { 499 bcopy(key2->key, key_ptr, key2->keylen); 500 key_ptr += key2->keylen; 501 } 502 } else { 503 /* key is key2 + shared + key1 */ 504 if (sctp_get_keylen(key2)) { 505 bcopy(key2->key, key_ptr, key2->keylen); 506 key_ptr += key2->keylen; 507 } 508 if (sctp_get_keylen(shared)) { 509 bcopy(shared->key, key_ptr, shared->keylen); 510 key_ptr += shared->keylen; 511 } 512 if (sctp_get_keylen(key1)) { 513 bcopy(key1->key, key_ptr, key1->keylen); 514 key_ptr += key1->keylen; 515 } 516 } 517 return (new_key); 518 } 519 520 521 sctp_sharedkey_t * 522 sctp_alloc_sharedkey(void) 523 { 524 sctp_sharedkey_t *new_key; 525 526 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key), 527 "AUTH skey"); 528 if (new_key == NULL) { 529 /* out of memory */ 530 return (NULL); 531 } 532 new_key->keyid = 0; 533 new_key->key = NULL; 534 return (new_key); 535 } 536 537 void 538 sctp_free_sharedkey(sctp_sharedkey_t * skey) 539 { 540 if (skey != NULL) { 541 if (skey->key != NULL) 542 sctp_free_key(skey->key); 543 SCTP_FREE(skey); 544 } 545 } 546 547 sctp_sharedkey_t * 548 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id) 549 { 550 sctp_sharedkey_t *skey; 551 552 LIST_FOREACH(skey, shared_keys, next) { 553 if (skey->keyid == key_id) 554 return (skey); 555 } 556 return (NULL); 557 } 558 559 void 560 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 561 sctp_sharedkey_t * new_skey) 562 { 563 sctp_sharedkey_t *skey; 564 565 if ((shared_keys == NULL) || (new_skey == NULL)) 566 return; 567 568 /* insert into an empty list? */ 569 if (LIST_EMPTY(shared_keys)) { 570 LIST_INSERT_HEAD(shared_keys, new_skey, next); 571 return; 572 } 573 /* insert into the existing list, ordered by key id */ 574 LIST_FOREACH(skey, shared_keys, next) { 575 if (new_skey->keyid < skey->keyid) { 576 /* insert it before here */ 577 LIST_INSERT_BEFORE(skey, new_skey, next); 578 return; 579 } else if (new_skey->keyid == skey->keyid) { 580 /* replace the existing key */ 581 #ifdef SCTP_DEBUG 582 if (SCTP_AUTH_DEBUG) 583 printf("replacing shared key id %u\n", new_skey->keyid); 584 #endif 585 LIST_INSERT_BEFORE(skey, new_skey, next); 586 LIST_REMOVE(skey, next); 587 sctp_free_sharedkey(skey); 588 return; 589 } 590 if (LIST_NEXT(skey, next) == NULL) { 591 /* belongs at the end of the list */ 592 LIST_INSERT_AFTER(skey, new_skey, next); 593 return; 594 } 595 } 596 } 597 598 static sctp_sharedkey_t * 599 sctp_copy_sharedkey(const sctp_sharedkey_t * skey) 600 { 601 sctp_sharedkey_t *new_skey; 602 603 if (skey == NULL) 604 return (NULL); 605 new_skey = sctp_alloc_sharedkey(); 606 if (new_skey == NULL) 607 return (NULL); 608 if (skey->key != NULL) 609 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen); 610 else 611 new_skey->key = NULL; 612 new_skey->keyid = skey->keyid; 613 return (new_skey); 614 } 615 616 int 617 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest) 618 { 619 sctp_sharedkey_t *skey, *new_skey; 620 int count = 0; 621 622 if ((src == NULL) || (dest == NULL)) 623 return (0); 624 LIST_FOREACH(skey, src, next) { 625 new_skey = sctp_copy_sharedkey(skey); 626 if (new_skey != NULL) { 627 sctp_insert_sharedkey(dest, new_skey); 628 count++; 629 } 630 } 631 return (count); 632 } 633 634 635 sctp_hmaclist_t * 636 sctp_alloc_hmaclist(uint8_t num_hmacs) 637 { 638 sctp_hmaclist_t *new_list; 639 int alloc_size; 640 641 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]); 642 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size, 643 "AUTH HMAC list"); 644 if (new_list == NULL) { 645 /* out of memory */ 646 return (NULL); 647 } 648 new_list->max_algo = num_hmacs; 649 new_list->num_algo = 0; 650 return (new_list); 651 } 652 653 void 654 sctp_free_hmaclist(sctp_hmaclist_t * list) 655 { 656 if (list != NULL) { 657 SCTP_FREE(list); 658 list = NULL; 659 } 660 } 661 662 int 663 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id) 664 { 665 if (list == NULL) 666 return (-1); 667 if (list->num_algo == list->max_algo) { 668 #ifdef SCTP_DEBUG 669 if (SCTP_AUTH_DEBUG) 670 printf("SCTP: HMAC id list full, ignoring add %u\n", hmac_id); 671 #endif 672 return (-1); 673 } 674 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) && 675 #ifdef HAVE_SHA224 676 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) && 677 #endif 678 #ifdef HAVE_SHA2 679 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) && 680 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) && 681 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) && 682 #endif 683 (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) { 684 return (-1); 685 } 686 #ifdef SCTP_DEBUG 687 if (SCTP_AUTH_DEBUG) 688 printf("SCTP: add HMAC id %u to list\n", hmac_id); 689 #endif 690 list->hmac[list->num_algo++] = hmac_id; 691 return (0); 692 } 693 694 sctp_hmaclist_t * 695 sctp_copy_hmaclist(sctp_hmaclist_t * list) 696 { 697 sctp_hmaclist_t *new_list; 698 int i; 699 700 if (list == NULL) 701 return (NULL); 702 /* get a new list */ 703 new_list = sctp_alloc_hmaclist(list->max_algo); 704 if (new_list == NULL) 705 return (NULL); 706 /* copy it */ 707 new_list->max_algo = list->max_algo; 708 new_list->num_algo = list->num_algo; 709 for (i = 0; i < list->num_algo; i++) 710 new_list->hmac[i] = list->hmac[i]; 711 return (new_list); 712 } 713 714 sctp_hmaclist_t * 715 sctp_default_supported_hmaclist(void) 716 { 717 sctp_hmaclist_t *new_list; 718 719 new_list = sctp_alloc_hmaclist(2); 720 if (new_list == NULL) 721 return (NULL); 722 sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1); 723 sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256); 724 return (new_list); 725 } 726 727 /* 728 * HMAC algos are listed in priority/preference order find the best HMAC id 729 * to use for the peer based on local support 730 */ 731 uint16_t 732 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local) 733 { 734 int i, j; 735 736 if ((local == NULL) || (peer == NULL)) 737 return (SCTP_AUTH_HMAC_ID_RSVD); 738 739 for (i = 0; i < peer->num_algo; i++) { 740 for (j = 0; j < local->num_algo; j++) { 741 if (peer->hmac[i] == local->hmac[j]) { 742 #ifndef SCTP_AUTH_DRAFT_04 743 /* "skip" MD5 as it's been deprecated */ 744 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5) 745 continue; 746 #endif 747 748 /* found the "best" one */ 749 #ifdef SCTP_DEBUG 750 if (SCTP_AUTH_DEBUG) 751 printf("SCTP: negotiated peer HMAC id %u\n", peer->hmac[i]); 752 #endif 753 return (peer->hmac[i]); 754 } 755 } 756 } 757 /* didn't find one! */ 758 return (SCTP_AUTH_HMAC_ID_RSVD); 759 } 760 761 /* 762 * serialize the HMAC algo list and return space used caller must guarantee 763 * ptr has appropriate space 764 */ 765 int 766 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr) 767 { 768 int i; 769 uint16_t hmac_id; 770 771 if (list == NULL) 772 return (0); 773 774 for (i = 0; i < list->num_algo; i++) { 775 hmac_id = htons(list->hmac[i]); 776 bcopy(&hmac_id, ptr, sizeof(hmac_id)); 777 ptr += sizeof(hmac_id); 778 } 779 return (list->num_algo * sizeof(hmac_id)); 780 } 781 782 int 783 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs) 784 { 785 uint32_t i; 786 uint16_t hmac_id; 787 uint32_t sha1_supported = 0; 788 789 for (i = 0; i < num_hmacs; i++) { 790 hmac_id = ntohs(hmacs->hmac_ids[i]); 791 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1) 792 sha1_supported = 1; 793 } 794 /* all HMAC id's are supported */ 795 if (sha1_supported == 0) 796 return (-1); 797 else 798 return (0); 799 } 800 801 sctp_authinfo_t * 802 sctp_alloc_authinfo(void) 803 { 804 sctp_authinfo_t *new_authinfo; 805 806 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo), 807 "AUTH info"); 808 if (new_authinfo == NULL) { 809 /* out of memory */ 810 return (NULL); 811 } 812 bzero(&new_authinfo, sizeof(*new_authinfo)); 813 return (new_authinfo); 814 } 815 816 void 817 sctp_free_authinfo(sctp_authinfo_t * authinfo) 818 { 819 if (authinfo == NULL) 820 return; 821 822 if (authinfo->random != NULL) 823 sctp_free_key(authinfo->random); 824 if (authinfo->peer_random != NULL) 825 sctp_free_key(authinfo->peer_random); 826 if (authinfo->assoc_key != NULL) 827 sctp_free_key(authinfo->assoc_key); 828 if (authinfo->recv_key != NULL) 829 sctp_free_key(authinfo->recv_key); 830 831 /* We are NOT dynamically allocating authinfo's right now... */ 832 /* SCTP_FREE(authinfo); */ 833 } 834 835 836 inline uint32_t 837 sctp_get_auth_chunk_len(uint16_t hmac_algo) 838 { 839 int size; 840 841 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo); 842 return (SCTP_SIZE32(size)); 843 } 844 845 uint32_t 846 sctp_get_hmac_digest_len(uint16_t hmac_algo) 847 { 848 switch (hmac_algo) { 849 case SCTP_AUTH_HMAC_ID_SHA1: 850 return (SCTP_AUTH_DIGEST_LEN_SHA1); 851 case SCTP_AUTH_HMAC_ID_MD5: 852 return (SCTP_AUTH_DIGEST_LEN_MD5); 853 #ifdef HAVE_SHA224 854 case SCTP_AUTH_HMAC_ID_SHA224: 855 return (SCTP_AUTH_DIGEST_LEN_SHA224); 856 #endif 857 #ifdef HAVE_SHA2 858 case SCTP_AUTH_HMAC_ID_SHA256: 859 return (SCTP_AUTH_DIGEST_LEN_SHA256); 860 case SCTP_AUTH_HMAC_ID_SHA384: 861 return (SCTP_AUTH_DIGEST_LEN_SHA384); 862 case SCTP_AUTH_HMAC_ID_SHA512: 863 return (SCTP_AUTH_DIGEST_LEN_SHA512); 864 #endif 865 default: 866 /* unknown HMAC algorithm: can't do anything */ 867 return (0); 868 } /* end switch */ 869 } 870 871 static inline int 872 sctp_get_hmac_block_len(uint16_t hmac_algo) 873 { 874 switch (hmac_algo) { 875 case SCTP_AUTH_HMAC_ID_SHA1: 876 case SCTP_AUTH_HMAC_ID_MD5: 877 #ifdef HAVE_SHA224 878 case SCTP_AUTH_HMAC_ID_SHA224: 879 return (64); 880 #endif 881 #ifdef HAVE_SHA2 882 case SCTP_AUTH_HMAC_ID_SHA256: 883 return (64); 884 case SCTP_AUTH_HMAC_ID_SHA384: 885 case SCTP_AUTH_HMAC_ID_SHA512: 886 return (128); 887 #endif 888 case SCTP_AUTH_HMAC_ID_RSVD: 889 default: 890 /* unknown HMAC algorithm: can't do anything */ 891 return (0); 892 } /* end switch */ 893 } 894 895 static void 896 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx) 897 { 898 switch (hmac_algo) { 899 case SCTP_AUTH_HMAC_ID_SHA1: 900 SHA1_Init(&ctx->sha1); 901 break; 902 case SCTP_AUTH_HMAC_ID_MD5: 903 MD5_Init(&ctx->md5); 904 break; 905 #ifdef HAVE_SHA224 906 case SCTP_AUTH_HMAC_ID_SHA224: 907 break; 908 #endif 909 #ifdef HAVE_SHA2 910 case SCTP_AUTH_HMAC_ID_SHA256: 911 SHA256_Init(&ctx->sha256); 912 break; 913 case SCTP_AUTH_HMAC_ID_SHA384: 914 SHA384_Init(&ctx->sha384); 915 break; 916 case SCTP_AUTH_HMAC_ID_SHA512: 917 SHA512_Init(&ctx->sha512); 918 break; 919 #endif 920 case SCTP_AUTH_HMAC_ID_RSVD: 921 default: 922 /* unknown HMAC algorithm: can't do anything */ 923 return; 924 } /* end switch */ 925 } 926 927 static void 928 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx, 929 const uint8_t * text, uint32_t textlen) 930 { 931 switch (hmac_algo) { 932 case SCTP_AUTH_HMAC_ID_SHA1: 933 SHA1_Update(&ctx->sha1, text, textlen); 934 break; 935 case SCTP_AUTH_HMAC_ID_MD5: 936 MD5_Update(&ctx->md5, text, textlen); 937 break; 938 #ifdef HAVE_SHA224 939 case SCTP_AUTH_HMAC_ID_SHA224: 940 break; 941 #endif 942 #ifdef HAVE_SHA2 943 case SCTP_AUTH_HMAC_ID_SHA256: 944 SHA256_Update(&ctx->sha256, text, textlen); 945 break; 946 case SCTP_AUTH_HMAC_ID_SHA384: 947 SHA384_Update(&ctx->sha384, text, textlen); 948 break; 949 case SCTP_AUTH_HMAC_ID_SHA512: 950 SHA512_Update(&ctx->sha512, text, textlen); 951 break; 952 #endif 953 case SCTP_AUTH_HMAC_ID_RSVD: 954 default: 955 /* unknown HMAC algorithm: can't do anything */ 956 return; 957 } /* end switch */ 958 } 959 960 static void 961 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx, 962 uint8_t * digest) 963 { 964 switch (hmac_algo) { 965 case SCTP_AUTH_HMAC_ID_SHA1: 966 SHA1_Final(digest, &ctx->sha1); 967 break; 968 case SCTP_AUTH_HMAC_ID_MD5: 969 MD5_Final(digest, &ctx->md5); 970 break; 971 #ifdef HAVE_SHA224 972 case SCTP_AUTH_HMAC_ID_SHA224: 973 break; 974 #endif 975 #ifdef HAVE_SHA2 976 case SCTP_AUTH_HMAC_ID_SHA256: 977 SHA256_Final(digest, &ctx->sha256); 978 break; 979 case SCTP_AUTH_HMAC_ID_SHA384: 980 /* SHA384 is truncated SHA512 */ 981 SHA384_Final(digest, &ctx->sha384); 982 break; 983 case SCTP_AUTH_HMAC_ID_SHA512: 984 SHA512_Final(digest, &ctx->sha512); 985 break; 986 #endif 987 case SCTP_AUTH_HMAC_ID_RSVD: 988 default: 989 /* unknown HMAC algorithm: can't do anything */ 990 return; 991 } /* end switch */ 992 } 993 994 /* 995 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104) 996 * 997 * Compute the HMAC digest using the desired hash key, text, and HMAC 998 * algorithm. Resulting digest is placed in 'digest' and digest length 999 * is returned, if the HMAC was performed. 1000 * 1001 * WARNING: it is up to the caller to supply sufficient space to hold the 1002 * resultant digest. 1003 */ 1004 uint32_t 1005 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1006 const uint8_t * text, uint32_t textlen, uint8_t * digest) 1007 { 1008 uint32_t digestlen; 1009 uint32_t blocklen; 1010 sctp_hash_context_t ctx; 1011 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1012 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1013 uint32_t i; 1014 1015 /* sanity check the material and length */ 1016 if ((key == NULL) || (keylen == 0) || (text == NULL) || 1017 (textlen == 0) || (digest == NULL)) { 1018 /* can't do HMAC with empty key or text or digest store */ 1019 return (0); 1020 } 1021 /* validate the hmac algo and get the digest length */ 1022 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1023 if (digestlen == 0) 1024 return (0); 1025 1026 /* hash the key if it is longer than the hash block size */ 1027 blocklen = sctp_get_hmac_block_len(hmac_algo); 1028 if (keylen > blocklen) { 1029 sctp_hmac_init(hmac_algo, &ctx); 1030 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1031 sctp_hmac_final(hmac_algo, &ctx, temp); 1032 /* set the hashed key as the key */ 1033 keylen = digestlen; 1034 key = temp; 1035 } 1036 /* initialize the inner/outer pads with the key and "append" zeroes */ 1037 bzero(ipad, blocklen); 1038 bzero(opad, blocklen); 1039 bcopy(key, ipad, keylen); 1040 bcopy(key, opad, keylen); 1041 1042 /* XOR the key with ipad and opad values */ 1043 for (i = 0; i < blocklen; i++) { 1044 ipad[i] ^= 0x36; 1045 opad[i] ^= 0x5c; 1046 } 1047 1048 /* perform inner hash */ 1049 sctp_hmac_init(hmac_algo, &ctx); 1050 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1051 sctp_hmac_update(hmac_algo, &ctx, text, textlen); 1052 sctp_hmac_final(hmac_algo, &ctx, temp); 1053 1054 /* perform outer hash */ 1055 sctp_hmac_init(hmac_algo, &ctx); 1056 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1057 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1058 sctp_hmac_final(hmac_algo, &ctx, digest); 1059 1060 return (digestlen); 1061 } 1062 1063 /* mbuf version */ 1064 uint32_t 1065 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1066 struct mbuf *m, uint32_t m_offset, uint8_t * digest) 1067 { 1068 uint32_t digestlen; 1069 uint32_t blocklen; 1070 sctp_hash_context_t ctx; 1071 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1072 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1073 uint32_t i; 1074 struct mbuf *m_tmp; 1075 1076 /* sanity check the material and length */ 1077 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) { 1078 /* can't do HMAC with empty key or text or digest store */ 1079 return (0); 1080 } 1081 /* validate the hmac algo and get the digest length */ 1082 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1083 if (digestlen == 0) 1084 return (0); 1085 1086 /* hash the key if it is longer than the hash block size */ 1087 blocklen = sctp_get_hmac_block_len(hmac_algo); 1088 if (keylen > blocklen) { 1089 sctp_hmac_init(hmac_algo, &ctx); 1090 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1091 sctp_hmac_final(hmac_algo, &ctx, temp); 1092 /* set the hashed key as the key */ 1093 keylen = digestlen; 1094 key = temp; 1095 } 1096 /* initialize the inner/outer pads with the key and "append" zeroes */ 1097 bzero(ipad, blocklen); 1098 bzero(opad, blocklen); 1099 bcopy(key, ipad, keylen); 1100 bcopy(key, opad, keylen); 1101 1102 /* XOR the key with ipad and opad values */ 1103 for (i = 0; i < blocklen; i++) { 1104 ipad[i] ^= 0x36; 1105 opad[i] ^= 0x5c; 1106 } 1107 1108 /* perform inner hash */ 1109 sctp_hmac_init(hmac_algo, &ctx); 1110 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1111 /* find the correct starting mbuf and offset (get start of text) */ 1112 m_tmp = m; 1113 while ((m_tmp != NULL) && (m_offset >= (uint32_t) m_tmp->m_len)) { 1114 m_offset -= m_tmp->m_len; 1115 m_tmp = m_tmp->m_next; 1116 } 1117 /* now use the rest of the mbuf chain for the text */ 1118 while (m_tmp != NULL) { 1119 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, 1120 m_tmp->m_len - m_offset); 1121 /* clear the offset since it's only for the first mbuf */ 1122 m_offset = 0; 1123 m_tmp = m_tmp->m_next; 1124 } 1125 sctp_hmac_final(hmac_algo, &ctx, temp); 1126 1127 /* perform outer hash */ 1128 sctp_hmac_init(hmac_algo, &ctx); 1129 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1130 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1131 sctp_hmac_final(hmac_algo, &ctx, digest); 1132 1133 return (digestlen); 1134 } 1135 1136 /* 1137 * verify the HMAC digest using the desired hash key, text, and HMAC 1138 * algorithm. Returns -1 on error, 0 on success. 1139 */ 1140 int 1141 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1142 const uint8_t * text, uint32_t textlen, 1143 uint8_t * digest, uint32_t digestlen) 1144 { 1145 uint32_t len; 1146 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1147 1148 /* sanity check the material and length */ 1149 if ((key == NULL) || (keylen == 0) || 1150 (text == NULL) || (textlen == 0) || (digest == NULL)) { 1151 /* can't do HMAC with empty key or text or digest */ 1152 return (-1); 1153 } 1154 len = sctp_get_hmac_digest_len(hmac_algo); 1155 if ((len == 0) || (digestlen != len)) 1156 return (-1); 1157 1158 /* compute the expected hash */ 1159 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len) 1160 return (-1); 1161 1162 if (memcmp(digest, temp, digestlen) != 0) 1163 return (-1); 1164 else 1165 return (0); 1166 } 1167 1168 1169 /* 1170 * computes the requested HMAC using a key struct (which may be modified if 1171 * the keylen exceeds the HMAC block len). 1172 */ 1173 uint32_t 1174 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, const uint8_t * text, 1175 uint32_t textlen, uint8_t * digest) 1176 { 1177 uint32_t digestlen; 1178 uint32_t blocklen; 1179 sctp_hash_context_t ctx; 1180 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1181 1182 /* sanity check */ 1183 if ((key == NULL) || (text == NULL) || (textlen == 0) || 1184 (digest == NULL)) { 1185 /* can't do HMAC with empty key or text or digest store */ 1186 return (0); 1187 } 1188 /* validate the hmac algo and get the digest length */ 1189 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1190 if (digestlen == 0) 1191 return (0); 1192 1193 /* hash the key if it is longer than the hash block size */ 1194 blocklen = sctp_get_hmac_block_len(hmac_algo); 1195 if (key->keylen > blocklen) { 1196 sctp_hmac_init(hmac_algo, &ctx); 1197 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1198 sctp_hmac_final(hmac_algo, &ctx, temp); 1199 /* save the hashed key as the new key */ 1200 key->keylen = digestlen; 1201 bcopy(temp, key->key, key->keylen); 1202 } 1203 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen, 1204 digest)); 1205 } 1206 1207 /* mbuf version */ 1208 uint32_t 1209 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m, 1210 uint32_t m_offset, uint8_t * digest) 1211 { 1212 uint32_t digestlen; 1213 uint32_t blocklen; 1214 sctp_hash_context_t ctx; 1215 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1216 1217 /* sanity check */ 1218 if ((key == NULL) || (m == NULL) || (digest == NULL)) { 1219 /* can't do HMAC with empty key or text or digest store */ 1220 return (0); 1221 } 1222 /* validate the hmac algo and get the digest length */ 1223 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1224 if (digestlen == 0) 1225 return (0); 1226 1227 /* hash the key if it is longer than the hash block size */ 1228 blocklen = sctp_get_hmac_block_len(hmac_algo); 1229 if (key->keylen > blocklen) { 1230 sctp_hmac_init(hmac_algo, &ctx); 1231 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1232 sctp_hmac_final(hmac_algo, &ctx, temp); 1233 /* save the hashed key as the new key */ 1234 key->keylen = digestlen; 1235 bcopy(temp, key->key, key->keylen); 1236 } 1237 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest)); 1238 } 1239 1240 int 1241 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id) 1242 { 1243 int i; 1244 1245 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD)) 1246 return (0); 1247 1248 for (i = 0; i < list->num_algo; i++) 1249 if (list->hmac[i] == id) 1250 return (1); 1251 1252 /* not in the list */ 1253 return (0); 1254 } 1255 1256 1257 /* 1258 * clear any cached key(s) if they match the given key id on an association 1259 * the cached key(s) will be recomputed and re-cached at next use. ASSUMES 1260 * TCB_LOCK is already held 1261 */ 1262 void 1263 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid) 1264 { 1265 if (stcb == NULL) 1266 return; 1267 1268 if (keyid == stcb->asoc.authinfo.assoc_keyid) { 1269 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1270 stcb->asoc.authinfo.assoc_key = NULL; 1271 } 1272 if (keyid == stcb->asoc.authinfo.recv_keyid) { 1273 sctp_free_key(stcb->asoc.authinfo.recv_key); 1274 stcb->asoc.authinfo.recv_key = NULL; 1275 } 1276 } 1277 1278 /* 1279 * clear any cached key(s) if they match the given key id for all assocs on 1280 * an association ASSUMES INP_WLOCK is already held 1281 */ 1282 void 1283 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid) 1284 { 1285 struct sctp_tcb *stcb; 1286 1287 if (inp == NULL) 1288 return; 1289 1290 /* clear the cached keys on all assocs on this instance */ 1291 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1292 SCTP_TCB_LOCK(stcb); 1293 sctp_clear_cachedkeys(stcb, keyid); 1294 SCTP_TCB_UNLOCK(stcb); 1295 } 1296 } 1297 1298 /* 1299 * delete a shared key from an association ASSUMES TCB_LOCK is already held 1300 */ 1301 int 1302 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1303 { 1304 sctp_sharedkey_t *skey; 1305 1306 if (stcb == NULL) 1307 return (-1); 1308 1309 /* is the keyid the assoc active sending key */ 1310 if (keyid == stcb->asoc.authinfo.assoc_keyid) 1311 return (-1); 1312 1313 /* does the key exist? */ 1314 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1315 if (skey == NULL) 1316 return (-1); 1317 1318 /* remove it */ 1319 LIST_REMOVE(skey, next); 1320 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1321 1322 /* clear any cached keys */ 1323 sctp_clear_cachedkeys(stcb, keyid); 1324 return (0); 1325 } 1326 1327 /* 1328 * deletes a shared key from the endpoint ASSUMES INP_WLOCK is already held 1329 */ 1330 int 1331 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1332 { 1333 sctp_sharedkey_t *skey; 1334 struct sctp_tcb *stcb; 1335 1336 if (inp == NULL) 1337 return (-1); 1338 1339 /* is the keyid the active sending key on the endpoint or any assoc */ 1340 if (keyid == inp->sctp_ep.default_keyid) 1341 return (-1); 1342 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1343 SCTP_TCB_LOCK(stcb); 1344 if (keyid == stcb->asoc.authinfo.assoc_keyid) { 1345 SCTP_TCB_UNLOCK(stcb); 1346 return (-1); 1347 } 1348 SCTP_TCB_UNLOCK(stcb); 1349 } 1350 1351 /* does the key exist? */ 1352 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1353 if (skey == NULL) 1354 return (-1); 1355 1356 /* remove it */ 1357 LIST_REMOVE(skey, next); 1358 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1359 1360 /* clear any cached keys */ 1361 sctp_clear_cachedkeys_ep(inp, keyid); 1362 return (0); 1363 } 1364 1365 /* 1366 * set the active key on an association ASSUME TCB_LOCK is already held 1367 */ 1368 int 1369 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid) 1370 { 1371 sctp_sharedkey_t *skey = NULL; 1372 sctp_key_t *key = NULL; 1373 int using_ep_key = 0; 1374 1375 /* find the key on the assoc */ 1376 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1377 if (skey == NULL) { 1378 /* if not on the assoc, find the key on the endpoint */ 1379 SCTP_INP_RLOCK(stcb->sctp_ep); 1380 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys, 1381 keyid); 1382 using_ep_key = 1; 1383 } 1384 if (skey == NULL) { 1385 /* that key doesn't exist */ 1386 if (using_ep_key) 1387 SCTP_INP_RUNLOCK(stcb->sctp_ep); 1388 return (-1); 1389 } 1390 /* get the shared key text */ 1391 key = skey->key; 1392 1393 /* free any existing cached key */ 1394 if (stcb->asoc.authinfo.assoc_key != NULL) 1395 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1396 /* compute a new assoc key and cache it */ 1397 stcb->asoc.authinfo.assoc_key = 1398 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1399 stcb->asoc.authinfo.peer_random, key); 1400 stcb->asoc.authinfo.assoc_keyid = keyid; 1401 #ifdef SCTP_DEBUG 1402 if (SCTP_AUTH_DEBUG) 1403 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key"); 1404 #endif 1405 1406 if (using_ep_key) 1407 SCTP_INP_RUNLOCK(stcb->sctp_ep); 1408 return (0); 1409 } 1410 1411 /* 1412 * set the active key on an endpoint ASSUMES INP_WLOCK is already held 1413 */ 1414 int 1415 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1416 { 1417 sctp_sharedkey_t *skey; 1418 1419 /* find the key */ 1420 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1421 if (skey == NULL) { 1422 /* that key doesn't exist */ 1423 return (-1); 1424 } 1425 inp->sctp_ep.default_keyid = keyid; 1426 return (0); 1427 } 1428 1429 /* 1430 * get local authentication parameters from cookie (from INIT-ACK) 1431 */ 1432 void 1433 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 1434 uint32_t offset, uint32_t length) 1435 { 1436 struct sctp_paramhdr *phdr, tmp_param; 1437 uint16_t plen, ptype; 1438 uint8_t store[384]; 1439 struct sctp_auth_random *random = NULL; 1440 uint16_t random_len = 0; 1441 struct sctp_auth_hmac_algo *hmacs = NULL; 1442 uint16_t hmacs_len = 0; 1443 struct sctp_auth_chunk_list *chunks = NULL; 1444 uint16_t num_chunks = 0; 1445 sctp_key_t *new_key; 1446 uint32_t keylen; 1447 1448 /* convert to upper bound */ 1449 length += offset; 1450 1451 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, 1452 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param); 1453 while (phdr != NULL) { 1454 ptype = ntohs(phdr->param_type); 1455 plen = ntohs(phdr->param_length); 1456 1457 if ((plen == 0) || (offset + plen > length)) 1458 break; 1459 1460 if (ptype == SCTP_RANDOM) { 1461 if (plen > sizeof(store)) 1462 break; 1463 phdr = sctp_get_next_param(m, offset, 1464 (struct sctp_paramhdr *)store, plen); 1465 if (phdr == NULL) 1466 return; 1467 /* save the random and length for the key */ 1468 random = (struct sctp_auth_random *)phdr; 1469 random_len = plen - sizeof(*random); 1470 } else if (ptype == SCTP_HMAC_LIST) { 1471 int num_hmacs; 1472 int i; 1473 1474 if (plen > sizeof(store)) 1475 break; 1476 phdr = sctp_get_next_param(m, offset, 1477 (struct sctp_paramhdr *)store, plen); 1478 if (phdr == NULL) 1479 return; 1480 /* save the hmacs list and num for the key */ 1481 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1482 hmacs_len = plen - sizeof(*hmacs); 1483 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 1484 if (stcb->asoc.local_hmacs != NULL) 1485 sctp_free_hmaclist(stcb->asoc.local_hmacs); 1486 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs); 1487 if (stcb->asoc.local_hmacs != NULL) { 1488 for (i = 0; i < num_hmacs; i++) { 1489 sctp_auth_add_hmacid(stcb->asoc.local_hmacs, 1490 ntohs(hmacs->hmac_ids[i])); 1491 } 1492 } 1493 } else if (ptype == SCTP_CHUNK_LIST) { 1494 int i; 1495 1496 if (plen > sizeof(store)) 1497 break; 1498 phdr = sctp_get_next_param(m, offset, 1499 (struct sctp_paramhdr *)store, plen); 1500 if (phdr == NULL) 1501 return; 1502 chunks = (struct sctp_auth_chunk_list *)phdr; 1503 num_chunks = plen - sizeof(*chunks); 1504 /* save chunks list and num for the key */ 1505 if (stcb->asoc.local_auth_chunks != NULL) 1506 sctp_clear_chunklist(stcb->asoc.local_auth_chunks); 1507 else 1508 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist(); 1509 for (i = 0; i < num_chunks; i++) { 1510 sctp_auth_add_chunk(chunks->chunk_types[i], 1511 stcb->asoc.local_auth_chunks); 1512 } 1513 } 1514 /* get next parameter */ 1515 offset += SCTP_SIZE32(plen); 1516 if (offset + sizeof(struct sctp_paramhdr) > length) 1517 break; 1518 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), 1519 (uint8_t *) & tmp_param); 1520 } 1521 /* concatenate the full random key */ 1522 keylen = random_len + num_chunks + hmacs_len; 1523 new_key = sctp_alloc_key(keylen); 1524 if (new_key != NULL) { 1525 /* copy in the RANDOM */ 1526 if (random != NULL) 1527 bcopy(random->random_data, new_key->key, random_len); 1528 /* append in the AUTH chunks */ 1529 if (chunks != NULL) 1530 bcopy(chunks->chunk_types, new_key->key + random_len, 1531 num_chunks); 1532 /* append in the HMACs */ 1533 if (hmacs != NULL) 1534 bcopy(hmacs->hmac_ids, new_key->key + random_len + num_chunks, 1535 hmacs_len); 1536 } 1537 if (stcb->asoc.authinfo.random != NULL) 1538 sctp_free_key(stcb->asoc.authinfo.random); 1539 stcb->asoc.authinfo.random = new_key; 1540 stcb->asoc.authinfo.random_len = random_len; 1541 #ifdef SCTP_AUTH_DRAFT_04 1542 /* don't include the chunks and hmacs for draft -04 */ 1543 stcb->asoc.authinfo.random->keylen = random_len; 1544 #endif 1545 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 1546 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 1547 1548 /* negotiate what HMAC to use for the peer */ 1549 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 1550 stcb->asoc.local_hmacs); 1551 /* copy defaults from the endpoint */ 1552 /* FIX ME: put in cookie? */ 1553 stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid; 1554 } 1555 1556 /* 1557 * compute and fill in the HMAC digest for a packet 1558 */ 1559 void 1560 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 1561 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb) 1562 { 1563 uint32_t digestlen; 1564 sctp_sharedkey_t *skey; 1565 sctp_key_t *key; 1566 1567 if ((stcb == NULL) || (auth == NULL)) 1568 return; 1569 1570 /* zero the digest + chunk padding */ 1571 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); 1572 bzero(auth->hmac, SCTP_SIZE32(digestlen)); 1573 /* is an assoc key cached? */ 1574 if (stcb->asoc.authinfo.assoc_key == NULL) { 1575 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, 1576 stcb->asoc.authinfo.assoc_keyid); 1577 if (skey == NULL) { 1578 /* not in the assoc list, so check the endpoint list */ 1579 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys, 1580 stcb->asoc.authinfo.assoc_keyid); 1581 } 1582 /* the only way skey is NULL is if null key id 0 is used */ 1583 if (skey != NULL) 1584 key = skey->key; 1585 else 1586 key = NULL; 1587 /* compute a new assoc key and cache it */ 1588 stcb->asoc.authinfo.assoc_key = 1589 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1590 stcb->asoc.authinfo.peer_random, key); 1591 #ifdef SCTP_DEBUG 1592 if (SCTP_AUTH_DEBUG) { 1593 printf("caching key id %u\n", 1594 stcb->asoc.authinfo.assoc_keyid); 1595 sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key"); 1596 } 1597 #endif 1598 } 1599 /* set in the active key id */ 1600 auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid); 1601 1602 /* compute and fill in the digest */ 1603 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, 1604 stcb->asoc.authinfo.assoc_key, 1605 m, auth_offset, auth->hmac); 1606 } 1607 1608 1609 static void 1610 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size) 1611 { 1612 struct mbuf *m_tmp; 1613 uint8_t *data; 1614 1615 /* sanity check */ 1616 if (m == NULL) 1617 return; 1618 1619 /* find the correct starting mbuf and offset (get start position) */ 1620 m_tmp = m; 1621 while ((m_tmp != NULL) && (m_offset >= (uint32_t) m_tmp->m_len)) { 1622 m_offset -= m_tmp->m_len; 1623 m_tmp = m_tmp->m_next; 1624 } 1625 /* now use the rest of the mbuf chain */ 1626 while ((m_tmp != NULL) && (size > 0)) { 1627 data = mtod(m_tmp, uint8_t *) + m_offset; 1628 if (size > (uint32_t) m_tmp->m_len) { 1629 bzero(data, m_tmp->m_len); 1630 size -= m_tmp->m_len; 1631 } else { 1632 bzero(data, size); 1633 size = 0; 1634 } 1635 /* clear the offset since it's only for the first mbuf */ 1636 m_offset = 0; 1637 m_tmp = m_tmp->m_next; 1638 } 1639 } 1640 1641 /* 1642 * process the incoming Authentication chunk return codes: -1 on any 1643 * authentication error 0 on authentication verification 1644 */ 1645 int 1646 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth, 1647 struct mbuf *m, uint32_t offset) 1648 { 1649 uint16_t chunklen; 1650 uint16_t shared_key_id; 1651 uint16_t hmac_id; 1652 sctp_sharedkey_t *skey; 1653 uint32_t digestlen; 1654 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1655 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1656 1657 /* auth is checked for NULL by caller */ 1658 chunklen = ntohs(auth->ch.chunk_length); 1659 if (chunklen < sizeof(*auth)) { 1660 SCTP_STAT_INCR(sctps_recvauthfailed); 1661 return (-1); 1662 } 1663 SCTP_STAT_INCR(sctps_recvauth); 1664 1665 /* get the auth params */ 1666 shared_key_id = ntohs(auth->shared_key_id); 1667 hmac_id = ntohs(auth->hmac_id); 1668 #ifdef SCTP_DEBUG 1669 if (SCTP_AUTH_DEBUG) 1670 printf("SCTP AUTH Chunk: shared key %u, HMAC id %u\n", 1671 shared_key_id, hmac_id); 1672 #endif 1673 1674 /* is the indicated HMAC supported? */ 1675 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) { 1676 struct mbuf *m_err; 1677 struct sctp_auth_invalid_hmac *err; 1678 1679 SCTP_STAT_INCR(sctps_recvivalhmacid); 1680 #ifdef SCTP_DEBUG 1681 if (SCTP_AUTH_DEBUG) 1682 printf("SCTP Auth: unsupported HMAC id %u\n", hmac_id); 1683 #endif 1684 /* 1685 * report this in an Error Chunk: Unsupported HMAC 1686 * Identifier 1687 */ 1688 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 1, M_DONTWAIT, 1, MT_HEADER); 1689 if (m_err != NULL) { 1690 /* pre-reserve some space */ 1691 m_err->m_data += sizeof(struct sctp_chunkhdr); 1692 /* fill in the error */ 1693 err = mtod(m_err, struct sctp_auth_invalid_hmac *); 1694 bzero(err, sizeof(*err)); 1695 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID); 1696 err->ph.param_length = htons(sizeof(*err)); 1697 err->hmac_id = ntohs(hmac_id); 1698 m_err->m_pkthdr.len = m_err->m_len = sizeof(*err); 1699 /* queue it */ 1700 sctp_queue_op_err(stcb, m_err); 1701 } 1702 return (-1); 1703 } 1704 /* get the indicated shared key, if available */ 1705 if ((stcb->asoc.authinfo.recv_key == NULL) || 1706 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) { 1707 /* find the shared key on the assoc first */ 1708 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id); 1709 if (skey == NULL) { 1710 /* if not on the assoc, find it on the endpoint */ 1711 skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys, 1712 shared_key_id); 1713 } 1714 /* if the shared key isn't found, discard the chunk */ 1715 if (skey == NULL) { 1716 SCTP_STAT_INCR(sctps_recvivalkeyid); 1717 #ifdef SCTP_DEBUG 1718 if (SCTP_AUTH_DEBUG) 1719 printf("SCTP Auth: unknown key id %u\n", 1720 shared_key_id); 1721 #endif 1722 return (-1); 1723 } 1724 /* generate a notification if this is a new key id */ 1725 if (stcb->asoc.authinfo.recv_keyid != shared_key_id) 1726 /* 1727 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb, 1728 * shared_key_id, (void 1729 * *)stcb->asoc.authinfo.recv_keyid); 1730 */ 1731 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, 1732 shared_key_id, stcb->asoc.authinfo.recv_keyid); 1733 /* compute a new recv assoc key and cache it */ 1734 if (stcb->asoc.authinfo.recv_key != NULL) 1735 sctp_free_key(stcb->asoc.authinfo.recv_key); 1736 stcb->asoc.authinfo.recv_key = 1737 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1738 stcb->asoc.authinfo.peer_random, skey->key); 1739 stcb->asoc.authinfo.recv_keyid = shared_key_id; 1740 #ifdef SCTP_DEBUG 1741 if (SCTP_AUTH_DEBUG) 1742 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key"); 1743 #endif 1744 } 1745 /* validate the digest length */ 1746 digestlen = sctp_get_hmac_digest_len(hmac_id); 1747 if (chunklen < (sizeof(*auth) + digestlen)) { 1748 /* invalid digest length */ 1749 SCTP_STAT_INCR(sctps_recvauthfailed); 1750 #ifdef SCTP_DEBUG 1751 if (SCTP_AUTH_DEBUG) 1752 printf("SCTP Auth: chunk too short for HMAC\n"); 1753 #endif 1754 return (-1); 1755 } 1756 /* save a copy of the digest, zero the pseudo header, and validate */ 1757 bcopy(auth->hmac, digest, digestlen); 1758 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen)); 1759 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key, 1760 m, offset, computed_digest); 1761 1762 /* compare the computed digest with the one in the AUTH chunk */ 1763 if (memcmp(digest, computed_digest, digestlen) != 0) { 1764 SCTP_STAT_INCR(sctps_recvauthfailed); 1765 #ifdef SCTP_DEBUG 1766 if (SCTP_AUTH_DEBUG) 1767 printf("SCTP Auth: HMAC digest check failed\n"); 1768 #endif 1769 return (-1); 1770 } 1771 return (0); 1772 } 1773 1774 /* 1775 * Generate NOTIFICATION 1776 */ 1777 void 1778 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication, 1779 uint16_t keyid, uint16_t alt_keyid) 1780 { 1781 struct mbuf *m_notify; 1782 struct sctp_authkey_event *auth; 1783 struct sctp_queued_to_read *control; 1784 1785 if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT)) 1786 /* event not enabled */ 1787 return; 1788 1789 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event), 1790 1, M_DONTWAIT, 1, MT_HEADER); 1791 if (m_notify == NULL) 1792 /* no space left */ 1793 return; 1794 m_notify->m_len = 0; 1795 auth = mtod(m_notify, struct sctp_authkey_event *); 1796 auth->auth_type = SCTP_AUTHENTICATION_EVENT; 1797 auth->auth_flags = 0; 1798 auth->auth_length = sizeof(*auth); 1799 auth->auth_keynumber = keyid; 1800 auth->auth_altkeynumber = alt_keyid; 1801 auth->auth_indication = indication; 1802 auth->auth_assoc_id = sctp_get_associd(stcb); 1803 1804 m_notify->m_flags |= M_EOR | M_NOTIFICATION; 1805 m_notify->m_pkthdr.len = sizeof(*auth); 1806 m_notify->m_pkthdr.rcvif = 0; 1807 m_notify->m_len = sizeof(*auth); 1808 m_notify->m_next = NULL; 1809 1810 /* append to socket */ 1811 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 1812 0, 0, 0, 0, 0, 0, m_notify); 1813 if (control == NULL) { 1814 /* no memory */ 1815 sctp_m_freem(m_notify); 1816 return; 1817 } 1818 control->length = m_notify->m_len; 1819 /* not that we need this */ 1820 control->tail_mbuf = m_notify; 1821 sctp_add_to_readq(stcb->sctp_ep, stcb, control, 1822 &stcb->sctp_socket->so_rcv, 1); 1823 } 1824 1825 1826 /* 1827 * validates the AUTHentication related parameters in an INIT/INIT-ACK 1828 * Note: currently only used for INIT as INIT-ACK is handled inline 1829 * with sctp_load_addresses_from_init() 1830 */ 1831 int 1832 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit) 1833 { 1834 struct sctp_paramhdr *phdr, parm_buf; 1835 uint16_t ptype, plen; 1836 int peer_supports_asconf = 0; 1837 int peer_supports_auth = 0; 1838 int got_random = 0, got_hmacs = 0; 1839 1840 /* go through each of the params. */ 1841 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 1842 while (phdr) { 1843 ptype = ntohs(phdr->param_type); 1844 plen = ntohs(phdr->param_length); 1845 1846 if (offset + plen > limit) { 1847 break; 1848 } 1849 if (plen == 0) { 1850 break; 1851 } 1852 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 1853 /* A supported extension chunk */ 1854 struct sctp_supported_chunk_types_param *pr_supported; 1855 uint8_t local_store[128]; 1856 int num_ent, i; 1857 1858 phdr = sctp_get_next_param(m, offset, 1859 (struct sctp_paramhdr *)&local_store, plen); 1860 if (phdr == NULL) { 1861 return (-1); 1862 } 1863 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 1864 num_ent = plen - sizeof(struct sctp_paramhdr); 1865 for (i = 0; i < num_ent; i++) { 1866 switch (pr_supported->chunk_types[i]) { 1867 case SCTP_ASCONF: 1868 case SCTP_ASCONF_ACK: 1869 peer_supports_asconf = 1; 1870 break; 1871 case SCTP_AUTHENTICATION: 1872 peer_supports_auth = 1; 1873 break; 1874 default: 1875 /* one we don't care about */ 1876 break; 1877 } 1878 } 1879 } else if (ptype == SCTP_RANDOM) { 1880 got_random = 1; 1881 /* enforce the random length */ 1882 if (plen != (sizeof(struct sctp_auth_random) + 1883 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) { 1884 #ifdef SCTP_DEBUG 1885 if (sctp_debug_on & SCTP_DEBUG_AUTH1) 1886 printf("SCTP: invalid RANDOM len\n"); 1887 #endif 1888 return (-1); 1889 } 1890 } else if (ptype == SCTP_HMAC_LIST) { 1891 uint8_t store[256]; 1892 struct sctp_auth_hmac_algo *hmacs; 1893 int num_hmacs; 1894 1895 if (plen > sizeof(store)) 1896 break; 1897 phdr = sctp_get_next_param(m, offset, 1898 (struct sctp_paramhdr *)store, plen); 1899 if (phdr == NULL) 1900 return (-1); 1901 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1902 num_hmacs = (plen - sizeof(*hmacs)) / 1903 sizeof(hmacs->hmac_ids[0]); 1904 /* validate the hmac list */ 1905 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 1906 #ifdef SCTP_DEBUG 1907 if (sctp_debug_on & SCTP_DEBUG_AUTH1) 1908 printf("SCTP: invalid HMAC param\n"); 1909 #endif 1910 return (-1); 1911 } 1912 got_hmacs = 1; 1913 } 1914 offset += SCTP_SIZE32(plen); 1915 if (offset >= limit) { 1916 break; 1917 } 1918 phdr = sctp_get_next_param(m, offset, &parm_buf, 1919 sizeof(parm_buf)); 1920 } 1921 /* validate authentication required parameters */ 1922 if (got_random && got_hmacs) { 1923 peer_supports_auth = 1; 1924 } else { 1925 peer_supports_auth = 0; 1926 } 1927 if (!sctp_asconf_auth_nochk && peer_supports_asconf && 1928 !peer_supports_auth) { 1929 #ifdef SCTP_DEBUG 1930 if (sctp_debug_on & SCTP_DEBUG_AUTH1) 1931 printf("SCTP: peer supports ASCONF but not AUTH\n"); 1932 #endif 1933 return (-1); 1934 } 1935 return (0); 1936 } 1937 1938 void 1939 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 1940 { 1941 uint16_t chunks_len = 0; 1942 uint16_t hmacs_len = 0; 1943 uint16_t random_len = sctp_auth_random_len; 1944 sctp_key_t *new_key; 1945 uint16_t keylen; 1946 1947 /* initialize hmac list from endpoint */ 1948 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); 1949 if (stcb->asoc.local_hmacs != NULL) { 1950 hmacs_len = stcb->asoc.local_hmacs->num_algo * 1951 sizeof(stcb->asoc.local_hmacs->hmac[0]); 1952 } 1953 /* initialize auth chunks list from endpoint */ 1954 stcb->asoc.local_auth_chunks = 1955 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); 1956 if (stcb->asoc.local_auth_chunks != NULL) { 1957 int i; 1958 1959 for (i = 0; i < 256; i++) { 1960 if (stcb->asoc.local_auth_chunks->chunks[i]) 1961 chunks_len++; 1962 } 1963 } 1964 /* copy defaults from the endpoint */ 1965 stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid; 1966 1967 /* now set the concatenated key (random + chunks + hmacs) */ 1968 keylen = random_len + chunks_len + hmacs_len; 1969 new_key = sctp_alloc_key(keylen); 1970 if (new_key != NULL) { 1971 /* generate and copy in the RANDOM */ 1972 sctp_read_random(new_key->key, random_len); 1973 keylen = random_len; 1974 /* append in the AUTH chunks */ 1975 if (stcb->asoc.local_auth_chunks) { 1976 int i; 1977 1978 for (i = 0; i < 256; i++) { 1979 if (stcb->asoc.local_auth_chunks->chunks[i]) 1980 new_key->key[keylen++] = i; 1981 } 1982 } 1983 /* append in the HMACs */ 1984 sctp_serialize_hmaclist(stcb->asoc.local_hmacs, 1985 new_key->key + keylen); 1986 } 1987 if (stcb->asoc.authinfo.random != NULL) 1988 sctp_free_key(stcb->asoc.authinfo.random); 1989 stcb->asoc.authinfo.random = new_key; 1990 stcb->asoc.authinfo.random_len = random_len; 1991 #ifdef SCTP_AUTH_DRAFT_04 1992 /* don't include the chunks and hmacs for draft -04 */ 1993 stcb->asoc.authinfo.random->keylen = random_len; 1994 #endif 1995 } 1996 1997 1998 #ifdef SCTP_HMAC_TEST 1999 /* 2000 * HMAC and key concatenation tests 2001 */ 2002 static void 2003 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str) 2004 { 2005 uint32_t i; 2006 2007 printf("\n%s: 0x", str); 2008 if (digest == NULL) 2009 return; 2010 2011 for (i = 0; i < digestlen; i++) 2012 printf("%02x", digest[i]); 2013 } 2014 2015 static int 2016 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key, 2017 uint32_t keylen, uint8_t * text, uint32_t textlen, 2018 uint8_t * digest, uint32_t digestlen) 2019 { 2020 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; 2021 2022 printf("\n%s:", str); 2023 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest); 2024 sctp_print_digest(digest, digestlen, "Expected digest"); 2025 sctp_print_digest(computed_digest, digestlen, "Computed digest"); 2026 if (memcmp(digest, computed_digest, digestlen) != 0) { 2027 printf("\nFAILED"); 2028 return (-1); 2029 } else { 2030 printf("\nPASSED"); 2031 return (0); 2032 } 2033 } 2034 2035 2036 /* 2037 * RFC 2202: HMAC-SHA1 test cases 2038 */ 2039 void 2040 sctp_test_hmac_sha1(void) 2041 { 2042 uint8_t *digest; 2043 uint8_t key[128]; 2044 uint32_t keylen; 2045 uint8_t text[128]; 2046 uint32_t textlen; 2047 uint32_t digestlen = 20; 2048 int failed = 0; 2049 2050 /* 2051 * test_case = 1 key = 2052 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len = 20 2053 * data = "Hi There" data_len = 8 digest = 2054 * 0xb617318655057264e28bc0b6fb378c8ef146be00 2055 */ 2056 keylen = 20; 2057 memset(key, 0x0b, keylen); 2058 textlen = 8; 2059 strcpy(text, "Hi There"); 2060 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00"; 2061 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2062 text, textlen, digest, digestlen) < 0) 2063 failed++; 2064 2065 /* 2066 * test_case = 2 key = "Jefe" key_len = 4 data = 2067 * "what do ya want for nothing?" data_len = 28 digest = 2068 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79 2069 */ 2070 keylen = 4; 2071 strcpy(key, "Jefe"); 2072 textlen = 28; 2073 strcpy(text, "what do ya want for nothing?"); 2074 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79"; 2075 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2076 text, textlen, digest, digestlen) < 0) 2077 failed++; 2078 2079 /* 2080 * test_case = 3 key = 2081 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len = 20 2082 * data = 0xdd repeated 50 times data_len = 50 digest 2083 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3 2084 */ 2085 keylen = 20; 2086 memset(key, 0xaa, keylen); 2087 textlen = 50; 2088 memset(text, 0xdd, textlen); 2089 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3"; 2090 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2091 text, textlen, digest, digestlen) < 0) 2092 failed++; 2093 2094 /* 2095 * test_case = 4 key = 2096 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25 2097 * data = 0xcd repeated 50 times data_len = 50 digest 2098 * = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da 2099 */ 2100 keylen = 25; 2101 memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen); 2102 textlen = 50; 2103 memset(text, 0xcd, textlen); 2104 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda"; 2105 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2106 text, textlen, digest, digestlen) < 0) 2107 failed++; 2108 2109 /* 2110 * test_case = 5 key = 2111 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len = 20 2112 * data = "Test With Truncation" data_len = 20 digest 2113 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 = 2114 * 0x4c1a03424b55e07fe7f27be1 2115 */ 2116 keylen = 20; 2117 memset(key, 0x0c, keylen); 2118 textlen = 20; 2119 strcpy(text, "Test With Truncation"); 2120 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04"; 2121 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2122 text, textlen, digest, digestlen) < 0) 2123 failed++; 2124 2125 /* 2126 * test_case = 6 key = 0xaa repeated 80 times key_len 2127 * = 80 data = "Test Using Larger Than Block-Size Key - 2128 * Hash Key First" data_len = 54 digest = 2129 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112 2130 */ 2131 keylen = 80; 2132 memset(key, 0xaa, keylen); 2133 textlen = 54; 2134 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First"); 2135 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12"; 2136 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2137 text, textlen, digest, digestlen) < 0) 2138 failed++; 2139 2140 /* 2141 * test_case = 7 key = 0xaa repeated 80 times key_len 2142 * = 80 data = "Test Using Larger Than Block-Size Key and 2143 * Larger Than One Block-Size Data" data_len = 73 digest = 2144 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91 2145 */ 2146 keylen = 80; 2147 memset(key, 0xaa, keylen); 2148 textlen = 73; 2149 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); 2150 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91"; 2151 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen, 2152 text, textlen, digest, digestlen) < 0) 2153 failed++; 2154 2155 /* done with all tests */ 2156 if (failed) 2157 printf("\nSHA1 test results: %d cases failed", failed); 2158 else 2159 printf("\nSHA1 test results: all test cases passed"); 2160 } 2161 2162 /* 2163 * RFC 2202: HMAC-MD5 test cases 2164 */ 2165 void 2166 sctp_test_hmac_md5(void) 2167 { 2168 uint8_t *digest; 2169 uint8_t key[128]; 2170 uint32_t keylen; 2171 uint8_t text[128]; 2172 uint32_t textlen; 2173 uint32_t digestlen = 16; 2174 int failed = 0; 2175 2176 /* 2177 * test_case = 1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b 2178 * key_len = 16 data = "Hi There" data_len = 8 digest = 2179 * 0x9294727a3638bb1c13f48ef8158bfc9d 2180 */ 2181 keylen = 16; 2182 memset(key, 0x0b, keylen); 2183 textlen = 8; 2184 strcpy(text, "Hi There"); 2185 digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d"; 2186 if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2187 text, textlen, digest, digestlen) < 0) 2188 failed++; 2189 2190 /* 2191 * test_case = 2 key = "Jefe" key_len = 4 data = 2192 * "what do ya want for nothing?" data_len = 28 digest = 2193 * 0x750c783e6ab0b503eaa86e310a5db738 2194 */ 2195 keylen = 4; 2196 strcpy(key, "Jefe"); 2197 textlen = 28; 2198 strcpy(text, "what do ya want for nothing?"); 2199 digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38"; 2200 if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2201 text, textlen, digest, digestlen) < 0) 2202 failed++; 2203 2204 /* 2205 * test_case = 3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa 2206 * key_len = 16 data = 0xdd repeated 50 times data_len = 50 2207 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6 2208 */ 2209 keylen = 16; 2210 memset(key, 0xaa, keylen); 2211 textlen = 50; 2212 memset(text, 0xdd, textlen); 2213 digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6"; 2214 if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2215 text, textlen, digest, digestlen) < 0) 2216 failed++; 2217 2218 /* 2219 * test_case = 4 key = 2220 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25 2221 * data = 0xcd repeated 50 times data_len = 50 digest 2222 * = 0x697eaf0aca3a3aea3a75164746ffaa79 2223 */ 2224 keylen = 25; 2225 memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen); 2226 textlen = 50; 2227 memset(text, 0xcd, textlen); 2228 digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79"; 2229 if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2230 text, textlen, digest, digestlen) < 0) 2231 failed++; 2232 2233 /* 2234 * test_case = 5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c 2235 * key_len = 16 data = "Test With Truncation" data_len = 20 2236 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96 2237 * 0x56461ef2342edc00f9bab995 2238 */ 2239 keylen = 16; 2240 memset(key, 0x0c, keylen); 2241 textlen = 20; 2242 strcpy(text, "Test With Truncation"); 2243 digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c"; 2244 if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2245 text, textlen, digest, digestlen) < 0) 2246 failed++; 2247 2248 /* 2249 * test_case = 6 key = 0xaa repeated 80 times key_len 2250 * = 80 data = "Test Using Larger Than Block-Size Key - 2251 * Hash Key First" data_len = 54 digest = 2252 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd 2253 */ 2254 keylen = 80; 2255 memset(key, 0xaa, keylen); 2256 textlen = 54; 2257 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First"); 2258 digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd"; 2259 if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2260 text, textlen, digest, digestlen) < 0) 2261 failed++; 2262 2263 /* 2264 * test_case = 7 key = 0xaa repeated 80 times key_len 2265 * = 80 data = "Test Using Larger Than Block-Size Key and 2266 * Larger Than One Block-Size Data" data_len = 73 digest = 2267 * 0x6f630fad67cda0ee1fb1f562db3aa53e 2268 */ 2269 keylen = 80; 2270 memset(key, 0xaa, keylen); 2271 textlen = 73; 2272 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"); 2273 digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e"; 2274 if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen, 2275 text, textlen, digest, digestlen) < 0) 2276 failed++; 2277 2278 /* done with all tests */ 2279 if (failed) 2280 printf("\nMD5 test results: %d cases failed", failed); 2281 else 2282 printf("\nMD5 test results: all test cases passed"); 2283 } 2284 2285 /* 2286 * test assoc key concatenation 2287 */ 2288 static int 2289 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2, 2290 sctp_key_t * expected_key) 2291 { 2292 sctp_key_t *key; 2293 int ret_val; 2294 2295 sctp_show_key(key1, "\nkey1"); 2296 sctp_show_key(key2, "\nkey2"); 2297 key = sctp_compute_hashkey(key1, key2, NULL); 2298 sctp_show_key(expected_key, "\nExpected"); 2299 sctp_show_key(key, "\nComputed"); 2300 if (memcmp(key, expected_key, expected_key->keylen) != 0) { 2301 printf("\nFAILED"); 2302 ret_val = -1; 2303 } else { 2304 printf("\nPASSED"); 2305 ret_val = 0; 2306 } 2307 sctp_free_key(key1); 2308 sctp_free_key(key2); 2309 sctp_free_key(expected_key); 2310 sctp_free_key(key); 2311 return (ret_val); 2312 } 2313 2314 2315 void 2316 sctp_test_authkey(void) 2317 { 2318 sctp_key_t *key1, *key2, *expected_key; 2319 int failed = 0; 2320 2321 /* test case 1 */ 2322 key1 = sctp_set_key("\x01\x01\x01\x01", 4); 2323 key2 = sctp_set_key("\x01\x02\x03\x04", 4); 2324 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8); 2325 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2326 failed++; 2327 2328 /* test case 2 */ 2329 key1 = sctp_set_key("\x00\x00\x00\x01", 4); 2330 key2 = sctp_set_key("\x02", 1); 2331 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5); 2332 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2333 failed++; 2334 2335 /* test case 3 */ 2336 key1 = sctp_set_key("\x01", 1); 2337 key2 = sctp_set_key("\x00\x00\x00\x02", 4); 2338 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5); 2339 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2340 failed++; 2341 2342 /* test case 4 */ 2343 key1 = sctp_set_key("\x00\x00\x00\x01", 4); 2344 key2 = sctp_set_key("\x01", 1); 2345 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5); 2346 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2347 failed++; 2348 2349 /* test case 5 */ 2350 key1 = sctp_set_key("\x01", 1); 2351 key2 = sctp_set_key("\x00\x00\x00\x01", 4); 2352 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5); 2353 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2354 failed++; 2355 2356 /* test case 6 */ 2357 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11); 2358 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11); 2359 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22); 2360 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2361 failed++; 2362 2363 /* test case 7 */ 2364 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11); 2365 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11); 2366 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22); 2367 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0) 2368 failed++; 2369 2370 /* done with all tests */ 2371 if (failed) 2372 printf("\nKey concatenation test results: %d cases failed", failed); 2373 else 2374 printf("\nKey concatenation test results: all test cases passed"); 2375 } 2376 2377 2378 #if defined(STANDALONE_HMAC_TEST) 2379 int 2380 main(void) 2381 { 2382 sctp_test_hmac_sha1(); 2383 sctp_test_hmac_md5(); 2384 sctp_test_authkey(); 2385 } 2386 2387 #endif /* STANDALONE_HMAC_TEST */ 2388 2389 #endif /* SCTP_HMAC_TEST */ 2390