auth.c 25 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012
  1. /* SCTP kernel implementation
  2. * (C) Copyright 2007 Hewlett-Packard Development Company, L.P.
  3. *
  4. * This file is part of the SCTP kernel implementation
  5. *
  6. * This SCTP implementation is free software;
  7. * you can redistribute it and/or modify it under the terms of
  8. * the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2, or (at your option)
  10. * any later version.
  11. *
  12. * This SCTP implementation is distributed in the hope that it
  13. * will be useful, but WITHOUT ANY WARRANTY; without even the implied
  14. * ************************
  15. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  16. * See the GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with GNU CC; see the file COPYING. If not, see
  20. * <http://www.gnu.org/licenses/>.
  21. *
  22. * Please send any bug reports or fixes you make to the
  23. * email address(es):
  24. * lksctp developers <linux-sctp@vger.kernel.org>
  25. *
  26. * Written or modified by:
  27. * Vlad Yasevich <vladislav.yasevich@hp.com>
  28. */
  29. #include <crypto/hash.h>
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/scatterlist.h>
  33. #include <net/sctp/sctp.h>
  34. #include <net/sctp/auth.h>
  35. static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
  36. {
  37. /* id 0 is reserved. as all 0 */
  38. .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
  39. },
  40. {
  41. .hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
  42. .hmac_name = "hmac(sha1)",
  43. .hmac_len = SCTP_SHA1_SIG_SIZE,
  44. },
  45. {
  46. /* id 2 is reserved as well */
  47. .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
  48. },
  49. #if IS_ENABLED(CONFIG_CRYPTO_SHA256)
  50. {
  51. .hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
  52. .hmac_name = "hmac(sha256)",
  53. .hmac_len = SCTP_SHA256_SIG_SIZE,
  54. }
  55. #endif
  56. };
  57. void sctp_auth_key_put(struct sctp_auth_bytes *key)
  58. {
  59. if (!key)
  60. return;
  61. if (refcount_dec_and_test(&key->refcnt)) {
  62. kzfree(key);
  63. SCTP_DBG_OBJCNT_DEC(keys);
  64. }
  65. }
  66. /* Create a new key structure of a given length */
  67. static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
  68. {
  69. struct sctp_auth_bytes *key;
  70. /* Verify that we are not going to overflow INT_MAX */
  71. if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
  72. return NULL;
  73. /* Allocate the shared key */
  74. key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
  75. if (!key)
  76. return NULL;
  77. key->len = key_len;
  78. refcount_set(&key->refcnt, 1);
  79. SCTP_DBG_OBJCNT_INC(keys);
  80. return key;
  81. }
  82. /* Create a new shared key container with a give key id */
  83. struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
  84. {
  85. struct sctp_shared_key *new;
  86. /* Allocate the shared key container */
  87. new = kzalloc(sizeof(struct sctp_shared_key), gfp);
  88. if (!new)
  89. return NULL;
  90. INIT_LIST_HEAD(&new->key_list);
  91. refcount_set(&new->refcnt, 1);
  92. new->key_id = key_id;
  93. return new;
  94. }
  95. /* Free the shared key structure */
  96. static void sctp_auth_shkey_destroy(struct sctp_shared_key *sh_key)
  97. {
  98. BUG_ON(!list_empty(&sh_key->key_list));
  99. sctp_auth_key_put(sh_key->key);
  100. sh_key->key = NULL;
  101. kfree(sh_key);
  102. }
  103. void sctp_auth_shkey_release(struct sctp_shared_key *sh_key)
  104. {
  105. if (refcount_dec_and_test(&sh_key->refcnt))
  106. sctp_auth_shkey_destroy(sh_key);
  107. }
  108. void sctp_auth_shkey_hold(struct sctp_shared_key *sh_key)
  109. {
  110. refcount_inc(&sh_key->refcnt);
  111. }
  112. /* Destroy the entire key list. This is done during the
  113. * associon and endpoint free process.
  114. */
  115. void sctp_auth_destroy_keys(struct list_head *keys)
  116. {
  117. struct sctp_shared_key *ep_key;
  118. struct sctp_shared_key *tmp;
  119. if (list_empty(keys))
  120. return;
  121. key_for_each_safe(ep_key, tmp, keys) {
  122. list_del_init(&ep_key->key_list);
  123. sctp_auth_shkey_release(ep_key);
  124. }
  125. }
  126. /* Compare two byte vectors as numbers. Return values
  127. * are:
  128. * 0 - vectors are equal
  129. * < 0 - vector 1 is smaller than vector2
  130. * > 0 - vector 1 is greater than vector2
  131. *
  132. * Algorithm is:
  133. * This is performed by selecting the numerically smaller key vector...
  134. * If the key vectors are equal as numbers but differ in length ...
  135. * the shorter vector is considered smaller
  136. *
  137. * Examples (with small values):
  138. * 000123456789 > 123456789 (first number is longer)
  139. * 000123456789 < 234567891 (second number is larger numerically)
  140. * 123456789 > 2345678 (first number is both larger & longer)
  141. */
  142. static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
  143. struct sctp_auth_bytes *vector2)
  144. {
  145. int diff;
  146. int i;
  147. const __u8 *longer;
  148. diff = vector1->len - vector2->len;
  149. if (diff) {
  150. longer = (diff > 0) ? vector1->data : vector2->data;
  151. /* Check to see if the longer number is
  152. * lead-zero padded. If it is not, it
  153. * is automatically larger numerically.
  154. */
  155. for (i = 0; i < abs(diff); i++) {
  156. if (longer[i] != 0)
  157. return diff;
  158. }
  159. }
  160. /* lengths are the same, compare numbers */
  161. return memcmp(vector1->data, vector2->data, vector1->len);
  162. }
  163. /*
  164. * Create a key vector as described in SCTP-AUTH, Section 6.1
  165. * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
  166. * parameter sent by each endpoint are concatenated as byte vectors.
  167. * These parameters include the parameter type, parameter length, and
  168. * the parameter value, but padding is omitted; all padding MUST be
  169. * removed from this concatenation before proceeding with further
  170. * computation of keys. Parameters which were not sent are simply
  171. * omitted from the concatenation process. The resulting two vectors
  172. * are called the two key vectors.
  173. */
  174. static struct sctp_auth_bytes *sctp_auth_make_key_vector(
  175. struct sctp_random_param *random,
  176. struct sctp_chunks_param *chunks,
  177. struct sctp_hmac_algo_param *hmacs,
  178. gfp_t gfp)
  179. {
  180. struct sctp_auth_bytes *new;
  181. __u32 len;
  182. __u32 offset = 0;
  183. __u16 random_len, hmacs_len, chunks_len = 0;
  184. random_len = ntohs(random->param_hdr.length);
  185. hmacs_len = ntohs(hmacs->param_hdr.length);
  186. if (chunks)
  187. chunks_len = ntohs(chunks->param_hdr.length);
  188. len = random_len + hmacs_len + chunks_len;
  189. new = sctp_auth_create_key(len, gfp);
  190. if (!new)
  191. return NULL;
  192. memcpy(new->data, random, random_len);
  193. offset += random_len;
  194. if (chunks) {
  195. memcpy(new->data + offset, chunks, chunks_len);
  196. offset += chunks_len;
  197. }
  198. memcpy(new->data + offset, hmacs, hmacs_len);
  199. return new;
  200. }
  201. /* Make a key vector based on our local parameters */
  202. static struct sctp_auth_bytes *sctp_auth_make_local_vector(
  203. const struct sctp_association *asoc,
  204. gfp_t gfp)
  205. {
  206. return sctp_auth_make_key_vector(
  207. (struct sctp_random_param *)asoc->c.auth_random,
  208. (struct sctp_chunks_param *)asoc->c.auth_chunks,
  209. (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs, gfp);
  210. }
  211. /* Make a key vector based on peer's parameters */
  212. static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
  213. const struct sctp_association *asoc,
  214. gfp_t gfp)
  215. {
  216. return sctp_auth_make_key_vector(asoc->peer.peer_random,
  217. asoc->peer.peer_chunks,
  218. asoc->peer.peer_hmacs,
  219. gfp);
  220. }
  221. /* Set the value of the association shared key base on the parameters
  222. * given. The algorithm is:
  223. * From the endpoint pair shared keys and the key vectors the
  224. * association shared keys are computed. This is performed by selecting
  225. * the numerically smaller key vector and concatenating it to the
  226. * endpoint pair shared key, and then concatenating the numerically
  227. * larger key vector to that. The result of the concatenation is the
  228. * association shared key.
  229. */
  230. static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
  231. struct sctp_shared_key *ep_key,
  232. struct sctp_auth_bytes *first_vector,
  233. struct sctp_auth_bytes *last_vector,
  234. gfp_t gfp)
  235. {
  236. struct sctp_auth_bytes *secret;
  237. __u32 offset = 0;
  238. __u32 auth_len;
  239. auth_len = first_vector->len + last_vector->len;
  240. if (ep_key->key)
  241. auth_len += ep_key->key->len;
  242. secret = sctp_auth_create_key(auth_len, gfp);
  243. if (!secret)
  244. return NULL;
  245. if (ep_key->key) {
  246. memcpy(secret->data, ep_key->key->data, ep_key->key->len);
  247. offset += ep_key->key->len;
  248. }
  249. memcpy(secret->data + offset, first_vector->data, first_vector->len);
  250. offset += first_vector->len;
  251. memcpy(secret->data + offset, last_vector->data, last_vector->len);
  252. return secret;
  253. }
  254. /* Create an association shared key. Follow the algorithm
  255. * described in SCTP-AUTH, Section 6.1
  256. */
  257. static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
  258. const struct sctp_association *asoc,
  259. struct sctp_shared_key *ep_key,
  260. gfp_t gfp)
  261. {
  262. struct sctp_auth_bytes *local_key_vector;
  263. struct sctp_auth_bytes *peer_key_vector;
  264. struct sctp_auth_bytes *first_vector,
  265. *last_vector;
  266. struct sctp_auth_bytes *secret = NULL;
  267. int cmp;
  268. /* Now we need to build the key vectors
  269. * SCTP-AUTH , Section 6.1
  270. * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
  271. * parameter sent by each endpoint are concatenated as byte vectors.
  272. * These parameters include the parameter type, parameter length, and
  273. * the parameter value, but padding is omitted; all padding MUST be
  274. * removed from this concatenation before proceeding with further
  275. * computation of keys. Parameters which were not sent are simply
  276. * omitted from the concatenation process. The resulting two vectors
  277. * are called the two key vectors.
  278. */
  279. local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
  280. peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
  281. if (!peer_key_vector || !local_key_vector)
  282. goto out;
  283. /* Figure out the order in which the key_vectors will be
  284. * added to the endpoint shared key.
  285. * SCTP-AUTH, Section 6.1:
  286. * This is performed by selecting the numerically smaller key
  287. * vector and concatenating it to the endpoint pair shared
  288. * key, and then concatenating the numerically larger key
  289. * vector to that. If the key vectors are equal as numbers
  290. * but differ in length, then the concatenation order is the
  291. * endpoint shared key, followed by the shorter key vector,
  292. * followed by the longer key vector. Otherwise, the key
  293. * vectors are identical, and may be concatenated to the
  294. * endpoint pair key in any order.
  295. */
  296. cmp = sctp_auth_compare_vectors(local_key_vector,
  297. peer_key_vector);
  298. if (cmp < 0) {
  299. first_vector = local_key_vector;
  300. last_vector = peer_key_vector;
  301. } else {
  302. first_vector = peer_key_vector;
  303. last_vector = local_key_vector;
  304. }
  305. secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
  306. gfp);
  307. out:
  308. sctp_auth_key_put(local_key_vector);
  309. sctp_auth_key_put(peer_key_vector);
  310. return secret;
  311. }
  312. /*
  313. * Populate the association overlay list with the list
  314. * from the endpoint.
  315. */
  316. int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
  317. struct sctp_association *asoc,
  318. gfp_t gfp)
  319. {
  320. struct sctp_shared_key *sh_key;
  321. struct sctp_shared_key *new;
  322. BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
  323. key_for_each(sh_key, &ep->endpoint_shared_keys) {
  324. new = sctp_auth_shkey_create(sh_key->key_id, gfp);
  325. if (!new)
  326. goto nomem;
  327. new->key = sh_key->key;
  328. sctp_auth_key_hold(new->key);
  329. list_add(&new->key_list, &asoc->endpoint_shared_keys);
  330. }
  331. return 0;
  332. nomem:
  333. sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
  334. return -ENOMEM;
  335. }
  336. /* Public interface to create the association shared key.
  337. * See code above for the algorithm.
  338. */
  339. int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
  340. {
  341. struct sctp_auth_bytes *secret;
  342. struct sctp_shared_key *ep_key;
  343. struct sctp_chunk *chunk;
  344. /* If we don't support AUTH, or peer is not capable
  345. * we don't need to do anything.
  346. */
  347. if (!asoc->ep->auth_enable || !asoc->peer.auth_capable)
  348. return 0;
  349. /* If the key_id is non-zero and we couldn't find an
  350. * endpoint pair shared key, we can't compute the
  351. * secret.
  352. * For key_id 0, endpoint pair shared key is a NULL key.
  353. */
  354. ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
  355. BUG_ON(!ep_key);
  356. secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
  357. if (!secret)
  358. return -ENOMEM;
  359. sctp_auth_key_put(asoc->asoc_shared_key);
  360. asoc->asoc_shared_key = secret;
  361. asoc->shkey = ep_key;
  362. /* Update send queue in case any chunk already in there now
  363. * needs authenticating
  364. */
  365. list_for_each_entry(chunk, &asoc->outqueue.out_chunk_list, list) {
  366. if (sctp_auth_send_cid(chunk->chunk_hdr->type, asoc)) {
  367. chunk->auth = 1;
  368. if (!chunk->shkey) {
  369. chunk->shkey = asoc->shkey;
  370. sctp_auth_shkey_hold(chunk->shkey);
  371. }
  372. }
  373. }
  374. return 0;
  375. }
  376. /* Find the endpoint pair shared key based on the key_id */
  377. struct sctp_shared_key *sctp_auth_get_shkey(
  378. const struct sctp_association *asoc,
  379. __u16 key_id)
  380. {
  381. struct sctp_shared_key *key;
  382. /* First search associations set of endpoint pair shared keys */
  383. key_for_each(key, &asoc->endpoint_shared_keys) {
  384. if (key->key_id == key_id) {
  385. if (!key->deactivated)
  386. return key;
  387. break;
  388. }
  389. }
  390. return NULL;
  391. }
  392. /*
  393. * Initialize all the possible digest transforms that we can use. Right now
  394. * now, the supported digests are SHA1 and SHA256. We do this here once
  395. * because of the restrictiong that transforms may only be allocated in
  396. * user context. This forces us to pre-allocated all possible transforms
  397. * at the endpoint init time.
  398. */
  399. int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
  400. {
  401. struct crypto_shash *tfm = NULL;
  402. __u16 id;
  403. /* If AUTH extension is disabled, we are done */
  404. if (!ep->auth_enable) {
  405. ep->auth_hmacs = NULL;
  406. return 0;
  407. }
  408. /* If the transforms are already allocated, we are done */
  409. if (ep->auth_hmacs)
  410. return 0;
  411. /* Allocated the array of pointers to transorms */
  412. ep->auth_hmacs = kzalloc(sizeof(struct crypto_shash *) *
  413. SCTP_AUTH_NUM_HMACS, gfp);
  414. if (!ep->auth_hmacs)
  415. return -ENOMEM;
  416. for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
  417. /* See is we support the id. Supported IDs have name and
  418. * length fields set, so that we can allocated and use
  419. * them. We can safely just check for name, for without the
  420. * name, we can't allocate the TFM.
  421. */
  422. if (!sctp_hmac_list[id].hmac_name)
  423. continue;
  424. /* If this TFM has been allocated, we are all set */
  425. if (ep->auth_hmacs[id])
  426. continue;
  427. /* Allocate the ID */
  428. tfm = crypto_alloc_shash(sctp_hmac_list[id].hmac_name, 0, 0);
  429. if (IS_ERR(tfm))
  430. goto out_err;
  431. ep->auth_hmacs[id] = tfm;
  432. }
  433. return 0;
  434. out_err:
  435. /* Clean up any successful allocations */
  436. sctp_auth_destroy_hmacs(ep->auth_hmacs);
  437. return -ENOMEM;
  438. }
  439. /* Destroy the hmac tfm array */
  440. void sctp_auth_destroy_hmacs(struct crypto_shash *auth_hmacs[])
  441. {
  442. int i;
  443. if (!auth_hmacs)
  444. return;
  445. for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++) {
  446. crypto_free_shash(auth_hmacs[i]);
  447. }
  448. kfree(auth_hmacs);
  449. }
  450. struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
  451. {
  452. return &sctp_hmac_list[hmac_id];
  453. }
  454. /* Get an hmac description information that we can use to build
  455. * the AUTH chunk
  456. */
  457. struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
  458. {
  459. struct sctp_hmac_algo_param *hmacs;
  460. __u16 n_elt;
  461. __u16 id = 0;
  462. int i;
  463. /* If we have a default entry, use it */
  464. if (asoc->default_hmac_id)
  465. return &sctp_hmac_list[asoc->default_hmac_id];
  466. /* Since we do not have a default entry, find the first entry
  467. * we support and return that. Do not cache that id.
  468. */
  469. hmacs = asoc->peer.peer_hmacs;
  470. if (!hmacs)
  471. return NULL;
  472. n_elt = (ntohs(hmacs->param_hdr.length) -
  473. sizeof(struct sctp_paramhdr)) >> 1;
  474. for (i = 0; i < n_elt; i++) {
  475. id = ntohs(hmacs->hmac_ids[i]);
  476. /* Check the id is in the supported range. And
  477. * see if we support the id. Supported IDs have name and
  478. * length fields set, so that we can allocate and use
  479. * them. We can safely just check for name, for without the
  480. * name, we can't allocate the TFM.
  481. */
  482. if (id > SCTP_AUTH_HMAC_ID_MAX ||
  483. !sctp_hmac_list[id].hmac_name) {
  484. id = 0;
  485. continue;
  486. }
  487. break;
  488. }
  489. if (id == 0)
  490. return NULL;
  491. return &sctp_hmac_list[id];
  492. }
  493. static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
  494. {
  495. int found = 0;
  496. int i;
  497. for (i = 0; i < n_elts; i++) {
  498. if (hmac_id == hmacs[i]) {
  499. found = 1;
  500. break;
  501. }
  502. }
  503. return found;
  504. }
  505. /* See if the HMAC_ID is one that we claim as supported */
  506. int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
  507. __be16 hmac_id)
  508. {
  509. struct sctp_hmac_algo_param *hmacs;
  510. __u16 n_elt;
  511. if (!asoc)
  512. return 0;
  513. hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
  514. n_elt = (ntohs(hmacs->param_hdr.length) -
  515. sizeof(struct sctp_paramhdr)) >> 1;
  516. return __sctp_auth_find_hmacid(hmacs->hmac_ids, n_elt, hmac_id);
  517. }
  518. /* Cache the default HMAC id. This to follow this text from SCTP-AUTH:
  519. * Section 6.1:
  520. * The receiver of a HMAC-ALGO parameter SHOULD use the first listed
  521. * algorithm it supports.
  522. */
  523. void sctp_auth_asoc_set_default_hmac(struct sctp_association *asoc,
  524. struct sctp_hmac_algo_param *hmacs)
  525. {
  526. struct sctp_endpoint *ep;
  527. __u16 id;
  528. int i;
  529. int n_params;
  530. /* if the default id is already set, use it */
  531. if (asoc->default_hmac_id)
  532. return;
  533. n_params = (ntohs(hmacs->param_hdr.length) -
  534. sizeof(struct sctp_paramhdr)) >> 1;
  535. ep = asoc->ep;
  536. for (i = 0; i < n_params; i++) {
  537. id = ntohs(hmacs->hmac_ids[i]);
  538. /* Check the id is in the supported range */
  539. if (id > SCTP_AUTH_HMAC_ID_MAX)
  540. continue;
  541. /* If this TFM has been allocated, use this id */
  542. if (ep->auth_hmacs[id]) {
  543. asoc->default_hmac_id = id;
  544. break;
  545. }
  546. }
  547. }
  548. /* Check to see if the given chunk is supposed to be authenticated */
  549. static int __sctp_auth_cid(enum sctp_cid chunk, struct sctp_chunks_param *param)
  550. {
  551. unsigned short len;
  552. int found = 0;
  553. int i;
  554. if (!param || param->param_hdr.length == 0)
  555. return 0;
  556. len = ntohs(param->param_hdr.length) - sizeof(struct sctp_paramhdr);
  557. /* SCTP-AUTH, Section 3.2
  558. * The chunk types for INIT, INIT-ACK, SHUTDOWN-COMPLETE and AUTH
  559. * chunks MUST NOT be listed in the CHUNKS parameter. However, if
  560. * a CHUNKS parameter is received then the types for INIT, INIT-ACK,
  561. * SHUTDOWN-COMPLETE and AUTH chunks MUST be ignored.
  562. */
  563. for (i = 0; !found && i < len; i++) {
  564. switch (param->chunks[i]) {
  565. case SCTP_CID_INIT:
  566. case SCTP_CID_INIT_ACK:
  567. case SCTP_CID_SHUTDOWN_COMPLETE:
  568. case SCTP_CID_AUTH:
  569. break;
  570. default:
  571. if (param->chunks[i] == chunk)
  572. found = 1;
  573. break;
  574. }
  575. }
  576. return found;
  577. }
  578. /* Check if peer requested that this chunk is authenticated */
  579. int sctp_auth_send_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
  580. {
  581. if (!asoc)
  582. return 0;
  583. if (!asoc->ep->auth_enable || !asoc->peer.auth_capable)
  584. return 0;
  585. return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
  586. }
  587. /* Check if we requested that peer authenticate this chunk. */
  588. int sctp_auth_recv_cid(enum sctp_cid chunk, const struct sctp_association *asoc)
  589. {
  590. if (!asoc)
  591. return 0;
  592. if (!asoc->ep->auth_enable)
  593. return 0;
  594. return __sctp_auth_cid(chunk,
  595. (struct sctp_chunks_param *)asoc->c.auth_chunks);
  596. }
  597. /* SCTP-AUTH: Section 6.2:
  598. * The sender MUST calculate the MAC as described in RFC2104 [2] using
  599. * the hash function H as described by the MAC Identifier and the shared
  600. * association key K based on the endpoint pair shared key described by
  601. * the shared key identifier. The 'data' used for the computation of
  602. * the AUTH-chunk is given by the AUTH chunk with its HMAC field set to
  603. * zero (as shown in Figure 6) followed by all chunks that are placed
  604. * after the AUTH chunk in the SCTP packet.
  605. */
  606. void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
  607. struct sk_buff *skb, struct sctp_auth_chunk *auth,
  608. struct sctp_shared_key *ep_key, gfp_t gfp)
  609. {
  610. struct sctp_auth_bytes *asoc_key;
  611. struct crypto_shash *tfm;
  612. __u16 key_id, hmac_id;
  613. unsigned char *end;
  614. int free_key = 0;
  615. __u8 *digest;
  616. /* Extract the info we need:
  617. * - hmac id
  618. * - key id
  619. */
  620. key_id = ntohs(auth->auth_hdr.shkey_id);
  621. hmac_id = ntohs(auth->auth_hdr.hmac_id);
  622. if (key_id == asoc->active_key_id)
  623. asoc_key = asoc->asoc_shared_key;
  624. else {
  625. /* ep_key can't be NULL here */
  626. asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
  627. if (!asoc_key)
  628. return;
  629. free_key = 1;
  630. }
  631. /* set up scatter list */
  632. end = skb_tail_pointer(skb);
  633. tfm = asoc->ep->auth_hmacs[hmac_id];
  634. digest = auth->auth_hdr.hmac;
  635. if (crypto_shash_setkey(tfm, &asoc_key->data[0], asoc_key->len))
  636. goto free;
  637. {
  638. SHASH_DESC_ON_STACK(desc, tfm);
  639. desc->tfm = tfm;
  640. desc->flags = 0;
  641. crypto_shash_digest(desc, (u8 *)auth,
  642. end - (unsigned char *)auth, digest);
  643. shash_desc_zero(desc);
  644. }
  645. free:
  646. if (free_key)
  647. sctp_auth_key_put(asoc_key);
  648. }
  649. /* API Helpers */
  650. /* Add a chunk to the endpoint authenticated chunk list */
  651. int sctp_auth_ep_add_chunkid(struct sctp_endpoint *ep, __u8 chunk_id)
  652. {
  653. struct sctp_chunks_param *p = ep->auth_chunk_list;
  654. __u16 nchunks;
  655. __u16 param_len;
  656. /* If this chunk is already specified, we are done */
  657. if (__sctp_auth_cid(chunk_id, p))
  658. return 0;
  659. /* Check if we can add this chunk to the array */
  660. param_len = ntohs(p->param_hdr.length);
  661. nchunks = param_len - sizeof(struct sctp_paramhdr);
  662. if (nchunks == SCTP_NUM_CHUNK_TYPES)
  663. return -EINVAL;
  664. p->chunks[nchunks] = chunk_id;
  665. p->param_hdr.length = htons(param_len + 1);
  666. return 0;
  667. }
  668. /* Add hmac identifires to the endpoint list of supported hmac ids */
  669. int sctp_auth_ep_set_hmacs(struct sctp_endpoint *ep,
  670. struct sctp_hmacalgo *hmacs)
  671. {
  672. int has_sha1 = 0;
  673. __u16 id;
  674. int i;
  675. /* Scan the list looking for unsupported id. Also make sure that
  676. * SHA1 is specified.
  677. */
  678. for (i = 0; i < hmacs->shmac_num_idents; i++) {
  679. id = hmacs->shmac_idents[i];
  680. if (id > SCTP_AUTH_HMAC_ID_MAX)
  681. return -EOPNOTSUPP;
  682. if (SCTP_AUTH_HMAC_ID_SHA1 == id)
  683. has_sha1 = 1;
  684. if (!sctp_hmac_list[id].hmac_name)
  685. return -EOPNOTSUPP;
  686. }
  687. if (!has_sha1)
  688. return -EINVAL;
  689. for (i = 0; i < hmacs->shmac_num_idents; i++)
  690. ep->auth_hmacs_list->hmac_ids[i] =
  691. htons(hmacs->shmac_idents[i]);
  692. ep->auth_hmacs_list->param_hdr.length =
  693. htons(sizeof(struct sctp_paramhdr) +
  694. hmacs->shmac_num_idents * sizeof(__u16));
  695. return 0;
  696. }
  697. /* Set a new shared key on either endpoint or association. If the
  698. * the key with a same ID already exists, replace the key (remove the
  699. * old key and add a new one).
  700. */
  701. int sctp_auth_set_key(struct sctp_endpoint *ep,
  702. struct sctp_association *asoc,
  703. struct sctp_authkey *auth_key)
  704. {
  705. struct sctp_shared_key *cur_key, *shkey;
  706. struct sctp_auth_bytes *key;
  707. struct list_head *sh_keys;
  708. int replace = 0;
  709. /* Try to find the given key id to see if
  710. * we are doing a replace, or adding a new key
  711. */
  712. if (asoc)
  713. sh_keys = &asoc->endpoint_shared_keys;
  714. else
  715. sh_keys = &ep->endpoint_shared_keys;
  716. key_for_each(shkey, sh_keys) {
  717. if (shkey->key_id == auth_key->sca_keynumber) {
  718. replace = 1;
  719. break;
  720. }
  721. }
  722. cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber, GFP_KERNEL);
  723. if (!cur_key)
  724. return -ENOMEM;
  725. /* Create a new key data based on the info passed in */
  726. key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
  727. if (!key) {
  728. kfree(cur_key);
  729. return -ENOMEM;
  730. }
  731. memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
  732. cur_key->key = key;
  733. if (replace) {
  734. list_del_init(&shkey->key_list);
  735. sctp_auth_shkey_release(shkey);
  736. }
  737. list_add(&cur_key->key_list, sh_keys);
  738. return 0;
  739. }
  740. int sctp_auth_set_active_key(struct sctp_endpoint *ep,
  741. struct sctp_association *asoc,
  742. __u16 key_id)
  743. {
  744. struct sctp_shared_key *key;
  745. struct list_head *sh_keys;
  746. int found = 0;
  747. /* The key identifier MUST correst to an existing key */
  748. if (asoc)
  749. sh_keys = &asoc->endpoint_shared_keys;
  750. else
  751. sh_keys = &ep->endpoint_shared_keys;
  752. key_for_each(key, sh_keys) {
  753. if (key->key_id == key_id) {
  754. found = 1;
  755. break;
  756. }
  757. }
  758. if (!found || key->deactivated)
  759. return -EINVAL;
  760. if (asoc) {
  761. asoc->active_key_id = key_id;
  762. sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
  763. } else
  764. ep->active_key_id = key_id;
  765. return 0;
  766. }
  767. int sctp_auth_del_key_id(struct sctp_endpoint *ep,
  768. struct sctp_association *asoc,
  769. __u16 key_id)
  770. {
  771. struct sctp_shared_key *key;
  772. struct list_head *sh_keys;
  773. int found = 0;
  774. /* The key identifier MUST NOT be the current active key
  775. * The key identifier MUST correst to an existing key
  776. */
  777. if (asoc) {
  778. if (asoc->active_key_id == key_id)
  779. return -EINVAL;
  780. sh_keys = &asoc->endpoint_shared_keys;
  781. } else {
  782. if (ep->active_key_id == key_id)
  783. return -EINVAL;
  784. sh_keys = &ep->endpoint_shared_keys;
  785. }
  786. key_for_each(key, sh_keys) {
  787. if (key->key_id == key_id) {
  788. found = 1;
  789. break;
  790. }
  791. }
  792. if (!found)
  793. return -EINVAL;
  794. /* Delete the shared key */
  795. list_del_init(&key->key_list);
  796. sctp_auth_shkey_release(key);
  797. return 0;
  798. }
  799. int sctp_auth_deact_key_id(struct sctp_endpoint *ep,
  800. struct sctp_association *asoc, __u16 key_id)
  801. {
  802. struct sctp_shared_key *key;
  803. struct list_head *sh_keys;
  804. int found = 0;
  805. /* The key identifier MUST NOT be the current active key
  806. * The key identifier MUST correst to an existing key
  807. */
  808. if (asoc) {
  809. if (asoc->active_key_id == key_id)
  810. return -EINVAL;
  811. sh_keys = &asoc->endpoint_shared_keys;
  812. } else {
  813. if (ep->active_key_id == key_id)
  814. return -EINVAL;
  815. sh_keys = &ep->endpoint_shared_keys;
  816. }
  817. key_for_each(key, sh_keys) {
  818. if (key->key_id == key_id) {
  819. found = 1;
  820. break;
  821. }
  822. }
  823. if (!found)
  824. return -EINVAL;
  825. /* refcnt == 1 and !list_empty mean it's not being used anywhere
  826. * and deactivated will be set, so it's time to notify userland
  827. * that this shkey can be freed.
  828. */
  829. if (asoc && !list_empty(&key->key_list) &&
  830. refcount_read(&key->refcnt) == 1) {
  831. struct sctp_ulpevent *ev;
  832. ev = sctp_ulpevent_make_authkey(asoc, key->key_id,
  833. SCTP_AUTH_FREE_KEY, GFP_KERNEL);
  834. if (ev)
  835. asoc->stream.si->enqueue_event(&asoc->ulpq, ev);
  836. }
  837. key->deactivated = 1;
  838. return 0;
  839. }