2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
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 SCTP_PRINTF("%s: [Null key]\n", str);
290 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
292 for (i = 0; i < key->keylen; i++)
293 SCTP_PRINTF("%02x", key->key[i]);
296 SCTP_PRINTF("[Null key]\n");
301 sctp_show_key(sctp_key_t * key, const char *str)
306 SCTP_PRINTF("%s: [Null key]\n", str);
309 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
311 for (i = 0; i < key->keylen; i++)
312 SCTP_PRINTF("%02x", key->key[i]);
315 SCTP_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);
474 /* key is shared + key2 + key1 */
475 if (sctp_get_keylen(shared)) {
476 bcopy(shared->key, key_ptr, shared->keylen);
477 key_ptr += shared->keylen;
479 if (sctp_get_keylen(key2)) {
480 bcopy(key2->key, key_ptr, key2->keylen);
481 key_ptr += key2->keylen;
483 if (sctp_get_keylen(key1)) {
484 bcopy(key1->key, key_ptr, key1->keylen);
492 sctp_alloc_sharedkey(void)
494 sctp_sharedkey_t *new_key;
496 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
498 if (new_key == NULL) {
504 new_key->refcount = 1;
505 new_key->deactivated = 0;
510 sctp_free_sharedkey(sctp_sharedkey_t * skey)
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);
523 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
525 sctp_sharedkey_t *skey;
527 LIST_FOREACH(skey, shared_keys, next) {
528 if (skey->keyid == key_id)
535 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
536 sctp_sharedkey_t * new_skey)
538 sctp_sharedkey_t *skey;
540 if ((shared_keys == NULL) || (new_skey == NULL))
543 /* insert into an empty list? */
544 if (LIST_EMPTY(shared_keys)) {
545 LIST_INSERT_HEAD(shared_keys, new_skey, next);
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);
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",
563 SCTPDBG(SCTP_DEBUG_AUTH1,
564 "replacing shared key id %u\n",
566 LIST_INSERT_BEFORE(skey, new_skey, next);
567 LIST_REMOVE(skey, next);
568 sctp_free_sharedkey(skey);
571 if (LIST_NEXT(skey, next) == NULL) {
572 /* belongs at the end of the list */
573 LIST_INSERT_AFTER(skey, new_skey, next);
577 /* shouldn't reach here */
582 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
584 sctp_sharedkey_t *skey;
586 /* find the shared key */
587 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
589 /* bump the ref count */
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);
599 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
600 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
605 sctp_sharedkey_t *skey;
607 /* find the shared key */
608 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
610 /* decrement the ref count */
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);
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);
629 static sctp_sharedkey_t *
630 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
632 sctp_sharedkey_t *new_skey;
636 new_skey = sctp_alloc_sharedkey();
637 if (new_skey == NULL)
639 if (skey->key != NULL)
640 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
642 new_skey->key = NULL;
643 new_skey->keyid = skey->keyid;
648 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
650 sctp_sharedkey_t *skey, *new_skey;
653 if ((src == NULL) || (dest == NULL))
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);
667 sctp_alloc_hmaclist(uint8_t num_hmacs)
669 sctp_hmaclist_t *new_list;
672 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
673 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
675 if (new_list == NULL) {
679 new_list->max_algo = num_hmacs;
680 new_list->num_algo = 0;
685 sctp_free_hmaclist(sctp_hmaclist_t * list)
688 SCTP_FREE(list, SCTP_M_AUTH_HL);
694 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
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);
705 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
707 (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
710 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
711 (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
712 (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
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 */
724 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
725 list->hmac[list->num_algo++] = hmac_id;
730 sctp_copy_hmaclist(sctp_hmaclist_t * list)
732 sctp_hmaclist_t *new_list;
738 new_list = sctp_alloc_hmaclist(list->max_algo);
739 if (new_list == NULL)
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];
750 sctp_default_supported_hmaclist(void)
752 sctp_hmaclist_t *new_list;
754 new_list = sctp_alloc_hmaclist(2);
755 if (new_list == 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);
763 * HMAC algos are listed in priority/preference order
764 * find the best HMAC id to use for the peer based on local support
767 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
771 if ((local == NULL) || (peer == NULL))
772 return (SCTP_AUTH_HMAC_ID_RSVD);
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",
781 return (peer->hmac[i]);
785 /* didn't find one! */
786 return (SCTP_AUTH_HMAC_ID_RSVD);
790 * serialize the HMAC algo list and return space used
791 * caller must guarantee ptr has appropriate space
794 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
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);
807 return (list->num_algo * sizeof(hmac_id));
811 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
815 uint32_t sha1_supported = 0;
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)
822 /* all HMAC id's are supported */
823 if (sha1_supported == 0)
830 sctp_alloc_authinfo(void)
832 sctp_authinfo_t *new_authinfo;
834 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
837 if (new_authinfo == NULL) {
841 bzero(new_authinfo, sizeof(*new_authinfo));
842 return (new_authinfo);
846 sctp_free_authinfo(sctp_authinfo_t * authinfo)
848 if (authinfo == NULL)
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);
860 /* We are NOT dynamically allocating authinfo's right now... */
861 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
866 sctp_get_auth_chunk_len(uint16_t hmac_algo)
870 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
871 return (SCTP_SIZE32(size));
875 sctp_get_hmac_digest_len(uint16_t hmac_algo)
878 case SCTP_AUTH_HMAC_ID_SHA1:
879 return (SCTP_AUTH_DIGEST_LEN_SHA1);
881 case SCTP_AUTH_HMAC_ID_SHA224:
882 return (SCTP_AUTH_DIGEST_LEN_SHA224);
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);
893 /* unknown HMAC algorithm: can't do anything */
899 sctp_get_hmac_block_len(uint16_t hmac_algo)
902 case SCTP_AUTH_HMAC_ID_SHA1:
904 case SCTP_AUTH_HMAC_ID_SHA224:
908 case SCTP_AUTH_HMAC_ID_SHA256:
910 case SCTP_AUTH_HMAC_ID_SHA384:
911 case SCTP_AUTH_HMAC_ID_SHA512:
914 case SCTP_AUTH_HMAC_ID_RSVD:
916 /* unknown HMAC algorithm: can't do anything */
922 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
925 case SCTP_AUTH_HMAC_ID_SHA1:
926 SHA1_Init(&ctx->sha1);
929 case SCTP_AUTH_HMAC_ID_SHA224:
933 case SCTP_AUTH_HMAC_ID_SHA256:
934 SHA256_Init(&ctx->sha256);
936 case SCTP_AUTH_HMAC_ID_SHA384:
937 SHA384_Init(&ctx->sha384);
939 case SCTP_AUTH_HMAC_ID_SHA512:
940 SHA512_Init(&ctx->sha512);
943 case SCTP_AUTH_HMAC_ID_RSVD:
945 /* unknown HMAC algorithm: can't do anything */
951 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
952 uint8_t * text, uint32_t textlen)
955 case SCTP_AUTH_HMAC_ID_SHA1:
956 SHA1_Update(&ctx->sha1, text, textlen);
959 case SCTP_AUTH_HMAC_ID_SHA224:
963 case SCTP_AUTH_HMAC_ID_SHA256:
964 SHA256_Update(&ctx->sha256, text, textlen);
966 case SCTP_AUTH_HMAC_ID_SHA384:
967 SHA384_Update(&ctx->sha384, text, textlen);
969 case SCTP_AUTH_HMAC_ID_SHA512:
970 SHA512_Update(&ctx->sha512, text, textlen);
973 case SCTP_AUTH_HMAC_ID_RSVD:
975 /* unknown HMAC algorithm: can't do anything */
981 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
985 case SCTP_AUTH_HMAC_ID_SHA1:
986 SHA1_Final(digest, &ctx->sha1);
989 case SCTP_AUTH_HMAC_ID_SHA224:
993 case SCTP_AUTH_HMAC_ID_SHA256:
994 SHA256_Final(digest, &ctx->sha256);
996 case SCTP_AUTH_HMAC_ID_SHA384:
997 /* SHA384 is truncated SHA512 */
998 SHA384_Final(digest, &ctx->sha384);
1000 case SCTP_AUTH_HMAC_ID_SHA512:
1001 SHA512_Final(digest, &ctx->sha512);
1004 case SCTP_AUTH_HMAC_ID_RSVD:
1006 /* unknown HMAC algorithm: can't do anything */
1012 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
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.
1018 * WARNING: it is up to the caller to supply sufficient space to hold the
1022 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1023 uint8_t * text, uint32_t textlen, uint8_t * digest)
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];
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 */
1038 /* validate the hmac algo and get the digest length */
1039 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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 */
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);
1059 /* XOR the key with ipad and opad values */
1060 for (i = 0; i < blocklen; i++) {
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);
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);
1082 sctp_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)
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];
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 */
1098 /* validate the hmac algo and get the digest length */
1099 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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 */
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);
1119 /* XOR the key with ipad and opad values */
1120 for (i = 0; i < blocklen; i++) {
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) */
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);
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));
1140 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1141 SCTP_BUF_LEN(m_tmp) - m_offset);
1144 /* clear the offset since it's only for the first mbuf */
1146 m_tmp = SCTP_BUF_NEXT(m_tmp);
1148 sctp_hmac_final(hmac_algo, &ctx, temp);
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);
1160 * verify the HMAC digest using the desired hash key, text, and HMAC
1162 * Returns -1 on error, 0 on success.
1165 sctp_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)
1170 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
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 */
1178 len = sctp_get_hmac_digest_len(hmac_algo);
1179 if ((len == 0) || (digestlen != len))
1182 /* compute the expected hash */
1183 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1186 if (memcmp(digest, temp, digestlen) != 0)
1194 * computes the requested HMAC using a key struct (which may be modified if
1195 * the keylen exceeds the HMAC block len).
1198 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1199 uint32_t textlen, uint8_t * digest)
1203 sctp_hash_context_t ctx;
1204 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1207 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1209 /* can't do HMAC with empty key or text or digest store */
1212 /* validate the hmac algo and get the digest length */
1213 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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);
1227 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1233 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1234 uint32_t m_offset, uint8_t * digest)
1238 sctp_hash_context_t ctx;
1239 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1242 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1243 /* can't do HMAC with empty key or text or digest store */
1246 /* validate the hmac algo and get the digest length */
1247 digestlen = sctp_get_hmac_digest_len(hmac_algo);
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);
1261 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1265 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1269 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1272 for (i = 0; i < list->num_algo; i++)
1273 if (list->hmac[i] == id)
1276 /* not in the list */
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
1287 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1292 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1293 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1294 stcb->asoc.authinfo.assoc_key = NULL;
1296 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1297 sctp_free_key(stcb->asoc.authinfo.recv_key);
1298 stcb->asoc.authinfo.recv_key = NULL;
1303 * clear any cached key(s) if they match the given key id for all assocs on
1305 * ASSUMES INP_WLOCK is already held
1308 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1310 struct sctp_tcb *stcb;
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);
1324 * delete a shared key from an association
1325 * ASSUMES TCB_LOCK is already held
1328 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1330 sctp_sharedkey_t *skey;
1335 /* is the keyid the assoc active sending key */
1336 if (keyid == stcb->asoc.authinfo.active_keyid)
1339 /* does the key exist? */
1340 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1344 /* are there other refcount holders on the key? */
1345 if (skey->refcount > 1)
1349 LIST_REMOVE(skey, next);
1350 sctp_free_sharedkey(skey); /* frees skey->key as well */
1352 /* clear any cached keys */
1353 sctp_clear_cachedkeys(stcb, keyid);
1358 * deletes a shared key from the endpoint
1359 * ASSUMES INP_WLOCK is already held
1362 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1364 sctp_sharedkey_t *skey;
1369 /* is the keyid the active sending key on the endpoint */
1370 if (keyid == inp->sctp_ep.default_keyid)
1373 /* does the key exist? */
1374 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1378 /* endpoint keys are not refcounted */
1381 LIST_REMOVE(skey, next);
1382 sctp_free_sharedkey(skey); /* frees skey->key as well */
1384 /* clear any cached keys */
1385 sctp_clear_cachedkeys_ep(inp, keyid);
1390 * set the active key on an association
1391 * ASSUMES TCB_LOCK is already held
1394 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1396 sctp_sharedkey_t *skey = NULL;
1398 /* find the key on the assoc */
1399 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1401 /* that key doesn't exist */
1404 if ((skey->deactivated) && (skey->refcount > 1)) {
1405 /* can't reactivate a deactivated key with other refcounts */
1408 /* set the (new) active key */
1409 stcb->asoc.authinfo.active_keyid = keyid;
1410 /* reset the deactivated flag */
1411 skey->deactivated = 0;
1417 * set the active key on an endpoint
1418 * ASSUMES INP_WLOCK is already held
1421 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1423 sctp_sharedkey_t *skey;
1426 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1428 /* that key doesn't exist */
1431 inp->sctp_ep.default_keyid = keyid;
1436 * deactivates a shared key from the association
1437 * ASSUMES INP_WLOCK is already held
1440 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1442 sctp_sharedkey_t *skey;
1447 /* is the keyid the assoc active sending key */
1448 if (keyid == stcb->asoc.authinfo.active_keyid)
1451 /* does the key exist? */
1452 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
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,
1462 /* mark the key as deactivated */
1463 skey->deactivated = 1;
1469 * deactivates a shared key from the endpoint
1470 * ASSUMES INP_WLOCK is already held
1473 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1475 sctp_sharedkey_t *skey;
1480 /* is the keyid the active sending key on the endpoint */
1481 if (keyid == inp->sctp_ep.default_keyid)
1484 /* does the key exist? */
1485 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1489 /* endpoint keys are not refcounted */
1492 LIST_REMOVE(skey, next);
1493 sctp_free_sharedkey(skey); /* frees skey->key as well */
1499 * get local authentication parameters from cookie (from INIT-ACK)
1502 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1503 uint32_t offset, uint32_t length)
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;
1519 /* convert to upper bound */
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);
1528 if ((plen == 0) || (offset + plen > length))
1531 if (ptype == SCTP_RANDOM) {
1532 if (plen > sizeof(random_store))
1534 phdr = sctp_get_next_param(m, offset,
1535 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
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) {
1545 if (plen > sizeof(hmacs_store))
1547 phdr = sctp_get_next_param(m, offset,
1548 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
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]));
1564 } else if (ptype == SCTP_CHUNK_LIST) {
1567 if (plen > sizeof(chunks_store))
1569 phdr = sctp_get_next_param(m, offset,
1570 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
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);
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);
1585 /* get next parameter */
1586 offset += SCTP_SIZE32(plen);
1587 if (offset + sizeof(struct sctp_paramhdr) > length)
1589 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1590 (uint8_t *) & tmp_param);
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;
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);
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;
1610 /* append in the HMACs */
1611 if (hmacs != NULL) {
1612 bcopy(hmacs, new_key->key + keylen,
1613 sizeof(*hmacs) + hmacs_len);
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);
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);
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);
1636 * compute and fill in the HMAC digest for a packet
1639 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1640 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1643 sctp_sharedkey_t *skey;
1646 if ((stcb == NULL) || (auth == NULL))
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));
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);
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 */
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);
1674 if (SCTP_AUTH_DEBUG)
1675 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1679 /* set in the active key id */
1680 auth->shared_key_id = htons(keyid);
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);
1689 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1698 /* find the correct starting mbuf and offset (get start position) */
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);
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);
1714 /* clear the offset since it's only for the first mbuf */
1716 m_tmp = SCTP_BUF_NEXT(m_tmp);
1721 * process the incoming Authentication chunk
1723 * -1 on any authentication error
1724 * 0 on authentication verification
1727 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1728 struct mbuf *m, uint32_t offset)
1731 uint16_t shared_key_id;
1733 sctp_sharedkey_t *skey;
1735 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1736 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
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);
1744 SCTP_STAT_INCR(sctps_recvauth);
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);
1753 /* is the indicated HMAC supported? */
1754 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1756 struct sctp_auth_invalid_hmac *err;
1758 SCTP_STAT_INCR(sctps_recvivalhmacid);
1759 SCTPDBG(SCTP_DEBUG_AUTH1,
1760 "SCTP Auth: unsupported HMAC id %u\n",
1763 * report this in an Error Chunk: Unsupported HMAC
1766 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
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);
1779 sctp_queue_op_err(stcb, m_err);
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,
1789 /* if the shared key isn't found, discard the chunk */
1791 SCTP_STAT_INCR(sctps_recvivalkeyid);
1792 SCTPDBG(SCTP_DEBUG_AUTH1,
1793 "SCTP Auth: unknown key id %u\n",
1797 /* generate a notification if this is a new key id */
1798 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1800 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1801 * shared_key_id, (void
1802 * *)stcb->asoc.authinfo.recv_keyid);
1804 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
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;
1815 if (SCTP_AUTH_DEBUG)
1816 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
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");
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);
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");
1845 * Generate NOTIFICATION
1848 sctp_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)
1855 struct mbuf *m_notify;
1856 struct sctp_authkey_event *auth;
1857 struct sctp_queued_to_read *control;
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)
1864 /* If the socket is gone we are out of here */
1867 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1868 /* event not enabled */
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)
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);
1887 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1888 SCTP_BUF_NEXT(m_notify) = NULL;
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) {
1895 sctp_m_freem(m_notify);
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);
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()
1913 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
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;
1923 /* go through each of the params. */
1924 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1926 ptype = ntohs(phdr->param_type);
1927 plen = ntohs(phdr->param_length);
1929 if (offset + plen > limit) {
1932 if (plen < sizeof(struct sctp_paramhdr)) {
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];
1941 phdr = sctp_get_next_param(m, offset,
1942 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
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]) {
1951 case SCTP_ASCONF_ACK:
1952 peer_supports_asconf = 1;
1955 /* one we don't care about */
1959 } else if (ptype == SCTP_RANDOM) {
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");
1968 } else if (ptype == SCTP_HMAC_LIST) {
1969 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1970 struct sctp_auth_hmac_algo *hmacs;
1973 if (plen > sizeof(store))
1975 phdr = sctp_get_next_param(m, offset,
1976 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
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");
1989 } else if (ptype == SCTP_CHUNK_LIST) {
1991 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1993 /* did the peer send a non-empty chunk list? */
1994 struct sctp_auth_chunk_list *chunks = NULL;
1996 phdr = sctp_get_next_param(m, offset,
1997 (struct sctp_paramhdr *)chunks_store,
1998 min(plen, sizeof(chunks_store)));
2003 * Flip through the list and mark that the
2004 * peer supports asconf/asconf_ack.
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)
2012 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
2019 offset += SCTP_SIZE32(plen);
2020 if (offset >= limit) {
2023 phdr = sctp_get_next_param(m, offset, &parm_buf,
2026 /* validate authentication required parameters */
2027 if (got_random && got_hmacs) {
2028 peer_supports_auth = 1;
2030 peer_supports_auth = 0;
2032 if (!peer_supports_auth && got_chklist) {
2033 SCTPDBG(SCTP_DEBUG_AUTH1,
2034 "SCTP: peer sent chunk list w/o AUTH\n");
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");
2042 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
2043 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2050 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
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;
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]);
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) {
2070 for (i = 0; i < 256; i++) {
2071 if (stcb->asoc.local_auth_chunks->chunks[i])
2075 /* copy defaults from the endpoint */
2076 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
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);
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 +
2086 new_key = sctp_alloc_key(keylen);
2087 if (new_key != NULL) {
2088 struct sctp_paramhdr *ph;
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);
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) {
2109 for (i = 0; i < 256; i++) {
2110 if (stcb->asoc.local_auth_chunks->chunks[i])
2111 new_key->key[keylen++] = i;
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);
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;