auth.c 25 KB

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