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