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