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