xref: /linux/net/sctp/auth.c (revision f4cdf7ca9a1fdcca413157df19753f388a5a224e)
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 void 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;
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 
734 /* API Helpers */
735 
736 /* Add a chunk to the endpoint authenticated chunk list */
737 int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
738 {
739 	struct sctp_chunks_param *p = ep->auth_chunk_list;
740 	__u16 nchunks;
741 	__u16 param_len;
742 
743 	/* If this chunk is already specified, we are done */
744 	if (__sctp_auth_cid(chunk_id, p))
745 		return 0;
746 
747 	/* Check if we can add this chunk to the array */
748 	param_len = ntohs(p->param_hdr.length);
749 	nchunks = param_len - sizeof(struct sctp_paramhdr);
750 	if (nchunks == SCTP_AUTH_MAX_CHUNKS)
751 		return -EINVAL;
752 
753 	p->chunks[nchunks] = chunk_id;
754 	p->param_hdr.length = htons(param_len + 1);
755 	return 0;
756 }
757 
758 /* Add hmac identifires to the endpoint list of supported hmac ids */
759 int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
760 			   struct sctp_hmacalgo *hmacs)
761 {
762 	int has_sha1 = 0;
763 	__u16 id;
764 	int i;
765 
766 	/* Scan the list looking for unsupported id.  Also make sure that
767 	 * SHA1 is specified.
768 	 */
769 	for (i = 0; i < hmacs->shmac_num_idents; i++) {
770 		id = hmacs->shmac_idents[i];
771 
772 		if (!sctp_hmac_supported(id))
773 			return -EOPNOTSUPP;
774 
775 		if (SCTP_AUTH_HMAC_ID_SHA1 == id)
776 			has_sha1 = 1;
777 	}
778 
779 	if (!has_sha1)
780 		return -EINVAL;
781 
782 	for (i = 0; i < hmacs->shmac_num_idents; i++)
783 		ep->auth_hmacs_list->hmac_ids[i] =
784 				htons(hmacs->shmac_idents[i]);
785 	ep->auth_hmacs_list->param_hdr.length =
786 			htons(sizeof(struct sctp_paramhdr) +
787 			hmacs->shmac_num_idents * sizeof(__u16));
788 	return 0;
789 }
790 
791 /* Set a new shared key on either endpoint or association.  If the
792  * key with a same ID already exists, replace the key (remove the
793  * old key and add a new one).
794  */
795 int sctp_auth_set_key(struct sctp_endpoint *ep,
796 		      struct sctp_association *asoc,
797 		      struct sctp_authkey *auth_key)
798 {
799 	struct sctp_shared_key *cur_key, *shkey;
800 	struct sctp_auth_bytes *key;
801 	struct list_head *sh_keys;
802 	int replace = 0;
803 
804 	/* Try to find the given key id to see if
805 	 * we are doing a replace, or adding a new key
806 	 */
807 	if (asoc) {
808 		if (!asoc->peer.auth_capable)
809 			return -EACCES;
810 		sh_keys = &asoc->endpoint_shared_keys;
811 	} else {
812 		if (!ep->auth_enable)
813 			return -EACCES;
814 		sh_keys = &ep->endpoint_shared_keys;
815 	}
816 
817 	key_for_each(shkey, sh_keys) {
818 		if (shkey->key_id == auth_key->sca_keynumber) {
819 			replace = 1;
820 			break;
821 		}
822 	}
823 
824 	cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL);
825 	if (!cur_key)
826 		return -ENOMEM;
827 
828 	/* Create a new key data based on the info passed in */
829 	key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
830 	if (!key) {
831 		kfree(cur_key);
832 		return -ENOMEM;
833 	}
834 
835 	memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
836 	cur_key->key = key;
837 
838 	if (!replace) {
839 		list_add(&cur_key->key_list, sh_keys);
840 		return 0;
841 	}
842 
843 	list_del_init(&shkey->key_list);
844 	list_add(&cur_key->key_list, sh_keys);
845 
846 	if (asoc && asoc->active_key_id == auth_key->sca_keynumber &&
847 	    sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) {
848 		list_del_init(&cur_key->key_list);
849 		sctp_auth_shkey_release(cur_key);
850 		list_add(&shkey->key_list, sh_keys);
851 		return -ENOMEM;
852 	}
853 
854 	sctp_auth_shkey_release(shkey);
855 	return 0;
856 }
857 
858 int sctp_auth_set_active_key(struct sctp_endpoint *ep,
859 			     struct sctp_association *asoc,
860 			     __u16  key_id)
861 {
862 	struct sctp_shared_key *key;
863 	struct list_head *sh_keys;
864 	int found = 0;
865 
866 	/* The key identifier MUST correst to an existing key */
867 	if (asoc) {
868 		if (!asoc->peer.auth_capable)
869 			return -EACCES;
870 		sh_keys = &asoc->endpoint_shared_keys;
871 	} else {
872 		if (!ep->auth_enable)
873 			return -EACCES;
874 		sh_keys = &ep->endpoint_shared_keys;
875 	}
876 
877 	key_for_each(key, sh_keys) {
878 		if (key->key_id == key_id) {
879 			found = 1;
880 			break;
881 		}
882 	}
883 
884 	if (!found || key->deactivated)
885 		return -EINVAL;
886 
887 	if (asoc) {
888 		__u16  active_key_id = asoc->active_key_id;
889 
890 		asoc->active_key_id = key_id;
891 		if (sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL)) {
892 			asoc->active_key_id = active_key_id;
893 			return -ENOMEM;
894 		}
895 	} else
896 		ep->active_key_id = key_id;
897 
898 	return 0;
899 }
900 
901 int sctp_auth_del_key_id(struct sctp_endpoint *ep,
902 			 struct sctp_association *asoc,
903 			 __u16  key_id)
904 {
905 	struct sctp_shared_key *key;
906 	struct list_head *sh_keys;
907 	int found = 0;
908 
909 	/* The key identifier MUST NOT be the current active key
910 	 * The key identifier MUST correst to an existing key
911 	 */
912 	if (asoc) {
913 		if (!asoc->peer.auth_capable)
914 			return -EACCES;
915 		if (asoc->active_key_id == key_id)
916 			return -EINVAL;
917 
918 		sh_keys = &asoc->endpoint_shared_keys;
919 	} else {
920 		if (!ep->auth_enable)
921 			return -EACCES;
922 		if (ep->active_key_id == key_id)
923 			return -EINVAL;
924 
925 		sh_keys = &ep->endpoint_shared_keys;
926 	}
927 
928 	key_for_each(key, sh_keys) {
929 		if (key->key_id == key_id) {
930 			found = 1;
931 			break;
932 		}
933 	}
934 
935 	if (!found)
936 		return -EINVAL;
937 
938 	/* Delete the shared key */
939 	list_del_init(&key->key_list);
940 	sctp_auth_shkey_release(key);
941 
942 	return 0;
943 }
944 
945 int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
946 			   struct sctp_association *asoc, __u16  key_id)
947 {
948 	struct sctp_shared_key *key;
949 	struct list_head *sh_keys;
950 	int found = 0;
951 
952 	/* The key identifier MUST NOT be the current active key
953 	 * The key identifier MUST correst to an existing key
954 	 */
955 	if (asoc) {
956 		if (!asoc->peer.auth_capable)
957 			return -EACCES;
958 		if (asoc->active_key_id == key_id)
959 			return -EINVAL;
960 
961 		sh_keys = &asoc->endpoint_shared_keys;
962 	} else {
963 		if (!ep->auth_enable)
964 			return -EACCES;
965 		if (ep->active_key_id == key_id)
966 			return -EINVAL;
967 
968 		sh_keys = &ep->endpoint_shared_keys;
969 	}
970 
971 	key_for_each(key, sh_keys) {
972 		if (key->key_id == key_id) {
973 			found = 1;
974 			break;
975 		}
976 	}
977 
978 	if (!found)
979 		return -EINVAL;
980 
981 	/* refcnt == 1 and !list_empty mean it's not being used anywhere
982 	 * and deactivated will be set, so it's time to notify userland
983 	 * that this shkey can be freed.
984 	 */
985 	if (asoc && !list_empty(&key->key_list) &&
986 	    refcount_read(&key->refcnt) == 1) {
987 		struct sctp_ulpevent *ev;
988 
989 		ev = sctp_ulpevent_make_authkey(asoc, key->key_id,
990 						SCTP_AUTH_FREE_KEY, GFP_KERNEL);
991 		if (ev)
992 			asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
993 	}
994 
995 	key->deactivated = 1;
996 
997 	return 0;
998 }
999 
1000 int sctp_auth_init(struct sctp_endpoint *ep, gfp_t gfp)
1001 {
1002 	/* Allocate space for HMACS and CHUNKS authentication
1003 	 * variables.  There are arrays that we encode directly
1004 	 * into parameters to make the rest of the operations easier.
1005 	 */
1006 	if (!ep->auth_hmacs_list) {
1007 		struct sctp_hmac_algo_param *auth_hmacs;
1008 
1009 		auth_hmacs = kzalloc_flex(*auth_hmacs, hmac_ids,
1010 					  SCTP_AUTH_NUM_HMACS, gfp);
1011 		if (!auth_hmacs)
1012 			goto nomem;
1013 		/* Initialize the HMACS parameter.
1014 		 * SCTP-AUTH: Section 3.3
1015 		 *    Every endpoint supporting SCTP chunk authentication MUST
1016 		 *    support the HMAC based on the SHA-1 algorithm.
1017 		 */
1018 		auth_hmacs->param_hdr.type = SCTP_PARAM_HMAC_ALGO;
1019 		auth_hmacs->param_hdr.length =
1020 				htons(sizeof(struct sctp_paramhdr) + 2);
1021 		auth_hmacs->hmac_ids[0] = htons(SCTP_AUTH_HMAC_ID_SHA1);
1022 		ep->auth_hmacs_list = auth_hmacs;
1023 	}
1024 
1025 	if (!ep->auth_chunk_list) {
1026 		struct sctp_chunks_param *auth_chunks;
1027 
1028 		auth_chunks = kzalloc(sizeof(*auth_chunks) +
1029 				      SCTP_NUM_CHUNK_TYPES, gfp);
1030 		if (!auth_chunks)
1031 			goto nomem;
1032 		/* Initialize the CHUNKS parameter */
1033 		auth_chunks->param_hdr.type = SCTP_PARAM_CHUNKS;
1034 		auth_chunks->param_hdr.length =
1035 				htons(sizeof(struct sctp_paramhdr));
1036 		ep->auth_chunk_list = auth_chunks;
1037 	}
1038 
1039 	return 0;
1040 
1041 nomem:
1042 	/* Free all allocations */
1043 	kfree(ep->auth_hmacs_list);
1044 	kfree(ep->auth_chunk_list);
1045 	ep->auth_hmacs_list = NULL;
1046 	ep->auth_chunk_list = NULL;
1047 	return -ENOMEM;
1048 }
1049 
1050 void sctp_auth_free(struct sctp_endpoint *ep)
1051 {
1052 	kfree(ep->auth_hmacs_list);
1053 	kfree(ep->auth_chunk_list);
1054 	ep->auth_hmacs_list = NULL;
1055 	ep->auth_chunk_list = NULL;
1056 }
1057