auth_x.c 20 KB

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