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

Cache object: 3bfcd20f6e80aa652865b720ba814656


[ 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.