487 } 488 } 489 return (new_key); 490} 491 492 493sctp_sharedkey_t * 494sctp_alloc_sharedkey(void) 495{ 496 sctp_sharedkey_t *new_key; 497 498 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key), 499 SCTP_M_AUTH_KY); 500 if (new_key == NULL) { 501 /* out of memory */ 502 return (NULL); 503 } 504 new_key->keyid = 0; 505 new_key->key = NULL; 506 new_key->refcount = 1; 507 new_key->deactivated = 0; 508 return (new_key); 509} 510 511void 512sctp_free_sharedkey(sctp_sharedkey_t * skey) 513{ 514 if (skey == NULL) 515 return; 516 517 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) { 518 if (skey->key != NULL) 519 sctp_free_key(skey->key); 520 SCTP_FREE(skey, SCTP_M_AUTH_KY); 521 } 522} 523 524sctp_sharedkey_t * 525sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id) 526{ 527 sctp_sharedkey_t *skey; 528 529 LIST_FOREACH(skey, shared_keys, next) { 530 if (skey->keyid == key_id) 531 return (skey); 532 } 533 return (NULL); 534} 535 536int 537sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 538 sctp_sharedkey_t * new_skey) 539{ 540 sctp_sharedkey_t *skey; 541 542 if ((shared_keys == NULL) || (new_skey == NULL)) 543 return (EINVAL); 544 545 /* insert into an empty list? */ 546 if (LIST_EMPTY(shared_keys)) { 547 LIST_INSERT_HEAD(shared_keys, new_skey, next); 548 return (0); 549 } 550 /* insert into the existing list, ordered by key id */ 551 LIST_FOREACH(skey, shared_keys, next) { 552 if (new_skey->keyid < skey->keyid) { 553 /* insert it before here */ 554 LIST_INSERT_BEFORE(skey, new_skey, next); 555 return (0); 556 } else if (new_skey->keyid == skey->keyid) { 557 /* replace the existing key */ 558 /* verify this key *can* be replaced */ 559 if ((skey->deactivated) && (skey->refcount > 1)) { 560 SCTPDBG(SCTP_DEBUG_AUTH1, 561 "can't replace shared key id %u\n", 562 new_skey->keyid); 563 return (EBUSY); 564 } 565 SCTPDBG(SCTP_DEBUG_AUTH1, 566 "replacing shared key id %u\n", 567 new_skey->keyid); 568 LIST_INSERT_BEFORE(skey, new_skey, next); 569 LIST_REMOVE(skey, next); 570 sctp_free_sharedkey(skey); 571 return (0); 572 } 573 if (LIST_NEXT(skey, next) == NULL) { 574 /* belongs at the end of the list */ 575 LIST_INSERT_AFTER(skey, new_skey, next); 576 return (0); 577 } 578 } 579 /* shouldn't reach here */ 580 return (0); 581} 582 583void 584sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id) 585{ 586 sctp_sharedkey_t *skey; 587 588 /* find the shared key */ 589 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 590 591 /* bump the ref count */ 592 if (skey) { 593 atomic_add_int(&skey->refcount, 1); 594 SCTPDBG(SCTP_DEBUG_AUTH2, 595 "%s: stcb %p key %u refcount acquire to %d\n", 596 __FUNCTION__, stcb, key_id, skey->refcount); 597 } 598} 599 600void 601sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked 602#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 603 SCTP_UNUSED 604#endif 605) 606{ 607 sctp_sharedkey_t *skey; 608 609 /* find the shared key */ 610 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 611 612 /* decrement the ref count */ 613 if (skey) { 614 sctp_free_sharedkey(skey); 615 SCTPDBG(SCTP_DEBUG_AUTH2, 616 "%s: stcb %p key %u refcount release to %d\n", 617 __FUNCTION__, stcb, key_id, skey->refcount); 618 619 /* see if a notification should be generated */ 620 if ((skey->refcount <= 1) && (skey->deactivated)) { 621 /* notify ULP that key is no longer used */ 622 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, 623 key_id, 0, so_locked); 624 SCTPDBG(SCTP_DEBUG_AUTH2, 625 "%s: stcb %p key %u no longer used, %d\n", 626 __FUNCTION__, stcb, key_id, skey->refcount); 627 } 628 } 629} 630 631static sctp_sharedkey_t * 632sctp_copy_sharedkey(const sctp_sharedkey_t * skey) 633{ 634 sctp_sharedkey_t *new_skey; 635 636 if (skey == NULL) 637 return (NULL); 638 new_skey = sctp_alloc_sharedkey(); 639 if (new_skey == NULL) 640 return (NULL); 641 if (skey->key != NULL) 642 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen); 643 else 644 new_skey->key = NULL; 645 new_skey->keyid = skey->keyid; 646 return (new_skey); 647} 648 649int 650sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest) 651{ 652 sctp_sharedkey_t *skey, *new_skey; 653 int count = 0; 654 655 if ((src == NULL) || (dest == NULL)) 656 return (0); 657 LIST_FOREACH(skey, src, next) { 658 new_skey = sctp_copy_sharedkey(skey); 659 if (new_skey != NULL) { 660 (void)sctp_insert_sharedkey(dest, new_skey); 661 count++; 662 } 663 } 664 return (count); 665} 666 667 668sctp_hmaclist_t * 669sctp_alloc_hmaclist(uint8_t num_hmacs) 670{ 671 sctp_hmaclist_t *new_list; 672 int alloc_size; 673 674 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]); 675 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size, 676 SCTP_M_AUTH_HL); 677 if (new_list == NULL) { 678 /* out of memory */ 679 return (NULL); 680 } 681 new_list->max_algo = num_hmacs; 682 new_list->num_algo = 0; 683 return (new_list); 684} 685 686void 687sctp_free_hmaclist(sctp_hmaclist_t * list) 688{ 689 if (list != NULL) { 690 SCTP_FREE(list, SCTP_M_AUTH_HL); 691 list = NULL; 692 } 693} 694 695int 696sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id) 697{ 698 int i; 699 700 if (list == NULL) 701 return (-1); 702 if (list->num_algo == list->max_algo) { 703 SCTPDBG(SCTP_DEBUG_AUTH1, 704 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id); 705 return (-1); 706 } 707 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) && 708#ifdef HAVE_SHA224 709 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) && 710#endif 711#ifdef HAVE_SHA2 712 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) && 713 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) && 714 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) && 715#endif 716 1) { 717 return (-1); 718 } 719 /* Now is it already in the list */ 720 for (i = 0; i < list->num_algo; i++) { 721 if (list->hmac[i] == hmac_id) { 722 /* already in list */ 723 return (-1); 724 } 725 } 726 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id); 727 list->hmac[list->num_algo++] = hmac_id; 728 return (0); 729} 730 731sctp_hmaclist_t * 732sctp_copy_hmaclist(sctp_hmaclist_t * list) 733{ 734 sctp_hmaclist_t *new_list; 735 int i; 736 737 if (list == NULL) 738 return (NULL); 739 /* get a new list */ 740 new_list = sctp_alloc_hmaclist(list->max_algo); 741 if (new_list == NULL) 742 return (NULL); 743 /* copy it */ 744 new_list->max_algo = list->max_algo; 745 new_list->num_algo = list->num_algo; 746 for (i = 0; i < list->num_algo; i++) 747 new_list->hmac[i] = list->hmac[i]; 748 return (new_list); 749} 750 751sctp_hmaclist_t * 752sctp_default_supported_hmaclist(void) 753{ 754 sctp_hmaclist_t *new_list; 755 756 new_list = sctp_alloc_hmaclist(2); 757 if (new_list == NULL) 758 return (NULL); 759 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1); 760 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256); 761 return (new_list); 762} 763 764/*- 765 * HMAC algos are listed in priority/preference order 766 * find the best HMAC id to use for the peer based on local support 767 */ 768uint16_t 769sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local) 770{ 771 int i, j; 772 773 if ((local == NULL) || (peer == NULL)) 774 return (SCTP_AUTH_HMAC_ID_RSVD); 775 776 for (i = 0; i < peer->num_algo; i++) { 777 for (j = 0; j < local->num_algo; j++) { 778 if (peer->hmac[i] == local->hmac[j]) { 779 /* found the "best" one */ 780 SCTPDBG(SCTP_DEBUG_AUTH1, 781 "SCTP: negotiated peer HMAC id %u\n", 782 peer->hmac[i]); 783 return (peer->hmac[i]); 784 } 785 } 786 } 787 /* didn't find one! */ 788 return (SCTP_AUTH_HMAC_ID_RSVD); 789} 790 791/*- 792 * serialize the HMAC algo list and return space used 793 * caller must guarantee ptr has appropriate space 794 */ 795int 796sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr) 797{ 798 int i; 799 uint16_t hmac_id; 800 801 if (list == NULL) 802 return (0); 803 804 for (i = 0; i < list->num_algo; i++) { 805 hmac_id = htons(list->hmac[i]); 806 bcopy(&hmac_id, ptr, sizeof(hmac_id)); 807 ptr += sizeof(hmac_id); 808 } 809 return (list->num_algo * sizeof(hmac_id)); 810} 811 812int 813sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs) 814{ 815 uint32_t i; 816 uint16_t hmac_id; 817 uint32_t sha1_supported = 0; 818 819 for (i = 0; i < num_hmacs; i++) { 820 hmac_id = ntohs(hmacs->hmac_ids[i]); 821 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1) 822 sha1_supported = 1; 823 } 824 /* all HMAC id's are supported */ 825 if (sha1_supported == 0) 826 return (-1); 827 else 828 return (0); 829} 830 831sctp_authinfo_t * 832sctp_alloc_authinfo(void) 833{ 834 sctp_authinfo_t *new_authinfo; 835 836 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo), 837 SCTP_M_AUTH_IF); 838 839 if (new_authinfo == NULL) { 840 /* out of memory */ 841 return (NULL); 842 } 843 bzero(new_authinfo, sizeof(*new_authinfo)); 844 return (new_authinfo); 845} 846 847void 848sctp_free_authinfo(sctp_authinfo_t * authinfo) 849{ 850 if (authinfo == NULL) 851 return; 852 853 if (authinfo->random != NULL) 854 sctp_free_key(authinfo->random); 855 if (authinfo->peer_random != NULL) 856 sctp_free_key(authinfo->peer_random); 857 if (authinfo->assoc_key != NULL) 858 sctp_free_key(authinfo->assoc_key); 859 if (authinfo->recv_key != NULL) 860 sctp_free_key(authinfo->recv_key); 861 862 /* We are NOT dynamically allocating authinfo's right now... */ 863 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */ 864} 865 866 867uint32_t 868sctp_get_auth_chunk_len(uint16_t hmac_algo) 869{ 870 int size; 871 872 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo); 873 return (SCTP_SIZE32(size)); 874} 875 876uint32_t 877sctp_get_hmac_digest_len(uint16_t hmac_algo) 878{ 879 switch (hmac_algo) { 880 case SCTP_AUTH_HMAC_ID_SHA1: 881 return (SCTP_AUTH_DIGEST_LEN_SHA1); 882#ifdef HAVE_SHA224 883 case SCTP_AUTH_HMAC_ID_SHA224: 884 return (SCTP_AUTH_DIGEST_LEN_SHA224); 885#endif 886#ifdef HAVE_SHA2 887 case SCTP_AUTH_HMAC_ID_SHA256: 888 return (SCTP_AUTH_DIGEST_LEN_SHA256); 889 case SCTP_AUTH_HMAC_ID_SHA384: 890 return (SCTP_AUTH_DIGEST_LEN_SHA384); 891 case SCTP_AUTH_HMAC_ID_SHA512: 892 return (SCTP_AUTH_DIGEST_LEN_SHA512); 893#endif 894 default: 895 /* unknown HMAC algorithm: can't do anything */ 896 return (0); 897 } /* end switch */ 898} 899 900static inline int 901sctp_get_hmac_block_len(uint16_t hmac_algo) 902{ 903 switch (hmac_algo) { 904 case SCTP_AUTH_HMAC_ID_SHA1: 905#ifdef HAVE_SHA224 906 case SCTP_AUTH_HMAC_ID_SHA224: 907#endif 908 return (64); 909#ifdef HAVE_SHA2 910 case SCTP_AUTH_HMAC_ID_SHA256: 911 return (64); 912 case SCTP_AUTH_HMAC_ID_SHA384: 913 case SCTP_AUTH_HMAC_ID_SHA512: 914 return (128); 915#endif 916 case SCTP_AUTH_HMAC_ID_RSVD: 917 default: 918 /* unknown HMAC algorithm: can't do anything */ 919 return (0); 920 } /* end switch */ 921} 922 923static void 924sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx) 925{ 926 switch (hmac_algo) { 927 case SCTP_AUTH_HMAC_ID_SHA1: 928 SHA1_Init(&ctx->sha1); 929 break; 930#ifdef HAVE_SHA224 931 case SCTP_AUTH_HMAC_ID_SHA224: 932 break; 933#endif 934#ifdef HAVE_SHA2 935 case SCTP_AUTH_HMAC_ID_SHA256: 936 SHA256_Init(&ctx->sha256); 937 break; 938 case SCTP_AUTH_HMAC_ID_SHA384: 939 SHA384_Init(&ctx->sha384); 940 break; 941 case SCTP_AUTH_HMAC_ID_SHA512: 942 SHA512_Init(&ctx->sha512); 943 break; 944#endif 945 case SCTP_AUTH_HMAC_ID_RSVD: 946 default: 947 /* unknown HMAC algorithm: can't do anything */ 948 return; 949 } /* end switch */ 950} 951 952static void 953sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx, 954 uint8_t * text, uint32_t textlen) 955{ 956 switch (hmac_algo) { 957 case SCTP_AUTH_HMAC_ID_SHA1: 958 SHA1_Update(&ctx->sha1, text, textlen); 959 break; 960#ifdef HAVE_SHA224 961 case SCTP_AUTH_HMAC_ID_SHA224: 962 break; 963#endif 964#ifdef HAVE_SHA2 965 case SCTP_AUTH_HMAC_ID_SHA256: 966 SHA256_Update(&ctx->sha256, text, textlen); 967 break; 968 case SCTP_AUTH_HMAC_ID_SHA384: 969 SHA384_Update(&ctx->sha384, text, textlen); 970 break; 971 case SCTP_AUTH_HMAC_ID_SHA512: 972 SHA512_Update(&ctx->sha512, text, textlen); 973 break; 974#endif 975 case SCTP_AUTH_HMAC_ID_RSVD: 976 default: 977 /* unknown HMAC algorithm: can't do anything */ 978 return; 979 } /* end switch */ 980} 981 982static void 983sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx, 984 uint8_t * digest) 985{ 986 switch (hmac_algo) { 987 case SCTP_AUTH_HMAC_ID_SHA1: 988 SHA1_Final(digest, &ctx->sha1); 989 break; 990#ifdef HAVE_SHA224 991 case SCTP_AUTH_HMAC_ID_SHA224: 992 break; 993#endif 994#ifdef HAVE_SHA2 995 case SCTP_AUTH_HMAC_ID_SHA256: 996 SHA256_Final(digest, &ctx->sha256); 997 break; 998 case SCTP_AUTH_HMAC_ID_SHA384: 999 /* SHA384 is truncated SHA512 */ 1000 SHA384_Final(digest, &ctx->sha384); 1001 break; 1002 case SCTP_AUTH_HMAC_ID_SHA512: 1003 SHA512_Final(digest, &ctx->sha512); 1004 break; 1005#endif 1006 case SCTP_AUTH_HMAC_ID_RSVD: 1007 default: 1008 /* unknown HMAC algorithm: can't do anything */ 1009 return; 1010 } /* end switch */ 1011} 1012 1013/*- 1014 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104) 1015 * 1016 * Compute the HMAC digest using the desired hash key, text, and HMAC 1017 * algorithm. Resulting digest is placed in 'digest' and digest length 1018 * is returned, if the HMAC was performed. 1019 * 1020 * WARNING: it is up to the caller to supply sufficient space to hold the 1021 * resultant digest. 1022 */ 1023uint32_t 1024sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1025 uint8_t * text, uint32_t textlen, uint8_t * digest) 1026{ 1027 uint32_t digestlen; 1028 uint32_t blocklen; 1029 sctp_hash_context_t ctx; 1030 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1031 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1032 uint32_t i; 1033 1034 /* sanity check the material and length */ 1035 if ((key == NULL) || (keylen == 0) || (text == NULL) || 1036 (textlen == 0) || (digest == NULL)) { 1037 /* can't do HMAC with empty key or text or digest store */ 1038 return (0); 1039 } 1040 /* validate the hmac algo and get the digest length */ 1041 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1042 if (digestlen == 0) 1043 return (0); 1044 1045 /* hash the key if it is longer than the hash block size */ 1046 blocklen = sctp_get_hmac_block_len(hmac_algo); 1047 if (keylen > blocklen) { 1048 sctp_hmac_init(hmac_algo, &ctx); 1049 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1050 sctp_hmac_final(hmac_algo, &ctx, temp); 1051 /* set the hashed key as the key */ 1052 keylen = digestlen; 1053 key = temp; 1054 } 1055 /* initialize the inner/outer pads with the key and "append" zeroes */ 1056 bzero(ipad, blocklen); 1057 bzero(opad, blocklen); 1058 bcopy(key, ipad, keylen); 1059 bcopy(key, opad, keylen); 1060 1061 /* XOR the key with ipad and opad values */ 1062 for (i = 0; i < blocklen; i++) { 1063 ipad[i] ^= 0x36; 1064 opad[i] ^= 0x5c; 1065 } 1066 1067 /* perform inner hash */ 1068 sctp_hmac_init(hmac_algo, &ctx); 1069 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1070 sctp_hmac_update(hmac_algo, &ctx, text, textlen); 1071 sctp_hmac_final(hmac_algo, &ctx, temp); 1072 1073 /* perform outer hash */ 1074 sctp_hmac_init(hmac_algo, &ctx); 1075 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1076 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1077 sctp_hmac_final(hmac_algo, &ctx, digest); 1078 1079 return (digestlen); 1080} 1081 1082/* mbuf version */ 1083uint32_t 1084sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1085 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer) 1086{ 1087 uint32_t digestlen; 1088 uint32_t blocklen; 1089 sctp_hash_context_t ctx; 1090 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1091 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1092 uint32_t i; 1093 struct mbuf *m_tmp; 1094 1095 /* sanity check the material and length */ 1096 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) { 1097 /* can't do HMAC with empty key or text or digest store */ 1098 return (0); 1099 } 1100 /* validate the hmac algo and get the digest length */ 1101 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1102 if (digestlen == 0) 1103 return (0); 1104 1105 /* hash the key if it is longer than the hash block size */ 1106 blocklen = sctp_get_hmac_block_len(hmac_algo); 1107 if (keylen > blocklen) { 1108 sctp_hmac_init(hmac_algo, &ctx); 1109 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1110 sctp_hmac_final(hmac_algo, &ctx, temp); 1111 /* set the hashed key as the key */ 1112 keylen = digestlen; 1113 key = temp; 1114 } 1115 /* initialize the inner/outer pads with the key and "append" zeroes */ 1116 bzero(ipad, blocklen); 1117 bzero(opad, blocklen); 1118 bcopy(key, ipad, keylen); 1119 bcopy(key, opad, keylen); 1120 1121 /* XOR the key with ipad and opad values */ 1122 for (i = 0; i < blocklen; i++) { 1123 ipad[i] ^= 0x36; 1124 opad[i] ^= 0x5c; 1125 } 1126 1127 /* perform inner hash */ 1128 sctp_hmac_init(hmac_algo, &ctx); 1129 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1130 /* find the correct starting mbuf and offset (get start of text) */ 1131 m_tmp = m; 1132 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { 1133 m_offset -= SCTP_BUF_LEN(m_tmp); 1134 m_tmp = SCTP_BUF_NEXT(m_tmp); 1135 } 1136 /* now use the rest of the mbuf chain for the text */ 1137 while (m_tmp != NULL) { 1138 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) { 1139 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, 1140 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset)); 1141 } else { 1142 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, 1143 SCTP_BUF_LEN(m_tmp) - m_offset); 1144 } 1145 1146 /* clear the offset since it's only for the first mbuf */ 1147 m_offset = 0; 1148 m_tmp = SCTP_BUF_NEXT(m_tmp); 1149 } 1150 sctp_hmac_final(hmac_algo, &ctx, temp); 1151 1152 /* perform outer hash */ 1153 sctp_hmac_init(hmac_algo, &ctx); 1154 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1155 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1156 sctp_hmac_final(hmac_algo, &ctx, digest); 1157 1158 return (digestlen); 1159} 1160 1161/*- 1162 * verify the HMAC digest using the desired hash key, text, and HMAC 1163 * algorithm. 1164 * Returns -1 on error, 0 on success. 1165 */ 1166int 1167sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1168 uint8_t * text, uint32_t textlen, 1169 uint8_t * digest, uint32_t digestlen) 1170{ 1171 uint32_t len; 1172 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1173 1174 /* sanity check the material and length */ 1175 if ((key == NULL) || (keylen == 0) || 1176 (text == NULL) || (textlen == 0) || (digest == NULL)) { 1177 /* can't do HMAC with empty key or text or digest */ 1178 return (-1); 1179 } 1180 len = sctp_get_hmac_digest_len(hmac_algo); 1181 if ((len == 0) || (digestlen != len)) 1182 return (-1); 1183 1184 /* compute the expected hash */ 1185 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len) 1186 return (-1); 1187 1188 if (memcmp(digest, temp, digestlen) != 0) 1189 return (-1); 1190 else 1191 return (0); 1192} 1193 1194 1195/* 1196 * computes the requested HMAC using a key struct (which may be modified if 1197 * the keylen exceeds the HMAC block len). 1198 */ 1199uint32_t 1200sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text, 1201 uint32_t textlen, uint8_t * digest) 1202{ 1203 uint32_t digestlen; 1204 uint32_t blocklen; 1205 sctp_hash_context_t ctx; 1206 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1207 1208 /* sanity check */ 1209 if ((key == NULL) || (text == NULL) || (textlen == 0) || 1210 (digest == NULL)) { 1211 /* can't do HMAC with empty key or text or digest store */ 1212 return (0); 1213 } 1214 /* validate the hmac algo and get the digest length */ 1215 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1216 if (digestlen == 0) 1217 return (0); 1218 1219 /* hash the key if it is longer than the hash block size */ 1220 blocklen = sctp_get_hmac_block_len(hmac_algo); 1221 if (key->keylen > blocklen) { 1222 sctp_hmac_init(hmac_algo, &ctx); 1223 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1224 sctp_hmac_final(hmac_algo, &ctx, temp); 1225 /* save the hashed key as the new key */ 1226 key->keylen = digestlen; 1227 bcopy(temp, key->key, key->keylen); 1228 } 1229 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen, 1230 digest)); 1231} 1232 1233/* mbuf version */ 1234uint32_t 1235sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m, 1236 uint32_t m_offset, uint8_t * digest) 1237{ 1238 uint32_t digestlen; 1239 uint32_t blocklen; 1240 sctp_hash_context_t ctx; 1241 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1242 1243 /* sanity check */ 1244 if ((key == NULL) || (m == NULL) || (digest == NULL)) { 1245 /* can't do HMAC with empty key or text or digest store */ 1246 return (0); 1247 } 1248 /* validate the hmac algo and get the digest length */ 1249 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1250 if (digestlen == 0) 1251 return (0); 1252 1253 /* hash the key if it is longer than the hash block size */ 1254 blocklen = sctp_get_hmac_block_len(hmac_algo); 1255 if (key->keylen > blocklen) { 1256 sctp_hmac_init(hmac_algo, &ctx); 1257 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1258 sctp_hmac_final(hmac_algo, &ctx, temp); 1259 /* save the hashed key as the new key */ 1260 key->keylen = digestlen; 1261 bcopy(temp, key->key, key->keylen); 1262 } 1263 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0)); 1264} 1265 1266int 1267sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id) 1268{ 1269 int i; 1270 1271 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD)) 1272 return (0); 1273 1274 for (i = 0; i < list->num_algo; i++) 1275 if (list->hmac[i] == id) 1276 return (1); 1277 1278 /* not in the list */ 1279 return (0); 1280} 1281 1282 1283/*- 1284 * clear any cached key(s) if they match the given key id on an association. 1285 * the cached key(s) will be recomputed and re-cached at next use. 1286 * ASSUMES TCB_LOCK is already held 1287 */ 1288void 1289sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid) 1290{ 1291 if (stcb == NULL) 1292 return; 1293 1294 if (keyid == stcb->asoc.authinfo.assoc_keyid) { 1295 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1296 stcb->asoc.authinfo.assoc_key = NULL; 1297 } 1298 if (keyid == stcb->asoc.authinfo.recv_keyid) { 1299 sctp_free_key(stcb->asoc.authinfo.recv_key); 1300 stcb->asoc.authinfo.recv_key = NULL; 1301 } 1302} 1303 1304/*- 1305 * clear any cached key(s) if they match the given key id for all assocs on 1306 * an endpoint. 1307 * ASSUMES INP_WLOCK is already held 1308 */ 1309void 1310sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid) 1311{ 1312 struct sctp_tcb *stcb; 1313 1314 if (inp == NULL) 1315 return; 1316 1317 /* clear the cached keys on all assocs on this instance */ 1318 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1319 SCTP_TCB_LOCK(stcb); 1320 sctp_clear_cachedkeys(stcb, keyid); 1321 SCTP_TCB_UNLOCK(stcb); 1322 } 1323} 1324 1325/*- 1326 * delete a shared key from an association 1327 * ASSUMES TCB_LOCK is already held 1328 */ 1329int 1330sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1331{ 1332 sctp_sharedkey_t *skey; 1333 1334 if (stcb == NULL) 1335 return (-1); 1336 1337 /* is the keyid the assoc active sending key */ 1338 if (keyid == stcb->asoc.authinfo.active_keyid) 1339 return (-1); 1340 1341 /* does the key exist? */ 1342 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1343 if (skey == NULL) 1344 return (-1); 1345 1346 /* are there other refcount holders on the key? */ 1347 if (skey->refcount > 1) 1348 return (-1); 1349 1350 /* remove it */ 1351 LIST_REMOVE(skey, next); 1352 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1353 1354 /* clear any cached keys */ 1355 sctp_clear_cachedkeys(stcb, keyid); 1356 return (0); 1357} 1358 1359/*- 1360 * deletes a shared key from the endpoint 1361 * ASSUMES INP_WLOCK is already held 1362 */ 1363int 1364sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1365{ 1366 sctp_sharedkey_t *skey; 1367 1368 if (inp == NULL) 1369 return (-1); 1370 1371 /* is the keyid the active sending key on the endpoint */ 1372 if (keyid == inp->sctp_ep.default_keyid) 1373 return (-1); 1374 1375 /* does the key exist? */ 1376 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1377 if (skey == NULL) 1378 return (-1); 1379 1380 /* endpoint keys are not refcounted */ 1381 1382 /* remove it */ 1383 LIST_REMOVE(skey, next); 1384 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1385 1386 /* clear any cached keys */ 1387 sctp_clear_cachedkeys_ep(inp, keyid); 1388 return (0); 1389} 1390 1391/*- 1392 * set the active key on an association 1393 * ASSUMES TCB_LOCK is already held 1394 */ 1395int 1396sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid) 1397{ 1398 sctp_sharedkey_t *skey = NULL; 1399 1400 /* find the key on the assoc */ 1401 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1402 if (skey == NULL) { 1403 /* that key doesn't exist */ 1404 return (-1); 1405 } 1406 if ((skey->deactivated) && (skey->refcount > 1)) { 1407 /* can't reactivate a deactivated key with other refcounts */ 1408 return (-1); 1409 } 1410 /* set the (new) active key */ 1411 stcb->asoc.authinfo.active_keyid = keyid; 1412 /* reset the deactivated flag */ 1413 skey->deactivated = 0; 1414 1415 return (0); 1416} 1417 1418/*- 1419 * set the active key on an endpoint 1420 * ASSUMES INP_WLOCK is already held 1421 */ 1422int 1423sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1424{ 1425 sctp_sharedkey_t *skey; 1426 1427 /* find the key */ 1428 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1429 if (skey == NULL) { 1430 /* that key doesn't exist */ 1431 return (-1); 1432 } 1433 inp->sctp_ep.default_keyid = keyid; 1434 return (0); 1435} 1436 1437/*- 1438 * deactivates a shared key from the association 1439 * ASSUMES INP_WLOCK is already held 1440 */ 1441int 1442sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1443{ 1444 sctp_sharedkey_t *skey; 1445 1446 if (stcb == NULL) 1447 return (-1); 1448 1449 /* is the keyid the assoc active sending key */ 1450 if (keyid == stcb->asoc.authinfo.active_keyid) 1451 return (-1); 1452 1453 /* does the key exist? */ 1454 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1455 if (skey == NULL) 1456 return (-1); 1457 1458 /* are there other refcount holders on the key? */ 1459 if (skey->refcount == 1) { 1460 /* no other users, send a notification for this key */ 1461 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0, 1462 SCTP_SO_LOCKED); 1463 } 1464 /* mark the key as deactivated */ 1465 skey->deactivated = 1; 1466 1467 return (0); 1468} 1469 1470/*- 1471 * deactivates a shared key from the endpoint 1472 * ASSUMES INP_WLOCK is already held 1473 */ 1474int 1475sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1476{ 1477 sctp_sharedkey_t *skey; 1478 1479 if (inp == NULL) 1480 return (-1); 1481 1482 /* is the keyid the active sending key on the endpoint */ 1483 if (keyid == inp->sctp_ep.default_keyid) 1484 return (-1); 1485 1486 /* does the key exist? */ 1487 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1488 if (skey == NULL) 1489 return (-1); 1490 1491 /* endpoint keys are not refcounted */ 1492 1493 /* remove it */ 1494 LIST_REMOVE(skey, next); 1495 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1496 1497 return (0); 1498} 1499 1500/* 1501 * get local authentication parameters from cookie (from INIT-ACK) 1502 */ 1503void 1504sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 1505 uint32_t offset, uint32_t length) 1506{ 1507 struct sctp_paramhdr *phdr, tmp_param; 1508 uint16_t plen, ptype; 1509 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 1510 struct sctp_auth_random *p_random = NULL; 1511 uint16_t random_len = 0; 1512 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 1513 struct sctp_auth_hmac_algo *hmacs = NULL; 1514 uint16_t hmacs_len = 0; 1515 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 1516 struct sctp_auth_chunk_list *chunks = NULL; 1517 uint16_t num_chunks = 0; 1518 sctp_key_t *new_key; 1519 uint32_t keylen; 1520 1521 /* convert to upper bound */ 1522 length += offset; 1523 1524 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, 1525 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param); 1526 while (phdr != NULL) { 1527 ptype = ntohs(phdr->param_type); 1528 plen = ntohs(phdr->param_length); 1529 1530 if ((plen == 0) || (offset + plen > length)) 1531 break; 1532 1533 if (ptype == SCTP_RANDOM) { 1534 if (plen > sizeof(random_store)) 1535 break; 1536 phdr = sctp_get_next_param(m, offset, 1537 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store))); 1538 if (phdr == NULL) 1539 return; 1540 /* save the random and length for the key */ 1541 p_random = (struct sctp_auth_random *)phdr; 1542 random_len = plen - sizeof(*p_random); 1543 } else if (ptype == SCTP_HMAC_LIST) { 1544 int num_hmacs; 1545 int i; 1546 1547 if (plen > sizeof(hmacs_store)) 1548 break; 1549 phdr = sctp_get_next_param(m, offset, 1550 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store))); 1551 if (phdr == NULL) 1552 return; 1553 /* save the hmacs list and num for the key */ 1554 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1555 hmacs_len = plen - sizeof(*hmacs); 1556 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 1557 if (stcb->asoc.local_hmacs != NULL) 1558 sctp_free_hmaclist(stcb->asoc.local_hmacs); 1559 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs); 1560 if (stcb->asoc.local_hmacs != NULL) { 1561 for (i = 0; i < num_hmacs; i++) { 1562 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs, 1563 ntohs(hmacs->hmac_ids[i])); 1564 } 1565 } 1566 } else if (ptype == SCTP_CHUNK_LIST) { 1567 int i; 1568 1569 if (plen > sizeof(chunks_store)) 1570 break; 1571 phdr = sctp_get_next_param(m, offset, 1572 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store))); 1573 if (phdr == NULL) 1574 return; 1575 chunks = (struct sctp_auth_chunk_list *)phdr; 1576 num_chunks = plen - sizeof(*chunks); 1577 /* save chunks list and num for the key */ 1578 if (stcb->asoc.local_auth_chunks != NULL) 1579 sctp_clear_chunklist(stcb->asoc.local_auth_chunks); 1580 else 1581 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist(); 1582 for (i = 0; i < num_chunks; i++) { 1583 (void)sctp_auth_add_chunk(chunks->chunk_types[i], 1584 stcb->asoc.local_auth_chunks); 1585 } 1586 } 1587 /* get next parameter */ 1588 offset += SCTP_SIZE32(plen); 1589 if (offset + sizeof(struct sctp_paramhdr) > length) 1590 break; 1591 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), 1592 (uint8_t *) & tmp_param); 1593 } 1594 /* concatenate the full random key */ 1595 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; 1596 if (chunks != NULL) { 1597 keylen += sizeof(*chunks) + num_chunks; 1598 } 1599 new_key = sctp_alloc_key(keylen); 1600 if (new_key != NULL) { 1601 /* copy in the RANDOM */ 1602 if (p_random != NULL) { 1603 keylen = sizeof(*p_random) + random_len; 1604 bcopy(p_random, new_key->key, keylen); 1605 } 1606 /* append in the AUTH chunks */ 1607 if (chunks != NULL) { 1608 bcopy(chunks, new_key->key + keylen, 1609 sizeof(*chunks) + num_chunks); 1610 keylen += sizeof(*chunks) + num_chunks; 1611 } 1612 /* append in the HMACs */ 1613 if (hmacs != NULL) { 1614 bcopy(hmacs, new_key->key + keylen, 1615 sizeof(*hmacs) + hmacs_len); 1616 } 1617 } 1618 if (stcb->asoc.authinfo.random != NULL) 1619 sctp_free_key(stcb->asoc.authinfo.random); 1620 stcb->asoc.authinfo.random = new_key; 1621 stcb->asoc.authinfo.random_len = random_len; 1622 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 1623 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 1624 1625 /* negotiate what HMAC to use for the peer */ 1626 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 1627 stcb->asoc.local_hmacs); 1628 1629 /* copy defaults from the endpoint */ 1630 /* FIX ME: put in cookie? */ 1631 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid; 1632 /* copy out the shared key list (by reference) from the endpoint */ 1633 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys, 1634 &stcb->asoc.shared_keys); 1635} 1636 1637/* 1638 * compute and fill in the HMAC digest for a packet 1639 */ 1640void 1641sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 1642 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid) 1643{ 1644 uint32_t digestlen; 1645 sctp_sharedkey_t *skey; 1646 sctp_key_t *key; 1647 1648 if ((stcb == NULL) || (auth == NULL)) 1649 return; 1650 1651 /* zero the digest + chunk padding */ 1652 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); 1653 bzero(auth->hmac, SCTP_SIZE32(digestlen)); 1654 1655 /* is the desired key cached? */ 1656 if ((keyid != stcb->asoc.authinfo.assoc_keyid) || 1657 (stcb->asoc.authinfo.assoc_key == NULL)) { 1658 if (stcb->asoc.authinfo.assoc_key != NULL) { 1659 /* free the old cached key */ 1660 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1661 } 1662 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1663 /* the only way skey is NULL is if null key id 0 is used */ 1664 if (skey != NULL) 1665 key = skey->key; 1666 else 1667 key = NULL; 1668 /* compute a new assoc key and cache it */ 1669 stcb->asoc.authinfo.assoc_key = 1670 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1671 stcb->asoc.authinfo.peer_random, key); 1672 stcb->asoc.authinfo.assoc_keyid = keyid; 1673 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n", 1674 stcb->asoc.authinfo.assoc_keyid); 1675#ifdef SCTP_DEBUG 1676 if (SCTP_AUTH_DEBUG) 1677 sctp_print_key(stcb->asoc.authinfo.assoc_key, 1678 "Assoc Key"); 1679#endif 1680 } 1681 /* set in the active key id */ 1682 auth->shared_key_id = htons(keyid); 1683 1684 /* compute and fill in the digest */ 1685 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key, 1686 m, auth_offset, auth->hmac); 1687} 1688 1689 1690static void 1691sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size) 1692{ 1693 struct mbuf *m_tmp; 1694 uint8_t *data; 1695 1696 /* sanity check */ 1697 if (m == NULL) 1698 return; 1699 1700 /* find the correct starting mbuf and offset (get start position) */ 1701 m_tmp = m; 1702 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { 1703 m_offset -= SCTP_BUF_LEN(m_tmp); 1704 m_tmp = SCTP_BUF_NEXT(m_tmp); 1705 } 1706 /* now use the rest of the mbuf chain */ 1707 while ((m_tmp != NULL) && (size > 0)) { 1708 data = mtod(m_tmp, uint8_t *) + m_offset; 1709 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) { 1710 bzero(data, SCTP_BUF_LEN(m_tmp)); 1711 size -= SCTP_BUF_LEN(m_tmp); 1712 } else { 1713 bzero(data, size); 1714 size = 0; 1715 } 1716 /* clear the offset since it's only for the first mbuf */ 1717 m_offset = 0; 1718 m_tmp = SCTP_BUF_NEXT(m_tmp); 1719 } 1720} 1721 1722/*- 1723 * process the incoming Authentication chunk 1724 * return codes: 1725 * -1 on any authentication error 1726 * 0 on authentication verification 1727 */ 1728int 1729sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth, 1730 struct mbuf *m, uint32_t offset) 1731{ 1732 uint16_t chunklen; 1733 uint16_t shared_key_id; 1734 uint16_t hmac_id; 1735 sctp_sharedkey_t *skey; 1736 uint32_t digestlen; 1737 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1738 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1739 1740 /* auth is checked for NULL by caller */ 1741 chunklen = ntohs(auth->ch.chunk_length); 1742 if (chunklen < sizeof(*auth)) { 1743 SCTP_STAT_INCR(sctps_recvauthfailed); 1744 return (-1); 1745 } 1746 SCTP_STAT_INCR(sctps_recvauth); 1747 1748 /* get the auth params */ 1749 shared_key_id = ntohs(auth->shared_key_id); 1750 hmac_id = ntohs(auth->hmac_id); 1751 SCTPDBG(SCTP_DEBUG_AUTH1, 1752 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n", 1753 shared_key_id, hmac_id); 1754 1755 /* is the indicated HMAC supported? */ 1756 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) { 1757 struct mbuf *m_err; 1758 struct sctp_auth_invalid_hmac *err; 1759 1760 SCTP_STAT_INCR(sctps_recvivalhmacid); 1761 SCTPDBG(SCTP_DEBUG_AUTH1, 1762 "SCTP Auth: unsupported HMAC id %u\n", 1763 hmac_id); 1764 /* 1765 * report this in an Error Chunk: Unsupported HMAC 1766 * Identifier 1767 */ 1768 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT, 1769 1, MT_HEADER); 1770 if (m_err != NULL) { 1771 /* pre-reserve some space */ 1772 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr)); 1773 /* fill in the error */ 1774 err = mtod(m_err, struct sctp_auth_invalid_hmac *); 1775 bzero(err, sizeof(*err)); 1776 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID); 1777 err->ph.param_length = htons(sizeof(*err)); 1778 err->hmac_id = ntohs(hmac_id); 1779 SCTP_BUF_LEN(m_err) = sizeof(*err); 1780 /* queue it */ 1781 sctp_queue_op_err(stcb, m_err); 1782 } 1783 return (-1); 1784 } 1785 /* get the indicated shared key, if available */ 1786 if ((stcb->asoc.authinfo.recv_key == NULL) || 1787 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) { 1788 /* find the shared key on the assoc first */ 1789 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, 1790 shared_key_id); 1791 /* if the shared key isn't found, discard the chunk */ 1792 if (skey == NULL) { 1793 SCTP_STAT_INCR(sctps_recvivalkeyid); 1794 SCTPDBG(SCTP_DEBUG_AUTH1, 1795 "SCTP Auth: unknown key id %u\n", 1796 shared_key_id); 1797 return (-1); 1798 } 1799 /* generate a notification if this is a new key id */ 1800 if (stcb->asoc.authinfo.recv_keyid != shared_key_id) 1801 /* 1802 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb, 1803 * shared_key_id, (void 1804 * *)stcb->asoc.authinfo.recv_keyid); 1805 */ 1806 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, 1807 shared_key_id, stcb->asoc.authinfo.recv_keyid, 1808 SCTP_SO_NOT_LOCKED); 1809 /* compute a new recv assoc key and cache it */ 1810 if (stcb->asoc.authinfo.recv_key != NULL) 1811 sctp_free_key(stcb->asoc.authinfo.recv_key); 1812 stcb->asoc.authinfo.recv_key = 1813 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1814 stcb->asoc.authinfo.peer_random, skey->key); 1815 stcb->asoc.authinfo.recv_keyid = shared_key_id; 1816#ifdef SCTP_DEBUG 1817 if (SCTP_AUTH_DEBUG) 1818 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key"); 1819#endif 1820 } 1821 /* validate the digest length */ 1822 digestlen = sctp_get_hmac_digest_len(hmac_id); 1823 if (chunklen < (sizeof(*auth) + digestlen)) { 1824 /* invalid digest length */ 1825 SCTP_STAT_INCR(sctps_recvauthfailed); 1826 SCTPDBG(SCTP_DEBUG_AUTH1, 1827 "SCTP Auth: chunk too short for HMAC\n"); 1828 return (-1); 1829 } 1830 /* save a copy of the digest, zero the pseudo header, and validate */ 1831 bcopy(auth->hmac, digest, digestlen); 1832 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen)); 1833 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key, 1834 m, offset, computed_digest); 1835 1836 /* compare the computed digest with the one in the AUTH chunk */ 1837 if (memcmp(digest, computed_digest, digestlen) != 0) { 1838 SCTP_STAT_INCR(sctps_recvauthfailed); 1839 SCTPDBG(SCTP_DEBUG_AUTH1, 1840 "SCTP Auth: HMAC digest check failed\n"); 1841 return (-1); 1842 } 1843 return (0); 1844} 1845 1846/* 1847 * Generate NOTIFICATION 1848 */ 1849void 1850sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication, 1851 uint16_t keyid, uint16_t alt_keyid, int so_locked 1852#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 1853 SCTP_UNUSED 1854#endif 1855) 1856{ 1857 struct mbuf *m_notify; 1858 struct sctp_authkey_event *auth; 1859 struct sctp_queued_to_read *control; 1860 1861 if ((stcb == NULL) || 1862 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 1863 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1864 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) 1865 ) { 1866 /* If the socket is gone we are out of here */ 1867 return; 1868 } 1869 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT)) 1870 /* event not enabled */ 1871 return; 1872 1873 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event), 1874 0, M_DONTWAIT, 1, MT_HEADER); 1875 if (m_notify == NULL) 1876 /* no space left */ 1877 return; 1878 1879 SCTP_BUF_LEN(m_notify) = 0; 1880 auth = mtod(m_notify, struct sctp_authkey_event *); 1881 auth->auth_type = SCTP_AUTHENTICATION_EVENT; 1882 auth->auth_flags = 0; 1883 auth->auth_length = sizeof(*auth); 1884 auth->auth_keynumber = keyid; 1885 auth->auth_altkeynumber = alt_keyid; 1886 auth->auth_indication = indication; 1887 auth->auth_assoc_id = sctp_get_associd(stcb); 1888 1889 SCTP_BUF_LEN(m_notify) = sizeof(*auth); 1890 SCTP_BUF_NEXT(m_notify) = NULL; 1891 1892 /* append to socket */ 1893 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 1894 0, 0, stcb->asoc.context, 0, 0, 0, m_notify); 1895 if (control == NULL) { 1896 /* no memory */ 1897 sctp_m_freem(m_notify); 1898 return; 1899 } 1900 control->spec_flags = M_NOTIFICATION; 1901 control->length = SCTP_BUF_LEN(m_notify); 1902 /* not that we need this */ 1903 control->tail_mbuf = m_notify; 1904 sctp_add_to_readq(stcb->sctp_ep, stcb, control, 1905 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); 1906} 1907 1908 1909/*- 1910 * validates the AUTHentication related parameters in an INIT/INIT-ACK 1911 * Note: currently only used for INIT as INIT-ACK is handled inline 1912 * with sctp_load_addresses_from_init() 1913 */ 1914int 1915sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit) 1916{ 1917 struct sctp_paramhdr *phdr, parm_buf; 1918 uint16_t ptype, plen; 1919 int peer_supports_asconf = 0; 1920 int peer_supports_auth = 0; 1921 int got_random = 0, got_hmacs = 0, got_chklist = 0; 1922 uint8_t saw_asconf = 0; 1923 uint8_t saw_asconf_ack = 0; 1924 1925 /* go through each of the params. */ 1926 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 1927 while (phdr) { 1928 ptype = ntohs(phdr->param_type); 1929 plen = ntohs(phdr->param_length); 1930 1931 if (offset + plen > limit) { 1932 break; 1933 } 1934 if (plen < sizeof(struct sctp_paramhdr)) { 1935 break; 1936 } 1937 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 1938 /* A supported extension chunk */ 1939 struct sctp_supported_chunk_types_param *pr_supported; 1940 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 1941 int num_ent, i; 1942 1943 phdr = sctp_get_next_param(m, offset, 1944 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store))); 1945 if (phdr == NULL) { 1946 return (-1); 1947 } 1948 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 1949 num_ent = plen - sizeof(struct sctp_paramhdr); 1950 for (i = 0; i < num_ent; i++) { 1951 switch (pr_supported->chunk_types[i]) { 1952 case SCTP_ASCONF: 1953 case SCTP_ASCONF_ACK: 1954 peer_supports_asconf = 1; 1955 break; 1956 default: 1957 /* one we don't care about */ 1958 break; 1959 } 1960 } 1961 } else if (ptype == SCTP_RANDOM) { 1962 got_random = 1; 1963 /* enforce the random length */ 1964 if (plen != (sizeof(struct sctp_auth_random) + 1965 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) { 1966 SCTPDBG(SCTP_DEBUG_AUTH1, 1967 "SCTP: invalid RANDOM len\n"); 1968 return (-1); 1969 } 1970 } else if (ptype == SCTP_HMAC_LIST) { 1971 uint8_t store[SCTP_PARAM_BUFFER_SIZE]; 1972 struct sctp_auth_hmac_algo *hmacs; 1973 int num_hmacs; 1974 1975 if (plen > sizeof(store)) 1976 break; 1977 phdr = sctp_get_next_param(m, offset, 1978 (struct sctp_paramhdr *)store, min(plen, sizeof(store))); 1979 if (phdr == NULL) 1980 return (-1); 1981 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1982 num_hmacs = (plen - sizeof(*hmacs)) / 1983 sizeof(hmacs->hmac_ids[0]); 1984 /* validate the hmac list */ 1985 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 1986 SCTPDBG(SCTP_DEBUG_AUTH1, 1987 "SCTP: invalid HMAC param\n"); 1988 return (-1); 1989 } 1990 got_hmacs = 1; 1991 } else if (ptype == SCTP_CHUNK_LIST) { 1992 int i, num_chunks; 1993 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE]; 1994 1995 /* did the peer send a non-empty chunk list? */ 1996 struct sctp_auth_chunk_list *chunks = NULL; 1997 1998 phdr = sctp_get_next_param(m, offset, 1999 (struct sctp_paramhdr *)chunks_store, 2000 min(plen, sizeof(chunks_store))); 2001 if (phdr == NULL) 2002 return (-1); 2003 2004 /*- 2005 * Flip through the list and mark that the 2006 * peer supports asconf/asconf_ack. 2007 */ 2008 chunks = (struct sctp_auth_chunk_list *)phdr; 2009 num_chunks = plen - sizeof(*chunks); 2010 for (i = 0; i < num_chunks; i++) { 2011 /* record asconf/asconf-ack if listed */ 2012 if (chunks->chunk_types[i] == SCTP_ASCONF) 2013 saw_asconf = 1; 2014 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 2015 saw_asconf_ack = 1; 2016 2017 } 2018 if (num_chunks) 2019 got_chklist = 1; 2020 } 2021 offset += SCTP_SIZE32(plen); 2022 if (offset >= limit) { 2023 break; 2024 } 2025 phdr = sctp_get_next_param(m, offset, &parm_buf, 2026 sizeof(parm_buf)); 2027 } 2028 /* validate authentication required parameters */ 2029 if (got_random && got_hmacs) { 2030 peer_supports_auth = 1; 2031 } else { 2032 peer_supports_auth = 0; 2033 } 2034 if (!peer_supports_auth && got_chklist) { 2035 SCTPDBG(SCTP_DEBUG_AUTH1, 2036 "SCTP: peer sent chunk list w/o AUTH\n"); 2037 return (-1); 2038 } 2039 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf && 2040 !peer_supports_auth) { 2041 SCTPDBG(SCTP_DEBUG_AUTH1, 2042 "SCTP: peer supports ASCONF but not AUTH\n"); 2043 return (-1); 2044 } else if ((peer_supports_asconf) && (peer_supports_auth) && 2045 ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 2046 return (-2); 2047 } 2048 return (0); 2049} 2050 2051void 2052sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 2053{ 2054 uint16_t chunks_len = 0; 2055 uint16_t hmacs_len = 0; 2056 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; 2057 sctp_key_t *new_key; 2058 uint16_t keylen; 2059 2060 /* initialize hmac list from endpoint */ 2061 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); 2062 if (stcb->asoc.local_hmacs != NULL) { 2063 hmacs_len = stcb->asoc.local_hmacs->num_algo * 2064 sizeof(stcb->asoc.local_hmacs->hmac[0]); 2065 } 2066 /* initialize auth chunks list from endpoint */ 2067 stcb->asoc.local_auth_chunks = 2068 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); 2069 if (stcb->asoc.local_auth_chunks != NULL) { 2070 int i; 2071 2072 for (i = 0; i < 256; i++) { 2073 if (stcb->asoc.local_auth_chunks->chunks[i]) 2074 chunks_len++; 2075 } 2076 } 2077 /* copy defaults from the endpoint */ 2078 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid; 2079 2080 /* copy out the shared key list (by reference) from the endpoint */ 2081 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, 2082 &stcb->asoc.shared_keys); 2083 2084 /* now set the concatenated key (random + chunks + hmacs) */ 2085 /* key includes parameter headers */ 2086 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len + 2087 hmacs_len; 2088 new_key = sctp_alloc_key(keylen); 2089 if (new_key != NULL) { 2090 struct sctp_paramhdr *ph; 2091 int plen; 2092 2093 /* generate and copy in the RANDOM */ 2094 ph = (struct sctp_paramhdr *)new_key->key; 2095 ph->param_type = htons(SCTP_RANDOM); 2096 plen = sizeof(*ph) + random_len; 2097 ph->param_length = htons(plen); 2098 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len); 2099 keylen = plen; 2100 2101 /* append in the AUTH chunks */ 2102 /* NOTE: currently we always have chunks to list */ 2103 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 2104 ph->param_type = htons(SCTP_CHUNK_LIST); 2105 plen = sizeof(*ph) + chunks_len; 2106 ph->param_length = htons(plen); 2107 keylen += sizeof(*ph); 2108 if (stcb->asoc.local_auth_chunks) { 2109 int i; 2110 2111 for (i = 0; i < 256; i++) { 2112 if (stcb->asoc.local_auth_chunks->chunks[i]) 2113 new_key->key[keylen++] = i; 2114 } 2115 } 2116 /* append in the HMACs */ 2117 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 2118 ph->param_type = htons(SCTP_HMAC_LIST); 2119 plen = sizeof(*ph) + hmacs_len; 2120 ph->param_length = htons(plen); 2121 keylen += sizeof(*ph); 2122 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs, 2123 new_key->key + keylen); 2124 } 2125 if (stcb->asoc.authinfo.random != NULL) 2126 sctp_free_key(stcb->asoc.authinfo.random); 2127 stcb->asoc.authinfo.random = new_key; 2128 stcb->asoc.authinfo.random_len = random_len; 2129}
| 485 } 486 } 487 return (new_key); 488} 489 490 491sctp_sharedkey_t * 492sctp_alloc_sharedkey(void) 493{ 494 sctp_sharedkey_t *new_key; 495 496 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key), 497 SCTP_M_AUTH_KY); 498 if (new_key == NULL) { 499 /* out of memory */ 500 return (NULL); 501 } 502 new_key->keyid = 0; 503 new_key->key = NULL; 504 new_key->refcount = 1; 505 new_key->deactivated = 0; 506 return (new_key); 507} 508 509void 510sctp_free_sharedkey(sctp_sharedkey_t * skey) 511{ 512 if (skey == NULL) 513 return; 514 515 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) { 516 if (skey->key != NULL) 517 sctp_free_key(skey->key); 518 SCTP_FREE(skey, SCTP_M_AUTH_KY); 519 } 520} 521 522sctp_sharedkey_t * 523sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id) 524{ 525 sctp_sharedkey_t *skey; 526 527 LIST_FOREACH(skey, shared_keys, next) { 528 if (skey->keyid == key_id) 529 return (skey); 530 } 531 return (NULL); 532} 533 534int 535sctp_insert_sharedkey(struct sctp_keyhead *shared_keys, 536 sctp_sharedkey_t * new_skey) 537{ 538 sctp_sharedkey_t *skey; 539 540 if ((shared_keys == NULL) || (new_skey == NULL)) 541 return (EINVAL); 542 543 /* insert into an empty list? */ 544 if (LIST_EMPTY(shared_keys)) { 545 LIST_INSERT_HEAD(shared_keys, new_skey, next); 546 return (0); 547 } 548 /* insert into the existing list, ordered by key id */ 549 LIST_FOREACH(skey, shared_keys, next) { 550 if (new_skey->keyid < skey->keyid) { 551 /* insert it before here */ 552 LIST_INSERT_BEFORE(skey, new_skey, next); 553 return (0); 554 } else if (new_skey->keyid == skey->keyid) { 555 /* replace the existing key */ 556 /* verify this key *can* be replaced */ 557 if ((skey->deactivated) && (skey->refcount > 1)) { 558 SCTPDBG(SCTP_DEBUG_AUTH1, 559 "can't replace shared key id %u\n", 560 new_skey->keyid); 561 return (EBUSY); 562 } 563 SCTPDBG(SCTP_DEBUG_AUTH1, 564 "replacing shared key id %u\n", 565 new_skey->keyid); 566 LIST_INSERT_BEFORE(skey, new_skey, next); 567 LIST_REMOVE(skey, next); 568 sctp_free_sharedkey(skey); 569 return (0); 570 } 571 if (LIST_NEXT(skey, next) == NULL) { 572 /* belongs at the end of the list */ 573 LIST_INSERT_AFTER(skey, new_skey, next); 574 return (0); 575 } 576 } 577 /* shouldn't reach here */ 578 return (0); 579} 580 581void 582sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id) 583{ 584 sctp_sharedkey_t *skey; 585 586 /* find the shared key */ 587 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 588 589 /* bump the ref count */ 590 if (skey) { 591 atomic_add_int(&skey->refcount, 1); 592 SCTPDBG(SCTP_DEBUG_AUTH2, 593 "%s: stcb %p key %u refcount acquire to %d\n", 594 __FUNCTION__, stcb, key_id, skey->refcount); 595 } 596} 597 598void 599sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked 600#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 601 SCTP_UNUSED 602#endif 603) 604{ 605 sctp_sharedkey_t *skey; 606 607 /* find the shared key */ 608 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id); 609 610 /* decrement the ref count */ 611 if (skey) { 612 sctp_free_sharedkey(skey); 613 SCTPDBG(SCTP_DEBUG_AUTH2, 614 "%s: stcb %p key %u refcount release to %d\n", 615 __FUNCTION__, stcb, key_id, skey->refcount); 616 617 /* see if a notification should be generated */ 618 if ((skey->refcount <= 1) && (skey->deactivated)) { 619 /* notify ULP that key is no longer used */ 620 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, 621 key_id, 0, so_locked); 622 SCTPDBG(SCTP_DEBUG_AUTH2, 623 "%s: stcb %p key %u no longer used, %d\n", 624 __FUNCTION__, stcb, key_id, skey->refcount); 625 } 626 } 627} 628 629static sctp_sharedkey_t * 630sctp_copy_sharedkey(const sctp_sharedkey_t * skey) 631{ 632 sctp_sharedkey_t *new_skey; 633 634 if (skey == NULL) 635 return (NULL); 636 new_skey = sctp_alloc_sharedkey(); 637 if (new_skey == NULL) 638 return (NULL); 639 if (skey->key != NULL) 640 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen); 641 else 642 new_skey->key = NULL; 643 new_skey->keyid = skey->keyid; 644 return (new_skey); 645} 646 647int 648sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest) 649{ 650 sctp_sharedkey_t *skey, *new_skey; 651 int count = 0; 652 653 if ((src == NULL) || (dest == NULL)) 654 return (0); 655 LIST_FOREACH(skey, src, next) { 656 new_skey = sctp_copy_sharedkey(skey); 657 if (new_skey != NULL) { 658 (void)sctp_insert_sharedkey(dest, new_skey); 659 count++; 660 } 661 } 662 return (count); 663} 664 665 666sctp_hmaclist_t * 667sctp_alloc_hmaclist(uint8_t num_hmacs) 668{ 669 sctp_hmaclist_t *new_list; 670 int alloc_size; 671 672 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]); 673 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size, 674 SCTP_M_AUTH_HL); 675 if (new_list == NULL) { 676 /* out of memory */ 677 return (NULL); 678 } 679 new_list->max_algo = num_hmacs; 680 new_list->num_algo = 0; 681 return (new_list); 682} 683 684void 685sctp_free_hmaclist(sctp_hmaclist_t * list) 686{ 687 if (list != NULL) { 688 SCTP_FREE(list, SCTP_M_AUTH_HL); 689 list = NULL; 690 } 691} 692 693int 694sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id) 695{ 696 int i; 697 698 if (list == NULL) 699 return (-1); 700 if (list->num_algo == list->max_algo) { 701 SCTPDBG(SCTP_DEBUG_AUTH1, 702 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id); 703 return (-1); 704 } 705 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) && 706#ifdef HAVE_SHA224 707 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) && 708#endif 709#ifdef HAVE_SHA2 710 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) && 711 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) && 712 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) && 713#endif 714 1) { 715 return (-1); 716 } 717 /* Now is it already in the list */ 718 for (i = 0; i < list->num_algo; i++) { 719 if (list->hmac[i] == hmac_id) { 720 /* already in list */ 721 return (-1); 722 } 723 } 724 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id); 725 list->hmac[list->num_algo++] = hmac_id; 726 return (0); 727} 728 729sctp_hmaclist_t * 730sctp_copy_hmaclist(sctp_hmaclist_t * list) 731{ 732 sctp_hmaclist_t *new_list; 733 int i; 734 735 if (list == NULL) 736 return (NULL); 737 /* get a new list */ 738 new_list = sctp_alloc_hmaclist(list->max_algo); 739 if (new_list == NULL) 740 return (NULL); 741 /* copy it */ 742 new_list->max_algo = list->max_algo; 743 new_list->num_algo = list->num_algo; 744 for (i = 0; i < list->num_algo; i++) 745 new_list->hmac[i] = list->hmac[i]; 746 return (new_list); 747} 748 749sctp_hmaclist_t * 750sctp_default_supported_hmaclist(void) 751{ 752 sctp_hmaclist_t *new_list; 753 754 new_list = sctp_alloc_hmaclist(2); 755 if (new_list == NULL) 756 return (NULL); 757 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1); 758 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256); 759 return (new_list); 760} 761 762/*- 763 * HMAC algos are listed in priority/preference order 764 * find the best HMAC id to use for the peer based on local support 765 */ 766uint16_t 767sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local) 768{ 769 int i, j; 770 771 if ((local == NULL) || (peer == NULL)) 772 return (SCTP_AUTH_HMAC_ID_RSVD); 773 774 for (i = 0; i < peer->num_algo; i++) { 775 for (j = 0; j < local->num_algo; j++) { 776 if (peer->hmac[i] == local->hmac[j]) { 777 /* found the "best" one */ 778 SCTPDBG(SCTP_DEBUG_AUTH1, 779 "SCTP: negotiated peer HMAC id %u\n", 780 peer->hmac[i]); 781 return (peer->hmac[i]); 782 } 783 } 784 } 785 /* didn't find one! */ 786 return (SCTP_AUTH_HMAC_ID_RSVD); 787} 788 789/*- 790 * serialize the HMAC algo list and return space used 791 * caller must guarantee ptr has appropriate space 792 */ 793int 794sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr) 795{ 796 int i; 797 uint16_t hmac_id; 798 799 if (list == NULL) 800 return (0); 801 802 for (i = 0; i < list->num_algo; i++) { 803 hmac_id = htons(list->hmac[i]); 804 bcopy(&hmac_id, ptr, sizeof(hmac_id)); 805 ptr += sizeof(hmac_id); 806 } 807 return (list->num_algo * sizeof(hmac_id)); 808} 809 810int 811sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs) 812{ 813 uint32_t i; 814 uint16_t hmac_id; 815 uint32_t sha1_supported = 0; 816 817 for (i = 0; i < num_hmacs; i++) { 818 hmac_id = ntohs(hmacs->hmac_ids[i]); 819 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1) 820 sha1_supported = 1; 821 } 822 /* all HMAC id's are supported */ 823 if (sha1_supported == 0) 824 return (-1); 825 else 826 return (0); 827} 828 829sctp_authinfo_t * 830sctp_alloc_authinfo(void) 831{ 832 sctp_authinfo_t *new_authinfo; 833 834 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo), 835 SCTP_M_AUTH_IF); 836 837 if (new_authinfo == NULL) { 838 /* out of memory */ 839 return (NULL); 840 } 841 bzero(new_authinfo, sizeof(*new_authinfo)); 842 return (new_authinfo); 843} 844 845void 846sctp_free_authinfo(sctp_authinfo_t * authinfo) 847{ 848 if (authinfo == NULL) 849 return; 850 851 if (authinfo->random != NULL) 852 sctp_free_key(authinfo->random); 853 if (authinfo->peer_random != NULL) 854 sctp_free_key(authinfo->peer_random); 855 if (authinfo->assoc_key != NULL) 856 sctp_free_key(authinfo->assoc_key); 857 if (authinfo->recv_key != NULL) 858 sctp_free_key(authinfo->recv_key); 859 860 /* We are NOT dynamically allocating authinfo's right now... */ 861 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */ 862} 863 864 865uint32_t 866sctp_get_auth_chunk_len(uint16_t hmac_algo) 867{ 868 int size; 869 870 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo); 871 return (SCTP_SIZE32(size)); 872} 873 874uint32_t 875sctp_get_hmac_digest_len(uint16_t hmac_algo) 876{ 877 switch (hmac_algo) { 878 case SCTP_AUTH_HMAC_ID_SHA1: 879 return (SCTP_AUTH_DIGEST_LEN_SHA1); 880#ifdef HAVE_SHA224 881 case SCTP_AUTH_HMAC_ID_SHA224: 882 return (SCTP_AUTH_DIGEST_LEN_SHA224); 883#endif 884#ifdef HAVE_SHA2 885 case SCTP_AUTH_HMAC_ID_SHA256: 886 return (SCTP_AUTH_DIGEST_LEN_SHA256); 887 case SCTP_AUTH_HMAC_ID_SHA384: 888 return (SCTP_AUTH_DIGEST_LEN_SHA384); 889 case SCTP_AUTH_HMAC_ID_SHA512: 890 return (SCTP_AUTH_DIGEST_LEN_SHA512); 891#endif 892 default: 893 /* unknown HMAC algorithm: can't do anything */ 894 return (0); 895 } /* end switch */ 896} 897 898static inline int 899sctp_get_hmac_block_len(uint16_t hmac_algo) 900{ 901 switch (hmac_algo) { 902 case SCTP_AUTH_HMAC_ID_SHA1: 903#ifdef HAVE_SHA224 904 case SCTP_AUTH_HMAC_ID_SHA224: 905#endif 906 return (64); 907#ifdef HAVE_SHA2 908 case SCTP_AUTH_HMAC_ID_SHA256: 909 return (64); 910 case SCTP_AUTH_HMAC_ID_SHA384: 911 case SCTP_AUTH_HMAC_ID_SHA512: 912 return (128); 913#endif 914 case SCTP_AUTH_HMAC_ID_RSVD: 915 default: 916 /* unknown HMAC algorithm: can't do anything */ 917 return (0); 918 } /* end switch */ 919} 920 921static void 922sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx) 923{ 924 switch (hmac_algo) { 925 case SCTP_AUTH_HMAC_ID_SHA1: 926 SHA1_Init(&ctx->sha1); 927 break; 928#ifdef HAVE_SHA224 929 case SCTP_AUTH_HMAC_ID_SHA224: 930 break; 931#endif 932#ifdef HAVE_SHA2 933 case SCTP_AUTH_HMAC_ID_SHA256: 934 SHA256_Init(&ctx->sha256); 935 break; 936 case SCTP_AUTH_HMAC_ID_SHA384: 937 SHA384_Init(&ctx->sha384); 938 break; 939 case SCTP_AUTH_HMAC_ID_SHA512: 940 SHA512_Init(&ctx->sha512); 941 break; 942#endif 943 case SCTP_AUTH_HMAC_ID_RSVD: 944 default: 945 /* unknown HMAC algorithm: can't do anything */ 946 return; 947 } /* end switch */ 948} 949 950static void 951sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx, 952 uint8_t * text, uint32_t textlen) 953{ 954 switch (hmac_algo) { 955 case SCTP_AUTH_HMAC_ID_SHA1: 956 SHA1_Update(&ctx->sha1, text, textlen); 957 break; 958#ifdef HAVE_SHA224 959 case SCTP_AUTH_HMAC_ID_SHA224: 960 break; 961#endif 962#ifdef HAVE_SHA2 963 case SCTP_AUTH_HMAC_ID_SHA256: 964 SHA256_Update(&ctx->sha256, text, textlen); 965 break; 966 case SCTP_AUTH_HMAC_ID_SHA384: 967 SHA384_Update(&ctx->sha384, text, textlen); 968 break; 969 case SCTP_AUTH_HMAC_ID_SHA512: 970 SHA512_Update(&ctx->sha512, text, textlen); 971 break; 972#endif 973 case SCTP_AUTH_HMAC_ID_RSVD: 974 default: 975 /* unknown HMAC algorithm: can't do anything */ 976 return; 977 } /* end switch */ 978} 979 980static void 981sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx, 982 uint8_t * digest) 983{ 984 switch (hmac_algo) { 985 case SCTP_AUTH_HMAC_ID_SHA1: 986 SHA1_Final(digest, &ctx->sha1); 987 break; 988#ifdef HAVE_SHA224 989 case SCTP_AUTH_HMAC_ID_SHA224: 990 break; 991#endif 992#ifdef HAVE_SHA2 993 case SCTP_AUTH_HMAC_ID_SHA256: 994 SHA256_Final(digest, &ctx->sha256); 995 break; 996 case SCTP_AUTH_HMAC_ID_SHA384: 997 /* SHA384 is truncated SHA512 */ 998 SHA384_Final(digest, &ctx->sha384); 999 break; 1000 case SCTP_AUTH_HMAC_ID_SHA512: 1001 SHA512_Final(digest, &ctx->sha512); 1002 break; 1003#endif 1004 case SCTP_AUTH_HMAC_ID_RSVD: 1005 default: 1006 /* unknown HMAC algorithm: can't do anything */ 1007 return; 1008 } /* end switch */ 1009} 1010 1011/*- 1012 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104) 1013 * 1014 * Compute the HMAC digest using the desired hash key, text, and HMAC 1015 * algorithm. Resulting digest is placed in 'digest' and digest length 1016 * is returned, if the HMAC was performed. 1017 * 1018 * WARNING: it is up to the caller to supply sufficient space to hold the 1019 * resultant digest. 1020 */ 1021uint32_t 1022sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1023 uint8_t * text, uint32_t textlen, uint8_t * digest) 1024{ 1025 uint32_t digestlen; 1026 uint32_t blocklen; 1027 sctp_hash_context_t ctx; 1028 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1029 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1030 uint32_t i; 1031 1032 /* sanity check the material and length */ 1033 if ((key == NULL) || (keylen == 0) || (text == NULL) || 1034 (textlen == 0) || (digest == NULL)) { 1035 /* can't do HMAC with empty key or text or digest store */ 1036 return (0); 1037 } 1038 /* validate the hmac algo and get the digest length */ 1039 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1040 if (digestlen == 0) 1041 return (0); 1042 1043 /* hash the key if it is longer than the hash block size */ 1044 blocklen = sctp_get_hmac_block_len(hmac_algo); 1045 if (keylen > blocklen) { 1046 sctp_hmac_init(hmac_algo, &ctx); 1047 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1048 sctp_hmac_final(hmac_algo, &ctx, temp); 1049 /* set the hashed key as the key */ 1050 keylen = digestlen; 1051 key = temp; 1052 } 1053 /* initialize the inner/outer pads with the key and "append" zeroes */ 1054 bzero(ipad, blocklen); 1055 bzero(opad, blocklen); 1056 bcopy(key, ipad, keylen); 1057 bcopy(key, opad, keylen); 1058 1059 /* XOR the key with ipad and opad values */ 1060 for (i = 0; i < blocklen; i++) { 1061 ipad[i] ^= 0x36; 1062 opad[i] ^= 0x5c; 1063 } 1064 1065 /* perform inner hash */ 1066 sctp_hmac_init(hmac_algo, &ctx); 1067 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1068 sctp_hmac_update(hmac_algo, &ctx, text, textlen); 1069 sctp_hmac_final(hmac_algo, &ctx, temp); 1070 1071 /* perform outer hash */ 1072 sctp_hmac_init(hmac_algo, &ctx); 1073 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1074 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1075 sctp_hmac_final(hmac_algo, &ctx, digest); 1076 1077 return (digestlen); 1078} 1079 1080/* mbuf version */ 1081uint32_t 1082sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1083 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer) 1084{ 1085 uint32_t digestlen; 1086 uint32_t blocklen; 1087 sctp_hash_context_t ctx; 1088 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */ 1089 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1090 uint32_t i; 1091 struct mbuf *m_tmp; 1092 1093 /* sanity check the material and length */ 1094 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) { 1095 /* can't do HMAC with empty key or text or digest store */ 1096 return (0); 1097 } 1098 /* validate the hmac algo and get the digest length */ 1099 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1100 if (digestlen == 0) 1101 return (0); 1102 1103 /* hash the key if it is longer than the hash block size */ 1104 blocklen = sctp_get_hmac_block_len(hmac_algo); 1105 if (keylen > blocklen) { 1106 sctp_hmac_init(hmac_algo, &ctx); 1107 sctp_hmac_update(hmac_algo, &ctx, key, keylen); 1108 sctp_hmac_final(hmac_algo, &ctx, temp); 1109 /* set the hashed key as the key */ 1110 keylen = digestlen; 1111 key = temp; 1112 } 1113 /* initialize the inner/outer pads with the key and "append" zeroes */ 1114 bzero(ipad, blocklen); 1115 bzero(opad, blocklen); 1116 bcopy(key, ipad, keylen); 1117 bcopy(key, opad, keylen); 1118 1119 /* XOR the key with ipad and opad values */ 1120 for (i = 0; i < blocklen; i++) { 1121 ipad[i] ^= 0x36; 1122 opad[i] ^= 0x5c; 1123 } 1124 1125 /* perform inner hash */ 1126 sctp_hmac_init(hmac_algo, &ctx); 1127 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen); 1128 /* find the correct starting mbuf and offset (get start of text) */ 1129 m_tmp = m; 1130 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { 1131 m_offset -= SCTP_BUF_LEN(m_tmp); 1132 m_tmp = SCTP_BUF_NEXT(m_tmp); 1133 } 1134 /* now use the rest of the mbuf chain for the text */ 1135 while (m_tmp != NULL) { 1136 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) { 1137 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, 1138 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset)); 1139 } else { 1140 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset, 1141 SCTP_BUF_LEN(m_tmp) - m_offset); 1142 } 1143 1144 /* clear the offset since it's only for the first mbuf */ 1145 m_offset = 0; 1146 m_tmp = SCTP_BUF_NEXT(m_tmp); 1147 } 1148 sctp_hmac_final(hmac_algo, &ctx, temp); 1149 1150 /* perform outer hash */ 1151 sctp_hmac_init(hmac_algo, &ctx); 1152 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen); 1153 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen); 1154 sctp_hmac_final(hmac_algo, &ctx, digest); 1155 1156 return (digestlen); 1157} 1158 1159/*- 1160 * verify the HMAC digest using the desired hash key, text, and HMAC 1161 * algorithm. 1162 * Returns -1 on error, 0 on success. 1163 */ 1164int 1165sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen, 1166 uint8_t * text, uint32_t textlen, 1167 uint8_t * digest, uint32_t digestlen) 1168{ 1169 uint32_t len; 1170 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1171 1172 /* sanity check the material and length */ 1173 if ((key == NULL) || (keylen == 0) || 1174 (text == NULL) || (textlen == 0) || (digest == NULL)) { 1175 /* can't do HMAC with empty key or text or digest */ 1176 return (-1); 1177 } 1178 len = sctp_get_hmac_digest_len(hmac_algo); 1179 if ((len == 0) || (digestlen != len)) 1180 return (-1); 1181 1182 /* compute the expected hash */ 1183 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len) 1184 return (-1); 1185 1186 if (memcmp(digest, temp, digestlen) != 0) 1187 return (-1); 1188 else 1189 return (0); 1190} 1191 1192 1193/* 1194 * computes the requested HMAC using a key struct (which may be modified if 1195 * the keylen exceeds the HMAC block len). 1196 */ 1197uint32_t 1198sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text, 1199 uint32_t textlen, uint8_t * digest) 1200{ 1201 uint32_t digestlen; 1202 uint32_t blocklen; 1203 sctp_hash_context_t ctx; 1204 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1205 1206 /* sanity check */ 1207 if ((key == NULL) || (text == NULL) || (textlen == 0) || 1208 (digest == NULL)) { 1209 /* can't do HMAC with empty key or text or digest store */ 1210 return (0); 1211 } 1212 /* validate the hmac algo and get the digest length */ 1213 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1214 if (digestlen == 0) 1215 return (0); 1216 1217 /* hash the key if it is longer than the hash block size */ 1218 blocklen = sctp_get_hmac_block_len(hmac_algo); 1219 if (key->keylen > blocklen) { 1220 sctp_hmac_init(hmac_algo, &ctx); 1221 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1222 sctp_hmac_final(hmac_algo, &ctx, temp); 1223 /* save the hashed key as the new key */ 1224 key->keylen = digestlen; 1225 bcopy(temp, key->key, key->keylen); 1226 } 1227 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen, 1228 digest)); 1229} 1230 1231/* mbuf version */ 1232uint32_t 1233sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m, 1234 uint32_t m_offset, uint8_t * digest) 1235{ 1236 uint32_t digestlen; 1237 uint32_t blocklen; 1238 sctp_hash_context_t ctx; 1239 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX]; 1240 1241 /* sanity check */ 1242 if ((key == NULL) || (m == NULL) || (digest == NULL)) { 1243 /* can't do HMAC with empty key or text or digest store */ 1244 return (0); 1245 } 1246 /* validate the hmac algo and get the digest length */ 1247 digestlen = sctp_get_hmac_digest_len(hmac_algo); 1248 if (digestlen == 0) 1249 return (0); 1250 1251 /* hash the key if it is longer than the hash block size */ 1252 blocklen = sctp_get_hmac_block_len(hmac_algo); 1253 if (key->keylen > blocklen) { 1254 sctp_hmac_init(hmac_algo, &ctx); 1255 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen); 1256 sctp_hmac_final(hmac_algo, &ctx, temp); 1257 /* save the hashed key as the new key */ 1258 key->keylen = digestlen; 1259 bcopy(temp, key->key, key->keylen); 1260 } 1261 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0)); 1262} 1263 1264int 1265sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id) 1266{ 1267 int i; 1268 1269 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD)) 1270 return (0); 1271 1272 for (i = 0; i < list->num_algo; i++) 1273 if (list->hmac[i] == id) 1274 return (1); 1275 1276 /* not in the list */ 1277 return (0); 1278} 1279 1280 1281/*- 1282 * clear any cached key(s) if they match the given key id on an association. 1283 * the cached key(s) will be recomputed and re-cached at next use. 1284 * ASSUMES TCB_LOCK is already held 1285 */ 1286void 1287sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid) 1288{ 1289 if (stcb == NULL) 1290 return; 1291 1292 if (keyid == stcb->asoc.authinfo.assoc_keyid) { 1293 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1294 stcb->asoc.authinfo.assoc_key = NULL; 1295 } 1296 if (keyid == stcb->asoc.authinfo.recv_keyid) { 1297 sctp_free_key(stcb->asoc.authinfo.recv_key); 1298 stcb->asoc.authinfo.recv_key = NULL; 1299 } 1300} 1301 1302/*- 1303 * clear any cached key(s) if they match the given key id for all assocs on 1304 * an endpoint. 1305 * ASSUMES INP_WLOCK is already held 1306 */ 1307void 1308sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid) 1309{ 1310 struct sctp_tcb *stcb; 1311 1312 if (inp == NULL) 1313 return; 1314 1315 /* clear the cached keys on all assocs on this instance */ 1316 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) { 1317 SCTP_TCB_LOCK(stcb); 1318 sctp_clear_cachedkeys(stcb, keyid); 1319 SCTP_TCB_UNLOCK(stcb); 1320 } 1321} 1322 1323/*- 1324 * delete a shared key from an association 1325 * ASSUMES TCB_LOCK is already held 1326 */ 1327int 1328sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1329{ 1330 sctp_sharedkey_t *skey; 1331 1332 if (stcb == NULL) 1333 return (-1); 1334 1335 /* is the keyid the assoc active sending key */ 1336 if (keyid == stcb->asoc.authinfo.active_keyid) 1337 return (-1); 1338 1339 /* does the key exist? */ 1340 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1341 if (skey == NULL) 1342 return (-1); 1343 1344 /* are there other refcount holders on the key? */ 1345 if (skey->refcount > 1) 1346 return (-1); 1347 1348 /* remove it */ 1349 LIST_REMOVE(skey, next); 1350 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1351 1352 /* clear any cached keys */ 1353 sctp_clear_cachedkeys(stcb, keyid); 1354 return (0); 1355} 1356 1357/*- 1358 * deletes a shared key from the endpoint 1359 * ASSUMES INP_WLOCK is already held 1360 */ 1361int 1362sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1363{ 1364 sctp_sharedkey_t *skey; 1365 1366 if (inp == NULL) 1367 return (-1); 1368 1369 /* is the keyid the active sending key on the endpoint */ 1370 if (keyid == inp->sctp_ep.default_keyid) 1371 return (-1); 1372 1373 /* does the key exist? */ 1374 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1375 if (skey == NULL) 1376 return (-1); 1377 1378 /* endpoint keys are not refcounted */ 1379 1380 /* remove it */ 1381 LIST_REMOVE(skey, next); 1382 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1383 1384 /* clear any cached keys */ 1385 sctp_clear_cachedkeys_ep(inp, keyid); 1386 return (0); 1387} 1388 1389/*- 1390 * set the active key on an association 1391 * ASSUMES TCB_LOCK is already held 1392 */ 1393int 1394sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid) 1395{ 1396 sctp_sharedkey_t *skey = NULL; 1397 1398 /* find the key on the assoc */ 1399 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1400 if (skey == NULL) { 1401 /* that key doesn't exist */ 1402 return (-1); 1403 } 1404 if ((skey->deactivated) && (skey->refcount > 1)) { 1405 /* can't reactivate a deactivated key with other refcounts */ 1406 return (-1); 1407 } 1408 /* set the (new) active key */ 1409 stcb->asoc.authinfo.active_keyid = keyid; 1410 /* reset the deactivated flag */ 1411 skey->deactivated = 0; 1412 1413 return (0); 1414} 1415 1416/*- 1417 * set the active key on an endpoint 1418 * ASSUMES INP_WLOCK is already held 1419 */ 1420int 1421sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1422{ 1423 sctp_sharedkey_t *skey; 1424 1425 /* find the key */ 1426 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1427 if (skey == NULL) { 1428 /* that key doesn't exist */ 1429 return (-1); 1430 } 1431 inp->sctp_ep.default_keyid = keyid; 1432 return (0); 1433} 1434 1435/*- 1436 * deactivates a shared key from the association 1437 * ASSUMES INP_WLOCK is already held 1438 */ 1439int 1440sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid) 1441{ 1442 sctp_sharedkey_t *skey; 1443 1444 if (stcb == NULL) 1445 return (-1); 1446 1447 /* is the keyid the assoc active sending key */ 1448 if (keyid == stcb->asoc.authinfo.active_keyid) 1449 return (-1); 1450 1451 /* does the key exist? */ 1452 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1453 if (skey == NULL) 1454 return (-1); 1455 1456 /* are there other refcount holders on the key? */ 1457 if (skey->refcount == 1) { 1458 /* no other users, send a notification for this key */ 1459 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0, 1460 SCTP_SO_LOCKED); 1461 } 1462 /* mark the key as deactivated */ 1463 skey->deactivated = 1; 1464 1465 return (0); 1466} 1467 1468/*- 1469 * deactivates a shared key from the endpoint 1470 * ASSUMES INP_WLOCK is already held 1471 */ 1472int 1473sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid) 1474{ 1475 sctp_sharedkey_t *skey; 1476 1477 if (inp == NULL) 1478 return (-1); 1479 1480 /* is the keyid the active sending key on the endpoint */ 1481 if (keyid == inp->sctp_ep.default_keyid) 1482 return (-1); 1483 1484 /* does the key exist? */ 1485 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid); 1486 if (skey == NULL) 1487 return (-1); 1488 1489 /* endpoint keys are not refcounted */ 1490 1491 /* remove it */ 1492 LIST_REMOVE(skey, next); 1493 sctp_free_sharedkey(skey); /* frees skey->key as well */ 1494 1495 return (0); 1496} 1497 1498/* 1499 * get local authentication parameters from cookie (from INIT-ACK) 1500 */ 1501void 1502sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m, 1503 uint32_t offset, uint32_t length) 1504{ 1505 struct sctp_paramhdr *phdr, tmp_param; 1506 uint16_t plen, ptype; 1507 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE]; 1508 struct sctp_auth_random *p_random = NULL; 1509 uint16_t random_len = 0; 1510 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE]; 1511 struct sctp_auth_hmac_algo *hmacs = NULL; 1512 uint16_t hmacs_len = 0; 1513 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE]; 1514 struct sctp_auth_chunk_list *chunks = NULL; 1515 uint16_t num_chunks = 0; 1516 sctp_key_t *new_key; 1517 uint32_t keylen; 1518 1519 /* convert to upper bound */ 1520 length += offset; 1521 1522 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, 1523 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param); 1524 while (phdr != NULL) { 1525 ptype = ntohs(phdr->param_type); 1526 plen = ntohs(phdr->param_length); 1527 1528 if ((plen == 0) || (offset + plen > length)) 1529 break; 1530 1531 if (ptype == SCTP_RANDOM) { 1532 if (plen > sizeof(random_store)) 1533 break; 1534 phdr = sctp_get_next_param(m, offset, 1535 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store))); 1536 if (phdr == NULL) 1537 return; 1538 /* save the random and length for the key */ 1539 p_random = (struct sctp_auth_random *)phdr; 1540 random_len = plen - sizeof(*p_random); 1541 } else if (ptype == SCTP_HMAC_LIST) { 1542 int num_hmacs; 1543 int i; 1544 1545 if (plen > sizeof(hmacs_store)) 1546 break; 1547 phdr = sctp_get_next_param(m, offset, 1548 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store))); 1549 if (phdr == NULL) 1550 return; 1551 /* save the hmacs list and num for the key */ 1552 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1553 hmacs_len = plen - sizeof(*hmacs); 1554 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]); 1555 if (stcb->asoc.local_hmacs != NULL) 1556 sctp_free_hmaclist(stcb->asoc.local_hmacs); 1557 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs); 1558 if (stcb->asoc.local_hmacs != NULL) { 1559 for (i = 0; i < num_hmacs; i++) { 1560 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs, 1561 ntohs(hmacs->hmac_ids[i])); 1562 } 1563 } 1564 } else if (ptype == SCTP_CHUNK_LIST) { 1565 int i; 1566 1567 if (plen > sizeof(chunks_store)) 1568 break; 1569 phdr = sctp_get_next_param(m, offset, 1570 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store))); 1571 if (phdr == NULL) 1572 return; 1573 chunks = (struct sctp_auth_chunk_list *)phdr; 1574 num_chunks = plen - sizeof(*chunks); 1575 /* save chunks list and num for the key */ 1576 if (stcb->asoc.local_auth_chunks != NULL) 1577 sctp_clear_chunklist(stcb->asoc.local_auth_chunks); 1578 else 1579 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist(); 1580 for (i = 0; i < num_chunks; i++) { 1581 (void)sctp_auth_add_chunk(chunks->chunk_types[i], 1582 stcb->asoc.local_auth_chunks); 1583 } 1584 } 1585 /* get next parameter */ 1586 offset += SCTP_SIZE32(plen); 1587 if (offset + sizeof(struct sctp_paramhdr) > length) 1588 break; 1589 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr), 1590 (uint8_t *) & tmp_param); 1591 } 1592 /* concatenate the full random key */ 1593 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len; 1594 if (chunks != NULL) { 1595 keylen += sizeof(*chunks) + num_chunks; 1596 } 1597 new_key = sctp_alloc_key(keylen); 1598 if (new_key != NULL) { 1599 /* copy in the RANDOM */ 1600 if (p_random != NULL) { 1601 keylen = sizeof(*p_random) + random_len; 1602 bcopy(p_random, new_key->key, keylen); 1603 } 1604 /* append in the AUTH chunks */ 1605 if (chunks != NULL) { 1606 bcopy(chunks, new_key->key + keylen, 1607 sizeof(*chunks) + num_chunks); 1608 keylen += sizeof(*chunks) + num_chunks; 1609 } 1610 /* append in the HMACs */ 1611 if (hmacs != NULL) { 1612 bcopy(hmacs, new_key->key + keylen, 1613 sizeof(*hmacs) + hmacs_len); 1614 } 1615 } 1616 if (stcb->asoc.authinfo.random != NULL) 1617 sctp_free_key(stcb->asoc.authinfo.random); 1618 stcb->asoc.authinfo.random = new_key; 1619 stcb->asoc.authinfo.random_len = random_len; 1620 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid); 1621 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid); 1622 1623 /* negotiate what HMAC to use for the peer */ 1624 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs, 1625 stcb->asoc.local_hmacs); 1626 1627 /* copy defaults from the endpoint */ 1628 /* FIX ME: put in cookie? */ 1629 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid; 1630 /* copy out the shared key list (by reference) from the endpoint */ 1631 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys, 1632 &stcb->asoc.shared_keys); 1633} 1634 1635/* 1636 * compute and fill in the HMAC digest for a packet 1637 */ 1638void 1639sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset, 1640 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid) 1641{ 1642 uint32_t digestlen; 1643 sctp_sharedkey_t *skey; 1644 sctp_key_t *key; 1645 1646 if ((stcb == NULL) || (auth == NULL)) 1647 return; 1648 1649 /* zero the digest + chunk padding */ 1650 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id); 1651 bzero(auth->hmac, SCTP_SIZE32(digestlen)); 1652 1653 /* is the desired key cached? */ 1654 if ((keyid != stcb->asoc.authinfo.assoc_keyid) || 1655 (stcb->asoc.authinfo.assoc_key == NULL)) { 1656 if (stcb->asoc.authinfo.assoc_key != NULL) { 1657 /* free the old cached key */ 1658 sctp_free_key(stcb->asoc.authinfo.assoc_key); 1659 } 1660 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid); 1661 /* the only way skey is NULL is if null key id 0 is used */ 1662 if (skey != NULL) 1663 key = skey->key; 1664 else 1665 key = NULL; 1666 /* compute a new assoc key and cache it */ 1667 stcb->asoc.authinfo.assoc_key = 1668 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1669 stcb->asoc.authinfo.peer_random, key); 1670 stcb->asoc.authinfo.assoc_keyid = keyid; 1671 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n", 1672 stcb->asoc.authinfo.assoc_keyid); 1673#ifdef SCTP_DEBUG 1674 if (SCTP_AUTH_DEBUG) 1675 sctp_print_key(stcb->asoc.authinfo.assoc_key, 1676 "Assoc Key"); 1677#endif 1678 } 1679 /* set in the active key id */ 1680 auth->shared_key_id = htons(keyid); 1681 1682 /* compute and fill in the digest */ 1683 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key, 1684 m, auth_offset, auth->hmac); 1685} 1686 1687 1688static void 1689sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size) 1690{ 1691 struct mbuf *m_tmp; 1692 uint8_t *data; 1693 1694 /* sanity check */ 1695 if (m == NULL) 1696 return; 1697 1698 /* find the correct starting mbuf and offset (get start position) */ 1699 m_tmp = m; 1700 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) { 1701 m_offset -= SCTP_BUF_LEN(m_tmp); 1702 m_tmp = SCTP_BUF_NEXT(m_tmp); 1703 } 1704 /* now use the rest of the mbuf chain */ 1705 while ((m_tmp != NULL) && (size > 0)) { 1706 data = mtod(m_tmp, uint8_t *) + m_offset; 1707 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) { 1708 bzero(data, SCTP_BUF_LEN(m_tmp)); 1709 size -= SCTP_BUF_LEN(m_tmp); 1710 } else { 1711 bzero(data, size); 1712 size = 0; 1713 } 1714 /* clear the offset since it's only for the first mbuf */ 1715 m_offset = 0; 1716 m_tmp = SCTP_BUF_NEXT(m_tmp); 1717 } 1718} 1719 1720/*- 1721 * process the incoming Authentication chunk 1722 * return codes: 1723 * -1 on any authentication error 1724 * 0 on authentication verification 1725 */ 1726int 1727sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth, 1728 struct mbuf *m, uint32_t offset) 1729{ 1730 uint16_t chunklen; 1731 uint16_t shared_key_id; 1732 uint16_t hmac_id; 1733 sctp_sharedkey_t *skey; 1734 uint32_t digestlen; 1735 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1736 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX]; 1737 1738 /* auth is checked for NULL by caller */ 1739 chunklen = ntohs(auth->ch.chunk_length); 1740 if (chunklen < sizeof(*auth)) { 1741 SCTP_STAT_INCR(sctps_recvauthfailed); 1742 return (-1); 1743 } 1744 SCTP_STAT_INCR(sctps_recvauth); 1745 1746 /* get the auth params */ 1747 shared_key_id = ntohs(auth->shared_key_id); 1748 hmac_id = ntohs(auth->hmac_id); 1749 SCTPDBG(SCTP_DEBUG_AUTH1, 1750 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n", 1751 shared_key_id, hmac_id); 1752 1753 /* is the indicated HMAC supported? */ 1754 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) { 1755 struct mbuf *m_err; 1756 struct sctp_auth_invalid_hmac *err; 1757 1758 SCTP_STAT_INCR(sctps_recvivalhmacid); 1759 SCTPDBG(SCTP_DEBUG_AUTH1, 1760 "SCTP Auth: unsupported HMAC id %u\n", 1761 hmac_id); 1762 /* 1763 * report this in an Error Chunk: Unsupported HMAC 1764 * Identifier 1765 */ 1766 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT, 1767 1, MT_HEADER); 1768 if (m_err != NULL) { 1769 /* pre-reserve some space */ 1770 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr)); 1771 /* fill in the error */ 1772 err = mtod(m_err, struct sctp_auth_invalid_hmac *); 1773 bzero(err, sizeof(*err)); 1774 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID); 1775 err->ph.param_length = htons(sizeof(*err)); 1776 err->hmac_id = ntohs(hmac_id); 1777 SCTP_BUF_LEN(m_err) = sizeof(*err); 1778 /* queue it */ 1779 sctp_queue_op_err(stcb, m_err); 1780 } 1781 return (-1); 1782 } 1783 /* get the indicated shared key, if available */ 1784 if ((stcb->asoc.authinfo.recv_key == NULL) || 1785 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) { 1786 /* find the shared key on the assoc first */ 1787 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, 1788 shared_key_id); 1789 /* if the shared key isn't found, discard the chunk */ 1790 if (skey == NULL) { 1791 SCTP_STAT_INCR(sctps_recvivalkeyid); 1792 SCTPDBG(SCTP_DEBUG_AUTH1, 1793 "SCTP Auth: unknown key id %u\n", 1794 shared_key_id); 1795 return (-1); 1796 } 1797 /* generate a notification if this is a new key id */ 1798 if (stcb->asoc.authinfo.recv_keyid != shared_key_id) 1799 /* 1800 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb, 1801 * shared_key_id, (void 1802 * *)stcb->asoc.authinfo.recv_keyid); 1803 */ 1804 sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY, 1805 shared_key_id, stcb->asoc.authinfo.recv_keyid, 1806 SCTP_SO_NOT_LOCKED); 1807 /* compute a new recv assoc key and cache it */ 1808 if (stcb->asoc.authinfo.recv_key != NULL) 1809 sctp_free_key(stcb->asoc.authinfo.recv_key); 1810 stcb->asoc.authinfo.recv_key = 1811 sctp_compute_hashkey(stcb->asoc.authinfo.random, 1812 stcb->asoc.authinfo.peer_random, skey->key); 1813 stcb->asoc.authinfo.recv_keyid = shared_key_id; 1814#ifdef SCTP_DEBUG 1815 if (SCTP_AUTH_DEBUG) 1816 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key"); 1817#endif 1818 } 1819 /* validate the digest length */ 1820 digestlen = sctp_get_hmac_digest_len(hmac_id); 1821 if (chunklen < (sizeof(*auth) + digestlen)) { 1822 /* invalid digest length */ 1823 SCTP_STAT_INCR(sctps_recvauthfailed); 1824 SCTPDBG(SCTP_DEBUG_AUTH1, 1825 "SCTP Auth: chunk too short for HMAC\n"); 1826 return (-1); 1827 } 1828 /* save a copy of the digest, zero the pseudo header, and validate */ 1829 bcopy(auth->hmac, digest, digestlen); 1830 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen)); 1831 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key, 1832 m, offset, computed_digest); 1833 1834 /* compare the computed digest with the one in the AUTH chunk */ 1835 if (memcmp(digest, computed_digest, digestlen) != 0) { 1836 SCTP_STAT_INCR(sctps_recvauthfailed); 1837 SCTPDBG(SCTP_DEBUG_AUTH1, 1838 "SCTP Auth: HMAC digest check failed\n"); 1839 return (-1); 1840 } 1841 return (0); 1842} 1843 1844/* 1845 * Generate NOTIFICATION 1846 */ 1847void 1848sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication, 1849 uint16_t keyid, uint16_t alt_keyid, int so_locked 1850#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING) 1851 SCTP_UNUSED 1852#endif 1853) 1854{ 1855 struct mbuf *m_notify; 1856 struct sctp_authkey_event *auth; 1857 struct sctp_queued_to_read *control; 1858 1859 if ((stcb == NULL) || 1860 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) || 1861 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) || 1862 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET) 1863 ) { 1864 /* If the socket is gone we are out of here */ 1865 return; 1866 } 1867 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT)) 1868 /* event not enabled */ 1869 return; 1870 1871 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event), 1872 0, M_DONTWAIT, 1, MT_HEADER); 1873 if (m_notify == NULL) 1874 /* no space left */ 1875 return; 1876 1877 SCTP_BUF_LEN(m_notify) = 0; 1878 auth = mtod(m_notify, struct sctp_authkey_event *); 1879 auth->auth_type = SCTP_AUTHENTICATION_EVENT; 1880 auth->auth_flags = 0; 1881 auth->auth_length = sizeof(*auth); 1882 auth->auth_keynumber = keyid; 1883 auth->auth_altkeynumber = alt_keyid; 1884 auth->auth_indication = indication; 1885 auth->auth_assoc_id = sctp_get_associd(stcb); 1886 1887 SCTP_BUF_LEN(m_notify) = sizeof(*auth); 1888 SCTP_BUF_NEXT(m_notify) = NULL; 1889 1890 /* append to socket */ 1891 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination, 1892 0, 0, stcb->asoc.context, 0, 0, 0, m_notify); 1893 if (control == NULL) { 1894 /* no memory */ 1895 sctp_m_freem(m_notify); 1896 return; 1897 } 1898 control->spec_flags = M_NOTIFICATION; 1899 control->length = SCTP_BUF_LEN(m_notify); 1900 /* not that we need this */ 1901 control->tail_mbuf = m_notify; 1902 sctp_add_to_readq(stcb->sctp_ep, stcb, control, 1903 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked); 1904} 1905 1906 1907/*- 1908 * validates the AUTHentication related parameters in an INIT/INIT-ACK 1909 * Note: currently only used for INIT as INIT-ACK is handled inline 1910 * with sctp_load_addresses_from_init() 1911 */ 1912int 1913sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit) 1914{ 1915 struct sctp_paramhdr *phdr, parm_buf; 1916 uint16_t ptype, plen; 1917 int peer_supports_asconf = 0; 1918 int peer_supports_auth = 0; 1919 int got_random = 0, got_hmacs = 0, got_chklist = 0; 1920 uint8_t saw_asconf = 0; 1921 uint8_t saw_asconf_ack = 0; 1922 1923 /* go through each of the params. */ 1924 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf)); 1925 while (phdr) { 1926 ptype = ntohs(phdr->param_type); 1927 plen = ntohs(phdr->param_length); 1928 1929 if (offset + plen > limit) { 1930 break; 1931 } 1932 if (plen < sizeof(struct sctp_paramhdr)) { 1933 break; 1934 } 1935 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) { 1936 /* A supported extension chunk */ 1937 struct sctp_supported_chunk_types_param *pr_supported; 1938 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE]; 1939 int num_ent, i; 1940 1941 phdr = sctp_get_next_param(m, offset, 1942 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store))); 1943 if (phdr == NULL) { 1944 return (-1); 1945 } 1946 pr_supported = (struct sctp_supported_chunk_types_param *)phdr; 1947 num_ent = plen - sizeof(struct sctp_paramhdr); 1948 for (i = 0; i < num_ent; i++) { 1949 switch (pr_supported->chunk_types[i]) { 1950 case SCTP_ASCONF: 1951 case SCTP_ASCONF_ACK: 1952 peer_supports_asconf = 1; 1953 break; 1954 default: 1955 /* one we don't care about */ 1956 break; 1957 } 1958 } 1959 } else if (ptype == SCTP_RANDOM) { 1960 got_random = 1; 1961 /* enforce the random length */ 1962 if (plen != (sizeof(struct sctp_auth_random) + 1963 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) { 1964 SCTPDBG(SCTP_DEBUG_AUTH1, 1965 "SCTP: invalid RANDOM len\n"); 1966 return (-1); 1967 } 1968 } else if (ptype == SCTP_HMAC_LIST) { 1969 uint8_t store[SCTP_PARAM_BUFFER_SIZE]; 1970 struct sctp_auth_hmac_algo *hmacs; 1971 int num_hmacs; 1972 1973 if (plen > sizeof(store)) 1974 break; 1975 phdr = sctp_get_next_param(m, offset, 1976 (struct sctp_paramhdr *)store, min(plen, sizeof(store))); 1977 if (phdr == NULL) 1978 return (-1); 1979 hmacs = (struct sctp_auth_hmac_algo *)phdr; 1980 num_hmacs = (plen - sizeof(*hmacs)) / 1981 sizeof(hmacs->hmac_ids[0]); 1982 /* validate the hmac list */ 1983 if (sctp_verify_hmac_param(hmacs, num_hmacs)) { 1984 SCTPDBG(SCTP_DEBUG_AUTH1, 1985 "SCTP: invalid HMAC param\n"); 1986 return (-1); 1987 } 1988 got_hmacs = 1; 1989 } else if (ptype == SCTP_CHUNK_LIST) { 1990 int i, num_chunks; 1991 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE]; 1992 1993 /* did the peer send a non-empty chunk list? */ 1994 struct sctp_auth_chunk_list *chunks = NULL; 1995 1996 phdr = sctp_get_next_param(m, offset, 1997 (struct sctp_paramhdr *)chunks_store, 1998 min(plen, sizeof(chunks_store))); 1999 if (phdr == NULL) 2000 return (-1); 2001 2002 /*- 2003 * Flip through the list and mark that the 2004 * peer supports asconf/asconf_ack. 2005 */ 2006 chunks = (struct sctp_auth_chunk_list *)phdr; 2007 num_chunks = plen - sizeof(*chunks); 2008 for (i = 0; i < num_chunks; i++) { 2009 /* record asconf/asconf-ack if listed */ 2010 if (chunks->chunk_types[i] == SCTP_ASCONF) 2011 saw_asconf = 1; 2012 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK) 2013 saw_asconf_ack = 1; 2014 2015 } 2016 if (num_chunks) 2017 got_chklist = 1; 2018 } 2019 offset += SCTP_SIZE32(plen); 2020 if (offset >= limit) { 2021 break; 2022 } 2023 phdr = sctp_get_next_param(m, offset, &parm_buf, 2024 sizeof(parm_buf)); 2025 } 2026 /* validate authentication required parameters */ 2027 if (got_random && got_hmacs) { 2028 peer_supports_auth = 1; 2029 } else { 2030 peer_supports_auth = 0; 2031 } 2032 if (!peer_supports_auth && got_chklist) { 2033 SCTPDBG(SCTP_DEBUG_AUTH1, 2034 "SCTP: peer sent chunk list w/o AUTH\n"); 2035 return (-1); 2036 } 2037 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf && 2038 !peer_supports_auth) { 2039 SCTPDBG(SCTP_DEBUG_AUTH1, 2040 "SCTP: peer supports ASCONF but not AUTH\n"); 2041 return (-1); 2042 } else if ((peer_supports_asconf) && (peer_supports_auth) && 2043 ((saw_asconf == 0) || (saw_asconf_ack == 0))) { 2044 return (-2); 2045 } 2046 return (0); 2047} 2048 2049void 2050sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb) 2051{ 2052 uint16_t chunks_len = 0; 2053 uint16_t hmacs_len = 0; 2054 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT; 2055 sctp_key_t *new_key; 2056 uint16_t keylen; 2057 2058 /* initialize hmac list from endpoint */ 2059 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs); 2060 if (stcb->asoc.local_hmacs != NULL) { 2061 hmacs_len = stcb->asoc.local_hmacs->num_algo * 2062 sizeof(stcb->asoc.local_hmacs->hmac[0]); 2063 } 2064 /* initialize auth chunks list from endpoint */ 2065 stcb->asoc.local_auth_chunks = 2066 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks); 2067 if (stcb->asoc.local_auth_chunks != NULL) { 2068 int i; 2069 2070 for (i = 0; i < 256; i++) { 2071 if (stcb->asoc.local_auth_chunks->chunks[i]) 2072 chunks_len++; 2073 } 2074 } 2075 /* copy defaults from the endpoint */ 2076 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid; 2077 2078 /* copy out the shared key list (by reference) from the endpoint */ 2079 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys, 2080 &stcb->asoc.shared_keys); 2081 2082 /* now set the concatenated key (random + chunks + hmacs) */ 2083 /* key includes parameter headers */ 2084 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len + 2085 hmacs_len; 2086 new_key = sctp_alloc_key(keylen); 2087 if (new_key != NULL) { 2088 struct sctp_paramhdr *ph; 2089 int plen; 2090 2091 /* generate and copy in the RANDOM */ 2092 ph = (struct sctp_paramhdr *)new_key->key; 2093 ph->param_type = htons(SCTP_RANDOM); 2094 plen = sizeof(*ph) + random_len; 2095 ph->param_length = htons(plen); 2096 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len); 2097 keylen = plen; 2098 2099 /* append in the AUTH chunks */ 2100 /* NOTE: currently we always have chunks to list */ 2101 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 2102 ph->param_type = htons(SCTP_CHUNK_LIST); 2103 plen = sizeof(*ph) + chunks_len; 2104 ph->param_length = htons(plen); 2105 keylen += sizeof(*ph); 2106 if (stcb->asoc.local_auth_chunks) { 2107 int i; 2108 2109 for (i = 0; i < 256; i++) { 2110 if (stcb->asoc.local_auth_chunks->chunks[i]) 2111 new_key->key[keylen++] = i; 2112 } 2113 } 2114 /* append in the HMACs */ 2115 ph = (struct sctp_paramhdr *)(new_key->key + keylen); 2116 ph->param_type = htons(SCTP_HMAC_LIST); 2117 plen = sizeof(*ph) + hmacs_len; 2118 ph->param_length = htons(plen); 2119 keylen += sizeof(*ph); 2120 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs, 2121 new_key->key + keylen); 2122 } 2123 if (stcb->asoc.authinfo.random != NULL) 2124 sctp_free_key(stcb->asoc.authinfo.random); 2125 stcb->asoc.authinfo.random = new_key; 2126 stcb->asoc.authinfo.random_len = random_len; 2127}
|