1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* SCTP kernel implementation 3 * (C) Copyright 2007 Hewlett-Packard Development Company, L.P. 4 * 5 * This file is part of the SCTP kernel implementation 6 * 7 * Please send any bug reports or fixes you make to the 8 * email address(es): 9 * lksctp developers <linux-sctp@vger.kernel.org> 10 * 11 * Written or modified by: 12 * Vlad Yasevich <vladislav.yasevich@hp.com> 13 */ 14 15 #include <crypto/sha1.h> 16 #include <crypto/sha2.h> 17 #include <linux/slab.h> 18 #include <linux/types.h> 19 #include <net/sctp/sctp.h> 20 #include <net/sctp/auth.h> 21 22 static const struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = { 23 { 24 /* id 0 is reserved. as all 0 */ 25 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0, 26 }, 27 { 28 .hmac_id = SCTP_AUTH_HMAC_ID_SHA1, 29 .hmac_len = SHA1_DIGEST_SIZE, 30 }, 31 { 32 /* id 2 is reserved as well */ 33 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2, 34 }, 35 { 36 .hmac_id = SCTP_AUTH_HMAC_ID_SHA256, 37 .hmac_len = SHA256_DIGEST_SIZE, 38 } 39 }; 40 41 static bool sctp_hmac_supported(__u16 hmac_id) 42 { 43 return hmac_id < ARRAY_SIZE(sctp_hmac_list) && 44 sctp_hmac_list[hmac_id].hmac_len != 0; 45 } 46 47 void sctp_auth_key_put(struct sctp_auth_bytes *key) 48 { 49 if (!key) 50 return; 51 52 if (refcount_dec_and_test(&key->refcnt)) { 53 kfree_sensitive(key); 54 SCTP_DBG_OBJCNT_DEC(keys); 55 } 56 } 57 58 /* Create a new key structure of a given length */ 59 static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp) 60 { 61 struct sctp_auth_bytes *key; 62 63 /* Verify that we are not going to overflow INT_MAX */ 64 if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes))) 65 return NULL; 66 67 /* Allocate the shared key */ 68 key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp); 69 if (!key) 70 return NULL; 71 72 key->len = key_len; 73 refcount_set(&key->refcnt, 1); 74 SCTP_DBG_OBJCNT_INC(keys); 75 76 return key; 77 } 78 79 /* Create a new shared key container with a give key id */ 80 struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp) 81 { 82 struct sctp_shared_key *new; 83 84 /* Allocate the shared key container */ 85 new = kzalloc_obj(struct sctp_shared_key, gfp); 86 if (!new) 87 return NULL; 88 89 INIT_LIST_HEAD(&new->key_list); 90 refcount_set(&new->refcnt, 1); 91 new->key_id = key_id; 92 93 return new; 94 } 95 96 /* Free the shared key structure */ 97 static void sctp_auth_shkey_destroy(struct sctp_shared_key *sh_key) 98 { 99 BUG_ON(!list_empty(&sh_key->key_list)); 100 sctp_auth_key_put(sh_key->key); 101 sh_key->key = NULL; 102 kfree(sh_key); 103 } 104 105 void sctp_auth_shkey_release(struct sctp_shared_key *sh_key) 106 { 107 if (refcount_dec_and_test(&sh_key->refcnt)) 108 sctp_auth_shkey_destroy(sh_key); 109 } 110 111 void sctp_auth_shkey_hold(struct sctp_shared_key *sh_key) 112 { 113 refcount_inc(&sh_key->refcnt); 114 } 115 116 /* Destroy the entire key list. This is done during the 117 * associon and endpoint free process. 118 */ 119 void sctp_auth_destroy_keys(struct list_head *keys) 120 { 121 struct sctp_shared_key *ep_key; 122 struct sctp_shared_key *tmp; 123 124 if (list_empty(keys)) 125 return; 126 127 key_for_each_safe(ep_key, tmp, keys) { 128 list_del_init(&ep_key->key_list); 129 sctp_auth_shkey_release(ep_key); 130 } 131 } 132 133 /* Compare two byte vectors as numbers. Return values 134 * are: 135 * 0 - vectors are equal 136 * < 0 - vector 1 is smaller than vector2 137 * > 0 - vector 1 is greater than vector2 138 * 139 * Algorithm is: 140 * This is performed by selecting the numerically smaller key vector... 141 * If the key vectors are equal as numbers but differ in length ... 142 * the shorter vector is considered smaller 143 * 144 * Examples (with small values): 145 * 000123456789 > 123456789 (first number is longer) 146 * 000123456789 < 234567891 (second number is larger numerically) 147 * 123456789 > 2345678 (first number is both larger & longer) 148 */ 149 static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1, 150 struct sctp_auth_bytes *vector2) 151 { 152 int diff; 153 int i; 154 const __u8 *longer; 155 156 diff = vector1->len - vector2->len; 157 if (diff) { 158 longer = (diff > 0) ? vector1->data : vector2->data; 159 160 /* Check to see if the longer number is 161 * lead-zero padded. If it is not, it 162 * is automatically larger numerically. 163 */ 164 for (i = 0; i < abs(diff); i++) { 165 if (longer[i] != 0) 166 return diff; 167 } 168 } 169 170 /* lengths are the same, compare numbers */ 171 return memcmp(vector1->data, vector2->data, vector1->len); 172 } 173 174 /* 175 * Create a key vector as described in SCTP-AUTH, Section 6.1 176 * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO 177 * parameter sent by each endpoint are concatenated as byte vectors. 178 * These parameters include the parameter type, parameter length, and 179 * the parameter value, but padding is omitted; all padding MUST be 180 * removed from this concatenation before proceeding with further 181 * computation of keys. Parameters which were not sent are simply 182 * omitted from the concatenation process. The resulting two vectors 183 * are called the two key vectors. 184 */ 185 static struct sctp_auth_bytes *sctp_auth_make_key_vector( 186 struct sctp_random_param *random, 187 struct sctp_chunks_param *chunks, 188 struct sctp_hmac_algo_param *hmacs, 189 gfp_t gfp) 190 { 191 struct sctp_auth_bytes *new; 192 __u32 len; 193 __u32 offset = 0; 194 __u16 random_len, hmacs_len, chunks_len = 0; 195 196 random_len = ntohs(random->param_hdr.length); 197 hmacs_len = ntohs(hmacs->param_hdr.length); 198 if (chunks) 199 chunks_len = ntohs(chunks->param_hdr.length); 200 201 len = random_len + hmacs_len + chunks_len; 202 203 new = sctp_auth_create_key(len, gfp); 204 if (!new) 205 return NULL; 206 207 memcpy(new->data, random, random_len); 208 offset += random_len; 209 210 if (chunks) { 211 memcpy(new->data + offset, chunks, chunks_len); 212 offset += chunks_len; 213 } 214 215 memcpy(new->data + offset, hmacs, hmacs_len); 216 217 return new; 218 } 219 220 221 /* Make a key vector based on our local parameters */ 222 static struct sctp_auth_bytes *sctp_auth_make_local_vector( 223 const struct sctp_association *asoc, 224 gfp_t gfp) 225 { 226 return sctp_auth_make_key_vector( 227 (struct sctp_random_param *)asoc->c.auth_random, 228 (struct sctp_chunks_param *)asoc->c.auth_chunks, 229 (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs, gfp); 230 } 231 232 /* Make a key vector based on peer's parameters */ 233 static struct sctp_auth_bytes *sctp_auth_make_peer_vector( 234 const struct sctp_association *asoc, 235 gfp_t gfp) 236 { 237 return sctp_auth_make_key_vector(asoc->peer.peer_random, 238 asoc->peer.peer_chunks, 239 asoc->peer.peer_hmacs, 240 gfp); 241 } 242 243 244 /* Set the value of the association shared key base on the parameters 245 * given. The algorithm is: 246 * From the endpoint pair shared keys and the key vectors the 247 * association shared keys are computed. This is performed by selecting 248 * the numerically smaller key vector and concatenating it to the 249 * endpoint pair shared key, and then concatenating the numerically 250 * larger key vector to that. The result of the concatenation is the 251 * association shared key. 252 */ 253 static struct sctp_auth_bytes *sctp_auth_asoc_set_secret( 254 struct sctp_shared_key *ep_key, 255 struct sctp_auth_bytes *first_vector, 256 struct sctp_auth_bytes *last_vector, 257 gfp_t gfp) 258 { 259 struct sctp_auth_bytes *secret; 260 __u32 offset = 0; 261 __u32 auth_len; 262 263 auth_len = first_vector->len + last_vector->len; 264 if (ep_key->key) 265 auth_len += ep_key->key->len; 266 267 secret = sctp_auth_create_key(auth_len, gfp); 268 if (!secret) 269 return NULL; 270 271 if (ep_key->key) { 272 memcpy(secret->data, ep_key->key->data, ep_key->key->len); 273 offset += ep_key->key->len; 274 } 275 276 memcpy(secret->data + offset, first_vector->data, first_vector->len); 277 offset += first_vector->len; 278 279 memcpy(secret->data + offset, last_vector->data, last_vector->len); 280 281 return secret; 282 } 283 284 /* Create an association shared key. Follow the algorithm 285 * described in SCTP-AUTH, Section 6.1 286 */ 287 static struct sctp_auth_bytes *sctp_auth_asoc_create_secret( 288 const struct sctp_association *asoc, 289 struct sctp_shared_key *ep_key, 290 gfp_t gfp) 291 { 292 struct sctp_auth_bytes *local_key_vector; 293 struct sctp_auth_bytes *peer_key_vector; 294 struct sctp_auth_bytes *first_vector, 295 *last_vector; 296 struct sctp_auth_bytes *secret = NULL; 297 int cmp; 298 299 300 /* Now we need to build the key vectors 301 * SCTP-AUTH , Section 6.1 302 * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO 303 * parameter sent by each endpoint are concatenated as byte vectors. 304 * These parameters include the parameter type, parameter length, and 305 * the parameter value, but padding is omitted; all padding MUST be 306 * removed from this concatenation before proceeding with further 307 * computation of keys. Parameters which were not sent are simply 308 * omitted from the concatenation process. The resulting two vectors 309 * are called the two key vectors. 310 */ 311 312 local_key_vector = sctp_auth_make_local_vector(asoc, gfp); 313 peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp); 314 315 if (!peer_key_vector || !local_key_vector) 316 goto out; 317 318 /* Figure out the order in which the key_vectors will be 319 * added to the endpoint shared key. 320 * SCTP-AUTH, Section 6.1: 321 * This is performed by selecting the numerically smaller key 322 * vector and concatenating it to the endpoint pair shared 323 * key, and then concatenating the numerically larger key 324 * vector to that. If the key vectors are equal as numbers 325 * but differ in length, then the concatenation order is the 326 * endpoint shared key, followed by the shorter key vector, 327 * followed by the longer key vector. Otherwise, the key 328 * vectors are identical, and may be concatenated to the 329 * endpoint pair key in any order. 330 */ 331 cmp = sctp_auth_compare_vectors(local_key_vector, 332 peer_key_vector); 333 if (cmp < 0) { 334 first_vector = local_key_vector; 335 last_vector = peer_key_vector; 336 } else { 337 first_vector = peer_key_vector; 338 last_vector = local_key_vector; 339 } 340 341 secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector, 342 gfp); 343 out: 344 sctp_auth_key_put(local_key_vector); 345 sctp_auth_key_put(peer_key_vector); 346 347 return secret; 348 } 349 350 /* 351 * Populate the association overlay list with the list 352 * from the endpoint. 353 */ 354 int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep, 355 struct sctp_association *asoc, 356 gfp_t gfp) 357 { 358 struct sctp_shared_key *sh_key; 359 struct sctp_shared_key *new; 360 361 BUG_ON(!list_empty(&asoc->endpoint_shared_keys)); 362 363 key_for_each(sh_key, &ep->endpoint_shared_keys) { 364 new = sctp_auth_shkey_create(sh_key->key_id, gfp); 365 if (!new) 366 goto nomem; 367 368 new->key = sh_key->key; 369 sctp_auth_key_hold(new->key); 370 list_add(&new->key_list, &asoc->endpoint_shared_keys); 371 } 372 373 return 0; 374 375 nomem: 376 sctp_auth_destroy_keys(&asoc->endpoint_shared_keys); 377 return -ENOMEM; 378 } 379 380 static bool sctp_auth_chunk_id_forbidden(__u8 chunk_id) 381 { 382 switch (chunk_id) { 383 case SCTP_CID_INIT: 384 case SCTP_CID_INIT_ACK: 385 case SCTP_CID_SHUTDOWN_COMPLETE: 386 case SCTP_CID_AUTH: 387 return true; 388 default: 389 return false; 390 } 391 } 392 393 /* Verify AUTH parameters copied from a state cookie before they are restored 394 * into an association. When cookie authentication is disabled these fields 395 * are peer-controlled, so they must satisfy the same constraints as locally 396 * generated AUTH parameters. 397 */ 398 bool sctp_auth_verify_cookie_params(const struct sctp_endpoint *ep, 399 const struct sctp_cookie *cookie) 400 { 401 const struct sctp_paramhdr *random; 402 const struct sctp_hmac_algo_param *hmacs; 403 const struct sctp_chunks_param *chunks; 404 u16 hmacs_len, chunks_len; 405 u16 n_hmacs, n_chunks, i; 406 bool has_sha1 = false; 407 408 if (sctp_sk(ep->base.sk)->cookie_auth_enable || !ep->auth_enable) 409 return true; 410 411 random = (const struct sctp_paramhdr *)cookie->auth_random; 412 if (random->type != SCTP_PARAM_RANDOM || 413 ntohs(random->length) != sizeof(*random) + SCTP_AUTH_RANDOM_LENGTH) 414 return false; 415 416 hmacs = (const struct sctp_hmac_algo_param *)cookie->auth_hmacs; 417 hmacs_len = ntohs(hmacs->param_hdr.length); 418 if (hmacs->param_hdr.type != SCTP_PARAM_HMAC_ALGO || 419 hmacs_len < sizeof(struct sctp_paramhdr) + 420 sizeof(hmacs->hmac_ids[0]) || 421 hmacs_len > sizeof(cookie->auth_hmacs) || 422 (hmacs_len - sizeof(struct sctp_paramhdr)) % 423 sizeof(hmacs->hmac_ids[0])) 424 return false; 425 426 n_hmacs = (hmacs_len - sizeof(struct sctp_paramhdr)) / 427 sizeof(hmacs->hmac_ids[0]); 428 for (i = 0; i < n_hmacs; i++) { 429 u16 hmac_id = ntohs(hmacs->hmac_ids[i]); 430 431 if (!sctp_hmac_supported(hmac_id)) 432 return false; 433 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1) 434 has_sha1 = true; 435 } 436 if (!has_sha1) 437 return false; 438 439 chunks = (const struct sctp_chunks_param *)cookie->auth_chunks; 440 chunks_len = ntohs(chunks->param_hdr.length); 441 if (chunks->param_hdr.type != SCTP_PARAM_CHUNKS || 442 chunks_len < sizeof(struct sctp_paramhdr) || 443 chunks_len > sizeof(cookie->auth_chunks)) 444 return false; 445 446 n_chunks = chunks_len - sizeof(struct sctp_paramhdr); 447 for (i = 0; i < n_chunks; i++) { 448 if (sctp_auth_chunk_id_forbidden(chunks->chunks[i])) 449 return false; 450 } 451 452 return true; 453 } 454 455 456 /* Public interface to create the association shared key. 457 * See code above for the algorithm. 458 */ 459 int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp) 460 { 461 struct sctp_auth_bytes *secret; 462 struct sctp_shared_key *ep_key; 463 struct sctp_chunk *chunk; 464 465 /* If we don't support AUTH, or peer is not capable 466 * we don't need to do anything. 467 */ 468 if (!asoc->peer.auth_capable) 469 return 0; 470 471 /* If the key_id is non-zero and we couldn't find an 472 * endpoint pair shared key, we can't compute the 473 * secret. 474 * For key_id 0, endpoint pair shared key is a NULL key. 475 */ 476 ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id); 477 BUG_ON(!ep_key); 478 479 secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp); 480 if (!secret) 481 return -ENOMEM; 482 483 sctp_auth_key_put(asoc->asoc_shared_key); 484 asoc->asoc_shared_key = secret; 485 asoc->shkey = ep_key; 486 487 /* Update send queue in case any chunk already in there now 488 * needs authenticating 489 */ 490 list_for_each_entry(chunk, &asoc->outqueue.out_chunk_list, list) { 491 if (sctp_auth_send_cid(chunk->chunk_hdr->type, asoc)) { 492 chunk->auth = 1; 493 if (!chunk->shkey) { 494 chunk->shkey = asoc->shkey; 495 sctp_auth_shkey_hold(chunk->shkey); 496 } 497 } 498 } 499 500 return 0; 501 } 502 503 504 /* Find the endpoint pair shared key based on the key_id */ 505 struct sctp_shared_key *sctp_auth_get_shkey( 506 const struct sctp_association *asoc, 507 __u16 key_id) 508 { 509 struct sctp_shared_key *key; 510 511 /* First search associations set of endpoint pair shared keys */ 512 key_for_each(key, &asoc->endpoint_shared_keys) { 513 if (key->key_id == key_id) { 514 if (!key->deactivated) 515 return key; 516 break; 517 } 518 } 519 520 return NULL; 521 } 522 523 const struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id) 524 { 525 return &sctp_hmac_list[hmac_id]; 526 } 527 528 /* Get an hmac description information that we can use to build 529 * the AUTH chunk 530 */ 531 const struct sctp_hmac * 532 sctp_auth_asoc_get_hmac(const struct sctp_association *asoc) 533 { 534 struct sctp_hmac_algo_param *hmacs; 535 __u16 n_elt; 536 __u16 id = 0; 537 int i; 538 539 /* If we have a default entry, use it */ 540 if (asoc->default_hmac_id) 541 return &sctp_hmac_list[asoc->default_hmac_id]; 542 543 /* Since we do not have a default entry, find the first entry 544 * we support and return that. Do not cache that id. 545 */ 546 hmacs = asoc->peer.peer_hmacs; 547 if (!hmacs) 548 return NULL; 549 550 n_elt = (ntohs(hmacs->param_hdr.length) - 551 sizeof(struct sctp_paramhdr)) >> 1; 552 for (i = 0; i < n_elt; i++) { 553 id = ntohs(hmacs->hmac_ids[i]); 554 if (sctp_hmac_supported(id)) 555 return &sctp_hmac_list[id]; 556 } 557 return NULL; 558 } 559 560 static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id) 561 { 562 int found = 0; 563 int i; 564 565 for (i = 0; i < n_elts; i++) { 566 if (hmac_id == hmacs[i]) { 567 found = 1; 568 break; 569 } 570 } 571 572 return found; 573 } 574 575 /* See if the HMAC_ID is one that we claim as supported */ 576 int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc, 577 __be16 hmac_id) 578 { 579 struct sctp_hmac_algo_param *hmacs; 580 __u16 n_elt; 581 582 if (!asoc) 583 return 0; 584 585 hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs; 586 n_elt = (ntohs(hmacs->param_hdr.length) - 587 sizeof(struct sctp_paramhdr)) >> 1; 588 589 return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id); 590 } 591 592 593 /* Cache the default HMAC id. This to follow this text from SCTP-AUTH: 594 * Section 6.1: 595 * The receiver of a HMAC-ALGO parameter SHOULD use the first listed 596 * algorithm it supports. 597 */ 598 void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc, 599 struct sctp_hmac_algo_param *hmacs) 600 { 601 __u16 id; 602 int i; 603 int n_params; 604 605 /* if the default id is already set, use it */ 606 if (asoc->default_hmac_id) 607 return; 608 609 n_params = (ntohs(hmacs->param_hdr.length) - 610 sizeof(struct sctp_paramhdr)) >> 1; 611 for (i = 0; i < n_params; i++) { 612 id = ntohs(hmacs->hmac_ids[i]); 613 if (sctp_hmac_supported(id)) { 614 asoc->default_hmac_id = id; 615 break; 616 } 617 } 618 } 619 620 621 /* Check to see if the given chunk is supposed to be authenticated */ 622 static int __sctp_auth_cid(enum sctp_cid chunk, struct sctp_chunks_param *param) 623 { 624 unsigned short len; 625 int found = 0; 626 int i; 627 628 if (!param || param->param_hdr.length == 0) 629 return 0; 630 631 len = ntohs(param->param_hdr.length) - sizeof(struct sctp_paramhdr); 632 633 /* SCTP-AUTH, Section 3.2 634 * The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH 635 * chunks MUST NOT be listed in the CHUNKS parameter. However, if 636 * a CHUNKS parameter is received then the types for INIT, INIT-ACK, 637 * SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored. 638 */ 639 for (i = 0; !found && i < len; i++) { 640 switch (param->chunks[i]) { 641 case SCTP_CID_INIT: 642 case SCTP_CID_INIT_ACK: 643 case SCTP_CID_SHUTDOWN_COMPLETE: 644 case SCTP_CID_AUTH: 645 break; 646 647 default: 648 if (param->chunks[i] == chunk) 649 found = 1; 650 break; 651 } 652 } 653 654 return found; 655 } 656 657 /* Check if peer requested that this chunk is authenticated */ 658 int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc) 659 { 660 if (!asoc) 661 return 0; 662 663 if (!asoc->peer.auth_capable) 664 return 0; 665 666 return __sctp_auth_cid(chunk, asoc->peer.peer_chunks); 667 } 668 669 /* Check if we requested that peer authenticate this chunk. */ 670 int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc) 671 { 672 if (!asoc) 673 return 0; 674 675 if (!asoc->peer.auth_capable) 676 return 0; 677 678 return __sctp_auth_cid(chunk, 679 (struct sctp_chunks_param *)asoc->c.auth_chunks); 680 } 681 682 /* SCTP-AUTH: Section 6.2: 683 * The sender MUST calculate the MAC as described in RFC2104 [2] using 684 * the hash function H as described by the MAC Identifier and the shared 685 * association key K based on the endpoint pair shared key described by 686 * the shared key identifier. The 'data' used for the computation of 687 * the AUTH-chunk is given by the AUTH chunk with its HMAC field set to 688 * zero (as shown in Figure 6) followed by all chunks that are placed 689 * after the AUTH chunk in the SCTP packet. 690 */ 691 int sctp_auth_calculate_hmac(const struct sctp_association *asoc, 692 struct sk_buff *skb, struct sctp_auth_chunk *auth, 693 struct sctp_shared_key *ep_key, gfp_t gfp) 694 { 695 struct sctp_auth_bytes *asoc_key; 696 __u16 key_id, hmac_id; 697 int free_key = 0; 698 size_t data_len; 699 __u8 *digest; 700 701 /* Extract the info we need: 702 * - hmac id 703 * - key id 704 */ 705 key_id = ntohs(auth->auth_hdr.shkey_id); 706 hmac_id = ntohs(auth->auth_hdr.hmac_id); 707 708 if (key_id == asoc->active_key_id) 709 asoc_key = asoc->asoc_shared_key; 710 else { 711 /* ep_key can't be NULL here */ 712 asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp); 713 if (!asoc_key) 714 return -ENOMEM; 715 716 free_key = 1; 717 } 718 719 data_len = skb_tail_pointer(skb) - (unsigned char *)auth; 720 digest = (u8 *)(&auth->auth_hdr + 1); 721 if (hmac_id == SCTP_AUTH_HMAC_ID_SHA1) { 722 hmac_sha1_usingrawkey(asoc_key->data, asoc_key->len, 723 (const u8 *)auth, data_len, digest); 724 } else { 725 WARN_ON_ONCE(hmac_id != SCTP_AUTH_HMAC_ID_SHA256); 726 hmac_sha256_usingrawkey(asoc_key->data, asoc_key->len, 727 (const u8 *)auth, data_len, digest); 728 } 729 730 if (free_key) 731 sctp_auth_key_put(asoc_key); 732 733 return 0; 734 } 735 736 /* API Helpers */ 737 738 /* Add a chunk to the endpoint authenticated chunk list */ 739 int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id) 740 { 741 struct sctp_chunks_param *p = ep->auth_chunk_list; 742 __u16 nchunks; 743 __u16 param_len; 744 745 /* If this chunk is already specified, we are done */ 746 if (__sctp_auth_cid(chunk_id, p)) 747 return 0; 748 749 /* Check if we can add this chunk to the array */ 750 param_len = ntohs(p->param_hdr.length); 751 nchunks = param_len - sizeof(struct sctp_paramhdr); 752 if (nchunks == SCTP_AUTH_MAX_CHUNKS) 753 return -EINVAL; 754 755 p->chunks[nchunks] = chunk_id; 756 p->param_hdr.length = htons(param_len + 1); 757 return 0; 758 } 759 760 /* Add hmac identifires to the endpoint list of supported hmac ids */ 761 int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep, 762 struct sctp_hmacalgo *hmacs) 763 { 764 int has_sha1 = 0; 765 __u16 id; 766 int i; 767 768 /* Scan the list looking for unsupported id. Also make sure that 769 * SHA1 is specified. 770 */ 771 for (i = 0; i < hmacs->shmac_num_idents; i++) { 772 id = hmacs->shmac_idents[i]; 773 774 if (!sctp_hmac_supported(id)) 775 return -EOPNOTSUPP; 776 777 if (SCTP_AUTH_HMAC_ID_SHA1 == id) 778 has_sha1 = 1; 779 } 780 781 if (!has_sha1) 782 return -EINVAL; 783 784 for (i = 0; i < hmacs->shmac_num_idents; i++) 785 ep->auth_hmacs_list->hmac_ids[i] = 786 htons(hmacs->shmac_idents[i]); 787 ep->auth_hmacs_list->param_hdr.length = 788 htons(sizeof(struct sctp_paramhdr) + 789 hmacs->shmac_num_idents * sizeof(__u16)); 790 return 0; 791 } 792 793 /* Set a new shared key on either endpoint or association. If the 794 * key with a same ID already exists, replace the key (remove the 795 * old key and add a new one). 796 */ 797 int sctp_auth_set_key(struct sctp_endpoint *ep, 798 struct sctp_association *asoc, 799 struct sctp_authkey *auth_key) 800 { 801 struct sctp_shared_key *cur_key, *shkey; 802 struct sctp_auth_bytes *key; 803 struct list_head *sh_keys; 804 int replace = 0; 805 806 /* Try to find the given key id to see if 807 * we are doing a replace, or adding a new key 808 */ 809 if (asoc) { 810 if (!asoc->peer.auth_capable) 811 return -EACCES; 812 sh_keys = &asoc->endpoint_shared_keys; 813 } else { 814 if (!ep->auth_enable) 815 return -EACCES; 816 sh_keys = &ep->endpoint_shared_keys; 817 } 818 819 key_for_each(shkey, sh_keys) { 820 if (shkey->key_id == auth_key->sca_keynumber) { 821 replace = 1; 822 break; 823 } 824 } 825 826 cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL); 827 if (!cur_key) 828 return -ENOMEM; 829 830 /* Create a new key data based on the info passed in */ 831 key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL); 832 if (!key) { 833 kfree(cur_key); 834 return -ENOMEM; 835 } 836 837 memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength); 838 cur_key->key = key; 839 840 if (!replace) { 841 list_add(&cur_key->key_list, sh_keys); 842 return 0; 843 } 844 845 list_del_init(&shkey->key_list); 846 list_add(&cur_key->key_list, sh_keys); 847 848 if (asoc && asoc->active_key_id == auth_key->sca_keynumber && 849 sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) { 850 list_del_init(&cur_key->key_list); 851 sctp_auth_shkey_release(cur_key); 852 list_add(&shkey->key_list, sh_keys); 853 return -ENOMEM; 854 } 855 856 sctp_auth_shkey_release(shkey); 857 return 0; 858 } 859 860 int sctp_auth_set_active_key(struct sctp_endpoint *ep, 861 struct sctp_association *asoc, 862 __u16 key_id) 863 { 864 struct sctp_shared_key *key; 865 struct list_head *sh_keys; 866 int found = 0; 867 868 /* The key identifier MUST correst to an existing key */ 869 if (asoc) { 870 if (!asoc->peer.auth_capable) 871 return -EACCES; 872 sh_keys = &asoc->endpoint_shared_keys; 873 } else { 874 if (!ep->auth_enable) 875 return -EACCES; 876 sh_keys = &ep->endpoint_shared_keys; 877 } 878 879 key_for_each(key, sh_keys) { 880 if (key->key_id == key_id) { 881 found = 1; 882 break; 883 } 884 } 885 886 if (!found || key->deactivated) 887 return -EINVAL; 888 889 if (asoc) { 890 __u16 active_key_id = asoc->active_key_id; 891 892 asoc->active_key_id = key_id; 893 if (sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) { 894 asoc->active_key_id = active_key_id; 895 return -ENOMEM; 896 } 897 } else 898 ep->active_key_id = key_id; 899 900 return 0; 901 } 902 903 int sctp_auth_del_key_id(struct sctp_endpoint *ep, 904 struct sctp_association *asoc, 905 __u16 key_id) 906 { 907 struct sctp_shared_key *key; 908 struct list_head *sh_keys; 909 int found = 0; 910 911 /* The key identifier MUST NOT be the current active key 912 * The key identifier MUST correst to an existing key 913 */ 914 if (asoc) { 915 if (!asoc->peer.auth_capable) 916 return -EACCES; 917 if (asoc->active_key_id == key_id) 918 return -EINVAL; 919 920 sh_keys = &asoc->endpoint_shared_keys; 921 } else { 922 if (!ep->auth_enable) 923 return -EACCES; 924 if (ep->active_key_id == key_id) 925 return -EINVAL; 926 927 sh_keys = &ep->endpoint_shared_keys; 928 } 929 930 key_for_each(key, sh_keys) { 931 if (key->key_id == key_id) { 932 found = 1; 933 break; 934 } 935 } 936 937 if (!found) 938 return -EINVAL; 939 940 /* Delete the shared key */ 941 list_del_init(&key->key_list); 942 sctp_auth_shkey_release(key); 943 944 return 0; 945 } 946 947 int sctp_auth_deact_key_id(struct sctp_endpoint *ep, 948 struct sctp_association *asoc, __u16 key_id) 949 { 950 struct sctp_shared_key *key; 951 struct list_head *sh_keys; 952 int found = 0; 953 954 /* The key identifier MUST NOT be the current active key 955 * The key identifier MUST correst to an existing key 956 */ 957 if (asoc) { 958 if (!asoc->peer.auth_capable) 959 return -EACCES; 960 if (asoc->active_key_id == key_id) 961 return -EINVAL; 962 963 sh_keys = &asoc->endpoint_shared_keys; 964 } else { 965 if (!ep->auth_enable) 966 return -EACCES; 967 if (ep->active_key_id == key_id) 968 return -EINVAL; 969 970 sh_keys = &ep->endpoint_shared_keys; 971 } 972 973 key_for_each(key, sh_keys) { 974 if (key->key_id == key_id) { 975 found = 1; 976 break; 977 } 978 } 979 980 if (!found) 981 return -EINVAL; 982 983 /* refcnt == 1 and !list_empty mean it's not being used anywhere 984 * and deactivated will be set, so it's time to notify userland 985 * that this shkey can be freed. 986 */ 987 if (asoc && !list_empty(&key->key_list) && 988 refcount_read(&key->refcnt) == 1) { 989 struct sctp_ulpevent *ev; 990 991 ev = sctp_ulpevent_make_authkey(asoc, key->key_id, 992 SCTP_AUTH_FREE_KEY, GFP_KERNEL); 993 if (ev) 994 asoc->stream.si->enqueue_event(&asoc->ulpq, ev); 995 } 996 997 key->deactivated = 1; 998 999 return 0; 1000 } 1001 1002 int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp) 1003 { 1004 /* Allocate space for HMACS and CHUNKS authentication 1005 * variables. There are arrays that we encode directly 1006 * into parameters to make the rest of the operations easier. 1007 */ 1008 if (!ep->auth_hmacs_list) { 1009 struct sctp_hmac_algo_param *auth_hmacs; 1010 1011 auth_hmacs = kzalloc_flex(*auth_hmacs, hmac_ids, 1012 SCTP_AUTH_NUM_HMACS, gfp); 1013 if (!auth_hmacs) 1014 goto nomem; 1015 /* Initialize the HMACS parameter. 1016 * SCTP-AUTH: Section 3.3 1017 * Every endpoint supporting SCTP chunk authentication MUST 1018 * support the HMAC based on the SHA-1 algorithm. 1019 */ 1020 auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO; 1021 auth_hmacs->param_hdr.length = 1022 htons(sizeof(struct sctp_paramhdr) + 2); 1023 auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1); 1024 ep->auth_hmacs_list = auth_hmacs; 1025 } 1026 1027 if (!ep->auth_chunk_list) { 1028 struct sctp_chunks_param *auth_chunks; 1029 1030 auth_chunks = kzalloc(sizeof(*auth_chunks) + 1031 SCTP_NUM_CHUNK_TYPES, gfp); 1032 if (!auth_chunks) 1033 goto nomem; 1034 /* Initialize the CHUNKS parameter */ 1035 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS; 1036 auth_chunks->param_hdr.length = 1037 htons(sizeof(struct sctp_paramhdr)); 1038 ep->auth_chunk_list = auth_chunks; 1039 } 1040 1041 return 0; 1042 1043 nomem: 1044 /* Free all allocations */ 1045 kfree(ep->auth_hmacs_list); 1046 kfree(ep->auth_chunk_list); 1047 ep->auth_hmacs_list = NULL; 1048 ep->auth_chunk_list = NULL; 1049 return -ENOMEM; 1050 } 1051 1052 void sctp_auth_free(struct sctp_endpoint *ep) 1053 { 1054 kfree(ep->auth_hmacs_list); 1055 kfree(ep->auth_chunk_list); 1056 ep->auth_hmacs_list = NULL; 1057 ep->auth_chunk_list = NULL; 1058 } 1059