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 #if defined(__FreeBSD__) && !defined(__Userspace__)
36 #include <sys/cdefs.h>
37 __FBSDID("$FreeBSD: head/sys/netinet/sctp_auth.c 362054 2020-06-11 13:34:09Z tuexen $");
40 #include <netinet/sctp_os.h>
41 #include <netinet/sctp.h>
42 #include <netinet/sctp_header.h>
43 #include <netinet/sctp_pcb.h>
44 #include <netinet/sctp_var.h>
45 #include <netinet/sctp_sysctl.h>
46 #include <netinet/sctputil.h>
47 #include <netinet/sctp_indata.h>
48 #include <netinet/sctp_output.h>
49 #include <netinet/sctp_auth.h>
52 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
53 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
54 #endif /* SCTP_DEBUG */
58 sctp_clear_chunklist(sctp_auth_chklist_t *chklist)
60 memset(chklist, 0, sizeof(*chklist));
61 /* chklist->num_chunks = 0; */
65 sctp_alloc_chunklist(void)
67 sctp_auth_chklist_t *chklist;
69 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
71 if (chklist == NULL) {
72 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
74 sctp_clear_chunklist(chklist);
80 sctp_free_chunklist(sctp_auth_chklist_t *list)
83 SCTP_FREE(list, SCTP_M_AUTH_CL);
87 sctp_copy_chunklist(sctp_auth_chklist_t *list)
89 sctp_auth_chklist_t *new_list;
95 new_list = sctp_alloc_chunklist();
99 memcpy(new_list, list, sizeof(*new_list));
106 * add a chunk to the required chunks list
109 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
114 /* is chunk restricted? */
115 if ((chunk == SCTP_INITIATION) ||
116 (chunk == SCTP_INITIATION_ACK) ||
117 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
118 (chunk == SCTP_AUTHENTICATION)) {
121 if (list->chunks[chunk] == 0) {
122 list->chunks[chunk] = 1;
124 SCTPDBG(SCTP_DEBUG_AUTH1,
125 "SCTP: added chunk %u (0x%02x) to Auth list\n",
132 * delete a chunk from the required chunks list
135 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
140 if (list->chunks[chunk] == 1) {
141 list->chunks[chunk] = 0;
143 SCTPDBG(SCTP_DEBUG_AUTH1,
144 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
151 sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list)
156 return (list->num_chunks);
160 * return the current number and list of required chunks caller must
161 * guarantee ptr has space for up to 256 bytes
164 sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
171 for (i = 0; i < 256; i++) {
172 if (list->chunks[i] != 0) {
181 sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
188 if (list->num_chunks <= 32) {
189 /* just list them, one byte each */
190 for (i = 0; i < 256; i++) {
191 if (list->chunks[i] != 0) {
199 /* pack into a 32 byte bitfield */
200 for (i = 0; i < 256; i++) {
201 if (list->chunks[i] != 0) {
204 ptr[index] |= (1 << offset);
213 sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks,
214 sctp_auth_chklist_t *list)
222 if (num_chunks <= 32) {
223 /* just pull them, one byte each */
224 for (i = 0; i < num_chunks; i++) {
225 (void)sctp_auth_add_chunk(*ptr++, list);
231 /* unpack from a 32 byte bitfield */
232 for (index = 0; index < 32; index++) {
233 for (offset = 0; offset < 8; offset++) {
234 if (ptr[index] & (1 << offset)) {
235 (void)sctp_auth_add_chunk((index * 8) + offset, list);
246 * allocate structure space for a key of length keylen
249 sctp_alloc_key(uint32_t keylen)
253 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
255 if (new_key == NULL) {
259 new_key->keylen = keylen;
264 sctp_free_key(sctp_key_t *key)
267 SCTP_FREE(key,SCTP_M_AUTH_KY);
271 sctp_print_key(sctp_key_t *key, const char *str)
276 SCTP_PRINTF("%s: [Null key]\n", str);
279 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
281 for (i = 0; i < key->keylen; i++)
282 SCTP_PRINTF("%02x", key->key[i]);
285 SCTP_PRINTF("[Null key]\n");
290 sctp_show_key(sctp_key_t *key, const char *str)
295 SCTP_PRINTF("%s: [Null key]\n", str);
298 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
300 for (i = 0; i < key->keylen; i++)
301 SCTP_PRINTF("%02x", key->key[i]);
304 SCTP_PRINTF("[Null key]\n");
309 sctp_get_keylen(sctp_key_t *key)
312 return (key->keylen);
318 * generate a new random key of length 'keylen'
321 sctp_generate_random_key(uint32_t keylen)
325 new_key = sctp_alloc_key(keylen);
326 if (new_key == NULL) {
330 SCTP_READ_RANDOM(new_key->key, keylen);
331 new_key->keylen = keylen;
336 sctp_set_key(uint8_t *key, uint32_t keylen)
340 new_key = sctp_alloc_key(keylen);
341 if (new_key == NULL) {
345 memcpy(new_key->key, key, keylen);
350 * given two keys of variable size, compute which key is "larger/smaller"
351 * returns: 1 if key1 > key2
356 sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2)
360 uint32_t key1len, key2len;
361 uint8_t *key_1, *key_2;
364 /* sanity/length check */
365 key1len = sctp_get_keylen(key1);
366 key2len = sctp_get_keylen(key2);
367 if ((key1len == 0) && (key2len == 0))
369 else if (key1len == 0)
371 else if (key2len == 0)
374 if (key1len < key2len) {
381 /* check for numeric equality */
382 for (i = 0; i < maxlen; i++) {
383 /* left-pad with zeros */
384 val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++);
385 val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++);
388 } else if (val1 < val2) {
392 /* keys are equal value, so check lengths */
393 if (key1len == key2len)
395 else if (key1len < key2len)
402 * generate the concatenated keying material based on the two keys and the
403 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
404 * order for concatenation
407 sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared)
413 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
414 sctp_get_keylen(shared);
417 /* get space for the new key */
418 new_key = sctp_alloc_key(keylen);
419 if (new_key == NULL) {
423 new_key->keylen = keylen;
424 key_ptr = new_key->key;
426 /* all keys empty/null?! */
430 /* concatenate the keys */
431 if (sctp_compare_key(key1, key2) <= 0) {
432 /* key is shared + key1 + key2 */
433 if (sctp_get_keylen(shared)) {
434 memcpy(key_ptr, shared->key, shared->keylen);
435 key_ptr += shared->keylen;
437 if (sctp_get_keylen(key1)) {
438 memcpy(key_ptr, key1->key, key1->keylen);
439 key_ptr += key1->keylen;
441 if (sctp_get_keylen(key2)) {
442 memcpy(key_ptr, key2->key, key2->keylen);
445 /* key is shared + key2 + key1 */
446 if (sctp_get_keylen(shared)) {
447 memcpy(key_ptr, shared->key, shared->keylen);
448 key_ptr += shared->keylen;
450 if (sctp_get_keylen(key2)) {
451 memcpy(key_ptr, key2->key, key2->keylen);
452 key_ptr += key2->keylen;
454 if (sctp_get_keylen(key1)) {
455 memcpy(key_ptr, key1->key, key1->keylen);
463 sctp_alloc_sharedkey(void)
465 sctp_sharedkey_t *new_key;
467 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
469 if (new_key == NULL) {
475 new_key->refcount = 1;
476 new_key->deactivated = 0;
481 sctp_free_sharedkey(sctp_sharedkey_t *skey)
486 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
487 if (skey->key != NULL)
488 sctp_free_key(skey->key);
489 SCTP_FREE(skey, SCTP_M_AUTH_KY);
494 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
496 sctp_sharedkey_t *skey;
498 LIST_FOREACH(skey, shared_keys, next) {
499 if (skey->keyid == key_id)
506 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
507 sctp_sharedkey_t *new_skey)
509 sctp_sharedkey_t *skey;
511 if ((shared_keys == NULL) || (new_skey == NULL))
514 /* insert into an empty list? */
515 if (LIST_EMPTY(shared_keys)) {
516 LIST_INSERT_HEAD(shared_keys, new_skey, next);
519 /* insert into the existing list, ordered by key id */
520 LIST_FOREACH(skey, shared_keys, next) {
521 if (new_skey->keyid < skey->keyid) {
522 /* insert it before here */
523 LIST_INSERT_BEFORE(skey, new_skey, next);
525 } else if (new_skey->keyid == skey->keyid) {
526 /* replace the existing key */
527 /* verify this key *can* be replaced */
528 if ((skey->deactivated) || (skey->refcount > 1)) {
529 SCTPDBG(SCTP_DEBUG_AUTH1,
530 "can't replace shared key id %u\n",
534 SCTPDBG(SCTP_DEBUG_AUTH1,
535 "replacing shared key id %u\n",
537 LIST_INSERT_BEFORE(skey, new_skey, next);
538 LIST_REMOVE(skey, next);
539 sctp_free_sharedkey(skey);
542 if (LIST_NEXT(skey, next) == NULL) {
543 /* belongs at the end of the list */
544 LIST_INSERT_AFTER(skey, new_skey, next);
548 /* shouldn't reach here */
553 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
555 sctp_sharedkey_t *skey;
557 /* find the shared key */
558 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
560 /* bump the ref count */
562 atomic_add_int(&skey->refcount, 1);
563 SCTPDBG(SCTP_DEBUG_AUTH2,
564 "%s: stcb %p key %u refcount acquire to %d\n",
565 __func__, (void *)stcb, key_id, skey->refcount);
570 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked)
572 sctp_sharedkey_t *skey;
574 /* find the shared key */
575 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
577 /* decrement the ref count */
579 SCTPDBG(SCTP_DEBUG_AUTH2,
580 "%s: stcb %p key %u refcount release to %d\n",
581 __func__, (void *)stcb, key_id, skey->refcount);
583 /* see if a notification should be generated */
584 if ((skey->refcount <= 2) && (skey->deactivated)) {
585 /* notify ULP that key is no longer used */
586 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
587 key_id, 0, so_locked);
588 SCTPDBG(SCTP_DEBUG_AUTH2,
589 "%s: stcb %p key %u no longer used, %d\n",
590 __func__, (void *)stcb, key_id, skey->refcount);
592 sctp_free_sharedkey(skey);
596 static sctp_sharedkey_t *
597 sctp_copy_sharedkey(const sctp_sharedkey_t *skey)
599 sctp_sharedkey_t *new_skey;
603 new_skey = sctp_alloc_sharedkey();
604 if (new_skey == NULL)
606 if (skey->key != NULL)
607 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
609 new_skey->key = NULL;
610 new_skey->keyid = skey->keyid;
615 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
617 sctp_sharedkey_t *skey, *new_skey;
620 if ((src == NULL) || (dest == NULL))
622 LIST_FOREACH(skey, src, next) {
623 new_skey = sctp_copy_sharedkey(skey);
624 if (new_skey != NULL) {
625 if (sctp_insert_sharedkey(dest, new_skey)) {
626 sctp_free_sharedkey(new_skey);
637 sctp_alloc_hmaclist(uint16_t num_hmacs)
639 sctp_hmaclist_t *new_list;
642 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
643 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
645 if (new_list == NULL) {
649 new_list->max_algo = num_hmacs;
650 new_list->num_algo = 0;
655 sctp_free_hmaclist(sctp_hmaclist_t *list)
658 SCTP_FREE(list,SCTP_M_AUTH_HL);
663 sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id)
668 if (list->num_algo == list->max_algo) {
669 SCTPDBG(SCTP_DEBUG_AUTH1,
670 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
673 #if defined(SCTP_SUPPORT_HMAC_SHA256)
674 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
675 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
677 if (hmac_id != SCTP_AUTH_HMAC_ID_SHA1) {
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 #if defined(SCTP_SUPPORT_HMAC_SHA256)
719 new_list = sctp_alloc_hmaclist(2);
721 new_list = sctp_alloc_hmaclist(1);
723 if (new_list == NULL)
725 #if defined(SCTP_SUPPORT_HMAC_SHA256)
726 /* We prefer SHA256, so list it first */
727 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
729 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
734 * HMAC algos are listed in priority/preference order
735 * find the best HMAC id to use for the peer based on local support
738 sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local)
742 if ((local == NULL) || (peer == NULL))
743 return (SCTP_AUTH_HMAC_ID_RSVD);
745 for (i = 0; i < peer->num_algo; i++) {
746 for (j = 0; j < local->num_algo; j++) {
747 if (peer->hmac[i] == local->hmac[j]) {
748 /* found the "best" one */
749 SCTPDBG(SCTP_DEBUG_AUTH1,
750 "SCTP: negotiated peer HMAC id %u\n",
752 return (peer->hmac[i]);
756 /* didn't find one! */
757 return (SCTP_AUTH_HMAC_ID_RSVD);
761 * serialize the HMAC algo list and return space used
762 * caller must guarantee ptr has appropriate space
765 sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr)
773 for (i = 0; i < list->num_algo; i++) {
774 hmac_id = htons(list->hmac[i]);
775 memcpy(ptr, &hmac_id, sizeof(hmac_id));
776 ptr += sizeof(hmac_id);
778 return (list->num_algo * sizeof(hmac_id));
782 sctp_verify_hmac_param (struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
786 for (i = 0; i < num_hmacs; i++) {
787 if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
795 sctp_alloc_authinfo(void)
797 sctp_authinfo_t *new_authinfo;
799 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
802 if (new_authinfo == NULL) {
806 memset(new_authinfo, 0, sizeof(*new_authinfo));
807 return (new_authinfo);
811 sctp_free_authinfo(sctp_authinfo_t *authinfo)
813 if (authinfo == NULL)
816 if (authinfo->random != NULL)
817 sctp_free_key(authinfo->random);
818 if (authinfo->peer_random != NULL)
819 sctp_free_key(authinfo->peer_random);
820 if (authinfo->assoc_key != NULL)
821 sctp_free_key(authinfo->assoc_key);
822 if (authinfo->recv_key != NULL)
823 sctp_free_key(authinfo->recv_key);
825 /* We are NOT dynamically allocating authinfo's right now... */
826 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
831 sctp_get_auth_chunk_len(uint16_t hmac_algo)
835 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
836 return (SCTP_SIZE32(size));
840 sctp_get_hmac_digest_len(uint16_t hmac_algo)
843 case SCTP_AUTH_HMAC_ID_SHA1:
844 return (SCTP_AUTH_DIGEST_LEN_SHA1);
845 #if defined(SCTP_SUPPORT_HMAC_SHA256)
846 case SCTP_AUTH_HMAC_ID_SHA256:
847 return (SCTP_AUTH_DIGEST_LEN_SHA256);
850 /* unknown HMAC algorithm: can't do anything */
856 sctp_get_hmac_block_len(uint16_t hmac_algo)
859 case SCTP_AUTH_HMAC_ID_SHA1:
861 #if defined(SCTP_SUPPORT_HMAC_SHA256)
862 case SCTP_AUTH_HMAC_ID_SHA256:
865 case SCTP_AUTH_HMAC_ID_RSVD:
867 /* unknown HMAC algorithm: can't do anything */
872 #if defined(__Userspace__)
873 /* __Userspace__ SHA1_Init is defined in libcrypto.a (libssl-dev on Ubuntu) */
876 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx)
879 case SCTP_AUTH_HMAC_ID_SHA1:
880 SCTP_SHA1_INIT(&ctx->sha1);
882 #if defined(SCTP_SUPPORT_HMAC_SHA256)
883 case SCTP_AUTH_HMAC_ID_SHA256:
884 SCTP_SHA256_INIT(&ctx->sha256);
887 case SCTP_AUTH_HMAC_ID_RSVD:
889 /* unknown HMAC algorithm: can't do anything */
895 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx,
896 uint8_t *text, uint32_t textlen)
899 case SCTP_AUTH_HMAC_ID_SHA1:
900 SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
902 #if defined(SCTP_SUPPORT_HMAC_SHA256)
903 case SCTP_AUTH_HMAC_ID_SHA256:
904 SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
907 case SCTP_AUTH_HMAC_ID_RSVD:
909 /* unknown HMAC algorithm: can't do anything */
915 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx,
919 case SCTP_AUTH_HMAC_ID_SHA1:
920 SCTP_SHA1_FINAL(digest, &ctx->sha1);
922 #if defined(SCTP_SUPPORT_HMAC_SHA256)
923 case SCTP_AUTH_HMAC_ID_SHA256:
924 SCTP_SHA256_FINAL(digest, &ctx->sha256);
927 case SCTP_AUTH_HMAC_ID_RSVD:
929 /* unknown HMAC algorithm: can't do anything */
935 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
937 * Compute the HMAC digest using the desired hash key, text, and HMAC
938 * algorithm. Resulting digest is placed in 'digest' and digest length
939 * is returned, if the HMAC was performed.
941 * WARNING: it is up to the caller to supply sufficient space to hold the
945 sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
946 uint8_t *text, uint32_t textlen, uint8_t *digest)
950 sctp_hash_context_t ctx;
951 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
952 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
955 /* sanity check the material and length */
956 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
957 (textlen == 0) || (digest == NULL)) {
958 /* can't do HMAC with empty key or text or digest store */
961 /* validate the hmac algo and get the digest length */
962 digestlen = sctp_get_hmac_digest_len(hmac_algo);
966 /* hash the key if it is longer than the hash block size */
967 blocklen = sctp_get_hmac_block_len(hmac_algo);
968 if (keylen > blocklen) {
969 sctp_hmac_init(hmac_algo, &ctx);
970 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
971 sctp_hmac_final(hmac_algo, &ctx, temp);
972 /* set the hashed key as the key */
976 /* initialize the inner/outer pads with the key and "append" zeroes */
977 memset(ipad, 0, blocklen);
978 memset(opad, 0, blocklen);
979 memcpy(ipad, key, keylen);
980 memcpy(opad, key, keylen);
982 /* XOR the key with ipad and opad values */
983 for (i = 0; i < blocklen; i++) {
988 /* perform inner hash */
989 sctp_hmac_init(hmac_algo, &ctx);
990 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
991 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
992 sctp_hmac_final(hmac_algo, &ctx, temp);
994 /* perform outer hash */
995 sctp_hmac_init(hmac_algo, &ctx);
996 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
997 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
998 sctp_hmac_final(hmac_algo, &ctx, digest);
1005 sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
1006 struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer)
1010 sctp_hash_context_t ctx;
1011 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1012 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1016 /* sanity check the material and length */
1017 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1018 /* can't do HMAC with empty key or text or digest store */
1021 /* validate the hmac algo and get the digest length */
1022 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1026 /* hash the key if it is longer than the hash block size */
1027 blocklen = sctp_get_hmac_block_len(hmac_algo);
1028 if (keylen > blocklen) {
1029 sctp_hmac_init(hmac_algo, &ctx);
1030 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1031 sctp_hmac_final(hmac_algo, &ctx, temp);
1032 /* set the hashed key as the key */
1036 /* initialize the inner/outer pads with the key and "append" zeroes */
1037 memset(ipad, 0, blocklen);
1038 memset(opad, 0, blocklen);
1039 memcpy(ipad, key, keylen);
1040 memcpy(opad, key, keylen);
1042 /* XOR the key with ipad and opad values */
1043 for (i = 0; i < blocklen; i++) {
1048 /* perform inner hash */
1049 sctp_hmac_init(hmac_algo, &ctx);
1050 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1051 /* find the correct starting mbuf and offset (get start of text) */
1053 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1054 m_offset -= SCTP_BUF_LEN(m_tmp);
1055 m_tmp = SCTP_BUF_NEXT(m_tmp);
1057 /* now use the rest of the mbuf chain for the text */
1058 while (m_tmp != NULL) {
1059 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1060 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1061 SCTP_BUF_LEN(m_tmp) - (trailer+m_offset));
1063 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1064 SCTP_BUF_LEN(m_tmp) - m_offset);
1067 /* clear the offset since it's only for the first mbuf */
1069 m_tmp = SCTP_BUF_NEXT(m_tmp);
1071 sctp_hmac_final(hmac_algo, &ctx, temp);
1073 /* perform outer hash */
1074 sctp_hmac_init(hmac_algo, &ctx);
1075 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1076 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1077 sctp_hmac_final(hmac_algo, &ctx, digest);
1083 * computes the requested HMAC using a key struct (which may be modified if
1084 * the keylen exceeds the HMAC block len).
1087 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1088 uint32_t textlen, uint8_t *digest)
1092 sctp_hash_context_t ctx;
1093 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1096 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1098 /* can't do HMAC with empty key or text or digest store */
1101 /* validate the hmac algo and get the digest length */
1102 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1106 /* hash the key if it is longer than the hash block size */
1107 blocklen = sctp_get_hmac_block_len(hmac_algo);
1108 if (key->keylen > blocklen) {
1109 sctp_hmac_init(hmac_algo, &ctx);
1110 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1111 sctp_hmac_final(hmac_algo, &ctx, temp);
1112 /* save the hashed key as the new key */
1113 key->keylen = digestlen;
1114 memcpy(key->key, temp, key->keylen);
1116 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1122 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1123 uint32_t m_offset, uint8_t *digest)
1127 sctp_hash_context_t ctx;
1128 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1131 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1132 /* can't do HMAC with empty key or text or digest store */
1135 /* validate the hmac algo and get the digest length */
1136 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1140 /* hash the key if it is longer than the hash block size */
1141 blocklen = sctp_get_hmac_block_len(hmac_algo);
1142 if (key->keylen > blocklen) {
1143 sctp_hmac_init(hmac_algo, &ctx);
1144 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1145 sctp_hmac_final(hmac_algo, &ctx, temp);
1146 /* save the hashed key as the new key */
1147 key->keylen = digestlen;
1148 memcpy(key->key, temp, key->keylen);
1150 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1154 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1158 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1161 for (i = 0; i < list->num_algo; i++)
1162 if (list->hmac[i] == id)
1165 /* not in the list */
1171 * clear any cached key(s) if they match the given key id on an association.
1172 * the cached key(s) will be recomputed and re-cached at next use.
1173 * ASSUMES TCB_LOCK is already held
1176 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1181 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1182 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1183 stcb->asoc.authinfo.assoc_key = NULL;
1185 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1186 sctp_free_key(stcb->asoc.authinfo.recv_key);
1187 stcb->asoc.authinfo.recv_key = NULL;
1192 * clear any cached key(s) if they match the given key id for all assocs on
1194 * ASSUMES INP_WLOCK is already held
1197 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1199 struct sctp_tcb *stcb;
1204 /* clear the cached keys on all assocs on this instance */
1205 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1206 SCTP_TCB_LOCK(stcb);
1207 sctp_clear_cachedkeys(stcb, keyid);
1208 SCTP_TCB_UNLOCK(stcb);
1213 * delete a shared key from an association
1214 * ASSUMES TCB_LOCK is already held
1217 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1219 sctp_sharedkey_t *skey;
1224 /* is the keyid the assoc active sending key */
1225 if (keyid == stcb->asoc.authinfo.active_keyid)
1228 /* does the key exist? */
1229 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1233 /* are there other refcount holders on the key? */
1234 if (skey->refcount > 1)
1238 LIST_REMOVE(skey, next);
1239 sctp_free_sharedkey(skey); /* frees skey->key as well */
1241 /* clear any cached keys */
1242 sctp_clear_cachedkeys(stcb, keyid);
1247 * deletes a shared key from the endpoint
1248 * ASSUMES INP_WLOCK is already held
1251 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1253 sctp_sharedkey_t *skey;
1258 /* is the keyid the active sending key on the endpoint */
1259 if (keyid == inp->sctp_ep.default_keyid)
1262 /* does the key exist? */
1263 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1267 /* endpoint keys are not refcounted */
1270 LIST_REMOVE(skey, next);
1271 sctp_free_sharedkey(skey); /* frees skey->key as well */
1273 /* clear any cached keys */
1274 sctp_clear_cachedkeys_ep(inp, keyid);
1279 * set the active key on an association
1280 * ASSUMES TCB_LOCK is already held
1283 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1285 sctp_sharedkey_t *skey = NULL;
1287 /* find the key on the assoc */
1288 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1290 /* that key doesn't exist */
1293 if ((skey->deactivated) && (skey->refcount > 1)) {
1294 /* can't reactivate a deactivated key with other refcounts */
1298 /* set the (new) active key */
1299 stcb->asoc.authinfo.active_keyid = keyid;
1300 /* reset the deactivated flag */
1301 skey->deactivated = 0;
1307 * set the active key on an endpoint
1308 * ASSUMES INP_WLOCK is already held
1311 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1313 sctp_sharedkey_t *skey;
1316 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1318 /* that key doesn't exist */
1321 inp->sctp_ep.default_keyid = keyid;
1326 * deactivates a shared key from the association
1327 * ASSUMES INP_WLOCK is already held
1330 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1332 sctp_sharedkey_t *skey;
1337 /* is the keyid the assoc active sending key */
1338 if (keyid == stcb->asoc.authinfo.active_keyid)
1341 /* does the key exist? */
1342 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1346 /* are there other refcount holders on the key? */
1347 if (skey->refcount == 1) {
1348 /* no other users, send a notification for this key */
1349 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1353 /* mark the key as deactivated */
1354 skey->deactivated = 1;
1360 * deactivates a shared key from the endpoint
1361 * ASSUMES INP_WLOCK is already held
1364 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1366 sctp_sharedkey_t *skey;
1371 /* is the keyid the active sending key on the endpoint */
1372 if (keyid == inp->sctp_ep.default_keyid)
1375 /* does the key exist? */
1376 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1380 /* endpoint keys are not refcounted */
1383 LIST_REMOVE(skey, next);
1384 sctp_free_sharedkey(skey); /* frees skey->key as well */
1390 * get local authentication parameters from cookie (from INIT-ACK)
1393 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1394 uint32_t offset, uint32_t length)
1396 struct sctp_paramhdr *phdr, tmp_param;
1397 uint16_t plen, ptype;
1398 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1399 struct sctp_auth_random *p_random = NULL;
1400 uint16_t random_len = 0;
1401 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1402 struct sctp_auth_hmac_algo *hmacs = NULL;
1403 uint16_t hmacs_len = 0;
1404 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1405 struct sctp_auth_chunk_list *chunks = NULL;
1406 uint16_t num_chunks = 0;
1407 sctp_key_t *new_key;
1410 /* convert to upper bound */
1413 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1414 sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1415 while (phdr != NULL) {
1416 ptype = ntohs(phdr->param_type);
1417 plen = ntohs(phdr->param_length);
1419 if ((plen < sizeof(struct sctp_paramhdr)) ||
1420 (offset + plen > length))
1423 if (ptype == SCTP_RANDOM) {
1424 if (plen > sizeof(random_store))
1426 phdr = sctp_get_next_param(m, offset,
1427 (struct sctp_paramhdr *)random_store, plen);
1430 /* save the random and length for the key */
1431 p_random = (struct sctp_auth_random *)phdr;
1432 random_len = plen - sizeof(*p_random);
1433 } else if (ptype == SCTP_HMAC_LIST) {
1437 if (plen > sizeof(hmacs_store))
1439 phdr = sctp_get_next_param(m, offset,
1440 (struct sctp_paramhdr *)hmacs_store, plen);
1443 /* save the hmacs list and num for the key */
1444 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1445 hmacs_len = plen - sizeof(*hmacs);
1446 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1447 if (stcb->asoc.local_hmacs != NULL)
1448 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1449 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1450 if (stcb->asoc.local_hmacs != NULL) {
1451 for (i = 0; i < num_hmacs; i++) {
1452 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1453 ntohs(hmacs->hmac_ids[i]));
1456 } else if (ptype == SCTP_CHUNK_LIST) {
1459 if (plen > sizeof(chunks_store))
1461 phdr = sctp_get_next_param(m, offset,
1462 (struct sctp_paramhdr *)chunks_store, plen);
1465 chunks = (struct sctp_auth_chunk_list *)phdr;
1466 num_chunks = plen - sizeof(*chunks);
1467 /* save chunks list and num for the key */
1468 if (stcb->asoc.local_auth_chunks != NULL)
1469 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1471 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1472 for (i = 0; i < num_chunks; i++) {
1473 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1474 stcb->asoc.local_auth_chunks);
1477 /* get next parameter */
1478 offset += SCTP_SIZE32(plen);
1479 if (offset + sizeof(struct sctp_paramhdr) > length)
1481 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1482 (uint8_t *)&tmp_param);
1484 /* concatenate the full random key */
1485 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1486 if (chunks != NULL) {
1487 keylen += sizeof(*chunks) + num_chunks;
1489 new_key = sctp_alloc_key(keylen);
1490 if (new_key != NULL) {
1491 /* copy in the RANDOM */
1492 if (p_random != NULL) {
1493 keylen = sizeof(*p_random) + random_len;
1494 memcpy(new_key->key, p_random, keylen);
1498 /* append in the AUTH chunks */
1499 if (chunks != NULL) {
1500 memcpy(new_key->key + keylen, chunks,
1501 sizeof(*chunks) + num_chunks);
1502 keylen += sizeof(*chunks) + num_chunks;
1504 /* append in the HMACs */
1505 if (hmacs != NULL) {
1506 memcpy(new_key->key + keylen, hmacs,
1507 sizeof(*hmacs) + hmacs_len);
1510 if (stcb->asoc.authinfo.random != NULL)
1511 sctp_free_key(stcb->asoc.authinfo.random);
1512 stcb->asoc.authinfo.random = new_key;
1513 stcb->asoc.authinfo.random_len = random_len;
1514 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1515 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1517 /* negotiate what HMAC to use for the peer */
1518 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1519 stcb->asoc.local_hmacs);
1521 /* copy defaults from the endpoint */
1522 /* FIX ME: put in cookie? */
1523 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1524 /* copy out the shared key list (by reference) from the endpoint */
1525 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1526 &stcb->asoc.shared_keys);
1530 * compute and fill in the HMAC digest for a packet
1533 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1534 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1537 sctp_sharedkey_t *skey;
1540 if ((stcb == NULL) || (auth == NULL))
1543 /* zero the digest + chunk padding */
1544 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1545 memset(auth->hmac, 0, SCTP_SIZE32(digestlen));
1547 /* is the desired key cached? */
1548 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1549 (stcb->asoc.authinfo.assoc_key == NULL)) {
1550 if (stcb->asoc.authinfo.assoc_key != NULL) {
1551 /* free the old cached key */
1552 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1554 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1555 /* the only way skey is NULL is if null key id 0 is used */
1560 /* compute a new assoc key and cache it */
1561 stcb->asoc.authinfo.assoc_key =
1562 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1563 stcb->asoc.authinfo.peer_random, key);
1564 stcb->asoc.authinfo.assoc_keyid = keyid;
1565 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1566 stcb->asoc.authinfo.assoc_keyid);
1568 if (SCTP_AUTH_DEBUG)
1569 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1574 /* set in the active key id */
1575 auth->shared_key_id = htons(keyid);
1577 /* compute and fill in the digest */
1578 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1579 m, auth_offset, auth->hmac);
1584 sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1593 /* find the correct starting mbuf and offset (get start position) */
1595 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1596 m_offset -= SCTP_BUF_LEN(m_tmp);
1597 m_tmp = SCTP_BUF_NEXT(m_tmp);
1599 /* now use the rest of the mbuf chain */
1600 while ((m_tmp != NULL) && (size > 0)) {
1601 data = mtod(m_tmp, uint8_t *) + m_offset;
1602 if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) {
1603 memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset);
1604 size -= SCTP_BUF_LEN(m_tmp) - m_offset;
1606 memset(data, 0, size);
1609 /* clear the offset since it's only for the first mbuf */
1611 m_tmp = SCTP_BUF_NEXT(m_tmp);
1616 * process the incoming Authentication chunk
1618 * -1 on any authentication error
1619 * 0 on authentication verification
1622 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1623 struct mbuf *m, uint32_t offset)
1626 uint16_t shared_key_id;
1628 sctp_sharedkey_t *skey;
1630 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1631 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1633 /* auth is checked for NULL by caller */
1634 chunklen = ntohs(auth->ch.chunk_length);
1635 if (chunklen < sizeof(*auth)) {
1636 SCTP_STAT_INCR(sctps_recvauthfailed);
1639 SCTP_STAT_INCR(sctps_recvauth);
1641 /* get the auth params */
1642 shared_key_id = ntohs(auth->shared_key_id);
1643 hmac_id = ntohs(auth->hmac_id);
1644 SCTPDBG(SCTP_DEBUG_AUTH1,
1645 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1646 shared_key_id, hmac_id);
1648 #if defined(__Userspace__)
1649 #ifdef FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION
1653 /* is the indicated HMAC supported? */
1654 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1655 struct mbuf *op_err;
1656 struct sctp_error_auth_invalid_hmac *cause;
1658 SCTP_STAT_INCR(sctps_recvivalhmacid);
1659 SCTPDBG(SCTP_DEBUG_AUTH1,
1660 "SCTP Auth: unsupported HMAC id %u\n",
1663 * report this in an Error Chunk: Unsupported HMAC
1666 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
1667 0, M_NOWAIT, 1, MT_HEADER);
1668 if (op_err != NULL) {
1669 /* pre-reserve some space */
1670 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1671 /* fill in the error */
1672 cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
1673 cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1674 cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
1675 cause->hmac_id = ntohs(hmac_id);
1676 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
1678 sctp_queue_op_err(stcb, op_err);
1682 /* get the indicated shared key, if available */
1683 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1684 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1685 /* find the shared key on the assoc first */
1686 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1688 /* if the shared key isn't found, discard the chunk */
1690 SCTP_STAT_INCR(sctps_recvivalkeyid);
1691 SCTPDBG(SCTP_DEBUG_AUTH1,
1692 "SCTP Auth: unknown key id %u\n",
1696 /* generate a notification if this is a new key id */
1697 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1699 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1700 * shared_key_id, (void
1701 * *)stcb->asoc.authinfo.recv_keyid);
1703 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1704 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1705 SCTP_SO_NOT_LOCKED);
1706 /* compute a new recv assoc key and cache it */
1707 if (stcb->asoc.authinfo.recv_key != NULL)
1708 sctp_free_key(stcb->asoc.authinfo.recv_key);
1709 stcb->asoc.authinfo.recv_key =
1710 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1711 stcb->asoc.authinfo.peer_random, skey->key);
1712 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1714 if (SCTP_AUTH_DEBUG)
1715 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1718 /* validate the digest length */
1719 digestlen = sctp_get_hmac_digest_len(hmac_id);
1720 if (chunklen < (sizeof(*auth) + digestlen)) {
1721 /* invalid digest length */
1722 SCTP_STAT_INCR(sctps_recvauthfailed);
1723 SCTPDBG(SCTP_DEBUG_AUTH1,
1724 "SCTP Auth: chunk too short for HMAC\n");
1727 /* save a copy of the digest, zero the pseudo header, and validate */
1728 memcpy(digest, auth->hmac, digestlen);
1729 sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1730 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1731 m, offset, computed_digest);
1733 /* compare the computed digest with the one in the AUTH chunk */
1734 if (timingsafe_bcmp(digest, computed_digest, digestlen) != 0) {
1735 SCTP_STAT_INCR(sctps_recvauthfailed);
1736 SCTPDBG(SCTP_DEBUG_AUTH1,
1737 "SCTP Auth: HMAC digest check failed\n");
1744 * Generate NOTIFICATION
1747 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1748 uint16_t keyid, uint16_t alt_keyid, int so_locked)
1750 struct mbuf *m_notify;
1751 struct sctp_authkey_event *auth;
1752 struct sctp_queued_to_read *control;
1754 if ((stcb == NULL) ||
1755 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1756 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1757 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1759 /* If the socket is gone we are out of here */
1763 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1764 /* event not enabled */
1767 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1768 0, M_NOWAIT, 1, MT_HEADER);
1769 if (m_notify == NULL)
1773 SCTP_BUF_LEN(m_notify) = 0;
1774 auth = mtod(m_notify, struct sctp_authkey_event *);
1775 memset(auth, 0, sizeof(struct sctp_authkey_event));
1776 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1777 auth->auth_flags = 0;
1778 auth->auth_length = sizeof(*auth);
1779 auth->auth_keynumber = keyid;
1780 auth->auth_altkeynumber = alt_keyid;
1781 auth->auth_indication = indication;
1782 auth->auth_assoc_id = sctp_get_associd(stcb);
1784 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1785 SCTP_BUF_NEXT(m_notify) = NULL;
1787 /* append to socket */
1788 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1789 0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1790 if (control == NULL) {
1792 sctp_m_freem(m_notify);
1795 control->length = SCTP_BUF_LEN(m_notify);
1796 control->spec_flags = M_NOTIFICATION;
1797 /* not that we need this */
1798 control->tail_mbuf = m_notify;
1799 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1800 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1805 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1806 * Note: currently only used for INIT as INIT-ACK is handled inline
1807 * with sctp_load_addresses_from_init()
1810 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1812 struct sctp_paramhdr *phdr, param_buf;
1813 uint16_t ptype, plen;
1814 int peer_supports_asconf = 0;
1815 int peer_supports_auth = 0;
1816 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1817 uint8_t saw_asconf = 0;
1818 uint8_t saw_asconf_ack = 0;
1820 /* go through each of the params. */
1821 phdr = sctp_get_next_param(m, offset, ¶m_buf, sizeof(param_buf));
1823 ptype = ntohs(phdr->param_type);
1824 plen = ntohs(phdr->param_length);
1826 if (offset + plen > limit) {
1829 if (plen < sizeof(struct sctp_paramhdr)) {
1832 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1833 /* A supported extension chunk */
1834 struct sctp_supported_chunk_types_param *pr_supported;
1835 uint8_t local_store[SCTP_SMALL_CHUNK_STORE];
1838 if (plen > sizeof(local_store)) {
1841 phdr = sctp_get_next_param(m, offset,
1842 (struct sctp_paramhdr *)&local_store,
1847 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1848 num_ent = plen - sizeof(struct sctp_paramhdr);
1849 for (i = 0; i < num_ent; i++) {
1850 switch (pr_supported->chunk_types[i]) {
1852 case SCTP_ASCONF_ACK:
1853 peer_supports_asconf = 1;
1856 /* one we don't care about */
1860 } else if (ptype == SCTP_RANDOM) {
1861 /* enforce the random length */
1862 if (plen != (sizeof(struct sctp_auth_random) +
1863 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1864 SCTPDBG(SCTP_DEBUG_AUTH1,
1865 "SCTP: invalid RANDOM len\n");
1869 } else if (ptype == SCTP_HMAC_LIST) {
1870 struct sctp_auth_hmac_algo *hmacs;
1871 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1874 if (plen > sizeof(store)) {
1877 phdr = sctp_get_next_param(m, offset,
1878 (struct sctp_paramhdr *)store,
1883 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1884 num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]);
1885 /* validate the hmac list */
1886 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1887 SCTPDBG(SCTP_DEBUG_AUTH1,
1888 "SCTP: invalid HMAC param\n");
1892 } else if (ptype == SCTP_CHUNK_LIST) {
1893 struct sctp_auth_chunk_list *chunks;
1894 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1897 if (plen > sizeof(chunks_store)) {
1900 phdr = sctp_get_next_param(m, offset,
1901 (struct sctp_paramhdr *)chunks_store,
1907 * Flip through the list and mark that the
1908 * peer supports asconf/asconf_ack.
1910 chunks = (struct sctp_auth_chunk_list *)phdr;
1911 num_chunks = plen - sizeof(*chunks);
1912 for (i = 0; i < num_chunks; i++) {
1913 /* record asconf/asconf-ack if listed */
1914 if (chunks->chunk_types[i] == SCTP_ASCONF)
1916 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1924 offset += SCTP_SIZE32(plen);
1925 if (offset >= limit) {
1928 phdr = sctp_get_next_param(m, offset, ¶m_buf,
1931 /* validate authentication required parameters */
1932 if (got_random && got_hmacs) {
1933 peer_supports_auth = 1;
1935 peer_supports_auth = 0;
1937 if (!peer_supports_auth && got_chklist) {
1938 SCTPDBG(SCTP_DEBUG_AUTH1,
1939 "SCTP: peer sent chunk list w/o AUTH\n");
1942 if (peer_supports_asconf && !peer_supports_auth) {
1943 SCTPDBG(SCTP_DEBUG_AUTH1,
1944 "SCTP: peer supports ASCONF but not AUTH\n");
1946 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1947 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1954 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1956 uint16_t chunks_len = 0;
1957 uint16_t hmacs_len = 0;
1958 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1959 sctp_key_t *new_key;
1962 /* initialize hmac list from endpoint */
1963 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1964 if (stcb->asoc.local_hmacs != NULL) {
1965 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1966 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1968 /* initialize auth chunks list from endpoint */
1969 stcb->asoc.local_auth_chunks =
1970 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1971 if (stcb->asoc.local_auth_chunks != NULL) {
1973 for (i = 0; i < 256; i++) {
1974 if (stcb->asoc.local_auth_chunks->chunks[i])
1978 /* copy defaults from the endpoint */
1979 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
1981 /* copy out the shared key list (by reference) from the endpoint */
1982 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
1983 &stcb->asoc.shared_keys);
1985 /* now set the concatenated key (random + chunks + hmacs) */
1986 /* key includes parameter headers */
1987 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1989 new_key = sctp_alloc_key(keylen);
1990 if (new_key != NULL) {
1991 struct sctp_paramhdr *ph;
1993 /* generate and copy in the RANDOM */
1994 ph = (struct sctp_paramhdr *)new_key->key;
1995 ph->param_type = htons(SCTP_RANDOM);
1996 plen = sizeof(*ph) + random_len;
1997 ph->param_length = htons(plen);
1998 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2001 /* append in the AUTH chunks */
2002 /* NOTE: currently we always have chunks to list */
2003 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2004 ph->param_type = htons(SCTP_CHUNK_LIST);
2005 plen = sizeof(*ph) + chunks_len;
2006 ph->param_length = htons(plen);
2007 keylen += sizeof(*ph);
2008 if (stcb->asoc.local_auth_chunks) {
2010 for (i = 0; i < 256; i++) {
2011 if (stcb->asoc.local_auth_chunks->chunks[i])
2012 new_key->key[keylen++] = i;
2016 /* append in the HMACs */
2017 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2018 ph->param_type = htons(SCTP_HMAC_LIST);
2019 plen = sizeof(*ph) + hmacs_len;
2020 ph->param_length = htons(plen);
2021 keylen += sizeof(*ph);
2022 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2023 new_key->key + keylen);
2025 if (stcb->asoc.authinfo.random != NULL)
2026 sctp_free_key(stcb->asoc.authinfo.random);
2027 stcb->asoc.authinfo.random = new_key;
2028 stcb->asoc.authinfo.random_len = random_len;
2032 #ifdef SCTP_HMAC_TEST
2034 * HMAC and key concatenation tests
2037 sctp_print_digest(uint8_t *digest, uint32_t digestlen, const char *str)
2041 SCTP_PRINTF("\n%s: 0x", str);
2045 for (i = 0; i < digestlen; i++)
2046 SCTP_PRINTF("%02x", digest[i]);
2050 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t *key,
2051 uint32_t keylen, uint8_t *text, uint32_t textlen,
2052 uint8_t *digest, uint32_t digestlen)
2054 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2056 SCTP_PRINTF("\n%s:", str);
2057 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2058 sctp_print_digest(digest, digestlen, "Expected digest");
2059 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2060 if (memcmp(digest, computed_digest, digestlen) != 0) {
2061 SCTP_PRINTF("\nFAILED");
2064 SCTP_PRINTF("\nPASSED");
2071 * RFC 2202: HMAC-SHA1 test cases
2074 sctp_test_hmac_sha1(void)
2081 uint32_t digestlen = 20;
2086 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2090 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
2093 memset(key, 0x0b, keylen);
2095 strcpy(text, "Hi There");
2096 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2097 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2098 text, textlen, digest, digestlen) < 0)
2105 * data = "what do ya want for nothing?"
2107 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2110 strcpy(key, "Jefe");
2112 strcpy(text, "what do ya want for nothing?");
2113 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2114 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2115 text, textlen, digest, digestlen) < 0)
2120 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2122 * data = 0xdd repeated 50 times
2124 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2127 memset(key, 0xaa, keylen);
2129 memset(text, 0xdd, textlen);
2130 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2131 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2132 text, textlen, digest, digestlen) < 0)
2137 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2139 * data = 0xcd repeated 50 times
2141 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2144 memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2146 memset(text, 0xcd, textlen);
2147 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2148 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2149 text, textlen, digest, digestlen) < 0)
2154 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2156 * data = "Test With Truncation"
2158 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2159 * digest-96 = 0x4c1a03424b55e07fe7f27be1
2162 memset(key, 0x0c, keylen);
2164 strcpy(text, "Test With Truncation");
2165 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2166 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2167 text, textlen, digest, digestlen) < 0)
2172 * key = 0xaa repeated 80 times
2174 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2176 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2179 memset(key, 0xaa, keylen);
2181 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2182 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2183 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2184 text, textlen, digest, digestlen) < 0)
2189 * key = 0xaa repeated 80 times
2191 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2193 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2196 memset(key, 0xaa, keylen);
2198 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2199 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2200 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2201 text, textlen, digest, digestlen) < 0)
2204 /* done with all tests */
2206 SCTP_PRINTF("\nSHA1 test results: %d cases failed", failed);
2208 SCTP_PRINTF("\nSHA1 test results: all test cases passed");
2212 * test assoc key concatenation
2215 sctp_test_key_concatenation(sctp_key_t *key1, sctp_key_t *key2,
2216 sctp_key_t *expected_key)
2221 sctp_show_key(key1, "\nkey1");
2222 sctp_show_key(key2, "\nkey2");
2223 key = sctp_compute_hashkey(key1, key2, NULL);
2224 sctp_show_key(expected_key, "\nExpected");
2225 sctp_show_key(key, "\nComputed");
2226 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2227 SCTP_PRINTF("\nFAILED");
2230 SCTP_PRINTF("\nPASSED");
2233 sctp_free_key(key1);
2234 sctp_free_key(key2);
2235 sctp_free_key(expected_key);
2242 sctp_test_authkey(void)
2244 sctp_key_t *key1, *key2, *expected_key;
2248 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2249 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2250 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2251 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2255 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2256 key2 = sctp_set_key("\x02", 1);
2257 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2258 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2262 key1 = sctp_set_key("\x01", 1);
2263 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2264 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2265 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2269 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2270 key2 = sctp_set_key("\x01", 1);
2271 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2272 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2276 key1 = sctp_set_key("\x01", 1);
2277 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2278 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2279 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2283 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2284 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2285 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2286 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2290 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2291 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2292 expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2293 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2296 /* done with all tests */
2298 SCTP_PRINTF("\nKey concatenation test results: %d cases failed", failed);
2300 SCTP_PRINTF("\nKey concatenation test results: all test cases passed");
2304 #if defined(STANDALONE_HMAC_TEST)
2308 sctp_test_hmac_sha1();
2309 sctp_test_authkey();
2312 #endif /* STANDALONE_HMAC_TEST */
2314 #endif /* SCTP_HMAC_TEST */