xref: /freebsd/sys/netinet/sctp_auth.c (revision 0053ed28ff8acb3c7a266cd40d2b18b0bd9e2338)
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 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
570     SCTP_UNUSED
571 #endif
572 )
573 {
574 	sctp_sharedkey_t *skey;
575 
576 	/* find the shared key */
577 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
578 
579 	/* decrement the ref count */
580 	if (skey) {
581 		SCTPDBG(SCTP_DEBUG_AUTH2,
582 		    "%s: stcb %p key %u refcount release to %d\n",
583 		    __func__, (void *)stcb, key_id, skey->refcount);
584 
585 		/* see if a notification should be generated */
586 		if ((skey->refcount <= 2) && (skey->deactivated)) {
587 			/* notify ULP that key is no longer used */
588 			sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
589 			    key_id, 0, so_locked);
590 			SCTPDBG(SCTP_DEBUG_AUTH2,
591 			    "%s: stcb %p key %u no longer used, %d\n",
592 			    __func__, (void *)stcb, key_id, skey->refcount);
593 		}
594 		sctp_free_sharedkey(skey);
595 	}
596 }
597 
598 static sctp_sharedkey_t *
599 sctp_copy_sharedkey(const sctp_sharedkey_t *skey)
600 {
601 	sctp_sharedkey_t *new_skey;
602 
603 	if (skey == NULL)
604 		return (NULL);
605 	new_skey = sctp_alloc_sharedkey();
606 	if (new_skey == NULL)
607 		return (NULL);
608 	if (skey->key != NULL)
609 		new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
610 	else
611 		new_skey->key = NULL;
612 	new_skey->keyid = skey->keyid;
613 	return (new_skey);
614 }
615 
616 int
617 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
618 {
619 	sctp_sharedkey_t *skey, *new_skey;
620 	int count = 0;
621 
622 	if ((src == NULL) || (dest == NULL))
623 		return (0);
624 	LIST_FOREACH(skey, src, next) {
625 		new_skey = sctp_copy_sharedkey(skey);
626 		if (new_skey != NULL) {
627 			if (sctp_insert_sharedkey(dest, new_skey)) {
628 				sctp_free_sharedkey(new_skey);
629 			} else {
630 				count++;
631 			}
632 		}
633 	}
634 	return (count);
635 }
636 
637 
638 sctp_hmaclist_t *
639 sctp_alloc_hmaclist(uint16_t num_hmacs)
640 {
641 	sctp_hmaclist_t *new_list;
642 	int alloc_size;
643 
644 	alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
645 	SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
646 	    SCTP_M_AUTH_HL);
647 	if (new_list == NULL) {
648 		/* out of memory */
649 		return (NULL);
650 	}
651 	new_list->max_algo = num_hmacs;
652 	new_list->num_algo = 0;
653 	return (new_list);
654 }
655 
656 void
657 sctp_free_hmaclist(sctp_hmaclist_t *list)
658 {
659 	if (list != NULL) {
660 		SCTP_FREE(list, SCTP_M_AUTH_HL);
661 		list = NULL;
662 	}
663 }
664 
665 int
666 sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id)
667 {
668 	int i;
669 
670 	if (list == NULL)
671 		return (-1);
672 	if (list->num_algo == list->max_algo) {
673 		SCTPDBG(SCTP_DEBUG_AUTH1,
674 		    "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
675 		return (-1);
676 	}
677 	if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
678 	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
679 		return (-1);
680 	}
681 	/* Now is it already in the list */
682 	for (i = 0; i < list->num_algo; i++) {
683 		if (list->hmac[i] == hmac_id) {
684 			/* already in list */
685 			return (-1);
686 		}
687 	}
688 	SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
689 	list->hmac[list->num_algo++] = hmac_id;
690 	return (0);
691 }
692 
693 sctp_hmaclist_t *
694 sctp_copy_hmaclist(sctp_hmaclist_t *list)
695 {
696 	sctp_hmaclist_t *new_list;
697 	int i;
698 
699 	if (list == NULL)
700 		return (NULL);
701 	/* get a new list */
702 	new_list = sctp_alloc_hmaclist(list->max_algo);
703 	if (new_list == NULL)
704 		return (NULL);
705 	/* copy it */
706 	new_list->max_algo = list->max_algo;
707 	new_list->num_algo = list->num_algo;
708 	for (i = 0; i < list->num_algo; i++)
709 		new_list->hmac[i] = list->hmac[i];
710 	return (new_list);
711 }
712 
713 sctp_hmaclist_t *
714 sctp_default_supported_hmaclist(void)
715 {
716 	sctp_hmaclist_t *new_list;
717 
718 	new_list = sctp_alloc_hmaclist(2);
719 	if (new_list == NULL)
720 		return (NULL);
721 	/* We prefer SHA256, so list it first */
722 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
723 	(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
724 	return (new_list);
725 }
726 
727 /*-
728  * HMAC algos are listed in priority/preference order
729  * find the best HMAC id to use for the peer based on local support
730  */
731 uint16_t
732 sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local)
733 {
734 	int i, j;
735 
736 	if ((local == NULL) || (peer == NULL))
737 		return (SCTP_AUTH_HMAC_ID_RSVD);
738 
739 	for (i = 0; i < peer->num_algo; i++) {
740 		for (j = 0; j < local->num_algo; j++) {
741 			if (peer->hmac[i] == local->hmac[j]) {
742 				/* found the "best" one */
743 				SCTPDBG(SCTP_DEBUG_AUTH1,
744 				    "SCTP: negotiated peer HMAC id %u\n",
745 				    peer->hmac[i]);
746 				return (peer->hmac[i]);
747 			}
748 		}
749 	}
750 	/* didn't find one! */
751 	return (SCTP_AUTH_HMAC_ID_RSVD);
752 }
753 
754 /*-
755  * serialize the HMAC algo list and return space used
756  * caller must guarantee ptr has appropriate space
757  */
758 int
759 sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr)
760 {
761 	int i;
762 	uint16_t hmac_id;
763 
764 	if (list == NULL)
765 		return (0);
766 
767 	for (i = 0; i < list->num_algo; i++) {
768 		hmac_id = htons(list->hmac[i]);
769 		memcpy(ptr, &hmac_id, sizeof(hmac_id));
770 		ptr += sizeof(hmac_id);
771 	}
772 	return (list->num_algo * sizeof(hmac_id));
773 }
774 
775 int
776 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
777 {
778 	uint32_t i;
779 
780 	for (i = 0; i < num_hmacs; i++) {
781 		if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
782 			return (0);
783 		}
784 	}
785 	return (-1);
786 }
787 
788 sctp_authinfo_t *
789 sctp_alloc_authinfo(void)
790 {
791 	sctp_authinfo_t *new_authinfo;
792 
793 	SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
794 	    SCTP_M_AUTH_IF);
795 
796 	if (new_authinfo == NULL) {
797 		/* out of memory */
798 		return (NULL);
799 	}
800 	memset(new_authinfo, 0, sizeof(*new_authinfo));
801 	return (new_authinfo);
802 }
803 
804 void
805 sctp_free_authinfo(sctp_authinfo_t *authinfo)
806 {
807 	if (authinfo == NULL)
808 		return;
809 
810 	if (authinfo->random != NULL)
811 		sctp_free_key(authinfo->random);
812 	if (authinfo->peer_random != NULL)
813 		sctp_free_key(authinfo->peer_random);
814 	if (authinfo->assoc_key != NULL)
815 		sctp_free_key(authinfo->assoc_key);
816 	if (authinfo->recv_key != NULL)
817 		sctp_free_key(authinfo->recv_key);
818 
819 	/* We are NOT dynamically allocating authinfo's right now... */
820 	/* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
821 }
822 
823 
824 uint32_t
825 sctp_get_auth_chunk_len(uint16_t hmac_algo)
826 {
827 	int size;
828 
829 	size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
830 	return (SCTP_SIZE32(size));
831 }
832 
833 uint32_t
834 sctp_get_hmac_digest_len(uint16_t hmac_algo)
835 {
836 	switch (hmac_algo) {
837 	case SCTP_AUTH_HMAC_ID_SHA1:
838 		return (SCTP_AUTH_DIGEST_LEN_SHA1);
839 	case SCTP_AUTH_HMAC_ID_SHA256:
840 		return (SCTP_AUTH_DIGEST_LEN_SHA256);
841 	default:
842 		/* unknown HMAC algorithm: can't do anything */
843 		return (0);
844 	}			/* end switch */
845 }
846 
847 static inline int
848 sctp_get_hmac_block_len(uint16_t hmac_algo)
849 {
850 	switch (hmac_algo) {
851 	case SCTP_AUTH_HMAC_ID_SHA1:
852 		return (64);
853 	case SCTP_AUTH_HMAC_ID_SHA256:
854 		return (64);
855 	case SCTP_AUTH_HMAC_ID_RSVD:
856 	default:
857 		/* unknown HMAC algorithm: can't do anything */
858 		return (0);
859 	}			/* end switch */
860 }
861 
862 static void
863 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx)
864 {
865 	switch (hmac_algo) {
866 	case SCTP_AUTH_HMAC_ID_SHA1:
867 		SCTP_SHA1_INIT(&ctx->sha1);
868 		break;
869 	case SCTP_AUTH_HMAC_ID_SHA256:
870 		SCTP_SHA256_INIT(&ctx->sha256);
871 		break;
872 	case SCTP_AUTH_HMAC_ID_RSVD:
873 	default:
874 		/* unknown HMAC algorithm: can't do anything */
875 		return;
876 	}			/* end switch */
877 }
878 
879 static void
880 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx,
881     uint8_t *text, uint32_t textlen)
882 {
883 	switch (hmac_algo) {
884 	case SCTP_AUTH_HMAC_ID_SHA1:
885 		SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
886 		break;
887 	case SCTP_AUTH_HMAC_ID_SHA256:
888 		SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
889 		break;
890 	case SCTP_AUTH_HMAC_ID_RSVD:
891 	default:
892 		/* unknown HMAC algorithm: can't do anything */
893 		return;
894 	}			/* end switch */
895 }
896 
897 static void
898 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx,
899     uint8_t *digest)
900 {
901 	switch (hmac_algo) {
902 	case SCTP_AUTH_HMAC_ID_SHA1:
903 		SCTP_SHA1_FINAL(digest, &ctx->sha1);
904 		break;
905 	case SCTP_AUTH_HMAC_ID_SHA256:
906 		SCTP_SHA256_FINAL(digest, &ctx->sha256);
907 		break;
908 	case SCTP_AUTH_HMAC_ID_RSVD:
909 	default:
910 		/* unknown HMAC algorithm: can't do anything */
911 		return;
912 	}			/* end switch */
913 }
914 
915 /*-
916  * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
917  *
918  * Compute the HMAC digest using the desired hash key, text, and HMAC
919  * algorithm.  Resulting digest is placed in 'digest' and digest length
920  * is returned, if the HMAC was performed.
921  *
922  * WARNING: it is up to the caller to supply sufficient space to hold the
923  * resultant digest.
924  */
925 uint32_t
926 sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
927     uint8_t *text, uint32_t textlen, uint8_t *digest)
928 {
929 	uint32_t digestlen;
930 	uint32_t blocklen;
931 	sctp_hash_context_t ctx;
932 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
933 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
934 	uint32_t i;
935 
936 	/* sanity check the material and length */
937 	if ((key == NULL) || (keylen == 0) || (text == NULL) ||
938 	    (textlen == 0) || (digest == NULL)) {
939 		/* can't do HMAC with empty key or text or digest store */
940 		return (0);
941 	}
942 	/* validate the hmac algo and get the digest length */
943 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
944 	if (digestlen == 0)
945 		return (0);
946 
947 	/* hash the key if it is longer than the hash block size */
948 	blocklen = sctp_get_hmac_block_len(hmac_algo);
949 	if (keylen > blocklen) {
950 		sctp_hmac_init(hmac_algo, &ctx);
951 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
952 		sctp_hmac_final(hmac_algo, &ctx, temp);
953 		/* set the hashed key as the key */
954 		keylen = digestlen;
955 		key = temp;
956 	}
957 	/* initialize the inner/outer pads with the key and "append" zeroes */
958 	memset(ipad, 0, blocklen);
959 	memset(opad, 0, blocklen);
960 	memcpy(ipad, key, keylen);
961 	memcpy(opad, key, keylen);
962 
963 	/* XOR the key with ipad and opad values */
964 	for (i = 0; i < blocklen; i++) {
965 		ipad[i] ^= 0x36;
966 		opad[i] ^= 0x5c;
967 	}
968 
969 	/* perform inner hash */
970 	sctp_hmac_init(hmac_algo, &ctx);
971 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
972 	sctp_hmac_update(hmac_algo, &ctx, text, textlen);
973 	sctp_hmac_final(hmac_algo, &ctx, temp);
974 
975 	/* perform outer hash */
976 	sctp_hmac_init(hmac_algo, &ctx);
977 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
978 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
979 	sctp_hmac_final(hmac_algo, &ctx, digest);
980 
981 	return (digestlen);
982 }
983 
984 /* mbuf version */
985 uint32_t
986 sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
987     struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer)
988 {
989 	uint32_t digestlen;
990 	uint32_t blocklen;
991 	sctp_hash_context_t ctx;
992 	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
993 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
994 	uint32_t i;
995 	struct mbuf *m_tmp;
996 
997 	/* sanity check the material and length */
998 	if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
999 		/* can't do HMAC with empty key or text or digest store */
1000 		return (0);
1001 	}
1002 	/* validate the hmac algo and get the digest length */
1003 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1004 	if (digestlen == 0)
1005 		return (0);
1006 
1007 	/* hash the key if it is longer than the hash block size */
1008 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1009 	if (keylen > blocklen) {
1010 		sctp_hmac_init(hmac_algo, &ctx);
1011 		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1012 		sctp_hmac_final(hmac_algo, &ctx, temp);
1013 		/* set the hashed key as the key */
1014 		keylen = digestlen;
1015 		key = temp;
1016 	}
1017 	/* initialize the inner/outer pads with the key and "append" zeroes */
1018 	memset(ipad, 0, blocklen);
1019 	memset(opad, 0, blocklen);
1020 	memcpy(ipad, key, keylen);
1021 	memcpy(opad, key, keylen);
1022 
1023 	/* XOR the key with ipad and opad values */
1024 	for (i = 0; i < blocklen; i++) {
1025 		ipad[i] ^= 0x36;
1026 		opad[i] ^= 0x5c;
1027 	}
1028 
1029 	/* perform inner hash */
1030 	sctp_hmac_init(hmac_algo, &ctx);
1031 	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1032 	/* find the correct starting mbuf and offset (get start of text) */
1033 	m_tmp = m;
1034 	while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1035 		m_offset -= SCTP_BUF_LEN(m_tmp);
1036 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1037 	}
1038 	/* now use the rest of the mbuf chain for the text */
1039 	while (m_tmp != NULL) {
1040 		if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1041 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1042 			    SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1043 		} else {
1044 			sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *)+m_offset,
1045 			    SCTP_BUF_LEN(m_tmp) - m_offset);
1046 		}
1047 
1048 		/* clear the offset since it's only for the first mbuf */
1049 		m_offset = 0;
1050 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1051 	}
1052 	sctp_hmac_final(hmac_algo, &ctx, temp);
1053 
1054 	/* perform outer hash */
1055 	sctp_hmac_init(hmac_algo, &ctx);
1056 	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1057 	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1058 	sctp_hmac_final(hmac_algo, &ctx, digest);
1059 
1060 	return (digestlen);
1061 }
1062 
1063 /*-
1064  * verify the HMAC digest using the desired hash key, text, and HMAC
1065  * algorithm.
1066  * Returns -1 on error, 0 on success.
1067  */
1068 int
1069 sctp_verify_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
1070     uint8_t *text, uint32_t textlen,
1071     uint8_t *digest, uint32_t digestlen)
1072 {
1073 	uint32_t len;
1074 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1075 
1076 	/* sanity check the material and length */
1077 	if ((key == NULL) || (keylen == 0) ||
1078 	    (text == NULL) || (textlen == 0) || (digest == NULL)) {
1079 		/* can't do HMAC with empty key or text or digest */
1080 		return (-1);
1081 	}
1082 	len = sctp_get_hmac_digest_len(hmac_algo);
1083 	if ((len == 0) || (digestlen != len))
1084 		return (-1);
1085 
1086 	/* compute the expected hash */
1087 	if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1088 		return (-1);
1089 
1090 	if (memcmp(digest, temp, digestlen) != 0)
1091 		return (-1);
1092 	else
1093 		return (0);
1094 }
1095 
1096 
1097 /*
1098  * computes the requested HMAC using a key struct (which may be modified if
1099  * the keylen exceeds the HMAC block len).
1100  */
1101 uint32_t
1102 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1103     uint32_t textlen, uint8_t *digest)
1104 {
1105 	uint32_t digestlen;
1106 	uint32_t blocklen;
1107 	sctp_hash_context_t ctx;
1108 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1109 
1110 	/* sanity check */
1111 	if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1112 	    (digest == NULL)) {
1113 		/* can't do HMAC with empty key or text or digest store */
1114 		return (0);
1115 	}
1116 	/* validate the hmac algo and get the digest length */
1117 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1118 	if (digestlen == 0)
1119 		return (0);
1120 
1121 	/* hash the key if it is longer than the hash block size */
1122 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1123 	if (key->keylen > blocklen) {
1124 		sctp_hmac_init(hmac_algo, &ctx);
1125 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1126 		sctp_hmac_final(hmac_algo, &ctx, temp);
1127 		/* save the hashed key as the new key */
1128 		key->keylen = digestlen;
1129 		memcpy(key->key, temp, key->keylen);
1130 	}
1131 	return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1132 	    digest));
1133 }
1134 
1135 /* mbuf version */
1136 uint32_t
1137 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1138     uint32_t m_offset, uint8_t *digest)
1139 {
1140 	uint32_t digestlen;
1141 	uint32_t blocklen;
1142 	sctp_hash_context_t ctx;
1143 	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1144 
1145 	/* sanity check */
1146 	if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1147 		/* can't do HMAC with empty key or text or digest store */
1148 		return (0);
1149 	}
1150 	/* validate the hmac algo and get the digest length */
1151 	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1152 	if (digestlen == 0)
1153 		return (0);
1154 
1155 	/* hash the key if it is longer than the hash block size */
1156 	blocklen = sctp_get_hmac_block_len(hmac_algo);
1157 	if (key->keylen > blocklen) {
1158 		sctp_hmac_init(hmac_algo, &ctx);
1159 		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1160 		sctp_hmac_final(hmac_algo, &ctx, temp);
1161 		/* save the hashed key as the new key */
1162 		key->keylen = digestlen;
1163 		memcpy(key->key, temp, key->keylen);
1164 	}
1165 	return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1166 }
1167 
1168 int
1169 sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1170 {
1171 	int i;
1172 
1173 	if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1174 		return (0);
1175 
1176 	for (i = 0; i < list->num_algo; i++)
1177 		if (list->hmac[i] == id)
1178 			return (1);
1179 
1180 	/* not in the list */
1181 	return (0);
1182 }
1183 
1184 
1185 /*-
1186  * clear any cached key(s) if they match the given key id on an association.
1187  * the cached key(s) will be recomputed and re-cached at next use.
1188  * ASSUMES TCB_LOCK is already held
1189  */
1190 void
1191 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1192 {
1193 	if (stcb == NULL)
1194 		return;
1195 
1196 	if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1197 		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1198 		stcb->asoc.authinfo.assoc_key = NULL;
1199 	}
1200 	if (keyid == stcb->asoc.authinfo.recv_keyid) {
1201 		sctp_free_key(stcb->asoc.authinfo.recv_key);
1202 		stcb->asoc.authinfo.recv_key = NULL;
1203 	}
1204 }
1205 
1206 /*-
1207  * clear any cached key(s) if they match the given key id for all assocs on
1208  * an endpoint.
1209  * ASSUMES INP_WLOCK is already held
1210  */
1211 void
1212 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1213 {
1214 	struct sctp_tcb *stcb;
1215 
1216 	if (inp == NULL)
1217 		return;
1218 
1219 	/* clear the cached keys on all assocs on this instance */
1220 	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1221 		SCTP_TCB_LOCK(stcb);
1222 		sctp_clear_cachedkeys(stcb, keyid);
1223 		SCTP_TCB_UNLOCK(stcb);
1224 	}
1225 }
1226 
1227 /*-
1228  * delete a shared key from an association
1229  * ASSUMES TCB_LOCK is already held
1230  */
1231 int
1232 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1233 {
1234 	sctp_sharedkey_t *skey;
1235 
1236 	if (stcb == NULL)
1237 		return (-1);
1238 
1239 	/* is the keyid the assoc active sending key */
1240 	if (keyid == stcb->asoc.authinfo.active_keyid)
1241 		return (-1);
1242 
1243 	/* does the key exist? */
1244 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1245 	if (skey == NULL)
1246 		return (-1);
1247 
1248 	/* are there other refcount holders on the key? */
1249 	if (skey->refcount > 1)
1250 		return (-1);
1251 
1252 	/* remove it */
1253 	LIST_REMOVE(skey, next);
1254 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1255 
1256 	/* clear any cached keys */
1257 	sctp_clear_cachedkeys(stcb, keyid);
1258 	return (0);
1259 }
1260 
1261 /*-
1262  * deletes a shared key from the endpoint
1263  * ASSUMES INP_WLOCK is already held
1264  */
1265 int
1266 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1267 {
1268 	sctp_sharedkey_t *skey;
1269 
1270 	if (inp == NULL)
1271 		return (-1);
1272 
1273 	/* is the keyid the active sending key on the endpoint */
1274 	if (keyid == inp->sctp_ep.default_keyid)
1275 		return (-1);
1276 
1277 	/* does the key exist? */
1278 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1279 	if (skey == NULL)
1280 		return (-1);
1281 
1282 	/* endpoint keys are not refcounted */
1283 
1284 	/* remove it */
1285 	LIST_REMOVE(skey, next);
1286 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1287 
1288 	/* clear any cached keys */
1289 	sctp_clear_cachedkeys_ep(inp, keyid);
1290 	return (0);
1291 }
1292 
1293 /*-
1294  * set the active key on an association
1295  * ASSUMES TCB_LOCK is already held
1296  */
1297 int
1298 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1299 {
1300 	sctp_sharedkey_t *skey = NULL;
1301 
1302 	/* find the key on the assoc */
1303 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1304 	if (skey == NULL) {
1305 		/* that key doesn't exist */
1306 		return (-1);
1307 	}
1308 	if ((skey->deactivated) && (skey->refcount > 1)) {
1309 		/* can't reactivate a deactivated key with other refcounts */
1310 		return (-1);
1311 	}
1312 
1313 	/* set the (new) active key */
1314 	stcb->asoc.authinfo.active_keyid = keyid;
1315 	/* reset the deactivated flag */
1316 	skey->deactivated = 0;
1317 
1318 	return (0);
1319 }
1320 
1321 /*-
1322  * set the active key on an endpoint
1323  * ASSUMES INP_WLOCK is already held
1324  */
1325 int
1326 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1327 {
1328 	sctp_sharedkey_t *skey;
1329 
1330 	/* find the key */
1331 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1332 	if (skey == NULL) {
1333 		/* that key doesn't exist */
1334 		return (-1);
1335 	}
1336 	inp->sctp_ep.default_keyid = keyid;
1337 	return (0);
1338 }
1339 
1340 /*-
1341  * deactivates a shared key from the association
1342  * ASSUMES INP_WLOCK is already held
1343  */
1344 int
1345 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1346 {
1347 	sctp_sharedkey_t *skey;
1348 
1349 	if (stcb == NULL)
1350 		return (-1);
1351 
1352 	/* is the keyid the assoc active sending key */
1353 	if (keyid == stcb->asoc.authinfo.active_keyid)
1354 		return (-1);
1355 
1356 	/* does the key exist? */
1357 	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1358 	if (skey == NULL)
1359 		return (-1);
1360 
1361 	/* are there other refcount holders on the key? */
1362 	if (skey->refcount == 1) {
1363 		/* no other users, send a notification for this key */
1364 		sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1365 		    SCTP_SO_LOCKED);
1366 	}
1367 
1368 	/* mark the key as deactivated */
1369 	skey->deactivated = 1;
1370 
1371 	return (0);
1372 }
1373 
1374 /*-
1375  * deactivates a shared key from the endpoint
1376  * ASSUMES INP_WLOCK is already held
1377  */
1378 int
1379 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1380 {
1381 	sctp_sharedkey_t *skey;
1382 
1383 	if (inp == NULL)
1384 		return (-1);
1385 
1386 	/* is the keyid the active sending key on the endpoint */
1387 	if (keyid == inp->sctp_ep.default_keyid)
1388 		return (-1);
1389 
1390 	/* does the key exist? */
1391 	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1392 	if (skey == NULL)
1393 		return (-1);
1394 
1395 	/* endpoint keys are not refcounted */
1396 
1397 	/* remove it */
1398 	LIST_REMOVE(skey, next);
1399 	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1400 
1401 	return (0);
1402 }
1403 
1404 /*
1405  * get local authentication parameters from cookie (from INIT-ACK)
1406  */
1407 void
1408 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1409     uint32_t offset, uint32_t length)
1410 {
1411 	struct sctp_paramhdr *phdr, tmp_param;
1412 	uint16_t plen, ptype;
1413 	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1414 	struct sctp_auth_random *p_random = NULL;
1415 	uint16_t random_len = 0;
1416 	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1417 	struct sctp_auth_hmac_algo *hmacs = NULL;
1418 	uint16_t hmacs_len = 0;
1419 	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1420 	struct sctp_auth_chunk_list *chunks = NULL;
1421 	uint16_t num_chunks = 0;
1422 	sctp_key_t *new_key;
1423 	uint32_t keylen;
1424 
1425 	/* convert to upper bound */
1426 	length += offset;
1427 
1428 	phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1429 	    sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1430 	while (phdr != NULL) {
1431 		ptype = ntohs(phdr->param_type);
1432 		plen = ntohs(phdr->param_length);
1433 
1434 		if ((plen == 0) || (offset + plen > length))
1435 			break;
1436 
1437 		if (ptype == SCTP_RANDOM) {
1438 			if (plen > sizeof(random_store))
1439 				break;
1440 			phdr = sctp_get_next_param(m, offset,
1441 			    (struct sctp_paramhdr *)random_store, plen);
1442 			if (phdr == NULL)
1443 				return;
1444 			/* save the random and length for the key */
1445 			p_random = (struct sctp_auth_random *)phdr;
1446 			random_len = plen - sizeof(*p_random);
1447 		} else if (ptype == SCTP_HMAC_LIST) {
1448 			uint16_t num_hmacs;
1449 			uint16_t i;
1450 
1451 			if (plen > sizeof(hmacs_store))
1452 				break;
1453 			phdr = sctp_get_next_param(m, offset,
1454 			    (struct sctp_paramhdr *)hmacs_store, plen);
1455 			if (phdr == NULL)
1456 				return;
1457 			/* save the hmacs list and num for the key */
1458 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1459 			hmacs_len = plen - sizeof(*hmacs);
1460 			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1461 			if (stcb->asoc.local_hmacs != NULL)
1462 				sctp_free_hmaclist(stcb->asoc.local_hmacs);
1463 			stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1464 			if (stcb->asoc.local_hmacs != NULL) {
1465 				for (i = 0; i < num_hmacs; i++) {
1466 					(void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1467 					    ntohs(hmacs->hmac_ids[i]));
1468 				}
1469 			}
1470 		} else if (ptype == SCTP_CHUNK_LIST) {
1471 			int i;
1472 
1473 			if (plen > sizeof(chunks_store))
1474 				break;
1475 			phdr = sctp_get_next_param(m, offset,
1476 			    (struct sctp_paramhdr *)chunks_store, plen);
1477 			if (phdr == NULL)
1478 				return;
1479 			chunks = (struct sctp_auth_chunk_list *)phdr;
1480 			num_chunks = plen - sizeof(*chunks);
1481 			/* save chunks list and num for the key */
1482 			if (stcb->asoc.local_auth_chunks != NULL)
1483 				sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1484 			else
1485 				stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1486 			for (i = 0; i < num_chunks; i++) {
1487 				(void)sctp_auth_add_chunk(chunks->chunk_types[i],
1488 				    stcb->asoc.local_auth_chunks);
1489 			}
1490 		}
1491 		/* get next parameter */
1492 		offset += SCTP_SIZE32(plen);
1493 		if (offset + sizeof(struct sctp_paramhdr) > length)
1494 			break;
1495 		phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1496 		    (uint8_t *)&tmp_param);
1497 	}
1498 	/* concatenate the full random key */
1499 	keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1500 	if (chunks != NULL) {
1501 		keylen += sizeof(*chunks) + num_chunks;
1502 	}
1503 	new_key = sctp_alloc_key(keylen);
1504 	if (new_key != NULL) {
1505 		/* copy in the RANDOM */
1506 		if (p_random != NULL) {
1507 			keylen = sizeof(*p_random) + random_len;
1508 			memcpy(new_key->key, p_random, keylen);
1509 		} else {
1510 			keylen = 0;
1511 		}
1512 		/* append in the AUTH chunks */
1513 		if (chunks != NULL) {
1514 			memcpy(new_key->key + keylen, chunks,
1515 			    sizeof(*chunks) + num_chunks);
1516 			keylen += sizeof(*chunks) + num_chunks;
1517 		}
1518 		/* append in the HMACs */
1519 		if (hmacs != NULL) {
1520 			memcpy(new_key->key + keylen, hmacs,
1521 			    sizeof(*hmacs) + hmacs_len);
1522 		}
1523 	}
1524 	if (stcb->asoc.authinfo.random != NULL)
1525 		sctp_free_key(stcb->asoc.authinfo.random);
1526 	stcb->asoc.authinfo.random = new_key;
1527 	stcb->asoc.authinfo.random_len = random_len;
1528 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1529 	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1530 
1531 	/* negotiate what HMAC to use for the peer */
1532 	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1533 	    stcb->asoc.local_hmacs);
1534 
1535 	/* copy defaults from the endpoint */
1536 	/* FIX ME: put in cookie? */
1537 	stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1538 	/* copy out the shared key list (by reference) from the endpoint */
1539 	(void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1540 	    &stcb->asoc.shared_keys);
1541 }
1542 
1543 /*
1544  * compute and fill in the HMAC digest for a packet
1545  */
1546 void
1547 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1548     struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1549 {
1550 	uint32_t digestlen;
1551 	sctp_sharedkey_t *skey;
1552 	sctp_key_t *key;
1553 
1554 	if ((stcb == NULL) || (auth == NULL))
1555 		return;
1556 
1557 	/* zero the digest + chunk padding */
1558 	digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1559 	memset(auth->hmac, 0, SCTP_SIZE32(digestlen));
1560 
1561 	/* is the desired key cached? */
1562 	if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1563 	    (stcb->asoc.authinfo.assoc_key == NULL)) {
1564 		if (stcb->asoc.authinfo.assoc_key != NULL) {
1565 			/* free the old cached key */
1566 			sctp_free_key(stcb->asoc.authinfo.assoc_key);
1567 		}
1568 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1569 		/* the only way skey is NULL is if null key id 0 is used */
1570 		if (skey != NULL)
1571 			key = skey->key;
1572 		else
1573 			key = NULL;
1574 		/* compute a new assoc key and cache it */
1575 		stcb->asoc.authinfo.assoc_key =
1576 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1577 		    stcb->asoc.authinfo.peer_random, key);
1578 		stcb->asoc.authinfo.assoc_keyid = keyid;
1579 		SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1580 		    stcb->asoc.authinfo.assoc_keyid);
1581 #ifdef SCTP_DEBUG
1582 		if (SCTP_AUTH_DEBUG)
1583 			sctp_print_key(stcb->asoc.authinfo.assoc_key,
1584 			    "Assoc Key");
1585 #endif
1586 	}
1587 
1588 	/* set in the active key id */
1589 	auth->shared_key_id = htons(keyid);
1590 
1591 	/* compute and fill in the digest */
1592 	(void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1593 	    m, auth_offset, auth->hmac);
1594 }
1595 
1596 
1597 static void
1598 sctp_zero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1599 {
1600 	struct mbuf *m_tmp;
1601 	uint8_t *data;
1602 
1603 	/* sanity check */
1604 	if (m == NULL)
1605 		return;
1606 
1607 	/* find the correct starting mbuf and offset (get start position) */
1608 	m_tmp = m;
1609 	while ((m_tmp != NULL) && (m_offset >= (uint32_t)SCTP_BUF_LEN(m_tmp))) {
1610 		m_offset -= SCTP_BUF_LEN(m_tmp);
1611 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1612 	}
1613 	/* now use the rest of the mbuf chain */
1614 	while ((m_tmp != NULL) && (size > 0)) {
1615 		data = mtod(m_tmp, uint8_t *)+m_offset;
1616 		if (size > (uint32_t)(SCTP_BUF_LEN(m_tmp) - m_offset)) {
1617 			memset(data, 0, SCTP_BUF_LEN(m_tmp) - m_offset);
1618 			size -= SCTP_BUF_LEN(m_tmp) - m_offset;
1619 		} else {
1620 			memset(data, 0, size);
1621 			size = 0;
1622 		}
1623 		/* clear the offset since it's only for the first mbuf */
1624 		m_offset = 0;
1625 		m_tmp = SCTP_BUF_NEXT(m_tmp);
1626 	}
1627 }
1628 
1629 /*-
1630  * process the incoming Authentication chunk
1631  * return codes:
1632  *   -1 on any authentication error
1633  *    0 on authentication verification
1634  */
1635 int
1636 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1637     struct mbuf *m, uint32_t offset)
1638 {
1639 	uint16_t chunklen;
1640 	uint16_t shared_key_id;
1641 	uint16_t hmac_id;
1642 	sctp_sharedkey_t *skey;
1643 	uint32_t digestlen;
1644 	uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1645 	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1646 
1647 	/* auth is checked for NULL by caller */
1648 	chunklen = ntohs(auth->ch.chunk_length);
1649 	if (chunklen < sizeof(*auth)) {
1650 		SCTP_STAT_INCR(sctps_recvauthfailed);
1651 		return (-1);
1652 	}
1653 	SCTP_STAT_INCR(sctps_recvauth);
1654 
1655 	/* get the auth params */
1656 	shared_key_id = ntohs(auth->shared_key_id);
1657 	hmac_id = ntohs(auth->hmac_id);
1658 	SCTPDBG(SCTP_DEBUG_AUTH1,
1659 	    "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1660 	    shared_key_id, hmac_id);
1661 
1662 	/* is the indicated HMAC supported? */
1663 	if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1664 		struct mbuf *op_err;
1665 		struct sctp_error_auth_invalid_hmac *cause;
1666 
1667 		SCTP_STAT_INCR(sctps_recvivalhmacid);
1668 		SCTPDBG(SCTP_DEBUG_AUTH1,
1669 		    "SCTP Auth: unsupported HMAC id %u\n",
1670 		    hmac_id);
1671 		/*
1672 		 * report this in an Error Chunk: Unsupported HMAC
1673 		 * Identifier
1674 		 */
1675 		op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
1676 		    0, M_NOWAIT, 1, MT_HEADER);
1677 		if (op_err != NULL) {
1678 			/* pre-reserve some space */
1679 			SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1680 			/* fill in the error */
1681 			cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
1682 			cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1683 			cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
1684 			cause->hmac_id = ntohs(hmac_id);
1685 			SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
1686 			/* queue it */
1687 			sctp_queue_op_err(stcb, op_err);
1688 		}
1689 		return (-1);
1690 	}
1691 	/* get the indicated shared key, if available */
1692 	if ((stcb->asoc.authinfo.recv_key == NULL) ||
1693 	    (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1694 		/* find the shared key on the assoc first */
1695 		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1696 		    shared_key_id);
1697 		/* if the shared key isn't found, discard the chunk */
1698 		if (skey == NULL) {
1699 			SCTP_STAT_INCR(sctps_recvivalkeyid);
1700 			SCTPDBG(SCTP_DEBUG_AUTH1,
1701 			    "SCTP Auth: unknown key id %u\n",
1702 			    shared_key_id);
1703 			return (-1);
1704 		}
1705 		/* generate a notification if this is a new key id */
1706 		if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1707 			/*
1708 			 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1709 			 * shared_key_id, (void
1710 			 * *)stcb->asoc.authinfo.recv_keyid);
1711 			 */
1712 			sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1713 			    shared_key_id, stcb->asoc.authinfo.recv_keyid,
1714 			    SCTP_SO_NOT_LOCKED);
1715 		/* compute a new recv assoc key and cache it */
1716 		if (stcb->asoc.authinfo.recv_key != NULL)
1717 			sctp_free_key(stcb->asoc.authinfo.recv_key);
1718 		stcb->asoc.authinfo.recv_key =
1719 		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1720 		    stcb->asoc.authinfo.peer_random, skey->key);
1721 		stcb->asoc.authinfo.recv_keyid = shared_key_id;
1722 #ifdef SCTP_DEBUG
1723 		if (SCTP_AUTH_DEBUG)
1724 			sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1725 #endif
1726 	}
1727 	/* validate the digest length */
1728 	digestlen = sctp_get_hmac_digest_len(hmac_id);
1729 	if (chunklen < (sizeof(*auth) + digestlen)) {
1730 		/* invalid digest length */
1731 		SCTP_STAT_INCR(sctps_recvauthfailed);
1732 		SCTPDBG(SCTP_DEBUG_AUTH1,
1733 		    "SCTP Auth: chunk too short for HMAC\n");
1734 		return (-1);
1735 	}
1736 	/* save a copy of the digest, zero the pseudo header, and validate */
1737 	memcpy(digest, auth->hmac, digestlen);
1738 	sctp_zero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1739 	(void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1740 	    m, offset, computed_digest);
1741 
1742 	/* compare the computed digest with the one in the AUTH chunk */
1743 	if (memcmp(digest, computed_digest, digestlen) != 0) {
1744 		SCTP_STAT_INCR(sctps_recvauthfailed);
1745 		SCTPDBG(SCTP_DEBUG_AUTH1,
1746 		    "SCTP Auth: HMAC digest check failed\n");
1747 		return (-1);
1748 	}
1749 	return (0);
1750 }
1751 
1752 /*
1753  * Generate NOTIFICATION
1754  */
1755 void
1756 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1757     uint16_t keyid, uint16_t alt_keyid, int so_locked
1758 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1759     SCTP_UNUSED
1760 #endif
1761 )
1762 {
1763 	struct mbuf *m_notify;
1764 	struct sctp_authkey_event *auth;
1765 	struct sctp_queued_to_read *control;
1766 
1767 	if ((stcb == NULL) ||
1768 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1769 	    (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1770 	    (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1771 	    ) {
1772 		/* If the socket is gone we are out of here */
1773 		return;
1774 	}
1775 
1776 	if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1777 		/* event not enabled */
1778 		return;
1779 
1780 	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1781 	    0, M_NOWAIT, 1, MT_HEADER);
1782 	if (m_notify == NULL)
1783 		/* no space left */
1784 		return;
1785 
1786 	SCTP_BUF_LEN(m_notify) = 0;
1787 	auth = mtod(m_notify, struct sctp_authkey_event *);
1788 	memset(auth, 0, sizeof(struct sctp_authkey_event));
1789 	auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1790 	auth->auth_flags = 0;
1791 	auth->auth_length = sizeof(*auth);
1792 	auth->auth_keynumber = keyid;
1793 	auth->auth_altkeynumber = alt_keyid;
1794 	auth->auth_indication = indication;
1795 	auth->auth_assoc_id = sctp_get_associd(stcb);
1796 
1797 	SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1798 	SCTP_BUF_NEXT(m_notify) = NULL;
1799 
1800 	/* append to socket */
1801 	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1802 	    0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1803 	if (control == NULL) {
1804 		/* no memory */
1805 		sctp_m_freem(m_notify);
1806 		return;
1807 	}
1808 	control->length = SCTP_BUF_LEN(m_notify);
1809 	control->spec_flags = M_NOTIFICATION;
1810 	/* not that we need this */
1811 	control->tail_mbuf = m_notify;
1812 	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1813 	    &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1814 }
1815 
1816 
1817 /*-
1818  * validates the AUTHentication related parameters in an INIT/INIT-ACK
1819  * Note: currently only used for INIT as INIT-ACK is handled inline
1820  * with sctp_load_addresses_from_init()
1821  */
1822 int
1823 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1824 {
1825 	struct sctp_paramhdr *phdr, param_buf;
1826 	uint16_t ptype, plen;
1827 	int peer_supports_asconf = 0;
1828 	int peer_supports_auth = 0;
1829 	int got_random = 0, got_hmacs = 0, got_chklist = 0;
1830 	uint8_t saw_asconf = 0;
1831 	uint8_t saw_asconf_ack = 0;
1832 
1833 	/* go through each of the params. */
1834 	phdr = sctp_get_next_param(m, offset, &param_buf, sizeof(param_buf));
1835 	while (phdr) {
1836 		ptype = ntohs(phdr->param_type);
1837 		plen = ntohs(phdr->param_length);
1838 
1839 		if (offset + plen > limit) {
1840 			break;
1841 		}
1842 		if (plen < sizeof(struct sctp_paramhdr)) {
1843 			break;
1844 		}
1845 		if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1846 			/* A supported extension chunk */
1847 			struct sctp_supported_chunk_types_param *pr_supported;
1848 			uint8_t local_store[SCTP_SMALL_CHUNK_STORE];
1849 			int num_ent, i;
1850 
1851 			if (plen > sizeof(local_store)) {
1852 				break;
1853 			}
1854 			phdr = sctp_get_next_param(m, offset,
1855 			    (struct sctp_paramhdr *)&local_store,
1856 			    plen);
1857 			if (phdr == NULL) {
1858 				return (-1);
1859 			}
1860 			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1861 			num_ent = plen - sizeof(struct sctp_paramhdr);
1862 			for (i = 0; i < num_ent; i++) {
1863 				switch (pr_supported->chunk_types[i]) {
1864 				case SCTP_ASCONF:
1865 				case SCTP_ASCONF_ACK:
1866 					peer_supports_asconf = 1;
1867 					break;
1868 				default:
1869 					/* one we don't care about */
1870 					break;
1871 				}
1872 			}
1873 		} else if (ptype == SCTP_RANDOM) {
1874 			/* enforce the random length */
1875 			if (plen != (sizeof(struct sctp_auth_random) +
1876 			    SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1877 				SCTPDBG(SCTP_DEBUG_AUTH1,
1878 				    "SCTP: invalid RANDOM len\n");
1879 				return (-1);
1880 			}
1881 			got_random = 1;
1882 		} else if (ptype == SCTP_HMAC_LIST) {
1883 			struct sctp_auth_hmac_algo *hmacs;
1884 			uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1885 			int num_hmacs;
1886 
1887 			if (plen > sizeof(store)) {
1888 				break;
1889 			}
1890 			phdr = sctp_get_next_param(m, offset,
1891 			    (struct sctp_paramhdr *)store,
1892 			    plen);
1893 			if (phdr == NULL) {
1894 				return (-1);
1895 			}
1896 			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1897 			num_hmacs = (plen - sizeof(*hmacs)) / sizeof(hmacs->hmac_ids[0]);
1898 			/* validate the hmac list */
1899 			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1900 				SCTPDBG(SCTP_DEBUG_AUTH1,
1901 				    "SCTP: invalid HMAC param\n");
1902 				return (-1);
1903 			}
1904 			got_hmacs = 1;
1905 		} else if (ptype == SCTP_CHUNK_LIST) {
1906 			struct sctp_auth_chunk_list *chunks;
1907 			uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1908 			int i, num_chunks;
1909 
1910 			if (plen > sizeof(chunks_store)) {
1911 				break;
1912 			}
1913 			phdr = sctp_get_next_param(m, offset,
1914 			    (struct sctp_paramhdr *)chunks_store,
1915 			    plen);
1916 			if (phdr == NULL) {
1917 				return (-1);
1918 			}
1919 			/*-
1920 			 * Flip through the list and mark that the
1921 			 * peer supports asconf/asconf_ack.
1922 			 */
1923 			chunks = (struct sctp_auth_chunk_list *)phdr;
1924 			num_chunks = plen - sizeof(*chunks);
1925 			for (i = 0; i < num_chunks; i++) {
1926 				/* record asconf/asconf-ack if listed */
1927 				if (chunks->chunk_types[i] == SCTP_ASCONF)
1928 					saw_asconf = 1;
1929 				if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1930 					saw_asconf_ack = 1;
1931 
1932 			}
1933 			if (num_chunks)
1934 				got_chklist = 1;
1935 		}
1936 
1937 		offset += SCTP_SIZE32(plen);
1938 		if (offset >= limit) {
1939 			break;
1940 		}
1941 		phdr = sctp_get_next_param(m, offset, &param_buf,
1942 		    sizeof(param_buf));
1943 	}
1944 	/* validate authentication required parameters */
1945 	if (got_random && got_hmacs) {
1946 		peer_supports_auth = 1;
1947 	} else {
1948 		peer_supports_auth = 0;
1949 	}
1950 	if (!peer_supports_auth && got_chklist) {
1951 		SCTPDBG(SCTP_DEBUG_AUTH1,
1952 		    "SCTP: peer sent chunk list w/o AUTH\n");
1953 		return (-1);
1954 	}
1955 	if (peer_supports_asconf && !peer_supports_auth) {
1956 		SCTPDBG(SCTP_DEBUG_AUTH1,
1957 		    "SCTP: peer supports ASCONF but not AUTH\n");
1958 		return (-1);
1959 	} else if ((peer_supports_asconf) && (peer_supports_auth) &&
1960 	    ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1961 		return (-2);
1962 	}
1963 	return (0);
1964 }
1965 
1966 void
1967 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1968 {
1969 	uint16_t chunks_len = 0;
1970 	uint16_t hmacs_len = 0;
1971 	uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1972 	sctp_key_t *new_key;
1973 	uint16_t keylen;
1974 
1975 	/* initialize hmac list from endpoint */
1976 	stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1977 	if (stcb->asoc.local_hmacs != NULL) {
1978 		hmacs_len = stcb->asoc.local_hmacs->num_algo *
1979 		    sizeof(stcb->asoc.local_hmacs->hmac[0]);
1980 	}
1981 	/* initialize auth chunks list from endpoint */
1982 	stcb->asoc.local_auth_chunks =
1983 	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1984 	if (stcb->asoc.local_auth_chunks != NULL) {
1985 		int i;
1986 
1987 		for (i = 0; i < 256; i++) {
1988 			if (stcb->asoc.local_auth_chunks->chunks[i])
1989 				chunks_len++;
1990 		}
1991 	}
1992 	/* copy defaults from the endpoint */
1993 	stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
1994 
1995 	/* copy out the shared key list (by reference) from the endpoint */
1996 	(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
1997 	    &stcb->asoc.shared_keys);
1998 
1999 	/* now set the concatenated key (random + chunks + hmacs) */
2000 	/* key includes parameter headers */
2001 	keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2002 	    hmacs_len;
2003 	new_key = sctp_alloc_key(keylen);
2004 	if (new_key != NULL) {
2005 		struct sctp_paramhdr *ph;
2006 		int plen;
2007 
2008 		/* generate and copy in the RANDOM */
2009 		ph = (struct sctp_paramhdr *)new_key->key;
2010 		ph->param_type = htons(SCTP_RANDOM);
2011 		plen = sizeof(*ph) + random_len;
2012 		ph->param_length = htons(plen);
2013 		SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2014 		keylen = plen;
2015 
2016 		/* append in the AUTH chunks */
2017 		/* NOTE: currently we always have chunks to list */
2018 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2019 		ph->param_type = htons(SCTP_CHUNK_LIST);
2020 		plen = sizeof(*ph) + chunks_len;
2021 		ph->param_length = htons(plen);
2022 		keylen += sizeof(*ph);
2023 		if (stcb->asoc.local_auth_chunks) {
2024 			int i;
2025 
2026 			for (i = 0; i < 256; i++) {
2027 				if (stcb->asoc.local_auth_chunks->chunks[i])
2028 					new_key->key[keylen++] = i;
2029 			}
2030 		}
2031 
2032 		/* append in the HMACs */
2033 		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2034 		ph->param_type = htons(SCTP_HMAC_LIST);
2035 		plen = sizeof(*ph) + hmacs_len;
2036 		ph->param_length = htons(plen);
2037 		keylen += sizeof(*ph);
2038 		(void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2039 		    new_key->key + keylen);
2040 	}
2041 	if (stcb->asoc.authinfo.random != NULL)
2042 		sctp_free_key(stcb->asoc.authinfo.random);
2043 	stcb->asoc.authinfo.random = new_key;
2044 	stcb->asoc.authinfo.random_len = random_len;
2045 }
2046