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