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