ar-key.c 28 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201
  1. /* RxRPC key management
  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. * RxRPC keys should have a description of describing their purpose:
  12. * "afs@CAMBRIDGE.REDHAT.COM>
  13. */
  14. #include <linux/module.h>
  15. #include <linux/net.h>
  16. #include <linux/skbuff.h>
  17. #include <linux/key-type.h>
  18. #include <linux/crypto.h>
  19. #include <linux/ctype.h>
  20. #include <linux/slab.h>
  21. #include <net/sock.h>
  22. #include <net/af_rxrpc.h>
  23. #include <keys/rxrpc-type.h>
  24. #include <keys/user-type.h>
  25. #include "ar-internal.h"
  26. static int rxrpc_instantiate(struct key *, const void *, size_t);
  27. static int rxrpc_instantiate_s(struct key *, const void *, size_t);
  28. static void rxrpc_destroy(struct key *);
  29. static void rxrpc_destroy_s(struct key *);
  30. static void rxrpc_describe(const struct key *, struct seq_file *);
  31. static long rxrpc_read(const struct key *, char __user *, size_t);
  32. /*
  33. * rxrpc defined keys take an arbitrary string as the description and an
  34. * arbitrary blob of data as the payload
  35. */
  36. struct key_type key_type_rxrpc = {
  37. .name = "rxrpc",
  38. .instantiate = rxrpc_instantiate,
  39. .match = user_match,
  40. .destroy = rxrpc_destroy,
  41. .describe = rxrpc_describe,
  42. .read = rxrpc_read,
  43. };
  44. EXPORT_SYMBOL(key_type_rxrpc);
  45. /*
  46. * rxrpc server defined keys take "<serviceId>:<securityIndex>" as the
  47. * description and an 8-byte decryption key as the payload
  48. */
  49. struct key_type key_type_rxrpc_s = {
  50. .name = "rxrpc_s",
  51. .instantiate = rxrpc_instantiate_s,
  52. .match = user_match,
  53. .destroy = rxrpc_destroy_s,
  54. .describe = rxrpc_describe,
  55. };
  56. /*
  57. * parse an RxKAD type XDR format token
  58. * - the caller guarantees we have at least 4 words
  59. */
  60. static int rxrpc_instantiate_xdr_rxkad(struct key *key, const __be32 *xdr,
  61. unsigned toklen)
  62. {
  63. struct rxrpc_key_token *token, **pptoken;
  64. size_t plen;
  65. u32 tktlen;
  66. int ret;
  67. _enter(",{%x,%x,%x,%x},%u",
  68. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  69. toklen);
  70. if (toklen <= 8 * 4)
  71. return -EKEYREJECTED;
  72. tktlen = ntohl(xdr[7]);
  73. _debug("tktlen: %x", tktlen);
  74. if (tktlen > AFSTOKEN_RK_TIX_MAX)
  75. return -EKEYREJECTED;
  76. if (8 * 4 + tktlen != toklen)
  77. return -EKEYREJECTED;
  78. plen = sizeof(*token) + sizeof(*token->kad) + tktlen;
  79. ret = key_payload_reserve(key, key->datalen + plen);
  80. if (ret < 0)
  81. return ret;
  82. plen -= sizeof(*token);
  83. token = kmalloc(sizeof(*token), GFP_KERNEL);
  84. if (!token)
  85. return -ENOMEM;
  86. token->kad = kmalloc(plen, GFP_KERNEL);
  87. if (!token->kad) {
  88. kfree(token);
  89. return -ENOMEM;
  90. }
  91. token->security_index = RXRPC_SECURITY_RXKAD;
  92. token->kad->ticket_len = tktlen;
  93. token->kad->vice_id = ntohl(xdr[0]);
  94. token->kad->kvno = ntohl(xdr[1]);
  95. token->kad->start = ntohl(xdr[4]);
  96. token->kad->expiry = ntohl(xdr[5]);
  97. token->kad->primary_flag = ntohl(xdr[6]);
  98. memcpy(&token->kad->session_key, &xdr[2], 8);
  99. memcpy(&token->kad->ticket, &xdr[8], tktlen);
  100. _debug("SCIX: %u", token->security_index);
  101. _debug("TLEN: %u", token->kad->ticket_len);
  102. _debug("EXPY: %x", token->kad->expiry);
  103. _debug("KVNO: %u", token->kad->kvno);
  104. _debug("PRIM: %u", token->kad->primary_flag);
  105. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  106. token->kad->session_key[0], token->kad->session_key[1],
  107. token->kad->session_key[2], token->kad->session_key[3],
  108. token->kad->session_key[4], token->kad->session_key[5],
  109. token->kad->session_key[6], token->kad->session_key[7]);
  110. if (token->kad->ticket_len >= 8)
  111. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  112. token->kad->ticket[0], token->kad->ticket[1],
  113. token->kad->ticket[2], token->kad->ticket[3],
  114. token->kad->ticket[4], token->kad->ticket[5],
  115. token->kad->ticket[6], token->kad->ticket[7]);
  116. /* count the number of tokens attached */
  117. key->type_data.x[0]++;
  118. /* attach the data */
  119. for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
  120. *pptoken;
  121. pptoken = &(*pptoken)->next)
  122. continue;
  123. *pptoken = token;
  124. if (token->kad->expiry < key->expiry)
  125. key->expiry = token->kad->expiry;
  126. _leave(" = 0");
  127. return 0;
  128. }
  129. static void rxrpc_free_krb5_principal(struct krb5_principal *princ)
  130. {
  131. int loop;
  132. if (princ->name_parts) {
  133. for (loop = princ->n_name_parts - 1; loop >= 0; loop--)
  134. kfree(princ->name_parts[loop]);
  135. kfree(princ->name_parts);
  136. }
  137. kfree(princ->realm);
  138. }
  139. static void rxrpc_free_krb5_tagged(struct krb5_tagged_data *td)
  140. {
  141. kfree(td->data);
  142. }
  143. /*
  144. * free up an RxK5 token
  145. */
  146. static void rxrpc_rxk5_free(struct rxk5_key *rxk5)
  147. {
  148. int loop;
  149. rxrpc_free_krb5_principal(&rxk5->client);
  150. rxrpc_free_krb5_principal(&rxk5->server);
  151. rxrpc_free_krb5_tagged(&rxk5->session);
  152. if (rxk5->addresses) {
  153. for (loop = rxk5->n_addresses - 1; loop >= 0; loop--)
  154. rxrpc_free_krb5_tagged(&rxk5->addresses[loop]);
  155. kfree(rxk5->addresses);
  156. }
  157. if (rxk5->authdata) {
  158. for (loop = rxk5->n_authdata - 1; loop >= 0; loop--)
  159. rxrpc_free_krb5_tagged(&rxk5->authdata[loop]);
  160. kfree(rxk5->authdata);
  161. }
  162. kfree(rxk5->ticket);
  163. kfree(rxk5->ticket2);
  164. kfree(rxk5);
  165. }
  166. /*
  167. * extract a krb5 principal
  168. */
  169. static int rxrpc_krb5_decode_principal(struct krb5_principal *princ,
  170. const __be32 **_xdr,
  171. unsigned *_toklen)
  172. {
  173. const __be32 *xdr = *_xdr;
  174. unsigned toklen = *_toklen, n_parts, loop, tmp;
  175. /* there must be at least one name, and at least #names+1 length
  176. * words */
  177. if (toklen <= 12)
  178. return -EINVAL;
  179. _enter(",{%x,%x,%x},%u",
  180. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), toklen);
  181. n_parts = ntohl(*xdr++);
  182. toklen -= 4;
  183. if (n_parts <= 0 || n_parts > AFSTOKEN_K5_COMPONENTS_MAX)
  184. return -EINVAL;
  185. princ->n_name_parts = n_parts;
  186. if (toklen <= (n_parts + 1) * 4)
  187. return -EINVAL;
  188. princ->name_parts = kcalloc(sizeof(char *), n_parts, GFP_KERNEL);
  189. if (!princ->name_parts)
  190. return -ENOMEM;
  191. for (loop = 0; loop < n_parts; loop++) {
  192. if (toklen < 4)
  193. return -EINVAL;
  194. tmp = ntohl(*xdr++);
  195. toklen -= 4;
  196. if (tmp <= 0 || tmp > AFSTOKEN_STRING_MAX)
  197. return -EINVAL;
  198. if (tmp > toklen)
  199. return -EINVAL;
  200. princ->name_parts[loop] = kmalloc(tmp + 1, GFP_KERNEL);
  201. if (!princ->name_parts[loop])
  202. return -ENOMEM;
  203. memcpy(princ->name_parts[loop], xdr, tmp);
  204. princ->name_parts[loop][tmp] = 0;
  205. tmp = (tmp + 3) & ~3;
  206. toklen -= tmp;
  207. xdr += tmp >> 2;
  208. }
  209. if (toklen < 4)
  210. return -EINVAL;
  211. tmp = ntohl(*xdr++);
  212. toklen -= 4;
  213. if (tmp <= 0 || tmp > AFSTOKEN_K5_REALM_MAX)
  214. return -EINVAL;
  215. if (tmp > toklen)
  216. return -EINVAL;
  217. princ->realm = kmalloc(tmp + 1, GFP_KERNEL);
  218. if (!princ->realm)
  219. return -ENOMEM;
  220. memcpy(princ->realm, xdr, tmp);
  221. princ->realm[tmp] = 0;
  222. tmp = (tmp + 3) & ~3;
  223. toklen -= tmp;
  224. xdr += tmp >> 2;
  225. _debug("%s/...@%s", princ->name_parts[0], princ->realm);
  226. *_xdr = xdr;
  227. *_toklen = toklen;
  228. _leave(" = 0 [toklen=%u]", toklen);
  229. return 0;
  230. }
  231. /*
  232. * extract a piece of krb5 tagged data
  233. */
  234. static int rxrpc_krb5_decode_tagged_data(struct krb5_tagged_data *td,
  235. size_t max_data_size,
  236. const __be32 **_xdr,
  237. unsigned *_toklen)
  238. {
  239. const __be32 *xdr = *_xdr;
  240. unsigned toklen = *_toklen, len;
  241. /* there must be at least one tag and one length word */
  242. if (toklen <= 8)
  243. return -EINVAL;
  244. _enter(",%zu,{%x,%x},%u",
  245. max_data_size, ntohl(xdr[0]), ntohl(xdr[1]), toklen);
  246. td->tag = ntohl(*xdr++);
  247. len = ntohl(*xdr++);
  248. toklen -= 8;
  249. if (len > max_data_size)
  250. return -EINVAL;
  251. td->data_len = len;
  252. if (len > 0) {
  253. td->data = kmalloc(len, GFP_KERNEL);
  254. if (!td->data)
  255. return -ENOMEM;
  256. memcpy(td->data, xdr, len);
  257. len = (len + 3) & ~3;
  258. toklen -= len;
  259. xdr += len >> 2;
  260. }
  261. _debug("tag %x len %x", td->tag, td->data_len);
  262. *_xdr = xdr;
  263. *_toklen = toklen;
  264. _leave(" = 0 [toklen=%u]", toklen);
  265. return 0;
  266. }
  267. /*
  268. * extract an array of tagged data
  269. */
  270. static int rxrpc_krb5_decode_tagged_array(struct krb5_tagged_data **_td,
  271. u8 *_n_elem,
  272. u8 max_n_elem,
  273. size_t max_elem_size,
  274. const __be32 **_xdr,
  275. unsigned *_toklen)
  276. {
  277. struct krb5_tagged_data *td;
  278. const __be32 *xdr = *_xdr;
  279. unsigned toklen = *_toklen, n_elem, loop;
  280. int ret;
  281. /* there must be at least one count */
  282. if (toklen < 4)
  283. return -EINVAL;
  284. _enter(",,%u,%zu,{%x},%u",
  285. max_n_elem, max_elem_size, ntohl(xdr[0]), toklen);
  286. n_elem = ntohl(*xdr++);
  287. toklen -= 4;
  288. if (n_elem < 0 || n_elem > max_n_elem)
  289. return -EINVAL;
  290. *_n_elem = n_elem;
  291. if (n_elem > 0) {
  292. if (toklen <= (n_elem + 1) * 4)
  293. return -EINVAL;
  294. _debug("n_elem %d", n_elem);
  295. td = kcalloc(sizeof(struct krb5_tagged_data), n_elem,
  296. GFP_KERNEL);
  297. if (!td)
  298. return -ENOMEM;
  299. *_td = td;
  300. for (loop = 0; loop < n_elem; loop++) {
  301. ret = rxrpc_krb5_decode_tagged_data(&td[loop],
  302. max_elem_size,
  303. &xdr, &toklen);
  304. if (ret < 0)
  305. return ret;
  306. }
  307. }
  308. *_xdr = xdr;
  309. *_toklen = toklen;
  310. _leave(" = 0 [toklen=%u]", toklen);
  311. return 0;
  312. }
  313. /*
  314. * extract a krb5 ticket
  315. */
  316. static int rxrpc_krb5_decode_ticket(u8 **_ticket, u16 *_tktlen,
  317. const __be32 **_xdr, unsigned *_toklen)
  318. {
  319. const __be32 *xdr = *_xdr;
  320. unsigned toklen = *_toklen, len;
  321. /* there must be at least one length word */
  322. if (toklen <= 4)
  323. return -EINVAL;
  324. _enter(",{%x},%u", ntohl(xdr[0]), toklen);
  325. len = ntohl(*xdr++);
  326. toklen -= 4;
  327. if (len > AFSTOKEN_K5_TIX_MAX)
  328. return -EINVAL;
  329. *_tktlen = len;
  330. _debug("ticket len %u", len);
  331. if (len > 0) {
  332. *_ticket = kmalloc(len, GFP_KERNEL);
  333. if (!*_ticket)
  334. return -ENOMEM;
  335. memcpy(*_ticket, xdr, len);
  336. len = (len + 3) & ~3;
  337. toklen -= len;
  338. xdr += len >> 2;
  339. }
  340. *_xdr = xdr;
  341. *_toklen = toklen;
  342. _leave(" = 0 [toklen=%u]", toklen);
  343. return 0;
  344. }
  345. /*
  346. * parse an RxK5 type XDR format token
  347. * - the caller guarantees we have at least 4 words
  348. */
  349. static int rxrpc_instantiate_xdr_rxk5(struct key *key, const __be32 *xdr,
  350. unsigned toklen)
  351. {
  352. struct rxrpc_key_token *token, **pptoken;
  353. struct rxk5_key *rxk5;
  354. const __be32 *end_xdr = xdr + (toklen >> 2);
  355. int ret;
  356. _enter(",{%x,%x,%x,%x},%u",
  357. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  358. toklen);
  359. /* reserve some payload space for this subkey - the length of the token
  360. * is a reasonable approximation */
  361. ret = key_payload_reserve(key, key->datalen + toklen);
  362. if (ret < 0)
  363. return ret;
  364. token = kzalloc(sizeof(*token), GFP_KERNEL);
  365. if (!token)
  366. return -ENOMEM;
  367. rxk5 = kzalloc(sizeof(*rxk5), GFP_KERNEL);
  368. if (!rxk5) {
  369. kfree(token);
  370. return -ENOMEM;
  371. }
  372. token->security_index = RXRPC_SECURITY_RXK5;
  373. token->k5 = rxk5;
  374. /* extract the principals */
  375. ret = rxrpc_krb5_decode_principal(&rxk5->client, &xdr, &toklen);
  376. if (ret < 0)
  377. goto error;
  378. ret = rxrpc_krb5_decode_principal(&rxk5->server, &xdr, &toklen);
  379. if (ret < 0)
  380. goto error;
  381. /* extract the session key and the encoding type (the tag field ->
  382. * ENCTYPE_xxx) */
  383. ret = rxrpc_krb5_decode_tagged_data(&rxk5->session, AFSTOKEN_DATA_MAX,
  384. &xdr, &toklen);
  385. if (ret < 0)
  386. goto error;
  387. if (toklen < 4 * 8 + 2 * 4)
  388. goto inval;
  389. rxk5->authtime = be64_to_cpup((const __be64 *) xdr);
  390. xdr += 2;
  391. rxk5->starttime = be64_to_cpup((const __be64 *) xdr);
  392. xdr += 2;
  393. rxk5->endtime = be64_to_cpup((const __be64 *) xdr);
  394. xdr += 2;
  395. rxk5->renew_till = be64_to_cpup((const __be64 *) xdr);
  396. xdr += 2;
  397. rxk5->is_skey = ntohl(*xdr++);
  398. rxk5->flags = ntohl(*xdr++);
  399. toklen -= 4 * 8 + 2 * 4;
  400. _debug("times: a=%llx s=%llx e=%llx rt=%llx",
  401. rxk5->authtime, rxk5->starttime, rxk5->endtime,
  402. rxk5->renew_till);
  403. _debug("is_skey=%x flags=%x", rxk5->is_skey, rxk5->flags);
  404. /* extract the permitted client addresses */
  405. ret = rxrpc_krb5_decode_tagged_array(&rxk5->addresses,
  406. &rxk5->n_addresses,
  407. AFSTOKEN_K5_ADDRESSES_MAX,
  408. AFSTOKEN_DATA_MAX,
  409. &xdr, &toklen);
  410. if (ret < 0)
  411. goto error;
  412. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  413. /* extract the tickets */
  414. ret = rxrpc_krb5_decode_ticket(&rxk5->ticket, &rxk5->ticket_len,
  415. &xdr, &toklen);
  416. if (ret < 0)
  417. goto error;
  418. ret = rxrpc_krb5_decode_ticket(&rxk5->ticket2, &rxk5->ticket2_len,
  419. &xdr, &toklen);
  420. if (ret < 0)
  421. goto error;
  422. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  423. /* extract the typed auth data */
  424. ret = rxrpc_krb5_decode_tagged_array(&rxk5->authdata,
  425. &rxk5->n_authdata,
  426. AFSTOKEN_K5_AUTHDATA_MAX,
  427. AFSTOKEN_BDATALN_MAX,
  428. &xdr, &toklen);
  429. if (ret < 0)
  430. goto error;
  431. ASSERTCMP((end_xdr - xdr) << 2, ==, toklen);
  432. if (toklen != 0)
  433. goto inval;
  434. /* attach the payload to the key */
  435. for (pptoken = (struct rxrpc_key_token **)&key->payload.data;
  436. *pptoken;
  437. pptoken = &(*pptoken)->next)
  438. continue;
  439. *pptoken = token;
  440. if (token->kad->expiry < key->expiry)
  441. key->expiry = token->kad->expiry;
  442. _leave(" = 0");
  443. return 0;
  444. inval:
  445. ret = -EINVAL;
  446. error:
  447. rxrpc_rxk5_free(rxk5);
  448. kfree(token);
  449. _leave(" = %d", ret);
  450. return ret;
  451. }
  452. /*
  453. * attempt to parse the data as the XDR format
  454. * - the caller guarantees we have more than 7 words
  455. */
  456. static int rxrpc_instantiate_xdr(struct key *key, const void *data, size_t datalen)
  457. {
  458. const __be32 *xdr = data, *token;
  459. const char *cp;
  460. unsigned len, tmp, loop, ntoken, toklen, sec_ix;
  461. int ret;
  462. _enter(",{%x,%x,%x,%x},%zu",
  463. ntohl(xdr[0]), ntohl(xdr[1]), ntohl(xdr[2]), ntohl(xdr[3]),
  464. datalen);
  465. if (datalen > AFSTOKEN_LENGTH_MAX)
  466. goto not_xdr;
  467. /* XDR is an array of __be32's */
  468. if (datalen & 3)
  469. goto not_xdr;
  470. /* the flags should be 0 (the setpag bit must be handled by
  471. * userspace) */
  472. if (ntohl(*xdr++) != 0)
  473. goto not_xdr;
  474. datalen -= 4;
  475. /* check the cell name */
  476. len = ntohl(*xdr++);
  477. if (len < 1 || len > AFSTOKEN_CELL_MAX)
  478. goto not_xdr;
  479. datalen -= 4;
  480. tmp = (len + 3) & ~3;
  481. if (tmp > datalen)
  482. goto not_xdr;
  483. cp = (const char *) xdr;
  484. for (loop = 0; loop < len; loop++)
  485. if (!isprint(cp[loop]))
  486. goto not_xdr;
  487. if (len < tmp)
  488. for (; loop < tmp; loop++)
  489. if (cp[loop])
  490. goto not_xdr;
  491. _debug("cellname: [%u/%u] '%*.*s'",
  492. len, tmp, len, len, (const char *) xdr);
  493. datalen -= tmp;
  494. xdr += tmp >> 2;
  495. /* get the token count */
  496. if (datalen < 12)
  497. goto not_xdr;
  498. ntoken = ntohl(*xdr++);
  499. datalen -= 4;
  500. _debug("ntoken: %x", ntoken);
  501. if (ntoken < 1 || ntoken > AFSTOKEN_MAX)
  502. goto not_xdr;
  503. /* check each token wrapper */
  504. token = xdr;
  505. loop = ntoken;
  506. do {
  507. if (datalen < 8)
  508. goto not_xdr;
  509. toklen = ntohl(*xdr++);
  510. sec_ix = ntohl(*xdr);
  511. datalen -= 4;
  512. _debug("token: [%x/%zx] %x", toklen, datalen, sec_ix);
  513. if (toklen < 20 || toklen > datalen)
  514. goto not_xdr;
  515. datalen -= (toklen + 3) & ~3;
  516. xdr += (toklen + 3) >> 2;
  517. } while (--loop > 0);
  518. _debug("remainder: %zu", datalen);
  519. if (datalen != 0)
  520. goto not_xdr;
  521. /* okay: we're going to assume it's valid XDR format
  522. * - we ignore the cellname, relying on the key to be correctly named
  523. */
  524. do {
  525. xdr = token;
  526. toklen = ntohl(*xdr++);
  527. token = xdr + ((toklen + 3) >> 2);
  528. sec_ix = ntohl(*xdr++);
  529. toklen -= 4;
  530. _debug("TOKEN type=%u [%p-%p]", sec_ix, xdr, token);
  531. switch (sec_ix) {
  532. case RXRPC_SECURITY_RXKAD:
  533. ret = rxrpc_instantiate_xdr_rxkad(key, xdr, toklen);
  534. if (ret != 0)
  535. goto error;
  536. break;
  537. case RXRPC_SECURITY_RXK5:
  538. ret = rxrpc_instantiate_xdr_rxk5(key, xdr, toklen);
  539. if (ret != 0)
  540. goto error;
  541. break;
  542. default:
  543. ret = -EPROTONOSUPPORT;
  544. goto error;
  545. }
  546. } while (--ntoken > 0);
  547. _leave(" = 0");
  548. return 0;
  549. not_xdr:
  550. _leave(" = -EPROTO");
  551. return -EPROTO;
  552. error:
  553. _leave(" = %d", ret);
  554. return ret;
  555. }
  556. /*
  557. * instantiate an rxrpc defined key
  558. * data should be of the form:
  559. * OFFSET LEN CONTENT
  560. * 0 4 key interface version number
  561. * 4 2 security index (type)
  562. * 6 2 ticket length
  563. * 8 4 key expiry time (time_t)
  564. * 12 4 kvno
  565. * 16 8 session key
  566. * 24 [len] ticket
  567. *
  568. * if no data is provided, then a no-security key is made
  569. */
  570. static int rxrpc_instantiate(struct key *key, const void *data, size_t datalen)
  571. {
  572. const struct rxrpc_key_data_v1 *v1;
  573. struct rxrpc_key_token *token, **pp;
  574. size_t plen;
  575. u32 kver;
  576. int ret;
  577. _enter("{%x},,%zu", key_serial(key), datalen);
  578. /* handle a no-security key */
  579. if (!data && datalen == 0)
  580. return 0;
  581. /* determine if the XDR payload format is being used */
  582. if (datalen > 7 * 4) {
  583. ret = rxrpc_instantiate_xdr(key, data, datalen);
  584. if (ret != -EPROTO)
  585. return ret;
  586. }
  587. /* get the key interface version number */
  588. ret = -EINVAL;
  589. if (datalen <= 4 || !data)
  590. goto error;
  591. memcpy(&kver, data, sizeof(kver));
  592. data += sizeof(kver);
  593. datalen -= sizeof(kver);
  594. _debug("KEY I/F VERSION: %u", kver);
  595. ret = -EKEYREJECTED;
  596. if (kver != 1)
  597. goto error;
  598. /* deal with a version 1 key */
  599. ret = -EINVAL;
  600. if (datalen < sizeof(*v1))
  601. goto error;
  602. v1 = data;
  603. if (datalen != sizeof(*v1) + v1->ticket_length)
  604. goto error;
  605. _debug("SCIX: %u", v1->security_index);
  606. _debug("TLEN: %u", v1->ticket_length);
  607. _debug("EXPY: %x", v1->expiry);
  608. _debug("KVNO: %u", v1->kvno);
  609. _debug("SKEY: %02x%02x%02x%02x%02x%02x%02x%02x",
  610. v1->session_key[0], v1->session_key[1],
  611. v1->session_key[2], v1->session_key[3],
  612. v1->session_key[4], v1->session_key[5],
  613. v1->session_key[6], v1->session_key[7]);
  614. if (v1->ticket_length >= 8)
  615. _debug("TCKT: %02x%02x%02x%02x%02x%02x%02x%02x",
  616. v1->ticket[0], v1->ticket[1],
  617. v1->ticket[2], v1->ticket[3],
  618. v1->ticket[4], v1->ticket[5],
  619. v1->ticket[6], v1->ticket[7]);
  620. ret = -EPROTONOSUPPORT;
  621. if (v1->security_index != RXRPC_SECURITY_RXKAD)
  622. goto error;
  623. plen = sizeof(*token->kad) + v1->ticket_length;
  624. ret = key_payload_reserve(key, plen + sizeof(*token));
  625. if (ret < 0)
  626. goto error;
  627. ret = -ENOMEM;
  628. token = kmalloc(sizeof(*token), GFP_KERNEL);
  629. if (!token)
  630. goto error;
  631. token->kad = kmalloc(plen, GFP_KERNEL);
  632. if (!token->kad)
  633. goto error_free;
  634. token->security_index = RXRPC_SECURITY_RXKAD;
  635. token->kad->ticket_len = v1->ticket_length;
  636. token->kad->expiry = v1->expiry;
  637. token->kad->kvno = v1->kvno;
  638. memcpy(&token->kad->session_key, &v1->session_key, 8);
  639. memcpy(&token->kad->ticket, v1->ticket, v1->ticket_length);
  640. /* attach the data */
  641. key->type_data.x[0]++;
  642. pp = (struct rxrpc_key_token **)&key->payload.data;
  643. while (*pp)
  644. pp = &(*pp)->next;
  645. *pp = token;
  646. if (token->kad->expiry < key->expiry)
  647. key->expiry = token->kad->expiry;
  648. token = NULL;
  649. ret = 0;
  650. error_free:
  651. kfree(token);
  652. error:
  653. return ret;
  654. }
  655. /*
  656. * instantiate a server secret key
  657. * data should be a pointer to the 8-byte secret key
  658. */
  659. static int rxrpc_instantiate_s(struct key *key, const void *data,
  660. size_t datalen)
  661. {
  662. struct crypto_blkcipher *ci;
  663. _enter("{%x},,%zu", key_serial(key), datalen);
  664. if (datalen != 8)
  665. return -EINVAL;
  666. memcpy(&key->type_data, data, 8);
  667. ci = crypto_alloc_blkcipher("pcbc(des)", 0, CRYPTO_ALG_ASYNC);
  668. if (IS_ERR(ci)) {
  669. _leave(" = %ld", PTR_ERR(ci));
  670. return PTR_ERR(ci);
  671. }
  672. if (crypto_blkcipher_setkey(ci, data, 8) < 0)
  673. BUG();
  674. key->payload.data = ci;
  675. _leave(" = 0");
  676. return 0;
  677. }
  678. /*
  679. * dispose of the data dangling from the corpse of a rxrpc key
  680. */
  681. static void rxrpc_destroy(struct key *key)
  682. {
  683. struct rxrpc_key_token *token;
  684. while ((token = key->payload.data)) {
  685. key->payload.data = token->next;
  686. switch (token->security_index) {
  687. case RXRPC_SECURITY_RXKAD:
  688. kfree(token->kad);
  689. break;
  690. case RXRPC_SECURITY_RXK5:
  691. if (token->k5)
  692. rxrpc_rxk5_free(token->k5);
  693. break;
  694. default:
  695. printk(KERN_ERR "Unknown token type %x on rxrpc key\n",
  696. token->security_index);
  697. BUG();
  698. }
  699. kfree(token);
  700. }
  701. }
  702. /*
  703. * dispose of the data dangling from the corpse of a rxrpc key
  704. */
  705. static void rxrpc_destroy_s(struct key *key)
  706. {
  707. if (key->payload.data) {
  708. crypto_free_blkcipher(key->payload.data);
  709. key->payload.data = NULL;
  710. }
  711. }
  712. /*
  713. * describe the rxrpc key
  714. */
  715. static void rxrpc_describe(const struct key *key, struct seq_file *m)
  716. {
  717. seq_puts(m, key->description);
  718. }
  719. /*
  720. * grab the security key for a socket
  721. */
  722. int rxrpc_request_key(struct rxrpc_sock *rx, char __user *optval, int optlen)
  723. {
  724. struct key *key;
  725. char *description;
  726. _enter("");
  727. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  728. return -EINVAL;
  729. description = kmalloc(optlen + 1, GFP_KERNEL);
  730. if (!description)
  731. return -ENOMEM;
  732. if (copy_from_user(description, optval, optlen)) {
  733. kfree(description);
  734. return -EFAULT;
  735. }
  736. description[optlen] = 0;
  737. key = request_key(&key_type_rxrpc, description, NULL);
  738. if (IS_ERR(key)) {
  739. kfree(description);
  740. _leave(" = %ld", PTR_ERR(key));
  741. return PTR_ERR(key);
  742. }
  743. rx->key = key;
  744. kfree(description);
  745. _leave(" = 0 [key %x]", key->serial);
  746. return 0;
  747. }
  748. /*
  749. * grab the security keyring for a server socket
  750. */
  751. int rxrpc_server_keyring(struct rxrpc_sock *rx, char __user *optval,
  752. int optlen)
  753. {
  754. struct key *key;
  755. char *description;
  756. _enter("");
  757. if (optlen <= 0 || optlen > PAGE_SIZE - 1)
  758. return -EINVAL;
  759. description = kmalloc(optlen + 1, GFP_KERNEL);
  760. if (!description)
  761. return -ENOMEM;
  762. if (copy_from_user(description, optval, optlen)) {
  763. kfree(description);
  764. return -EFAULT;
  765. }
  766. description[optlen] = 0;
  767. key = request_key(&key_type_keyring, description, NULL);
  768. if (IS_ERR(key)) {
  769. kfree(description);
  770. _leave(" = %ld", PTR_ERR(key));
  771. return PTR_ERR(key);
  772. }
  773. rx->securities = key;
  774. kfree(description);
  775. _leave(" = 0 [key %x]", key->serial);
  776. return 0;
  777. }
  778. /*
  779. * generate a server data key
  780. */
  781. int rxrpc_get_server_data_key(struct rxrpc_connection *conn,
  782. const void *session_key,
  783. time_t expiry,
  784. u32 kvno)
  785. {
  786. const struct cred *cred = current_cred();
  787. struct key *key;
  788. int ret;
  789. struct {
  790. u32 kver;
  791. struct rxrpc_key_data_v1 v1;
  792. } data;
  793. _enter("");
  794. key = key_alloc(&key_type_rxrpc, "x", 0, 0, cred, 0,
  795. KEY_ALLOC_NOT_IN_QUOTA);
  796. if (IS_ERR(key)) {
  797. _leave(" = -ENOMEM [alloc %ld]", PTR_ERR(key));
  798. return -ENOMEM;
  799. }
  800. _debug("key %d", key_serial(key));
  801. data.kver = 1;
  802. data.v1.security_index = RXRPC_SECURITY_RXKAD;
  803. data.v1.ticket_length = 0;
  804. data.v1.expiry = expiry;
  805. data.v1.kvno = 0;
  806. memcpy(&data.v1.session_key, session_key, sizeof(data.v1.session_key));
  807. ret = key_instantiate_and_link(key, &data, sizeof(data), NULL, NULL);
  808. if (ret < 0)
  809. goto error;
  810. conn->key = key;
  811. _leave(" = 0 [%d]", key_serial(key));
  812. return 0;
  813. error:
  814. key_revoke(key);
  815. key_put(key);
  816. _leave(" = -ENOMEM [ins %d]", ret);
  817. return -ENOMEM;
  818. }
  819. EXPORT_SYMBOL(rxrpc_get_server_data_key);
  820. /**
  821. * rxrpc_get_null_key - Generate a null RxRPC key
  822. * @keyname: The name to give the key.
  823. *
  824. * Generate a null RxRPC key that can be used to indicate anonymous security is
  825. * required for a particular domain.
  826. */
  827. struct key *rxrpc_get_null_key(const char *keyname)
  828. {
  829. const struct cred *cred = current_cred();
  830. struct key *key;
  831. int ret;
  832. key = key_alloc(&key_type_rxrpc, keyname, 0, 0, cred,
  833. KEY_POS_SEARCH, KEY_ALLOC_NOT_IN_QUOTA);
  834. if (IS_ERR(key))
  835. return key;
  836. ret = key_instantiate_and_link(key, NULL, 0, NULL, NULL);
  837. if (ret < 0) {
  838. key_revoke(key);
  839. key_put(key);
  840. return ERR_PTR(ret);
  841. }
  842. return key;
  843. }
  844. EXPORT_SYMBOL(rxrpc_get_null_key);
  845. /*
  846. * read the contents of an rxrpc key
  847. * - this returns the result in XDR form
  848. */
  849. static long rxrpc_read(const struct key *key,
  850. char __user *buffer, size_t buflen)
  851. {
  852. const struct rxrpc_key_token *token;
  853. const struct krb5_principal *princ;
  854. size_t size;
  855. __be32 __user *xdr, *oldxdr;
  856. u32 cnlen, toksize, ntoks, tok, zero;
  857. u16 toksizes[AFSTOKEN_MAX];
  858. int loop;
  859. _enter("");
  860. /* we don't know what form we should return non-AFS keys in */
  861. if (memcmp(key->description, "afs@", 4) != 0)
  862. return -EOPNOTSUPP;
  863. cnlen = strlen(key->description + 4);
  864. #define RND(X) (((X) + 3) & ~3)
  865. /* AFS keys we return in XDR form, so we need to work out the size of
  866. * the XDR */
  867. size = 2 * 4; /* flags, cellname len */
  868. size += RND(cnlen); /* cellname */
  869. size += 1 * 4; /* token count */
  870. ntoks = 0;
  871. for (token = key->payload.data; token; token = token->next) {
  872. toksize = 4; /* sec index */
  873. switch (token->security_index) {
  874. case RXRPC_SECURITY_RXKAD:
  875. toksize += 8 * 4; /* viceid, kvno, key*2, begin,
  876. * end, primary, tktlen */
  877. toksize += RND(token->kad->ticket_len);
  878. break;
  879. case RXRPC_SECURITY_RXK5:
  880. princ = &token->k5->client;
  881. toksize += 4 + princ->n_name_parts * 4;
  882. for (loop = 0; loop < princ->n_name_parts; loop++)
  883. toksize += RND(strlen(princ->name_parts[loop]));
  884. toksize += 4 + RND(strlen(princ->realm));
  885. princ = &token->k5->server;
  886. toksize += 4 + princ->n_name_parts * 4;
  887. for (loop = 0; loop < princ->n_name_parts; loop++)
  888. toksize += RND(strlen(princ->name_parts[loop]));
  889. toksize += 4 + RND(strlen(princ->realm));
  890. toksize += 8 + RND(token->k5->session.data_len);
  891. toksize += 4 * 8 + 2 * 4;
  892. toksize += 4 + token->k5->n_addresses * 8;
  893. for (loop = 0; loop < token->k5->n_addresses; loop++)
  894. toksize += RND(token->k5->addresses[loop].data_len);
  895. toksize += 4 + RND(token->k5->ticket_len);
  896. toksize += 4 + RND(token->k5->ticket2_len);
  897. toksize += 4 + token->k5->n_authdata * 8;
  898. for (loop = 0; loop < token->k5->n_authdata; loop++)
  899. toksize += RND(token->k5->authdata[loop].data_len);
  900. break;
  901. default: /* we have a ticket we can't encode */
  902. BUG();
  903. continue;
  904. }
  905. _debug("token[%u]: toksize=%u", ntoks, toksize);
  906. ASSERTCMP(toksize, <=, AFSTOKEN_LENGTH_MAX);
  907. toksizes[ntoks++] = toksize;
  908. size += toksize + 4; /* each token has a length word */
  909. }
  910. #undef RND
  911. if (!buffer || buflen < size)
  912. return size;
  913. xdr = (__be32 __user *) buffer;
  914. zero = 0;
  915. #define ENCODE(x) \
  916. do { \
  917. __be32 y = htonl(x); \
  918. if (put_user(y, xdr++) < 0) \
  919. goto fault; \
  920. } while(0)
  921. #define ENCODE_DATA(l, s) \
  922. do { \
  923. u32 _l = (l); \
  924. ENCODE(l); \
  925. if (copy_to_user(xdr, (s), _l) != 0) \
  926. goto fault; \
  927. if (_l & 3 && \
  928. copy_to_user((u8 *)xdr + _l, &zero, 4 - (_l & 3)) != 0) \
  929. goto fault; \
  930. xdr += (_l + 3) >> 2; \
  931. } while(0)
  932. #define ENCODE64(x) \
  933. do { \
  934. __be64 y = cpu_to_be64(x); \
  935. if (copy_to_user(xdr, &y, 8) != 0) \
  936. goto fault; \
  937. xdr += 8 >> 2; \
  938. } while(0)
  939. #define ENCODE_STR(s) \
  940. do { \
  941. const char *_s = (s); \
  942. ENCODE_DATA(strlen(_s), _s); \
  943. } while(0)
  944. ENCODE(0); /* flags */
  945. ENCODE_DATA(cnlen, key->description + 4); /* cellname */
  946. ENCODE(ntoks);
  947. tok = 0;
  948. for (token = key->payload.data; token; token = token->next) {
  949. toksize = toksizes[tok++];
  950. ENCODE(toksize);
  951. oldxdr = xdr;
  952. ENCODE(token->security_index);
  953. switch (token->security_index) {
  954. case RXRPC_SECURITY_RXKAD:
  955. ENCODE(token->kad->vice_id);
  956. ENCODE(token->kad->kvno);
  957. ENCODE_DATA(8, token->kad->session_key);
  958. ENCODE(token->kad->start);
  959. ENCODE(token->kad->expiry);
  960. ENCODE(token->kad->primary_flag);
  961. ENCODE_DATA(token->kad->ticket_len, token->kad->ticket);
  962. break;
  963. case RXRPC_SECURITY_RXK5:
  964. princ = &token->k5->client;
  965. ENCODE(princ->n_name_parts);
  966. for (loop = 0; loop < princ->n_name_parts; loop++)
  967. ENCODE_STR(princ->name_parts[loop]);
  968. ENCODE_STR(princ->realm);
  969. princ = &token->k5->server;
  970. ENCODE(princ->n_name_parts);
  971. for (loop = 0; loop < princ->n_name_parts; loop++)
  972. ENCODE_STR(princ->name_parts[loop]);
  973. ENCODE_STR(princ->realm);
  974. ENCODE(token->k5->session.tag);
  975. ENCODE_DATA(token->k5->session.data_len,
  976. token->k5->session.data);
  977. ENCODE64(token->k5->authtime);
  978. ENCODE64(token->k5->starttime);
  979. ENCODE64(token->k5->endtime);
  980. ENCODE64(token->k5->renew_till);
  981. ENCODE(token->k5->is_skey);
  982. ENCODE(token->k5->flags);
  983. ENCODE(token->k5->n_addresses);
  984. for (loop = 0; loop < token->k5->n_addresses; loop++) {
  985. ENCODE(token->k5->addresses[loop].tag);
  986. ENCODE_DATA(token->k5->addresses[loop].data_len,
  987. token->k5->addresses[loop].data);
  988. }
  989. ENCODE_DATA(token->k5->ticket_len, token->k5->ticket);
  990. ENCODE_DATA(token->k5->ticket2_len, token->k5->ticket2);
  991. ENCODE(token->k5->n_authdata);
  992. for (loop = 0; loop < token->k5->n_authdata; loop++) {
  993. ENCODE(token->k5->authdata[loop].tag);
  994. ENCODE_DATA(token->k5->authdata[loop].data_len,
  995. token->k5->authdata[loop].data);
  996. }
  997. break;
  998. default:
  999. BUG();
  1000. break;
  1001. }
  1002. ASSERTCMP((unsigned long)xdr - (unsigned long)oldxdr, ==,
  1003. toksize);
  1004. }
  1005. #undef ENCODE_STR
  1006. #undef ENCODE_DATA
  1007. #undef ENCODE64
  1008. #undef ENCODE
  1009. ASSERTCMP(tok, ==, ntoks);
  1010. ASSERTCMP((char __user *) xdr - buffer, ==, size);
  1011. _leave(" = %zu", size);
  1012. return size;
  1013. fault:
  1014. _leave(" = -EFAULT");
  1015. return -EFAULT;
  1016. }