auth.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957
  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, write to
  20. * the Free Software Foundation, 59 Temple Place - Suite 330,
  21. * Boston, MA 02111-1307, USA.
  22. *
  23. * Please send any bug reports or fixes you make to the
  24. * email address(es):
  25. * lksctp developers <linux-sctp@vger.kernel.org>
  26. *
  27. * Written or modified by:
  28. * Vlad Yasevich <vladislav.yasevich@hp.com>
  29. */
  30. #include <linux/slab.h>
  31. #include <linux/types.h>
  32. #include <linux/crypto.h>
  33. #include <linux/scatterlist.h>
  34. #include <net/sctp/sctp.h>
  35. #include <net/sctp/auth.h>
  36. static struct sctp_hmac sctp_hmac_list[SCTP_AUTH_NUM_HMACS] = {
  37. {
  38. /* id 0 is reserved. as all 0 */
  39. .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_0,
  40. },
  41. {
  42. .hmac_id = SCTP_AUTH_HMAC_ID_SHA1,
  43. .hmac_name="hmac(sha1)",
  44. .hmac_len = SCTP_SHA1_SIG_SIZE,
  45. },
  46. {
  47. /* id 2 is reserved as well */
  48. .hmac_id = SCTP_AUTH_HMAC_ID_RESERVED_2,
  49. },
  50. #if defined (CONFIG_CRYPTO_SHA256) || defined (CONFIG_CRYPTO_SHA256_MODULE)
  51. {
  52. .hmac_id = SCTP_AUTH_HMAC_ID_SHA256,
  53. .hmac_name="hmac(sha256)",
  54. .hmac_len = SCTP_SHA256_SIG_SIZE,
  55. }
  56. #endif
  57. };
  58. void sctp_auth_key_put(struct sctp_auth_bytes *key)
  59. {
  60. if (!key)
  61. return;
  62. if (atomic_dec_and_test(&key->refcnt)) {
  63. kzfree(key);
  64. SCTP_DBG_OBJCNT_DEC(keys);
  65. }
  66. }
  67. /* Create a new key structure of a given length */
  68. static struct sctp_auth_bytes *sctp_auth_create_key(__u32 key_len, gfp_t gfp)
  69. {
  70. struct sctp_auth_bytes *key;
  71. /* Verify that we are not going to overflow INT_MAX */
  72. if (key_len > (INT_MAX - sizeof(struct sctp_auth_bytes)))
  73. return NULL;
  74. /* Allocate the shared key */
  75. key = kmalloc(sizeof(struct sctp_auth_bytes) + key_len, gfp);
  76. if (!key)
  77. return NULL;
  78. key->len = key_len;
  79. atomic_set(&key->refcnt, 1);
  80. SCTP_DBG_OBJCNT_INC(keys);
  81. return key;
  82. }
  83. /* Create a new shared key container with a give key id */
  84. struct sctp_shared_key *sctp_auth_shkey_create(__u16 key_id, gfp_t gfp)
  85. {
  86. struct sctp_shared_key *new;
  87. /* Allocate the shared key container */
  88. new = kzalloc(sizeof(struct sctp_shared_key), gfp);
  89. if (!new)
  90. return NULL;
  91. INIT_LIST_HEAD(&new->key_list);
  92. new->key_id = key_id;
  93. return new;
  94. }
  95. /* Free the shared key structure */
  96. static void sctp_auth_shkey_free(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. /* Destroy the entire key list. This is done during the
  104. * associon and endpoint free process.
  105. */
  106. void sctp_auth_destroy_keys(struct list_head *keys)
  107. {
  108. struct sctp_shared_key *ep_key;
  109. struct sctp_shared_key *tmp;
  110. if (list_empty(keys))
  111. return;
  112. key_for_each_safe(ep_key, tmp, keys) {
  113. list_del_init(&ep_key->key_list);
  114. sctp_auth_shkey_free(ep_key);
  115. }
  116. }
  117. /* Compare two byte vectors as numbers. Return values
  118. * are:
  119. * 0 - vectors are equal
  120. * < 0 - vector 1 is smaller than vector2
  121. * > 0 - vector 1 is greater than vector2
  122. *
  123. * Algorithm is:
  124. * This is performed by selecting the numerically smaller key vector...
  125. * If the key vectors are equal as numbers but differ in length ...
  126. * the shorter vector is considered smaller
  127. *
  128. * Examples (with small values):
  129. * 000123456789 > 123456789 (first number is longer)
  130. * 000123456789 < 234567891 (second number is larger numerically)
  131. * 123456789 > 2345678 (first number is both larger & longer)
  132. */
  133. static int sctp_auth_compare_vectors(struct sctp_auth_bytes *vector1,
  134. struct sctp_auth_bytes *vector2)
  135. {
  136. int diff;
  137. int i;
  138. const __u8 *longer;
  139. diff = vector1->len - vector2->len;
  140. if (diff) {
  141. longer = (diff > 0) ? vector1->data : vector2->data;
  142. /* Check to see if the longer number is
  143. * lead-zero padded. If it is not, it
  144. * is automatically larger numerically.
  145. */
  146. for (i = 0; i < abs(diff); i++ ) {
  147. if (longer[i] != 0)
  148. return diff;
  149. }
  150. }
  151. /* lengths are the same, compare numbers */
  152. return memcmp(vector1->data, vector2->data, vector1->len);
  153. }
  154. /*
  155. * Create a key vector as described in SCTP-AUTH, Section 6.1
  156. * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
  157. * parameter sent by each endpoint are concatenated as byte vectors.
  158. * These parameters include the parameter type, parameter length, and
  159. * the parameter value, but padding is omitted; all padding MUST be
  160. * removed from this concatenation before proceeding with further
  161. * computation of keys. Parameters which were not sent are simply
  162. * omitted from the concatenation process. The resulting two vectors
  163. * are called the two key vectors.
  164. */
  165. static struct sctp_auth_bytes *sctp_auth_make_key_vector(
  166. sctp_random_param_t *random,
  167. sctp_chunks_param_t *chunks,
  168. sctp_hmac_algo_param_t *hmacs,
  169. gfp_t gfp)
  170. {
  171. struct sctp_auth_bytes *new;
  172. __u32 len;
  173. __u32 offset = 0;
  174. __u16 random_len, hmacs_len, chunks_len = 0;
  175. random_len = ntohs(random->param_hdr.length);
  176. hmacs_len = ntohs(hmacs->param_hdr.length);
  177. if (chunks)
  178. chunks_len = ntohs(chunks->param_hdr.length);
  179. len = random_len + hmacs_len + chunks_len;
  180. new = sctp_auth_create_key(len, gfp);
  181. if (!new)
  182. return NULL;
  183. memcpy(new->data, random, random_len);
  184. offset += random_len;
  185. if (chunks) {
  186. memcpy(new->data + offset, chunks, chunks_len);
  187. offset += chunks_len;
  188. }
  189. memcpy(new->data + offset, hmacs, hmacs_len);
  190. return new;
  191. }
  192. /* Make a key vector based on our local parameters */
  193. static struct sctp_auth_bytes *sctp_auth_make_local_vector(
  194. const struct sctp_association *asoc,
  195. gfp_t gfp)
  196. {
  197. return sctp_auth_make_key_vector(
  198. (sctp_random_param_t*)asoc->c.auth_random,
  199. (sctp_chunks_param_t*)asoc->c.auth_chunks,
  200. (sctp_hmac_algo_param_t*)asoc->c.auth_hmacs,
  201. gfp);
  202. }
  203. /* Make a key vector based on peer's parameters */
  204. static struct sctp_auth_bytes *sctp_auth_make_peer_vector(
  205. const struct sctp_association *asoc,
  206. gfp_t gfp)
  207. {
  208. return sctp_auth_make_key_vector(asoc->peer.peer_random,
  209. asoc->peer.peer_chunks,
  210. asoc->peer.peer_hmacs,
  211. gfp);
  212. }
  213. /* Set the value of the association shared key base on the parameters
  214. * given. The algorithm is:
  215. * From the endpoint pair shared keys and the key vectors the
  216. * association shared keys are computed. This is performed by selecting
  217. * the numerically smaller key vector and concatenating it to the
  218. * endpoint pair shared key, and then concatenating the numerically
  219. * larger key vector to that. The result of the concatenation is the
  220. * association shared key.
  221. */
  222. static struct sctp_auth_bytes *sctp_auth_asoc_set_secret(
  223. struct sctp_shared_key *ep_key,
  224. struct sctp_auth_bytes *first_vector,
  225. struct sctp_auth_bytes *last_vector,
  226. gfp_t gfp)
  227. {
  228. struct sctp_auth_bytes *secret;
  229. __u32 offset = 0;
  230. __u32 auth_len;
  231. auth_len = first_vector->len + last_vector->len;
  232. if (ep_key->key)
  233. auth_len += ep_key->key->len;
  234. secret = sctp_auth_create_key(auth_len, gfp);
  235. if (!secret)
  236. return NULL;
  237. if (ep_key->key) {
  238. memcpy(secret->data, ep_key->key->data, ep_key->key->len);
  239. offset += ep_key->key->len;
  240. }
  241. memcpy(secret->data + offset, first_vector->data, first_vector->len);
  242. offset += first_vector->len;
  243. memcpy(secret->data + offset, last_vector->data, last_vector->len);
  244. return secret;
  245. }
  246. /* Create an association shared key. Follow the algorithm
  247. * described in SCTP-AUTH, Section 6.1
  248. */
  249. static struct sctp_auth_bytes *sctp_auth_asoc_create_secret(
  250. const struct sctp_association *asoc,
  251. struct sctp_shared_key *ep_key,
  252. gfp_t gfp)
  253. {
  254. struct sctp_auth_bytes *local_key_vector;
  255. struct sctp_auth_bytes *peer_key_vector;
  256. struct sctp_auth_bytes *first_vector,
  257. *last_vector;
  258. struct sctp_auth_bytes *secret = NULL;
  259. int cmp;
  260. /* Now we need to build the key vectors
  261. * SCTP-AUTH , Section 6.1
  262. * The RANDOM parameter, the CHUNKS parameter and the HMAC-ALGO
  263. * parameter sent by each endpoint are concatenated as byte vectors.
  264. * These parameters include the parameter type, parameter length, and
  265. * the parameter value, but padding is omitted; all padding MUST be
  266. * removed from this concatenation before proceeding with further
  267. * computation of keys. Parameters which were not sent are simply
  268. * omitted from the concatenation process. The resulting two vectors
  269. * are called the two key vectors.
  270. */
  271. local_key_vector = sctp_auth_make_local_vector(asoc, gfp);
  272. peer_key_vector = sctp_auth_make_peer_vector(asoc, gfp);
  273. if (!peer_key_vector || !local_key_vector)
  274. goto out;
  275. /* Figure out the order in which the key_vectors will be
  276. * added to the endpoint shared key.
  277. * SCTP-AUTH, Section 6.1:
  278. * This is performed by selecting the numerically smaller key
  279. * vector and concatenating it to the endpoint pair shared
  280. * key, and then concatenating the numerically larger key
  281. * vector to that. If the key vectors are equal as numbers
  282. * but differ in length, then the concatenation order is the
  283. * endpoint shared key, followed by the shorter key vector,
  284. * followed by the longer key vector. Otherwise, the key
  285. * vectors are identical, and may be concatenated to the
  286. * endpoint pair key in any order.
  287. */
  288. cmp = sctp_auth_compare_vectors(local_key_vector,
  289. peer_key_vector);
  290. if (cmp < 0) {
  291. first_vector = local_key_vector;
  292. last_vector = peer_key_vector;
  293. } else {
  294. first_vector = peer_key_vector;
  295. last_vector = local_key_vector;
  296. }
  297. secret = sctp_auth_asoc_set_secret(ep_key, first_vector, last_vector,
  298. gfp);
  299. out:
  300. sctp_auth_key_put(local_key_vector);
  301. sctp_auth_key_put(peer_key_vector);
  302. return secret;
  303. }
  304. /*
  305. * Populate the association overlay list with the list
  306. * from the endpoint.
  307. */
  308. int sctp_auth_asoc_copy_shkeys(const struct sctp_endpoint *ep,
  309. struct sctp_association *asoc,
  310. gfp_t gfp)
  311. {
  312. struct sctp_shared_key *sh_key;
  313. struct sctp_shared_key *new;
  314. BUG_ON(!list_empty(&asoc->endpoint_shared_keys));
  315. key_for_each(sh_key, &ep->endpoint_shared_keys) {
  316. new = sctp_auth_shkey_create(sh_key->key_id, gfp);
  317. if (!new)
  318. goto nomem;
  319. new->key = sh_key->key;
  320. sctp_auth_key_hold(new->key);
  321. list_add(&new->key_list, &asoc->endpoint_shared_keys);
  322. }
  323. return 0;
  324. nomem:
  325. sctp_auth_destroy_keys(&asoc->endpoint_shared_keys);
  326. return -ENOMEM;
  327. }
  328. /* Public interface to creat the association shared key.
  329. * See code above for the algorithm.
  330. */
  331. int sctp_auth_asoc_init_active_key(struct sctp_association *asoc, gfp_t gfp)
  332. {
  333. struct net *net = sock_net(asoc->base.sk);
  334. struct sctp_auth_bytes *secret;
  335. struct sctp_shared_key *ep_key;
  336. /* If we don't support AUTH, or peer is not capable
  337. * we don't need to do anything.
  338. */
  339. if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
  340. return 0;
  341. /* If the key_id is non-zero and we couldn't find an
  342. * endpoint pair shared key, we can't compute the
  343. * secret.
  344. * For key_id 0, endpoint pair shared key is a NULL key.
  345. */
  346. ep_key = sctp_auth_get_shkey(asoc, asoc->active_key_id);
  347. BUG_ON(!ep_key);
  348. secret = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
  349. if (!secret)
  350. return -ENOMEM;
  351. sctp_auth_key_put(asoc->asoc_shared_key);
  352. asoc->asoc_shared_key = secret;
  353. return 0;
  354. }
  355. /* Find the endpoint pair shared key based on the key_id */
  356. struct sctp_shared_key *sctp_auth_get_shkey(
  357. const struct sctp_association *asoc,
  358. __u16 key_id)
  359. {
  360. struct sctp_shared_key *key;
  361. /* First search associations set of endpoint pair shared keys */
  362. key_for_each(key, &asoc->endpoint_shared_keys) {
  363. if (key->key_id == key_id)
  364. return key;
  365. }
  366. return NULL;
  367. }
  368. /*
  369. * Initialize all the possible digest transforms that we can use. Right now
  370. * now, the supported digests are SHA1 and SHA256. We do this here once
  371. * because of the restrictiong that transforms may only be allocated in
  372. * user context. This forces us to pre-allocated all possible transforms
  373. * at the endpoint init time.
  374. */
  375. int sctp_auth_init_hmacs(struct sctp_endpoint *ep, gfp_t gfp)
  376. {
  377. struct net *net = sock_net(ep->base.sk);
  378. struct crypto_hash *tfm = NULL;
  379. __u16 id;
  380. /* if the transforms are already allocted, we are done */
  381. if (!net->sctp.auth_enable) {
  382. ep->auth_hmacs = NULL;
  383. return 0;
  384. }
  385. if (ep->auth_hmacs)
  386. return 0;
  387. /* Allocated the array of pointers to transorms */
  388. ep->auth_hmacs = kzalloc(
  389. sizeof(struct crypto_hash *) * SCTP_AUTH_NUM_HMACS,
  390. gfp);
  391. if (!ep->auth_hmacs)
  392. return -ENOMEM;
  393. for (id = 0; id < SCTP_AUTH_NUM_HMACS; id++) {
  394. /* See is we support the id. Supported IDs have name and
  395. * length fields set, so that we can allocated and use
  396. * them. We can safely just check for name, for without the
  397. * name, we can't allocate the TFM.
  398. */
  399. if (!sctp_hmac_list[id].hmac_name)
  400. continue;
  401. /* If this TFM has been allocated, we are all set */
  402. if (ep->auth_hmacs[id])
  403. continue;
  404. /* Allocate the ID */
  405. tfm = crypto_alloc_hash(sctp_hmac_list[id].hmac_name, 0,
  406. CRYPTO_ALG_ASYNC);
  407. if (IS_ERR(tfm))
  408. goto out_err;
  409. ep->auth_hmacs[id] = tfm;
  410. }
  411. return 0;
  412. out_err:
  413. /* Clean up any successful allocations */
  414. sctp_auth_destroy_hmacs(ep->auth_hmacs);
  415. return -ENOMEM;
  416. }
  417. /* Destroy the hmac tfm array */
  418. void sctp_auth_destroy_hmacs(struct crypto_hash *auth_hmacs[])
  419. {
  420. int i;
  421. if (!auth_hmacs)
  422. return;
  423. for (i = 0; i < SCTP_AUTH_NUM_HMACS; i++)
  424. {
  425. if (auth_hmacs[i])
  426. crypto_free_hash(auth_hmacs[i]);
  427. }
  428. kfree(auth_hmacs);
  429. }
  430. struct sctp_hmac *sctp_auth_get_hmac(__u16 hmac_id)
  431. {
  432. return &sctp_hmac_list[hmac_id];
  433. }
  434. /* Get an hmac description information that we can use to build
  435. * the AUTH chunk
  436. */
  437. struct sctp_hmac *sctp_auth_asoc_get_hmac(const struct sctp_association *asoc)
  438. {
  439. struct sctp_hmac_algo_param *hmacs;
  440. __u16 n_elt;
  441. __u16 id = 0;
  442. int i;
  443. /* If we have a default entry, use it */
  444. if (asoc->default_hmac_id)
  445. return &sctp_hmac_list[asoc->default_hmac_id];
  446. /* Since we do not have a default entry, find the first entry
  447. * we support and return that. Do not cache that id.
  448. */
  449. hmacs = asoc->peer.peer_hmacs;
  450. if (!hmacs)
  451. return NULL;
  452. n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 1;
  453. for (i = 0; i < n_elt; i++) {
  454. id = ntohs(hmacs->hmac_ids[i]);
  455. /* Check the id is in the supported range */
  456. if (id > SCTP_AUTH_HMAC_ID_MAX) {
  457. id = 0;
  458. continue;
  459. }
  460. /* See is we support the id. Supported IDs have name and
  461. * length fields set, so that we can allocated and use
  462. * them. We can safely just check for name, for without the
  463. * name, we can't allocate the TFM.
  464. */
  465. if (!sctp_hmac_list[id].hmac_name) {
  466. id = 0;
  467. continue;
  468. }
  469. break;
  470. }
  471. if (id == 0)
  472. return NULL;
  473. return &sctp_hmac_list[id];
  474. }
  475. static int __sctp_auth_find_hmacid(__be16 *hmacs, int n_elts, __be16 hmac_id)
  476. {
  477. int found = 0;
  478. int i;
  479. for (i = 0; i < n_elts; i++) {
  480. if (hmac_id == hmacs[i]) {
  481. found = 1;
  482. break;
  483. }
  484. }
  485. return found;
  486. }
  487. /* See if the HMAC_ID is one that we claim as supported */
  488. int sctp_auth_asoc_verify_hmac_id(const struct sctp_association *asoc,
  489. __be16 hmac_id)
  490. {
  491. struct sctp_hmac_algo_param *hmacs;
  492. __u16 n_elt;
  493. if (!asoc)
  494. return 0;
  495. hmacs = (struct sctp_hmac_algo_param *)asoc->c.auth_hmacs;
  496. n_elt = (ntohs(hmacs->param_hdr.length) - sizeof(sctp_paramhdr_t)) >> 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(sctp_paramhdr_t)) >> 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(sctp_cid_t 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(sctp_paramhdr_t);
  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(sctp_cid_t chunk, const struct sctp_association *asoc)
  561. {
  562. struct net *net;
  563. if (!asoc)
  564. return 0;
  565. net = sock_net(asoc->base.sk);
  566. if (!net->sctp.auth_enable || !asoc->peer.auth_capable)
  567. return 0;
  568. return __sctp_auth_cid(chunk, asoc->peer.peer_chunks);
  569. }
  570. /* Check if we requested that peer authenticate this chunk. */
  571. int sctp_auth_recv_cid(sctp_cid_t chunk, const struct sctp_association *asoc)
  572. {
  573. struct net *net;
  574. if (!asoc)
  575. return 0;
  576. net = sock_net(asoc->base.sk);
  577. if (!net->sctp.auth_enable)
  578. return 0;
  579. return __sctp_auth_cid(chunk,
  580. (struct sctp_chunks_param *)asoc->c.auth_chunks);
  581. }
  582. /* SCTP-AUTH: Section 6.2:
  583. * The sender MUST calculate the MAC as described in RFC2104 [2] using
  584. * the hash function H as described by the MAC Identifier and the shared
  585. * association key K based on the endpoint pair shared key described by
  586. * the shared key identifier. The 'data' used for the computation of
  587. * the AUTH-chunk is given by the AUTH chunk with its HMAC field set to
  588. * zero (as shown in Figure 6) followed by all chunks that are placed
  589. * after the AUTH chunk in the SCTP packet.
  590. */
  591. void sctp_auth_calculate_hmac(const struct sctp_association *asoc,
  592. struct sk_buff *skb,
  593. struct sctp_auth_chunk *auth,
  594. gfp_t gfp)
  595. {
  596. struct scatterlist sg;
  597. struct hash_desc desc;
  598. struct sctp_auth_bytes *asoc_key;
  599. __u16 key_id, hmac_id;
  600. __u8 *digest;
  601. unsigned char *end;
  602. int free_key = 0;
  603. /* Extract the info we need:
  604. * - hmac id
  605. * - key id
  606. */
  607. key_id = ntohs(auth->auth_hdr.shkey_id);
  608. hmac_id = ntohs(auth->auth_hdr.hmac_id);
  609. if (key_id == asoc->active_key_id)
  610. asoc_key = asoc->asoc_shared_key;
  611. else {
  612. struct sctp_shared_key *ep_key;
  613. ep_key = sctp_auth_get_shkey(asoc, key_id);
  614. if (!ep_key)
  615. return;
  616. asoc_key = sctp_auth_asoc_create_secret(asoc, ep_key, gfp);
  617. if (!asoc_key)
  618. return;
  619. free_key = 1;
  620. }
  621. /* set up scatter list */
  622. end = skb_tail_pointer(skb);
  623. sg_init_one(&sg, auth, end - (unsigned char *)auth);
  624. desc.tfm = asoc->ep->auth_hmacs[hmac_id];
  625. desc.flags = 0;
  626. digest = auth->auth_hdr.hmac;
  627. if (crypto_hash_setkey(desc.tfm, &asoc_key->data[0], asoc_key->len))
  628. goto free;
  629. crypto_hash_digest(&desc, &sg, sg.length, digest);
  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(sctp_paramhdr_t);
  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. memcpy(ep->auth_hmacs_list->hmac_ids, &hmacs->shmac_idents[0],
  675. hmacs->shmac_num_idents * sizeof(__u16));
  676. ep->auth_hmacs_list->param_hdr.length = htons(sizeof(sctp_paramhdr_t) +
  677. hmacs->shmac_num_idents * sizeof(__u16));
  678. return 0;
  679. }
  680. /* Set a new shared key on either endpoint or association. If the
  681. * the key with a same ID already exists, replace the key (remove the
  682. * old key and add a new one).
  683. */
  684. int sctp_auth_set_key(struct sctp_endpoint *ep,
  685. struct sctp_association *asoc,
  686. struct sctp_authkey *auth_key)
  687. {
  688. struct sctp_shared_key *cur_key = NULL;
  689. struct sctp_auth_bytes *key;
  690. struct list_head *sh_keys;
  691. int replace = 0;
  692. /* Try to find the given key id to see if
  693. * we are doing a replace, or adding a new key
  694. */
  695. if (asoc)
  696. sh_keys = &asoc->endpoint_shared_keys;
  697. else
  698. sh_keys = &ep->endpoint_shared_keys;
  699. key_for_each(cur_key, sh_keys) {
  700. if (cur_key->key_id == auth_key->sca_keynumber) {
  701. replace = 1;
  702. break;
  703. }
  704. }
  705. /* If we are not replacing a key id, we need to allocate
  706. * a shared key.
  707. */
  708. if (!replace) {
  709. cur_key = sctp_auth_shkey_create(auth_key->sca_keynumber,
  710. GFP_KERNEL);
  711. if (!cur_key)
  712. return -ENOMEM;
  713. }
  714. /* Create a new key data based on the info passed in */
  715. key = sctp_auth_create_key(auth_key->sca_keylength, GFP_KERNEL);
  716. if (!key)
  717. goto nomem;
  718. memcpy(key->data, &auth_key->sca_key[0], auth_key->sca_keylength);
  719. /* If we are replacing, remove the old keys data from the
  720. * key id. If we are adding new key id, add it to the
  721. * list.
  722. */
  723. if (replace)
  724. sctp_auth_key_put(cur_key->key);
  725. else
  726. list_add(&cur_key->key_list, sh_keys);
  727. cur_key->key = key;
  728. sctp_auth_key_hold(key);
  729. return 0;
  730. nomem:
  731. if (!replace)
  732. sctp_auth_shkey_free(cur_key);
  733. return -ENOMEM;
  734. }
  735. int sctp_auth_set_active_key(struct sctp_endpoint *ep,
  736. struct sctp_association *asoc,
  737. __u16 key_id)
  738. {
  739. struct sctp_shared_key *key;
  740. struct list_head *sh_keys;
  741. int found = 0;
  742. /* The key identifier MUST correst to an existing key */
  743. if (asoc)
  744. sh_keys = &asoc->endpoint_shared_keys;
  745. else
  746. sh_keys = &ep->endpoint_shared_keys;
  747. key_for_each(key, sh_keys) {
  748. if (key->key_id == key_id) {
  749. found = 1;
  750. break;
  751. }
  752. }
  753. if (!found)
  754. return -EINVAL;
  755. if (asoc) {
  756. asoc->active_key_id = key_id;
  757. sctp_auth_asoc_init_active_key(asoc, GFP_KERNEL);
  758. } else
  759. ep->active_key_id = key_id;
  760. return 0;
  761. }
  762. int sctp_auth_del_key_id(struct sctp_endpoint *ep,
  763. struct sctp_association *asoc,
  764. __u16 key_id)
  765. {
  766. struct sctp_shared_key *key;
  767. struct list_head *sh_keys;
  768. int found = 0;
  769. /* The key identifier MUST NOT be the current active key
  770. * The key identifier MUST correst to an existing key
  771. */
  772. if (asoc) {
  773. if (asoc->active_key_id == key_id)
  774. return -EINVAL;
  775. sh_keys = &asoc->endpoint_shared_keys;
  776. } else {
  777. if (ep->active_key_id == key_id)
  778. return -EINVAL;
  779. sh_keys = &ep->endpoint_shared_keys;
  780. }
  781. key_for_each(key, sh_keys) {
  782. if (key->key_id == key_id) {
  783. found = 1;
  784. break;
  785. }
  786. }
  787. if (!found)
  788. return -EINVAL;
  789. /* Delete the shared key */
  790. list_del_init(&key->key_list);
  791. sctp_auth_shkey_free(key);
  792. return 0;
  793. }