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