2006-11-03 15:23:16 +00:00
|
|
|
/*-
|
2008-12-06 13:19:54 +00:00
|
|
|
* Copyright (c) 2001-2008, by Cisco Systems, Inc. All rights reserved.
|
2012-05-23 11:26:28 +00:00
|
|
|
* Copyright (c) 2008-2012, by Randall Stewart. All rights reserved.
|
|
|
|
* Copyright (c) 2008-2012, by Michael Tuexen. All rights reserved.
|
2006-11-03 15:23:16 +00:00
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are met:
|
|
|
|
*
|
|
|
|
* a) Redistributions of source code must retain the above copyright notice,
|
2011-12-17 19:21:40 +00:00
|
|
|
* this list of conditions and the following disclaimer.
|
2006-11-03 15:23:16 +00:00
|
|
|
*
|
|
|
|
* b) Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
2011-12-17 19:21:40 +00:00
|
|
|
* the documentation and/or other materials provided with the distribution.
|
2006-11-03 15:23:16 +00:00
|
|
|
*
|
|
|
|
* c) Neither the name of Cisco Systems, Inc. nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
|
|
|
|
* THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
|
|
|
#include <netinet/sctp_os.h>
|
|
|
|
#include <netinet/sctp.h>
|
|
|
|
#include <netinet/sctp_header.h>
|
|
|
|
#include <netinet/sctp_pcb.h>
|
|
|
|
#include <netinet/sctp_var.h>
|
2007-03-15 11:27:14 +00:00
|
|
|
#include <netinet/sctp_sysctl.h>
|
2006-11-03 15:23:16 +00:00
|
|
|
#include <netinet/sctputil.h>
|
|
|
|
#include <netinet/sctp_indata.h>
|
|
|
|
#include <netinet/sctp_output.h>
|
|
|
|
#include <netinet/sctp_auth.h>
|
|
|
|
|
|
|
|
#ifdef SCTP_DEBUG
|
2008-06-14 07:58:05 +00:00
|
|
|
#define SCTP_AUTH_DEBUG (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH1)
|
|
|
|
#define SCTP_AUTH_DEBUG2 (SCTP_BASE_SYSCTL(sctp_debug_on) & SCTP_DEBUG_AUTH2)
|
2006-11-03 15:23:16 +00:00
|
|
|
#endif /* SCTP_DEBUG */
|
|
|
|
|
|
|
|
|
2007-06-16 00:33:47 +00:00
|
|
|
void
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_clear_chunklist(sctp_auth_chklist_t * chklist)
|
|
|
|
{
|
|
|
|
bzero(chklist, sizeof(*chklist));
|
|
|
|
/* chklist->num_chunks = 0; */
|
|
|
|
}
|
|
|
|
|
|
|
|
sctp_auth_chklist_t *
|
|
|
|
sctp_alloc_chunklist(void)
|
|
|
|
{
|
|
|
|
sctp_auth_chklist_t *chklist;
|
|
|
|
|
|
|
|
SCTP_MALLOC(chklist, sctp_auth_chklist_t *, sizeof(*chklist),
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_M_AUTH_CL);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (chklist == NULL) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1, "sctp_alloc_chunklist: failed to get memory!\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
|
|
|
sctp_clear_chunklist(chklist);
|
|
|
|
}
|
|
|
|
return (chklist);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_free_chunklist(sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
if (list != NULL)
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_FREE(list, SCTP_M_AUTH_CL);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sctp_auth_chklist_t *
|
|
|
|
sctp_copy_chunklist(sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
sctp_auth_chklist_t *new_list;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (NULL);
|
|
|
|
|
|
|
|
/* get a new list */
|
|
|
|
new_list = sctp_alloc_chunklist();
|
|
|
|
if (new_list == NULL)
|
|
|
|
return (NULL);
|
|
|
|
/* copy it */
|
|
|
|
bcopy(list, new_list, sizeof(*new_list));
|
|
|
|
|
|
|
|
return (new_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* add a chunk to the required chunks list
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_auth_add_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
if (list == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* is chunk restricted? */
|
|
|
|
if ((chunk == SCTP_INITIATION) ||
|
|
|
|
(chunk == SCTP_INITIATION_ACK) ||
|
|
|
|
(chunk == SCTP_SHUTDOWN_COMPLETE) ||
|
|
|
|
(chunk == SCTP_AUTHENTICATION)) {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if (list->chunks[chunk] == 0) {
|
|
|
|
list->chunks[chunk] = 1;
|
|
|
|
list->num_chunks++;
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: added chunk %u (0x%02x) to Auth list\n",
|
|
|
|
chunk, chunk);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* delete a chunk from the required chunks list
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_auth_delete_chunk(uint8_t chunk, sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
if (list == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (list->chunks[chunk] == 1) {
|
|
|
|
list->chunks[chunk] = 0;
|
|
|
|
list->num_chunks--;
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: deleted chunk %u (0x%02x) from Auth list\n",
|
|
|
|
chunk, chunk);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2007-06-16 00:33:47 +00:00
|
|
|
size_t
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_auth_get_chklist_size(const sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
if (list == NULL)
|
|
|
|
return (0);
|
|
|
|
else
|
|
|
|
return (list->num_chunks);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* return the current number and list of required chunks caller must
|
|
|
|
* guarantee ptr has space for up to 256 bytes
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_serialize_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
|
|
|
|
{
|
|
|
|
int i, count = 0;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (list->chunks[i] != 0) {
|
|
|
|
*ptr++ = i;
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_pack_auth_chunks(const sctp_auth_chklist_t * list, uint8_t * ptr)
|
|
|
|
{
|
|
|
|
int i, size = 0;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (list->num_chunks <= 32) {
|
|
|
|
/* just list them, one byte each */
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (list->chunks[i] != 0) {
|
|
|
|
*ptr++ = i;
|
|
|
|
size++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
int index, offset;
|
|
|
|
|
|
|
|
/* pack into a 32 byte bitfield */
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (list->chunks[i] != 0) {
|
|
|
|
index = i / 8;
|
|
|
|
offset = i % 8;
|
|
|
|
ptr[index] |= (1 << offset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = 32;
|
|
|
|
}
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_unpack_auth_chunks(const uint8_t * ptr, uint8_t num_chunks,
|
|
|
|
sctp_auth_chklist_t * list)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int size;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
if (num_chunks <= 32) {
|
|
|
|
/* just pull them, one byte each */
|
|
|
|
for (i = 0; i < num_chunks; i++) {
|
2007-05-08 14:32:53 +00:00
|
|
|
(void)sctp_auth_add_chunk(*ptr++, list);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
size = num_chunks;
|
|
|
|
} else {
|
|
|
|
int index, offset;
|
|
|
|
|
|
|
|
/* unpack from a 32 byte bitfield */
|
|
|
|
for (index = 0; index < 32; index++) {
|
|
|
|
for (offset = 0; offset < 8; offset++) {
|
|
|
|
if (ptr[index] & (1 << offset)) {
|
2007-05-08 14:32:53 +00:00
|
|
|
(void)sctp_auth_add_chunk((index * 8) + offset, list);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
size = 32;
|
|
|
|
}
|
|
|
|
return (size);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* allocate structure space for a key of length keylen
|
|
|
|
*/
|
|
|
|
sctp_key_t *
|
|
|
|
sctp_alloc_key(uint32_t keylen)
|
|
|
|
{
|
|
|
|
sctp_key_t *new_key;
|
|
|
|
|
|
|
|
SCTP_MALLOC(new_key, sctp_key_t *, sizeof(*new_key) + keylen,
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_M_AUTH_KY);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (new_key == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
new_key->keylen = keylen;
|
|
|
|
return (new_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_free_key(sctp_key_t * key)
|
|
|
|
{
|
|
|
|
if (key != NULL)
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_FREE(key, SCTP_M_AUTH_KY);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_print_key(sctp_key_t * key, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (key == NULL) {
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%s: [Null key]\n", str);
|
2006-11-03 15:23:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%s: len %u, ", str, key->keylen);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (key->keylen) {
|
|
|
|
for (i = 0; i < key->keylen; i++)
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%02x", key->key[i]);
|
|
|
|
SCTP_PRINTF("\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("[Null key]\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_show_key(sctp_key_t * key, const char *str)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
if (key == NULL) {
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%s: [Null key]\n", str);
|
2006-11-03 15:23:16 +00:00
|
|
|
return;
|
|
|
|
}
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%s: len %u, ", str, key->keylen);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (key->keylen) {
|
|
|
|
for (i = 0; i < key->keylen; i++)
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("%02x", key->key[i]);
|
|
|
|
SCTP_PRINTF("\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
2012-05-04 09:27:00 +00:00
|
|
|
SCTP_PRINTF("[Null key]\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-06-16 00:33:47 +00:00
|
|
|
static uint32_t
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_get_keylen(sctp_key_t * key)
|
|
|
|
{
|
|
|
|
if (key != NULL)
|
|
|
|
return (key->keylen);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate a new random key of length 'keylen'
|
|
|
|
*/
|
|
|
|
sctp_key_t *
|
|
|
|
sctp_generate_random_key(uint32_t keylen)
|
|
|
|
{
|
|
|
|
sctp_key_t *new_key;
|
|
|
|
|
|
|
|
new_key = sctp_alloc_key(keylen);
|
|
|
|
if (new_key == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
2006-12-29 20:21:42 +00:00
|
|
|
SCTP_READ_RANDOM(new_key->key, keylen);
|
2006-11-03 15:23:16 +00:00
|
|
|
new_key->keylen = keylen;
|
|
|
|
return (new_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
sctp_key_t *
|
|
|
|
sctp_set_key(uint8_t * key, uint32_t keylen)
|
|
|
|
{
|
|
|
|
sctp_key_t *new_key;
|
|
|
|
|
|
|
|
new_key = sctp_alloc_key(keylen);
|
|
|
|
if (new_key == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
bcopy(key, new_key->key, keylen);
|
|
|
|
return (new_key);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
2006-11-03 15:23:16 +00:00
|
|
|
* given two keys of variable size, compute which key is "larger/smaller"
|
2008-12-06 13:19:54 +00:00
|
|
|
* returns: 1 if key1 > key2
|
|
|
|
* -1 if key1 < key2
|
|
|
|
* 0 if key1 = key2
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
sctp_compare_key(sctp_key_t * key1, sctp_key_t * key2)
|
|
|
|
{
|
|
|
|
uint32_t maxlen;
|
|
|
|
uint32_t i;
|
|
|
|
uint32_t key1len, key2len;
|
|
|
|
uint8_t *key_1, *key_2;
|
2013-11-07 18:50:11 +00:00
|
|
|
uint8_t val1, val2;
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* sanity/length check */
|
|
|
|
key1len = sctp_get_keylen(key1);
|
|
|
|
key2len = sctp_get_keylen(key2);
|
|
|
|
if ((key1len == 0) && (key2len == 0))
|
|
|
|
return (0);
|
|
|
|
else if (key1len == 0)
|
|
|
|
return (-1);
|
|
|
|
else if (key2len == 0)
|
|
|
|
return (1);
|
|
|
|
|
2013-11-07 18:50:11 +00:00
|
|
|
if (key1len < key2len) {
|
|
|
|
maxlen = key2len;
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
|
|
|
maxlen = key1len;
|
|
|
|
}
|
2013-11-07 18:50:11 +00:00
|
|
|
key_1 = key1->key;
|
|
|
|
key_2 = key2->key;
|
|
|
|
/* check for numeric equality */
|
2006-11-03 15:23:16 +00:00
|
|
|
for (i = 0; i < maxlen; i++) {
|
2013-11-07 18:50:11 +00:00
|
|
|
/* left-pad with zeros */
|
|
|
|
val1 = (i < (maxlen - key1len)) ? 0 : *(key_1++);
|
|
|
|
val2 = (i < (maxlen - key2len)) ? 0 : *(key_2++);
|
|
|
|
if (val1 > val2) {
|
2006-11-03 15:23:16 +00:00
|
|
|
return (1);
|
2013-11-07 18:50:11 +00:00
|
|
|
} else if (val1 < val2) {
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
2013-11-07 18:50:11 +00:00
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
/* keys are equal value, so check lengths */
|
|
|
|
if (key1len == key2len)
|
|
|
|
return (0);
|
|
|
|
else if (key1len < key2len)
|
|
|
|
return (-1);
|
|
|
|
else
|
|
|
|
return (1);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* generate the concatenated keying material based on the two keys and the
|
|
|
|
* shared key (if available). draft-ietf-tsvwg-auth specifies the specific
|
|
|
|
* order for concatenation
|
|
|
|
*/
|
|
|
|
sctp_key_t *
|
|
|
|
sctp_compute_hashkey(sctp_key_t * key1, sctp_key_t * key2, sctp_key_t * shared)
|
|
|
|
{
|
|
|
|
uint32_t keylen;
|
|
|
|
sctp_key_t *new_key;
|
|
|
|
uint8_t *key_ptr;
|
|
|
|
|
|
|
|
keylen = sctp_get_keylen(key1) + sctp_get_keylen(key2) +
|
|
|
|
sctp_get_keylen(shared);
|
|
|
|
|
|
|
|
if (keylen > 0) {
|
|
|
|
/* get space for the new key */
|
|
|
|
new_key = sctp_alloc_key(keylen);
|
|
|
|
if (new_key == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
new_key->keylen = keylen;
|
|
|
|
key_ptr = new_key->key;
|
|
|
|
} else {
|
|
|
|
/* all keys empty/null?! */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* concatenate the keys */
|
|
|
|
if (sctp_compare_key(key1, key2) <= 0) {
|
2007-08-27 05:19:48 +00:00
|
|
|
/* key is shared + key1 + key2 */
|
|
|
|
if (sctp_get_keylen(shared)) {
|
|
|
|
bcopy(shared->key, key_ptr, shared->keylen);
|
|
|
|
key_ptr += shared->keylen;
|
|
|
|
}
|
|
|
|
if (sctp_get_keylen(key1)) {
|
|
|
|
bcopy(key1->key, key_ptr, key1->keylen);
|
|
|
|
key_ptr += key1->keylen;
|
|
|
|
}
|
|
|
|
if (sctp_get_keylen(key2)) {
|
|
|
|
bcopy(key2->key, key_ptr, key2->keylen);
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
2007-08-27 05:19:48 +00:00
|
|
|
/* key is shared + key2 + key1 */
|
|
|
|
if (sctp_get_keylen(shared)) {
|
|
|
|
bcopy(shared->key, key_ptr, shared->keylen);
|
|
|
|
key_ptr += shared->keylen;
|
|
|
|
}
|
|
|
|
if (sctp_get_keylen(key2)) {
|
|
|
|
bcopy(key2->key, key_ptr, key2->keylen);
|
|
|
|
key_ptr += key2->keylen;
|
|
|
|
}
|
|
|
|
if (sctp_get_keylen(key1)) {
|
|
|
|
bcopy(key1->key, key_ptr, key1->keylen);
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
return (new_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sctp_sharedkey_t *
|
|
|
|
sctp_alloc_sharedkey(void)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *new_key;
|
|
|
|
|
|
|
|
SCTP_MALLOC(new_key, sctp_sharedkey_t *, sizeof(*new_key),
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_M_AUTH_KY);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (new_key == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
new_key->keyid = 0;
|
|
|
|
new_key->key = NULL;
|
2008-12-06 13:19:54 +00:00
|
|
|
new_key->refcount = 1;
|
|
|
|
new_key->deactivated = 0;
|
2006-11-03 15:23:16 +00:00
|
|
|
return (new_key);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_free_sharedkey(sctp_sharedkey_t * skey)
|
|
|
|
{
|
2008-12-06 13:19:54 +00:00
|
|
|
if (skey == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (SCTP_DECREMENT_AND_CHECK_REFCOUNT(&skey->refcount)) {
|
2006-11-03 15:23:16 +00:00
|
|
|
if (skey->key != NULL)
|
|
|
|
sctp_free_key(skey->key);
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_FREE(skey, SCTP_M_AUTH_KY);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sctp_sharedkey_t *
|
|
|
|
sctp_find_sharedkey(struct sctp_keyhead *shared_keys, uint16_t key_id)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
LIST_FOREACH(skey, shared_keys, next) {
|
|
|
|
if (skey->keyid == key_id)
|
|
|
|
return (skey);
|
|
|
|
}
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
int
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_insert_sharedkey(struct sctp_keyhead *shared_keys,
|
|
|
|
sctp_sharedkey_t * new_skey)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
if ((shared_keys == NULL) || (new_skey == NULL))
|
2008-12-06 13:19:54 +00:00
|
|
|
return (EINVAL);
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* insert into an empty list? */
|
2009-11-17 20:56:14 +00:00
|
|
|
if (LIST_EMPTY(shared_keys)) {
|
2006-11-03 15:23:16 +00:00
|
|
|
LIST_INSERT_HEAD(shared_keys, new_skey, next);
|
2008-12-06 13:19:54 +00:00
|
|
|
return (0);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
/* insert into the existing list, ordered by key id */
|
|
|
|
LIST_FOREACH(skey, shared_keys, next) {
|
|
|
|
if (new_skey->keyid < skey->keyid) {
|
|
|
|
/* insert it before here */
|
|
|
|
LIST_INSERT_BEFORE(skey, new_skey, next);
|
2008-12-06 13:19:54 +00:00
|
|
|
return (0);
|
2006-11-03 15:23:16 +00:00
|
|
|
} else if (new_skey->keyid == skey->keyid) {
|
|
|
|
/* replace the existing key */
|
2008-12-06 13:19:54 +00:00
|
|
|
/* verify this key *can* be replaced */
|
|
|
|
if ((skey->deactivated) && (skey->refcount > 1)) {
|
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"can't replace shared key id %u\n",
|
|
|
|
new_skey->keyid);
|
|
|
|
return (EBUSY);
|
|
|
|
}
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"replacing shared key id %u\n",
|
|
|
|
new_skey->keyid);
|
2006-11-03 15:23:16 +00:00
|
|
|
LIST_INSERT_BEFORE(skey, new_skey, next);
|
|
|
|
LIST_REMOVE(skey, next);
|
|
|
|
sctp_free_sharedkey(skey);
|
2008-12-06 13:19:54 +00:00
|
|
|
return (0);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
if (LIST_NEXT(skey, next) == NULL) {
|
|
|
|
/* belongs at the end of the list */
|
|
|
|
LIST_INSERT_AFTER(skey, new_skey, next);
|
2008-12-06 13:19:54 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* shouldn't reach here */
|
2016-07-16 07:48:01 +00:00
|
|
|
return (EINVAL);
|
2008-12-06 13:19:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_auth_key_acquire(struct sctp_tcb *stcb, uint16_t key_id)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
/* find the shared key */
|
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
|
|
|
|
|
|
|
|
/* bump the ref count */
|
|
|
|
if (skey) {
|
|
|
|
atomic_add_int(&skey->refcount, 1);
|
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH2,
|
|
|
|
"%s: stcb %p key %u refcount acquire to %d\n",
|
2015-10-19 11:17:54 +00:00
|
|
|
__func__, (void *)stcb, key_id, skey->refcount);
|
2008-12-06 13:19:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2011-05-08 09:11:59 +00:00
|
|
|
sctp_auth_key_release(struct sctp_tcb *stcb, uint16_t key_id, int so_locked
|
|
|
|
#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
|
|
|
|
SCTP_UNUSED
|
|
|
|
#endif
|
|
|
|
)
|
2008-12-06 13:19:54 +00:00
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
/* find the shared key */
|
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, key_id);
|
|
|
|
|
|
|
|
/* decrement the ref count */
|
|
|
|
if (skey) {
|
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH2,
|
|
|
|
"%s: stcb %p key %u refcount release to %d\n",
|
2015-10-19 11:17:54 +00:00
|
|
|
__func__, (void *)stcb, key_id, skey->refcount);
|
2008-12-06 13:19:54 +00:00
|
|
|
|
|
|
|
/* see if a notification should be generated */
|
2015-03-25 22:45:54 +00:00
|
|
|
if ((skey->refcount <= 2) && (skey->deactivated)) {
|
2008-12-06 13:19:54 +00:00
|
|
|
/* notify ULP that key is no longer used */
|
|
|
|
sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb,
|
2011-05-08 09:11:59 +00:00
|
|
|
key_id, 0, so_locked);
|
2008-12-06 13:19:54 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH2,
|
|
|
|
"%s: stcb %p key %u no longer used, %d\n",
|
2015-10-19 11:17:54 +00:00
|
|
|
__func__, (void *)stcb, key_id, skey->refcount);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
2015-03-25 22:45:54 +00:00
|
|
|
sctp_free_sharedkey(skey);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static sctp_sharedkey_t *
|
|
|
|
sctp_copy_sharedkey(const sctp_sharedkey_t * skey)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *new_skey;
|
|
|
|
|
|
|
|
if (skey == NULL)
|
|
|
|
return (NULL);
|
|
|
|
new_skey = sctp_alloc_sharedkey();
|
|
|
|
if (new_skey == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (skey->key != NULL)
|
|
|
|
new_skey->key = sctp_set_key(skey->key->key, skey->key->keylen);
|
|
|
|
else
|
|
|
|
new_skey->key = NULL;
|
|
|
|
new_skey->keyid = skey->keyid;
|
|
|
|
return (new_skey);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_copy_skeylist(const struct sctp_keyhead *src, struct sctp_keyhead *dest)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey, *new_skey;
|
|
|
|
int count = 0;
|
|
|
|
|
|
|
|
if ((src == NULL) || (dest == NULL))
|
|
|
|
return (0);
|
|
|
|
LIST_FOREACH(skey, src, next) {
|
|
|
|
new_skey = sctp_copy_sharedkey(skey);
|
|
|
|
if (new_skey != NULL) {
|
2016-07-16 07:48:01 +00:00
|
|
|
if (sctp_insert_sharedkey(dest, new_skey)) {
|
|
|
|
sctp_free_sharedkey(new_skey);
|
|
|
|
} else {
|
|
|
|
count++;
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return (count);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
sctp_hmaclist_t *
|
2014-09-16 14:20:33 +00:00
|
|
|
sctp_alloc_hmaclist(uint16_t num_hmacs)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
sctp_hmaclist_t *new_list;
|
|
|
|
int alloc_size;
|
|
|
|
|
|
|
|
alloc_size = sizeof(*new_list) + num_hmacs * sizeof(new_list->hmac[0]);
|
|
|
|
SCTP_MALLOC(new_list, sctp_hmaclist_t *, alloc_size,
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_M_AUTH_HL);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (new_list == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
new_list->max_algo = num_hmacs;
|
|
|
|
new_list->num_algo = 0;
|
|
|
|
return (new_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_free_hmaclist(sctp_hmaclist_t * list)
|
|
|
|
{
|
|
|
|
if (list != NULL) {
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_FREE(list, SCTP_M_AUTH_HL);
|
2006-11-03 15:23:16 +00:00
|
|
|
list = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_auth_add_hmacid(sctp_hmaclist_t * list, uint16_t hmac_id)
|
|
|
|
{
|
2007-05-28 11:17:24 +00:00
|
|
|
int i;
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
if (list == NULL)
|
|
|
|
return (-1);
|
|
|
|
if (list->num_algo == list->max_algo) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: HMAC id list full, ignoring add %u\n", hmac_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
if ((hmac_id != SCTP_AUTH_HMAC_ID_SHA1) &&
|
2013-09-02 22:48:41 +00:00
|
|
|
(hmac_id != SCTP_AUTH_HMAC_ID_SHA256)) {
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2007-05-28 11:17:24 +00:00
|
|
|
/* Now is it already in the list */
|
|
|
|
for (i = 0; i < list->num_algo; i++) {
|
|
|
|
if (list->hmac[i] == hmac_id) {
|
|
|
|
/* already in list */
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
}
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1, "SCTP: add HMAC id %u to list\n", hmac_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
list->hmac[list->num_algo++] = hmac_id;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
sctp_hmaclist_t *
|
|
|
|
sctp_copy_hmaclist(sctp_hmaclist_t * list)
|
|
|
|
{
|
|
|
|
sctp_hmaclist_t *new_list;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (NULL);
|
|
|
|
/* get a new list */
|
|
|
|
new_list = sctp_alloc_hmaclist(list->max_algo);
|
|
|
|
if (new_list == NULL)
|
|
|
|
return (NULL);
|
|
|
|
/* copy it */
|
|
|
|
new_list->max_algo = list->max_algo;
|
|
|
|
new_list->num_algo = list->num_algo;
|
|
|
|
for (i = 0; i < list->num_algo; i++)
|
|
|
|
new_list->hmac[i] = list->hmac[i];
|
|
|
|
return (new_list);
|
|
|
|
}
|
|
|
|
|
|
|
|
sctp_hmaclist_t *
|
|
|
|
sctp_default_supported_hmaclist(void)
|
|
|
|
{
|
|
|
|
sctp_hmaclist_t *new_list;
|
|
|
|
|
|
|
|
new_list = sctp_alloc_hmaclist(2);
|
|
|
|
if (new_list == NULL)
|
|
|
|
return (NULL);
|
2013-09-02 22:48:41 +00:00
|
|
|
/* We prefer SHA256, so list it first */
|
2007-05-08 14:32:53 +00:00
|
|
|
(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA256);
|
2013-09-02 22:48:41 +00:00
|
|
|
(void)sctp_auth_add_hmacid(new_list, SCTP_AUTH_HMAC_ID_SHA1);
|
2006-11-03 15:23:16 +00:00
|
|
|
return (new_list);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* HMAC algos are listed in priority/preference order
|
|
|
|
* find the best HMAC id to use for the peer based on local support
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
uint16_t
|
|
|
|
sctp_negotiate_hmacid(sctp_hmaclist_t * peer, sctp_hmaclist_t * local)
|
|
|
|
{
|
|
|
|
int i, j;
|
|
|
|
|
|
|
|
if ((local == NULL) || (peer == NULL))
|
|
|
|
return (SCTP_AUTH_HMAC_ID_RSVD);
|
|
|
|
|
|
|
|
for (i = 0; i < peer->num_algo; i++) {
|
|
|
|
for (j = 0; j < local->num_algo; j++) {
|
|
|
|
if (peer->hmac[i] == local->hmac[j]) {
|
|
|
|
/* found the "best" one */
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: negotiated peer HMAC id %u\n",
|
|
|
|
peer->hmac[i]);
|
2006-11-03 15:23:16 +00:00
|
|
|
return (peer->hmac[i]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* didn't find one! */
|
|
|
|
return (SCTP_AUTH_HMAC_ID_RSVD);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* serialize the HMAC algo list and return space used
|
|
|
|
* caller must guarantee ptr has appropriate space
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_serialize_hmaclist(sctp_hmaclist_t * list, uint8_t * ptr)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
uint16_t hmac_id;
|
|
|
|
|
|
|
|
if (list == NULL)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (i = 0; i < list->num_algo; i++) {
|
|
|
|
hmac_id = htons(list->hmac[i]);
|
|
|
|
bcopy(&hmac_id, ptr, sizeof(hmac_id));
|
|
|
|
ptr += sizeof(hmac_id);
|
|
|
|
}
|
|
|
|
return (list->num_algo * sizeof(hmac_id));
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_verify_hmac_param(struct sctp_auth_hmac_algo *hmacs, uint32_t num_hmacs)
|
|
|
|
{
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
for (i = 0; i < num_hmacs; i++) {
|
2013-09-02 22:48:41 +00:00
|
|
|
if (ntohs(hmacs->hmac_ids[i]) == SCTP_AUTH_HMAC_ID_SHA1) {
|
|
|
|
return (0);
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
2013-09-02 22:48:41 +00:00
|
|
|
return (-1);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sctp_authinfo_t *
|
|
|
|
sctp_alloc_authinfo(void)
|
|
|
|
{
|
|
|
|
sctp_authinfo_t *new_authinfo;
|
|
|
|
|
|
|
|
SCTP_MALLOC(new_authinfo, sctp_authinfo_t *, sizeof(*new_authinfo),
|
2007-05-29 09:29:03 +00:00
|
|
|
SCTP_M_AUTH_IF);
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
if (new_authinfo == NULL) {
|
|
|
|
/* out of memory */
|
|
|
|
return (NULL);
|
|
|
|
}
|
2007-08-24 00:53:53 +00:00
|
|
|
bzero(new_authinfo, sizeof(*new_authinfo));
|
2006-11-03 15:23:16 +00:00
|
|
|
return (new_authinfo);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_free_authinfo(sctp_authinfo_t * authinfo)
|
|
|
|
{
|
|
|
|
if (authinfo == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (authinfo->random != NULL)
|
|
|
|
sctp_free_key(authinfo->random);
|
|
|
|
if (authinfo->peer_random != NULL)
|
|
|
|
sctp_free_key(authinfo->peer_random);
|
|
|
|
if (authinfo->assoc_key != NULL)
|
|
|
|
sctp_free_key(authinfo->assoc_key);
|
|
|
|
if (authinfo->recv_key != NULL)
|
|
|
|
sctp_free_key(authinfo->recv_key);
|
|
|
|
|
|
|
|
/* We are NOT dynamically allocating authinfo's right now... */
|
2007-05-29 09:29:03 +00:00
|
|
|
/* SCTP_FREE(authinfo, SCTP_M_AUTH_??); */
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-06-16 00:33:47 +00:00
|
|
|
uint32_t
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_get_auth_chunk_len(uint16_t hmac_algo)
|
|
|
|
{
|
|
|
|
int size;
|
|
|
|
|
|
|
|
size = sizeof(struct sctp_auth_chunk) + sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
return (SCTP_SIZE32(size));
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t
|
|
|
|
sctp_get_hmac_digest_len(uint16_t hmac_algo)
|
|
|
|
{
|
|
|
|
switch (hmac_algo) {
|
|
|
|
case SCTP_AUTH_HMAC_ID_SHA1:
|
|
|
|
return (SCTP_AUTH_DIGEST_LEN_SHA1);
|
|
|
|
case SCTP_AUTH_HMAC_ID_SHA256:
|
|
|
|
return (SCTP_AUTH_DIGEST_LEN_SHA256);
|
|
|
|
default:
|
|
|
|
/* unknown HMAC algorithm: can't do anything */
|
|
|
|
return (0);
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline int
|
|
|
|
sctp_get_hmac_block_len(uint16_t hmac_algo)
|
|
|
|
{
|
|
|
|
switch (hmac_algo) {
|
2010-06-05 21:33:16 +00:00
|
|
|
case SCTP_AUTH_HMAC_ID_SHA1:
|
2007-05-17 12:16:24 +00:00
|
|
|
return (64);
|
2006-11-03 15:23:16 +00:00
|
|
|
case SCTP_AUTH_HMAC_ID_SHA256:
|
|
|
|
return (64);
|
|
|
|
case SCTP_AUTH_HMAC_ID_RSVD:
|
|
|
|
default:
|
|
|
|
/* unknown HMAC algorithm: can't do anything */
|
|
|
|
return (0);
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sctp_hmac_init(uint16_t hmac_algo, sctp_hash_context_t * ctx)
|
|
|
|
{
|
|
|
|
switch (hmac_algo) {
|
2010-06-05 21:33:16 +00:00
|
|
|
case SCTP_AUTH_HMAC_ID_SHA1:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA1_INIT(&ctx->sha1);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_SHA256:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA256_INIT(&ctx->sha256);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_RSVD:
|
|
|
|
default:
|
|
|
|
/* unknown HMAC algorithm: can't do anything */
|
|
|
|
return;
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sctp_hmac_update(uint16_t hmac_algo, sctp_hash_context_t * ctx,
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t * text, uint32_t textlen)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
switch (hmac_algo) {
|
2010-06-05 21:33:16 +00:00
|
|
|
case SCTP_AUTH_HMAC_ID_SHA1:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA1_UPDATE(&ctx->sha1, text, textlen);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_SHA256:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA256_UPDATE(&ctx->sha256, text, textlen);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_RSVD:
|
|
|
|
default:
|
|
|
|
/* unknown HMAC algorithm: can't do anything */
|
|
|
|
return;
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
sctp_hmac_final(uint16_t hmac_algo, sctp_hash_context_t * ctx,
|
|
|
|
uint8_t * digest)
|
|
|
|
{
|
|
|
|
switch (hmac_algo) {
|
2010-06-05 21:33:16 +00:00
|
|
|
case SCTP_AUTH_HMAC_ID_SHA1:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA1_FINAL(digest, &ctx->sha1);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_SHA256:
|
2013-09-02 22:48:41 +00:00
|
|
|
SCTP_SHA256_FINAL(digest, &ctx->sha256);
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
case SCTP_AUTH_HMAC_ID_RSVD:
|
|
|
|
default:
|
|
|
|
/* unknown HMAC algorithm: can't do anything */
|
|
|
|
return;
|
|
|
|
} /* end switch */
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
2006-11-03 15:23:16 +00:00
|
|
|
* Keyed-Hashing for Message Authentication: FIPS 198 (RFC 2104)
|
|
|
|
*
|
|
|
|
* Compute the HMAC digest using the desired hash key, text, and HMAC
|
|
|
|
* algorithm. Resulting digest is placed in 'digest' and digest length
|
|
|
|
* is returned, if the HMAC was performed.
|
|
|
|
*
|
|
|
|
* WARNING: it is up to the caller to supply sufficient space to hold the
|
|
|
|
* resultant digest.
|
|
|
|
*/
|
|
|
|
uint32_t
|
|
|
|
sctp_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t * text, uint32_t textlen, uint8_t * digest)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
uint32_t digestlen;
|
|
|
|
uint32_t blocklen;
|
|
|
|
sctp_hash_context_t ctx;
|
|
|
|
uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
|
|
|
|
uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
uint32_t i;
|
|
|
|
|
|
|
|
/* sanity check the material and length */
|
|
|
|
if ((key == NULL) || (keylen == 0) || (text == NULL) ||
|
|
|
|
(textlen == 0) || (digest == NULL)) {
|
|
|
|
/* can't do HMAC with empty key or text or digest store */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* validate the hmac algo and get the digest length */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
if (digestlen == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* hash the key if it is longer than the hash block size */
|
|
|
|
blocklen = sctp_get_hmac_block_len(hmac_algo);
|
|
|
|
if (keylen > blocklen) {
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, key, keylen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
/* set the hashed key as the key */
|
|
|
|
keylen = digestlen;
|
|
|
|
key = temp;
|
|
|
|
}
|
|
|
|
/* initialize the inner/outer pads with the key and "append" zeroes */
|
|
|
|
bzero(ipad, blocklen);
|
|
|
|
bzero(opad, blocklen);
|
|
|
|
bcopy(key, ipad, keylen);
|
|
|
|
bcopy(key, opad, keylen);
|
|
|
|
|
|
|
|
/* XOR the key with ipad and opad values */
|
|
|
|
for (i = 0; i < blocklen; i++) {
|
|
|
|
ipad[i] ^= 0x36;
|
|
|
|
opad[i] ^= 0x5c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* perform inner hash */
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, text, textlen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
|
|
|
|
/* perform outer hash */
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, digest);
|
|
|
|
|
|
|
|
return (digestlen);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mbuf version */
|
|
|
|
uint32_t
|
|
|
|
sctp_hmac_m(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
|
2007-06-09 13:46:57 +00:00
|
|
|
struct mbuf *m, uint32_t m_offset, uint8_t * digest, uint32_t trailer)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
uint32_t digestlen;
|
|
|
|
uint32_t blocklen;
|
|
|
|
sctp_hash_context_t ctx;
|
|
|
|
uint8_t ipad[128], opad[128]; /* keyed hash inner/outer pads */
|
|
|
|
uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
uint32_t i;
|
|
|
|
struct mbuf *m_tmp;
|
|
|
|
|
|
|
|
/* sanity check the material and length */
|
|
|
|
if ((key == NULL) || (keylen == 0) || (m == NULL) || (digest == NULL)) {
|
|
|
|
/* can't do HMAC with empty key or text or digest store */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* validate the hmac algo and get the digest length */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
if (digestlen == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* hash the key if it is longer than the hash block size */
|
|
|
|
blocklen = sctp_get_hmac_block_len(hmac_algo);
|
|
|
|
if (keylen > blocklen) {
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, key, keylen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
/* set the hashed key as the key */
|
|
|
|
keylen = digestlen;
|
|
|
|
key = temp;
|
|
|
|
}
|
|
|
|
/* initialize the inner/outer pads with the key and "append" zeroes */
|
|
|
|
bzero(ipad, blocklen);
|
|
|
|
bzero(opad, blocklen);
|
|
|
|
bcopy(key, ipad, keylen);
|
|
|
|
bcopy(key, opad, keylen);
|
|
|
|
|
|
|
|
/* XOR the key with ipad and opad values */
|
|
|
|
for (i = 0; i < blocklen; i++) {
|
|
|
|
ipad[i] ^= 0x36;
|
|
|
|
opad[i] ^= 0x5c;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* perform inner hash */
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, ipad, blocklen);
|
|
|
|
/* find the correct starting mbuf and offset (get start of text) */
|
|
|
|
m_tmp = m;
|
2006-12-29 20:21:42 +00:00
|
|
|
while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
|
|
|
|
m_offset -= SCTP_BUF_LEN(m_tmp);
|
|
|
|
m_tmp = SCTP_BUF_NEXT(m_tmp);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
/* now use the rest of the mbuf chain for the text */
|
|
|
|
while (m_tmp != NULL) {
|
2007-06-09 13:46:57 +00:00
|
|
|
if ((SCTP_BUF_NEXT(m_tmp) == NULL) && trailer) {
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
|
|
|
|
SCTP_BUF_LEN(m_tmp) - (trailer + m_offset));
|
|
|
|
} else {
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, mtod(m_tmp, uint8_t *) + m_offset,
|
|
|
|
SCTP_BUF_LEN(m_tmp) - m_offset);
|
|
|
|
}
|
2006-12-29 20:21:42 +00:00
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* clear the offset since it's only for the first mbuf */
|
|
|
|
m_offset = 0;
|
2006-12-29 20:21:42 +00:00
|
|
|
m_tmp = SCTP_BUF_NEXT(m_tmp);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
|
|
|
|
/* perform outer hash */
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, opad, blocklen);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, temp, digestlen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, digest);
|
|
|
|
|
|
|
|
return (digestlen);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
2006-11-03 15:23:16 +00:00
|
|
|
* verify the HMAC digest using the desired hash key, text, and HMAC
|
2008-12-06 13:19:54 +00:00
|
|
|
* algorithm.
|
|
|
|
* Returns -1 on error, 0 on success.
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_verify_hmac(uint16_t hmac_algo, uint8_t * key, uint32_t keylen,
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t * text, uint32_t textlen,
|
2006-11-03 15:23:16 +00:00
|
|
|
uint8_t * digest, uint32_t digestlen)
|
|
|
|
{
|
|
|
|
uint32_t len;
|
|
|
|
uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
|
|
|
|
/* sanity check the material and length */
|
|
|
|
if ((key == NULL) || (keylen == 0) ||
|
|
|
|
(text == NULL) || (textlen == 0) || (digest == NULL)) {
|
|
|
|
/* can't do HMAC with empty key or text or digest */
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
len = sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
if ((len == 0) || (digestlen != len))
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* compute the expected hash */
|
|
|
|
if (sctp_hmac(hmac_algo, key, keylen, text, textlen, temp) != len)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
if (memcmp(digest, temp, digestlen) != 0)
|
|
|
|
return (-1);
|
|
|
|
else
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* computes the requested HMAC using a key struct (which may be modified if
|
|
|
|
* the keylen exceeds the HMAC block len).
|
|
|
|
*/
|
|
|
|
uint32_t
|
2007-02-12 23:24:31 +00:00
|
|
|
sctp_compute_hmac(uint16_t hmac_algo, sctp_key_t * key, uint8_t * text,
|
2006-11-03 15:23:16 +00:00
|
|
|
uint32_t textlen, uint8_t * digest)
|
|
|
|
{
|
|
|
|
uint32_t digestlen;
|
|
|
|
uint32_t blocklen;
|
|
|
|
sctp_hash_context_t ctx;
|
|
|
|
uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if ((key == NULL) || (text == NULL) || (textlen == 0) ||
|
|
|
|
(digest == NULL)) {
|
|
|
|
/* can't do HMAC with empty key or text or digest store */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* validate the hmac algo and get the digest length */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
if (digestlen == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* hash the key if it is longer than the hash block size */
|
|
|
|
blocklen = sctp_get_hmac_block_len(hmac_algo);
|
|
|
|
if (key->keylen > blocklen) {
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
/* save the hashed key as the new key */
|
|
|
|
key->keylen = digestlen;
|
|
|
|
bcopy(temp, key->key, key->keylen);
|
|
|
|
}
|
|
|
|
return (sctp_hmac(hmac_algo, key->key, key->keylen, text, textlen,
|
|
|
|
digest));
|
|
|
|
}
|
|
|
|
|
|
|
|
/* mbuf version */
|
|
|
|
uint32_t
|
|
|
|
sctp_compute_hmac_m(uint16_t hmac_algo, sctp_key_t * key, struct mbuf *m,
|
|
|
|
uint32_t m_offset, uint8_t * digest)
|
|
|
|
{
|
|
|
|
uint32_t digestlen;
|
|
|
|
uint32_t blocklen;
|
|
|
|
sctp_hash_context_t ctx;
|
|
|
|
uint8_t temp[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if ((key == NULL) || (m == NULL) || (digest == NULL)) {
|
|
|
|
/* can't do HMAC with empty key or text or digest store */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
/* validate the hmac algo and get the digest length */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(hmac_algo);
|
|
|
|
if (digestlen == 0)
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
/* hash the key if it is longer than the hash block size */
|
|
|
|
blocklen = sctp_get_hmac_block_len(hmac_algo);
|
|
|
|
if (key->keylen > blocklen) {
|
|
|
|
sctp_hmac_init(hmac_algo, &ctx);
|
|
|
|
sctp_hmac_update(hmac_algo, &ctx, key->key, key->keylen);
|
|
|
|
sctp_hmac_final(hmac_algo, &ctx, temp);
|
|
|
|
/* save the hashed key as the new key */
|
|
|
|
key->keylen = digestlen;
|
|
|
|
bcopy(temp, key->key, key->keylen);
|
|
|
|
}
|
2007-06-09 13:46:57 +00:00
|
|
|
return (sctp_hmac_m(hmac_algo, key->key, key->keylen, m, m_offset, digest, 0));
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
sctp_auth_is_supported_hmac(sctp_hmaclist_t * list, uint16_t id)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if ((list == NULL) || (id == SCTP_AUTH_HMAC_ID_RSVD))
|
|
|
|
return (0);
|
|
|
|
|
|
|
|
for (i = 0; i < list->num_algo; i++)
|
|
|
|
if (list->hmac[i] == id)
|
|
|
|
return (1);
|
|
|
|
|
|
|
|
/* not in the list */
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* clear any cached key(s) if they match the given key id on an association.
|
|
|
|
* the cached key(s) will be recomputed and re-cached at next use.
|
|
|
|
* ASSUMES TCB_LOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
sctp_clear_cachedkeys(struct sctp_tcb *stcb, uint16_t keyid)
|
|
|
|
{
|
|
|
|
if (stcb == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (keyid == stcb->asoc.authinfo.assoc_keyid) {
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.assoc_key);
|
|
|
|
stcb->asoc.authinfo.assoc_key = NULL;
|
|
|
|
}
|
|
|
|
if (keyid == stcb->asoc.authinfo.recv_keyid) {
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.recv_key);
|
|
|
|
stcb->asoc.authinfo.recv_key = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
2006-11-03 15:23:16 +00:00
|
|
|
* clear any cached key(s) if they match the given key id for all assocs on
|
2008-12-06 13:19:54 +00:00
|
|
|
* an endpoint.
|
|
|
|
* ASSUMES INP_WLOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
void
|
|
|
|
sctp_clear_cachedkeys_ep(struct sctp_inpcb *inp, uint16_t keyid)
|
|
|
|
{
|
|
|
|
struct sctp_tcb *stcb;
|
|
|
|
|
|
|
|
if (inp == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* clear the cached keys on all assocs on this instance */
|
|
|
|
LIST_FOREACH(stcb, &inp->sctp_asoc_list, sctp_tcblist) {
|
|
|
|
SCTP_TCB_LOCK(stcb);
|
|
|
|
sctp_clear_cachedkeys(stcb, keyid);
|
|
|
|
SCTP_TCB_UNLOCK(stcb);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* delete a shared key from an association
|
|
|
|
* ASSUMES TCB_LOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_delete_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
if (stcb == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* is the keyid the assoc active sending key */
|
2008-12-06 13:19:54 +00:00
|
|
|
if (keyid == stcb->asoc.authinfo.active_keyid)
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* does the key exist? */
|
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
|
|
|
|
if (skey == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/* are there other refcount holders on the key? */
|
|
|
|
if (skey->refcount > 1)
|
|
|
|
return (-1);
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* remove it */
|
|
|
|
LIST_REMOVE(skey, next);
|
|
|
|
sctp_free_sharedkey(skey); /* frees skey->key as well */
|
|
|
|
|
|
|
|
/* clear any cached keys */
|
|
|
|
sctp_clear_cachedkeys(stcb, keyid);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* deletes a shared key from the endpoint
|
|
|
|
* ASSUMES INP_WLOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_delete_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
if (inp == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/* is the keyid the active sending key on the endpoint */
|
2006-11-03 15:23:16 +00:00
|
|
|
if (keyid == inp->sctp_ep.default_keyid)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* does the key exist? */
|
|
|
|
skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
|
|
|
|
if (skey == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/* endpoint keys are not refcounted */
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* remove it */
|
|
|
|
LIST_REMOVE(skey, next);
|
|
|
|
sctp_free_sharedkey(skey); /* frees skey->key as well */
|
|
|
|
|
|
|
|
/* clear any cached keys */
|
|
|
|
sctp_clear_cachedkeys_ep(inp, keyid);
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* set the active key on an association
|
|
|
|
* ASSUMES TCB_LOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_auth_setactivekey(struct sctp_tcb *stcb, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey = NULL;
|
|
|
|
|
|
|
|
/* find the key on the assoc */
|
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
|
|
|
|
if (skey == NULL) {
|
|
|
|
/* that key doesn't exist */
|
|
|
|
return (-1);
|
|
|
|
}
|
2008-12-06 13:19:54 +00:00
|
|
|
if ((skey->deactivated) && (skey->refcount > 1)) {
|
|
|
|
/* can't reactivate a deactivated key with other refcounts */
|
|
|
|
return (-1);
|
2007-05-09 13:30:06 +00:00
|
|
|
}
|
2008-12-06 13:19:54 +00:00
|
|
|
/* set the (new) active key */
|
|
|
|
stcb->asoc.authinfo.active_keyid = keyid;
|
|
|
|
/* reset the deactivated flag */
|
|
|
|
skey->deactivated = 0;
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* set the active key on an endpoint
|
|
|
|
* ASSUMES INP_WLOCK is already held
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_auth_setactivekey_ep(struct sctp_inpcb *inp, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
/* find the key */
|
|
|
|
skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
|
|
|
|
if (skey == NULL) {
|
|
|
|
/* that key doesn't exist */
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
inp->sctp_ep.default_keyid = keyid;
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* deactivates a shared key from the association
|
|
|
|
* ASSUMES INP_WLOCK is already held
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_deact_sharedkey(struct sctp_tcb *stcb, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
if (stcb == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* is the keyid the assoc active sending key */
|
|
|
|
if (keyid == stcb->asoc.authinfo.active_keyid)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* does the key exist? */
|
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
|
|
|
|
if (skey == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* are there other refcount holders on the key? */
|
|
|
|
if (skey->refcount == 1) {
|
|
|
|
/* no other users, send a notification for this key */
|
|
|
|
sctp_ulp_notify(SCTP_NOTIFY_AUTH_FREE_KEY, stcb, keyid, 0,
|
|
|
|
SCTP_SO_LOCKED);
|
|
|
|
}
|
|
|
|
/* mark the key as deactivated */
|
|
|
|
skey->deactivated = 1;
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* deactivates a shared key from the endpoint
|
|
|
|
* ASSUMES INP_WLOCK is already held
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_deact_sharedkey_ep(struct sctp_inpcb *inp, uint16_t keyid)
|
|
|
|
{
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
|
|
|
|
if (inp == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* is the keyid the active sending key on the endpoint */
|
|
|
|
if (keyid == inp->sctp_ep.default_keyid)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* does the key exist? */
|
|
|
|
skey = sctp_find_sharedkey(&inp->sctp_ep.shared_keys, keyid);
|
|
|
|
if (skey == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/* endpoint keys are not refcounted */
|
|
|
|
|
|
|
|
/* remove it */
|
|
|
|
LIST_REMOVE(skey, next);
|
|
|
|
sctp_free_sharedkey(skey); /* frees skey->key as well */
|
|
|
|
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/*
|
|
|
|
* get local authentication parameters from cookie (from INIT-ACK)
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sctp_auth_get_cookie_params(struct sctp_tcb *stcb, struct mbuf *m,
|
|
|
|
uint32_t offset, uint32_t length)
|
|
|
|
{
|
|
|
|
struct sctp_paramhdr *phdr, tmp_param;
|
|
|
|
uint16_t plen, ptype;
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t random_store[SCTP_PARAM_BUFFER_SIZE];
|
2007-03-31 11:47:30 +00:00
|
|
|
struct sctp_auth_random *p_random = NULL;
|
2006-11-03 15:23:16 +00:00
|
|
|
uint16_t random_len = 0;
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t hmacs_store[SCTP_PARAM_BUFFER_SIZE];
|
2006-11-03 15:23:16 +00:00
|
|
|
struct sctp_auth_hmac_algo *hmacs = NULL;
|
|
|
|
uint16_t hmacs_len = 0;
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t chunks_store[SCTP_PARAM_BUFFER_SIZE];
|
2006-11-03 15:23:16 +00:00
|
|
|
struct sctp_auth_chunk_list *chunks = NULL;
|
|
|
|
uint16_t num_chunks = 0;
|
|
|
|
sctp_key_t *new_key;
|
|
|
|
uint32_t keylen;
|
|
|
|
|
|
|
|
/* convert to upper bound */
|
|
|
|
length += offset;
|
|
|
|
|
|
|
|
phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset,
|
|
|
|
sizeof(struct sctp_paramhdr), (uint8_t *) & tmp_param);
|
|
|
|
while (phdr != NULL) {
|
|
|
|
ptype = ntohs(phdr->param_type);
|
|
|
|
plen = ntohs(phdr->param_length);
|
|
|
|
|
|
|
|
if ((plen == 0) || (offset + plen > length))
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (ptype == SCTP_RANDOM) {
|
2007-02-12 23:24:31 +00:00
|
|
|
if (plen > sizeof(random_store))
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
2007-05-02 12:50:13 +00:00
|
|
|
(struct sctp_paramhdr *)random_store, min(plen, sizeof(random_store)));
|
2006-11-03 15:23:16 +00:00
|
|
|
if (phdr == NULL)
|
|
|
|
return;
|
|
|
|
/* save the random and length for the key */
|
2007-03-31 11:47:30 +00:00
|
|
|
p_random = (struct sctp_auth_random *)phdr;
|
|
|
|
random_len = plen - sizeof(*p_random);
|
2006-11-03 15:23:16 +00:00
|
|
|
} else if (ptype == SCTP_HMAC_LIST) {
|
2014-09-16 14:20:33 +00:00
|
|
|
uint16_t num_hmacs;
|
|
|
|
uint16_t i;
|
2006-11-03 15:23:16 +00:00
|
|
|
|
2007-02-12 23:24:31 +00:00
|
|
|
if (plen > sizeof(hmacs_store))
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
2007-05-02 12:50:13 +00:00
|
|
|
(struct sctp_paramhdr *)hmacs_store, min(plen, sizeof(hmacs_store)));
|
2006-11-03 15:23:16 +00:00
|
|
|
if (phdr == NULL)
|
|
|
|
return;
|
|
|
|
/* save the hmacs list and num for the key */
|
|
|
|
hmacs = (struct sctp_auth_hmac_algo *)phdr;
|
|
|
|
hmacs_len = plen - sizeof(*hmacs);
|
|
|
|
num_hmacs = hmacs_len / sizeof(hmacs->hmac_ids[0]);
|
|
|
|
if (stcb->asoc.local_hmacs != NULL)
|
|
|
|
sctp_free_hmaclist(stcb->asoc.local_hmacs);
|
|
|
|
stcb->asoc.local_hmacs = sctp_alloc_hmaclist(num_hmacs);
|
|
|
|
if (stcb->asoc.local_hmacs != NULL) {
|
|
|
|
for (i = 0; i < num_hmacs; i++) {
|
2007-05-08 14:32:53 +00:00
|
|
|
(void)sctp_auth_add_hmacid(stcb->asoc.local_hmacs,
|
2006-11-03 15:23:16 +00:00
|
|
|
ntohs(hmacs->hmac_ids[i]));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ptype == SCTP_CHUNK_LIST) {
|
|
|
|
int i;
|
|
|
|
|
2007-02-12 23:24:31 +00:00
|
|
|
if (plen > sizeof(chunks_store))
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
2007-05-02 12:50:13 +00:00
|
|
|
(struct sctp_paramhdr *)chunks_store, min(plen, sizeof(chunks_store)));
|
2006-11-03 15:23:16 +00:00
|
|
|
if (phdr == NULL)
|
|
|
|
return;
|
|
|
|
chunks = (struct sctp_auth_chunk_list *)phdr;
|
|
|
|
num_chunks = plen - sizeof(*chunks);
|
|
|
|
/* save chunks list and num for the key */
|
|
|
|
if (stcb->asoc.local_auth_chunks != NULL)
|
|
|
|
sctp_clear_chunklist(stcb->asoc.local_auth_chunks);
|
|
|
|
else
|
|
|
|
stcb->asoc.local_auth_chunks = sctp_alloc_chunklist();
|
|
|
|
for (i = 0; i < num_chunks; i++) {
|
2007-05-08 14:32:53 +00:00
|
|
|
(void)sctp_auth_add_chunk(chunks->chunk_types[i],
|
2006-11-03 15:23:16 +00:00
|
|
|
stcb->asoc.local_auth_chunks);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* get next parameter */
|
|
|
|
offset += SCTP_SIZE32(plen);
|
|
|
|
if (offset + sizeof(struct sctp_paramhdr) > length)
|
|
|
|
break;
|
|
|
|
phdr = (struct sctp_paramhdr *)sctp_m_getptr(m, offset, sizeof(struct sctp_paramhdr),
|
|
|
|
(uint8_t *) & tmp_param);
|
|
|
|
}
|
|
|
|
/* concatenate the full random key */
|
2009-02-03 11:04:03 +00:00
|
|
|
keylen = sizeof(*p_random) + random_len + sizeof(*hmacs) + hmacs_len;
|
|
|
|
if (chunks != NULL) {
|
|
|
|
keylen += sizeof(*chunks) + num_chunks;
|
|
|
|
}
|
2007-02-12 23:24:31 +00:00
|
|
|
new_key = sctp_alloc_key(keylen);
|
|
|
|
if (new_key != NULL) {
|
|
|
|
/* copy in the RANDOM */
|
2007-03-31 11:47:30 +00:00
|
|
|
if (p_random != NULL) {
|
|
|
|
keylen = sizeof(*p_random) + random_len;
|
|
|
|
bcopy(p_random, new_key->key, keylen);
|
2007-02-12 23:24:31 +00:00
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
/* append in the AUTH chunks */
|
2007-02-12 23:24:31 +00:00
|
|
|
if (chunks != NULL) {
|
|
|
|
bcopy(chunks, new_key->key + keylen,
|
|
|
|
sizeof(*chunks) + num_chunks);
|
|
|
|
keylen += sizeof(*chunks) + num_chunks;
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
/* append in the HMACs */
|
2007-02-12 23:24:31 +00:00
|
|
|
if (hmacs != NULL) {
|
|
|
|
bcopy(hmacs, new_key->key + keylen,
|
|
|
|
sizeof(*hmacs) + hmacs_len);
|
|
|
|
}
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
if (stcb->asoc.authinfo.random != NULL)
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.random);
|
|
|
|
stcb->asoc.authinfo.random = new_key;
|
|
|
|
stcb->asoc.authinfo.random_len = random_len;
|
|
|
|
sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.assoc_keyid);
|
|
|
|
sctp_clear_cachedkeys(stcb, stcb->asoc.authinfo.recv_keyid);
|
|
|
|
|
|
|
|
/* negotiate what HMAC to use for the peer */
|
|
|
|
stcb->asoc.peer_hmac_id = sctp_negotiate_hmacid(stcb->asoc.peer_hmacs,
|
|
|
|
stcb->asoc.local_hmacs);
|
2008-12-06 13:19:54 +00:00
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* copy defaults from the endpoint */
|
|
|
|
/* FIX ME: put in cookie? */
|
2008-12-06 13:19:54 +00:00
|
|
|
stcb->asoc.authinfo.active_keyid = stcb->sctp_ep->sctp_ep.default_keyid;
|
|
|
|
/* copy out the shared key list (by reference) from the endpoint */
|
|
|
|
(void)sctp_copy_skeylist(&stcb->sctp_ep->sctp_ep.shared_keys,
|
|
|
|
&stcb->asoc.shared_keys);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* compute and fill in the HMAC digest for a packet
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sctp_fill_hmac_digest_m(struct mbuf *m, uint32_t auth_offset,
|
2008-12-06 13:19:54 +00:00
|
|
|
struct sctp_auth_chunk *auth, struct sctp_tcb *stcb, uint16_t keyid)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
uint32_t digestlen;
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
sctp_key_t *key;
|
|
|
|
|
|
|
|
if ((stcb == NULL) || (auth == NULL))
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* zero the digest + chunk padding */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(stcb->asoc.peer_hmac_id);
|
|
|
|
bzero(auth->hmac, SCTP_SIZE32(digestlen));
|
2008-12-06 13:19:54 +00:00
|
|
|
|
|
|
|
/* is the desired key cached? */
|
|
|
|
if ((keyid != stcb->asoc.authinfo.assoc_keyid) ||
|
|
|
|
(stcb->asoc.authinfo.assoc_key == NULL)) {
|
|
|
|
if (stcb->asoc.authinfo.assoc_key != NULL) {
|
|
|
|
/* free the old cached key */
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.assoc_key);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
2008-12-06 13:19:54 +00:00
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys, keyid);
|
2006-11-03 15:23:16 +00:00
|
|
|
/* the only way skey is NULL is if null key id 0 is used */
|
|
|
|
if (skey != NULL)
|
|
|
|
key = skey->key;
|
|
|
|
else
|
|
|
|
key = NULL;
|
|
|
|
/* compute a new assoc key and cache it */
|
|
|
|
stcb->asoc.authinfo.assoc_key =
|
|
|
|
sctp_compute_hashkey(stcb->asoc.authinfo.random,
|
|
|
|
stcb->asoc.authinfo.peer_random, key);
|
2008-12-06 13:19:54 +00:00
|
|
|
stcb->asoc.authinfo.assoc_keyid = keyid;
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1, "caching key id %u\n",
|
|
|
|
stcb->asoc.authinfo.assoc_keyid);
|
2006-11-03 15:23:16 +00:00
|
|
|
#ifdef SCTP_DEBUG
|
2007-05-09 13:30:06 +00:00
|
|
|
if (SCTP_AUTH_DEBUG)
|
|
|
|
sctp_print_key(stcb->asoc.authinfo.assoc_key,
|
|
|
|
"Assoc Key");
|
2006-11-03 15:23:16 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* set in the active key id */
|
2008-12-06 13:19:54 +00:00
|
|
|
auth->shared_key_id = htons(keyid);
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* compute and fill in the digest */
|
2008-12-06 13:19:54 +00:00
|
|
|
(void)sctp_compute_hmac_m(stcb->asoc.peer_hmac_id, stcb->asoc.authinfo.assoc_key,
|
2006-11-03 15:23:16 +00:00
|
|
|
m, auth_offset, auth->hmac);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
sctp_bzero_m(struct mbuf *m, uint32_t m_offset, uint32_t size)
|
|
|
|
{
|
|
|
|
struct mbuf *m_tmp;
|
|
|
|
uint8_t *data;
|
|
|
|
|
|
|
|
/* sanity check */
|
|
|
|
if (m == NULL)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* find the correct starting mbuf and offset (get start position) */
|
|
|
|
m_tmp = m;
|
2006-12-29 20:21:42 +00:00
|
|
|
while ((m_tmp != NULL) && (m_offset >= (uint32_t) SCTP_BUF_LEN(m_tmp))) {
|
|
|
|
m_offset -= SCTP_BUF_LEN(m_tmp);
|
|
|
|
m_tmp = SCTP_BUF_NEXT(m_tmp);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
/* now use the rest of the mbuf chain */
|
|
|
|
while ((m_tmp != NULL) && (size > 0)) {
|
|
|
|
data = mtod(m_tmp, uint8_t *) + m_offset;
|
2006-12-29 20:21:42 +00:00
|
|
|
if (size > (uint32_t) SCTP_BUF_LEN(m_tmp)) {
|
|
|
|
bzero(data, SCTP_BUF_LEN(m_tmp));
|
|
|
|
size -= SCTP_BUF_LEN(m_tmp);
|
2006-11-03 15:23:16 +00:00
|
|
|
} else {
|
|
|
|
bzero(data, size);
|
|
|
|
size = 0;
|
|
|
|
}
|
|
|
|
/* clear the offset since it's only for the first mbuf */
|
|
|
|
m_offset = 0;
|
2006-12-29 20:21:42 +00:00
|
|
|
m_tmp = SCTP_BUF_NEXT(m_tmp);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
|
|
|
* process the incoming Authentication chunk
|
|
|
|
* return codes:
|
|
|
|
* -1 on any authentication error
|
|
|
|
* 0 on authentication verification
|
2006-11-03 15:23:16 +00:00
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_handle_auth(struct sctp_tcb *stcb, struct sctp_auth_chunk *auth,
|
|
|
|
struct mbuf *m, uint32_t offset)
|
|
|
|
{
|
|
|
|
uint16_t chunklen;
|
|
|
|
uint16_t shared_key_id;
|
|
|
|
uint16_t hmac_id;
|
|
|
|
sctp_sharedkey_t *skey;
|
|
|
|
uint32_t digestlen;
|
|
|
|
uint8_t digest[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
uint8_t computed_digest[SCTP_AUTH_DIGEST_LEN_MAX];
|
|
|
|
|
|
|
|
/* auth is checked for NULL by caller */
|
|
|
|
chunklen = ntohs(auth->ch.chunk_length);
|
|
|
|
if (chunklen < sizeof(*auth)) {
|
|
|
|
SCTP_STAT_INCR(sctps_recvauthfailed);
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
SCTP_STAT_INCR(sctps_recvauth);
|
|
|
|
|
|
|
|
/* get the auth params */
|
|
|
|
shared_key_id = ntohs(auth->shared_key_id);
|
|
|
|
hmac_id = ntohs(auth->hmac_id);
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP AUTH Chunk: shared key %u, HMAC id %u\n",
|
|
|
|
shared_key_id, hmac_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* is the indicated HMAC supported? */
|
|
|
|
if (!sctp_auth_is_supported_hmac(stcb->asoc.local_hmacs, hmac_id)) {
|
2015-09-12 17:08:51 +00:00
|
|
|
struct mbuf *op_err;
|
|
|
|
struct sctp_error_auth_invalid_hmac *cause;
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
SCTP_STAT_INCR(sctps_recvivalhmacid);
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP Auth: unsupported HMAC id %u\n",
|
|
|
|
hmac_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
/*
|
|
|
|
* report this in an Error Chunk: Unsupported HMAC
|
|
|
|
* Identifier
|
|
|
|
*/
|
2015-09-12 17:08:51 +00:00
|
|
|
op_err = sctp_get_mbuf_for_msg(sizeof(struct sctp_error_auth_invalid_hmac),
|
|
|
|
0, M_NOWAIT, 1, MT_HEADER);
|
|
|
|
if (op_err != NULL) {
|
2006-11-03 15:23:16 +00:00
|
|
|
/* pre-reserve some space */
|
2015-09-12 17:08:51 +00:00
|
|
|
SCTP_BUF_RESV_UF(op_err, sizeof(struct sctp_chunkhdr));
|
2006-11-03 15:23:16 +00:00
|
|
|
/* fill in the error */
|
2015-09-12 17:08:51 +00:00
|
|
|
cause = mtod(op_err, struct sctp_error_auth_invalid_hmac *);
|
|
|
|
cause->cause.code = htons(SCTP_CAUSE_UNSUPPORTED_HMACID);
|
|
|
|
cause->cause.length = htons(sizeof(struct sctp_error_auth_invalid_hmac));
|
|
|
|
cause->hmac_id = ntohs(hmac_id);
|
|
|
|
SCTP_BUF_LEN(op_err) = sizeof(struct sctp_error_auth_invalid_hmac);
|
2006-11-03 15:23:16 +00:00
|
|
|
/* queue it */
|
2015-09-12 17:08:51 +00:00
|
|
|
sctp_queue_op_err(stcb, op_err);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* get the indicated shared key, if available */
|
|
|
|
if ((stcb->asoc.authinfo.recv_key == NULL) ||
|
|
|
|
(stcb->asoc.authinfo.recv_keyid != shared_key_id)) {
|
|
|
|
/* find the shared key on the assoc first */
|
2008-12-06 13:19:54 +00:00
|
|
|
skey = sctp_find_sharedkey(&stcb->asoc.shared_keys,
|
|
|
|
shared_key_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
/* if the shared key isn't found, discard the chunk */
|
|
|
|
if (skey == NULL) {
|
|
|
|
SCTP_STAT_INCR(sctps_recvivalkeyid);
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP Auth: unknown key id %u\n",
|
|
|
|
shared_key_id);
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* generate a notification if this is a new key id */
|
|
|
|
if (stcb->asoc.authinfo.recv_keyid != shared_key_id)
|
|
|
|
/*
|
|
|
|
* sctp_ulp_notify(SCTP_NOTIFY_AUTH_NEW_KEY, stcb,
|
|
|
|
* shared_key_id, (void
|
|
|
|
* *)stcb->asoc.authinfo.recv_keyid);
|
|
|
|
*/
|
2012-04-26 11:07:15 +00:00
|
|
|
sctp_notify_authentication(stcb, SCTP_AUTH_NEW_KEY,
|
2008-12-06 13:19:54 +00:00
|
|
|
shared_key_id, stcb->asoc.authinfo.recv_keyid,
|
|
|
|
SCTP_SO_NOT_LOCKED);
|
2006-11-03 15:23:16 +00:00
|
|
|
/* compute a new recv assoc key and cache it */
|
|
|
|
if (stcb->asoc.authinfo.recv_key != NULL)
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.recv_key);
|
|
|
|
stcb->asoc.authinfo.recv_key =
|
|
|
|
sctp_compute_hashkey(stcb->asoc.authinfo.random,
|
|
|
|
stcb->asoc.authinfo.peer_random, skey->key);
|
|
|
|
stcb->asoc.authinfo.recv_keyid = shared_key_id;
|
|
|
|
#ifdef SCTP_DEBUG
|
|
|
|
if (SCTP_AUTH_DEBUG)
|
|
|
|
sctp_print_key(stcb->asoc.authinfo.recv_key, "Recv Key");
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
/* validate the digest length */
|
|
|
|
digestlen = sctp_get_hmac_digest_len(hmac_id);
|
|
|
|
if (chunklen < (sizeof(*auth) + digestlen)) {
|
|
|
|
/* invalid digest length */
|
|
|
|
SCTP_STAT_INCR(sctps_recvauthfailed);
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP Auth: chunk too short for HMAC\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
/* save a copy of the digest, zero the pseudo header, and validate */
|
|
|
|
bcopy(auth->hmac, digest, digestlen);
|
|
|
|
sctp_bzero_m(m, offset + sizeof(*auth), SCTP_SIZE32(digestlen));
|
|
|
|
(void)sctp_compute_hmac_m(hmac_id, stcb->asoc.authinfo.recv_key,
|
|
|
|
m, offset, computed_digest);
|
|
|
|
|
|
|
|
/* compare the computed digest with the one in the AUTH chunk */
|
|
|
|
if (memcmp(digest, computed_digest, digestlen) != 0) {
|
|
|
|
SCTP_STAT_INCR(sctps_recvauthfailed);
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP Auth: HMAC digest check failed\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Generate NOTIFICATION
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
sctp_notify_authentication(struct sctp_tcb *stcb, uint32_t indication,
|
2008-12-06 13:19:54 +00:00
|
|
|
uint16_t keyid, uint16_t alt_keyid, int so_locked
|
|
|
|
#if !defined(__APPLE__) && !defined(SCTP_SO_LOCK_TESTING)
|
|
|
|
SCTP_UNUSED
|
|
|
|
#endif
|
|
|
|
)
|
2006-11-03 15:23:16 +00:00
|
|
|
{
|
|
|
|
struct mbuf *m_notify;
|
|
|
|
struct sctp_authkey_event *auth;
|
|
|
|
struct sctp_queued_to_read *control;
|
|
|
|
|
2008-01-28 10:31:12 +00:00
|
|
|
if ((stcb == NULL) ||
|
|
|
|
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_GONE) ||
|
|
|
|
(stcb->sctp_ep->sctp_flags & SCTP_PCB_FLAGS_SOCKET_ALLGONE) ||
|
|
|
|
(stcb->asoc.state & SCTP_STATE_CLOSED_SOCKET)
|
|
|
|
) {
|
|
|
|
/* If the socket is gone we are out of here */
|
|
|
|
return;
|
|
|
|
}
|
2011-06-15 23:50:27 +00:00
|
|
|
if (sctp_stcb_is_feature_off(stcb->sctp_ep, stcb, SCTP_PCB_FLAGS_AUTHEVNT))
|
2006-11-03 15:23:16 +00:00
|
|
|
/* event not enabled */
|
|
|
|
return;
|
|
|
|
|
|
|
|
m_notify = sctp_get_mbuf_for_msg(sizeof(struct sctp_authkey_event),
|
2012-12-05 08:04:20 +00:00
|
|
|
0, M_NOWAIT, 1, MT_HEADER);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (m_notify == NULL)
|
|
|
|
/* no space left */
|
|
|
|
return;
|
2006-12-29 20:21:42 +00:00
|
|
|
|
|
|
|
SCTP_BUF_LEN(m_notify) = 0;
|
2006-11-03 15:23:16 +00:00
|
|
|
auth = mtod(m_notify, struct sctp_authkey_event *);
|
2014-07-08 21:54:27 +00:00
|
|
|
memset(auth, 0, sizeof(struct sctp_authkey_event));
|
2006-11-03 15:23:16 +00:00
|
|
|
auth->auth_type = SCTP_AUTHENTICATION_EVENT;
|
|
|
|
auth->auth_flags = 0;
|
|
|
|
auth->auth_length = sizeof(*auth);
|
|
|
|
auth->auth_keynumber = keyid;
|
|
|
|
auth->auth_altkeynumber = alt_keyid;
|
|
|
|
auth->auth_indication = indication;
|
|
|
|
auth->auth_assoc_id = sctp_get_associd(stcb);
|
|
|
|
|
2006-12-29 20:21:42 +00:00
|
|
|
SCTP_BUF_LEN(m_notify) = sizeof(*auth);
|
|
|
|
SCTP_BUF_NEXT(m_notify) = NULL;
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* append to socket */
|
|
|
|
control = sctp_build_readq_entry(stcb, stcb->asoc.primary_destination,
|
2011-12-17 19:21:40 +00:00
|
|
|
0, 0, stcb->asoc.context, 0, 0, 0, m_notify);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (control == NULL) {
|
|
|
|
/* no memory */
|
|
|
|
sctp_m_freem(m_notify);
|
|
|
|
return;
|
|
|
|
}
|
2006-12-29 20:21:42 +00:00
|
|
|
control->spec_flags = M_NOTIFICATION;
|
|
|
|
control->length = SCTP_BUF_LEN(m_notify);
|
2006-11-03 15:23:16 +00:00
|
|
|
/* not that we need this */
|
|
|
|
control->tail_mbuf = m_notify;
|
|
|
|
sctp_add_to_readq(stcb->sctp_ep, stcb, control,
|
2009-07-28 14:09:06 +00:00
|
|
|
&stcb->sctp_socket->so_rcv, 1, SCTP_READ_LOCK_NOT_HELD, so_locked);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2008-12-06 13:19:54 +00:00
|
|
|
/*-
|
2006-11-03 15:23:16 +00:00
|
|
|
* validates the AUTHentication related parameters in an INIT/INIT-ACK
|
|
|
|
* Note: currently only used for INIT as INIT-ACK is handled inline
|
|
|
|
* with sctp_load_addresses_from_init()
|
|
|
|
*/
|
|
|
|
int
|
|
|
|
sctp_validate_init_auth_params(struct mbuf *m, int offset, int limit)
|
|
|
|
{
|
|
|
|
struct sctp_paramhdr *phdr, parm_buf;
|
|
|
|
uint16_t ptype, plen;
|
|
|
|
int peer_supports_asconf = 0;
|
|
|
|
int peer_supports_auth = 0;
|
2007-03-31 11:47:30 +00:00
|
|
|
int got_random = 0, got_hmacs = 0, got_chklist = 0;
|
2007-08-27 05:19:48 +00:00
|
|
|
uint8_t saw_asconf = 0;
|
|
|
|
uint8_t saw_asconf_ack = 0;
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* go through each of the params. */
|
|
|
|
phdr = sctp_get_next_param(m, offset, &parm_buf, sizeof(parm_buf));
|
|
|
|
while (phdr) {
|
|
|
|
ptype = ntohs(phdr->param_type);
|
|
|
|
plen = ntohs(phdr->param_length);
|
|
|
|
|
|
|
|
if (offset + plen > limit) {
|
|
|
|
break;
|
|
|
|
}
|
2007-08-27 05:19:48 +00:00
|
|
|
if (plen < sizeof(struct sctp_paramhdr)) {
|
2006-11-03 15:23:16 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (ptype == SCTP_SUPPORTED_CHUNK_EXT) {
|
|
|
|
/* A supported extension chunk */
|
|
|
|
struct sctp_supported_chunk_types_param *pr_supported;
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t local_store[SCTP_PARAM_BUFFER_SIZE];
|
2006-11-03 15:23:16 +00:00
|
|
|
int num_ent, i;
|
|
|
|
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
2007-05-02 12:50:13 +00:00
|
|
|
(struct sctp_paramhdr *)&local_store, min(plen, sizeof(local_store)));
|
2006-11-03 15:23:16 +00:00
|
|
|
if (phdr == NULL) {
|
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
pr_supported = (struct sctp_supported_chunk_types_param *)phdr;
|
|
|
|
num_ent = plen - sizeof(struct sctp_paramhdr);
|
|
|
|
for (i = 0; i < num_ent; i++) {
|
|
|
|
switch (pr_supported->chunk_types[i]) {
|
|
|
|
case SCTP_ASCONF:
|
|
|
|
case SCTP_ASCONF_ACK:
|
|
|
|
peer_supports_asconf = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
/* one we don't care about */
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (ptype == SCTP_RANDOM) {
|
|
|
|
got_random = 1;
|
|
|
|
/* enforce the random length */
|
|
|
|
if (plen != (sizeof(struct sctp_auth_random) +
|
|
|
|
SCTP_AUTH_RANDOM_SIZE_REQUIRED)) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: invalid RANDOM len\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
} else if (ptype == SCTP_HMAC_LIST) {
|
2007-02-12 23:24:31 +00:00
|
|
|
uint8_t store[SCTP_PARAM_BUFFER_SIZE];
|
2006-11-03 15:23:16 +00:00
|
|
|
struct sctp_auth_hmac_algo *hmacs;
|
|
|
|
int num_hmacs;
|
|
|
|
|
|
|
|
if (plen > sizeof(store))
|
|
|
|
break;
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
2007-05-02 12:50:13 +00:00
|
|
|
(struct sctp_paramhdr *)store, min(plen, sizeof(store)));
|
2006-11-03 15:23:16 +00:00
|
|
|
if (phdr == NULL)
|
|
|
|
return (-1);
|
|
|
|
hmacs = (struct sctp_auth_hmac_algo *)phdr;
|
|
|
|
num_hmacs = (plen - sizeof(*hmacs)) /
|
|
|
|
sizeof(hmacs->hmac_ids[0]);
|
|
|
|
/* validate the hmac list */
|
|
|
|
if (sctp_verify_hmac_param(hmacs, num_hmacs)) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: invalid HMAC param\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
|
|
|
got_hmacs = 1;
|
2007-03-31 11:47:30 +00:00
|
|
|
} else if (ptype == SCTP_CHUNK_LIST) {
|
2007-08-27 05:19:48 +00:00
|
|
|
int i, num_chunks;
|
|
|
|
uint8_t chunks_store[SCTP_SMALL_CHUNK_STORE];
|
|
|
|
|
2007-03-31 11:47:30 +00:00
|
|
|
/* did the peer send a non-empty chunk list? */
|
2007-08-27 05:19:48 +00:00
|
|
|
struct sctp_auth_chunk_list *chunks = NULL;
|
|
|
|
|
|
|
|
phdr = sctp_get_next_param(m, offset,
|
|
|
|
(struct sctp_paramhdr *)chunks_store,
|
|
|
|
min(plen, sizeof(chunks_store)));
|
|
|
|
if (phdr == NULL)
|
|
|
|
return (-1);
|
|
|
|
|
|
|
|
/*-
|
|
|
|
* Flip through the list and mark that the
|
|
|
|
* peer supports asconf/asconf_ack.
|
|
|
|
*/
|
|
|
|
chunks = (struct sctp_auth_chunk_list *)phdr;
|
|
|
|
num_chunks = plen - sizeof(*chunks);
|
|
|
|
for (i = 0; i < num_chunks; i++) {
|
|
|
|
/* record asconf/asconf-ack if listed */
|
|
|
|
if (chunks->chunk_types[i] == SCTP_ASCONF)
|
|
|
|
saw_asconf = 1;
|
|
|
|
if (chunks->chunk_types[i] == SCTP_ASCONF_ACK)
|
|
|
|
saw_asconf_ack = 1;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (num_chunks)
|
2007-03-31 11:47:30 +00:00
|
|
|
got_chklist = 1;
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
offset += SCTP_SIZE32(plen);
|
|
|
|
if (offset >= limit) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
phdr = sctp_get_next_param(m, offset, &parm_buf,
|
|
|
|
sizeof(parm_buf));
|
|
|
|
}
|
|
|
|
/* validate authentication required parameters */
|
|
|
|
if (got_random && got_hmacs) {
|
|
|
|
peer_supports_auth = 1;
|
|
|
|
} else {
|
|
|
|
peer_supports_auth = 0;
|
|
|
|
}
|
2007-03-31 11:47:30 +00:00
|
|
|
if (!peer_supports_auth && got_chklist) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: peer sent chunk list w/o AUTH\n");
|
2007-03-31 11:47:30 +00:00
|
|
|
return (-1);
|
|
|
|
}
|
2014-08-01 20:49:27 +00:00
|
|
|
if (peer_supports_asconf && !peer_supports_auth) {
|
2007-05-09 13:30:06 +00:00
|
|
|
SCTPDBG(SCTP_DEBUG_AUTH1,
|
|
|
|
"SCTP: peer supports ASCONF but not AUTH\n");
|
2006-11-03 15:23:16 +00:00
|
|
|
return (-1);
|
2007-08-27 05:19:48 +00:00
|
|
|
} else if ((peer_supports_asconf) && (peer_supports_auth) &&
|
|
|
|
((saw_asconf == 0) || (saw_asconf_ack == 0))) {
|
|
|
|
return (-2);
|
2006-11-03 15:23:16 +00:00
|
|
|
}
|
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
sctp_initialize_auth_params(struct sctp_inpcb *inp, struct sctp_tcb *stcb)
|
|
|
|
{
|
|
|
|
uint16_t chunks_len = 0;
|
|
|
|
uint16_t hmacs_len = 0;
|
2007-03-15 11:27:14 +00:00
|
|
|
uint16_t random_len = SCTP_AUTH_RANDOM_SIZE_DEFAULT;
|
2006-11-03 15:23:16 +00:00
|
|
|
sctp_key_t *new_key;
|
|
|
|
uint16_t keylen;
|
|
|
|
|
|
|
|
/* initialize hmac list from endpoint */
|
|
|
|
stcb->asoc.local_hmacs = sctp_copy_hmaclist(inp->sctp_ep.local_hmacs);
|
|
|
|
if (stcb->asoc.local_hmacs != NULL) {
|
|
|
|
hmacs_len = stcb->asoc.local_hmacs->num_algo *
|
|
|
|
sizeof(stcb->asoc.local_hmacs->hmac[0]);
|
|
|
|
}
|
|
|
|
/* initialize auth chunks list from endpoint */
|
|
|
|
stcb->asoc.local_auth_chunks =
|
|
|
|
sctp_copy_chunklist(inp->sctp_ep.local_auth_chunks);
|
|
|
|
if (stcb->asoc.local_auth_chunks != NULL) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (stcb->asoc.local_auth_chunks->chunks[i])
|
|
|
|
chunks_len++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* copy defaults from the endpoint */
|
2008-12-06 13:19:54 +00:00
|
|
|
stcb->asoc.authinfo.active_keyid = inp->sctp_ep.default_keyid;
|
|
|
|
|
|
|
|
/* copy out the shared key list (by reference) from the endpoint */
|
|
|
|
(void)sctp_copy_skeylist(&inp->sctp_ep.shared_keys,
|
|
|
|
&stcb->asoc.shared_keys);
|
2006-11-03 15:23:16 +00:00
|
|
|
|
|
|
|
/* now set the concatenated key (random + chunks + hmacs) */
|
2007-02-12 23:24:31 +00:00
|
|
|
/* key includes parameter headers */
|
|
|
|
keylen = (3 * sizeof(struct sctp_paramhdr)) + random_len + chunks_len +
|
|
|
|
hmacs_len;
|
2006-11-03 15:23:16 +00:00
|
|
|
new_key = sctp_alloc_key(keylen);
|
|
|
|
if (new_key != NULL) {
|
2007-02-12 23:24:31 +00:00
|
|
|
struct sctp_paramhdr *ph;
|
|
|
|
int plen;
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* generate and copy in the RANDOM */
|
2007-02-12 23:24:31 +00:00
|
|
|
ph = (struct sctp_paramhdr *)new_key->key;
|
|
|
|
ph->param_type = htons(SCTP_RANDOM);
|
|
|
|
plen = sizeof(*ph) + random_len;
|
|
|
|
ph->param_length = htons(plen);
|
|
|
|
SCTP_READ_RANDOM(new_key->key + sizeof(*ph), random_len);
|
|
|
|
keylen = plen;
|
|
|
|
|
2006-11-03 15:23:16 +00:00
|
|
|
/* append in the AUTH chunks */
|
2007-02-12 23:24:31 +00:00
|
|
|
/* NOTE: currently we always have chunks to list */
|
|
|
|
ph = (struct sctp_paramhdr *)(new_key->key + keylen);
|
|
|
|
ph->param_type = htons(SCTP_CHUNK_LIST);
|
|
|
|
plen = sizeof(*ph) + chunks_len;
|
|
|
|
ph->param_length = htons(plen);
|
|
|
|
keylen += sizeof(*ph);
|
2006-11-03 15:23:16 +00:00
|
|
|
if (stcb->asoc.local_auth_chunks) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
for (i = 0; i < 256; i++) {
|
|
|
|
if (stcb->asoc.local_auth_chunks->chunks[i])
|
|
|
|
new_key->key[keylen++] = i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* append in the HMACs */
|
2007-02-12 23:24:31 +00:00
|
|
|
ph = (struct sctp_paramhdr *)(new_key->key + keylen);
|
|
|
|
ph->param_type = htons(SCTP_HMAC_LIST);
|
|
|
|
plen = sizeof(*ph) + hmacs_len;
|
|
|
|
ph->param_length = htons(plen);
|
|
|
|
keylen += sizeof(*ph);
|
2007-05-09 13:30:06 +00:00
|
|
|
(void)sctp_serialize_hmaclist(stcb->asoc.local_hmacs,
|
2006-11-03 15:23:16 +00:00
|
|
|
new_key->key + keylen);
|
|
|
|
}
|
|
|
|
if (stcb->asoc.authinfo.random != NULL)
|
|
|
|
sctp_free_key(stcb->asoc.authinfo.random);
|
|
|
|
stcb->asoc.authinfo.random = new_key;
|
|
|
|
stcb->asoc.authinfo.random_len = random_len;
|
|
|
|
}
|