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