auth.c 24 KB

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