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