rxkad.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Kerberos-based RxRPC security
  3. *
  4. * Copyright (C) 2007 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  8. #include <crypto/skcipher.h>
  9. #include <linux/module.h>
  10. #include <linux/net.h>
  11. #include <linux/skbuff.h>
  12. #include <linux/udp.h>
  13. #include <linux/scatterlist.h>
  14. #include <linux/ctype.h>
  15. #include <linux/slab.h>
  16. #include <net/sock.h>
  17. #include <net/af_rxrpc.h>
  18. #include <keys/rxrpc-type.h>
  19. #include "ar-internal.h"
  20. #define RXKAD_VERSION 2
  21. #define MAXKRB5TICKETLEN 1024
  22. #define RXKAD_TKT_TYPE_KERBEROS_V5 256
  23. #define ANAME_SZ 40 /* size of authentication name */
  24. #define INST_SZ 40 /* size of principal's instance */
  25. #define REALM_SZ 40 /* size of principal's auth domain */
  26. #define SNAME_SZ 40 /* size of service name */
  27. struct rxkad_level1_hdr {
  28. __be32 data_size; /* true data size (excluding padding) */
  29. };
  30. struct rxkad_level2_hdr {
  31. __be32 data_size; /* true data size (excluding padding) */
  32. __be32 checksum; /* decrypted data checksum */
  33. };
  34. /*
  35. * this holds a pinned cipher so that keventd doesn't get called by the cipher
  36. * alloc routine, but since we have it to hand, we use it to decrypt RESPONSE
  37. * packets
  38. */
  39. static struct crypto_sync_skcipher *rxkad_ci;
  40. static struct skcipher_request *rxkad_ci_req;
  41. static DEFINE_MUTEX(rxkad_ci_mutex);
  42. /*
  43. * initialise connection security
  44. */
  45. static int rxkad_init_connection_security(struct rxrpc_connection *conn)
  46. {
  47. struct crypto_sync_skcipher *ci;
  48. struct rxrpc_key_token *token;
  49. int ret;
  50. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->params.key));
  51. token = conn->params.key->payload.data[0];
  52. conn->security_ix = token->security_index;
  53. ci = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
  54. if (IS_ERR(ci)) {
  55. _debug("no cipher");
  56. ret = PTR_ERR(ci);
  57. goto error;
  58. }
  59. if (crypto_sync_skcipher_setkey(ci, token->kad->session_key,
  60. sizeof(token->kad->session_key)) < 0)
  61. BUG();
  62. switch (conn->params.security_level) {
  63. case RXRPC_SECURITY_PLAIN:
  64. break;
  65. case RXRPC_SECURITY_AUTH:
  66. conn->size_align = 8;
  67. conn->security_size = sizeof(struct rxkad_level1_hdr);
  68. break;
  69. case RXRPC_SECURITY_ENCRYPT:
  70. conn->size_align = 8;
  71. conn->security_size = sizeof(struct rxkad_level2_hdr);
  72. break;
  73. default:
  74. ret = -EKEYREJECTED;
  75. goto error;
  76. }
  77. conn->cipher = ci;
  78. ret = 0;
  79. error:
  80. _leave(" = %d", ret);
  81. return ret;
  82. }
  83. /*
  84. * prime the encryption state with the invariant parts of a connection's
  85. * description
  86. */
  87. static int rxkad_prime_packet_security(struct rxrpc_connection *conn)
  88. {
  89. struct skcipher_request *req;
  90. struct rxrpc_key_token *token;
  91. struct scatterlist sg;
  92. struct rxrpc_crypt iv;
  93. __be32 *tmpbuf;
  94. size_t tmpsize = 4 * sizeof(__be32);
  95. _enter("");
  96. if (!conn->params.key)
  97. return 0;
  98. tmpbuf = kmalloc(tmpsize, GFP_KERNEL);
  99. if (!tmpbuf)
  100. return -ENOMEM;
  101. req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
  102. if (!req) {
  103. kfree(tmpbuf);
  104. return -ENOMEM;
  105. }
  106. token = conn->params.key->payload.data[0];
  107. memcpy(&iv, token->kad->session_key, sizeof(iv));
  108. tmpbuf[0] = htonl(conn->proto.epoch);
  109. tmpbuf[1] = htonl(conn->proto.cid);
  110. tmpbuf[2] = 0;
  111. tmpbuf[3] = htonl(conn->security_ix);
  112. sg_init_one(&sg, tmpbuf, tmpsize);
  113. skcipher_request_set_sync_tfm(req, conn->cipher);
  114. skcipher_request_set_callback(req, 0, NULL, NULL);
  115. skcipher_request_set_crypt(req, &sg, &sg, tmpsize, iv.x);
  116. crypto_skcipher_encrypt(req);
  117. skcipher_request_free(req);
  118. memcpy(&conn->csum_iv, tmpbuf + 2, sizeof(conn->csum_iv));
  119. kfree(tmpbuf);
  120. _leave(" = 0");
  121. return 0;
  122. }
  123. /*
  124. * Allocate and prepare the crypto request on a call. For any particular call,
  125. * this is called serially for the packets, so no lock should be necessary.
  126. */
  127. static struct skcipher_request *rxkad_get_call_crypto(struct rxrpc_call *call)
  128. {
  129. struct crypto_skcipher *tfm = &call->conn->cipher->base;
  130. struct skcipher_request *cipher_req = call->cipher_req;
  131. if (!cipher_req) {
  132. cipher_req = skcipher_request_alloc(tfm, GFP_NOFS);
  133. if (!cipher_req)
  134. return NULL;
  135. call->cipher_req = cipher_req;
  136. }
  137. return cipher_req;
  138. }
  139. /*
  140. * Clean up the crypto on a call.
  141. */
  142. static void rxkad_free_call_crypto(struct rxrpc_call *call)
  143. {
  144. if (call->cipher_req)
  145. skcipher_request_free(call->cipher_req);
  146. call->cipher_req = NULL;
  147. }
  148. /*
  149. * partially encrypt a packet (level 1 security)
  150. */
  151. static int rxkad_secure_packet_auth(const struct rxrpc_call *call,
  152. struct sk_buff *skb,
  153. u32 data_size,
  154. void *sechdr,
  155. struct skcipher_request *req)
  156. {
  157. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  158. struct rxkad_level1_hdr hdr;
  159. struct rxrpc_crypt iv;
  160. struct scatterlist sg;
  161. u16 check;
  162. _enter("");
  163. check = sp->hdr.seq ^ call->call_id;
  164. data_size |= (u32)check << 16;
  165. hdr.data_size = htonl(data_size);
  166. memcpy(sechdr, &hdr, sizeof(hdr));
  167. /* start the encryption afresh */
  168. memset(&iv, 0, sizeof(iv));
  169. sg_init_one(&sg, sechdr, 8);
  170. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  171. skcipher_request_set_callback(req, 0, NULL, NULL);
  172. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  173. crypto_skcipher_encrypt(req);
  174. skcipher_request_zero(req);
  175. _leave(" = 0");
  176. return 0;
  177. }
  178. /*
  179. * wholly encrypt a packet (level 2 security)
  180. */
  181. static int rxkad_secure_packet_encrypt(const struct rxrpc_call *call,
  182. struct sk_buff *skb,
  183. u32 data_size,
  184. void *sechdr,
  185. struct skcipher_request *req)
  186. {
  187. const struct rxrpc_key_token *token;
  188. struct rxkad_level2_hdr rxkhdr;
  189. struct rxrpc_skb_priv *sp;
  190. struct rxrpc_crypt iv;
  191. struct scatterlist sg[16];
  192. unsigned int len;
  193. u16 check;
  194. int err;
  195. sp = rxrpc_skb(skb);
  196. _enter("");
  197. check = sp->hdr.seq ^ call->call_id;
  198. rxkhdr.data_size = htonl(data_size | (u32)check << 16);
  199. rxkhdr.checksum = 0;
  200. memcpy(sechdr, &rxkhdr, sizeof(rxkhdr));
  201. /* encrypt from the session key */
  202. token = call->conn->params.key->payload.data[0];
  203. memcpy(&iv, token->kad->session_key, sizeof(iv));
  204. sg_init_one(&sg[0], sechdr, sizeof(rxkhdr));
  205. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  206. skcipher_request_set_callback(req, 0, NULL, NULL);
  207. skcipher_request_set_crypt(req, &sg[0], &sg[0], sizeof(rxkhdr), iv.x);
  208. crypto_skcipher_encrypt(req);
  209. /* we want to encrypt the skbuff in-place */
  210. err = -EMSGSIZE;
  211. if (skb_shinfo(skb)->nr_frags > 16)
  212. goto out;
  213. len = data_size + call->conn->size_align - 1;
  214. len &= ~(call->conn->size_align - 1);
  215. sg_init_table(sg, ARRAY_SIZE(sg));
  216. err = skb_to_sgvec(skb, sg, 0, len);
  217. if (unlikely(err < 0))
  218. goto out;
  219. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  220. crypto_skcipher_encrypt(req);
  221. _leave(" = 0");
  222. err = 0;
  223. out:
  224. skcipher_request_zero(req);
  225. return err;
  226. }
  227. /*
  228. * checksum an RxRPC packet header
  229. */
  230. static int rxkad_secure_packet(struct rxrpc_call *call,
  231. struct sk_buff *skb,
  232. size_t data_size,
  233. void *sechdr)
  234. {
  235. struct rxrpc_skb_priv *sp;
  236. struct skcipher_request *req;
  237. struct rxrpc_crypt iv;
  238. struct scatterlist sg;
  239. u32 x, y;
  240. int ret;
  241. sp = rxrpc_skb(skb);
  242. _enter("{%d{%x}},{#%u},%zu,",
  243. call->debug_id, key_serial(call->conn->params.key),
  244. sp->hdr.seq, data_size);
  245. if (!call->conn->cipher)
  246. return 0;
  247. ret = key_validate(call->conn->params.key);
  248. if (ret < 0)
  249. return ret;
  250. req = rxkad_get_call_crypto(call);
  251. if (!req)
  252. return -ENOMEM;
  253. /* continue encrypting from where we left off */
  254. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  255. /* calculate the security checksum */
  256. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  257. x |= sp->hdr.seq & 0x3fffffff;
  258. call->crypto_buf[0] = htonl(call->call_id);
  259. call->crypto_buf[1] = htonl(x);
  260. sg_init_one(&sg, call->crypto_buf, 8);
  261. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  262. skcipher_request_set_callback(req, 0, NULL, NULL);
  263. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  264. crypto_skcipher_encrypt(req);
  265. skcipher_request_zero(req);
  266. y = ntohl(call->crypto_buf[1]);
  267. y = (y >> 16) & 0xffff;
  268. if (y == 0)
  269. y = 1; /* zero checksums are not permitted */
  270. sp->hdr.cksum = y;
  271. switch (call->conn->params.security_level) {
  272. case RXRPC_SECURITY_PLAIN:
  273. ret = 0;
  274. break;
  275. case RXRPC_SECURITY_AUTH:
  276. ret = rxkad_secure_packet_auth(call, skb, data_size, sechdr,
  277. req);
  278. break;
  279. case RXRPC_SECURITY_ENCRYPT:
  280. ret = rxkad_secure_packet_encrypt(call, skb, data_size,
  281. sechdr, req);
  282. break;
  283. default:
  284. ret = -EPERM;
  285. break;
  286. }
  287. _leave(" = %d [set %hx]", ret, y);
  288. return ret;
  289. }
  290. /*
  291. * decrypt partial encryption on a packet (level 1 security)
  292. */
  293. static int rxkad_verify_packet_1(struct rxrpc_call *call, struct sk_buff *skb,
  294. unsigned int offset, unsigned int len,
  295. rxrpc_seq_t seq,
  296. struct skcipher_request *req)
  297. {
  298. struct rxkad_level1_hdr sechdr;
  299. struct rxrpc_crypt iv;
  300. struct scatterlist sg[16];
  301. bool aborted;
  302. u32 data_size, buf;
  303. u16 check;
  304. int ret;
  305. _enter("");
  306. if (len < 8) {
  307. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_hdr", "V1H",
  308. RXKADSEALEDINCON);
  309. goto protocol_error;
  310. }
  311. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  312. * directly into the target buffer.
  313. */
  314. sg_init_table(sg, ARRAY_SIZE(sg));
  315. ret = skb_to_sgvec(skb, sg, offset, 8);
  316. if (unlikely(ret < 0))
  317. return ret;
  318. /* start the decryption afresh */
  319. memset(&iv, 0, sizeof(iv));
  320. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  321. skcipher_request_set_callback(req, 0, NULL, NULL);
  322. skcipher_request_set_crypt(req, sg, sg, 8, iv.x);
  323. crypto_skcipher_decrypt(req);
  324. skcipher_request_zero(req);
  325. /* Extract the decrypted packet length */
  326. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  327. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_len", "XV1",
  328. RXKADDATALEN);
  329. goto protocol_error;
  330. }
  331. offset += sizeof(sechdr);
  332. len -= sizeof(sechdr);
  333. buf = ntohl(sechdr.data_size);
  334. data_size = buf & 0xffff;
  335. check = buf >> 16;
  336. check ^= seq ^ call->call_id;
  337. check &= 0xffff;
  338. if (check != 0) {
  339. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_check", "V1C",
  340. RXKADSEALEDINCON);
  341. goto protocol_error;
  342. }
  343. if (data_size > len) {
  344. aborted = rxrpc_abort_eproto(call, skb, "rxkad_1_datalen", "V1L",
  345. RXKADDATALEN);
  346. goto protocol_error;
  347. }
  348. _leave(" = 0 [dlen=%x]", data_size);
  349. return 0;
  350. protocol_error:
  351. if (aborted)
  352. rxrpc_send_abort_packet(call);
  353. return -EPROTO;
  354. }
  355. /*
  356. * wholly decrypt a packet (level 2 security)
  357. */
  358. static int rxkad_verify_packet_2(struct rxrpc_call *call, struct sk_buff *skb,
  359. unsigned int offset, unsigned int len,
  360. rxrpc_seq_t seq,
  361. struct skcipher_request *req)
  362. {
  363. const struct rxrpc_key_token *token;
  364. struct rxkad_level2_hdr sechdr;
  365. struct rxrpc_crypt iv;
  366. struct scatterlist _sg[4], *sg;
  367. bool aborted;
  368. u32 data_size, buf;
  369. u16 check;
  370. int nsg, ret;
  371. _enter(",{%d}", skb->len);
  372. if (len < 8) {
  373. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_hdr", "V2H",
  374. RXKADSEALEDINCON);
  375. goto protocol_error;
  376. }
  377. /* Decrypt the skbuff in-place. TODO: We really want to decrypt
  378. * directly into the target buffer.
  379. */
  380. sg = _sg;
  381. nsg = skb_shinfo(skb)->nr_frags;
  382. if (nsg <= 4) {
  383. nsg = 4;
  384. } else {
  385. sg = kmalloc_array(nsg, sizeof(*sg), GFP_NOIO);
  386. if (!sg)
  387. goto nomem;
  388. }
  389. sg_init_table(sg, nsg);
  390. ret = skb_to_sgvec(skb, sg, offset, len);
  391. if (unlikely(ret < 0)) {
  392. if (sg != _sg)
  393. kfree(sg);
  394. return ret;
  395. }
  396. /* decrypt from the session key */
  397. token = call->conn->params.key->payload.data[0];
  398. memcpy(&iv, token->kad->session_key, sizeof(iv));
  399. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  400. skcipher_request_set_callback(req, 0, NULL, NULL);
  401. skcipher_request_set_crypt(req, sg, sg, len, iv.x);
  402. crypto_skcipher_decrypt(req);
  403. skcipher_request_zero(req);
  404. if (sg != _sg)
  405. kfree(sg);
  406. /* Extract the decrypted packet length */
  407. if (skb_copy_bits(skb, offset, &sechdr, sizeof(sechdr)) < 0) {
  408. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_len", "XV2",
  409. RXKADDATALEN);
  410. goto protocol_error;
  411. }
  412. offset += sizeof(sechdr);
  413. len -= sizeof(sechdr);
  414. buf = ntohl(sechdr.data_size);
  415. data_size = buf & 0xffff;
  416. check = buf >> 16;
  417. check ^= seq ^ call->call_id;
  418. check &= 0xffff;
  419. if (check != 0) {
  420. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_check", "V2C",
  421. RXKADSEALEDINCON);
  422. goto protocol_error;
  423. }
  424. if (data_size > len) {
  425. aborted = rxrpc_abort_eproto(call, skb, "rxkad_2_datalen", "V2L",
  426. RXKADDATALEN);
  427. goto protocol_error;
  428. }
  429. _leave(" = 0 [dlen=%x]", data_size);
  430. return 0;
  431. protocol_error:
  432. if (aborted)
  433. rxrpc_send_abort_packet(call);
  434. return -EPROTO;
  435. nomem:
  436. _leave(" = -ENOMEM");
  437. return -ENOMEM;
  438. }
  439. /*
  440. * Verify the security on a received packet or subpacket (if part of a
  441. * jumbo packet).
  442. */
  443. static int rxkad_verify_packet(struct rxrpc_call *call, struct sk_buff *skb,
  444. unsigned int offset, unsigned int len,
  445. rxrpc_seq_t seq, u16 expected_cksum)
  446. {
  447. struct skcipher_request *req;
  448. struct rxrpc_crypt iv;
  449. struct scatterlist sg;
  450. bool aborted;
  451. u16 cksum;
  452. u32 x, y;
  453. _enter("{%d{%x}},{#%u}",
  454. call->debug_id, key_serial(call->conn->params.key), seq);
  455. if (!call->conn->cipher)
  456. return 0;
  457. req = rxkad_get_call_crypto(call);
  458. if (!req)
  459. return -ENOMEM;
  460. /* continue encrypting from where we left off */
  461. memcpy(&iv, call->conn->csum_iv.x, sizeof(iv));
  462. /* validate the security checksum */
  463. x = (call->cid & RXRPC_CHANNELMASK) << (32 - RXRPC_CIDSHIFT);
  464. x |= seq & 0x3fffffff;
  465. call->crypto_buf[0] = htonl(call->call_id);
  466. call->crypto_buf[1] = htonl(x);
  467. sg_init_one(&sg, call->crypto_buf, 8);
  468. skcipher_request_set_sync_tfm(req, call->conn->cipher);
  469. skcipher_request_set_callback(req, 0, NULL, NULL);
  470. skcipher_request_set_crypt(req, &sg, &sg, 8, iv.x);
  471. crypto_skcipher_encrypt(req);
  472. skcipher_request_zero(req);
  473. y = ntohl(call->crypto_buf[1]);
  474. cksum = (y >> 16) & 0xffff;
  475. if (cksum == 0)
  476. cksum = 1; /* zero checksums are not permitted */
  477. if (cksum != expected_cksum) {
  478. aborted = rxrpc_abort_eproto(call, skb, "rxkad_csum", "VCK",
  479. RXKADSEALEDINCON);
  480. goto protocol_error;
  481. }
  482. switch (call->conn->params.security_level) {
  483. case RXRPC_SECURITY_PLAIN:
  484. return 0;
  485. case RXRPC_SECURITY_AUTH:
  486. return rxkad_verify_packet_1(call, skb, offset, len, seq, req);
  487. case RXRPC_SECURITY_ENCRYPT:
  488. return rxkad_verify_packet_2(call, skb, offset, len, seq, req);
  489. default:
  490. return -ENOANO;
  491. }
  492. protocol_error:
  493. if (aborted)
  494. rxrpc_send_abort_packet(call);
  495. return -EPROTO;
  496. }
  497. /*
  498. * Locate the data contained in a packet that was partially encrypted.
  499. */
  500. static void rxkad_locate_data_1(struct rxrpc_call *call, struct sk_buff *skb,
  501. unsigned int *_offset, unsigned int *_len)
  502. {
  503. struct rxkad_level1_hdr sechdr;
  504. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  505. BUG();
  506. *_offset += sizeof(sechdr);
  507. *_len = ntohl(sechdr.data_size) & 0xffff;
  508. }
  509. /*
  510. * Locate the data contained in a packet that was completely encrypted.
  511. */
  512. static void rxkad_locate_data_2(struct rxrpc_call *call, struct sk_buff *skb,
  513. unsigned int *_offset, unsigned int *_len)
  514. {
  515. struct rxkad_level2_hdr sechdr;
  516. if (skb_copy_bits(skb, *_offset, &sechdr, sizeof(sechdr)) < 0)
  517. BUG();
  518. *_offset += sizeof(sechdr);
  519. *_len = ntohl(sechdr.data_size) & 0xffff;
  520. }
  521. /*
  522. * Locate the data contained in an already decrypted packet.
  523. */
  524. static void rxkad_locate_data(struct rxrpc_call *call, struct sk_buff *skb,
  525. unsigned int *_offset, unsigned int *_len)
  526. {
  527. switch (call->conn->params.security_level) {
  528. case RXRPC_SECURITY_AUTH:
  529. rxkad_locate_data_1(call, skb, _offset, _len);
  530. return;
  531. case RXRPC_SECURITY_ENCRYPT:
  532. rxkad_locate_data_2(call, skb, _offset, _len);
  533. return;
  534. default:
  535. return;
  536. }
  537. }
  538. /*
  539. * issue a challenge
  540. */
  541. static int rxkad_issue_challenge(struct rxrpc_connection *conn)
  542. {
  543. struct rxkad_challenge challenge;
  544. struct rxrpc_wire_header whdr;
  545. struct msghdr msg;
  546. struct kvec iov[2];
  547. size_t len;
  548. u32 serial;
  549. int ret;
  550. _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
  551. ret = key_validate(conn->server_key);
  552. if (ret < 0)
  553. return ret;
  554. get_random_bytes(&conn->security_nonce, sizeof(conn->security_nonce));
  555. challenge.version = htonl(2);
  556. challenge.nonce = htonl(conn->security_nonce);
  557. challenge.min_level = htonl(0);
  558. challenge.__padding = 0;
  559. msg.msg_name = &conn->params.peer->srx.transport;
  560. msg.msg_namelen = conn->params.peer->srx.transport_len;
  561. msg.msg_control = NULL;
  562. msg.msg_controllen = 0;
  563. msg.msg_flags = 0;
  564. whdr.epoch = htonl(conn->proto.epoch);
  565. whdr.cid = htonl(conn->proto.cid);
  566. whdr.callNumber = 0;
  567. whdr.seq = 0;
  568. whdr.type = RXRPC_PACKET_TYPE_CHALLENGE;
  569. whdr.flags = conn->out_clientflag;
  570. whdr.userStatus = 0;
  571. whdr.securityIndex = conn->security_ix;
  572. whdr._rsvd = 0;
  573. whdr.serviceId = htons(conn->service_id);
  574. iov[0].iov_base = &whdr;
  575. iov[0].iov_len = sizeof(whdr);
  576. iov[1].iov_base = &challenge;
  577. iov[1].iov_len = sizeof(challenge);
  578. len = iov[0].iov_len + iov[1].iov_len;
  579. serial = atomic_inc_return(&conn->serial);
  580. whdr.serial = htonl(serial);
  581. _proto("Tx CHALLENGE %%%u", serial);
  582. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 2, len);
  583. if (ret < 0) {
  584. trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
  585. rxrpc_tx_point_rxkad_challenge);
  586. return -EAGAIN;
  587. }
  588. conn->params.peer->last_tx_at = ktime_get_seconds();
  589. trace_rxrpc_tx_packet(conn->debug_id, &whdr,
  590. rxrpc_tx_point_rxkad_challenge);
  591. _leave(" = 0");
  592. return 0;
  593. }
  594. /*
  595. * send a Kerberos security response
  596. */
  597. static int rxkad_send_response(struct rxrpc_connection *conn,
  598. struct rxrpc_host_header *hdr,
  599. struct rxkad_response *resp,
  600. const struct rxkad_key *s2)
  601. {
  602. struct rxrpc_wire_header whdr;
  603. struct msghdr msg;
  604. struct kvec iov[3];
  605. size_t len;
  606. u32 serial;
  607. int ret;
  608. _enter("");
  609. msg.msg_name = &conn->params.peer->srx.transport;
  610. msg.msg_namelen = conn->params.peer->srx.transport_len;
  611. msg.msg_control = NULL;
  612. msg.msg_controllen = 0;
  613. msg.msg_flags = 0;
  614. memset(&whdr, 0, sizeof(whdr));
  615. whdr.epoch = htonl(hdr->epoch);
  616. whdr.cid = htonl(hdr->cid);
  617. whdr.type = RXRPC_PACKET_TYPE_RESPONSE;
  618. whdr.flags = conn->out_clientflag;
  619. whdr.securityIndex = hdr->securityIndex;
  620. whdr.serviceId = htons(hdr->serviceId);
  621. iov[0].iov_base = &whdr;
  622. iov[0].iov_len = sizeof(whdr);
  623. iov[1].iov_base = resp;
  624. iov[1].iov_len = sizeof(*resp);
  625. iov[2].iov_base = (void *)s2->ticket;
  626. iov[2].iov_len = s2->ticket_len;
  627. len = iov[0].iov_len + iov[1].iov_len + iov[2].iov_len;
  628. serial = atomic_inc_return(&conn->serial);
  629. whdr.serial = htonl(serial);
  630. _proto("Tx RESPONSE %%%u", serial);
  631. ret = kernel_sendmsg(conn->params.local->socket, &msg, iov, 3, len);
  632. if (ret < 0) {
  633. trace_rxrpc_tx_fail(conn->debug_id, serial, ret,
  634. rxrpc_tx_point_rxkad_response);
  635. return -EAGAIN;
  636. }
  637. conn->params.peer->last_tx_at = ktime_get_seconds();
  638. _leave(" = 0");
  639. return 0;
  640. }
  641. /*
  642. * calculate the response checksum
  643. */
  644. static void rxkad_calc_response_checksum(struct rxkad_response *response)
  645. {
  646. u32 csum = 1000003;
  647. int loop;
  648. u8 *p = (u8 *) response;
  649. for (loop = sizeof(*response); loop > 0; loop--)
  650. csum = csum * 0x10204081 + *p++;
  651. response->encrypted.checksum = htonl(csum);
  652. }
  653. /*
  654. * encrypt the response packet
  655. */
  656. static int rxkad_encrypt_response(struct rxrpc_connection *conn,
  657. struct rxkad_response *resp,
  658. const struct rxkad_key *s2)
  659. {
  660. struct skcipher_request *req;
  661. struct rxrpc_crypt iv;
  662. struct scatterlist sg[1];
  663. req = skcipher_request_alloc(&conn->cipher->base, GFP_NOFS);
  664. if (!req)
  665. return -ENOMEM;
  666. /* continue encrypting from where we left off */
  667. memcpy(&iv, s2->session_key, sizeof(iv));
  668. sg_init_table(sg, 1);
  669. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  670. skcipher_request_set_sync_tfm(req, conn->cipher);
  671. skcipher_request_set_callback(req, 0, NULL, NULL);
  672. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  673. crypto_skcipher_encrypt(req);
  674. skcipher_request_free(req);
  675. return 0;
  676. }
  677. /*
  678. * respond to a challenge packet
  679. */
  680. static int rxkad_respond_to_challenge(struct rxrpc_connection *conn,
  681. struct sk_buff *skb,
  682. u32 *_abort_code)
  683. {
  684. const struct rxrpc_key_token *token;
  685. struct rxkad_challenge challenge;
  686. struct rxkad_response *resp;
  687. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  688. const char *eproto;
  689. u32 version, nonce, min_level, abort_code;
  690. int ret;
  691. _enter("{%d,%x}", conn->debug_id, key_serial(conn->params.key));
  692. eproto = tracepoint_string("chall_no_key");
  693. abort_code = RX_PROTOCOL_ERROR;
  694. if (!conn->params.key)
  695. goto protocol_error;
  696. abort_code = RXKADEXPIRED;
  697. ret = key_validate(conn->params.key);
  698. if (ret < 0)
  699. goto other_error;
  700. eproto = tracepoint_string("chall_short");
  701. abort_code = RXKADPACKETSHORT;
  702. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  703. &challenge, sizeof(challenge)) < 0)
  704. goto protocol_error;
  705. version = ntohl(challenge.version);
  706. nonce = ntohl(challenge.nonce);
  707. min_level = ntohl(challenge.min_level);
  708. _proto("Rx CHALLENGE %%%u { v=%u n=%u ml=%u }",
  709. sp->hdr.serial, version, nonce, min_level);
  710. eproto = tracepoint_string("chall_ver");
  711. abort_code = RXKADINCONSISTENCY;
  712. if (version != RXKAD_VERSION)
  713. goto protocol_error;
  714. abort_code = RXKADLEVELFAIL;
  715. ret = -EACCES;
  716. if (conn->params.security_level < min_level)
  717. goto other_error;
  718. token = conn->params.key->payload.data[0];
  719. /* build the response packet */
  720. resp = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
  721. if (!resp)
  722. return -ENOMEM;
  723. resp->version = htonl(RXKAD_VERSION);
  724. resp->encrypted.epoch = htonl(conn->proto.epoch);
  725. resp->encrypted.cid = htonl(conn->proto.cid);
  726. resp->encrypted.securityIndex = htonl(conn->security_ix);
  727. resp->encrypted.inc_nonce = htonl(nonce + 1);
  728. resp->encrypted.level = htonl(conn->params.security_level);
  729. resp->kvno = htonl(token->kad->kvno);
  730. resp->ticket_len = htonl(token->kad->ticket_len);
  731. resp->encrypted.call_id[0] = htonl(conn->channels[0].call_counter);
  732. resp->encrypted.call_id[1] = htonl(conn->channels[1].call_counter);
  733. resp->encrypted.call_id[2] = htonl(conn->channels[2].call_counter);
  734. resp->encrypted.call_id[3] = htonl(conn->channels[3].call_counter);
  735. /* calculate the response checksum and then do the encryption */
  736. rxkad_calc_response_checksum(resp);
  737. ret = rxkad_encrypt_response(conn, resp, token->kad);
  738. if (ret == 0)
  739. ret = rxkad_send_response(conn, &sp->hdr, resp, token->kad);
  740. kfree(resp);
  741. return ret;
  742. protocol_error:
  743. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  744. ret = -EPROTO;
  745. other_error:
  746. *_abort_code = abort_code;
  747. return ret;
  748. }
  749. /*
  750. * decrypt the kerberos IV ticket in the response
  751. */
  752. static int rxkad_decrypt_ticket(struct rxrpc_connection *conn,
  753. struct sk_buff *skb,
  754. void *ticket, size_t ticket_len,
  755. struct rxrpc_crypt *_session_key,
  756. time64_t *_expiry,
  757. u32 *_abort_code)
  758. {
  759. struct skcipher_request *req;
  760. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  761. struct rxrpc_crypt iv, key;
  762. struct scatterlist sg[1];
  763. struct in_addr addr;
  764. unsigned int life;
  765. const char *eproto;
  766. time64_t issue, now;
  767. bool little_endian;
  768. int ret;
  769. u32 abort_code;
  770. u8 *p, *q, *name, *end;
  771. _enter("{%d},{%x}", conn->debug_id, key_serial(conn->server_key));
  772. *_expiry = 0;
  773. ret = key_validate(conn->server_key);
  774. if (ret < 0) {
  775. switch (ret) {
  776. case -EKEYEXPIRED:
  777. abort_code = RXKADEXPIRED;
  778. goto other_error;
  779. default:
  780. abort_code = RXKADNOAUTH;
  781. goto other_error;
  782. }
  783. }
  784. ASSERT(conn->server_key->payload.data[0] != NULL);
  785. ASSERTCMP((unsigned long) ticket & 7UL, ==, 0);
  786. memcpy(&iv, &conn->server_key->payload.data[2], sizeof(iv));
  787. ret = -ENOMEM;
  788. req = skcipher_request_alloc(conn->server_key->payload.data[0],
  789. GFP_NOFS);
  790. if (!req)
  791. goto temporary_error;
  792. sg_init_one(&sg[0], ticket, ticket_len);
  793. skcipher_request_set_callback(req, 0, NULL, NULL);
  794. skcipher_request_set_crypt(req, sg, sg, ticket_len, iv.x);
  795. crypto_skcipher_decrypt(req);
  796. skcipher_request_free(req);
  797. p = ticket;
  798. end = p + ticket_len;
  799. #define Z(field) \
  800. ({ \
  801. u8 *__str = p; \
  802. eproto = tracepoint_string("rxkad_bad_"#field); \
  803. q = memchr(p, 0, end - p); \
  804. if (!q || q - p > (field##_SZ)) \
  805. goto bad_ticket; \
  806. for (; p < q; p++) \
  807. if (!isprint(*p)) \
  808. goto bad_ticket; \
  809. p++; \
  810. __str; \
  811. })
  812. /* extract the ticket flags */
  813. _debug("KIV FLAGS: %x", *p);
  814. little_endian = *p & 1;
  815. p++;
  816. /* extract the authentication name */
  817. name = Z(ANAME);
  818. _debug("KIV ANAME: %s", name);
  819. /* extract the principal's instance */
  820. name = Z(INST);
  821. _debug("KIV INST : %s", name);
  822. /* extract the principal's authentication domain */
  823. name = Z(REALM);
  824. _debug("KIV REALM: %s", name);
  825. eproto = tracepoint_string("rxkad_bad_len");
  826. if (end - p < 4 + 8 + 4 + 2)
  827. goto bad_ticket;
  828. /* get the IPv4 address of the entity that requested the ticket */
  829. memcpy(&addr, p, sizeof(addr));
  830. p += 4;
  831. _debug("KIV ADDR : %pI4", &addr);
  832. /* get the session key from the ticket */
  833. memcpy(&key, p, sizeof(key));
  834. p += 8;
  835. _debug("KIV KEY : %08x %08x", ntohl(key.n[0]), ntohl(key.n[1]));
  836. memcpy(_session_key, &key, sizeof(key));
  837. /* get the ticket's lifetime */
  838. life = *p++ * 5 * 60;
  839. _debug("KIV LIFE : %u", life);
  840. /* get the issue time of the ticket */
  841. if (little_endian) {
  842. __le32 stamp;
  843. memcpy(&stamp, p, 4);
  844. issue = rxrpc_u32_to_time64(le32_to_cpu(stamp));
  845. } else {
  846. __be32 stamp;
  847. memcpy(&stamp, p, 4);
  848. issue = rxrpc_u32_to_time64(be32_to_cpu(stamp));
  849. }
  850. p += 4;
  851. now = ktime_get_real_seconds();
  852. _debug("KIV ISSUE: %llx [%llx]", issue, now);
  853. /* check the ticket is in date */
  854. if (issue > now) {
  855. abort_code = RXKADNOAUTH;
  856. ret = -EKEYREJECTED;
  857. goto other_error;
  858. }
  859. if (issue < now - life) {
  860. abort_code = RXKADEXPIRED;
  861. ret = -EKEYEXPIRED;
  862. goto other_error;
  863. }
  864. *_expiry = issue + life;
  865. /* get the service name */
  866. name = Z(SNAME);
  867. _debug("KIV SNAME: %s", name);
  868. /* get the service instance name */
  869. name = Z(INST);
  870. _debug("KIV SINST: %s", name);
  871. return 0;
  872. bad_ticket:
  873. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  874. abort_code = RXKADBADTICKET;
  875. ret = -EPROTO;
  876. other_error:
  877. *_abort_code = abort_code;
  878. return ret;
  879. temporary_error:
  880. return ret;
  881. }
  882. /*
  883. * decrypt the response packet
  884. */
  885. static void rxkad_decrypt_response(struct rxrpc_connection *conn,
  886. struct rxkad_response *resp,
  887. const struct rxrpc_crypt *session_key)
  888. {
  889. struct skcipher_request *req = rxkad_ci_req;
  890. struct scatterlist sg[1];
  891. struct rxrpc_crypt iv;
  892. _enter(",,%08x%08x",
  893. ntohl(session_key->n[0]), ntohl(session_key->n[1]));
  894. mutex_lock(&rxkad_ci_mutex);
  895. if (crypto_sync_skcipher_setkey(rxkad_ci, session_key->x,
  896. sizeof(*session_key)) < 0)
  897. BUG();
  898. memcpy(&iv, session_key, sizeof(iv));
  899. sg_init_table(sg, 1);
  900. sg_set_buf(sg, &resp->encrypted, sizeof(resp->encrypted));
  901. skcipher_request_set_sync_tfm(req, rxkad_ci);
  902. skcipher_request_set_callback(req, 0, NULL, NULL);
  903. skcipher_request_set_crypt(req, sg, sg, sizeof(resp->encrypted), iv.x);
  904. crypto_skcipher_decrypt(req);
  905. skcipher_request_zero(req);
  906. mutex_unlock(&rxkad_ci_mutex);
  907. _leave("");
  908. }
  909. /*
  910. * verify a response
  911. */
  912. static int rxkad_verify_response(struct rxrpc_connection *conn,
  913. struct sk_buff *skb,
  914. u32 *_abort_code)
  915. {
  916. struct rxkad_response *response;
  917. struct rxrpc_skb_priv *sp = rxrpc_skb(skb);
  918. struct rxrpc_crypt session_key;
  919. const char *eproto;
  920. time64_t expiry;
  921. void *ticket;
  922. u32 abort_code, version, kvno, ticket_len, level;
  923. __be32 csum;
  924. int ret, i;
  925. _enter("{%d,%x}", conn->debug_id, key_serial(conn->server_key));
  926. ret = -ENOMEM;
  927. response = kzalloc(sizeof(struct rxkad_response), GFP_NOFS);
  928. if (!response)
  929. goto temporary_error;
  930. eproto = tracepoint_string("rxkad_rsp_short");
  931. abort_code = RXKADPACKETSHORT;
  932. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  933. response, sizeof(*response)) < 0)
  934. goto protocol_error;
  935. if (!pskb_pull(skb, sizeof(*response)))
  936. BUG();
  937. version = ntohl(response->version);
  938. ticket_len = ntohl(response->ticket_len);
  939. kvno = ntohl(response->kvno);
  940. _proto("Rx RESPONSE %%%u { v=%u kv=%u tl=%u }",
  941. sp->hdr.serial, version, kvno, ticket_len);
  942. eproto = tracepoint_string("rxkad_rsp_ver");
  943. abort_code = RXKADINCONSISTENCY;
  944. if (version != RXKAD_VERSION)
  945. goto protocol_error;
  946. eproto = tracepoint_string("rxkad_rsp_tktlen");
  947. abort_code = RXKADTICKETLEN;
  948. if (ticket_len < 4 || ticket_len > MAXKRB5TICKETLEN)
  949. goto protocol_error;
  950. eproto = tracepoint_string("rxkad_rsp_unkkey");
  951. abort_code = RXKADUNKNOWNKEY;
  952. if (kvno >= RXKAD_TKT_TYPE_KERBEROS_V5)
  953. goto protocol_error;
  954. /* extract the kerberos ticket and decrypt and decode it */
  955. ret = -ENOMEM;
  956. ticket = kmalloc(ticket_len, GFP_NOFS);
  957. if (!ticket)
  958. goto temporary_error_free_resp;
  959. eproto = tracepoint_string("rxkad_tkt_short");
  960. abort_code = RXKADPACKETSHORT;
  961. if (skb_copy_bits(skb, sizeof(struct rxrpc_wire_header),
  962. ticket, ticket_len) < 0)
  963. goto protocol_error_free;
  964. ret = rxkad_decrypt_ticket(conn, skb, ticket, ticket_len, &session_key,
  965. &expiry, _abort_code);
  966. if (ret < 0)
  967. goto temporary_error_free_ticket;
  968. /* use the session key from inside the ticket to decrypt the
  969. * response */
  970. rxkad_decrypt_response(conn, response, &session_key);
  971. eproto = tracepoint_string("rxkad_rsp_param");
  972. abort_code = RXKADSEALEDINCON;
  973. if (ntohl(response->encrypted.epoch) != conn->proto.epoch)
  974. goto protocol_error_free;
  975. if (ntohl(response->encrypted.cid) != conn->proto.cid)
  976. goto protocol_error_free;
  977. if (ntohl(response->encrypted.securityIndex) != conn->security_ix)
  978. goto protocol_error_free;
  979. csum = response->encrypted.checksum;
  980. response->encrypted.checksum = 0;
  981. rxkad_calc_response_checksum(response);
  982. eproto = tracepoint_string("rxkad_rsp_csum");
  983. if (response->encrypted.checksum != csum)
  984. goto protocol_error_free;
  985. spin_lock(&conn->bundle->channel_lock);
  986. for (i = 0; i < RXRPC_MAXCALLS; i++) {
  987. struct rxrpc_call *call;
  988. u32 call_id = ntohl(response->encrypted.call_id[i]);
  989. eproto = tracepoint_string("rxkad_rsp_callid");
  990. if (call_id > INT_MAX)
  991. goto protocol_error_unlock;
  992. eproto = tracepoint_string("rxkad_rsp_callctr");
  993. if (call_id < conn->channels[i].call_counter)
  994. goto protocol_error_unlock;
  995. eproto = tracepoint_string("rxkad_rsp_callst");
  996. if (call_id > conn->channels[i].call_counter) {
  997. call = rcu_dereference_protected(
  998. conn->channels[i].call,
  999. lockdep_is_held(&conn->bundle->channel_lock));
  1000. if (call && call->state < RXRPC_CALL_COMPLETE)
  1001. goto protocol_error_unlock;
  1002. conn->channels[i].call_counter = call_id;
  1003. }
  1004. }
  1005. spin_unlock(&conn->bundle->channel_lock);
  1006. eproto = tracepoint_string("rxkad_rsp_seq");
  1007. abort_code = RXKADOUTOFSEQUENCE;
  1008. if (ntohl(response->encrypted.inc_nonce) != conn->security_nonce + 1)
  1009. goto protocol_error_free;
  1010. eproto = tracepoint_string("rxkad_rsp_level");
  1011. abort_code = RXKADLEVELFAIL;
  1012. level = ntohl(response->encrypted.level);
  1013. if (level > RXRPC_SECURITY_ENCRYPT)
  1014. goto protocol_error_free;
  1015. conn->params.security_level = level;
  1016. /* create a key to hold the security data and expiration time - after
  1017. * this the connection security can be handled in exactly the same way
  1018. * as for a client connection */
  1019. ret = rxrpc_get_server_data_key(conn, &session_key, expiry, kvno);
  1020. if (ret < 0)
  1021. goto temporary_error_free_ticket;
  1022. kfree(ticket);
  1023. kfree(response);
  1024. _leave(" = 0");
  1025. return 0;
  1026. protocol_error_unlock:
  1027. spin_unlock(&conn->bundle->channel_lock);
  1028. protocol_error_free:
  1029. kfree(ticket);
  1030. protocol_error:
  1031. kfree(response);
  1032. trace_rxrpc_rx_eproto(NULL, sp->hdr.serial, eproto);
  1033. *_abort_code = abort_code;
  1034. return -EPROTO;
  1035. temporary_error_free_ticket:
  1036. kfree(ticket);
  1037. temporary_error_free_resp:
  1038. kfree(response);
  1039. temporary_error:
  1040. /* Ignore the response packet if we got a temporary error such as
  1041. * ENOMEM. We just want to send the challenge again. Note that we
  1042. * also come out this way if the ticket decryption fails.
  1043. */
  1044. return ret;
  1045. }
  1046. /*
  1047. * clear the connection security
  1048. */
  1049. static void rxkad_clear(struct rxrpc_connection *conn)
  1050. {
  1051. _enter("");
  1052. if (conn->cipher)
  1053. crypto_free_sync_skcipher(conn->cipher);
  1054. }
  1055. /*
  1056. * Initialise the rxkad security service.
  1057. */
  1058. static int rxkad_init(void)
  1059. {
  1060. struct crypto_sync_skcipher *tfm;
  1061. struct skcipher_request *req;
  1062. /* pin the cipher we need so that the crypto layer doesn't invoke
  1063. * keventd to go get it */
  1064. tfm = crypto_alloc_sync_skcipher("pcbc(fcrypt)", 0, 0);
  1065. if (IS_ERR(tfm))
  1066. return PTR_ERR(tfm);
  1067. req = skcipher_request_alloc(&tfm->base, GFP_KERNEL);
  1068. if (!req)
  1069. goto nomem_tfm;
  1070. rxkad_ci_req = req;
  1071. rxkad_ci = tfm;
  1072. return 0;
  1073. nomem_tfm:
  1074. crypto_free_sync_skcipher(tfm);
  1075. return -ENOMEM;
  1076. }
  1077. /*
  1078. * Clean up the rxkad security service.
  1079. */
  1080. static void rxkad_exit(void)
  1081. {
  1082. crypto_free_sync_skcipher(rxkad_ci);
  1083. skcipher_request_free(rxkad_ci_req);
  1084. }
  1085. /*
  1086. * RxRPC Kerberos-based security
  1087. */
  1088. const struct rxrpc_security rxkad = {
  1089. .name = "rxkad",
  1090. .security_index = RXRPC_SECURITY_RXKAD,
  1091. .no_key_abort = RXKADUNKNOWNKEY,
  1092. .init = rxkad_init,
  1093. .exit = rxkad_exit,
  1094. .init_connection_security = rxkad_init_connection_security,
  1095. .prime_packet_security = rxkad_prime_packet_security,
  1096. .secure_packet = rxkad_secure_packet,
  1097. .verify_packet = rxkad_verify_packet,
  1098. .free_call_crypto = rxkad_free_call_crypto,
  1099. .locate_data = rxkad_locate_data,
  1100. .issue_challenge = rxkad_issue_challenge,
  1101. .respond_to_challenge = rxkad_respond_to_challenge,
  1102. .verify_response = rxkad_verify_response,
  1103. .clear = rxkad_clear,
  1104. };