auth.c 24 KB

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