auth_x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783
  1. #include <linux/ceph/ceph_debug.h>
  2. #include <linux/err.h>
  3. #include <linux/module.h>
  4. #include <linux/random.h>
  5. #include <linux/slab.h>
  6. #include <linux/ceph/decode.h>
  7. #include <linux/ceph/auth.h>
  8. #include <linux/ceph/messenger.h>
  9. #include "crypto.h"
  10. #include "auth_x.h"
  11. #include "auth_x_protocol.h"
  12. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed);
  13. static int ceph_x_is_authenticated(struct ceph_auth_client *ac)
  14. {
  15. struct ceph_x_info *xi = ac->private;
  16. int need;
  17. ceph_x_validate_tickets(ac, &need);
  18. dout("ceph_x_is_authenticated want=%d need=%d have=%d\n",
  19. ac->want_keys, need, xi->have_keys);
  20. return (ac->want_keys & xi->have_keys) == ac->want_keys;
  21. }
  22. static int ceph_x_should_authenticate(struct ceph_auth_client *ac)
  23. {
  24. struct ceph_x_info *xi = ac->private;
  25. int need;
  26. ceph_x_validate_tickets(ac, &need);
  27. dout("ceph_x_should_authenticate want=%d need=%d have=%d\n",
  28. ac->want_keys, need, xi->have_keys);
  29. return need != 0;
  30. }
  31. static int ceph_x_encrypt_buflen(int ilen)
  32. {
  33. return sizeof(struct ceph_x_encrypt_header) + ilen + 16 +
  34. sizeof(u32);
  35. }
  36. static int ceph_x_encrypt(struct ceph_crypto_key *secret,
  37. void *ibuf, int ilen, void *obuf, size_t olen)
  38. {
  39. struct ceph_x_encrypt_header head = {
  40. .struct_v = 1,
  41. .magic = cpu_to_le64(CEPHX_ENC_MAGIC)
  42. };
  43. size_t len = olen - sizeof(u32);
  44. int ret;
  45. ret = ceph_encrypt2(secret, obuf + sizeof(u32), &len,
  46. &head, sizeof(head), ibuf, ilen);
  47. if (ret)
  48. return ret;
  49. ceph_encode_32(&obuf, len);
  50. return len + sizeof(u32);
  51. }
  52. static int ceph_x_decrypt(struct ceph_crypto_key *secret,
  53. void **p, void *end, void **obuf, size_t olen)
  54. {
  55. struct ceph_x_encrypt_header head;
  56. size_t head_len = sizeof(head);
  57. int len, ret;
  58. len = ceph_decode_32(p);
  59. if (*p + len > end)
  60. return -EINVAL;
  61. dout("ceph_x_decrypt len %d\n", len);
  62. if (*obuf == NULL) {
  63. *obuf = kmalloc(len, GFP_NOFS);
  64. if (!*obuf)
  65. return -ENOMEM;
  66. olen = len;
  67. }
  68. ret = ceph_decrypt2(secret, &head, &head_len, *obuf, &olen, *p, len);
  69. if (ret)
  70. return ret;
  71. if (head.struct_v != 1 || le64_to_cpu(head.magic) != CEPHX_ENC_MAGIC)
  72. return -EPERM;
  73. *p += len;
  74. return olen;
  75. }
  76. /*
  77. * get existing (or insert new) ticket handler
  78. */
  79. static struct ceph_x_ticket_handler *
  80. get_ticket_handler(struct ceph_auth_client *ac, int service)
  81. {
  82. struct ceph_x_ticket_handler *th;
  83. struct ceph_x_info *xi = ac->private;
  84. struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
  85. while (*p) {
  86. parent = *p;
  87. th = rb_entry(parent, struct ceph_x_ticket_handler, node);
  88. if (service < th->service)
  89. p = &(*p)->rb_left;
  90. else if (service > th->service)
  91. p = &(*p)->rb_right;
  92. else
  93. return th;
  94. }
  95. /* add it */
  96. th = kzalloc(sizeof(*th), GFP_NOFS);
  97. if (!th)
  98. return ERR_PTR(-ENOMEM);
  99. th->service = service;
  100. rb_link_node(&th->node, parent, p);
  101. rb_insert_color(&th->node, &xi->ticket_handlers);
  102. return th;
  103. }
  104. static void remove_ticket_handler(struct ceph_auth_client *ac,
  105. struct ceph_x_ticket_handler *th)
  106. {
  107. struct ceph_x_info *xi = ac->private;
  108. dout("remove_ticket_handler %p %d\n", th, th->service);
  109. rb_erase(&th->node, &xi->ticket_handlers);
  110. ceph_crypto_key_destroy(&th->session_key);
  111. if (th->ticket_blob)
  112. ceph_buffer_put(th->ticket_blob);
  113. kfree(th);
  114. }
  115. static int process_one_ticket(struct ceph_auth_client *ac,
  116. struct ceph_crypto_key *secret,
  117. void **p, void *end)
  118. {
  119. struct ceph_x_info *xi = ac->private;
  120. int type;
  121. u8 tkt_struct_v, blob_struct_v;
  122. struct ceph_x_ticket_handler *th;
  123. void *dbuf = NULL;
  124. void *dp, *dend;
  125. int dlen;
  126. char is_enc;
  127. struct timespec validity;
  128. struct ceph_crypto_key old_key;
  129. void *ticket_buf = NULL;
  130. void *tp, *tpend;
  131. void **ptp;
  132. struct ceph_timespec new_validity;
  133. struct ceph_crypto_key new_session_key;
  134. struct ceph_buffer *new_ticket_blob;
  135. unsigned long new_expires, new_renew_after;
  136. u64 new_secret_id;
  137. int ret;
  138. ceph_decode_need(p, end, sizeof(u32) + 1, bad);
  139. type = ceph_decode_32(p);
  140. dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
  141. tkt_struct_v = ceph_decode_8(p);
  142. if (tkt_struct_v != 1)
  143. goto bad;
  144. th = get_ticket_handler(ac, type);
  145. if (IS_ERR(th)) {
  146. ret = PTR_ERR(th);
  147. goto out;
  148. }
  149. /* blob for me */
  150. dlen = ceph_x_decrypt(secret, p, end, &dbuf, 0);
  151. if (dlen <= 0) {
  152. ret = dlen;
  153. goto out;
  154. }
  155. dout(" decrypted %d bytes\n", dlen);
  156. dp = dbuf;
  157. dend = dp + dlen;
  158. tkt_struct_v = ceph_decode_8(&dp);
  159. if (tkt_struct_v != 1)
  160. goto bad;
  161. memcpy(&old_key, &th->session_key, sizeof(old_key));
  162. ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
  163. if (ret)
  164. goto out;
  165. ceph_decode_copy(&dp, &new_validity, sizeof(new_validity));
  166. ceph_decode_timespec(&validity, &new_validity);
  167. new_expires = get_seconds() + validity.tv_sec;
  168. new_renew_after = new_expires - (validity.tv_sec / 4);
  169. dout(" expires=%lu renew_after=%lu\n", new_expires,
  170. new_renew_after);
  171. /* ticket blob for service */
  172. ceph_decode_8_safe(p, end, is_enc, bad);
  173. if (is_enc) {
  174. /* encrypted */
  175. dout(" encrypted ticket\n");
  176. dlen = ceph_x_decrypt(&old_key, p, end, &ticket_buf, 0);
  177. if (dlen < 0) {
  178. ret = dlen;
  179. goto out;
  180. }
  181. tp = ticket_buf;
  182. ptp = &tp;
  183. tpend = *ptp + dlen;
  184. } else {
  185. /* unencrypted */
  186. ptp = p;
  187. tpend = end;
  188. }
  189. ceph_decode_32_safe(ptp, tpend, dlen, bad);
  190. dout(" ticket blob is %d bytes\n", dlen);
  191. ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
  192. blob_struct_v = ceph_decode_8(ptp);
  193. new_secret_id = ceph_decode_64(ptp);
  194. ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
  195. if (ret)
  196. goto out;
  197. /* all is well, update our ticket */
  198. ceph_crypto_key_destroy(&th->session_key);
  199. if (th->ticket_blob)
  200. ceph_buffer_put(th->ticket_blob);
  201. th->session_key = new_session_key;
  202. th->ticket_blob = new_ticket_blob;
  203. th->validity = new_validity;
  204. th->secret_id = new_secret_id;
  205. th->expires = new_expires;
  206. th->renew_after = new_renew_after;
  207. dout(" got ticket service %d (%s) secret_id %lld len %d\n",
  208. type, ceph_entity_type_name(type), th->secret_id,
  209. (int)th->ticket_blob->vec.iov_len);
  210. xi->have_keys |= th->service;
  211. out:
  212. kfree(ticket_buf);
  213. kfree(dbuf);
  214. return ret;
  215. bad:
  216. ret = -EINVAL;
  217. goto out;
  218. }
  219. static int ceph_x_proc_ticket_reply(struct ceph_auth_client *ac,
  220. struct ceph_crypto_key *secret,
  221. void *buf, void *end)
  222. {
  223. void *p = buf;
  224. u8 reply_struct_v;
  225. u32 num;
  226. int ret;
  227. ceph_decode_8_safe(&p, end, reply_struct_v, bad);
  228. if (reply_struct_v != 1)
  229. return -EINVAL;
  230. ceph_decode_32_safe(&p, end, num, bad);
  231. dout("%d tickets\n", num);
  232. while (num--) {
  233. ret = process_one_ticket(ac, secret, &p, end);
  234. if (ret)
  235. return ret;
  236. }
  237. return 0;
  238. bad:
  239. return -EINVAL;
  240. }
  241. static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
  242. struct ceph_x_ticket_handler *th,
  243. struct ceph_x_authorizer *au)
  244. {
  245. int maxlen;
  246. struct ceph_x_authorize_a *msg_a;
  247. struct ceph_x_authorize_b msg_b;
  248. void *p, *end;
  249. int ret;
  250. int ticket_blob_len =
  251. (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
  252. dout("build_authorizer for %s %p\n",
  253. ceph_entity_type_name(th->service), au);
  254. ceph_crypto_key_destroy(&au->session_key);
  255. ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
  256. if (ret)
  257. return ret;
  258. maxlen = sizeof(*msg_a) + sizeof(msg_b) +
  259. ceph_x_encrypt_buflen(ticket_blob_len);
  260. dout(" need len %d\n", maxlen);
  261. if (au->buf && au->buf->alloc_len < maxlen) {
  262. ceph_buffer_put(au->buf);
  263. au->buf = NULL;
  264. }
  265. if (!au->buf) {
  266. au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
  267. if (!au->buf) {
  268. ceph_crypto_key_destroy(&au->session_key);
  269. return -ENOMEM;
  270. }
  271. }
  272. au->service = th->service;
  273. au->secret_id = th->secret_id;
  274. msg_a = au->buf->vec.iov_base;
  275. msg_a->struct_v = 1;
  276. msg_a->global_id = cpu_to_le64(ac->global_id);
  277. msg_a->service_id = cpu_to_le32(th->service);
  278. msg_a->ticket_blob.struct_v = 1;
  279. msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
  280. msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
  281. if (ticket_blob_len) {
  282. memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
  283. th->ticket_blob->vec.iov_len);
  284. }
  285. dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
  286. le64_to_cpu(msg_a->ticket_blob.secret_id));
  287. p = msg_a + 1;
  288. p += ticket_blob_len;
  289. end = au->buf->vec.iov_base + au->buf->vec.iov_len;
  290. get_random_bytes(&au->nonce, sizeof(au->nonce));
  291. msg_b.struct_v = 1;
  292. msg_b.nonce = cpu_to_le64(au->nonce);
  293. ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
  294. p, end - p);
  295. if (ret < 0)
  296. goto out_buf;
  297. p += ret;
  298. au->buf->vec.iov_len = p - au->buf->vec.iov_base;
  299. dout(" built authorizer nonce %llx len %d\n", au->nonce,
  300. (int)au->buf->vec.iov_len);
  301. BUG_ON(au->buf->vec.iov_len > maxlen);
  302. return 0;
  303. out_buf:
  304. ceph_buffer_put(au->buf);
  305. au->buf = NULL;
  306. return ret;
  307. }
  308. static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
  309. void **p, void *end)
  310. {
  311. ceph_decode_need(p, end, 1 + sizeof(u64), bad);
  312. ceph_encode_8(p, 1);
  313. ceph_encode_64(p, th->secret_id);
  314. if (th->ticket_blob) {
  315. const char *buf = th->ticket_blob->vec.iov_base;
  316. u32 len = th->ticket_blob->vec.iov_len;
  317. ceph_encode_32_safe(p, end, len, bad);
  318. ceph_encode_copy_safe(p, end, buf, len, bad);
  319. } else {
  320. ceph_encode_32_safe(p, end, 0, bad);
  321. }
  322. return 0;
  323. bad:
  324. return -ERANGE;
  325. }
  326. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
  327. {
  328. int want = ac->want_keys;
  329. struct ceph_x_info *xi = ac->private;
  330. int service;
  331. *pneed = ac->want_keys & ~(xi->have_keys);
  332. for (service = 1; service <= want; service <<= 1) {
  333. struct ceph_x_ticket_handler *th;
  334. if (!(ac->want_keys & service))
  335. continue;
  336. if (*pneed & service)
  337. continue;
  338. th = get_ticket_handler(ac, service);
  339. if (IS_ERR(th)) {
  340. *pneed |= service;
  341. continue;
  342. }
  343. if (get_seconds() >= th->renew_after)
  344. *pneed |= service;
  345. if (get_seconds() >= th->expires)
  346. xi->have_keys &= ~service;
  347. }
  348. }
  349. static int ceph_x_build_request(struct ceph_auth_client *ac,
  350. void *buf, void *end)
  351. {
  352. struct ceph_x_info *xi = ac->private;
  353. int need;
  354. struct ceph_x_request_header *head = buf;
  355. int ret;
  356. struct ceph_x_ticket_handler *th =
  357. get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  358. if (IS_ERR(th))
  359. return PTR_ERR(th);
  360. ceph_x_validate_tickets(ac, &need);
  361. dout("build_request want %x have %x need %x\n",
  362. ac->want_keys, xi->have_keys, need);
  363. if (need & CEPH_ENTITY_TYPE_AUTH) {
  364. struct ceph_x_authenticate *auth = (void *)(head + 1);
  365. void *p = auth + 1;
  366. struct ceph_x_challenge_blob tmp;
  367. char tmp_enc[40];
  368. u64 *u;
  369. if (p > end)
  370. return -ERANGE;
  371. dout(" get_auth_session_key\n");
  372. head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
  373. /* encrypt and hash */
  374. get_random_bytes(&auth->client_challenge, sizeof(u64));
  375. tmp.client_challenge = auth->client_challenge;
  376. tmp.server_challenge = cpu_to_le64(xi->server_challenge);
  377. ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
  378. tmp_enc, sizeof(tmp_enc));
  379. if (ret < 0)
  380. return ret;
  381. auth->struct_v = 1;
  382. auth->key = 0;
  383. for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
  384. auth->key ^= *(__le64 *)u;
  385. dout(" server_challenge %llx client_challenge %llx key %llx\n",
  386. xi->server_challenge, le64_to_cpu(auth->client_challenge),
  387. le64_to_cpu(auth->key));
  388. /* now encode the old ticket if exists */
  389. ret = ceph_x_encode_ticket(th, &p, end);
  390. if (ret < 0)
  391. return ret;
  392. return p - buf;
  393. }
  394. if (need) {
  395. void *p = head + 1;
  396. struct ceph_x_service_ticket_request *req;
  397. if (p > end)
  398. return -ERANGE;
  399. head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
  400. ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
  401. if (ret)
  402. return ret;
  403. ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
  404. xi->auth_authorizer.buf->vec.iov_len);
  405. req = p;
  406. req->keys = cpu_to_le32(need);
  407. p += sizeof(*req);
  408. return p - buf;
  409. }
  410. return 0;
  411. }
  412. static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
  413. void *buf, void *end)
  414. {
  415. struct ceph_x_info *xi = ac->private;
  416. struct ceph_x_reply_header *head = buf;
  417. struct ceph_x_ticket_handler *th;
  418. int len = end - buf;
  419. int op;
  420. int ret;
  421. if (result)
  422. return result; /* XXX hmm? */
  423. if (xi->starting) {
  424. /* it's a hello */
  425. struct ceph_x_server_challenge *sc = buf;
  426. if (len != sizeof(*sc))
  427. return -EINVAL;
  428. xi->server_challenge = le64_to_cpu(sc->server_challenge);
  429. dout("handle_reply got server challenge %llx\n",
  430. xi->server_challenge);
  431. xi->starting = false;
  432. xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
  433. return -EAGAIN;
  434. }
  435. op = le16_to_cpu(head->op);
  436. result = le32_to_cpu(head->result);
  437. dout("handle_reply op %d result %d\n", op, result);
  438. switch (op) {
  439. case CEPHX_GET_AUTH_SESSION_KEY:
  440. /* verify auth key */
  441. ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
  442. buf + sizeof(*head), end);
  443. break;
  444. case CEPHX_GET_PRINCIPAL_SESSION_KEY:
  445. th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  446. if (IS_ERR(th))
  447. return PTR_ERR(th);
  448. ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
  449. buf + sizeof(*head), end);
  450. break;
  451. default:
  452. return -EINVAL;
  453. }
  454. if (ret)
  455. return ret;
  456. if (ac->want_keys == xi->have_keys)
  457. return 0;
  458. return -EAGAIN;
  459. }
  460. static int ceph_x_create_authorizer(
  461. struct ceph_auth_client *ac, int peer_type,
  462. struct ceph_auth_handshake *auth)
  463. {
  464. struct ceph_x_authorizer *au;
  465. struct ceph_x_ticket_handler *th;
  466. int ret;
  467. th = get_ticket_handler(ac, peer_type);
  468. if (IS_ERR(th))
  469. return PTR_ERR(th);
  470. au = kzalloc(sizeof(*au), GFP_NOFS);
  471. if (!au)
  472. return -ENOMEM;
  473. ret = ceph_x_build_authorizer(ac, th, au);
  474. if (ret) {
  475. kfree(au);
  476. return ret;
  477. }
  478. auth->authorizer = (struct ceph_authorizer *) au;
  479. auth->authorizer_buf = au->buf->vec.iov_base;
  480. auth->authorizer_buf_len = au->buf->vec.iov_len;
  481. auth->authorizer_reply_buf = au->reply_buf;
  482. auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
  483. auth->sign_message = ac->ops->sign_message;
  484. auth->check_message_signature = ac->ops->check_message_signature;
  485. return 0;
  486. }
  487. static int ceph_x_update_authorizer(
  488. struct ceph_auth_client *ac, int peer_type,
  489. struct ceph_auth_handshake *auth)
  490. {
  491. struct ceph_x_authorizer *au;
  492. struct ceph_x_ticket_handler *th;
  493. th = get_ticket_handler(ac, peer_type);
  494. if (IS_ERR(th))
  495. return PTR_ERR(th);
  496. au = (struct ceph_x_authorizer *)auth->authorizer;
  497. if (au->secret_id < th->secret_id) {
  498. dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
  499. au->service, au->secret_id, th->secret_id);
  500. return ceph_x_build_authorizer(ac, th, au);
  501. }
  502. return 0;
  503. }
  504. static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
  505. struct ceph_authorizer *a, size_t len)
  506. {
  507. struct ceph_x_authorizer *au = (void *)a;
  508. int ret = 0;
  509. struct ceph_x_authorize_reply reply;
  510. void *preply = &reply;
  511. void *p = au->reply_buf;
  512. void *end = p + sizeof(au->reply_buf);
  513. ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply));
  514. if (ret < 0)
  515. return ret;
  516. if (ret != sizeof(reply))
  517. return -EPERM;
  518. if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
  519. ret = -EPERM;
  520. else
  521. ret = 0;
  522. dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
  523. au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
  524. return ret;
  525. }
  526. static void ceph_x_destroy_authorizer(struct ceph_auth_client *ac,
  527. struct ceph_authorizer *a)
  528. {
  529. struct ceph_x_authorizer *au = (void *)a;
  530. ceph_crypto_key_destroy(&au->session_key);
  531. ceph_buffer_put(au->buf);
  532. kfree(au);
  533. }
  534. static void ceph_x_reset(struct ceph_auth_client *ac)
  535. {
  536. struct ceph_x_info *xi = ac->private;
  537. dout("reset\n");
  538. xi->starting = true;
  539. xi->server_challenge = 0;
  540. }
  541. static void ceph_x_destroy(struct ceph_auth_client *ac)
  542. {
  543. struct ceph_x_info *xi = ac->private;
  544. struct rb_node *p;
  545. dout("ceph_x_destroy %p\n", ac);
  546. ceph_crypto_key_destroy(&xi->secret);
  547. while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
  548. struct ceph_x_ticket_handler *th =
  549. rb_entry(p, struct ceph_x_ticket_handler, node);
  550. remove_ticket_handler(ac, th);
  551. }
  552. if (xi->auth_authorizer.buf)
  553. ceph_buffer_put(xi->auth_authorizer.buf);
  554. kfree(ac->private);
  555. ac->private = NULL;
  556. }
  557. static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
  558. int peer_type)
  559. {
  560. struct ceph_x_ticket_handler *th;
  561. th = get_ticket_handler(ac, peer_type);
  562. if (!IS_ERR(th))
  563. memset(&th->validity, 0, sizeof(th->validity));
  564. }
  565. static int calcu_signature(struct ceph_x_authorizer *au,
  566. struct ceph_msg *msg, __le64 *sig)
  567. {
  568. int ret;
  569. char tmp_enc[40];
  570. __le32 tmp[5] = {
  571. cpu_to_le32(16), msg->hdr.crc, msg->footer.front_crc,
  572. msg->footer.middle_crc, msg->footer.data_crc,
  573. };
  574. ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp),
  575. tmp_enc, sizeof(tmp_enc));
  576. if (ret < 0)
  577. return ret;
  578. *sig = *(__le64*)(tmp_enc + 4);
  579. return 0;
  580. }
  581. static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
  582. struct ceph_msg *msg)
  583. {
  584. int ret;
  585. if (!auth->authorizer)
  586. return 0;
  587. ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
  588. msg, &msg->footer.sig);
  589. if (ret < 0)
  590. return ret;
  591. msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
  592. return 0;
  593. }
  594. static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
  595. struct ceph_msg *msg)
  596. {
  597. __le64 sig_check;
  598. int ret;
  599. if (!auth->authorizer)
  600. return 0;
  601. ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
  602. msg, &sig_check);
  603. if (ret < 0)
  604. return ret;
  605. if (sig_check == msg->footer.sig)
  606. return 0;
  607. if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
  608. dout("ceph_x_check_message_signature %p has signature %llx "
  609. "expect %llx\n", msg, msg->footer.sig, sig_check);
  610. else
  611. dout("ceph_x_check_message_signature %p sender did not set "
  612. "CEPH_MSG_FOOTER_SIGNED\n", msg);
  613. return -EBADMSG;
  614. }
  615. static const struct ceph_auth_client_ops ceph_x_ops = {
  616. .name = "x",
  617. .is_authenticated = ceph_x_is_authenticated,
  618. .should_authenticate = ceph_x_should_authenticate,
  619. .build_request = ceph_x_build_request,
  620. .handle_reply = ceph_x_handle_reply,
  621. .create_authorizer = ceph_x_create_authorizer,
  622. .update_authorizer = ceph_x_update_authorizer,
  623. .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
  624. .destroy_authorizer = ceph_x_destroy_authorizer,
  625. .invalidate_authorizer = ceph_x_invalidate_authorizer,
  626. .reset = ceph_x_reset,
  627. .destroy = ceph_x_destroy,
  628. .sign_message = ceph_x_sign_message,
  629. .check_message_signature = ceph_x_check_message_signature,
  630. };
  631. int ceph_x_init(struct ceph_auth_client *ac)
  632. {
  633. struct ceph_x_info *xi;
  634. int ret;
  635. dout("ceph_x_init %p\n", ac);
  636. ret = -ENOMEM;
  637. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  638. if (!xi)
  639. goto out;
  640. ret = -EINVAL;
  641. if (!ac->key) {
  642. pr_err("no secret set (for auth_x protocol)\n");
  643. goto out_nomem;
  644. }
  645. ret = ceph_crypto_key_clone(&xi->secret, ac->key);
  646. if (ret < 0) {
  647. pr_err("cannot clone key: %d\n", ret);
  648. goto out_nomem;
  649. }
  650. xi->starting = true;
  651. xi->ticket_handlers = RB_ROOT;
  652. ac->protocol = CEPH_AUTH_CEPHX;
  653. ac->private = xi;
  654. ac->ops = &ceph_x_ops;
  655. return 0;
  656. out_nomem:
  657. kfree(xi);
  658. out:
  659. return ret;
  660. }