sctp_auth.c revision 166675
1/*-
2 * Copyright (c) 2001-2006, Cisco Systems, Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are met:
6 *
7 * a) Redistributions of source code must retain the above copyright notice,
8 *   this list of conditions and the following disclaimer.
9 *
10 * b) Redistributions in binary form must reproduce the above copyright
11 *    notice, this list of conditions and the following disclaimer in
12 *   the documentation and/or other materials provided with the distribution.
13 *
14 * c) Neither the name of Cisco Systems, Inc. nor the names of its
15 *    contributors may be used to endorse or promote products derived
16 *    from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
22 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
28 * THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#include <sys/cdefs.h>
32__FBSDID("$FreeBSD: head/sys/netinet/sctp_auth.c 166675 2007-02-12 23:24:31Z rrs $");
33
34#include <netinet/sctp_os.h>
35#include <netinet/sctp.h>
36#include <netinet/sctp_header.h>
37#include <netinet/sctp_pcb.h>
38#include <netinet/sctp_var.h>
39#include <netinet/sctputil.h>
40#include <netinet/sctp_indata.h>
41#include <netinet/sctp_output.h>
42#include <netinet/sctp_auth.h>
43
44#ifdef SCTP_DEBUG
45extern uint32_t sctp_debug_on;
46
47#define SCTP_AUTH_DEBUG		(sctp_debug_on & SCTP_DEBUG_AUTH1)
48#define SCTP_AUTH_DEBUG2	(sctp_debug_on & SCTP_DEBUG_AUTH2)
49#endif				/* SCTP_DEBUG */
50
51
52inline void
53sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
54{
55	bzero(chklist, sizeof(*chklist));
56	/* chklist->num_chunks = 0; */
57}
58
59sctp_auth_chklist_t *
60sctp_alloc_chunklist(void)
61{
62	sctp_auth_chklist_t *chklist;
63
64	SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
65	    "AUTH chklist");
66	if (chklist == NULL) {
67#ifdef SCTP_DEBUG
68		if (sctp_debug_on & SCTP_AUTH_DEBUG) {
69			printf("sctp_alloc_chunklist: failed to get memory!\n");
70		}
71#endif				/* SCTP_DEBUG */
72	} else {
73		sctp_clear_chunklist(chklist);
74	}
75	return (chklist);
76}
77
78void
79sctp_free_chunklist(sctp_auth_chklist_t * list)
80{
81	if (list != NULL)
82		SCTP_FREE(list);
83}
84
85sctp_auth_chklist_t *
86sctp_copy_chunklist(sctp_auth_chklist_t * list)
87{
88	sctp_auth_chklist_t *new_list;
89
90	if (list == NULL)
91		return (NULL);
92
93	/* get a new list */
94	new_list = sctp_alloc_chunklist();
95	if (new_list == NULL)
96		return (NULL);
97	/* copy it */
98	bcopy(list, new_list, sizeof(*new_list));
99
100	return (new_list);
101}
102
103
104/*
105 * add a chunk to the required chunks list
106 */
107int
108sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
109{
110	if (list == NULL)
111		return (-1);
112
113	/* is chunk restricted? */
114	if ((chunk == SCTP_INITIATION) ||
115	    (chunk == SCTP_INITIATION_ACK) ||
116	    (chunk == SCTP_SHUTDOWN_COMPLETE) ||
117	    (chunk == SCTP_AUTHENTICATION)) {
118		return (-1);
119	}
120	if (list->chunks[chunk] == 0) {
121		list->chunks[chunk] = 1;
122		list->num_chunks++;
123#ifdef SCTP_DEBUG
124		if (SCTP_AUTH_DEBUG)
125			printf("SCTP: added chunk %u (0x%02x) to Auth list\n",
126			    chunk, chunk);
127#endif
128	}
129	return (0);
130}
131
132/*
133 * delete a chunk from the required chunks list
134 */
135int
136sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
137{
138	if (list == NULL)
139		return (-1);
140
141	/* is chunk restricted? */
142	if ((chunk == SCTP_ASCONF) ||
143	    (chunk == SCTP_ASCONF_ACK)) {
144		return (-1);
145	}
146	if (list->chunks[chunk] == 1) {
147		list->chunks[chunk] = 0;
148		list->num_chunks--;
149#ifdef SCTP_DEBUG
150		if (SCTP_AUTH_DEBUG)
151			printf("SCTP: deleted chunk %u (0x%02x) from Auth list\n",
152			    chunk, chunk);
153#endif
154	}
155	return (0);
156}
157
158inline size_t
159sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
160{
161	if (list == NULL)
162		return (0);
163	else
164		return (list->num_chunks);
165}
166
167/*
168 * set the default list of chunks requiring AUTH
169 */
170void
171sctp_auth_set_default_chunks(sctp_auth_chklist_t * list)
172{
173	sctp_auth_add_chunk(SCTP_ASCONF, list);
174	sctp_auth_add_chunk(SCTP_ASCONF_ACK, list);
175}
176
177/*
178 * return the current number and list of required chunks caller must
179 * guarantee ptr has space for up to 256 bytes
180 */
181int
182sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
183{
184	int i, count = 0;
185
186	if (list == NULL)
187		return (0);
188
189	for (i = 0; i < 256; i++) {
190		if (list->chunks[i] != 0) {
191			*ptr++ = i;
192			count++;
193		}
194	}
195	return (count);
196}
197
198int
199sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
200{
201	int i, size = 0;
202
203	if (list == NULL)
204		return (0);
205
206	if (list->num_chunks <= 32) {
207		/* just list them, one byte each */
208		for (i = 0; i < 256; i++) {
209			if (list->chunks[i] != 0) {
210				*ptr++ = i;
211				size++;
212			}
213		}
214	} else {
215		int index, offset;
216
217		/* pack into a 32 byte bitfield */
218		for (i = 0; i < 256; i++) {
219			if (list->chunks[i] != 0) {
220				index = i / 8;
221				offset = i % 8;
222				ptr[index] |= (1 << offset);
223			}
224		}
225		size = 32;
226	}
227	return (size);
228}
229
230int
231sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
232    sctp_auth_chklist_t * list)
233{
234	int i;
235	int size;
236
237	if (list == NULL)
238		return (0);
239
240	if (num_chunks <= 32) {
241		/* just pull them, one byte each */
242		for (i = 0; i < num_chunks; i++) {
243			sctp_auth_add_chunk(*ptr++, list);
244		}
245		size = num_chunks;
246	} else {
247		int index, offset;
248
249		/* unpack from a 32 byte bitfield */
250		for (index = 0; index < 32; index++) {
251			for (offset = 0; offset < 8; offset++) {
252				if (ptr[index] & (1 << offset)) {
253					sctp_auth_add_chunk((index * 8) + offset, list);
254				}
255			}
256		}
257		size = 32;
258	}
259	return (size);
260}
261
262
263/*
264 * allocate structure space for a key of length keylen
265 */
266sctp_key_t *
267sctp_alloc_key(uint32_t keylen)
268{
269	sctp_key_t *new_key;
270
271	SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
272	    "AUTH key");
273	if (new_key == NULL) {
274		/* out of memory */
275		return (NULL);
276	}
277	new_key->keylen = keylen;
278	return (new_key);
279}
280
281void
282sctp_free_key(sctp_key_t * key)
283{
284	if (key != NULL)
285		SCTP_FREE(key);
286}
287
288void
289sctp_print_key(sctp_key_t * key, const char *str)
290{
291	uint32_t i;
292
293	if (key == NULL) {
294		printf("%s: [Null key]\n", str);
295		return;
296	}
297	printf("%s: len %u, ", str, key->keylen);
298	if (key->keylen) {
299		for (i = 0; i < key->keylen; i++)
300			printf("%02x", key->key[i]);
301		printf("\n");
302	} else {
303		printf("[Null key]\n");
304	}
305}
306
307void
308sctp_show_key(sctp_key_t * key, const char *str)
309{
310	uint32_t i;
311
312	if (key == NULL) {
313		printf("%s: [Null key]\n", str);
314		return;
315	}
316	printf("%s: len %u, ", str, key->keylen);
317	if (key->keylen) {
318		for (i = 0; i < key->keylen; i++)
319			printf("%02x", key->key[i]);
320		printf("\n");
321	} else {
322		printf("[Null key]\n");
323	}
324}
325
326static inline uint32_t
327sctp_get_keylen(sctp_key_t * key)
328{
329	if (key != NULL)
330		return (key->keylen);
331	else
332		return (0);
333}
334
335/*
336 * generate a new random key of length 'keylen'
337 */
338sctp_key_t *
339sctp_generate_random_key(uint32_t keylen)
340{
341	sctp_key_t *new_key;
342
343	/* validate keylen */
344	if (keylen > SCTP_AUTH_RANDOM_SIZE_MAX)
345		keylen = SCTP_AUTH_RANDOM_SIZE_MAX;
346
347	new_key = sctp_alloc_key(keylen);
348	if (new_key == NULL) {
349		/* out of memory */
350		return (NULL);
351	}
352	SCTP_READ_RANDOM(new_key->key, keylen);
353	new_key->keylen = keylen;
354	return (new_key);
355}
356
357sctp_key_t *
358sctp_set_key(uint8_t * key, uint32_t keylen)
359{
360	sctp_key_t *new_key;
361
362	new_key = sctp_alloc_key(keylen);
363	if (new_key == NULL) {
364		/* out of memory */
365		return (NULL);
366	}
367	bcopy(key, new_key->key, keylen);
368	return (new_key);
369}
370
371/*
372 * given two keys of variable size, compute which key is "larger/smaller"
373 * returns: 1 if key1 > key2 -1 if key1 < key2 0 if key1 = key2
374 */
375static int
376sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
377{
378	uint32_t maxlen;
379	uint32_t i;
380	uint32_t key1len, key2len;
381	uint8_t *key_1, *key_2;
382	uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
383
384	/* sanity/length check */
385	key1len = sctp_get_keylen(key1);
386	key2len = sctp_get_keylen(key2);
387	if ((key1len == 0) && (key2len == 0))
388		return (0);
389	else if (key1len == 0)
390		return (-1);
391	else if (key2len == 0)
392		return (1);
393
394	if (key1len != key2len) {
395		if (key1len >= key2len)
396			maxlen = key1len;
397		else
398			maxlen = key2len;
399		bzero(temp, maxlen);
400		if (key1len < maxlen) {
401			/* prepend zeroes to key1 */
402			bcopy(key1->key, temp + (maxlen - key1len), key1len);
403			key_1 = temp;
404			key_2 = key2->key;
405		} else {
406			/* prepend zeroes to key2 */
407			bcopy(key2->key, temp + (maxlen - key2len), key2len);
408			key_1 = key1->key;
409			key_2 = temp;
410		}
411	} else {
412		maxlen = key1len;
413		key_1 = key1->key;
414		key_2 = key2->key;
415	}
416
417	for (i = 0; i < maxlen; i++) {
418		if (*key_1 > *key_2)
419			return (1);
420		else if (*key_1 < *key_2)
421			return (-1);
422		key_1++;
423		key_2++;
424	}
425
426	/* keys are equal value, so check lengths */
427	if (key1len == key2len)
428		return (0);
429	else if (key1len < key2len)
430		return (-1);
431	else
432		return (1);
433}
434
435/*
436 * generate the concatenated keying material based on the two keys and the
437 * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
438 * order for concatenation
439 */
440sctp_key_t *
441sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
442{
443	uint32_t keylen;
444	sctp_key_t *new_key;
445	uint8_t *key_ptr;
446
447	keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
448	    sctp_get_keylen(shared);
449
450	if (keylen > 0) {
451		/* get space for the new key */
452		new_key = sctp_alloc_key(keylen);
453		if (new_key == NULL) {
454			/* out of memory */
455			return (NULL);
456		}
457		new_key->keylen = keylen;
458		key_ptr = new_key->key;
459	} else {
460		/* all keys empty/null?! */
461		return (NULL);
462	}
463
464	/* concatenate the keys */
465	if (sctp_compare_key(key1, key2) <= 0) {
466		/* key is key1 + shared + key2 */
467		if (sctp_get_keylen(key1)) {
468			bcopy(key1->key, key_ptr, key1->keylen);
469			key_ptr += key1->keylen;
470		}
471		if (sctp_get_keylen(shared)) {
472			bcopy(shared->key, key_ptr, shared->keylen);
473			key_ptr += shared->keylen;
474		}
475		if (sctp_get_keylen(key2)) {
476			bcopy(key2->key, key_ptr, key2->keylen);
477			key_ptr += key2->keylen;
478		}
479	} else {
480		/* key is key2 + shared + key1 */
481		if (sctp_get_keylen(key2)) {
482			bcopy(key2->key, key_ptr, key2->keylen);
483			key_ptr += key2->keylen;
484		}
485		if (sctp_get_keylen(shared)) {
486			bcopy(shared->key, key_ptr, shared->keylen);
487			key_ptr += shared->keylen;
488		}
489		if (sctp_get_keylen(key1)) {
490			bcopy(key1->key, key_ptr, key1->keylen);
491			key_ptr += key1->keylen;
492		}
493	}
494	return (new_key);
495}
496
497
498sctp_sharedkey_t *
499sctp_alloc_sharedkey(void)
500{
501	sctp_sharedkey_t *new_key;
502
503	SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
504	    "AUTH skey");
505	if (new_key == NULL) {
506		/* out of memory */
507		return (NULL);
508	}
509	new_key->keyid = 0;
510	new_key->key = NULL;
511	return (new_key);
512}
513
514void
515sctp_free_sharedkey(sctp_sharedkey_t * skey)
516{
517	if (skey != NULL) {
518		if (skey->key != NULL)
519			sctp_free_key(skey->key);
520		SCTP_FREE(skey);
521	}
522}
523
524sctp_sharedkey_t *
525sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
526{
527	sctp_sharedkey_t *skey;
528
529	LIST_FOREACH(skey, shared_keys, next) {
530		if (skey->keyid == key_id)
531			return (skey);
532	}
533	return (NULL);
534}
535
536void
537sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
538    sctp_sharedkey_t * new_skey)
539{
540	sctp_sharedkey_t *skey;
541
542	if ((shared_keys == NULL) || (new_skey == NULL))
543		return;
544
545	/* insert into an empty list? */
546	if (SCTP_LIST_EMPTY(shared_keys)) {
547		LIST_INSERT_HEAD(shared_keys, new_skey, next);
548		return;
549	}
550	/* insert into the existing list, ordered by key id */
551	LIST_FOREACH(skey, shared_keys, next) {
552		if (new_skey->keyid < skey->keyid) {
553			/* insert it before here */
554			LIST_INSERT_BEFORE(skey, new_skey, next);
555			return;
556		} else if (new_skey->keyid == skey->keyid) {
557			/* replace the existing key */
558#ifdef SCTP_DEBUG
559			if (SCTP_AUTH_DEBUG)
560				printf("replacing shared key id %u\n", new_skey->keyid);
561#endif
562			LIST_INSERT_BEFORE(skey, new_skey, next);
563			LIST_REMOVE(skey, next);
564			sctp_free_sharedkey(skey);
565			return;
566		}
567		if (LIST_NEXT(skey, next) == NULL) {
568			/* belongs at the end of the list */
569			LIST_INSERT_AFTER(skey, new_skey, next);
570			return;
571		}
572	}
573}
574
575static sctp_sharedkey_t *
576sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
577{
578	sctp_sharedkey_t *new_skey;
579
580	if (skey == NULL)
581		return (NULL);
582	new_skey = sctp_alloc_sharedkey();
583	if (new_skey == NULL)
584		return (NULL);
585	if (skey->key != NULL)
586		new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
587	else
588		new_skey->key = NULL;
589	new_skey->keyid = skey->keyid;
590	return (new_skey);
591}
592
593int
594sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
595{
596	sctp_sharedkey_t *skey, *new_skey;
597	int count = 0;
598
599	if ((src == NULL) || (dest == NULL))
600		return (0);
601	LIST_FOREACH(skey, src, next) {
602		new_skey = sctp_copy_sharedkey(skey);
603		if (new_skey != NULL) {
604			sctp_insert_sharedkey(dest, new_skey);
605			count++;
606		}
607	}
608	return (count);
609}
610
611
612sctp_hmaclist_t *
613sctp_alloc_hmaclist(uint8_t num_hmacs)
614{
615	sctp_hmaclist_t *new_list;
616	int alloc_size;
617
618	alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
619	SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
620	    "AUTH HMAC list");
621	if (new_list == NULL) {
622		/* out of memory */
623		return (NULL);
624	}
625	new_list->max_algo = num_hmacs;
626	new_list->num_algo = 0;
627	return (new_list);
628}
629
630void
631sctp_free_hmaclist(sctp_hmaclist_t * list)
632{
633	if (list != NULL) {
634		SCTP_FREE(list);
635		list = NULL;
636	}
637}
638
639int
640sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
641{
642	if (list == NULL)
643		return (-1);
644	if (list->num_algo == list->max_algo) {
645#ifdef SCTP_DEBUG
646		if (SCTP_AUTH_DEBUG)
647			printf("SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
648#endif
649		return (-1);
650	}
651	if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
652#ifdef HAVE_SHA224
653	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
654#endif
655#ifdef HAVE_SHA2
656	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
657	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
658	    (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
659#endif
660	    (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
661		return (-1);
662	}
663#ifdef SCTP_DEBUG
664	if (SCTP_AUTH_DEBUG)
665		printf("SCTP: add HMAC id %u to list\n", hmac_id);
666#endif
667	list->hmac[list->num_algo++] = hmac_id;
668	return (0);
669}
670
671sctp_hmaclist_t *
672sctp_copy_hmaclist(sctp_hmaclist_t * list)
673{
674	sctp_hmaclist_t *new_list;
675	int i;
676
677	if (list == NULL)
678		return (NULL);
679	/* get a new list */
680	new_list = sctp_alloc_hmaclist(list->max_algo);
681	if (new_list == NULL)
682		return (NULL);
683	/* copy it */
684	new_list->max_algo = list->max_algo;
685	new_list->num_algo = list->num_algo;
686	for (i = 0; i < list->num_algo; i++)
687		new_list->hmac[i] = list->hmac[i];
688	return (new_list);
689}
690
691sctp_hmaclist_t *
692sctp_default_supported_hmaclist(void)
693{
694	sctp_hmaclist_t *new_list;
695
696	new_list = sctp_alloc_hmaclist(2);
697	if (new_list == NULL)
698		return (NULL);
699	sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
700	sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
701	return (new_list);
702}
703
704/*
705 * HMAC algos are listed in priority/preference order find the best HMAC id
706 * to use for the peer based on local support
707 */
708uint16_t
709sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
710{
711	int i, j;
712
713	if ((local == NULL) || (peer == NULL))
714		return (SCTP_AUTH_HMAC_ID_RSVD);
715
716	for (i = 0; i < peer->num_algo; i++) {
717		for (j = 0; j < local->num_algo; j++) {
718			if (peer->hmac[i] == local->hmac[j]) {
719#ifndef SCTP_AUTH_DRAFT_04
720				/* "skip" MD5 as it's been deprecated */
721				if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
722					continue;
723#endif
724
725				/* found the "best" one */
726#ifdef SCTP_DEBUG
727				if (SCTP_AUTH_DEBUG)
728					printf("SCTP: negotiated peer HMAC id %u\n", peer->hmac[i]);
729#endif
730				return (peer->hmac[i]);
731			}
732		}
733	}
734	/* didn't find one! */
735	return (SCTP_AUTH_HMAC_ID_RSVD);
736}
737
738/*
739 * serialize the HMAC algo list and return space used caller must guarantee
740 * ptr has appropriate space
741 */
742int
743sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
744{
745	int i;
746	uint16_t hmac_id;
747
748	if (list == NULL)
749		return (0);
750
751	for (i = 0; i < list->num_algo; i++) {
752		hmac_id = htons(list->hmac[i]);
753		bcopy(&hmac_id, ptr, sizeof(hmac_id));
754		ptr += sizeof(hmac_id);
755	}
756	return (list->num_algo * sizeof(hmac_id));
757}
758
759int
760sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
761{
762	uint32_t i;
763	uint16_t hmac_id;
764	uint32_t sha1_supported = 0;
765
766	for (i = 0; i < num_hmacs; i++) {
767		hmac_id = ntohs(hmacs->hmac_ids[i]);
768		if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
769			sha1_supported = 1;
770	}
771	/* all HMAC id's are supported */
772	if (sha1_supported == 0)
773		return (-1);
774	else
775		return (0);
776}
777
778sctp_authinfo_t *
779sctp_alloc_authinfo(void)
780{
781	sctp_authinfo_t *new_authinfo;
782
783	SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
784	    "AUTH info");
785	if (new_authinfo == NULL) {
786		/* out of memory */
787		return (NULL);
788	}
789	bzero(&new_authinfo, sizeof(*new_authinfo));
790	return (new_authinfo);
791}
792
793void
794sctp_free_authinfo(sctp_authinfo_t * authinfo)
795{
796	if (authinfo == NULL)
797		return;
798
799	if (authinfo->random != NULL)
800		sctp_free_key(authinfo->random);
801	if (authinfo->peer_random != NULL)
802		sctp_free_key(authinfo->peer_random);
803	if (authinfo->assoc_key != NULL)
804		sctp_free_key(authinfo->assoc_key);
805	if (authinfo->recv_key != NULL)
806		sctp_free_key(authinfo->recv_key);
807
808	/* We are NOT dynamically allocating authinfo's right now... */
809	/* SCTP_FREE(authinfo); */
810}
811
812
813inline uint32_t
814sctp_get_auth_chunk_len(uint16_t hmac_algo)
815{
816	int size;
817
818	size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
819	return (SCTP_SIZE32(size));
820}
821
822uint32_t
823sctp_get_hmac_digest_len(uint16_t hmac_algo)
824{
825	switch (hmac_algo) {
826	case SCTP_AUTH_HMAC_ID_SHA1:
827		return (SCTP_AUTH_DIGEST_LEN_SHA1);
828	case SCTP_AUTH_HMAC_ID_MD5:
829		return (SCTP_AUTH_DIGEST_LEN_MD5);
830#ifdef HAVE_SHA224
831	case SCTP_AUTH_HMAC_ID_SHA224:
832		return (SCTP_AUTH_DIGEST_LEN_SHA224);
833#endif
834#ifdef HAVE_SHA2
835	case SCTP_AUTH_HMAC_ID_SHA256:
836		return (SCTP_AUTH_DIGEST_LEN_SHA256);
837	case SCTP_AUTH_HMAC_ID_SHA384:
838		return (SCTP_AUTH_DIGEST_LEN_SHA384);
839	case SCTP_AUTH_HMAC_ID_SHA512:
840		return (SCTP_AUTH_DIGEST_LEN_SHA512);
841#endif
842	default:
843		/* unknown HMAC algorithm: can't do anything */
844		return (0);
845	}			/* end switch */
846}
847
848static inline int
849sctp_get_hmac_block_len(uint16_t hmac_algo)
850{
851	switch (hmac_algo) {
852		case SCTP_AUTH_HMAC_ID_SHA1:
853		case SCTP_AUTH_HMAC_ID_MD5:
854#ifdef HAVE_SHA224
855		case SCTP_AUTH_HMAC_ID_SHA224:
856		return (64);
857#endif
858#ifdef HAVE_SHA2
859	case SCTP_AUTH_HMAC_ID_SHA256:
860		return (64);
861	case SCTP_AUTH_HMAC_ID_SHA384:
862	case SCTP_AUTH_HMAC_ID_SHA512:
863		return (128);
864#endif
865	case SCTP_AUTH_HMAC_ID_RSVD:
866	default:
867		/* unknown HMAC algorithm: can't do anything */
868		return (0);
869	}			/* end switch */
870}
871
872static void
873sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
874{
875	switch (hmac_algo) {
876		case SCTP_AUTH_HMAC_ID_SHA1:
877		SHA1_Init(&ctx->sha1);
878		break;
879	case SCTP_AUTH_HMAC_ID_MD5:
880		MD5_Init(&ctx->md5);
881		break;
882#ifdef HAVE_SHA224
883	case SCTP_AUTH_HMAC_ID_SHA224:
884		break;
885#endif
886#ifdef HAVE_SHA2
887	case SCTP_AUTH_HMAC_ID_SHA256:
888		SHA256_Init(&ctx->sha256);
889		break;
890	case SCTP_AUTH_HMAC_ID_SHA384:
891		SHA384_Init(&ctx->sha384);
892		break;
893	case SCTP_AUTH_HMAC_ID_SHA512:
894		SHA512_Init(&ctx->sha512);
895		break;
896#endif
897	case SCTP_AUTH_HMAC_ID_RSVD:
898	default:
899		/* unknown HMAC algorithm: can't do anything */
900		return;
901	}			/* end switch */
902}
903
904static void
905sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
906    uint8_t * text, uint32_t textlen)
907{
908	switch (hmac_algo) {
909		case SCTP_AUTH_HMAC_ID_SHA1:
910		SHA1_Update(&ctx->sha1, text, textlen);
911		break;
912	case SCTP_AUTH_HMAC_ID_MD5:
913		MD5_Update(&ctx->md5, text, textlen);
914		break;
915#ifdef HAVE_SHA224
916	case SCTP_AUTH_HMAC_ID_SHA224:
917		break;
918#endif
919#ifdef HAVE_SHA2
920	case SCTP_AUTH_HMAC_ID_SHA256:
921		SHA256_Update(&ctx->sha256, text, textlen);
922		break;
923	case SCTP_AUTH_HMAC_ID_SHA384:
924		SHA384_Update(&ctx->sha384, text, textlen);
925		break;
926	case SCTP_AUTH_HMAC_ID_SHA512:
927		SHA512_Update(&ctx->sha512, text, textlen);
928		break;
929#endif
930	case SCTP_AUTH_HMAC_ID_RSVD:
931	default:
932		/* unknown HMAC algorithm: can't do anything */
933		return;
934	}			/* end switch */
935}
936
937static void
938sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
939    uint8_t * digest)
940{
941	switch (hmac_algo) {
942		case SCTP_AUTH_HMAC_ID_SHA1:
943		SHA1_Final(digest, &ctx->sha1);
944		break;
945	case SCTP_AUTH_HMAC_ID_MD5:
946		MD5_Final(digest, &ctx->md5);
947		break;
948#ifdef HAVE_SHA224
949	case SCTP_AUTH_HMAC_ID_SHA224:
950		break;
951#endif
952#ifdef HAVE_SHA2
953	case SCTP_AUTH_HMAC_ID_SHA256:
954		SHA256_Final(digest, &ctx->sha256);
955		break;
956	case SCTP_AUTH_HMAC_ID_SHA384:
957		/* SHA384 is truncated SHA512 */
958		SHA384_Final(digest, &ctx->sha384);
959		break;
960	case SCTP_AUTH_HMAC_ID_SHA512:
961		SHA512_Final(digest, &ctx->sha512);
962		break;
963#endif
964	case SCTP_AUTH_HMAC_ID_RSVD:
965	default:
966		/* unknown HMAC algorithm: can't do anything */
967		return;
968	}			/* end switch */
969}
970
971/*
972 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
973 *
974 * Compute the HMAC digest using the desired hash key, text, and HMAC
975 * algorithm.  Resulting digest is placed in 'digest' and digest length
976 * is returned, if the HMAC was performed.
977 *
978 * WARNING: it is up to the caller to supply sufficient space to hold the
979 * resultant digest.
980 */
981uint32_t
982sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
983    uint8_t * text, uint32_t textlen, uint8_t * digest)
984{
985	uint32_t digestlen;
986	uint32_t blocklen;
987	sctp_hash_context_t ctx;
988	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
989	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
990	uint32_t i;
991
992	/* sanity check the material and length */
993	if ((key == NULL) || (keylen == 0) || (text == NULL) ||
994	    (textlen == 0) || (digest == NULL)) {
995		/* can't do HMAC with empty key or text or digest store */
996		return (0);
997	}
998	/* validate the hmac algo and get the digest length */
999	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1000	if (digestlen == 0)
1001		return (0);
1002
1003	/* hash the key if it is longer than the hash block size */
1004	blocklen = sctp_get_hmac_block_len(hmac_algo);
1005	if (keylen > blocklen) {
1006		sctp_hmac_init(hmac_algo, &ctx);
1007		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1008		sctp_hmac_final(hmac_algo, &ctx, temp);
1009		/* set the hashed key as the key */
1010		keylen = digestlen;
1011		key = temp;
1012	}
1013	/* initialize the inner/outer pads with the key and "append" zeroes */
1014	bzero(ipad, blocklen);
1015	bzero(opad, blocklen);
1016	bcopy(key, ipad, keylen);
1017	bcopy(key, opad, keylen);
1018
1019	/* XOR the key with ipad and opad values */
1020	for (i = 0; i < blocklen; i++) {
1021		ipad[i] ^= 0x36;
1022		opad[i] ^= 0x5c;
1023	}
1024
1025	/* perform inner hash */
1026	sctp_hmac_init(hmac_algo, &ctx);
1027	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1028	sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1029	sctp_hmac_final(hmac_algo, &ctx, temp);
1030
1031	/* perform outer hash */
1032	sctp_hmac_init(hmac_algo, &ctx);
1033	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1034	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1035	sctp_hmac_final(hmac_algo, &ctx, digest);
1036
1037	return (digestlen);
1038}
1039
1040/* mbuf version */
1041uint32_t
1042sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1043    struct mbuf *m, uint32_t m_offset, uint8_t * digest)
1044{
1045	uint32_t digestlen;
1046	uint32_t blocklen;
1047	sctp_hash_context_t ctx;
1048	uint8_t ipad[128], opad[128];	/* keyed hash inner/outer pads */
1049	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1050	uint32_t i;
1051	struct mbuf *m_tmp;
1052
1053	/* sanity check the material and length */
1054	if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1055		/* can't do HMAC with empty key or text or digest store */
1056		return (0);
1057	}
1058	/* validate the hmac algo and get the digest length */
1059	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1060	if (digestlen == 0)
1061		return (0);
1062
1063	/* hash the key if it is longer than the hash block size */
1064	blocklen = sctp_get_hmac_block_len(hmac_algo);
1065	if (keylen > blocklen) {
1066		sctp_hmac_init(hmac_algo, &ctx);
1067		sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1068		sctp_hmac_final(hmac_algo, &ctx, temp);
1069		/* set the hashed key as the key */
1070		keylen = digestlen;
1071		key = temp;
1072	}
1073	/* initialize the inner/outer pads with the key and "append" zeroes */
1074	bzero(ipad, blocklen);
1075	bzero(opad, blocklen);
1076	bcopy(key, ipad, keylen);
1077	bcopy(key, opad, keylen);
1078
1079	/* XOR the key with ipad and opad values */
1080	for (i = 0; i < blocklen; i++) {
1081		ipad[i] ^= 0x36;
1082		opad[i] ^= 0x5c;
1083	}
1084
1085	/* perform inner hash */
1086	sctp_hmac_init(hmac_algo, &ctx);
1087	sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1088	/* find the correct starting mbuf and offset (get start of text) */
1089	m_tmp = m;
1090	while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1091		m_offset -= SCTP_BUF_LEN(m_tmp);
1092		m_tmp = SCTP_BUF_NEXT(m_tmp);
1093	}
1094	/* now use the rest of the mbuf chain for the text */
1095	while (m_tmp != NULL) {
1096		sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1097		    SCTP_BUF_LEN(m_tmp) - m_offset);
1098
1099		/* clear the offset since it's only for the first mbuf */
1100		m_offset = 0;
1101		m_tmp = SCTP_BUF_NEXT(m_tmp);
1102	}
1103	sctp_hmac_final(hmac_algo, &ctx, temp);
1104
1105	/* perform outer hash */
1106	sctp_hmac_init(hmac_algo, &ctx);
1107	sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1108	sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1109	sctp_hmac_final(hmac_algo, &ctx, digest);
1110
1111	return (digestlen);
1112}
1113
1114/*
1115 * verify the HMAC digest using the desired hash key, text, and HMAC
1116 * algorithm. Returns -1 on error, 0 on success.
1117 */
1118int
1119sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1120    uint8_t * text, uint32_t textlen,
1121    uint8_t * digest, uint32_t digestlen)
1122{
1123	uint32_t len;
1124	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1125
1126	/* sanity check the material and length */
1127	if ((key == NULL) || (keylen == 0) ||
1128	    (text == NULL) || (textlen == 0) || (digest == NULL)) {
1129		/* can't do HMAC with empty key or text or digest */
1130		return (-1);
1131	}
1132	len = sctp_get_hmac_digest_len(hmac_algo);
1133	if ((len == 0) || (digestlen != len))
1134		return (-1);
1135
1136	/* compute the expected hash */
1137	if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1138		return (-1);
1139
1140	if (memcmp(digest, temp, digestlen) != 0)
1141		return (-1);
1142	else
1143		return (0);
1144}
1145
1146
1147/*
1148 * computes the requested HMAC using a key struct (which may be modified if
1149 * the keylen exceeds the HMAC block len).
1150 */
1151uint32_t
1152sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1153    uint32_t textlen, uint8_t * digest)
1154{
1155	uint32_t digestlen;
1156	uint32_t blocklen;
1157	sctp_hash_context_t ctx;
1158	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1159
1160	/* sanity check */
1161	if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1162	    (digest == NULL)) {
1163		/* can't do HMAC with empty key or text or digest store */
1164		return (0);
1165	}
1166	/* validate the hmac algo and get the digest length */
1167	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1168	if (digestlen == 0)
1169		return (0);
1170
1171	/* hash the key if it is longer than the hash block size */
1172	blocklen = sctp_get_hmac_block_len(hmac_algo);
1173	if (key->keylen > blocklen) {
1174		sctp_hmac_init(hmac_algo, &ctx);
1175		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1176		sctp_hmac_final(hmac_algo, &ctx, temp);
1177		/* save the hashed key as the new key */
1178		key->keylen = digestlen;
1179		bcopy(temp, key->key, key->keylen);
1180	}
1181	return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1182	    digest));
1183}
1184
1185/* mbuf version */
1186uint32_t
1187sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1188    uint32_t m_offset, uint8_t * digest)
1189{
1190	uint32_t digestlen;
1191	uint32_t blocklen;
1192	sctp_hash_context_t ctx;
1193	uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1194
1195	/* sanity check */
1196	if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1197		/* can't do HMAC with empty key or text or digest store */
1198		return (0);
1199	}
1200	/* validate the hmac algo and get the digest length */
1201	digestlen = sctp_get_hmac_digest_len(hmac_algo);
1202	if (digestlen == 0)
1203		return (0);
1204
1205	/* hash the key if it is longer than the hash block size */
1206	blocklen = sctp_get_hmac_block_len(hmac_algo);
1207	if (key->keylen > blocklen) {
1208		sctp_hmac_init(hmac_algo, &ctx);
1209		sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1210		sctp_hmac_final(hmac_algo, &ctx, temp);
1211		/* save the hashed key as the new key */
1212		key->keylen = digestlen;
1213		bcopy(temp, key->key, key->keylen);
1214	}
1215	return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest));
1216}
1217
1218int
1219sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1220{
1221	int i;
1222
1223	if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1224		return (0);
1225
1226	for (i = 0; i < list->num_algo; i++)
1227		if (list->hmac[i] == id)
1228			return (1);
1229
1230	/* not in the list */
1231	return (0);
1232}
1233
1234
1235/*
1236 * clear any cached key(s) if they match the given key id on an association
1237 * the cached key(s) will be recomputed and re-cached at next use. ASSUMES
1238 * TCB_LOCK is already held
1239 */
1240void
1241sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1242{
1243	if (stcb == NULL)
1244		return;
1245
1246	if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1247		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1248		stcb->asoc.authinfo.assoc_key = NULL;
1249	}
1250	if (keyid == stcb->asoc.authinfo.recv_keyid) {
1251		sctp_free_key(stcb->asoc.authinfo.recv_key);
1252		stcb->asoc.authinfo.recv_key = NULL;
1253	}
1254}
1255
1256/*
1257 * clear any cached key(s) if they match the given key id for all assocs on
1258 * an association ASSUMES INP_WLOCK is already held
1259 */
1260void
1261sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1262{
1263	struct sctp_tcb *stcb;
1264
1265	if (inp == NULL)
1266		return;
1267
1268	/* clear the cached keys on all assocs on this instance */
1269	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1270		SCTP_TCB_LOCK(stcb);
1271		sctp_clear_cachedkeys(stcb, keyid);
1272		SCTP_TCB_UNLOCK(stcb);
1273	}
1274}
1275
1276/*
1277 * delete a shared key from an association ASSUMES TCB_LOCK is already held
1278 */
1279int
1280sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1281{
1282	sctp_sharedkey_t *skey;
1283
1284	if (stcb == NULL)
1285		return (-1);
1286
1287	/* is the keyid the assoc active sending key */
1288	if (keyid == stcb->asoc.authinfo.assoc_keyid)
1289		return (-1);
1290
1291	/* does the key exist? */
1292	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1293	if (skey == NULL)
1294		return (-1);
1295
1296	/* remove it */
1297	LIST_REMOVE(skey, next);
1298	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1299
1300	/* clear any cached keys */
1301	sctp_clear_cachedkeys(stcb, keyid);
1302	return (0);
1303}
1304
1305/*
1306 * deletes a shared key from the endpoint ASSUMES INP_WLOCK is already held
1307 */
1308int
1309sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1310{
1311	sctp_sharedkey_t *skey;
1312	struct sctp_tcb *stcb;
1313
1314	if (inp == NULL)
1315		return (-1);
1316
1317	/* is the keyid the active sending key on the endpoint or any assoc */
1318	if (keyid == inp->sctp_ep.default_keyid)
1319		return (-1);
1320	LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1321		SCTP_TCB_LOCK(stcb);
1322		if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1323			SCTP_TCB_UNLOCK(stcb);
1324			return (-1);
1325		}
1326		SCTP_TCB_UNLOCK(stcb);
1327	}
1328
1329	/* does the key exist? */
1330	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1331	if (skey == NULL)
1332		return (-1);
1333
1334	/* remove it */
1335	LIST_REMOVE(skey, next);
1336	sctp_free_sharedkey(skey);	/* frees skey->key as well */
1337
1338	/* clear any cached keys */
1339	sctp_clear_cachedkeys_ep(inp, keyid);
1340	return (0);
1341}
1342
1343/*
1344 * set the active key on an association ASSUME TCB_LOCK is already held
1345 */
1346int
1347sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1348{
1349	sctp_sharedkey_t *skey = NULL;
1350	sctp_key_t *key = NULL;
1351	int using_ep_key = 0;
1352
1353	/* find the key on the assoc */
1354	skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1355	if (skey == NULL) {
1356		/* if not on the assoc, find the key on the endpoint */
1357		SCTP_INP_RLOCK(stcb->sctp_ep);
1358		skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1359		    keyid);
1360		using_ep_key = 1;
1361	}
1362	if (skey == NULL) {
1363		/* that key doesn't exist */
1364		if (using_ep_key)
1365			SCTP_INP_RUNLOCK(stcb->sctp_ep);
1366		return (-1);
1367	}
1368	/* get the shared key text */
1369	key = skey->key;
1370
1371	/* free any existing cached key */
1372	if (stcb->asoc.authinfo.assoc_key != NULL)
1373		sctp_free_key(stcb->asoc.authinfo.assoc_key);
1374	/* compute a new assoc key and cache it */
1375	stcb->asoc.authinfo.assoc_key =
1376	    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1377	    stcb->asoc.authinfo.peer_random, key);
1378	stcb->asoc.authinfo.assoc_keyid = keyid;
1379#ifdef SCTP_DEBUG
1380	if (SCTP_AUTH_DEBUG)
1381		sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1382#endif
1383
1384	if (using_ep_key)
1385		SCTP_INP_RUNLOCK(stcb->sctp_ep);
1386	return (0);
1387}
1388
1389/*
1390 * set the active key on an endpoint ASSUMES INP_WLOCK is already held
1391 */
1392int
1393sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1394{
1395	sctp_sharedkey_t *skey;
1396
1397	/* find the key */
1398	skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1399	if (skey == NULL) {
1400		/* that key doesn't exist */
1401		return (-1);
1402	}
1403	inp->sctp_ep.default_keyid = keyid;
1404	return (0);
1405}
1406
1407/*
1408 * get local authentication parameters from cookie (from INIT-ACK)
1409 */
1410void
1411sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1412    uint32_t offset, uint32_t length)
1413{
1414	struct sctp_paramhdr *phdr, tmp_param;
1415	uint16_t plen, ptype;
1416	uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1417	struct sctp_auth_random *random = NULL;
1418	uint16_t random_len = 0;
1419	uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1420	struct sctp_auth_hmac_algo *hmacs = NULL;
1421	uint16_t hmacs_len = 0;
1422	uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1423	struct sctp_auth_chunk_list *chunks = NULL;
1424	uint16_t num_chunks = 0;
1425	sctp_key_t *new_key;
1426	uint32_t keylen;
1427
1428	/* convert to upper bound */
1429	length += offset;
1430
1431	phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1432	    sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1433	while (phdr != NULL) {
1434		ptype = ntohs(phdr->param_type);
1435		plen = ntohs(phdr->param_length);
1436
1437		if ((plen == 0) || (offset + plen > length))
1438			break;
1439
1440		if (ptype == SCTP_RANDOM) {
1441			if (plen > sizeof(random_store))
1442				break;
1443			phdr = sctp_get_next_param(m, offset,
1444			    (struct sctp_paramhdr *)random_store, plen);
1445			if (phdr == NULL)
1446				return;
1447			/* save the random and length for the key */
1448			random = (struct sctp_auth_random *)phdr;
1449			random_len = plen - sizeof(*random);
1450		} else if (ptype == SCTP_HMAC_LIST) {
1451			int num_hmacs;
1452			int i;
1453
1454			if (plen > sizeof(hmacs_store))
1455				break;
1456			phdr = sctp_get_next_param(m, offset,
1457			    (struct sctp_paramhdr *)hmacs_store, plen);
1458			if (phdr == NULL)
1459				return;
1460			/* save the hmacs list and num for the key */
1461			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1462			hmacs_len = plen - sizeof(*hmacs);
1463			num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1464			if (stcb->asoc.local_hmacs != NULL)
1465				sctp_free_hmaclist(stcb->asoc.local_hmacs);
1466			stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1467			if (stcb->asoc.local_hmacs != NULL) {
1468				for (i = 0; i < num_hmacs; i++) {
1469					sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1470					    ntohs(hmacs->hmac_ids[i]));
1471				}
1472			}
1473		} else if (ptype == SCTP_CHUNK_LIST) {
1474			int i;
1475
1476			if (plen > sizeof(chunks_store))
1477				break;
1478			phdr = sctp_get_next_param(m, offset,
1479			    (struct sctp_paramhdr *)chunks_store, plen);
1480			if (phdr == NULL)
1481				return;
1482			chunks = (struct sctp_auth_chunk_list *)phdr;
1483			num_chunks = plen - sizeof(*chunks);
1484			/* save chunks list and num for the key */
1485			if (stcb->asoc.local_auth_chunks != NULL)
1486				sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1487			else
1488				stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1489			for (i = 0; i < num_chunks; i++) {
1490				sctp_auth_add_chunk(chunks->chunk_types[i],
1491				    stcb->asoc.local_auth_chunks);
1492			}
1493		}
1494		/* get next parameter */
1495		offset += SCTP_SIZE32(plen);
1496		if (offset + sizeof(struct sctp_paramhdr) > length)
1497			break;
1498		phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1499		    (uint8_t *) & tmp_param);
1500	}
1501	/* concatenate the full random key */
1502#ifdef SCTP_AUTH_DRAFT_04
1503	keylen = random_len;
1504	new_key = sctp_alloc_key(keylen);
1505	if (new_key != NULL) {
1506		/* copy in the RANDOM */
1507		if (random != NULL)
1508			bcopy(random->random_data, new_key->key, random_len);
1509	}
1510#else
1511	keylen = sizeof(*random) + random_len + sizeof(*chunks) + num_chunks +
1512	    sizeof(*hmacs) + hmacs_len;
1513	new_key = sctp_alloc_key(keylen);
1514	if (new_key != NULL) {
1515		/* copy in the RANDOM */
1516		if (random != NULL) {
1517			keylen = sizeof(*random) + random_len;
1518			bcopy(random, new_key->key, keylen);
1519		}
1520		/* append in the AUTH chunks */
1521		if (chunks != NULL) {
1522			bcopy(chunks, new_key->key + keylen,
1523			    sizeof(*chunks) + num_chunks);
1524			keylen += sizeof(*chunks) + num_chunks;
1525		}
1526		/* append in the HMACs */
1527		if (hmacs != NULL) {
1528			bcopy(hmacs, new_key->key + keylen,
1529			    sizeof(*hmacs) + hmacs_len);
1530		}
1531	}
1532#endif
1533	if (stcb->asoc.authinfo.random != NULL)
1534		sctp_free_key(stcb->asoc.authinfo.random);
1535	stcb->asoc.authinfo.random = new_key;
1536	stcb->asoc.authinfo.random_len = random_len;
1537#ifdef SCTP_AUTH_DRAFT_04
1538	/* don't include the chunks and hmacs for draft -04 */
1539	stcb->asoc.authinfo.random->keylen = random_len;
1540#endif
1541	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1542	sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1543
1544	/* negotiate what HMAC to use for the peer */
1545	stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1546	    stcb->asoc.local_hmacs);
1547	/* copy defaults from the endpoint */
1548	/* FIX ME: put in cookie? */
1549	stcb->asoc.authinfo.assoc_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1550}
1551
1552/*
1553 * compute and fill in the HMAC digest for a packet
1554 */
1555void
1556sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1557    struct sctp_auth_chunk *auth, struct sctp_tcb *stcb)
1558{
1559	uint32_t digestlen;
1560	sctp_sharedkey_t *skey;
1561	sctp_key_t *key;
1562
1563	if ((stcb == NULL) || (auth == NULL))
1564		return;
1565
1566	/* zero the digest + chunk padding */
1567	digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1568	bzero(auth->hmac, SCTP_SIZE32(digestlen));
1569	/* is an assoc key cached? */
1570	if (stcb->asoc.authinfo.assoc_key == NULL) {
1571		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1572		    stcb->asoc.authinfo.assoc_keyid);
1573		if (skey == NULL) {
1574			/* not in the assoc list, so check the endpoint list */
1575			skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1576			    stcb->asoc.authinfo.assoc_keyid);
1577		}
1578		/* the only way skey is NULL is if null key id 0 is used */
1579		if (skey != NULL)
1580			key = skey->key;
1581		else
1582			key = NULL;
1583		/* compute a new assoc key and cache it */
1584		stcb->asoc.authinfo.assoc_key =
1585		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1586		    stcb->asoc.authinfo.peer_random, key);
1587#ifdef SCTP_DEBUG
1588		if (SCTP_AUTH_DEBUG) {
1589			printf("caching key id %u\n",
1590			    stcb->asoc.authinfo.assoc_keyid);
1591			sctp_print_key(stcb->asoc.authinfo.assoc_key, "Assoc Key");
1592		}
1593#endif
1594	}
1595	/* set in the active key id */
1596	auth->shared_key_id = htons(stcb->asoc.authinfo.assoc_keyid);
1597
1598	/* compute and fill in the digest */
1599	(void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id,
1600	    stcb->asoc.authinfo.assoc_key,
1601	    m, auth_offset, auth->hmac);
1602}
1603
1604
1605static void
1606sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1607{
1608	struct mbuf *m_tmp;
1609	uint8_t *data;
1610
1611	/* sanity check */
1612	if (m == NULL)
1613		return;
1614
1615	/* find the correct starting mbuf and offset (get start position) */
1616	m_tmp = m;
1617	while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1618		m_offset -= SCTP_BUF_LEN(m_tmp);
1619		m_tmp = SCTP_BUF_NEXT(m_tmp);
1620	}
1621	/* now use the rest of the mbuf chain */
1622	while ((m_tmp != NULL) && (size > 0)) {
1623		data = mtod(m_tmp, uint8_t *) + m_offset;
1624		if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1625			bzero(data, SCTP_BUF_LEN(m_tmp));
1626			size -= SCTP_BUF_LEN(m_tmp);
1627		} else {
1628			bzero(data, size);
1629			size = 0;
1630		}
1631		/* clear the offset since it's only for the first mbuf */
1632		m_offset = 0;
1633		m_tmp = SCTP_BUF_NEXT(m_tmp);
1634	}
1635}
1636
1637/*
1638 * process the incoming Authentication chunk return codes: -1 on any
1639 * authentication error 0 on authentication verification
1640 */
1641int
1642sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1643    struct mbuf *m, uint32_t offset)
1644{
1645	uint16_t chunklen;
1646	uint16_t shared_key_id;
1647	uint16_t hmac_id;
1648	sctp_sharedkey_t *skey;
1649	uint32_t digestlen;
1650	uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1651	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1652
1653	/* auth is checked for NULL by caller */
1654	chunklen = ntohs(auth->ch.chunk_length);
1655	if (chunklen < sizeof(*auth)) {
1656		SCTP_STAT_INCR(sctps_recvauthfailed);
1657		return (-1);
1658	}
1659	SCTP_STAT_INCR(sctps_recvauth);
1660
1661	/* get the auth params */
1662	shared_key_id = ntohs(auth->shared_key_id);
1663	hmac_id = ntohs(auth->hmac_id);
1664#ifdef SCTP_DEBUG
1665	if (SCTP_AUTH_DEBUG)
1666		printf("SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1667		    shared_key_id, hmac_id);
1668#endif
1669
1670	/* is the indicated HMAC supported? */
1671	if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1672		struct mbuf *m_err;
1673		struct sctp_auth_invalid_hmac *err;
1674
1675		SCTP_STAT_INCR(sctps_recvivalhmacid);
1676#ifdef SCTP_DEBUG
1677		if (SCTP_AUTH_DEBUG)
1678			printf("SCTP Auth: unsupported HMAC id %u\n", hmac_id);
1679#endif
1680		/*
1681		 * report this in an Error Chunk: Unsupported HMAC
1682		 * Identifier
1683		 */
1684		m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT, 1, MT_HEADER);
1685		if (m_err != NULL) {
1686			/* pre-reserve some space */
1687			SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1688			/* fill in the error */
1689			err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1690			bzero(err, sizeof(*err));
1691			err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1692			err->ph.param_length = htons(sizeof(*err));
1693			err->hmac_id = ntohs(hmac_id);
1694			SCTP_BUF_LEN(m_err) = sizeof(*err);
1695			/* queue it */
1696			sctp_queue_op_err(stcb, m_err);
1697		}
1698		return (-1);
1699	}
1700	/* get the indicated shared key, if available */
1701	if ((stcb->asoc.authinfo.recv_key == NULL) ||
1702	    (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1703		/* find the shared key on the assoc first */
1704		skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, shared_key_id);
1705		if (skey == NULL) {
1706			/* if not on the assoc, find it on the endpoint */
1707			skey = sctp_find_sharedkey(&stcb->sctp_ep->sctp_ep.shared_keys,
1708			    shared_key_id);
1709		}
1710		/* if the shared key isn't found, discard the chunk */
1711		if (skey == NULL) {
1712			SCTP_STAT_INCR(sctps_recvivalkeyid);
1713#ifdef SCTP_DEBUG
1714			if (SCTP_AUTH_DEBUG)
1715				printf("SCTP Auth: unknown key id %u\n",
1716				    shared_key_id);
1717#endif
1718			return (-1);
1719		}
1720		/* generate a notification if this is a new key id */
1721		if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1722			/*
1723			 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1724			 * shared_key_id, (void
1725			 * *)stcb->asoc.authinfo.recv_keyid);
1726			 */
1727			sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1728			    shared_key_id, stcb->asoc.authinfo.recv_keyid);
1729		/* compute a new recv assoc key and cache it */
1730		if (stcb->asoc.authinfo.recv_key != NULL)
1731			sctp_free_key(stcb->asoc.authinfo.recv_key);
1732		stcb->asoc.authinfo.recv_key =
1733		    sctp_compute_hashkey(stcb->asoc.authinfo.random,
1734		    stcb->asoc.authinfo.peer_random, skey->key);
1735		stcb->asoc.authinfo.recv_keyid = shared_key_id;
1736#ifdef SCTP_DEBUG
1737		if (SCTP_AUTH_DEBUG)
1738			sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1739#endif
1740	}
1741	/* validate the digest length */
1742	digestlen = sctp_get_hmac_digest_len(hmac_id);
1743	if (chunklen < (sizeof(*auth) + digestlen)) {
1744		/* invalid digest length */
1745		SCTP_STAT_INCR(sctps_recvauthfailed);
1746#ifdef SCTP_DEBUG
1747		if (SCTP_AUTH_DEBUG)
1748			printf("SCTP Auth: chunk too short for HMAC\n");
1749#endif
1750		return (-1);
1751	}
1752	/* save a copy of the digest, zero the pseudo header, and validate */
1753	bcopy(auth->hmac, digest, digestlen);
1754	sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1755	(void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1756	    m, offset, computed_digest);
1757
1758	/* compare the computed digest with the one in the AUTH chunk */
1759	if (memcmp(digest, computed_digest, digestlen) != 0) {
1760		SCTP_STAT_INCR(sctps_recvauthfailed);
1761#ifdef SCTP_DEBUG
1762		if (SCTP_AUTH_DEBUG)
1763			printf("SCTP Auth: HMAC digest check failed\n");
1764#endif
1765		return (-1);
1766	}
1767	return (0);
1768}
1769
1770/*
1771 * Generate NOTIFICATION
1772 */
1773void
1774sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1775    uint16_t keyid, uint16_t alt_keyid)
1776{
1777	struct mbuf *m_notify;
1778	struct sctp_authkey_event *auth;
1779	struct sctp_queued_to_read *control;
1780
1781	if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1782		/* event not enabled */
1783		return;
1784
1785	m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1786	    0, M_DONTWAIT, 1, MT_HEADER);
1787	if (m_notify == NULL)
1788		/* no space left */
1789		return;
1790
1791	SCTP_BUF_LEN(m_notify) = 0;
1792	auth = mtod(m_notify, struct sctp_authkey_event *);
1793	auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1794	auth->auth_flags = 0;
1795	auth->auth_length = sizeof(*auth);
1796	auth->auth_keynumber = keyid;
1797	auth->auth_altkeynumber = alt_keyid;
1798	auth->auth_indication = indication;
1799	auth->auth_assoc_id = sctp_get_associd(stcb);
1800
1801	SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1802	SCTP_BUF_NEXT(m_notify) = NULL;
1803
1804	/* append to socket */
1805	control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1806	    0, 0, 0, 0, 0, 0, m_notify);
1807	if (control == NULL) {
1808		/* no memory */
1809		sctp_m_freem(m_notify);
1810		return;
1811	}
1812	control->spec_flags = M_NOTIFICATION;
1813	control->length = SCTP_BUF_LEN(m_notify);
1814	/* not that we need this */
1815	control->tail_mbuf = m_notify;
1816	sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1817	    &stcb->sctp_socket->so_rcv, 1);
1818}
1819
1820
1821/*
1822 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1823 * Note: currently only used for INIT as INIT-ACK is handled inline
1824 * with sctp_load_addresses_from_init()
1825 */
1826int
1827sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1828{
1829	struct sctp_paramhdr *phdr, parm_buf;
1830	uint16_t ptype, plen;
1831	int peer_supports_asconf = 0;
1832	int peer_supports_auth = 0;
1833	int got_random = 0, got_hmacs = 0;
1834
1835	/* go through each of the params. */
1836	phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1837	while (phdr) {
1838		ptype = ntohs(phdr->param_type);
1839		plen = ntohs(phdr->param_length);
1840
1841		if (offset + plen > limit) {
1842			break;
1843		}
1844		if (plen == 0) {
1845			break;
1846		}
1847		if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1848			/* A supported extension chunk */
1849			struct sctp_supported_chunk_types_param *pr_supported;
1850			uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1851			int num_ent, i;
1852
1853			phdr = sctp_get_next_param(m, offset,
1854			    (struct sctp_paramhdr *)&local_store, plen);
1855			if (phdr == NULL) {
1856				return (-1);
1857			}
1858			pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1859			num_ent = plen - sizeof(struct sctp_paramhdr);
1860			for (i = 0; i < num_ent; i++) {
1861				switch (pr_supported->chunk_types[i]) {
1862				case SCTP_ASCONF:
1863				case SCTP_ASCONF_ACK:
1864					peer_supports_asconf = 1;
1865					break;
1866				case SCTP_AUTHENTICATION:
1867					peer_supports_auth = 1;
1868					break;
1869				default:
1870					/* one we don't care about */
1871					break;
1872				}
1873			}
1874		} else if (ptype == SCTP_RANDOM) {
1875			got_random = 1;
1876			/* enforce the random length */
1877			if (plen != (sizeof(struct sctp_auth_random) +
1878			    SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1879#ifdef SCTP_DEBUG
1880				if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1881					printf("SCTP: invalid RANDOM len\n");
1882#endif
1883				return (-1);
1884			}
1885		} else if (ptype == SCTP_HMAC_LIST) {
1886			uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1887			struct sctp_auth_hmac_algo *hmacs;
1888			int num_hmacs;
1889
1890			if (plen > sizeof(store))
1891				break;
1892			phdr = sctp_get_next_param(m, offset,
1893			    (struct sctp_paramhdr *)store, plen);
1894			if (phdr == NULL)
1895				return (-1);
1896			hmacs = (struct sctp_auth_hmac_algo *)phdr;
1897			num_hmacs = (plen - sizeof(*hmacs)) /
1898			    sizeof(hmacs->hmac_ids[0]);
1899			/* validate the hmac list */
1900			if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1901#ifdef SCTP_DEBUG
1902				if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1903					printf("SCTP: invalid HMAC param\n");
1904#endif
1905				return (-1);
1906			}
1907			got_hmacs = 1;
1908		}
1909		offset += SCTP_SIZE32(plen);
1910		if (offset >= limit) {
1911			break;
1912		}
1913		phdr = sctp_get_next_param(m, offset, &parm_buf,
1914		    sizeof(parm_buf));
1915	}
1916	/* validate authentication required parameters */
1917	if (got_random && got_hmacs) {
1918		peer_supports_auth = 1;
1919	} else {
1920		peer_supports_auth = 0;
1921	}
1922	if (!sctp_asconf_auth_nochk && peer_supports_asconf &&
1923	    !peer_supports_auth) {
1924#ifdef SCTP_DEBUG
1925		if (sctp_debug_on & SCTP_DEBUG_AUTH1)
1926			printf("SCTP: peer supports ASCONF but not AUTH\n");
1927#endif
1928		return (-1);
1929	}
1930	return (0);
1931}
1932
1933void
1934sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1935{
1936	uint16_t chunks_len = 0;
1937	uint16_t hmacs_len = 0;
1938	uint16_t random_len = sctp_auth_random_len;
1939	sctp_key_t *new_key;
1940	uint16_t keylen;
1941
1942	/* initialize hmac list from endpoint */
1943	stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1944	if (stcb->asoc.local_hmacs != NULL) {
1945		hmacs_len = stcb->asoc.local_hmacs->num_algo *
1946		    sizeof(stcb->asoc.local_hmacs->hmac[0]);
1947	}
1948	/* initialize auth chunks list from endpoint */
1949	stcb->asoc.local_auth_chunks =
1950	    sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1951	if (stcb->asoc.local_auth_chunks != NULL) {
1952		int i;
1953
1954		for (i = 0; i < 256; i++) {
1955			if (stcb->asoc.local_auth_chunks->chunks[i])
1956				chunks_len++;
1957		}
1958	}
1959	/* copy defaults from the endpoint */
1960	stcb->asoc.authinfo.assoc_keyid = inp->sctp_ep.default_keyid;
1961
1962	/* now set the concatenated key (random + chunks + hmacs) */
1963#ifdef SCTP_AUTH_DRAFT_04
1964	/* don't include the chunks and hmacs for draft -04 */
1965	keylen = random_len;
1966	new_key = sctp_generate_random_key(keylen);
1967#else
1968	/* key includes parameter headers */
1969	keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
1970	    hmacs_len;
1971	new_key = sctp_alloc_key(keylen);
1972	if (new_key != NULL) {
1973		struct sctp_paramhdr *ph;
1974		int plen;
1975
1976		/* generate and copy in the RANDOM */
1977		ph = (struct sctp_paramhdr *)new_key->key;
1978		ph->param_type = htons(SCTP_RANDOM);
1979		plen = sizeof(*ph) + random_len;
1980		ph->param_length = htons(plen);
1981		SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
1982		keylen = plen;
1983
1984		/* append in the AUTH chunks */
1985		/* NOTE: currently we always have chunks to list */
1986		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
1987		ph->param_type = htons(SCTP_CHUNK_LIST);
1988		plen = sizeof(*ph) + chunks_len;
1989		ph->param_length = htons(plen);
1990		keylen += sizeof(*ph);
1991		if (stcb->asoc.local_auth_chunks) {
1992			int i;
1993
1994			for (i = 0; i < 256; i++) {
1995				if (stcb->asoc.local_auth_chunks->chunks[i])
1996					new_key->key[keylen++] = i;
1997			}
1998		}
1999		/* append in the HMACs */
2000		ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2001		ph->param_type = htons(SCTP_HMAC_LIST);
2002		plen = sizeof(*ph) + hmacs_len;
2003		ph->param_length = htons(plen);
2004		keylen += sizeof(*ph);
2005		sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2006		    new_key->key + keylen);
2007	}
2008#endif
2009	if (stcb->asoc.authinfo.random != NULL)
2010		sctp_free_key(stcb->asoc.authinfo.random);
2011	stcb->asoc.authinfo.random = new_key;
2012	stcb->asoc.authinfo.random_len = random_len;
2013}
2014
2015
2016#ifdef SCTP_HMAC_TEST
2017/*
2018 * HMAC and key concatenation tests
2019 */
2020static void
2021sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2022{
2023	uint32_t i;
2024
2025	printf("\n%s: 0x", str);
2026	if (digest == NULL)
2027		return;
2028
2029	for (i = 0; i < digestlen; i++)
2030		printf("%02x", digest[i]);
2031}
2032
2033static int
2034sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2035    uint32_t keylen, uint8_t * text, uint32_t textlen,
2036    uint8_t * digest, uint32_t digestlen)
2037{
2038	uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2039
2040	printf("\n%s:", str);
2041	sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2042	sctp_print_digest(digest, digestlen, "Expected digest");
2043	sctp_print_digest(computed_digest, digestlen, "Computed digest");
2044	if (memcmp(digest, computed_digest, digestlen) != 0) {
2045		printf("\nFAILED");
2046		return (-1);
2047	} else {
2048		printf("\nPASSED");
2049		return (0);
2050	}
2051}
2052
2053
2054/*
2055 * RFC 2202: HMAC-SHA1 test cases
2056 */
2057void
2058sctp_test_hmac_sha1(void)
2059{
2060	uint8_t *digest;
2061	uint8_t key[128];
2062	uint32_t keylen;
2063	uint8_t text[128];
2064	uint32_t textlen;
2065	uint32_t digestlen = 20;
2066	int failed = 0;
2067
2068	/*
2069	 * test_case =     1 key =
2070	 * 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b key_len =       20
2071	 * data =          "Hi There" data_len =      8 digest =
2072	 * 0xb617318655057264e28bc0b6fb378c8ef146be00
2073	 */
2074	keylen = 20;
2075	memset(key, 0x0b, keylen);
2076	textlen = 8;
2077	strcpy(text, "Hi There");
2078	digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2079	if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2080	    text, textlen, digest, digestlen) < 0)
2081		failed++;
2082
2083	/*
2084	 * test_case =     2 key =           "Jefe" key_len =       4 data =
2085	 * "what do ya want for nothing?" data_len =      28 digest =
2086	 * 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2087	 */
2088	keylen = 4;
2089	strcpy(key, "Jefe");
2090	textlen = 28;
2091	strcpy(text, "what do ya want for nothing?");
2092	digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2093	if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2094	    text, textlen, digest, digestlen) < 0)
2095		failed++;
2096
2097	/*
2098	 * test_case =     3 key =
2099	 * 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa key_len =       20
2100	 * data =          0xdd repeated 50 times data_len =      50 digest
2101	 * = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2102	 */
2103	keylen = 20;
2104	memset(key, 0xaa, keylen);
2105	textlen = 50;
2106	memset(text, 0xdd, textlen);
2107	digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2108	if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2109	    text, textlen, digest, digestlen) < 0)
2110		failed++;
2111
2112	/*
2113	 * test_case =     4 key =
2114	 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2115	 * data =          0xcd repeated 50 times data_len =      50 digest
2116	 * =        0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2117	 */
2118	keylen = 25;
2119	memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2120	textlen = 50;
2121	memset(text, 0xcd, textlen);
2122	digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2123	if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2124	    text, textlen, digest, digestlen) < 0)
2125		failed++;
2126
2127	/*
2128	 * test_case =     5 key =
2129	 * 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c key_len =       20
2130	 * data =          "Test With Truncation" data_len =      20 digest
2131	 * = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04 digest-96 =
2132	 * 0x4c1a03424b55e07fe7f27be1
2133	 */
2134	keylen = 20;
2135	memset(key, 0x0c, keylen);
2136	textlen = 20;
2137	strcpy(text, "Test With Truncation");
2138	digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2139	if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2140	    text, textlen, digest, digestlen) < 0)
2141		failed++;
2142
2143	/*
2144	 * test_case =     6 key =           0xaa repeated 80 times key_len
2145	 * = 80 data =          "Test Using Larger Than Block-Size Key -
2146	 * Hash Key First" data_len =      54 digest =
2147	 * 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2148	 */
2149	keylen = 80;
2150	memset(key, 0xaa, keylen);
2151	textlen = 54;
2152	strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2153	digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2154	if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2155	    text, textlen, digest, digestlen) < 0)
2156		failed++;
2157
2158	/*
2159	 * test_case =     7 key =           0xaa repeated 80 times key_len
2160	 * = 80 data =          "Test Using Larger Than Block-Size Key and
2161	 * Larger Than One Block-Size Data" data_len =      73 digest =
2162	 * 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2163	 */
2164	keylen = 80;
2165	memset(key, 0xaa, keylen);
2166	textlen = 73;
2167	strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2168	digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2169	if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2170	    text, textlen, digest, digestlen) < 0)
2171		failed++;
2172
2173	/* done with all tests */
2174	if (failed)
2175		printf("\nSHA1 test results: %d cases failed", failed);
2176	else
2177		printf("\nSHA1 test results: all test cases passed");
2178}
2179
2180/*
2181 * RFC 2202: HMAC-MD5 test cases
2182 */
2183void
2184sctp_test_hmac_md5(void)
2185{
2186	uint8_t *digest;
2187	uint8_t key[128];
2188	uint32_t keylen;
2189	uint8_t text[128];
2190	uint32_t textlen;
2191	uint32_t digestlen = 16;
2192	int failed = 0;
2193
2194	/*
2195	 * test_case =     1 key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2196	 * key_len =       16 data = "Hi There" data_len =      8 digest =
2197	 * 0x9294727a3638bb1c13f48ef8158bfc9d
2198	 */
2199	keylen = 16;
2200	memset(key, 0x0b, keylen);
2201	textlen = 8;
2202	strcpy(text, "Hi There");
2203	digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2204	if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2205	    text, textlen, digest, digestlen) < 0)
2206		failed++;
2207
2208	/*
2209	 * test_case =     2 key =           "Jefe" key_len =       4 data =
2210	 * "what do ya want for nothing?" data_len =      28 digest =
2211	 * 0x750c783e6ab0b503eaa86e310a5db738
2212	 */
2213	keylen = 4;
2214	strcpy(key, "Jefe");
2215	textlen = 28;
2216	strcpy(text, "what do ya want for nothing?");
2217	digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2218	if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2219	    text, textlen, digest, digestlen) < 0)
2220		failed++;
2221
2222	/*
2223	 * test_case =     3 key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2224	 * key_len =       16 data = 0xdd repeated 50 times data_len = 50
2225	 * digest = 0x56be34521d144c88dbb8c733f0e8b3f6
2226	 */
2227	keylen = 16;
2228	memset(key, 0xaa, keylen);
2229	textlen = 50;
2230	memset(text, 0xdd, textlen);
2231	digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2232	if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2233	    text, textlen, digest, digestlen) < 0)
2234		failed++;
2235
2236	/*
2237	 * test_case =     4 key =
2238	 * 0x0102030405060708090a0b0c0d0e0f10111213141516171819 key_len = 25
2239	 * data =          0xcd repeated 50 times data_len =      50 digest
2240	 * =        0x697eaf0aca3a3aea3a75164746ffaa79
2241	 */
2242	keylen = 25;
2243	memcpy(key, "\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0a\x0b\x0c\x0d\x0e\x0f\x10\x11\x12\x13\x14\x15\x16\x17\x18\x19", keylen);
2244	textlen = 50;
2245	memset(text, 0xcd, textlen);
2246	digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2247	if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2248	    text, textlen, digest, digestlen) < 0)
2249		failed++;
2250
2251	/*
2252	 * test_case =     5 key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2253	 * key_len =       16 data = "Test With Truncation" data_len = 20
2254	 * digest = 0x56461ef2342edc00f9bab995690efd4c digest-96
2255	 * 0x56461ef2342edc00f9bab995
2256	 */
2257	keylen = 16;
2258	memset(key, 0x0c, keylen);
2259	textlen = 20;
2260	strcpy(text, "Test With Truncation");
2261	digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2262	if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2263	    text, textlen, digest, digestlen) < 0)
2264		failed++;
2265
2266	/*
2267	 * test_case =     6 key =           0xaa repeated 80 times key_len
2268	 * = 80 data =          "Test Using Larger Than Block-Size Key -
2269	 * Hash Key First" data_len =      54 digest =
2270	 * 0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2271	 */
2272	keylen = 80;
2273	memset(key, 0xaa, keylen);
2274	textlen = 54;
2275	strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2276	digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2277	if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2278	    text, textlen, digest, digestlen) < 0)
2279		failed++;
2280
2281	/*
2282	 * test_case =     7 key =           0xaa repeated 80 times key_len
2283	 * = 80 data =          "Test Using Larger Than Block-Size Key and
2284	 * Larger Than One Block-Size Data" data_len =      73 digest =
2285	 * 0x6f630fad67cda0ee1fb1f562db3aa53e
2286	 */
2287	keylen = 80;
2288	memset(key, 0xaa, keylen);
2289	textlen = 73;
2290	strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2291	digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2292	if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2293	    text, textlen, digest, digestlen) < 0)
2294		failed++;
2295
2296	/* done with all tests */
2297	if (failed)
2298		printf("\nMD5 test results: %d cases failed", failed);
2299	else
2300		printf("\nMD5 test results: all test cases passed");
2301}
2302
2303/*
2304 * test assoc key concatenation
2305 */
2306static int
2307sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2308    sctp_key_t * expected_key)
2309{
2310	sctp_key_t *key;
2311	int ret_val;
2312
2313	sctp_show_key(key1, "\nkey1");
2314	sctp_show_key(key2, "\nkey2");
2315	key = sctp_compute_hashkey(key1, key2, NULL);
2316	sctp_show_key(expected_key, "\nExpected");
2317	sctp_show_key(key, "\nComputed");
2318	if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2319		printf("\nFAILED");
2320		ret_val = -1;
2321	} else {
2322		printf("\nPASSED");
2323		ret_val = 0;
2324	}
2325	sctp_free_key(key1);
2326	sctp_free_key(key2);
2327	sctp_free_key(expected_key);
2328	sctp_free_key(key);
2329	return (ret_val);
2330}
2331
2332
2333void
2334sctp_test_authkey(void)
2335{
2336	sctp_key_t *key1, *key2, *expected_key;
2337	int failed = 0;
2338
2339	/* test case 1 */
2340	key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2341	key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2342	expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2343	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2344		failed++;
2345
2346	/* test case 2 */
2347	key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2348	key2 = sctp_set_key("\x02", 1);
2349	expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2350	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2351		failed++;
2352
2353	/* test case 3 */
2354	key1 = sctp_set_key("\x01", 1);
2355	key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2356	expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2357	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2358		failed++;
2359
2360	/* test case 4 */
2361	key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2362	key2 = sctp_set_key("\x01", 1);
2363	expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2364	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2365		failed++;
2366
2367	/* test case 5 */
2368	key1 = sctp_set_key("\x01", 1);
2369	key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2370	expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2371	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2372		failed++;
2373
2374	/* test case 6 */
2375	key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2376	key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2377	expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2378	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2379		failed++;
2380
2381	/* test case 7 */
2382	key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2383	key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2384	expected_key = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 22);
2385	if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2386		failed++;
2387
2388	/* done with all tests */
2389	if (failed)
2390		printf("\nKey concatenation test results: %d cases failed", failed);
2391	else
2392		printf("\nKey concatenation test results: all test cases passed");
2393}
2394
2395
2396#if defined(STANDALONE_HMAC_TEST)
2397int
2398main(void)
2399{
2400	sctp_test_hmac_sha1();
2401	sctp_test_hmac_md5();
2402	sctp_test_authkey();
2403}
2404
2405#endif				/* STANDALONE_HMAC_TEST */
2406
2407#endif				/* SCTP_HMAC_TEST */
2408