ar-key.c 28 KB

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