2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
36 #include <netinet/sctp_os.h>
37 #include <netinet/sctp.h>
38 #include <netinet/sctp_header.h>
39 #include <netinet/sctp_pcb.h>
40 #include <netinet/sctp_var.h>
41 #include <netinet/sctp_sysctl.h>
42 #include <netinet/sctputil.h>
43 #include <netinet/sctp_indata.h>
44 #include <netinet/sctp_output.h>
45 #include <netinet/sctp_auth.h>
48 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
49 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
50 #endif /* SCTP_DEBUG */
54 sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
56 bzero(chklist, sizeof(*chklist));
57 /* chklist->num_chunks = 0; */
61 sctp_alloc_chunklist(void)
63 sctp_auth_chklist_t *chklist;
65 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
67 if (chklist == NULL) {
68 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
70 sctp_clear_chunklist(chklist);
76 sctp_free_chunklist(sctp_auth_chklist_t * list)
79 SCTP_FREE(list, SCTP_M_AUTH_CL);
83 sctp_copy_chunklist(sctp_auth_chklist_t * list)
85 sctp_auth_chklist_t *new_list;
91 new_list = sctp_alloc_chunklist();
95 bcopy(list, new_list, sizeof(*new_list));
102 * add a chunk to the required chunks list
105 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
110 /* is chunk restricted? */
111 if ((chunk == SCTP_INITIATION) ||
112 (chunk == SCTP_INITIATION_ACK) ||
113 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
114 (chunk == SCTP_AUTHENTICATION)) {
117 if (list->chunks[chunk] == 0) {
118 list->chunks[chunk] = 1;
120 SCTPDBG(SCTP_DEBUG_AUTH1,
121 "SCTP: added chunk %u (0x%02x) to Auth list\n",
128 * delete a chunk from the required chunks list
131 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
136 /* is chunk restricted? */
137 if ((chunk == SCTP_ASCONF) ||
138 (chunk == SCTP_ASCONF_ACK)) {
141 if (list->chunks[chunk] == 1) {
142 list->chunks[chunk] = 0;
144 SCTPDBG(SCTP_DEBUG_AUTH1,
145 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
152 sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
157 return (list->num_chunks);
161 * set the default list of chunks requiring AUTH
164 sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
166 (void)sctp_auth_add_chunk(SCTP_ASCONF, list);
167 (void)sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
171 * return the current number and list of required chunks caller must
172 * guarantee ptr has space for up to 256 bytes
175 sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
182 for (i = 0; i < 256; i++) {
183 if (list->chunks[i] != 0) {
192 sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
199 if (list->num_chunks <= 32) {
200 /* just list them, one byte each */
201 for (i = 0; i < 256; i++) {
202 if (list->chunks[i] != 0) {
210 /* pack into a 32 byte bitfield */
211 for (i = 0; i < 256; i++) {
212 if (list->chunks[i] != 0) {
215 ptr[index] |= (1 << offset);
224 sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
225 sctp_auth_chklist_t * list)
233 if (num_chunks <= 32) {
234 /* just pull them, one byte each */
235 for (i = 0; i < num_chunks; i++) {
236 (void)sctp_auth_add_chunk(*ptr++, list);
242 /* unpack from a 32 byte bitfield */
243 for (index = 0; index < 32; index++) {
244 for (offset = 0; offset < 8; offset++) {
245 if (ptr[index] & (1 << offset)) {
246 (void)sctp_auth_add_chunk((index * 8) + offset, list);
257 * allocate structure space for a key of length keylen
260 sctp_alloc_key(uint32_t keylen)
264 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
266 if (new_key == NULL) {
270 new_key->keylen = keylen;
275 sctp_free_key(sctp_key_t * key)
278 SCTP_FREE(key, SCTP_M_AUTH_KY);
282 sctp_print_key(sctp_key_t * key, const char *str)
287 SCTP_PRINTF("%s: [Null key]\n", str);
290 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
292 for (i = 0; i < key->keylen; i++)
293 SCTP_PRINTF("%02x", key->key[i]);
296 SCTP_PRINTF("[Null key]\n");
301 sctp_show_key(sctp_key_t * key, const char *str)
306 SCTP_PRINTF("%s: [Null key]\n", str);
309 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
311 for (i = 0; i < key->keylen; i++)
312 SCTP_PRINTF("%02x", key->key[i]);
315 SCTP_PRINTF("[Null key]\n");
320 sctp_get_keylen(sctp_key_t * key)
323 return (key->keylen);
329 * generate a new random key of length 'keylen'
332 sctp_generate_random_key(uint32_t keylen)
336 /* validate keylen */
337 if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
338 keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
340 new_key = sctp_alloc_key(keylen);
341 if (new_key == NULL) {
345 SCTP_READ_RANDOM(new_key->key, keylen);
346 new_key->keylen = keylen;
351 sctp_set_key(uint8_t * key, uint32_t keylen)
355 new_key = sctp_alloc_key(keylen);
356 if (new_key == NULL) {
360 bcopy(key, new_key->key, keylen);
365 * given two keys of variable size, compute which key is "larger/smaller"
366 * returns: 1 if key1 > key2
371 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
375 uint32_t key1len, key2len;
376 uint8_t *key_1, *key_2;
377 uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
379 /* sanity/length check */
380 key1len = sctp_get_keylen(key1);
381 key2len = sctp_get_keylen(key2);
382 if ((key1len == 0) && (key2len == 0))
384 else if (key1len == 0)
386 else if (key2len == 0)
389 if (key1len != key2len) {
390 if (key1len >= key2len)
395 if (key1len < maxlen) {
396 /* prepend zeroes to key1 */
397 bcopy(key1->key, temp + (maxlen - key1len), key1len);
401 /* prepend zeroes to key2 */
402 bcopy(key2->key, temp + (maxlen - key2len), key2len);
412 for (i = 0; i < maxlen; i++) {
415 else if (*key_1 < *key_2)
421 /* keys are equal value, so check lengths */
422 if (key1len == key2len)
424 else if (key1len < key2len)
431 * generate the concatenated keying material based on the two keys and the
432 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
433 * order for concatenation
436 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
442 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
443 sctp_get_keylen(shared);
446 /* get space for the new key */
447 new_key = sctp_alloc_key(keylen);
448 if (new_key == NULL) {
452 new_key->keylen = keylen;
453 key_ptr = new_key->key;
455 /* all keys empty/null?! */
459 /* concatenate the keys */
460 if (sctp_compare_key(key1, key2) <= 0) {
461 /* key is shared + key1 + key2 */
462 if (sctp_get_keylen(shared)) {
463 bcopy(shared->key, key_ptr, shared->keylen);
464 key_ptr += shared->keylen;
466 if (sctp_get_keylen(key1)) {
467 bcopy(key1->key, key_ptr, key1->keylen);
468 key_ptr += key1->keylen;
470 if (sctp_get_keylen(key2)) {
471 bcopy(key2->key, key_ptr, key2->keylen);
474 /* key is shared + key2 + key1 */
475 if (sctp_get_keylen(shared)) {
476 bcopy(shared->key, key_ptr, shared->keylen);
477 key_ptr += shared->keylen;
479 if (sctp_get_keylen(key2)) {
480 bcopy(key2->key, key_ptr, key2->keylen);
481 key_ptr += key2->keylen;
483 if (sctp_get_keylen(key1)) {
484 bcopy(key1->key, key_ptr, key1->keylen);
492 sctp_alloc_sharedkey(void)
494 sctp_sharedkey_t *new_key;
496 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
498 if (new_key == NULL) {
504 new_key->refcount = 1;
505 new_key->deactivated = 0;
510 sctp_free_sharedkey(sctp_sharedkey_t * skey)
515 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
516 if (skey->key != NULL)
517 sctp_free_key(skey->key);
518 SCTP_FREE(skey, SCTP_M_AUTH_KY);
523 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
525 sctp_sharedkey_t *skey;
527 LIST_FOREACH(skey, shared_keys, next) {
528 if (skey->keyid == key_id)
535 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
536 sctp_sharedkey_t * new_skey)
538 sctp_sharedkey_t *skey;
540 if ((shared_keys == NULL) || (new_skey == NULL))
543 /* insert into an empty list? */
544 if (LIST_EMPTY(shared_keys)) {
545 LIST_INSERT_HEAD(shared_keys, new_skey, next);
548 /* insert into the existing list, ordered by key id */
549 LIST_FOREACH(skey, shared_keys, next) {
550 if (new_skey->keyid < skey->keyid) {
551 /* insert it before here */
552 LIST_INSERT_BEFORE(skey, new_skey, next);
554 } else if (new_skey->keyid == skey->keyid) {
555 /* replace the existing key */
556 /* verify this key *can* be replaced */
557 if ((skey->deactivated) && (skey->refcount > 1)) {
558 SCTPDBG(SCTP_DEBUG_AUTH1,
559 "can't replace shared key id %u\n",
563 SCTPDBG(SCTP_DEBUG_AUTH1,
564 "replacing shared key id %u\n",
566 LIST_INSERT_BEFORE(skey, new_skey, next);
567 LIST_REMOVE(skey, next);
568 sctp_free_sharedkey(skey);
571 if (LIST_NEXT(skey, next) == NULL) {
572 /* belongs at the end of the list */
573 LIST_INSERT_AFTER(skey, new_skey, next);
577 /* shouldn't reach here */
582 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
584 sctp_sharedkey_t *skey;
586 /* find the shared key */
587 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
589 /* bump the ref count */
591 atomic_add_int(&skey->refcount, 1);
592 SCTPDBG(SCTP_DEBUG_AUTH2,
593 "%s: stcb %p key %u refcount acquire to %d\n",
594 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
599 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
600 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
605 sctp_sharedkey_t *skey;
607 /* find the shared key */
608 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
610 /* decrement the ref count */
612 sctp_free_sharedkey(skey);
613 SCTPDBG(SCTP_DEBUG_AUTH2,
614 "%s: stcb %p key %u refcount release to %d\n",
615 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
617 /* see if a notification should be generated */
618 if ((skey->refcount <= 1) && (skey->deactivated)) {
619 /* notify ULP that key is no longer used */
620 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
621 key_id, 0, so_locked);
622 SCTPDBG(SCTP_DEBUG_AUTH2,
623 "%s: stcb %p key %u no longer used, %d\n",
624 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
629 static sctp_sharedkey_t *
630 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
632 sctp_sharedkey_t *new_skey;
636 new_skey = sctp_alloc_sharedkey();
637 if (new_skey == NULL)
639 if (skey->key != NULL)
640 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
642 new_skey->key = NULL;
643 new_skey->keyid = skey->keyid;
648 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
650 sctp_sharedkey_t *skey, *new_skey;
653 if ((src == NULL) || (dest == NULL))
655 LIST_FOREACH(skey, src, next) {
656 new_skey = sctp_copy_sharedkey(skey);
657 if (new_skey != NULL) {
658 (void)sctp_insert_sharedkey(dest, new_skey);
667 sctp_alloc_hmaclist(uint8_t num_hmacs)
669 sctp_hmaclist_t *new_list;
672 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
673 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
675 if (new_list == NULL) {
679 new_list->max_algo = num_hmacs;
680 new_list->num_algo = 0;
685 sctp_free_hmaclist(sctp_hmaclist_t * list)
688 SCTP_FREE(list, SCTP_M_AUTH_HL);
694 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
700 if (list->num_algo == list->max_algo) {
701 SCTPDBG(SCTP_DEBUG_AUTH1,
702 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
705 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
706 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
709 /* Now is it already in the list */
710 for (i = 0; i < list->num_algo; i++) {
711 if (list->hmac[i] == hmac_id) {
712 /* already in list */
716 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
717 list->hmac[list->num_algo++] = hmac_id;
722 sctp_copy_hmaclist(sctp_hmaclist_t * list)
724 sctp_hmaclist_t *new_list;
730 new_list = sctp_alloc_hmaclist(list->max_algo);
731 if (new_list == NULL)
734 new_list->max_algo = list->max_algo;
735 new_list->num_algo = list->num_algo;
736 for (i = 0; i < list->num_algo; i++)
737 new_list->hmac[i] = list->hmac[i];
742 sctp_default_supported_hmaclist(void)
744 sctp_hmaclist_t *new_list;
746 new_list = sctp_alloc_hmaclist(2);
747 if (new_list == NULL)
749 /* We prefer SHA256, so list it first */
750 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
751 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
756 * HMAC algos are listed in priority/preference order
757 * find the best HMAC id to use for the peer based on local support
760 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
764 if ((local == NULL) || (peer == NULL))
765 return (SCTP_AUTH_HMAC_ID_RSVD);
767 for (i = 0; i < peer->num_algo; i++) {
768 for (j = 0; j < local->num_algo; j++) {
769 if (peer->hmac[i] == local->hmac[j]) {
770 /* found the "best" one */
771 SCTPDBG(SCTP_DEBUG_AUTH1,
772 "SCTP: negotiated peer HMAC id %u\n",
774 return (peer->hmac[i]);
778 /* didn't find one! */
779 return (SCTP_AUTH_HMAC_ID_RSVD);
783 * serialize the HMAC algo list and return space used
784 * caller must guarantee ptr has appropriate space
787 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
795 for (i = 0; i < list->num_algo; i++) {
796 hmac_id = htons(list->hmac[i]);
797 bcopy(&hmac_id, ptr, sizeof(hmac_id));
798 ptr += sizeof(hmac_id);
800 return (list->num_algo * sizeof(hmac_id));
804 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
808 for (i = 0; i < num_hmacs; i++) {
809 if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
817 sctp_alloc_authinfo(void)
819 sctp_authinfo_t *new_authinfo;
821 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
824 if (new_authinfo == NULL) {
828 bzero(new_authinfo, sizeof(*new_authinfo));
829 return (new_authinfo);
833 sctp_free_authinfo(sctp_authinfo_t * authinfo)
835 if (authinfo == NULL)
838 if (authinfo->random != NULL)
839 sctp_free_key(authinfo->random);
840 if (authinfo->peer_random != NULL)
841 sctp_free_key(authinfo->peer_random);
842 if (authinfo->assoc_key != NULL)
843 sctp_free_key(authinfo->assoc_key);
844 if (authinfo->recv_key != NULL)
845 sctp_free_key(authinfo->recv_key);
847 /* We are NOT dynamically allocating authinfo's right now... */
848 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
853 sctp_get_auth_chunk_len(uint16_t hmac_algo)
857 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
858 return (SCTP_SIZE32(size));
862 sctp_get_hmac_digest_len(uint16_t hmac_algo)
865 case SCTP_AUTH_HMAC_ID_SHA1:
866 return (SCTP_AUTH_DIGEST_LEN_SHA1);
867 case SCTP_AUTH_HMAC_ID_SHA256:
868 return (SCTP_AUTH_DIGEST_LEN_SHA256);
870 /* unknown HMAC algorithm: can't do anything */
876 sctp_get_hmac_block_len(uint16_t hmac_algo)
879 case SCTP_AUTH_HMAC_ID_SHA1:
881 case SCTP_AUTH_HMAC_ID_SHA256:
883 case SCTP_AUTH_HMAC_ID_RSVD:
885 /* unknown HMAC algorithm: can't do anything */
891 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
894 case SCTP_AUTH_HMAC_ID_SHA1:
895 SCTP_SHA1_INIT(&ctx->sha1);
897 case SCTP_AUTH_HMAC_ID_SHA256:
898 SCTP_SHA256_INIT(&ctx->sha256);
900 case SCTP_AUTH_HMAC_ID_RSVD:
902 /* unknown HMAC algorithm: can't do anything */
908 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
909 uint8_t * text, uint32_t textlen)
912 case SCTP_AUTH_HMAC_ID_SHA1:
913 SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
915 case SCTP_AUTH_HMAC_ID_SHA256:
916 SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
918 case SCTP_AUTH_HMAC_ID_RSVD:
920 /* unknown HMAC algorithm: can't do anything */
926 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
930 case SCTP_AUTH_HMAC_ID_SHA1:
931 SCTP_SHA1_FINAL(digest, &ctx->sha1);
933 case SCTP_AUTH_HMAC_ID_SHA256:
934 SCTP_SHA256_FINAL(digest, &ctx->sha256);
936 case SCTP_AUTH_HMAC_ID_RSVD:
938 /* unknown HMAC algorithm: can't do anything */
944 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
946 * Compute the HMAC digest using the desired hash key, text, and HMAC
947 * algorithm. Resulting digest is placed in 'digest' and digest length
948 * is returned, if the HMAC was performed.
950 * WARNING: it is up to the caller to supply sufficient space to hold the
954 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
955 uint8_t * text, uint32_t textlen, uint8_t * digest)
959 sctp_hash_context_t ctx;
960 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
961 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
964 /* sanity check the material and length */
965 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
966 (textlen == 0) || (digest == NULL)) {
967 /* can't do HMAC with empty key or text or digest store */
970 /* validate the hmac algo and get the digest length */
971 digestlen = sctp_get_hmac_digest_len(hmac_algo);
975 /* hash the key if it is longer than the hash block size */
976 blocklen = sctp_get_hmac_block_len(hmac_algo);
977 if (keylen > blocklen) {
978 sctp_hmac_init(hmac_algo, &ctx);
979 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
980 sctp_hmac_final(hmac_algo, &ctx, temp);
981 /* set the hashed key as the key */
985 /* initialize the inner/outer pads with the key and "append" zeroes */
986 bzero(ipad, blocklen);
987 bzero(opad, blocklen);
988 bcopy(key, ipad, keylen);
989 bcopy(key, opad, keylen);
991 /* XOR the key with ipad and opad values */
992 for (i = 0; i < blocklen; i++) {
997 /* perform inner hash */
998 sctp_hmac_init(hmac_algo, &ctx);
999 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1000 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1001 sctp_hmac_final(hmac_algo, &ctx, temp);
1003 /* perform outer hash */
1004 sctp_hmac_init(hmac_algo, &ctx);
1005 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1006 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1007 sctp_hmac_final(hmac_algo, &ctx, digest);
1014 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1015 struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1019 sctp_hash_context_t ctx;
1020 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1021 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1025 /* sanity check the material and length */
1026 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1027 /* can't do HMAC with empty key or text or digest store */
1030 /* validate the hmac algo and get the digest length */
1031 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1035 /* hash the key if it is longer than the hash block size */
1036 blocklen = sctp_get_hmac_block_len(hmac_algo);
1037 if (keylen > blocklen) {
1038 sctp_hmac_init(hmac_algo, &ctx);
1039 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1040 sctp_hmac_final(hmac_algo, &ctx, temp);
1041 /* set the hashed key as the key */
1045 /* initialize the inner/outer pads with the key and "append" zeroes */
1046 bzero(ipad, blocklen);
1047 bzero(opad, blocklen);
1048 bcopy(key, ipad, keylen);
1049 bcopy(key, opad, keylen);
1051 /* XOR the key with ipad and opad values */
1052 for (i = 0; i < blocklen; i++) {
1057 /* perform inner hash */
1058 sctp_hmac_init(hmac_algo, &ctx);
1059 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1060 /* find the correct starting mbuf and offset (get start of text) */
1062 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1063 m_offset -= SCTP_BUF_LEN(m_tmp);
1064 m_tmp = SCTP_BUF_NEXT(m_tmp);
1066 /* now use the rest of the mbuf chain for the text */
1067 while (m_tmp != NULL) {
1068 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1069 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1070 SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1072 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1073 SCTP_BUF_LEN(m_tmp) - m_offset);
1076 /* clear the offset since it's only for the first mbuf */
1078 m_tmp = SCTP_BUF_NEXT(m_tmp);
1080 sctp_hmac_final(hmac_algo, &ctx, temp);
1082 /* perform outer hash */
1083 sctp_hmac_init(hmac_algo, &ctx);
1084 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1085 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1086 sctp_hmac_final(hmac_algo, &ctx, digest);
1092 * verify the HMAC digest using the desired hash key, text, and HMAC
1094 * Returns -1 on error, 0 on success.
1097 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1098 uint8_t * text, uint32_t textlen,
1099 uint8_t * digest, uint32_t digestlen)
1102 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1104 /* sanity check the material and length */
1105 if ((key == NULL) || (keylen == 0) ||
1106 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1107 /* can't do HMAC with empty key or text or digest */
1110 len = sctp_get_hmac_digest_len(hmac_algo);
1111 if ((len == 0) || (digestlen != len))
1114 /* compute the expected hash */
1115 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1118 if (memcmp(digest, temp, digestlen) != 0)
1126 * computes the requested HMAC using a key struct (which may be modified if
1127 * the keylen exceeds the HMAC block len).
1130 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1131 uint32_t textlen, uint8_t * digest)
1135 sctp_hash_context_t ctx;
1136 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1139 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1141 /* can't do HMAC with empty key or text or digest store */
1144 /* validate the hmac algo and get the digest length */
1145 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1149 /* hash the key if it is longer than the hash block size */
1150 blocklen = sctp_get_hmac_block_len(hmac_algo);
1151 if (key->keylen > blocklen) {
1152 sctp_hmac_init(hmac_algo, &ctx);
1153 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1154 sctp_hmac_final(hmac_algo, &ctx, temp);
1155 /* save the hashed key as the new key */
1156 key->keylen = digestlen;
1157 bcopy(temp, key->key, key->keylen);
1159 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1165 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1166 uint32_t m_offset, uint8_t * digest)
1170 sctp_hash_context_t ctx;
1171 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1174 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1175 /* can't do HMAC with empty key or text or digest store */
1178 /* validate the hmac algo and get the digest length */
1179 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1183 /* hash the key if it is longer than the hash block size */
1184 blocklen = sctp_get_hmac_block_len(hmac_algo);
1185 if (key->keylen > blocklen) {
1186 sctp_hmac_init(hmac_algo, &ctx);
1187 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1188 sctp_hmac_final(hmac_algo, &ctx, temp);
1189 /* save the hashed key as the new key */
1190 key->keylen = digestlen;
1191 bcopy(temp, key->key, key->keylen);
1193 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1197 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1201 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1204 for (i = 0; i < list->num_algo; i++)
1205 if (list->hmac[i] == id)
1208 /* not in the list */
1214 * clear any cached key(s) if they match the given key id on an association.
1215 * the cached key(s) will be recomputed and re-cached at next use.
1216 * ASSUMES TCB_LOCK is already held
1219 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1224 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1225 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1226 stcb->asoc.authinfo.assoc_key = NULL;
1228 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1229 sctp_free_key(stcb->asoc.authinfo.recv_key);
1230 stcb->asoc.authinfo.recv_key = NULL;
1235 * clear any cached key(s) if they match the given key id for all assocs on
1237 * ASSUMES INP_WLOCK is already held
1240 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1242 struct sctp_tcb *stcb;
1247 /* clear the cached keys on all assocs on this instance */
1248 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1249 SCTP_TCB_LOCK(stcb);
1250 sctp_clear_cachedkeys(stcb, keyid);
1251 SCTP_TCB_UNLOCK(stcb);
1256 * delete a shared key from an association
1257 * ASSUMES TCB_LOCK is already held
1260 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1262 sctp_sharedkey_t *skey;
1267 /* is the keyid the assoc active sending key */
1268 if (keyid == stcb->asoc.authinfo.active_keyid)
1271 /* does the key exist? */
1272 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1276 /* are there other refcount holders on the key? */
1277 if (skey->refcount > 1)
1281 LIST_REMOVE(skey, next);
1282 sctp_free_sharedkey(skey); /* frees skey->key as well */
1284 /* clear any cached keys */
1285 sctp_clear_cachedkeys(stcb, keyid);
1290 * deletes a shared key from the endpoint
1291 * ASSUMES INP_WLOCK is already held
1294 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1296 sctp_sharedkey_t *skey;
1301 /* is the keyid the active sending key on the endpoint */
1302 if (keyid == inp->sctp_ep.default_keyid)
1305 /* does the key exist? */
1306 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1310 /* endpoint keys are not refcounted */
1313 LIST_REMOVE(skey, next);
1314 sctp_free_sharedkey(skey); /* frees skey->key as well */
1316 /* clear any cached keys */
1317 sctp_clear_cachedkeys_ep(inp, keyid);
1322 * set the active key on an association
1323 * ASSUMES TCB_LOCK is already held
1326 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1328 sctp_sharedkey_t *skey = NULL;
1330 /* find the key on the assoc */
1331 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1333 /* that key doesn't exist */
1336 if ((skey->deactivated) && (skey->refcount > 1)) {
1337 /* can't reactivate a deactivated key with other refcounts */
1340 /* set the (new) active key */
1341 stcb->asoc.authinfo.active_keyid = keyid;
1342 /* reset the deactivated flag */
1343 skey->deactivated = 0;
1349 * set the active key on an endpoint
1350 * ASSUMES INP_WLOCK is already held
1353 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1355 sctp_sharedkey_t *skey;
1358 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1360 /* that key doesn't exist */
1363 inp->sctp_ep.default_keyid = keyid;
1368 * deactivates a shared key from the association
1369 * ASSUMES INP_WLOCK is already held
1372 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1374 sctp_sharedkey_t *skey;
1379 /* is the keyid the assoc active sending key */
1380 if (keyid == stcb->asoc.authinfo.active_keyid)
1383 /* does the key exist? */
1384 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1388 /* are there other refcount holders on the key? */
1389 if (skey->refcount == 1) {
1390 /* no other users, send a notification for this key */
1391 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1394 /* mark the key as deactivated */
1395 skey->deactivated = 1;
1401 * deactivates a shared key from the endpoint
1402 * ASSUMES INP_WLOCK is already held
1405 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1407 sctp_sharedkey_t *skey;
1412 /* is the keyid the active sending key on the endpoint */
1413 if (keyid == inp->sctp_ep.default_keyid)
1416 /* does the key exist? */
1417 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1421 /* endpoint keys are not refcounted */
1424 LIST_REMOVE(skey, next);
1425 sctp_free_sharedkey(skey); /* frees skey->key as well */
1431 * get local authentication parameters from cookie (from INIT-ACK)
1434 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1435 uint32_t offset, uint32_t length)
1437 struct sctp_paramhdr *phdr, tmp_param;
1438 uint16_t plen, ptype;
1439 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1440 struct sctp_auth_random *p_random = NULL;
1441 uint16_t random_len = 0;
1442 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1443 struct sctp_auth_hmac_algo *hmacs = NULL;
1444 uint16_t hmacs_len = 0;
1445 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1446 struct sctp_auth_chunk_list *chunks = NULL;
1447 uint16_t num_chunks = 0;
1448 sctp_key_t *new_key;
1451 /* convert to upper bound */
1454 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1455 sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1456 while (phdr != NULL) {
1457 ptype = ntohs(phdr->param_type);
1458 plen = ntohs(phdr->param_length);
1460 if ((plen == 0) || (offset + plen > length))
1463 if (ptype == SCTP_RANDOM) {
1464 if (plen > sizeof(random_store))
1466 phdr = sctp_get_next_param(m, offset,
1467 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1470 /* save the random and length for the key */
1471 p_random = (struct sctp_auth_random *)phdr;
1472 random_len = plen - sizeof(*p_random);
1473 } else if (ptype == SCTP_HMAC_LIST) {
1477 if (plen > sizeof(hmacs_store))
1479 phdr = sctp_get_next_param(m, offset,
1480 (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1483 /* save the hmacs list and num for the key */
1484 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1485 hmacs_len = plen - sizeof(*hmacs);
1486 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1487 if (stcb->asoc.local_hmacs != NULL)
1488 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1489 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1490 if (stcb->asoc.local_hmacs != NULL) {
1491 for (i = 0; i < num_hmacs; i++) {
1492 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1493 ntohs(hmacs->hmac_ids[i]));
1496 } else if (ptype == SCTP_CHUNK_LIST) {
1499 if (plen > sizeof(chunks_store))
1501 phdr = sctp_get_next_param(m, offset,
1502 (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1505 chunks = (struct sctp_auth_chunk_list *)phdr;
1506 num_chunks = plen - sizeof(*chunks);
1507 /* save chunks list and num for the key */
1508 if (stcb->asoc.local_auth_chunks != NULL)
1509 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1511 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1512 for (i = 0; i < num_chunks; i++) {
1513 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1514 stcb->asoc.local_auth_chunks);
1517 /* get next parameter */
1518 offset += SCTP_SIZE32(plen);
1519 if (offset + sizeof(struct sctp_paramhdr) > length)
1521 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1522 (uint8_t *) & tmp_param);
1524 /* concatenate the full random key */
1525 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1526 if (chunks != NULL) {
1527 keylen += sizeof(*chunks) + num_chunks;
1529 new_key = sctp_alloc_key(keylen);
1530 if (new_key != NULL) {
1531 /* copy in the RANDOM */
1532 if (p_random != NULL) {
1533 keylen = sizeof(*p_random) + random_len;
1534 bcopy(p_random, new_key->key, keylen);
1536 /* append in the AUTH chunks */
1537 if (chunks != NULL) {
1538 bcopy(chunks, new_key->key + keylen,
1539 sizeof(*chunks) + num_chunks);
1540 keylen += sizeof(*chunks) + num_chunks;
1542 /* append in the HMACs */
1543 if (hmacs != NULL) {
1544 bcopy(hmacs, new_key->key + keylen,
1545 sizeof(*hmacs) + hmacs_len);
1548 if (stcb->asoc.authinfo.random != NULL)
1549 sctp_free_key(stcb->asoc.authinfo.random);
1550 stcb->asoc.authinfo.random = new_key;
1551 stcb->asoc.authinfo.random_len = random_len;
1552 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1553 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1555 /* negotiate what HMAC to use for the peer */
1556 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1557 stcb->asoc.local_hmacs);
1559 /* copy defaults from the endpoint */
1560 /* FIX ME: put in cookie? */
1561 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1562 /* copy out the shared key list (by reference) from the endpoint */
1563 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1564 &stcb->asoc.shared_keys);
1568 * compute and fill in the HMAC digest for a packet
1571 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1572 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1575 sctp_sharedkey_t *skey;
1578 if ((stcb == NULL) || (auth == NULL))
1581 /* zero the digest + chunk padding */
1582 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1583 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1585 /* is the desired key cached? */
1586 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1587 (stcb->asoc.authinfo.assoc_key == NULL)) {
1588 if (stcb->asoc.authinfo.assoc_key != NULL) {
1589 /* free the old cached key */
1590 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1592 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1593 /* the only way skey is NULL is if null key id 0 is used */
1598 /* compute a new assoc key and cache it */
1599 stcb->asoc.authinfo.assoc_key =
1600 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1601 stcb->asoc.authinfo.peer_random, key);
1602 stcb->asoc.authinfo.assoc_keyid = keyid;
1603 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1604 stcb->asoc.authinfo.assoc_keyid);
1606 if (SCTP_AUTH_DEBUG)
1607 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1611 /* set in the active key id */
1612 auth->shared_key_id = htons(keyid);
1614 /* compute and fill in the digest */
1615 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1616 m, auth_offset, auth->hmac);
1621 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1630 /* find the correct starting mbuf and offset (get start position) */
1632 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1633 m_offset -= SCTP_BUF_LEN(m_tmp);
1634 m_tmp = SCTP_BUF_NEXT(m_tmp);
1636 /* now use the rest of the mbuf chain */
1637 while ((m_tmp != NULL) && (size > 0)) {
1638 data = mtod(m_tmp, uint8_t *) + m_offset;
1639 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1640 bzero(data, SCTP_BUF_LEN(m_tmp));
1641 size -= SCTP_BUF_LEN(m_tmp);
1646 /* clear the offset since it's only for the first mbuf */
1648 m_tmp = SCTP_BUF_NEXT(m_tmp);
1653 * process the incoming Authentication chunk
1655 * -1 on any authentication error
1656 * 0 on authentication verification
1659 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1660 struct mbuf *m, uint32_t offset)
1663 uint16_t shared_key_id;
1665 sctp_sharedkey_t *skey;
1667 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1668 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1670 /* auth is checked for NULL by caller */
1671 chunklen = ntohs(auth->ch.chunk_length);
1672 if (chunklen < sizeof(*auth)) {
1673 SCTP_STAT_INCR(sctps_recvauthfailed);
1676 SCTP_STAT_INCR(sctps_recvauth);
1678 /* get the auth params */
1679 shared_key_id = ntohs(auth->shared_key_id);
1680 hmac_id = ntohs(auth->hmac_id);
1681 SCTPDBG(SCTP_DEBUG_AUTH1,
1682 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1683 shared_key_id, hmac_id);
1685 /* is the indicated HMAC supported? */
1686 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1688 struct sctp_auth_invalid_hmac *err;
1690 SCTP_STAT_INCR(sctps_recvivalhmacid);
1691 SCTPDBG(SCTP_DEBUG_AUTH1,
1692 "SCTP Auth: unsupported HMAC id %u\n",
1695 * report this in an Error Chunk: Unsupported HMAC
1698 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_NOWAIT,
1700 if (m_err != NULL) {
1701 /* pre-reserve some space */
1702 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1703 /* fill in the error */
1704 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1705 bzero(err, sizeof(*err));
1706 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1707 err->ph.param_length = htons(sizeof(*err));
1708 err->hmac_id = ntohs(hmac_id);
1709 SCTP_BUF_LEN(m_err) = sizeof(*err);
1711 sctp_queue_op_err(stcb, m_err);
1715 /* get the indicated shared key, if available */
1716 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1717 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1718 /* find the shared key on the assoc first */
1719 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1721 /* if the shared key isn't found, discard the chunk */
1723 SCTP_STAT_INCR(sctps_recvivalkeyid);
1724 SCTPDBG(SCTP_DEBUG_AUTH1,
1725 "SCTP Auth: unknown key id %u\n",
1729 /* generate a notification if this is a new key id */
1730 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1732 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1733 * shared_key_id, (void
1734 * *)stcb->asoc.authinfo.recv_keyid);
1736 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1737 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1738 SCTP_SO_NOT_LOCKED);
1739 /* compute a new recv assoc key and cache it */
1740 if (stcb->asoc.authinfo.recv_key != NULL)
1741 sctp_free_key(stcb->asoc.authinfo.recv_key);
1742 stcb->asoc.authinfo.recv_key =
1743 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1744 stcb->asoc.authinfo.peer_random, skey->key);
1745 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1747 if (SCTP_AUTH_DEBUG)
1748 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1751 /* validate the digest length */
1752 digestlen = sctp_get_hmac_digest_len(hmac_id);
1753 if (chunklen < (sizeof(*auth) + digestlen)) {
1754 /* invalid digest length */
1755 SCTP_STAT_INCR(sctps_recvauthfailed);
1756 SCTPDBG(SCTP_DEBUG_AUTH1,
1757 "SCTP Auth: chunk too short for HMAC\n");
1760 /* save a copy of the digest, zero the pseudo header, and validate */
1761 bcopy(auth->hmac, digest, digestlen);
1762 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1763 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1764 m, offset, computed_digest);
1766 /* compare the computed digest with the one in the AUTH chunk */
1767 if (memcmp(digest, computed_digest, digestlen) != 0) {
1768 SCTP_STAT_INCR(sctps_recvauthfailed);
1769 SCTPDBG(SCTP_DEBUG_AUTH1,
1770 "SCTP Auth: HMAC digest check failed\n");
1777 * Generate NOTIFICATION
1780 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1781 uint16_t keyid, uint16_t alt_keyid, int so_locked
1782 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1787 struct mbuf *m_notify;
1788 struct sctp_authkey_event *auth;
1789 struct sctp_queued_to_read *control;
1791 if ((stcb == NULL) ||
1792 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1793 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1794 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1796 /* If the socket is gone we are out of here */
1799 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1800 /* event not enabled */
1803 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1804 0, M_NOWAIT, 1, MT_HEADER);
1805 if (m_notify == NULL)
1809 SCTP_BUF_LEN(m_notify) = 0;
1810 auth = mtod(m_notify, struct sctp_authkey_event *);
1811 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1812 auth->auth_flags = 0;
1813 auth->auth_length = sizeof(*auth);
1814 auth->auth_keynumber = keyid;
1815 auth->auth_altkeynumber = alt_keyid;
1816 auth->auth_indication = indication;
1817 auth->auth_assoc_id = sctp_get_associd(stcb);
1819 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1820 SCTP_BUF_NEXT(m_notify) = NULL;
1822 /* append to socket */
1823 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1824 0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1825 if (control == NULL) {
1827 sctp_m_freem(m_notify);
1830 control->spec_flags = M_NOTIFICATION;
1831 control->length = SCTP_BUF_LEN(m_notify);
1832 /* not that we need this */
1833 control->tail_mbuf = m_notify;
1834 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1835 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1840 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1841 * Note: currently only used for INIT as INIT-ACK is handled inline
1842 * with sctp_load_addresses_from_init()
1845 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1847 struct sctp_paramhdr *phdr, parm_buf;
1848 uint16_t ptype, plen;
1849 int peer_supports_asconf = 0;
1850 int peer_supports_auth = 0;
1851 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1852 uint8_t saw_asconf = 0;
1853 uint8_t saw_asconf_ack = 0;
1855 /* go through each of the params. */
1856 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1858 ptype = ntohs(phdr->param_type);
1859 plen = ntohs(phdr->param_length);
1861 if (offset + plen > limit) {
1864 if (plen < sizeof(struct sctp_paramhdr)) {
1867 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1868 /* A supported extension chunk */
1869 struct sctp_supported_chunk_types_param *pr_supported;
1870 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1873 phdr = sctp_get_next_param(m, offset,
1874 (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
1878 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1879 num_ent = plen - sizeof(struct sctp_paramhdr);
1880 for (i = 0; i < num_ent; i++) {
1881 switch (pr_supported->chunk_types[i]) {
1883 case SCTP_ASCONF_ACK:
1884 peer_supports_asconf = 1;
1887 /* one we don't care about */
1891 } else if (ptype == SCTP_RANDOM) {
1893 /* enforce the random length */
1894 if (plen != (sizeof(struct sctp_auth_random) +
1895 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1896 SCTPDBG(SCTP_DEBUG_AUTH1,
1897 "SCTP: invalid RANDOM len\n");
1900 } else if (ptype == SCTP_HMAC_LIST) {
1901 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1902 struct sctp_auth_hmac_algo *hmacs;
1905 if (plen > sizeof(store))
1907 phdr = sctp_get_next_param(m, offset,
1908 (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
1911 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1912 num_hmacs = (plen - sizeof(*hmacs)) /
1913 sizeof(hmacs->hmac_ids[0]);
1914 /* validate the hmac list */
1915 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1916 SCTPDBG(SCTP_DEBUG_AUTH1,
1917 "SCTP: invalid HMAC param\n");
1921 } else if (ptype == SCTP_CHUNK_LIST) {
1923 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1925 /* did the peer send a non-empty chunk list? */
1926 struct sctp_auth_chunk_list *chunks = NULL;
1928 phdr = sctp_get_next_param(m, offset,
1929 (struct sctp_paramhdr *)chunks_store,
1930 min(plen, sizeof(chunks_store)));
1935 * Flip through the list and mark that the
1936 * peer supports asconf/asconf_ack.
1938 chunks = (struct sctp_auth_chunk_list *)phdr;
1939 num_chunks = plen - sizeof(*chunks);
1940 for (i = 0; i < num_chunks; i++) {
1941 /* record asconf/asconf-ack if listed */
1942 if (chunks->chunk_types[i] == SCTP_ASCONF)
1944 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1951 offset += SCTP_SIZE32(plen);
1952 if (offset >= limit) {
1955 phdr = sctp_get_next_param(m, offset, &parm_buf,
1958 /* validate authentication required parameters */
1959 if (got_random && got_hmacs) {
1960 peer_supports_auth = 1;
1962 peer_supports_auth = 0;
1964 if (!peer_supports_auth && got_chklist) {
1965 SCTPDBG(SCTP_DEBUG_AUTH1,
1966 "SCTP: peer sent chunk list w/o AUTH\n");
1969 if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf &&
1970 !peer_supports_auth) {
1971 SCTPDBG(SCTP_DEBUG_AUTH1,
1972 "SCTP: peer supports ASCONF but not AUTH\n");
1974 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1975 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1982 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1984 uint16_t chunks_len = 0;
1985 uint16_t hmacs_len = 0;
1986 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1987 sctp_key_t *new_key;
1990 /* initialize hmac list from endpoint */
1991 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1992 if (stcb->asoc.local_hmacs != NULL) {
1993 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1994 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1996 /* initialize auth chunks list from endpoint */
1997 stcb->asoc.local_auth_chunks =
1998 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1999 if (stcb->asoc.local_auth_chunks != NULL) {
2002 for (i = 0; i < 256; i++) {
2003 if (stcb->asoc.local_auth_chunks->chunks[i])
2007 /* copy defaults from the endpoint */
2008 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2010 /* copy out the shared key list (by reference) from the endpoint */
2011 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2012 &stcb->asoc.shared_keys);
2014 /* now set the concatenated key (random + chunks + hmacs) */
2015 /* key includes parameter headers */
2016 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2018 new_key = sctp_alloc_key(keylen);
2019 if (new_key != NULL) {
2020 struct sctp_paramhdr *ph;
2023 /* generate and copy in the RANDOM */
2024 ph = (struct sctp_paramhdr *)new_key->key;
2025 ph->param_type = htons(SCTP_RANDOM);
2026 plen = sizeof(*ph) + random_len;
2027 ph->param_length = htons(plen);
2028 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2031 /* append in the AUTH chunks */
2032 /* NOTE: currently we always have chunks to list */
2033 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2034 ph->param_type = htons(SCTP_CHUNK_LIST);
2035 plen = sizeof(*ph) + chunks_len;
2036 ph->param_length = htons(plen);
2037 keylen += sizeof(*ph);
2038 if (stcb->asoc.local_auth_chunks) {
2041 for (i = 0; i < 256; i++) {
2042 if (stcb->asoc.local_auth_chunks->chunks[i])
2043 new_key->key[keylen++] = i;
2046 /* append in the HMACs */
2047 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2048 ph->param_type = htons(SCTP_HMAC_LIST);
2049 plen = sizeof(*ph) + hmacs_len;
2050 ph->param_length = htons(plen);
2051 keylen += sizeof(*ph);
2052 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2053 new_key->key + keylen);
2055 if (stcb->asoc.authinfo.random != NULL)
2056 sctp_free_key(stcb->asoc.authinfo.random);
2057 stcb->asoc.authinfo.random = new_key;
2058 stcb->asoc.authinfo.random_len = random_len;