2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2011, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2011, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_indata.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_auth.h>
48 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
49 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
50 #endif /* SCTP_DEBUG */
54 sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
56 bzero(chklist, sizeof(*chklist));
57 /* chklist->num_chunks = 0; */
61 sctp_alloc_chunklist(void)
63 sctp_auth_chklist_t *chklist;
65 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
67 if (chklist == NULL) {
68 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
70 sctp_clear_chunklist(chklist);
76 sctp_free_chunklist(sctp_auth_chklist_t * list)
79 SCTP_FREE(list, SCTP_M_AUTH_CL);
83 sctp_copy_chunklist(sctp_auth_chklist_t * list)
85 sctp_auth_chklist_t *new_list;
91 new_list = sctp_alloc_chunklist();
95 bcopy(list, new_list, sizeof(*new_list));
102 * add a chunk to the required chunks list
105 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
110 /* is chunk restricted? */
111 if ((chunk == SCTP_INITIATION) ||
112 (chunk == SCTP_INITIATION_ACK) ||
113 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
114 (chunk == SCTP_AUTHENTICATION)) {
117 if (list->chunks[chunk] == 0) {
118 list->chunks[chunk] = 1;
120 SCTPDBG(SCTP_DEBUG_AUTH1,
121 "SCTP: added chunk %u (0x%02x) to Auth list\n",
128 * delete a chunk from the required chunks list
131 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
136 /* is chunk restricted? */
137 if ((chunk == SCTP_ASCONF) ||
138 (chunk == SCTP_ASCONF_ACK)) {
141 if (list->chunks[chunk] == 1) {
142 list->chunks[chunk] = 0;
144 SCTPDBG(SCTP_DEBUG_AUTH1,
145 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
152 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
157 return (list->num_chunks);
161 * set the default list of chunks requiring AUTH
164 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
166 (void)sctp_auth_add_chunk(SCTP_ASCONF, list);
167 (void)sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
171 * return the current number and list of required chunks caller must
172 * guarantee ptr has space for up to 256 bytes
175 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
182 for (i = 0; i < 256; i++) {
183 if (list->chunks[i] != 0) {
192 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
199 if (list->num_chunks <= 32) {
200 /* just list them, one byte each */
201 for (i = 0; i < 256; i++) {
202 if (list->chunks[i] != 0) {
210 /* pack into a 32 byte bitfield */
211 for (i = 0; i < 256; i++) {
212 if (list->chunks[i] != 0) {
215 ptr[index] |= (1 << offset);
224 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
225 sctp_auth_chklist_t * list)
233 if (num_chunks <= 32) {
234 /* just pull them, one byte each */
235 for (i = 0; i < num_chunks; i++) {
236 (void)sctp_auth_add_chunk(*ptr++, list);
242 /* unpack from a 32 byte bitfield */
243 for (index = 0; index < 32; index++) {
244 for (offset = 0; offset < 8; offset++) {
245 if (ptr[index] & (1 << offset)) {
246 (void)sctp_auth_add_chunk((index * 8) + offset, list);
257 * allocate structure space for a key of length keylen
260 sctp_alloc_key(uint32_t keylen)
264 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
266 if (new_key == NULL) {
270 new_key->keylen = keylen;
275 sctp_free_key(sctp_key_t * key)
278 SCTP_FREE(key, SCTP_M_AUTH_KY);
282 sctp_print_key(sctp_key_t * key, const char *str)
287 printf("%s: [Null key]\n", str);
290 printf("%s: len %u, ", str, key->keylen);
292 for (i = 0; i < key->keylen; i++)
293 printf("%02x", key->key[i]);
296 printf("[Null key]\n");
301 sctp_show_key(sctp_key_t * key, const char *str)
306 printf("%s: [Null key]\n", str);
309 printf("%s: len %u, ", str, key->keylen);
311 for (i = 0; i < key->keylen; i++)
312 printf("%02x", key->key[i]);
315 printf("[Null key]\n");
320 sctp_get_keylen(sctp_key_t * key)
323 return (key->keylen);
329 * generate a new random key of length 'keylen'
332 sctp_generate_random_key(uint32_t keylen)
336 /* validate keylen */
337 if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
338 keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
340 new_key = sctp_alloc_key(keylen);
341 if (new_key == NULL) {
345 SCTP_READ_RANDOM(new_key->key, keylen);
346 new_key->keylen = keylen;
351 sctp_set_key(uint8_t * key, uint32_t keylen)
355 new_key = sctp_alloc_key(keylen);
356 if (new_key == NULL) {
360 bcopy(key, new_key->key, keylen);
365 * given two keys of variable size, compute which key is "larger/smaller"
366 * returns: 1 if key1 > key2
371 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
375 uint32_t key1len, key2len;
376 uint8_t *key_1, *key_2;
377 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
379 /* sanity/length check */
380 key1len = sctp_get_keylen(key1);
381 key2len = sctp_get_keylen(key2);
382 if ((key1len == 0) && (key2len == 0))
384 else if (key1len == 0)
386 else if (key2len == 0)
389 if (key1len != key2len) {
390 if (key1len >= key2len)
395 if (key1len < maxlen) {
396 /* prepend zeroes to key1 */
397 bcopy(key1->key, temp + (maxlen - key1len), key1len);
401 /* prepend zeroes to key2 */
402 bcopy(key2->key, temp + (maxlen - key2len), key2len);
412 for (i = 0; i < maxlen; i++) {
415 else if (*key_1 < *key_2)
421 /* keys are equal value, so check lengths */
422 if (key1len == key2len)
424 else if (key1len < key2len)
431 * generate the concatenated keying material based on the two keys and the
432 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
433 * order for concatenation
436 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
442 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
443 sctp_get_keylen(shared);
446 /* get space for the new key */
447 new_key = sctp_alloc_key(keylen);
448 if (new_key == NULL) {
452 new_key->keylen = keylen;
453 key_ptr = new_key->key;
455 /* all keys empty/null?! */
459 /* concatenate the keys */
460 if (sctp_compare_key(key1, key2) <= 0) {
461 /* key is shared + key1 + key2 */
462 if (sctp_get_keylen(shared)) {
463 bcopy(shared->key, key_ptr, shared->keylen);
464 key_ptr += shared->keylen;
466 if (sctp_get_keylen(key1)) {
467 bcopy(key1->key, key_ptr, key1->keylen);
468 key_ptr += key1->keylen;
470 if (sctp_get_keylen(key2)) {
471 bcopy(key2->key, key_ptr, key2->keylen);
472 key_ptr += key2->keylen;
475 /* key is shared + key2 + key1 */
476 if (sctp_get_keylen(shared)) {
477 bcopy(shared->key, key_ptr, shared->keylen);
478 key_ptr += shared->keylen;
480 if (sctp_get_keylen(key2)) {
481 bcopy(key2->key, key_ptr, key2->keylen);
482 key_ptr += key2->keylen;
484 if (sctp_get_keylen(key1)) {
485 bcopy(key1->key, key_ptr, key1->keylen);
486 key_ptr += key1->keylen;
494 sctp_alloc_sharedkey(void)
496 sctp_sharedkey_t *new_key;
498 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
500 if (new_key == NULL) {
506 new_key->refcount = 1;
507 new_key->deactivated = 0;
512 sctp_free_sharedkey(sctp_sharedkey_t * skey)
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);
525 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
527 sctp_sharedkey_t *skey;
529 LIST_FOREACH(skey, shared_keys, next) {
530 if (skey->keyid == key_id)
537 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
538 sctp_sharedkey_t * new_skey)
540 sctp_sharedkey_t *skey;
542 if ((shared_keys == NULL) || (new_skey == NULL))
545 /* insert into an empty list? */
546 if (LIST_EMPTY(shared_keys)) {
547 LIST_INSERT_HEAD(shared_keys, new_skey, next);
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);
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",
565 SCTPDBG(SCTP_DEBUG_AUTH1,
566 "replacing shared key id %u\n",
568 LIST_INSERT_BEFORE(skey, new_skey, next);
569 LIST_REMOVE(skey, next);
570 sctp_free_sharedkey(skey);
573 if (LIST_NEXT(skey, next) == NULL) {
574 /* belongs at the end of the list */
575 LIST_INSERT_AFTER(skey, new_skey, next);
579 /* shouldn't reach here */
584 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
586 sctp_sharedkey_t *skey;
588 /* find the shared key */
589 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
591 /* bump the ref count */
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);
601 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
602 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
607 sctp_sharedkey_t *skey;
609 /* find the shared key */
610 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
612 /* decrement the ref count */
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);
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);
631 static sctp_sharedkey_t *
632 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
634 sctp_sharedkey_t *new_skey;
638 new_skey = sctp_alloc_sharedkey();
639 if (new_skey == NULL)
641 if (skey->key != NULL)
642 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
644 new_skey->key = NULL;
645 new_skey->keyid = skey->keyid;
650 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
652 sctp_sharedkey_t *skey, *new_skey;
655 if ((src == NULL) || (dest == NULL))
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);
669 sctp_alloc_hmaclist(uint8_t num_hmacs)
671 sctp_hmaclist_t *new_list;
674 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
675 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
677 if (new_list == NULL) {
681 new_list->max_algo = num_hmacs;
682 new_list->num_algo = 0;
687 sctp_free_hmaclist(sctp_hmaclist_t * list)
690 SCTP_FREE(list, SCTP_M_AUTH_HL);
696 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
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);
707 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
709 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
712 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
713 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
714 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
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 */
726 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
727 list->hmac[list->num_algo++] = hmac_id;
732 sctp_copy_hmaclist(sctp_hmaclist_t * list)
734 sctp_hmaclist_t *new_list;
740 new_list = sctp_alloc_hmaclist(list->max_algo);
741 if (new_list == NULL)
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];
752 sctp_default_supported_hmaclist(void)
754 sctp_hmaclist_t *new_list;
756 new_list = sctp_alloc_hmaclist(2);
757 if (new_list == 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);
765 * HMAC algos are listed in priority/preference order
766 * find the best HMAC id to use for the peer based on local support
769 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
773 if ((local == NULL) || (peer == NULL))
774 return (SCTP_AUTH_HMAC_ID_RSVD);
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",
783 return (peer->hmac[i]);
787 /* didn't find one! */
788 return (SCTP_AUTH_HMAC_ID_RSVD);
792 * serialize the HMAC algo list and return space used
793 * caller must guarantee ptr has appropriate space
796 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
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);
809 return (list->num_algo * sizeof(hmac_id));
813 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
817 uint32_t sha1_supported = 0;
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)
824 /* all HMAC id's are supported */
825 if (sha1_supported == 0)
832 sctp_alloc_authinfo(void)
834 sctp_authinfo_t *new_authinfo;
836 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
839 if (new_authinfo == NULL) {
843 bzero(new_authinfo, sizeof(*new_authinfo));
844 return (new_authinfo);
848 sctp_free_authinfo(sctp_authinfo_t * authinfo)
850 if (authinfo == NULL)
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);
862 /* We are NOT dynamically allocating authinfo's right now... */
863 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
868 sctp_get_auth_chunk_len(uint16_t hmac_algo)
872 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
873 return (SCTP_SIZE32(size));
877 sctp_get_hmac_digest_len(uint16_t hmac_algo)
880 case SCTP_AUTH_HMAC_ID_SHA1:
881 return (SCTP_AUTH_DIGEST_LEN_SHA1);
883 case SCTP_AUTH_HMAC_ID_SHA224:
884 return (SCTP_AUTH_DIGEST_LEN_SHA224);
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);
895 /* unknown HMAC algorithm: can't do anything */
901 sctp_get_hmac_block_len(uint16_t hmac_algo)
904 case SCTP_AUTH_HMAC_ID_SHA1:
906 case SCTP_AUTH_HMAC_ID_SHA224:
910 case SCTP_AUTH_HMAC_ID_SHA256:
912 case SCTP_AUTH_HMAC_ID_SHA384:
913 case SCTP_AUTH_HMAC_ID_SHA512:
916 case SCTP_AUTH_HMAC_ID_RSVD:
918 /* unknown HMAC algorithm: can't do anything */
924 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
927 case SCTP_AUTH_HMAC_ID_SHA1:
928 SHA1_Init(&ctx->sha1);
931 case SCTP_AUTH_HMAC_ID_SHA224:
935 case SCTP_AUTH_HMAC_ID_SHA256:
936 SHA256_Init(&ctx->sha256);
938 case SCTP_AUTH_HMAC_ID_SHA384:
939 SHA384_Init(&ctx->sha384);
941 case SCTP_AUTH_HMAC_ID_SHA512:
942 SHA512_Init(&ctx->sha512);
945 case SCTP_AUTH_HMAC_ID_RSVD:
947 /* unknown HMAC algorithm: can't do anything */
953 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
954 uint8_t * text, uint32_t textlen)
957 case SCTP_AUTH_HMAC_ID_SHA1:
958 SHA1_Update(&ctx->sha1, text, textlen);
961 case SCTP_AUTH_HMAC_ID_SHA224:
965 case SCTP_AUTH_HMAC_ID_SHA256:
966 SHA256_Update(&ctx->sha256, text, textlen);
968 case SCTP_AUTH_HMAC_ID_SHA384:
969 SHA384_Update(&ctx->sha384, text, textlen);
971 case SCTP_AUTH_HMAC_ID_SHA512:
972 SHA512_Update(&ctx->sha512, text, textlen);
975 case SCTP_AUTH_HMAC_ID_RSVD:
977 /* unknown HMAC algorithm: can't do anything */
983 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
987 case SCTP_AUTH_HMAC_ID_SHA1:
988 SHA1_Final(digest, &ctx->sha1);
991 case SCTP_AUTH_HMAC_ID_SHA224:
995 case SCTP_AUTH_HMAC_ID_SHA256:
996 SHA256_Final(digest, &ctx->sha256);
998 case SCTP_AUTH_HMAC_ID_SHA384:
999 /* SHA384 is truncated SHA512 */
1000 SHA384_Final(digest, &ctx->sha384);
1002 case SCTP_AUTH_HMAC_ID_SHA512:
1003 SHA512_Final(digest, &ctx->sha512);
1006 case SCTP_AUTH_HMAC_ID_RSVD:
1008 /* unknown HMAC algorithm: can't do anything */
1014 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
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.
1020 * WARNING: it is up to the caller to supply sufficient space to hold the
1024 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1025 uint8_t * text, uint32_t textlen, uint8_t * digest)
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];
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 */
1040 /* validate the hmac algo and get the digest length */
1041 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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 */
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);
1061 /* XOR the key with ipad and opad values */
1062 for (i = 0; i < blocklen; i++) {
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);
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);
1084 sctp_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)
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];
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 */
1100 /* validate the hmac algo and get the digest length */
1101 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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 */
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);
1121 /* XOR the key with ipad and opad values */
1122 for (i = 0; i < blocklen; i++) {
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) */
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);
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));
1142 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1143 SCTP_BUF_LEN(m_tmp) - m_offset);
1146 /* clear the offset since it's only for the first mbuf */
1148 m_tmp = SCTP_BUF_NEXT(m_tmp);
1150 sctp_hmac_final(hmac_algo, &ctx, temp);
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);
1162 * verify the HMAC digest using the desired hash key, text, and HMAC
1164 * Returns -1 on error, 0 on success.
1167 sctp_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)
1172 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
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 */
1180 len = sctp_get_hmac_digest_len(hmac_algo);
1181 if ((len == 0) || (digestlen != len))
1184 /* compute the expected hash */
1185 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1188 if (memcmp(digest, temp, digestlen) != 0)
1196 * computes the requested HMAC using a key struct (which may be modified if
1197 * the keylen exceeds the HMAC block len).
1200 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1201 uint32_t textlen, uint8_t * digest)
1205 sctp_hash_context_t ctx;
1206 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1209 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1211 /* can't do HMAC with empty key or text or digest store */
1214 /* validate the hmac algo and get the digest length */
1215 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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);
1229 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1235 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1236 uint32_t m_offset, uint8_t * digest)
1240 sctp_hash_context_t ctx;
1241 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1244 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1245 /* can't do HMAC with empty key or text or digest store */
1248 /* validate the hmac algo and get the digest length */
1249 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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);
1263 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1267 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1271 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1274 for (i = 0; i < list->num_algo; i++)
1275 if (list->hmac[i] == id)
1278 /* not in the list */
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
1289 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1294 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1295 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1296 stcb->asoc.authinfo.assoc_key = NULL;
1298 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1299 sctp_free_key(stcb->asoc.authinfo.recv_key);
1300 stcb->asoc.authinfo.recv_key = NULL;
1305 * clear any cached key(s) if they match the given key id for all assocs on
1307 * ASSUMES INP_WLOCK is already held
1310 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1312 struct sctp_tcb *stcb;
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);
1326 * delete a shared key from an association
1327 * ASSUMES TCB_LOCK is already held
1330 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1332 sctp_sharedkey_t *skey;
1337 /* is the keyid the assoc active sending key */
1338 if (keyid == stcb->asoc.authinfo.active_keyid)
1341 /* does the key exist? */
1342 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1346 /* are there other refcount holders on the key? */
1347 if (skey->refcount > 1)
1351 LIST_REMOVE(skey, next);
1352 sctp_free_sharedkey(skey); /* frees skey->key as well */
1354 /* clear any cached keys */
1355 sctp_clear_cachedkeys(stcb, keyid);
1360 * deletes a shared key from the endpoint
1361 * ASSUMES INP_WLOCK is already held
1364 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1366 sctp_sharedkey_t *skey;
1371 /* is the keyid the active sending key on the endpoint */
1372 if (keyid == inp->sctp_ep.default_keyid)
1375 /* does the key exist? */
1376 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1380 /* endpoint keys are not refcounted */
1383 LIST_REMOVE(skey, next);
1384 sctp_free_sharedkey(skey); /* frees skey->key as well */
1386 /* clear any cached keys */
1387 sctp_clear_cachedkeys_ep(inp, keyid);
1392 * set the active key on an association
1393 * ASSUMES TCB_LOCK is already held
1396 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1398 sctp_sharedkey_t *skey = NULL;
1400 /* find the key on the assoc */
1401 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1403 /* that key doesn't exist */
1406 if ((skey->deactivated) && (skey->refcount > 1)) {
1407 /* can't reactivate a deactivated key with other refcounts */
1410 /* set the (new) active key */
1411 stcb->asoc.authinfo.active_keyid = keyid;
1412 /* reset the deactivated flag */
1413 skey->deactivated = 0;
1419 * set the active key on an endpoint
1420 * ASSUMES INP_WLOCK is already held
1423 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1425 sctp_sharedkey_t *skey;
1428 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1430 /* that key doesn't exist */
1433 inp->sctp_ep.default_keyid = keyid;
1438 * deactivates a shared key from the association
1439 * ASSUMES INP_WLOCK is already held
1442 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1444 sctp_sharedkey_t *skey;
1449 /* is the keyid the assoc active sending key */
1450 if (keyid == stcb->asoc.authinfo.active_keyid)
1453 /* does the key exist? */
1454 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
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,
1464 /* mark the key as deactivated */
1465 skey->deactivated = 1;
1471 * deactivates a shared key from the endpoint
1472 * ASSUMES INP_WLOCK is already held
1475 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1477 sctp_sharedkey_t *skey;
1482 /* is the keyid the active sending key on the endpoint */
1483 if (keyid == inp->sctp_ep.default_keyid)
1486 /* does the key exist? */
1487 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1491 /* endpoint keys are not refcounted */
1494 LIST_REMOVE(skey, next);
1495 sctp_free_sharedkey(skey); /* frees skey->key as well */
1501 * get local authentication parameters from cookie (from INIT-ACK)
1504 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1505 uint32_t offset, uint32_t length)
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;
1521 /* convert to upper bound */
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);
1530 if ((plen == 0) || (offset + plen > length))
1533 if (ptype == SCTP_RANDOM) {
1534 if (plen > sizeof(random_store))
1536 phdr = sctp_get_next_param(m, offset,
1537 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
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) {
1547 if (plen > sizeof(hmacs_store))
1549 phdr = sctp_get_next_param(m, offset,
1550 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
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]));
1566 } else if (ptype == SCTP_CHUNK_LIST) {
1569 if (plen > sizeof(chunks_store))
1571 phdr = sctp_get_next_param(m, offset,
1572 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
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);
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);
1587 /* get next parameter */
1588 offset += SCTP_SIZE32(plen);
1589 if (offset + sizeof(struct sctp_paramhdr) > length)
1591 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1592 (uint8_t *) & tmp_param);
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;
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);
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;
1612 /* append in the HMACs */
1613 if (hmacs != NULL) {
1614 bcopy(hmacs, new_key->key + keylen,
1615 sizeof(*hmacs) + hmacs_len);
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);
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);
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);
1638 * compute and fill in the HMAC digest for a packet
1641 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1642 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1645 sctp_sharedkey_t *skey;
1648 if ((stcb == NULL) || (auth == NULL))
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));
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);
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 */
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);
1676 if (SCTP_AUTH_DEBUG)
1677 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1681 /* set in the active key id */
1682 auth->shared_key_id = htons(keyid);
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);
1691 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1700 /* find the correct starting mbuf and offset (get start position) */
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);
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);
1716 /* clear the offset since it's only for the first mbuf */
1718 m_tmp = SCTP_BUF_NEXT(m_tmp);
1723 * process the incoming Authentication chunk
1725 * -1 on any authentication error
1726 * 0 on authentication verification
1729 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1730 struct mbuf *m, uint32_t offset)
1733 uint16_t shared_key_id;
1735 sctp_sharedkey_t *skey;
1737 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1738 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
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);
1746 SCTP_STAT_INCR(sctps_recvauth);
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);
1755 /* is the indicated HMAC supported? */
1756 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1758 struct sctp_auth_invalid_hmac *err;
1760 SCTP_STAT_INCR(sctps_recvivalhmacid);
1761 SCTPDBG(SCTP_DEBUG_AUTH1,
1762 "SCTP Auth: unsupported HMAC id %u\n",
1765 * report this in an Error Chunk: Unsupported HMAC
1768 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
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);
1781 sctp_queue_op_err(stcb, m_err);
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,
1791 /* if the shared key isn't found, discard the chunk */
1793 SCTP_STAT_INCR(sctps_recvivalkeyid);
1794 SCTPDBG(SCTP_DEBUG_AUTH1,
1795 "SCTP Auth: unknown key id %u\n",
1799 /* generate a notification if this is a new key id */
1800 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1802 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1803 * shared_key_id, (void
1804 * *)stcb->asoc.authinfo.recv_keyid);
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;
1817 if (SCTP_AUTH_DEBUG)
1818 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
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");
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);
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");
1847 * Generate NOTIFICATION
1850 sctp_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)
1857 struct mbuf *m_notify;
1858 struct sctp_authkey_event *auth;
1859 struct sctp_queued_to_read *control;
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)
1866 /* If the socket is gone we are out of here */
1869 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1870 /* event not enabled */
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)
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);
1889 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1890 SCTP_BUF_NEXT(m_notify) = NULL;
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) {
1897 sctp_m_freem(m_notify);
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);
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()
1915 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
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;
1925 /* go through each of the params. */
1926 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1928 ptype = ntohs(phdr->param_type);
1929 plen = ntohs(phdr->param_length);
1931 if (offset + plen > limit) {
1934 if (plen < sizeof(struct sctp_paramhdr)) {
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];
1943 phdr = sctp_get_next_param(m, offset,
1944 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
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]) {
1953 case SCTP_ASCONF_ACK:
1954 peer_supports_asconf = 1;
1957 /* one we don't care about */
1961 } else if (ptype == SCTP_RANDOM) {
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");
1970 } else if (ptype == SCTP_HMAC_LIST) {
1971 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1972 struct sctp_auth_hmac_algo *hmacs;
1975 if (plen > sizeof(store))
1977 phdr = sctp_get_next_param(m, offset,
1978 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
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");
1991 } else if (ptype == SCTP_CHUNK_LIST) {
1993 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1995 /* did the peer send a non-empty chunk list? */
1996 struct sctp_auth_chunk_list *chunks = NULL;
1998 phdr = sctp_get_next_param(m, offset,
1999 (struct sctp_paramhdr *)chunks_store,
2000 min(plen, sizeof(chunks_store)));
2005 * Flip through the list and mark that the
2006 * peer supports asconf/asconf_ack.
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)
2014 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
2021 offset += SCTP_SIZE32(plen);
2022 if (offset >= limit) {
2025 phdr = sctp_get_next_param(m, offset, &parm_buf,
2028 /* validate authentication required parameters */
2029 if (got_random && got_hmacs) {
2030 peer_supports_auth = 1;
2032 peer_supports_auth = 0;
2034 if (!peer_supports_auth && got_chklist) {
2035 SCTPDBG(SCTP_DEBUG_AUTH1,
2036 "SCTP: peer sent chunk list w/o AUTH\n");
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");
2044 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
2045 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2052 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
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;
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]);
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) {
2072 for (i = 0; i < 256; i++) {
2073 if (stcb->asoc.local_auth_chunks->chunks[i])
2077 /* copy defaults from the endpoint */
2078 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
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);
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 +
2088 new_key = sctp_alloc_key(keylen);
2089 if (new_key != NULL) {
2090 struct sctp_paramhdr *ph;
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);
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) {
2111 for (i = 0; i < 256; i++) {
2112 if (stcb->asoc.local_auth_chunks->chunks[i])
2113 new_key->key[keylen++] = i;
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);
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;