xref: /freebsd/sys/netinet/sctp_auth.c (revision eda14cbc264d6969b02f2b1994cef11148e914f1)
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
5  * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
6  * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
11  * a) Redistributions of source code must retain the above copyright notice,
12  *    this list of conditions and the following disclaimer.
13  *
14  * b) Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the distribution.
17  *
18  * c) Neither the name of Cisco Systems, Inc. nor the names of its
19  *    contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
24  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
26  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
32  * THE POSSIBILITY OF SUCH DAMAGE.
33  */
34 
35 #include <sys/cdefs.h>
36 __FBSDID("$FreeBSD$");
37 
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>
48 
49 #ifdef SCTP_DEBUG
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 */
53 
54 
55 void
56 sctp_clear_chunklist(sctp_auth_chklist_t *chklist)
57 {
58 	memset(chklist, 0, sizeof(*chklist));
59 	/* chklist->num_chunks = 0; */
60 }
61 
62 sctp_auth_chklist_t *
63 sctp_alloc_chunklist(void)
64 {
65 	sctp_auth_chklist_t *chklist;
66 
67 	SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
68 	    SCTP_M_AUTH_CL);
69 	if (chklist == NULL) {
70 		SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
71 	} else {
72 		sctp_clear_chunklist(chklist);
73 	}
74 	return (chklist);
75 }
76 
77 void
78 sctp_free_chunklist(sctp_auth_chklist_t *list)
79 {
80 	if (list != NULL)
81 		SCTP_FREE(list, SCTP_M_AUTH_CL);
82 }
83 
84 sctp_auth_chklist_t *
85 sctp_copy_chunklist(sctp_auth_chklist_t *list)
86 {
87 	sctp_auth_chklist_t *new_list;
88 
89 	if (list == NULL)
90 		return (NULL);
91 
92 	/* get a new list */
93 	new_list = sctp_alloc_chunklist();
94 	if (new_list == NULL)
95 		return (NULL);
96 	/* copy it */
97 	memcpy(new_list, list, sizeof(*new_list));
98 
99 	return (new_list);
100 }
101 
102 
103 /*
104  * add a chunk to the required chunks list
105  */
106 int
107 sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
108 {
109 	if (list == NULL)
110 		return (-1);
111 
112 	/* is chunk restricted? */
113 	if ((chunk == SCTP_INITIATION) ||
114 	    (chunk == SCTP_INITIATION_ACK) ||
115 	    (chunk == SCTP_SHUTDOWN_COMPLETE) ||
116 	    (chunk == SCTP_AUTHENTICATION)) {
117 		return (-1);
118 	}
119 	if (list->chunks[chunk] == 0) {
120 		list->chunks[chunk] = 1;
121 		list->num_chunks++;
122 		SCTPDBG(SCTP_DEBUG_AUTH1,
123 		    "SCTP: added chunk %u (0x%02x) to Auth list\n",
124 		    chunk, chunk);
125 	}
126 	return (0);
127 }
128 
129 /*
130  * delete a chunk from the required chunks list
131  */
132 int
133 sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t *list)
134 {
135 	if (list == NULL)
136 		return (-1);
137 
138 	if (list->chunks[chunk] == 1) {
139 		list->chunks[chunk] = 0;
140 		list->num_chunks--;
141 		SCTPDBG(SCTP_DEBUG_AUTH1,
142 		    "SCTP: deleted chunk %u (0x%02x) from Auth list\n",
143 		    chunk, chunk);
144 	}
145 	return (0);
146 }
147 
148 size_t
149 sctp_auth_get_chklist_size(const sctp_auth_chklist_t *list)
150 {
151 	if (list == NULL)
152 		return (0);
153 	else
154 		return (list->num_chunks);
155 }
156 
157 /*
158  * return the current number and list of required chunks caller must
159  * guarantee ptr has space for up to 256 bytes
160  */
161 int
162 sctp_serialize_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
163 {
164 	int i, count = 0;
165 
166 	if (list == NULL)
167 		return (0);
168 
169 	for (i = 0; i < 256; i++) {
170 		if (list->chunks[i] != 0) {
171 			*ptr++ = i;
172 			count++;
173 		}
174 	}
175 	return (count);
176 }
177 
178 int
179 sctp_pack_auth_chunks(const sctp_auth_chklist_t *list, uint8_t *ptr)
180 {
181 	int i, size = 0;
182 
183 	if (list == NULL)
184 		return (0);
185 
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) {
190 				*ptr++ = i;
191 				size++;
192 			}
193 		}
194 	} else {
195 		int index, offset;
196 
197 		/* pack into a 32 byte bitfield */
198 		for (i = 0; i < 256; i++) {
199 			if (list->chunks[i] != 0) {
200 				index = i / 8;
201 				offset = i % 8;
202 				ptr[index] |= (1 << offset);
203 			}
204 		}
205 		size = 32;
206 	}
207 	return (size);
208 }
209 
210 int
211 sctp_unpack_auth_chunks(const uint8_t *ptr, uint8_t num_chunks,
212     sctp_auth_chklist_t *list)
213 {
214 	int i;
215 	int size;
216 
217 	if (list == NULL)
218 		return (0);
219 
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);
224 		}
225 		size = num_chunks;
226 	} else {
227 		int index, offset;
228 
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);
234 				}
235 			}
236 		}
237 		size = 32;
238 	}
239 	return (size);
240 }
241 
242 
243 /*
244  * allocate structure space for a key of length keylen
245  */
246 sctp_key_t *
247 sctp_alloc_key(uint32_t keylen)
248 {
249 	sctp_key_t *new_key;
250 
251 	SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
252 	    SCTP_M_AUTH_KY);
253 	if (new_key == NULL) {
254 		/* out of memory */
255 		return (NULL);
256 	}
257 	new_key->keylen = keylen;
258 	return (new_key);
259 }
260 
261 void
262 sctp_free_key(sctp_key_t *key)
263 {
264 	if (key != NULL)
265 		SCTP_FREE(key, SCTP_M_AUTH_KY);
266 }
267 
268 void
269 sctp_print_key(sctp_key_t *key, const char *str)
270 {
271 	uint32_t i;
272 
273 	if (key == NULL) {
274 		SCTP_PRINTF("%s: [Null key]\n", str);
275 		return;
276 	}
277 	SCTP_PRINTF("%s: len %u, ", str, key->keylen);
278 	if (key->keylen) {
279 		for (i = 0; i < key->keylen; i++)
280 			SCTP_PRINTF("%02x", key->key[i]);
281 		SCTP_PRINTF("\n");
282 	} else {
283 		SCTP_PRINTF("[Null key]\n");
284 	}
285 }
286 
287 void
288 sctp_show_key(sctp_key_t *key, const char *str)
289 {
290 	uint32_t i;
291 
292 	if (key == NULL) {
293 		SCTP_PRINTF("%s: [Null key]\n", str);
294 		return;
295 	}
296 	SCTP_PRINTF("%s: len %u, ", str, key->keylen);
297 	if (key->keylen) {
298 		for (i = 0; i < key->keylen; i++)
299 			SCTP_PRINTF("%02x", key->key[i]);
300 		SCTP_PRINTF("\n");
301 	} else {
302 		SCTP_PRINTF("[Null key]\n");
303 	}
304 }
305 
306 static uint32_t
307 sctp_get_keylen(sctp_key_t *key)
308 {
309 	if (key != NULL)
310 		return (key->keylen);
311 	else
312 		return (0);
313 }
314 
315 /*
316  * generate a new random key of length 'keylen'
317  */
318 sctp_key_t *
319 sctp_generate_random_key(uint32_t keylen)
320 {
321 	sctp_key_t *new_key;
322 
323 	new_key = sctp_alloc_key(keylen);
324 	if (new_key == NULL) {
325 		/* out of memory */
326 		return (NULL);
327 	}
328 	SCTP_READ_RANDOM(new_key->key, keylen);
329 	new_key->keylen = keylen;
330 	return (new_key);
331 }
332 
333 sctp_key_t *
334 sctp_set_key(uint8_t *key, uint32_t keylen)
335 {
336 	sctp_key_t *new_key;
337 
338 	new_key = sctp_alloc_key(keylen);
339 	if (new_key == NULL) {
340 		/* out of memory */
341 		return (NULL);
342 	}
343 	memcpy(new_key->key, key, keylen);
344 	return (new_key);
345 }
346 
347 /*-
348  * given two keys of variable size, compute which key is "larger/smaller"
349  * returns:  1 if key1 > key2
350  *          -1 if key1 < key2
351  *           0 if key1 = key2
352  */
353 static int
354 sctp_compare_key(sctp_key_t *key1, sctp_key_t *key2)
355 {
356 	uint32_t maxlen;
357 	uint32_t i;
358 	uint32_t key1len, key2len;
359 	uint8_t *key_1, *key_2;
360 	uint8_t val1, val2;
361 
362 	/* sanity/length check */
363 	key1len = sctp_get_keylen(key1);
364 	key2len = sctp_get_keylen(key2);
365 	if ((key1len == 0) && (key2len == 0))
366 		return (0);
367 	else if (key1len == 0)
368 		return (-1);
369 	else if (key2len == 0)
370 		return (1);
371 
372 	if (key1len < key2len) {
373 		maxlen = key2len;
374 	} else {
375 		maxlen = key1len;
376 	}
377 	key_1 = key1->key;
378 	key_2 = key2->key;
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++);
384 		if (val1 > val2) {
385 			return (1);
386 		} else if (val1 < val2) {
387 			return (-1);
388 		}
389 	}
390 	/* keys are equal value, so check lengths */
391 	if (key1len == key2len)
392 		return (0);
393 	else if (key1len < key2len)
394 		return (-1);
395 	else
396 		return (1);
397 }
398 
399 /*
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
403  */
404 sctp_key_t *
405 sctp_compute_hashkey(sctp_key_t *key1, sctp_key_t *key2, sctp_key_t *shared)
406 {
407 	uint32_t keylen;
408 	sctp_key_t *new_key;
409 	uint8_t *key_ptr;
410 
411 	keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
412 	    sctp_get_keylen(shared);
413 
414 	if (keylen > 0) {
415 		/* get space for the new key */
416 		new_key = sctp_alloc_key(keylen);
417 		if (new_key == NULL) {
418 			/* out of memory */
419 			return (NULL);
420 		}
421 		new_key->keylen = keylen;
422 		key_ptr = new_key->key;
423 	} else {
424 		/* all keys empty/null?! */
425 		return (NULL);
426 	}
427 
428 	/* concatenate the keys */
429 	if (sctp_compare_key(key1, key2) <= 0) {
430 		/* key is shared + key1 + key2 */
431 		if (sctp_get_keylen(shared)) {
432 			memcpy(key_ptr, shared->key, shared->keylen);
433 			key_ptr += shared->keylen;
434 		}
435 		if (sctp_get_keylen(key1)) {
436 			memcpy(key_ptr, key1->key, key1->keylen);
437 			key_ptr += key1->keylen;
438 		}
439 		if (sctp_get_keylen(key2)) {
440 			memcpy(key_ptr, key2->key, key2->keylen);
441 		}
442 	} else {
443 		/* key is shared + key2 + key1 */
444 		if (sctp_get_keylen(shared)) {
445 			memcpy(key_ptr, shared->key, shared->keylen);
446 			key_ptr += shared->keylen;
447 		}
448 		if (sctp_get_keylen(key2)) {
449 			memcpy(key_ptr, key2->key, key2->keylen);
450 			key_ptr += key2->keylen;
451 		}
452 		if (sctp_get_keylen(key1)) {
453 			memcpy(key_ptr, key1->key, key1->keylen);
454 		}
455 	}
456 	return (new_key);
457 }
458 
459 
460 sctp_sharedkey_t *
461 sctp_alloc_sharedkey(void)
462 {
463 	sctp_sharedkey_t *new_key;
464 
465 	SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
466 	    SCTP_M_AUTH_KY);
467 	if (new_key == NULL) {
468 		/* out of memory */
469 		return (NULL);
470 	}
471 	new_key->keyid = 0;
472 	new_key->key = NULL;
473 	new_key->refcount = 1;
474 	new_key->deactivated = 0;
475 	return (new_key);
476 }
477 
478 void
479 sctp_free_sharedkey(sctp_sharedkey_t *skey)
480 {
481 	if (skey == NULL)
482 		return;
483 
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);
488 	}
489 }
490 
491 sctp_sharedkey_t *
492 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
493 {
494 	sctp_sharedkey_t *skey;
495 
496 	LIST_FOREACH(skey, shared_keys, next) {
497 		if (skey->keyid == key_id)
498 			return (skey);
499 	}
500 	return (NULL);
501 }
502 
503 int
504 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
505     sctp_sharedkey_t *new_skey)
506 {
507 	sctp_sharedkey_t *skey;
508 
509 	if ((shared_keys == NULL) || (new_skey == NULL))
510 		return (EINVAL);
511 
512 	/* insert into an empty list? */
513 	if (LIST_EMPTY(shared_keys)) {
514 		LIST_INSERT_HEAD(shared_keys, new_skey, next);
515 		return (0);
516 	}
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);
522 			return (0);
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",
529 				    new_skey->keyid);
530 				return (EBUSY);
531 			}
532 			SCTPDBG(SCTP_DEBUG_AUTH1,
533 			    "replacing shared key id %u\n",
534 			    new_skey->keyid);
535 			LIST_INSERT_BEFORE(skey, new_skey, next);
536 			LIST_REMOVE(skey, next);
537 			sctp_free_sharedkey(skey);
538 			return (0);
539 		}
540 		if (LIST_NEXT(skey, next) == NULL) {
541 			/* belongs at the end of the list */
542 			LIST_INSERT_AFTER(skey, new_skey, next);
543 			return (0);
544 		}
545 	}
546 	/* shouldn't reach here */
547 	return (EINVAL);
548 }
549 
550 void
551 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
552 {
553 	sctp_sharedkey_t *skey;
554 
555 	/* find the shared key */
556 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
557 
558 	/* bump the ref count */
559 	if (skey) {
560 		atomic_add_int(&skey->refcount, 1);
561 		SCTPDBG(SCTP_DEBUG_AUTH2,
562 		    "%s: stcb %p key %u refcount acquire to %d\n",
563 		    __func__, (void *)stcb, key_id, skey->refcount);
564 	}
565 }
566 
567 void
568 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked)
569 {
570 	sctp_sharedkey_t *skey;
571 
572 	/* find the shared key */
573 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
574 
575 	/* decrement the ref count */
576 	if (skey) {
577 		SCTPDBG(SCTP_DEBUG_AUTH2,
578 		    "%s: stcb %p key %u refcount release to %d\n",
579 		    __func__, (void *)stcb, key_id, skey->refcount);
580 
581 		/* see if a notification should be generated */
582 		if ((skey->refcount <= 2) && (skey->deactivated)) {
583 			/* notify ULP that key is no longer used */
584 			sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
585 			    key_id, 0, so_locked);
586 			SCTPDBG(SCTP_DEBUG_AUTH2,
587 			    "%s: stcb %p key %u no longer used, %d\n",
588 			    __func__, (void *)stcb, key_id, skey->refcount);
589 		}
590 		sctp_free_sharedkey(skey);
591 	}
592 }
593 
594 static sctp_sharedkey_t *
595 sctp_copy_sharedkey(const sctp_sharedkey_t *skey)
596 {
597 	sctp_sharedkey_t *new_skey;
598 
599 	if (skey == NULL)
600 		return (NULL);
601 	new_skey = sctp_alloc_sharedkey();
602 	if (new_skey == NULL)
603 		return (NULL);
604 	if (skey->key != NULL)
605 		new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
606 	else
607 		new_skey->key = NULL;
608 	new_skey->keyid = skey->keyid;
609 	return (new_skey);
610 }
611 
612 int
613 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
614 {
615 	sctp_sharedkey_t *skey, *new_skey;
616 	int count = 0;
617 
618 	if ((src == NULL) || (dest == NULL))
619 		return (0);
620 	LIST_FOREACH(skey, src, next) {
621 		new_skey = sctp_copy_sharedkey(skey);
622 		if (new_skey != NULL) {
623 			if (sctp_insert_sharedkey(dest, new_skey)) {
624 				sctp_free_sharedkey(new_skey);
625 			} else {
626 				count++;
627 			}
628 		}
629 	}
630 	return (count);
631 }
632 
633 
634 sctp_hmaclist_t *
635 sctp_alloc_hmaclist(uint16_t num_hmacs)
636 {
637 	sctp_hmaclist_t *new_list;
638 	int alloc_size;
639 
640 	alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
641 	SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
642 	    SCTP_M_AUTH_HL);
643 	if (new_list == NULL) {
644 		/* out of memory */
645 		return (NULL);
646 	}
647 	new_list->max_algo = num_hmacs;
648 	new_list->num_algo = 0;
649 	return (new_list);
650 }
651 
652 void
653 sctp_free_hmaclist(sctp_hmaclist_t *list)
654 {
655 	if (list != NULL) {
656 		SCTP_FREE(list, SCTP_M_AUTH_HL);
657 	}
658 }
659 
660 int
661 sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id)
662 {
663 	int i;
664 
665 	if (list == NULL)
666 		return (-1);
667 	if (list->num_algo == list->max_algo) {
668 		SCTPDBG(SCTP_DEBUG_AUTH1,
669 		    "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
670 		return (-1);
671 	}
672 	if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
673 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
674 		return (-1);
675 	}
676 	/* Now is it already in the list */
677 	for (i = 0; i < list->num_algo; i++) {
678 		if (list->hmac[i] == hmac_id) {
679 			/* already in list */
680 			return (-1);
681 		}
682 	}
683 	SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
684 	list->hmac[list->num_algo++] = hmac_id;
685 	return (0);
686 }
687 
688 sctp_hmaclist_t *
689 sctp_copy_hmaclist(sctp_hmaclist_t *list)
690 {
691 	sctp_hmaclist_t *new_list;
692 	int i;
693 
694 	if (list == NULL)
695 		return (NULL);
696 	/* get a new list */
697 	new_list = sctp_alloc_hmaclist(list->max_algo);
698 	if (new_list == NULL)
699 		return (NULL);
700 	/* copy it */
701 	new_list->max_algo = list->max_algo;
702 	new_list->num_algo = list->num_algo;
703 	for (i = 0; i < list->num_algo; i++)
704 		new_list->hmac[i] = list->hmac[i];
705 	return (new_list);
706 }
707 
708 sctp_hmaclist_t *
709 sctp_default_supported_hmaclist(void)
710 {
711 	sctp_hmaclist_t *new_list;
712 
713 	new_list = sctp_alloc_hmaclist(2);
714 	if (new_list == NULL)
715 		return (NULL);
716 	/* We prefer SHA256, so list it first */
717 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
718 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
719 	return (new_list);
720 }
721 
722 /*-
723  * HMAC algos are listed in priority/preference order
724  * find the best HMAC id to use for the peer based on local support
725  */
726 uint16_t
727 sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local)
728 {
729 	int i, j;
730 
731 	if ((local == NULL) || (peer == NULL))
732 		return (SCTP_AUTH_HMAC_ID_RSVD);
733 
734 	for (i = 0; i < peer->num_algo; i++) {
735 		for (j = 0; j < local->num_algo; j++) {
736 			if (peer->hmac[i] == local->hmac[j]) {
737 				/* found the "best" one */
738 				SCTPDBG(SCTP_DEBUG_AUTH1,
739 				    "SCTP: negotiated peer HMAC id %u\n",
740 				    peer->hmac[i]);
741 				return (peer->hmac[i]);
742 			}
743 		}
744 	}
745 	/* didn't find one! */
746 	return (SCTP_AUTH_HMAC_ID_RSVD);
747 }
748 
749 /*-
750  * serialize the HMAC algo list and return space used
751  * caller must guarantee ptr has appropriate space
752  */
753 int
754 sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr)
755 {
756 	int i;
757 	uint16_t hmac_id;
758 
759 	if (list == NULL)
760 		return (0);
761 
762 	for (i = 0; i < list->num_algo; i++) {
763 		hmac_id = htons(list->hmac[i]);
764 		memcpy(ptr, &hmac_id, sizeof(hmac_id));
765 		ptr += sizeof(hmac_id);
766 	}
767 	return (list->num_algo * sizeof(hmac_id));
768 }
769 
770 int
771 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
772 {
773 	uint32_t i;
774 
775 	for (i = 0; i < num_hmacs; i++) {
776 		if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
777 			return (0);
778 		}
779 	}
780 	return (-1);
781 }
782 
783 sctp_authinfo_t *
784 sctp_alloc_authinfo(void)
785 {
786 	sctp_authinfo_t *new_authinfo;
787 
788 	SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
789 	    SCTP_M_AUTH_IF);
790 
791 	if (new_authinfo == NULL) {
792 		/* out of memory */
793 		return (NULL);
794 	}
795 	memset(new_authinfo, 0, sizeof(*new_authinfo));
796 	return (new_authinfo);
797 }
798 
799 void
800 sctp_free_authinfo(sctp_authinfo_t *authinfo)
801 {
802 	if (authinfo == NULL)
803 		return;
804 
805 	if (authinfo->random != NULL)
806 		sctp_free_key(authinfo->random);
807 	if (authinfo->peer_random != NULL)
808 		sctp_free_key(authinfo->peer_random);
809 	if (authinfo->assoc_key != NULL)
810 		sctp_free_key(authinfo->assoc_key);
811 	if (authinfo->recv_key != NULL)
812 		sctp_free_key(authinfo->recv_key);
813 
814 	/* We are NOT dynamically allocating authinfo's right now... */
815 	/* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
816 }
817 
818 
819 uint32_t
820 sctp_get_auth_chunk_len(uint16_t hmac_algo)
821 {
822 	int size;
823 
824 	size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
825 	return (SCTP_SIZE32(size));
826 }
827 
828 uint32_t
829 sctp_get_hmac_digest_len(uint16_t hmac_algo)
830 {
831 	switch (hmac_algo) {
832 	case SCTP_AUTH_HMAC_ID_SHA1:
833 		return (SCTP_AUTH_DIGEST_LEN_SHA1);
834 	case SCTP_AUTH_HMAC_ID_SHA256:
835 		return (SCTP_AUTH_DIGEST_LEN_SHA256);
836 	default:
837 		/* unknown HMAC algorithm: can't do anything */
838 		return (0);
839 	}			/* end switch */
840 }
841 
842 static inline int
843 sctp_get_hmac_block_len(uint16_t hmac_algo)
844 {
845 	switch (hmac_algo) {
846 	case SCTP_AUTH_HMAC_ID_SHA1:
847 		return (64);
848 	case SCTP_AUTH_HMAC_ID_SHA256:
849 		return (64);
850 	case SCTP_AUTH_HMAC_ID_RSVD:
851 	default:
852 		/* unknown HMAC algorithm: can't do anything */
853 		return (0);
854 	}			/* end switch */
855 }
856 
857 static void
858 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx)
859 {
860 	switch (hmac_algo) {
861 	case SCTP_AUTH_HMAC_ID_SHA1:
862 		SCTP_SHA1_INIT(&ctx->sha1);
863 		break;
864 	case SCTP_AUTH_HMAC_ID_SHA256:
865 		SCTP_SHA256_INIT(&ctx->sha256);
866 		break;
867 	case SCTP_AUTH_HMAC_ID_RSVD:
868 	default:
869 		/* unknown HMAC algorithm: can't do anything */
870 		return;
871 	}			/* end switch */
872 }
873 
874 static void
875 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx,
876     uint8_t *text, uint32_t textlen)
877 {
878 	switch (hmac_algo) {
879 	case SCTP_AUTH_HMAC_ID_SHA1:
880 		SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
881 		break;
882 	case SCTP_AUTH_HMAC_ID_SHA256:
883 		SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
884 		break;
885 	case SCTP_AUTH_HMAC_ID_RSVD:
886 	default:
887 		/* unknown HMAC algorithm: can't do anything */
888 		return;
889 	}			/* end switch */
890 }
891 
892 static void
893 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx,
894     uint8_t *digest)
895 {
896 	switch (hmac_algo) {
897 	case SCTP_AUTH_HMAC_ID_SHA1:
898 		SCTP_SHA1_FINAL(digest, &ctx->sha1);
899 		break;
900 	case SCTP_AUTH_HMAC_ID_SHA256:
901 		SCTP_SHA256_FINAL(digest, &ctx->sha256);
902 		break;
903 	case SCTP_AUTH_HMAC_ID_RSVD:
904 	default:
905 		/* unknown HMAC algorithm: can't do anything */
906 		return;
907 	}			/* end switch */
908 }
909 
910 /*-
911  * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
912  *
913  * Compute the HMAC digest using the desired hash key, text, and HMAC
914  * algorithm.  Resulting digest is placed in 'digest' and digest length
915  * is returned, if the HMAC was performed.
916  *
917  * WARNING: it is up to the caller to supply sufficient space to hold the
918  * resultant digest.
919  */
920 uint32_t
921 sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
922     uint8_t *text, uint32_t textlen, uint8_t *digest)
923 {
924 	uint32_t digestlen;
925 	uint32_t blocklen;
926 	sctp_hash_context_t ctx;
927 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
928 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
929 	uint32_t i;
930 
931 	/* sanity check the material and length */
932 	if ((key == NULL) || (keylen == 0) || (text == NULL) ||
933 	    (textlen == 0) || (digest == NULL)) {
934 		/* can't do HMAC with empty key or text or digest store */
935 		return (0);
936 	}
937 	/* validate the hmac algo and get the digest length */
938 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
939 	if (digestlen == 0)
940 		return (0);
941 
942 	/* hash the key if it is longer than the hash block size */
943 	blocklen = sctp_get_hmac_block_len(hmac_algo);
944 	if (keylen > blocklen) {
945 		sctp_hmac_init(hmac_algo, &ctx);
946 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
947 		sctp_hmac_final(hmac_algo, &ctx, temp);
948 		/* set the hashed key as the key */
949 		keylen = digestlen;
950 		key = temp;
951 	}
952 	/* initialize the inner/outer pads with the key and "append" zeroes */
953 	memset(ipad, 0, blocklen);
954 	memset(opad, 0, blocklen);
955 	memcpy(ipad, key, keylen);
956 	memcpy(opad, key, keylen);
957 
958 	/* XOR the key with ipad and opad values */
959 	for (i = 0; i < blocklen; i++) {
960 		ipad[i] ^= 0x36;
961 		opad[i] ^= 0x5c;
962 	}
963 
964 	/* perform inner hash */
965 	sctp_hmac_init(hmac_algo, &ctx);
966 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
967 	sctp_hmac_update(hmac_algo, &ctx, text, textlen);
968 	sctp_hmac_final(hmac_algo, &ctx, temp);
969 
970 	/* perform outer hash */
971 	sctp_hmac_init(hmac_algo, &ctx);
972 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
973 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
974 	sctp_hmac_final(hmac_algo, &ctx, digest);
975 
976 	return (digestlen);
977 }
978 
979 /* mbuf version */
980 uint32_t
981 sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
982     struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer)
983 {
984 	uint32_t digestlen;
985 	uint32_t blocklen;
986 	sctp_hash_context_t ctx;
987 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
988 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
989 	uint32_t i;
990 	struct mbuf *m_tmp;
991 
992 	/* sanity check the material and length */
993 	if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
994 		/* can't do HMAC with empty key or text or digest store */
995 		return (0);
996 	}
997 	/* validate the hmac algo and get the digest length */
998 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
999 	if (digestlen == 0)
1000 		return (0);
1001 
1002 	/* hash the key if it is longer than the hash block size */
1003 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1004 	if (keylen > blocklen) {
1005 		sctp_hmac_init(hmac_algo, &ctx);
1006 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1007 		sctp_hmac_final(hmac_algo, &ctx, temp);
1008 		/* set the hashed key as the key */
1009 		keylen = digestlen;
1010 		key = temp;
1011 	}
1012 	/* initialize the inner/outer pads with the key and "append" zeroes */
1013 	memset(ipad, 0, blocklen);
1014 	memset(opad, 0, blocklen);
1015 	memcpy(ipad, key, keylen);
1016 	memcpy(opad, key, keylen);
1017 
1018 	/* XOR the key with ipad and opad values */
1019 	for (i = 0; i < blocklen; i++) {
1020 		ipad[i] ^= 0x36;
1021 		opad[i] ^= 0x5c;
1022 	}
1023 
1024 	/* perform inner hash */
1025 	sctp_hmac_init(hmac_algo, &ctx);
1026 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1027 	/* find the correct starting mbuf and offset (get start of text) */
1028 	m_tmp = m;
1029 	while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1030 		m_offset -= SCTP_BUF_LEN(m_tmp);
1031 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1032 	}
1033 	/* now use the rest of the mbuf chain for the text */
1034 	while (m_tmp != NULL) {
1035 		if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1036 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1037 			    SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1038 		} else {
1039 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1040 			    SCTP_BUF_LEN(m_tmp) - m_offset);
1041 		}
1042 
1043 		/* clear the offset since it's only for the first mbuf */
1044 		m_offset = 0;
1045 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1046 	}
1047 	sctp_hmac_final(hmac_algo, &ctx, temp);
1048 
1049 	/* perform outer hash */
1050 	sctp_hmac_init(hmac_algo, &ctx);
1051 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1052 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1053 	sctp_hmac_final(hmac_algo, &ctx, digest);
1054 
1055 	return (digestlen);
1056 }
1057 
1058 /*
1059  * computes the requested HMAC using a key struct (which may be modified if
1060  * the keylen exceeds the HMAC block len).
1061  */
1062 uint32_t
1063 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1064     uint32_t textlen, uint8_t *digest)
1065 {
1066 	uint32_t digestlen;
1067 	uint32_t blocklen;
1068 	sctp_hash_context_t ctx;
1069 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1070 
1071 	/* sanity check */
1072 	if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1073 	    (digest == NULL)) {
1074 		/* can't do HMAC with empty key or text or digest store */
1075 		return (0);
1076 	}
1077 	/* validate the hmac algo and get the digest length */
1078 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1079 	if (digestlen == 0)
1080 		return (0);
1081 
1082 	/* hash the key if it is longer than the hash block size */
1083 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1084 	if (key->keylen > blocklen) {
1085 		sctp_hmac_init(hmac_algo, &ctx);
1086 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1087 		sctp_hmac_final(hmac_algo, &ctx, temp);
1088 		/* save the hashed key as the new key */
1089 		key->keylen = digestlen;
1090 		memcpy(key->key, temp, key->keylen);
1091 	}
1092 	return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1093 	    digest));
1094 }
1095 
1096 /* mbuf version */
1097 uint32_t
1098 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1099     uint32_t m_offset, uint8_t *digest)
1100 {
1101 	uint32_t digestlen;
1102 	uint32_t blocklen;
1103 	sctp_hash_context_t ctx;
1104 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1105 
1106 	/* sanity check */
1107 	if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1108 		/* can't do HMAC with empty key or text or digest store */
1109 		return (0);
1110 	}
1111 	/* validate the hmac algo and get the digest length */
1112 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1113 	if (digestlen == 0)
1114 		return (0);
1115 
1116 	/* hash the key if it is longer than the hash block size */
1117 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1118 	if (key->keylen > blocklen) {
1119 		sctp_hmac_init(hmac_algo, &ctx);
1120 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1121 		sctp_hmac_final(hmac_algo, &ctx, temp);
1122 		/* save the hashed key as the new key */
1123 		key->keylen = digestlen;
1124 		memcpy(key->key, temp, key->keylen);
1125 	}
1126 	return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1127 }
1128 
1129 int
1130 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1131 {
1132 	int i;
1133 
1134 	if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1135 		return (0);
1136 
1137 	for (i = 0; i < list->num_algo; i++)
1138 		if (list->hmac[i] == id)
1139 			return (1);
1140 
1141 	/* not in the list */
1142 	return (0);
1143 }
1144 
1145 
1146 /*-
1147  * clear any cached key(s) if they match the given key id on an association.
1148  * the cached key(s) will be recomputed and re-cached at next use.
1149  * ASSUMES TCB_LOCK is already held
1150  */
1151 void
1152 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1153 {
1154 	if (stcb == NULL)
1155 		return;
1156 
1157 	if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1158 		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1159 		stcb->asoc.authinfo.assoc_key = NULL;
1160 	}
1161 	if (keyid == stcb->asoc.authinfo.recv_keyid) {
1162 		sctp_free_key(stcb->asoc.authinfo.recv_key);
1163 		stcb->asoc.authinfo.recv_key = NULL;
1164 	}
1165 }
1166 
1167 /*-
1168  * clear any cached key(s) if they match the given key id for all assocs on
1169  * an endpoint.
1170  * ASSUMES INP_WLOCK is already held
1171  */
1172 void
1173 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1174 {
1175 	struct sctp_tcb *stcb;
1176 
1177 	if (inp == NULL)
1178 		return;
1179 
1180 	/* clear the cached keys on all assocs on this instance */
1181 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1182 		SCTP_TCB_LOCK(stcb);
1183 		sctp_clear_cachedkeys(stcb, keyid);
1184 		SCTP_TCB_UNLOCK(stcb);
1185 	}
1186 }
1187 
1188 /*-
1189  * delete a shared key from an association
1190  * ASSUMES TCB_LOCK is already held
1191  */
1192 int
1193 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1194 {
1195 	sctp_sharedkey_t *skey;
1196 
1197 	if (stcb == NULL)
1198 		return (-1);
1199 
1200 	/* is the keyid the assoc active sending key */
1201 	if (keyid == stcb->asoc.authinfo.active_keyid)
1202 		return (-1);
1203 
1204 	/* does the key exist? */
1205 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1206 	if (skey == NULL)
1207 		return (-1);
1208 
1209 	/* are there other refcount holders on the key? */
1210 	if (skey->refcount > 1)
1211 		return (-1);
1212 
1213 	/* remove it */
1214 	LIST_REMOVE(skey, next);
1215 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1216 
1217 	/* clear any cached keys */
1218 	sctp_clear_cachedkeys(stcb, keyid);
1219 	return (0);
1220 }
1221 
1222 /*-
1223  * deletes a shared key from the endpoint
1224  * ASSUMES INP_WLOCK is already held
1225  */
1226 int
1227 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1228 {
1229 	sctp_sharedkey_t *skey;
1230 
1231 	if (inp == NULL)
1232 		return (-1);
1233 
1234 	/* is the keyid the active sending key on the endpoint */
1235 	if (keyid == inp->sctp_ep.default_keyid)
1236 		return (-1);
1237 
1238 	/* does the key exist? */
1239 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1240 	if (skey == NULL)
1241 		return (-1);
1242 
1243 	/* endpoint keys are not refcounted */
1244 
1245 	/* remove it */
1246 	LIST_REMOVE(skey, next);
1247 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1248 
1249 	/* clear any cached keys */
1250 	sctp_clear_cachedkeys_ep(inp, keyid);
1251 	return (0);
1252 }
1253 
1254 /*-
1255  * set the active key on an association
1256  * ASSUMES TCB_LOCK is already held
1257  */
1258 int
1259 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1260 {
1261 	sctp_sharedkey_t *skey = NULL;
1262 
1263 	/* find the key on the assoc */
1264 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1265 	if (skey == NULL) {
1266 		/* that key doesn't exist */
1267 		return (-1);
1268 	}
1269 	if ((skey->deactivated) && (skey->refcount > 1)) {
1270 		/* can't reactivate a deactivated key with other refcounts */
1271 		return (-1);
1272 	}
1273 
1274 	/* set the (new) active key */
1275 	stcb->asoc.authinfo.active_keyid = keyid;
1276 	/* reset the deactivated flag */
1277 	skey->deactivated = 0;
1278 
1279 	return (0);
1280 }
1281 
1282 /*-
1283  * set the active key on an endpoint
1284  * ASSUMES INP_WLOCK is already held
1285  */
1286 int
1287 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1288 {
1289 	sctp_sharedkey_t *skey;
1290 
1291 	/* find the key */
1292 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1293 	if (skey == NULL) {
1294 		/* that key doesn't exist */
1295 		return (-1);
1296 	}
1297 	inp->sctp_ep.default_keyid = keyid;
1298 	return (0);
1299 }
1300 
1301 /*-
1302  * deactivates a shared key from the association
1303  * ASSUMES INP_WLOCK is already held
1304  */
1305 int
1306 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1307 {
1308 	sctp_sharedkey_t *skey;
1309 
1310 	if (stcb == NULL)
1311 		return (-1);
1312 
1313 	/* is the keyid the assoc active sending key */
1314 	if (keyid == stcb->asoc.authinfo.active_keyid)
1315 		return (-1);
1316 
1317 	/* does the key exist? */
1318 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1319 	if (skey == NULL)
1320 		return (-1);
1321 
1322 	/* are there other refcount holders on the key? */
1323 	if (skey->refcount == 1) {
1324 		/* no other users, send a notification for this key */
1325 		sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1326 		    SCTP_SO_LOCKED);
1327 	}
1328 
1329 	/* mark the key as deactivated */
1330 	skey->deactivated = 1;
1331 
1332 	return (0);
1333 }
1334 
1335 /*-
1336  * deactivates a shared key from the endpoint
1337  * ASSUMES INP_WLOCK is already held
1338  */
1339 int
1340 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1341 {
1342 	sctp_sharedkey_t *skey;
1343 
1344 	if (inp == NULL)
1345 		return (-1);
1346 
1347 	/* is the keyid the active sending key on the endpoint */
1348 	if (keyid == inp->sctp_ep.default_keyid)
1349 		return (-1);
1350 
1351 	/* does the key exist? */
1352 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1353 	if (skey == NULL)
1354 		return (-1);
1355 
1356 	/* endpoint keys are not refcounted */
1357 
1358 	/* remove it */
1359 	LIST_REMOVE(skey, next);
1360 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1361 
1362 	return (0);
1363 }
1364 
1365 /*
1366  * get local authentication parameters from cookie (from INIT-ACK)
1367  */
1368 void
1369 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1370     uint32_t offset, uint32_t length)
1371 {
1372 	struct sctp_paramhdr *phdr, tmp_param;
1373 	uint16_t plen, ptype;
1374 	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1375 	struct sctp_auth_random *p_random = NULL;
1376 	uint16_t random_len = 0;
1377 	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1378 	struct sctp_auth_hmac_algo *hmacs = NULL;
1379 	uint16_t hmacs_len = 0;
1380 	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1381 	struct sctp_auth_chunk_list *chunks = NULL;
1382 	uint16_t num_chunks = 0;
1383 	sctp_key_t *new_key;
1384 	uint32_t keylen;
1385 
1386 	/* convert to upper bound */
1387 	length += offset;
1388 
1389 	phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1390 	    sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1391 	while (phdr != NULL) {
1392 		ptype = ntohs(phdr->param_type);
1393 		plen = ntohs(phdr->param_length);
1394 
1395 		if ((plen < sizeof(struct sctp_paramhdr)) ||
1396 		    (offset + plen > length))
1397 			break;
1398 
1399 		if (ptype == SCTP_RANDOM) {
1400 			if (plen > sizeof(random_store))
1401 				break;
1402 			phdr = sctp_get_next_param(m, offset,
1403 			    (struct sctp_paramhdr *)random_store, plen);
1404 			if (phdr == NULL)
1405 				return;
1406 			/* save the random and length for the key */
1407 			p_random = (struct sctp_auth_random *)phdr;
1408 			random_len = plen - sizeof(*p_random);
1409 		} else if (ptype == SCTP_HMAC_LIST) {
1410 			uint16_t num_hmacs;
1411 			uint16_t i;
1412 
1413 			if (plen > sizeof(hmacs_store))
1414 				break;
1415 			phdr = sctp_get_next_param(m, offset,
1416 			    (struct sctp_paramhdr *)hmacs_store, plen);
1417 			if (phdr == NULL)
1418 				return;
1419 			/* save the hmacs list and num for the key */
1420 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1421 			hmacs_len = plen - sizeof(*hmacs);
1422 			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1423 			if (stcb->asoc.local_hmacs != NULL)
1424 				sctp_free_hmaclist(stcb->asoc.local_hmacs);
1425 			stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1426 			if (stcb->asoc.local_hmacs != NULL) {
1427 				for (i = 0; i < num_hmacs; i++) {
1428 					(void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1429 					    ntohs(hmacs->hmac_ids[i]));
1430 				}
1431 			}
1432 		} else if (ptype == SCTP_CHUNK_LIST) {
1433 			int i;
1434 
1435 			if (plen > sizeof(chunks_store))
1436 				break;
1437 			phdr = sctp_get_next_param(m, offset,
1438 			    (struct sctp_paramhdr *)chunks_store, plen);
1439 			if (phdr == NULL)
1440 				return;
1441 			chunks = (struct sctp_auth_chunk_list *)phdr;
1442 			num_chunks = plen - sizeof(*chunks);
1443 			/* save chunks list and num for the key */
1444 			if (stcb->asoc.local_auth_chunks != NULL)
1445 				sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1446 			else
1447 				stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1448 			for (i = 0; i < num_chunks; i++) {
1449 				(void)sctp_auth_add_chunk(chunks->chunk_types[i],
1450 				    stcb->asoc.local_auth_chunks);
1451 			}
1452 		}
1453 		/* get next parameter */
1454 		offset += SCTP_SIZE32(plen);
1455 		if (offset + sizeof(struct sctp_paramhdr) > length)
1456 			break;
1457 		phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1458 		    (uint8_t *)&tmp_param);
1459 	}
1460 	/* concatenate the full random key */
1461 	keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1462 	if (chunks != NULL) {
1463 		keylen += sizeof(*chunks) + num_chunks;
1464 	}
1465 	new_key = sctp_alloc_key(keylen);
1466 	if (new_key != NULL) {
1467 		/* copy in the RANDOM */
1468 		if (p_random != NULL) {
1469 			keylen = sizeof(*p_random) + random_len;
1470 			memcpy(new_key->key, p_random, keylen);
1471 		} else {
1472 			keylen = 0;
1473 		}
1474 		/* append in the AUTH chunks */
1475 		if (chunks != NULL) {
1476 			memcpy(new_key->key + keylen, chunks,
1477 			    sizeof(*chunks) + num_chunks);
1478 			keylen += sizeof(*chunks) + num_chunks;
1479 		}
1480 		/* append in the HMACs */
1481 		if (hmacs != NULL) {
1482 			memcpy(new_key->key + keylen, hmacs,
1483 			    sizeof(*hmacs) + hmacs_len);
1484 		}
1485 	}
1486 	if (stcb->asoc.authinfo.random != NULL)
1487 		sctp_free_key(stcb->asoc.authinfo.random);
1488 	stcb->asoc.authinfo.random = new_key;
1489 	stcb->asoc.authinfo.random_len = random_len;
1490 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1491 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1492 
1493 	/* negotiate what HMAC to use for the peer */
1494 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1495 	    stcb->asoc.local_hmacs);
1496 
1497 	/* copy defaults from the endpoint */
1498 	/* FIX ME: put in cookie? */
1499 	stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1500 	/* copy out the shared key list (by reference) from the endpoint */
1501 	(void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1502 	    &stcb->asoc.shared_keys);
1503 }
1504 
1505 /*
1506  * compute and fill in the HMAC digest for a packet
1507  */
1508 void
1509 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1510     struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1511 {
1512 	uint32_t digestlen;
1513 	sctp_sharedkey_t *skey;
1514 	sctp_key_t *key;
1515 
1516 	if ((stcb == NULL) || (auth == NULL))
1517 		return;
1518 
1519 	/* zero the digest + chunk padding */
1520 	digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1521 	memset(auth->hmac, 0, SCTP_SIZE32(digestlen));
1522 
1523 	/* is the desired key cached? */
1524 	if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1525 	    (stcb->asoc.authinfo.assoc_key == NULL)) {
1526 		if (stcb->asoc.authinfo.assoc_key != NULL) {
1527 			/* free the old cached key */
1528 			sctp_free_key(stcb->asoc.authinfo.assoc_key);
1529 		}
1530 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1531 		/* the only way skey is NULL is if null key id 0 is used */
1532 		if (skey != NULL)
1533 			key = skey->key;
1534 		else
1535 			key = NULL;
1536 		/* compute a new assoc key and cache it */
1537 		stcb->asoc.authinfo.assoc_key =
1538 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1539 		    stcb->asoc.authinfo.peer_random, key);
1540 		stcb->asoc.authinfo.assoc_keyid = keyid;
1541 		SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1542 		    stcb->asoc.authinfo.assoc_keyid);
1543 #ifdef SCTP_DEBUG
1544 		if (SCTP_AUTH_DEBUG)
1545 			sctp_print_key(stcb->asoc.authinfo.assoc_key,
1546 			    "Assoc Key");
1547 #endif
1548 	}
1549 
1550 	/* set in the active key id */
1551 	auth->shared_key_id = htons(keyid);
1552 
1553 	/* compute and fill in the digest */
1554 	(void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1555 	    m, auth_offset, auth->hmac);
1556 }
1557 
1558 
1559 static void
1560 sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1561 {
1562 	struct mbuf *m_tmp;
1563 	uint8_t *data;
1564 
1565 	/* sanity check */
1566 	if (m == NULL)
1567 		return;
1568 
1569 	/* find the correct starting mbuf and offset (get start position) */
1570 	m_tmp = m;
1571 	while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1572 		m_offset -= SCTP_BUF_LEN(m_tmp);
1573 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1574 	}
1575 	/* now use the rest of the mbuf chain */
1576 	while ((m_tmp != NULL) && (size > 0)) {
1577 		data = mtod(m_tmp, uint8_t *)+m_offset;
1578 		if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) {
1579 			memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset);
1580 			size -= SCTP_BUF_LEN(m_tmp) - m_offset;
1581 		} else {
1582 			memset(data, 0, size);
1583 			size = 0;
1584 		}
1585 		/* clear the offset since it's only for the first mbuf */
1586 		m_offset = 0;
1587 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1588 	}
1589 }
1590 
1591 /*-
1592  * process the incoming Authentication chunk
1593  * return codes:
1594  *   -1 on any authentication error
1595  *    0 on authentication verification
1596  */
1597 int
1598 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1599     struct mbuf *m, uint32_t offset)
1600 {
1601 	uint16_t chunklen;
1602 	uint16_t shared_key_id;
1603 	uint16_t hmac_id;
1604 	sctp_sharedkey_t *skey;
1605 	uint32_t digestlen;
1606 	uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1607 	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1608 
1609 	/* auth is checked for NULL by caller */
1610 	chunklen = ntohs(auth->ch.chunk_length);
1611 	if (chunklen < sizeof(*auth)) {
1612 		SCTP_STAT_INCR(sctps_recvauthfailed);
1613 		return (-1);
1614 	}
1615 	SCTP_STAT_INCR(sctps_recvauth);
1616 
1617 	/* get the auth params */
1618 	shared_key_id = ntohs(auth->shared_key_id);
1619 	hmac_id = ntohs(auth->hmac_id);
1620 	SCTPDBG(SCTP_DEBUG_AUTH1,
1621 	    "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1622 	    shared_key_id, hmac_id);
1623 
1624 	/* is the indicated HMAC supported? */
1625 	if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1626 		struct mbuf *op_err;
1627 		struct sctp_error_auth_invalid_hmac *cause;
1628 
1629 		SCTP_STAT_INCR(sctps_recvivalhmacid);
1630 		SCTPDBG(SCTP_DEBUG_AUTH1,
1631 		    "SCTP Auth: unsupported HMAC id %u\n",
1632 		    hmac_id);
1633 		/*
1634 		 * report this in an Error Chunk: Unsupported HMAC
1635 		 * Identifier
1636 		 */
1637 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
1638 		    0, M_NOWAIT, 1, MT_HEADER);
1639 		if (op_err != NULL) {
1640 			/* pre-reserve some space */
1641 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1642 			/* fill in the error */
1643 			cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
1644 			cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1645 			cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
1646 			cause->hmac_id = ntohs(hmac_id);
1647 			SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
1648 			/* queue it */
1649 			sctp_queue_op_err(stcb, op_err);
1650 		}
1651 		return (-1);
1652 	}
1653 	/* get the indicated shared key, if available */
1654 	if ((stcb->asoc.authinfo.recv_key == NULL) ||
1655 	    (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1656 		/* find the shared key on the assoc first */
1657 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1658 		    shared_key_id);
1659 		/* if the shared key isn't found, discard the chunk */
1660 		if (skey == NULL) {
1661 			SCTP_STAT_INCR(sctps_recvivalkeyid);
1662 			SCTPDBG(SCTP_DEBUG_AUTH1,
1663 			    "SCTP Auth: unknown key id %u\n",
1664 			    shared_key_id);
1665 			return (-1);
1666 		}
1667 		/* generate a notification if this is a new key id */
1668 		if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1669 			/*
1670 			 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1671 			 * shared_key_id, (void
1672 			 * *)stcb->asoc.authinfo.recv_keyid);
1673 			 */
1674 			sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1675 			    shared_key_id, stcb->asoc.authinfo.recv_keyid,
1676 			    SCTP_SO_NOT_LOCKED);
1677 		/* compute a new recv assoc key and cache it */
1678 		if (stcb->asoc.authinfo.recv_key != NULL)
1679 			sctp_free_key(stcb->asoc.authinfo.recv_key);
1680 		stcb->asoc.authinfo.recv_key =
1681 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1682 		    stcb->asoc.authinfo.peer_random, skey->key);
1683 		stcb->asoc.authinfo.recv_keyid = shared_key_id;
1684 #ifdef SCTP_DEBUG
1685 		if (SCTP_AUTH_DEBUG)
1686 			sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1687 #endif
1688 	}
1689 	/* validate the digest length */
1690 	digestlen = sctp_get_hmac_digest_len(hmac_id);
1691 	if (chunklen < (sizeof(*auth) + digestlen)) {
1692 		/* invalid digest length */
1693 		SCTP_STAT_INCR(sctps_recvauthfailed);
1694 		SCTPDBG(SCTP_DEBUG_AUTH1,
1695 		    "SCTP Auth: chunk too short for HMAC\n");
1696 		return (-1);
1697 	}
1698 	/* save a copy of the digest, zero the pseudo header, and validate */
1699 	memcpy(digest, auth->hmac, digestlen);
1700 	sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1701 	(void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1702 	    m, offset, computed_digest);
1703 
1704 	/* compare the computed digest with the one in the AUTH chunk */
1705 	if (timingsafe_bcmp(digest, computed_digest, digestlen) != 0) {
1706 		SCTP_STAT_INCR(sctps_recvauthfailed);
1707 		SCTPDBG(SCTP_DEBUG_AUTH1,
1708 		    "SCTP Auth: HMAC digest check failed\n");
1709 		return (-1);
1710 	}
1711 	return (0);
1712 }
1713 
1714 /*
1715  * Generate NOTIFICATION
1716  */
1717 void
1718 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1719     uint16_t keyid, uint16_t alt_keyid, int so_locked)
1720 {
1721 	struct mbuf *m_notify;
1722 	struct sctp_authkey_event *auth;
1723 	struct sctp_queued_to_read *control;
1724 
1725 	if ((stcb == NULL) ||
1726 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1727 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1728 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1729 	    ) {
1730 		/* If the socket is gone we are out of here */
1731 		return;
1732 	}
1733 
1734 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1735 		/* event not enabled */
1736 		return;
1737 
1738 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1739 	    0, M_NOWAIT, 1, MT_HEADER);
1740 	if (m_notify == NULL)
1741 		/* no space left */
1742 		return;
1743 
1744 	SCTP_BUF_LEN(m_notify) = 0;
1745 	auth = mtod(m_notify, struct sctp_authkey_event *);
1746 	memset(auth, 0, sizeof(struct sctp_authkey_event));
1747 	auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1748 	auth->auth_flags = 0;
1749 	auth->auth_length = sizeof(*auth);
1750 	auth->auth_keynumber = keyid;
1751 	auth->auth_altkeynumber = alt_keyid;
1752 	auth->auth_indication = indication;
1753 	auth->auth_assoc_id = sctp_get_associd(stcb);
1754 
1755 	SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1756 	SCTP_BUF_NEXT(m_notify) = NULL;
1757 
1758 	/* append to socket */
1759 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1760 	    0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1761 	if (control == NULL) {
1762 		/* no memory */
1763 		sctp_m_freem(m_notify);
1764 		return;
1765 	}
1766 	control->length = SCTP_BUF_LEN(m_notify);
1767 	control->spec_flags = M_NOTIFICATION;
1768 	/* not that we need this */
1769 	control->tail_mbuf = m_notify;
1770 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1771 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1772 }
1773 
1774 
1775 /*-
1776  * validates the AUTHentication related parameters in an INIT/INIT-ACK
1777  * Note: currently only used for INIT as INIT-ACK is handled inline
1778  * with sctp_load_addresses_from_init()
1779  */
1780 int
1781 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1782 {
1783 	struct sctp_paramhdr *phdr, param_buf;
1784 	uint16_t ptype, plen;
1785 	int peer_supports_asconf = 0;
1786 	int peer_supports_auth = 0;
1787 	int got_random = 0, got_hmacs = 0, got_chklist = 0;
1788 	uint8_t saw_asconf = 0;
1789 	uint8_t saw_asconf_ack = 0;
1790 
1791 	/* go through each of the params. */
1792 	phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
1793 	while (phdr) {
1794 		ptype = ntohs(phdr->param_type);
1795 		plen = ntohs(phdr->param_length);
1796 
1797 		if (offset + plen > limit) {
1798 			break;
1799 		}
1800 		if (plen < sizeof(struct sctp_paramhdr)) {
1801 			break;
1802 		}
1803 		if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1804 			/* A supported extension chunk */
1805 			struct sctp_supported_chunk_types_param *pr_supported;
1806 			uint8_t local_store[SCTP_SMALL_CHUNK_STORE];
1807 			int num_ent, i;
1808 
1809 			if (plen > sizeof(local_store)) {
1810 				break;
1811 			}
1812 			phdr = sctp_get_next_param(m, offset,
1813 			    (struct sctp_paramhdr *)&local_store,
1814 			    plen);
1815 			if (phdr == NULL) {
1816 				return (-1);
1817 			}
1818 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1819 			num_ent = plen - sizeof(struct sctp_paramhdr);
1820 			for (i = 0; i < num_ent; i++) {
1821 				switch (pr_supported->chunk_types[i]) {
1822 				case SCTP_ASCONF:
1823 				case SCTP_ASCONF_ACK:
1824 					peer_supports_asconf = 1;
1825 					break;
1826 				default:
1827 					/* one we don't care about */
1828 					break;
1829 				}
1830 			}
1831 		} else if (ptype == SCTP_RANDOM) {
1832 			/* enforce the random length */
1833 			if (plen != (sizeof(struct sctp_auth_random) +
1834 			    SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1835 				SCTPDBG(SCTP_DEBUG_AUTH1,
1836 				    "SCTP: invalid RANDOM len\n");
1837 				return (-1);
1838 			}
1839 			got_random = 1;
1840 		} else if (ptype == SCTP_HMAC_LIST) {
1841 			struct sctp_auth_hmac_algo *hmacs;
1842 			uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1843 			int num_hmacs;
1844 
1845 			if (plen > sizeof(store)) {
1846 				break;
1847 			}
1848 			phdr = sctp_get_next_param(m, offset,
1849 			    (struct sctp_paramhdr *)store,
1850 			    plen);
1851 			if (phdr == NULL) {
1852 				return (-1);
1853 			}
1854 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1855 			num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]);
1856 			/* validate the hmac list */
1857 			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1858 				SCTPDBG(SCTP_DEBUG_AUTH1,
1859 				    "SCTP: invalid HMAC param\n");
1860 				return (-1);
1861 			}
1862 			got_hmacs = 1;
1863 		} else if (ptype == SCTP_CHUNK_LIST) {
1864 			struct sctp_auth_chunk_list *chunks;
1865 			uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1866 			int i, num_chunks;
1867 
1868 			if (plen > sizeof(chunks_store)) {
1869 				break;
1870 			}
1871 			phdr = sctp_get_next_param(m, offset,
1872 			    (struct sctp_paramhdr *)chunks_store,
1873 			    plen);
1874 			if (phdr == NULL) {
1875 				return (-1);
1876 			}
1877 			/*-
1878 			 * Flip through the list and mark that the
1879 			 * peer supports asconf/asconf_ack.
1880 			 */
1881 			chunks = (struct sctp_auth_chunk_list *)phdr;
1882 			num_chunks = plen - sizeof(*chunks);
1883 			for (i = 0; i < num_chunks; i++) {
1884 				/* record asconf/asconf-ack if listed */
1885 				if (chunks->chunk_types[i] == SCTP_ASCONF)
1886 					saw_asconf = 1;
1887 				if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1888 					saw_asconf_ack = 1;
1889 
1890 			}
1891 			if (num_chunks)
1892 				got_chklist = 1;
1893 		}
1894 
1895 		offset += SCTP_SIZE32(plen);
1896 		if (offset >= limit) {
1897 			break;
1898 		}
1899 		phdr = sctp_get_next_param(m, offset, &param_buf,
1900 		    sizeof(param_buf));
1901 	}
1902 	/* validate authentication required parameters */
1903 	if (got_random && got_hmacs) {
1904 		peer_supports_auth = 1;
1905 	} else {
1906 		peer_supports_auth = 0;
1907 	}
1908 	if (!peer_supports_auth && got_chklist) {
1909 		SCTPDBG(SCTP_DEBUG_AUTH1,
1910 		    "SCTP: peer sent chunk list w/o AUTH\n");
1911 		return (-1);
1912 	}
1913 	if (peer_supports_asconf && !peer_supports_auth) {
1914 		SCTPDBG(SCTP_DEBUG_AUTH1,
1915 		    "SCTP: peer supports ASCONF but not AUTH\n");
1916 		return (-1);
1917 	} else if ((peer_supports_asconf) && (peer_supports_auth) &&
1918 	    ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1919 		return (-2);
1920 	}
1921 	return (0);
1922 }
1923 
1924 void
1925 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1926 {
1927 	uint16_t chunks_len = 0;
1928 	uint16_t hmacs_len = 0;
1929 	uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1930 	sctp_key_t *new_key;
1931 	uint16_t keylen;
1932 
1933 	/* initialize hmac list from endpoint */
1934 	stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1935 	if (stcb->asoc.local_hmacs != NULL) {
1936 		hmacs_len = stcb->asoc.local_hmacs->num_algo *
1937 		    sizeof(stcb->asoc.local_hmacs->hmac[0]);
1938 	}
1939 	/* initialize auth chunks list from endpoint */
1940 	stcb->asoc.local_auth_chunks =
1941 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1942 	if (stcb->asoc.local_auth_chunks != NULL) {
1943 		int i;
1944 
1945 		for (i = 0; i < 256; i++) {
1946 			if (stcb->asoc.local_auth_chunks->chunks[i])
1947 				chunks_len++;
1948 		}
1949 	}
1950 	/* copy defaults from the endpoint */
1951 	stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
1952 
1953 	/* copy out the shared key list (by reference) from the endpoint */
1954 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
1955 	    &stcb->asoc.shared_keys);
1956 
1957 	/* now set the concatenated key (random + chunks + hmacs) */
1958 	/* key includes parameter headers */
1959 	keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1960 	    hmacs_len;
1961 	new_key = sctp_alloc_key(keylen);
1962 	if (new_key != NULL) {
1963 		struct sctp_paramhdr *ph;
1964 		int plen;
1965 
1966 		/* generate and copy in the RANDOM */
1967 		ph = (struct sctp_paramhdr *)new_key->key;
1968 		ph->param_type = htons(SCTP_RANDOM);
1969 		plen = sizeof(*ph) + random_len;
1970 		ph->param_length = htons(plen);
1971 		SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
1972 		keylen = plen;
1973 
1974 		/* append in the AUTH chunks */
1975 		/* NOTE: currently we always have chunks to list */
1976 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1977 		ph->param_type = htons(SCTP_CHUNK_LIST);
1978 		plen = sizeof(*ph) + chunks_len;
1979 		ph->param_length = htons(plen);
1980 		keylen += sizeof(*ph);
1981 		if (stcb->asoc.local_auth_chunks) {
1982 			int i;
1983 
1984 			for (i = 0; i < 256; i++) {
1985 				if (stcb->asoc.local_auth_chunks->chunks[i])
1986 					new_key->key[keylen++] = i;
1987 			}
1988 		}
1989 
1990 		/* append in the HMACs */
1991 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1992 		ph->param_type = htons(SCTP_HMAC_LIST);
1993 		plen = sizeof(*ph) + hmacs_len;
1994 		ph->param_length = htons(plen);
1995 		keylen += sizeof(*ph);
1996 		(void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
1997 		    new_key->key + keylen);
1998 	}
1999 	if (stcb->asoc.authinfo.random != NULL)
2000 		sctp_free_key(stcb->asoc.authinfo.random);
2001 	stcb->asoc.authinfo.random = new_key;
2002 	stcb->asoc.authinfo.random_len = random_len;
2003 }
2004