rxkad.c 29 KB

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