rxkad.c 30 KB

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