]> CyberLeo.Net >> Repos - FreeBSD/releng/8.0.git/blob - sys/netinet/sctp_auth.c
Adjust to reflect 8.0-RELEASE.
[FreeBSD/releng/8.0.git] / sys / netinet / sctp_auth.c
1 /*-
2  * Copyright (c) 2001-2008, 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_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
47 #define SCTP_AUTH_DEBUG2        (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
48 #endif                          /* SCTP_DEBUG */
49
50
51 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             SCTP_M_AUTH_CL);
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, SCTP_M_AUTH_CL);
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 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             SCTP_M_AUTH_KY);
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, SCTP_M_AUTH_KY);
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 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
365  *          -1 if key1 < key2
366  *           0 if key1 = key2
367  */
368 static int
369 sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
370 {
371         uint32_t maxlen;
372         uint32_t i;
373         uint32_t key1len, key2len;
374         uint8_t *key_1, *key_2;
375         uint8_t temp[SCTP_AUTH_RANDOM_SIZE_MAX];
376
377         /* sanity/length check */
378         key1len = sctp_get_keylen(key1);
379         key2len = sctp_get_keylen(key2);
380         if ((key1len == 0) && (key2len == 0))
381                 return (0);
382         else if (key1len == 0)
383                 return (-1);
384         else if (key2len == 0)
385                 return (1);
386
387         if (key1len != key2len) {
388                 if (key1len >= key2len)
389                         maxlen = key1len;
390                 else
391                         maxlen = key2len;
392                 bzero(temp, maxlen);
393                 if (key1len < maxlen) {
394                         /* prepend zeroes to key1 */
395                         bcopy(key1->key, temp + (maxlen - key1len), key1len);
396                         key_1 = temp;
397                         key_2 = key2->key;
398                 } else {
399                         /* prepend zeroes to key2 */
400                         bcopy(key2->key, temp + (maxlen - key2len), key2len);
401                         key_1 = key1->key;
402                         key_2 = temp;
403                 }
404         } else {
405                 maxlen = key1len;
406                 key_1 = key1->key;
407                 key_2 = key2->key;
408         }
409
410         for (i = 0; i < maxlen; i++) {
411                 if (*key_1 > *key_2)
412                         return (1);
413                 else if (*key_1 < *key_2)
414                         return (-1);
415                 key_1++;
416                 key_2++;
417         }
418
419         /* keys are equal value, so check lengths */
420         if (key1len == key2len)
421                 return (0);
422         else if (key1len < key2len)
423                 return (-1);
424         else
425                 return (1);
426 }
427
428 /*
429  * generate the concatenated keying material based on the two keys and the
430  * shared key (if available). draft-ietf-tsvwg-auth specifies the specific
431  * order for concatenation
432  */
433 sctp_key_t *
434 sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
435 {
436         uint32_t keylen;
437         sctp_key_t *new_key;
438         uint8_t *key_ptr;
439
440         keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
441             sctp_get_keylen(shared);
442
443         if (keylen > 0) {
444                 /* get space for the new key */
445                 new_key = sctp_alloc_key(keylen);
446                 if (new_key == NULL) {
447                         /* out of memory */
448                         return (NULL);
449                 }
450                 new_key->keylen = keylen;
451                 key_ptr = new_key->key;
452         } else {
453                 /* all keys empty/null?! */
454                 return (NULL);
455         }
456
457         /* concatenate the keys */
458         if (sctp_compare_key(key1, key2) <= 0) {
459 #ifdef SCTP_AUTH_DRAFT_04
460                 /* key is key1 + shared + key2 */
461                 if (sctp_get_keylen(key1)) {
462                         bcopy(key1->key, key_ptr, key1->keylen);
463                         key_ptr += key1->keylen;
464                 }
465                 if (sctp_get_keylen(shared)) {
466                         bcopy(shared->key, key_ptr, shared->keylen);
467                         key_ptr += shared->keylen;
468                 }
469                 if (sctp_get_keylen(key2)) {
470                         bcopy(key2->key, key_ptr, key2->keylen);
471                         key_ptr += key2->keylen;
472                 }
473 #else
474                 /* key is shared + key1 + key2 */
475                 if (sctp_get_keylen(shared)) {
476                         bcopy(shared->key, key_ptr, shared->keylen);
477                         key_ptr += shared->keylen;
478                 }
479                 if (sctp_get_keylen(key1)) {
480                         bcopy(key1->key, key_ptr, key1->keylen);
481                         key_ptr += key1->keylen;
482                 }
483                 if (sctp_get_keylen(key2)) {
484                         bcopy(key2->key, key_ptr, key2->keylen);
485                         key_ptr += key2->keylen;
486                 }
487 #endif
488         } else {
489 #ifdef SCTP_AUTH_DRAFT_04
490                 /* key is key2 + shared + key1 */
491                 if (sctp_get_keylen(key2)) {
492                         bcopy(key2->key, key_ptr, key2->keylen);
493                         key_ptr += key2->keylen;
494                 }
495                 if (sctp_get_keylen(shared)) {
496                         bcopy(shared->key, key_ptr, shared->keylen);
497                         key_ptr += shared->keylen;
498                 }
499                 if (sctp_get_keylen(key1)) {
500                         bcopy(key1->key, key_ptr, key1->keylen);
501                         key_ptr += key1->keylen;
502                 }
503 #else
504                 /* key is shared + key2 + key1 */
505                 if (sctp_get_keylen(shared)) {
506                         bcopy(shared->key, key_ptr, shared->keylen);
507                         key_ptr += shared->keylen;
508                 }
509                 if (sctp_get_keylen(key2)) {
510                         bcopy(key2->key, key_ptr, key2->keylen);
511                         key_ptr += key2->keylen;
512                 }
513                 if (sctp_get_keylen(key1)) {
514                         bcopy(key1->key, key_ptr, key1->keylen);
515                         key_ptr += key1->keylen;
516                 }
517 #endif
518         }
519         return (new_key);
520 }
521
522
523 sctp_sharedkey_t *
524 sctp_alloc_sharedkey(void)
525 {
526         sctp_sharedkey_t *new_key;
527
528         SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
529             SCTP_M_AUTH_KY);
530         if (new_key == NULL) {
531                 /* out of memory */
532                 return (NULL);
533         }
534         new_key->keyid = 0;
535         new_key->key = NULL;
536         new_key->refcount = 1;
537         new_key->deactivated = 0;
538         return (new_key);
539 }
540
541 void
542 sctp_free_sharedkey(sctp_sharedkey_t * skey)
543 {
544         if (skey == NULL)
545                 return;
546
547         if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
548                 if (skey->key != NULL)
549                         sctp_free_key(skey->key);
550                 SCTP_FREE(skey, SCTP_M_AUTH_KY);
551         }
552 }
553
554 sctp_sharedkey_t *
555 sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
556 {
557         sctp_sharedkey_t *skey;
558
559         LIST_FOREACH(skey, shared_keys, next) {
560                 if (skey->keyid == key_id)
561                         return (skey);
562         }
563         return (NULL);
564 }
565
566 int
567 sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
568     sctp_sharedkey_t * new_skey)
569 {
570         sctp_sharedkey_t *skey;
571
572         if ((shared_keys == NULL) || (new_skey == NULL))
573                 return (EINVAL);
574
575         /* insert into an empty list? */
576         if (SCTP_LIST_EMPTY(shared_keys)) {
577                 LIST_INSERT_HEAD(shared_keys, new_skey, next);
578                 return (0);
579         }
580         /* insert into the existing list, ordered by key id */
581         LIST_FOREACH(skey, shared_keys, next) {
582                 if (new_skey->keyid < skey->keyid) {
583                         /* insert it before here */
584                         LIST_INSERT_BEFORE(skey, new_skey, next);
585                         return (0);
586                 } else if (new_skey->keyid == skey->keyid) {
587                         /* replace the existing key */
588                         /* verify this key *can* be replaced */
589                         if ((skey->deactivated) && (skey->refcount > 1)) {
590                                 SCTPDBG(SCTP_DEBUG_AUTH1,
591                                     "can't replace shared key id %u\n",
592                                     new_skey->keyid);
593                                 return (EBUSY);
594                         }
595                         SCTPDBG(SCTP_DEBUG_AUTH1,
596                             "replacing shared key id %u\n",
597                             new_skey->keyid);
598                         LIST_INSERT_BEFORE(skey, new_skey, next);
599                         LIST_REMOVE(skey, next);
600                         sctp_free_sharedkey(skey);
601                         return (0);
602                 }
603                 if (LIST_NEXT(skey, next) == NULL) {
604                         /* belongs at the end of the list */
605                         LIST_INSERT_AFTER(skey, new_skey, next);
606                         return (0);
607                 }
608         }
609         /* shouldn't reach here */
610         return (0);
611 }
612
613 void
614 sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
615 {
616         sctp_sharedkey_t *skey;
617
618         /* find the shared key */
619         skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
620
621         /* bump the ref count */
622         if (skey) {
623                 atomic_add_int(&skey->refcount, 1);
624                 SCTPDBG(SCTP_DEBUG_AUTH2,
625                     "%s: stcb %p key %u refcount acquire to %d\n",
626                     __FUNCTION__, stcb, key_id, skey->refcount);
627         }
628 }
629
630 void
631 sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id)
632 {
633         sctp_sharedkey_t *skey;
634
635         /* find the shared key */
636         skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
637
638         /* decrement the ref count */
639         if (skey) {
640                 sctp_free_sharedkey(skey);
641                 SCTPDBG(SCTP_DEBUG_AUTH2,
642                     "%s: stcb %p key %u refcount release to %d\n",
643                     __FUNCTION__, stcb, key_id, skey->refcount);
644
645                 /* see if a notification should be generated */
646                 if ((skey->refcount <= 1) && (skey->deactivated)) {
647                         /* notify ULP that key is no longer used */
648                         sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
649                             key_id, 0, SCTP_SO_NOT_LOCKED);
650                         SCTPDBG(SCTP_DEBUG_AUTH2,
651                             "%s: stcb %p key %u no longer used, %d\n",
652                             __FUNCTION__, stcb, key_id, skey->refcount);
653                 }
654         }
655 }
656
657 static sctp_sharedkey_t *
658 sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
659 {
660         sctp_sharedkey_t *new_skey;
661
662         if (skey == NULL)
663                 return (NULL);
664         new_skey = sctp_alloc_sharedkey();
665         if (new_skey == NULL)
666                 return (NULL);
667         if (skey->key != NULL)
668                 new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
669         else
670                 new_skey->key = NULL;
671         new_skey->keyid = skey->keyid;
672         return (new_skey);
673 }
674
675 int
676 sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
677 {
678         sctp_sharedkey_t *skey, *new_skey;
679         int count = 0;
680
681         if ((src == NULL) || (dest == NULL))
682                 return (0);
683         LIST_FOREACH(skey, src, next) {
684                 new_skey = sctp_copy_sharedkey(skey);
685                 if (new_skey != NULL) {
686                         (void)sctp_insert_sharedkey(dest, new_skey);
687                         count++;
688                 }
689         }
690         return (count);
691 }
692
693
694 sctp_hmaclist_t *
695 sctp_alloc_hmaclist(uint8_t num_hmacs)
696 {
697         sctp_hmaclist_t *new_list;
698         int alloc_size;
699
700         alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
701         SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
702             SCTP_M_AUTH_HL);
703         if (new_list == NULL) {
704                 /* out of memory */
705                 return (NULL);
706         }
707         new_list->max_algo = num_hmacs;
708         new_list->num_algo = 0;
709         return (new_list);
710 }
711
712 void
713 sctp_free_hmaclist(sctp_hmaclist_t * list)
714 {
715         if (list != NULL) {
716                 SCTP_FREE(list, SCTP_M_AUTH_HL);
717                 list = NULL;
718         }
719 }
720
721 int
722 sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
723 {
724         int i;
725
726         if (list == NULL)
727                 return (-1);
728         if (list->num_algo == list->max_algo) {
729                 SCTPDBG(SCTP_DEBUG_AUTH1,
730                     "SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
731                 return (-1);
732         }
733         if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
734 #ifdef HAVE_SHA224
735             (hmac_id != SCTP_AUTH_HMAC_ID_SHA224) &&
736 #endif
737 #ifdef HAVE_SHA2
738             (hmac_id != SCTP_AUTH_HMAC_ID_SHA256) &&
739             (hmac_id != SCTP_AUTH_HMAC_ID_SHA384) &&
740             (hmac_id != SCTP_AUTH_HMAC_ID_SHA512) &&
741 #endif
742             (hmac_id != SCTP_AUTH_HMAC_ID_MD5)) {
743                 return (-1);
744         }
745         /* Now is it already in the list */
746         for (i = 0; i < list->num_algo; i++) {
747                 if (list->hmac[i] == hmac_id) {
748                         /* already in list */
749                         return (-1);
750                 }
751         }
752         SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
753         list->hmac[list->num_algo++] = hmac_id;
754         return (0);
755 }
756
757 sctp_hmaclist_t *
758 sctp_copy_hmaclist(sctp_hmaclist_t * list)
759 {
760         sctp_hmaclist_t *new_list;
761         int i;
762
763         if (list == NULL)
764                 return (NULL);
765         /* get a new list */
766         new_list = sctp_alloc_hmaclist(list->max_algo);
767         if (new_list == NULL)
768                 return (NULL);
769         /* copy it */
770         new_list->max_algo = list->max_algo;
771         new_list->num_algo = list->num_algo;
772         for (i = 0; i < list->num_algo; i++)
773                 new_list->hmac[i] = list->hmac[i];
774         return (new_list);
775 }
776
777 sctp_hmaclist_t *
778 sctp_default_supported_hmaclist(void)
779 {
780         sctp_hmaclist_t *new_list;
781
782         new_list = sctp_alloc_hmaclist(2);
783         if (new_list == NULL)
784                 return (NULL);
785         (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
786         (void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
787         return (new_list);
788 }
789
790 /*-
791  * HMAC algos are listed in priority/preference order
792  * find the best HMAC id to use for the peer based on local support
793  */
794 uint16_t
795 sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
796 {
797         int i, j;
798
799         if ((local == NULL) || (peer == NULL))
800                 return (SCTP_AUTH_HMAC_ID_RSVD);
801
802         for (i = 0; i < peer->num_algo; i++) {
803                 for (j = 0; j < local->num_algo; j++) {
804                         if (peer->hmac[i] == local->hmac[j]) {
805 #ifndef SCTP_AUTH_DRAFT_04
806                                 /* "skip" MD5 as it's been deprecated */
807                                 if (peer->hmac[i] == SCTP_AUTH_HMAC_ID_MD5)
808                                         continue;
809 #endif
810
811                                 /* found the "best" one */
812                                 SCTPDBG(SCTP_DEBUG_AUTH1,
813                                     "SCTP: negotiated peer HMAC id %u\n",
814                                     peer->hmac[i]);
815                                 return (peer->hmac[i]);
816                         }
817                 }
818         }
819         /* didn't find one! */
820         return (SCTP_AUTH_HMAC_ID_RSVD);
821 }
822
823 /*-
824  * serialize the HMAC algo list and return space used
825  * caller must guarantee ptr has appropriate space
826  */
827 int
828 sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
829 {
830         int i;
831         uint16_t hmac_id;
832
833         if (list == NULL)
834                 return (0);
835
836         for (i = 0; i < list->num_algo; i++) {
837                 hmac_id = htons(list->hmac[i]);
838                 bcopy(&hmac_id, ptr, sizeof(hmac_id));
839                 ptr += sizeof(hmac_id);
840         }
841         return (list->num_algo * sizeof(hmac_id));
842 }
843
844 int
845 sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
846 {
847         uint32_t i;
848         uint16_t hmac_id;
849         uint32_t sha1_supported = 0;
850
851         for (i = 0; i < num_hmacs; i++) {
852                 hmac_id = ntohs(hmacs->hmac_ids[i]);
853                 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1)
854                         sha1_supported = 1;
855         }
856         /* all HMAC id's are supported */
857         if (sha1_supported == 0)
858                 return (-1);
859         else
860                 return (0);
861 }
862
863 sctp_authinfo_t *
864 sctp_alloc_authinfo(void)
865 {
866         sctp_authinfo_t *new_authinfo;
867
868         SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
869             SCTP_M_AUTH_IF);
870
871         if (new_authinfo == NULL) {
872                 /* out of memory */
873                 return (NULL);
874         }
875         bzero(new_authinfo, sizeof(*new_authinfo));
876         return (new_authinfo);
877 }
878
879 void
880 sctp_free_authinfo(sctp_authinfo_t * authinfo)
881 {
882         if (authinfo == NULL)
883                 return;
884
885         if (authinfo->random != NULL)
886                 sctp_free_key(authinfo->random);
887         if (authinfo->peer_random != NULL)
888                 sctp_free_key(authinfo->peer_random);
889         if (authinfo->assoc_key != NULL)
890                 sctp_free_key(authinfo->assoc_key);
891         if (authinfo->recv_key != NULL)
892                 sctp_free_key(authinfo->recv_key);
893
894         /* We are NOT dynamically allocating authinfo's right now... */
895         /* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
896 }
897
898
899 uint32_t
900 sctp_get_auth_chunk_len(uint16_t hmac_algo)
901 {
902         int size;
903
904         size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
905         return (SCTP_SIZE32(size));
906 }
907
908 uint32_t
909 sctp_get_hmac_digest_len(uint16_t hmac_algo)
910 {
911         switch (hmac_algo) {
912         case SCTP_AUTH_HMAC_ID_SHA1:
913                 return (SCTP_AUTH_DIGEST_LEN_SHA1);
914         case SCTP_AUTH_HMAC_ID_MD5:
915                 return (SCTP_AUTH_DIGEST_LEN_MD5);
916 #ifdef HAVE_SHA224
917         case SCTP_AUTH_HMAC_ID_SHA224:
918                 return (SCTP_AUTH_DIGEST_LEN_SHA224);
919 #endif
920 #ifdef HAVE_SHA2
921         case SCTP_AUTH_HMAC_ID_SHA256:
922                 return (SCTP_AUTH_DIGEST_LEN_SHA256);
923         case SCTP_AUTH_HMAC_ID_SHA384:
924                 return (SCTP_AUTH_DIGEST_LEN_SHA384);
925         case SCTP_AUTH_HMAC_ID_SHA512:
926                 return (SCTP_AUTH_DIGEST_LEN_SHA512);
927 #endif
928         default:
929                 /* unknown HMAC algorithm: can't do anything */
930                 return (0);
931         }                       /* end switch */
932 }
933
934 static inline int
935 sctp_get_hmac_block_len(uint16_t hmac_algo)
936 {
937         switch (hmac_algo) {
938                 case SCTP_AUTH_HMAC_ID_SHA1:
939                 case SCTP_AUTH_HMAC_ID_MD5:
940 #ifdef HAVE_SHA224
941                 case SCTP_AUTH_HMAC_ID_SHA224:
942 #endif
943                 return (64);
944 #ifdef HAVE_SHA2
945         case SCTP_AUTH_HMAC_ID_SHA256:
946                 return (64);
947         case SCTP_AUTH_HMAC_ID_SHA384:
948         case SCTP_AUTH_HMAC_ID_SHA512:
949                 return (128);
950 #endif
951         case SCTP_AUTH_HMAC_ID_RSVD:
952         default:
953                 /* unknown HMAC algorithm: can't do anything */
954                 return (0);
955         }                       /* end switch */
956 }
957
958 static void
959 sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
960 {
961         switch (hmac_algo) {
962                 case SCTP_AUTH_HMAC_ID_SHA1:
963                 SHA1_Init(&ctx->sha1);
964                 break;
965         case SCTP_AUTH_HMAC_ID_MD5:
966                 MD5_Init(&ctx->md5);
967                 break;
968 #ifdef HAVE_SHA224
969         case SCTP_AUTH_HMAC_ID_SHA224:
970                 break;
971 #endif
972 #ifdef HAVE_SHA2
973         case SCTP_AUTH_HMAC_ID_SHA256:
974                 SHA256_Init(&ctx->sha256);
975                 break;
976         case SCTP_AUTH_HMAC_ID_SHA384:
977                 SHA384_Init(&ctx->sha384);
978                 break;
979         case SCTP_AUTH_HMAC_ID_SHA512:
980                 SHA512_Init(&ctx->sha512);
981                 break;
982 #endif
983         case SCTP_AUTH_HMAC_ID_RSVD:
984         default:
985                 /* unknown HMAC algorithm: can't do anything */
986                 return;
987         }                       /* end switch */
988 }
989
990 static void
991 sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
992     uint8_t * text, uint32_t textlen)
993 {
994         switch (hmac_algo) {
995                 case SCTP_AUTH_HMAC_ID_SHA1:
996                 SHA1_Update(&ctx->sha1, text, textlen);
997                 break;
998         case SCTP_AUTH_HMAC_ID_MD5:
999                 MD5_Update(&ctx->md5, text, textlen);
1000                 break;
1001 #ifdef HAVE_SHA224
1002         case SCTP_AUTH_HMAC_ID_SHA224:
1003                 break;
1004 #endif
1005 #ifdef HAVE_SHA2
1006         case SCTP_AUTH_HMAC_ID_SHA256:
1007                 SHA256_Update(&ctx->sha256, text, textlen);
1008                 break;
1009         case SCTP_AUTH_HMAC_ID_SHA384:
1010                 SHA384_Update(&ctx->sha384, text, textlen);
1011                 break;
1012         case SCTP_AUTH_HMAC_ID_SHA512:
1013                 SHA512_Update(&ctx->sha512, text, textlen);
1014                 break;
1015 #endif
1016         case SCTP_AUTH_HMAC_ID_RSVD:
1017         default:
1018                 /* unknown HMAC algorithm: can't do anything */
1019                 return;
1020         }                       /* end switch */
1021 }
1022
1023 static void
1024 sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
1025     uint8_t * digest)
1026 {
1027         switch (hmac_algo) {
1028                 case SCTP_AUTH_HMAC_ID_SHA1:
1029                 SHA1_Final(digest, &ctx->sha1);
1030                 break;
1031         case SCTP_AUTH_HMAC_ID_MD5:
1032                 MD5_Final(digest, &ctx->md5);
1033                 break;
1034 #ifdef HAVE_SHA224
1035         case SCTP_AUTH_HMAC_ID_SHA224:
1036                 break;
1037 #endif
1038 #ifdef HAVE_SHA2
1039         case SCTP_AUTH_HMAC_ID_SHA256:
1040                 SHA256_Final(digest, &ctx->sha256);
1041                 break;
1042         case SCTP_AUTH_HMAC_ID_SHA384:
1043                 /* SHA384 is truncated SHA512 */
1044                 SHA384_Final(digest, &ctx->sha384);
1045                 break;
1046         case SCTP_AUTH_HMAC_ID_SHA512:
1047                 SHA512_Final(digest, &ctx->sha512);
1048                 break;
1049 #endif
1050         case SCTP_AUTH_HMAC_ID_RSVD:
1051         default:
1052                 /* unknown HMAC algorithm: can't do anything */
1053                 return;
1054         }                       /* end switch */
1055 }
1056
1057 /*-
1058  * Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
1059  *
1060  * Compute the HMAC digest using the desired hash key, text, and HMAC
1061  * algorithm.  Resulting digest is placed in 'digest' and digest length
1062  * is returned, if the HMAC was performed.
1063  *
1064  * WARNING: it is up to the caller to supply sufficient space to hold the
1065  * resultant digest.
1066  */
1067 uint32_t
1068 sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1069     uint8_t * text, uint32_t textlen, uint8_t * digest)
1070 {
1071         uint32_t digestlen;
1072         uint32_t blocklen;
1073         sctp_hash_context_t ctx;
1074         uint8_t ipad[128], opad[128];   /* keyed hash inner/outer pads */
1075         uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1076         uint32_t i;
1077
1078         /* sanity check the material and length */
1079         if ((key == NULL) || (keylen == 0) || (text == NULL) ||
1080             (textlen == 0) || (digest == NULL)) {
1081                 /* can't do HMAC with empty key or text or digest store */
1082                 return (0);
1083         }
1084         /* validate the hmac algo and get the digest length */
1085         digestlen = sctp_get_hmac_digest_len(hmac_algo);
1086         if (digestlen == 0)
1087                 return (0);
1088
1089         /* hash the key if it is longer than the hash block size */
1090         blocklen = sctp_get_hmac_block_len(hmac_algo);
1091         if (keylen > blocklen) {
1092                 sctp_hmac_init(hmac_algo, &ctx);
1093                 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1094                 sctp_hmac_final(hmac_algo, &ctx, temp);
1095                 /* set the hashed key as the key */
1096                 keylen = digestlen;
1097                 key = temp;
1098         }
1099         /* initialize the inner/outer pads with the key and "append" zeroes */
1100         bzero(ipad, blocklen);
1101         bzero(opad, blocklen);
1102         bcopy(key, ipad, keylen);
1103         bcopy(key, opad, keylen);
1104
1105         /* XOR the key with ipad and opad values */
1106         for (i = 0; i < blocklen; i++) {
1107                 ipad[i] ^= 0x36;
1108                 opad[i] ^= 0x5c;
1109         }
1110
1111         /* perform inner hash */
1112         sctp_hmac_init(hmac_algo, &ctx);
1113         sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1114         sctp_hmac_update(hmac_algo, &ctx, text, textlen);
1115         sctp_hmac_final(hmac_algo, &ctx, temp);
1116
1117         /* perform outer hash */
1118         sctp_hmac_init(hmac_algo, &ctx);
1119         sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1120         sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1121         sctp_hmac_final(hmac_algo, &ctx, digest);
1122
1123         return (digestlen);
1124 }
1125
1126 /* mbuf version */
1127 uint32_t
1128 sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1129     struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
1130 {
1131         uint32_t digestlen;
1132         uint32_t blocklen;
1133         sctp_hash_context_t ctx;
1134         uint8_t ipad[128], opad[128];   /* keyed hash inner/outer pads */
1135         uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1136         uint32_t i;
1137         struct mbuf *m_tmp;
1138
1139         /* sanity check the material and length */
1140         if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
1141                 /* can't do HMAC with empty key or text or digest store */
1142                 return (0);
1143         }
1144         /* validate the hmac algo and get the digest length */
1145         digestlen = sctp_get_hmac_digest_len(hmac_algo);
1146         if (digestlen == 0)
1147                 return (0);
1148
1149         /* hash the key if it is longer than the hash block size */
1150         blocklen = sctp_get_hmac_block_len(hmac_algo);
1151         if (keylen > blocklen) {
1152                 sctp_hmac_init(hmac_algo, &ctx);
1153                 sctp_hmac_update(hmac_algo, &ctx, key, keylen);
1154                 sctp_hmac_final(hmac_algo, &ctx, temp);
1155                 /* set the hashed key as the key */
1156                 keylen = digestlen;
1157                 key = temp;
1158         }
1159         /* initialize the inner/outer pads with the key and "append" zeroes */
1160         bzero(ipad, blocklen);
1161         bzero(opad, blocklen);
1162         bcopy(key, ipad, keylen);
1163         bcopy(key, opad, keylen);
1164
1165         /* XOR the key with ipad and opad values */
1166         for (i = 0; i < blocklen; i++) {
1167                 ipad[i] ^= 0x36;
1168                 opad[i] ^= 0x5c;
1169         }
1170
1171         /* perform inner hash */
1172         sctp_hmac_init(hmac_algo, &ctx);
1173         sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
1174         /* find the correct starting mbuf and offset (get start of text) */
1175         m_tmp = m;
1176         while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1177                 m_offset -= SCTP_BUF_LEN(m_tmp);
1178                 m_tmp = SCTP_BUF_NEXT(m_tmp);
1179         }
1180         /* now use the rest of the mbuf chain for the text */
1181         while (m_tmp != NULL) {
1182                 if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
1183                         sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1184                             SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
1185                 } else {
1186                         sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
1187                             SCTP_BUF_LEN(m_tmp) - m_offset);
1188                 }
1189
1190                 /* clear the offset since it's only for the first mbuf */
1191                 m_offset = 0;
1192                 m_tmp = SCTP_BUF_NEXT(m_tmp);
1193         }
1194         sctp_hmac_final(hmac_algo, &ctx, temp);
1195
1196         /* perform outer hash */
1197         sctp_hmac_init(hmac_algo, &ctx);
1198         sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
1199         sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
1200         sctp_hmac_final(hmac_algo, &ctx, digest);
1201
1202         return (digestlen);
1203 }
1204
1205 /*-
1206  * verify the HMAC digest using the desired hash key, text, and HMAC
1207  * algorithm.
1208  * Returns -1 on error, 0 on success.
1209  */
1210 int
1211 sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
1212     uint8_t * text, uint32_t textlen,
1213     uint8_t * digest, uint32_t digestlen)
1214 {
1215         uint32_t len;
1216         uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1217
1218         /* sanity check the material and length */
1219         if ((key == NULL) || (keylen == 0) ||
1220             (text == NULL) || (textlen == 0) || (digest == NULL)) {
1221                 /* can't do HMAC with empty key or text or digest */
1222                 return (-1);
1223         }
1224         len = sctp_get_hmac_digest_len(hmac_algo);
1225         if ((len == 0) || (digestlen != len))
1226                 return (-1);
1227
1228         /* compute the expected hash */
1229         if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
1230                 return (-1);
1231
1232         if (memcmp(digest, temp, digestlen) != 0)
1233                 return (-1);
1234         else
1235                 return (0);
1236 }
1237
1238
1239 /*
1240  * computes the requested HMAC using a key struct (which may be modified if
1241  * the keylen exceeds the HMAC block len).
1242  */
1243 uint32_t
1244 sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
1245     uint32_t textlen, uint8_t * digest)
1246 {
1247         uint32_t digestlen;
1248         uint32_t blocklen;
1249         sctp_hash_context_t ctx;
1250         uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1251
1252         /* sanity check */
1253         if ((key == NULL) || (text == NULL) || (textlen == 0) ||
1254             (digest == NULL)) {
1255                 /* can't do HMAC with empty key or text or digest store */
1256                 return (0);
1257         }
1258         /* validate the hmac algo and get the digest length */
1259         digestlen = sctp_get_hmac_digest_len(hmac_algo);
1260         if (digestlen == 0)
1261                 return (0);
1262
1263         /* hash the key if it is longer than the hash block size */
1264         blocklen = sctp_get_hmac_block_len(hmac_algo);
1265         if (key->keylen > blocklen) {
1266                 sctp_hmac_init(hmac_algo, &ctx);
1267                 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1268                 sctp_hmac_final(hmac_algo, &ctx, temp);
1269                 /* save the hashed key as the new key */
1270                 key->keylen = digestlen;
1271                 bcopy(temp, key->key, key->keylen);
1272         }
1273         return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
1274             digest));
1275 }
1276
1277 /* mbuf version */
1278 uint32_t
1279 sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
1280     uint32_t m_offset, uint8_t * digest)
1281 {
1282         uint32_t digestlen;
1283         uint32_t blocklen;
1284         sctp_hash_context_t ctx;
1285         uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
1286
1287         /* sanity check */
1288         if ((key == NULL) || (m == NULL) || (digest == NULL)) {
1289                 /* can't do HMAC with empty key or text or digest store */
1290                 return (0);
1291         }
1292         /* validate the hmac algo and get the digest length */
1293         digestlen = sctp_get_hmac_digest_len(hmac_algo);
1294         if (digestlen == 0)
1295                 return (0);
1296
1297         /* hash the key if it is longer than the hash block size */
1298         blocklen = sctp_get_hmac_block_len(hmac_algo);
1299         if (key->keylen > blocklen) {
1300                 sctp_hmac_init(hmac_algo, &ctx);
1301                 sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
1302                 sctp_hmac_final(hmac_algo, &ctx, temp);
1303                 /* save the hashed key as the new key */
1304                 key->keylen = digestlen;
1305                 bcopy(temp, key->key, key->keylen);
1306         }
1307         return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
1308 }
1309
1310 int
1311 sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
1312 {
1313         int i;
1314
1315         if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
1316                 return (0);
1317
1318         for (i = 0; i < list->num_algo; i++)
1319                 if (list->hmac[i] == id)
1320                         return (1);
1321
1322         /* not in the list */
1323         return (0);
1324 }
1325
1326
1327 /*-
1328  * clear any cached key(s) if they match the given key id on an association.
1329  * the cached key(s) will be recomputed and re-cached at next use.
1330  * ASSUMES TCB_LOCK is already held
1331  */
1332 void
1333 sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
1334 {
1335         if (stcb == NULL)
1336                 return;
1337
1338         if (keyid == stcb->asoc.authinfo.assoc_keyid) {
1339                 sctp_free_key(stcb->asoc.authinfo.assoc_key);
1340                 stcb->asoc.authinfo.assoc_key = NULL;
1341         }
1342         if (keyid == stcb->asoc.authinfo.recv_keyid) {
1343                 sctp_free_key(stcb->asoc.authinfo.recv_key);
1344                 stcb->asoc.authinfo.recv_key = NULL;
1345         }
1346 }
1347
1348 /*-
1349  * clear any cached key(s) if they match the given key id for all assocs on
1350  * an endpoint.
1351  * ASSUMES INP_WLOCK is already held
1352  */
1353 void
1354 sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
1355 {
1356         struct sctp_tcb *stcb;
1357
1358         if (inp == NULL)
1359                 return;
1360
1361         /* clear the cached keys on all assocs on this instance */
1362         LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
1363                 SCTP_TCB_LOCK(stcb);
1364                 sctp_clear_cachedkeys(stcb, keyid);
1365                 SCTP_TCB_UNLOCK(stcb);
1366         }
1367 }
1368
1369 /*-
1370  * delete a shared key from an association
1371  * ASSUMES TCB_LOCK is already held
1372  */
1373 int
1374 sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1375 {
1376         sctp_sharedkey_t *skey;
1377
1378         if (stcb == NULL)
1379                 return (-1);
1380
1381         /* is the keyid the assoc active sending key */
1382         if (keyid == stcb->asoc.authinfo.active_keyid)
1383                 return (-1);
1384
1385         /* does the key exist? */
1386         skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1387         if (skey == NULL)
1388                 return (-1);
1389
1390         /* are there other refcount holders on the key? */
1391         if (skey->refcount > 1)
1392                 return (-1);
1393
1394         /* remove it */
1395         LIST_REMOVE(skey, next);
1396         sctp_free_sharedkey(skey);      /* frees skey->key as well */
1397
1398         /* clear any cached keys */
1399         sctp_clear_cachedkeys(stcb, keyid);
1400         return (0);
1401 }
1402
1403 /*-
1404  * deletes a shared key from the endpoint
1405  * ASSUMES INP_WLOCK is already held
1406  */
1407 int
1408 sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1409 {
1410         sctp_sharedkey_t *skey;
1411
1412         if (inp == NULL)
1413                 return (-1);
1414
1415         /* is the keyid the active sending key on the endpoint */
1416         if (keyid == inp->sctp_ep.default_keyid)
1417                 return (-1);
1418
1419         /* does the key exist? */
1420         skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1421         if (skey == NULL)
1422                 return (-1);
1423
1424         /* endpoint keys are not refcounted */
1425
1426         /* remove it */
1427         LIST_REMOVE(skey, next);
1428         sctp_free_sharedkey(skey);      /* frees skey->key as well */
1429
1430         /* clear any cached keys */
1431         sctp_clear_cachedkeys_ep(inp, keyid);
1432         return (0);
1433 }
1434
1435 /*-
1436  * set the active key on an association
1437  * ASSUMES TCB_LOCK is already held
1438  */
1439 int
1440 sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
1441 {
1442         sctp_sharedkey_t *skey = NULL;
1443
1444         /* find the key on the assoc */
1445         skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1446         if (skey == NULL) {
1447                 /* that key doesn't exist */
1448                 return (-1);
1449         }
1450         if ((skey->deactivated) && (skey->refcount > 1)) {
1451                 /* can't reactivate a deactivated key with other refcounts */
1452                 return (-1);
1453         }
1454         /* set the (new) active key */
1455         stcb->asoc.authinfo.active_keyid = keyid;
1456         /* reset the deactivated flag */
1457         skey->deactivated = 0;
1458
1459         return (0);
1460 }
1461
1462 /*-
1463  * set the active key on an endpoint
1464  * ASSUMES INP_WLOCK is already held
1465  */
1466 int
1467 sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1468 {
1469         sctp_sharedkey_t *skey;
1470
1471         /* find the key */
1472         skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1473         if (skey == NULL) {
1474                 /* that key doesn't exist */
1475                 return (-1);
1476         }
1477         inp->sctp_ep.default_keyid = keyid;
1478         return (0);
1479 }
1480
1481 /*-
1482  * deactivates a shared key from the association
1483  * ASSUMES INP_WLOCK is already held
1484  */
1485 int
1486 sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
1487 {
1488         sctp_sharedkey_t *skey;
1489
1490         if (stcb == NULL)
1491                 return (-1);
1492
1493         /* is the keyid the assoc active sending key */
1494         if (keyid == stcb->asoc.authinfo.active_keyid)
1495                 return (-1);
1496
1497         /* does the key exist? */
1498         skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1499         if (skey == NULL)
1500                 return (-1);
1501
1502         /* are there other refcount holders on the key? */
1503         if (skey->refcount == 1) {
1504                 /* no other users, send a notification for this key */
1505                 sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
1506                     SCTP_SO_LOCKED);
1507         }
1508         /* mark the key as deactivated */
1509         skey->deactivated = 1;
1510
1511         return (0);
1512 }
1513
1514 /*-
1515  * deactivates a shared key from the endpoint
1516  * ASSUMES INP_WLOCK is already held
1517  */
1518 int
1519 sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
1520 {
1521         sctp_sharedkey_t *skey;
1522
1523         if (inp == NULL)
1524                 return (-1);
1525
1526         /* is the keyid the active sending key on the endpoint */
1527         if (keyid == inp->sctp_ep.default_keyid)
1528                 return (-1);
1529
1530         /* does the key exist? */
1531         skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
1532         if (skey == NULL)
1533                 return (-1);
1534
1535         /* endpoint keys are not refcounted */
1536
1537         /* remove it */
1538         LIST_REMOVE(skey, next);
1539         sctp_free_sharedkey(skey);      /* frees skey->key as well */
1540
1541         return (0);
1542 }
1543
1544 /*
1545  * get local authentication parameters from cookie (from INIT-ACK)
1546  */
1547 void
1548 sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
1549     uint32_t offset, uint32_t length)
1550 {
1551         struct sctp_paramhdr *phdr, tmp_param;
1552         uint16_t plen, ptype;
1553         uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
1554         struct sctp_auth_random *p_random = NULL;
1555         uint16_t random_len = 0;
1556         uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
1557         struct sctp_auth_hmac_algo *hmacs = NULL;
1558         uint16_t hmacs_len = 0;
1559         uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
1560         struct sctp_auth_chunk_list *chunks = NULL;
1561         uint16_t num_chunks = 0;
1562         sctp_key_t *new_key;
1563         uint32_t keylen;
1564
1565         /* convert to upper bound */
1566         length += offset;
1567
1568         phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
1569             sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
1570         while (phdr != NULL) {
1571                 ptype = ntohs(phdr->param_type);
1572                 plen = ntohs(phdr->param_length);
1573
1574                 if ((plen == 0) || (offset + plen > length))
1575                         break;
1576
1577                 if (ptype == SCTP_RANDOM) {
1578                         if (plen > sizeof(random_store))
1579                                 break;
1580                         phdr = sctp_get_next_param(m, offset,
1581                             (struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
1582                         if (phdr == NULL)
1583                                 return;
1584                         /* save the random and length for the key */
1585                         p_random = (struct sctp_auth_random *)phdr;
1586                         random_len = plen - sizeof(*p_random);
1587                 } else if (ptype == SCTP_HMAC_LIST) {
1588                         int num_hmacs;
1589                         int i;
1590
1591                         if (plen > sizeof(hmacs_store))
1592                                 break;
1593                         phdr = sctp_get_next_param(m, offset,
1594                             (struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
1595                         if (phdr == NULL)
1596                                 return;
1597                         /* save the hmacs list and num for the key */
1598                         hmacs = (struct sctp_auth_hmac_algo *)phdr;
1599                         hmacs_len = plen - sizeof(*hmacs);
1600                         num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
1601                         if (stcb->asoc.local_hmacs != NULL)
1602                                 sctp_free_hmaclist(stcb->asoc.local_hmacs);
1603                         stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
1604                         if (stcb->asoc.local_hmacs != NULL) {
1605                                 for (i = 0; i < num_hmacs; i++) {
1606                                         (void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
1607                                             ntohs(hmacs->hmac_ids[i]));
1608                                 }
1609                         }
1610                 } else if (ptype == SCTP_CHUNK_LIST) {
1611                         int i;
1612
1613                         if (plen > sizeof(chunks_store))
1614                                 break;
1615                         phdr = sctp_get_next_param(m, offset,
1616                             (struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
1617                         if (phdr == NULL)
1618                                 return;
1619                         chunks = (struct sctp_auth_chunk_list *)phdr;
1620                         num_chunks = plen - sizeof(*chunks);
1621                         /* save chunks list and num for the key */
1622                         if (stcb->asoc.local_auth_chunks != NULL)
1623                                 sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
1624                         else
1625                                 stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
1626                         for (i = 0; i < num_chunks; i++) {
1627                                 (void)sctp_auth_add_chunk(chunks->chunk_types[i],
1628                                     stcb->asoc.local_auth_chunks);
1629                         }
1630                 }
1631                 /* get next parameter */
1632                 offset += SCTP_SIZE32(plen);
1633                 if (offset + sizeof(struct sctp_paramhdr) > length)
1634                         break;
1635                 phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
1636                     (uint8_t *) & tmp_param);
1637         }
1638         /* concatenate the full random key */
1639 #ifdef SCTP_AUTH_DRAFT_04
1640         keylen = random_len;
1641         new_key = sctp_alloc_key(keylen);
1642         if (new_key != NULL) {
1643                 /* copy in the RANDOM */
1644                 if (p_random != NULL)
1645                         bcopy(p_random->random_data, new_key->key, random_len);
1646         }
1647 #else
1648         keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
1649         if (chunks != NULL) {
1650                 keylen += sizeof(*chunks) + num_chunks;
1651         }
1652         new_key = sctp_alloc_key(keylen);
1653         if (new_key != NULL) {
1654                 /* copy in the RANDOM */
1655                 if (p_random != NULL) {
1656                         keylen = sizeof(*p_random) + random_len;
1657                         bcopy(p_random, new_key->key, keylen);
1658                 }
1659                 /* append in the AUTH chunks */
1660                 if (chunks != NULL) {
1661                         bcopy(chunks, new_key->key + keylen,
1662                             sizeof(*chunks) + num_chunks);
1663                         keylen += sizeof(*chunks) + num_chunks;
1664                 }
1665                 /* append in the HMACs */
1666                 if (hmacs != NULL) {
1667                         bcopy(hmacs, new_key->key + keylen,
1668                             sizeof(*hmacs) + hmacs_len);
1669                 }
1670         }
1671 #endif
1672         if (stcb->asoc.authinfo.random != NULL)
1673                 sctp_free_key(stcb->asoc.authinfo.random);
1674         stcb->asoc.authinfo.random = new_key;
1675         stcb->asoc.authinfo.random_len = random_len;
1676 #ifdef SCTP_AUTH_DRAFT_04
1677         /* don't include the chunks and hmacs for draft -04 */
1678         stcb->asoc.authinfo.random->keylen = random_len;
1679 #endif
1680         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
1681         sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
1682
1683         /* negotiate what HMAC to use for the peer */
1684         stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
1685             stcb->asoc.local_hmacs);
1686
1687         /* copy defaults from the endpoint */
1688         /* FIX ME: put in cookie? */
1689         stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
1690         /* copy out the shared key list (by reference) from the endpoint */
1691         (void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
1692             &stcb->asoc.shared_keys);
1693 }
1694
1695 /*
1696  * compute and fill in the HMAC digest for a packet
1697  */
1698 void
1699 sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
1700     struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
1701 {
1702         uint32_t digestlen;
1703         sctp_sharedkey_t *skey;
1704         sctp_key_t *key;
1705
1706         if ((stcb == NULL) || (auth == NULL))
1707                 return;
1708
1709         /* zero the digest + chunk padding */
1710         digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
1711         bzero(auth->hmac, SCTP_SIZE32(digestlen));
1712
1713         /* is the desired key cached? */
1714         if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
1715             (stcb->asoc.authinfo.assoc_key == NULL)) {
1716                 if (stcb->asoc.authinfo.assoc_key != NULL) {
1717                         /* free the old cached key */
1718                         sctp_free_key(stcb->asoc.authinfo.assoc_key);
1719                 }
1720                 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
1721                 /* the only way skey is NULL is if null key id 0 is used */
1722                 if (skey != NULL)
1723                         key = skey->key;
1724                 else
1725                         key = NULL;
1726                 /* compute a new assoc key and cache it */
1727                 stcb->asoc.authinfo.assoc_key =
1728                     sctp_compute_hashkey(stcb->asoc.authinfo.random,
1729                     stcb->asoc.authinfo.peer_random, key);
1730                 stcb->asoc.authinfo.assoc_keyid = keyid;
1731                 SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
1732                     stcb->asoc.authinfo.assoc_keyid);
1733 #ifdef SCTP_DEBUG
1734                 if (SCTP_AUTH_DEBUG)
1735                         sctp_print_key(stcb->asoc.authinfo.assoc_key,
1736                             "Assoc Key");
1737 #endif
1738         }
1739         /* set in the active key id */
1740         auth->shared_key_id = htons(keyid);
1741
1742         /* compute and fill in the digest */
1743         (void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
1744             m, auth_offset, auth->hmac);
1745 }
1746
1747
1748 static void
1749 sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
1750 {
1751         struct mbuf *m_tmp;
1752         uint8_t *data;
1753
1754         /* sanity check */
1755         if (m == NULL)
1756                 return;
1757
1758         /* find the correct starting mbuf and offset (get start position) */
1759         m_tmp = m;
1760         while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
1761                 m_offset -= SCTP_BUF_LEN(m_tmp);
1762                 m_tmp = SCTP_BUF_NEXT(m_tmp);
1763         }
1764         /* now use the rest of the mbuf chain */
1765         while ((m_tmp != NULL) && (size > 0)) {
1766                 data = mtod(m_tmp, uint8_t *) + m_offset;
1767                 if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
1768                         bzero(data, SCTP_BUF_LEN(m_tmp));
1769                         size -= SCTP_BUF_LEN(m_tmp);
1770                 } else {
1771                         bzero(data, size);
1772                         size = 0;
1773                 }
1774                 /* clear the offset since it's only for the first mbuf */
1775                 m_offset = 0;
1776                 m_tmp = SCTP_BUF_NEXT(m_tmp);
1777         }
1778 }
1779
1780 /*-
1781  * process the incoming Authentication chunk
1782  * return codes:
1783  *   -1 on any authentication error
1784  *    0 on authentication verification
1785  */
1786 int
1787 sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
1788     struct mbuf *m, uint32_t offset)
1789 {
1790         uint16_t chunklen;
1791         uint16_t shared_key_id;
1792         uint16_t hmac_id;
1793         sctp_sharedkey_t *skey;
1794         uint32_t digestlen;
1795         uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
1796         uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
1797
1798         /* auth is checked for NULL by caller */
1799         chunklen = ntohs(auth->ch.chunk_length);
1800         if (chunklen < sizeof(*auth)) {
1801                 SCTP_STAT_INCR(sctps_recvauthfailed);
1802                 return (-1);
1803         }
1804         SCTP_STAT_INCR(sctps_recvauth);
1805
1806         /* get the auth params */
1807         shared_key_id = ntohs(auth->shared_key_id);
1808         hmac_id = ntohs(auth->hmac_id);
1809         SCTPDBG(SCTP_DEBUG_AUTH1,
1810             "SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
1811             shared_key_id, hmac_id);
1812
1813         /* is the indicated HMAC supported? */
1814         if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
1815                 struct mbuf *m_err;
1816                 struct sctp_auth_invalid_hmac *err;
1817
1818                 SCTP_STAT_INCR(sctps_recvivalhmacid);
1819                 SCTPDBG(SCTP_DEBUG_AUTH1,
1820                     "SCTP Auth: unsupported HMAC id %u\n",
1821                     hmac_id);
1822                 /*
1823                  * report this in an Error Chunk: Unsupported HMAC
1824                  * Identifier
1825                  */
1826                 m_err = sctp_get_mbuf_for_msg(sizeof(*err), 0, M_DONTWAIT,
1827                     1, MT_HEADER);
1828                 if (m_err != NULL) {
1829                         /* pre-reserve some space */
1830                         SCTP_BUF_RESV_UF(m_err, sizeof(struct sctp_chunkhdr));
1831                         /* fill in the error */
1832                         err = mtod(m_err, struct sctp_auth_invalid_hmac *);
1833                         bzero(err, sizeof(*err));
1834                         err->ph.param_type = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
1835                         err->ph.param_length = htons(sizeof(*err));
1836                         err->hmac_id = ntohs(hmac_id);
1837                         SCTP_BUF_LEN(m_err) = sizeof(*err);
1838                         /* queue it */
1839                         sctp_queue_op_err(stcb, m_err);
1840                 }
1841                 return (-1);
1842         }
1843         /* get the indicated shared key, if available */
1844         if ((stcb->asoc.authinfo.recv_key == NULL) ||
1845             (stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
1846                 /* find the shared key on the assoc first */
1847                 skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
1848                     shared_key_id);
1849                 /* if the shared key isn't found, discard the chunk */
1850                 if (skey == NULL) {
1851                         SCTP_STAT_INCR(sctps_recvivalkeyid);
1852                         SCTPDBG(SCTP_DEBUG_AUTH1,
1853                             "SCTP Auth: unknown key id %u\n",
1854                             shared_key_id);
1855                         return (-1);
1856                 }
1857                 /* generate a notification if this is a new key id */
1858                 if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
1859                         /*
1860                          * sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
1861                          * shared_key_id, (void
1862                          * *)stcb->asoc.authinfo.recv_keyid);
1863                          */
1864                         sctp_notify_authentication(stcb, SCTP_AUTH_NEWKEY,
1865                             shared_key_id, stcb->asoc.authinfo.recv_keyid,
1866                             SCTP_SO_NOT_LOCKED);
1867                 /* compute a new recv assoc key and cache it */
1868                 if (stcb->asoc.authinfo.recv_key != NULL)
1869                         sctp_free_key(stcb->asoc.authinfo.recv_key);
1870                 stcb->asoc.authinfo.recv_key =
1871                     sctp_compute_hashkey(stcb->asoc.authinfo.random,
1872                     stcb->asoc.authinfo.peer_random, skey->key);
1873                 stcb->asoc.authinfo.recv_keyid = shared_key_id;
1874 #ifdef SCTP_DEBUG
1875                 if (SCTP_AUTH_DEBUG)
1876                         sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
1877 #endif
1878         }
1879         /* validate the digest length */
1880         digestlen = sctp_get_hmac_digest_len(hmac_id);
1881         if (chunklen < (sizeof(*auth) + digestlen)) {
1882                 /* invalid digest length */
1883                 SCTP_STAT_INCR(sctps_recvauthfailed);
1884                 SCTPDBG(SCTP_DEBUG_AUTH1,
1885                     "SCTP Auth: chunk too short for HMAC\n");
1886                 return (-1);
1887         }
1888         /* save a copy of the digest, zero the pseudo header, and validate */
1889         bcopy(auth->hmac, digest, digestlen);
1890         sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
1891         (void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
1892             m, offset, computed_digest);
1893
1894         /* compare the computed digest with the one in the AUTH chunk */
1895         if (memcmp(digest, computed_digest, digestlen) != 0) {
1896                 SCTP_STAT_INCR(sctps_recvauthfailed);
1897                 SCTPDBG(SCTP_DEBUG_AUTH1,
1898                     "SCTP Auth: HMAC digest check failed\n");
1899                 return (-1);
1900         }
1901         return (0);
1902 }
1903
1904 /*
1905  * Generate NOTIFICATION
1906  */
1907 void
1908 sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
1909     uint16_t keyid, uint16_t alt_keyid, int so_locked
1910 #if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
1911     SCTP_UNUSED
1912 #endif
1913 )
1914 {
1915         struct mbuf *m_notify;
1916         struct sctp_authkey_event *auth;
1917         struct sctp_queued_to_read *control;
1918
1919         if ((stcb == NULL) ||
1920             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
1921             (stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
1922             (stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
1923             ) {
1924                 /* If the socket is gone we are out of here */
1925                 return;
1926         }
1927         if (sctp_is_feature_off(stcb->sctp_ep, SCTP_PCB_FLAGS_AUTHEVNT))
1928                 /* event not enabled */
1929                 return;
1930
1931         m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
1932             0, M_DONTWAIT, 1, MT_HEADER);
1933         if (m_notify == NULL)
1934                 /* no space left */
1935                 return;
1936
1937         SCTP_BUF_LEN(m_notify) = 0;
1938         auth = mtod(m_notify, struct sctp_authkey_event *);
1939         auth->auth_type = SCTP_AUTHENTICATION_EVENT;
1940         auth->auth_flags = 0;
1941         auth->auth_length = sizeof(*auth);
1942         auth->auth_keynumber = keyid;
1943         auth->auth_altkeynumber = alt_keyid;
1944         auth->auth_indication = indication;
1945         auth->auth_assoc_id = sctp_get_associd(stcb);
1946
1947         SCTP_BUF_LEN(m_notify) = sizeof(*auth);
1948         SCTP_BUF_NEXT(m_notify) = NULL;
1949
1950         /* append to socket */
1951         control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
1952             0, 0, 0, 0, 0, 0, m_notify);
1953         if (control == NULL) {
1954                 /* no memory */
1955                 sctp_m_freem(m_notify);
1956                 return;
1957         }
1958         control->spec_flags = M_NOTIFICATION;
1959         control->length = SCTP_BUF_LEN(m_notify);
1960         /* not that we need this */
1961         control->tail_mbuf = m_notify;
1962         sctp_add_to_readq(stcb->sctp_ep, stcb, control,
1963             &stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
1964 }
1965
1966
1967 /*-
1968  * validates the AUTHentication related parameters in an INIT/INIT-ACK
1969  * Note: currently only used for INIT as INIT-ACK is handled inline
1970  * with sctp_load_addresses_from_init()
1971  */
1972 int
1973 sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
1974 {
1975         struct sctp_paramhdr *phdr, parm_buf;
1976         uint16_t ptype, plen;
1977         int peer_supports_asconf = 0;
1978         int peer_supports_auth = 0;
1979         int got_random = 0, got_hmacs = 0, got_chklist = 0;
1980         uint8_t saw_asconf = 0;
1981         uint8_t saw_asconf_ack = 0;
1982
1983         /* go through each of the params. */
1984         phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
1985         while (phdr) {
1986                 ptype = ntohs(phdr->param_type);
1987                 plen = ntohs(phdr->param_length);
1988
1989                 if (offset + plen > limit) {
1990                         break;
1991                 }
1992                 if (plen < sizeof(struct sctp_paramhdr)) {
1993                         break;
1994                 }
1995                 if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
1996                         /* A supported extension chunk */
1997                         struct sctp_supported_chunk_types_param *pr_supported;
1998                         uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
1999                         int num_ent, i;
2000
2001                         phdr = sctp_get_next_param(m, offset,
2002                             (struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
2003                         if (phdr == NULL) {
2004                                 return (-1);
2005                         }
2006                         pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
2007                         num_ent = plen - sizeof(struct sctp_paramhdr);
2008                         for (i = 0; i < num_ent; i++) {
2009                                 switch (pr_supported->chunk_types[i]) {
2010                                 case SCTP_ASCONF:
2011                                 case SCTP_ASCONF_ACK:
2012                                         peer_supports_asconf = 1;
2013                                         break;
2014                                 case SCTP_AUTHENTICATION:
2015                                         peer_supports_auth = 1;
2016                                         break;
2017                                 default:
2018                                         /* one we don't care about */
2019                                         break;
2020                                 }
2021                         }
2022                 } else if (ptype == SCTP_RANDOM) {
2023                         got_random = 1;
2024                         /* enforce the random length */
2025                         if (plen != (sizeof(struct sctp_auth_random) +
2026                             SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
2027                                 SCTPDBG(SCTP_DEBUG_AUTH1,
2028                                     "SCTP: invalid RANDOM len\n");
2029                                 return (-1);
2030                         }
2031                 } else if (ptype == SCTP_HMAC_LIST) {
2032                         uint8_t store[SCTP_PARAM_BUFFER_SIZE];
2033                         struct sctp_auth_hmac_algo *hmacs;
2034                         int num_hmacs;
2035
2036                         if (plen > sizeof(store))
2037                                 break;
2038                         phdr = sctp_get_next_param(m, offset,
2039                             (struct sctp_paramhdr *)store, min(plen, sizeof(store)));
2040                         if (phdr == NULL)
2041                                 return (-1);
2042                         hmacs = (struct sctp_auth_hmac_algo *)phdr;
2043                         num_hmacs = (plen - sizeof(*hmacs)) /
2044                             sizeof(hmacs->hmac_ids[0]);
2045                         /* validate the hmac list */
2046                         if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
2047                                 SCTPDBG(SCTP_DEBUG_AUTH1,
2048                                     "SCTP: invalid HMAC param\n");
2049                                 return (-1);
2050                         }
2051                         got_hmacs = 1;
2052                 } else if (ptype == SCTP_CHUNK_LIST) {
2053                         int i, num_chunks;
2054                         uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
2055
2056                         /* did the peer send a non-empty chunk list? */
2057                         struct sctp_auth_chunk_list *chunks = NULL;
2058
2059                         phdr = sctp_get_next_param(m, offset,
2060                             (struct sctp_paramhdr *)chunks_store,
2061                             min(plen, sizeof(chunks_store)));
2062                         if (phdr == NULL)
2063                                 return (-1);
2064
2065                         /*-
2066                          * Flip through the list and mark that the
2067                          * peer supports asconf/asconf_ack.
2068                          */
2069                         chunks = (struct sctp_auth_chunk_list *)phdr;
2070                         num_chunks = plen - sizeof(*chunks);
2071                         for (i = 0; i < num_chunks; i++) {
2072                                 /* record asconf/asconf-ack if listed */
2073                                 if (chunks->chunk_types[i] == SCTP_ASCONF)
2074                                         saw_asconf = 1;
2075                                 if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
2076                                         saw_asconf_ack = 1;
2077
2078                         }
2079                         if (num_chunks)
2080                                 got_chklist = 1;
2081                 }
2082                 offset += SCTP_SIZE32(plen);
2083                 if (offset >= limit) {
2084                         break;
2085                 }
2086                 phdr = sctp_get_next_param(m, offset, &parm_buf,
2087                     sizeof(parm_buf));
2088         }
2089         /* validate authentication required parameters */
2090         if (got_random && got_hmacs) {
2091                 peer_supports_auth = 1;
2092         } else {
2093                 peer_supports_auth = 0;
2094         }
2095         if (!peer_supports_auth && got_chklist) {
2096                 SCTPDBG(SCTP_DEBUG_AUTH1,
2097                     "SCTP: peer sent chunk list w/o AUTH\n");
2098                 return (-1);
2099         }
2100         if (!SCTP_BASE_SYSCTL(sctp_asconf_auth_nochk) && peer_supports_asconf &&
2101             !peer_supports_auth) {
2102                 SCTPDBG(SCTP_DEBUG_AUTH1,
2103                     "SCTP: peer supports ASCONF but not AUTH\n");
2104                 return (-1);
2105         } else if ((peer_supports_asconf) && (peer_supports_auth) &&
2106             ((saw_asconf == 0) || (saw_asconf_ack == 0))) {
2107                 return (-2);
2108         }
2109         return (0);
2110 }
2111
2112 void
2113 sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
2114 {
2115         uint16_t chunks_len = 0;
2116         uint16_t hmacs_len = 0;
2117         uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
2118         sctp_key_t *new_key;
2119         uint16_t keylen;
2120
2121         /* initialize hmac list from endpoint */
2122         stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
2123         if (stcb->asoc.local_hmacs != NULL) {
2124                 hmacs_len = stcb->asoc.local_hmacs->num_algo *
2125                     sizeof(stcb->asoc.local_hmacs->hmac[0]);
2126         }
2127         /* initialize auth chunks list from endpoint */
2128         stcb->asoc.local_auth_chunks =
2129             sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
2130         if (stcb->asoc.local_auth_chunks != NULL) {
2131                 int i;
2132
2133                 for (i = 0; i < 256; i++) {
2134                         if (stcb->asoc.local_auth_chunks->chunks[i])
2135                                 chunks_len++;
2136                 }
2137         }
2138         /* copy defaults from the endpoint */
2139         stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
2140
2141         /* copy out the shared key list (by reference) from the endpoint */
2142         (void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
2143             &stcb->asoc.shared_keys);
2144
2145         /* now set the concatenated key (random + chunks + hmacs) */
2146 #ifdef SCTP_AUTH_DRAFT_04
2147         /* don't include the chunks and hmacs for draft -04 */
2148         keylen = random_len;
2149         new_key = sctp_generate_random_key(keylen);
2150 #else
2151         /* key includes parameter headers */
2152         keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
2153             hmacs_len;
2154         new_key = sctp_alloc_key(keylen);
2155         if (new_key != NULL) {
2156                 struct sctp_paramhdr *ph;
2157                 int plen;
2158
2159                 /* generate and copy in the RANDOM */
2160                 ph = (struct sctp_paramhdr *)new_key->key;
2161                 ph->param_type = htons(SCTP_RANDOM);
2162                 plen = sizeof(*ph) + random_len;
2163                 ph->param_length = htons(plen);
2164                 SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
2165                 keylen = plen;
2166
2167                 /* append in the AUTH chunks */
2168                 /* NOTE: currently we always have chunks to list */
2169                 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2170                 ph->param_type = htons(SCTP_CHUNK_LIST);
2171                 plen = sizeof(*ph) + chunks_len;
2172                 ph->param_length = htons(plen);
2173                 keylen += sizeof(*ph);
2174                 if (stcb->asoc.local_auth_chunks) {
2175                         int i;
2176
2177                         for (i = 0; i < 256; i++) {
2178                                 if (stcb->asoc.local_auth_chunks->chunks[i])
2179                                         new_key->key[keylen++] = i;
2180                         }
2181                 }
2182                 /* append in the HMACs */
2183                 ph = (struct sctp_paramhdr *)(new_key->key + keylen);
2184                 ph->param_type = htons(SCTP_HMAC_LIST);
2185                 plen = sizeof(*ph) + hmacs_len;
2186                 ph->param_length = htons(plen);
2187                 keylen += sizeof(*ph);
2188                 (void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
2189                     new_key->key + keylen);
2190         }
2191 #endif
2192         if (stcb->asoc.authinfo.random != NULL)
2193                 sctp_free_key(stcb->asoc.authinfo.random);
2194         stcb->asoc.authinfo.random = new_key;
2195         stcb->asoc.authinfo.random_len = random_len;
2196 }
2197
2198
2199 #ifdef SCTP_HMAC_TEST
2200 /*
2201  * HMAC and key concatenation tests
2202  */
2203 static void
2204 sctp_print_digest(uint8_t * digest, uint32_t digestlen, const char *str)
2205 {
2206         uint32_t i;
2207
2208         printf("\n%s: 0x", str);
2209         if (digest == NULL)
2210                 return;
2211
2212         for (i = 0; i < digestlen; i++)
2213                 printf("%02x", digest[i]);
2214 }
2215
2216 static int
2217 sctp_test_hmac(const char *str, uint16_t hmac_id, uint8_t * key,
2218     uint32_t keylen, uint8_t * text, uint32_t textlen,
2219     uint8_t * digest, uint32_t digestlen)
2220 {
2221         uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
2222
2223         printf("\n%s:", str);
2224         sctp_hmac(hmac_id, key, keylen, text, textlen, computed_digest);
2225         sctp_print_digest(digest, digestlen, "Expected digest");
2226         sctp_print_digest(computed_digest, digestlen, "Computed digest");
2227         if (memcmp(digest, computed_digest, digestlen) != 0) {
2228                 printf("\nFAILED");
2229                 return (-1);
2230         } else {
2231                 printf("\nPASSED");
2232                 return (0);
2233         }
2234 }
2235
2236
2237 /*
2238  * RFC 2202: HMAC-SHA1 test cases
2239  */
2240 void
2241 sctp_test_hmac_sha1(void)
2242 {
2243         uint8_t *digest;
2244         uint8_t key[128];
2245         uint32_t keylen;
2246         uint8_t text[128];
2247         uint32_t textlen;
2248         uint32_t digestlen = 20;
2249         int failed = 0;
2250
2251         /*-
2252          * test_case =     1
2253          * key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2254          * key_len =       20
2255          * data =          "Hi There"
2256          * data_len =      8
2257          * digest =        0xb617318655057264e28bc0b6fb378c8ef146be00
2258          */
2259         keylen = 20;
2260         memset(key, 0x0b, keylen);
2261         textlen = 8;
2262         strcpy(text, "Hi There");
2263         digest = "\xb6\x17\x31\x86\x55\x05\x72\x64\xe2\x8b\xc0\xb6\xfb\x37\x8c\x8e\xf1\x46\xbe\x00";
2264         if (sctp_test_hmac("SHA1 test case 1", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2265             text, textlen, digest, digestlen) < 0)
2266                 failed++;
2267
2268         /*-
2269          * test_case =     2
2270          * key =           "Jefe"
2271          * key_len =       4
2272          * data =          "what do ya want for nothing?"
2273          * data_len =      28
2274          * digest =        0xeffcdf6ae5eb2fa2d27416d5f184df9c259a7c79
2275          */
2276         keylen = 4;
2277         strcpy(key, "Jefe");
2278         textlen = 28;
2279         strcpy(text, "what do ya want for nothing?");
2280         digest = "\xef\xfc\xdf\x6a\xe5\xeb\x2f\xa2\xd2\x74\x16\xd5\xf1\x84\xdf\x9c\x25\x9a\x7c\x79";
2281         if (sctp_test_hmac("SHA1 test case 2", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2282             text, textlen, digest, digestlen) < 0)
2283                 failed++;
2284
2285         /*-
2286          * test_case =     3
2287          * key =           0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2288          * key_len =       20
2289          * data =          0xdd repeated 50 times
2290          * data_len =      50
2291          * digest =        0x125d7342b9ac11cd91a39af48aa17b4f63f175d3
2292          */
2293         keylen = 20;
2294         memset(key, 0xaa, keylen);
2295         textlen = 50;
2296         memset(text, 0xdd, textlen);
2297         digest = "\x12\x5d\x73\x42\xb9\xac\x11\xcd\x91\xa3\x9a\xf4\x8a\xa1\x7b\x4f\x63\xf1\x75\xd3";
2298         if (sctp_test_hmac("SHA1 test case 3", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2299             text, textlen, digest, digestlen) < 0)
2300                 failed++;
2301
2302         /*-
2303          * test_case =     4
2304          * key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
2305          * key_len =       25
2306          * data =          0xcd repeated 50 times
2307          * data_len =      50
2308          * digest =        0x4c9007f4026250c6bc8414f9bf50c86c2d7235da
2309          */
2310         keylen = 25;
2311         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);
2312         textlen = 50;
2313         memset(text, 0xcd, textlen);
2314         digest = "\x4c\x90\x07\xf4\x02\x62\x50\xc6\xbc\x84\x14\xf9\xbf\x50\xc8\x6c\x2d\x72\x35\xda";
2315         if (sctp_test_hmac("SHA1 test case 4", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2316             text, textlen, digest, digestlen) < 0)
2317                 failed++;
2318
2319         /*-
2320          * test_case =     5
2321          * key =           0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2322          * key_len =       20
2323          * data =          "Test With Truncation"
2324          * data_len =      20
2325          * digest =        0x4c1a03424b55e07fe7f27be1d58bb9324a9a5a04
2326          * digest-96 =     0x4c1a03424b55e07fe7f27be1
2327          */
2328         keylen = 20;
2329         memset(key, 0x0c, keylen);
2330         textlen = 20;
2331         strcpy(text, "Test With Truncation");
2332         digest = "\x4c\x1a\x03\x42\x4b\x55\xe0\x7f\xe7\xf2\x7b\xe1\xd5\x8b\xb9\x32\x4a\x9a\x5a\x04";
2333         if (sctp_test_hmac("SHA1 test case 5", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2334             text, textlen, digest, digestlen) < 0)
2335                 failed++;
2336
2337         /*-
2338          * test_case =     6
2339          * key =           0xaa repeated 80 times
2340          * key_len =       80
2341          * data =          "Test Using Larger Than Block-Size Key - Hash Key First"
2342          * data_len =      54
2343          * digest =        0xaa4ae5e15272d00e95705637ce8a3b55ed402112
2344          */
2345         keylen = 80;
2346         memset(key, 0xaa, keylen);
2347         textlen = 54;
2348         strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2349         digest = "\xaa\x4a\xe5\xe1\x52\x72\xd0\x0e\x95\x70\x56\x37\xce\x8a\x3b\x55\xed\x40\x21\x12";
2350         if (sctp_test_hmac("SHA1 test case 6", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2351             text, textlen, digest, digestlen) < 0)
2352                 failed++;
2353
2354         /*-
2355          * test_case =     7
2356          * key =           0xaa repeated 80 times
2357          * key_len =       80
2358          * data =          "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2359          * data_len =      73
2360          * digest =        0xe8e99d0f45237d786d6bbaa7965c7808bbff1a91
2361          */
2362         keylen = 80;
2363         memset(key, 0xaa, keylen);
2364         textlen = 73;
2365         strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2366         digest = "\xe8\xe9\x9d\x0f\x45\x23\x7d\x78\x6d\x6b\xba\xa7\x96\x5c\x78\x08\xbb\xff\x1a\x91";
2367         if (sctp_test_hmac("SHA1 test case 7", SCTP_AUTH_HMAC_ID_SHA1, key, keylen,
2368             text, textlen, digest, digestlen) < 0)
2369                 failed++;
2370
2371         /* done with all tests */
2372         if (failed)
2373                 printf("\nSHA1 test results: %d cases failed", failed);
2374         else
2375                 printf("\nSHA1 test results: all test cases passed");
2376 }
2377
2378 /*
2379  * RFC 2202: HMAC-MD5 test cases
2380  */
2381 void
2382 sctp_test_hmac_md5(void)
2383 {
2384         uint8_t *digest;
2385         uint8_t key[128];
2386         uint32_t keylen;
2387         uint8_t text[128];
2388         uint32_t textlen;
2389         uint32_t digestlen = 16;
2390         int failed = 0;
2391
2392         /*-
2393          * test_case =     1
2394          * key =           0x0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b0b
2395          * key_len =       16
2396          * data =          "Hi There"
2397          * data_len =      8
2398          * digest =        0x9294727a3638bb1c13f48ef8158bfc9d
2399          */
2400         keylen = 16;
2401         memset(key, 0x0b, keylen);
2402         textlen = 8;
2403         strcpy(text, "Hi There");
2404         digest = "\x92\x94\x72\x7a\x36\x38\xbb\x1c\x13\xf4\x8e\xf8\x15\x8b\xfc\x9d";
2405         if (sctp_test_hmac("MD5 test case 1", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2406             text, textlen, digest, digestlen) < 0)
2407                 failed++;
2408
2409         /*-
2410          * test_case =     2
2411          * key =           "Jefe"
2412          * key_len =       4
2413          * data =          "what do ya want for nothing?"
2414          * data_len =      28
2415          * digest =        0x750c783e6ab0b503eaa86e310a5db738
2416          */
2417         keylen = 4;
2418         strcpy(key, "Jefe");
2419         textlen = 28;
2420         strcpy(text, "what do ya want for nothing?");
2421         digest = "\x75\x0c\x78\x3e\x6a\xb0\xb5\x03\xea\xa8\x6e\x31\x0a\x5d\xb7\x38";
2422         if (sctp_test_hmac("MD5 test case 2", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2423             text, textlen, digest, digestlen) < 0)
2424                 failed++;
2425
2426         /*-
2427          * test_case =     3
2428          * key =           0xaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
2429          * key_len =       16
2430          * data =          0xdd repeated 50 times
2431          * data_len =      50
2432          * digest =        0x56be34521d144c88dbb8c733f0e8b3f6
2433          */
2434         keylen = 16;
2435         memset(key, 0xaa, keylen);
2436         textlen = 50;
2437         memset(text, 0xdd, textlen);
2438         digest = "\x56\xbe\x34\x52\x1d\x14\x4c\x88\xdb\xb8\xc7\x33\xf0\xe8\xb3\xf6";
2439         if (sctp_test_hmac("MD5 test case 3", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2440             text, textlen, digest, digestlen) < 0)
2441                 failed++;
2442
2443         /*-
2444          * test_case =     4
2445          * key =           0x0102030405060708090a0b0c0d0e0f10111213141516171819
2446          * key_len =       25
2447          * data =          0xcd repeated 50 times
2448          * data_len =      50
2449          * digest =        0x697eaf0aca3a3aea3a75164746ffaa79
2450          */
2451         keylen = 25;
2452         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);
2453         textlen = 50;
2454         memset(text, 0xcd, textlen);
2455         digest = "\x69\x7e\xaf\x0a\xca\x3a\x3a\xea\x3a\x75\x16\x47\x46\xff\xaa\x79";
2456         if (sctp_test_hmac("MD5 test case 4", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2457             text, textlen, digest, digestlen) < 0)
2458                 failed++;
2459
2460         /*-
2461          * test_case =     5
2462          * key = 0x0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c0c
2463          * key_len =       16
2464          * data =          "Test With Truncation"
2465          * data_len =      20
2466          * digest =        0x56461ef2342edc00f9bab995690efd4c
2467          * digest-96 =     0x56461ef2342edc00f9bab995
2468          */
2469         keylen = 16;
2470         memset(key, 0x0c, keylen);
2471         textlen = 20;
2472         strcpy(text, "Test With Truncation");
2473         digest = "\x56\x46\x1e\xf2\x34\x2e\xdc\x00\xf9\xba\xb9\x95\x69\x0e\xfd\x4c";
2474         if (sctp_test_hmac("MD5 test case 5", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2475             text, textlen, digest, digestlen) < 0)
2476                 failed++;
2477
2478         /*-
2479          * test_case =     6
2480          * key =           0xaa repeated 80 times
2481          * key_len =       80
2482          * data =          "Test Using Larger Than Block-Size Key - Hash Key First"
2483          * data_len =      54
2484          * digest =        0x6b1ab7fe4bd7bf8f0b62e6ce61b9d0cd
2485          */
2486         keylen = 80;
2487         memset(key, 0xaa, keylen);
2488         textlen = 54;
2489         strcpy(text, "Test Using Larger Than Block-Size Key - Hash Key First");
2490         digest = "\x6b\x1a\xb7\xfe\x4b\xd7\xbf\x8f\x0b\x62\xe6\xce\x61\xb9\xd0\xcd";
2491         if (sctp_test_hmac("MD5 test case 6", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2492             text, textlen, digest, digestlen) < 0)
2493                 failed++;
2494
2495         /*-
2496          * test_case =     7
2497          * key =           0xaa repeated 80 times
2498          * key_len =       80
2499          * data =          "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data"
2500          * data_len =      73
2501          * digest =        0x6f630fad67cda0ee1fb1f562db3aa53e
2502          */
2503         keylen = 80;
2504         memset(key, 0xaa, keylen);
2505         textlen = 73;
2506         strcpy(text, "Test Using Larger Than Block-Size Key and Larger Than One Block-Size Data");
2507         digest = "\x6f\x63\x0f\xad\x67\xcd\xa0\xee\x1f\xb1\xf5\x62\xdb\x3a\xa5\x3e";
2508         if (sctp_test_hmac("MD5 test case 7", SCTP_AUTH_HMAC_ID_MD5, key, keylen,
2509             text, textlen, digest, digestlen) < 0)
2510                 failed++;
2511
2512         /* done with all tests */
2513         if (failed)
2514                 printf("\nMD5 test results: %d cases failed", failed);
2515         else
2516                 printf("\nMD5 test results: all test cases passed");
2517 }
2518
2519 /*
2520  * test assoc key concatenation
2521  */
2522 static int
2523 sctp_test_key_concatenation(sctp_key_t * key1, sctp_key_t * key2,
2524     sctp_key_t * expected_key)
2525 {
2526         sctp_key_t *key;
2527         int ret_val;
2528
2529         sctp_show_key(key1, "\nkey1");
2530         sctp_show_key(key2, "\nkey2");
2531         key = sctp_compute_hashkey(key1, key2, NULL);
2532         sctp_show_key(expected_key, "\nExpected");
2533         sctp_show_key(key, "\nComputed");
2534         if (memcmp(key, expected_key, expected_key->keylen) != 0) {
2535                 printf("\nFAILED");
2536                 ret_val = -1;
2537         } else {
2538                 printf("\nPASSED");
2539                 ret_val = 0;
2540         }
2541         sctp_free_key(key1);
2542         sctp_free_key(key2);
2543         sctp_free_key(expected_key);
2544         sctp_free_key(key);
2545         return (ret_val);
2546 }
2547
2548
2549 void
2550 sctp_test_authkey(void)
2551 {
2552         sctp_key_t *key1, *key2, *expected_key;
2553         int failed = 0;
2554
2555         /* test case 1 */
2556         key1 = sctp_set_key("\x01\x01\x01\x01", 4);
2557         key2 = sctp_set_key("\x01\x02\x03\x04", 4);
2558         expected_key = sctp_set_key("\x01\x01\x01\x01\x01\x02\x03\x04", 8);
2559         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2560                 failed++;
2561
2562         /* test case 2 */
2563         key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2564         key2 = sctp_set_key("\x02", 1);
2565         expected_key = sctp_set_key("\x00\x00\x00\x01\x02", 5);
2566         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2567                 failed++;
2568
2569         /* test case 3 */
2570         key1 = sctp_set_key("\x01", 1);
2571         key2 = sctp_set_key("\x00\x00\x00\x02", 4);
2572         expected_key = sctp_set_key("\x01\x00\x00\x00\x02", 5);
2573         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2574                 failed++;
2575
2576         /* test case 4 */
2577         key1 = sctp_set_key("\x00\x00\x00\x01", 4);
2578         key2 = sctp_set_key("\x01", 1);
2579         expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2580         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2581                 failed++;
2582
2583         /* test case 5 */
2584         key1 = sctp_set_key("\x01", 1);
2585         key2 = sctp_set_key("\x00\x00\x00\x01", 4);
2586         expected_key = sctp_set_key("\x01\x00\x00\x00\x01", 5);
2587         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2588                 failed++;
2589
2590         /* test case 6 */
2591         key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2592         key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2593         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);
2594         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2595                 failed++;
2596
2597         /* test case 7 */
2598         key1 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x08", 11);
2599         key2 = sctp_set_key("\x00\x00\x00\x00\x01\x02\x03\x04\x05\x06\x07", 11);
2600         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);
2601         if (sctp_test_key_concatenation(key1, key2, expected_key) < 0)
2602                 failed++;
2603
2604         /* done with all tests */
2605         if (failed)
2606                 printf("\nKey concatenation test results: %d cases failed", failed);
2607         else
2608                 printf("\nKey concatenation test results: all test cases passed");
2609 }
2610
2611
2612 #if defined(STANDALONE_HMAC_TEST)
2613 int
2614 main(void)
2615 {
2616         sctp_test_hmac_sha1();
2617         sctp_test_hmac_md5();
2618         sctp_test_authkey();
2619 }
2620
2621 #endif                          /* STANDALONE_HMAC_TEST */
2622
2623 #endif                          /* SCTP_HMAC_TEST */