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.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD: head/sys/netinet/sctp_auth.c 271673 2014-09-16 14:20:33Z tuexen $");
38 #include <netinet/sctp_os.h>
39 #include <netinet/sctp.h>
40 #include <netinet/sctp_header.h>
41 #include <netinet/sctp_pcb.h>
42 #include <netinet/sctp_var.h>
43 #include <netinet/sctp_sysctl.h>
44 #include <netinet/sctputil.h>
45 #include <netinet/sctp_indata.h>
46 #include <netinet/sctp_output.h>
47 #include <netinet/sctp_auth.h>
50 #define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
51 #define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
52 #endif /* SCTP_DEBUG */
56 sctp_clear_chunklist(sctp_auth_chklist_t *chklist)
58 bzero(chklist, sizeof(*chklist));
59 /* chklist->num_chunks = 0; */
63 sctp_alloc_chunklist(void)
65 sctp_auth_chklist_t *chklist;
67 SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
69 if (chklist == NULL) {
70 SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
72 sctp_clear_chunklist(chklist);
78 sctp_free_chunklist(sctp_auth_chklist_t *list)
81 SCTP_FREE(list, SCTP_M_AUTH_CL);
85 sctp_copy_chunklist(sctp_auth_chklist_t *list)
87 sctp_auth_chklist_t *new_list;
93 new_list = sctp_alloc_chunklist();
97 bcopy(list, new_list, sizeof(*new_list));
104 * add a chunk to the required chunks list
107 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
112 /* is chunk restricted? */
113 if ((chunk == SCTP_INITIATION) ||
114 (chunk == SCTP_INITIATION_ACK) ||
115 (chunk == SCTP_SHUTDOWN_COMPLETE) ||
116 (chunk == SCTP_AUTHENTICATION)) {
119 if (list->chunks[chunk] == 0) {
120 list->chunks[chunk] = 1;
122 SCTPDBG(SCTP_DEBUG_AUTH1,
123 "SCTP: added chunk %u (0x%02x) to Auth list\n",
130 * delete a chunk from the required chunks list
133 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
138 if (list->chunks[chunk] == 1) {
139 list->chunks[chunk] = 0;
141 SCTPDBG(SCTP_DEBUG_AUTH1,
142 "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
149 sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list)
154 return (list->num_chunks);
158 * return the current number and list of required chunks caller must
159 * guarantee ptr has space for up to 256 bytes
162 sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
169 for (i = 0; i < 256; i++) {
170 if (list->chunks[i] != 0) {
179 sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
186 if (list->num_chunks <= 32) {
187 /* just list them, one byte each */
188 for (i = 0; i < 256; i++) {
189 if (list->chunks[i] != 0) {
197 /* pack into a 32 byte bitfield */
198 for (i = 0; i < 256; i++) {
199 if (list->chunks[i] != 0) {
202 ptr[index] |= (1 << offset);
211 sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks,
212 sctp_auth_chklist_t *list)
220 if (num_chunks <= 32) {
221 /* just pull them, one byte each */
222 for (i = 0; i < num_chunks; i++) {
223 (void)sctp_auth_add_chunk(*ptr++, list);
229 /* unpack from a 32 byte bitfield */
230 for (index = 0; index < 32; index++) {
231 for (offset = 0; offset < 8; offset++) {
232 if (ptr[index] & (1 << offset)) {
233 (void)sctp_auth_add_chunk((index * 8) + offset, list);
244 * allocate structure space for a key of length keylen
247 sctp_alloc_key(uint32_t keylen)
251 SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
253 if (new_key == NULL) {
257 new_key->keylen = keylen;
262 sctp_free_key(sctp_key_t *key)
265 SCTP_FREE(key,SCTP_M_AUTH_KY);
269 sctp_print_key(sctp_key_t *key, const char *str)
274 SCTP_PRINTF("%s: [Null key]\n", str);
277 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
279 for (i = 0; i < key->keylen; i++)
280 SCTP_PRINTF("%02x", key->key[i]);
283 SCTP_PRINTF("[Null key]\n");
288 sctp_show_key(sctp_key_t *key, const char *str)
293 SCTP_PRINTF("%s: [Null key]\n", str);
296 SCTP_PRINTF("%s: len %u, ", str, key->keylen);
298 for (i = 0; i < key->keylen; i++)
299 SCTP_PRINTF("%02x", key->key[i]);
302 SCTP_PRINTF("[Null key]\n");
307 sctp_get_keylen(sctp_key_t *key)
310 return (key->keylen);
316 * generate a new random key of length 'keylen'
319 sctp_generate_random_key(uint32_t keylen)
323 new_key = sctp_alloc_key(keylen);
324 if (new_key == NULL) {
328 SCTP_READ_RANDOM(new_key->key, keylen);
329 new_key->keylen = keylen;
334 sctp_set_key(uint8_t *key, uint32_t keylen)
338 new_key = sctp_alloc_key(keylen);
339 if (new_key == NULL) {
343 bcopy(key, new_key->key, keylen);
348 * given two keys of variable size, compute which key is "larger/smaller"
349 * returns: 1 if key1 > key2
354 sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2)
358 uint32_t key1len, key2len;
359 uint8_t *key_1, *key_2;
362 /* sanity/length check */
363 key1len = sctp_get_keylen(key1);
364 key2len = sctp_get_keylen(key2);
365 if ((key1len == 0) && (key2len == 0))
367 else if (key1len == 0)
369 else if (key2len == 0)
372 if (key1len < key2len) {
379 /* check for numeric equality */
380 for (i = 0; i < maxlen; i++) {
381 /* left-pad with zeros */
382 val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++);
383 val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++);
386 } else if (val1 < val2) {
390 /* keys are equal value, so check lengths */
391 if (key1len == key2len)
393 else if (key1len < key2len)
400 * generate the concatenated keying material based on the two keys and the
401 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
402 * order for concatenation
405 sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared)
411 keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
412 sctp_get_keylen(shared);
415 /* get space for the new key */
416 new_key = sctp_alloc_key(keylen);
417 if (new_key == NULL) {
421 new_key->keylen = keylen;
422 key_ptr = new_key->key;
424 /* all keys empty/null?! */
428 /* concatenate the keys */
429 if (sctp_compare_key(key1, key2) <= 0) {
430 /* key is shared + key1 + key2 */
431 if (sctp_get_keylen(shared)) {
432 bcopy(shared->key, key_ptr, shared->keylen);
433 key_ptr += shared->keylen;
435 if (sctp_get_keylen(key1)) {
436 bcopy(key1->key, key_ptr, key1->keylen);
437 key_ptr += key1->keylen;
439 if (sctp_get_keylen(key2)) {
440 bcopy(key2->key, key_ptr, key2->keylen);
443 /* key is shared + key2 + key1 */
444 if (sctp_get_keylen(shared)) {
445 bcopy(shared->key, key_ptr, shared->keylen);
446 key_ptr += shared->keylen;
448 if (sctp_get_keylen(key2)) {
449 bcopy(key2->key, key_ptr, key2->keylen);
450 key_ptr += key2->keylen;
452 if (sctp_get_keylen(key1)) {
453 bcopy(key1->key, key_ptr, key1->keylen);
461 sctp_alloc_sharedkey(void)
463 sctp_sharedkey_t *new_key;
465 SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
467 if (new_key == NULL) {
473 new_key->refcount = 1;
474 new_key->deactivated = 0;
479 sctp_free_sharedkey(sctp_sharedkey_t *skey)
484 if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
485 if (skey->key != NULL)
486 sctp_free_key(skey->key);
487 SCTP_FREE(skey, SCTP_M_AUTH_KY);
492 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
494 sctp_sharedkey_t *skey;
496 LIST_FOREACH(skey, shared_keys, next) {
497 if (skey->keyid == key_id)
504 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
505 sctp_sharedkey_t *new_skey)
507 sctp_sharedkey_t *skey;
509 if ((shared_keys == NULL) || (new_skey == NULL))
512 /* insert into an empty list? */
513 if (LIST_EMPTY(shared_keys)) {
514 LIST_INSERT_HEAD(shared_keys, new_skey, next);
517 /* insert into the existing list, ordered by key id */
518 LIST_FOREACH(skey, shared_keys, next) {
519 if (new_skey->keyid < skey->keyid) {
520 /* insert it before here */
521 LIST_INSERT_BEFORE(skey, new_skey, next);
523 } else if (new_skey->keyid == skey->keyid) {
524 /* replace the existing key */
525 /* verify this key *can* be replaced */
526 if ((skey->deactivated) && (skey->refcount > 1)) {
527 SCTPDBG(SCTP_DEBUG_AUTH1,
528 "can't replace shared key id %u\n",
532 SCTPDBG(SCTP_DEBUG_AUTH1,
533 "replacing shared key id %u\n",
535 LIST_INSERT_BEFORE(skey, new_skey, next);
536 LIST_REMOVE(skey, next);
537 sctp_free_sharedkey(skey);
540 if (LIST_NEXT(skey, next) == NULL) {
541 /* belongs at the end of the list */
542 LIST_INSERT_AFTER(skey, new_skey, next);
546 /* shouldn't reach here */
551 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
553 sctp_sharedkey_t *skey;
555 /* find the shared key */
556 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
558 /* bump the ref count */
560 atomic_add_int(&skey->refcount, 1);
561 SCTPDBG(SCTP_DEBUG_AUTH2,
562 "%s: stcb %p key %u refcount acquire to %d\n",
563 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
568 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
569 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
574 sctp_sharedkey_t *skey;
576 /* find the shared key */
577 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
579 /* decrement the ref count */
581 sctp_free_sharedkey(skey);
582 SCTPDBG(SCTP_DEBUG_AUTH2,
583 "%s: stcb %p key %u refcount release to %d\n",
584 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
586 /* see if a notification should be generated */
587 if ((skey->refcount <= 1) && (skey->deactivated)) {
588 /* notify ULP that key is no longer used */
589 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
590 key_id, 0, so_locked);
591 SCTPDBG(SCTP_DEBUG_AUTH2,
592 "%s: stcb %p key %u no longer used, %d\n",
593 __FUNCTION__, (void *)stcb, key_id, skey->refcount);
598 static sctp_sharedkey_t *
599 sctp_copy_sharedkey(const sctp_sharedkey_t *skey)
601 sctp_sharedkey_t *new_skey;
605 new_skey = sctp_alloc_sharedkey();
606 if (new_skey == NULL)
608 if (skey->key != NULL)
609 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
611 new_skey->key = NULL;
612 new_skey->keyid = skey->keyid;
617 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
619 sctp_sharedkey_t *skey, *new_skey;
622 if ((src == NULL) || (dest == NULL))
624 LIST_FOREACH(skey, src, next) {
625 new_skey = sctp_copy_sharedkey(skey);
626 if (new_skey != NULL) {
627 (void)sctp_insert_sharedkey(dest, new_skey);
636 sctp_alloc_hmaclist(uint16_t num_hmacs)
638 sctp_hmaclist_t *new_list;
641 alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
642 SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
644 if (new_list == NULL) {
648 new_list->max_algo = num_hmacs;
649 new_list->num_algo = 0;
654 sctp_free_hmaclist(sctp_hmaclist_t *list)
657 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 bcopy(&hmac_id, ptr, 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 bzero(new_authinfo, 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 bzero(ipad, blocklen);
978 bzero(opad, blocklen);
979 bcopy(key, ipad, keylen);
980 bcopy(key, opad, 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 bzero(ipad, blocklen);
1038 bzero(opad, blocklen);
1039 bcopy(key, ipad, keylen);
1040 bcopy(key, opad, 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 * verify the HMAC digest using the desired hash key, text, and HMAC
1085 * Returns -1 on error, 0 on success.
1088 sctp_verify_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
1089 uint8_t *text, uint32_t textlen,
1090 uint8_t *digest, uint32_t digestlen)
1093 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1095 /* sanity check the material and length */
1096 if ((key == NULL) || (keylen == 0) ||
1097 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1098 /* can't do HMAC with empty key or text or digest */
1101 len = sctp_get_hmac_digest_len(hmac_algo);
1102 if ((len == 0) || (digestlen != len))
1105 /* compute the expected hash */
1106 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1109 if (memcmp(digest, temp, digestlen) != 0)
1117 * computes the requested HMAC using a key struct (which may be modified if
1118 * the keylen exceeds the HMAC block len).
1121 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1122 uint32_t textlen, uint8_t *digest)
1126 sctp_hash_context_t ctx;
1127 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1130 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
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 bcopy(temp, key->key, key->keylen);
1150 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1156 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1157 uint32_t m_offset, uint8_t *digest)
1161 sctp_hash_context_t ctx;
1162 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1165 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1166 /* can't do HMAC with empty key or text or digest store */
1169 /* validate the hmac algo and get the digest length */
1170 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1174 /* hash the key if it is longer than the hash block size */
1175 blocklen = sctp_get_hmac_block_len(hmac_algo);
1176 if (key->keylen > blocklen) {
1177 sctp_hmac_init(hmac_algo, &ctx);
1178 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1179 sctp_hmac_final(hmac_algo, &ctx, temp);
1180 /* save the hashed key as the new key */
1181 key->keylen = digestlen;
1182 bcopy(temp, key->key, key->keylen);
1184 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1188 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1192 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1195 for (i = 0; i < list->num_algo; i++)
1196 if (list->hmac[i] == id)
1199 /* not in the list */
1205 * clear any cached key(s) if they match the given key id on an association.
1206 * the cached key(s) will be recomputed and re-cached at next use.
1207 * ASSUMES TCB_LOCK is already held
1210 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1215 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1216 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1217 stcb->asoc.authinfo.assoc_key = NULL;
1219 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1220 sctp_free_key(stcb->asoc.authinfo.recv_key);
1221 stcb->asoc.authinfo.recv_key = NULL;
1226 * clear any cached key(s) if they match the given key id for all assocs on
1228 * ASSUMES INP_WLOCK is already held
1231 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1233 struct sctp_tcb *stcb;
1238 /* clear the cached keys on all assocs on this instance */
1239 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1240 SCTP_TCB_LOCK(stcb);
1241 sctp_clear_cachedkeys(stcb, keyid);
1242 SCTP_TCB_UNLOCK(stcb);
1247 * delete a shared key from an association
1248 * ASSUMES TCB_LOCK is already held
1251 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1253 sctp_sharedkey_t *skey;
1258 /* is the keyid the assoc active sending key */
1259 if (keyid == stcb->asoc.authinfo.active_keyid)
1262 /* does the key exist? */
1263 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1267 /* are there other refcount holders on the key? */
1268 if (skey->refcount > 1)
1272 LIST_REMOVE(skey, next);
1273 sctp_free_sharedkey(skey); /* frees skey->key as well */
1275 /* clear any cached keys */
1276 sctp_clear_cachedkeys(stcb, keyid);
1281 * deletes a shared key from the endpoint
1282 * ASSUMES INP_WLOCK is already held
1285 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1287 sctp_sharedkey_t *skey;
1292 /* is the keyid the active sending key on the endpoint */
1293 if (keyid == inp->sctp_ep.default_keyid)
1296 /* does the key exist? */
1297 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1301 /* endpoint keys are not refcounted */
1304 LIST_REMOVE(skey, next);
1305 sctp_free_sharedkey(skey); /* frees skey->key as well */
1307 /* clear any cached keys */
1308 sctp_clear_cachedkeys_ep(inp, keyid);
1313 * set the active key on an association
1314 * ASSUMES TCB_LOCK is already held
1317 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1319 sctp_sharedkey_t *skey = NULL;
1321 /* find the key on the assoc */
1322 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1324 /* that key doesn't exist */
1327 if ((skey->deactivated) && (skey->refcount > 1)) {
1328 /* can't reactivate a deactivated key with other refcounts */
1332 /* set the (new) active key */
1333 stcb->asoc.authinfo.active_keyid = keyid;
1334 /* reset the deactivated flag */
1335 skey->deactivated = 0;
1341 * set the active key on an endpoint
1342 * ASSUMES INP_WLOCK is already held
1345 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1347 sctp_sharedkey_t *skey;
1350 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1352 /* that key doesn't exist */
1355 inp->sctp_ep.default_keyid = keyid;
1360 * deactivates a shared key from the association
1361 * ASSUMES INP_WLOCK is already held
1364 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1366 sctp_sharedkey_t *skey;
1371 /* is the keyid the assoc active sending key */
1372 if (keyid == stcb->asoc.authinfo.active_keyid)
1375 /* does the key exist? */
1376 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1380 /* are there other refcount holders on the key? */
1381 if (skey->refcount == 1) {
1382 /* no other users, send a notification for this key */
1383 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1387 /* mark the key as deactivated */
1388 skey->deactivated = 1;
1394 * deactivates a shared key from the endpoint
1395 * ASSUMES INP_WLOCK is already held
1398 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1400 sctp_sharedkey_t *skey;
1405 /* is the keyid the active sending key on the endpoint */
1406 if (keyid == inp->sctp_ep.default_keyid)
1409 /* does the key exist? */
1410 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1414 /* endpoint keys are not refcounted */
1417 LIST_REMOVE(skey, next);
1418 sctp_free_sharedkey(skey); /* frees skey->key as well */
1424 * get local authentication parameters from cookie (from INIT-ACK)
1427 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1428 uint32_t offset, uint32_t length)
1430 struct sctp_paramhdr *phdr, tmp_param;
1431 uint16_t plen, ptype;
1432 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1433 struct sctp_auth_random *p_random = NULL;
1434 uint16_t random_len = 0;
1435 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1436 struct sctp_auth_hmac_algo *hmacs = NULL;
1437 uint16_t hmacs_len = 0;
1438 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1439 struct sctp_auth_chunk_list *chunks = NULL;
1440 uint16_t num_chunks = 0;
1441 sctp_key_t *new_key;
1444 /* convert to upper bound */
1447 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1448 sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1449 while (phdr != NULL) {
1450 ptype = ntohs(phdr->param_type);
1451 plen = ntohs(phdr->param_length);
1453 if ((plen == 0) || (offset + plen > length))
1456 if (ptype == SCTP_RANDOM) {
1457 if (plen > sizeof(random_store))
1459 phdr = sctp_get_next_param(m, offset,
1460 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1463 /* save the random and length for the key */
1464 p_random = (struct sctp_auth_random *)phdr;
1465 random_len = plen - sizeof(*p_random);
1466 } else if (ptype == SCTP_HMAC_LIST) {
1470 if (plen > sizeof(hmacs_store))
1472 phdr = sctp_get_next_param(m, offset,
1473 (struct sctp_paramhdr *)hmacs_store, min(plen,sizeof(hmacs_store)));
1476 /* save the hmacs list and num for the key */
1477 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1478 hmacs_len = plen - sizeof(*hmacs);
1479 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1480 if (stcb->asoc.local_hmacs != NULL)
1481 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1482 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1483 if (stcb->asoc.local_hmacs != NULL) {
1484 for (i = 0; i < num_hmacs; i++) {
1485 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1486 ntohs(hmacs->hmac_ids[i]));
1489 } else if (ptype == SCTP_CHUNK_LIST) {
1492 if (plen > sizeof(chunks_store))
1494 phdr = sctp_get_next_param(m, offset,
1495 (struct sctp_paramhdr *)chunks_store, min(plen,sizeof(chunks_store)));
1498 chunks = (struct sctp_auth_chunk_list *)phdr;
1499 num_chunks = plen - sizeof(*chunks);
1500 /* save chunks list and num for the key */
1501 if (stcb->asoc.local_auth_chunks != NULL)
1502 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1504 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1505 for (i = 0; i < num_chunks; i++) {
1506 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1507 stcb->asoc.local_auth_chunks);
1510 /* get next parameter */
1511 offset += SCTP_SIZE32(plen);
1512 if (offset + sizeof(struct sctp_paramhdr) > length)
1514 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1515 (uint8_t *)&tmp_param);
1517 /* concatenate the full random key */
1518 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1519 if (chunks != NULL) {
1520 keylen += sizeof(*chunks) + num_chunks;
1522 new_key = sctp_alloc_key(keylen);
1523 if (new_key != NULL) {
1524 /* copy in the RANDOM */
1525 if (p_random != NULL) {
1526 keylen = sizeof(*p_random) + random_len;
1527 bcopy(p_random, new_key->key, keylen);
1529 /* append in the AUTH chunks */
1530 if (chunks != NULL) {
1531 bcopy(chunks, new_key->key + keylen,
1532 sizeof(*chunks) + num_chunks);
1533 keylen += sizeof(*chunks) + num_chunks;
1535 /* append in the HMACs */
1536 if (hmacs != NULL) {
1537 bcopy(hmacs, new_key->key + keylen,
1538 sizeof(*hmacs) + hmacs_len);
1541 if (stcb->asoc.authinfo.random != NULL)
1542 sctp_free_key(stcb->asoc.authinfo.random);
1543 stcb->asoc.authinfo.random = new_key;
1544 stcb->asoc.authinfo.random_len = random_len;
1545 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1546 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1548 /* negotiate what HMAC to use for the peer */
1549 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1550 stcb->asoc.local_hmacs);
1552 /* copy defaults from the endpoint */
1553 /* FIX ME: put in cookie? */
1554 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1555 /* copy out the shared key list (by reference) from the endpoint */
1556 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1557 &stcb->asoc.shared_keys);
1561 * compute and fill in the HMAC digest for a packet
1564 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1565 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1568 sctp_sharedkey_t *skey;
1571 if ((stcb == NULL) || (auth == NULL))
1574 /* zero the digest + chunk padding */
1575 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1576 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1578 /* is the desired key cached? */
1579 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1580 (stcb->asoc.authinfo.assoc_key == NULL)) {
1581 if (stcb->asoc.authinfo.assoc_key != NULL) {
1582 /* free the old cached key */
1583 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1585 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1586 /* the only way skey is NULL is if null key id 0 is used */
1591 /* compute a new assoc key and cache it */
1592 stcb->asoc.authinfo.assoc_key =
1593 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1594 stcb->asoc.authinfo.peer_random, key);
1595 stcb->asoc.authinfo.assoc_keyid = keyid;
1596 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1597 stcb->asoc.authinfo.assoc_keyid);
1599 if (SCTP_AUTH_DEBUG)
1600 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1605 /* set in the active key id */
1606 auth->shared_key_id = htons(keyid);
1608 /* compute and fill in the digest */
1609 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1610 m, auth_offset, auth->hmac);
1615 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1624 /* find the correct starting mbuf and offset (get start position) */
1626 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1627 m_offset -= SCTP_BUF_LEN(m_tmp);
1628 m_tmp = SCTP_BUF_NEXT(m_tmp);
1630 /* now use the rest of the mbuf chain */
1631 while ((m_tmp != NULL) && (size > 0)) {
1632 data = mtod(m_tmp, uint8_t *) + m_offset;
1633 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1634 bzero(data, SCTP_BUF_LEN(m_tmp));
1635 size -= SCTP_BUF_LEN(m_tmp);
1640 /* clear the offset since it's only for the first mbuf */
1642 m_tmp = SCTP_BUF_NEXT(m_tmp);
1647 * process the incoming Authentication chunk
1649 * -1 on any authentication error
1650 * 0 on authentication verification
1653 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1654 struct mbuf *m, uint32_t offset)
1657 uint16_t shared_key_id;
1659 sctp_sharedkey_t *skey;
1661 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1662 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1664 /* auth is checked for NULL by caller */
1665 chunklen = ntohs(auth->ch.chunk_length);
1666 if (chunklen < sizeof(*auth)) {
1667 SCTP_STAT_INCR(sctps_recvauthfailed);
1670 SCTP_STAT_INCR(sctps_recvauth);
1672 /* get the auth params */
1673 shared_key_id = ntohs(auth->shared_key_id);
1674 hmac_id = ntohs(auth->hmac_id);
1675 SCTPDBG(SCTP_DEBUG_AUTH1,
1676 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1677 shared_key_id, hmac_id);
1679 /* is the indicated HMAC supported? */
1680 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1682 struct sctp_auth_invalid_hmac *err;
1684 SCTP_STAT_INCR(sctps_recvivalhmacid);
1685 SCTPDBG(SCTP_DEBUG_AUTH1,
1686 "SCTP Auth: unsupported HMAC id %u\n",
1689 * report this in an Error Chunk: Unsupported HMAC
1692 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_NOWAIT,
1694 if (m_err != NULL) {
1695 /* pre-reserve some space */
1696 SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1697 /* fill in the error */
1698 err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1699 bzero(err, sizeof(*err));
1700 err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1701 err->ph.param_length = htons(sizeof(*err));
1702 err->hmac_id = ntohs(hmac_id);
1703 SCTP_BUF_LEN(m_err) = sizeof(*err);
1705 sctp_queue_op_err(stcb, m_err);
1709 /* get the indicated shared key, if available */
1710 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1711 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1712 /* find the shared key on the assoc first */
1713 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1715 /* if the shared key isn't found, discard the chunk */
1717 SCTP_STAT_INCR(sctps_recvivalkeyid);
1718 SCTPDBG(SCTP_DEBUG_AUTH1,
1719 "SCTP Auth: unknown key id %u\n",
1723 /* generate a notification if this is a new key id */
1724 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1726 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1727 * shared_key_id, (void
1728 * *)stcb->asoc.authinfo.recv_keyid);
1730 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1731 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1732 SCTP_SO_NOT_LOCKED);
1733 /* compute a new recv assoc key and cache it */
1734 if (stcb->asoc.authinfo.recv_key != NULL)
1735 sctp_free_key(stcb->asoc.authinfo.recv_key);
1736 stcb->asoc.authinfo.recv_key =
1737 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1738 stcb->asoc.authinfo.peer_random, skey->key);
1739 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1741 if (SCTP_AUTH_DEBUG)
1742 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1745 /* validate the digest length */
1746 digestlen = sctp_get_hmac_digest_len(hmac_id);
1747 if (chunklen < (sizeof(*auth) + digestlen)) {
1748 /* invalid digest length */
1749 SCTP_STAT_INCR(sctps_recvauthfailed);
1750 SCTPDBG(SCTP_DEBUG_AUTH1,
1751 "SCTP Auth: chunk too short for HMAC\n");
1754 /* save a copy of the digest, zero the pseudo header, and validate */
1755 bcopy(auth->hmac, digest, digestlen);
1756 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1757 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1758 m, offset, computed_digest);
1760 /* compare the computed digest with the one in the AUTH chunk */
1761 if (memcmp(digest, computed_digest, digestlen) != 0) {
1762 SCTP_STAT_INCR(sctps_recvauthfailed);
1763 SCTPDBG(SCTP_DEBUG_AUTH1,
1764 "SCTP Auth: HMAC digest check failed\n");
1771 * Generate NOTIFICATION
1774 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1775 uint16_t keyid, uint16_t alt_keyid, int so_locked
1776 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1781 struct mbuf *m_notify;
1782 struct sctp_authkey_event *auth;
1783 struct sctp_queued_to_read *control;
1785 if ((stcb == NULL) ||
1786 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1787 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1788 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1790 /* If the socket is gone we are out of here */
1794 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1795 /* event not enabled */
1798 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1799 0, M_NOWAIT, 1, MT_HEADER);
1800 if (m_notify == NULL)
1804 SCTP_BUF_LEN(m_notify) = 0;
1805 auth = mtod(m_notify, struct sctp_authkey_event *);
1806 memset(auth, 0, sizeof(struct sctp_authkey_event));
1807 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1808 auth->auth_flags = 0;
1809 auth->auth_length = sizeof(*auth);
1810 auth->auth_keynumber = keyid;
1811 auth->auth_altkeynumber = alt_keyid;
1812 auth->auth_indication = indication;
1813 auth->auth_assoc_id = sctp_get_associd(stcb);
1815 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1816 SCTP_BUF_NEXT(m_notify) = NULL;
1818 /* append to socket */
1819 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1820 0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1821 if (control == NULL) {
1823 sctp_m_freem(m_notify);
1826 control->spec_flags = M_NOTIFICATION;
1827 control->length = SCTP_BUF_LEN(m_notify);
1828 /* not that we need this */
1829 control->tail_mbuf = m_notify;
1830 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1831 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1836 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1837 * Note: currently only used for INIT as INIT-ACK is handled inline
1838 * with sctp_load_addresses_from_init()
1841 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1843 struct sctp_paramhdr *phdr, parm_buf;
1844 uint16_t ptype, plen;
1845 int peer_supports_asconf = 0;
1846 int peer_supports_auth = 0;
1847 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1848 uint8_t saw_asconf = 0;
1849 uint8_t saw_asconf_ack = 0;
1851 /* go through each of the params. */
1852 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1854 ptype = ntohs(phdr->param_type);
1855 plen = ntohs(phdr->param_length);
1857 if (offset + plen > limit) {
1860 if (plen < sizeof(struct sctp_paramhdr)) {
1863 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1864 /* A supported extension chunk */
1865 struct sctp_supported_chunk_types_param *pr_supported;
1866 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1869 phdr = sctp_get_next_param(m, offset,
1870 (struct sctp_paramhdr *)&local_store, min(plen,sizeof(local_store)));
1874 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1875 num_ent = plen - sizeof(struct sctp_paramhdr);
1876 for (i = 0; i < num_ent; i++) {
1877 switch (pr_supported->chunk_types[i]) {
1879 case SCTP_ASCONF_ACK:
1880 peer_supports_asconf = 1;
1883 /* one we don't care about */
1887 } else if (ptype == SCTP_RANDOM) {
1889 /* enforce the random length */
1890 if (plen != (sizeof(struct sctp_auth_random) +
1891 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1892 SCTPDBG(SCTP_DEBUG_AUTH1,
1893 "SCTP: invalid RANDOM len\n");
1896 } else if (ptype == SCTP_HMAC_LIST) {
1897 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1898 struct sctp_auth_hmac_algo *hmacs;
1901 if (plen > sizeof(store))
1903 phdr = sctp_get_next_param(m, offset,
1904 (struct sctp_paramhdr *)store, min(plen,sizeof(store)));
1907 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1908 num_hmacs = (plen - sizeof(*hmacs)) /
1909 sizeof(hmacs->hmac_ids[0]);
1910 /* validate the hmac list */
1911 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1912 SCTPDBG(SCTP_DEBUG_AUTH1,
1913 "SCTP: invalid HMAC param\n");
1917 } else if (ptype == SCTP_CHUNK_LIST) {
1919 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1920 /* did the peer send a non-empty chunk list? */
1921 struct sctp_auth_chunk_list *chunks = NULL;
1922 phdr = sctp_get_next_param(m, offset,
1923 (struct sctp_paramhdr *)chunks_store,
1924 min(plen,sizeof(chunks_store)));
1929 * Flip through the list and mark that the
1930 * peer supports asconf/asconf_ack.
1932 chunks = (struct sctp_auth_chunk_list *)phdr;
1933 num_chunks = plen - sizeof(*chunks);
1934 for (i = 0; i < num_chunks; i++) {
1935 /* record asconf/asconf-ack if listed */
1936 if (chunks->chunk_types[i] == SCTP_ASCONF)
1938 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1946 offset += SCTP_SIZE32(plen);
1947 if (offset >= limit) {
1950 phdr = sctp_get_next_param(m, offset, &parm_buf,
1953 /* validate authentication required parameters */
1954 if (got_random && got_hmacs) {
1955 peer_supports_auth = 1;
1957 peer_supports_auth = 0;
1959 if (!peer_supports_auth && got_chklist) {
1960 SCTPDBG(SCTP_DEBUG_AUTH1,
1961 "SCTP: peer sent chunk list w/o AUTH\n");
1964 if (peer_supports_asconf && !peer_supports_auth) {
1965 SCTPDBG(SCTP_DEBUG_AUTH1,
1966 "SCTP: peer supports ASCONF but not AUTH\n");
1968 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1969 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1976 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1978 uint16_t chunks_len = 0;
1979 uint16_t hmacs_len = 0;
1980 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1981 sctp_key_t *new_key;
1984 /* initialize hmac list from endpoint */
1985 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1986 if (stcb->asoc.local_hmacs != NULL) {
1987 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1988 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1990 /* initialize auth chunks list from endpoint */
1991 stcb->asoc.local_auth_chunks =
1992 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1993 if (stcb->asoc.local_auth_chunks != NULL) {
1995 for (i = 0; i < 256; i++) {
1996 if (stcb->asoc.local_auth_chunks->chunks[i])
2000 /* copy defaults from the endpoint */
2001 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2003 /* copy out the shared key list (by reference) from the endpoint */
2004 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2005 &stcb->asoc.shared_keys);
2007 /* now set the concatenated key (random + chunks + hmacs) */
2008 /* key includes parameter headers */
2009 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2011 new_key = sctp_alloc_key(keylen);
2012 if (new_key != NULL) {
2013 struct sctp_paramhdr *ph;
2015 /* generate and copy in the RANDOM */
2016 ph = (struct sctp_paramhdr *)new_key->key;
2017 ph->param_type = htons(SCTP_RANDOM);
2018 plen = sizeof(*ph) + random_len;
2019 ph->param_length = htons(plen);
2020 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2023 /* append in the AUTH chunks */
2024 /* NOTE: currently we always have chunks to list */
2025 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2026 ph->param_type = htons(SCTP_CHUNK_LIST);
2027 plen = sizeof(*ph) + chunks_len;
2028 ph->param_length = htons(plen);
2029 keylen += sizeof(*ph);
2030 if (stcb->asoc.local_auth_chunks) {
2032 for (i = 0; i < 256; i++) {
2033 if (stcb->asoc.local_auth_chunks->chunks[i])
2034 new_key->key[keylen++] = i;
2038 /* append in the HMACs */
2039 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2040 ph->param_type = htons(SCTP_HMAC_LIST);
2041 plen = sizeof(*ph) + hmacs_len;
2042 ph->param_length = htons(plen);
2043 keylen += sizeof(*ph);
2044 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2045 new_key->key + keylen);
2047 if (stcb->asoc.authinfo.random != NULL)
2048 sctp_free_key(stcb->asoc.authinfo.random);
2049 stcb->asoc.authinfo.random = new_key;
2050 stcb->asoc.authinfo.random_len = random_len;
2054 #ifdef SCTP_HMAC_TEST
2056 * HMAC and key concatenation tests
2059 sctp_print_digest(uint8_t *digest, uint32_t digestlen, const char *str)
2063 SCTP_PRINTF("\n%s: 0x", str);
2067 for (i = 0; i < digestlen; i++)
2068 SCTP_PRINTF("%02x", digest[i]);
2072 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t *key,
2073 uint32_t keylen, uint8_t *text, uint32_t textlen,
2074 uint8_t *digest, uint32_t digestlen)
2076 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2078 SCTP_PRINTF("\n%s:", str);
2079 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2080 sctp_print_digest(digest, digestlen, "Expected digest");
2081 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2082 if (memcmp(digest, computed_digest, digestlen) != 0) {
2083 SCTP_PRINTF("\nFAILED");
2086 SCTP_PRINTF("\nPASSED");
2093 * RFC 2202: HMAC-SHA1 test cases
2096 sctp_test_hmac_sha1(void)
2103 uint32_t digestlen = 20;
2108 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2112 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
2115 memset(key, 0x0b, keylen);
2117 strcpy(text, "Hi There");
2118 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2119 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2120 text, textlen, digest, digestlen) < 0)
2127 * data = "what do ya want for nothing?"
2129 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2132 strcpy(key, "Jefe");
2134 strcpy(text, "what do ya want for nothing?");
2135 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2136 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2137 text, textlen, digest, digestlen) < 0)
2142 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2144 * data = 0xdd repeated 50 times
2146 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2149 memset(key, 0xaa, keylen);
2151 memset(text, 0xdd, textlen);
2152 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2153 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2154 text, textlen, digest, digestlen) < 0)
2159 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2161 * data = 0xcd repeated 50 times
2163 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2166 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);
2168 memset(text, 0xcd, textlen);
2169 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2170 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2171 text, textlen, digest, digestlen) < 0)
2176 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2178 * data = "Test With Truncation"
2180 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2181 * digest-96 = 0x4c1a03424b55e07fe7f27be1
2184 memset(key, 0x0c, keylen);
2186 strcpy(text, "Test With Truncation");
2187 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2188 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2189 text, textlen, digest, digestlen) < 0)
2194 * key = 0xaa repeated 80 times
2196 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2198 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2201 memset(key, 0xaa, keylen);
2203 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2204 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2205 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2206 text, textlen, digest, digestlen) < 0)
2211 * key = 0xaa repeated 80 times
2213 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2215 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2218 memset(key, 0xaa, keylen);
2220 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2221 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2222 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2223 text, textlen, digest, digestlen) < 0)
2226 /* done with all tests */
2228 SCTP_PRINTF("\nSHA1 test results: %d cases failed", failed);
2230 SCTP_PRINTF("\nSHA1 test results: all test cases passed");
2234 * test assoc key concatenation
2237 sctp_test_key_concatenation(sctp_key_t *key1, sctp_key_t *key2,
2238 sctp_key_t *expected_key)
2243 sctp_show_key(key1, "\nkey1");
2244 sctp_show_key(key2, "\nkey2");
2245 key = sctp_compute_hashkey(key1, key2, NULL);
2246 sctp_show_key(expected_key, "\nExpected");
2247 sctp_show_key(key, "\nComputed");
2248 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2249 SCTP_PRINTF("\nFAILED");
2252 SCTP_PRINTF("\nPASSED");
2255 sctp_free_key(key1);
2256 sctp_free_key(key2);
2257 sctp_free_key(expected_key);
2264 sctp_test_authkey(void)
2266 sctp_key_t *key1, *key2, *expected_key;
2270 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2271 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2272 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2273 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2277 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2278 key2 = sctp_set_key("\x02", 1);
2279 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2280 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2284 key1 = sctp_set_key("\x01", 1);
2285 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2286 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2287 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2291 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2292 key2 = sctp_set_key("\x01", 1);
2293 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2294 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2298 key1 = sctp_set_key("\x01", 1);
2299 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2300 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2301 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2305 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2306 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2307 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);
2308 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2312 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2313 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2314 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);
2315 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2318 /* done with all tests */
2320 SCTP_PRINTF("\nKey concatenation test results: %d cases failed", failed);
2322 SCTP_PRINTF("\nKey concatenation test results: all test cases passed");
2326 #if defined(STANDALONE_HMAC_TEST)
2330 sctp_test_hmac_sha1();
2331 sctp_test_authkey();
2334 #endif /* STANDALONE_HMAC_TEST */
2336 #endif /* SCTP_HMAC_TEST */