The Design and Implementation of the FreeBSD Operating System, Second Edition
Now available: The Design and Implementation of the FreeBSD Operating System (Second Edition)


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]

FreeBSD/Linux Kernel Cross Reference
sys/netinet/sctp_auth.c

Version: -  FREEBSD  -  FREEBSD-13-STABLE  -  FREEBSD-13-0  -  FREEBSD-12-STABLE  -  FREEBSD-12-0  -  FREEBSD-11-STABLE  -  FREEBSD-11-0  -  FREEBSD-10-STABLE  -  FREEBSD-10-0  -  FREEBSD-9-STABLE  -  FREEBSD-9-0  -  FREEBSD-8-STABLE  -  FREEBSD-8-0  -  FREEBSD-7-STABLE  -  FREEBSD-7-0  -  FREEBSD-6-STABLE  -  FREEBSD-6-0  -  FREEBSD-5-STABLE  -  FREEBSD-5-0  -  FREEBSD-4-STABLE  -  FREEBSD-3-STABLE  -  FREEBSD22  -  l41  -  OPENBSD  -  linux-2.6  -  MK84  -  PLAN9  -  xnu-8792 
SearchContext: -  none  -  3  -  10 

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

Cache object: 4531356f26b368f65f8310479e2caf10


[ source navigation ] [ diff markup ] [ identifier search ] [ freetext search ] [ file search ] [ list types ] [ track identifier ]


This page is part of the FreeBSD/Linux Linux Kernel Cross-Reference, and was automatically generated using a modified version of the LXR engine.