1 /*- 2 * SPDX-License-Identifier: BSD-3-Clause 3 * 4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved. 5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved. 6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without 9 * modification, are permitted provided that the following conditions are met: 10 * 11 * a) Redistributions of source code must retain the above copyright notice, 12 * this list of conditions and the following disclaimer. 13 * 14 * b) Redistributions in binary form must reproduce the above copyright 15 * notice, this list of conditions and the following disclaimer in 16 * the documentation and/or other materials provided with the distribution. 17 * 18 * c) Neither the name of Cisco Systems, Inc. nor the names of its 19 * contributors may be used to endorse or promote products derived 20 * from this software without specific prior written permission. 21 * 22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, 24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF 32 * THE POSSIBILITY OF SUCH DAMAGE. 33 */ 34 35 #include <sys/cdefs.h> 36 __FBSDID("$FreeBSD$"); 37 38 #include <netinet/sctp_os.h> 39 #include <netinet/sctp.h> 40 #include <netinet/sctp_header.h> 41 #include <netinet/sctp_pcb.h> 42 #include <netinet/sctp_var.h> 43 #include <netinet/sctp_sysctl.h> 44 #include <netinet/sctputil.h> 45 #include <netinet/sctp_indata.h> 46 #include <netinet/sctp_output.h> 47 #include <netinet/sctp_auth.h> 48 49 #ifdef SCTP_DEBUG 50 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1) 51 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2) 52 #endif /* SCTP_DEBUG */ 53 54 55 void 56 sctp_clear_chunklist(sctp_auth_chklist_t *chklist) 57 { 58 memset(chklist, 0, sizeof(*chklist)); 59 /* chklist->num_chunks = 0; */ 60 } 61 62 sctp_auth_chklist_t * 63 sctp_alloc_chunklist(void) 64 { 65 sctp_auth_chklist_t *chklist; 66 67 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist), 68 SCTP_M_AUTH_CL); 69 if (chklist == NULL) { 70 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n"); 71 } else { 72 sctp_clear_chunklist(chklist); 73 } 74 return (chklist); 75 } 76 77 void 78 sctp_free_chunklist(sctp_auth_chklist_t *list) 79 { 80 if (list != NULL) 81 SCTP_FREE(list, SCTP_M_AUTH_CL); 82 } 83 84 sctp_auth_chklist_t * 85 sctp_copy_chunklist(sctp_auth_chklist_t *list) 86 { 87 sctp_auth_chklist_t *new_list; 88 89 if (list == NULL) 90 return (NULL); 91 92 /* get a new list */ 93 new_list = sctp_alloc_chunklist(); 94 if (new_list == NULL) 95 return (NULL); 96 /* copy it */ 97 memcpy(new_list, list, sizeof(*new_list)); 98 99 return (new_list); 100 } 101 102 103 /* 104 * add a chunk to the required chunks list 105 */ 106 int 107 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list) 108 { 109 if (list == NULL) 110 return (-1); 111 112 /* is chunk restricted? */ 113 if ((chunk == SCTP_INITIATION) || 114 (chunk == SCTP_INITIATION_ACK) || 115 (chunk == SCTP_SHUTDOWN_COMPLETE) || 116 (chunk == SCTP_AUTHENTICATION)) { 117 return (-1); 118 } 119 if (list->chunks[chunk] == 0) { 120 list->chunks[chunk] = 1; 121 list->num_chunks++; 122 SCTPDBG(SCTP_DEBUG_AUTH1, 123 "SCTP: added chunk %u (0x%02x) to Auth list\n", 124 chunk, chunk); 125 } 126 return (0); 127 } 128 129 /* 130 * delete a chunk from the required chunks list 131 */ 132 int 133 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list) 134 { 135 if (list == NULL) 136 return (-1); 137 138 if (list->chunks[chunk] == 1) { 139 list->chunks[chunk] = 0; 140 list->num_chunks--; 141 SCTPDBG(SCTP_DEBUG_AUTH1, 142 "SCTP: deleted chunk %u (0x%02x) from Auth list\n", 143 chunk, chunk); 144 } 145 return (0); 146 } 147 148 size_t 149 sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list) 150 { 151 if (list == NULL) 152 return (0); 153 else 154 return (list->num_chunks); 155 } 156 157 /* 158 * return the current number and list of required chunks caller must 159 * guarantee ptr has space for up to 256 bytes 160 */ 161 int 162 sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr) 163 { 164 int i, count = 0; 165 166 if (list == NULL) 167 return (0); 168 169 for (i = 0; i < 256; i++) { 170 if (list->chunks[i] != 0) { 171 *ptr++ = i; 172 count++; 173 } 174 } 175 return (count); 176 } 177 178 int 179 sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr) 180 { 181 int i, size = 0; 182 183 if (list == NULL) 184 return (0); 185 186 if (list->num_chunks <= 32) { 187 /* just list them, one byte each */ 188 for (i = 0; i < 256; i++) { 189 if (list->chunks[i] != 0) { 190 *ptr++ = i; 191 size++; 192 } 193 } 194 } else { 195 int index, offset; 196 197 /* pack into a 32 byte bitfield */ 198 for (i = 0; i < 256; i++) { 199 if (list->chunks[i] != 0) { 200 index = i / 8; 201 offset = i % 8; 202 ptr[index] |= (1 << offset); 203 } 204 } 205 size = 32; 206 } 207 return (size); 208 } 209 210 int 211 sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks, 212 sctp_auth_chklist_t *list) 213 { 214 int i; 215 int size; 216 217 if (list == NULL) 218 return (0); 219 220 if (num_chunks <= 32) { 221 /* just pull them, one byte each */ 222 for (i = 0; i < num_chunks; i++) { 223 (void)sctp_auth_add_chunk(*ptr++, list); 224 } 225 size = num_chunks; 226 } else { 227 int index, offset; 228 229 /* unpack from a 32 byte bitfield */ 230 for (index = 0; index < 32; index++) { 231 for (offset = 0; offset < 8; offset++) { 232 if (ptr[index] & (1 << offset)) { 233 (void)sctp_auth_add_chunk((index * 8) + offset, list); 234 } 235 } 236 } 237 size = 32; 238 } 239 return (size); 240 } 241 242 243 /* 244 * allocate structure space for a key of length keylen 245 */ 246 sctp_key_t * 247 sctp_alloc_key(uint32_t keylen) 248 { 249 sctp_key_t *new_key; 250 251 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen, 252 SCTP_M_AUTH_KY); 253 if (new_key == NULL) { 254 /* out of memory */ 255 return (NULL); 256 } 257 new_key->keylen = keylen; 258 return (new_key); 259 } 260 261 void 262 sctp_free_key(sctp_key_t *key) 263 { 264 if (key != NULL) 265 SCTP_FREE(key, SCTP_M_AUTH_KY); 266 } 267 268 void 269 sctp_print_key(sctp_key_t *key, const char *str) 270 { 271 uint32_t i; 272 273 if (key == NULL) { 274 SCTP_PRINTF("%s: [Null key]\n", str); 275 return; 276 } 277 SCTP_PRINTF("%s: len %u, ", str, key->keylen); 278 if (key->keylen) { 279 for (i = 0; i < key->keylen; i++) 280 SCTP_PRINTF("%02x", key->key[i]); 281 SCTP_PRINTF("\n"); 282 } else { 283 SCTP_PRINTF("[Null key]\n"); 284 } 285 } 286 287 void 288 sctp_show_key(sctp_key_t *key, const char *str) 289 { 290 uint32_t i; 291 292 if (key == NULL) { 293 SCTP_PRINTF("%s: [Null key]\n", str); 294 return; 295 } 296 SCTP_PRINTF("%s: len %u, ", str, key->keylen); 297 if (key->keylen) { 298 for (i = 0; i < key->keylen; i++) 299 SCTP_PRINTF("%02x", key->key[i]); 300 SCTP_PRINTF("\n"); 301 } else { 302 SCTP_PRINTF("[Null key]\n"); 303 } 304 } 305 306 static uint32_t 307 sctp_get_keylen(sctp_key_t *key) 308 { 309 if (key != NULL) 310 return (key->keylen); 311 else 312 return (0); 313 } 314 315 /* 316 * generate a new random key of length 'keylen' 317 */ 318 sctp_key_t * 319 sctp_generate_random_key(uint32_t keylen) 320 { 321 sctp_key_t *new_key; 322 323 new_key = sctp_alloc_key(keylen); 324 if (new_key == NULL) { 325 /* out of memory */ 326 return (NULL); 327 } 328 SCTP_READ_RANDOM(new_key->key, keylen); 329 new_key->keylen = keylen; 330 return (new_key); 331 } 332 333 sctp_key_t * 334 sctp_set_key(uint8_t *key, uint32_t keylen) 335 { 336 sctp_key_t *new_key; 337 338 new_key = sctp_alloc_key(keylen); 339 if (new_key == NULL) { 340 /* out of memory */ 341 return (NULL); 342 } 343 memcpy(new_key->key, key, keylen); 344 return (new_key); 345 } 346 347 /*- 348 * given two keys of variable size, compute which key is "larger/smaller" 349 * returns: 1 if key1 > key2 350 * -1 if key1 < key2 351 * 0 if key1 = key2 352 */ 353 static int 354 sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2) 355 { 356 uint32_t maxlen; 357 uint32_t i; 358 uint32_t key1len, key2len; 359 uint8_t *key_1, *key_2; 360 uint8_t val1, val2; 361 362 /* sanity/length check */ 363 key1len = sctp_get_keylen(key1); 364 key2len = sctp_get_keylen(key2); 365 if ((key1len == 0) && (key2len == 0)) 366 return (0); 367 else if (key1len == 0) 368 return (-1); 369 else if (key2len == 0) 370 return (1); 371 372 if (key1len < key2len) { 373 maxlen = key2len; 374 } else { 375 maxlen = key1len; 376 } 377 key_1 = key1->key; 378 key_2 = key2->key; 379 /* check for numeric equality */ 380 for (i = 0; i < maxlen; i++) { 381 /* left-pad with zeros */ 382 val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++); 383 val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++); 384 if (val1 > val2) { 385 return (1); 386 } else if (val1 < val2) { 387 return (-1); 388 } 389 } 390 /* keys are equal value, so check lengths */ 391 if (key1len == key2len) 392 return (0); 393 else if (key1len < key2len) 394 return (-1); 395 else 396 return (1); 397 } 398 399 /* 400 * generate the concatenated keying material based on the two keys and the 401 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific 402 * order for concatenation 403 */ 404 sctp_key_t * 405 sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared) 406 { 407 uint32_t keylen; 408 sctp_key_t *new_key; 409 uint8_t *key_ptr; 410 411 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) + 412 sctp_get_keylen(shared); 413 414 if (keylen > 0) { 415 /* get space for the new key */ 416 new_key = sctp_alloc_key(keylen); 417 if (new_key == NULL) { 418 /* out of memory */ 419 return (NULL); 420 } 421 new_key->keylen = keylen; 422 key_ptr = new_key->key; 423 } else { 424 /* all keys empty/null?! */ 425 return (NULL); 426 } 427 428 /* concatenate the keys */ 429 if (sctp_compare_key(key1, key2) <= 0) { 430 /* key is shared + key1 + key2 */ 431 if (sctp_get_keylen(shared)) { 432 memcpy(key_ptr, shared->key, shared->keylen); 433 key_ptr += shared->keylen; 434 } 435 if (sctp_get_keylen(key1)) { 436 memcpy(key_ptr, key1->key, key1->keylen); 437 key_ptr += key1->keylen; 438 } 439 if (sctp_get_keylen(key2)) { 440 memcpy(key_ptr, key2->key, key2->keylen); 441 } 442 } else { 443 /* key is shared + key2 + key1 */ 444 if (sctp_get_keylen(shared)) { 445 memcpy(key_ptr, shared->key, shared->keylen); 446 key_ptr += shared->keylen; 447 } 448 if (sctp_get_keylen(key2)) { 449 memcpy(key_ptr, key2->key, key2->keylen); 450 key_ptr += key2->keylen; 451 } 452 if (sctp_get_keylen(key1)) { 453 memcpy(key_ptr, key1->key, key1->keylen); 454 } 455 } 456 return (new_key); 457 } 458 459 460 sctp_sharedkey_t * 461 sctp_alloc_sharedkey(void) 462 { 463 sctp_sharedkey_t *new_key; 464 465 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key), 466 SCTP_M_AUTH_KY); 467 if (new_key == NULL) { 468 /* out of memory */ 469 return (NULL); 470 } 471 new_key->keyid = 0; 472 new_key->key = NULL; 473 new_key->refcount = 1; 474 new_key->deactivated = 0; 475 return (new_key); 476 } 477 478 void 479 sctp_free_sharedkey(sctp_sharedkey_t *skey) 480 { 481 if (skey == NULL) 482 return; 483 484 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) { 485 if (skey->key != NULL) 486 sctp_free_key(skey->key); 487 SCTP_FREE(skey, SCTP_M_AUTH_KY); 488 } 489 } 490 491 sctp_sharedkey_t * 492 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id) 493 { 494 sctp_sharedkey_t *skey; 495 496 LIST_FOREACH(skey, shared_keys, next) { 497 if (skey->keyid == key_id) 498 return (skey); 499 } 500 return (NULL); 501 } 502 503 int 504 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 505 sctp_sharedkey_t *new_skey) 506 { 507 sctp_sharedkey_t *skey; 508 509 if ((shared_keys == NULL) || (new_skey == NULL)) 510 return (EINVAL); 511 512 /* insert into an empty list? */ 513 if (LIST_EMPTY(shared_keys)) { 514 LIST_INSERT_HEAD(shared_keys, new_skey, next); 515 return (0); 516 } 517 /* insert into the existing list, ordered by key id */ 518 LIST_FOREACH(skey, shared_keys, next) { 519 if (new_skey->keyid < skey->keyid) { 520 /* insert it before here */ 521 LIST_INSERT_BEFORE(skey, new_skey, next); 522 return (0); 523 } else if (new_skey->keyid == skey->keyid) { 524 /* replace the existing key */ 525 /* verify this key *can* be replaced */ 526 if ((skey->deactivated) || (skey->refcount > 1)) { 527 SCTPDBG(SCTP_DEBUG_AUTH1, 528 "can't replace shared key id %u\n", 529 new_skey->keyid); 530 return (EBUSY); 531 } 532 SCTPDBG(SCTP_DEBUG_AUTH1, 533 "replacing shared key id %u\n", 534 new_skey->keyid); 535 LIST_INSERT_BEFORE(skey, new_skey, next); 536 LIST_REMOVE(skey, next); 537 sctp_free_sharedkey(skey); 538 return (0); 539 } 540 if (LIST_NEXT(skey, next) == NULL) { 541 /* belongs at the end of the list */ 542 LIST_INSERT_AFTER(skey, new_skey, next); 543 return (0); 544 } 545 } 546 /* shouldn't reach here */ 547 return (EINVAL); 548 } 549 550 void 551 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id) 552 { 553 sctp_sharedkey_t *skey; 554 555 /* find the shared key */ 556 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 557 558 /* bump the ref count */ 559 if (skey) { 560 atomic_add_int(&skey->refcount, 1); 561 SCTPDBG(SCTP_DEBUG_AUTH2, 562 "%s: stcb %p key %u refcount acquire to %d\n", 563 __func__, (void *)stcb, key_id, skey->refcount); 564 } 565 } 566 567 void 568 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked 569 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 570 SCTP_UNUSED 571 #endif 572 ) 573 { 574 sctp_sharedkey_t *skey; 575 576 /* find the shared key */ 577 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 578 579 /* decrement the ref count */ 580 if (skey) { 581 SCTPDBG(SCTP_DEBUG_AUTH2, 582 "%s: stcb %p key %u refcount release to %d\n", 583 __func__, (void *)stcb, key_id, skey->refcount); 584 585 /* see if a notification should be generated */ 586 if ((skey->refcount <= 2) && (skey->deactivated)) { 587 /* notify ULP that key is no longer used */ 588 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, 589 key_id, 0, so_locked); 590 SCTPDBG(SCTP_DEBUG_AUTH2, 591 "%s: stcb %p key %u no longer used, %d\n", 592 __func__, (void *)stcb, key_id, skey->refcount); 593 } 594 sctp_free_sharedkey(skey); 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 if (sctp_insert_sharedkey(dest, new_skey)) { 628 sctp_free_sharedkey(new_skey); 629 } else { 630 count++; 631 } 632 } 633 } 634 return (count); 635 } 636 637 638 sctp_hmaclist_t * 639 sctp_alloc_hmaclist(uint16_t num_hmacs) 640 { 641 sctp_hmaclist_t *new_list; 642 int alloc_size; 643 644 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]); 645 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size, 646 SCTP_M_AUTH_HL); 647 if (new_list == NULL) { 648 /* out of memory */ 649 return (NULL); 650 } 651 new_list->max_algo = num_hmacs; 652 new_list->num_algo = 0; 653 return (new_list); 654 } 655 656 void 657 sctp_free_hmaclist(sctp_hmaclist_t *list) 658 { 659 if (list != NULL) { 660 SCTP_FREE(list, SCTP_M_AUTH_HL); 661 } 662 } 663 664 int 665 sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id) 666 { 667 int i; 668 669 if (list == NULL) 670 return (-1); 671 if (list->num_algo == list->max_algo) { 672 SCTPDBG(SCTP_DEBUG_AUTH1, 673 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id); 674 return (-1); 675 } 676 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) && 677 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) { 678 return (-1); 679 } 680 /* Now is it already in the list */ 681 for (i = 0; i < list->num_algo; i++) { 682 if (list->hmac[i] == hmac_id) { 683 /* already in list */ 684 return (-1); 685 } 686 } 687 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id); 688 list->hmac[list->num_algo++] = hmac_id; 689 return (0); 690 } 691 692 sctp_hmaclist_t * 693 sctp_copy_hmaclist(sctp_hmaclist_t *list) 694 { 695 sctp_hmaclist_t *new_list; 696 int i; 697 698 if (list == NULL) 699 return (NULL); 700 /* get a new list */ 701 new_list = sctp_alloc_hmaclist(list->max_algo); 702 if (new_list == NULL) 703 return (NULL); 704 /* copy it */ 705 new_list->max_algo = list->max_algo; 706 new_list->num_algo = list->num_algo; 707 for (i = 0; i < list->num_algo; i++) 708 new_list->hmac[i] = list->hmac[i]; 709 return (new_list); 710 } 711 712 sctp_hmaclist_t * 713 sctp_default_supported_hmaclist(void) 714 { 715 sctp_hmaclist_t *new_list; 716 717 new_list = sctp_alloc_hmaclist(2); 718 if (new_list == NULL) 719 return (NULL); 720 /* We prefer SHA256, so list it first */ 721 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256); 722 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1); 723 return (new_list); 724 } 725 726 /*- 727 * HMAC algos are listed in priority/preference order 728 * find the best HMAC id to use for the peer based on local support 729 */ 730 uint16_t 731 sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local) 732 { 733 int i, j; 734 735 if ((local == NULL) || (peer == NULL)) 736 return (SCTP_AUTH_HMAC_ID_RSVD); 737 738 for (i = 0; i < peer->num_algo; i++) { 739 for (j = 0; j < local->num_algo; j++) { 740 if (peer->hmac[i] == local->hmac[j]) { 741 /* found the "best" one */ 742 SCTPDBG(SCTP_DEBUG_AUTH1, 743 "SCTP: negotiated peer HMAC id %u\n", 744 peer->hmac[i]); 745 return (peer->hmac[i]); 746 } 747 } 748 } 749 /* didn't find one! */ 750 return (SCTP_AUTH_HMAC_ID_RSVD); 751 } 752 753 /*- 754 * serialize the HMAC algo list and return space used 755 * caller must guarantee ptr has appropriate space 756 */ 757 int 758 sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr) 759 { 760 int i; 761 uint16_t hmac_id; 762 763 if (list == NULL) 764 return (0); 765 766 for (i = 0; i < list->num_algo; i++) { 767 hmac_id = htons(list->hmac[i]); 768 memcpy(ptr, &hmac_id, sizeof(hmac_id)); 769 ptr += sizeof(hmac_id); 770 } 771 return (list->num_algo * sizeof(hmac_id)); 772 } 773 774 int 775 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs) 776 { 777 uint32_t i; 778 779 for (i = 0; i < num_hmacs; i++) { 780 if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) { 781 return (0); 782 } 783 } 784 return (-1); 785 } 786 787 sctp_authinfo_t * 788 sctp_alloc_authinfo(void) 789 { 790 sctp_authinfo_t *new_authinfo; 791 792 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo), 793 SCTP_M_AUTH_IF); 794 795 if (new_authinfo == NULL) { 796 /* out of memory */ 797 return (NULL); 798 } 799 memset(new_authinfo, 0, sizeof(*new_authinfo)); 800 return (new_authinfo); 801 } 802 803 void 804 sctp_free_authinfo(sctp_authinfo_t *authinfo) 805 { 806 if (authinfo == NULL) 807 return; 808 809 if (authinfo->random != NULL) 810 sctp_free_key(authinfo->random); 811 if (authinfo->peer_random != NULL) 812 sctp_free_key(authinfo->peer_random); 813 if (authinfo->assoc_key != NULL) 814 sctp_free_key(authinfo->assoc_key); 815 if (authinfo->recv_key != NULL) 816 sctp_free_key(authinfo->recv_key); 817 818 /* We are NOT dynamically allocating authinfo's right now... */ 819 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */ 820 } 821 822 823 uint32_t 824 sctp_get_auth_chunk_len(uint16_t hmac_algo) 825 { 826 int size; 827 828 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo); 829 return (SCTP_SIZE32(size)); 830 } 831 832 uint32_t 833 sctp_get_hmac_digest_len(uint16_t hmac_algo) 834 { 835 switch (hmac_algo) { 836 case SCTP_AUTH_HMAC_ID_SHA1: 837 return (SCTP_AUTH_DIGEST_LEN_SHA1); 838 case SCTP_AUTH_HMAC_ID_SHA256: 839 return (SCTP_AUTH_DIGEST_LEN_SHA256); 840 default: 841 /* unknown HMAC algorithm: can't do anything */ 842 return (0); 843 } /* end switch */ 844 } 845 846 static inline int 847 sctp_get_hmac_block_len(uint16_t hmac_algo) 848 { 849 switch (hmac_algo) { 850 case SCTP_AUTH_HMAC_ID_SHA1: 851 return (64); 852 case SCTP_AUTH_HMAC_ID_SHA256: 853 return (64); 854 case SCTP_AUTH_HMAC_ID_RSVD: 855 default: 856 /* unknown HMAC algorithm: can't do anything */ 857 return (0); 858 } /* end switch */ 859 } 860 861 static void 862 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx) 863 { 864 switch (hmac_algo) { 865 case SCTP_AUTH_HMAC_ID_SHA1: 866 SCTP_SHA1_INIT(&ctx->sha1); 867 break; 868 case SCTP_AUTH_HMAC_ID_SHA256: 869 SCTP_SHA256_INIT(&ctx->sha256); 870 break; 871 case SCTP_AUTH_HMAC_ID_RSVD: 872 default: 873 /* unknown HMAC algorithm: can't do anything */ 874 return; 875 } /* end switch */ 876 } 877 878 static void 879 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx, 880 uint8_t *text, uint32_t textlen) 881 { 882 switch (hmac_algo) { 883 case SCTP_AUTH_HMAC_ID_SHA1: 884 SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen); 885 break; 886 case SCTP_AUTH_HMAC_ID_SHA256: 887 SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen); 888 break; 889 case SCTP_AUTH_HMAC_ID_RSVD: 890 default: 891 /* unknown HMAC algorithm: can't do anything */ 892 return; 893 } /* end switch */ 894 } 895 896 static void 897 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx, 898 uint8_t *digest) 899 { 900 switch (hmac_algo) { 901 case SCTP_AUTH_HMAC_ID_SHA1: 902 SCTP_SHA1_FINAL(digest, &ctx->sha1); 903 break; 904 case SCTP_AUTH_HMAC_ID_SHA256: 905 SCTP_SHA256_FINAL(digest, &ctx->sha256); 906 break; 907 case SCTP_AUTH_HMAC_ID_RSVD: 908 default: 909 /* unknown HMAC algorithm: can't do anything */ 910 return; 911 } /* end switch */ 912 } 913 914 /*- 915 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104) 916 * 917 * Compute the HMAC digest using the desired hash key, text, and HMAC 918 * algorithm. Resulting digest is placed in 'digest' and digest length 919 * is returned, if the HMAC was performed. 920 * 921 * WARNING: it is up to the caller to supply sufficient space to hold the 922 * resultant digest. 923 */ 924 uint32_t 925 sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen, 926 uint8_t *text, uint32_t textlen, uint8_t *digest) 927 { 928 uint32_t digestlen; 929 uint32_t blocklen; 930 sctp_hash_context_t ctx; 931 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 932 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 933 uint32_t i; 934 935 /* sanity check the material and length */ 936 if ((key == NULL) || (keylen == 0) || (text == NULL) || 937 (textlen == 0) || (digest == NULL)) { 938 /* can't do HMAC with empty key or text or digest store */ 939 return (0); 940 } 941 /* validate the hmac algo and get the digest length */ 942 digestlen = sctp_get_hmac_digest_len(hmac_algo); 943 if (digestlen == 0) 944 return (0); 945 946 /* hash the key if it is longer than the hash block size */ 947 blocklen = sctp_get_hmac_block_len(hmac_algo); 948 if (keylen > blocklen) { 949 sctp_hmac_init(hmac_algo, &ctx); 950 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 951 sctp_hmac_final(hmac_algo, &ctx, temp); 952 /* set the hashed key as the key */ 953 keylen = digestlen; 954 key = temp; 955 } 956 /* initialize the inner/outer pads with the key and "append" zeroes */ 957 memset(ipad, 0, blocklen); 958 memset(opad, 0, blocklen); 959 memcpy(ipad, key, keylen); 960 memcpy(opad, key, keylen); 961 962 /* XOR the key with ipad and opad values */ 963 for (i = 0; i < blocklen; i++) { 964 ipad[i] ^= 0x36; 965 opad[i] ^= 0x5c; 966 } 967 968 /* perform inner hash */ 969 sctp_hmac_init(hmac_algo, &ctx); 970 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 971 sctp_hmac_update(hmac_algo, &ctx, text, textlen); 972 sctp_hmac_final(hmac_algo, &ctx, temp); 973 974 /* perform outer hash */ 975 sctp_hmac_init(hmac_algo, &ctx); 976 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 977 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 978 sctp_hmac_final(hmac_algo, &ctx, digest); 979 980 return (digestlen); 981 } 982 983 /* mbuf version */ 984 uint32_t 985 sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen, 986 struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer) 987 { 988 uint32_t digestlen; 989 uint32_t blocklen; 990 sctp_hash_context_t ctx; 991 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 992 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 993 uint32_t i; 994 struct mbuf *m_tmp; 995 996 /* sanity check the material and length */ 997 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) { 998 /* can't do HMAC with empty key or text or digest store */ 999 return (0); 1000 } 1001 /* validate the hmac algo and get the digest length */ 1002 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1003 if (digestlen == 0) 1004 return (0); 1005 1006 /* hash the key if it is longer than the hash block size */ 1007 blocklen = sctp_get_hmac_block_len(hmac_algo); 1008 if (keylen > blocklen) { 1009 sctp_hmac_init(hmac_algo, &ctx); 1010 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1011 sctp_hmac_final(hmac_algo, &ctx, temp); 1012 /* set the hashed key as the key */ 1013 keylen = digestlen; 1014 key = temp; 1015 } 1016 /* initialize the inner/outer pads with the key and "append" zeroes */ 1017 memset(ipad, 0, blocklen); 1018 memset(opad, 0, blocklen); 1019 memcpy(ipad, key, keylen); 1020 memcpy(opad, key, keylen); 1021 1022 /* XOR the key with ipad and opad values */ 1023 for (i = 0; i < blocklen; i++) { 1024 ipad[i] ^= 0x36; 1025 opad[i] ^= 0x5c; 1026 } 1027 1028 /* perform inner hash */ 1029 sctp_hmac_init(hmac_algo, &ctx); 1030 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1031 /* find the correct starting mbuf and offset (get start of text) */ 1032 m_tmp = m; 1033 while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) { 1034 m_offset -= SCTP_BUF_LEN(m_tmp); 1035 m_tmp = SCTP_BUF_NEXT(m_tmp); 1036 } 1037 /* now use the rest of the mbuf chain for the text */ 1038 while (m_tmp != NULL) { 1039 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) { 1040 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset, 1041 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset)); 1042 } else { 1043 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset, 1044 SCTP_BUF_LEN(m_tmp) - m_offset); 1045 } 1046 1047 /* clear the offset since it's only for the first mbuf */ 1048 m_offset = 0; 1049 m_tmp = SCTP_BUF_NEXT(m_tmp); 1050 } 1051 sctp_hmac_final(hmac_algo, &ctx, temp); 1052 1053 /* perform outer hash */ 1054 sctp_hmac_init(hmac_algo, &ctx); 1055 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1056 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1057 sctp_hmac_final(hmac_algo, &ctx, digest); 1058 1059 return (digestlen); 1060 } 1061 1062 /* 1063 * computes the requested HMAC using a key struct (which may be modified if 1064 * the keylen exceeds the HMAC block len). 1065 */ 1066 uint32_t 1067 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text, 1068 uint32_t textlen, uint8_t *digest) 1069 { 1070 uint32_t digestlen; 1071 uint32_t blocklen; 1072 sctp_hash_context_t ctx; 1073 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1074 1075 /* sanity check */ 1076 if ((key == NULL) || (text == NULL) || (textlen == 0) || 1077 (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 (key->keylen > blocklen) { 1089 sctp_hmac_init(hmac_algo, &ctx); 1090 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1091 sctp_hmac_final(hmac_algo, &ctx, temp); 1092 /* save the hashed key as the new key */ 1093 key->keylen = digestlen; 1094 memcpy(key->key, temp, key->keylen); 1095 } 1096 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen, 1097 digest)); 1098 } 1099 1100 /* mbuf version */ 1101 uint32_t 1102 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m, 1103 uint32_t m_offset, uint8_t *digest) 1104 { 1105 uint32_t digestlen; 1106 uint32_t blocklen; 1107 sctp_hash_context_t ctx; 1108 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1109 1110 /* sanity check */ 1111 if ((key == NULL) || (m == NULL) || (digest == NULL)) { 1112 /* can't do HMAC with empty key or text or digest store */ 1113 return (0); 1114 } 1115 /* validate the hmac algo and get the digest length */ 1116 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1117 if (digestlen == 0) 1118 return (0); 1119 1120 /* hash the key if it is longer than the hash block size */ 1121 blocklen = sctp_get_hmac_block_len(hmac_algo); 1122 if (key->keylen > blocklen) { 1123 sctp_hmac_init(hmac_algo, &ctx); 1124 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1125 sctp_hmac_final(hmac_algo, &ctx, temp); 1126 /* save the hashed key as the new key */ 1127 key->keylen = digestlen; 1128 memcpy(key->key, temp, key->keylen); 1129 } 1130 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0)); 1131 } 1132 1133 int 1134 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id) 1135 { 1136 int i; 1137 1138 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD)) 1139 return (0); 1140 1141 for (i = 0; i < list->num_algo; i++) 1142 if (list->hmac[i] == id) 1143 return (1); 1144 1145 /* not in the list */ 1146 return (0); 1147 } 1148 1149 1150 /*- 1151 * clear any cached key(s) if they match the given key id on an association. 1152 * the cached key(s) will be recomputed and re-cached at next use. 1153 * ASSUMES TCB_LOCK is already held 1154 */ 1155 void 1156 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid) 1157 { 1158 if (stcb == NULL) 1159 return; 1160 1161 if (keyid == stcb->asoc.authinfo.assoc_keyid) { 1162 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1163 stcb->asoc.authinfo.assoc_key = NULL; 1164 } 1165 if (keyid == stcb->asoc.authinfo.recv_keyid) { 1166 sctp_free_key(stcb->asoc.authinfo.recv_key); 1167 stcb->asoc.authinfo.recv_key = NULL; 1168 } 1169 } 1170 1171 /*- 1172 * clear any cached key(s) if they match the given key id for all assocs on 1173 * an endpoint. 1174 * ASSUMES INP_WLOCK is already held 1175 */ 1176 void 1177 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid) 1178 { 1179 struct sctp_tcb *stcb; 1180 1181 if (inp == NULL) 1182 return; 1183 1184 /* clear the cached keys on all assocs on this instance */ 1185 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1186 SCTP_TCB_LOCK(stcb); 1187 sctp_clear_cachedkeys(stcb, keyid); 1188 SCTP_TCB_UNLOCK(stcb); 1189 } 1190 } 1191 1192 /*- 1193 * delete a shared key from an association 1194 * ASSUMES TCB_LOCK is already held 1195 */ 1196 int 1197 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1198 { 1199 sctp_sharedkey_t *skey; 1200 1201 if (stcb == NULL) 1202 return (-1); 1203 1204 /* is the keyid the assoc active sending key */ 1205 if (keyid == stcb->asoc.authinfo.active_keyid) 1206 return (-1); 1207 1208 /* does the key exist? */ 1209 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1210 if (skey == NULL) 1211 return (-1); 1212 1213 /* are there other refcount holders on the key? */ 1214 if (skey->refcount > 1) 1215 return (-1); 1216 1217 /* remove it */ 1218 LIST_REMOVE(skey, next); 1219 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1220 1221 /* clear any cached keys */ 1222 sctp_clear_cachedkeys(stcb, keyid); 1223 return (0); 1224 } 1225 1226 /*- 1227 * deletes a shared key from the endpoint 1228 * ASSUMES INP_WLOCK is already held 1229 */ 1230 int 1231 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1232 { 1233 sctp_sharedkey_t *skey; 1234 1235 if (inp == NULL) 1236 return (-1); 1237 1238 /* is the keyid the active sending key on the endpoint */ 1239 if (keyid == inp->sctp_ep.default_keyid) 1240 return (-1); 1241 1242 /* does the key exist? */ 1243 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1244 if (skey == NULL) 1245 return (-1); 1246 1247 /* endpoint keys are not refcounted */ 1248 1249 /* remove it */ 1250 LIST_REMOVE(skey, next); 1251 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1252 1253 /* clear any cached keys */ 1254 sctp_clear_cachedkeys_ep(inp, keyid); 1255 return (0); 1256 } 1257 1258 /*- 1259 * set the active key on an association 1260 * ASSUMES TCB_LOCK is already held 1261 */ 1262 int 1263 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid) 1264 { 1265 sctp_sharedkey_t *skey = NULL; 1266 1267 /* find the key on the assoc */ 1268 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1269 if (skey == NULL) { 1270 /* that key doesn't exist */ 1271 return (-1); 1272 } 1273 if ((skey->deactivated) && (skey->refcount > 1)) { 1274 /* can't reactivate a deactivated key with other refcounts */ 1275 return (-1); 1276 } 1277 1278 /* set the (new) active key */ 1279 stcb->asoc.authinfo.active_keyid = keyid; 1280 /* reset the deactivated flag */ 1281 skey->deactivated = 0; 1282 1283 return (0); 1284 } 1285 1286 /*- 1287 * set the active key on an endpoint 1288 * ASSUMES INP_WLOCK is already held 1289 */ 1290 int 1291 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1292 { 1293 sctp_sharedkey_t *skey; 1294 1295 /* find the key */ 1296 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1297 if (skey == NULL) { 1298 /* that key doesn't exist */ 1299 return (-1); 1300 } 1301 inp->sctp_ep.default_keyid = keyid; 1302 return (0); 1303 } 1304 1305 /*- 1306 * deactivates a shared key from the association 1307 * ASSUMES INP_WLOCK is already held 1308 */ 1309 int 1310 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1311 { 1312 sctp_sharedkey_t *skey; 1313 1314 if (stcb == NULL) 1315 return (-1); 1316 1317 /* is the keyid the assoc active sending key */ 1318 if (keyid == stcb->asoc.authinfo.active_keyid) 1319 return (-1); 1320 1321 /* does the key exist? */ 1322 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1323 if (skey == NULL) 1324 return (-1); 1325 1326 /* are there other refcount holders on the key? */ 1327 if (skey->refcount == 1) { 1328 /* no other users, send a notification for this key */ 1329 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0, 1330 SCTP_SO_LOCKED); 1331 } 1332 1333 /* mark the key as deactivated */ 1334 skey->deactivated = 1; 1335 1336 return (0); 1337 } 1338 1339 /*- 1340 * deactivates a shared key from the endpoint 1341 * ASSUMES INP_WLOCK is already held 1342 */ 1343 int 1344 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1345 { 1346 sctp_sharedkey_t *skey; 1347 1348 if (inp == NULL) 1349 return (-1); 1350 1351 /* is the keyid the active sending key on the endpoint */ 1352 if (keyid == inp->sctp_ep.default_keyid) 1353 return (-1); 1354 1355 /* does the key exist? */ 1356 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1357 if (skey == NULL) 1358 return (-1); 1359 1360 /* endpoint keys are not refcounted */ 1361 1362 /* remove it */ 1363 LIST_REMOVE(skey, next); 1364 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1365 1366 return (0); 1367 } 1368 1369 /* 1370 * get local authentication parameters from cookie (from INIT-ACK) 1371 */ 1372 void 1373 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 1374 uint32_t offset, uint32_t length) 1375 { 1376 struct sctp_paramhdr *phdr, tmp_param; 1377 uint16_t plen, ptype; 1378 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 1379 struct sctp_auth_random *p_random = NULL; 1380 uint16_t random_len = 0; 1381 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 1382 struct sctp_auth_hmac_algo *hmacs = NULL; 1383 uint16_t hmacs_len = 0; 1384 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 1385 struct sctp_auth_chunk_list *chunks = NULL; 1386 uint16_t num_chunks = 0; 1387 sctp_key_t *new_key; 1388 uint32_t keylen; 1389 1390 /* convert to upper bound */ 1391 length += offset; 1392 1393 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, 1394 sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param); 1395 while (phdr != NULL) { 1396 ptype = ntohs(phdr->param_type); 1397 plen = ntohs(phdr->param_length); 1398 1399 if ((plen < sizeof(struct sctp_paramhdr)) || 1400 (offset + plen > length)) 1401 break; 1402 1403 if (ptype == SCTP_RANDOM) { 1404 if (plen > sizeof(random_store)) 1405 break; 1406 phdr = sctp_get_next_param(m, offset, 1407 (struct sctp_paramhdr *)random_store, plen); 1408 if (phdr == NULL) 1409 return; 1410 /* save the random and length for the key */ 1411 p_random = (struct sctp_auth_random *)phdr; 1412 random_len = plen - sizeof(*p_random); 1413 } else if (ptype == SCTP_HMAC_LIST) { 1414 uint16_t num_hmacs; 1415 uint16_t i; 1416 1417 if (plen > sizeof(hmacs_store)) 1418 break; 1419 phdr = sctp_get_next_param(m, offset, 1420 (struct sctp_paramhdr *)hmacs_store, plen); 1421 if (phdr == NULL) 1422 return; 1423 /* save the hmacs list and num for the key */ 1424 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1425 hmacs_len = plen - sizeof(*hmacs); 1426 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 1427 if (stcb->asoc.local_hmacs != NULL) 1428 sctp_free_hmaclist(stcb->asoc.local_hmacs); 1429 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs); 1430 if (stcb->asoc.local_hmacs != NULL) { 1431 for (i = 0; i < num_hmacs; i++) { 1432 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs, 1433 ntohs(hmacs->hmac_ids[i])); 1434 } 1435 } 1436 } else if (ptype == SCTP_CHUNK_LIST) { 1437 int i; 1438 1439 if (plen > sizeof(chunks_store)) 1440 break; 1441 phdr = sctp_get_next_param(m, offset, 1442 (struct sctp_paramhdr *)chunks_store, plen); 1443 if (phdr == NULL) 1444 return; 1445 chunks = (struct sctp_auth_chunk_list *)phdr; 1446 num_chunks = plen - sizeof(*chunks); 1447 /* save chunks list and num for the key */ 1448 if (stcb->asoc.local_auth_chunks != NULL) 1449 sctp_clear_chunklist(stcb->asoc.local_auth_chunks); 1450 else 1451 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist(); 1452 for (i = 0; i < num_chunks; i++) { 1453 (void)sctp_auth_add_chunk(chunks->chunk_types[i], 1454 stcb->asoc.local_auth_chunks); 1455 } 1456 } 1457 /* get next parameter */ 1458 offset += SCTP_SIZE32(plen); 1459 if (offset + sizeof(struct sctp_paramhdr) > length) 1460 break; 1461 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), 1462 (uint8_t *)&tmp_param); 1463 } 1464 /* concatenate the full random key */ 1465 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; 1466 if (chunks != NULL) { 1467 keylen += sizeof(*chunks) + num_chunks; 1468 } 1469 new_key = sctp_alloc_key(keylen); 1470 if (new_key != NULL) { 1471 /* copy in the RANDOM */ 1472 if (p_random != NULL) { 1473 keylen = sizeof(*p_random) + random_len; 1474 memcpy(new_key->key, p_random, keylen); 1475 } else { 1476 keylen = 0; 1477 } 1478 /* append in the AUTH chunks */ 1479 if (chunks != NULL) { 1480 memcpy(new_key->key + keylen, chunks, 1481 sizeof(*chunks) + num_chunks); 1482 keylen += sizeof(*chunks) + num_chunks; 1483 } 1484 /* append in the HMACs */ 1485 if (hmacs != NULL) { 1486 memcpy(new_key->key + keylen, hmacs, 1487 sizeof(*hmacs) + hmacs_len); 1488 } 1489 } 1490 if (stcb->asoc.authinfo.random != NULL) 1491 sctp_free_key(stcb->asoc.authinfo.random); 1492 stcb->asoc.authinfo.random = new_key; 1493 stcb->asoc.authinfo.random_len = random_len; 1494 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 1495 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 1496 1497 /* negotiate what HMAC to use for the peer */ 1498 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 1499 stcb->asoc.local_hmacs); 1500 1501 /* copy defaults from the endpoint */ 1502 /* FIX ME: put in cookie? */ 1503 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid; 1504 /* copy out the shared key list (by reference) from the endpoint */ 1505 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys, 1506 &stcb->asoc.shared_keys); 1507 } 1508 1509 /* 1510 * compute and fill in the HMAC digest for a packet 1511 */ 1512 void 1513 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 1514 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid) 1515 { 1516 uint32_t digestlen; 1517 sctp_sharedkey_t *skey; 1518 sctp_key_t *key; 1519 1520 if ((stcb == NULL) || (auth == NULL)) 1521 return; 1522 1523 /* zero the digest + chunk padding */ 1524 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); 1525 memset(auth->hmac, 0, SCTP_SIZE32(digestlen)); 1526 1527 /* is the desired key cached? */ 1528 if ((keyid != stcb->asoc.authinfo.assoc_keyid) || 1529 (stcb->asoc.authinfo.assoc_key == NULL)) { 1530 if (stcb->asoc.authinfo.assoc_key != NULL) { 1531 /* free the old cached key */ 1532 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1533 } 1534 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1535 /* the only way skey is NULL is if null key id 0 is used */ 1536 if (skey != NULL) 1537 key = skey->key; 1538 else 1539 key = NULL; 1540 /* compute a new assoc key and cache it */ 1541 stcb->asoc.authinfo.assoc_key = 1542 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1543 stcb->asoc.authinfo.peer_random, key); 1544 stcb->asoc.authinfo.assoc_keyid = keyid; 1545 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n", 1546 stcb->asoc.authinfo.assoc_keyid); 1547 #ifdef SCTP_DEBUG 1548 if (SCTP_AUTH_DEBUG) 1549 sctp_print_key(stcb->asoc.authinfo.assoc_key, 1550 "Assoc Key"); 1551 #endif 1552 } 1553 1554 /* set in the active key id */ 1555 auth->shared_key_id = htons(keyid); 1556 1557 /* compute and fill in the digest */ 1558 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key, 1559 m, auth_offset, auth->hmac); 1560 } 1561 1562 1563 static void 1564 sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size) 1565 { 1566 struct mbuf *m_tmp; 1567 uint8_t *data; 1568 1569 /* sanity check */ 1570 if (m == NULL) 1571 return; 1572 1573 /* find the correct starting mbuf and offset (get start position) */ 1574 m_tmp = m; 1575 while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) { 1576 m_offset -= SCTP_BUF_LEN(m_tmp); 1577 m_tmp = SCTP_BUF_NEXT(m_tmp); 1578 } 1579 /* now use the rest of the mbuf chain */ 1580 while ((m_tmp != NULL) && (size > 0)) { 1581 data = mtod(m_tmp, uint8_t *)+m_offset; 1582 if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) { 1583 memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset); 1584 size -= SCTP_BUF_LEN(m_tmp) - m_offset; 1585 } else { 1586 memset(data, 0, size); 1587 size = 0; 1588 } 1589 /* clear the offset since it's only for the first mbuf */ 1590 m_offset = 0; 1591 m_tmp = SCTP_BUF_NEXT(m_tmp); 1592 } 1593 } 1594 1595 /*- 1596 * process the incoming Authentication chunk 1597 * return codes: 1598 * -1 on any authentication error 1599 * 0 on authentication verification 1600 */ 1601 int 1602 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth, 1603 struct mbuf *m, uint32_t offset) 1604 { 1605 uint16_t chunklen; 1606 uint16_t shared_key_id; 1607 uint16_t hmac_id; 1608 sctp_sharedkey_t *skey; 1609 uint32_t digestlen; 1610 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1611 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1612 1613 /* auth is checked for NULL by caller */ 1614 chunklen = ntohs(auth->ch.chunk_length); 1615 if (chunklen < sizeof(*auth)) { 1616 SCTP_STAT_INCR(sctps_recvauthfailed); 1617 return (-1); 1618 } 1619 SCTP_STAT_INCR(sctps_recvauth); 1620 1621 /* get the auth params */ 1622 shared_key_id = ntohs(auth->shared_key_id); 1623 hmac_id = ntohs(auth->hmac_id); 1624 SCTPDBG(SCTP_DEBUG_AUTH1, 1625 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n", 1626 shared_key_id, hmac_id); 1627 1628 /* is the indicated HMAC supported? */ 1629 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) { 1630 struct mbuf *op_err; 1631 struct sctp_error_auth_invalid_hmac *cause; 1632 1633 SCTP_STAT_INCR(sctps_recvivalhmacid); 1634 SCTPDBG(SCTP_DEBUG_AUTH1, 1635 "SCTP Auth: unsupported HMAC id %u\n", 1636 hmac_id); 1637 /* 1638 * report this in an Error Chunk: Unsupported HMAC 1639 * Identifier 1640 */ 1641 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac), 1642 0, M_NOWAIT, 1, MT_HEADER); 1643 if (op_err != NULL) { 1644 /* pre-reserve some space */ 1645 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr)); 1646 /* fill in the error */ 1647 cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *); 1648 cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID); 1649 cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac)); 1650 cause->hmac_id = ntohs(hmac_id); 1651 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac); 1652 /* queue it */ 1653 sctp_queue_op_err(stcb, op_err); 1654 } 1655 return (-1); 1656 } 1657 /* get the indicated shared key, if available */ 1658 if ((stcb->asoc.authinfo.recv_key == NULL) || 1659 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) { 1660 /* find the shared key on the assoc first */ 1661 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, 1662 shared_key_id); 1663 /* if the shared key isn't found, discard the chunk */ 1664 if (skey == NULL) { 1665 SCTP_STAT_INCR(sctps_recvivalkeyid); 1666 SCTPDBG(SCTP_DEBUG_AUTH1, 1667 "SCTP Auth: unknown key id %u\n", 1668 shared_key_id); 1669 return (-1); 1670 } 1671 /* generate a notification if this is a new key id */ 1672 if (stcb->asoc.authinfo.recv_keyid != shared_key_id) 1673 /* 1674 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb, 1675 * shared_key_id, (void 1676 * *)stcb->asoc.authinfo.recv_keyid); 1677 */ 1678 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY, 1679 shared_key_id, stcb->asoc.authinfo.recv_keyid, 1680 SCTP_SO_NOT_LOCKED); 1681 /* compute a new recv assoc key and cache it */ 1682 if (stcb->asoc.authinfo.recv_key != NULL) 1683 sctp_free_key(stcb->asoc.authinfo.recv_key); 1684 stcb->asoc.authinfo.recv_key = 1685 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1686 stcb->asoc.authinfo.peer_random, skey->key); 1687 stcb->asoc.authinfo.recv_keyid = shared_key_id; 1688 #ifdef SCTP_DEBUG 1689 if (SCTP_AUTH_DEBUG) 1690 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key"); 1691 #endif 1692 } 1693 /* validate the digest length */ 1694 digestlen = sctp_get_hmac_digest_len(hmac_id); 1695 if (chunklen < (sizeof(*auth) + digestlen)) { 1696 /* invalid digest length */ 1697 SCTP_STAT_INCR(sctps_recvauthfailed); 1698 SCTPDBG(SCTP_DEBUG_AUTH1, 1699 "SCTP Auth: chunk too short for HMAC\n"); 1700 return (-1); 1701 } 1702 /* save a copy of the digest, zero the pseudo header, and validate */ 1703 memcpy(digest, auth->hmac, digestlen); 1704 sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen)); 1705 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key, 1706 m, offset, computed_digest); 1707 1708 /* compare the computed digest with the one in the AUTH chunk */ 1709 if (timingsafe_bcmp(digest, computed_digest, digestlen) != 0) { 1710 SCTP_STAT_INCR(sctps_recvauthfailed); 1711 SCTPDBG(SCTP_DEBUG_AUTH1, 1712 "SCTP Auth: HMAC digest check failed\n"); 1713 return (-1); 1714 } 1715 return (0); 1716 } 1717 1718 /* 1719 * Generate NOTIFICATION 1720 */ 1721 void 1722 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication, 1723 uint16_t keyid, uint16_t alt_keyid, int so_locked 1724 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 1725 SCTP_UNUSED 1726 #endif 1727 ) 1728 { 1729 struct mbuf *m_notify; 1730 struct sctp_authkey_event *auth; 1731 struct sctp_queued_to_read *control; 1732 1733 if ((stcb == NULL) || 1734 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 1735 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1736 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) 1737 ) { 1738 /* If the socket is gone we are out of here */ 1739 return; 1740 } 1741 1742 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT)) 1743 /* event not enabled */ 1744 return; 1745 1746 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event), 1747 0, M_NOWAIT, 1, MT_HEADER); 1748 if (m_notify == NULL) 1749 /* no space left */ 1750 return; 1751 1752 SCTP_BUF_LEN(m_notify) = 0; 1753 auth = mtod(m_notify, struct sctp_authkey_event *); 1754 memset(auth, 0, sizeof(struct sctp_authkey_event)); 1755 auth->auth_type = SCTP_AUTHENTICATION_EVENT; 1756 auth->auth_flags = 0; 1757 auth->auth_length = sizeof(*auth); 1758 auth->auth_keynumber = keyid; 1759 auth->auth_altkeynumber = alt_keyid; 1760 auth->auth_indication = indication; 1761 auth->auth_assoc_id = sctp_get_associd(stcb); 1762 1763 SCTP_BUF_LEN(m_notify) = sizeof(*auth); 1764 SCTP_BUF_NEXT(m_notify) = NULL; 1765 1766 /* append to socket */ 1767 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 1768 0, 0, stcb->asoc.context, 0, 0, 0, m_notify); 1769 if (control == NULL) { 1770 /* no memory */ 1771 sctp_m_freem(m_notify); 1772 return; 1773 } 1774 control->length = SCTP_BUF_LEN(m_notify); 1775 control->spec_flags = M_NOTIFICATION; 1776 /* not that we need this */ 1777 control->tail_mbuf = m_notify; 1778 sctp_add_to_readq(stcb->sctp_ep, stcb, control, 1779 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); 1780 } 1781 1782 1783 /*- 1784 * validates the AUTHentication related parameters in an INIT/INIT-ACK 1785 * Note: currently only used for INIT as INIT-ACK is handled inline 1786 * with sctp_load_addresses_from_init() 1787 */ 1788 int 1789 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit) 1790 { 1791 struct sctp_paramhdr *phdr, param_buf; 1792 uint16_t ptype, plen; 1793 int peer_supports_asconf = 0; 1794 int peer_supports_auth = 0; 1795 int got_random = 0, got_hmacs = 0, got_chklist = 0; 1796 uint8_t saw_asconf = 0; 1797 uint8_t saw_asconf_ack = 0; 1798 1799 /* go through each of the params. */ 1800 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf)); 1801 while (phdr) { 1802 ptype = ntohs(phdr->param_type); 1803 plen = ntohs(phdr->param_length); 1804 1805 if (offset + plen > limit) { 1806 break; 1807 } 1808 if (plen < sizeof(struct sctp_paramhdr)) { 1809 break; 1810 } 1811 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 1812 /* A supported extension chunk */ 1813 struct sctp_supported_chunk_types_param *pr_supported; 1814 uint8_t local_store[SCTP_SMALL_CHUNK_STORE]; 1815 int num_ent, i; 1816 1817 if (plen > sizeof(local_store)) { 1818 break; 1819 } 1820 phdr = sctp_get_next_param(m, offset, 1821 (struct sctp_paramhdr *)&local_store, 1822 plen); 1823 if (phdr == NULL) { 1824 return (-1); 1825 } 1826 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 1827 num_ent = plen - sizeof(struct sctp_paramhdr); 1828 for (i = 0; i < num_ent; i++) { 1829 switch (pr_supported->chunk_types[i]) { 1830 case SCTP_ASCONF: 1831 case SCTP_ASCONF_ACK: 1832 peer_supports_asconf = 1; 1833 break; 1834 default: 1835 /* one we don't care about */ 1836 break; 1837 } 1838 } 1839 } else if (ptype == SCTP_RANDOM) { 1840 /* enforce the random length */ 1841 if (plen != (sizeof(struct sctp_auth_random) + 1842 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) { 1843 SCTPDBG(SCTP_DEBUG_AUTH1, 1844 "SCTP: invalid RANDOM len\n"); 1845 return (-1); 1846 } 1847 got_random = 1; 1848 } else if (ptype == SCTP_HMAC_LIST) { 1849 struct sctp_auth_hmac_algo *hmacs; 1850 uint8_t store[SCTP_PARAM_BUFFER_SIZE]; 1851 int num_hmacs; 1852 1853 if (plen > sizeof(store)) { 1854 break; 1855 } 1856 phdr = sctp_get_next_param(m, offset, 1857 (struct sctp_paramhdr *)store, 1858 plen); 1859 if (phdr == NULL) { 1860 return (-1); 1861 } 1862 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1863 num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]); 1864 /* validate the hmac list */ 1865 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 1866 SCTPDBG(SCTP_DEBUG_AUTH1, 1867 "SCTP: invalid HMAC param\n"); 1868 return (-1); 1869 } 1870 got_hmacs = 1; 1871 } else if (ptype == SCTP_CHUNK_LIST) { 1872 struct sctp_auth_chunk_list *chunks; 1873 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE]; 1874 int i, num_chunks; 1875 1876 if (plen > sizeof(chunks_store)) { 1877 break; 1878 } 1879 phdr = sctp_get_next_param(m, offset, 1880 (struct sctp_paramhdr *)chunks_store, 1881 plen); 1882 if (phdr == NULL) { 1883 return (-1); 1884 } 1885 /*- 1886 * Flip through the list and mark that the 1887 * peer supports asconf/asconf_ack. 1888 */ 1889 chunks = (struct sctp_auth_chunk_list *)phdr; 1890 num_chunks = plen - sizeof(*chunks); 1891 for (i = 0; i < num_chunks; i++) { 1892 /* record asconf/asconf-ack if listed */ 1893 if (chunks->chunk_types[i] == SCTP_ASCONF) 1894 saw_asconf = 1; 1895 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 1896 saw_asconf_ack = 1; 1897 1898 } 1899 if (num_chunks) 1900 got_chklist = 1; 1901 } 1902 1903 offset += SCTP_SIZE32(plen); 1904 if (offset >= limit) { 1905 break; 1906 } 1907 phdr = sctp_get_next_param(m, offset, ¶m_buf, 1908 sizeof(param_buf)); 1909 } 1910 /* validate authentication required parameters */ 1911 if (got_random && got_hmacs) { 1912 peer_supports_auth = 1; 1913 } else { 1914 peer_supports_auth = 0; 1915 } 1916 if (!peer_supports_auth && got_chklist) { 1917 SCTPDBG(SCTP_DEBUG_AUTH1, 1918 "SCTP: peer sent chunk list w/o AUTH\n"); 1919 return (-1); 1920 } 1921 if (peer_supports_asconf && !peer_supports_auth) { 1922 SCTPDBG(SCTP_DEBUG_AUTH1, 1923 "SCTP: peer supports ASCONF but not AUTH\n"); 1924 return (-1); 1925 } else if ((peer_supports_asconf) && (peer_supports_auth) && 1926 ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 1927 return (-2); 1928 } 1929 return (0); 1930 } 1931 1932 void 1933 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 1934 { 1935 uint16_t chunks_len = 0; 1936 uint16_t hmacs_len = 0; 1937 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; 1938 sctp_key_t *new_key; 1939 uint16_t keylen; 1940 1941 /* initialize hmac list from endpoint */ 1942 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); 1943 if (stcb->asoc.local_hmacs != NULL) { 1944 hmacs_len = stcb->asoc.local_hmacs->num_algo * 1945 sizeof(stcb->asoc.local_hmacs->hmac[0]); 1946 } 1947 /* initialize auth chunks list from endpoint */ 1948 stcb->asoc.local_auth_chunks = 1949 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); 1950 if (stcb->asoc.local_auth_chunks != NULL) { 1951 int i; 1952 1953 for (i = 0; i < 256; i++) { 1954 if (stcb->asoc.local_auth_chunks->chunks[i]) 1955 chunks_len++; 1956 } 1957 } 1958 /* copy defaults from the endpoint */ 1959 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid; 1960 1961 /* copy out the shared key list (by reference) from the endpoint */ 1962 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, 1963 &stcb->asoc.shared_keys); 1964 1965 /* now set the concatenated key (random + chunks + hmacs) */ 1966 /* key includes parameter headers */ 1967 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len + 1968 hmacs_len; 1969 new_key = sctp_alloc_key(keylen); 1970 if (new_key != NULL) { 1971 struct sctp_paramhdr *ph; 1972 int plen; 1973 1974 /* generate and copy in the RANDOM */ 1975 ph = (struct sctp_paramhdr *)new_key->key; 1976 ph->param_type = htons(SCTP_RANDOM); 1977 plen = sizeof(*ph) + random_len; 1978 ph->param_length = htons(plen); 1979 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len); 1980 keylen = plen; 1981 1982 /* append in the AUTH chunks */ 1983 /* NOTE: currently we always have chunks to list */ 1984 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 1985 ph->param_type = htons(SCTP_CHUNK_LIST); 1986 plen = sizeof(*ph) + chunks_len; 1987 ph->param_length = htons(plen); 1988 keylen += sizeof(*ph); 1989 if (stcb->asoc.local_auth_chunks) { 1990 int i; 1991 1992 for (i = 0; i < 256; i++) { 1993 if (stcb->asoc.local_auth_chunks->chunks[i]) 1994 new_key->key[keylen++] = i; 1995 } 1996 } 1997 1998 /* append in the HMACs */ 1999 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 2000 ph->param_type = htons(SCTP_HMAC_LIST); 2001 plen = sizeof(*ph) + hmacs_len; 2002 ph->param_length = htons(plen); 2003 keylen += sizeof(*ph); 2004 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs, 2005 new_key->key + keylen); 2006 } 2007 if (stcb->asoc.authinfo.random != NULL) 2008 sctp_free_key(stcb->asoc.authinfo.random); 2009 stcb->asoc.authinfo.random = new_key; 2010 stcb->asoc.authinfo.random_len = random_len; 2011 } 2012