auth_x.c 20 KB

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