1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31#include <linux/slab.h>
32#include <linux/types.h>
33#include <linux/crypto.h>
34#include <linux/scatterlist.h>
35#include <net/sctp/sctp.h>
36#include <net/sctp/auth.h>
37
38static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
39 {
40
41 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
42 },
43 {
44 .hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
45 .hmac_name="hmac(sha1)",
46 .hmac_len = SCTP_SHA1_SIG_SIZE,
47 },
48 {
49
50 .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
51 },
52#if defined (CONFIG_CRYPTO_SHA256) || defined (CONFIG_CRYPTO_SHA256_MODULE)
53 {
54 .hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
55 .hmac_name="hmac(sha256)",
56 .hmac_len = SCTP_SHA256_SIG_SIZE,
57 }
58#endif
59};
60
61
62void sctp_auth_key_put(struct sctp_auth_bytes *key)
63{
64 if (!key)
65 return;
66
67 if (atomic_dec_and_test(&key->refcnt)) {
68 kzfree(key);
69 SCTP_DBG_OBJCNT_DEC(keys);
70 }
71}
72
73
74static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
75{
76 struct sctp_auth_bytes *key;
77
78
79 if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
80 return NULL;
81
82
83 key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
84 if (!key)
85 return NULL;
86
87 key->len = key_len;
88 atomic_set(&key->refcnt, 1);
89 SCTP_DBG_OBJCNT_INC(keys);
90
91 return key;
92}
93
94
95struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
96{
97 struct sctp_shared_key *new;
98
99
100 new = kzalloc(sizeof(struct sctp_shared_key), gfp);
101 if (!new)
102 return NULL;
103
104 INIT_LIST_HEAD(&new->key_list);
105 new->key_id = key_id;
106
107 return new;
108}
109
110
111static void sctp_auth_shkey_free(struct sctp_shared_key *sh_key)
112{
113 BUG_ON(!list_empty(&sh_key->key_list));
114 sctp_auth_key_put(sh_key->key);
115 sh_key->key = NULL;
116 kfree(sh_key);
117}
118
119
120
121
122void sctp_auth_destroy_keys(struct list_head *keys)
123{
124 struct sctp_shared_key *ep_key;
125 struct sctp_shared_key *tmp;
126
127 if (list_empty(keys))
128 return;
129
130 key_for_each_safe(ep_key, tmp, keys) {
131 list_del_init(&ep_key->key_list);
132 sctp_auth_shkey_free(ep_key);
133 }
134}
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
153 struct sctp_auth_bytes *vector2)
154{
155 int diff;
156 int i;
157 const __u8 *longer;
158
159 diff = vector1->len - vector2->len;
160 if (diff) {
161 longer = (diff > 0) ? vector1->data : vector2->data;
162
163
164
165
166
167 for (i = 0; i < abs(diff); i++ ) {
168 if (longer[i] != 0)
169 return diff;
170 }
171 }
172
173
174 return memcmp(vector1->data, vector2->data, vector1->len);
175}
176
177
178
179
180
181
182
183
184
185
186
187
188static struct sctp_auth_bytes *sctp_auth_make_key_vector(
189 sctp_random_param_t *random,
190 sctp_chunks_param_t *chunks,
191 sctp_hmac_algo_param_t *hmacs,
192 gfp_t gfp)
193{
194 struct sctp_auth_bytes *new;
195 __u32 len;
196 __u32 offset = 0;
197 __u16 random_len, hmacs_len, chunks_len = 0;
198
199 random_len = ntohs(random->param_hdr.length);
200 hmacs_len = ntohs(hmacs->param_hdr.length);
201 if (chunks)
202 chunks_len = ntohs(chunks->param_hdr.length);
203
204 len = random_len + hmacs_len + chunks_len;
205
206 new = sctp_auth_create_key(len, gfp);
207 if (!new)
208 return NULL;
209
210 memcpy(new->data, random, random_len);
211 offset += random_len;
212
213 if (chunks) {
214 memcpy(new->data + offset, chunks, chunks_len);
215 offset += chunks_len;
216 }
217
218 memcpy(new->data + offset, hmacs, hmacs_len);
219
220 return new;
221}
222
223
224
225static struct sctp_auth_bytes *sctp_auth_make_local_vector(
226 const struct sctp_association *asoc,
227 gfp_t gfp)
228{
229 return sctp_auth_make_key_vector(
230 (sctp_random_param_t*)asoc->c.auth_random,
231 (sctp_chunks_param_t*)asoc->c.auth_chunks,
232 (sctp_hmac_algo_param_t*)asoc->c.auth_hmacs,
233 gfp);
234}
235
236
237static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
238 const struct sctp_association *asoc,
239 gfp_t gfp)
240{
241 return sctp_auth_make_key_vector(asoc->peer.peer_random,
242 asoc->peer.peer_chunks,
243 asoc->peer.peer_hmacs,
244 gfp);
245}
246
247
248
249
250
251
252
253
254
255
256
257static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
258 struct sctp_shared_key *ep_key,
259 struct sctp_auth_bytes *first_vector,
260 struct sctp_auth_bytes *last_vector,
261 gfp_t gfp)
262{
263 struct sctp_auth_bytes *secret;
264 __u32 offset = 0;
265 __u32 auth_len;
266
267 auth_len = first_vector->len + last_vector->len;
268 if (ep_key->key)
269 auth_len += ep_key->key->len;
270
271 secret = sctp_auth_create_key(auth_len, gfp);
272 if (!secret)
273 return NULL;
274
275 if (ep_key->key) {
276 memcpy(secret->data, ep_key->key->data, ep_key->key->len);
277 offset += ep_key->key->len;
278 }
279
280 memcpy(secret->data + offset, first_vector->data, first_vector->len);
281 offset += first_vector->len;
282
283 memcpy(secret->data + offset, last_vector->data, last_vector->len);
284
285 return secret;
286}
287
288
289
290
291static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
292 const struct sctp_association *asoc,
293 struct sctp_shared_key *ep_key,
294 gfp_t gfp)
295{
296 struct sctp_auth_bytes *local_key_vector;
297 struct sctp_auth_bytes *peer_key_vector;
298 struct sctp_auth_bytes *first_vector,
299 *last_vector;
300 struct sctp_auth_bytes *secret = NULL;
301 int cmp;
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316 local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
317 peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
318
319 if (!peer_key_vector || !local_key_vector)
320 goto out;
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335 cmp = sctp_auth_compare_vectors(local_key_vector,
336 peer_key_vector);
337 if (cmp < 0) {
338 first_vector = local_key_vector;
339 last_vector = peer_key_vector;
340 } else {
341 first_vector = peer_key_vector;
342 last_vector = local_key_vector;
343 }
344
345 secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
346 gfp);
347out:
348 sctp_auth_key_put(local_key_vector);
349 sctp_auth_key_put(peer_key_vector);
350
351 return secret;
352}
353
354
355
356
357
358int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
359 struct sctp_association *asoc,
360 gfp_t gfp)
361{
362 struct sctp_shared_key *sh_key;
363 struct sctp_shared_key *new;
364
365 BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
366
367 key_for_each(sh_key, &ep->endpoint_shared_keys) {
368 new = sctp_auth_shkey_create(sh_key->key_id, gfp);
369 if (!new)
370 goto nomem;
371
372 new->key = sh_key->key;
373 sctp_auth_key_hold(new->key);
374 list_add(&new->key_list, &asoc->endpoint_shared_keys);
375 }
376
377 return 0;
378
379nomem:
380 sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
381 return -ENOMEM;
382}
383
384
385
386
387
388int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
389{
390 struct net *net = sock_net(asoc->base.sk);
391 struct sctp_auth_bytes *secret;
392 struct sctp_shared_key *ep_key;
393
394
395
396
397 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
398 return 0;
399
400
401
402
403
404
405 ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
406 BUG_ON(!ep_key);
407
408 secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
409 if (!secret)
410 return -ENOMEM;
411
412 sctp_auth_key_put(asoc->asoc_shared_key);
413 asoc->asoc_shared_key = secret;
414
415 return 0;
416}
417
418
419
420struct sctp_shared_key *sctp_auth_get_shkey(
421 const struct sctp_association *asoc,
422 __u16 key_id)
423{
424 struct sctp_shared_key *key;
425
426
427 key_for_each(key, &asoc->endpoint_shared_keys) {
428 if (key->key_id == key_id)
429 return key;
430 }
431
432 return NULL;
433}
434
435
436
437
438
439
440
441
442int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
443{
444 struct net *net = sock_net(ep->base.sk);
445 struct crypto_hash *tfm = NULL;
446 __u16 id;
447
448
449 if (!net->sctp.auth_enable) {
450 ep->auth_hmacs = NULL;
451 return 0;
452 }
453
454 if (ep->auth_hmacs)
455 return 0;
456
457
458 ep->auth_hmacs = kzalloc(
459 sizeof(struct crypto_hash *) * SCTP_AUTH_NUM_HMACS,
460 gfp);
461 if (!ep->auth_hmacs)
462 return -ENOMEM;
463
464 for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
465
466
467
468
469
470
471 if (!sctp_hmac_list[id].hmac_name)
472 continue;
473
474
475 if (ep->auth_hmacs[id])
476 continue;
477
478
479 tfm = crypto_alloc_hash(sctp_hmac_list[id].hmac_name, 0,
480 CRYPTO_ALG_ASYNC);
481 if (IS_ERR(tfm))
482 goto out_err;
483
484 ep->auth_hmacs[id] = tfm;
485 }
486
487 return 0;
488
489out_err:
490
491 sctp_auth_destroy_hmacs(ep->auth_hmacs);
492 return -ENOMEM;
493}
494
495
496void sctp_auth_destroy_hmacs(struct crypto_hash *auth_hmacs[])
497{
498 int i;
499
500 if (!auth_hmacs)
501 return;
502
503 for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++)
504 {
505 if (auth_hmacs[i])
506 crypto_free_hash(auth_hmacs[i]);
507 }
508 kfree(auth_hmacs);
509}
510
511
512struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
513{
514 return &sctp_hmac_list[hmac_id];
515}
516
517
518
519
520struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
521{
522 struct sctp_hmac_algo_param *hmacs;
523 __u16 n_elt;
524 __u16 id = 0;
525 int i;
526
527
528 if (asoc->default_hmac_id)
529 return &sctp_hmac_list[asoc->default_hmac_id];
530
531
532
533
534 hmacs = asoc->peer.peer_hmacs;
535 if (!hmacs)
536 return NULL;
537
538 n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1;
539 for (i = 0; i < n_elt; i++) {
540 id = ntohs(hmacs->hmac_ids[i]);
541
542
543 if (id > SCTP_AUTH_HMAC_ID_MAX) {
544 id = 0;
545 continue;
546 }
547
548
549
550
551
552
553 if (!sctp_hmac_list[id].hmac_name) {
554 id = 0;
555 continue;
556 }
557
558 break;
559 }
560
561 if (id == 0)
562 return NULL;
563
564 return &sctp_hmac_list[id];
565}
566
567static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
568{
569 int found = 0;
570 int i;
571
572 for (i = 0; i < n_elts; i++) {
573 if (hmac_id == hmacs[i]) {
574 found = 1;
575 break;
576 }
577 }
578
579 return found;
580}
581
582
583int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
584 __be16 hmac_id)
585{
586 struct sctp_hmac_algo_param *hmacs;
587 __u16 n_elt;
588
589 if (!asoc)
590 return 0;
591
592 hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
593 n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1;
594
595 return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
596}
597
598
599
600
601
602
603
604void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
605 struct sctp_hmac_algo_param *hmacs)
606{
607 struct sctp_endpoint *ep;
608 __u16 id;
609 int i;
610 int n_params;
611
612
613 if (asoc->default_hmac_id)
614 return;
615
616 n_params = (ntohs(hmacs->param_hdr.length)
617 - sizeof(sctp_paramhdr_t)) >> 1;
618 ep = asoc->ep;
619 for (i = 0; i < n_params; i++) {
620 id = ntohs(hmacs->hmac_ids[i]);
621
622
623 if (id > SCTP_AUTH_HMAC_ID_MAX)
624 continue;
625
626
627 if (ep->auth_hmacs[id]) {
628 asoc->default_hmac_id = id;
629 break;
630 }
631 }
632}
633
634
635
636static int __sctp_auth_cid(sctp_cid_t chunk, struct sctp_chunks_param *param)
637{
638 unsigned short len;
639 int found = 0;
640 int i;
641
642 if (!param || param->param_hdr.length == 0)
643 return 0;
644
645 len = ntohs(param->param_hdr.length) - sizeof(sctp_paramhdr_t);
646
647
648
649
650
651
652
653 for (i = 0; !found && i < len; i++) {
654 switch (param->chunks[i]) {
655 case SCTP_CID_INIT:
656 case SCTP_CID_INIT_ACK:
657 case SCTP_CID_SHUTDOWN_COMPLETE:
658 case SCTP_CID_AUTH:
659 break;
660
661 default:
662 if (param->chunks[i] == chunk)
663 found = 1;
664 break;
665 }
666 }
667
668 return found;
669}
670
671
672int sctp_auth_send_cid(sctp_cid_t chunk, const struct sctp_association *asoc)
673{
674 struct net *net;
675 if (!asoc)
676 return 0;
677
678 net = sock_net(asoc->base.sk);
679 if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
680 return 0;
681
682 return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
683}
684
685
686int sctp_auth_recv_cid(sctp_cid_t chunk, const struct sctp_association *asoc)
687{
688 struct net *net;
689 if (!asoc)
690 return 0;
691
692 net = sock_net(asoc->base.sk);
693 if (!net->sctp.auth_enable)
694 return 0;
695
696 return __sctp_auth_cid(chunk,
697 (struct sctp_chunks_param *)asoc->c.auth_chunks);
698}
699
700
701
702
703
704
705
706
707
708
709void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
710 struct sk_buff *skb,
711 struct sctp_auth_chunk *auth,
712 gfp_t gfp)
713{
714 struct scatterlist sg;
715 struct hash_desc desc;
716 struct sctp_auth_bytes *asoc_key;
717 __u16 key_id, hmac_id;
718 __u8 *digest;
719 unsigned char *end;
720 int free_key = 0;
721
722
723
724
725
726 key_id = ntohs(auth->auth_hdr.shkey_id);
727 hmac_id = ntohs(auth->auth_hdr.hmac_id);
728
729 if (key_id == asoc->active_key_id)
730 asoc_key = asoc->asoc_shared_key;
731 else {
732 struct sctp_shared_key *ep_key;
733
734 ep_key = sctp_auth_get_shkey(asoc, key_id);
735 if (!ep_key)
736 return;
737
738 asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
739 if (!asoc_key)
740 return;
741
742 free_key = 1;
743 }
744
745
746 end = skb_tail_pointer(skb);
747 sg_init_one(&sg, auth, end - (unsigned char *)auth);
748
749 desc.tfm = asoc->ep->auth_hmacs[hmac_id];
750 desc.flags = 0;
751
752 digest = auth->auth_hdr.hmac;
753 if (crypto_hash_setkey(desc.tfm, &asoc_key->data[0], asoc_key->len))
754 goto free;
755
756 crypto_hash_digest(&desc, &sg, sg.length, digest);
757
758free:
759 if (free_key)
760 sctp_auth_key_put(asoc_key);
761}
762
763
764
765
766int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
767{
768 struct sctp_chunks_param *p = ep->auth_chunk_list;
769 __u16 nchunks;
770 __u16 param_len;
771
772
773 if (__sctp_auth_cid(chunk_id, p))
774 return 0;
775
776
777 param_len = ntohs(p->param_hdr.length);
778 nchunks = param_len - sizeof(sctp_paramhdr_t);
779 if (nchunks == SCTP_NUM_CHUNK_TYPES)
780 return -EINVAL;
781
782 p->chunks[nchunks] = chunk_id;
783 p->param_hdr.length = htons(param_len + 1);
784 return 0;
785}
786
787
788int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
789 struct sctp_hmacalgo *hmacs)
790{
791 int has_sha1 = 0;
792 __u16 id;
793 int i;
794
795
796
797
798 for (i = 0; i < hmacs->shmac_num_idents; i++) {
799 id = hmacs->shmac_idents[i];
800
801 if (id > SCTP_AUTH_HMAC_ID_MAX)
802 return -EOPNOTSUPP;
803
804 if (SCTP_AUTH_HMAC_ID_SHA1 == id)
805 has_sha1 = 1;
806
807 if (!sctp_hmac_list[id].hmac_name)
808 return -EOPNOTSUPP;
809 }
810
811 if (!has_sha1)
812 return -EINVAL;
813
814 memcpy(ep->auth_hmacs_list->hmac_ids, &hmacs->shmac_idents[0],
815 hmacs->shmac_num_idents * sizeof(__u16));
816 ep->auth_hmacs_list->param_hdr.length = htons(sizeof(sctp_paramhdr_t) +
817 hmacs->shmac_num_idents * sizeof(__u16));
818 return 0;
819}
820
821
822
823
824
825int sctp_auth_set_key(struct sctp_endpoint *ep,
826 struct sctp_association *asoc,
827 struct sctp_authkey *auth_key)
828{
829 struct sctp_shared_key *cur_key = NULL;
830 struct sctp_auth_bytes *key;
831 struct list_head *sh_keys;
832 int replace = 0;
833
834
835
836
837 if (asoc)
838 sh_keys = &asoc->endpoint_shared_keys;
839 else
840 sh_keys = &ep->endpoint_shared_keys;
841
842 key_for_each(cur_key, sh_keys) {
843 if (cur_key->key_id == auth_key->sca_keynumber) {
844 replace = 1;
845 break;
846 }
847 }
848
849
850
851
852 if (!replace) {
853 cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber,
854 GFP_KERNEL);
855 if (!cur_key)
856 return -ENOMEM;
857 }
858
859
860 key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
861 if (!key)
862 goto nomem;
863
864 memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
865
866
867
868
869
870 if (replace)
871 sctp_auth_key_put(cur_key->key);
872 else
873 list_add(&cur_key->key_list, sh_keys);
874
875 cur_key->key = key;
876 sctp_auth_key_hold(key);
877
878 return 0;
879nomem:
880 if (!replace)
881 sctp_auth_shkey_free(cur_key);
882
883 return -ENOMEM;
884}
885
886int sctp_auth_set_active_key(struct sctp_endpoint *ep,
887 struct sctp_association *asoc,
888 __u16 key_id)
889{
890 struct sctp_shared_key *key;
891 struct list_head *sh_keys;
892 int found = 0;
893
894
895 if (asoc)
896 sh_keys = &asoc->endpoint_shared_keys;
897 else
898 sh_keys = &ep->endpoint_shared_keys;
899
900 key_for_each(key, sh_keys) {
901 if (key->key_id == key_id) {
902 found = 1;
903 break;
904 }
905 }
906
907 if (!found)
908 return -EINVAL;
909
910 if (asoc) {
911 asoc->active_key_id = key_id;
912 sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
913 } else
914 ep->active_key_id = key_id;
915
916 return 0;
917}
918
919int sctp_auth_del_key_id(struct sctp_endpoint *ep,
920 struct sctp_association *asoc,
921 __u16 key_id)
922{
923 struct sctp_shared_key *key;
924 struct list_head *sh_keys;
925 int found = 0;
926
927
928
929
930 if (asoc) {
931 if (asoc->active_key_id == key_id)
932 return -EINVAL;
933
934 sh_keys = &asoc->endpoint_shared_keys;
935 } else {
936 if (ep->active_key_id == key_id)
937 return -EINVAL;
938
939 sh_keys = &ep->endpoint_shared_keys;
940 }
941
942 key_for_each(key, sh_keys) {
943 if (key->key_id == key_id) {
944 found = 1;
945 break;
946 }
947 }
948
949 if (!found)
950 return -EINVAL;
951
952
953 list_del_init(&key->key_list);
954 sctp_auth_shkey_free(key);
955
956 return 0;
957}
958