auth_x.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965
  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/ceph_features.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,
  58. int ciphertext_len)
  59. {
  60. struct ceph_x_encrypt_header *hdr = p;
  61. int plaintext_len;
  62. int ret;
  63. ret = ceph_crypt(secret, false, p, ciphertext_len, ciphertext_len,
  64. &plaintext_len);
  65. if (ret)
  66. return ret;
  67. if (le64_to_cpu(hdr->magic) != CEPHX_ENC_MAGIC) {
  68. pr_err("%s bad magic\n", __func__);
  69. return -EINVAL;
  70. }
  71. return plaintext_len - sizeof(*hdr);
  72. }
  73. static int ceph_x_decrypt(struct ceph_crypto_key *secret, void **p, void *end)
  74. {
  75. int ciphertext_len;
  76. int ret;
  77. ceph_decode_32_safe(p, end, ciphertext_len, e_inval);
  78. ceph_decode_need(p, end, ciphertext_len, e_inval);
  79. ret = __ceph_x_decrypt(secret, *p, ciphertext_len);
  80. if (ret < 0)
  81. return ret;
  82. *p += ciphertext_len;
  83. return ret;
  84. e_inval:
  85. return -EINVAL;
  86. }
  87. /*
  88. * get existing (or insert new) ticket handler
  89. */
  90. static struct ceph_x_ticket_handler *
  91. get_ticket_handler(struct ceph_auth_client *ac, int service)
  92. {
  93. struct ceph_x_ticket_handler *th;
  94. struct ceph_x_info *xi = ac->private;
  95. struct rb_node *parent = NULL, **p = &xi->ticket_handlers.rb_node;
  96. while (*p) {
  97. parent = *p;
  98. th = rb_entry(parent, struct ceph_x_ticket_handler, node);
  99. if (service < th->service)
  100. p = &(*p)->rb_left;
  101. else if (service > th->service)
  102. p = &(*p)->rb_right;
  103. else
  104. return th;
  105. }
  106. /* add it */
  107. th = kzalloc(sizeof(*th), GFP_NOFS);
  108. if (!th)
  109. return ERR_PTR(-ENOMEM);
  110. th->service = service;
  111. rb_link_node(&th->node, parent, p);
  112. rb_insert_color(&th->node, &xi->ticket_handlers);
  113. return th;
  114. }
  115. static void remove_ticket_handler(struct ceph_auth_client *ac,
  116. struct ceph_x_ticket_handler *th)
  117. {
  118. struct ceph_x_info *xi = ac->private;
  119. dout("remove_ticket_handler %p %d\n", th, th->service);
  120. rb_erase(&th->node, &xi->ticket_handlers);
  121. ceph_crypto_key_destroy(&th->session_key);
  122. if (th->ticket_blob)
  123. ceph_buffer_put(th->ticket_blob);
  124. kfree(th);
  125. }
  126. static int process_one_ticket(struct ceph_auth_client *ac,
  127. struct ceph_crypto_key *secret,
  128. void **p, void *end)
  129. {
  130. struct ceph_x_info *xi = ac->private;
  131. int type;
  132. u8 tkt_struct_v, blob_struct_v;
  133. struct ceph_x_ticket_handler *th;
  134. void *dp, *dend;
  135. int dlen;
  136. char is_enc;
  137. struct timespec validity;
  138. void *tp, *tpend;
  139. void **ptp;
  140. struct ceph_crypto_key new_session_key;
  141. struct ceph_buffer *new_ticket_blob;
  142. unsigned long new_expires, new_renew_after;
  143. u64 new_secret_id;
  144. int ret;
  145. ceph_decode_need(p, end, sizeof(u32) + 1, bad);
  146. type = ceph_decode_32(p);
  147. dout(" ticket type %d %s\n", type, ceph_entity_type_name(type));
  148. tkt_struct_v = ceph_decode_8(p);
  149. if (tkt_struct_v != 1)
  150. goto bad;
  151. th = get_ticket_handler(ac, type);
  152. if (IS_ERR(th)) {
  153. ret = PTR_ERR(th);
  154. goto out;
  155. }
  156. /* blob for me */
  157. dp = *p + ceph_x_encrypt_offset();
  158. ret = ceph_x_decrypt(secret, p, end);
  159. if (ret < 0)
  160. goto out;
  161. dout(" decrypted %d bytes\n", ret);
  162. dend = dp + ret;
  163. tkt_struct_v = ceph_decode_8(&dp);
  164. if (tkt_struct_v != 1)
  165. goto bad;
  166. ret = ceph_crypto_key_decode(&new_session_key, &dp, dend);
  167. if (ret)
  168. goto out;
  169. ceph_decode_timespec(&validity, dp);
  170. dp += sizeof(struct ceph_timespec);
  171. new_expires = get_seconds() + validity.tv_sec;
  172. new_renew_after = new_expires - (validity.tv_sec / 4);
  173. dout(" expires=%lu renew_after=%lu\n", new_expires,
  174. new_renew_after);
  175. /* ticket blob for service */
  176. ceph_decode_8_safe(p, end, is_enc, bad);
  177. if (is_enc) {
  178. /* encrypted */
  179. tp = *p + ceph_x_encrypt_offset();
  180. ret = ceph_x_decrypt(&th->session_key, p, end);
  181. if (ret < 0)
  182. goto out;
  183. dout(" encrypted ticket, decrypted %d bytes\n", ret);
  184. ptp = &tp;
  185. tpend = tp + ret;
  186. } else {
  187. /* unencrypted */
  188. ptp = p;
  189. tpend = end;
  190. }
  191. ceph_decode_32_safe(ptp, tpend, dlen, bad);
  192. dout(" ticket blob is %d bytes\n", dlen);
  193. ceph_decode_need(ptp, tpend, 1 + sizeof(u64), bad);
  194. blob_struct_v = ceph_decode_8(ptp);
  195. new_secret_id = ceph_decode_64(ptp);
  196. ret = ceph_decode_buffer(&new_ticket_blob, ptp, tpend);
  197. if (ret)
  198. goto out;
  199. /* all is well, update our ticket */
  200. ceph_crypto_key_destroy(&th->session_key);
  201. if (th->ticket_blob)
  202. ceph_buffer_put(th->ticket_blob);
  203. th->session_key = new_session_key;
  204. th->ticket_blob = new_ticket_blob;
  205. th->secret_id = new_secret_id;
  206. th->expires = new_expires;
  207. th->renew_after = new_renew_after;
  208. th->have_key = true;
  209. dout(" got ticket service %d (%s) secret_id %lld len %d\n",
  210. type, ceph_entity_type_name(type), th->secret_id,
  211. (int)th->ticket_blob->vec.iov_len);
  212. xi->have_keys |= th->service;
  213. out:
  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. /*
  242. * Encode and encrypt the second part (ceph_x_authorize_b) of the
  243. * authorizer. The first part (ceph_x_authorize_a) should already be
  244. * encoded.
  245. */
  246. static int encrypt_authorizer(struct ceph_x_authorizer *au,
  247. u64 *server_challenge)
  248. {
  249. struct ceph_x_authorize_a *msg_a;
  250. struct ceph_x_authorize_b *msg_b;
  251. void *p, *end;
  252. int ret;
  253. msg_a = au->buf->vec.iov_base;
  254. WARN_ON(msg_a->ticket_blob.secret_id != cpu_to_le64(au->secret_id));
  255. p = (void *)(msg_a + 1) + le32_to_cpu(msg_a->ticket_blob.blob_len);
  256. end = au->buf->vec.iov_base + au->buf->vec.iov_len;
  257. msg_b = p + ceph_x_encrypt_offset();
  258. msg_b->struct_v = 2;
  259. msg_b->nonce = cpu_to_le64(au->nonce);
  260. if (server_challenge) {
  261. msg_b->have_challenge = 1;
  262. msg_b->server_challenge_plus_one =
  263. cpu_to_le64(*server_challenge + 1);
  264. } else {
  265. msg_b->have_challenge = 0;
  266. msg_b->server_challenge_plus_one = 0;
  267. }
  268. ret = ceph_x_encrypt(&au->session_key, p, end - p, sizeof(*msg_b));
  269. if (ret < 0)
  270. return ret;
  271. p += ret;
  272. if (server_challenge) {
  273. WARN_ON(p != end);
  274. } else {
  275. WARN_ON(p > end);
  276. au->buf->vec.iov_len = p - au->buf->vec.iov_base;
  277. }
  278. return 0;
  279. }
  280. static void ceph_x_authorizer_cleanup(struct ceph_x_authorizer *au)
  281. {
  282. ceph_crypto_key_destroy(&au->session_key);
  283. if (au->buf) {
  284. ceph_buffer_put(au->buf);
  285. au->buf = NULL;
  286. }
  287. }
  288. static int ceph_x_build_authorizer(struct ceph_auth_client *ac,
  289. struct ceph_x_ticket_handler *th,
  290. struct ceph_x_authorizer *au)
  291. {
  292. int maxlen;
  293. struct ceph_x_authorize_a *msg_a;
  294. struct ceph_x_authorize_b *msg_b;
  295. int ret;
  296. int ticket_blob_len =
  297. (th->ticket_blob ? th->ticket_blob->vec.iov_len : 0);
  298. dout("build_authorizer for %s %p\n",
  299. ceph_entity_type_name(th->service), au);
  300. ceph_crypto_key_destroy(&au->session_key);
  301. ret = ceph_crypto_key_clone(&au->session_key, &th->session_key);
  302. if (ret)
  303. goto out_au;
  304. maxlen = sizeof(*msg_a) + ticket_blob_len +
  305. ceph_x_encrypt_buflen(sizeof(*msg_b));
  306. dout(" need len %d\n", maxlen);
  307. if (au->buf && au->buf->alloc_len < maxlen) {
  308. ceph_buffer_put(au->buf);
  309. au->buf = NULL;
  310. }
  311. if (!au->buf) {
  312. au->buf = ceph_buffer_new(maxlen, GFP_NOFS);
  313. if (!au->buf) {
  314. ret = -ENOMEM;
  315. goto out_au;
  316. }
  317. }
  318. au->service = th->service;
  319. au->secret_id = th->secret_id;
  320. msg_a = au->buf->vec.iov_base;
  321. msg_a->struct_v = 1;
  322. msg_a->global_id = cpu_to_le64(ac->global_id);
  323. msg_a->service_id = cpu_to_le32(th->service);
  324. msg_a->ticket_blob.struct_v = 1;
  325. msg_a->ticket_blob.secret_id = cpu_to_le64(th->secret_id);
  326. msg_a->ticket_blob.blob_len = cpu_to_le32(ticket_blob_len);
  327. if (ticket_blob_len) {
  328. memcpy(msg_a->ticket_blob.blob, th->ticket_blob->vec.iov_base,
  329. th->ticket_blob->vec.iov_len);
  330. }
  331. dout(" th %p secret_id %lld %lld\n", th, th->secret_id,
  332. le64_to_cpu(msg_a->ticket_blob.secret_id));
  333. get_random_bytes(&au->nonce, sizeof(au->nonce));
  334. ret = encrypt_authorizer(au, NULL);
  335. if (ret) {
  336. pr_err("failed to encrypt authorizer: %d", ret);
  337. goto out_au;
  338. }
  339. dout(" built authorizer nonce %llx len %d\n", au->nonce,
  340. (int)au->buf->vec.iov_len);
  341. return 0;
  342. out_au:
  343. ceph_x_authorizer_cleanup(au);
  344. return ret;
  345. }
  346. static int ceph_x_encode_ticket(struct ceph_x_ticket_handler *th,
  347. void **p, void *end)
  348. {
  349. ceph_decode_need(p, end, 1 + sizeof(u64), bad);
  350. ceph_encode_8(p, 1);
  351. ceph_encode_64(p, th->secret_id);
  352. if (th->ticket_blob) {
  353. const char *buf = th->ticket_blob->vec.iov_base;
  354. u32 len = th->ticket_blob->vec.iov_len;
  355. ceph_encode_32_safe(p, end, len, bad);
  356. ceph_encode_copy_safe(p, end, buf, len, bad);
  357. } else {
  358. ceph_encode_32_safe(p, end, 0, bad);
  359. }
  360. return 0;
  361. bad:
  362. return -ERANGE;
  363. }
  364. static bool need_key(struct ceph_x_ticket_handler *th)
  365. {
  366. if (!th->have_key)
  367. return true;
  368. return get_seconds() >= th->renew_after;
  369. }
  370. static bool have_key(struct ceph_x_ticket_handler *th)
  371. {
  372. if (th->have_key) {
  373. if (get_seconds() >= th->expires)
  374. th->have_key = false;
  375. }
  376. return th->have_key;
  377. }
  378. static void ceph_x_validate_tickets(struct ceph_auth_client *ac, int *pneed)
  379. {
  380. int want = ac->want_keys;
  381. struct ceph_x_info *xi = ac->private;
  382. int service;
  383. *pneed = ac->want_keys & ~(xi->have_keys);
  384. for (service = 1; service <= want; service <<= 1) {
  385. struct ceph_x_ticket_handler *th;
  386. if (!(ac->want_keys & service))
  387. continue;
  388. if (*pneed & service)
  389. continue;
  390. th = get_ticket_handler(ac, service);
  391. if (IS_ERR(th)) {
  392. *pneed |= service;
  393. continue;
  394. }
  395. if (need_key(th))
  396. *pneed |= service;
  397. if (!have_key(th))
  398. xi->have_keys &= ~service;
  399. }
  400. }
  401. static int ceph_x_build_request(struct ceph_auth_client *ac,
  402. void *buf, void *end)
  403. {
  404. struct ceph_x_info *xi = ac->private;
  405. int need;
  406. struct ceph_x_request_header *head = buf;
  407. int ret;
  408. struct ceph_x_ticket_handler *th =
  409. get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  410. if (IS_ERR(th))
  411. return PTR_ERR(th);
  412. ceph_x_validate_tickets(ac, &need);
  413. dout("build_request want %x have %x need %x\n",
  414. ac->want_keys, xi->have_keys, need);
  415. if (need & CEPH_ENTITY_TYPE_AUTH) {
  416. struct ceph_x_authenticate *auth = (void *)(head + 1);
  417. void *p = auth + 1;
  418. void *enc_buf = xi->auth_authorizer.enc_buf;
  419. struct ceph_x_challenge_blob *blob = enc_buf +
  420. ceph_x_encrypt_offset();
  421. u64 *u;
  422. if (p > end)
  423. return -ERANGE;
  424. dout(" get_auth_session_key\n");
  425. head->op = cpu_to_le16(CEPHX_GET_AUTH_SESSION_KEY);
  426. /* encrypt and hash */
  427. get_random_bytes(&auth->client_challenge, sizeof(u64));
  428. blob->client_challenge = auth->client_challenge;
  429. blob->server_challenge = cpu_to_le64(xi->server_challenge);
  430. ret = ceph_x_encrypt(&xi->secret, enc_buf, CEPHX_AU_ENC_BUF_LEN,
  431. sizeof(*blob));
  432. if (ret < 0)
  433. return ret;
  434. auth->struct_v = 1;
  435. auth->key = 0;
  436. for (u = (u64 *)enc_buf; u + 1 <= (u64 *)(enc_buf + ret); u++)
  437. auth->key ^= *(__le64 *)u;
  438. dout(" server_challenge %llx client_challenge %llx key %llx\n",
  439. xi->server_challenge, le64_to_cpu(auth->client_challenge),
  440. le64_to_cpu(auth->key));
  441. /* now encode the old ticket if exists */
  442. ret = ceph_x_encode_ticket(th, &p, end);
  443. if (ret < 0)
  444. return ret;
  445. return p - buf;
  446. }
  447. if (need) {
  448. void *p = head + 1;
  449. struct ceph_x_service_ticket_request *req;
  450. if (p > end)
  451. return -ERANGE;
  452. head->op = cpu_to_le16(CEPHX_GET_PRINCIPAL_SESSION_KEY);
  453. ret = ceph_x_build_authorizer(ac, th, &xi->auth_authorizer);
  454. if (ret)
  455. return ret;
  456. ceph_encode_copy(&p, xi->auth_authorizer.buf->vec.iov_base,
  457. xi->auth_authorizer.buf->vec.iov_len);
  458. req = p;
  459. req->keys = cpu_to_le32(need);
  460. p += sizeof(*req);
  461. return p - buf;
  462. }
  463. return 0;
  464. }
  465. static int ceph_x_handle_reply(struct ceph_auth_client *ac, int result,
  466. void *buf, void *end)
  467. {
  468. struct ceph_x_info *xi = ac->private;
  469. struct ceph_x_reply_header *head = buf;
  470. struct ceph_x_ticket_handler *th;
  471. int len = end - buf;
  472. int op;
  473. int ret;
  474. if (result)
  475. return result; /* XXX hmm? */
  476. if (xi->starting) {
  477. /* it's a hello */
  478. struct ceph_x_server_challenge *sc = buf;
  479. if (len != sizeof(*sc))
  480. return -EINVAL;
  481. xi->server_challenge = le64_to_cpu(sc->server_challenge);
  482. dout("handle_reply got server challenge %llx\n",
  483. xi->server_challenge);
  484. xi->starting = false;
  485. xi->have_keys &= ~CEPH_ENTITY_TYPE_AUTH;
  486. return -EAGAIN;
  487. }
  488. op = le16_to_cpu(head->op);
  489. result = le32_to_cpu(head->result);
  490. dout("handle_reply op %d result %d\n", op, result);
  491. switch (op) {
  492. case CEPHX_GET_AUTH_SESSION_KEY:
  493. /* verify auth key */
  494. ret = ceph_x_proc_ticket_reply(ac, &xi->secret,
  495. buf + sizeof(*head), end);
  496. break;
  497. case CEPHX_GET_PRINCIPAL_SESSION_KEY:
  498. th = get_ticket_handler(ac, CEPH_ENTITY_TYPE_AUTH);
  499. if (IS_ERR(th))
  500. return PTR_ERR(th);
  501. ret = ceph_x_proc_ticket_reply(ac, &th->session_key,
  502. buf + sizeof(*head), end);
  503. break;
  504. default:
  505. return -EINVAL;
  506. }
  507. if (ret)
  508. return ret;
  509. if (ac->want_keys == xi->have_keys)
  510. return 0;
  511. return -EAGAIN;
  512. }
  513. static void ceph_x_destroy_authorizer(struct ceph_authorizer *a)
  514. {
  515. struct ceph_x_authorizer *au = (void *)a;
  516. ceph_x_authorizer_cleanup(au);
  517. kfree(au);
  518. }
  519. static int ceph_x_create_authorizer(
  520. struct ceph_auth_client *ac, int peer_type,
  521. struct ceph_auth_handshake *auth)
  522. {
  523. struct ceph_x_authorizer *au;
  524. struct ceph_x_ticket_handler *th;
  525. int ret;
  526. th = get_ticket_handler(ac, peer_type);
  527. if (IS_ERR(th))
  528. return PTR_ERR(th);
  529. au = kzalloc(sizeof(*au), GFP_NOFS);
  530. if (!au)
  531. return -ENOMEM;
  532. au->base.destroy = ceph_x_destroy_authorizer;
  533. ret = ceph_x_build_authorizer(ac, th, au);
  534. if (ret) {
  535. kfree(au);
  536. return ret;
  537. }
  538. auth->authorizer = (struct ceph_authorizer *) au;
  539. auth->authorizer_buf = au->buf->vec.iov_base;
  540. auth->authorizer_buf_len = au->buf->vec.iov_len;
  541. auth->authorizer_reply_buf = au->enc_buf;
  542. auth->authorizer_reply_buf_len = CEPHX_AU_ENC_BUF_LEN;
  543. auth->sign_message = ac->ops->sign_message;
  544. auth->check_message_signature = ac->ops->check_message_signature;
  545. return 0;
  546. }
  547. static int ceph_x_update_authorizer(
  548. struct ceph_auth_client *ac, int peer_type,
  549. struct ceph_auth_handshake *auth)
  550. {
  551. struct ceph_x_authorizer *au;
  552. struct ceph_x_ticket_handler *th;
  553. th = get_ticket_handler(ac, peer_type);
  554. if (IS_ERR(th))
  555. return PTR_ERR(th);
  556. au = (struct ceph_x_authorizer *)auth->authorizer;
  557. if (au->secret_id < th->secret_id) {
  558. dout("ceph_x_update_authorizer service %u secret %llu < %llu\n",
  559. au->service, au->secret_id, th->secret_id);
  560. return ceph_x_build_authorizer(ac, th, au);
  561. }
  562. return 0;
  563. }
  564. static int decrypt_authorize_challenge(struct ceph_x_authorizer *au,
  565. void *challenge_buf,
  566. int challenge_buf_len,
  567. u64 *server_challenge)
  568. {
  569. struct ceph_x_authorize_challenge *ch =
  570. challenge_buf + sizeof(struct ceph_x_encrypt_header);
  571. int ret;
  572. /* no leading len */
  573. ret = __ceph_x_decrypt(&au->session_key, challenge_buf,
  574. challenge_buf_len);
  575. if (ret < 0)
  576. return ret;
  577. if (ret < sizeof(*ch)) {
  578. pr_err("bad size %d for ceph_x_authorize_challenge\n", ret);
  579. return -EINVAL;
  580. }
  581. *server_challenge = le64_to_cpu(ch->server_challenge);
  582. return 0;
  583. }
  584. static int ceph_x_add_authorizer_challenge(struct ceph_auth_client *ac,
  585. struct ceph_authorizer *a,
  586. void *challenge_buf,
  587. int challenge_buf_len)
  588. {
  589. struct ceph_x_authorizer *au = (void *)a;
  590. u64 server_challenge;
  591. int ret;
  592. ret = decrypt_authorize_challenge(au, challenge_buf, challenge_buf_len,
  593. &server_challenge);
  594. if (ret) {
  595. pr_err("failed to decrypt authorize challenge: %d", ret);
  596. return ret;
  597. }
  598. ret = encrypt_authorizer(au, &server_challenge);
  599. if (ret) {
  600. pr_err("failed to encrypt authorizer w/ challenge: %d", ret);
  601. return ret;
  602. }
  603. return 0;
  604. }
  605. static int ceph_x_verify_authorizer_reply(struct ceph_auth_client *ac,
  606. struct ceph_authorizer *a)
  607. {
  608. struct ceph_x_authorizer *au = (void *)a;
  609. void *p = au->enc_buf;
  610. struct ceph_x_authorize_reply *reply = p + ceph_x_encrypt_offset();
  611. int ret;
  612. ret = ceph_x_decrypt(&au->session_key, &p, p + CEPHX_AU_ENC_BUF_LEN);
  613. if (ret < 0)
  614. return ret;
  615. if (ret < sizeof(*reply)) {
  616. pr_err("bad size %d for ceph_x_authorize_reply\n", ret);
  617. return -EINVAL;
  618. }
  619. if (au->nonce + 1 != le64_to_cpu(reply->nonce_plus_one))
  620. ret = -EPERM;
  621. else
  622. ret = 0;
  623. dout("verify_authorizer_reply nonce %llx got %llx ret %d\n",
  624. au->nonce, le64_to_cpu(reply->nonce_plus_one), ret);
  625. return ret;
  626. }
  627. static void ceph_x_reset(struct ceph_auth_client *ac)
  628. {
  629. struct ceph_x_info *xi = ac->private;
  630. dout("reset\n");
  631. xi->starting = true;
  632. xi->server_challenge = 0;
  633. }
  634. static void ceph_x_destroy(struct ceph_auth_client *ac)
  635. {
  636. struct ceph_x_info *xi = ac->private;
  637. struct rb_node *p;
  638. dout("ceph_x_destroy %p\n", ac);
  639. ceph_crypto_key_destroy(&xi->secret);
  640. while ((p = rb_first(&xi->ticket_handlers)) != NULL) {
  641. struct ceph_x_ticket_handler *th =
  642. rb_entry(p, struct ceph_x_ticket_handler, node);
  643. remove_ticket_handler(ac, th);
  644. }
  645. ceph_x_authorizer_cleanup(&xi->auth_authorizer);
  646. kfree(ac->private);
  647. ac->private = NULL;
  648. }
  649. static void invalidate_ticket(struct ceph_auth_client *ac, int peer_type)
  650. {
  651. struct ceph_x_ticket_handler *th;
  652. th = get_ticket_handler(ac, peer_type);
  653. if (!IS_ERR(th))
  654. th->have_key = false;
  655. }
  656. static void ceph_x_invalidate_authorizer(struct ceph_auth_client *ac,
  657. int peer_type)
  658. {
  659. /*
  660. * We are to invalidate a service ticket in the hopes of
  661. * getting a new, hopefully more valid, one. But, we won't get
  662. * it unless our AUTH ticket is good, so invalidate AUTH ticket
  663. * as well, just in case.
  664. */
  665. invalidate_ticket(ac, peer_type);
  666. invalidate_ticket(ac, CEPH_ENTITY_TYPE_AUTH);
  667. }
  668. static int calc_signature(struct ceph_x_authorizer *au, struct ceph_msg *msg,
  669. __le64 *psig)
  670. {
  671. void *enc_buf = au->enc_buf;
  672. int ret;
  673. if (!(msg->con->peer_features & CEPH_FEATURE_CEPHX_V2)) {
  674. struct {
  675. __le32 len;
  676. __le32 header_crc;
  677. __le32 front_crc;
  678. __le32 middle_crc;
  679. __le32 data_crc;
  680. } __packed *sigblock = enc_buf + ceph_x_encrypt_offset();
  681. sigblock->len = cpu_to_le32(4*sizeof(u32));
  682. sigblock->header_crc = msg->hdr.crc;
  683. sigblock->front_crc = msg->footer.front_crc;
  684. sigblock->middle_crc = msg->footer.middle_crc;
  685. sigblock->data_crc = msg->footer.data_crc;
  686. ret = ceph_x_encrypt(&au->session_key, enc_buf,
  687. CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock));
  688. if (ret < 0)
  689. return ret;
  690. *psig = *(__le64 *)(enc_buf + sizeof(u32));
  691. } else {
  692. struct {
  693. __le32 header_crc;
  694. __le32 front_crc;
  695. __le32 front_len;
  696. __le32 middle_crc;
  697. __le32 middle_len;
  698. __le32 data_crc;
  699. __le32 data_len;
  700. __le32 seq_lower_word;
  701. } __packed *sigblock = enc_buf;
  702. struct {
  703. __le64 a, b, c, d;
  704. } __packed *penc = enc_buf;
  705. int ciphertext_len;
  706. sigblock->header_crc = msg->hdr.crc;
  707. sigblock->front_crc = msg->footer.front_crc;
  708. sigblock->front_len = msg->hdr.front_len;
  709. sigblock->middle_crc = msg->footer.middle_crc;
  710. sigblock->middle_len = msg->hdr.middle_len;
  711. sigblock->data_crc = msg->footer.data_crc;
  712. sigblock->data_len = msg->hdr.data_len;
  713. sigblock->seq_lower_word = *(__le32 *)&msg->hdr.seq;
  714. /* no leading len, no ceph_x_encrypt_header */
  715. ret = ceph_crypt(&au->session_key, true, enc_buf,
  716. CEPHX_AU_ENC_BUF_LEN, sizeof(*sigblock),
  717. &ciphertext_len);
  718. if (ret)
  719. return ret;
  720. *psig = penc->a ^ penc->b ^ penc->c ^ penc->d;
  721. }
  722. return 0;
  723. }
  724. static int ceph_x_sign_message(struct ceph_auth_handshake *auth,
  725. struct ceph_msg *msg)
  726. {
  727. __le64 sig;
  728. int ret;
  729. if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
  730. return 0;
  731. ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
  732. msg, &sig);
  733. if (ret)
  734. return ret;
  735. msg->footer.sig = sig;
  736. msg->footer.flags |= CEPH_MSG_FOOTER_SIGNED;
  737. return 0;
  738. }
  739. static int ceph_x_check_message_signature(struct ceph_auth_handshake *auth,
  740. struct ceph_msg *msg)
  741. {
  742. __le64 sig_check;
  743. int ret;
  744. if (ceph_test_opt(from_msgr(msg->con->msgr), NOMSGSIGN))
  745. return 0;
  746. ret = calc_signature((struct ceph_x_authorizer *)auth->authorizer,
  747. msg, &sig_check);
  748. if (ret)
  749. return ret;
  750. if (sig_check == msg->footer.sig)
  751. return 0;
  752. if (msg->footer.flags & CEPH_MSG_FOOTER_SIGNED)
  753. dout("ceph_x_check_message_signature %p has signature %llx "
  754. "expect %llx\n", msg, msg->footer.sig, sig_check);
  755. else
  756. dout("ceph_x_check_message_signature %p sender did not set "
  757. "CEPH_MSG_FOOTER_SIGNED\n", msg);
  758. return -EBADMSG;
  759. }
  760. static const struct ceph_auth_client_ops ceph_x_ops = {
  761. .name = "x",
  762. .is_authenticated = ceph_x_is_authenticated,
  763. .should_authenticate = ceph_x_should_authenticate,
  764. .build_request = ceph_x_build_request,
  765. .handle_reply = ceph_x_handle_reply,
  766. .create_authorizer = ceph_x_create_authorizer,
  767. .update_authorizer = ceph_x_update_authorizer,
  768. .add_authorizer_challenge = ceph_x_add_authorizer_challenge,
  769. .verify_authorizer_reply = ceph_x_verify_authorizer_reply,
  770. .invalidate_authorizer = ceph_x_invalidate_authorizer,
  771. .reset = ceph_x_reset,
  772. .destroy = ceph_x_destroy,
  773. .sign_message = ceph_x_sign_message,
  774. .check_message_signature = ceph_x_check_message_signature,
  775. };
  776. int ceph_x_init(struct ceph_auth_client *ac)
  777. {
  778. struct ceph_x_info *xi;
  779. int ret;
  780. dout("ceph_x_init %p\n", ac);
  781. ret = -ENOMEM;
  782. xi = kzalloc(sizeof(*xi), GFP_NOFS);
  783. if (!xi)
  784. goto out;
  785. ret = -EINVAL;
  786. if (!ac->key) {
  787. pr_err("no secret set (for auth_x protocol)\n");
  788. goto out_nomem;
  789. }
  790. ret = ceph_crypto_key_clone(&xi->secret, ac->key);
  791. if (ret < 0) {
  792. pr_err("cannot clone key: %d\n", ret);
  793. goto out_nomem;
  794. }
  795. xi->starting = true;
  796. xi->ticket_handlers = RB_ROOT;
  797. ac->protocol = CEPH_AUTH_CEPHX;
  798. ac->private = xi;
  799. ac->ops = &ceph_x_ops;
  800. return 0;
  801. out_nomem:
  802. kfree(xi);
  803. out:
  804. return ret;
  805. }