0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015 #include <crypto/hash.h>
0016 #include <linux/slab.h>
0017 #include <linux/types.h>
0018 #include <linux/scatterlist.h>
0019 #include <net/sctp/sctp.h>
0020 #include <net/sctp/auth.h>
0021
0022 static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
0023 {
0024
0025 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
0026 },
0027 {
0028 .hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
0029 .hmac_name = "hmac(sha1)",
0030 .hmac_len = SCTP_SHA1_SIG_SIZE,
0031 },
0032 {
0033
0034 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
0035 },
0036 #if IS_ENABLED(CONFIG_CRYPTO_SHA256)
0037 {
0038 .hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
0039 .hmac_name = "hmac(sha256)",
0040 .hmac_len = SCTP_SHA256_SIG_SIZE,
0041 }
0042 #endif
0043 };
0044
0045
0046 void sctp_auth_key_put(struct sctp_auth_bytes *key)
0047 {
0048 if (!key)
0049 return;
0050
0051 if (refcount_dec_and_test(&key->refcnt)) {
0052 kfree_sensitive(key);
0053 SCTP_DBG_OBJCNT_DEC(keys);
0054 }
0055 }
0056
0057
0058 static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
0059 {
0060 struct sctp_auth_bytes *key;
0061
0062
0063 if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
0064 return NULL;
0065
0066
0067 key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
0068 if (!key)
0069 return NULL;
0070
0071 key->len = key_len;
0072 refcount_set(&key->refcnt, 1);
0073 SCTP_DBG_OBJCNT_INC(keys);
0074
0075 return key;
0076 }
0077
0078
0079 struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
0080 {
0081 struct sctp_shared_key *new;
0082
0083
0084 new = kzalloc(sizeof(struct sctp_shared_key), gfp);
0085 if (!new)
0086 return NULL;
0087
0088 INIT_LIST_HEAD(&new->key_list);
0089 refcount_set(&new->refcnt, 1);
0090 new->key_id = key_id;
0091
0092 return new;
0093 }
0094
0095
0096 static void sctp_auth_shkey_destroy(struct sctp_shared_key *sh_key)
0097 {
0098 BUG_ON(!list_empty(&sh_key->key_list));
0099 sctp_auth_key_put(sh_key->key);
0100 sh_key->key = NULL;
0101 kfree(sh_key);
0102 }
0103
0104 void sctp_auth_shkey_release(struct sctp_shared_key *sh_key)
0105 {
0106 if (refcount_dec_and_test(&sh_key->refcnt))
0107 sctp_auth_shkey_destroy(sh_key);
0108 }
0109
0110 void sctp_auth_shkey_hold(struct sctp_shared_key *sh_key)
0111 {
0112 refcount_inc(&sh_key->refcnt);
0113 }
0114
0115
0116
0117
0118 void sctp_auth_destroy_keys(struct list_head *keys)
0119 {
0120 struct sctp_shared_key *ep_key;
0121 struct sctp_shared_key *tmp;
0122
0123 if (list_empty(keys))
0124 return;
0125
0126 key_for_each_safe(ep_key, tmp, keys) {
0127 list_del_init(&ep_key->key_list);
0128 sctp_auth_shkey_release(ep_key);
0129 }
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145
0146
0147
0148 static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
0149 struct sctp_auth_bytes *vector2)
0150 {
0151 int diff;
0152 int i;
0153 const __u8 *longer;
0154
0155 diff = vector1->len - vector2->len;
0156 if (diff) {
0157 longer = (diff > 0) ? vector1->data : vector2->data;
0158
0159
0160
0161
0162
0163 for (i = 0; i < abs(diff); i++) {
0164 if (longer[i] != 0)
0165 return diff;
0166 }
0167 }
0168
0169
0170 return memcmp(vector1->data, vector2->data, vector1->len);
0171 }
0172
0173
0174
0175
0176
0177
0178
0179
0180
0181
0182
0183
0184 static struct sctp_auth_bytes *sctp_auth_make_key_vector(
0185 struct sctp_random_param *random,
0186 struct sctp_chunks_param *chunks,
0187 struct sctp_hmac_algo_param *hmacs,
0188 gfp_t gfp)
0189 {
0190 struct sctp_auth_bytes *new;
0191 __u32 len;
0192 __u32 offset = 0;
0193 __u16 random_len, hmacs_len, chunks_len = 0;
0194
0195 random_len = ntohs(random->param_hdr.length);
0196 hmacs_len = ntohs(hmacs->param_hdr.length);
0197 if (chunks)
0198 chunks_len = ntohs(chunks->param_hdr.length);
0199
0200 len = random_len + hmacs_len + chunks_len;
0201
0202 new = sctp_auth_create_key(len, gfp);
0203 if (!new)
0204 return NULL;
0205
0206 memcpy(new->data, random, random_len);
0207 offset += random_len;
0208
0209 if (chunks) {
0210 memcpy(new->data + offset, chunks, chunks_len);
0211 offset += chunks_len;
0212 }
0213
0214 memcpy(new->data + offset, hmacs, hmacs_len);
0215
0216 return new;
0217 }
0218
0219
0220
0221 static struct sctp_auth_bytes *sctp_auth_make_local_vector(
0222 const struct sctp_association *asoc,
0223 gfp_t gfp)
0224 {
0225 return sctp_auth_make_key_vector(
0226 (struct sctp_random_param *)asoc->c.auth_random,
0227 (struct sctp_chunks_param *)asoc->c.auth_chunks,
0228 (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs, gfp);
0229 }
0230
0231
0232 static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
0233 const struct sctp_association *asoc,
0234 gfp_t gfp)
0235 {
0236 return sctp_auth_make_key_vector(asoc->peer.peer_random,
0237 asoc->peer.peer_chunks,
0238 asoc->peer.peer_hmacs,
0239 gfp);
0240 }
0241
0242
0243
0244
0245
0246
0247
0248
0249
0250
0251
0252 static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
0253 struct sctp_shared_key *ep_key,
0254 struct sctp_auth_bytes *first_vector,
0255 struct sctp_auth_bytes *last_vector,
0256 gfp_t gfp)
0257 {
0258 struct sctp_auth_bytes *secret;
0259 __u32 offset = 0;
0260 __u32 auth_len;
0261
0262 auth_len = first_vector->len + last_vector->len;
0263 if (ep_key->key)
0264 auth_len += ep_key->key->len;
0265
0266 secret = sctp_auth_create_key(auth_len, gfp);
0267 if (!secret)
0268 return NULL;
0269
0270 if (ep_key->key) {
0271 memcpy(secret->data, ep_key->key->data, ep_key->key->len);
0272 offset += ep_key->key->len;
0273 }
0274
0275 memcpy(secret->data + offset, first_vector->data, first_vector->len);
0276 offset += first_vector->len;
0277
0278 memcpy(secret->data + offset, last_vector->data, last_vector->len);
0279
0280 return secret;
0281 }
0282
0283
0284
0285
0286 static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
0287 const struct sctp_association *asoc,
0288 struct sctp_shared_key *ep_key,
0289 gfp_t gfp)
0290 {
0291 struct sctp_auth_bytes *local_key_vector;
0292 struct sctp_auth_bytes *peer_key_vector;
0293 struct sctp_auth_bytes *first_vector,
0294 *last_vector;
0295 struct sctp_auth_bytes *secret = NULL;
0296 int cmp;
0297
0298
0299
0300
0301
0302
0303
0304
0305
0306
0307
0308
0309
0310
0311 local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
0312 peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
0313
0314 if (!peer_key_vector || !local_key_vector)
0315 goto out;
0316
0317
0318
0319
0320
0321
0322
0323
0324
0325
0326
0327
0328
0329
0330 cmp = sctp_auth_compare_vectors(local_key_vector,
0331 peer_key_vector);
0332 if (cmp < 0) {
0333 first_vector = local_key_vector;
0334 last_vector = peer_key_vector;
0335 } else {
0336 first_vector = peer_key_vector;
0337 last_vector = local_key_vector;
0338 }
0339
0340 secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
0341 gfp);
0342 out:
0343 sctp_auth_key_put(local_key_vector);
0344 sctp_auth_key_put(peer_key_vector);
0345
0346 return secret;
0347 }
0348
0349
0350
0351
0352
0353 int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
0354 struct sctp_association *asoc,
0355 gfp_t gfp)
0356 {
0357 struct sctp_shared_key *sh_key;
0358 struct sctp_shared_key *new;
0359
0360 BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
0361
0362 key_for_each(sh_key, &ep->endpoint_shared_keys) {
0363 new = sctp_auth_shkey_create(sh_key->key_id, gfp);
0364 if (!new)
0365 goto nomem;
0366
0367 new->key = sh_key->key;
0368 sctp_auth_key_hold(new->key);
0369 list_add(&new->key_list, &asoc->endpoint_shared_keys);
0370 }
0371
0372 return 0;
0373
0374 nomem:
0375 sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
0376 return -ENOMEM;
0377 }
0378
0379
0380
0381
0382
0383 int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
0384 {
0385 struct sctp_auth_bytes *secret;
0386 struct sctp_shared_key *ep_key;
0387 struct sctp_chunk *chunk;
0388
0389
0390
0391
0392 if (!asoc->peer.auth_capable)
0393 return 0;
0394
0395
0396
0397
0398
0399
0400 ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
0401 BUG_ON(!ep_key);
0402
0403 secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
0404 if (!secret)
0405 return -ENOMEM;
0406
0407 sctp_auth_key_put(asoc->asoc_shared_key);
0408 asoc->asoc_shared_key = secret;
0409 asoc->shkey = ep_key;
0410
0411
0412
0413
0414 list_for_each_entry(chunk, &asoc->outqueue.out_chunk_list, list) {
0415 if (sctp_auth_send_cid(chunk->chunk_hdr->type, asoc)) {
0416 chunk->auth = 1;
0417 if (!chunk->shkey) {
0418 chunk->shkey = asoc->shkey;
0419 sctp_auth_shkey_hold(chunk->shkey);
0420 }
0421 }
0422 }
0423
0424 return 0;
0425 }
0426
0427
0428
0429 struct sctp_shared_key *sctp_auth_get_shkey(
0430 const struct sctp_association *asoc,
0431 __u16 key_id)
0432 {
0433 struct sctp_shared_key *key;
0434
0435
0436 key_for_each(key, &asoc->endpoint_shared_keys) {
0437 if (key->key_id == key_id) {
0438 if (!key->deactivated)
0439 return key;
0440 break;
0441 }
0442 }
0443
0444 return NULL;
0445 }
0446
0447
0448
0449
0450
0451
0452
0453
0454 int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
0455 {
0456 struct crypto_shash *tfm = NULL;
0457 __u16 id;
0458
0459
0460 if (ep->auth_hmacs)
0461 return 0;
0462
0463
0464 ep->auth_hmacs = kcalloc(SCTP_AUTH_NUM_HMACS,
0465 sizeof(struct crypto_shash *),
0466 gfp);
0467 if (!ep->auth_hmacs)
0468 return -ENOMEM;
0469
0470 for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
0471
0472
0473
0474
0475
0476
0477 if (!sctp_hmac_list[id].hmac_name)
0478 continue;
0479
0480
0481 if (ep->auth_hmacs[id])
0482 continue;
0483
0484
0485 tfm = crypto_alloc_shash(sctp_hmac_list[id].hmac_name, 0, 0);
0486 if (IS_ERR(tfm))
0487 goto out_err;
0488
0489 ep->auth_hmacs[id] = tfm;
0490 }
0491
0492 return 0;
0493
0494 out_err:
0495
0496 sctp_auth_destroy_hmacs(ep->auth_hmacs);
0497 ep->auth_hmacs = NULL;
0498 return -ENOMEM;
0499 }
0500
0501
0502 void sctp_auth_destroy_hmacs(struct crypto_shash *auth_hmacs[])
0503 {
0504 int i;
0505
0506 if (!auth_hmacs)
0507 return;
0508
0509 for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++) {
0510 crypto_free_shash(auth_hmacs[i]);
0511 }
0512 kfree(auth_hmacs);
0513 }
0514
0515
0516 struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
0517 {
0518 return &sctp_hmac_list[hmac_id];
0519 }
0520
0521
0522
0523
0524 struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
0525 {
0526 struct sctp_hmac_algo_param *hmacs;
0527 __u16 n_elt;
0528 __u16 id = 0;
0529 int i;
0530
0531
0532 if (asoc->default_hmac_id)
0533 return &sctp_hmac_list[asoc->default_hmac_id];
0534
0535
0536
0537
0538 hmacs = asoc->peer.peer_hmacs;
0539 if (!hmacs)
0540 return NULL;
0541
0542 n_elt = (ntohs(hmacs->param_hdr.length) -
0543 sizeof(struct sctp_paramhdr)) >> 1;
0544 for (i = 0; i < n_elt; i++) {
0545 id = ntohs(hmacs->hmac_ids[i]);
0546
0547
0548
0549
0550
0551
0552
0553 if (id > SCTP_AUTH_HMAC_ID_MAX ||
0554 !sctp_hmac_list[id].hmac_name) {
0555 id = 0;
0556 continue;
0557 }
0558
0559 break;
0560 }
0561
0562 if (id == 0)
0563 return NULL;
0564
0565 return &sctp_hmac_list[id];
0566 }
0567
0568 static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
0569 {
0570 int found = 0;
0571 int i;
0572
0573 for (i = 0; i < n_elts; i++) {
0574 if (hmac_id == hmacs[i]) {
0575 found = 1;
0576 break;
0577 }
0578 }
0579
0580 return found;
0581 }
0582
0583
0584 int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
0585 __be16 hmac_id)
0586 {
0587 struct sctp_hmac_algo_param *hmacs;
0588 __u16 n_elt;
0589
0590 if (!asoc)
0591 return 0;
0592
0593 hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
0594 n_elt = (ntohs(hmacs->param_hdr.length) -
0595 sizeof(struct sctp_paramhdr)) >> 1;
0596
0597 return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
0598 }
0599
0600
0601
0602
0603
0604
0605
0606 void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
0607 struct sctp_hmac_algo_param *hmacs)
0608 {
0609 struct sctp_endpoint *ep;
0610 __u16 id;
0611 int i;
0612 int n_params;
0613
0614
0615 if (asoc->default_hmac_id)
0616 return;
0617
0618 n_params = (ntohs(hmacs->param_hdr.length) -
0619 sizeof(struct sctp_paramhdr)) >> 1;
0620 ep = asoc->ep;
0621 for (i = 0; i < n_params; i++) {
0622 id = ntohs(hmacs->hmac_ids[i]);
0623
0624
0625 if (id > SCTP_AUTH_HMAC_ID_MAX)
0626 continue;
0627
0628
0629 if (ep->auth_hmacs[id]) {
0630 asoc->default_hmac_id = id;
0631 break;
0632 }
0633 }
0634 }
0635
0636
0637
0638 static int __sctp_auth_cid(enum sctp_cid chunk, struct sctp_chunks_param *param)
0639 {
0640 unsigned short len;
0641 int found = 0;
0642 int i;
0643
0644 if (!param || param->param_hdr.length == 0)
0645 return 0;
0646
0647 len = ntohs(param->param_hdr.length) - sizeof(struct sctp_paramhdr);
0648
0649
0650
0651
0652
0653
0654
0655 for (i = 0; !found && i < len; i++) {
0656 switch (param->chunks[i]) {
0657 case SCTP_CID_INIT:
0658 case SCTP_CID_INIT_ACK:
0659 case SCTP_CID_SHUTDOWN_COMPLETE:
0660 case SCTP_CID_AUTH:
0661 break;
0662
0663 default:
0664 if (param->chunks[i] == chunk)
0665 found = 1;
0666 break;
0667 }
0668 }
0669
0670 return found;
0671 }
0672
0673
0674 int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
0675 {
0676 if (!asoc)
0677 return 0;
0678
0679 if (!asoc->peer.auth_capable)
0680 return 0;
0681
0682 return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
0683 }
0684
0685
0686 int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
0687 {
0688 if (!asoc)
0689 return 0;
0690
0691 if (!asoc->peer.auth_capable)
0692 return 0;
0693
0694 return __sctp_auth_cid(chunk,
0695 (struct sctp_chunks_param *)asoc->c.auth_chunks);
0696 }
0697
0698
0699
0700
0701
0702
0703
0704
0705
0706
0707 void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
0708 struct sk_buff *skb, struct sctp_auth_chunk *auth,
0709 struct sctp_shared_key *ep_key, gfp_t gfp)
0710 {
0711 struct sctp_auth_bytes *asoc_key;
0712 struct crypto_shash *tfm;
0713 __u16 key_id, hmac_id;
0714 unsigned char *end;
0715 int free_key = 0;
0716 __u8 *digest;
0717
0718
0719
0720
0721
0722 key_id = ntohs(auth->auth_hdr.shkey_id);
0723 hmac_id = ntohs(auth->auth_hdr.hmac_id);
0724
0725 if (key_id == asoc->active_key_id)
0726 asoc_key = asoc->asoc_shared_key;
0727 else {
0728
0729 asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
0730 if (!asoc_key)
0731 return;
0732
0733 free_key = 1;
0734 }
0735
0736
0737 end = skb_tail_pointer(skb);
0738
0739 tfm = asoc->ep->auth_hmacs[hmac_id];
0740
0741 digest = auth->auth_hdr.hmac;
0742 if (crypto_shash_setkey(tfm, &asoc_key->data[0], asoc_key->len))
0743 goto free;
0744
0745 crypto_shash_tfm_digest(tfm, (u8 *)auth, end - (unsigned char *)auth,
0746 digest);
0747
0748 free:
0749 if (free_key)
0750 sctp_auth_key_put(asoc_key);
0751 }
0752
0753
0754
0755
0756 int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
0757 {
0758 struct sctp_chunks_param *p = ep->auth_chunk_list;
0759 __u16 nchunks;
0760 __u16 param_len;
0761
0762
0763 if (__sctp_auth_cid(chunk_id, p))
0764 return 0;
0765
0766
0767 param_len = ntohs(p->param_hdr.length);
0768 nchunks = param_len - sizeof(struct sctp_paramhdr);
0769 if (nchunks == SCTP_NUM_CHUNK_TYPES)
0770 return -EINVAL;
0771
0772 p->chunks[nchunks] = chunk_id;
0773 p->param_hdr.length = htons(param_len + 1);
0774 return 0;
0775 }
0776
0777
0778 int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
0779 struct sctp_hmacalgo *hmacs)
0780 {
0781 int has_sha1 = 0;
0782 __u16 id;
0783 int i;
0784
0785
0786
0787
0788 for (i = 0; i < hmacs->shmac_num_idents; i++) {
0789 id = hmacs->shmac_idents[i];
0790
0791 if (id > SCTP_AUTH_HMAC_ID_MAX)
0792 return -EOPNOTSUPP;
0793
0794 if (SCTP_AUTH_HMAC_ID_SHA1 == id)
0795 has_sha1 = 1;
0796
0797 if (!sctp_hmac_list[id].hmac_name)
0798 return -EOPNOTSUPP;
0799 }
0800
0801 if (!has_sha1)
0802 return -EINVAL;
0803
0804 for (i = 0; i < hmacs->shmac_num_idents; i++)
0805 ep->auth_hmacs_list->hmac_ids[i] =
0806 htons(hmacs->shmac_idents[i]);
0807 ep->auth_hmacs_list->param_hdr.length =
0808 htons(sizeof(struct sctp_paramhdr) +
0809 hmacs->shmac_num_idents * sizeof(__u16));
0810 return 0;
0811 }
0812
0813
0814
0815
0816
0817 int sctp_auth_set_key(struct sctp_endpoint *ep,
0818 struct sctp_association *asoc,
0819 struct sctp_authkey *auth_key)
0820 {
0821 struct sctp_shared_key *cur_key, *shkey;
0822 struct sctp_auth_bytes *key;
0823 struct list_head *sh_keys;
0824 int replace = 0;
0825
0826
0827
0828
0829 if (asoc) {
0830 if (!asoc->peer.auth_capable)
0831 return -EACCES;
0832 sh_keys = &asoc->endpoint_shared_keys;
0833 } else {
0834 if (!ep->auth_enable)
0835 return -EACCES;
0836 sh_keys = &ep->endpoint_shared_keys;
0837 }
0838
0839 key_for_each(shkey, sh_keys) {
0840 if (shkey->key_id == auth_key->sca_keynumber) {
0841 replace = 1;
0842 break;
0843 }
0844 }
0845
0846 cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL);
0847 if (!cur_key)
0848 return -ENOMEM;
0849
0850
0851 key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
0852 if (!key) {
0853 kfree(cur_key);
0854 return -ENOMEM;
0855 }
0856
0857 memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
0858 cur_key->key = key;
0859
0860 if (!replace) {
0861 list_add(&cur_key->key_list, sh_keys);
0862 return 0;
0863 }
0864
0865 list_del_init(&shkey->key_list);
0866 sctp_auth_shkey_release(shkey);
0867 list_add(&cur_key->key_list, sh_keys);
0868
0869 if (asoc && asoc->active_key_id == auth_key->sca_keynumber)
0870 sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
0871
0872 return 0;
0873 }
0874
0875 int sctp_auth_set_active_key(struct sctp_endpoint *ep,
0876 struct sctp_association *asoc,
0877 __u16 key_id)
0878 {
0879 struct sctp_shared_key *key;
0880 struct list_head *sh_keys;
0881 int found = 0;
0882
0883
0884 if (asoc) {
0885 if (!asoc->peer.auth_capable)
0886 return -EACCES;
0887 sh_keys = &asoc->endpoint_shared_keys;
0888 } else {
0889 if (!ep->auth_enable)
0890 return -EACCES;
0891 sh_keys = &ep->endpoint_shared_keys;
0892 }
0893
0894 key_for_each(key, sh_keys) {
0895 if (key->key_id == key_id) {
0896 found = 1;
0897 break;
0898 }
0899 }
0900
0901 if (!found || key->deactivated)
0902 return -EINVAL;
0903
0904 if (asoc) {
0905 asoc->active_key_id = key_id;
0906 sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
0907 } else
0908 ep->active_key_id = key_id;
0909
0910 return 0;
0911 }
0912
0913 int sctp_auth_del_key_id(struct sctp_endpoint *ep,
0914 struct sctp_association *asoc,
0915 __u16 key_id)
0916 {
0917 struct sctp_shared_key *key;
0918 struct list_head *sh_keys;
0919 int found = 0;
0920
0921
0922
0923
0924 if (asoc) {
0925 if (!asoc->peer.auth_capable)
0926 return -EACCES;
0927 if (asoc->active_key_id == key_id)
0928 return -EINVAL;
0929
0930 sh_keys = &asoc->endpoint_shared_keys;
0931 } else {
0932 if (!ep->auth_enable)
0933 return -EACCES;
0934 if (ep->active_key_id == key_id)
0935 return -EINVAL;
0936
0937 sh_keys = &ep->endpoint_shared_keys;
0938 }
0939
0940 key_for_each(key, sh_keys) {
0941 if (key->key_id == key_id) {
0942 found = 1;
0943 break;
0944 }
0945 }
0946
0947 if (!found)
0948 return -EINVAL;
0949
0950
0951 list_del_init(&key->key_list);
0952 sctp_auth_shkey_release(key);
0953
0954 return 0;
0955 }
0956
0957 int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
0958 struct sctp_association *asoc, __u16 key_id)
0959 {
0960 struct sctp_shared_key *key;
0961 struct list_head *sh_keys;
0962 int found = 0;
0963
0964
0965
0966
0967 if (asoc) {
0968 if (!asoc->peer.auth_capable)
0969 return -EACCES;
0970 if (asoc->active_key_id == key_id)
0971 return -EINVAL;
0972
0973 sh_keys = &asoc->endpoint_shared_keys;
0974 } else {
0975 if (!ep->auth_enable)
0976 return -EACCES;
0977 if (ep->active_key_id == key_id)
0978 return -EINVAL;
0979
0980 sh_keys = &ep->endpoint_shared_keys;
0981 }
0982
0983 key_for_each(key, sh_keys) {
0984 if (key->key_id == key_id) {
0985 found = 1;
0986 break;
0987 }
0988 }
0989
0990 if (!found)
0991 return -EINVAL;
0992
0993
0994
0995
0996
0997 if (asoc && !list_empty(&key->key_list) &&
0998 refcount_read(&key->refcnt) == 1) {
0999 struct sctp_ulpevent *ev;
1000
1001 ev = sctp_ulpevent_make_authkey(asoc, key->key_id,
1002 SCTP_AUTH_FREE_KEY, GFP_KERNEL);
1003 if (ev)
1004 asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
1005 }
1006
1007 key->deactivated = 1;
1008
1009 return 0;
1010 }
1011
1012 int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp)
1013 {
1014 int err = -ENOMEM;
1015
1016
1017
1018
1019
1020 if (!ep->auth_hmacs_list) {
1021 struct sctp_hmac_algo_param *auth_hmacs;
1022
1023 auth_hmacs = kzalloc(struct_size(auth_hmacs, hmac_ids,
1024 SCTP_AUTH_NUM_HMACS), gfp);
1025 if (!auth_hmacs)
1026 goto nomem;
1027
1028
1029
1030
1031
1032 auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
1033 auth_hmacs->param_hdr.length =
1034 htons(sizeof(struct sctp_paramhdr) + 2);
1035 auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
1036 ep->auth_hmacs_list = auth_hmacs;
1037 }
1038
1039 if (!ep->auth_chunk_list) {
1040 struct sctp_chunks_param *auth_chunks;
1041
1042 auth_chunks = kzalloc(sizeof(*auth_chunks) +
1043 SCTP_NUM_CHUNK_TYPES, gfp);
1044 if (!auth_chunks)
1045 goto nomem;
1046
1047 auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
1048 auth_chunks->param_hdr.length =
1049 htons(sizeof(struct sctp_paramhdr));
1050 ep->auth_chunk_list = auth_chunks;
1051 }
1052
1053
1054
1055
1056 err = sctp_auth_init_hmacs(ep, gfp);
1057 if (err)
1058 goto nomem;
1059
1060 return 0;
1061
1062 nomem:
1063
1064 kfree(ep->auth_hmacs_list);
1065 kfree(ep->auth_chunk_list);
1066 ep->auth_hmacs_list = NULL;
1067 ep->auth_chunk_list = NULL;
1068 return err;
1069 }
1070
1071 void sctp_auth_free(struct sctp_endpoint *ep)
1072 {
1073 kfree(ep->auth_hmacs_list);
1074 kfree(ep->auth_chunk_list);
1075 ep->auth_hmacs_list = NULL;
1076 ep->auth_chunk_list = NULL;
1077 sctp_auth_destroy_hmacs(ep->auth_hmacs);
1078 ep->auth_hmacs = NULL;
1079 }