auth_x.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818
  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_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_timespec(&validity, dp);
  166. dp += sizeof(struct ceph_timespec);
  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->secret_id = new_secret_id;
  204. th->expires = new_expires;
  205. th->renew_after = new_renew_after;
  206. th->have_key = true;
  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 void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
  242. {
  243. ceph_crypto_key_destroy(&au->session_key);
  244. if (au->buf) {
  245. ceph_buffer_put(au->buf);
  246. au->buf = NULL;
  247. }
  248. }
  249. static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
  250. struct ceph_x_ticket_handler *th,
  251. struct ceph_x_authorizer *au)
  252. {
  253. int maxlen;
  254. struct ceph_x_authorize_a *msg_a;
  255. struct ceph_x_authorize_b msg_b;
  256. void *p, *end;
  257. int ret;
  258. int ticket_blob_len =
  259. (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
  260. dout("build_authorizer for %s %p\n",
  261. ceph_entity_type_name(th->service), au);
  262. ceph_crypto_key_destroy(&au->session_key);
  263. ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
  264. if (ret)
  265. goto out_au;
  266. maxlen = sizeof(*msg_a) + sizeof(msg_b) +
  267. ceph_x_encrypt_buflen(ticket_blob_len);
  268. dout(" need len %d\n", maxlen);
  269. if (au->buf && au->buf->alloc_len < maxlen) {
  270. ceph_buffer_put(au->buf);
  271. au->buf = NULL;
  272. }
  273. if (!au->buf) {
  274. au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
  275. if (!au->buf) {
  276. ret = -ENOMEM;
  277. goto out_au;
  278. }
  279. }
  280. au->service = th->service;
  281. au->secret_id = th->secret_id;
  282. msg_a = au->buf->vec.iov_base;
  283. msg_a->struct_v = 1;
  284. msg_a->global_id = cpu_to_le64(ac->global_id);
  285. msg_a->service_id = cpu_to_le32(th->service);
  286. msg_a->ticket_blob.struct_v = 1;
  287. msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
  288. msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
  289. if (ticket_blob_len) {
  290. memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
  291. th->ticket_blob->vec.iov_len);
  292. }
  293. dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
  294. le64_to_cpu(msg_a->ticket_blob.secret_id));
  295. p = msg_a + 1;
  296. p += ticket_blob_len;
  297. end = au->buf->vec.iov_base + au->buf->vec.iov_len;
  298. get_random_bytes(&au->nonce, sizeof(au->nonce));
  299. msg_b.struct_v = 1;
  300. msg_b.nonce = cpu_to_le64(au->nonce);
  301. ret = ceph_x_encrypt(&au->session_key, &msg_b, sizeof(msg_b),
  302. p, end - p);
  303. if (ret < 0)
  304. goto out_au;
  305. p += ret;
  306. au->buf->vec.iov_len = p - au->buf->vec.iov_base;
  307. dout(" built authorizer nonce %llx len %d\n", au->nonce,
  308. (int)au->buf->vec.iov_len);
  309. BUG_ON(au->buf->vec.iov_len > maxlen);
  310. return 0;
  311. out_au:
  312. ceph_x_authorizer_cleanup(au);
  313. return ret;
  314. }
  315. static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
  316. void **p, void *end)
  317. {
  318. ceph_decode_need(p, end, 1 + sizeof(u64), bad);
  319. ceph_encode_8(p, 1);
  320. ceph_encode_64(p, th->secret_id);
  321. if (th->ticket_blob) {
  322. const char *buf = th->ticket_blob->vec.iov_base;
  323. u32 len = th->ticket_blob->vec.iov_len;
  324. ceph_encode_32_safe(p, end, len, bad);
  325. ceph_encode_copy_safe(p, end, buf, len, bad);
  326. } else {
  327. ceph_encode_32_safe(p, end, 0, bad);
  328. }
  329. return 0;
  330. bad:
  331. return -ERANGE;
  332. }
  333. static bool need_key(struct ceph_x_ticket_handler *th)
  334. {
  335. if (!th->have_key)
  336. return true;
  337. return get_seconds() >= th->renew_after;
  338. }
  339. static bool have_key(struct ceph_x_ticket_handler *th)
  340. {
  341. if (th->have_key) {
  342. if (get_seconds() >= th->expires)
  343. th->have_key = false;
  344. }
  345. return th->have_key;
  346. }
  347. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
  348. {
  349. int want = ac->want_keys;
  350. struct ceph_x_info *xi = ac->private;
  351. int service;
  352. *pneed = ac->want_keys & ~(xi->have_keys);
  353. for (service = 1; service <= want; service <<= 1) {
  354. struct ceph_x_ticket_handler *th;
  355. if (!(ac->want_keys & service))
  356. continue;
  357. if (*pneed & service)
  358. continue;
  359. th = get_ticket_handler(ac, service);
  360. if (IS_ERR(th)) {
  361. *pneed |= service;
  362. continue;
  363. }
  364. if (need_key(th))
  365. *pneed |= service;
  366. if (!have_key(th))
  367. xi->have_keys &= ~service;
  368. }
  369. }
  370. static int ceph_x_build_request(struct ceph_auth_client *ac,
  371. void *buf, void *end)
  372. {
  373. struct ceph_x_info *xi = ac->private;
  374. int need;
  375. struct ceph_x_request_header *head = buf;
  376. int ret;
  377. struct ceph_x_ticket_handler *th =
  378. get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  379. if (IS_ERR(th))
  380. return PTR_ERR(th);
  381. ceph_x_validate_tickets(ac, &need);
  382. dout("build_request want %x have %x need %x\n",
  383. ac->want_keys, xi->have_keys, need);
  384. if (need & CEPH_ENTITY_TYPE_AUTH) {
  385. struct ceph_x_authenticate *auth = (void *)(head + 1);
  386. void *p = auth + 1;
  387. struct ceph_x_challenge_blob tmp;
  388. char tmp_enc[40];
  389. u64 *u;
  390. if (p > end)
  391. return -ERANGE;
  392. dout(" get_auth_session_key\n");
  393. head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
  394. /* encrypt and hash */
  395. get_random_bytes(&auth->client_challenge, sizeof(u64));
  396. tmp.client_challenge = auth->client_challenge;
  397. tmp.server_challenge = cpu_to_le64(xi->server_challenge);
  398. ret = ceph_x_encrypt(&xi->secret, &tmp, sizeof(tmp),
  399. tmp_enc, sizeof(tmp_enc));
  400. if (ret < 0)
  401. return ret;
  402. auth->struct_v = 1;
  403. auth->key = 0;
  404. for (u = (u64 *)tmp_enc; u + 1 <= (u64 *)(tmp_enc + ret); u++)
  405. auth->key ^= *(__le64 *)u;
  406. dout(" server_challenge %llx client_challenge %llx key %llx\n",
  407. xi->server_challenge, le64_to_cpu(auth->client_challenge),
  408. le64_to_cpu(auth->key));
  409. /* now encode the old ticket if exists */
  410. ret = ceph_x_encode_ticket(th, &p, end);
  411. if (ret < 0)
  412. return ret;
  413. return p - buf;
  414. }
  415. if (need) {
  416. void *p = head + 1;
  417. struct ceph_x_service_ticket_request *req;
  418. if (p > end)
  419. return -ERANGE;
  420. head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
  421. ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
  422. if (ret)
  423. return ret;
  424. ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
  425. xi->auth_authorizer.buf->vec.iov_len);
  426. req = p;
  427. req->keys = cpu_to_le32(need);
  428. p += sizeof(*req);
  429. return p - buf;
  430. }
  431. return 0;
  432. }
  433. static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
  434. void *buf, void *end)
  435. {
  436. struct ceph_x_info *xi = ac->private;
  437. struct ceph_x_reply_header *head = buf;
  438. struct ceph_x_ticket_handler *th;
  439. int len = end - buf;
  440. int op;
  441. int ret;
  442. if (result)
  443. return result; /* XXX hmm? */
  444. if (xi->starting) {
  445. /* it's a hello */
  446. struct ceph_x_server_challenge *sc = buf;
  447. if (len != sizeof(*sc))
  448. return -EINVAL;
  449. xi->server_challenge = le64_to_cpu(sc->server_challenge);
  450. dout("handle_reply got server challenge %llx\n",
  451. xi->server_challenge);
  452. xi->starting = false;
  453. xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
  454. return -EAGAIN;
  455. }
  456. op = le16_to_cpu(head->op);
  457. result = le32_to_cpu(head->result);
  458. dout("handle_reply op %d result %d\n", op, result);
  459. switch (op) {
  460. case CEPHX_GET_AUTH_SESSION_KEY:
  461. /* verify auth key */
  462. ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
  463. buf + sizeof(*head), end);
  464. break;
  465. case CEPHX_GET_PRINCIPAL_SESSION_KEY:
  466. th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  467. if (IS_ERR(th))
  468. return PTR_ERR(th);
  469. ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
  470. buf + sizeof(*head), end);
  471. break;
  472. default:
  473. return -EINVAL;
  474. }
  475. if (ret)
  476. return ret;
  477. if (ac->want_keys == xi->have_keys)
  478. return 0;
  479. return -EAGAIN;
  480. }
  481. static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
  482. {
  483. struct ceph_x_authorizer *au = (void *)a;
  484. ceph_x_authorizer_cleanup(au);
  485. kfree(au);
  486. }
  487. static int ceph_x_create_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. int ret;
  494. th = get_ticket_handler(ac, peer_type);
  495. if (IS_ERR(th))
  496. return PTR_ERR(th);
  497. au = kzalloc(sizeof(*au), GFP_NOFS);
  498. if (!au)
  499. return -ENOMEM;
  500. au->base.destroy = ceph_x_destroy_authorizer;
  501. ret = ceph_x_build_authorizer(ac, th, au);
  502. if (ret) {
  503. kfree(au);
  504. return ret;
  505. }
  506. auth->authorizer = (struct ceph_authorizer *) au;
  507. auth->authorizer_buf = au->buf->vec.iov_base;
  508. auth->authorizer_buf_len = au->buf->vec.iov_len;
  509. auth->authorizer_reply_buf = au->reply_buf;
  510. auth->authorizer_reply_buf_len = sizeof (au->reply_buf);
  511. auth->sign_message = ac->ops->sign_message;
  512. auth->check_message_signature = ac->ops->check_message_signature;
  513. return 0;
  514. }
  515. static int ceph_x_update_authorizer(
  516. struct ceph_auth_client *ac, int peer_type,
  517. struct ceph_auth_handshake *auth)
  518. {
  519. struct ceph_x_authorizer *au;
  520. struct ceph_x_ticket_handler *th;
  521. th = get_ticket_handler(ac, peer_type);
  522. if (IS_ERR(th))
  523. return PTR_ERR(th);
  524. au = (struct ceph_x_authorizer *)auth->authorizer;
  525. if (au->secret_id < th->secret_id) {
  526. dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
  527. au->service, au->secret_id, th->secret_id);
  528. return ceph_x_build_authorizer(ac, th, au);
  529. }
  530. return 0;
  531. }
  532. static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
  533. struct ceph_authorizer *a, size_t len)
  534. {
  535. struct ceph_x_authorizer *au = (void *)a;
  536. int ret = 0;
  537. struct ceph_x_authorize_reply reply;
  538. void *preply = &reply;
  539. void *p = au->reply_buf;
  540. void *end = p + sizeof(au->reply_buf);
  541. ret = ceph_x_decrypt(&au->session_key, &p, end, &preply, sizeof(reply));
  542. if (ret < 0)
  543. return ret;
  544. if (ret != sizeof(reply))
  545. return -EPERM;
  546. if (au->nonce + 1 != le64_to_cpu(reply.nonce_plus_one))
  547. ret = -EPERM;
  548. else
  549. ret = 0;
  550. dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
  551. au->nonce, le64_to_cpu(reply.nonce_plus_one), ret);
  552. return ret;
  553. }
  554. static void ceph_x_reset(struct ceph_auth_client *ac)
  555. {
  556. struct ceph_x_info *xi = ac->private;
  557. dout("reset\n");
  558. xi->starting = true;
  559. xi->server_challenge = 0;
  560. }
  561. static void ceph_x_destroy(struct ceph_auth_client *ac)
  562. {
  563. struct ceph_x_info *xi = ac->private;
  564. struct rb_node *p;
  565. dout("ceph_x_destroy %p\n", ac);
  566. ceph_crypto_key_destroy(&xi->secret);
  567. while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
  568. struct ceph_x_ticket_handler *th =
  569. rb_entry(p, struct ceph_x_ticket_handler, node);
  570. remove_ticket_handler(ac, th);
  571. }
  572. ceph_x_authorizer_cleanup(&xi->auth_authorizer);
  573. kfree(ac->private);
  574. ac->private = NULL;
  575. }
  576. static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
  577. {
  578. struct ceph_x_ticket_handler *th;
  579. th = get_ticket_handler(ac, peer_type);
  580. if (!IS_ERR(th))
  581. th->have_key = false;
  582. }
  583. static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
  584. int peer_type)
  585. {
  586. /*
  587. * We are to invalidate a service ticket in the hopes of
  588. * getting a new, hopefully more valid, one. But, we won't get
  589. * it unless our AUTH ticket is good, so invalidate AUTH ticket
  590. * as well, just in case.
  591. */
  592. invalidate_ticket(ac, peer_type);
  593. invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
  594. }
  595. static int calcu_signature(struct ceph_x_authorizer *au,
  596. struct ceph_msg *msg, __le64 *sig)
  597. {
  598. int ret;
  599. char tmp_enc[40];
  600. __le32 tmp[5] = {
  601. cpu_to_le32(16), msg->hdr.crc, msg->footer.front_crc,
  602. msg->footer.middle_crc, msg->footer.data_crc,
  603. };
  604. ret = ceph_x_encrypt(&au->session_key, &tmp, sizeof(tmp),
  605. tmp_enc, sizeof(tmp_enc));
  606. if (ret < 0)
  607. return ret;
  608. *sig = *(__le64*)(tmp_enc + 4);
  609. return 0;
  610. }
  611. static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
  612. struct ceph_msg *msg)
  613. {
  614. int ret;
  615. if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
  616. return 0;
  617. ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
  618. msg, &msg->footer.sig);
  619. if (ret < 0)
  620. return ret;
  621. msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
  622. return 0;
  623. }
  624. static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
  625. struct ceph_msg *msg)
  626. {
  627. __le64 sig_check;
  628. int ret;
  629. if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
  630. return 0;
  631. ret = calcu_signature((struct ceph_x_authorizer *)auth->authorizer,
  632. msg, &sig_check);
  633. if (ret < 0)
  634. return ret;
  635. if (sig_check == msg->footer.sig)
  636. return 0;
  637. if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
  638. dout("ceph_x_check_message_signature %p has signature %llx "
  639. "expect %llx\n", msg, msg->footer.sig, sig_check);
  640. else
  641. dout("ceph_x_check_message_signature %p sender did not set "
  642. "CEPH_MSG_FOOTER_SIGNED\n", msg);
  643. return -EBADMSG;
  644. }
  645. static const struct ceph_auth_client_ops ceph_x_ops = {
  646. .name = "x",
  647. .is_authenticated = ceph_x_is_authenticated,
  648. .should_authenticate = ceph_x_should_authenticate,
  649. .build_request = ceph_x_build_request,
  650. .handle_reply = ceph_x_handle_reply,
  651. .create_authorizer = ceph_x_create_authorizer,
  652. .update_authorizer = ceph_x_update_authorizer,
  653. .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
  654. .invalidate_authorizer = ceph_x_invalidate_authorizer,
  655. .reset = ceph_x_reset,
  656. .destroy = ceph_x_destroy,
  657. .sign_message = ceph_x_sign_message,
  658. .check_message_signature = ceph_x_check_message_signature,
  659. };
  660. int ceph_x_init(struct ceph_auth_client *ac)
  661. {
  662. struct ceph_x_info *xi;
  663. int ret;
  664. dout("ceph_x_init %p\n", ac);
  665. ret = -ENOMEM;
  666. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  667. if (!xi)
  668. goto out;
  669. ret = -EINVAL;
  670. if (!ac->key) {
  671. pr_err("no secret set (for auth_x protocol)\n");
  672. goto out_nomem;
  673. }
  674. ret = ceph_crypto_key_clone(&xi->secret, ac->key);
  675. if (ret < 0) {
  676. pr_err("cannot clone key: %d\n", ret);
  677. goto out_nomem;
  678. }
  679. xi->starting = true;
  680. xi->ticket_handlers = RB_ROOT;
  681. ac->protocol = CEPH_AUTH_CEPHX;
  682. ac->private = xi;
  683. ac->ops = &ceph_x_ops;
  684. return 0;
  685. out_nomem:
  686. kfree(xi);
  687. out:
  688. return ret;
  689. }