auth.c 24 KB

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