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