2 * SPDX-License-Identifier: BSD-3-Clause
4 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * a) Redistributions of source code must retain the above copyright notice,
12 * this list of conditions and the following disclaimer.
14 * b) Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the distribution.
18 * c) Neither the name of Cisco Systems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32 * THE POSSIBILITY OF SUCH DAMAGE.
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_auth.h>
50 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
51 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
52 #endif /* SCTP_DEBUG */
56 sctp_clear_chunklist(sctp_auth_chklist_t *chklist)
58 memset(chklist, 0, sizeof(*chklist));
59 /* chklist->num_chunks = 0; */
63 sctp_alloc_chunklist(void)
65 sctp_auth_chklist_t *chklist;
67 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
69 if (chklist == NULL) {
70 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
72 sctp_clear_chunklist(chklist);
78 sctp_free_chunklist(sctp_auth_chklist_t *list)
81 SCTP_FREE(list, SCTP_M_AUTH_CL);
85 sctp_copy_chunklist(sctp_auth_chklist_t *list)
87 sctp_auth_chklist_t *new_list;
93 new_list = sctp_alloc_chunklist();
97 memcpy(new_list, list, sizeof(*new_list));
104 * add a chunk to the required chunks list
107 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
112 /* is chunk restricted? */
113 if ((chunk == SCTP_INITIATION) ||
114 (chunk == SCTP_INITIATION_ACK) ||
115 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
116 (chunk == SCTP_AUTHENTICATION)) {
119 if (list->chunks[chunk] == 0) {
120 list->chunks[chunk] = 1;
122 SCTPDBG(SCTP_DEBUG_AUTH1,
123 "SCTP: added chunk %u (0x%02x) to Auth list\n",
130 * delete a chunk from the required chunks list
133 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
138 if (list->chunks[chunk] == 1) {
139 list->chunks[chunk] = 0;
141 SCTPDBG(SCTP_DEBUG_AUTH1,
142 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
149 sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list)
154 return (list->num_chunks);
158 * return the current number and list of required chunks caller must
159 * guarantee ptr has space for up to 256 bytes
162 sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
169 for (i = 0; i < 256; i++) {
170 if (list->chunks[i] != 0) {
179 sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
186 if (list->num_chunks <= 32) {
187 /* just list them, one byte each */
188 for (i = 0; i < 256; i++) {
189 if (list->chunks[i] != 0) {
197 /* pack into a 32 byte bitfield */
198 for (i = 0; i < 256; i++) {
199 if (list->chunks[i] != 0) {
202 ptr[index] |= (1 << offset);
211 sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks,
212 sctp_auth_chklist_t *list)
220 if (num_chunks <= 32) {
221 /* just pull them, one byte each */
222 for (i = 0; i < num_chunks; i++) {
223 (void)sctp_auth_add_chunk(*ptr++, list);
229 /* unpack from a 32 byte bitfield */
230 for (index = 0; index < 32; index++) {
231 for (offset = 0; offset < 8; offset++) {
232 if (ptr[index] & (1 << offset)) {
233 (void)sctp_auth_add_chunk((index * 8) + offset, list);
244 * allocate structure space for a key of length keylen
247 sctp_alloc_key(uint32_t keylen)
251 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
253 if (new_key == NULL) {
257 new_key->keylen = keylen;
262 sctp_free_key(sctp_key_t *key)
265 SCTP_FREE(key, SCTP_M_AUTH_KY);
269 sctp_print_key(sctp_key_t *key, const char *str)
274 SCTP_PRINTF("%s: [Null key]\n", str);
277 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
279 for (i = 0; i < key->keylen; i++)
280 SCTP_PRINTF("%02x", key->key[i]);
283 SCTP_PRINTF("[Null key]\n");
288 sctp_show_key(sctp_key_t *key, const char *str)
293 SCTP_PRINTF("%s: [Null key]\n", str);
296 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
298 for (i = 0; i < key->keylen; i++)
299 SCTP_PRINTF("%02x", key->key[i]);
302 SCTP_PRINTF("[Null key]\n");
307 sctp_get_keylen(sctp_key_t *key)
310 return (key->keylen);
316 * generate a new random key of length 'keylen'
319 sctp_generate_random_key(uint32_t keylen)
323 new_key = sctp_alloc_key(keylen);
324 if (new_key == NULL) {
328 SCTP_READ_RANDOM(new_key->key, keylen);
329 new_key->keylen = keylen;
334 sctp_set_key(uint8_t *key, uint32_t keylen)
338 new_key = sctp_alloc_key(keylen);
339 if (new_key == NULL) {
343 memcpy(new_key->key, key, keylen);
348 * given two keys of variable size, compute which key is "larger/smaller"
349 * returns: 1 if key1 > key2
354 sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2)
358 uint32_t key1len, key2len;
359 uint8_t *key_1, *key_2;
362 /* sanity/length check */
363 key1len = sctp_get_keylen(key1);
364 key2len = sctp_get_keylen(key2);
365 if ((key1len == 0) && (key2len == 0))
367 else if (key1len == 0)
369 else if (key2len == 0)
372 if (key1len < key2len) {
379 /* check for numeric equality */
380 for (i = 0; i < maxlen; i++) {
381 /* left-pad with zeros */
382 val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++);
383 val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++);
386 } else if (val1 < val2) {
390 /* keys are equal value, so check lengths */
391 if (key1len == key2len)
393 else if (key1len < key2len)
400 * generate the concatenated keying material based on the two keys and the
401 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
402 * order for concatenation
405 sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared)
411 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
412 sctp_get_keylen(shared);
415 /* get space for the new key */
416 new_key = sctp_alloc_key(keylen);
417 if (new_key == NULL) {
421 new_key->keylen = keylen;
422 key_ptr = new_key->key;
424 /* all keys empty/null?! */
428 /* concatenate the keys */
429 if (sctp_compare_key(key1, key2) <= 0) {
430 /* key is shared + key1 + key2 */
431 if (sctp_get_keylen(shared)) {
432 memcpy(key_ptr, shared->key, shared->keylen);
433 key_ptr += shared->keylen;
435 if (sctp_get_keylen(key1)) {
436 memcpy(key_ptr, key1->key, key1->keylen);
437 key_ptr += key1->keylen;
439 if (sctp_get_keylen(key2)) {
440 memcpy(key_ptr, key2->key, key2->keylen);
443 /* key is shared + key2 + key1 */
444 if (sctp_get_keylen(shared)) {
445 memcpy(key_ptr, shared->key, shared->keylen);
446 key_ptr += shared->keylen;
448 if (sctp_get_keylen(key2)) {
449 memcpy(key_ptr, key2->key, key2->keylen);
450 key_ptr += key2->keylen;
452 if (sctp_get_keylen(key1)) {
453 memcpy(key_ptr, key1->key, key1->keylen);
461 sctp_alloc_sharedkey(void)
463 sctp_sharedkey_t *new_key;
465 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
467 if (new_key == NULL) {
473 new_key->refcount = 1;
474 new_key->deactivated = 0;
479 sctp_free_sharedkey(sctp_sharedkey_t *skey)
484 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
485 if (skey->key != NULL)
486 sctp_free_key(skey->key);
487 SCTP_FREE(skey, SCTP_M_AUTH_KY);
492 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
494 sctp_sharedkey_t *skey;
496 LIST_FOREACH(skey, shared_keys, next) {
497 if (skey->keyid == key_id)
504 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
505 sctp_sharedkey_t *new_skey)
507 sctp_sharedkey_t *skey;
509 if ((shared_keys == NULL) || (new_skey == NULL))
512 /* insert into an empty list? */
513 if (LIST_EMPTY(shared_keys)) {
514 LIST_INSERT_HEAD(shared_keys, new_skey, next);
517 /* insert into the existing list, ordered by key id */
518 LIST_FOREACH(skey, shared_keys, next) {
519 if (new_skey->keyid < skey->keyid) {
520 /* insert it before here */
521 LIST_INSERT_BEFORE(skey, new_skey, next);
523 } else if (new_skey->keyid == skey->keyid) {
524 /* replace the existing key */
525 /* verify this key *can* be replaced */
526 if ((skey->deactivated) || (skey->refcount > 1)) {
527 SCTPDBG(SCTP_DEBUG_AUTH1,
528 "can't replace shared key id %u\n",
532 SCTPDBG(SCTP_DEBUG_AUTH1,
533 "replacing shared key id %u\n",
535 LIST_INSERT_BEFORE(skey, new_skey, next);
536 LIST_REMOVE(skey, next);
537 sctp_free_sharedkey(skey);
540 if (LIST_NEXT(skey, next) == NULL) {
541 /* belongs at the end of the list */
542 LIST_INSERT_AFTER(skey, new_skey, next);
546 /* shouldn't reach here */
551 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
553 sctp_sharedkey_t *skey;
555 /* find the shared key */
556 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
558 /* bump the ref count */
560 atomic_add_int(&skey->refcount, 1);
561 SCTPDBG(SCTP_DEBUG_AUTH2,
562 "%s: stcb %p key %u refcount acquire to %d\n",
563 __func__, (void *)stcb, key_id, skey->refcount);
568 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
569 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
574 sctp_sharedkey_t *skey;
576 /* find the shared key */
577 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
579 /* decrement the ref count */
581 SCTPDBG(SCTP_DEBUG_AUTH2,
582 "%s: stcb %p key %u refcount release to %d\n",
583 __func__, (void *)stcb, key_id, skey->refcount);
585 /* see if a notification should be generated */
586 if ((skey->refcount <= 2) && (skey->deactivated)) {
587 /* notify ULP that key is no longer used */
588 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
589 key_id, 0, so_locked);
590 SCTPDBG(SCTP_DEBUG_AUTH2,
591 "%s: stcb %p key %u no longer used, %d\n",
592 __func__, (void *)stcb, key_id, skey->refcount);
594 sctp_free_sharedkey(skey);
598 static sctp_sharedkey_t *
599 sctp_copy_sharedkey(const sctp_sharedkey_t *skey)
601 sctp_sharedkey_t *new_skey;
605 new_skey = sctp_alloc_sharedkey();
606 if (new_skey == NULL)
608 if (skey->key != NULL)
609 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
611 new_skey->key = NULL;
612 new_skey->keyid = skey->keyid;
617 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
619 sctp_sharedkey_t *skey, *new_skey;
622 if ((src == NULL) || (dest == NULL))
624 LIST_FOREACH(skey, src, next) {
625 new_skey = sctp_copy_sharedkey(skey);
626 if (new_skey != NULL) {
627 if (sctp_insert_sharedkey(dest, new_skey)) {
628 sctp_free_sharedkey(new_skey);
639 sctp_alloc_hmaclist(uint16_t num_hmacs)
641 sctp_hmaclist_t *new_list;
644 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
645 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
647 if (new_list == NULL) {
651 new_list->max_algo = num_hmacs;
652 new_list->num_algo = 0;
657 sctp_free_hmaclist(sctp_hmaclist_t *list)
660 SCTP_FREE(list, SCTP_M_AUTH_HL);
666 sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id)
672 if (list->num_algo == list->max_algo) {
673 SCTPDBG(SCTP_DEBUG_AUTH1,
674 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
677 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
678 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
681 /* Now is it already in the list */
682 for (i = 0; i < list->num_algo; i++) {
683 if (list->hmac[i] == hmac_id) {
684 /* already in list */
688 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
689 list->hmac[list->num_algo++] = hmac_id;
694 sctp_copy_hmaclist(sctp_hmaclist_t *list)
696 sctp_hmaclist_t *new_list;
702 new_list = sctp_alloc_hmaclist(list->max_algo);
703 if (new_list == NULL)
706 new_list->max_algo = list->max_algo;
707 new_list->num_algo = list->num_algo;
708 for (i = 0; i < list->num_algo; i++)
709 new_list->hmac[i] = list->hmac[i];
714 sctp_default_supported_hmaclist(void)
716 sctp_hmaclist_t *new_list;
718 new_list = sctp_alloc_hmaclist(2);
719 if (new_list == NULL)
721 /* We prefer SHA256, so list it first */
722 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
723 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
728 * HMAC algos are listed in priority/preference order
729 * find the best HMAC id to use for the peer based on local support
732 sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local)
736 if ((local == NULL) || (peer == NULL))
737 return (SCTP_AUTH_HMAC_ID_RSVD);
739 for (i = 0; i < peer->num_algo; i++) {
740 for (j = 0; j < local->num_algo; j++) {
741 if (peer->hmac[i] == local->hmac[j]) {
742 /* found the "best" one */
743 SCTPDBG(SCTP_DEBUG_AUTH1,
744 "SCTP: negotiated peer HMAC id %u\n",
746 return (peer->hmac[i]);
750 /* didn't find one! */
751 return (SCTP_AUTH_HMAC_ID_RSVD);
755 * serialize the HMAC algo list and return space used
756 * caller must guarantee ptr has appropriate space
759 sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr)
767 for (i = 0; i < list->num_algo; i++) {
768 hmac_id = htons(list->hmac[i]);
769 memcpy(ptr, &hmac_id, sizeof(hmac_id));
770 ptr += sizeof(hmac_id);
772 return (list->num_algo * sizeof(hmac_id));
776 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
780 for (i = 0; i < num_hmacs; i++) {
781 if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
789 sctp_alloc_authinfo(void)
791 sctp_authinfo_t *new_authinfo;
793 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
796 if (new_authinfo == NULL) {
800 memset(new_authinfo, 0, sizeof(*new_authinfo));
801 return (new_authinfo);
805 sctp_free_authinfo(sctp_authinfo_t *authinfo)
807 if (authinfo == NULL)
810 if (authinfo->random != NULL)
811 sctp_free_key(authinfo->random);
812 if (authinfo->peer_random != NULL)
813 sctp_free_key(authinfo->peer_random);
814 if (authinfo->assoc_key != NULL)
815 sctp_free_key(authinfo->assoc_key);
816 if (authinfo->recv_key != NULL)
817 sctp_free_key(authinfo->recv_key);
819 /* We are NOT dynamically allocating authinfo's right now... */
820 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
825 sctp_get_auth_chunk_len(uint16_t hmac_algo)
829 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
830 return (SCTP_SIZE32(size));
834 sctp_get_hmac_digest_len(uint16_t hmac_algo)
837 case SCTP_AUTH_HMAC_ID_SHA1:
838 return (SCTP_AUTH_DIGEST_LEN_SHA1);
839 case SCTP_AUTH_HMAC_ID_SHA256:
840 return (SCTP_AUTH_DIGEST_LEN_SHA256);
842 /* unknown HMAC algorithm: can't do anything */
848 sctp_get_hmac_block_len(uint16_t hmac_algo)
851 case SCTP_AUTH_HMAC_ID_SHA1:
853 case SCTP_AUTH_HMAC_ID_SHA256:
855 case SCTP_AUTH_HMAC_ID_RSVD:
857 /* unknown HMAC algorithm: can't do anything */
863 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx)
866 case SCTP_AUTH_HMAC_ID_SHA1:
867 SCTP_SHA1_INIT(&ctx->sha1);
869 case SCTP_AUTH_HMAC_ID_SHA256:
870 SCTP_SHA256_INIT(&ctx->sha256);
872 case SCTP_AUTH_HMAC_ID_RSVD:
874 /* unknown HMAC algorithm: can't do anything */
880 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx,
881 uint8_t *text, uint32_t textlen)
884 case SCTP_AUTH_HMAC_ID_SHA1:
885 SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
887 case SCTP_AUTH_HMAC_ID_SHA256:
888 SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
890 case SCTP_AUTH_HMAC_ID_RSVD:
892 /* unknown HMAC algorithm: can't do anything */
898 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx,
902 case SCTP_AUTH_HMAC_ID_SHA1:
903 SCTP_SHA1_FINAL(digest, &ctx->sha1);
905 case SCTP_AUTH_HMAC_ID_SHA256:
906 SCTP_SHA256_FINAL(digest, &ctx->sha256);
908 case SCTP_AUTH_HMAC_ID_RSVD:
910 /* unknown HMAC algorithm: can't do anything */
916 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
918 * Compute the HMAC digest using the desired hash key, text, and HMAC
919 * algorithm. Resulting digest is placed in 'digest' and digest length
920 * is returned, if the HMAC was performed.
922 * WARNING: it is up to the caller to supply sufficient space to hold the
926 sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
927 uint8_t *text, uint32_t textlen, uint8_t *digest)
931 sctp_hash_context_t ctx;
932 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
933 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
936 /* sanity check the material and length */
937 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
938 (textlen == 0) || (digest == NULL)) {
939 /* can't do HMAC with empty key or text or digest store */
942 /* validate the hmac algo and get the digest length */
943 digestlen = sctp_get_hmac_digest_len(hmac_algo);
947 /* hash the key if it is longer than the hash block size */
948 blocklen = sctp_get_hmac_block_len(hmac_algo);
949 if (keylen > blocklen) {
950 sctp_hmac_init(hmac_algo, &ctx);
951 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
952 sctp_hmac_final(hmac_algo, &ctx, temp);
953 /* set the hashed key as the key */
957 /* initialize the inner/outer pads with the key and "append" zeroes */
958 memset(ipad, 0, blocklen);
959 memset(opad, 0, blocklen);
960 memcpy(ipad, key, keylen);
961 memcpy(opad, key, keylen);
963 /* XOR the key with ipad and opad values */
964 for (i = 0; i < blocklen; i++) {
969 /* perform inner hash */
970 sctp_hmac_init(hmac_algo, &ctx);
971 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
972 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
973 sctp_hmac_final(hmac_algo, &ctx, temp);
975 /* perform outer hash */
976 sctp_hmac_init(hmac_algo, &ctx);
977 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
978 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
979 sctp_hmac_final(hmac_algo, &ctx, digest);
986 sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
987 struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer)
991 sctp_hash_context_t ctx;
992 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
993 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
997 /* sanity check the material and length */
998 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
999 /* can't do HMAC with empty key or text or digest store */
1002 /* validate the hmac algo and get the digest length */
1003 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1007 /* hash the key if it is longer than the hash block size */
1008 blocklen = sctp_get_hmac_block_len(hmac_algo);
1009 if (keylen > blocklen) {
1010 sctp_hmac_init(hmac_algo, &ctx);
1011 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1012 sctp_hmac_final(hmac_algo, &ctx, temp);
1013 /* set the hashed key as the key */
1017 /* initialize the inner/outer pads with the key and "append" zeroes */
1018 memset(ipad, 0, blocklen);
1019 memset(opad, 0, blocklen);
1020 memcpy(ipad, key, keylen);
1021 memcpy(opad, key, keylen);
1023 /* XOR the key with ipad and opad values */
1024 for (i = 0; i < blocklen; i++) {
1029 /* perform inner hash */
1030 sctp_hmac_init(hmac_algo, &ctx);
1031 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1032 /* find the correct starting mbuf and offset (get start of text) */
1034 while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1035 m_offset -= SCTP_BUF_LEN(m_tmp);
1036 m_tmp = SCTP_BUF_NEXT(m_tmp);
1038 /* now use the rest of the mbuf chain for the text */
1039 while (m_tmp != NULL) {
1040 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1041 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1042 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1044 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1045 SCTP_BUF_LEN(m_tmp) - m_offset);
1048 /* clear the offset since it's only for the first mbuf */
1050 m_tmp = SCTP_BUF_NEXT(m_tmp);
1052 sctp_hmac_final(hmac_algo, &ctx, temp);
1054 /* perform outer hash */
1055 sctp_hmac_init(hmac_algo, &ctx);
1056 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1057 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1058 sctp_hmac_final(hmac_algo, &ctx, digest);
1064 * computes the requested HMAC using a key struct (which may be modified if
1065 * the keylen exceeds the HMAC block len).
1068 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1069 uint32_t textlen, uint8_t *digest)
1073 sctp_hash_context_t ctx;
1074 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1077 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1079 /* can't do HMAC with empty key or text or digest store */
1082 /* validate the hmac algo and get the digest length */
1083 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1087 /* hash the key if it is longer than the hash block size */
1088 blocklen = sctp_get_hmac_block_len(hmac_algo);
1089 if (key->keylen > blocklen) {
1090 sctp_hmac_init(hmac_algo, &ctx);
1091 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1092 sctp_hmac_final(hmac_algo, &ctx, temp);
1093 /* save the hashed key as the new key */
1094 key->keylen = digestlen;
1095 memcpy(key->key, temp, key->keylen);
1097 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1103 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1104 uint32_t m_offset, uint8_t *digest)
1108 sctp_hash_context_t ctx;
1109 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1112 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1113 /* can't do HMAC with empty key or text or digest store */
1116 /* validate the hmac algo and get the digest length */
1117 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1121 /* hash the key if it is longer than the hash block size */
1122 blocklen = sctp_get_hmac_block_len(hmac_algo);
1123 if (key->keylen > blocklen) {
1124 sctp_hmac_init(hmac_algo, &ctx);
1125 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1126 sctp_hmac_final(hmac_algo, &ctx, temp);
1127 /* save the hashed key as the new key */
1128 key->keylen = digestlen;
1129 memcpy(key->key, temp, key->keylen);
1131 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1135 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1139 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1142 for (i = 0; i < list->num_algo; i++)
1143 if (list->hmac[i] == id)
1146 /* not in the list */
1152 * clear any cached key(s) if they match the given key id on an association.
1153 * the cached key(s) will be recomputed and re-cached at next use.
1154 * ASSUMES TCB_LOCK is already held
1157 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1162 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1163 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1164 stcb->asoc.authinfo.assoc_key = NULL;
1166 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1167 sctp_free_key(stcb->asoc.authinfo.recv_key);
1168 stcb->asoc.authinfo.recv_key = NULL;
1173 * clear any cached key(s) if they match the given key id for all assocs on
1175 * ASSUMES INP_WLOCK is already held
1178 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1180 struct sctp_tcb *stcb;
1185 /* clear the cached keys on all assocs on this instance */
1186 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1187 SCTP_TCB_LOCK(stcb);
1188 sctp_clear_cachedkeys(stcb, keyid);
1189 SCTP_TCB_UNLOCK(stcb);
1194 * delete a shared key from an association
1195 * ASSUMES TCB_LOCK is already held
1198 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1200 sctp_sharedkey_t *skey;
1205 /* is the keyid the assoc active sending key */
1206 if (keyid == stcb->asoc.authinfo.active_keyid)
1209 /* does the key exist? */
1210 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1214 /* are there other refcount holders on the key? */
1215 if (skey->refcount > 1)
1219 LIST_REMOVE(skey, next);
1220 sctp_free_sharedkey(skey); /* frees skey->key as well */
1222 /* clear any cached keys */
1223 sctp_clear_cachedkeys(stcb, keyid);
1228 * deletes a shared key from the endpoint
1229 * ASSUMES INP_WLOCK is already held
1232 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1234 sctp_sharedkey_t *skey;
1239 /* is the keyid the active sending key on the endpoint */
1240 if (keyid == inp->sctp_ep.default_keyid)
1243 /* does the key exist? */
1244 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1248 /* endpoint keys are not refcounted */
1251 LIST_REMOVE(skey, next);
1252 sctp_free_sharedkey(skey); /* frees skey->key as well */
1254 /* clear any cached keys */
1255 sctp_clear_cachedkeys_ep(inp, keyid);
1260 * set the active key on an association
1261 * ASSUMES TCB_LOCK is already held
1264 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1266 sctp_sharedkey_t *skey = NULL;
1268 /* find the key on the assoc */
1269 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1271 /* that key doesn't exist */
1274 if ((skey->deactivated) && (skey->refcount > 1)) {
1275 /* can't reactivate a deactivated key with other refcounts */
1279 /* set the (new) active key */
1280 stcb->asoc.authinfo.active_keyid = keyid;
1281 /* reset the deactivated flag */
1282 skey->deactivated = 0;
1288 * set the active key on an endpoint
1289 * ASSUMES INP_WLOCK is already held
1292 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1294 sctp_sharedkey_t *skey;
1297 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1299 /* that key doesn't exist */
1302 inp->sctp_ep.default_keyid = keyid;
1307 * deactivates a shared key from the association
1308 * ASSUMES INP_WLOCK is already held
1311 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1313 sctp_sharedkey_t *skey;
1318 /* is the keyid the assoc active sending key */
1319 if (keyid == stcb->asoc.authinfo.active_keyid)
1322 /* does the key exist? */
1323 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1327 /* are there other refcount holders on the key? */
1328 if (skey->refcount == 1) {
1329 /* no other users, send a notification for this key */
1330 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1334 /* mark the key as deactivated */
1335 skey->deactivated = 1;
1341 * deactivates a shared key from the endpoint
1342 * ASSUMES INP_WLOCK is already held
1345 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1347 sctp_sharedkey_t *skey;
1352 /* is the keyid the active sending key on the endpoint */
1353 if (keyid == inp->sctp_ep.default_keyid)
1356 /* does the key exist? */
1357 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1361 /* endpoint keys are not refcounted */
1364 LIST_REMOVE(skey, next);
1365 sctp_free_sharedkey(skey); /* frees skey->key as well */
1371 * get local authentication parameters from cookie (from INIT-ACK)
1374 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1375 uint32_t offset, uint32_t length)
1377 struct sctp_paramhdr *phdr, tmp_param;
1378 uint16_t plen, ptype;
1379 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1380 struct sctp_auth_random *p_random = NULL;
1381 uint16_t random_len = 0;
1382 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1383 struct sctp_auth_hmac_algo *hmacs = NULL;
1384 uint16_t hmacs_len = 0;
1385 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1386 struct sctp_auth_chunk_list *chunks = NULL;
1387 uint16_t num_chunks = 0;
1388 sctp_key_t *new_key;
1391 /* convert to upper bound */
1394 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1395 sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1396 while (phdr != NULL) {
1397 ptype = ntohs(phdr->param_type);
1398 plen = ntohs(phdr->param_length);
1400 if ((plen < sizeof(struct sctp_paramhdr)) ||
1401 (offset + plen > length))
1404 if (ptype == SCTP_RANDOM) {
1405 if (plen > sizeof(random_store))
1407 phdr = sctp_get_next_param(m, offset,
1408 (struct sctp_paramhdr *)random_store, plen);
1411 /* save the random and length for the key */
1412 p_random = (struct sctp_auth_random *)phdr;
1413 random_len = plen - sizeof(*p_random);
1414 } else if (ptype == SCTP_HMAC_LIST) {
1418 if (plen > sizeof(hmacs_store))
1420 phdr = sctp_get_next_param(m, offset,
1421 (struct sctp_paramhdr *)hmacs_store, plen);
1424 /* save the hmacs list and num for the key */
1425 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1426 hmacs_len = plen - sizeof(*hmacs);
1427 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1428 if (stcb->asoc.local_hmacs != NULL)
1429 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1430 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1431 if (stcb->asoc.local_hmacs != NULL) {
1432 for (i = 0; i < num_hmacs; i++) {
1433 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1434 ntohs(hmacs->hmac_ids[i]));
1437 } else if (ptype == SCTP_CHUNK_LIST) {
1440 if (plen > sizeof(chunks_store))
1442 phdr = sctp_get_next_param(m, offset,
1443 (struct sctp_paramhdr *)chunks_store, plen);
1446 chunks = (struct sctp_auth_chunk_list *)phdr;
1447 num_chunks = plen - sizeof(*chunks);
1448 /* save chunks list and num for the key */
1449 if (stcb->asoc.local_auth_chunks != NULL)
1450 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1452 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1453 for (i = 0; i < num_chunks; i++) {
1454 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1455 stcb->asoc.local_auth_chunks);
1458 /* get next parameter */
1459 offset += SCTP_SIZE32(plen);
1460 if (offset + sizeof(struct sctp_paramhdr) > length)
1462 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1463 (uint8_t *)&tmp_param);
1465 /* concatenate the full random key */
1466 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1467 if (chunks != NULL) {
1468 keylen += sizeof(*chunks) + num_chunks;
1470 new_key = sctp_alloc_key(keylen);
1471 if (new_key != NULL) {
1472 /* copy in the RANDOM */
1473 if (p_random != NULL) {
1474 keylen = sizeof(*p_random) + random_len;
1475 memcpy(new_key->key, p_random, keylen);
1479 /* append in the AUTH chunks */
1480 if (chunks != NULL) {
1481 memcpy(new_key->key + keylen, chunks,
1482 sizeof(*chunks) + num_chunks);
1483 keylen += sizeof(*chunks) + num_chunks;
1485 /* append in the HMACs */
1486 if (hmacs != NULL) {
1487 memcpy(new_key->key + keylen, hmacs,
1488 sizeof(*hmacs) + hmacs_len);
1491 if (stcb->asoc.authinfo.random != NULL)
1492 sctp_free_key(stcb->asoc.authinfo.random);
1493 stcb->asoc.authinfo.random = new_key;
1494 stcb->asoc.authinfo.random_len = random_len;
1495 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1496 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1498 /* negotiate what HMAC to use for the peer */
1499 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1500 stcb->asoc.local_hmacs);
1502 /* copy defaults from the endpoint */
1503 /* FIX ME: put in cookie? */
1504 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1505 /* copy out the shared key list (by reference) from the endpoint */
1506 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1507 &stcb->asoc.shared_keys);
1511 * compute and fill in the HMAC digest for a packet
1514 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1515 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1518 sctp_sharedkey_t *skey;
1521 if ((stcb == NULL) || (auth == NULL))
1524 /* zero the digest + chunk padding */
1525 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1526 memset(auth->hmac, 0, SCTP_SIZE32(digestlen));
1528 /* is the desired key cached? */
1529 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1530 (stcb->asoc.authinfo.assoc_key == NULL)) {
1531 if (stcb->asoc.authinfo.assoc_key != NULL) {
1532 /* free the old cached key */
1533 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1535 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1536 /* the only way skey is NULL is if null key id 0 is used */
1541 /* compute a new assoc key and cache it */
1542 stcb->asoc.authinfo.assoc_key =
1543 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1544 stcb->asoc.authinfo.peer_random, key);
1545 stcb->asoc.authinfo.assoc_keyid = keyid;
1546 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1547 stcb->asoc.authinfo.assoc_keyid);
1549 if (SCTP_AUTH_DEBUG)
1550 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1555 /* set in the active key id */
1556 auth->shared_key_id = htons(keyid);
1558 /* compute and fill in the digest */
1559 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1560 m, auth_offset, auth->hmac);
1565 sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1574 /* find the correct starting mbuf and offset (get start position) */
1576 while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1577 m_offset -= SCTP_BUF_LEN(m_tmp);
1578 m_tmp = SCTP_BUF_NEXT(m_tmp);
1580 /* now use the rest of the mbuf chain */
1581 while ((m_tmp != NULL) && (size > 0)) {
1582 data = mtod(m_tmp, uint8_t *)+m_offset;
1583 if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) {
1584 memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset);
1585 size -= SCTP_BUF_LEN(m_tmp) - m_offset;
1587 memset(data, 0, size);
1590 /* clear the offset since it's only for the first mbuf */
1592 m_tmp = SCTP_BUF_NEXT(m_tmp);
1597 * process the incoming Authentication chunk
1599 * -1 on any authentication error
1600 * 0 on authentication verification
1603 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1604 struct mbuf *m, uint32_t offset)
1607 uint16_t shared_key_id;
1609 sctp_sharedkey_t *skey;
1611 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1612 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1614 /* auth is checked for NULL by caller */
1615 chunklen = ntohs(auth->ch.chunk_length);
1616 if (chunklen < sizeof(*auth)) {
1617 SCTP_STAT_INCR(sctps_recvauthfailed);
1620 SCTP_STAT_INCR(sctps_recvauth);
1622 /* get the auth params */
1623 shared_key_id = ntohs(auth->shared_key_id);
1624 hmac_id = ntohs(auth->hmac_id);
1625 SCTPDBG(SCTP_DEBUG_AUTH1,
1626 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1627 shared_key_id, hmac_id);
1629 /* is the indicated HMAC supported? */
1630 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1631 struct mbuf *op_err;
1632 struct sctp_error_auth_invalid_hmac *cause;
1634 SCTP_STAT_INCR(sctps_recvivalhmacid);
1635 SCTPDBG(SCTP_DEBUG_AUTH1,
1636 "SCTP Auth: unsupported HMAC id %u\n",
1639 * report this in an Error Chunk: Unsupported HMAC
1642 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
1643 0, M_NOWAIT, 1, MT_HEADER);
1644 if (op_err != NULL) {
1645 /* pre-reserve some space */
1646 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1647 /* fill in the error */
1648 cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
1649 cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1650 cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
1651 cause->hmac_id = ntohs(hmac_id);
1652 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
1654 sctp_queue_op_err(stcb, op_err);
1658 /* get the indicated shared key, if available */
1659 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1660 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1661 /* find the shared key on the assoc first */
1662 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1664 /* if the shared key isn't found, discard the chunk */
1666 SCTP_STAT_INCR(sctps_recvivalkeyid);
1667 SCTPDBG(SCTP_DEBUG_AUTH1,
1668 "SCTP Auth: unknown key id %u\n",
1672 /* generate a notification if this is a new key id */
1673 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1675 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1676 * shared_key_id, (void
1677 * *)stcb->asoc.authinfo.recv_keyid);
1679 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1680 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1681 SCTP_SO_NOT_LOCKED);
1682 /* compute a new recv assoc key and cache it */
1683 if (stcb->asoc.authinfo.recv_key != NULL)
1684 sctp_free_key(stcb->asoc.authinfo.recv_key);
1685 stcb->asoc.authinfo.recv_key =
1686 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1687 stcb->asoc.authinfo.peer_random, skey->key);
1688 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1690 if (SCTP_AUTH_DEBUG)
1691 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1694 /* validate the digest length */
1695 digestlen = sctp_get_hmac_digest_len(hmac_id);
1696 if (chunklen < (sizeof(*auth) + digestlen)) {
1697 /* invalid digest length */
1698 SCTP_STAT_INCR(sctps_recvauthfailed);
1699 SCTPDBG(SCTP_DEBUG_AUTH1,
1700 "SCTP Auth: chunk too short for HMAC\n");
1703 /* save a copy of the digest, zero the pseudo header, and validate */
1704 memcpy(digest, auth->hmac, digestlen);
1705 sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1706 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1707 m, offset, computed_digest);
1709 /* compare the computed digest with the one in the AUTH chunk */
1710 if (timingsafe_bcmp(digest, computed_digest, digestlen) != 0) {
1711 SCTP_STAT_INCR(sctps_recvauthfailed);
1712 SCTPDBG(SCTP_DEBUG_AUTH1,
1713 "SCTP Auth: HMAC digest check failed\n");
1720 * Generate NOTIFICATION
1723 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1724 uint16_t keyid, uint16_t alt_keyid, int so_locked
1725 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1730 struct mbuf *m_notify;
1731 struct sctp_authkey_event *auth;
1732 struct sctp_queued_to_read *control;
1734 if ((stcb == NULL) ||
1735 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1736 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1737 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1739 /* If the socket is gone we are out of here */
1743 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1744 /* event not enabled */
1747 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1748 0, M_NOWAIT, 1, MT_HEADER);
1749 if (m_notify == NULL)
1753 SCTP_BUF_LEN(m_notify) = 0;
1754 auth = mtod(m_notify, struct sctp_authkey_event *);
1755 memset(auth, 0, sizeof(struct sctp_authkey_event));
1756 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1757 auth->auth_flags = 0;
1758 auth->auth_length = sizeof(*auth);
1759 auth->auth_keynumber = keyid;
1760 auth->auth_altkeynumber = alt_keyid;
1761 auth->auth_indication = indication;
1762 auth->auth_assoc_id = sctp_get_associd(stcb);
1764 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1765 SCTP_BUF_NEXT(m_notify) = NULL;
1767 /* append to socket */
1768 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1769 0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1770 if (control == NULL) {
1772 sctp_m_freem(m_notify);
1775 control->length = SCTP_BUF_LEN(m_notify);
1776 control->spec_flags = M_NOTIFICATION;
1777 /* not that we need this */
1778 control->tail_mbuf = m_notify;
1779 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1780 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1785 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1786 * Note: currently only used for INIT as INIT-ACK is handled inline
1787 * with sctp_load_addresses_from_init()
1790 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1792 struct sctp_paramhdr *phdr, param_buf;
1793 uint16_t ptype, plen;
1794 int peer_supports_asconf = 0;
1795 int peer_supports_auth = 0;
1796 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1797 uint8_t saw_asconf = 0;
1798 uint8_t saw_asconf_ack = 0;
1800 /* go through each of the params. */
1801 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf));
1803 ptype = ntohs(phdr->param_type);
1804 plen = ntohs(phdr->param_length);
1806 if (offset + plen > limit) {
1809 if (plen < sizeof(struct sctp_paramhdr)) {
1812 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1813 /* A supported extension chunk */
1814 struct sctp_supported_chunk_types_param *pr_supported;
1815 uint8_t local_store[SCTP_SMALL_CHUNK_STORE];
1818 if (plen > sizeof(local_store)) {
1821 phdr = sctp_get_next_param(m, offset,
1822 (struct sctp_paramhdr *)&local_store,
1827 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1828 num_ent = plen - sizeof(struct sctp_paramhdr);
1829 for (i = 0; i < num_ent; i++) {
1830 switch (pr_supported->chunk_types[i]) {
1832 case SCTP_ASCONF_ACK:
1833 peer_supports_asconf = 1;
1836 /* one we don't care about */
1840 } else if (ptype == SCTP_RANDOM) {
1841 /* enforce the random length */
1842 if (plen != (sizeof(struct sctp_auth_random) +
1843 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1844 SCTPDBG(SCTP_DEBUG_AUTH1,
1845 "SCTP: invalid RANDOM len\n");
1849 } else if (ptype == SCTP_HMAC_LIST) {
1850 struct sctp_auth_hmac_algo *hmacs;
1851 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1854 if (plen > sizeof(store)) {
1857 phdr = sctp_get_next_param(m, offset,
1858 (struct sctp_paramhdr *)store,
1863 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1864 num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]);
1865 /* validate the hmac list */
1866 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1867 SCTPDBG(SCTP_DEBUG_AUTH1,
1868 "SCTP: invalid HMAC param\n");
1872 } else if (ptype == SCTP_CHUNK_LIST) {
1873 struct sctp_auth_chunk_list *chunks;
1874 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1877 if (plen > sizeof(chunks_store)) {
1880 phdr = sctp_get_next_param(m, offset,
1881 (struct sctp_paramhdr *)chunks_store,
1887 * Flip through the list and mark that the
1888 * peer supports asconf/asconf_ack.
1890 chunks = (struct sctp_auth_chunk_list *)phdr;
1891 num_chunks = plen - sizeof(*chunks);
1892 for (i = 0; i < num_chunks; i++) {
1893 /* record asconf/asconf-ack if listed */
1894 if (chunks->chunk_types[i] == SCTP_ASCONF)
1896 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1904 offset += SCTP_SIZE32(plen);
1905 if (offset >= limit) {
1908 phdr = sctp_get_next_param(m, offset, ¶m_buf,
1911 /* validate authentication required parameters */
1912 if (got_random && got_hmacs) {
1913 peer_supports_auth = 1;
1915 peer_supports_auth = 0;
1917 if (!peer_supports_auth && got_chklist) {
1918 SCTPDBG(SCTP_DEBUG_AUTH1,
1919 "SCTP: peer sent chunk list w/o AUTH\n");
1922 if (peer_supports_asconf && !peer_supports_auth) {
1923 SCTPDBG(SCTP_DEBUG_AUTH1,
1924 "SCTP: peer supports ASCONF but not AUTH\n");
1926 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1927 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1934 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1936 uint16_t chunks_len = 0;
1937 uint16_t hmacs_len = 0;
1938 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1939 sctp_key_t *new_key;
1942 /* initialize hmac list from endpoint */
1943 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1944 if (stcb->asoc.local_hmacs != NULL) {
1945 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1946 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1948 /* initialize auth chunks list from endpoint */
1949 stcb->asoc.local_auth_chunks =
1950 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1951 if (stcb->asoc.local_auth_chunks != NULL) {
1954 for (i = 0; i < 256; i++) {
1955 if (stcb->asoc.local_auth_chunks->chunks[i])
1959 /* copy defaults from the endpoint */
1960 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
1962 /* copy out the shared key list (by reference) from the endpoint */
1963 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
1964 &stcb->asoc.shared_keys);
1966 /* now set the concatenated key (random + chunks + hmacs) */
1967 /* key includes parameter headers */
1968 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1970 new_key = sctp_alloc_key(keylen);
1971 if (new_key != NULL) {
1972 struct sctp_paramhdr *ph;
1975 /* generate and copy in the RANDOM */
1976 ph = (struct sctp_paramhdr *)new_key->key;
1977 ph->param_type = htons(SCTP_RANDOM);
1978 plen = sizeof(*ph) + random_len;
1979 ph->param_length = htons(plen);
1980 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
1983 /* append in the AUTH chunks */
1984 /* NOTE: currently we always have chunks to list */
1985 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1986 ph->param_type = htons(SCTP_CHUNK_LIST);
1987 plen = sizeof(*ph) + chunks_len;
1988 ph->param_length = htons(plen);
1989 keylen += sizeof(*ph);
1990 if (stcb->asoc.local_auth_chunks) {
1993 for (i = 0; i < 256; i++) {
1994 if (stcb->asoc.local_auth_chunks->chunks[i])
1995 new_key->key[keylen++] = i;
1999 /* append in the HMACs */
2000 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2001 ph->param_type = htons(SCTP_HMAC_LIST);
2002 plen = sizeof(*ph) + hmacs_len;
2003 ph->param_length = htons(plen);
2004 keylen += sizeof(*ph);
2005 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2006 new_key->key + keylen);
2008 if (stcb->asoc.authinfo.random != NULL)
2009 sctp_free_key(stcb->asoc.authinfo.random);
2010 stcb->asoc.authinfo.random = new_key;
2011 stcb->asoc.authinfo.random_len = random_len;