auth_x.c 19 KB

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