ar-key.c 28 KB

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