auth_x.c 20 KB

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