rxkad.c 32 KB

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