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