blob: de81ffd2e4feaf28e2332ff0e158f2d120834c6f [file] [log] [blame]
James Kuszmaul4cb043c2021-01-17 11:25:51 -08001/*-
2 * Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
3 * Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
4 * Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * a) Redistributions of source code must retain the above copyright notice,
10 * this list of conditions and the following disclaimer.
11 *
12 * b) Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the distribution.
15 *
16 * c) Neither the name of Cisco Systems, Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived
18 * from this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
30 * THE POSSIBILITY OF SUCH DAMAGE.
31 */
32
33#ifdef __FreeBSD__
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD: head/sys/netinet/sctp_auth.c 310590 2016-12-26 11:06:41Z tuexen $");
36#endif
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
55void
56sctp_clear_chunklist(sctp_auth_chklist_t *chklist)
57{
58 bzero(chklist, sizeof(*chklist));
59 /* chklist->num_chunks = 0; */
60}
61
62sctp_auth_chklist_t *
63sctp_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
77void
78sctp_free_chunklist(sctp_auth_chklist_t *list)
79{
80 if (list != NULL)
81 SCTP_FREE(list, SCTP_M_AUTH_CL);
82}
83
84sctp_auth_chklist_t *
85sctp_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 bcopy(list, new_list, sizeof(*new_list));
98
99 return (new_list);
100}
101
102
103/*
104 * add a chunk to the required chunks list
105 */
106int
107sctp_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 */
132int
133sctp_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
148size_t
149sctp_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 */
161int
162sctp_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
178int
179sctp_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
210int
211sctp_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 */
246sctp_key_t *
247sctp_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
261void
262sctp_free_key(sctp_key_t *key)
263{
264 if (key != NULL)
265 SCTP_FREE(key,SCTP_M_AUTH_KY);
266}
267
268void
269sctp_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
287void
288sctp_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
306static uint32_t
307sctp_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 */
318sctp_key_t *
319sctp_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
333sctp_key_t *
334sctp_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 bcopy(key, new_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 */
353static int
354sctp_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 */
404sctp_key_t *
405sctp_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 bcopy(shared->key, key_ptr, shared->keylen);
433 key_ptr += shared->keylen;
434 }
435 if (sctp_get_keylen(key1)) {
436 bcopy(key1->key, key_ptr, key1->keylen);
437 key_ptr += key1->keylen;
438 }
439 if (sctp_get_keylen(key2)) {
440 bcopy(key2->key, key_ptr, key2->keylen);
441 }
442 } else {
443 /* key is shared + key2 + key1 */
444 if (sctp_get_keylen(shared)) {
445 bcopy(shared->key, key_ptr, shared->keylen);
446 key_ptr += shared->keylen;
447 }
448 if (sctp_get_keylen(key2)) {
449 bcopy(key2->key, key_ptr, key2->keylen);
450 key_ptr += key2->keylen;
451 }
452 if (sctp_get_keylen(key1)) {
453 bcopy(key1->key, key_ptr, key1->keylen);
454 }
455 }
456 return (new_key);
457}
458
459
460sctp_sharedkey_t *
461sctp_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
478void
479sctp_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
491sctp_sharedkey_t *
492sctp_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
503int
504sctp_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
550void
551sctp_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
567void
568sctp_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
598static sctp_sharedkey_t *
599sctp_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
616int
617sctp_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
638sctp_hmaclist_t *
639sctp_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
656void
657sctp_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
665int
666sctp_auth_add_hmacid(sctp_hmaclist_t *list, uint16_t hmac_id)
667{
668 int i;
669 if (list == NULL)
670 return (-1);
671 if (list->num_algo == list->max_algo) {
672 SCTPDBG(SCTP_DEBUG_AUTH1,
673 "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
674 return (-1);
675 }
676#if defined(SCTP_SUPPORT_HMAC_SHA256)
677 if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
678 (hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
679#else
680 if (hmac_id != SCTP_AUTH_HMAC_ID_SHA1) {
681#endif
682 return (-1);
683 }
684 /* Now is it already in the list */
685 for (i = 0; i < list->num_algo; i++) {
686 if (list->hmac[i] == hmac_id) {
687 /* already in list */
688 return (-1);
689 }
690 }
691 SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
692 list->hmac[list->num_algo++] = hmac_id;
693 return (0);
694}
695
696sctp_hmaclist_t *
697sctp_copy_hmaclist(sctp_hmaclist_t *list)
698{
699 sctp_hmaclist_t *new_list;
700 int i;
701
702 if (list == NULL)
703 return (NULL);
704 /* get a new list */
705 new_list = sctp_alloc_hmaclist(list->max_algo);
706 if (new_list == NULL)
707 return (NULL);
708 /* copy it */
709 new_list->max_algo = list->max_algo;
710 new_list->num_algo = list->num_algo;
711 for (i = 0; i < list->num_algo; i++)
712 new_list->hmac[i] = list->hmac[i];
713 return (new_list);
714}
715
716sctp_hmaclist_t *
717sctp_default_supported_hmaclist(void)
718{
719 sctp_hmaclist_t *new_list;
720
721#if defined(SCTP_SUPPORT_HMAC_SHA256)
722 new_list = sctp_alloc_hmaclist(2);
723#else
724 new_list = sctp_alloc_hmaclist(1);
725#endif
726 if (new_list == NULL)
727 return (NULL);
728#if defined(SCTP_SUPPORT_HMAC_SHA256)
729 /* We prefer SHA256, so list it first */
730 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
731#endif
732 (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
733 return (new_list);
734}
735
736/*-
737 * HMAC algos are listed in priority/preference order
738 * find the best HMAC id to use for the peer based on local support
739 */
740uint16_t
741sctp_negotiate_hmacid(sctp_hmaclist_t *peer, sctp_hmaclist_t *local)
742{
743 int i, j;
744
745 if ((local == NULL) || (peer == NULL))
746 return (SCTP_AUTH_HMAC_ID_RSVD);
747
748 for (i = 0; i < peer->num_algo; i++) {
749 for (j = 0; j < local->num_algo; j++) {
750 if (peer->hmac[i] == local->hmac[j]) {
751 /* found the "best" one */
752 SCTPDBG(SCTP_DEBUG_AUTH1,
753 "SCTP: negotiated peer HMAC id %u\n",
754 peer->hmac[i]);
755 return (peer->hmac[i]);
756 }
757 }
758 }
759 /* didn't find one! */
760 return (SCTP_AUTH_HMAC_ID_RSVD);
761}
762
763/*-
764 * serialize the HMAC algo list and return space used
765 * caller must guarantee ptr has appropriate space
766 */
767int
768sctp_serialize_hmaclist(sctp_hmaclist_t *list, uint8_t *ptr)
769{
770 int i;
771 uint16_t hmac_id;
772
773 if (list == NULL)
774 return (0);
775
776 for (i = 0; i < list->num_algo; i++) {
777 hmac_id = htons(list->hmac[i]);
778 bcopy(&hmac_id, ptr, sizeof(hmac_id));
779 ptr += sizeof(hmac_id);
780 }
781 return (list->num_algo * sizeof(hmac_id));
782}
783
784int
785sctp_verify_hmac_param (struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
786{
787 uint32_t i;
788
789 for (i = 0; i < num_hmacs; i++) {
790 if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
791 return (0);
792 }
793 }
794 return (-1);
795}
796
797sctp_authinfo_t *
798sctp_alloc_authinfo(void)
799{
800 sctp_authinfo_t *new_authinfo;
801
802 SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
803 SCTP_M_AUTH_IF);
804
805 if (new_authinfo == NULL) {
806 /* out of memory */
807 return (NULL);
808 }
809 bzero(new_authinfo, sizeof(*new_authinfo));
810 return (new_authinfo);
811}
812
813void
814sctp_free_authinfo(sctp_authinfo_t *authinfo)
815{
816 if (authinfo == NULL)
817 return;
818
819 if (authinfo->random != NULL)
820 sctp_free_key(authinfo->random);
821 if (authinfo->peer_random != NULL)
822 sctp_free_key(authinfo->peer_random);
823 if (authinfo->assoc_key != NULL)
824 sctp_free_key(authinfo->assoc_key);
825 if (authinfo->recv_key != NULL)
826 sctp_free_key(authinfo->recv_key);
827
828 /* We are NOT dynamically allocating authinfo's right now... */
829 /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
830}
831
832
833uint32_t
834sctp_get_auth_chunk_len(uint16_t hmac_algo)
835{
836 int size;
837
838 size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
839 return (SCTP_SIZE32(size));
840}
841
842uint32_t
843sctp_get_hmac_digest_len(uint16_t hmac_algo)
844{
845 switch (hmac_algo) {
846 case SCTP_AUTH_HMAC_ID_SHA1:
847 return (SCTP_AUTH_DIGEST_LEN_SHA1);
848#if defined(SCTP_SUPPORT_HMAC_SHA256)
849 case SCTP_AUTH_HMAC_ID_SHA256:
850 return (SCTP_AUTH_DIGEST_LEN_SHA256);
851#endif
852 default:
853 /* unknown HMAC algorithm: can't do anything */
854 return (0);
855 } /* end switch */
856}
857
858static inline int
859sctp_get_hmac_block_len(uint16_t hmac_algo)
860{
861 switch (hmac_algo) {
862 case SCTP_AUTH_HMAC_ID_SHA1:
863 return (64);
864#if defined(SCTP_SUPPORT_HMAC_SHA256)
865 case SCTP_AUTH_HMAC_ID_SHA256:
866 return (64);
867#endif
868 case SCTP_AUTH_HMAC_ID_RSVD:
869 default:
870 /* unknown HMAC algorithm: can't do anything */
871 return (0);
872 } /* end switch */
873}
874
875#if defined(__Userspace__)
876/* __Userspace__ SHA1_Init is defined in libcrypto.a (libssl-dev on Ubuntu) */
877#endif
878static void
879sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t *ctx)
880{
881 switch (hmac_algo) {
882 case SCTP_AUTH_HMAC_ID_SHA1:
883 SCTP_SHA1_INIT(&ctx->sha1);
884 break;
885#if defined(SCTP_SUPPORT_HMAC_SHA256)
886 case SCTP_AUTH_HMAC_ID_SHA256:
887 SCTP_SHA256_INIT(&ctx->sha256);
888 break;
889#endif
890 case SCTP_AUTH_HMAC_ID_RSVD:
891 default:
892 /* unknown HMAC algorithm: can't do anything */
893 return;
894 } /* end switch */
895}
896
897static void
898sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t *ctx,
899 uint8_t *text, uint32_t textlen)
900{
901 switch (hmac_algo) {
902 case SCTP_AUTH_HMAC_ID_SHA1:
903 SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
904 break;
905#if defined(SCTP_SUPPORT_HMAC_SHA256)
906 case SCTP_AUTH_HMAC_ID_SHA256:
907 SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
908 break;
909#endif
910 case SCTP_AUTH_HMAC_ID_RSVD:
911 default:
912 /* unknown HMAC algorithm: can't do anything */
913 return;
914 } /* end switch */
915}
916
917static void
918sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t *ctx,
919 uint8_t *digest)
920{
921 switch (hmac_algo) {
922 case SCTP_AUTH_HMAC_ID_SHA1:
923 SCTP_SHA1_FINAL(digest, &ctx->sha1);
924 break;
925#if defined(SCTP_SUPPORT_HMAC_SHA256)
926 case SCTP_AUTH_HMAC_ID_SHA256:
927 SCTP_SHA256_FINAL(digest, &ctx->sha256);
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
937/*-
938 * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
939 *
940 * Compute the HMAC digest using the desired hash key, text, and HMAC
941 * algorithm. Resulting digest is placed in 'digest' and digest length
942 * is returned, if the HMAC was performed.
943 *
944 * WARNING: it is up to the caller to supply sufficient space to hold the
945 * resultant digest.
946 */
947uint32_t
948sctp_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
949 uint8_t *text, uint32_t textlen, uint8_t *digest)
950{
951 uint32_t digestlen;
952 uint32_t blocklen;
953 sctp_hash_context_t ctx;
954 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
955 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
956 uint32_t i;
957
958 /* sanity check the material and length */
959 if ((key == NULL) || (keylen == 0) || (text == NULL) ||
960 (textlen == 0) || (digest == NULL)) {
961 /* can't do HMAC with empty key or text or digest store */
962 return (0);
963 }
964 /* validate the hmac algo and get the digest length */
965 digestlen = sctp_get_hmac_digest_len(hmac_algo);
966 if (digestlen == 0)
967 return (0);
968
969 /* hash the key if it is longer than the hash block size */
970 blocklen = sctp_get_hmac_block_len(hmac_algo);
971 if (keylen > blocklen) {
972 sctp_hmac_init(hmac_algo, &ctx);
973 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
974 sctp_hmac_final(hmac_algo, &ctx, temp);
975 /* set the hashed key as the key */
976 keylen = digestlen;
977 key = temp;
978 }
979 /* initialize the inner/outer pads with the key and "append" zeroes */
980 bzero(ipad, blocklen);
981 bzero(opad, blocklen);
982 bcopy(key, ipad, keylen);
983 bcopy(key, opad, keylen);
984
985 /* XOR the key with ipad and opad values */
986 for (i = 0; i < blocklen; i++) {
987 ipad[i] ^= 0x36;
988 opad[i] ^= 0x5c;
989 }
990
991 /* perform inner hash */
992 sctp_hmac_init(hmac_algo, &ctx);
993 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
994 sctp_hmac_update(hmac_algo, &ctx, text, textlen);
995 sctp_hmac_final(hmac_algo, &ctx, temp);
996
997 /* perform outer hash */
998 sctp_hmac_init(hmac_algo, &ctx);
999 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1000 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1001 sctp_hmac_final(hmac_algo, &ctx, digest);
1002
1003 return (digestlen);
1004}
1005
1006/* mbuf version */
1007uint32_t
1008sctp_hmac_m(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
1009 struct mbuf *m, uint32_t m_offset, uint8_t *digest, uint32_t trailer)
1010{
1011 uint32_t digestlen;
1012 uint32_t blocklen;
1013 sctp_hash_context_t ctx;
1014 uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
1015 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1016 uint32_t i;
1017 struct mbuf *m_tmp;
1018
1019 /* sanity check the material and length */
1020 if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1021 /* can't do HMAC with empty key or text or digest store */
1022 return (0);
1023 }
1024 /* validate the hmac algo and get the digest length */
1025 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1026 if (digestlen == 0)
1027 return (0);
1028
1029 /* hash the key if it is longer than the hash block size */
1030 blocklen = sctp_get_hmac_block_len(hmac_algo);
1031 if (keylen > blocklen) {
1032 sctp_hmac_init(hmac_algo, &ctx);
1033 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1034 sctp_hmac_final(hmac_algo, &ctx, temp);
1035 /* set the hashed key as the key */
1036 keylen = digestlen;
1037 key = temp;
1038 }
1039 /* initialize the inner/outer pads with the key and "append" zeroes */
1040 bzero(ipad, blocklen);
1041 bzero(opad, blocklen);
1042 bcopy(key, ipad, keylen);
1043 bcopy(key, opad, keylen);
1044
1045 /* XOR the key with ipad and opad values */
1046 for (i = 0; i < blocklen; i++) {
1047 ipad[i] ^= 0x36;
1048 opad[i] ^= 0x5c;
1049 }
1050
1051 /* perform inner hash */
1052 sctp_hmac_init(hmac_algo, &ctx);
1053 sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1054 /* find the correct starting mbuf and offset (get start of text) */
1055 m_tmp = m;
1056 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1057 m_offset -= SCTP_BUF_LEN(m_tmp);
1058 m_tmp = SCTP_BUF_NEXT(m_tmp);
1059 }
1060 /* now use the rest of the mbuf chain for the text */
1061 while (m_tmp != NULL) {
1062 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1063 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1064 SCTP_BUF_LEN(m_tmp) - (trailer+m_offset));
1065 } else {
1066 sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1067 SCTP_BUF_LEN(m_tmp) - m_offset);
1068 }
1069
1070 /* clear the offset since it's only for the first mbuf */
1071 m_offset = 0;
1072 m_tmp = SCTP_BUF_NEXT(m_tmp);
1073 }
1074 sctp_hmac_final(hmac_algo, &ctx, temp);
1075
1076 /* perform outer hash */
1077 sctp_hmac_init(hmac_algo, &ctx);
1078 sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1079 sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1080 sctp_hmac_final(hmac_algo, &ctx, digest);
1081
1082 return (digestlen);
1083}
1084
1085/*-
1086 * verify the HMAC digest using the desired hash key, text, and HMAC
1087 * algorithm.
1088 * Returns -1 on error, 0 on success.
1089 */
1090int
1091sctp_verify_hmac(uint16_t hmac_algo, uint8_t *key, uint32_t keylen,
1092 uint8_t *text, uint32_t textlen,
1093 uint8_t *digest, uint32_t digestlen)
1094{
1095 uint32_t len;
1096 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1097
1098 /* sanity check the material and length */
1099 if ((key == NULL) || (keylen == 0) ||
1100 (text == NULL) || (textlen == 0) || (digest == NULL)) {
1101 /* can't do HMAC with empty key or text or digest */
1102 return (-1);
1103 }
1104 len = sctp_get_hmac_digest_len(hmac_algo);
1105 if ((len == 0) || (digestlen != len))
1106 return (-1);
1107
1108 /* compute the expected hash */
1109 if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1110 return (-1);
1111
1112 if (memcmp(digest, temp, digestlen) != 0)
1113 return (-1);
1114 else
1115 return (0);
1116}
1117
1118
1119/*
1120 * computes the requested HMAC using a key struct (which may be modified if
1121 * the keylen exceeds the HMAC block len).
1122 */
1123uint32_t
1124sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t *key, uint8_t *text,
1125 uint32_t textlen, uint8_t *digest)
1126{
1127 uint32_t digestlen;
1128 uint32_t blocklen;
1129 sctp_hash_context_t ctx;
1130 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1131
1132 /* sanity check */
1133 if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1134 (digest == NULL)) {
1135 /* can't do HMAC with empty key or text or digest store */
1136 return (0);
1137 }
1138 /* validate the hmac algo and get the digest length */
1139 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1140 if (digestlen == 0)
1141 return (0);
1142
1143 /* hash the key if it is longer than the hash block size */
1144 blocklen = sctp_get_hmac_block_len(hmac_algo);
1145 if (key->keylen > blocklen) {
1146 sctp_hmac_init(hmac_algo, &ctx);
1147 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1148 sctp_hmac_final(hmac_algo, &ctx, temp);
1149 /* save the hashed key as the new key */
1150 key->keylen = digestlen;
1151 bcopy(temp, key->key, key->keylen);
1152 }
1153 return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1154 digest));
1155}
1156
1157/* mbuf version */
1158uint32_t
1159sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t *key, struct mbuf *m,
1160 uint32_t m_offset, uint8_t *digest)
1161{
1162 uint32_t digestlen;
1163 uint32_t blocklen;
1164 sctp_hash_context_t ctx;
1165 uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1166
1167 /* sanity check */
1168 if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1169 /* can't do HMAC with empty key or text or digest store */
1170 return (0);
1171 }
1172 /* validate the hmac algo and get the digest length */
1173 digestlen = sctp_get_hmac_digest_len(hmac_algo);
1174 if (digestlen == 0)
1175 return (0);
1176
1177 /* hash the key if it is longer than the hash block size */
1178 blocklen = sctp_get_hmac_block_len(hmac_algo);
1179 if (key->keylen > blocklen) {
1180 sctp_hmac_init(hmac_algo, &ctx);
1181 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1182 sctp_hmac_final(hmac_algo, &ctx, temp);
1183 /* save the hashed key as the new key */
1184 key->keylen = digestlen;
1185 bcopy(temp, key->key, key->keylen);
1186 }
1187 return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1188}
1189
1190int
1191sctp_auth_is_supported_hmac(sctp_hmaclist_t *list, uint16_t id)
1192{
1193 int i;
1194
1195 if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1196 return (0);
1197
1198 for (i = 0; i < list->num_algo; i++)
1199 if (list->hmac[i] == id)
1200 return (1);
1201
1202 /* not in the list */
1203 return (0);
1204}
1205
1206
1207/*-
1208 * clear any cached key(s) if they match the given key id on an association.
1209 * the cached key(s) will be recomputed and re-cached at next use.
1210 * ASSUMES TCB_LOCK is already held
1211 */
1212void
1213sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1214{
1215 if (stcb == NULL)
1216 return;
1217
1218 if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1219 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1220 stcb->asoc.authinfo.assoc_key = NULL;
1221 }
1222 if (keyid == stcb->asoc.authinfo.recv_keyid) {
1223 sctp_free_key(stcb->asoc.authinfo.recv_key);
1224 stcb->asoc.authinfo.recv_key = NULL;
1225 }
1226}
1227
1228/*-
1229 * clear any cached key(s) if they match the given key id for all assocs on
1230 * an endpoint.
1231 * ASSUMES INP_WLOCK is already held
1232 */
1233void
1234sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1235{
1236 struct sctp_tcb *stcb;
1237
1238 if (inp == NULL)
1239 return;
1240
1241 /* clear the cached keys on all assocs on this instance */
1242 LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1243 SCTP_TCB_LOCK(stcb);
1244 sctp_clear_cachedkeys(stcb, keyid);
1245 SCTP_TCB_UNLOCK(stcb);
1246 }
1247}
1248
1249/*-
1250 * delete a shared key from an association
1251 * ASSUMES TCB_LOCK is already held
1252 */
1253int
1254sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1255{
1256 sctp_sharedkey_t *skey;
1257
1258 if (stcb == NULL)
1259 return (-1);
1260
1261 /* is the keyid the assoc active sending key */
1262 if (keyid == stcb->asoc.authinfo.active_keyid)
1263 return (-1);
1264
1265 /* does the key exist? */
1266 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1267 if (skey == NULL)
1268 return (-1);
1269
1270 /* are there other refcount holders on the key? */
1271 if (skey->refcount > 1)
1272 return (-1);
1273
1274 /* remove it */
1275 LIST_REMOVE(skey, next);
1276 sctp_free_sharedkey(skey); /* frees skey->key as well */
1277
1278 /* clear any cached keys */
1279 sctp_clear_cachedkeys(stcb, keyid);
1280 return (0);
1281}
1282
1283/*-
1284 * deletes a shared key from the endpoint
1285 * ASSUMES INP_WLOCK is already held
1286 */
1287int
1288sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1289{
1290 sctp_sharedkey_t *skey;
1291
1292 if (inp == NULL)
1293 return (-1);
1294
1295 /* is the keyid the active sending key on the endpoint */
1296 if (keyid == inp->sctp_ep.default_keyid)
1297 return (-1);
1298
1299 /* does the key exist? */
1300 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1301 if (skey == NULL)
1302 return (-1);
1303
1304 /* endpoint keys are not refcounted */
1305
1306 /* remove it */
1307 LIST_REMOVE(skey, next);
1308 sctp_free_sharedkey(skey); /* frees skey->key as well */
1309
1310 /* clear any cached keys */
1311 sctp_clear_cachedkeys_ep(inp, keyid);
1312 return (0);
1313}
1314
1315/*-
1316 * set the active key on an association
1317 * ASSUMES TCB_LOCK is already held
1318 */
1319int
1320sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1321{
1322 sctp_sharedkey_t *skey = NULL;
1323
1324 /* find the key on the assoc */
1325 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1326 if (skey == NULL) {
1327 /* that key doesn't exist */
1328 return (-1);
1329 }
1330 if ((skey->deactivated) && (skey->refcount > 1)) {
1331 /* can't reactivate a deactivated key with other refcounts */
1332 return (-1);
1333 }
1334
1335 /* set the (new) active key */
1336 stcb->asoc.authinfo.active_keyid = keyid;
1337 /* reset the deactivated flag */
1338 skey->deactivated = 0;
1339
1340 return (0);
1341}
1342
1343/*-
1344 * set the active key on an endpoint
1345 * ASSUMES INP_WLOCK is already held
1346 */
1347int
1348sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1349{
1350 sctp_sharedkey_t *skey;
1351
1352 /* find the key */
1353 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1354 if (skey == NULL) {
1355 /* that key doesn't exist */
1356 return (-1);
1357 }
1358 inp->sctp_ep.default_keyid = keyid;
1359 return (0);
1360}
1361
1362/*-
1363 * deactivates a shared key from the association
1364 * ASSUMES INP_WLOCK is already held
1365 */
1366int
1367sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1368{
1369 sctp_sharedkey_t *skey;
1370
1371 if (stcb == NULL)
1372 return (-1);
1373
1374 /* is the keyid the assoc active sending key */
1375 if (keyid == stcb->asoc.authinfo.active_keyid)
1376 return (-1);
1377
1378 /* does the key exist? */
1379 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1380 if (skey == NULL)
1381 return (-1);
1382
1383 /* are there other refcount holders on the key? */
1384 if (skey->refcount == 1) {
1385 /* no other users, send a notification for this key */
1386 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1387 SCTP_SO_LOCKED);
1388 }
1389
1390 /* mark the key as deactivated */
1391 skey->deactivated = 1;
1392
1393 return (0);
1394}
1395
1396/*-
1397 * deactivates a shared key from the endpoint
1398 * ASSUMES INP_WLOCK is already held
1399 */
1400int
1401sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1402{
1403 sctp_sharedkey_t *skey;
1404
1405 if (inp == NULL)
1406 return (-1);
1407
1408 /* is the keyid the active sending key on the endpoint */
1409 if (keyid == inp->sctp_ep.default_keyid)
1410 return (-1);
1411
1412 /* does the key exist? */
1413 skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1414 if (skey == NULL)
1415 return (-1);
1416
1417 /* endpoint keys are not refcounted */
1418
1419 /* remove it */
1420 LIST_REMOVE(skey, next);
1421 sctp_free_sharedkey(skey); /* frees skey->key as well */
1422
1423 return (0);
1424}
1425
1426/*
1427 * get local authentication parameters from cookie (from INIT-ACK)
1428 */
1429void
1430sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1431 uint32_t offset, uint32_t length)
1432{
1433 struct sctp_paramhdr *phdr, tmp_param;
1434 uint16_t plen, ptype;
1435 uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1436 struct sctp_auth_random *p_random = NULL;
1437 uint16_t random_len = 0;
1438 uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1439 struct sctp_auth_hmac_algo *hmacs = NULL;
1440 uint16_t hmacs_len = 0;
1441 uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1442 struct sctp_auth_chunk_list *chunks = NULL;
1443 uint16_t num_chunks = 0;
1444 sctp_key_t *new_key;
1445 uint32_t keylen;
1446
1447 /* convert to upper bound */
1448 length += offset;
1449
1450 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1451 sizeof(struct sctp_paramhdr), (uint8_t *)&tmp_param);
1452 while (phdr != NULL) {
1453 ptype = ntohs(phdr->param_type);
1454 plen = ntohs(phdr->param_length);
1455
1456 if ((plen == 0) || (offset + plen > length))
1457 break;
1458
1459 if (ptype == SCTP_RANDOM) {
1460 if (plen > sizeof(random_store))
1461 break;
1462 phdr = sctp_get_next_param(m, offset,
1463 (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1464 if (phdr == NULL)
1465 return;
1466 /* save the random and length for the key */
1467 p_random = (struct sctp_auth_random *)phdr;
1468 random_len = plen - sizeof(*p_random);
1469 } else if (ptype == SCTP_HMAC_LIST) {
1470 uint16_t num_hmacs;
1471 uint16_t i;
1472
1473 if (plen > sizeof(hmacs_store))
1474 break;
1475 phdr = sctp_get_next_param(m, offset,
1476 (struct sctp_paramhdr *)hmacs_store, min(plen,sizeof(hmacs_store)));
1477 if (phdr == NULL)
1478 return;
1479 /* save the hmacs list and num for the key */
1480 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1481 hmacs_len = plen - sizeof(*hmacs);
1482 num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1483 if (stcb->asoc.local_hmacs != NULL)
1484 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1485 stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1486 if (stcb->asoc.local_hmacs != NULL) {
1487 for (i = 0; i < num_hmacs; i++) {
1488 (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1489 ntohs(hmacs->hmac_ids[i]));
1490 }
1491 }
1492 } else if (ptype == SCTP_CHUNK_LIST) {
1493 int i;
1494
1495 if (plen > sizeof(chunks_store))
1496 break;
1497 phdr = sctp_get_next_param(m, offset,
1498 (struct sctp_paramhdr *)chunks_store, min(plen,sizeof(chunks_store)));
1499 if (phdr == NULL)
1500 return;
1501 chunks = (struct sctp_auth_chunk_list *)phdr;
1502 num_chunks = plen - sizeof(*chunks);
1503 /* save chunks list and num for the key */
1504 if (stcb->asoc.local_auth_chunks != NULL)
1505 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1506 else
1507 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1508 for (i = 0; i < num_chunks; i++) {
1509 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1510 stcb->asoc.local_auth_chunks);
1511 }
1512 }
1513 /* get next parameter */
1514 offset += SCTP_SIZE32(plen);
1515 if (offset + sizeof(struct sctp_paramhdr) > length)
1516 break;
1517 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1518 (uint8_t *)&tmp_param);
1519 }
1520 /* concatenate the full random key */
1521 keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1522 if (chunks != NULL) {
1523 keylen += sizeof(*chunks) + num_chunks;
1524 }
1525 new_key = sctp_alloc_key(keylen);
1526 if (new_key != NULL) {
1527 /* copy in the RANDOM */
1528 if (p_random != NULL) {
1529 keylen = sizeof(*p_random) + random_len;
1530 bcopy(p_random, new_key->key, keylen);
1531 }
1532 /* append in the AUTH chunks */
1533 if (chunks != NULL) {
1534 bcopy(chunks, new_key->key + keylen,
1535 sizeof(*chunks) + num_chunks);
1536 keylen += sizeof(*chunks) + num_chunks;
1537 }
1538 /* append in the HMACs */
1539 if (hmacs != NULL) {
1540 bcopy(hmacs, new_key->key + keylen,
1541 sizeof(*hmacs) + hmacs_len);
1542 }
1543 }
1544 if (stcb->asoc.authinfo.random != NULL)
1545 sctp_free_key(stcb->asoc.authinfo.random);
1546 stcb->asoc.authinfo.random = new_key;
1547 stcb->asoc.authinfo.random_len = random_len;
1548 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1549 sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1550
1551 /* negotiate what HMAC to use for the peer */
1552 stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1553 stcb->asoc.local_hmacs);
1554
1555 /* copy defaults from the endpoint */
1556 /* FIX ME: put in cookie? */
1557 stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1558 /* copy out the shared key list (by reference) from the endpoint */
1559 (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1560 &stcb->asoc.shared_keys);
1561}
1562
1563/*
1564 * compute and fill in the HMAC digest for a packet
1565 */
1566void
1567sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1568 struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1569{
1570 uint32_t digestlen;
1571 sctp_sharedkey_t *skey;
1572 sctp_key_t *key;
1573
1574 if ((stcb == NULL) || (auth == NULL))
1575 return;
1576
1577 /* zero the digest + chunk padding */
1578 digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1579 bzero(auth->hmac, SCTP_SIZE32(digestlen));
1580
1581 /* is the desired key cached? */
1582 if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1583 (stcb->asoc.authinfo.assoc_key == NULL)) {
1584 if (stcb->asoc.authinfo.assoc_key != NULL) {
1585 /* free the old cached key */
1586 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1587 }
1588 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1589 /* the only way skey is NULL is if null key id 0 is used */
1590 if (skey != NULL)
1591 key = skey->key;
1592 else
1593 key = NULL;
1594 /* compute a new assoc key and cache it */
1595 stcb->asoc.authinfo.assoc_key =
1596 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1597 stcb->asoc.authinfo.peer_random, key);
1598 stcb->asoc.authinfo.assoc_keyid = keyid;
1599 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1600 stcb->asoc.authinfo.assoc_keyid);
1601#ifdef SCTP_DEBUG
1602 if (SCTP_AUTH_DEBUG)
1603 sctp_print_key(stcb->asoc.authinfo.assoc_key,
1604 "Assoc Key");
1605#endif
1606 }
1607
1608 /* set in the active key id */
1609 auth->shared_key_id = htons(keyid);
1610
1611 /* compute and fill in the digest */
1612 (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1613 m, auth_offset, auth->hmac);
1614}
1615
1616
1617static void
1618sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1619{
1620 struct mbuf *m_tmp;
1621 uint8_t *data;
1622
1623 /* sanity check */
1624 if (m == NULL)
1625 return;
1626
1627 /* find the correct starting mbuf and offset (get start position) */
1628 m_tmp = m;
1629 while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1630 m_offset -= SCTP_BUF_LEN(m_tmp);
1631 m_tmp = SCTP_BUF_NEXT(m_tmp);
1632 }
1633 /* now use the rest of the mbuf chain */
1634 while ((m_tmp != NULL) && (size > 0)) {
1635 data = mtod(m_tmp, uint8_t *) + m_offset;
1636 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1637 bzero(data, SCTP_BUF_LEN(m_tmp));
1638 size -= SCTP_BUF_LEN(m_tmp);
1639 } else {
1640 bzero(data, size);
1641 size = 0;
1642 }
1643 /* clear the offset since it's only for the first mbuf */
1644 m_offset = 0;
1645 m_tmp = SCTP_BUF_NEXT(m_tmp);
1646 }
1647}
1648
1649/*-
1650 * process the incoming Authentication chunk
1651 * return codes:
1652 * -1 on any authentication error
1653 * 0 on authentication verification
1654 */
1655int
1656sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1657 struct mbuf *m, uint32_t offset)
1658{
1659 uint16_t chunklen;
1660 uint16_t shared_key_id;
1661 uint16_t hmac_id;
1662 sctp_sharedkey_t *skey;
1663 uint32_t digestlen;
1664 uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1665 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1666
1667 /* auth is checked for NULL by caller */
1668 chunklen = ntohs(auth->ch.chunk_length);
1669 if (chunklen < sizeof(*auth)) {
1670 SCTP_STAT_INCR(sctps_recvauthfailed);
1671 return (-1);
1672 }
1673 SCTP_STAT_INCR(sctps_recvauth);
1674
1675 /* get the auth params */
1676 shared_key_id = ntohs(auth->shared_key_id);
1677 hmac_id = ntohs(auth->hmac_id);
1678 SCTPDBG(SCTP_DEBUG_AUTH1,
1679 "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1680 shared_key_id, hmac_id);
1681
1682 /* is the indicated HMAC supported? */
1683 if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1684 struct mbuf *op_err;
1685 struct sctp_error_auth_invalid_hmac *cause;
1686
1687 SCTP_STAT_INCR(sctps_recvivalhmacid);
1688 SCTPDBG(SCTP_DEBUG_AUTH1,
1689 "SCTP Auth: unsupported HMAC id %u\n",
1690 hmac_id);
1691 /*
1692 * report this in an Error Chunk: Unsupported HMAC
1693 * Identifier
1694 */
1695 op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
1696 0, M_NOWAIT, 1, MT_HEADER);
1697 if (op_err != NULL) {
1698 /* pre-reserve some space */
1699 SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
1700 /* fill in the error */
1701 cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
1702 cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1703 cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
1704 cause->hmac_id = ntohs(hmac_id);
1705 SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
1706 /* queue it */
1707 sctp_queue_op_err(stcb, op_err);
1708 }
1709 return (-1);
1710 }
1711 /* get the indicated shared key, if available */
1712 if ((stcb->asoc.authinfo.recv_key == NULL) ||
1713 (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1714 /* find the shared key on the assoc first */
1715 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1716 shared_key_id);
1717 /* if the shared key isn't found, discard the chunk */
1718 if (skey == NULL) {
1719 SCTP_STAT_INCR(sctps_recvivalkeyid);
1720 SCTPDBG(SCTP_DEBUG_AUTH1,
1721 "SCTP Auth: unknown key id %u\n",
1722 shared_key_id);
1723 return (-1);
1724 }
1725 /* generate a notification if this is a new key id */
1726 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1727 /*
1728 * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1729 * shared_key_id, (void
1730 * *)stcb->asoc.authinfo.recv_keyid);
1731 */
1732 sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
1733 shared_key_id, stcb->asoc.authinfo.recv_keyid,
1734 SCTP_SO_NOT_LOCKED);
1735 /* compute a new recv assoc key and cache it */
1736 if (stcb->asoc.authinfo.recv_key != NULL)
1737 sctp_free_key(stcb->asoc.authinfo.recv_key);
1738 stcb->asoc.authinfo.recv_key =
1739 sctp_compute_hashkey(stcb->asoc.authinfo.random,
1740 stcb->asoc.authinfo.peer_random, skey->key);
1741 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1742#ifdef SCTP_DEBUG
1743 if (SCTP_AUTH_DEBUG)
1744 sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1745#endif
1746 }
1747 /* validate the digest length */
1748 digestlen = sctp_get_hmac_digest_len(hmac_id);
1749 if (chunklen < (sizeof(*auth) + digestlen)) {
1750 /* invalid digest length */
1751 SCTP_STAT_INCR(sctps_recvauthfailed);
1752 SCTPDBG(SCTP_DEBUG_AUTH1,
1753 "SCTP Auth: chunk too short for HMAC\n");
1754 return (-1);
1755 }
1756 /* save a copy of the digest, zero the pseudo header, and validate */
1757 bcopy(auth->hmac, digest, digestlen);
1758 sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1759 (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1760 m, offset, computed_digest);
1761
1762 /* compare the computed digest with the one in the AUTH chunk */
1763 if (memcmp(digest, computed_digest, digestlen) != 0) {
1764 SCTP_STAT_INCR(sctps_recvauthfailed);
1765 SCTPDBG(SCTP_DEBUG_AUTH1,
1766 "SCTP Auth: HMAC digest check failed\n");
1767 return (-1);
1768 }
1769 return (0);
1770}
1771
1772/*
1773 * Generate NOTIFICATION
1774 */
1775void
1776sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1777 uint16_t keyid, uint16_t alt_keyid, int so_locked
1778#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1779 SCTP_UNUSED
1780#endif
1781)
1782{
1783 struct mbuf *m_notify;
1784 struct sctp_authkey_event *auth;
1785 struct sctp_queued_to_read *control;
1786
1787 if ((stcb == NULL) ||
1788 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1789 (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1790 (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1791 ) {
1792 /* If the socket is gone we are out of here */
1793 return;
1794 }
1795
1796 if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
1797 /* event not enabled */
1798 return;
1799
1800 m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1801 0, M_NOWAIT, 1, MT_HEADER);
1802 if (m_notify == NULL)
1803 /* no space left */
1804 return;
1805
1806 SCTP_BUF_LEN(m_notify) = 0;
1807 auth = mtod(m_notify, struct sctp_authkey_event *);
1808 memset(auth, 0, sizeof(struct sctp_authkey_event));
1809 auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1810 auth->auth_flags = 0;
1811 auth->auth_length = sizeof(*auth);
1812 auth->auth_keynumber = keyid;
1813 auth->auth_altkeynumber = alt_keyid;
1814 auth->auth_indication = indication;
1815 auth->auth_assoc_id = sctp_get_associd(stcb);
1816
1817 SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1818 SCTP_BUF_NEXT(m_notify) = NULL;
1819
1820 /* append to socket */
1821 control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1822 0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
1823 if (control == NULL) {
1824 /* no memory */
1825 sctp_m_freem(m_notify);
1826 return;
1827 }
1828 control->spec_flags = M_NOTIFICATION;
1829 control->length = SCTP_BUF_LEN(m_notify);
1830 /* not that we need this */
1831 control->tail_mbuf = m_notify;
1832 sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1833 &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1834}
1835
1836
1837/*-
1838 * validates the AUTHentication related parameters in an INIT/INIT-ACK
1839 * Note: currently only used for INIT as INIT-ACK is handled inline
1840 * with sctp_load_addresses_from_init()
1841 */
1842int
1843sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1844{
1845 struct sctp_paramhdr *phdr, parm_buf;
1846 uint16_t ptype, plen;
1847 int peer_supports_asconf = 0;
1848 int peer_supports_auth = 0;
1849 int got_random = 0, got_hmacs = 0, got_chklist = 0;
1850 uint8_t saw_asconf = 0;
1851 uint8_t saw_asconf_ack = 0;
1852
1853 /* go through each of the params. */
1854 phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1855 while (phdr) {
1856 ptype = ntohs(phdr->param_type);
1857 plen = ntohs(phdr->param_length);
1858
1859 if (offset + plen > limit) {
1860 break;
1861 }
1862 if (plen < sizeof(struct sctp_paramhdr)) {
1863 break;
1864 }
1865 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1866 /* A supported extension chunk */
1867 struct sctp_supported_chunk_types_param *pr_supported;
1868 uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1869 int num_ent, i;
1870
1871 phdr = sctp_get_next_param(m, offset,
1872 (struct sctp_paramhdr *)&local_store, min(plen,sizeof(local_store)));
1873 if (phdr == NULL) {
1874 return (-1);
1875 }
1876 pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
1877 num_ent = plen - sizeof(struct sctp_paramhdr);
1878 for (i = 0; i < num_ent; i++) {
1879 switch (pr_supported->chunk_types[i]) {
1880 case SCTP_ASCONF:
1881 case SCTP_ASCONF_ACK:
1882 peer_supports_asconf = 1;
1883 break;
1884 default:
1885 /* one we don't care about */
1886 break;
1887 }
1888 }
1889 } else if (ptype == SCTP_RANDOM) {
1890 got_random = 1;
1891 /* enforce the random length */
1892 if (plen != (sizeof(struct sctp_auth_random) +
1893 SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
1894 SCTPDBG(SCTP_DEBUG_AUTH1,
1895 "SCTP: invalid RANDOM len\n");
1896 return (-1);
1897 }
1898 } else if (ptype == SCTP_HMAC_LIST) {
1899 uint8_t store[SCTP_PARAM_BUFFER_SIZE];
1900 struct sctp_auth_hmac_algo *hmacs;
1901 int num_hmacs;
1902
1903 if (plen > sizeof(store))
1904 break;
1905 phdr = sctp_get_next_param(m, offset,
1906 (struct sctp_paramhdr *)store, min(plen,sizeof(store)));
1907 if (phdr == NULL)
1908 return (-1);
1909 hmacs = (struct sctp_auth_hmac_algo *)phdr;
1910 num_hmacs = (plen - sizeof(*hmacs)) /
1911 sizeof(hmacs->hmac_ids[0]);
1912 /* validate the hmac list */
1913 if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
1914 SCTPDBG(SCTP_DEBUG_AUTH1,
1915 "SCTP: invalid HMAC param\n");
1916 return (-1);
1917 }
1918 got_hmacs = 1;
1919 } else if (ptype == SCTP_CHUNK_LIST) {
1920 int i, num_chunks;
1921 uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
1922 /* did the peer send a non-empty chunk list? */
1923 struct sctp_auth_chunk_list *chunks = NULL;
1924 phdr = sctp_get_next_param(m, offset,
1925 (struct sctp_paramhdr *)chunks_store,
1926 min(plen,sizeof(chunks_store)));
1927 if (phdr == NULL)
1928 return (-1);
1929
1930 /*-
1931 * Flip through the list and mark that the
1932 * peer supports asconf/asconf_ack.
1933 */
1934 chunks = (struct sctp_auth_chunk_list *)phdr;
1935 num_chunks = plen - sizeof(*chunks);
1936 for (i = 0; i < num_chunks; i++) {
1937 /* record asconf/asconf-ack if listed */
1938 if (chunks->chunk_types[i] == SCTP_ASCONF)
1939 saw_asconf = 1;
1940 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
1941 saw_asconf_ack = 1;
1942
1943 }
1944 if (num_chunks)
1945 got_chklist = 1;
1946 }
1947
1948 offset += SCTP_SIZE32(plen);
1949 if (offset >= limit) {
1950 break;
1951 }
1952 phdr = sctp_get_next_param(m, offset, &parm_buf,
1953 sizeof(parm_buf));
1954 }
1955 /* validate authentication required parameters */
1956 if (got_random && got_hmacs) {
1957 peer_supports_auth = 1;
1958 } else {
1959 peer_supports_auth = 0;
1960 }
1961 if (!peer_supports_auth && got_chklist) {
1962 SCTPDBG(SCTP_DEBUG_AUTH1,
1963 "SCTP: peer sent chunk list w/o AUTH\n");
1964 return (-1);
1965 }
1966 if (peer_supports_asconf && !peer_supports_auth) {
1967 SCTPDBG(SCTP_DEBUG_AUTH1,
1968 "SCTP: peer supports ASCONF but not AUTH\n");
1969 return (-1);
1970 } else if ((peer_supports_asconf) && (peer_supports_auth) &&
1971 ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
1972 return (-2);
1973 }
1974 return (0);
1975}
1976
1977void
1978sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
1979{
1980 uint16_t chunks_len = 0;
1981 uint16_t hmacs_len = 0;
1982 uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
1983 sctp_key_t *new_key;
1984 uint16_t keylen;
1985
1986 /* initialize hmac list from endpoint */
1987 stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
1988 if (stcb->asoc.local_hmacs != NULL) {
1989 hmacs_len = stcb->asoc.local_hmacs->num_algo *
1990 sizeof(stcb->asoc.local_hmacs->hmac[0]);
1991 }
1992 /* initialize auth chunks list from endpoint */
1993 stcb->asoc.local_auth_chunks =
1994 sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
1995 if (stcb->asoc.local_auth_chunks != NULL) {
1996 int i;
1997 for (i = 0; i < 256; i++) {
1998 if (stcb->asoc.local_auth_chunks->chunks[i])
1999 chunks_len++;
2000 }
2001 }
2002 /* copy defaults from the endpoint */
2003 stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2004
2005 /* copy out the shared key list (by reference) from the endpoint */
2006 (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2007 &stcb->asoc.shared_keys);
2008
2009 /* now set the concatenated key (random + chunks + hmacs) */
2010 /* key includes parameter headers */
2011 keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2012 hmacs_len;
2013 new_key = sctp_alloc_key(keylen);
2014 if (new_key != NULL) {
2015 struct sctp_paramhdr *ph;
2016 int plen;
2017 /* generate and copy in the RANDOM */
2018 ph = (struct sctp_paramhdr *)new_key->key;
2019 ph->param_type = htons(SCTP_RANDOM);
2020 plen = sizeof(*ph) + random_len;
2021 ph->param_length = htons(plen);
2022 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2023 keylen = plen;
2024
2025 /* append in the AUTH chunks */
2026 /* NOTE: currently we always have chunks to list */
2027 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2028 ph->param_type = htons(SCTP_CHUNK_LIST);
2029 plen = sizeof(*ph) + chunks_len;
2030 ph->param_length = htons(plen);
2031 keylen += sizeof(*ph);
2032 if (stcb->asoc.local_auth_chunks) {
2033 int i;
2034 for (i = 0; i < 256; i++) {
2035 if (stcb->asoc.local_auth_chunks->chunks[i])
2036 new_key->key[keylen++] = i;
2037 }
2038 }
2039
2040 /* append in the HMACs */
2041 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2042 ph->param_type = htons(SCTP_HMAC_LIST);
2043 plen = sizeof(*ph) + hmacs_len;
2044 ph->param_length = htons(plen);
2045 keylen += sizeof(*ph);
2046 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2047 new_key->key + keylen);
2048 }
2049 if (stcb->asoc.authinfo.random != NULL)
2050 sctp_free_key(stcb->asoc.authinfo.random);
2051 stcb->asoc.authinfo.random = new_key;
2052 stcb->asoc.authinfo.random_len = random_len;
2053}
2054
2055
2056#ifdef SCTP_HMAC_TEST
2057/*
2058 * HMAC and key concatenation tests
2059 */
2060static void
2061sctp_print_digest(uint8_t *digest, uint32_t digestlen, const char *str)
2062{
2063 uint32_t i;
2064
2065 SCTP_PRINTF("\n%s: 0x", str);
2066 if (digest == NULL)
2067 return;
2068
2069 for (i = 0; i < digestlen; i++)
2070 SCTP_PRINTF("%02x", digest[i]);
2071}
2072
2073static int
2074sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t *key,
2075 uint32_t keylen, uint8_t *text, uint32_t textlen,
2076 uint8_t *digest, uint32_t digestlen)
2077{
2078 uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2079
2080 SCTP_PRINTF("\n%s:", str);
2081 sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2082 sctp_print_digest(digest, digestlen, "Expected digest");
2083 sctp_print_digest(computed_digest, digestlen, "Computed digest");
2084 if (memcmp(digest, computed_digest, digestlen) != 0) {
2085 SCTP_PRINTF("\nFAILED");
2086 return (-1);
2087 } else {
2088 SCTP_PRINTF("\nPASSED");
2089 return (0);
2090 }
2091}
2092
2093
2094/*
2095 * RFC 2202: HMAC-SHA1 test cases
2096 */
2097void
2098sctp_test_hmac_sha1(void)
2099{
2100 uint8_t *digest;
2101 uint8_t key[128];
2102 uint32_t keylen;
2103 uint8_t text[128];
2104 uint32_t textlen;
2105 uint32_t digestlen = 20;
2106 int failed = 0;
2107
2108 /*-
2109 * test_case = 1
2110 * key = 0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2111 * key_len = 20
2112 * data = "Hi There"
2113 * data_len = 8
2114 * digest = 0xb617318655057264e28bc0b6fb378c8ef146be00
2115 */
2116 keylen = 20;
2117 memset(key, 0x0b, keylen);
2118 textlen = 8;
2119 strcpy(text, "Hi There");
2120 digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2121 if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2122 text, textlen, digest, digestlen) < 0)
2123 failed++;
2124
2125 /*-
2126 * test_case = 2
2127 * key = "Jefe"
2128 * key_len = 4
2129 * data = "what do ya want for nothing?"
2130 * data_len = 28
2131 * digest = 0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2132 */
2133 keylen = 4;
2134 strcpy(key, "Jefe");
2135 textlen = 28;
2136 strcpy(text, "what do ya want for nothing?");
2137 digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2138 if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2139 text, textlen, digest, digestlen) < 0)
2140 failed++;
2141
2142 /*-
2143 * test_case = 3
2144 * key = 0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2145 * key_len = 20
2146 * data = 0xdd repeated 50 times
2147 * data_len = 50
2148 * digest = 0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2149 */
2150 keylen = 20;
2151 memset(key, 0xaa, keylen);
2152 textlen = 50;
2153 memset(text, 0xdd, textlen);
2154 digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2155 if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2156 text, textlen, digest, digestlen) < 0)
2157 failed++;
2158
2159 /*-
2160 * test_case = 4
2161 * key = 0x0102030405060708090a0b0c0d0e0f10111213141516171819
2162 * key_len = 25
2163 * data = 0xcd repeated 50 times
2164 * data_len = 50
2165 * digest = 0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2166 */
2167 keylen = 25;
2168 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);
2169 textlen = 50;
2170 memset(text, 0xcd, textlen);
2171 digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2172 if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2173 text, textlen, digest, digestlen) < 0)
2174 failed++;
2175
2176 /*-
2177 * test_case = 5
2178 * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2179 * key_len = 20
2180 * data = "Test With Truncation"
2181 * data_len = 20
2182 * digest = 0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2183 * digest-96 = 0x4c1a03424b55e07fe7f27be1
2184 */
2185 keylen = 20;
2186 memset(key, 0x0c, keylen);
2187 textlen = 20;
2188 strcpy(text, "Test With Truncation");
2189 digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2190 if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2191 text, textlen, digest, digestlen) < 0)
2192 failed++;
2193
2194 /*-
2195 * test_case = 6
2196 * key = 0xaa repeated 80 times
2197 * key_len = 80
2198 * data = "Test Using Larger Than Block-Size Key - Hash Key First"
2199 * data_len = 54
2200 * digest = 0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2201 */
2202 keylen = 80;
2203 memset(key, 0xaa, keylen);
2204 textlen = 54;
2205 strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2206 digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2207 if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2208 text, textlen, digest, digestlen) < 0)
2209 failed++;
2210
2211 /*-
2212 * test_case = 7
2213 * key = 0xaa repeated 80 times
2214 * key_len = 80
2215 * data = "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2216 * data_len = 73
2217 * digest = 0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2218 */
2219 keylen = 80;
2220 memset(key, 0xaa, keylen);
2221 textlen = 73;
2222 strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2223 digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2224 if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2225 text, textlen, digest, digestlen) < 0)
2226 failed++;
2227
2228 /* done with all tests */
2229 if (failed)
2230 SCTP_PRINTF("\nSHA1 test results: %d cases failed", failed);
2231 else
2232 SCTP_PRINTF("\nSHA1 test results: all test cases passed");
2233}
2234
2235/*
2236 * test assoc key concatenation
2237 */
2238static int
2239sctp_test_key_concatenation(sctp_key_t *key1, sctp_key_t *key2,
2240 sctp_key_t *expected_key)
2241{
2242 sctp_key_t *key;
2243 int ret_val;
2244
2245 sctp_show_key(key1, "\nkey1");
2246 sctp_show_key(key2, "\nkey2");
2247 key = sctp_compute_hashkey(key1, key2, NULL);
2248 sctp_show_key(expected_key, "\nExpected");
2249 sctp_show_key(key, "\nComputed");
2250 if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2251 SCTP_PRINTF("\nFAILED");
2252 ret_val = -1;
2253 } else {
2254 SCTP_PRINTF("\nPASSED");
2255 ret_val = 0;
2256 }
2257 sctp_free_key(key1);
2258 sctp_free_key(key2);
2259 sctp_free_key(expected_key);
2260 sctp_free_key(key);
2261 return (ret_val);
2262}
2263
2264
2265void
2266sctp_test_authkey(void)
2267{
2268 sctp_key_t *key1, *key2, *expected_key;
2269 int failed = 0;
2270
2271 /* test case 1 */
2272 key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2273 key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2274 expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2275 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2276 failed++;
2277
2278 /* test case 2 */
2279 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2280 key2 = sctp_set_key("\x02", 1);
2281 expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2282 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2283 failed++;
2284
2285 /* test case 3 */
2286 key1 = sctp_set_key("\x01", 1);
2287 key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2288 expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2289 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2290 failed++;
2291
2292 /* test case 4 */
2293 key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2294 key2 = sctp_set_key("\x01", 1);
2295 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2296 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2297 failed++;
2298
2299 /* test case 5 */
2300 key1 = sctp_set_key("\x01", 1);
2301 key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2302 expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2303 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2304 failed++;
2305
2306 /* test case 6 */
2307 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2308 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2309 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);
2310 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2311 failed++;
2312
2313 /* test case 7 */
2314 key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2315 key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2316 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);
2317 if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2318 failed++;
2319
2320 /* done with all tests */
2321 if (failed)
2322 SCTP_PRINTF("\nKey concatenation test results: %d cases failed", failed);
2323 else
2324 SCTP_PRINTF("\nKey concatenation test results: all test cases passed");
2325}
2326
2327
2328#if defined(STANDALONE_HMAC_TEST)
2329int
2330main(void)
2331{
2332 sctp_test_hmac_sha1();
2333 sctp_test_authkey();
2334}
2335
2336#endif /* STANDALONE_HMAC_TEST */
2337
2338#endif /* SCTP_HMAC_TEST */